Re: normalizing histograms

From: Rene Brun (Rene.Brun@cern.ch)
Date: Wed Jul 02 1997 - 08:37:06 MEST


Richard Dubois wrote:
> 
> I am using 2 PAW ntuples h2root-ed. I want to overplot variables from
> each file, normalized by entries. I can now sort out (see attachment) how
> to normalize them when I have explicitly defined the histograms.
> 
>  I am curious to know if I can also do it in the 'interactive' mode,
> using the h10 TTree object, viz:
> 
>  TFile f("file1.root");
>  TFile g("file2.root");
> 
>  f.cd();
>  h10.Draw("momentum");
>  g.cd();
>  h10.Draw("momentum","","same");
> 
>  Can I get pointers to these two histograms so I can normalize them
> as per Rene's solution (in the attachment)?
> 
> Thanks,
> 
> Richard
> --
> Richard Dubois
> SLD, Stanford Linear Accelerator Center
> Richard@slac.stanford.edu
> http://www.slac.stanford.edu/~richard/
> 415-926-3824
> 415-926-2923 (FAX)
> 
>     ---------------------------------------------------------------
> 
> Subject: Re: root question
> Date: Tue, 01 Jul 1997 18:00:12 +0200
> From: Rene Brun <Rene.Brun@cern.ch>
> Organization: CERN. European Lab. for Particle Physics
> To: richard@SLAC.Stanford.EDU
> References: <33B926D0.530F@slac.stanford.edu>
> 
> Richard Dubois wrote:
> >
> > Hi Rene,
> >
> >  In a quick search I didn't find an address to send root questions...
> >
> >  I want to do an overlay of two plots, normalized by the number of
> > entries for each. I haven't been able to figure out how to do the
> > normalization. How do I do it?
> >
> 
> Hi Richard,
> We have two lists roottalk and rootdev. To suscribe see http://root.cern.ch
> roottalk is a general discussion list (we have about 300 people now)
> rootdev is the Root developers. Send mail to rootdev@hpsalo.cern.ch
> for bug reports (if any).
> Now the answer to your question.
> Suppose you have a Root file containing two histograms h1 and h2
> You can do:
>   Root > float norm1 = 1/h1.GetEntries()
>   Root > float norm2 = 1/h2.GetEntries()
>   Root > TH1F hnorm1 = norm1*(*h1)
>   Root > TH1F hnorm2 = norm2*(*h2)
>   Root > hnorm1.Draw();
>   Root > hnorm2.Draw("same")
> 
> Note that if h1 and h2 are two TH1F objects (not pointers) created with
>   TH1F h1(...)
>   TH2F h2(..)
> you should replace the statement  above TH1F hnorm1 = norm1*(*h1) by
>   TH1F hnorm1 = norm1*h1
> (*h1) dereferences the pointer (normal C++).
> 
> Rene

I should probably make an HOWTO about this.
Here is a detailed example explaining the normalization procedure.
This example also shows how to divide two histograms and save
the result to a new file

Rene Brun


  TFile f("file1.root");
//  project variable into histogram hf, graphics off
  h10.Draw("momentum>>hf","","goff");
//  compute normalisation factor
  float fnorm = 1/hf->GetEntries()
//  create a new histogram equal to normalized hf histogram
  TH1F hfnorm = fnorm*(*hf);
  hfnorm.Draw();

//  same operations on second file
  TFile g("file2.root");
  h10.Draw("momentum>>hg","","goff");
  float gnorm = 1/hg->GetEntries()
  TH1F hgnorm = gnorm*(*hg);
  hgnorm.Draw("same");

//  Create an histogram equal to the ratio of the two normalized hsitograms
  TH1F hratio = hfnorm/hgnorm;

//save normalized histograms into a 3rd file
  TFile result("result.root","NEW");
  hfnorm.Write();
  hgnorm.Write();
  hratio.Write();



This archive was generated by hypermail 2b29 : Tue Jan 04 2000 - 00:26:19 MET