Re: Histogram normalization

From: George Heintzelman (gah@bnl.gov)
Date: Thu Jan 20 2000 - 20:06:44 MET


> > I  normalize histograms like this :
> > 
> > let's say h1 contains the TH1 descendant histogram you want to
> > normalize (TH1F *h1)
> > 	
> >   if (h1->Integral()!=0)
> >     h1->Scale(1/h1->Integral());
> 
> This is not exactly a FAST way to do this, since it invloves sending
> to messages to the TH1 object. If you're not pursuaded that this is
> slow, take a look at the code for the method TH1::Intergal(void),
> which uses the method TH1::Interal(int, int). The later contains THREE
> for loops, and multiple uses of TH1<x>::GetBinContent(int) (one for
> each bin)!
> 
> Rather, you should save the returned value in a temporary variable,
> compare the value to zero, and if ture, use that value. It may be,
> that the compiler actually does exactly this for you, but it really
> depends on the compiler optimization, and hence on the compiler - you
> really want to minize that dependency. 

In fact, I don't think any compiler will be able to do this here. This 
depends on the sure knowledge that a) none of the factors that go into 
the calculation of the Integral() function have changed between the two 
calls -- difficult for a compiler to be certain of, given the 
contortions of aliasing capabilities in C++ -- and b) that Integral() 
has no possible side effects that the user wants to carry out (eg, 
writing a message to screen). Since Integral is a virtual function, 
there is even no theoretical way for the compiler to check either of 
these two things here -- the function called at run-time may not even 
have been written when this code is compiled.

However, this may be a place that I should argue for this and similar 
'expensive' functions in ROOT histogram objects to be cached -- 
calculated & stored the first time it is requested, and recalculated 
only if the histogram has been changed in between. This is not too hard 
to do, though it requires a little thought to do right for objects 
(such as histograms) where there are many possible such values to 
calculate independently. If the Root team were to do this, then the 
original code would be perfectly fine.

George Heintzelman
gah@bnl.gov



This archive was generated by hypermail 2b29 : Tue Jan 02 2001 - 11:50:17 MET