Hello,
I have plotted two overlapping histograms and I need to calculate the overlapping area between them by using absolute integral method (since h2 is sometimes greater than h1). Below is my code ( I get the subtracted histogram! ) -
TH1F *h3 = new TH1F(“h3”,“subtracted_histo”,100,-120,-40);
h3->Add(h1,h2,1.,-1.);
TCanvas *c1 = new TCanvas(“h3”,“h3”);
h3->Draw();
//absolute value of the integral
h3->Integral();
Also, I need to print the overlapping integral on the canvas, any help is appreciated !
Hi Pepe,
Thank you for the help. I am getting a negative Integral in this case, will this code give me the absolute value of the overlapping area (I am a bit skeptical about it, as I get something like -4.8 e^-9 ).
Apart from that, all is good
I’m afraid you will need to manually loop over histograms’ bins (choosing the “smaller” one).
If you can switch to functions, you may try: To find the value of overlapping two histogram
Hi Pepe,
Okay, I understand what is needed to be done. Could you explain this - manually loop over histograms’ bins in more detail, or refer to a thread of documentation.
Both bins for my histograms are the same. I am afraid, using functions would be complicated in my case as that is the output I get, instead of plotting it.
// Assuming that "h1" and "h2" have EXACTLY the same x-bins' edges ...
TH1F *h3 = new TH1F(*h1);
h3->SetNameTitle("h3", "min(h1, h2)");
h3->Reset("M");
for (int i = 1; i <= h3->GetNbinsX(); i++)
h3->Fill(h3->GetBinCenter(i),
TMath::Min(h1->GetBinContent(i), h2->GetBinContent(i)));
std::cout << h3->Integral() << std::endl;
std::cout << h3->Integral("width") << std::endl;
Hello Pepe,
Thanks for the information, I get a positive integral now !
Just to be sure, the definitions of h1, h2, h31 (subtracted histo) are below -
TH1F *h1 = new TH1F(“h1”,“log_likelihood_ttbar”,100,-120,-40);
TH1F *h2 = new TH1F(“h2”,“log_likelihood_ttH”,100,-120,-40);
TH1F *h31 = new TH1F(“h31”,“subtracted_histo”,100,-120,-40);
h31->Add(h1,h2,1.,-1.);
Just a small question - what is the " * " before h1 in TH1F *h3 = new TH1F(*h1);
Is the h3 you defined, still the same as my h31 as above ?
Hence, the nbins is the same, if that is what is meant by same x-bins’ edges.
Thanks !
Shreya