Hello,
I am trying to change the X-Axis Range and then rebin the data. I want the x-axis range to be from [-120,0] and use Rebin (12), for 10 bins. I have the following lines of code below, the rebin still considers the original x-axis ranges from [-200, 0] and does not work -
Warning in TH1F::Rebin: ngroup=12 is not an exact divider of nbins=200.
It looks like you want to rebin only a part of the histogram. Seems to me that can not work… What will happen to the bins between -200 and -120 ? they will stay un-rebinned ?
Seems to me you should rebin the whole histogram and set the Rebin parameter such as you get the number of bins you need between -120 and 0.
Hello,
Thank you for your reply. I tried this option as well as shown below, it gives me ~ 8 bins from [-120,0]. I am then adding the two histograms to calculate the difference in the area under the histograms as shown in the code below and this part is problematic…
TFile *f = new TFile("output.18.root");
TH1F * h1 = (TH1F*)f->Get("h_KLFitterLL");
h1->Rebin(10); //gives 20 bins from [-200, 0]
h1->SetAxisRange(-120.,0.,"X"); //gives 8 bins from [-120, 0]
h1->Scale(1/h1->Integral());
TFile *f1 = new TFile("output.1.root");
TH1F * h2 =(TH1F*)f1->Get("h_KLFitterLL");
h2->Rebin(10);
h2->SetAxisRange(-120.,0.,"X");
h2->Scale(1/h2->Integral());
//Calculate overlap area
TH1F *hnew = new TH1F("hnew","subtracted_histo",8,-120,0); // creating a histogram with 8 bins between -120, 0.
hnew->Add(h1,h2,1.,-1.);
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),
((h1->GetBinContent(i) < h2->GetBinContent(i)) ? h2->GetBinContent(i)- h1->GetBinContent(i):h1->GetBinContent(i)- h2->GetBinContent(i)));
std::cout << h3->Integral() << std::endl;
std::cout <<"width_integral"<<" "<< h3->Integral("width") << std::endl;
Error in <TH1F::Add>: Attempt to add histograms with different number of bins : nbins h1 = 10 , nbins h2 = 20**
I do not understand why nbins are different in h1 and h2, when I am doing the same rebinning and same X-axis range…
Thank you !