1 // ----------------------------------------------------------------------
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
8 // Author: M. Floris (CERN)
9 // ----------------------------------------------------------------------
11 #include "AliPWGHistoTools.h"
16 #include "TGraphErrors.h"
17 #include "TVirtualFitter.h"
25 ClassImp(AliPWGHistoTools)
27 TF1 * AliPWGHistoTools::fdNdptForETCalc = 0;
29 AliPWGHistoTools::AliPWGHistoTools() {
33 AliPWGHistoTools::~AliPWGHistoTools(){
37 TH1 * AliPWGHistoTools::GetdNdmtFromdNdpt(const TH1 * hpt, Double_t mass) {
38 // convert the x axis from pt to mt. Assumes you have 1/pt dNdpt in the histo you start with
40 Int_t nbins = hpt->GetNbinsX();
41 Float_t * xbins = new Float_t[nbins+1];
42 for(Int_t ibins = 0; ibins <= nbins; ibins++){
43 xbins[ibins] = TMath::Sqrt(hpt->GetBinLowEdge(ibins+1)*hpt->GetBinLowEdge(ibins+1) +
45 // // xbins[ibins] = TMath::Sqrt(hpt->GetBinLowEdge(ibins+1)*hpt->GetBinLowEdge(ibins+1) +
47 // cout << ibins << " "<< xbins[ibins] << endl;
51 TH1D * hmt = new TH1D(TString(hpt->GetName())+"_mt",
52 TString(hpt->GetName())+"_mt",
54 for(Int_t ibins = 1; ibins <= nbins; ibins++){
55 hmt->SetBinContent(ibins, hpt->GetBinContent(ibins));
56 hmt->SetBinError(ibins, hpt->GetBinError(ibins));
60 hmt->SetXTitle("m_{t} - m_{0} (GeV/c^{2})");
61 hmt->SetYTitle("1/m_{t} dN/dm_{t} (a.u.)");
62 hmt->SetMarkerStyle(hpt->GetMarkerStyle());
63 hmt->SetMarkerColor(hpt->GetMarkerColor());
64 hmt->SetLineColor(hpt->GetLineColor());
70 TH1 * AliPWGHistoTools::GetdNdptFromdNdmt(const TH1 * hmt, Double_t mass) {
71 // convert the x axis from mt to pt. Assumes you have 1/mt dNdmt in the histo you start with
73 Int_t nbins = hmt->GetNbinsX();
74 Float_t * xbins = new Float_t[nbins+1];
75 for(Int_t ibins = 0; ibins <= nbins; ibins++){
76 xbins[ibins] = TMath::Sqrt((hmt->GetBinLowEdge(ibins+1)+mass)*(hmt->GetBinLowEdge(ibins+1)+mass) -
78 xbins[ibins] = Float_t(TMath::Nint(xbins[ibins]*100))/100;
79 // // xbins[ibins] = TMath::Sqrt(hmt->GetBinLowEdge(ibins+1)*hmt->GetBinLowEdge(ibins+1) +
81 cout << ibins << " "<< xbins[ibins] << endl;
85 TH1D * hptL = new TH1D(TString(hmt->GetName())+"_pt",
86 TString(hmt->GetName())+"_pt",
88 for(Int_t ibins = 1; ibins <= nbins; ibins++){
89 hptL->SetBinContent(ibins, hmt->GetBinContent(ibins));
90 hptL->SetBinError(ibins, hmt->GetBinError(ibins));
94 hptL->SetXTitle("p_{t} (GeV/c)");
95 hptL->SetYTitle("1/p_{t} dN/dp_{t} (a.u.)");
96 hptL->SetMarkerStyle(hmt->GetMarkerStyle());
97 hptL->SetMarkerColor(hmt->GetMarkerColor());
98 hptL->SetLineColor(hmt->GetLineColor());
105 TH1 * AliPWGHistoTools::GetdNdPtFromOneOverPt(const TH1 * h1Pt) {
107 // convert an histo from 1/pt dNdpt to dNdpt, using the pt at the center of the bin
110 TH1 * hPt = (TH1 *) h1Pt->Clone((TString(h1Pt->GetName()) + "_inv").Data());
113 Int_t nbinx = hPt->GetNbinsX();
115 for(Int_t ibinx = 1; ibinx <= nbinx; ibinx++){
117 Double_t cont = h1Pt->GetBinContent(ibinx);
118 Double_t err = h1Pt->GetBinError(ibinx);
120 Double_t pt = h1Pt->GetBinCenter(ibinx);
130 hPt->SetBinContent(ibinx, cont);
131 hPt->SetBinError(ibinx, err);
135 hPt->SetXTitle("p_{t} (GeV)");
136 hPt->SetYTitle("dN/dp_{t} (GeV^{-2})");
145 TH1 * AliPWGHistoTools::GetOneOverPtdNdPt(const TH1 * hPt) {
147 // convert an histo from dNdpt to 1/pt dNdpt, using the pt at the center of the bin
149 TH1 * h1Pt = (TH1 *) hPt->Clone((TString(hPt->GetName()) + "_inv").Data());
152 Int_t nbinx = h1Pt->GetNbinsX();
154 for(Int_t ibinx = 1; ibinx <= nbinx; ibinx++){
156 Double_t cont = hPt->GetBinContent(ibinx);
157 Double_t err = hPt->GetBinError(ibinx);
159 Double_t pt = hPt->GetBinCenter(ibinx);
169 h1Pt->SetBinContent(ibinx, cont);
170 h1Pt->SetBinError(ibinx, err);
174 h1Pt->SetXTitle("p_{t} (GeV)");
175 h1Pt->SetYTitle("1/p_{t} dN/dp_{t} (GeV^{-2})");
182 TGraphErrors * AliPWGHistoTools::GetGraphFromHisto(const TH1F * h, Bool_t binWidth) {
183 // Convert a histo to a graph
184 // if binWidth is true ex is set to the bin width of the histos, otherwise it is set to zero
185 Int_t nbin = h->GetNbinsX();
187 TGraphErrors * g = new TGraphErrors();
189 for(Int_t ibin = 1; ibin <= nbin; ibin++){
190 Double_t xerr = binWidth ? h->GetBinWidth(ibin)/2 : 0;
191 if (h->GetBinContent(ibin)) {
192 g->SetPoint (ipoint, h->GetBinCenter(ibin), h->GetBinContent(ibin));
193 g->SetPointError(ipoint, xerr, h->GetBinError(ibin));
198 g->SetMarkerStyle(h->GetMarkerStyle());
199 g->SetMarkerColor(h->GetMarkerColor());
200 g->SetLineColor(h->GetLineColor());
201 g->SetLineStyle(h->GetLineStyle());
202 g->SetLineWidth(h->GetLineWidth());
204 g->SetTitle(h->GetTitle());
205 g->SetName(TString("g_")+h->GetName());
211 TH1F * AliPWGHistoTools::GetHistoFromGraph(const TGraphErrors * g, const TH1F* hTemplate) {
213 // convert a graph to histo with the binning of hTemplate.
214 // Warning: the template should be chosen
215 // properly: if you have two graph points in the same histo bin this
218 TH1F * h = (TH1F*) hTemplate->Clone(TString("h_")+g->GetName());
220 Int_t npoint = g->GetN();
222 for(Int_t ipoint = 0; ipoint < npoint; ipoint++){
223 Float_t x = g->GetX() [ipoint];
224 Float_t y = g->GetY() [ipoint];
226 if(g->InheritsFrom("TGraphErrors"))
227 ey = g->GetEY()[ipoint];
228 Int_t bin = h->FindBin(x);
229 // cout << "bin: "<< bin << " -> " << x << ", "<< y <<", " << ey << endl;
231 h->SetBinContent(bin,y);
232 h->SetBinError (bin,ey);
235 h->SetMarkerStyle(g->GetMarkerStyle());
236 h->SetMarkerColor(g->GetMarkerColor());
237 h->SetLineColor (g->GetLineColor());
243 TGraphErrors * AliPWGHistoTools::ConcatenateGraphs(const TGraphErrors * g1,const TGraphErrors * g2){
245 // concatenates two graphs
247 Int_t npoint1=g1->GetN();
248 Int_t npoint2=g2->GetN();
250 TGraphErrors * gClone = (TGraphErrors*) g1->Clone();
252 // for(Int_t ipoint = 0; ipoint < npoint1; ipoint++){
253 // gClone->SetPointError(ipoint,0,g1->GetEY()[ipoint]);
256 for(Int_t ipoint = 0; ipoint < npoint2; ipoint++){
257 gClone->SetPoint(ipoint+npoint1,g2->GetX()[ipoint],g2->GetY()[ipoint]);
258 gClone->SetPointError(ipoint+npoint1,g2->GetEX()[ipoint],g2->GetEY()[ipoint]);
259 // gClone->SetPointError(ipoint+npoint1,0,g2->GetEY()[ipoint]);
262 gClone->GetHistogram()->GetXaxis()->SetTimeDisplay(1);
263 gClone->SetTitle(TString(gClone->GetTitle())+" + "+g2->GetTitle());
264 gClone->SetName(TString(gClone->GetName())+"_"+g2->GetName());
270 TH1F * AliPWGHistoTools::Combine3HistosWithErrors(const TH1 * h1, const TH1 * h2, const TH1* h3,
271 TH1 * he1, TH1 * he2, TH1 * he3,
272 const TH1* htemplate, Int_t statFrom,
273 Float_t renorm1, Float_t renorm2, Float_t renorm3,
274 TH1 ** hSyst, Bool_t errorFromBinContent) {
276 // Combines 3 histos (h1,h2,h3), weighting by the errors provided in
277 // he1,he2,he3, supposed to be the independent systematic errors.
278 // he1,he2,he3 are also assumed to have the same binning as h1,h2,h3
279 // The combined histo must fit the template provided (no check is performed on this)
280 // The histogram are supposed to come from the same (nearly) sample
281 // of tracks. statFrom tells the stat error of which of the 3 is
282 // going to be assigned to the combined
283 // Optionally, it is possible to rescale any of the histograms.
284 // if hSyst is give, the histo is filled with combined syst error vs pt
285 // if errorFromBinContent is true, the weights are taken from the he* content rather than errors
286 TH1F * hcomb = (TH1F*) htemplate->Clone(TString("hComb_")+h1->GetName()+"_"+h2->GetName()+h3->GetName());
288 // TODO: I should have used an array for h*local...
290 // Clone histos locally to rescale them
291 TH1F * h1local = (TH1F*) h1->Clone();
292 TH1F * h2local = (TH1F*) h2->Clone();
293 TH1F * h3local = (TH1F*) h3->Clone();
294 h1local->Scale(renorm1);
295 h2local->Scale(renorm2);
296 h3local->Scale(renorm3);
298 const TH1 * hStatError = 0;
299 if (statFrom == 0) hStatError = h1;
300 else if (statFrom == 1) hStatError = h2;
301 else if (statFrom == 2) hStatError = h3;
303 Printf("AliPWGHistoTools::Combine3HistosWithErrors: wrong value of the statFrom parameter");
306 Printf("AliPWGHistoTools::Combine3HistosWithErrors: improve error on combined");
307 // Loop over all bins and take weighted mean of all points
308 Int_t nBinComb = hcomb->GetNbinsX();
309 for(Int_t ibin = 1; ibin <= nBinComb; ibin++){
310 Int_t ibin1 = h1local->FindBin(hcomb->GetBinCenter(ibin));
311 Int_t ibin2 = h2local->FindBin(hcomb->GetBinCenter(ibin));
312 Int_t ibin3 = h3local->FindBin(hcomb->GetBinCenter(ibin));
313 Int_t ibinError = -1; // bin used to get stat error
315 if (statFrom == 0) ibinError = ibin1;
316 else if (statFrom == 1) ibinError = ibin2;
317 else if (statFrom == 2) ibinError = ibin3;
318 else Printf("AliPWGHistoTools::Combine3HistosWithErrors: wrong value of the statFrom parameter");
323 y[0] = h1local->GetBinContent(ibin1);
324 y[1] = h2local->GetBinContent(ibin2);
325 y[2] = h3local->GetBinContent(ibin3);
326 if (errorFromBinContent) {
327 ye[0] = he1->GetBinContent(he1->FindBin(hcomb->GetBinCenter(ibin)));
328 ye[1] = he2->GetBinContent(he2->FindBin(hcomb->GetBinCenter(ibin)));
329 ye[2] = he3->GetBinContent(he3->FindBin(hcomb->GetBinCenter(ibin)));
331 ye[0] = he1->GetBinError(ibin1);
332 ye[1] = he2->GetBinError(ibin2);
333 ye[2] = he3->GetBinError(ibin3);
335 // Set error to 0 if content is 0 (means it was not filled)
336 if(!h1local->GetBinContent(ibin1)) ye[0] = 0;
337 if(!h2local->GetBinContent(ibin2)) ye[1] = 0;
338 if(!h3local->GetBinContent(ibin3)) ye[2] = 0;
340 // Compute weighted mean
341 // cout << "Bins: "<< ibin1 << " " << ibin2 << " " << ibin3 << endl;
343 WeightedMean(3,y,ye,mean,err);
347 hcomb->SetBinContent(ibin,mean);
348 Double_t statError = 0;
349 if (hStatError->GetBinContent(ibinError)) {
350 // cout << "def" << endl;
351 statError = hStatError->GetBinError(ibinError)/hStatError->GetBinContent(ibinError)*hcomb->GetBinContent(ibin);
353 else if (h1local->GetBinContent(ibin1)) {
354 // cout << "1" << endl;
355 statError = h1local->GetBinError(ibin1)/h1local->GetBinContent(ibin1)*hcomb->GetBinContent(ibin);
357 else if (h2local->GetBinContent(ibin2)) {
358 // cout << "2" << endl;
359 statError = h2local->GetBinError(ibin2)/h2local->GetBinContent(ibin2)*hcomb->GetBinContent(ibin);
361 else if (h3local->GetBinContent(ibin3)) {
362 // cout << "3" << endl;
363 statError = h3local->GetBinError(ibin3)/h3local->GetBinContent(ibin3)*hcomb->GetBinContent(ibin);
365 hcomb->SetBinError (ibin,statError);
366 if(hSyst) (*hSyst)->SetBinContent(ibin,err);
367 // cout << "BIN " << ibin << " " << mean << " " << statError << endl;
371 hcomb->SetMarkerStyle(hStatError->GetMarkerStyle());
372 hcomb->SetMarkerColor(hStatError->GetMarkerColor());
373 hcomb->SetLineColor (hStatError->GetLineColor());
375 hcomb->SetXTitle(hStatError->GetXaxis()->GetTitle());
376 hcomb->SetYTitle(hStatError->GetYaxis()->GetTitle());
386 void AliPWGHistoTools::GetMeanDataAndExtrapolation(const TH1 * hData, TF1 * fExtrapolation, Double_t &mean, Double_t &error, Float_t min, Float_t max){
387 // Computes the mean of the combined data and extrapolation in a
388 // given range, use data where they are available and the function
389 // where data are not available
390 // ERROR from DATA ONLY is returned in this version!
392 Printf("AliPWGHistoTools::GetMeanDataAndExtrapolation: WARNING from data only");
393 Float_t minData = GetLowestNotEmptyBinEdge (hData);
394 Int_t minDataBin = GetLowestNotEmptyBin (hData);
395 Float_t maxData = GetHighestNotEmptyBinEdge(hData);
396 Int_t maxDataBin = GetHighestNotEmptyBin (hData);
397 Double_t integral = 0;
401 // Compute integral exploiting root function to calculate moments, "unnormalizing" them
402 mean += fExtrapolation->Mean(min,minData)*fExtrapolation->Integral(min,minData);
403 integral += fExtrapolation->Integral(min,minData);
404 cout << " Low "<< mean << " " << integral << endl;
409 // Compute integral exploiting root function to calculate moments, "unnormalizing" them
410 mean += fExtrapolation->Mean(maxData,max)*fExtrapolation->Integral(maxData,max);
411 integral += fExtrapolation->Integral(maxData,max);
412 cout << " Hi "<< mean << " " << integral << endl;
416 for(Int_t ibin = minDataBin; ibin <= maxDataBin; ibin++){
417 if(hData->GetBinCenter(ibin) < min) continue;
418 if(hData->GetBinCenter(ibin) > max) continue;
419 mean = mean + (hData->GetBinCenter(ibin) * hData->GetBinWidth(ibin)* hData->GetBinContent(ibin));
420 err2 = err2 + TMath::Power(hData->GetBinError(ibin) * hData->GetBinCenter(ibin) * hData->GetBinWidth(ibin),2);
421 integral = integral + hData->GetBinContent(ibin) * hData->GetBinWidth(ibin);
423 cout << " Data "<< mean << " " << integral << endl;
425 mean = mean/integral;
426 error = TMath::Sqrt(err2)/integral;
431 TH1F * AliPWGHistoTools::CombineHistos(const TH1 * h1, TH1 * h2, const TH1* htemplate, Float_t renorm1){
432 // Combine two histos. This assumes the histos have the same binning
433 // in the overlapping region. It computes the arithmetic mean in the
434 // overlapping region and assigns as an error the relative error
435 // h2. TO BE IMPROVED
437 TH1F * hcomb = (TH1F*) htemplate->Clone(TString(h1->GetName())+"_"+h2->GetName());
439 TH1F * h1local = (TH1F*) h1->Clone();
440 h1local->Scale(renorm1);
442 Int_t nBinComb = hcomb->GetNbinsX();
443 for(Int_t ibin = 1; ibin <= nBinComb; ibin++){
444 Int_t ibin1 = h1local->FindBin(hcomb->GetBinCenter(ibin));
445 Int_t ibin2 = h2->FindBin(hcomb->GetBinCenter(ibin));
447 if (!h1local->GetBinContent(ibin1) && !h2->GetBinContent(ibin2) ) {
448 // None has data: go to next bin
449 hcomb->SetBinContent(ibin,0);
450 hcomb->SetBinError (ibin,0);
451 } else if(h1local->GetBinContent(ibin1) && !h2->GetBinContent(ibin2)) {
452 // take data from h1local:
453 hcomb->SetBinContent(ibin,h1local->GetBinContent(ibin1));
454 hcomb->SetBinError (ibin,h1local->GetBinError(ibin1));
455 } else if(!h1local->GetBinContent(ibin1) && h2->GetBinContent(ibin2)) {
456 // take data from h2:
457 hcomb->SetBinContent(ibin,h2->GetBinContent(ibin2));
458 hcomb->SetBinError (ibin,h2->GetBinError(ibin2));
460 hcomb->SetBinContent(ibin,(h1local->GetBinContent(ibin1) +h2->GetBinContent(ibin2))/2);
461 // hcomb->SetBinError (ibin,h1local->GetBinError(ibin1)/h1local->GetBinContent(ibin1)*hcomb->GetBinContent(ibin));
462 hcomb->SetBinError (ibin,h2->GetBinError(ibin2)/h2->GetBinContent(ibin2)*hcomb->GetBinContent(ibin));
469 hcomb->SetMarkerStyle(h1local->GetMarkerStyle());
470 hcomb->SetMarkerColor(h1local->GetMarkerColor());
471 hcomb->SetLineColor (h1local->GetLineColor());
473 hcomb->SetXTitle(h1local->GetXaxis()->GetTitle());
474 hcomb->SetYTitle(h1local->GetYaxis()->GetTitle());
481 void AliPWGHistoTools::GetFromHistoGraphDifferentX(const TH1F * h, TF1 * f, TGraphErrors ** gBarycentre, TGraphErrors ** gXlw) {
483 // Computes the baycentre in each bin and the xlw as defined in NIMA
484 // 355 - 541 using f. Returs 2 graphs with the same y content of h
485 // but with a different x (barycentre and xlw)
487 Int_t nbin = h->GetNbinsX();
489 (*gBarycentre) = new TGraphErrors();
490 (*gXlw) = new TGraphErrors();
493 for(Int_t ibin = 1; ibin <= nbin; ibin++){
494 Float_t min = h->GetBinLowEdge(ibin);
495 Float_t max = h->GetBinLowEdge(ibin+1);
497 Double_t xbar = f->Mean(min,max);
499 Double_t temp = 1./(max-min) * f->Integral(min,max);
500 Double_t epsilon = 0.000000001;
501 Double_t increment = 0.0000000001;
504 while ((f->Eval(xLW)- temp) > epsilon) {
507 Printf("Cannot find xLW");
512 if (h->GetBinContent(ibin)!=0) {
513 (*gBarycentre)->SetPoint (ipoint, xbar, h->GetBinContent(ibin));
514 (*gBarycentre)->SetPointError(ipoint, xerr, h->GetBinError(ibin));
515 (*gXlw) ->SetPoint (ipoint, xLW, h->GetBinContent(ibin));
516 (*gXlw) ->SetPointError(ipoint, xerr, h->GetBinError(ibin));
521 (*gBarycentre)->SetMarkerStyle(h->GetMarkerStyle());
522 (*gBarycentre)->SetMarkerColor(h->GetMarkerColor());
523 (*gBarycentre)->SetLineColor(h->GetLineColor());
525 (*gBarycentre)->SetTitle(h->GetTitle());
526 (*gBarycentre)->SetName(TString("g_")+h->GetName());
528 (*gXlw)->SetMarkerStyle(h->GetMarkerStyle());
529 (*gXlw)->SetMarkerColor(h->GetMarkerColor());
530 (*gXlw)->SetLineColor(h->GetLineColor());
531 (*gXlw)->SetTitle(h->GetTitle());
532 (*gXlw)->SetName(TString("g_")+h->GetName());
538 Float_t AliPWGHistoTools::GetMean(TH1F * h, Float_t min, Float_t max, Float_t * error) {
540 // Get the mean of histo in a range; root is not reliable in sub
541 // ranges with variable binning.
542 Int_t minBin = h->FindBin(min);
543 Int_t maxBin = h->FindBin(max-0.00001);
546 Float_t integral = 0;
548 for(Int_t ibin = minBin; ibin <= maxBin; ibin++){
549 mean = mean + (h->GetBinCenter(ibin) * h->GetBinWidth(ibin)* h->GetBinContent(ibin));
550 err2 = err2 + TMath::Power(h->GetBinError(ibin) * h->GetBinCenter(ibin) * h->GetBinWidth(ibin),2);
551 integral = integral + h->GetBinContent(ibin) * h->GetBinWidth(ibin);
554 Float_t value = mean/integral;
555 if (error) (*error) = TMath::Sqrt(err2);
561 void AliPWGHistoTools::GetMean(TF1 * func, Float_t &mean, Float_t &error, Float_t min, Float_t max, Int_t normPar) {
563 // Get the mean of function in a range; If normPar is >= 0, it means
564 // that the function is defined such that par[normPar] is its
565 // integral. In this case the error on meanpt can be calculated
566 // correctly. Otherwise, the function is normalized in get moment,
567 // but the error is not computed correctly.
569 return GetMoment("fMean", TString("x*")+func->GetExpFormula(), func, mean, error, min, max, normPar);
573 void AliPWGHistoTools::GetMeanSquare(TF1 * func, Float_t &mean, Float_t &error, Float_t min, Float_t max, Int_t normPar) {
575 // Get the mean2 of function in a range; If normPar is >= 0, it means
576 // that the function is defined such that par[normPar] is its
577 // integral. In this case the error on meanpt can be calculated
578 // correctly. Otherwise, the function is normalized in get moment,
579 // but the error is not computed correctly.
581 return GetMoment("fMean2", TString("x*x*")+func->GetExpFormula(), func, mean, error, min, max, normPar);
586 void AliPWGHistoTools::GetMoment(TString name, TString var, TF1 * func, Float_t &mean, Float_t &error, Float_t min, Float_t max, Int_t normPar) {
588 // returns the integral of a function derived from func by prefixing some operation.
589 // the derived function MUST have the same parameter in the same order
590 // Used as a base method for mean and mean2
591 // If normPar is >= 0, it means that the function is defined such
592 // that par[normPar] is its integral. In this case the error on
593 // meanpt can be calculated correctly. Otherwise, the function is
594 // normalized using its numerical integral, but the error is not computed
598 // - improve to propagate error also in the case you need the
599 // numerical integrals (will be rather slow)
600 // - this version assumes that func is defined using a
601 // TFormula. Generalize to the case of a C++ function
603 if (normPar<0) Printf("AliPWGHistoTools::GetMoment: Warning: If normalization is required, the error may bot be correct");
604 if (!strcmp(func->GetExpFormula(),"")) Printf("AliPWGHistoTools::GetMoment: Warning: Empty formula in the base function");
605 Int_t npar = func->GetNpar();
607 // Definition changes according to the value of normPar
608 TF1 * f = normPar < 0 ?
609 new TF1(name, var) : // not normalized
610 new TF1(name, var+Form("/[%d]",normPar)); // normalized with par normPar
612 // integr is used to normalize if no parameter is provided
613 Double_t integr = normPar < 0 ? func->Integral(min,max) : 1;
615 // The parameter of the function used to compute the mean should be
616 // the same as the parent function: fixed if needed and they should
617 // also have the same errors.
619 // cout << "npar :" << npar << endl;
621 for(Int_t ipar = 0; ipar < npar; ipar++){
622 Double_t parmin, parmax;
623 Double_t value = func->GetParameter(ipar);
624 f->SetParameter(ipar,value);
625 func->GetParLimits(ipar, parmin, parmax);
626 if ( parmin == parmax ) {
627 // if ( parmin || (parmin == 1 && !value) ) { // not sure we I check parmin == 1 here.
628 if ( parmin || (TMath::Abs(parmin-1)<0.000001 && !value) ) { // not sure we I check parmin == 1 here. Changed like this because of coding conventions. Does it still work? FIXME
629 f->FixParameter(ipar,func->GetParameter(ipar));
630 // cout << "Fixing " << ipar << "("<<value<<","<<parmin<<","<<parmax<<")"<<endl;
633 f->SetParError (ipar,func->GetParError(ipar) );
634 // cout << "Setting Err" << ipar << "("<<func->GetParError(ipar)<<")"<<endl;
638 f->SetParError (ipar,func->GetParError(ipar) );
639 // cout << "Setting Err" << ipar << "("<<func->GetParError(ipar)<<")"<<endl;
644 // cout << "----" << endl;
647 mean = normPar < 0 ? f->Integral (min,max)/integr : f->Integral (min,max);
648 error = normPar < 0 ? f->IntegralError(min,max)/integr : f->IntegralError(min,max);
649 // cout << "Mean " << mean <<"+-"<< error<< endl;
650 // cout << "Integral Error " << error << endl;
656 Bool_t AliPWGHistoTools::Fit (TH1 * h1, TF1* func, Float_t min, Float_t max) {
658 // Fits h1 with func, preforms several checks on the quality of the
659 // fit and tries to improve it. If the fit is not good enough, it
662 Double_t amin; Double_t edm; Double_t errdef; Int_t nvpar; Int_t nparx;
663 TVirtualFitter *fitter;
664 cout << "--- Fitting : " << h1->GetName() << " ["<< h1->GetTitle() <<"] ---"<< endl;
666 h1-> Fit(func,"IME0","",min,max);
667 Int_t fitResult = h1-> Fit(func,"IME0","",min,max);
668 // h1-> Fit(func,"0","",min,max);
669 // Int_t fitResult = h1-> Fit(func,"0IE","",min,max);
673 // The fitStatus is 0 if the fit is OK (i.e no error occurred). The
674 // value of the fit status code is negative in case of an error not
675 // connected with the minimization procedure, for example when a wrong
676 // function is used. Otherwise the return value is the one returned
677 // from the minimization procedure. When TMinuit (default case) or
678 // Minuit2 are used as minimizer the status returned is : fitStatus =
679 // migradResult + 10*minosResult + 100*hesseResult +
680 // 1000*improveResult. TMinuit will return 0 (for migrad, minos,
681 // hesse or improve) in case of success and 4 in case of error (see
682 // the documentation of TMinuit::mnexcm). So for example, for an error
683 // only in Minos but not in Migrad a fitStatus of 40 will be returned.
684 // Minuit2 will return also 0 in case of success and different values
685 // in migrad minos or hesse depending on the error. See in this case
686 // the documentation of Minuit2Minimizer::Minimize for the
687 // migradResult, Minuit2Minimizer::GetMinosError for the minosResult
688 // and Minuit2Minimizer::Hesse for the hesseResult. If other
689 // minimizers are used see their specific documentation for the status
690 // code returned. For example in the case of Fumili, for the status
691 // returned see TFumili::Minimize.
694 if( gMinuit->fLimset ) {
695 Printf("ERROR: AliPWGHistoTools: Parameters at limits");
701 fitter = TVirtualFitter::GetFitter();
702 Int_t fitStat = fitter->GetStats(amin, edm, errdef, nvpar, nparx);
704 if( ( (fitStat < 3 && gMinuit->fCstatu != "UNCHANGED ")|| (edm > 1e6) || (fitResult !=0 && fitResult < 4000) ) &&
705 TString(gMinuit->fCstatu) != "SUCCESSFUL" &&
706 TString(gMinuit->fCstatu) != "CONVERGED " ) {
707 if(fitStat < 3 && gMinuit->fCstatu != "UNCHANGED ") {
708 Printf("WARNING: AliPWGHistoTools: Cannot properly compute errors");
709 if (fitStat == 0) Printf(" not calculated at all");
710 if (fitStat == 1) Printf(" approximation only, not accurate");
711 if (fitStat == 2) Printf(" full matrix, but forced positive-definite");
716 Printf("WARNING: AliPWGHistoTools: Huge EDM (%f): Fit probably failed!", edm);
718 if (fitResult != 0) {
719 Printf("WARNING: AliPWGHistoTools: Fit Result (%d)", fitResult);
722 Printf ("AliPWGHistoTools: Trying Again with Strategy = 2");
724 gMinuit->Command("SET STRATEGY 2"); // more effort
726 fitResult = h1-> Fit(func,"0","",min,max);
727 fitResult = h1-> Fit(func,"IME0","",min,max);
728 fitResult = h1-> Fit(func,"IME0","",min,max);
730 fitter = TVirtualFitter::GetFitter();
732 fitStat = fitter->GetStats(amin, edm, errdef, nvpar, nparx);
734 if(fitStat < 3 && gMinuit->fCstatu != "UNCHANGED ") {
735 Printf("ERROR: AliPWGHistoTools: Cannot properly compute errors");
736 if (fitStat == 0) Printf(" not calculated at all");
737 if (fitStat == 1) Printf(" approximation only, not accurate");
738 if (fitStat == 2) Printf(" full matrix, but forced positive-definite");
739 cout << "[" <<gMinuit->fCstatu<<"]" << endl;
744 Printf("ERROR: AliPWGHistoTools: Huge EDM (%f): Fit probably failed!", edm);
748 if (fitResult != 0) {
749 Printf("ERROR: AliPWGHistoTools: Fit Result (%d)", fitResult);
754 gMinuit->Command("SET STRATEGY 1"); // back to normal value
758 cout << "---- FIT OK ----" << endl;
764 Int_t AliPWGHistoTools::GetLowestNotEmptyBin(const TH1*h) {
766 // Return the index of the lowest non empty bin in the histo h
768 Int_t nbin = h->GetNbinsX();
769 for(Int_t ibin = 1; ibin <= nbin; ibin++){
770 if(h->GetBinContent(ibin)>0) return ibin;
777 Int_t AliPWGHistoTools::GetHighestNotEmptyBin(const TH1*h) {
779 // Return the index of the highest non empty bin in the histo h
781 Int_t nbin = h->GetNbinsX();
782 for(Int_t ibin = nbin; ibin > 0; ibin--){
783 if(h->GetBinContent(ibin)>0) return ibin;
790 void AliPWGHistoTools::GetResiduals(const TGraphErrors * gdata, const TF1 * func, TH1F ** hres, TGraphErrors ** gres) {
792 // Returns a graph of residuals vs point and the res/err distribution
794 Int_t npoint = gdata->GetN();
796 (*gres) =new TGraphErrors();
797 (*hres) = new TH1F(TString("hres_")+gdata->GetName()+"-"+func->GetName(),
798 TString("hres_")+gdata->GetName()+"-"+func->GetName(),
802 for(Int_t ipoint = 0; ipoint < npoint; ipoint++){
803 Float_t x = gdata->GetX()[ipoint];
804 Float_t res = (gdata->GetY()[ipoint] - func->Eval(x))/func->Eval(x);
805 Float_t err = gdata->GetEY()[ipoint]/func->Eval(x);
806 (*hres)->Fill(res/err);
807 (*gres)->SetPoint(ipoint, x, res/err);
808 // (*gres)->SetPointError(ipoint, 0, err);
812 (*gres)->SetMarkerStyle(gdata->GetMarkerStyle());
813 (*gres)->SetMarkerColor(gdata->GetMarkerColor());
814 (*gres)->SetLineColor (gdata->GetLineColor());
815 (*gres)->GetHistogram()->GetYaxis()->SetTitle("(data-function)/function");
816 (*hres)->SetMarkerStyle(gdata->GetMarkerStyle());
817 (*hres)->SetMarkerColor(gdata->GetMarkerColor());
818 (*hres)->SetLineColor (gdata->GetLineColor());
824 void AliPWGHistoTools::GetResiduals(const TH1F* hdata, const TF1 * func, TH1F ** hres, TH1F ** hresVsBin) {
826 // Returns an histo of residuals bin by bin and the res/err distribution
829 Printf("AliPWGHistoTools::GetResiduals: No function provided");
833 Printf("AliPWGHistoTools::GetResiduals: No data provided");
837 (*hresVsBin) = (TH1F*) hdata->Clone(TString("hres_")+hdata->GetName());
838 (*hresVsBin)->Reset();
839 (*hres) = new TH1F(TString("hres_")+hdata->GetName()+"-"+func->GetName(),
840 TString("hres_")+hdata->GetName()+"-"+func->GetName(),
843 Int_t nbin = hdata->GetNbinsX();
844 for(Int_t ibin = 1; ibin <= nbin; ibin++){
845 if(!hdata->GetBinContent(ibin)) continue;
846 Float_t res = (hdata->GetBinContent(ibin) - func->Eval(hdata->GetBinCenter(ibin)) ) /
847 func->Eval(hdata->GetBinCenter(ibin));
848 Float_t err = hdata->GetBinError (ibin) / func->Eval(hdata->GetBinCenter(ibin));
849 (*hresVsBin)->SetBinContent(ibin,res);
850 (*hresVsBin)->SetBinError (ibin,err);
851 (*hres)->Fill(res/err);
855 (*hresVsBin)->SetMarkerStyle(hdata->GetMarkerStyle());
856 (*hresVsBin)->SetMarkerColor(hdata->GetMarkerColor());
857 (*hresVsBin)->SetLineColor (hdata->GetLineColor() );
858 (*hresVsBin)->GetYaxis()->SetTitle("(data-function)/function");
859 (*hres)->SetMarkerStyle(hdata->GetMarkerStyle());
860 (*hres)->SetMarkerColor(hdata->GetMarkerColor());
861 (*hres)->SetLineColor (hdata->GetLineColor() );
865 void AliPWGHistoTools::GetYield(TH1* h, TF1 * f, Double_t &yield, Double_t &yieldError, Float_t min, Float_t max,
866 Double_t *partialYields, Double_t *partialYieldsErrors){
868 // Returns the yield extracted from the data in the histo where
869 // there are points and from the fit to extrapolate, in the given
872 // Partial yields are also returned if the corresponding pointers are non null
874 Int_t bin1 = h->FindBin(min);
875 Int_t bin2 = h->FindBin(max);
876 Float_t bin1Edge = GetLowestNotEmptyBinEdge (h);
877 Float_t bin2Edge = GetHighestNotEmptyBinEdge(h);
879 Double_t integralFromHistoError ;
880 Double_t integralFromHisto = DoIntegral(h,bin1,bin2,-1,-1,-1,-1,integralFromHistoError,"width",1);
882 Double_t integralBelow = min < bin1Edge ? f->Integral(min,bin1Edge) : 0;
883 Double_t integralBelowError = min < bin1Edge ? f->IntegralError(min,bin1Edge) : 0;
884 Double_t integralAbove = max > bin2Edge ? f->Integral(bin2Edge,max) : 0;
885 Double_t integralAboveError = max > bin2Edge ? f->IntegralError(bin2Edge,max) : 0;
887 // cout << "GetYield INFO" << endl;
888 // cout << " " << bin1Edge << " " << bin2Edge << endl;
889 // cout << " " << integralFromHisto << " " << integralBelow << " " << integralAbove << endl;
890 // cout << " " << integralFromHistoError << " " << integralBelowError << " " << integralAboveError << endl;
893 partialYields[0] = integralFromHisto;
894 partialYields[1] = integralBelow;
895 partialYields[2] = integralAbove;
897 if(partialYieldsErrors) {
898 partialYieldsErrors[0] = integralFromHistoError;
899 partialYieldsErrors[1] = integralBelowError;
900 partialYieldsErrors[2] = integralAboveError;
902 yield = integralFromHisto+integralBelow+integralAbove;
903 yieldError = TMath::Sqrt(integralFromHistoError*integralFromHistoError+
904 integralBelowError*integralBelowError+
905 integralAboveError*integralAboveError);
909 TGraphErrors * AliPWGHistoTools::DivideGraphByFunc(const TGraphErrors * g, const TF1 * f, Bool_t invert){
911 // Divides g/f. If invert == true => f/g
913 TGraphErrors * gRatio = new TGraphErrors();
914 Int_t npoint = g->GetN();
915 for(Int_t ipoint = 0; ipoint < npoint; ipoint++){
916 Double_t x = g->GetX()[ipoint];
917 Double_t ratio = invert ? f->Eval(x)/g->GetY()[ipoint] :g->GetY()[ipoint]/f->Eval(x);
918 gRatio->SetPoint (ipoint, x, ratio);
919 if(f->Eval(x) && strcmp(g->ClassName(),"TGraphAsymmErrors")) gRatio->SetPointError(ipoint, 0, g->GetEY()[ipoint]/f->Eval(x));
920 // cout << x << " " << g->GetY()[ipoint] << " " << f->Eval(x) << endl;
923 gRatio->SetMarkerStyle(20);
929 TGraphErrors * AliPWGHistoTools::Divide2Graphs(const TGraphErrors * g1, const TGraphErrors * g2){
931 // Divides g1/g2, looks for point with very close centers
933 TGraphErrors * gRatio = new TGraphErrors();
934 Int_t npoint1 = g1->GetN();
935 Int_t npoint2 = g2->GetN();
936 for(Int_t ipoint1 = 0; ipoint1 < npoint1; ipoint1++){
937 Double_t x1 = g1->GetX()[ipoint1];
938 for(Int_t ipoint2 = 0; ipoint2 < npoint2; ipoint2++){
939 Double_t x2 = g2->GetX()[ipoint2];
940 if((TMath::Abs(x1-x2)/(x1+x2)*2)<0.01) {
941 Double_t ratio = g2->GetY()[ipoint2] ? g1->GetY()[ipoint1]/g2->GetY()[ipoint2] : 0;
943 if(g1->InheritsFrom("TGraphErrors") && g2->InheritsFrom("TGraphErrors")) {
944 eratio = g2->GetY()[ipoint2] ?
945 TMath::Sqrt(g1->GetEY()[ipoint1]*g1->GetEY()[ipoint1]/g1->GetY()[ipoint1]/g1->GetY()[ipoint1] +
946 g2->GetEY()[ipoint2]/g2->GetY()[ipoint2]/g2->GetY()[ipoint2] ) * ratio
949 gRatio->SetPoint (ipoint, x1, ratio);
950 gRatio->SetPointError(ipoint, 0, eratio);
952 cout << ipoint << " [" << x1 << "] " << g1->GetY()[ipoint1] << "/" << g2->GetY()[ipoint2] << " = " << ratio <<"+-"<<eratio<< endl;
954 // cout << x << " " << g->GetY()[ipoint] << " " << f->Eval(x) << endl;
959 gRatio->SetMarkerStyle(20);
964 TGraphErrors * AliPWGHistoTools::Add2Graphs(const TGraphErrors * g1, const TGraphErrors * g2){
966 // Sums g1/g2, looks for point with very close centers
968 TGraphErrors * gSum = new TGraphErrors();
969 Int_t npoint1 = g1->GetN();
970 Int_t npoint2 = g2->GetN();
971 for(Int_t ipoint1 = 0; ipoint1 < npoint1; ipoint1++){
972 Double_t x1 = g1->GetX()[ipoint1];
973 for(Int_t ipoint2 = 0; ipoint2 < npoint2; ipoint2++){
974 Double_t x2 = g2->GetX()[ipoint2];
975 if((TMath::Abs(x1-x2)/(x1+x2)*2)<0.01) {
976 Double_t sum = g1->GetY()[ipoint1]+g2->GetY()[ipoint2];
978 if(g1->InheritsFrom("TGraphErrors") && g2->InheritsFrom("TGraphErrors")) {
980 TMath::Sqrt(g1->GetEY()[ipoint1]*g1->GetEY()[ipoint1] +
981 g2->GetEY()[ipoint2]*g2->GetEY()[ipoint2] ) ;
984 gSum->SetPoint (ipoint, x1, sum);
985 gSum->SetPointError(ipoint, 0, esum);
987 cout << ipoint << " [" << x1 << "] " << g1->GetY()[ipoint1] << "+" << g2->GetY()[ipoint2] << " = " << sum <<"+-"<<esum<< endl;
989 // cout << x << " " << g->GetY()[ipoint] << " " << f->Eval(x) << endl;
994 gSum->SetMarkerStyle(20);
1000 TGraphErrors * AliPWGHistoTools::DivideGraphByHisto(const TGraphErrors * g, TH1 * h, Bool_t invert){
1002 // Divides g/h. If invert == true => h/g
1004 Bool_t skipError = kFALSE;
1005 if(!strcmp(g->ClassName(),"TGraph")) skipError = kTRUE;
1006 if(!strcmp(g->ClassName(),"TGraphAsymmErrors")) skipError = kTRUE;
1008 Printf("WARNING: Skipping graph errors");
1010 TGraphErrors * gRatio = new TGraphErrors();
1011 Int_t npoint = g->GetN();
1012 for(Int_t ipoint = 0; ipoint < npoint; ipoint++){
1013 Double_t xj = g->GetX()[ipoint];
1014 Double_t yj = g->GetY()[ipoint];
1015 Double_t yje = skipError ? 0 : g->GetEY()[ipoint];
1017 Int_t binData = h->FindBin(xj);
1018 Double_t yd = h->GetBinContent(binData);
1019 Double_t yde = h->GetBinError(binData);
1020 Double_t xd = h->GetBinCenter(binData);
1026 if (TMath::Abs(xd-xj)/TMath::Abs(xd) > 0.01){
1027 Printf( "WARNING: bin center (%f) and x graph (%f) are more than 1 %% away, skipping",xd,xj );
1032 Double_t ratio = invert ? yd/yj : yj/yd;
1034 gRatio->SetPoint(ipoint, xj, ratio);
1035 gRatio->SetPointError(ipoint, 0, TMath::Sqrt(yde*yde/yd/yd + yje*yje/yj/yj)*ratio);
1036 // gRatio->SetPointError(ipoint, 0, yje/yj * ratio);
1044 TH1F * AliPWGHistoTools::DivideHistoByFunc(TH1F * h, TF1 * f, Bool_t invert){
1046 // Divides h/f. If invert == true => f/g
1047 // Performs the integral of f on the bin range to perform the ratio
1048 // Returns a histo with the same binnig as h
1050 // Prepare histo for ratio
1051 TH1F * hRatio = (TH1F*) h->Clone(TString("hRatio_")+h->GetName()+"_"+f->GetName());
1054 if(!invert) hRatio->SetYTitle(TString(h->GetName())+"/"+f->GetName());
1055 else hRatio->SetYTitle(TString(f->GetName())+"/"+h->GetName());
1057 // Loop over all bins
1058 Int_t nbin = hRatio->GetNbinsX();
1060 for(Int_t ibin = 1; ibin <= nbin; ibin++){
1061 Double_t yhisto = h->GetBinContent(ibin);
1062 Double_t yerror = h->GetBinError(ibin);
1063 Double_t xmin = h->GetBinLowEdge(ibin);
1064 Double_t xmax = h->GetBinLowEdge(ibin+1);
1065 Double_t yfunc = f->Integral(xmin,xmax)/(xmax-xmin);
1066 Double_t ratio = invert ? yfunc/yhisto : yhisto/yfunc ;
1067 Double_t error = yerror/yfunc ;
1068 hRatio->SetBinContent(ibin,ratio);
1069 hRatio->SetBinError (ibin,error);
1076 void AliPWGHistoTools::WeightedMean(Int_t npoints, const Double_t *x, const Double_t *xerr, Double_t &mean, Double_t &meanerr){
1078 // Performs the weighted mean of npoints numbers in x with errors in xerr
1083 Double_t sumweight = 0;
1085 for (Int_t ipoint = 0; ipoint < npoints; ipoint++){
1087 Double_t xerr2 = xerr[ipoint]*xerr[ipoint];
1089 // cout << "xe2 " << xerr2 << endl;
1090 Double_t weight = 1. / xerr2;
1091 sumweight += weight;
1092 mean += weight * x[ipoint];
1093 }// else cout << " Skipping " << ipoint << endl;
1100 meanerr = TMath::Sqrt(1./ sumweight);
1103 // cout << " No sumweight" << endl;
1111 TH1 * AliPWGHistoTools::GetRelativeError(TH1 * h){
1112 // Returns an histogram with the same binning as h, filled with the relative error bin by bin
1113 TH1 * hrel = (TH1*) h->Clone(TString(h->GetName())+"_rel");
1115 Int_t nbin = hrel->GetNbinsX();
1116 for(Int_t ibin = 1; ibin <= nbin; ibin++){
1117 hrel->SetBinContent(ibin,h->GetBinError(ibin)/h->GetBinContent(ibin));
1118 hrel->SetBinError(ibin,0);
1125 void AliPWGHistoTools::GetValueAndError(TH1 * hdest, const TH1 * hvalue, const TH1 * herror, Bool_t isPercentError) {
1127 // Put into source, bin-by-bin, the values from hvalue and the
1128 // errors from content from herror.
1129 // Used mainly to combine histos of systemati errors with their spectra
1130 // Set isPercentError to kTRUE if the error is given in %
1133 Printf("AliPWGHistoTools::GetValueAndError Errror: hdest is null");
1138 Int_t nbin = hdest->GetNbinsX();
1139 Int_t nBinSourceVal = hvalue->GetNbinsX();
1140 Int_t nBinSourceErr = herror->GetNbinsX();
1142 for(Int_t iBinDest = 1; iBinDest <= nbin; iBinDest++){
1143 Float_t lowPtDest=hdest->GetBinLowEdge(iBinDest);
1144 Float_t binWidDest=hdest->GetBinWidth(iBinDest);
1145 // Loop over Source bins and find overlapping bins to Dest
1146 // First value then error
1148 Bool_t foundValue = kFALSE;
1149 for(Int_t iBinSourceVal=1; iBinSourceVal<=nBinSourceVal; iBinSourceVal++){
1150 Float_t lowPtSource= hvalue->GetBinLowEdge(iBinSourceVal) ;
1151 Float_t binWidSource= hvalue->GetBinWidth(iBinSourceVal);
1152 if(TMath::Abs(lowPtDest-lowPtSource)<0.001 && TMath::Abs(binWidSource-binWidDest)<0.001){
1153 Double_t content = hvalue->GetBinContent(iBinSourceVal);
1154 hdest->SetBinContent(iBinDest, content);
1159 // if (!foundValue){
1160 // Printf("AliPWGHistoTools::GetValueAndError: Error: cannot find matching value source bin for destination %d",iBinDest);
1164 Bool_t foundError = kFALSE;
1165 for(Int_t iBinSourceErr=1; iBinSourceErr<=nBinSourceErr; iBinSourceErr++){
1166 Float_t lowPtSource= herror->GetBinLowEdge(iBinSourceErr) ;
1167 Float_t binWidSource= herror->GetBinWidth(iBinSourceErr);
1168 if(TMath::Abs(lowPtDest-lowPtSource)<0.001 && TMath::Abs(binWidSource-binWidDest)<0.001){
1169 Double_t error = herror->GetBinContent(iBinSourceErr);
1170 // cout << "-> " << iBinDest << " " << error << " " << hdest->GetBinContent(iBinDest) << endl;
1172 hdest->SetBinError(iBinDest, isPercentError ? error * hdest->GetBinContent(iBinDest) : error);
1177 // if (!foundError ){
1178 // Printf("AliPWGHistoTools::GetValueAndError: Error: cannot find matching error source bin for destination %d",iBinDest);
1185 void AliPWGHistoTools::AddHisto(TH1 * hdest, const TH1* hsource, Bool_t getMirrorBins) {
1187 // Adds hsource to hdest bin by bin, even if they have a different
1188 // binning If getMirrorBins is true, it takes the negative bins
1189 // (Needed because sometimes the TPC uses the positive axis for
1190 // negative particles and the possitive axis for positive
1194 if (hdest == NULL) {
1195 Printf("Error: hdest is NULL\n");
1198 if (hsource == NULL) {
1199 Printf("Error: hsource is NULL\n");
1203 Int_t nBinSource = hsource->GetNbinsX();
1204 Int_t nBinDest = hdest->GetNbinsX();
1206 // Loop over destination bins,
1207 for(Int_t iBinDest=1; iBinDest<=nBinDest; iBinDest++){
1208 Float_t lowPtDest=hdest->GetBinLowEdge(iBinDest);
1209 Float_t binWidDest=hdest->GetBinWidth(iBinDest);
1210 // Loop over Source bins and find overlapping bins to Dest
1211 Bool_t found = kFALSE;
1212 for(Int_t iBinSource=1; iBinSource<=nBinSource; iBinSource++){
1213 Float_t lowPtSource= getMirrorBins ? -hsource->GetBinLowEdge(iBinSource)+hsource->GetBinWidth(iBinSource) : hsource->GetBinLowEdge(iBinSource) ;
1214 Float_t binWidSource= hsource->GetBinWidth(iBinSource) ;
1215 if(TMath::Abs(lowPtDest-lowPtSource)<0.001 && TMath::Abs(binWidSource-binWidDest)<0.001){
1216 Float_t dest=hdest->GetBinContent(iBinDest);
1217 Float_t source=hsource->GetBinContent(iBinSource);
1218 Float_t edest=hdest->GetBinError(iBinDest);
1219 Float_t esource=hsource->GetBinError(iBinSource);
1220 Double_t cont=dest+source;
1221 Double_t econt=TMath::Sqrt(edest*edest+esource*esource);
1222 hdest->SetBinContent(iBinDest,cont);
1223 hdest->SetBinError (iBinDest,econt);
1230 // Printf("Error: cannot find matching source bin for destination %d",iBinDest);
1237 void AliPWGHistoTools::GetHistoCombinedErrors(TH1 * hdest, const TH1 * h1) {
1239 // Combine the errors of hdest with the errors of h1, summing in
1240 // quadrature. Results are put in hdest. Histograms are assumed to
1241 // have the same binning
1243 Int_t nbin = hdest->GetNbinsX();
1244 for(Int_t ibin = 1; ibin <= nbin; ibin++){
1245 Double_t e1 = hdest->GetBinError(ibin);
1246 Double_t e2 = h1->GetBinError(ibin);
1247 hdest->SetBinError(ibin, TMath::Sqrt(e1*e1+e2*e2));
1253 TH1F * AliPWGHistoTools::DivideHistosDifferentBins(const TH1F* h1, const TH1F* h2) {
1254 // Divides 2 histos even if they have a different binning. Finds
1255 // overlapping bins and divides them
1258 TH1F * hRatio = new TH1F(*h1);
1259 Int_t nBinsH1=h1->GetNbinsX();
1260 Int_t nBinsH2=h2->GetNbinsX();
1261 // Loop over H1 bins,
1262 for(Int_t iBin=1; iBin<=nBinsH1; iBin++){
1263 hRatio->SetBinContent(iBin,0.);
1264 hRatio->SetBinContent(iBin,0.);
1265 Float_t lowPtH1=h1->GetBinLowEdge(iBin);
1266 Float_t binWidH1=h1->GetBinWidth(iBin);
1267 // Loop over H2 bins and find overlapping bins to H1
1268 for(Int_t jBin=1; jBin<=nBinsH2; jBin++){
1269 Float_t lowPtH2=h2->GetBinLowEdge(jBin);
1270 Float_t binWidH2=h2->GetBinWidth(jBin);
1271 if(TMath::Abs(lowPtH1-lowPtH2)<0.001 && TMath::Abs(binWidH2-binWidH1)<0.001){
1272 Float_t numer=h1->GetBinContent(iBin);
1273 Float_t denom=h2->GetBinContent(jBin);
1274 Float_t enumer=h1->GetBinError(iBin);
1275 Float_t edenom=h2->GetBinError(jBin);
1278 if(numer>0. && denom>0.){
1280 eratio=ratio*TMath::Sqrt((enumer/numer)*(enumer/numer)+(edenom/denom)*(edenom/denom));
1282 hRatio->SetBinContent(iBin,ratio);
1283 hRatio->SetBinError(iBin,eratio);
1291 Double_t AliPWGHistoTools::DoIntegral(TH1* h, Int_t binx1, Int_t binx2, Int_t biny1, Int_t biny2, Int_t binz1, Int_t binz2, Double_t & error ,
1292 Option_t *option, Bool_t doError)
1294 // function to compute integral and optionally the error between the limits
1295 // specified by the bin number values working for all histograms (1D, 2D and 3D)
1296 // copied from TH! to fix a bug still present in 5-27-06b
1297 Int_t nbinsx = h->GetNbinsX();
1298 if (binx1 < 0) binx1 = 0;
1299 if (binx2 > nbinsx+1 || binx2 < binx1) binx2 = nbinsx+1;
1300 if (h->GetDimension() > 1) {
1301 Int_t nbinsy = h->GetNbinsY();
1302 if (biny1 < 0) biny1 = 0;
1303 if (biny2 > nbinsy+1 || biny2 < biny1) biny2 = nbinsy+1;
1305 biny1 = 0; biny2 = 0;
1307 if (h->GetDimension() > 2) {
1308 Int_t nbinsz = h->GetNbinsZ();
1309 if (binz1 < 0) binz1 = 0;
1310 if (binz2 > nbinsz+1 || binz2 < binz1) binz2 = nbinsz+1;
1312 binz1 = 0; binz2 = 0;
1315 // - Loop on bins in specified range
1316 TString opt = option;
1318 Bool_t width = kFALSE;
1319 if (opt.Contains("width")) width = kTRUE;
1325 Double_t integral = 0;
1326 Double_t igerr2 = 0;
1327 for (Int_t binx = binx1; binx <= binx2; ++binx) {
1328 if (width) dx = h->GetXaxis()->GetBinWidth(binx);
1329 for (Int_t biny = biny1; biny <= biny2; ++biny) {
1330 if (width) dy = h->GetYaxis()->GetBinWidth(biny);
1331 for (Int_t binz = binz1; binz <= binz2; ++binz) {
1332 if (width) dz = h->GetZaxis()->GetBinWidth(binz);
1333 Int_t bin = h->GetBin(binx, biny, binz);
1334 if (width) integral += h->GetBinContent(bin)*dx*dy*dz;
1335 else integral += h->GetBinContent(bin);
1337 if (width) igerr2 += h->GetBinError(bin)*h->GetBinError(bin)*dx*dy*dz*dx*dy*dz;
1338 else igerr2 += h->GetBinError(bin)*h->GetBinError(bin);
1340 // cout << h->GetBinContent(bin) << " " << h->GetBinError(bin) << " " << dx*dy*dz << " " << integral << " +- " << igerr2 << endl;
1346 if (doError) error = TMath::Sqrt(igerr2);
1350 Double_t AliPWGHistoTools::dMtdptFunction(Double_t *x, Double_t *p) {
1352 // Computes the dmt/dptdeta function using the dN/dpt function
1353 // This is a protected function used internally by GetdMtdy to integrate dN/dpt function using mt as a weight
1354 // The mass of the particle is given as p[0]
1356 Double_t mass = p[0];
1357 Double_t mt = TMath::Sqrt(pt*pt + mass*mass);
1358 Double_t jacobian = pt/mt;
1359 if(!fdNdptForETCalc){
1360 Printf("AliPWGHistoTools::dMtdptFunction: ERROR: fdNdptForETCalc not defined");
1363 Double_t dNdpt = fdNdptForETCalc->Eval(pt);
1364 return dNdpt*mt*jacobian; // FIXME: do I have to normalize somehow?
1368 Double_t AliPWGHistoTools::GetdMtdEta(TH1 *hData, TF1 * fExtrapolation, Double_t mass) {
1369 // Computes dMtdEta integrating dN/dptdy with the proper weights and jacobian.
1370 Printf("WARNING ALIBWTOOLS::GetdMtdEta: ONLY USING FUNCTION FOR THE TIME BEING, hData");
1372 Printf("hData not set");
1375 // Assign the fiunction used internally by dMtdptFunction
1376 fdNdptForETCalc = fExtrapolation;
1377 // Create the function to be integrated
1378 TF1 * funcdMtdPt = new TF1 ("funcdMtdPt", dMtdptFunction, 0.0, 20, 1);
1379 funcdMtdPt->SetParameter(0,mass);
1381 Double_t dMtdEta = funcdMtdPt->Integral(0,100);