Reading an array of histogram from a root file

I have an array of histogram in a root file like this:

   sprintf(cha[1],"%dE",i);
   sprintf(cha[2]," vE%d",i);
   h[i]=new TH2D(cha[1],cha[2],70,0,140,70,0,140); 

now I want to read these histograms and draw them i use this program

void main(){
  TH2D  *h[22];
  TCanvas *c1 = new TCanvas("c1","calib");
  TPostScript  *ps = new TPostScript("psfile",111); ps->Range(18,27);
  TFile f("SS.root");
  for (int i=1;i<21;i++){
    sprintf(cha[1],"%dE",i);
    sprintf(cha[2]," vE%d",i);
    h[i]=new TH2D(cha[1],cha[2],70,0,140,70,0,140);
    h[i]=(TH2D*)f.Get(cha[1]);

    ps->NewPage();
    c1->cd();
    h[i]->Draw("colz");
    c1->Update();
  }
}

I don’t have any error but my histograms are empty.please help me

Hi,

Empty histograms? I’m surprised anything is displayed at all! You need a TApplication object and call its event loop using its Run() method.

Axel.

When you inspect “SS.root” within an interactive ROOT session, are the histograms filled ?

Hi
originally i want to load or read an array of histogram from a root file(ss.root) and draw them.

Can you attach SS.root?

Did my comment help?

Axel

I think my file(ss.root) don’t load I only use “TFile f(ss.root)” to use my file and for reading histogram i use f->Get(“histogram name”) I think it is no enough. my file(ss.root) is containing of histogram not ntuple.
I don’t know about TApplication and Run(), please explain me about them
thank you

I have a root file and in this file i have several loop of histogram(for example h1[i]),h2[i],…) i defined h1[i ] like this:

   sprintf(cha[1],"%dE",i);
   sprintf(cha[2]," vE%d",i);
   h1[i]=new TH2D(cha[1],cha[2],70,0,140,70,0,140);

now i want to read this array of histogram i use below ways to read but i can’t read my histograms

h1[i]= (TH2D*)gDirectory->FindObject(cha[1]);
h1[i] = *((TH2D *) f->Get(cha[1])); 
h1[i]->SetDirectory(0);

Can you attach SS.root?

Axel.

ss.root (1.2 MB)

Your file contains many different kind of objects. Some of them are THD2.

Some kind of “naming conventions” appear for some of them.

The following macro reads one of the “collection of histograms” named Det1dE, Det2dE, …, Det9dE.

{
   auto ss = new TFile("ss.root");
   TH2D *h;
   for (int i=1; i<=9; i++) {
     h = (TH2D*)ss->Get(TString::Format("Det%dE",i));
     if (h) printf("Histogram named \"%s\" has the title \"%s\" and %g entries\n",
     h->GetName(),h->GetTitle(), h->GetEntries());
   }
}

When I run this macro I get the following result:

root [0] .x ss.C
Histogram named "Det1E" has the title "SecondCalib  E1 vs E10" and 0 entries
Histogram named "Det2E" has the title "SecondCalib  E2 vs E9" and 0 entries
Histogram named "Det3E" has the title "SecondCalib  E3 vs E8" and 0 entries
Histogram named "Det4E" has the title "SecondCalib  E4 vs E7" and 0 entries
Histogram named "Det5E" has the title "SecondCalib  E5 vs E6" and 0 entries
Histogram named "Det6E" has the title "SecondCalib  E6 vs E5" and 0 entries
Histogram named "Det7E" has the title "SecondCalib  E7 vs E4" and 0 entries
Histogram named "Det8E" has the title "SecondCalib  E8 vs E3" and 0 entries
Histogram named "Det9E" has the title "SecondCalib  E9 vs E2" and 0 entries
root [1] 

I hope it helps…

Thanks a lot
I have a question instead of h how can i have an array of histogram whit h[i]?

Thank you sooooo much
I can write it as an array of histogram

for (int i=1; i<=9; i++) {
    h[i] = (TH2D*)ss->Get(TString::Format("Det%dE",i));
     if (h[i]) printf("Histogram name %s with title %s has %g entries\n",
    h[i]->GetName(),h[i]->GetTitle(), h[i]->GetEntries());
ps->NewPage();
c->cd();
h[i]->Draw("colz");
c->Update();
}

:slight_smile:

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