ROOT file. Access to histograms


Hi guys,

I have a problem with a macro I wrote to access histograms stored in a ROOT file.
The file contains histograms named “Fiber_0”, “Fiber_1” and “Fiber_2”.

Now, with the macro I wrote (see the screenshot) I’m able to retrieve only one of them using the code lines I commented. But, when I try to loop over them I have problems. Moreover, I don’t know how to index histogram names.

Could anyone help me?

Cheers.

TH1D *hist; file->GetObject(TString::Format("Fiber_%d", i), hist);
if (!hist) continue; // just a precaution

Hi,
I wrote this
for(i=0;i<NofLayers;i++){
TH1D *hist;
file->GetObject(TString::Format(“Fiber_%d”, i), hist);
hist->Draw();
//hist->SetDirectory(gROOT);
}//for end

but I got error: cannot initialize an array element of type ‘void ’ with an rvalue of type 'TString ()(const char *, …)’

{
  TFile *file = TFile::Open("/path/to/file.root");
  if (!file || file->IsZombie()) { delete file; file = 0; } // just a precaution
  if (file) file->ls();
  int NofLayers = 3;
  for(int i = 0; i < NofLayers; i++) {
    TH1D *hist = 0;
    if (file) file->GetObject(TString::Format("Fiber_%d", i), hist);
    if (!hist) continue; // just a precaution
    hist->SetDirectory(gROOT);
    new TCanvas(); hist->Draw(); // each histogram in its own canvas
  }
  delete file; file = 0;
}

I get segmentation violation :confused:

Attach your complete macro.

{
//Open the ROOT file
TFile *file = TFile::Open(“path_to_file”);

//See what the ROOT file contains
file->ls();

//total number of fibers/histograms
int NofFibers = 3;

//Retrieve histograms from the tree

/*
TH1D* h = (TH1D*)file->Get(“Fiber_2”);
h->Draw();
h->SetDirectory(gROOT);
*/

for(int i=0; i<NofLayers ;i++){
TH1D *hist;
file->GetObject(TString::Format(“Fiber_%d”, i), hist);
//TH1D hist = (TH1D)file->GetObject(TString::Format(“Fiber_%d”,i),hist));
hist->Draw();
hist->SetDirectory(gROOT);
}//for end

for(int i = 0; i < NofLayers; i++) {
TH1D *hist;
file->GetObject(TString::Format(“Fiber_%d”, i), hist);
if (!hist) continue; // just a precaution
hist->SetDirectory(gROOT);
new TCanvas();
hist->Draw(); // each histogram in its own canvas
}
delete file; // close ROOT file

//close ROOT file
//file->Close();

} //macro end

Try the complete macro from my previous post.

Yes!!

Now it works!

Thanks!

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