Migrating PWG2/SPECTRA/Fit to new PWG structure
[u/mrichter/AliRoot.git] / PWG / Tools / AliPWGHistoTools.cxx
CommitLineData
4c0d7bc7 1// ----------------------------------------------------------------------
fef6f2b3 2// AliPWGHistoTools
4c0d7bc7 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
fef6f2b3 11#include "AliPWGHistoTools.h"
4c0d7bc7 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
4c0d7bc7 24
fef6f2b3 25ClassImp(AliPWGHistoTools)
4c0d7bc7 26
fef6f2b3 27TF1 * AliPWGHistoTools::fdNdptForETCalc = 0;
90421222 28
fef6f2b3 29AliPWGHistoTools::AliPWGHistoTools() {
4c0d7bc7 30 // ctor
31}
32
fef6f2b3 33AliPWGHistoTools::~AliPWGHistoTools(){
4c0d7bc7 34 // dtor
35}
36
fef6f2b3 37TH1 * AliPWGHistoTools::GetdNdmtFromdNdpt(const TH1 * hpt, Double_t mass) {
4c0d7bc7 38 // convert the x axis from pt to mt. Assumes you have 1/pt dNdpt in the histo you start with
39
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) +
0e019ec6 44 mass *mass) - mass;
77233d47 45 // // xbins[ibins] = TMath::Sqrt(hpt->GetBinLowEdge(ibins+1)*hpt->GetBinLowEdge(ibins+1) +
46 // // mass *mass);
4c0d7bc7 47 // cout << ibins << " "<< xbins[ibins] << endl;
48
49 }
50
51 TH1D * hmt = new TH1D(TString(hpt->GetName())+"_mt",
52 TString(hpt->GetName())+"_mt",
53 nbins, xbins);
54 for(Int_t ibins = 1; ibins <= nbins; ibins++){
55 hmt->SetBinContent(ibins, hpt->GetBinContent(ibins));
56 hmt->SetBinError(ibins, hpt->GetBinError(ibins));
57
58 }
59
60 hmt->SetXTitle("m_{t} - m_{0} (GeV/c^{2})");
61 hmt->SetYTitle("1/m_{t} dN/dm_{t} (a.u.)");
0e019ec6 62 hmt->SetMarkerStyle(hpt->GetMarkerStyle());
63 hmt->SetMarkerColor(hpt->GetMarkerColor());
64 hmt->SetLineColor(hpt->GetLineColor());
65
4c0d7bc7 66 return hmt;
67
68}
69
fef6f2b3 70TH1 * AliPWGHistoTools::GetdNdptFromdNdmt(const TH1 * hmt, Double_t mass) {
9ca56c22 71 // convert the x axis from mt to pt. Assumes you have 1/mt dNdmt in the histo you start with
72
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) -
77 mass *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) +
80 // // mass *mass);
81 cout << ibins << " "<< xbins[ibins] << endl;
82
83 }
84
85 TH1D * hptL = new TH1D(TString(hmt->GetName())+"_pt",
86 TString(hmt->GetName())+"_pt",
87 nbins, xbins);
88 for(Int_t ibins = 1; ibins <= nbins; ibins++){
89 hptL->SetBinContent(ibins, hmt->GetBinContent(ibins));
90 hptL->SetBinError(ibins, hmt->GetBinError(ibins));
91
92 }
93
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());
99
100 return hptL;
101
102}
103
104
fef6f2b3 105TH1 * AliPWGHistoTools::GetdNdPtFromOneOverPt(const TH1 * h1Pt) {
4c0d7bc7 106
107 // convert an histo from 1/pt dNdpt to dNdpt, using the pt at the center of the bin
108
109
110 TH1 * hPt = (TH1 *) h1Pt->Clone((TString(h1Pt->GetName()) + "_inv").Data());
111 hPt->Reset();
112
113 Int_t nbinx = hPt->GetNbinsX();
114
115 for(Int_t ibinx = 1; ibinx <= nbinx; ibinx++){
116
117 Double_t cont = h1Pt->GetBinContent(ibinx);
118 Double_t err = h1Pt->GetBinError(ibinx);
119
120 Double_t pt = h1Pt->GetBinCenter(ibinx);
121
c3c47c65 122 if(pt > 0) {
4c0d7bc7 123 cont *= pt;
124 err *= pt;
125 } else {
126 cont = 0;
127 err = 0;
128 }
129
130 hPt->SetBinContent(ibinx, cont);
131 hPt->SetBinError(ibinx, err);
132
133 }
134
135 hPt->SetXTitle("p_{t} (GeV)");
136 hPt->SetYTitle("dN/dp_{t} (GeV^{-2})");
137
138 return hPt;
139
140}
141
142
143
144
fef6f2b3 145TH1 * AliPWGHistoTools::GetOneOverPtdNdPt(const TH1 * hPt) {
4c0d7bc7 146
147 // convert an histo from dNdpt to 1/pt dNdpt, using the pt at the center of the bin
148
149 TH1 * h1Pt = (TH1 *) hPt->Clone((TString(hPt->GetName()) + "_inv").Data());
150 h1Pt->Reset();
151
152 Int_t nbinx = h1Pt->GetNbinsX();
153
154 for(Int_t ibinx = 1; ibinx <= nbinx; ibinx++){
155
156 Double_t cont = hPt->GetBinContent(ibinx);
157 Double_t err = hPt->GetBinError(ibinx);
158
159 Double_t pt = hPt->GetBinCenter(ibinx);
160
c3c47c65 161 if(pt > 0) {
4c0d7bc7 162 cont /= pt;
163 err /= pt;
164 } else {
165 cont = 0;
166 err = 0;
167 }
168
169 h1Pt->SetBinContent(ibinx, cont);
170 h1Pt->SetBinError(ibinx, err);
171
172 }
173
174 h1Pt->SetXTitle("p_{t} (GeV)");
175 h1Pt->SetYTitle("1/p_{t} dN/dp_{t} (GeV^{-2})");
176
177 return h1Pt;
178
179}
180
181
fef6f2b3 182TGraphErrors * AliPWGHistoTools::GetGraphFromHisto(const TH1F * h, Bool_t binWidth) {
4c0d7bc7 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();
186
187 TGraphErrors * g = new TGraphErrors();
188 Int_t ipoint = 0;
189 for(Int_t ibin = 1; ibin <= nbin; ibin++){
190 Double_t xerr = binWidth ? h->GetBinWidth(ibin)/2 : 0;
3beb22a2 191 if (h->GetBinContent(ibin)) {
4c0d7bc7 192 g->SetPoint (ipoint, h->GetBinCenter(ibin), h->GetBinContent(ibin));
193 g->SetPointError(ipoint, xerr, h->GetBinError(ibin));
194 ipoint++;
195 }
196 }
197
198 g->SetMarkerStyle(h->GetMarkerStyle());
199 g->SetMarkerColor(h->GetMarkerColor());
200 g->SetLineColor(h->GetLineColor());
2210b002 201 g->SetLineStyle(h->GetLineStyle());
202 g->SetLineWidth(h->GetLineWidth());
4c0d7bc7 203
204 g->SetTitle(h->GetTitle());
205 g->SetName(TString("g_")+h->GetName());
206
207 return g;
208
209}
210
fef6f2b3 211TH1F * AliPWGHistoTools::GetHistoFromGraph(const TGraphErrors * g, const TH1F* hTemplate) {
4c0d7bc7 212
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
216 // won't work!
217
218 TH1F * h = (TH1F*) hTemplate->Clone(TString("h_")+g->GetName());
219 h->Reset();
220 Int_t npoint = g->GetN();
221 // g->Print();
222 for(Int_t ipoint = 0; ipoint < npoint; ipoint++){
223 Float_t x = g->GetX() [ipoint];
224 Float_t y = g->GetY() [ipoint];
fef6f2b3 225 Float_t ey = 0;
226 if(g->InheritsFrom("TGraphErrors"))
227 ey = g->GetEY()[ipoint];
4c0d7bc7 228 Int_t bin = h->FindBin(x);
229 // cout << "bin: "<< bin << " -> " << x << ", "<< y <<", " << ey << endl;
230
231 h->SetBinContent(bin,y);
232 h->SetBinError (bin,ey);
233 }
234
235 h->SetMarkerStyle(g->GetMarkerStyle());
236 h->SetMarkerColor(g->GetMarkerColor());
237 h->SetLineColor (g->GetLineColor());
238
239
240 return h;
241}
242
fef6f2b3 243TGraphErrors * AliPWGHistoTools::ConcatenateGraphs(const TGraphErrors * g1,const TGraphErrors * g2){
4c0d7bc7 244
245 // concatenates two graphs
246
247 Int_t npoint1=g1->GetN();
248 Int_t npoint2=g2->GetN();
249
250 TGraphErrors * gClone = (TGraphErrors*) g1->Clone();
251
252// for(Int_t ipoint = 0; ipoint < npoint1; ipoint++){
253// gClone->SetPointError(ipoint,0,g1->GetEY()[ipoint]);
254
255// }
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]);
260 }
261
262 gClone->GetHistogram()->GetXaxis()->SetTimeDisplay(1);
263 gClone->SetTitle(TString(gClone->GetTitle())+" + "+g2->GetTitle());
264 gClone->SetName(TString(gClone->GetName())+"_"+g2->GetName());
265
266 return gClone;
267}
268
269
fef6f2b3 270TH1F * AliPWGHistoTools::Combine3HistosWithErrors(const TH1 * h1, const TH1 * h2, const TH1* h3,
3725266c 271 TH1 * he1, TH1 * he2, TH1 * he3,
3beb22a2 272 const TH1* htemplate, Int_t statFrom,
3725266c 273 Float_t renorm1, Float_t renorm2, Float_t renorm3,
274 TH1 ** hSyst, Bool_t errorFromBinContent) {
ee08b77d 275
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.
3725266c 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
ee08b77d 286 TH1F * hcomb = (TH1F*) htemplate->Clone(TString("hComb_")+h1->GetName()+"_"+h2->GetName()+h3->GetName());
287
288 // TODO: I should have used an array for h*local...
289
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);
297
3beb22a2 298 const TH1 * hStatError = 0;
ee08b77d 299 if (statFrom == 0) hStatError = h1;
300 else if (statFrom == 1) hStatError = h2;
301 else if (statFrom == 2) hStatError = h3;
37e1d042 302 else {
fef6f2b3 303 Printf("AliPWGHistoTools::Combine3HistosWithErrors: wrong value of the statFrom parameter");
37e1d042 304 return NULL;
305 }
fef6f2b3 306 Printf("AliPWGHistoTools::Combine3HistosWithErrors: improve error on combined");
ee08b77d 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
314
315 if (statFrom == 0) ibinError = ibin1;
316 else if (statFrom == 1) ibinError = ibin2;
317 else if (statFrom == 2) ibinError = ibin3;
fef6f2b3 318 else Printf("AliPWGHistoTools::Combine3HistosWithErrors: wrong value of the statFrom parameter");
ee08b77d 319
320
321 Double_t y[3];
322 Double_t ye[3];
323 y[0] = h1local->GetBinContent(ibin1);
324 y[1] = h2local->GetBinContent(ibin2);
325 y[2] = h3local->GetBinContent(ibin3);
3725266c 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)));
330 } else {
331 ye[0] = he1->GetBinError(ibin1);
332 ye[1] = he2->GetBinError(ibin2);
333 ye[2] = he3->GetBinError(ibin3);
334 }
ee08b77d 335 // Set error to 0 if content is 0 (means it was not filled)
3beb22a2 336 if(!h1local->GetBinContent(ibin1)) ye[0] = 0;
337 if(!h2local->GetBinContent(ibin2)) ye[1] = 0;
338 if(!h3local->GetBinContent(ibin3)) ye[2] = 0;
ee08b77d 339
340 // Compute weighted mean
3725266c 341 // cout << "Bins: "<< ibin1 << " " << ibin2 << " " << ibin3 << endl;
ee08b77d 342 Double_t mean, err;
343 WeightedMean(3,y,ye,mean,err);
344
345
346 // Fill combined
ee08b77d 347 hcomb->SetBinContent(ibin,mean);
348 Double_t statError = 0;
3beb22a2 349 if (hStatError->GetBinContent(ibinError)) {
ee08b77d 350 // cout << "def" << endl;
351 statError = hStatError->GetBinError(ibinError)/hStatError->GetBinContent(ibinError)*hcomb->GetBinContent(ibin);
352 }
3beb22a2 353 else if (h1local->GetBinContent(ibin1)) {
ee08b77d 354 // cout << "1" << endl;
355 statError = h1local->GetBinError(ibin1)/h1local->GetBinContent(ibin1)*hcomb->GetBinContent(ibin);
356 }
3beb22a2 357 else if (h2local->GetBinContent(ibin2)) {
ee08b77d 358 // cout << "2" << endl;
359 statError = h2local->GetBinError(ibin2)/h2local->GetBinContent(ibin2)*hcomb->GetBinContent(ibin);
360 }
3beb22a2 361 else if (h3local->GetBinContent(ibin3)) {
ee08b77d 362 // cout << "3" << endl;
363 statError = h3local->GetBinError(ibin3)/h3local->GetBinContent(ibin3)*hcomb->GetBinContent(ibin);
364 }
365 hcomb->SetBinError (ibin,statError);
3725266c 366 if(hSyst) (*hSyst)->SetBinContent(ibin,err);
ee08b77d 367 // cout << "BIN " << ibin << " " << mean << " " << statError << endl;
368
369 }
370
371 hcomb->SetMarkerStyle(hStatError->GetMarkerStyle());
372 hcomb->SetMarkerColor(hStatError->GetMarkerColor());
373 hcomb->SetLineColor (hStatError->GetLineColor());
374
375 hcomb->SetXTitle(hStatError->GetXaxis()->GetTitle());
376 hcomb->SetYTitle(hStatError->GetYaxis()->GetTitle());
377
378 delete h1local;
379 delete h2local;
380 delete h3local;
381
382 return hcomb;
383}
384
385
fef6f2b3 386void AliPWGHistoTools::GetMeanDataAndExtrapolation(const TH1 * hData, TF1 * fExtrapolation, Double_t &mean, Double_t &error, Float_t min, Float_t max){
ddecf727 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
90421222 390 // ERROR from DATA ONLY is returned in this version!
ddecf727 391 //
fef6f2b3 392 Printf("AliPWGHistoTools::GetMeanDataAndExtrapolation: WARNING from data only");
ddecf727 393 Float_t minData = GetLowestNotEmptyBinEdge (hData);
58eeda84 394 Int_t minDataBin = GetLowestNotEmptyBin (hData);
ddecf727 395 Float_t maxData = GetHighestNotEmptyBinEdge(hData);
58eeda84 396 Int_t maxDataBin = GetHighestNotEmptyBin (hData);
ddecf727 397 Double_t integral = 0;
398 mean = 0;
399 error = 0;
400 if (min < minData) {
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;
405
406 }
407
408 if (max > maxData) {
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;
413 }
414 Float_t err2 = 0;
415
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);
422 }
423 cout << " Data "<< mean << " " << integral << endl;
424
425 mean = mean/integral;
ffcaf7be 426 error = TMath::Sqrt(err2)/integral;
ddecf727 427
4c0d7bc7 428
ddecf727 429}
430
fef6f2b3 431TH1F * AliPWGHistoTools::CombineHistos(const TH1 * h1, TH1 * h2, const TH1* htemplate, Float_t renorm1){
4c0d7bc7 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
436
437 TH1F * hcomb = (TH1F*) htemplate->Clone(TString(h1->GetName())+"_"+h2->GetName());
438
439 TH1F * h1local = (TH1F*) h1->Clone();
440 h1local->Scale(renorm1);
441
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));
446
3beb22a2 447 if (!h1local->GetBinContent(ibin1) && !h2->GetBinContent(ibin2) ) {
4c0d7bc7 448 // None has data: go to next bin
449 hcomb->SetBinContent(ibin,0);
450 hcomb->SetBinError (ibin,0);
3beb22a2 451 } else if(h1local->GetBinContent(ibin1) && !h2->GetBinContent(ibin2)) {
4c0d7bc7 452 // take data from h1local:
453 hcomb->SetBinContent(ibin,h1local->GetBinContent(ibin1));
454 hcomb->SetBinError (ibin,h1local->GetBinError(ibin1));
3beb22a2 455 } else if(!h1local->GetBinContent(ibin1) && h2->GetBinContent(ibin2)) {
4c0d7bc7 456 // take data from h2:
457 hcomb->SetBinContent(ibin,h2->GetBinContent(ibin2));
458 hcomb->SetBinError (ibin,h2->GetBinError(ibin2));
459 } else {
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));
463 }
464
465
466 }
467
468
469 hcomb->SetMarkerStyle(h1local->GetMarkerStyle());
470 hcomb->SetMarkerColor(h1local->GetMarkerColor());
471 hcomb->SetLineColor (h1local->GetLineColor());
472
473 hcomb->SetXTitle(h1local->GetXaxis()->GetTitle());
474 hcomb->SetYTitle(h1local->GetYaxis()->GetTitle());
475 delete h1local;
476 return hcomb;
477
478}
479
480
fef6f2b3 481void AliPWGHistoTools::GetFromHistoGraphDifferentX(const TH1F * h, TF1 * f, TGraphErrors ** gBarycentre, TGraphErrors ** gXlw) {
4c0d7bc7 482
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)
486
487 Int_t nbin = h->GetNbinsX();
488
489 (*gBarycentre) = new TGraphErrors();
490 (*gXlw) = new TGraphErrors();
491
492 Int_t ipoint = 0;
493 for(Int_t ibin = 1; ibin <= nbin; ibin++){
494 Float_t min = h->GetBinLowEdge(ibin);
495 Float_t max = h->GetBinLowEdge(ibin+1);
496 Double_t xerr = 0;
497 Double_t xbar = f->Mean(min,max);
c3c47c65 498 // compute xLW
4c0d7bc7 499 Double_t temp = 1./(max-min) * f->Integral(min,max);
500 Double_t epsilon = 0.000000001;
501 Double_t increment = 0.0000000001;
c3c47c65 502 Double_t xLW = min;
4c0d7bc7 503
c3c47c65 504 while ((f->Eval(xLW)- temp) > epsilon) {
505 xLW += increment;
506 if(xLW > max) {
507 Printf("Cannot find xLW");
4c0d7bc7 508 break;
509 }
510 }
511
512 if (h->GetBinContent(ibin)!=0) {
513 (*gBarycentre)->SetPoint (ipoint, xbar, h->GetBinContent(ibin));
514 (*gBarycentre)->SetPointError(ipoint, xerr, h->GetBinError(ibin));
c3c47c65 515 (*gXlw) ->SetPoint (ipoint, xLW, h->GetBinContent(ibin));
4c0d7bc7 516 (*gXlw) ->SetPointError(ipoint, xerr, h->GetBinError(ibin));
517 ipoint++;
518 }
519 }
520
521 (*gBarycentre)->SetMarkerStyle(h->GetMarkerStyle());
522 (*gBarycentre)->SetMarkerColor(h->GetMarkerColor());
523 (*gBarycentre)->SetLineColor(h->GetLineColor());
524
525 (*gBarycentre)->SetTitle(h->GetTitle());
526 (*gBarycentre)->SetName(TString("g_")+h->GetName());
527
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());
533
534
535}
536
537
fef6f2b3 538Float_t AliPWGHistoTools::GetMean(TH1F * h, Float_t min, Float_t max, Float_t * error) {
4c0d7bc7 539
3472a634 540 // Get the mean of histo in a range; root is not reliable in sub
541 // ranges with variable binning.
4c0d7bc7 542 Int_t minBin = h->FindBin(min);
ddecf727 543 Int_t maxBin = h->FindBin(max-0.00001);
4c0d7bc7 544
545 Float_t mean = 0 ;
546 Float_t integral = 0;
ddecf727 547 Float_t err2 = 0;
548 for(Int_t ibin = minBin; ibin <= maxBin; ibin++){
4c0d7bc7 549 mean = mean + (h->GetBinCenter(ibin) * h->GetBinWidth(ibin)* h->GetBinContent(ibin));
ddecf727 550 err2 = err2 + TMath::Power(h->GetBinError(ibin) * h->GetBinCenter(ibin) * h->GetBinWidth(ibin),2);
4c0d7bc7 551 integral = integral + h->GetBinContent(ibin) * h->GetBinWidth(ibin);
552 }
553
ddecf727 554 Float_t value = mean/integral;
555 if (error) (*error) = TMath::Sqrt(err2);
556 return value;
4c0d7bc7 557
558
559}
560
fef6f2b3 561void AliPWGHistoTools::GetMean(TF1 * func, Float_t &mean, Float_t &error, Float_t min, Float_t max, Int_t normPar) {
4c0d7bc7 562
3472a634 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.
4c0d7bc7 568
3472a634 569 return GetMoment("fMean", TString("x*")+func->GetExpFormula(), func, mean, error, min, max, normPar);
4c0d7bc7 570
571}
572
fef6f2b3 573void AliPWGHistoTools::GetMeanSquare(TF1 * func, Float_t &mean, Float_t &error, Float_t min, Float_t max, Int_t normPar) {
4c0d7bc7 574
3472a634 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.
4c0d7bc7 580
3472a634 581 return GetMoment("fMean2", TString("x*x*")+func->GetExpFormula(), func, mean, error, min, max, normPar);
4c0d7bc7 582
583
584}
585
fef6f2b3 586void AliPWGHistoTools::GetMoment(TString name, TString var, TF1 * func, Float_t &mean, Float_t &error, Float_t min, Float_t max, Int_t normPar) {
4c0d7bc7 587
588 // returns the integral of a function derived from func by prefixing some operation.
3472a634 589 // the derived function MUST have the same parameter in the same order
4c0d7bc7 590 // Used as a base method for mean and mean2
3472a634 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
595 // correctly.
596
597 // TODO:
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
602
fef6f2b3 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");
4c0d7bc7 605 Int_t npar = func->GetNpar();
606
3472a634 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
611
612 // integr is used to normalize if no parameter is provided
613 Double_t integr = normPar < 0 ? func->Integral(min,max) : 1;
4c0d7bc7 614
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.
618
619 // cout << "npar :" << npar << endl;
620
621 for(Int_t ipar = 0; ipar < npar; ipar++){
622 Double_t parmin, parmax;
623 Double_t value = func->GetParameter(ipar);
3472a634 624 f->SetParameter(ipar,value);
4c0d7bc7 625 func->GetParLimits(ipar, parmin, parmax);
626 if ( parmin == parmax ) {
3beb22a2 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
4c0d7bc7 629 f->FixParameter(ipar,func->GetParameter(ipar));
630 // cout << "Fixing " << ipar << "("<<value<<","<<parmin<<","<<parmax<<")"<<endl;
631 }
632 else {
633 f->SetParError (ipar,func->GetParError(ipar) );
634 // cout << "Setting Err" << ipar << "("<<func->GetParError(ipar)<<")"<<endl;
635 }
636 }
637 else {
638 f->SetParError (ipar,func->GetParError(ipar) );
639 // cout << "Setting Err" << ipar << "("<<func->GetParError(ipar)<<")"<<endl;
640 }
641 }
3472a634 642
643// f->Print();
644// cout << "----" << endl;
645// func->Print();
646
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);
4c0d7bc7 649// cout << "Mean " << mean <<"+-"<< error<< endl;
650// cout << "Integral Error " << error << endl;
651
652}
653
4c0d7bc7 654
655
fef6f2b3 656Bool_t AliPWGHistoTools::Fit (TH1 * h1, TF1* func, Float_t min, Float_t max) {
4c0d7bc7 657
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
660 // returs false.
661
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;
665
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);
670
671
672// From TH1:
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.
692
693
694 if( gMinuit->fLimset ) {
fef6f2b3 695 Printf("ERROR: AliPWGHistoTools: Parameters at limits");
4c0d7bc7 696 return kFALSE;
697 }
698
699
700 ///
701 fitter = TVirtualFitter::GetFitter();
702 Int_t fitStat = fitter->GetStats(amin, edm, errdef, nvpar, nparx);
703
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 ") {
fef6f2b3 708 Printf("WARNING: AliPWGHistoTools: Cannot properly compute errors");
4c0d7bc7 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");
712 }
713
714 if (edm > 1e6) {
715
fef6f2b3 716 Printf("WARNING: AliPWGHistoTools: Huge EDM (%f): Fit probably failed!", edm);
4c0d7bc7 717 }
718 if (fitResult != 0) {
fef6f2b3 719 Printf("WARNING: AliPWGHistoTools: Fit Result (%d)", fitResult);
4c0d7bc7 720 }
721
fef6f2b3 722 Printf ("AliPWGHistoTools: Trying Again with Strategy = 2");
4c0d7bc7 723
724 gMinuit->Command("SET STRATEGY 2"); // more effort
725 fitResult = 0;
726 fitResult = h1-> Fit(func,"0","",min,max);
727 fitResult = h1-> Fit(func,"IME0","",min,max);
728 fitResult = h1-> Fit(func,"IME0","",min,max);
729
730 fitter = TVirtualFitter::GetFitter();
731
732 fitStat = fitter->GetStats(amin, edm, errdef, nvpar, nparx);
733
734 if(fitStat < 3 && gMinuit->fCstatu != "UNCHANGED ") {
fef6f2b3 735 Printf("ERROR: AliPWGHistoTools: Cannot properly compute errors");
4c0d7bc7 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;
740 return kFALSE;
741 }
742
743 if (edm > 1e6) {
fef6f2b3 744 Printf("ERROR: AliPWGHistoTools: Huge EDM (%f): Fit probably failed!", edm);
4c0d7bc7 745
746 return kFALSE;
747 }
748 if (fitResult != 0) {
fef6f2b3 749 Printf("ERROR: AliPWGHistoTools: Fit Result (%d)", fitResult);
4c0d7bc7 750
751 return kFALSE;
752 }
753
754 gMinuit->Command("SET STRATEGY 1"); // back to normal value
755
756 }
757
758 cout << "---- FIT OK ----" << endl;
759
760 return kTRUE;
761
762}
763
fef6f2b3 764Int_t AliPWGHistoTools::GetLowestNotEmptyBin(const TH1*h) {
4c0d7bc7 765
766 // Return the index of the lowest non empty bin in the histo h
767
768 Int_t nbin = h->GetNbinsX();
769 for(Int_t ibin = 1; ibin <= nbin; ibin++){
770 if(h->GetBinContent(ibin)>0) return ibin;
771 }
772
773 return -1;
774
775}
776
fef6f2b3 777Int_t AliPWGHistoTools::GetHighestNotEmptyBin(const TH1*h) {
4c0d7bc7 778
779 // Return the index of the highest non empty bin in the histo h
780
781 Int_t nbin = h->GetNbinsX();
782 for(Int_t ibin = nbin; ibin > 0; ibin--){
783 if(h->GetBinContent(ibin)>0) return ibin;
784 }
785
786 return -1;
787
788}
789
fef6f2b3 790void AliPWGHistoTools::GetResiduals(const TGraphErrors * gdata, const TF1 * func, TH1F ** hres, TGraphErrors ** gres) {
4c0d7bc7 791
792 // Returns a graph of residuals vs point and the res/err distribution
793
794 Int_t npoint = gdata->GetN();
795
796 (*gres) =new TGraphErrors();
797 (*hres) = new TH1F(TString("hres_")+gdata->GetName()+"-"+func->GetName(),
798 TString("hres_")+gdata->GetName()+"-"+func->GetName(),
799 20,-5,5);
800
801
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);
809
810 }
811
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());
819
820
821
822}
823
fef6f2b3 824void AliPWGHistoTools::GetResiduals(const TH1F* hdata, const TF1 * func, TH1F ** hres, TH1F ** hresVsBin) {
4c0d7bc7 825
826 // Returns an histo of residuals bin by bin and the res/err distribution
827
828 if (!func) {
fef6f2b3 829 Printf("AliPWGHistoTools::GetResiduals: No function provided");
4c0d7bc7 830 return;
831 }
832 if (!hdata) {
fef6f2b3 833 Printf("AliPWGHistoTools::GetResiduals: No data provided");
4c0d7bc7 834 return;
835 }
836
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(),
841 20,-5,5);
842
843 Int_t nbin = hdata->GetNbinsX();
844 for(Int_t ibin = 1; ibin <= nbin; ibin++){
3beb22a2 845 if(!hdata->GetBinContent(ibin)) continue;
4c0d7bc7 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);
852
853 }
854
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() );
862
863}
864
fef6f2b3 865void AliPWGHistoTools::GetYield(TH1* h, TF1 * f, Double_t &yield, Double_t &yieldError, Float_t min, Float_t max,
4c0d7bc7 866 Double_t *partialYields, Double_t *partialYieldsErrors){
867
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
870 // range.
871
872 // Partial yields are also returned if the corresponding pointers are non null
873
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);
878
879 Double_t integralFromHistoError ;
57b5143c 880 Double_t integralFromHisto = DoIntegral(h,bin1,bin2,-1,-1,-1,-1,integralFromHistoError,"width",1);
4c0d7bc7 881
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;
169d33f6 885 Double_t integralAboveError = max > bin2Edge ? f->IntegralError(bin2Edge,max) : 0;
4c0d7bc7 886
3beb22a2 887// cout << "GetYield INFO" << endl;
888// cout << " " << bin1Edge << " " << bin2Edge << endl;
889// cout << " " << integralFromHisto << " " << integralBelow << " " << integralAbove << endl;
890// cout << " " << integralFromHistoError << " " << integralBelowError << " " << integralAboveError << endl;
4c0d7bc7 891
892 if(partialYields) {
893 partialYields[0] = integralFromHisto;
894 partialYields[1] = integralBelow;
895 partialYields[2] = integralAbove;
896 }
897 if(partialYieldsErrors) {
898 partialYieldsErrors[0] = integralFromHistoError;
899 partialYieldsErrors[1] = integralBelowError;
900 partialYieldsErrors[2] = integralAboveError;
901 }
902 yield = integralFromHisto+integralBelow+integralAbove;
903 yieldError = TMath::Sqrt(integralFromHistoError*integralFromHistoError+
904 integralBelowError*integralBelowError+
905 integralAboveError*integralAboveError);
906
907}
908
fef6f2b3 909TGraphErrors * AliPWGHistoTools::DivideGraphByFunc(const TGraphErrors * g, const TF1 * f, Bool_t invert){
4c0d7bc7 910
911 // Divides g/f. If invert == true => f/g
912
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);
90421222 919 if(f->Eval(x) && strcmp(g->ClassName(),"TGraphAsymmErrors")) gRatio->SetPointError(ipoint, 0, g->GetEY()[ipoint]/f->Eval(x));
4c0d7bc7 920 // cout << x << " " << g->GetY()[ipoint] << " " << f->Eval(x) << endl;
921
922 }
923 gRatio->SetMarkerStyle(20);
924 //gRatio->Print();
925 return gRatio;
926
927}
928
fef6f2b3 929TGraphErrors * AliPWGHistoTools::Divide2Graphs(const TGraphErrors * g1, const TGraphErrors * g2){
90421222 930
931 // Divides g1/g2, looks for point with very close centers
932 Int_t ipoint=0;
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;
fef6f2b3 942 Double_t eratio = 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
947 : 0;
948 }
90421222 949 gRatio->SetPoint (ipoint, x1, ratio);
950 gRatio->SetPointError(ipoint, 0, eratio);
951 ipoint++;
952 cout << ipoint << " [" << x1 << "] " << g1->GetY()[ipoint1] << "/" << g2->GetY()[ipoint2] << " = " << ratio <<"+-"<<eratio<< endl;
953
954 // cout << x << " " << g->GetY()[ipoint] << " " << f->Eval(x) << endl;
955 }
956
957 }
958 }
959 gRatio->SetMarkerStyle(20);
960 //gRatio->Print();
961 return gRatio;
962
fef6f2b3 963}
964TGraphErrors * AliPWGHistoTools::Add2Graphs(const TGraphErrors * g1, const TGraphErrors * g2){
965
966 // Sums g1/g2, looks for point with very close centers
967 Int_t ipoint=0;
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];
977 Double_t esum = 0;
978 if(g1->InheritsFrom("TGraphErrors") && g2->InheritsFrom("TGraphErrors")) {
979 esum =
980 TMath::Sqrt(g1->GetEY()[ipoint1]*g1->GetEY()[ipoint1] +
981 g2->GetEY()[ipoint2]*g2->GetEY()[ipoint2] ) ;
982
983 }
984 gSum->SetPoint (ipoint, x1, sum);
985 gSum->SetPointError(ipoint, 0, esum);
986 ipoint++;
987 cout << ipoint << " [" << x1 << "] " << g1->GetY()[ipoint1] << "+" << g2->GetY()[ipoint2] << " = " << sum <<"+-"<<esum<< endl;
988
989 // cout << x << " " << g->GetY()[ipoint] << " " << f->Eval(x) << endl;
990 }
991
992 }
993 }
994 gSum->SetMarkerStyle(20);
995 //gSum->Print();
996 return gSum;
997
90421222 998}
999
fef6f2b3 1000TGraphErrors * AliPWGHistoTools::DivideGraphByHisto(const TGraphErrors * g, TH1 * h, Bool_t invert){
4c0d7bc7 1001
1002 // Divides g/h. If invert == true => h/g
1003
169d33f6 1004 Bool_t skipError = kFALSE;
1005 if(!strcmp(g->ClassName(),"TGraph")) skipError = kTRUE;
1006 if(!strcmp(g->ClassName(),"TGraphAsymmErrors")) skipError = kTRUE;
1007 if(skipError) {
1008 Printf("WARNING: Skipping graph errors");
1009 }
4c0d7bc7 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];
169d33f6 1015 Double_t yje = skipError ? 0 : g->GetEY()[ipoint];
4c0d7bc7 1016
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);
1021
4c0d7bc7 1022
1023
3beb22a2 1024 if (!yd) continue;
4c0d7bc7 1025
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 );
1028 continue;
1029
1030 }
1031
1032 Double_t ratio = invert ? yd/yj : yj/yd;
1033
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);
1037 }
1038
1039 return gRatio;
1040
1041
1042}
1043
fef6f2b3 1044TH1F * AliPWGHistoTools::DivideHistoByFunc(TH1F * h, TF1 * f, Bool_t invert){
4c0d7bc7 1045
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
1049
1050 // Prepare histo for ratio
1051 TH1F * hRatio = (TH1F*) h->Clone(TString("hRatio_")+h->GetName()+"_"+f->GetName());
1052 hRatio->Reset();
1053 // Set y title
1054 if(!invert) hRatio->SetYTitle(TString(h->GetName())+"/"+f->GetName());
1055 else hRatio->SetYTitle(TString(f->GetName())+"/"+h->GetName());
1056
1057 // Loop over all bins
1058 Int_t nbin = hRatio->GetNbinsX();
1059
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);
1070 }
1071
1072 return hRatio;
1073
1074}
ee08b77d 1075
fef6f2b3 1076void AliPWGHistoTools::WeightedMean(Int_t npoints, const Double_t *x, const Double_t *xerr, Double_t &mean, Double_t &meanerr){
ee08b77d 1077
3beb22a2 1078 // Performs the weighted mean of npoints numbers in x with errors in xerr
ee08b77d 1079
1080 mean = 0;
1081 meanerr = 0;
1082
1083 Double_t sumweight = 0;
1084
1085 for (Int_t ipoint = 0; ipoint < npoints; ipoint++){
1086
1087 Double_t xerr2 = xerr[ipoint]*xerr[ipoint];
1088 if(xerr2>0){
1089 // cout << "xe2 " << xerr2 << endl;
1090 Double_t weight = 1. / xerr2;
1091 sumweight += weight;
1092 mean += weight * x[ipoint];
3725266c 1093 }// else cout << " Skipping " << ipoint << endl;
ee08b77d 1094
1095 }
1096
1097
1098 if(sumweight){
1099 mean /= sumweight;
1100 meanerr = TMath::Sqrt(1./ sumweight);
1101 }
1102 else {
3725266c 1103 // cout << " No sumweight" << endl;
ee08b77d 1104 mean = 0;
1105 meanerr = 0;
1106 }
1107
1108
1109}
3725266c 1110
fef6f2b3 1111TH1 * AliPWGHistoTools::GetRelativeError(TH1 * h){
57b5143c 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");
1114 hrel->Reset();
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);
1119 }
1120
1121 return hrel;
1122}
1123
1124
fef6f2b3 1125void AliPWGHistoTools::GetValueAndError(TH1 * hdest, const TH1 * hvalue, const TH1 * herror, Bool_t isPercentError) {
3725266c 1126
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 %
1131
1132 if(hdest == NULL){
fef6f2b3 1133 Printf("AliPWGHistoTools::GetValueAndError Errror: hdest is null");
3725266c 1134 return;
1135 }
1136
1137
1138 Int_t nbin = hdest->GetNbinsX();
1139 Int_t nBinSourceVal = hvalue->GetNbinsX();
1140 Int_t nBinSourceErr = herror->GetNbinsX();
1141
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
1147 // Value
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);
1155 foundValue = kTRUE;
1156 break;
1157 }
1158 }
1159 // if (!foundValue){
fef6f2b3 1160 // Printf("AliPWGHistoTools::GetValueAndError: Error: cannot find matching value source bin for destination %d",iBinDest);
3725266c 1161 // }
1162
1163 // Error
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;
1171
1172 hdest->SetBinError(iBinDest, isPercentError ? error * hdest->GetBinContent(iBinDest) : error);
1173 foundError=kTRUE;
1174 break;
1175 }
1176 }
1177 // if (!foundError ){
fef6f2b3 1178 // Printf("AliPWGHistoTools::GetValueAndError: Error: cannot find matching error source bin for destination %d",iBinDest);
3725266c 1179 // }
1180 }
1181
1182
1183}
1184
fef6f2b3 1185void AliPWGHistoTools::AddHisto(TH1 * hdest, const TH1* hsource, Bool_t getMirrorBins) {
3725266c 1186
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
1191 // particles).
1192
1193
1194 if (hdest == NULL) {
1195 Printf("Error: hdest is NULL\n");
37e1d042 1196 return;
3725266c 1197 }
1198 if (hsource == NULL) {
1199 Printf("Error: hsource is NULL\n");
37e1d042 1200 return;
3725266c 1201 }
1202
1203 Int_t nBinSource = hsource->GetNbinsX();
1204 Int_t nBinDest = hdest->GetNbinsX();
1205
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);
1224 found = kTRUE;
1225
1226 break;
1227 }
1228 }
77233d47 1229 // if (!found){
1230 // Printf("Error: cannot find matching source bin for destination %d",iBinDest);
1231 // }
3725266c 1232 }
1233
1234
1235}
1236
fef6f2b3 1237void AliPWGHistoTools::GetHistoCombinedErrors(TH1 * hdest, const TH1 * h1) {
058137a6 1238
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
1242
1243 Int_t nbin = hdest->GetNbinsX();
90421222 1244 for(Int_t ibin = 1; ibin <= nbin; ibin++){
058137a6 1245 Double_t e1 = hdest->GetBinError(ibin);
1246 Double_t e2 = h1->GetBinError(ibin);
1247 hdest->SetBinError(ibin, TMath::Sqrt(e1*e1+e2*e2));
1248 }
1249
90421222 1250
058137a6 1251}
d8e6677d 1252
fef6f2b3 1253TH1F * AliPWGHistoTools::DivideHistosDifferentBins(const TH1F* h1, const TH1F* h2) {
d8e6677d 1254 // Divides 2 histos even if they have a different binning. Finds
1255 // overlapping bins and divides them
1256
1257 // 1. clone histo
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);
1276 Double_t ratio=0.;
1277 Double_t eratio=0.;
1278 if(numer>0. && denom>0.){
1279 ratio=numer/denom;
1280 eratio=ratio*TMath::Sqrt((enumer/numer)*(enumer/numer)+(edenom/denom)*(edenom/denom));
1281 }
1282 hRatio->SetBinContent(iBin,ratio);
1283 hRatio->SetBinError(iBin,eratio);
1284 break;
1285 }
1286 }
1287 }
1288 return hRatio;
1289}
57b5143c 1290
fef6f2b3 1291Double_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 ,
57b5143c 1292 Option_t *option, Bool_t doError)
1293{
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;
1304 } else {
1305 biny1 = 0; biny2 = 0;
1306 }
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;
1311 } else {
1312 binz1 = 0; binz2 = 0;
1313 }
1314
1315 // - Loop on bins in specified range
1316 TString opt = option;
1317 opt.ToLower();
1318 Bool_t width = kFALSE;
1319 if (opt.Contains("width")) width = kTRUE;
1320
1321
1322 Double_t dx = 1.;
1323 Double_t dy = 1.;
1324 Double_t dz = 1.;
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);
1336 if (doError) {
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);
1339 }
1340 // cout << h->GetBinContent(bin) << " " << h->GetBinError(bin) << " " << dx*dy*dz << " " << integral << " +- " << igerr2 << endl;
1341
1342 }
1343 }
1344 }
1345
1346 if (doError) error = TMath::Sqrt(igerr2);
1347 return integral;
1348}
90421222 1349
fef6f2b3 1350Double_t AliPWGHistoTools::dMtdptFunction(Double_t *x, Double_t *p) {
90421222 1351
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]
1355 Double_t pt = x[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){
fef6f2b3 1360 Printf("AliPWGHistoTools::dMtdptFunction: ERROR: fdNdptForETCalc not defined");
90421222 1361 return 0;
1362 }
1363 Double_t dNdpt = fdNdptForETCalc->Eval(pt);
1364 return dNdpt*mt*jacobian; // FIXME: do I have to normalize somehow?
1365
1366}
1367
fef6f2b3 1368Double_t AliPWGHistoTools::GetdMtdEta(TH1 *hData, TF1 * fExtrapolation, Double_t mass) {
90421222 1369 // Computes dMtdEta integrating dN/dptdy with the proper weights and jacobian.
5e1a7931 1370 Printf("WARNING ALIBWTOOLS::GetdMtdEta: ONLY USING FUNCTION FOR THE TIME BEING, hData");
1371 if(!hData) {
1372 Printf("hData not set");
1373 }
90421222 1374
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);
1380 // Integrate it
1381 Double_t dMtdEta = funcdMtdPt->Integral(0,100);
1382 // Clean up
1383 fdNdptForETCalc=0;
1384 delete funcdMtdPt;
1385 //return
1386 return dMtdEta;
1387
1388}