Re: Histogram normalization

From: Rene Brun (Rene.Brun@cern.ch)
Date: Thu Jan 20 2000 - 18:21:39 MET


Hi Christiam, Tim, etc,

Most of the time, you can use the number of entries if all your weights
= 1.
 Double_t norm = h->GetEntries();
 if (norm) h->Scale(1/norm);

If you have weights, you can do:
 Double_t norm = h->GetSumOfWeights();

At the end, use h->Integral() if you want to normalize to a sub-range
of an histogram.

Rene Brun

Christian Holm Christensen wrote:
> 
> Hi Tim,
> 
> You wrote in a reply to Christopher B. Lirakis:
> 
> > 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 short, do:
> 
>   THF1* h = new THF1(....);
>   // Fill histogram
>   int intergal = h->Intergral();
>   if (intergral > 0)
>      h->Scale(1/intergral);
>   else {
>      cerr << "Empty Histogram: " << h->GetName() << endl;
>      return kError;
>   }
> 
> Omit the last `else' if you don't consider an empty histogram an
> error.
> 
> Perhaps a method called TH1* TH1::Normalize() is in order, so that
> nobody feels inclinded to write (I belive) slow code.
> 
> I hope this was of some help to you. If my intuition with regards to
> speed are completly wrong, I apologize and stand corrected.
> 
> Cheers,
> 
> Christian Holm Christensen
> ______________________________________________________________________
> Address:                                     Phone:  (+45) 35 35 96 91
>   Sankt Hansgade 23, 1. th.                  Office: (+45) 353  25 307
>   DK-2200 Copenhagen N                       Web:    www.nbi.dk/~cholm
>   Denmark                                    Email:       cholm@nbi.dk
>



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