Divide two TH2F histograms in different ranges

Dear Helpers,

I am trying to plot a TH2F histogram which present the devision of the values from two other TH2F histogram with different ranges, please, could someone help ? I am using this part of the code but it doesn’t work.

// Style setting

TH2F *histAll = new TH2F(“histAll”,“hist All”,100,0.,456.,100,0,1); histAll->GetXaxis()->SetNdivisions(505);

TFile *f1 = new TFile(“myroot.root”);
TTree tree = (TTree)gDirectory->Get(“run_181951/Wave/Tree”);

Int_t nEntries = tree->GetEntries();

Int_t Sampling;
Int_t Region;
Int_t Eta;
Int_t Phi;
Double_t Fact;

tree->SetBranchAddress(“Sampling”, &Sampling);
tree->SetBranchAddress(“Region”, &Region);
tree->SetBranchAddress(“Eta”, &Eta);
tree->SetBranchAddress(“Phi”, &Phi);
tree->SetBranchAddress(“Fact”, &Fact);

TH1F* hist1 = new TH1F("hist1","hist1",100,0,3);
TH1F* hist2 = new TH1F("hist2","hist2",100,0,20);

for(i=0; i< tree->GetEntries(); ++i) {

  if (Sampling==1 && Region == 0 ) hist1->Fill(Fact);   // the range here on Eta is between 0 to 400 
  if (Sampling==2 && Region == 0 ) hist2->Fill(Fact);   // the range here on Eta is between 0 to 50

  TH2F* ratio = hist1->Divide(hist2);

  TH2F* ratio = hist1->Divide(hist2);


Please, is there a way how can get this correct?

Thank you so much for your help


Hi All,

I think I was not clear in my previous message, I have in my tree the branches Eta, Region, Phi, Fact and Sampling. I am interesting to get the ratio of two histograms, hist1 and hist2 which present the value of the Fact in different region versus Eta (hist1: present the value of the Fact in Sampling==1 && Region==1 and hist2 present the value of the Fact in Sampling==2 && Region==1), the X-axis that present Eta for hist1 and hist2 has different axis limit.

Please, is there a cleaver way to do it?

Many many thanks for help


I am not sure what is the semantic you are meaning. If the 2 histogram have a different range (and you are not interested in ‘just’ the division on the overlap region), you would need to provide the algorithm to match the 2 ranges. In other word, when creating the 2 histogram you should have to encode the matching so that the 2 range match. For example:if (Sampling==1 && Region == 0 ) hist1->Fill(Fact / 8); // the range here on Eta is between 0 to 400 if (Sampling==2 && Region == 0 ) hist2->Fill(Fact); // the range here on Eta is between 0 to 50where Fact/8 needs to be replaced by the semantically correct algorithm.