Home | News | Documentation | Download

Looping over root file to create histograms on same plot

So I have a root file: output.root, which has a subfolder ‘FirstCut’, containing 2 histograms, differing in a parameter which is 30 for the first histogram and 50 for the second. Now I want to loop over this parameter, and draw both histograms on the same plot. The path of these histograms is FirstCut_30 and FirstCut_50. This is my attempt:

ROOT Version: 6.18/04
Linux

void Function(){
Tfile *f = new TFile ("output.root");
std::vector<double> FirstCut = {30, 50};
for (unsigned i = 0; i < FirstCut.size(); ++i){
  TH1D *h[i] = (TH1D*)f->Get("FirstCut_[i]");
  h[i]->Draw(same); 
  }
}

I get 2 errors:

  • variable sized object may not be initialised (where it points at the h in h[i])
  • use of undeclared identifier ‘same’ (in Draw(same))

Many mistakes in your code…
I do not have your ROOT file, so here is my guess:

void Function(){
   auto f = new TFile ("output.root");
   f->cd("FirstCut"); // you said your histograms are in the subfolder "FisrtCut"
   std::vector<int> FirstCut = {30, 50};
   int n = FirstCut.size();
   TH1D *h[n];
   for (unsigned i = 0; i < n; i++) {
     h[i] = (TH1D*)f->Get(Form("FirstCut_%d",FirstCut[i]));
     if (i==0) h[i]->Draw(); 
     else h[i]->Draw("same")
  }
}
1 Like

Thanks! It worked! Do you know how I can give the different histograms different colours?

Yes you can:

h->SetLineColor(...);
1 Like

I am now trying to divide a histogram (let’s call it k1) by these histograms and plot these ‘ratios’ on the same graph. I tried this:

void Function(){
  auto f = new TFile ("output.root");
  TH1D *k1 = (TH1D*)f->Get("SecondCut");
  std::vector<int> FirstCut = {30, 50};
  int n = FirstCut.size();
  TH1D *h[n];
  TH1D *ratio[n]; 
  for (unsigned i = 0; i < n; i++) {
    h[i] = (TH1D*)f->Get(Form ("FirstCut_%d", FirstCut[i]));
    TH1D *ratio[i] = new TH1D(*k1);
    ratio[i]->Divide(h[i]);
    ratio[i]->SetNameTitle("hratio", "k1/h_%d"); 
    if (i==0) ratio[i]->Draw();
    else ratio[i]->Draw("same")
  }
}

But I get the error that variable-sized object may not be initialised: TH1D *ratio[i] = new TH1D(*k1);

Do you perhaps know what I am doing wrong?

should be:

ratio[i] = new TH1D(*k1);

It works again! Thanks a lot!

1 Like

I am back again… I was just wondering, I am dividing these histograms to obtain an efficiency and so I want the y values to be between 0 and 1. However, the errors exceed these limitations, and I think this might be the case because my k1 histogram is a subset of my h histograms. I read something about using TEfficiency class rather than the Divide function, but I am not sure on how to use that in this case.

I think @moneta can help you with this.

1 Like

See the TEfficiency documentation
https://root.cern.ch/doc/master/classTEfficiency.html

It is very detailed and there you have examples on how to use it .
If you have specific questions, please let us know.

Lorenzo

1 Like