Ratio plot of two histograms

Hey All,
I am trying to plot the ratio of two normalized histograms using the following piece of code:

#include "TFile.h"
#include "TTree.h"

void histogram_qinv()
{
        //gStyle->SetOptStat(0);
        TFile *f1 = TFile::Open("toymodel_fluctuation_mixed.root");
        TH1D *h1A = (TH1D*)gDirectory->Get("qinv_mixed");
        TH1D *h2A = (TH1D*)gDirectory->Get("Del_pT_mixed");
       
        TFile *f2 = TFile::Open("toymodel_fluctuation.root");
        TH1D *h1B = (TH1D*)gDirectory->Get("qinv_data");
        TH1D *h2B = (TH1D*)gDirectory->Get("Del_pT_data");
       
        TH1F *ratio_qinv = (TH1F*)h1B->Clone("ratio_qinv");
        TH1F *ratio_Del_pT = (TH1F*)h2B->Clone("ratio_Del_pT");
       
        ratio_qinv->Divide(h1A);
        ratio_Del_pT->Divide(h2A);
               
        
        TFile *f = new TFile("toymodel_qinv.root","RECREATE"); //create a root file 
        h1A->Write();
        h1B->Write();
        h2A->Write();
        h2B->Write();
        ratio_qinv->Write();
        ratio_qinv->Draw("HIST");
        ratio_Del_pT->Write();
        f->Write();  //write histogram in root file
        f->Close();
}

It creates a ratio plot but the

number of entries in each plot is not the same as well as there is something wrong when there are few points which zero for each plot and that gives an undefined point in ratio plot. Suggest me if you find something wrong or some smart way to draw ratio plot of two histograms.


Please read tips for efficient and successful posting and posting code

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided


Hi Haradhan,

The number of entries should equal number of bins in either one of the original histograms. What happens is that ROOT loops over the bins of the original histogram, divides bin contents and then assigns new bin contents to the bins. It happens once per bin. However, it does not look like your ratio plot has 1010 bins - do you show only a part of it maybe? Or did you rebin outside of your piece of code?

Like where exactly? What you describe should yield 0 on the ratio plot, and this is exactly what I see in your picture. What undefined point are you referring to?

Hey Yus,
Thank you once again.So you mean to say, number of entries in the ratio plot may not be the same as the individual plot? But I wanted to calculate each points of my individual histograms and I expected entries should same in the ratio plot also.

That’s right. Not sure what you mean by

, but if you just want the ratio histogram to have the same number of entries as in, e.g., the numerator histogram, you can just do

ratio_qinv->SetEntries(h1B->GetEntries());

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