Filling a function to a histogram

Hello Rooters,

I was trying to calculate the significance value for every value using a signal and background histogram. I need to save this significance value and the corresponding bin value for later use. I was trying to use TH1D for storing significance values. I need to save the significance value for the first bin cut in the first bin. First I am not sure how to do that. Second I feel using a histogram to store a function is not the most natural way here, there must be something better. I have attached the code below. Thanks for your time!

int nent = h1_s->GetNbinsX();
		for(int e=0; e<nent; e++){
			double cut_value = h1_s->GetBinCenter(e);
			s = h1_s->Integral(e,nent);
			b = h1_b->Integral(e,nent);
			significance = s/sqrt(s+b);

h1_s and h1_b are signal and background histograms respectively. h_declengthNorm is a histogram with the same number of bins, bin min, and bin max as h1_s and h1_b, this is where I intend to save the significance function.

ROOT Version:6.24/02
Compiler: gcc 11.1.0

Hi @Aman_Upadhyay; I am sure either @couet or @moneta can help you with this.


1 Like

histo->Fill(x) does not do what you think it does; maybe you want SetBinContent instead (search for it on that same page). In any case, I think saving the bin centre and the significance (as 2 variables) in a TTree is probably a better choice than a histogram, to let you use the values later for anything else.

Yes, I know histo->Fill(x) will not work in this case :sweat_smile:, that’s why the question. I needed SetBinContent, thanks for that! And I see that a TTree will be much better. Thanks a lot.