How to read a histogram from TList in .root file

Hello experts, I’m having a trouble in getting the data from a TList which is stored in a .root file. it has 2 histograms i need to write a macros that reads it and make a histogram. please tell me how can i do that ? i know how to read from TH1F, but this i couldn’t please help me .
Regards,
Sana Shahid.

Hi @Sana,
Does this help?
Best,
Vincenzo

1 Like

Hi vpadulan, no i doesn’t work i tried it after the last line it gives the *** Break *** segmentation violation error and a huge error list.

Hi Sana,
what Vincenzo linked is indeed the standard way to read a TList from a ROOT file, and access its contents.

A segmentation violation might indicate that one of the pointers you use is invalid. This can happen, for example, if the file does not return the TList (in which case TFile::Get will return a null pointer, which you can check for).

Anyway in general, if you have a file “file.root” with a TList as key “list”, you extract the list with

TFile* f = TFile::Open("file.root");
TList* list = (TList*)f->Get("list");

or, in v6.18:

TFile* f = TFile::Open("sample.root");
TList* list = f->Get<TList>("list");

You can check whether the list was correctly retrieved with:

if (list == nullptr) {
  std::cout << "something went wrong! abort!" << std::endl;
  return 1;
}

If list is valid, you can loop over its contents to make sure they are what you expect:

for(auto obj: *list)
   std::cout << "obj in list: " << obj->GetName() << std::endl;

Hope this helps!
Cheers,
Enrico

Thank you so much everyone, it works now .
Regards,
Sana Shahid.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.