# Example of FirstBinAbove

**URL:** <https://root-forum.cern.ch/t/example-of-firstbinabove/47151>\
**Category:** Newbie\
**Created:** [October 7, 2021, 11:28am UTC](https://root-forum.cern.ch/t/example-of-firstbinabove/47151 "2021-10-07T11:28:39Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Saradindu](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/saradindu/32/26687_2.png) [@Saradindu](https://root-forum.cern.ch/u/Saradindu)\
**Post date:** [October 7, 2021, 11:28am UTC](https://root-forum.cern.ch/t/example-of-firstbinabove/47151/1 "2021-10-07T11:28:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [October 7, 2021, 11:52am UTC](https://root-forum.cern.ch/t/example-of-firstbinabove/47151/2 "2021-10-07T11:52:04Z")

</div>

```cpp
{
  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;
  }
}

```

---

<div class="post-metadata">

**Author:** ![Saradindu](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/saradindu/32/26687_2.png) [@Saradindu](https://root-forum.cern.ch/u/Saradindu)\
**Post date:** [October 7, 2021, 11:55am UTC](https://root-forum.cern.ch/t/example-of-firstbinabove/47151/3 "2021-10-07T11:55:44Z")

</div>

Many many thanks ! I am trying it now.

Regards  
Saradindu

---

<div class="post-metadata">

**Author:** ![Saradindu](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/saradindu/32/26687_2.png) [@Saradindu](https://root-forum.cern.ch/u/Saradindu)\
**Post date:** [October 7, 2021, 12:15pm UTC](https://root-forum.cern.ch/t/example-of-firstbinabove/47151/4 "2021-10-07T12:15:17Z")

</div>

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](https://root-forum.cern.ch/uploads/short-url/jveBvvhVa6IcZRLV9yG8xtaSnLm.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

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [October 7, 2021, 12:24pm UTC](https://root-forum.cern.ch/t/example-of-firstbinabove/47151/5 "2021-10-07T12:24:11Z")

</div>

You can get the “bin number” corresponding to some “x” value by using: [TH1::FindFixBin](https://root.cern/doc/master/classTH1.html#aee13ee34b23f0a238af6279ef5441bc4)

---

<div class="post-metadata">

**Author:** ![Saradindu](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/saradindu/32/26687_2.png) [@Saradindu](https://root-forum.cern.ch/u/Saradindu)\
**Post date:** [October 7, 2021, 12:27pm UTC](https://root-forum.cern.ch/t/example-of-firstbinabove/47151/6 "2021-10-07T12:27:59Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [October 7, 2021, 12:41pm UTC](https://root-forum.cern.ch/t/example-of-firstbinabove/47151/7 "2021-10-07T12:41:18Z")

</div>

```cpp
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);

```

---

<div class="post-metadata">

**Author:** ![Saradindu](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/saradindu/32/26687_2.png) [@Saradindu](https://root-forum.cern.ch/u/Saradindu)\
**Post date:** [October 7, 2021, 1:00pm UTC](https://root-forum.cern.ch/t/example-of-firstbinabove/47151/8 "2021-10-07T13:00:43Z")

</div>

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)  
^

---

<div class="post-metadata">

**Author:** ![Wile\_E\_Coyote](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/wile_e_coyote/32/12417_2.png) [@Wile\_E\_Coyote](https://root-forum.cern.ch/u/Wile_E_Coyote)\
**Post date:** [October 7, 2021, 2:08pm UTC](https://root-forum.cern.ch/t/example-of-firstbinabove/47151/9 "2021-10-07T14:08:18Z")

</div>

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”.

---

<div class="post-metadata">

**Author:** ![Saradindu](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/saradindu/32/26687_2.png) [@Saradindu](https://root-forum.cern.ch/u/Saradindu)\
**Post date:** [October 7, 2021, 2:45pm UTC](https://root-forum.cern.ch/t/example-of-firstbinabove/47151/10 "2021-10-07T14:45:10Z")

</div>

Dear Wile\_E\_Coyote ,  
Thank you very much . It works fine. I hope I can learn from you more.

Regards  
Saradindu
