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 ? 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 archive was generated by hypermail 2b29 : Tue Jan 04 2000 - 00:26:19 MET