Re: Need some advice

From: Nick van Eijndhoven (Nick@fys.ruu.nl)
Date: Wed Jun 04 1997 - 09:36:38 MEST


*** Rene Brun wrote :
> 
> Stefan Kluth wrote:
> > 
> > > Nick van Eijndhoven wrote:
> > > >
> > > > Dear friends,
> > > > In ROOT I have 3 histos (1 dimensional), h1, h2 and h3.
> > > > h1 I fit with some function f and now I would like to subtract
> > > > h3 from h2 in which I weight each bin of h3 with the function value of
> > > > f in that bin. I.e. result= h3 - f(x)*h2.
> > > > Could anyone of you tell me the most effective (c.q. elegant) way to
> > > > do this, such that I don't have to change all the code when changing the
> > > > function f ?
> > >
> > > On Tue, 3 Jun 1997, Rene Brun wrote:
> > >
> > > Below is a script illustrating a possible solution:
> > >
> > > Rene Brun
> > >
> > > {
> > >    gROOT->Reset();
> > >    TFile f("hsimple.root");  //assume a file with histogram named hpx
> > >    Int_t ncx = hpx->GetNbinsX();
> > >    TH1F hnew = *hpx;        //copy hpx to hnew
> > >    hnew.Reset();
> > >    TFormula f1("f1","x*x*x");
> > >    for (Int_t bin=1;bin<=ncx;bin++) {  //fill hnew with function values
> > >       Float_t x = hnew.GetBinCenter(bin);
> > >       hnew.Fill(x, f1.Eval(x));
> > >    }
> > >    TH1F hresult = hnew*(*hpx);    //make result histogram
> > >    //you could also do directly
> > >    TH1F hresult = h1 -hnew*(*hpx);
> > >    hresult.Draw();
> > > }
> > >
> > This looks like a candidate for a member function (or method) of TH1F,
> > perhaps one should be able to say something like (once a histo hnew and a
> > function f1 have been defined):
> > root> hnew.Sample("f1")
> > which would run the algorithm shown in Rene's script, i.e. sample the
> > function f1 at the bin centres of hnew and fill the results into hnew.
> > 
> > A word of caution though with function sampling, quite often a simple
> > sampling at the bin centres is not what you want, you would rather want
> > your function *integrated* over the bin range to be able to compare a
> > theory prediction with the experimental contents of a possibly wide bin.
> > Now, if somebody implements the "Sample" method for root histo objects
> > then a version which numerically integrates a function over the bin
> > range (and divides by the bin width) should perhaps also be provided. This
> > could be a very powerful tool.
> >
> 
> Stefan,
> This function already exists. It is called TH1::FillRandom
> But we could implement a member function like
>    TH1::MultyplyFunction(TFormula *f)
> to multiply the contents of an histogram by the value of a function.
> 
> Rene Brun
> 
Hi Rene,
Last night I found a solution for the problem which for me is elegant
enough and provides all flexibility.
Here follows the essential part of my code, which not only subtracts the
histos but also determines the errors.
Since this is a rather simple thing to do, I would say there is no need
for a new member function.


 siga1 = new TH1F("siga1","Signal A1",nbl,0.,2.);
 fp_fit = new TF1("fp_fit","pol0",0.3,2.);

 float err1,val1;
 float err2,val2;
 float err,val;
 float x,fx;
 for (int i=0; i<nbl; i++)
 {
  val1=cnvtlgc->GetBinContent(i+1)/nclgc;
  val2=pnvtlgc->GetBinContent(i+1)/nplgc;
  x=cnvtlgc->GetBinCenter(i+1);
  fx=fp_fit->Eval(x);
  val=val1-fx*val2;
  err1=cnvtlgc->GetBinError(i+1)/nclgc;
  err2=pnvtlgc->GetBinError(i+1)/nplgc;
  err=sqrt((err1*err1)+(err2*err2));
  siga1->Fill(x,val);
  siga1->SetBinError(i+1,err);
 }


                                                    Cheers,
                                                     Nick.


*----------------------------------------------------------------------*
 Dr. Nick van Eijndhoven                Department of Subatomic Physics
 email : nick@fys.ruu.nl                Utrecht University / NIKHEF
 tel. +31-30-2532331 (direct)           P.O. Box 80.000
 tel. +31-30-2531492 (secr.)            NL-3508 TA Utrecht
 fax. +31-30-2518689                    The Netherlands
 WWW : http://www.fys.ruu.nl/~nick      Office : Ornstein lab. 172
 ----------------------------------------------------------------------
 tel. +41-22-7679751 (direct)           CERN PPE Division / ALICE exp.
 tel. +41-22-7675857 (secr.)            CH-1211 Geneva 23
 fax. +41-22-7679480                    Switzerland
 CERN beep : 13+7294                    Office : B 160 1-012
*----------------------------------------------------------------------*



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