Example of FirstBinAbove

Hello,

Could you please tell me how to use FirstBinAbove and LastBinAbove with a example , I mean with a range between 2000 to 4000 I have a histogram. To get the fwhm i need this FirstBinAbove and LastBinAbove. But I can not understand how do I give the range ?

Thanks
Saradindu

{
  TH1D *h = new TH1D("h", "h", 100, -5., 5.);
  h->FillRandom("gaus");
  h->Draw();
  Double_t first_y = 100.;
  Int_t first = h->FindFirstBinAbove(first_y);
  // Int_t first = h->FindFirstBinAbove(first_y, 1, 1, h->GetNbinsX());
  std::cout << "first bin = " << first << std::endl;
  if (first != -1) {
    std::cout << "first x = " << h->GetBinCenter(first) << std::endl;
    std::cout << "first y = " << h->GetBinContent(first) << std::endl;
  }
  Double_t last_y = 100.;
  Int_t last = h->FindLastBinAbove(last_y);
  // Int_t last = h->FindLastBinAbove(last_y, 1, 1, h->GetNbinsX());
  std::cout << "last bin = " << last << std::endl;
  if (last != -1) {
    std::cout << "last x = " << h->GetBinCenter(last) << std::endl;
    std::cout << "last y = " << h->GetBinContent(last) << std::endl;
  }
}

Many many thanks ! I am trying it now.

Regards
Saradindu

Hello Wile_E_Coyote,

This is not working . I am writing the problem .

"Create a TH1D histogram in the range of 200-200 keV , with a bin width of 2 keV. Attached to the exercise is an event file named “eventFile.txt”. This is the list of measurements.Each no represent a single event with the listed energy in keV. Write a function that reads the event file and sorts the data using Fill() into the above histogram. Processing: eventFile.dat…
eventFile.txt (154.0 KB)
Use GetMaximum(),FindFirstBinAbove()and FindLastBinAbove() to obtain the full width at half maximum of the second peak. Note that you need to provide a range in bin numbers for the former functions.

Thanks
Saradindu

You can get the “bin number” corresponding to some “x” value by using: TH1::FindFixBin

I can not get the correct fwhm .
My program was like

root [0] TH1D *histo = new TH1D (“histo”,“histo”,900,200,2000)
root [1] ifstream inp;
root [2] double x;
root [3] inp.open(“eventFile.dat”);
root [4] while(!(inp>>x)==0) { histo->Fill(x);}
root [5] histo->Draw()
Info in TCanvas::MakeDefCanvas: created default TCanvas with name c1
root [6] inp.close();
root [7] histo->GetXaxis()->SetRangeUser(720,770)
root [8] histo->Draw()
root [9] histo->GetMaximum()
(const Double_t)1.59000000000000000e+02
root [10] int bin1=histo->FindFirstBinAbove(histo->GetMaximum()/2);
root [11] int bin2=histo->FindLastBinAbove(histo->GetMaximum()/2);
root [12] double fwhm = histo->GetBinCenter(bin2)-histo->GetBinCenter(bin1);
root [13] fwhm
(double)1.36200000000000000e+03

***** in defining bin1 and bin 2 I have give the range of bin but I can not give the range . How do I give the range ?

double first_x = 720., last_x = 770.;
histo->GetXaxis()->SetRangeUser(first_x, last_x);
int first = histo->FindFixBin(first_x), last = histo->FindFixBin(last_x);
int bin1 = histo->FindFirstBinAbove(histo->GetMaximum() / 2., 1, first, last);
int bin2 = histo->FindLastBinAbove(histo->GetMaximum() / 2., 1, first, last);

Following errors are coming . I am running root6 .

root [15] int left = histo->FindFixBin(720),right=histo->FindFixBin(770);
root [16] int left = histo->FindFixBin(720);
root [17] int right =histo->FindFixBin(770);
root [18] int bin1= histo->FindFirstBinAbove(histo->GetMaximum()/2.,1,left,right);
ROOT_prompt_18:1:61: error: reference to ‘left’ is ambiguous
int bin1= histo->FindFirstBinAbove(histo->GetMaximum()/2.,1,left,right);
^
ROOT_prompt_16:1:5: note: candidate found by name lookup is ‘__cling_N517::left’
int left = histo->FindFixBin(720);
^
/usr/include/c++/5/bits/ios_base.h:999:3: note: candidate found by name lookup is ‘std::left’
left(ios_base& __base)
^
ROOT_prompt_18:1:66: error: reference to ‘right’ is ambiguous
int bin1= histo->FindFirstBinAbove(histo->GetMaximum()/2.,1,left,right);
^
ROOT_prompt_17:1:5: note: candidate found by name lookup is ‘__cling_N518::right’
int right =histo->FindFixBin(770);
^
/usr/include/c++/5/bits/ios_base.h:1007:3: note: candidate found by name lookup is ‘std::right’
right(ios_base& __base)
^

That’s a problem in the CLING interpreter, when used directly from the ROOT prompt.
Instead of “left” and “right”, use, e.g., “left_bin” and “right_bin”.

Dear Wile_E_Coyote ,
Thank you very much . It works fine. I hope I can learn from you more.

Regards
Saradindu