Re: Need some advice

Rene Brun (Rene.Brun@cern.ch)
Tue, 03 Jun 1997 18:10:35 +0200


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