# Resetting the bin content and recalling the bin content error

**URL:** <https://root-forum.cern.ch/t/resetting-the-bin-content-and-recalling-the-bin-content-error/40927>\
**Category:** ROOT\
**Created:** [August 9, 2020, 9:22pm UTC](https://root-forum.cern.ch/t/resetting-the-bin-content-and-recalling-the-bin-content-error/40927 "2020-08-09T21:22:08Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![spyhunter01](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/spyhunter01/32/23019_2.png) [@spyhunter01](https://root-forum.cern.ch/u/spyhunter01)\
**Post date:** [August 9, 2020, 9:22pm UTC](https://root-forum.cern.ch/t/resetting-the-bin-content-and-recalling-the-bin-content-error/40927/1 "2020-08-09T21:22:08Z")

</div>

Imagine I created an histogram h1. Then, I set my bin contents and errors by myself by using SetBinContent(…) and SetBinError(…) functions. Then, I clone this histogram by giving a new name like h2. I changed the 2nd bin content with SetBinContent(2, 10) option as the value of 10. It was different value before. I noticed that bin error couldn’t recalculated by itself. Thus, I set it by myself again.

If I scale the h1 with 2 to create h4 , the error is recalculated by itself at this timre. Why I couldn’t get it in the 1st case. I used h2-\>Sumw2(); earlier in the code to make sure it recalculates the errors.

TH1F _h2 = (TH1F_)h1-\>Clone(“h2”);  
h2-\>SetBinContent(2, 10);  
h2-\>SetBinError(2, 10./100.);  
cout \<\< "\tContentError1: " \<\< h2-\>GetBinError(1) \<\< "\tContentError2: " \<\< h2-\>GetBinError(2)\<\< endl;

* * *

TH1F _h4 = (TH1F_)h1-\>Clone(“h4”);  
h4-\>Scale(2.);  
h4-\>Draw(“histo”);   
cout \<\< "\tContentError1: " \<\< h4-\>GetBinError(1) \<\< "\tContentError2: " \<\< h4-\>GetBinError(2)\<\< endl;

---

<div class="post-metadata">

**Author:** ![eguiraud](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/eguiraud/32/32217_2.png) [@eguiraud](https://root-forum.cern.ch/u/eguiraud)\
**Post date:** [August 13, 2020, 7:48am UTC](https://root-forum.cern.ch/t/resetting-the-bin-content-and-recalling-the-bin-content-error/40927/2 "2020-08-13T07:48:49Z")

</div>

Hi @spyhunter01,  
@moneta can probably help here.

Cheers,  
Enrico

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [August 13, 2020, 8:16am UTC](https://root-forum.cern.ch/t/resetting-the-bin-content-and-recalling-the-bin-content-error/40927/3 "2020-08-13T08:16:49Z")

</div>

Hi,

I am not sure I have understood your problem, maybe posting a full working macro could help.

After calling SetBinError, errors are computed from the stored values (fSumw2 array, which keeps the square of the bin errors).  
When you scale the histogram the array is also scaled in order to get the scaled errors. So if you have set the bin error to a value `error` , after scaling the error will be `scale_factor * error`.  
If you have never set the bin errors in your histograms, the errors will be computed as `sqrt(bin_content)`. If you set the bin error for only some bins, the errors will be by default zero for the other bins.

Cheers

Lorenzo

---

<div class="post-metadata">

**Author:** ![spyhunter01](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/spyhunter01/32/23019_2.png) [@spyhunter01](https://root-forum.cern.ch/u/spyhunter01)\
**Post date:** [August 13, 2020, 8:55am UTC](https://root-forum.cern.ch/t/resetting-the-bin-content-and-recalling-the-bin-content-error/40927/4 "2020-08-13T08:55:40Z")

</div>

#include   
#include “TAttMarker.h”  
#include “TCanvas.h”  
#include “TLatex.h”  
#include “TDirectory.h”  
#include “TAxis.h”  
#include //string  
#include “TH1F.h”  
#include “TH2F.h”  
#include “TF1.h”  
#include “TChain.h”  
#include “TStyle.h” //for gStyle decleration  
#include “TMath.h”  
#include “TRandom.h”  
#include “TAxis.h”  
int testbinning()

{

TCanvas \*c1 = new TCanvas(“c1”, “c1”,0,75,3665,2044);  
c1-\>Range(0,0,1,1);  
float pad\_x1= 0.05, pad\_x2= 0.5 , pad\_x3= 0.95, pad\_y1= 0.5, pad\_y2= 0.95, pad\_y3= 0.05 ;  
TPad \*c1\_1 = new TPad(“c1\_1”, “newpad”, pad\_x1, pad\_y1, pad\_x2, pad\_y2);  
TPad \*c1\_2 = new TPad(“c1\_2”, “newpad”, pad\_x2, pad\_y1, pad\_x3, pad\_y2);  
TPad \*c1\_3 = new TPad(“c1\_3”, “newpad”, pad\_x1, pad\_y3, pad\_x2, pad\_y1);  
TPad \*c1\_4 = new TPad(“c1\_4”, “newpad”, pad\_x2, pad\_y3, pad\_x3 ,pad\_y1);  
c1\_1-\>Draw();  
c1\_1-\>cd();

const char \*name=“name”;  
const char \*title=“title”;  
Int\_t BinWidth= 1;  
Double\_t hist\_lower\_xlimit=5.;  
Double\_t hist\_upper\_xlimit=11. ;  
int NbinsX = TMath::Ceil((hist\_upper\_xlimit-hist\_lower\_xlimit) / BinWidth );

TH1F_h1= new TH1F( name,title,NbinsX,hist\_lower\_xlimit,hist\_upper\_xlimit); //TH1F : histograms with one float per channel. Maximum precision 7 digits  
TAxis_ xAxis = h1-\>GetXaxis();  
/\*  
for(double i= hist\_lower\_xlimit ; i \< hist\_upper\_xlimit ; i++){  
h1-\>Fill(i, i+20);  
}  
\*/  
for(int i=0 ; i \<= h1-\>GetNbinsX() ; i++){

h1-\>SetBinContent(i, i+20);  
h1-\>SetBinError(i, (i+20)/100.);  
}

h1-\>Draw(“E,histo”);

c1\_1-\>Modified(); //retain  
c1-\>cd();

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

int binx1= xAxis-\>FindBin(hist\_lower\_xlimit); //FindBin (Double\_t x, Double\_t y=0, Double\_t z=0) Return Global bin number corresponding to x,y,z  
cout \<\< "\n\nTotal bin number ROOT finds: " \<\< h1-\>GetNbinsX()\<\<endl;  
cout \<\< “To compare with my calculation of bin number:” \<\< NbinsX \<\< endl;  
cout \<\< "Bin width ROOT calculates: "\<\< h1-\>GetBinWidth(1) \<\< endl; //GetBinWidth(bin no)  
cout \<\< "To compare with my bin width: " \<\< BinWidth \<\< endl;  
cout \<\< “hist\_lower\_xlimit= “\<\<hist\_lower\_xlimit\<\<” related bin number:”\<\< binx1\<\<endl;  
cout \<\< “To find the X value details from the bin INFO:”\<\< endl;  
cout \<\< "X Lower edge: " \<\< xAxis-\>GetBinLowEdge(binx1) \<\< endl;  
cout \<\< "X Center: " \<\< xAxis-\>GetBinCenter(binx1) \<\< endl;  
cout \<\< "X Upper edge: " \<\< xAxis-\>GetBinUpEdge(binx1) \<\< endl;  
cout \<\< "Bin content from bin no: " \<\< h1-\>GetBinContent(binx1) \<\< endl;  
cout \<\< "Bin content error from bin no: “\<\<binx1 \<\<” : " \<\< h1-\>GetBinError(binx1) \<\< endl;  
cout \<\< "Bin content error from bin no: “\<\< 2 \<\<” : " \<\< h1-\>GetBinError(2) \<\< endl;  
cout \<\< "Bin content from integral of binx1-binx1 range: "\<\< h1-\>Integral(binx1,binx1) \<\< endl;

//GetBinContent (Int\_t bin) const ,Return content of bin number whatever bin is.

c1\_2-\>Draw();  
c1\_2-\>cd();

TH1F _h2 = (TH1F_)h1-\>Clone(“h2”);  
//c1-\>Update(); // or create a pad and clear before drawing the 2nd one  
cout\<\<"\nLet’s merge 2 bins into one bin."\<\<endl;  
h2-\>Rebin(2.); //merge 2 bins into one bin. //https://root.cern/doc/v610/classTH1.html#aff6520fdae026334bf34fa1800946790  
h2-\>Sumw2();  
cout\<\< "\n\n\nTotal bin number: " \<\< h2-\>GetNbinsX()\<\< endl;  
//cout\<\<“Only 1st bin content is changed.”\<\<endl;  
h2-\>Draw(“E,histo”);   
cout \<\< "\tContent1: " \<\< h2-\>GetBinContent(1) \<\< "\tContent2: " \<\< h2-\>GetBinContent(2)\<\< endl;  
cout \<\< "\tContentError1: " \<\< h2-\>GetBinError(1) \<\< "\tContentError2: " \<\< h2-\>GetBinError(2)\<\< endl;  
//c1-\>Modified(); //not working  
cout\<\< "integral of 1-2 bins: "\<\< h2-\>Integral(1,2) \<\< endl;  
c1\_2-\>Modified(); // koru , kaybetme , retain  
c1-\>cd();

/////////////////////////////////////////////////////////////  
c1\_3-\>Draw();  
c1\_3-\>cd();

TH1F _h3 = (TH1F_)h1-\>Clone(“h3”);  
h3-\>SetBinContent(2, 10);  
h3-\>SetBinError(2, 10./100.);  
cout\<\<"\n\nOnly 2nd bin content is changed."\<\<endl;  
h3-\>Draw(“E,histo”);   
cout \<\< "\tContent1: " \<\< h3-\>GetBinContent(1) \<\< "\tContent2: " \<\< h3-\>GetBinContent(2)\<\<endl;  
cout \<\< "\tContentError1: " \<\< h3-\>GetBinError(1) \<\< "\tContentError2: " \<\< h3-\>GetBinError(2)\<\< endl;  
cout\<\< "integral of 1-2 bins: "\<\< h3-\>Integral(1,2) \<\< " integral of 2-3 bins: "\<\< h3-\>Integral(2,3) \<\< " integral of 3-4 bins: "\<\< h3-\>Integral(3,4) \<\< endl;  
c1\_3-\>Modified(); // koru , kaybetme , retain  
c1-\>cd();  
/////////////////////////////////////////////////////////////  
c1\_4-\>Draw();  
c1\_4-\>cd();

TH1F _h4 = (TH1F_)h1-\>Clone(“h4”);  
h4-\>Scale(2.);  
h4-\>Draw(“E,histo”);  
cout \<\< "\n\nNew Scaled Content1: " \<\< h4-\>GetBinContent(1) \<\< "\tContent2: " \<\< h4-\>GetBinContent(2)\<\< endl;  
cout \<\< "\tContentError1: " \<\< h4-\>GetBinError(1) \<\< "\tContentError2: " \<\< h4-\>GetBinError(2)\<\< endl;  
cout\<\< "integral of 1-2 bins: "\<\< h4-\>Integral(1,2) \<\< endl;  
c1\_4-\>Modified(); // retain  
c1-\>cd();  
/////////////////////////////////////////////////////////////  
c1-\>Modified(); // retain  
c1-\>cd();  
c1-\>SetSelected(c1);  
c1-\>ToggleToolBar();

return 0;  
}

---

<div class="post-metadata">

**Author:** ![spyhunter01](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/spyhunter01/32/23019_2.png) [@spyhunter01](https://root-forum.cern.ch/u/spyhunter01)\
**Post date:** [August 13, 2020, 9:00am UTC](https://root-forum.cern.ch/t/resetting-the-bin-content-and-recalling-the-bin-content-error/40927/5 "2020-08-13T09:00:02Z")

</div>

I posted the code that I was testing some options. I clone the 1st histogram for h3, then I only set up the 2nd bin’s error and content. The errors of the other bins didn’t become 0 as you said. They kept the original histogram h1’s information.

Another think I want to ask is about Sumw2(). When I got warning saying that I have created it already even if I typed it only one tiöe in the whole code.

Cheers.

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [August 14, 2020, 8:06am UTC](https://root-forum.cern.ch/t/resetting-the-bin-content-and-recalling-the-bin-content-error/40927/6 "2020-08-14T08:06:38Z")

</div>

Hi,

Thank you for posting your code, but do not paste as text as it becomes unreadable and one cannot execute. Use code tags (```) or add the code as a file attachment.  
See [Tips for Efficient and Successful Posting](https://root-forum.cern.ch/t/tips-for-efficient-and-successful-posting/28292)

---

<div class="post-metadata">

**Author:** ![moneta](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/moneta/32/19423_2.png) [@moneta](https://root-forum.cern.ch/u/moneta)\
**Post date:** [August 14, 2020, 8:20am UTC](https://root-forum.cern.ch/t/resetting-the-bin-content-and-recalling-the-bin-content-error/40927/7 "2020-08-14T08:20:38Z")

</div>

Concerning your questions:

- when you call SetBinError(…), Sumw2() is called internally, since the array needs to be created to store the errors so you don’t need to call it again.
- In my previous quote:

> [@moneta](#):
>
> If you have never set the bin errors in your histograms, the errors will be computed as `sqrt(bin_content)` . If you set the bin error for only some bins, the errors will be by default zero for the other bins.

it is true I did not explain correctly. The errors by default are computed as `sqrt(bin_content)`. If you have an already filled histograms (i.e. with content not equal to zero) and you call `SetBinError` on some bins, the error will be computed as default as `sqrt(bin_content)` for all bins where you have not called `SetBinError`. However, if you have an empty bin and then you call `SetBinContent` to fill that bin, and you have called before `SetBinError` for some another bins, the error will be zero for the bin where you have called just `SetBinContent`.

Lorenzo

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [August 28, 2020, 8:20am UTC](https://root-forum.cern.ch/t/resetting-the-bin-content-and-recalling-the-bin-content-error/40927/8 "2020-08-28T08:20:43Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
