> 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. cheers, Stefan ---Stefan Kluth------------CERN PPE Fellow---------------------/-\/\---- - CERN PPE Division - phone: +41 22 767 2975 - \_/--\ - - CH-1211 Geneva 23 - fax: +41 22 782 1974 - |_|| - ---Switzerland-------------e-mail: stefan.kluth@cern.ch--------|--|__---
This archive was generated by hypermail 2b29 : Tue Jan 04 2000 - 00:26:19 MET