Accessing histos in a list in a folder

I have a root file with histos within a list in a folder. How I access the histos?
code:
TString AnaFile = “AnalysisResults_BasicCuts.root”;
TFile* HistosFile = new TFile(AnaFile.Data(), “READ”);
HistosFile->cd(“MyTask”);
TH1F* hmass0= (TH1F*) HistosFile->Get(“MyOutputContainer::fHistJPsiInvMass”);
hmass0->Draw();
/endcode

error message:
Error: illegal pointer to class object hmass0 0x0 204 AnalysePlots.C:16:

I know that syntax “MyOutputContainer::fHistJPsiInvMass” (TList::HistName) is wrong. What is right?


ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


… see appearance in TBrouser:
RootFile

Try:

gDirectory->Get("MyOutputContainer/fHistJPsiInvMass");

and/or:

HistosFile->Get("/MyTask/MyOutputContainer/fHistJPsiInvMass");

none of the suggestions worked!!

  1. HistosFile->cd(“MyTask”);
    TH1F* hmass0= (TH1F*) gDirectory->Get(“MyOutputContainer/fHistJPsiInvMass”);

  2. TH1F* hmass0= (TH1F*) HistosFile->Get("/MyTask/MyOutputContainer/fHistJPsiInvMass");

Attach your root file here.

AnalysisResults_BasicCuts.root (6.4 KB)

It’s not a “TDirectoryFile”:

 KEY: TList     MyOutputContainer;1     Doubly linked list

… ok. which means? any idea how to access the histos?

Try:

{
  TFile *f = TFile::Open("AnalysisResults_BasicCuts.root");
  if ((!f) || f->IsZombie()) { delete f; return; } // just a precaution
  TList *l; f->GetObject("MyTask/MyOutputContainer", l);
  if (!l) { delete f; return; } // just a precaution
  // l->Print();
  // l->ls();
  TH1F *h = dynamic_cast<TH1F*>(l->FindObject("fHistJPsiInvMass"));
  if (h) h->Print();
}

You are missing a cast here. FindObject returns a TObject*.

auto histo = dynamic_cast<TH1*>(l->FindObject("fHistJPsiInvMass"));
if (!histo) cerr << "fHistJPsiInvMass does not exist or is not a histogram\n";

thanks. both option work.

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