Trouble calculating the ratio of two histograms

Hi all,
I’m trying to use root to calculate the ratio between two histograms (trying to find the acceptance of my experiment). I’ve been following the tutorial shown here, but when I try to apply the code to my histograms, it doesn’t work. As far as I can tell, the problem is with Clone() and/or Divide().
Here’s the code I wrote. I’m creating the canvas as a diagnostic more than anything else. The numerator and denominator histogram will display fine, but the ratio histogram won’t, both before and after I take the ratio. If anyone sees something I’m doing wrong, that would be so helpful.

void testratio() {
  //Open ROOT files
  TFile *fnum = TFile::Open("hist_pomegapi_30496.acc.root");
  TFile *fden = TFile::Open("hist_thrown.root");
  
  //Read the 3 Pion Mass histograms
  TH1I *locHist_3PiMass_num = (TH1I*)fnum->Get("3PiMass_Measured");
  TH1I *locHist_3PiMass_den = (TH1I*)fden->Get("3PiMass_thrown");

  //Define a canvas
  TCanvas *cc = new TCanvas("cc", "cc", 600, 600);
  cc->Divide(2,2);

  //Draw the 3 Pion Mass histograms
  cc->cd(1);
  locHist_3PiMass_num->Draw();
  cc->cd(2);
  locHist_3PiMass_den->Draw();

  //Take their ratio and draw it
  TH1I *locHist_ratio = (TH1I*)locHist_3PiMass_num->Clone();
  cc->cd(3);
  locHist_ratio->Draw();
  locHist_ratio->Divide(locHist_3PiMass_den);
  cc->cd(4);
  locHist_ratio->Draw();
}

Try to “subtract” your histograms (“division” works fine for TH1D and TH1F but not really for TH1I):

cc->cd(3);
TH1I *locHist_ratio = new TH1I(*locHist_3PiMass_num);
locHist_ratio->SetNameTitle("locHist_ratio", "measured - thrown");
locHist_ratio->Add(locHist_3PiMass_den, -1.0);
locHist_ratio->Draw();

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