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.
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;
}
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