Plotting Multiple Histograms with Same Title

Hello,

I am trying to plot multiple (~20) histograms with the same name, from the same .root file, onto one plot.

In my case, I have .root file, with ~20 histograms, each of the same name (myhist;1 myhist;2 myhist;3…)
and all the same title (myhist).

Here is [some parts pseudo-]code of what I am doing:

TFile *f=new TFile("myfile.root");
TCanvas *c1=new TCanvas("c1","c1",800,800);

bool setupComplete(false);
  
for (int i(1);i<=20;++i){

    TH1F* h;
    h = (TH1F*) f->Get("myhist;i"); //pseudocode, I call a little function which returns the char* properly e.g. "myhist;1"

    if (!h){
      std::cout<<"Error: histogram not found"<<std::endl;
      continue;
    }

    if (!setupComplete) { h->Draw(); setupComplete=true; }
    else h->Draw("SAME");
  }

So I want to draw all of these histograms on the same plot, adding one at a time. All the histograms have the same # of entries, same binning, same boundaries. What I get back I have attached as canvas.png, obviously not what I was going for.

I have tried similar loops where the histograms all have different names (myhist1;1 myhist2;1 myhist3;1) and that seems to work fine, so I think the problem is with the shared name.

I can also print out info from the histogram, statistical quantities like the mean, etc., so it appears to be reading the histograms from the file perfectly fine.

If I print out the value of h (the address in memory), it is always the same for all the histograms. I am not sure how to interpret this…

Any help would be greatly appreciated. Thank you.


The problem is NOT the same “title” but it’s the same “name” of all histograms (the number after the “;” is the so called “cycle”).
Try with “f->FindObjectAny” instead of “f->Get”.
See [url]Problem with Reading Root File and [url]Problem with Reading Root File and [url]Aux;1 aux;2 aux3;
P.S. Of course, the preferred solution is to create / store histograms with distinct “names”, and then use “GetObject” to “retrieve” them as described in http://root.cern.ch/root/html/TDirectoryFile.html#TDirectoryFile:Get
BTW. The “pseudocode” can be as simple as … TString::Format(“myhist;%d”, i)

Thanks, those solutions work perfectly.

(and I made sure I used ‘name’ all through the post, but forgot to fix the ‘title’ in the subject)