TH1 Weighted Fill. Problem with errors and draw

TH1 Weighted Fill. Problem with errors and draw.

Hello I’m using ROOT version 5.99/01
I’m having a peculiar error when filling a histogram directly from stored files.
Specifically when using weighted fill:
TH1::Fill(x, w)

When I fill using this method the error in each bin is set to the bin value rather than sqrt N. Also the draw option is forced to draw “simple errors” which cannot be turned in the editor.

I have found ways around this, TH1::fill(x) and TH1::AddBinContent(bin, w) both work. Or errors can be corrected manually and TH1::Draw(“HIST”) used.

Is this a known error or is there an issue with the installation on our system?

I’ve included code and an example file to show the problem:

{ 
TH1F *hista = new TH1F("firsthist","thefirsthist",20,0,10); 

ifstream theinfileA; 
theinfileA.open("data.dat"); 
if(theinfileA.is_open()){ 
	while(!theinfileA.eof()){ 
		Double_t x,value; 
		theinfileA >> x >> value; 

		hista->Fill(x,value);				//OPTION A Errors and Drawing problems 

		//for(int i=0;i<value;i++)hista->Fill(x);	//OPTION B This works, but obviously is slow. 

		//int bin = hista->FindBin(x);			//OPTION C This works fine. 
		//hista->AddBinContent(bin, value); 
	} 
} 
theinfileA.close(); 

for(int j=1;j<=20;j++)	cout << hista->GetBinContent(j) << " " << hista->GetBinError(j) << endl; 

//hista->Draw(); 
hista->Draw("HIST");	//This fixes the drawing option but not the errors are still wrong 
}

data.dat
0.0 3354569.0
0.5 3257999.0
1.0 4168587.0
1.5 3872104.0
2.0 2201810.0
2.5 1091245.0
3.0 819896.0
3.5 881504.0
4.0 1226076.0
4.5 1770540.0
5.0 2007821.0
5.5 1578562.0
6.0 1194769.0
6.5 1016185.0
7.0 940008.0
7.5 898785.0
8.0 904193.0
8.5 966456.0
9.0 983741.0
9.5 963953.0





data.dat (272 Bytes)
code.C (704 Bytes)

Hi,

This is expected. The behaviour has changed in ROOT 5.99 and now by calling TH1::Fill with a weights different than one makes automatically the computation of error using the square root of the sum of the weights.
In your case you, since you are having an histogram with counts, you should just call SetBinContent (or AddBinContent) for setting the content in each bin.
Eventually, we could add a new method TH1::FillN( double x, int n) for filling n times an histogram from the same value x. It is like SetBinContent( FindBin(x), n), but will set also the correct number of entries.

Best Regards

Lorenzo

1 Like

Thank you for the clarification.

Is the change in draw options also to be expected with this method for errors?

Hi,

The drawing changed because the histograms stores now the bin error for each bin. In this case you need to call
TH1::Draw(“HIST”) for not drawing the errors. The behaviour of TH1::Draw has not changed with respect to 5.34

Lorenzo

Dear Lorenzo,

I am confused: if now ROOT “makes automatically the computation of error using the square root of the sum of the weights” why in the example of James the errors are just equal to the sum of weights? His option B works properly, but the option A does not?

All the best,
Alexey

Hi,

Sorry I have misspelled it in my previous post of 4 years ago. The error is computed using the
square root of the sum of the square of the weights.
So in his example James was filling each bin with a single entry weighted by the bin content. In this case the error is then just equal to the weight.

Sorry for the confusion
Cheers

Lorenzo

Thanks for the confirmation, Lorenzo! I also saw in the source code that it is ‘square root of the sum of the square of the weights’, but just to be sure that there are no caveats in that sensible issue (I have to use both v5.34 and v6.x intermittently).

Cheers,
Alexey

Hi Lorenzo,

I am plotting a histogram of some weighted events. When I draw it I see the error bars. What can I do to get rid of the error bars?

Thank you

Best Regards,
Your previous student, Pourya :slight_smile:

someHisto->Draw("HIST");

Thanks a lot. It worked.

So as of 2019, what is the best way to make a histogram from a ASCII file, which already has a list of bin contents of MCA? I could not find TH1::FillN(double x, int n). Would be great if the number of entries is also automatically calculated from the sum of n.

Maybe it is easier to use a TGraph.

You can use myHist->Sumw2(0); to remove the error bar.

Why TGraph? It is not a histogram.

In general, from a MCA, I expect data in form “channel number” plus its “number of counts”. In ROOT, you can easily read files which contain two columns of numbers and create a TGraph. Moreover if you want to “calibrate” your “channel numbers” into a quantity like “pulse-height” or “time” or “energy”, you will often need to apply some nonlinear function, which will again be easy for a TGraph but, with a histogram, you would need to use a “variable bin sizes” histogram (and this may not be that straightforward, if you started with a “fixed bin sizes” histogram).

Thank you very much for the clarification. But I was not assuming a very detailed analysis of a MCA data set, because my motivation was for a beginners’ data analysis class to tell them how to analyze real data by ROOT. Looks that there is no simple way to convert an existing MCA data to a THI. Only the way is to repeat TH1::Fill n times.

You can use TH1::Fill(Double_t x, Double_t weight) (where “x” would be the “channel number” and “weight” would be the “number of counts”).

The original post and my question were asking how to “easily” set the bin error and the number of entries when calling TH1::Fill(x, weight). We have to call TH1::SetBinError.

Hi Wile, I face the similar problem, and my problem is I need to subtract the pedestal value from my original TTree. How could I do this? From your answer, I need to use [TH1::Fill(Double_t x, Double_t weight)],and I need to change the weight i.e. the number of counts (number of counts minus some number, need to save the new difference to the original tree also), and how could I do it? Could you please tell me a little more details? Thanks so much for your time!