]>
Commit | Line | Data |
---|---|---|
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 | ||
22 | using namespace std; | |
23 | ||
4c0d7bc7 | 24 | |
fef6f2b3 | 25 | ClassImp(AliPWGHistoTools) |
4c0d7bc7 | 26 | |
fef6f2b3 | 27 | TF1 * AliPWGHistoTools::fdNdptForETCalc = 0; |
90421222 | 28 | |
fef6f2b3 | 29 | AliPWGHistoTools::AliPWGHistoTools() { |
4c0d7bc7 | 30 | // ctor |
31 | } | |
32 | ||
fef6f2b3 | 33 | AliPWGHistoTools::~AliPWGHistoTools(){ |
4c0d7bc7 | 34 | // dtor |
35 | } | |
36 | ||
fef6f2b3 | 37 | TH1 * 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 | 70 | TH1 * 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 | 105 | TH1 * 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 | 145 | TH1 * 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 | 182 | TGraphErrors * 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 | 211 | TH1F * 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 | 243 | TGraphErrors * 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 | 270 | TH1F * 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 | 386 | void 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; | |
1fe08fa5 | 419 | if(hData->GetBinContent(ibin)) { |
420 | mean = mean + (hData->GetBinCenter(ibin) * hData->GetBinWidth(ibin)* hData->GetBinContent(ibin)); | |
421 | err2 = err2 + TMath::Power(hData->GetBinError(ibin) * hData->GetBinCenter(ibin) * hData->GetBinWidth(ibin),2); | |
422 | integral = integral + hData->GetBinContent(ibin) * hData->GetBinWidth(ibin); | |
423 | } | |
424 | else { | |
425 | Double_t locMin = hData->GetBinLowEdge(ibin); | |
426 | Double_t locMax = hData->GetBinLowEdge(ibin) + hData->GetBinWidth(ibin); | |
427 | mean += fExtrapolation->Mean(locMin,locMax)*fExtrapolation->Integral(locMin,locMax); | |
428 | Double_t deltaIntegral = fExtrapolation->Integral(locMin,locMax); | |
429 | integral += deltaIntegral; | |
430 | cout << "WARNING: bin " << ibin << " is empty, patching with function (+" << deltaIntegral<<")" << endl; | |
431 | } | |
ddecf727 | 432 | } |
433 | cout << " Data "<< mean << " " << integral << endl; | |
434 | ||
435 | mean = mean/integral; | |
ffcaf7be | 436 | error = TMath::Sqrt(err2)/integral; |
ddecf727 | 437 | |
4c0d7bc7 | 438 | |
ddecf727 | 439 | } |
440 | ||
fef6f2b3 | 441 | TH1F * AliPWGHistoTools::CombineHistos(const TH1 * h1, TH1 * h2, const TH1* htemplate, Float_t renorm1){ |
4c0d7bc7 | 442 | // Combine two histos. This assumes the histos have the same binning |
443 | // in the overlapping region. It computes the arithmetic mean in the | |
444 | // overlapping region and assigns as an error the relative error | |
445 | // h2. TO BE IMPROVED | |
446 | ||
447 | TH1F * hcomb = (TH1F*) htemplate->Clone(TString(h1->GetName())+"_"+h2->GetName()); | |
448 | ||
449 | TH1F * h1local = (TH1F*) h1->Clone(); | |
450 | h1local->Scale(renorm1); | |
451 | ||
452 | Int_t nBinComb = hcomb->GetNbinsX(); | |
453 | for(Int_t ibin = 1; ibin <= nBinComb; ibin++){ | |
454 | Int_t ibin1 = h1local->FindBin(hcomb->GetBinCenter(ibin)); | |
455 | Int_t ibin2 = h2->FindBin(hcomb->GetBinCenter(ibin)); | |
456 | ||
3beb22a2 | 457 | if (!h1local->GetBinContent(ibin1) && !h2->GetBinContent(ibin2) ) { |
4c0d7bc7 | 458 | // None has data: go to next bin |
459 | hcomb->SetBinContent(ibin,0); | |
460 | hcomb->SetBinError (ibin,0); | |
3beb22a2 | 461 | } else if(h1local->GetBinContent(ibin1) && !h2->GetBinContent(ibin2)) { |
4c0d7bc7 | 462 | // take data from h1local: |
463 | hcomb->SetBinContent(ibin,h1local->GetBinContent(ibin1)); | |
464 | hcomb->SetBinError (ibin,h1local->GetBinError(ibin1)); | |
3beb22a2 | 465 | } else if(!h1local->GetBinContent(ibin1) && h2->GetBinContent(ibin2)) { |
4c0d7bc7 | 466 | // take data from h2: |
467 | hcomb->SetBinContent(ibin,h2->GetBinContent(ibin2)); | |
468 | hcomb->SetBinError (ibin,h2->GetBinError(ibin2)); | |
469 | } else { | |
470 | hcomb->SetBinContent(ibin,(h1local->GetBinContent(ibin1) +h2->GetBinContent(ibin2))/2); | |
471 | // hcomb->SetBinError (ibin,h1local->GetBinError(ibin1)/h1local->GetBinContent(ibin1)*hcomb->GetBinContent(ibin)); | |
472 | hcomb->SetBinError (ibin,h2->GetBinError(ibin2)/h2->GetBinContent(ibin2)*hcomb->GetBinContent(ibin)); | |
473 | } | |
474 | ||
475 | ||
476 | } | |
477 | ||
478 | ||
479 | hcomb->SetMarkerStyle(h1local->GetMarkerStyle()); | |
480 | hcomb->SetMarkerColor(h1local->GetMarkerColor()); | |
481 | hcomb->SetLineColor (h1local->GetLineColor()); | |
482 | ||
483 | hcomb->SetXTitle(h1local->GetXaxis()->GetTitle()); | |
484 | hcomb->SetYTitle(h1local->GetYaxis()->GetTitle()); | |
485 | delete h1local; | |
486 | return hcomb; | |
487 | ||
488 | } | |
489 | ||
490 | ||
fef6f2b3 | 491 | void AliPWGHistoTools::GetFromHistoGraphDifferentX(const TH1F * h, TF1 * f, TGraphErrors ** gBarycentre, TGraphErrors ** gXlw) { |
4c0d7bc7 | 492 | |
493 | // Computes the baycentre in each bin and the xlw as defined in NIMA | |
494 | // 355 - 541 using f. Returs 2 graphs with the same y content of h | |
495 | // but with a different x (barycentre and xlw) | |
496 | ||
497 | Int_t nbin = h->GetNbinsX(); | |
498 | ||
499 | (*gBarycentre) = new TGraphErrors(); | |
500 | (*gXlw) = new TGraphErrors(); | |
501 | ||
502 | Int_t ipoint = 0; | |
503 | for(Int_t ibin = 1; ibin <= nbin; ibin++){ | |
504 | Float_t min = h->GetBinLowEdge(ibin); | |
505 | Float_t max = h->GetBinLowEdge(ibin+1); | |
506 | Double_t xerr = 0; | |
507 | Double_t xbar = f->Mean(min,max); | |
c3c47c65 | 508 | // compute xLW |
4c0d7bc7 | 509 | Double_t temp = 1./(max-min) * f->Integral(min,max); |
510 | Double_t epsilon = 0.000000001; | |
511 | Double_t increment = 0.0000000001; | |
c3c47c65 | 512 | Double_t xLW = min; |
4c0d7bc7 | 513 | |
c3c47c65 | 514 | while ((f->Eval(xLW)- temp) > epsilon) { |
515 | xLW += increment; | |
516 | if(xLW > max) { | |
517 | Printf("Cannot find xLW"); | |
4c0d7bc7 | 518 | break; |
519 | } | |
520 | } | |
521 | ||
522 | if (h->GetBinContent(ibin)!=0) { | |
523 | (*gBarycentre)->SetPoint (ipoint, xbar, h->GetBinContent(ibin)); | |
524 | (*gBarycentre)->SetPointError(ipoint, xerr, h->GetBinError(ibin)); | |
c3c47c65 | 525 | (*gXlw) ->SetPoint (ipoint, xLW, h->GetBinContent(ibin)); |
4c0d7bc7 | 526 | (*gXlw) ->SetPointError(ipoint, xerr, h->GetBinError(ibin)); |
527 | ipoint++; | |
528 | } | |
529 | } | |
530 | ||
531 | (*gBarycentre)->SetMarkerStyle(h->GetMarkerStyle()); | |
532 | (*gBarycentre)->SetMarkerColor(h->GetMarkerColor()); | |
533 | (*gBarycentre)->SetLineColor(h->GetLineColor()); | |
534 | ||
535 | (*gBarycentre)->SetTitle(h->GetTitle()); | |
536 | (*gBarycentre)->SetName(TString("g_")+h->GetName()); | |
537 | ||
538 | (*gXlw)->SetMarkerStyle(h->GetMarkerStyle()); | |
539 | (*gXlw)->SetMarkerColor(h->GetMarkerColor()); | |
540 | (*gXlw)->SetLineColor(h->GetLineColor()); | |
541 | (*gXlw)->SetTitle(h->GetTitle()); | |
542 | (*gXlw)->SetName(TString("g_")+h->GetName()); | |
543 | ||
544 | ||
545 | } | |
546 | ||
547 | ||
fef6f2b3 | 548 | Float_t AliPWGHistoTools::GetMean(TH1F * h, Float_t min, Float_t max, Float_t * error) { |
4c0d7bc7 | 549 | |
3472a634 | 550 | // Get the mean of histo in a range; root is not reliable in sub |
551 | // ranges with variable binning. | |
4c0d7bc7 | 552 | Int_t minBin = h->FindBin(min); |
ddecf727 | 553 | Int_t maxBin = h->FindBin(max-0.00001); |
4c0d7bc7 | 554 | |
555 | Float_t mean = 0 ; | |
556 | Float_t integral = 0; | |
ddecf727 | 557 | Float_t err2 = 0; |
558 | for(Int_t ibin = minBin; ibin <= maxBin; ibin++){ | |
4c0d7bc7 | 559 | mean = mean + (h->GetBinCenter(ibin) * h->GetBinWidth(ibin)* h->GetBinContent(ibin)); |
ddecf727 | 560 | err2 = err2 + TMath::Power(h->GetBinError(ibin) * h->GetBinCenter(ibin) * h->GetBinWidth(ibin),2); |
4c0d7bc7 | 561 | integral = integral + h->GetBinContent(ibin) * h->GetBinWidth(ibin); |
562 | } | |
563 | ||
ddecf727 | 564 | Float_t value = mean/integral; |
565 | if (error) (*error) = TMath::Sqrt(err2); | |
566 | return value; | |
4c0d7bc7 | 567 | |
568 | ||
569 | } | |
570 | ||
fef6f2b3 | 571 | void AliPWGHistoTools::GetMean(TF1 * func, Float_t &mean, Float_t &error, Float_t min, Float_t max, Int_t normPar) { |
4c0d7bc7 | 572 | |
3472a634 | 573 | // Get the mean of function in a range; If normPar is >= 0, it means |
574 | // that the function is defined such that par[normPar] is its | |
575 | // integral. In this case the error on meanpt can be calculated | |
576 | // correctly. Otherwise, the function is normalized in get moment, | |
577 | // but the error is not computed correctly. | |
4c0d7bc7 | 578 | |
3472a634 | 579 | return GetMoment("fMean", TString("x*")+func->GetExpFormula(), func, mean, error, min, max, normPar); |
4c0d7bc7 | 580 | |
581 | } | |
582 | ||
fef6f2b3 | 583 | void AliPWGHistoTools::GetMeanSquare(TF1 * func, Float_t &mean, Float_t &error, Float_t min, Float_t max, Int_t normPar) { |
4c0d7bc7 | 584 | |
3472a634 | 585 | // Get the mean2 of function in a range; If normPar is >= 0, it means |
586 | // that the function is defined such that par[normPar] is its | |
587 | // integral. In this case the error on meanpt can be calculated | |
588 | // correctly. Otherwise, the function is normalized in get moment, | |
589 | // but the error is not computed correctly. | |
4c0d7bc7 | 590 | |
3472a634 | 591 | return GetMoment("fMean2", TString("x*x*")+func->GetExpFormula(), func, mean, error, min, max, normPar); |
4c0d7bc7 | 592 | |
593 | ||
594 | } | |
595 | ||
fef6f2b3 | 596 | void AliPWGHistoTools::GetMoment(TString name, TString var, TF1 * func, Float_t &mean, Float_t &error, Float_t min, Float_t max, Int_t normPar) { |
4c0d7bc7 | 597 | |
598 | // returns the integral of a function derived from func by prefixing some operation. | |
3472a634 | 599 | // the derived function MUST have the same parameter in the same order |
4c0d7bc7 | 600 | // Used as a base method for mean and mean2 |
3472a634 | 601 | // If normPar is >= 0, it means that the function is defined such |
602 | // that par[normPar] is its integral. In this case the error on | |
603 | // meanpt can be calculated correctly. Otherwise, the function is | |
604 | // normalized using its numerical integral, but the error is not computed | |
605 | // correctly. | |
606 | ||
607 | // TODO: | |
608 | // - improve to propagate error also in the case you need the | |
609 | // numerical integrals (will be rather slow) | |
610 | // - this version assumes that func is defined using a | |
611 | // TFormula. Generalize to the case of a C++ function | |
612 | ||
fef6f2b3 | 613 | if (normPar<0) Printf("AliPWGHistoTools::GetMoment: Warning: If normalization is required, the error may bot be correct"); |
614 | if (!strcmp(func->GetExpFormula(),"")) Printf("AliPWGHistoTools::GetMoment: Warning: Empty formula in the base function"); | |
4c0d7bc7 | 615 | Int_t npar = func->GetNpar(); |
616 | ||
3472a634 | 617 | // Definition changes according to the value of normPar |
618 | TF1 * f = normPar < 0 ? | |
619 | new TF1(name, var) : // not normalized | |
620 | new TF1(name, var+Form("/[%d]",normPar)); // normalized with par normPar | |
621 | ||
622 | // integr is used to normalize if no parameter is provided | |
623 | Double_t integr = normPar < 0 ? func->Integral(min,max) : 1; | |
4c0d7bc7 | 624 | |
625 | // The parameter of the function used to compute the mean should be | |
626 | // the same as the parent function: fixed if needed and they should | |
627 | // also have the same errors. | |
628 | ||
629 | // cout << "npar :" << npar << endl; | |
630 | ||
631 | for(Int_t ipar = 0; ipar < npar; ipar++){ | |
632 | Double_t parmin, parmax; | |
633 | Double_t value = func->GetParameter(ipar); | |
3472a634 | 634 | f->SetParameter(ipar,value); |
4c0d7bc7 | 635 | func->GetParLimits(ipar, parmin, parmax); |
636 | if ( parmin == parmax ) { | |
3beb22a2 | 637 | // if ( parmin || (parmin == 1 && !value) ) { // not sure we I check parmin == 1 here. |
638 | 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 | 639 | f->FixParameter(ipar,func->GetParameter(ipar)); |
640 | // cout << "Fixing " << ipar << "("<<value<<","<<parmin<<","<<parmax<<")"<<endl; | |
641 | } | |
642 | else { | |
643 | f->SetParError (ipar,func->GetParError(ipar) ); | |
644 | // cout << "Setting Err" << ipar << "("<<func->GetParError(ipar)<<")"<<endl; | |
645 | } | |
646 | } | |
647 | else { | |
648 | f->SetParError (ipar,func->GetParError(ipar) ); | |
649 | // cout << "Setting Err" << ipar << "("<<func->GetParError(ipar)<<")"<<endl; | |
650 | } | |
651 | } | |
3472a634 | 652 | |
653 | // f->Print(); | |
654 | // cout << "----" << endl; | |
655 | // func->Print(); | |
656 | ||
657 | mean = normPar < 0 ? f->Integral (min,max)/integr : f->Integral (min,max); | |
658 | error = normPar < 0 ? f->IntegralError(min,max)/integr : f->IntegralError(min,max); | |
4c0d7bc7 | 659 | // cout << "Mean " << mean <<"+-"<< error<< endl; |
660 | // cout << "Integral Error " << error << endl; | |
661 | ||
662 | } | |
663 | ||
4c0d7bc7 | 664 | |
665 | ||
fef6f2b3 | 666 | Bool_t AliPWGHistoTools::Fit (TH1 * h1, TF1* func, Float_t min, Float_t max) { |
4c0d7bc7 | 667 | |
668 | // Fits h1 with func, preforms several checks on the quality of the | |
669 | // fit and tries to improve it. If the fit is not good enough, it | |
670 | // returs false. | |
671 | ||
672 | Double_t amin; Double_t edm; Double_t errdef; Int_t nvpar; Int_t nparx; | |
673 | TVirtualFitter *fitter; | |
674 | cout << "--- Fitting : " << h1->GetName() << " ["<< h1->GetTitle() <<"] ---"<< endl; | |
675 | ||
676 | h1-> Fit(func,"IME0","",min,max); | |
677 | Int_t fitResult = h1-> Fit(func,"IME0","",min,max); | |
678 | // h1-> Fit(func,"0","",min,max); | |
679 | // Int_t fitResult = h1-> Fit(func,"0IE","",min,max); | |
680 | ||
681 | ||
682 | // From TH1: | |
683 | // The fitStatus is 0 if the fit is OK (i.e no error occurred). The | |
684 | // value of the fit status code is negative in case of an error not | |
685 | // connected with the minimization procedure, for example when a wrong | |
686 | // function is used. Otherwise the return value is the one returned | |
687 | // from the minimization procedure. When TMinuit (default case) or | |
688 | // Minuit2 are used as minimizer the status returned is : fitStatus = | |
689 | // migradResult + 10*minosResult + 100*hesseResult + | |
690 | // 1000*improveResult. TMinuit will return 0 (for migrad, minos, | |
691 | // hesse or improve) in case of success and 4 in case of error (see | |
692 | // the documentation of TMinuit::mnexcm). So for example, for an error | |
693 | // only in Minos but not in Migrad a fitStatus of 40 will be returned. | |
694 | // Minuit2 will return also 0 in case of success and different values | |
695 | // in migrad minos or hesse depending on the error. See in this case | |
696 | // the documentation of Minuit2Minimizer::Minimize for the | |
697 | // migradResult, Minuit2Minimizer::GetMinosError for the minosResult | |
698 | // and Minuit2Minimizer::Hesse for the hesseResult. If other | |
699 | // minimizers are used see their specific documentation for the status | |
700 | // code returned. For example in the case of Fumili, for the status | |
701 | // returned see TFumili::Minimize. | |
702 | ||
703 | ||
704 | if( gMinuit->fLimset ) { | |
fef6f2b3 | 705 | Printf("ERROR: AliPWGHistoTools: Parameters at limits"); |
4c0d7bc7 | 706 | return kFALSE; |
707 | } | |
708 | ||
709 | ||
710 | /// | |
711 | fitter = TVirtualFitter::GetFitter(); | |
712 | Int_t fitStat = fitter->GetStats(amin, edm, errdef, nvpar, nparx); | |
713 | ||
714 | if( ( (fitStat < 3 && gMinuit->fCstatu != "UNCHANGED ")|| (edm > 1e6) || (fitResult !=0 && fitResult < 4000) ) && | |
715 | TString(gMinuit->fCstatu) != "SUCCESSFUL" && | |
716 | TString(gMinuit->fCstatu) != "CONVERGED " ) { | |
717 | if(fitStat < 3 && gMinuit->fCstatu != "UNCHANGED ") { | |
fef6f2b3 | 718 | Printf("WARNING: AliPWGHistoTools: Cannot properly compute errors"); |
4c0d7bc7 | 719 | if (fitStat == 0) Printf(" not calculated at all"); |
720 | if (fitStat == 1) Printf(" approximation only, not accurate"); | |
721 | if (fitStat == 2) Printf(" full matrix, but forced positive-definite"); | |
722 | } | |
723 | ||
724 | if (edm > 1e6) { | |
725 | ||
fef6f2b3 | 726 | Printf("WARNING: AliPWGHistoTools: Huge EDM (%f): Fit probably failed!", edm); |
4c0d7bc7 | 727 | } |
728 | if (fitResult != 0) { | |
fef6f2b3 | 729 | Printf("WARNING: AliPWGHistoTools: Fit Result (%d)", fitResult); |
4c0d7bc7 | 730 | } |
731 | ||
fef6f2b3 | 732 | Printf ("AliPWGHistoTools: Trying Again with Strategy = 2"); |
4c0d7bc7 | 733 | |
734 | gMinuit->Command("SET STRATEGY 2"); // more effort | |
735 | fitResult = 0; | |
736 | fitResult = h1-> Fit(func,"0","",min,max); | |
737 | fitResult = h1-> Fit(func,"IME0","",min,max); | |
738 | fitResult = h1-> Fit(func,"IME0","",min,max); | |
739 | ||
740 | fitter = TVirtualFitter::GetFitter(); | |
741 | ||
742 | fitStat = fitter->GetStats(amin, edm, errdef, nvpar, nparx); | |
743 | ||
744 | if(fitStat < 3 && gMinuit->fCstatu != "UNCHANGED ") { | |
fef6f2b3 | 745 | Printf("ERROR: AliPWGHistoTools: Cannot properly compute errors"); |
4c0d7bc7 | 746 | if (fitStat == 0) Printf(" not calculated at all"); |
747 | if (fitStat == 1) Printf(" approximation only, not accurate"); | |
748 | if (fitStat == 2) Printf(" full matrix, but forced positive-definite"); | |
749 | cout << "[" <<gMinuit->fCstatu<<"]" << endl; | |
750 | return kFALSE; | |
751 | } | |
752 | ||
753 | if (edm > 1e6) { | |
fef6f2b3 | 754 | Printf("ERROR: AliPWGHistoTools: Huge EDM (%f): Fit probably failed!", edm); |
4c0d7bc7 | 755 | |
756 | return kFALSE; | |
757 | } | |
758 | if (fitResult != 0) { | |
fef6f2b3 | 759 | Printf("ERROR: AliPWGHistoTools: Fit Result (%d)", fitResult); |
4c0d7bc7 | 760 | |
761 | return kFALSE; | |
762 | } | |
763 | ||
764 | gMinuit->Command("SET STRATEGY 1"); // back to normal value | |
765 | ||
766 | } | |
767 | ||
768 | cout << "---- FIT OK ----" << endl; | |
769 | ||
770 | return kTRUE; | |
771 | ||
772 | } | |
773 | ||
fef6f2b3 | 774 | Int_t AliPWGHistoTools::GetLowestNotEmptyBin(const TH1*h) { |
4c0d7bc7 | 775 | |
776 | // Return the index of the lowest non empty bin in the histo h | |
777 | ||
778 | Int_t nbin = h->GetNbinsX(); | |
779 | for(Int_t ibin = 1; ibin <= nbin; ibin++){ | |
780 | if(h->GetBinContent(ibin)>0) return ibin; | |
781 | } | |
782 | ||
783 | return -1; | |
784 | ||
785 | } | |
786 | ||
fef6f2b3 | 787 | Int_t AliPWGHistoTools::GetHighestNotEmptyBin(const TH1*h) { |
4c0d7bc7 | 788 | |
789 | // Return the index of the highest non empty bin in the histo h | |
790 | ||
791 | Int_t nbin = h->GetNbinsX(); | |
792 | for(Int_t ibin = nbin; ibin > 0; ibin--){ | |
793 | if(h->GetBinContent(ibin)>0) return ibin; | |
794 | } | |
795 | ||
796 | return -1; | |
797 | ||
798 | } | |
799 | ||
fef6f2b3 | 800 | void AliPWGHistoTools::GetResiduals(const TGraphErrors * gdata, const TF1 * func, TH1F ** hres, TGraphErrors ** gres) { |
4c0d7bc7 | 801 | |
802 | // Returns a graph of residuals vs point and the res/err distribution | |
803 | ||
804 | Int_t npoint = gdata->GetN(); | |
805 | ||
806 | (*gres) =new TGraphErrors(); | |
807 | (*hres) = new TH1F(TString("hres_")+gdata->GetName()+"-"+func->GetName(), | |
808 | TString("hres_")+gdata->GetName()+"-"+func->GetName(), | |
809 | 20,-5,5); | |
810 | ||
811 | ||
812 | for(Int_t ipoint = 0; ipoint < npoint; ipoint++){ | |
813 | Float_t x = gdata->GetX()[ipoint]; | |
814 | Float_t res = (gdata->GetY()[ipoint] - func->Eval(x))/func->Eval(x); | |
815 | Float_t err = gdata->GetEY()[ipoint]/func->Eval(x); | |
816 | (*hres)->Fill(res/err); | |
817 | (*gres)->SetPoint(ipoint, x, res/err); | |
818 | // (*gres)->SetPointError(ipoint, 0, err); | |
819 | ||
820 | } | |
821 | ||
822 | (*gres)->SetMarkerStyle(gdata->GetMarkerStyle()); | |
823 | (*gres)->SetMarkerColor(gdata->GetMarkerColor()); | |
824 | (*gres)->SetLineColor (gdata->GetLineColor()); | |
825 | (*gres)->GetHistogram()->GetYaxis()->SetTitle("(data-function)/function"); | |
826 | (*hres)->SetMarkerStyle(gdata->GetMarkerStyle()); | |
827 | (*hres)->SetMarkerColor(gdata->GetMarkerColor()); | |
828 | (*hres)->SetLineColor (gdata->GetLineColor()); | |
829 | ||
830 | ||
831 | ||
832 | } | |
833 | ||
fef6f2b3 | 834 | void AliPWGHistoTools::GetResiduals(const TH1F* hdata, const TF1 * func, TH1F ** hres, TH1F ** hresVsBin) { |
4c0d7bc7 | 835 | |
836 | // Returns an histo of residuals bin by bin and the res/err distribution | |
837 | ||
838 | if (!func) { | |
fef6f2b3 | 839 | Printf("AliPWGHistoTools::GetResiduals: No function provided"); |
4c0d7bc7 | 840 | return; |
841 | } | |
842 | if (!hdata) { | |
fef6f2b3 | 843 | Printf("AliPWGHistoTools::GetResiduals: No data provided"); |
4c0d7bc7 | 844 | return; |
845 | } | |
846 | ||
847 | (*hresVsBin) = (TH1F*) hdata->Clone(TString("hres_")+hdata->GetName()); | |
848 | (*hresVsBin)->Reset(); | |
849 | (*hres) = new TH1F(TString("hres_")+hdata->GetName()+"-"+func->GetName(), | |
850 | TString("hres_")+hdata->GetName()+"-"+func->GetName(), | |
851 | 20,-5,5); | |
852 | ||
853 | Int_t nbin = hdata->GetNbinsX(); | |
854 | for(Int_t ibin = 1; ibin <= nbin; ibin++){ | |
3beb22a2 | 855 | if(!hdata->GetBinContent(ibin)) continue; |
4c0d7bc7 | 856 | Float_t res = (hdata->GetBinContent(ibin) - func->Eval(hdata->GetBinCenter(ibin)) ) / |
857 | func->Eval(hdata->GetBinCenter(ibin)); | |
858 | Float_t err = hdata->GetBinError (ibin) / func->Eval(hdata->GetBinCenter(ibin)); | |
859 | (*hresVsBin)->SetBinContent(ibin,res); | |
860 | (*hresVsBin)->SetBinError (ibin,err); | |
861 | (*hres)->Fill(res/err); | |
862 | ||
863 | } | |
864 | ||
865 | (*hresVsBin)->SetMarkerStyle(hdata->GetMarkerStyle()); | |
866 | (*hresVsBin)->SetMarkerColor(hdata->GetMarkerColor()); | |
867 | (*hresVsBin)->SetLineColor (hdata->GetLineColor() ); | |
868 | (*hresVsBin)->GetYaxis()->SetTitle("(data-function)/function"); | |
869 | (*hres)->SetMarkerStyle(hdata->GetMarkerStyle()); | |
870 | (*hres)->SetMarkerColor(hdata->GetMarkerColor()); | |
871 | (*hres)->SetLineColor (hdata->GetLineColor() ); | |
872 | ||
873 | } | |
874 | ||
fef6f2b3 | 875 | void AliPWGHistoTools::GetYield(TH1* h, TF1 * f, Double_t &yield, Double_t &yieldError, Float_t min, Float_t max, |
4c0d7bc7 | 876 | Double_t *partialYields, Double_t *partialYieldsErrors){ |
877 | ||
878 | // Returns the yield extracted from the data in the histo where | |
879 | // there are points and from the fit to extrapolate, in the given | |
880 | // range. | |
881 | ||
882 | // Partial yields are also returned if the corresponding pointers are non null | |
883 | ||
884 | Int_t bin1 = h->FindBin(min); | |
885 | Int_t bin2 = h->FindBin(max); | |
886 | Float_t bin1Edge = GetLowestNotEmptyBinEdge (h); | |
887 | Float_t bin2Edge = GetHighestNotEmptyBinEdge(h); | |
1fe08fa5 | 888 | Int_t lowestNE = GetLowestNotEmptyBin (h); |
889 | Int_t highestNE = GetHighestNotEmptyBin(h); | |
890 | ||
891 | Double_t integralFromHistoError = 0; | |
892 | Double_t integralFromHisto = 0; | |
893 | // Double_t integralFromHisto = DoIntegral(h,bin1,bin2,-1,-1,-1,-1,integralFromHistoError,"width",1); | |
4c0d7bc7 | 894 | |
1fe08fa5 | 895 | for(Int_t ibin = TMath::Max(bin1,lowestNE); ibin <= TMath::Min(bin2,highestNE); ibin++){ |
896 | if(h->GetBinContent(ibin)) { | |
897 | integralFromHisto = integralFromHisto + h->GetBinContent(ibin) * h->GetBinWidth(ibin); | |
898 | integralFromHistoError = integralFromHistoError + h->GetBinError(ibin)*h->GetBinError(ibin) * h->GetBinWidth(ibin); | |
899 | } | |
900 | else { | |
901 | Double_t locMin = h->GetBinLowEdge(ibin); | |
902 | Double_t locMax = h->GetBinLowEdge(ibin) + h->GetBinWidth(ibin); | |
903 | Double_t deltaIntegral = f->Integral(locMin,locMax); | |
904 | integralFromHisto += deltaIntegral; | |
905 | integralFromHistoError = integralFromHistoError + f->IntegralError(locMin,locMax)*f->IntegralError(locMin,locMax); | |
906 | cout << "WARNING: bin " << ibin << " is empty, patching with function (+" << deltaIntegral<<")" << endl; | |
907 | } | |
908 | } | |
909 | ||
910 | integralFromHistoError = TMath::Sqrt(integralFromHistoError);// ok, this is a bit dumb. Just in case the code above is changed again. | |
911 | ||
4c0d7bc7 | 912 | |
913 | Double_t integralBelow = min < bin1Edge ? f->Integral(min,bin1Edge) : 0; | |
914 | Double_t integralBelowError = min < bin1Edge ? f->IntegralError(min,bin1Edge) : 0; | |
915 | Double_t integralAbove = max > bin2Edge ? f->Integral(bin2Edge,max) : 0; | |
169d33f6 | 916 | Double_t integralAboveError = max > bin2Edge ? f->IntegralError(bin2Edge,max) : 0; |
4c0d7bc7 | 917 | |
3beb22a2 | 918 | // cout << "GetYield INFO" << endl; |
919 | // cout << " " << bin1Edge << " " << bin2Edge << endl; | |
920 | // cout << " " << integralFromHisto << " " << integralBelow << " " << integralAbove << endl; | |
921 | // cout << " " << integralFromHistoError << " " << integralBelowError << " " << integralAboveError << endl; | |
4c0d7bc7 | 922 | |
923 | if(partialYields) { | |
924 | partialYields[0] = integralFromHisto; | |
925 | partialYields[1] = integralBelow; | |
926 | partialYields[2] = integralAbove; | |
927 | } | |
928 | if(partialYieldsErrors) { | |
929 | partialYieldsErrors[0] = integralFromHistoError; | |
930 | partialYieldsErrors[1] = integralBelowError; | |
931 | partialYieldsErrors[2] = integralAboveError; | |
932 | } | |
933 | yield = integralFromHisto+integralBelow+integralAbove; | |
934 | yieldError = TMath::Sqrt(integralFromHistoError*integralFromHistoError+ | |
935 | integralBelowError*integralBelowError+ | |
936 | integralAboveError*integralAboveError); | |
937 | ||
938 | } | |
939 | ||
fef6f2b3 | 940 | TGraphErrors * AliPWGHistoTools::DivideGraphByFunc(const TGraphErrors * g, const TF1 * f, Bool_t invert){ |
4c0d7bc7 | 941 | |
942 | // Divides g/f. If invert == true => f/g | |
943 | ||
944 | TGraphErrors * gRatio = new TGraphErrors(); | |
945 | Int_t npoint = g->GetN(); | |
946 | for(Int_t ipoint = 0; ipoint < npoint; ipoint++){ | |
947 | Double_t x = g->GetX()[ipoint]; | |
948 | Double_t ratio = invert ? f->Eval(x)/g->GetY()[ipoint] :g->GetY()[ipoint]/f->Eval(x); | |
949 | gRatio->SetPoint (ipoint, x, ratio); | |
90421222 | 950 | if(f->Eval(x) && strcmp(g->ClassName(),"TGraphAsymmErrors")) gRatio->SetPointError(ipoint, 0, g->GetEY()[ipoint]/f->Eval(x)); |
4c0d7bc7 | 951 | // cout << x << " " << g->GetY()[ipoint] << " " << f->Eval(x) << endl; |
952 | ||
953 | } | |
954 | gRatio->SetMarkerStyle(20); | |
955 | //gRatio->Print(); | |
956 | return gRatio; | |
957 | ||
958 | } | |
959 | ||
fef6f2b3 | 960 | TGraphErrors * AliPWGHistoTools::Divide2Graphs(const TGraphErrors * g1, const TGraphErrors * g2){ |
90421222 | 961 | |
962 | // Divides g1/g2, looks for point with very close centers | |
963 | Int_t ipoint=0; | |
964 | TGraphErrors * gRatio = new TGraphErrors(); | |
965 | Int_t npoint1 = g1->GetN(); | |
966 | Int_t npoint2 = g2->GetN(); | |
967 | for(Int_t ipoint1 = 0; ipoint1 < npoint1; ipoint1++){ | |
968 | Double_t x1 = g1->GetX()[ipoint1]; | |
969 | for(Int_t ipoint2 = 0; ipoint2 < npoint2; ipoint2++){ | |
970 | Double_t x2 = g2->GetX()[ipoint2]; | |
971 | if((TMath::Abs(x1-x2)/(x1+x2)*2)<0.01) { | |
972 | Double_t ratio = g2->GetY()[ipoint2] ? g1->GetY()[ipoint1]/g2->GetY()[ipoint2] : 0; | |
fef6f2b3 | 973 | Double_t eratio = 0; |
974 | if(g1->InheritsFrom("TGraphErrors") && g2->InheritsFrom("TGraphErrors")) { | |
975 | eratio = g2->GetY()[ipoint2] ? | |
976 | TMath::Sqrt(g1->GetEY()[ipoint1]*g1->GetEY()[ipoint1]/g1->GetY()[ipoint1]/g1->GetY()[ipoint1] + | |
977 | g2->GetEY()[ipoint2]/g2->GetY()[ipoint2]/g2->GetY()[ipoint2] ) * ratio | |
978 | : 0; | |
979 | } | |
90421222 | 980 | gRatio->SetPoint (ipoint, x1, ratio); |
981 | gRatio->SetPointError(ipoint, 0, eratio); | |
982 | ipoint++; | |
983 | cout << ipoint << " [" << x1 << "] " << g1->GetY()[ipoint1] << "/" << g2->GetY()[ipoint2] << " = " << ratio <<"+-"<<eratio<< endl; | |
984 | ||
985 | // cout << x << " " << g->GetY()[ipoint] << " " << f->Eval(x) << endl; | |
986 | } | |
987 | ||
988 | } | |
989 | } | |
990 | gRatio->SetMarkerStyle(20); | |
991 | //gRatio->Print(); | |
992 | return gRatio; | |
993 | ||
fef6f2b3 | 994 | } |
995 | TGraphErrors * AliPWGHistoTools::Add2Graphs(const TGraphErrors * g1, const TGraphErrors * g2){ | |
996 | ||
997 | // Sums g1/g2, looks for point with very close centers | |
998 | Int_t ipoint=0; | |
999 | TGraphErrors * gSum = new TGraphErrors(); | |
1000 | Int_t npoint1 = g1->GetN(); | |
1001 | Int_t npoint2 = g2->GetN(); | |
1002 | for(Int_t ipoint1 = 0; ipoint1 < npoint1; ipoint1++){ | |
1003 | Double_t x1 = g1->GetX()[ipoint1]; | |
1004 | for(Int_t ipoint2 = 0; ipoint2 < npoint2; ipoint2++){ | |
1005 | Double_t x2 = g2->GetX()[ipoint2]; | |
1006 | if((TMath::Abs(x1-x2)/(x1+x2)*2)<0.01) { | |
1007 | Double_t sum = g1->GetY()[ipoint1]+g2->GetY()[ipoint2]; | |
1008 | Double_t esum = 0; | |
1009 | if(g1->InheritsFrom("TGraphErrors") && g2->InheritsFrom("TGraphErrors")) { | |
1010 | esum = | |
1011 | TMath::Sqrt(g1->GetEY()[ipoint1]*g1->GetEY()[ipoint1] + | |
1012 | g2->GetEY()[ipoint2]*g2->GetEY()[ipoint2] ) ; | |
1013 | ||
1014 | } | |
1015 | gSum->SetPoint (ipoint, x1, sum); | |
1016 | gSum->SetPointError(ipoint, 0, esum); | |
1017 | ipoint++; | |
1018 | cout << ipoint << " [" << x1 << "] " << g1->GetY()[ipoint1] << "+" << g2->GetY()[ipoint2] << " = " << sum <<"+-"<<esum<< endl; | |
1019 | ||
1020 | // cout << x << " " << g->GetY()[ipoint] << " " << f->Eval(x) << endl; | |
1021 | } | |
1022 | ||
1023 | } | |
1024 | } | |
1025 | gSum->SetMarkerStyle(20); | |
1026 | //gSum->Print(); | |
1027 | return gSum; | |
1028 | ||
90421222 | 1029 | } |
1030 | ||
fef6f2b3 | 1031 | TGraphErrors * AliPWGHistoTools::DivideGraphByHisto(const TGraphErrors * g, TH1 * h, Bool_t invert){ |
4c0d7bc7 | 1032 | |
1033 | // Divides g/h. If invert == true => h/g | |
1034 | ||
169d33f6 | 1035 | Bool_t skipError = kFALSE; |
1036 | if(!strcmp(g->ClassName(),"TGraph")) skipError = kTRUE; | |
1037 | if(!strcmp(g->ClassName(),"TGraphAsymmErrors")) skipError = kTRUE; | |
1038 | if(skipError) { | |
1039 | Printf("WARNING: Skipping graph errors"); | |
1040 | } | |
4c0d7bc7 | 1041 | TGraphErrors * gRatio = new TGraphErrors(); |
1042 | Int_t npoint = g->GetN(); | |
1043 | for(Int_t ipoint = 0; ipoint < npoint; ipoint++){ | |
1044 | Double_t xj = g->GetX()[ipoint]; | |
1045 | Double_t yj = g->GetY()[ipoint]; | |
169d33f6 | 1046 | Double_t yje = skipError ? 0 : g->GetEY()[ipoint]; |
4c0d7bc7 | 1047 | |
1048 | Int_t binData = h->FindBin(xj); | |
1049 | Double_t yd = h->GetBinContent(binData); | |
1050 | Double_t yde = h->GetBinError(binData); | |
1051 | Double_t xd = h->GetBinCenter(binData); | |
1052 | ||
4c0d7bc7 | 1053 | |
1054 | ||
3beb22a2 | 1055 | if (!yd) continue; |
4c0d7bc7 | 1056 | |
1057 | if (TMath::Abs(xd-xj)/TMath::Abs(xd) > 0.01){ | |
1058 | Printf( "WARNING: bin center (%f) and x graph (%f) are more than 1 %% away, skipping",xd,xj ); | |
1059 | continue; | |
1060 | ||
1061 | } | |
1062 | ||
1063 | Double_t ratio = invert ? yd/yj : yj/yd; | |
1064 | ||
1065 | gRatio->SetPoint(ipoint, xj, ratio); | |
1066 | gRatio->SetPointError(ipoint, 0, TMath::Sqrt(yde*yde/yd/yd + yje*yje/yj/yj)*ratio); | |
1067 | // gRatio->SetPointError(ipoint, 0, yje/yj * ratio); | |
1068 | } | |
1069 | ||
1070 | return gRatio; | |
1071 | ||
1072 | ||
1073 | } | |
1074 | ||
fef6f2b3 | 1075 | TH1F * AliPWGHistoTools::DivideHistoByFunc(TH1F * h, TF1 * f, Bool_t invert){ |
4c0d7bc7 | 1076 | |
1077 | // Divides h/f. If invert == true => f/g | |
1078 | // Performs the integral of f on the bin range to perform the ratio | |
1079 | // Returns a histo with the same binnig as h | |
1080 | ||
1081 | // Prepare histo for ratio | |
1082 | TH1F * hRatio = (TH1F*) h->Clone(TString("hRatio_")+h->GetName()+"_"+f->GetName()); | |
1083 | hRatio->Reset(); | |
1084 | // Set y title | |
1085 | if(!invert) hRatio->SetYTitle(TString(h->GetName())+"/"+f->GetName()); | |
1086 | else hRatio->SetYTitle(TString(f->GetName())+"/"+h->GetName()); | |
1087 | ||
1088 | // Loop over all bins | |
1089 | Int_t nbin = hRatio->GetNbinsX(); | |
1090 | ||
1091 | for(Int_t ibin = 1; ibin <= nbin; ibin++){ | |
1092 | Double_t yhisto = h->GetBinContent(ibin); | |
1093 | Double_t yerror = h->GetBinError(ibin); | |
1094 | Double_t xmin = h->GetBinLowEdge(ibin); | |
1095 | Double_t xmax = h->GetBinLowEdge(ibin+1); | |
1096 | Double_t yfunc = f->Integral(xmin,xmax)/(xmax-xmin); | |
097f6933 | 1097 | Double_t ratio = yfunc&&yhisto ? (invert ? yfunc/yhisto : yhisto/yfunc) : 0 ; |
1098 | Double_t error = yfunc ? yerror/yfunc : 0 ; | |
4c0d7bc7 | 1099 | hRatio->SetBinContent(ibin,ratio); |
1100 | hRatio->SetBinError (ibin,error); | |
1101 | } | |
1102 | ||
1103 | return hRatio; | |
1104 | ||
1105 | } | |
ee08b77d | 1106 | |
fef6f2b3 | 1107 | void AliPWGHistoTools::WeightedMean(Int_t npoints, const Double_t *x, const Double_t *xerr, Double_t &mean, Double_t &meanerr){ |
ee08b77d | 1108 | |
3beb22a2 | 1109 | // Performs the weighted mean of npoints numbers in x with errors in xerr |
ee08b77d | 1110 | |
1111 | mean = 0; | |
1112 | meanerr = 0; | |
1113 | ||
1114 | Double_t sumweight = 0; | |
1115 | ||
1116 | for (Int_t ipoint = 0; ipoint < npoints; ipoint++){ | |
1117 | ||
1118 | Double_t xerr2 = xerr[ipoint]*xerr[ipoint]; | |
1119 | if(xerr2>0){ | |
1120 | // cout << "xe2 " << xerr2 << endl; | |
1121 | Double_t weight = 1. / xerr2; | |
1122 | sumweight += weight; | |
1123 | mean += weight * x[ipoint]; | |
3725266c | 1124 | }// else cout << " Skipping " << ipoint << endl; |
ee08b77d | 1125 | |
1126 | } | |
1127 | ||
1128 | ||
1129 | if(sumweight){ | |
1130 | mean /= sumweight; | |
1131 | meanerr = TMath::Sqrt(1./ sumweight); | |
1132 | } | |
1133 | else { | |
3725266c | 1134 | // cout << " No sumweight" << endl; |
ee08b77d | 1135 | mean = 0; |
1136 | meanerr = 0; | |
1137 | } | |
1138 | ||
1139 | ||
1140 | } | |
3725266c | 1141 | |
fef6f2b3 | 1142 | TH1 * AliPWGHistoTools::GetRelativeError(TH1 * h){ |
57b5143c | 1143 | // Returns an histogram with the same binning as h, filled with the relative error bin by bin |
1144 | TH1 * hrel = (TH1*) h->Clone(TString(h->GetName())+"_rel"); | |
1145 | hrel->Reset(); | |
1146 | Int_t nbin = hrel->GetNbinsX(); | |
1147 | for(Int_t ibin = 1; ibin <= nbin; ibin++){ | |
097f6933 | 1148 | if(h->GetBinError(ibin)){ |
1149 | hrel->SetBinContent(ibin,h->GetBinError(ibin)/h->GetBinContent(ibin)); | |
1150 | } else { | |
1151 | hrel->SetBinContent(ibin,0); | |
1152 | } | |
57b5143c | 1153 | hrel->SetBinError(ibin,0); |
1154 | } | |
1155 | ||
1156 | return hrel; | |
1157 | } | |
1158 | ||
1159 | ||
fef6f2b3 | 1160 | void AliPWGHistoTools::GetValueAndError(TH1 * hdest, const TH1 * hvalue, const TH1 * herror, Bool_t isPercentError) { |
3725266c | 1161 | |
1162 | // Put into source, bin-by-bin, the values from hvalue and the | |
1163 | // errors from content from herror. | |
1164 | // Used mainly to combine histos of systemati errors with their spectra | |
1165 | // Set isPercentError to kTRUE if the error is given in % | |
1166 | ||
1167 | if(hdest == NULL){ | |
fef6f2b3 | 1168 | Printf("AliPWGHistoTools::GetValueAndError Errror: hdest is null"); |
3725266c | 1169 | return; |
1170 | } | |
1171 | ||
1172 | ||
1173 | Int_t nbin = hdest->GetNbinsX(); | |
1174 | Int_t nBinSourceVal = hvalue->GetNbinsX(); | |
1175 | Int_t nBinSourceErr = herror->GetNbinsX(); | |
1176 | ||
1177 | for(Int_t iBinDest = 1; iBinDest <= nbin; iBinDest++){ | |
1178 | Float_t lowPtDest=hdest->GetBinLowEdge(iBinDest); | |
1179 | Float_t binWidDest=hdest->GetBinWidth(iBinDest); | |
1180 | // Loop over Source bins and find overlapping bins to Dest | |
1181 | // First value then error | |
1182 | // Value | |
1183 | Bool_t foundValue = kFALSE; | |
1184 | for(Int_t iBinSourceVal=1; iBinSourceVal<=nBinSourceVal; iBinSourceVal++){ | |
1185 | Float_t lowPtSource= hvalue->GetBinLowEdge(iBinSourceVal) ; | |
1186 | Float_t binWidSource= hvalue->GetBinWidth(iBinSourceVal); | |
1187 | if(TMath::Abs(lowPtDest-lowPtSource)<0.001 && TMath::Abs(binWidSource-binWidDest)<0.001){ | |
1188 | Double_t content = hvalue->GetBinContent(iBinSourceVal); | |
1189 | hdest->SetBinContent(iBinDest, content); | |
1190 | foundValue = kTRUE; | |
1191 | break; | |
1192 | } | |
1193 | } | |
1194 | // if (!foundValue){ | |
fef6f2b3 | 1195 | // Printf("AliPWGHistoTools::GetValueAndError: Error: cannot find matching value source bin for destination %d",iBinDest); |
3725266c | 1196 | // } |
1197 | ||
1198 | // Error | |
1199 | Bool_t foundError = kFALSE; | |
1200 | for(Int_t iBinSourceErr=1; iBinSourceErr<=nBinSourceErr; iBinSourceErr++){ | |
1201 | Float_t lowPtSource= herror->GetBinLowEdge(iBinSourceErr) ; | |
1202 | Float_t binWidSource= herror->GetBinWidth(iBinSourceErr); | |
1203 | if(TMath::Abs(lowPtDest-lowPtSource)<0.001 && TMath::Abs(binWidSource-binWidDest)<0.001){ | |
1204 | Double_t error = herror->GetBinContent(iBinSourceErr); | |
1205 | // cout << "-> " << iBinDest << " " << error << " " << hdest->GetBinContent(iBinDest) << endl; | |
1206 | ||
1207 | hdest->SetBinError(iBinDest, isPercentError ? error * hdest->GetBinContent(iBinDest) : error); | |
1208 | foundError=kTRUE; | |
1209 | break; | |
1210 | } | |
1211 | } | |
1212 | // if (!foundError ){ | |
fef6f2b3 | 1213 | // Printf("AliPWGHistoTools::GetValueAndError: Error: cannot find matching error source bin for destination %d",iBinDest); |
3725266c | 1214 | // } |
1215 | } | |
1216 | ||
1217 | ||
1218 | } | |
1219 | ||
fef6f2b3 | 1220 | void AliPWGHistoTools::AddHisto(TH1 * hdest, const TH1* hsource, Bool_t getMirrorBins) { |
3725266c | 1221 | |
1222 | // Adds hsource to hdest bin by bin, even if they have a different | |
1223 | // binning If getMirrorBins is true, it takes the negative bins | |
1224 | // (Needed because sometimes the TPC uses the positive axis for | |
1225 | // negative particles and the possitive axis for positive | |
1226 | // particles). | |
1227 | ||
1228 | ||
1229 | if (hdest == NULL) { | |
1230 | Printf("Error: hdest is NULL\n"); | |
37e1d042 | 1231 | return; |
3725266c | 1232 | } |
1233 | if (hsource == NULL) { | |
1234 | Printf("Error: hsource is NULL\n"); | |
37e1d042 | 1235 | return; |
3725266c | 1236 | } |
1237 | ||
1238 | Int_t nBinSource = hsource->GetNbinsX(); | |
1239 | Int_t nBinDest = hdest->GetNbinsX(); | |
1240 | ||
1241 | // Loop over destination bins, | |
1242 | for(Int_t iBinDest=1; iBinDest<=nBinDest; iBinDest++){ | |
1243 | Float_t lowPtDest=hdest->GetBinLowEdge(iBinDest); | |
1244 | Float_t binWidDest=hdest->GetBinWidth(iBinDest); | |
1245 | // Loop over Source bins and find overlapping bins to Dest | |
1246 | Bool_t found = kFALSE; | |
1247 | for(Int_t iBinSource=1; iBinSource<=nBinSource; iBinSource++){ | |
1248 | Float_t lowPtSource= getMirrorBins ? -hsource->GetBinLowEdge(iBinSource)+hsource->GetBinWidth(iBinSource) : hsource->GetBinLowEdge(iBinSource) ; | |
1249 | Float_t binWidSource= hsource->GetBinWidth(iBinSource) ; | |
1250 | if(TMath::Abs(lowPtDest-lowPtSource)<0.001 && TMath::Abs(binWidSource-binWidDest)<0.001){ | |
1251 | Float_t dest=hdest->GetBinContent(iBinDest); | |
1252 | Float_t source=hsource->GetBinContent(iBinSource); | |
1253 | Float_t edest=hdest->GetBinError(iBinDest); | |
1254 | Float_t esource=hsource->GetBinError(iBinSource); | |
1255 | Double_t cont=dest+source; | |
1256 | Double_t econt=TMath::Sqrt(edest*edest+esource*esource); | |
1257 | hdest->SetBinContent(iBinDest,cont); | |
1258 | hdest->SetBinError (iBinDest,econt); | |
1259 | found = kTRUE; | |
1260 | ||
1261 | break; | |
1262 | } | |
1263 | } | |
77233d47 | 1264 | // if (!found){ |
1265 | // Printf("Error: cannot find matching source bin for destination %d",iBinDest); | |
1266 | // } | |
3725266c | 1267 | } |
1268 | ||
1269 | ||
1270 | } | |
1271 | ||
fef6f2b3 | 1272 | void AliPWGHistoTools::GetHistoCombinedErrors(TH1 * hdest, const TH1 * h1) { |
058137a6 | 1273 | |
1274 | // Combine the errors of hdest with the errors of h1, summing in | |
1275 | // quadrature. Results are put in hdest. Histograms are assumed to | |
1276 | // have the same binning | |
1277 | ||
1278 | Int_t nbin = hdest->GetNbinsX(); | |
90421222 | 1279 | for(Int_t ibin = 1; ibin <= nbin; ibin++){ |
058137a6 | 1280 | Double_t e1 = hdest->GetBinError(ibin); |
1281 | Double_t e2 = h1->GetBinError(ibin); | |
1282 | hdest->SetBinError(ibin, TMath::Sqrt(e1*e1+e2*e2)); | |
1283 | } | |
1284 | ||
90421222 | 1285 | |
058137a6 | 1286 | } |
d8e6677d | 1287 | |
fef6f2b3 | 1288 | TH1F * AliPWGHistoTools::DivideHistosDifferentBins(const TH1F* h1, const TH1F* h2) { |
d8e6677d | 1289 | // Divides 2 histos even if they have a different binning. Finds |
1290 | // overlapping bins and divides them | |
1291 | ||
1292 | // 1. clone histo | |
1293 | TH1F * hRatio = new TH1F(*h1); | |
1294 | Int_t nBinsH1=h1->GetNbinsX(); | |
1295 | Int_t nBinsH2=h2->GetNbinsX(); | |
1296 | // Loop over H1 bins, | |
1297 | for(Int_t iBin=1; iBin<=nBinsH1; iBin++){ | |
1298 | hRatio->SetBinContent(iBin,0.); | |
1299 | hRatio->SetBinContent(iBin,0.); | |
1300 | Float_t lowPtH1=h1->GetBinLowEdge(iBin); | |
1301 | Float_t binWidH1=h1->GetBinWidth(iBin); | |
1302 | // Loop over H2 bins and find overlapping bins to H1 | |
1303 | for(Int_t jBin=1; jBin<=nBinsH2; jBin++){ | |
1304 | Float_t lowPtH2=h2->GetBinLowEdge(jBin); | |
1305 | Float_t binWidH2=h2->GetBinWidth(jBin); | |
1306 | if(TMath::Abs(lowPtH1-lowPtH2)<0.001 && TMath::Abs(binWidH2-binWidH1)<0.001){ | |
1307 | Float_t numer=h1->GetBinContent(iBin); | |
1308 | Float_t denom=h2->GetBinContent(jBin); | |
1309 | Float_t enumer=h1->GetBinError(iBin); | |
1310 | Float_t edenom=h2->GetBinError(jBin); | |
1311 | Double_t ratio=0.; | |
1312 | Double_t eratio=0.; | |
1313 | if(numer>0. && denom>0.){ | |
1314 | ratio=numer/denom; | |
1315 | eratio=ratio*TMath::Sqrt((enumer/numer)*(enumer/numer)+(edenom/denom)*(edenom/denom)); | |
1316 | } | |
1317 | hRatio->SetBinContent(iBin,ratio); | |
1318 | hRatio->SetBinError(iBin,eratio); | |
1319 | break; | |
1320 | } | |
1321 | } | |
1322 | } | |
1323 | return hRatio; | |
1324 | } | |
57b5143c | 1325 | |
fef6f2b3 | 1326 | 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 , |
57b5143c | 1327 | Option_t *option, Bool_t doError) |
1328 | { | |
1329 | // function to compute integral and optionally the error between the limits | |
1330 | // specified by the bin number values working for all histograms (1D, 2D and 3D) | |
1331 | // copied from TH! to fix a bug still present in 5-27-06b | |
1332 | Int_t nbinsx = h->GetNbinsX(); | |
1333 | if (binx1 < 0) binx1 = 0; | |
1334 | if (binx2 > nbinsx+1 || binx2 < binx1) binx2 = nbinsx+1; | |
1335 | if (h->GetDimension() > 1) { | |
1336 | Int_t nbinsy = h->GetNbinsY(); | |
1337 | if (biny1 < 0) biny1 = 0; | |
1338 | if (biny2 > nbinsy+1 || biny2 < biny1) biny2 = nbinsy+1; | |
1339 | } else { | |
1340 | biny1 = 0; biny2 = 0; | |
1341 | } | |
1342 | if (h->GetDimension() > 2) { | |
1343 | Int_t nbinsz = h->GetNbinsZ(); | |
1344 | if (binz1 < 0) binz1 = 0; | |
1345 | if (binz2 > nbinsz+1 || binz2 < binz1) binz2 = nbinsz+1; | |
1346 | } else { | |
1347 | binz1 = 0; binz2 = 0; | |
1348 | } | |
1349 | ||
1350 | // - Loop on bins in specified range | |
1351 | TString opt = option; | |
1352 | opt.ToLower(); | |
1353 | Bool_t width = kFALSE; | |
1354 | if (opt.Contains("width")) width = kTRUE; | |
1355 | ||
1356 | ||
1357 | Double_t dx = 1.; | |
1358 | Double_t dy = 1.; | |
1359 | Double_t dz = 1.; | |
1360 | Double_t integral = 0; | |
1361 | Double_t igerr2 = 0; | |
1362 | for (Int_t binx = binx1; binx <= binx2; ++binx) { | |
1363 | if (width) dx = h->GetXaxis()->GetBinWidth(binx); | |
1364 | for (Int_t biny = biny1; biny <= biny2; ++biny) { | |
1365 | if (width) dy = h->GetYaxis()->GetBinWidth(biny); | |
1366 | for (Int_t binz = binz1; binz <= binz2; ++binz) { | |
1367 | if (width) dz = h->GetZaxis()->GetBinWidth(binz); | |
1368 | Int_t bin = h->GetBin(binx, biny, binz); | |
1369 | if (width) integral += h->GetBinContent(bin)*dx*dy*dz; | |
1370 | else integral += h->GetBinContent(bin); | |
1371 | if (doError) { | |
1372 | if (width) igerr2 += h->GetBinError(bin)*h->GetBinError(bin)*dx*dy*dz*dx*dy*dz; | |
1373 | else igerr2 += h->GetBinError(bin)*h->GetBinError(bin); | |
1374 | } | |
1375 | // cout << h->GetBinContent(bin) << " " << h->GetBinError(bin) << " " << dx*dy*dz << " " << integral << " +- " << igerr2 << endl; | |
1376 | ||
1377 | } | |
1378 | } | |
1379 | } | |
1380 | ||
1381 | if (doError) error = TMath::Sqrt(igerr2); | |
1382 | return integral; | |
1383 | } | |
90421222 | 1384 | |
fef6f2b3 | 1385 | Double_t AliPWGHistoTools::dMtdptFunction(Double_t *x, Double_t *p) { |
90421222 | 1386 | |
1387 | // Computes the dmt/dptdeta function using the dN/dpt function | |
1388 | // This is a protected function used internally by GetdMtdy to integrate dN/dpt function using mt as a weight | |
1389 | // The mass of the particle is given as p[0] | |
1390 | Double_t pt = x[0]; | |
1391 | Double_t mass = p[0]; | |
1392 | Double_t mt = TMath::Sqrt(pt*pt + mass*mass); | |
1393 | Double_t jacobian = pt/mt; | |
1394 | if(!fdNdptForETCalc){ | |
fef6f2b3 | 1395 | Printf("AliPWGHistoTools::dMtdptFunction: ERROR: fdNdptForETCalc not defined"); |
90421222 | 1396 | return 0; |
1397 | } | |
1398 | Double_t dNdpt = fdNdptForETCalc->Eval(pt); | |
1399 | return dNdpt*mt*jacobian; // FIXME: do I have to normalize somehow? | |
1400 | ||
1401 | } | |
1402 | ||
fef6f2b3 | 1403 | Double_t AliPWGHistoTools::GetdMtdEta(TH1 *hData, TF1 * fExtrapolation, Double_t mass) { |
90421222 | 1404 | // Computes dMtdEta integrating dN/dptdy with the proper weights and jacobian. |
5e1a7931 | 1405 | Printf("WARNING ALIBWTOOLS::GetdMtdEta: ONLY USING FUNCTION FOR THE TIME BEING, hData"); |
1406 | if(!hData) { | |
1407 | Printf("hData not set"); | |
1408 | } | |
90421222 | 1409 | |
1410 | // Assign the fiunction used internally by dMtdptFunction | |
1411 | fdNdptForETCalc = fExtrapolation; | |
1412 | // Create the function to be integrated | |
1413 | TF1 * funcdMtdPt = new TF1 ("funcdMtdPt", dMtdptFunction, 0.0, 20, 1); | |
1414 | funcdMtdPt->SetParameter(0,mass); | |
1415 | // Integrate it | |
1416 | Double_t dMtdEta = funcdMtdPt->Integral(0,100); | |
1417 | // Clean up | |
1418 | fdNdptForETCalc=0; | |
1419 | delete funcdMtdPt; | |
1420 | //return | |
1421 | return dMtdEta; | |
1422 | ||
1423 | } | |
1fe08fa5 | 1424 | |
1425 | ||
1426 |