]> git.uio.no Git - u/mrichter/AliRoot.git/blame - PWG2/SPECTRA/Fit/AliBWTools.cxx
Adding dummy object for HighMult specie
[u/mrichter/AliRoot.git] / PWG2 / SPECTRA / Fit / AliBWTools.cxx
CommitLineData
4c0d7bc7 1// ----------------------------------------------------------------------
2// AliBWTools
3//
4// This class provides some tools which can be useful in the analsis
5// of spectra, to fit or transform histograms. See the comments of the
6// individual methods for details
7//
8// Author: M. Floris (CERN)
9// ----------------------------------------------------------------------
10
11#include "AliBWTools.h"
12#include "TH1D.h"
13#include "TF1.h"
14#include "TH1.h"
15#include "TMath.h"
16#include "TGraphErrors.h"
17#include "TVirtualFitter.h"
18#include "TMinuit.h"
19#include "AliLog.h"
20#include <iostream>
21
22using namespace std;
23
24TF1 * AliBWTools::fFuncForNormalized = 0;
25
26ClassImp(AliBWTools)
27
28AliBWTools::AliBWTools() {
29 // ctor
30}
31
32AliBWTools::~AliBWTools(){
33 // dtor
34}
35
36TH1 * AliBWTools::GetdNdmtFromdNdpt(TH1 * hpt, Double_t mass) {
37 // convert the x axis from pt to mt. Assumes you have 1/pt dNdpt in the histo you start with
38
39 Int_t nbins = hpt->GetNbinsX();
40 Float_t * xbins = new Float_t[nbins+1];
41 for(Int_t ibins = 0; ibins <= nbins; ibins++){
42 xbins[ibins] = TMath::Sqrt(hpt->GetBinLowEdge(ibins+1)*hpt->GetBinLowEdge(ibins+1) +
43 mass *mass) - mass;
44// xbins[ibins] = TMath::Sqrt(hpt->GetBinLowEdge(ibins+1)*hpt->GetBinLowEdge(ibins+1) +
45// mass *mass);
46 // cout << ibins << " "<< xbins[ibins] << endl;
47
48 }
49
50 TH1D * hmt = new TH1D(TString(hpt->GetName())+"_mt",
51 TString(hpt->GetName())+"_mt",
52 nbins, xbins);
53 for(Int_t ibins = 1; ibins <= nbins; ibins++){
54 hmt->SetBinContent(ibins, hpt->GetBinContent(ibins));
55 hmt->SetBinError(ibins, hpt->GetBinError(ibins));
56
57 }
58
59 hmt->SetXTitle("m_{t} - m_{0} (GeV/c^{2})");
60 hmt->SetYTitle("1/m_{t} dN/dm_{t} (a.u.)");
61
62 return hmt;
63
64}
65
66TH1 * AliBWTools::GetdNdPtFromOneOverPt(TH1 * h1Pt) {
67
68 // convert an histo from 1/pt dNdpt to dNdpt, using the pt at the center of the bin
69
70
71 TH1 * hPt = (TH1 *) h1Pt->Clone((TString(h1Pt->GetName()) + "_inv").Data());
72 hPt->Reset();
73
74 Int_t nbinx = hPt->GetNbinsX();
75
76 for(Int_t ibinx = 1; ibinx <= nbinx; ibinx++){
77
78 Double_t cont = h1Pt->GetBinContent(ibinx);
79 Double_t err = h1Pt->GetBinError(ibinx);
80
81 Double_t pt = h1Pt->GetBinCenter(ibinx);
82
83 if(pt != 0) {
84 cont *= pt;
85 err *= pt;
86 } else {
87 cont = 0;
88 err = 0;
89 }
90
91 hPt->SetBinContent(ibinx, cont);
92 hPt->SetBinError(ibinx, err);
93
94 }
95
96 hPt->SetXTitle("p_{t} (GeV)");
97 hPt->SetYTitle("dN/dp_{t} (GeV^{-2})");
98
99 return hPt;
100
101}
102
103
104
105
106TH1 * AliBWTools::GetOneOverPtdNdPt(TH1 * hPt) {
107
108 // convert an histo from dNdpt to 1/pt dNdpt, using the pt at the center of the bin
109
110 TH1 * h1Pt = (TH1 *) hPt->Clone((TString(hPt->GetName()) + "_inv").Data());
111 h1Pt->Reset();
112
113 Int_t nbinx = h1Pt->GetNbinsX();
114
115 for(Int_t ibinx = 1; ibinx <= nbinx; ibinx++){
116
117 Double_t cont = hPt->GetBinContent(ibinx);
118 Double_t err = hPt->GetBinError(ibinx);
119
120 Double_t pt = hPt->GetBinCenter(ibinx);
121
122 if(pt != 0) {
123 cont /= pt;
124 err /= pt;
125 } else {
126 cont = 0;
127 err = 0;
128 }
129
130 h1Pt->SetBinContent(ibinx, cont);
131 h1Pt->SetBinError(ibinx, err);
132
133 }
134
135 h1Pt->SetXTitle("p_{t} (GeV)");
136 h1Pt->SetYTitle("1/p_{t} dN/dp_{t} (GeV^{-2})");
137
138 return h1Pt;
139
140}
141
142
143TGraphErrors * AliBWTools::GetGraphFromHisto(TH1F * h, Bool_t binWidth) {
144 // Convert a histo to a graph
145 // if binWidth is true ex is set to the bin width of the histos, otherwise it is set to zero
146 Int_t nbin = h->GetNbinsX();
147
148 TGraphErrors * g = new TGraphErrors();
149 Int_t ipoint = 0;
150 for(Int_t ibin = 1; ibin <= nbin; ibin++){
151 Double_t xerr = binWidth ? h->GetBinWidth(ibin)/2 : 0;
152 if (h->GetBinContent(ibin)!=0) {
153 g->SetPoint (ipoint, h->GetBinCenter(ibin), h->GetBinContent(ibin));
154 g->SetPointError(ipoint, xerr, h->GetBinError(ibin));
155 ipoint++;
156 }
157 }
158
159 g->SetMarkerStyle(h->GetMarkerStyle());
160 g->SetMarkerColor(h->GetMarkerColor());
161 g->SetLineColor(h->GetLineColor());
162
163 g->SetTitle(h->GetTitle());
164 g->SetName(TString("g_")+h->GetName());
165
166 return g;
167
168}
169
170TH1F * AliBWTools::GetHistoFromGraph(TGraphErrors * g, TH1F* hTemplate) {
171
172 // convert a graph to histo with the binning of hTemplate.
173 // Warning: the template should be chosen
174 // properly: if you have two graph points in the same histo bin this
175 // won't work!
176
177 TH1F * h = (TH1F*) hTemplate->Clone(TString("h_")+g->GetName());
178 h->Reset();
179 Int_t npoint = g->GetN();
180 // g->Print();
181 for(Int_t ipoint = 0; ipoint < npoint; ipoint++){
182 Float_t x = g->GetX() [ipoint];
183 Float_t y = g->GetY() [ipoint];
184 Float_t ey = g->GetEY()[ipoint];
185 Int_t bin = h->FindBin(x);
186 // cout << "bin: "<< bin << " -> " << x << ", "<< y <<", " << ey << endl;
187
188 h->SetBinContent(bin,y);
189 h->SetBinError (bin,ey);
190 }
191
192 h->SetMarkerStyle(g->GetMarkerStyle());
193 h->SetMarkerColor(g->GetMarkerColor());
194 h->SetLineColor (g->GetLineColor());
195
196
197 return h;
198}
199
200TGraphErrors * AliBWTools::ConcatenateGraphs(TGraphErrors * g1,TGraphErrors * g2){
201
202 // concatenates two graphs
203
204 Int_t npoint1=g1->GetN();
205 Int_t npoint2=g2->GetN();
206
207 TGraphErrors * gClone = (TGraphErrors*) g1->Clone();
208
209// for(Int_t ipoint = 0; ipoint < npoint1; ipoint++){
210// gClone->SetPointError(ipoint,0,g1->GetEY()[ipoint]);
211
212// }
213 for(Int_t ipoint = 0; ipoint < npoint2; ipoint++){
214 gClone->SetPoint(ipoint+npoint1,g2->GetX()[ipoint],g2->GetY()[ipoint]);
215 gClone->SetPointError(ipoint+npoint1,g2->GetEX()[ipoint],g2->GetEY()[ipoint]);
216 // gClone->SetPointError(ipoint+npoint1,0,g2->GetEY()[ipoint]);
217 }
218
219 gClone->GetHistogram()->GetXaxis()->SetTimeDisplay(1);
220 gClone->SetTitle(TString(gClone->GetTitle())+" + "+g2->GetTitle());
221 gClone->SetName(TString(gClone->GetName())+"_"+g2->GetName());
222
223 return gClone;
224}
225
226
227TH1F * AliBWTools::CombineHistos(TH1 * h1, TH1 * h2, TH1* htemplate, Float_t renorm1){
228
229 // Combine two histos. This assumes the histos have the same binning
230 // in the overlapping region. It computes the arithmetic mean in the
231 // overlapping region and assigns as an error the relative error
232 // h2. TO BE IMPROVED
233
234 TH1F * hcomb = (TH1F*) htemplate->Clone(TString(h1->GetName())+"_"+h2->GetName());
235
236 TH1F * h1local = (TH1F*) h1->Clone();
237 h1local->Scale(renorm1);
238
239 Int_t nBinComb = hcomb->GetNbinsX();
240 for(Int_t ibin = 1; ibin <= nBinComb; ibin++){
241 Int_t ibin1 = h1local->FindBin(hcomb->GetBinCenter(ibin));
242 Int_t ibin2 = h2->FindBin(hcomb->GetBinCenter(ibin));
243
244 if (h1local->GetBinContent(ibin1) == 0 && h2->GetBinContent(ibin2) == 0) {
245 // None has data: go to next bin
246 hcomb->SetBinContent(ibin,0);
247 hcomb->SetBinError (ibin,0);
248 } else if(h1local->GetBinContent(ibin1) != 0 && h2->GetBinContent(ibin2) == 0) {
249 // take data from h1local:
250 hcomb->SetBinContent(ibin,h1local->GetBinContent(ibin1));
251 hcomb->SetBinError (ibin,h1local->GetBinError(ibin1));
252 } else if(h1local->GetBinContent(ibin1) == 0 && h2->GetBinContent(ibin2) != 0) {
253 // take data from h2:
254 hcomb->SetBinContent(ibin,h2->GetBinContent(ibin2));
255 hcomb->SetBinError (ibin,h2->GetBinError(ibin2));
256 } else {
257 hcomb->SetBinContent(ibin,(h1local->GetBinContent(ibin1) +h2->GetBinContent(ibin2))/2);
258 // hcomb->SetBinError (ibin,h1local->GetBinError(ibin1)/h1local->GetBinContent(ibin1)*hcomb->GetBinContent(ibin));
259 hcomb->SetBinError (ibin,h2->GetBinError(ibin2)/h2->GetBinContent(ibin2)*hcomb->GetBinContent(ibin));
260 }
261
262
263 }
264
265
266 hcomb->SetMarkerStyle(h1local->GetMarkerStyle());
267 hcomb->SetMarkerColor(h1local->GetMarkerColor());
268 hcomb->SetLineColor (h1local->GetLineColor());
269
270 hcomb->SetXTitle(h1local->GetXaxis()->GetTitle());
271 hcomb->SetYTitle(h1local->GetYaxis()->GetTitle());
272 delete h1local;
273 return hcomb;
274
275}
276
277
278void AliBWTools::GetFromHistoGraphDifferentX(TH1F * h, TF1 * f, TGraphErrors ** gBarycentre, TGraphErrors ** gXlw) {
279
280 // Computes the baycentre in each bin and the xlw as defined in NIMA
281 // 355 - 541 using f. Returs 2 graphs with the same y content of h
282 // but with a different x (barycentre and xlw)
283
284 Int_t nbin = h->GetNbinsX();
285
286 (*gBarycentre) = new TGraphErrors();
287 (*gXlw) = new TGraphErrors();
288
289 Int_t ipoint = 0;
290 for(Int_t ibin = 1; ibin <= nbin; ibin++){
291 Float_t min = h->GetBinLowEdge(ibin);
292 Float_t max = h->GetBinLowEdge(ibin+1);
293 Double_t xerr = 0;
294 Double_t xbar = f->Mean(min,max);
295 // compute x_LW
296 Double_t temp = 1./(max-min) * f->Integral(min,max);
297 Double_t epsilon = 0.000000001;
298 Double_t increment = 0.0000000001;
299 Double_t x_lw = min;
300
301 while ((f->Eval(x_lw)- temp) > epsilon) {
302 x_lw += increment;
303 if(x_lw > max) {
304 Printf("Cannot find x_lw");
305 break;
306 }
307 }
308
309 if (h->GetBinContent(ibin)!=0) {
310 (*gBarycentre)->SetPoint (ipoint, xbar, h->GetBinContent(ibin));
311 (*gBarycentre)->SetPointError(ipoint, xerr, h->GetBinError(ibin));
312 (*gXlw) ->SetPoint (ipoint, x_lw, h->GetBinContent(ibin));
313 (*gXlw) ->SetPointError(ipoint, xerr, h->GetBinError(ibin));
314 ipoint++;
315 }
316 }
317
318 (*gBarycentre)->SetMarkerStyle(h->GetMarkerStyle());
319 (*gBarycentre)->SetMarkerColor(h->GetMarkerColor());
320 (*gBarycentre)->SetLineColor(h->GetLineColor());
321
322 (*gBarycentre)->SetTitle(h->GetTitle());
323 (*gBarycentre)->SetName(TString("g_")+h->GetName());
324
325 (*gXlw)->SetMarkerStyle(h->GetMarkerStyle());
326 (*gXlw)->SetMarkerColor(h->GetMarkerColor());
327 (*gXlw)->SetLineColor(h->GetLineColor());
328 (*gXlw)->SetTitle(h->GetTitle());
329 (*gXlw)->SetName(TString("g_")+h->GetName());
330
331
332}
333
334
335Float_t AliBWTools::GetMean(TH1F * h, Float_t min, Float_t max) {
336
337 // Get the mean of histo in a range; root is not reliable in sub ranges with variable binning.
338
339 Int_t minBin = h->FindBin(min);
340 Int_t maxBin = h->FindBin(max);
341
342 Float_t mean = 0 ;
343 Float_t integral = 0;
344 for(Int_t ibin = minBin; ibin < maxBin; ibin++){
345 mean = mean + (h->GetBinCenter(ibin) * h->GetBinWidth(ibin)* h->GetBinContent(ibin));
346 integral = integral + h->GetBinContent(ibin) * h->GetBinWidth(ibin);
347 }
348
349 return mean/integral;
350
351
352}
353
354void AliBWTools::GetMean(TF1 * func, Float_t &mean, Float_t &error, Float_t min, Float_t max) {
355
356 // Get the mean of function in a range;
357
358 return GetMoment("fMean", "x*", func, mean, error, min,max);
359
360}
361
362void AliBWTools::GetMeanSquare(TF1 * func, Float_t &mean, Float_t &error, Float_t min, Float_t max) {
363
364 // Get the mean2 of function in a range;
365
366 return GetMoment("fMean2", "x*x*", func, mean, error, min,max);
367
368
369}
370
371void AliBWTools::GetMoment(TString name, TString var, TF1 * func, Float_t &mean, Float_t &error, Float_t min, Float_t max) {
372
373 // returns the integral of a function derived from func by prefixing some operation.
374 // Used as a base method for mean and mean2
375 Printf("AliBWTools::GetMoment: Error on <pt> is not correct!!! It is overestimated, fix required");
376 Int_t npar = func->GetNpar();
377
378 TF1 * f = new TF1(name, var+func->GetName()); // FIXME
379// fFuncForNormalized = func;// TMP: computing mean pt
380// TF1 * f = new TF1(name,GetNormalizedFunc,0,10,npar);// FIXME
381// for(Int_t ipar = 0; ipar < npar; ipar++){ // FIXME
382// f->SetParameter(ipar,func->GetParameter(ipar)); // FIXME
383// }
384
385
386 // The parameter of the function used to compute the mean should be
387 // the same as the parent function: fixed if needed and they should
388 // also have the same errors.
389
390 // cout << "npar :" << npar << endl;
391
392 for(Int_t ipar = 0; ipar < npar; ipar++){
393 Double_t parmin, parmax;
394 Double_t value = func->GetParameter(ipar);
395 func->GetParLimits(ipar, parmin, parmax);
396 if ( parmin == parmax ) {
397 if ( parmin != 0 || (parmin == 1 && value == 0) ) {
398 f->FixParameter(ipar,func->GetParameter(ipar));
399 // cout << "Fixing " << ipar << "("<<value<<","<<parmin<<","<<parmax<<")"<<endl;
400 }
401 else {
402 f->SetParError (ipar,func->GetParError(ipar) );
403 // cout << "Setting Err" << ipar << "("<<func->GetParError(ipar)<<")"<<endl;
404 }
405 }
406 else {
407 f->SetParError (ipar,func->GetParError(ipar) );
408 // cout << "Setting Err" << ipar << "("<<func->GetParError(ipar)<<")"<<endl;
409 }
410 }
411 // f->Print();
412// mean = f->Integral (min,max)/func->Integral(min,max);
413// error = f->IntegralError(min,max)/func->Integral(min,max);
414 mean = f->Integral (min,max);
415 error = f->IntegralError(min,max);
416// cout << "Mean " << mean <<"+-"<< error<< endl;
417// cout << "Integral Error " << error << endl;
418
419}
420
421Double_t AliBWTools::GetNormalizedFunc(double * x, double* p){
422
423 // Static function used to provide normalized pointer to a function
424
425 Int_t npar = fFuncForNormalized->GetNpar();
426 for(Int_t ipar = 0; ipar < npar; ipar++){ // FIXME
427 fFuncForNormalized->SetParameter(ipar,p[ipar]); // FIXME
428 }
429
430 return x[0]*fFuncForNormalized->Eval(x[0])/fFuncForNormalized->Integral(0,100);
431
432}
433
434
435Bool_t AliBWTools::Fit (TH1 * h1, TF1* func, Float_t min, Float_t max) {
436
437 // Fits h1 with func, preforms several checks on the quality of the
438 // fit and tries to improve it. If the fit is not good enough, it
439 // returs false.
440
441 Double_t amin; Double_t edm; Double_t errdef; Int_t nvpar; Int_t nparx;
442 TVirtualFitter *fitter;
443 cout << "--- Fitting : " << h1->GetName() << " ["<< h1->GetTitle() <<"] ---"<< endl;
444
445 h1-> Fit(func,"IME0","",min,max);
446 Int_t fitResult = h1-> Fit(func,"IME0","",min,max);
447// h1-> Fit(func,"0","",min,max);
448// Int_t fitResult = h1-> Fit(func,"0IE","",min,max);
449
450
451// From TH1:
452// The fitStatus is 0 if the fit is OK (i.e no error occurred). The
453// value of the fit status code is negative in case of an error not
454// connected with the minimization procedure, for example when a wrong
455// function is used. Otherwise the return value is the one returned
456// from the minimization procedure. When TMinuit (default case) or
457// Minuit2 are used as minimizer the status returned is : fitStatus =
458// migradResult + 10*minosResult + 100*hesseResult +
459// 1000*improveResult. TMinuit will return 0 (for migrad, minos,
460// hesse or improve) in case of success and 4 in case of error (see
461// the documentation of TMinuit::mnexcm). So for example, for an error
462// only in Minos but not in Migrad a fitStatus of 40 will be returned.
463// Minuit2 will return also 0 in case of success and different values
464// in migrad minos or hesse depending on the error. See in this case
465// the documentation of Minuit2Minimizer::Minimize for the
466// migradResult, Minuit2Minimizer::GetMinosError for the minosResult
467// and Minuit2Minimizer::Hesse for the hesseResult. If other
468// minimizers are used see their specific documentation for the status
469// code returned. For example in the case of Fumili, for the status
470// returned see TFumili::Minimize.
471
472
473 if( gMinuit->fLimset ) {
474 Printf("ERROR: AliBWTools: Parameters at limits");
475 return kFALSE;
476 }
477
478
479 ///
480 fitter = TVirtualFitter::GetFitter();
481 Int_t fitStat = fitter->GetStats(amin, edm, errdef, nvpar, nparx);
482
483 if( ( (fitStat < 3 && gMinuit->fCstatu != "UNCHANGED ")|| (edm > 1e6) || (fitResult !=0 && fitResult < 4000) ) &&
484 TString(gMinuit->fCstatu) != "SUCCESSFUL" &&
485 TString(gMinuit->fCstatu) != "CONVERGED " ) {
486 if(fitStat < 3 && gMinuit->fCstatu != "UNCHANGED ") {
487 Printf("WARNING: AliBWTools: Cannot properly compute errors");
488 if (fitStat == 0) Printf(" not calculated at all");
489 if (fitStat == 1) Printf(" approximation only, not accurate");
490 if (fitStat == 2) Printf(" full matrix, but forced positive-definite");
491 }
492
493 if (edm > 1e6) {
494
495 Printf("WARNING: AliBWTools: Huge EDM (%f): Fit probably failed!", edm);
496 }
497 if (fitResult != 0) {
498 Printf("WARNING: AliBWTools: Fit Result (%d)", fitResult);
499 }
500
501 Printf ("AliBWTools: Trying Again with Strategy = 2");
502
503 gMinuit->Command("SET STRATEGY 2"); // more effort
504 fitResult = 0;
505 fitResult = h1-> Fit(func,"0","",min,max);
506 fitResult = h1-> Fit(func,"IME0","",min,max);
507 fitResult = h1-> Fit(func,"IME0","",min,max);
508
509 fitter = TVirtualFitter::GetFitter();
510
511 fitStat = fitter->GetStats(amin, edm, errdef, nvpar, nparx);
512
513 if(fitStat < 3 && gMinuit->fCstatu != "UNCHANGED ") {
514 Printf("ERROR: AliBWTools: Cannot properly compute errors");
515 if (fitStat == 0) Printf(" not calculated at all");
516 if (fitStat == 1) Printf(" approximation only, not accurate");
517 if (fitStat == 2) Printf(" full matrix, but forced positive-definite");
518 cout << "[" <<gMinuit->fCstatu<<"]" << endl;
519 return kFALSE;
520 }
521
522 if (edm > 1e6) {
523 Printf("ERROR: AliBWTools: Huge EDM (%f): Fit probably failed!", edm);
524
525 return kFALSE;
526 }
527 if (fitResult != 0) {
528 Printf("ERROR: AliBWTools: Fit Result (%d)", fitResult);
529
530 return kFALSE;
531 }
532
533 gMinuit->Command("SET STRATEGY 1"); // back to normal value
534
535 }
536
537 cout << "---- FIT OK ----" << endl;
538
539 return kTRUE;
540
541}
542
543Int_t AliBWTools::GetLowestNotEmptyBin(TH1*h) {
544
545 // Return the index of the lowest non empty bin in the histo h
546
547 Int_t nbin = h->GetNbinsX();
548 for(Int_t ibin = 1; ibin <= nbin; ibin++){
549 if(h->GetBinContent(ibin)>0) return ibin;
550 }
551
552 return -1;
553
554}
555
556Int_t AliBWTools::GetHighestNotEmptyBin(TH1*h) {
557
558 // Return the index of the highest non empty bin in the histo h
559
560 Int_t nbin = h->GetNbinsX();
561 for(Int_t ibin = nbin; ibin > 0; ibin--){
562 if(h->GetBinContent(ibin)>0) return ibin;
563 }
564
565 return -1;
566
567}
568
569void AliBWTools::GetResiduals(TGraphErrors * gdata, TF1 * func, TH1F ** hres, TGraphErrors ** gres) {
570
571 // Returns a graph of residuals vs point and the res/err distribution
572
573 Int_t npoint = gdata->GetN();
574
575 (*gres) =new TGraphErrors();
576 (*hres) = new TH1F(TString("hres_")+gdata->GetName()+"-"+func->GetName(),
577 TString("hres_")+gdata->GetName()+"-"+func->GetName(),
578 20,-5,5);
579
580
581 for(Int_t ipoint = 0; ipoint < npoint; ipoint++){
582 Float_t x = gdata->GetX()[ipoint];
583 Float_t res = (gdata->GetY()[ipoint] - func->Eval(x))/func->Eval(x);
584 Float_t err = gdata->GetEY()[ipoint]/func->Eval(x);
585 (*hres)->Fill(res/err);
586 (*gres)->SetPoint(ipoint, x, res/err);
587 // (*gres)->SetPointError(ipoint, 0, err);
588
589 }
590
591 (*gres)->SetMarkerStyle(gdata->GetMarkerStyle());
592 (*gres)->SetMarkerColor(gdata->GetMarkerColor());
593 (*gres)->SetLineColor (gdata->GetLineColor());
594 (*gres)->GetHistogram()->GetYaxis()->SetTitle("(data-function)/function");
595 (*hres)->SetMarkerStyle(gdata->GetMarkerStyle());
596 (*hres)->SetMarkerColor(gdata->GetMarkerColor());
597 (*hres)->SetLineColor (gdata->GetLineColor());
598
599
600
601}
602
603void AliBWTools::GetResiduals(TH1F* hdata, TF1 * func, TH1F ** hres, TH1F ** hresVsBin) {
604
605 // Returns an histo of residuals bin by bin and the res/err distribution
606
607 if (!func) {
608 Printf("AliBWTools::GetResiduals: No function provided");
609 return;
610 }
611 if (!hdata) {
612 Printf("AliBWTools::GetResiduals: No data provided");
613 return;
614 }
615
616 (*hresVsBin) = (TH1F*) hdata->Clone(TString("hres_")+hdata->GetName());
617 (*hresVsBin)->Reset();
618 (*hres) = new TH1F(TString("hres_")+hdata->GetName()+"-"+func->GetName(),
619 TString("hres_")+hdata->GetName()+"-"+func->GetName(),
620 20,-5,5);
621
622 Int_t nbin = hdata->GetNbinsX();
623 for(Int_t ibin = 1; ibin <= nbin; ibin++){
624 if(hdata->GetBinContent(ibin)==0) continue;
625 Float_t res = (hdata->GetBinContent(ibin) - func->Eval(hdata->GetBinCenter(ibin)) ) /
626 func->Eval(hdata->GetBinCenter(ibin));
627 Float_t err = hdata->GetBinError (ibin) / func->Eval(hdata->GetBinCenter(ibin));
628 (*hresVsBin)->SetBinContent(ibin,res);
629 (*hresVsBin)->SetBinError (ibin,err);
630 (*hres)->Fill(res/err);
631
632 }
633
634 (*hresVsBin)->SetMarkerStyle(hdata->GetMarkerStyle());
635 (*hresVsBin)->SetMarkerColor(hdata->GetMarkerColor());
636 (*hresVsBin)->SetLineColor (hdata->GetLineColor() );
637 (*hresVsBin)->GetYaxis()->SetTitle("(data-function)/function");
638 (*hres)->SetMarkerStyle(hdata->GetMarkerStyle());
639 (*hres)->SetMarkerColor(hdata->GetMarkerColor());
640 (*hres)->SetLineColor (hdata->GetLineColor() );
641
642}
643
644void AliBWTools::GetYield(TH1* h, TF1 * f, Double_t &yield, Double_t &yieldError, Float_t min, Float_t max,
645 Double_t *partialYields, Double_t *partialYieldsErrors){
646
647 // Returns the yield extracted from the data in the histo where
648 // there are points and from the fit to extrapolate, in the given
649 // range.
650
651 // Partial yields are also returned if the corresponding pointers are non null
652
653 Int_t bin1 = h->FindBin(min);
654 Int_t bin2 = h->FindBin(max);
655 Float_t bin1Edge = GetLowestNotEmptyBinEdge (h);
656 Float_t bin2Edge = GetHighestNotEmptyBinEdge(h);
657
658 Double_t integralFromHistoError ;
659 Double_t integralFromHisto = h->IntegralAndError(bin1,bin2,integralFromHistoError,"width");
660
661 Double_t integralBelow = min < bin1Edge ? f->Integral(min,bin1Edge) : 0;
662 Double_t integralBelowError = min < bin1Edge ? f->IntegralError(min,bin1Edge) : 0;
663 Double_t integralAbove = max > bin2Edge ? f->Integral(bin2Edge,max) : 0;
664 Double_t integralAboveError = max > bin2Edge ? f->Integral(bin2Edge,max) : 0;
665
666 cout << "GetYield INFO" << endl;
667 cout << " " << bin1Edge << " " << bin2Edge << endl;
668 cout << " " << integralFromHisto << " " << integralBelow << " " << integralAbove << endl;
669 cout << " " << integralFromHistoError << " " << integralBelowError << " " << integralAboveError << endl;
670
671 if(partialYields) {
672 partialYields[0] = integralFromHisto;
673 partialYields[1] = integralBelow;
674 partialYields[2] = integralAbove;
675 }
676 if(partialYieldsErrors) {
677 partialYieldsErrors[0] = integralFromHistoError;
678 partialYieldsErrors[1] = integralBelowError;
679 partialYieldsErrors[2] = integralAboveError;
680 }
681 yield = integralFromHisto+integralBelow+integralAbove;
682 yieldError = TMath::Sqrt(integralFromHistoError*integralFromHistoError+
683 integralBelowError*integralBelowError+
684 integralAboveError*integralAboveError);
685
686}
687
688TGraphErrors * AliBWTools::DivideGraphByFunc(TGraphErrors * g, TF1 * f, Bool_t invert){
689
690 // Divides g/f. If invert == true => f/g
691
692 TGraphErrors * gRatio = new TGraphErrors();
693 Int_t npoint = g->GetN();
694 for(Int_t ipoint = 0; ipoint < npoint; ipoint++){
695 Double_t x = g->GetX()[ipoint];
696 Double_t ratio = invert ? f->Eval(x)/g->GetY()[ipoint] :g->GetY()[ipoint]/f->Eval(x);
697 gRatio->SetPoint (ipoint, x, ratio);
698 gRatio->SetPointError(ipoint, 0, g->GetEY()[ipoint]/f->Eval(x));
699 // cout << x << " " << g->GetY()[ipoint] << " " << f->Eval(x) << endl;
700
701 }
702 gRatio->SetMarkerStyle(20);
703 //gRatio->Print();
704 return gRatio;
705
706}
707
708TGraphErrors * AliBWTools::DivideGraphByHisto(TGraphErrors * g, TH1 * h, Bool_t invert){
709
710 // Divides g/h. If invert == true => h/g
711
712
713 TGraphErrors * gRatio = new TGraphErrors();
714 Int_t npoint = g->GetN();
715 for(Int_t ipoint = 0; ipoint < npoint; ipoint++){
716 Double_t xj = g->GetX()[ipoint];
717 Double_t yj = g->GetY()[ipoint];
718 Double_t yje = g->GetEY()[ipoint];
719
720 Int_t binData = h->FindBin(xj);
721 Double_t yd = h->GetBinContent(binData);
722 Double_t yde = h->GetBinError(binData);
723 Double_t xd = h->GetBinCenter(binData);
724
725 // cout << TMath::Abs((xd-xj)/xd) << endl;
726
727
728
729 if (yd == 0) continue;
730 // if (binData == 28 ) cout << TMath::Abs(xd-xj)/TMath::Abs(xd) << endl;
731
732 if (TMath::Abs(xd-xj)/TMath::Abs(xd) > 0.01){
733 Printf( "WARNING: bin center (%f) and x graph (%f) are more than 1 %% away, skipping",xd,xj );
734 continue;
735
736 }
737
738 Double_t ratio = invert ? yd/yj : yj/yd;
739
740 gRatio->SetPoint(ipoint, xj, ratio);
741 gRatio->SetPointError(ipoint, 0, TMath::Sqrt(yde*yde/yd/yd + yje*yje/yj/yj)*ratio);
742 // gRatio->SetPointError(ipoint, 0, yje/yj * ratio);
743 }
744
745 return gRatio;
746
747
748}
749
750TH1F * AliBWTools::DivideHistoByFunc(TH1F * h, TF1 * f, Bool_t invert){
751
752 // Divides h/f. If invert == true => f/g
753 // Performs the integral of f on the bin range to perform the ratio
754 // Returns a histo with the same binnig as h
755
756 // Prepare histo for ratio
757 TH1F * hRatio = (TH1F*) h->Clone(TString("hRatio_")+h->GetName()+"_"+f->GetName());
758 hRatio->Reset();
759 // Set y title
760 if(!invert) hRatio->SetYTitle(TString(h->GetName())+"/"+f->GetName());
761 else hRatio->SetYTitle(TString(f->GetName())+"/"+h->GetName());
762
763 // Loop over all bins
764 Int_t nbin = hRatio->GetNbinsX();
765
766 for(Int_t ibin = 1; ibin <= nbin; ibin++){
767 Double_t yhisto = h->GetBinContent(ibin);
768 Double_t yerror = h->GetBinError(ibin);
769 Double_t xmin = h->GetBinLowEdge(ibin);
770 Double_t xmax = h->GetBinLowEdge(ibin+1);
771 Double_t yfunc = f->Integral(xmin,xmax)/(xmax-xmin);
772 Double_t ratio = invert ? yfunc/yhisto : yhisto/yfunc ;
773 Double_t error = yerror/yfunc ;
774 hRatio->SetBinContent(ibin,ratio);
775 hRatio->SetBinError (ibin,error);
776 }
777
778 return hRatio;
779
780}