]>
Commit | Line | Data |
---|---|---|
4c0d7bc7 | 1 | // ---------------------------------------------------------------------- |
2 | // AliBWTools | |
3 | // | |
4 | // This class provides some tools which can be useful in the analsis | |
5 | // of spectra, to fit or transform histograms. See the comments of the | |
6 | // individual methods for details | |
7 | // | |
8 | // Author: M. Floris (CERN) | |
9 | // ---------------------------------------------------------------------- | |
10 | ||
11 | #include "AliBWTools.h" | |
12 | #include "TH1D.h" | |
13 | #include "TF1.h" | |
14 | #include "TH1.h" | |
15 | #include "TMath.h" | |
16 | #include "TGraphErrors.h" | |
17 | #include "TVirtualFitter.h" | |
18 | #include "TMinuit.h" | |
19 | #include "AliLog.h" | |
20 | #include <iostream> | |
21 | ||
22 | using namespace std; | |
23 | ||
4c0d7bc7 | 24 | |
25 | ClassImp(AliBWTools) | |
26 | ||
27 | AliBWTools::AliBWTools() { | |
28 | // ctor | |
29 | } | |
30 | ||
31 | AliBWTools::~AliBWTools(){ | |
32 | // dtor | |
33 | } | |
34 | ||
3beb22a2 | 35 | TH1 * AliBWTools::GetdNdmtFromdNdpt(const TH1 * hpt, Double_t mass) { |
4c0d7bc7 | 36 | // convert the x axis from pt to mt. Assumes you have 1/pt dNdpt in the histo you start with |
37 | ||
38 | Int_t nbins = hpt->GetNbinsX(); | |
39 | Float_t * xbins = new Float_t[nbins+1]; | |
40 | for(Int_t ibins = 0; ibins <= nbins; ibins++){ | |
41 | xbins[ibins] = TMath::Sqrt(hpt->GetBinLowEdge(ibins+1)*hpt->GetBinLowEdge(ibins+1) + | |
42 | mass *mass) - mass; | |
43 | // xbins[ibins] = TMath::Sqrt(hpt->GetBinLowEdge(ibins+1)*hpt->GetBinLowEdge(ibins+1) + | |
44 | // mass *mass); | |
45 | // cout << ibins << " "<< xbins[ibins] << endl; | |
46 | ||
47 | } | |
48 | ||
49 | TH1D * hmt = new TH1D(TString(hpt->GetName())+"_mt", | |
50 | TString(hpt->GetName())+"_mt", | |
51 | nbins, xbins); | |
52 | for(Int_t ibins = 1; ibins <= nbins; ibins++){ | |
53 | hmt->SetBinContent(ibins, hpt->GetBinContent(ibins)); | |
54 | hmt->SetBinError(ibins, hpt->GetBinError(ibins)); | |
55 | ||
56 | } | |
57 | ||
58 | hmt->SetXTitle("m_{t} - m_{0} (GeV/c^{2})"); | |
59 | hmt->SetYTitle("1/m_{t} dN/dm_{t} (a.u.)"); | |
60 | ||
61 | return hmt; | |
62 | ||
63 | } | |
64 | ||
3beb22a2 | 65 | TH1 * AliBWTools::GetdNdPtFromOneOverPt(const TH1 * h1Pt) { |
4c0d7bc7 | 66 | |
67 | // convert an histo from 1/pt dNdpt to dNdpt, using the pt at the center of the bin | |
68 | ||
69 | ||
70 | TH1 * hPt = (TH1 *) h1Pt->Clone((TString(h1Pt->GetName()) + "_inv").Data()); | |
71 | hPt->Reset(); | |
72 | ||
73 | Int_t nbinx = hPt->GetNbinsX(); | |
74 | ||
75 | for(Int_t ibinx = 1; ibinx <= nbinx; ibinx++){ | |
76 | ||
77 | Double_t cont = h1Pt->GetBinContent(ibinx); | |
78 | Double_t err = h1Pt->GetBinError(ibinx); | |
79 | ||
80 | Double_t pt = h1Pt->GetBinCenter(ibinx); | |
81 | ||
c3c47c65 | 82 | if(pt > 0) { |
4c0d7bc7 | 83 | cont *= pt; |
84 | err *= pt; | |
85 | } else { | |
86 | cont = 0; | |
87 | err = 0; | |
88 | } | |
89 | ||
90 | hPt->SetBinContent(ibinx, cont); | |
91 | hPt->SetBinError(ibinx, err); | |
92 | ||
93 | } | |
94 | ||
95 | hPt->SetXTitle("p_{t} (GeV)"); | |
96 | hPt->SetYTitle("dN/dp_{t} (GeV^{-2})"); | |
97 | ||
98 | return hPt; | |
99 | ||
100 | } | |
101 | ||
102 | ||
103 | ||
104 | ||
3beb22a2 | 105 | TH1 * AliBWTools::GetOneOverPtdNdPt(const TH1 * hPt) { |
4c0d7bc7 | 106 | |
107 | // convert an histo from dNdpt to 1/pt dNdpt, using the pt at the center of the bin | |
108 | ||
109 | TH1 * h1Pt = (TH1 *) hPt->Clone((TString(hPt->GetName()) + "_inv").Data()); | |
110 | h1Pt->Reset(); | |
111 | ||
112 | Int_t nbinx = h1Pt->GetNbinsX(); | |
113 | ||
114 | for(Int_t ibinx = 1; ibinx <= nbinx; ibinx++){ | |
115 | ||
116 | Double_t cont = hPt->GetBinContent(ibinx); | |
117 | Double_t err = hPt->GetBinError(ibinx); | |
118 | ||
119 | Double_t pt = hPt->GetBinCenter(ibinx); | |
120 | ||
c3c47c65 | 121 | if(pt > 0) { |
4c0d7bc7 | 122 | cont /= pt; |
123 | err /= pt; | |
124 | } else { | |
125 | cont = 0; | |
126 | err = 0; | |
127 | } | |
128 | ||
129 | h1Pt->SetBinContent(ibinx, cont); | |
130 | h1Pt->SetBinError(ibinx, err); | |
131 | ||
132 | } | |
133 | ||
134 | h1Pt->SetXTitle("p_{t} (GeV)"); | |
135 | h1Pt->SetYTitle("1/p_{t} dN/dp_{t} (GeV^{-2})"); | |
136 | ||
137 | return h1Pt; | |
138 | ||
139 | } | |
140 | ||
141 | ||
3beb22a2 | 142 | TGraphErrors * AliBWTools::GetGraphFromHisto(const TH1F * h, Bool_t binWidth) { |
4c0d7bc7 | 143 | // Convert a histo to a graph |
144 | // if binWidth is true ex is set to the bin width of the histos, otherwise it is set to zero | |
145 | Int_t nbin = h->GetNbinsX(); | |
146 | ||
147 | TGraphErrors * g = new TGraphErrors(); | |
148 | Int_t ipoint = 0; | |
149 | for(Int_t ibin = 1; ibin <= nbin; ibin++){ | |
150 | Double_t xerr = binWidth ? h->GetBinWidth(ibin)/2 : 0; | |
3beb22a2 | 151 | if (h->GetBinContent(ibin)) { |
4c0d7bc7 | 152 | g->SetPoint (ipoint, h->GetBinCenter(ibin), h->GetBinContent(ibin)); |
153 | g->SetPointError(ipoint, xerr, h->GetBinError(ibin)); | |
154 | ipoint++; | |
155 | } | |
156 | } | |
157 | ||
158 | g->SetMarkerStyle(h->GetMarkerStyle()); | |
159 | g->SetMarkerColor(h->GetMarkerColor()); | |
160 | g->SetLineColor(h->GetLineColor()); | |
161 | ||
162 | g->SetTitle(h->GetTitle()); | |
163 | g->SetName(TString("g_")+h->GetName()); | |
164 | ||
165 | return g; | |
166 | ||
167 | } | |
168 | ||
3beb22a2 | 169 | TH1F * AliBWTools::GetHistoFromGraph(const TGraphErrors * g, const TH1F* hTemplate) { |
4c0d7bc7 | 170 | |
171 | // convert a graph to histo with the binning of hTemplate. | |
172 | // Warning: the template should be chosen | |
173 | // properly: if you have two graph points in the same histo bin this | |
174 | // won't work! | |
175 | ||
176 | TH1F * h = (TH1F*) hTemplate->Clone(TString("h_")+g->GetName()); | |
177 | h->Reset(); | |
178 | Int_t npoint = g->GetN(); | |
179 | // g->Print(); | |
180 | for(Int_t ipoint = 0; ipoint < npoint; ipoint++){ | |
181 | Float_t x = g->GetX() [ipoint]; | |
182 | Float_t y = g->GetY() [ipoint]; | |
183 | Float_t ey = g->GetEY()[ipoint]; | |
184 | Int_t bin = h->FindBin(x); | |
185 | // cout << "bin: "<< bin << " -> " << x << ", "<< y <<", " << ey << endl; | |
186 | ||
187 | h->SetBinContent(bin,y); | |
188 | h->SetBinError (bin,ey); | |
189 | } | |
190 | ||
191 | h->SetMarkerStyle(g->GetMarkerStyle()); | |
192 | h->SetMarkerColor(g->GetMarkerColor()); | |
193 | h->SetLineColor (g->GetLineColor()); | |
194 | ||
195 | ||
196 | return h; | |
197 | } | |
198 | ||
3beb22a2 | 199 | TGraphErrors * AliBWTools::ConcatenateGraphs(const TGraphErrors * g1,const TGraphErrors * g2){ |
4c0d7bc7 | 200 | |
201 | // concatenates two graphs | |
202 | ||
203 | Int_t npoint1=g1->GetN(); | |
204 | Int_t npoint2=g2->GetN(); | |
205 | ||
206 | TGraphErrors * gClone = (TGraphErrors*) g1->Clone(); | |
207 | ||
208 | // for(Int_t ipoint = 0; ipoint < npoint1; ipoint++){ | |
209 | // gClone->SetPointError(ipoint,0,g1->GetEY()[ipoint]); | |
210 | ||
211 | // } | |
212 | for(Int_t ipoint = 0; ipoint < npoint2; ipoint++){ | |
213 | gClone->SetPoint(ipoint+npoint1,g2->GetX()[ipoint],g2->GetY()[ipoint]); | |
214 | gClone->SetPointError(ipoint+npoint1,g2->GetEX()[ipoint],g2->GetEY()[ipoint]); | |
215 | // gClone->SetPointError(ipoint+npoint1,0,g2->GetEY()[ipoint]); | |
216 | } | |
217 | ||
218 | gClone->GetHistogram()->GetXaxis()->SetTimeDisplay(1); | |
219 | gClone->SetTitle(TString(gClone->GetTitle())+" + "+g2->GetTitle()); | |
220 | gClone->SetName(TString(gClone->GetName())+"_"+g2->GetName()); | |
221 | ||
222 | return gClone; | |
223 | } | |
224 | ||
225 | ||
3beb22a2 | 226 | TH1F * AliBWTools::Combine3HistosWithErrors(const TH1 * h1, const TH1 * h2, const TH1* h3, |
3725266c | 227 | TH1 * he1, TH1 * he2, TH1 * he3, |
3beb22a2 | 228 | const TH1* htemplate, Int_t statFrom, |
3725266c | 229 | Float_t renorm1, Float_t renorm2, Float_t renorm3, |
230 | TH1 ** hSyst, Bool_t errorFromBinContent) { | |
ee08b77d | 231 | |
232 | // Combines 3 histos (h1,h2,h3), weighting by the errors provided in | |
233 | // he1,he2,he3, supposed to be the independent systematic errors. | |
234 | // he1,he2,he3 are also assumed to have the same binning as h1,h2,h3 | |
235 | // The combined histo must fit the template provided (no check is performed on this) | |
236 | // The histogram are supposed to come from the same (nearly) sample | |
237 | // of tracks. statFrom tells the stat error of which of the 3 is | |
238 | // going to be assigned to the combined | |
239 | // Optionally, it is possible to rescale any of the histograms. | |
3725266c | 240 | // if hSyst is give, the histo is filled with combined syst error vs pt |
241 | // if errorFromBinContent is true, the weights are taken from the he* content rather than errors | |
ee08b77d | 242 | TH1F * hcomb = (TH1F*) htemplate->Clone(TString("hComb_")+h1->GetName()+"_"+h2->GetName()+h3->GetName()); |
243 | ||
244 | // TODO: I should have used an array for h*local... | |
245 | ||
246 | // Clone histos locally to rescale them | |
247 | TH1F * h1local = (TH1F*) h1->Clone(); | |
248 | TH1F * h2local = (TH1F*) h2->Clone(); | |
249 | TH1F * h3local = (TH1F*) h3->Clone(); | |
250 | h1local->Scale(renorm1); | |
251 | h2local->Scale(renorm2); | |
252 | h3local->Scale(renorm3); | |
253 | ||
3beb22a2 | 254 | const TH1 * hStatError = 0; |
ee08b77d | 255 | if (statFrom == 0) hStatError = h1; |
256 | else if (statFrom == 1) hStatError = h2; | |
257 | else if (statFrom == 2) hStatError = h3; | |
258 | else Printf("AliBWTools::Combine3HistosWithErrors: wrong value of the statFrom parameter"); | |
259 | Printf("AliBWTools::Combine3HistosWithErrors: improve error on combined"); | |
260 | // Loop over all bins and take weighted mean of all points | |
261 | Int_t nBinComb = hcomb->GetNbinsX(); | |
262 | for(Int_t ibin = 1; ibin <= nBinComb; ibin++){ | |
263 | Int_t ibin1 = h1local->FindBin(hcomb->GetBinCenter(ibin)); | |
264 | Int_t ibin2 = h2local->FindBin(hcomb->GetBinCenter(ibin)); | |
265 | Int_t ibin3 = h3local->FindBin(hcomb->GetBinCenter(ibin)); | |
266 | Int_t ibinError = -1; // bin used to get stat error | |
267 | ||
268 | if (statFrom == 0) ibinError = ibin1; | |
269 | else if (statFrom == 1) ibinError = ibin2; | |
270 | else if (statFrom == 2) ibinError = ibin3; | |
271 | else Printf("AliBWTools::Combine3HistosWithErrors: wrong value of the statFrom parameter"); | |
272 | ||
273 | ||
274 | Double_t y[3]; | |
275 | Double_t ye[3]; | |
276 | y[0] = h1local->GetBinContent(ibin1); | |
277 | y[1] = h2local->GetBinContent(ibin2); | |
278 | y[2] = h3local->GetBinContent(ibin3); | |
3725266c | 279 | if (errorFromBinContent) { |
280 | ye[0] = he1->GetBinContent(he1->FindBin(hcomb->GetBinCenter(ibin))); | |
281 | ye[1] = he2->GetBinContent(he2->FindBin(hcomb->GetBinCenter(ibin))); | |
282 | ye[2] = he3->GetBinContent(he3->FindBin(hcomb->GetBinCenter(ibin))); | |
283 | } else { | |
284 | ye[0] = he1->GetBinError(ibin1); | |
285 | ye[1] = he2->GetBinError(ibin2); | |
286 | ye[2] = he3->GetBinError(ibin3); | |
287 | } | |
ee08b77d | 288 | // Set error to 0 if content is 0 (means it was not filled) |
3beb22a2 | 289 | if(!h1local->GetBinContent(ibin1)) ye[0] = 0; |
290 | if(!h2local->GetBinContent(ibin2)) ye[1] = 0; | |
291 | if(!h3local->GetBinContent(ibin3)) ye[2] = 0; | |
ee08b77d | 292 | |
293 | // Compute weighted mean | |
3725266c | 294 | // cout << "Bins: "<< ibin1 << " " << ibin2 << " " << ibin3 << endl; |
ee08b77d | 295 | Double_t mean, err; |
296 | WeightedMean(3,y,ye,mean,err); | |
297 | ||
298 | ||
299 | // Fill combined | |
ee08b77d | 300 | hcomb->SetBinContent(ibin,mean); |
301 | Double_t statError = 0; | |
3beb22a2 | 302 | if (hStatError->GetBinContent(ibinError)) { |
ee08b77d | 303 | // cout << "def" << endl; |
304 | statError = hStatError->GetBinError(ibinError)/hStatError->GetBinContent(ibinError)*hcomb->GetBinContent(ibin); | |
305 | } | |
3beb22a2 | 306 | else if (h1local->GetBinContent(ibin1)) { |
ee08b77d | 307 | // cout << "1" << endl; |
308 | statError = h1local->GetBinError(ibin1)/h1local->GetBinContent(ibin1)*hcomb->GetBinContent(ibin); | |
309 | } | |
3beb22a2 | 310 | else if (h2local->GetBinContent(ibin2)) { |
ee08b77d | 311 | // cout << "2" << endl; |
312 | statError = h2local->GetBinError(ibin2)/h2local->GetBinContent(ibin2)*hcomb->GetBinContent(ibin); | |
313 | } | |
3beb22a2 | 314 | else if (h3local->GetBinContent(ibin3)) { |
ee08b77d | 315 | // cout << "3" << endl; |
316 | statError = h3local->GetBinError(ibin3)/h3local->GetBinContent(ibin3)*hcomb->GetBinContent(ibin); | |
317 | } | |
318 | hcomb->SetBinError (ibin,statError); | |
3725266c | 319 | if(hSyst) (*hSyst)->SetBinContent(ibin,err); |
ee08b77d | 320 | // cout << "BIN " << ibin << " " << mean << " " << statError << endl; |
321 | ||
322 | } | |
323 | ||
324 | hcomb->SetMarkerStyle(hStatError->GetMarkerStyle()); | |
325 | hcomb->SetMarkerColor(hStatError->GetMarkerColor()); | |
326 | hcomb->SetLineColor (hStatError->GetLineColor()); | |
327 | ||
328 | hcomb->SetXTitle(hStatError->GetXaxis()->GetTitle()); | |
329 | hcomb->SetYTitle(hStatError->GetYaxis()->GetTitle()); | |
330 | ||
331 | delete h1local; | |
332 | delete h2local; | |
333 | delete h3local; | |
334 | ||
335 | return hcomb; | |
336 | } | |
337 | ||
338 | ||
ddecf727 | 339 | void AliBWTools::GetMeanDataAndExtrapolation(const TH1 * hData, TF1 * fExtrapolation, Double_t &mean, Double_t &error, Float_t min, Float_t max){ |
340 | // Computes the mean of the combined data and extrapolation in a | |
341 | // given range, use data where they are available and the function | |
342 | // where data are not available | |
343 | // ERROR is from DATA ONLY returned in this version! | |
344 | // | |
345 | Printf("AliBWTools::GetMeanDataAndExtrapolation: WARNING from data only"); | |
346 | Float_t minData = GetLowestNotEmptyBinEdge (hData); | |
347 | Float_t minDataBin = GetLowestNotEmptyBin (hData); | |
348 | Float_t maxData = GetHighestNotEmptyBinEdge(hData); | |
349 | Float_t maxDataBin = GetHighestNotEmptyBin (hData); | |
350 | Double_t integral = 0; | |
351 | mean = 0; | |
352 | error = 0; | |
353 | if (min < minData) { | |
354 | // Compute integral exploiting root function to calculate moments, "unnormalizing" them | |
355 | mean += fExtrapolation->Mean(min,minData)*fExtrapolation->Integral(min,minData); | |
356 | integral += fExtrapolation->Integral(min,minData); | |
357 | cout << " Low "<< mean << " " << integral << endl; | |
358 | ||
359 | } | |
360 | ||
361 | if (max > maxData) { | |
362 | // Compute integral exploiting root function to calculate moments, "unnormalizing" them | |
363 | mean += fExtrapolation->Mean(maxData,max)*fExtrapolation->Integral(maxData,max); | |
364 | integral += fExtrapolation->Integral(maxData,max); | |
365 | cout << " Hi "<< mean << " " << integral << endl; | |
366 | } | |
367 | Float_t err2 = 0; | |
368 | ||
369 | for(Int_t ibin = minDataBin; ibin <= maxDataBin; ibin++){ | |
370 | if(hData->GetBinCenter(ibin) < min) continue; | |
371 | if(hData->GetBinCenter(ibin) > max) continue; | |
372 | mean = mean + (hData->GetBinCenter(ibin) * hData->GetBinWidth(ibin)* hData->GetBinContent(ibin)); | |
373 | err2 = err2 + TMath::Power(hData->GetBinError(ibin) * hData->GetBinCenter(ibin) * hData->GetBinWidth(ibin),2); | |
374 | integral = integral + hData->GetBinContent(ibin) * hData->GetBinWidth(ibin); | |
375 | } | |
376 | cout << " Data "<< mean << " " << integral << endl; | |
377 | ||
378 | mean = mean/integral; | |
379 | error = TMath::Sqrt(err2); | |
380 | ||
4c0d7bc7 | 381 | |
ddecf727 | 382 | } |
383 | ||
384 | TH1F * AliBWTools::CombineHistos(const TH1 * h1, TH1 * h2, const TH1* htemplate, Float_t renorm1){ | |
4c0d7bc7 | 385 | // Combine two histos. This assumes the histos have the same binning |
386 | // in the overlapping region. It computes the arithmetic mean in the | |
387 | // overlapping region and assigns as an error the relative error | |
388 | // h2. TO BE IMPROVED | |
389 | ||
390 | TH1F * hcomb = (TH1F*) htemplate->Clone(TString(h1->GetName())+"_"+h2->GetName()); | |
391 | ||
392 | TH1F * h1local = (TH1F*) h1->Clone(); | |
393 | h1local->Scale(renorm1); | |
394 | ||
395 | Int_t nBinComb = hcomb->GetNbinsX(); | |
396 | for(Int_t ibin = 1; ibin <= nBinComb; ibin++){ | |
397 | Int_t ibin1 = h1local->FindBin(hcomb->GetBinCenter(ibin)); | |
398 | Int_t ibin2 = h2->FindBin(hcomb->GetBinCenter(ibin)); | |
399 | ||
3beb22a2 | 400 | if (!h1local->GetBinContent(ibin1) && !h2->GetBinContent(ibin2) ) { |
4c0d7bc7 | 401 | // None has data: go to next bin |
402 | hcomb->SetBinContent(ibin,0); | |
403 | hcomb->SetBinError (ibin,0); | |
3beb22a2 | 404 | } else if(h1local->GetBinContent(ibin1) && !h2->GetBinContent(ibin2)) { |
4c0d7bc7 | 405 | // take data from h1local: |
406 | hcomb->SetBinContent(ibin,h1local->GetBinContent(ibin1)); | |
407 | hcomb->SetBinError (ibin,h1local->GetBinError(ibin1)); | |
3beb22a2 | 408 | } else if(!h1local->GetBinContent(ibin1) && h2->GetBinContent(ibin2)) { |
4c0d7bc7 | 409 | // take data from h2: |
410 | hcomb->SetBinContent(ibin,h2->GetBinContent(ibin2)); | |
411 | hcomb->SetBinError (ibin,h2->GetBinError(ibin2)); | |
412 | } else { | |
413 | hcomb->SetBinContent(ibin,(h1local->GetBinContent(ibin1) +h2->GetBinContent(ibin2))/2); | |
414 | // hcomb->SetBinError (ibin,h1local->GetBinError(ibin1)/h1local->GetBinContent(ibin1)*hcomb->GetBinContent(ibin)); | |
415 | hcomb->SetBinError (ibin,h2->GetBinError(ibin2)/h2->GetBinContent(ibin2)*hcomb->GetBinContent(ibin)); | |
416 | } | |
417 | ||
418 | ||
419 | } | |
420 | ||
421 | ||
422 | hcomb->SetMarkerStyle(h1local->GetMarkerStyle()); | |
423 | hcomb->SetMarkerColor(h1local->GetMarkerColor()); | |
424 | hcomb->SetLineColor (h1local->GetLineColor()); | |
425 | ||
426 | hcomb->SetXTitle(h1local->GetXaxis()->GetTitle()); | |
427 | hcomb->SetYTitle(h1local->GetYaxis()->GetTitle()); | |
428 | delete h1local; | |
429 | return hcomb; | |
430 | ||
431 | } | |
432 | ||
433 | ||
3beb22a2 | 434 | void AliBWTools::GetFromHistoGraphDifferentX(const TH1F * h, TF1 * f, TGraphErrors ** gBarycentre, TGraphErrors ** gXlw) { |
4c0d7bc7 | 435 | |
436 | // Computes the baycentre in each bin and the xlw as defined in NIMA | |
437 | // 355 - 541 using f. Returs 2 graphs with the same y content of h | |
438 | // but with a different x (barycentre and xlw) | |
439 | ||
440 | Int_t nbin = h->GetNbinsX(); | |
441 | ||
442 | (*gBarycentre) = new TGraphErrors(); | |
443 | (*gXlw) = new TGraphErrors(); | |
444 | ||
445 | Int_t ipoint = 0; | |
446 | for(Int_t ibin = 1; ibin <= nbin; ibin++){ | |
447 | Float_t min = h->GetBinLowEdge(ibin); | |
448 | Float_t max = h->GetBinLowEdge(ibin+1); | |
449 | Double_t xerr = 0; | |
450 | Double_t xbar = f->Mean(min,max); | |
c3c47c65 | 451 | // compute xLW |
4c0d7bc7 | 452 | Double_t temp = 1./(max-min) * f->Integral(min,max); |
453 | Double_t epsilon = 0.000000001; | |
454 | Double_t increment = 0.0000000001; | |
c3c47c65 | 455 | Double_t xLW = min; |
4c0d7bc7 | 456 | |
c3c47c65 | 457 | while ((f->Eval(xLW)- temp) > epsilon) { |
458 | xLW += increment; | |
459 | if(xLW > max) { | |
460 | Printf("Cannot find xLW"); | |
4c0d7bc7 | 461 | break; |
462 | } | |
463 | } | |
464 | ||
465 | if (h->GetBinContent(ibin)!=0) { | |
466 | (*gBarycentre)->SetPoint (ipoint, xbar, h->GetBinContent(ibin)); | |
467 | (*gBarycentre)->SetPointError(ipoint, xerr, h->GetBinError(ibin)); | |
c3c47c65 | 468 | (*gXlw) ->SetPoint (ipoint, xLW, h->GetBinContent(ibin)); |
4c0d7bc7 | 469 | (*gXlw) ->SetPointError(ipoint, xerr, h->GetBinError(ibin)); |
470 | ipoint++; | |
471 | } | |
472 | } | |
473 | ||
474 | (*gBarycentre)->SetMarkerStyle(h->GetMarkerStyle()); | |
475 | (*gBarycentre)->SetMarkerColor(h->GetMarkerColor()); | |
476 | (*gBarycentre)->SetLineColor(h->GetLineColor()); | |
477 | ||
478 | (*gBarycentre)->SetTitle(h->GetTitle()); | |
479 | (*gBarycentre)->SetName(TString("g_")+h->GetName()); | |
480 | ||
481 | (*gXlw)->SetMarkerStyle(h->GetMarkerStyle()); | |
482 | (*gXlw)->SetMarkerColor(h->GetMarkerColor()); | |
483 | (*gXlw)->SetLineColor(h->GetLineColor()); | |
484 | (*gXlw)->SetTitle(h->GetTitle()); | |
485 | (*gXlw)->SetName(TString("g_")+h->GetName()); | |
486 | ||
487 | ||
488 | } | |
489 | ||
490 | ||
ddecf727 | 491 | Float_t AliBWTools::GetMean(TH1F * h, Float_t min, Float_t max, Float_t * error) { |
4c0d7bc7 | 492 | |
3472a634 | 493 | // Get the mean of histo in a range; root is not reliable in sub |
494 | // ranges with variable binning. | |
4c0d7bc7 | 495 | Int_t minBin = h->FindBin(min); |
ddecf727 | 496 | Int_t maxBin = h->FindBin(max-0.00001); |
4c0d7bc7 | 497 | |
498 | Float_t mean = 0 ; | |
499 | Float_t integral = 0; | |
ddecf727 | 500 | Float_t err2 = 0; |
501 | for(Int_t ibin = minBin; ibin <= maxBin; ibin++){ | |
4c0d7bc7 | 502 | mean = mean + (h->GetBinCenter(ibin) * h->GetBinWidth(ibin)* h->GetBinContent(ibin)); |
ddecf727 | 503 | err2 = err2 + TMath::Power(h->GetBinError(ibin) * h->GetBinCenter(ibin) * h->GetBinWidth(ibin),2); |
4c0d7bc7 | 504 | integral = integral + h->GetBinContent(ibin) * h->GetBinWidth(ibin); |
505 | } | |
506 | ||
ddecf727 | 507 | Float_t value = mean/integral; |
508 | if (error) (*error) = TMath::Sqrt(err2); | |
509 | return value; | |
4c0d7bc7 | 510 | |
511 | ||
512 | } | |
513 | ||
3472a634 | 514 | void AliBWTools::GetMean(TF1 * func, Float_t &mean, Float_t &error, Float_t min, Float_t max, Int_t normPar) { |
4c0d7bc7 | 515 | |
3472a634 | 516 | // Get the mean of function in a range; If normPar is >= 0, it means |
517 | // that the function is defined such that par[normPar] is its | |
518 | // integral. In this case the error on meanpt can be calculated | |
519 | // correctly. Otherwise, the function is normalized in get moment, | |
520 | // but the error is not computed correctly. | |
4c0d7bc7 | 521 | |
3472a634 | 522 | return GetMoment("fMean", TString("x*")+func->GetExpFormula(), func, mean, error, min, max, normPar); |
4c0d7bc7 | 523 | |
524 | } | |
525 | ||
3472a634 | 526 | void AliBWTools::GetMeanSquare(TF1 * func, Float_t &mean, Float_t &error, Float_t min, Float_t max, Int_t normPar) { |
4c0d7bc7 | 527 | |
3472a634 | 528 | // Get the mean2 of function in a range; If normPar is >= 0, it means |
529 | // that the function is defined such that par[normPar] is its | |
530 | // integral. In this case the error on meanpt can be calculated | |
531 | // correctly. Otherwise, the function is normalized in get moment, | |
532 | // but the error is not computed correctly. | |
4c0d7bc7 | 533 | |
3472a634 | 534 | return GetMoment("fMean2", TString("x*x*")+func->GetExpFormula(), func, mean, error, min, max, normPar); |
4c0d7bc7 | 535 | |
536 | ||
537 | } | |
538 | ||
3472a634 | 539 | void AliBWTools::GetMoment(TString name, TString var, TF1 * func, Float_t &mean, Float_t &error, Float_t min, Float_t max, Int_t normPar) { |
4c0d7bc7 | 540 | |
541 | // returns the integral of a function derived from func by prefixing some operation. | |
3472a634 | 542 | // the derived function MUST have the same parameter in the same order |
4c0d7bc7 | 543 | // Used as a base method for mean and mean2 |
3472a634 | 544 | // If normPar is >= 0, it means that the function is defined such |
545 | // that par[normPar] is its integral. In this case the error on | |
546 | // meanpt can be calculated correctly. Otherwise, the function is | |
547 | // normalized using its numerical integral, but the error is not computed | |
548 | // correctly. | |
549 | ||
550 | // TODO: | |
551 | // - improve to propagate error also in the case you need the | |
552 | // numerical integrals (will be rather slow) | |
553 | // - this version assumes that func is defined using a | |
554 | // TFormula. Generalize to the case of a C++ function | |
555 | ||
556 | if (normPar<0) Printf("AliBWTools::GetMoment: Warning: If normalization is required, the error may bot be correct"); | |
557 | if (!strcmp(func->GetExpFormula(),"")) Printf("AliBWTools::GetMoment: Warning: Empty formula in the base function"); | |
4c0d7bc7 | 558 | Int_t npar = func->GetNpar(); |
559 | ||
3472a634 | 560 | // Definition changes according to the value of normPar |
561 | TF1 * f = normPar < 0 ? | |
562 | new TF1(name, var) : // not normalized | |
563 | new TF1(name, var+Form("/[%d]",normPar)); // normalized with par normPar | |
564 | ||
565 | // integr is used to normalize if no parameter is provided | |
566 | Double_t integr = normPar < 0 ? func->Integral(min,max) : 1; | |
4c0d7bc7 | 567 | |
568 | // The parameter of the function used to compute the mean should be | |
569 | // the same as the parent function: fixed if needed and they should | |
570 | // also have the same errors. | |
571 | ||
572 | // cout << "npar :" << npar << endl; | |
573 | ||
574 | for(Int_t ipar = 0; ipar < npar; ipar++){ | |
575 | Double_t parmin, parmax; | |
576 | Double_t value = func->GetParameter(ipar); | |
3472a634 | 577 | f->SetParameter(ipar,value); |
4c0d7bc7 | 578 | func->GetParLimits(ipar, parmin, parmax); |
579 | if ( parmin == parmax ) { | |
3beb22a2 | 580 | // if ( parmin || (parmin == 1 && !value) ) { // not sure we I check parmin == 1 here. |
581 | 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 | 582 | f->FixParameter(ipar,func->GetParameter(ipar)); |
583 | // cout << "Fixing " << ipar << "("<<value<<","<<parmin<<","<<parmax<<")"<<endl; | |
584 | } | |
585 | else { | |
586 | f->SetParError (ipar,func->GetParError(ipar) ); | |
587 | // cout << "Setting Err" << ipar << "("<<func->GetParError(ipar)<<")"<<endl; | |
588 | } | |
589 | } | |
590 | else { | |
591 | f->SetParError (ipar,func->GetParError(ipar) ); | |
592 | // cout << "Setting Err" << ipar << "("<<func->GetParError(ipar)<<")"<<endl; | |
593 | } | |
594 | } | |
3472a634 | 595 | |
596 | // f->Print(); | |
597 | // cout << "----" << endl; | |
598 | // func->Print(); | |
599 | ||
600 | mean = normPar < 0 ? f->Integral (min,max)/integr : f->Integral (min,max); | |
601 | error = normPar < 0 ? f->IntegralError(min,max)/integr : f->IntegralError(min,max); | |
4c0d7bc7 | 602 | // cout << "Mean " << mean <<"+-"<< error<< endl; |
603 | // cout << "Integral Error " << error << endl; | |
604 | ||
605 | } | |
606 | ||
4c0d7bc7 | 607 | |
608 | ||
609 | Bool_t AliBWTools::Fit (TH1 * h1, TF1* func, Float_t min, Float_t max) { | |
610 | ||
611 | // Fits h1 with func, preforms several checks on the quality of the | |
612 | // fit and tries to improve it. If the fit is not good enough, it | |
613 | // returs false. | |
614 | ||
615 | Double_t amin; Double_t edm; Double_t errdef; Int_t nvpar; Int_t nparx; | |
616 | TVirtualFitter *fitter; | |
617 | cout << "--- Fitting : " << h1->GetName() << " ["<< h1->GetTitle() <<"] ---"<< endl; | |
618 | ||
619 | h1-> Fit(func,"IME0","",min,max); | |
620 | Int_t fitResult = h1-> Fit(func,"IME0","",min,max); | |
621 | // h1-> Fit(func,"0","",min,max); | |
622 | // Int_t fitResult = h1-> Fit(func,"0IE","",min,max); | |
623 | ||
624 | ||
625 | // From TH1: | |
626 | // The fitStatus is 0 if the fit is OK (i.e no error occurred). The | |
627 | // value of the fit status code is negative in case of an error not | |
628 | // connected with the minimization procedure, for example when a wrong | |
629 | // function is used. Otherwise the return value is the one returned | |
630 | // from the minimization procedure. When TMinuit (default case) or | |
631 | // Minuit2 are used as minimizer the status returned is : fitStatus = | |
632 | // migradResult + 10*minosResult + 100*hesseResult + | |
633 | // 1000*improveResult. TMinuit will return 0 (for migrad, minos, | |
634 | // hesse or improve) in case of success and 4 in case of error (see | |
635 | // the documentation of TMinuit::mnexcm). So for example, for an error | |
636 | // only in Minos but not in Migrad a fitStatus of 40 will be returned. | |
637 | // Minuit2 will return also 0 in case of success and different values | |
638 | // in migrad minos or hesse depending on the error. See in this case | |
639 | // the documentation of Minuit2Minimizer::Minimize for the | |
640 | // migradResult, Minuit2Minimizer::GetMinosError for the minosResult | |
641 | // and Minuit2Minimizer::Hesse for the hesseResult. If other | |
642 | // minimizers are used see their specific documentation for the status | |
643 | // code returned. For example in the case of Fumili, for the status | |
644 | // returned see TFumili::Minimize. | |
645 | ||
646 | ||
647 | if( gMinuit->fLimset ) { | |
648 | Printf("ERROR: AliBWTools: Parameters at limits"); | |
649 | return kFALSE; | |
650 | } | |
651 | ||
652 | ||
653 | /// | |
654 | fitter = TVirtualFitter::GetFitter(); | |
655 | Int_t fitStat = fitter->GetStats(amin, edm, errdef, nvpar, nparx); | |
656 | ||
657 | if( ( (fitStat < 3 && gMinuit->fCstatu != "UNCHANGED ")|| (edm > 1e6) || (fitResult !=0 && fitResult < 4000) ) && | |
658 | TString(gMinuit->fCstatu) != "SUCCESSFUL" && | |
659 | TString(gMinuit->fCstatu) != "CONVERGED " ) { | |
660 | if(fitStat < 3 && gMinuit->fCstatu != "UNCHANGED ") { | |
661 | Printf("WARNING: AliBWTools: Cannot properly compute errors"); | |
662 | if (fitStat == 0) Printf(" not calculated at all"); | |
663 | if (fitStat == 1) Printf(" approximation only, not accurate"); | |
664 | if (fitStat == 2) Printf(" full matrix, but forced positive-definite"); | |
665 | } | |
666 | ||
667 | if (edm > 1e6) { | |
668 | ||
669 | Printf("WARNING: AliBWTools: Huge EDM (%f): Fit probably failed!", edm); | |
670 | } | |
671 | if (fitResult != 0) { | |
672 | Printf("WARNING: AliBWTools: Fit Result (%d)", fitResult); | |
673 | } | |
674 | ||
675 | Printf ("AliBWTools: Trying Again with Strategy = 2"); | |
676 | ||
677 | gMinuit->Command("SET STRATEGY 2"); // more effort | |
678 | fitResult = 0; | |
679 | fitResult = h1-> Fit(func,"0","",min,max); | |
680 | fitResult = h1-> Fit(func,"IME0","",min,max); | |
681 | fitResult = h1-> Fit(func,"IME0","",min,max); | |
682 | ||
683 | fitter = TVirtualFitter::GetFitter(); | |
684 | ||
685 | fitStat = fitter->GetStats(amin, edm, errdef, nvpar, nparx); | |
686 | ||
687 | if(fitStat < 3 && gMinuit->fCstatu != "UNCHANGED ") { | |
688 | Printf("ERROR: AliBWTools: Cannot properly compute errors"); | |
689 | if (fitStat == 0) Printf(" not calculated at all"); | |
690 | if (fitStat == 1) Printf(" approximation only, not accurate"); | |
691 | if (fitStat == 2) Printf(" full matrix, but forced positive-definite"); | |
692 | cout << "[" <<gMinuit->fCstatu<<"]" << endl; | |
693 | return kFALSE; | |
694 | } | |
695 | ||
696 | if (edm > 1e6) { | |
697 | Printf("ERROR: AliBWTools: Huge EDM (%f): Fit probably failed!", edm); | |
698 | ||
699 | return kFALSE; | |
700 | } | |
701 | if (fitResult != 0) { | |
702 | Printf("ERROR: AliBWTools: Fit Result (%d)", fitResult); | |
703 | ||
704 | return kFALSE; | |
705 | } | |
706 | ||
707 | gMinuit->Command("SET STRATEGY 1"); // back to normal value | |
708 | ||
709 | } | |
710 | ||
711 | cout << "---- FIT OK ----" << endl; | |
712 | ||
713 | return kTRUE; | |
714 | ||
715 | } | |
716 | ||
3beb22a2 | 717 | Int_t AliBWTools::GetLowestNotEmptyBin(const TH1*h) { |
4c0d7bc7 | 718 | |
719 | // Return the index of the lowest non empty bin in the histo h | |
720 | ||
721 | Int_t nbin = h->GetNbinsX(); | |
722 | for(Int_t ibin = 1; ibin <= nbin; ibin++){ | |
723 | if(h->GetBinContent(ibin)>0) return ibin; | |
724 | } | |
725 | ||
726 | return -1; | |
727 | ||
728 | } | |
729 | ||
3beb22a2 | 730 | Int_t AliBWTools::GetHighestNotEmptyBin(const TH1*h) { |
4c0d7bc7 | 731 | |
732 | // Return the index of the highest non empty bin in the histo h | |
733 | ||
734 | Int_t nbin = h->GetNbinsX(); | |
735 | for(Int_t ibin = nbin; ibin > 0; ibin--){ | |
736 | if(h->GetBinContent(ibin)>0) return ibin; | |
737 | } | |
738 | ||
739 | return -1; | |
740 | ||
741 | } | |
742 | ||
3beb22a2 | 743 | void AliBWTools::GetResiduals(const TGraphErrors * gdata, const TF1 * func, TH1F ** hres, TGraphErrors ** gres) { |
4c0d7bc7 | 744 | |
745 | // Returns a graph of residuals vs point and the res/err distribution | |
746 | ||
747 | Int_t npoint = gdata->GetN(); | |
748 | ||
749 | (*gres) =new TGraphErrors(); | |
750 | (*hres) = new TH1F(TString("hres_")+gdata->GetName()+"-"+func->GetName(), | |
751 | TString("hres_")+gdata->GetName()+"-"+func->GetName(), | |
752 | 20,-5,5); | |
753 | ||
754 | ||
755 | for(Int_t ipoint = 0; ipoint < npoint; ipoint++){ | |
756 | Float_t x = gdata->GetX()[ipoint]; | |
757 | Float_t res = (gdata->GetY()[ipoint] - func->Eval(x))/func->Eval(x); | |
758 | Float_t err = gdata->GetEY()[ipoint]/func->Eval(x); | |
759 | (*hres)->Fill(res/err); | |
760 | (*gres)->SetPoint(ipoint, x, res/err); | |
761 | // (*gres)->SetPointError(ipoint, 0, err); | |
762 | ||
763 | } | |
764 | ||
765 | (*gres)->SetMarkerStyle(gdata->GetMarkerStyle()); | |
766 | (*gres)->SetMarkerColor(gdata->GetMarkerColor()); | |
767 | (*gres)->SetLineColor (gdata->GetLineColor()); | |
768 | (*gres)->GetHistogram()->GetYaxis()->SetTitle("(data-function)/function"); | |
769 | (*hres)->SetMarkerStyle(gdata->GetMarkerStyle()); | |
770 | (*hres)->SetMarkerColor(gdata->GetMarkerColor()); | |
771 | (*hres)->SetLineColor (gdata->GetLineColor()); | |
772 | ||
773 | ||
774 | ||
775 | } | |
776 | ||
3beb22a2 | 777 | void AliBWTools::GetResiduals(const TH1F* hdata, const TF1 * func, TH1F ** hres, TH1F ** hresVsBin) { |
4c0d7bc7 | 778 | |
779 | // Returns an histo of residuals bin by bin and the res/err distribution | |
780 | ||
781 | if (!func) { | |
782 | Printf("AliBWTools::GetResiduals: No function provided"); | |
783 | return; | |
784 | } | |
785 | if (!hdata) { | |
786 | Printf("AliBWTools::GetResiduals: No data provided"); | |
787 | return; | |
788 | } | |
789 | ||
790 | (*hresVsBin) = (TH1F*) hdata->Clone(TString("hres_")+hdata->GetName()); | |
791 | (*hresVsBin)->Reset(); | |
792 | (*hres) = new TH1F(TString("hres_")+hdata->GetName()+"-"+func->GetName(), | |
793 | TString("hres_")+hdata->GetName()+"-"+func->GetName(), | |
794 | 20,-5,5); | |
795 | ||
796 | Int_t nbin = hdata->GetNbinsX(); | |
797 | for(Int_t ibin = 1; ibin <= nbin; ibin++){ | |
3beb22a2 | 798 | if(!hdata->GetBinContent(ibin)) continue; |
4c0d7bc7 | 799 | Float_t res = (hdata->GetBinContent(ibin) - func->Eval(hdata->GetBinCenter(ibin)) ) / |
800 | func->Eval(hdata->GetBinCenter(ibin)); | |
801 | Float_t err = hdata->GetBinError (ibin) / func->Eval(hdata->GetBinCenter(ibin)); | |
802 | (*hresVsBin)->SetBinContent(ibin,res); | |
803 | (*hresVsBin)->SetBinError (ibin,err); | |
804 | (*hres)->Fill(res/err); | |
805 | ||
806 | } | |
807 | ||
808 | (*hresVsBin)->SetMarkerStyle(hdata->GetMarkerStyle()); | |
809 | (*hresVsBin)->SetMarkerColor(hdata->GetMarkerColor()); | |
810 | (*hresVsBin)->SetLineColor (hdata->GetLineColor() ); | |
811 | (*hresVsBin)->GetYaxis()->SetTitle("(data-function)/function"); | |
812 | (*hres)->SetMarkerStyle(hdata->GetMarkerStyle()); | |
813 | (*hres)->SetMarkerColor(hdata->GetMarkerColor()); | |
814 | (*hres)->SetLineColor (hdata->GetLineColor() ); | |
815 | ||
816 | } | |
817 | ||
3beb22a2 | 818 | void AliBWTools::GetYield(TH1* h, TF1 * f, Double_t &yield, Double_t &yieldError, Float_t min, Float_t max, |
4c0d7bc7 | 819 | Double_t *partialYields, Double_t *partialYieldsErrors){ |
820 | ||
821 | // Returns the yield extracted from the data in the histo where | |
822 | // there are points and from the fit to extrapolate, in the given | |
823 | // range. | |
824 | ||
825 | // Partial yields are also returned if the corresponding pointers are non null | |
826 | ||
827 | Int_t bin1 = h->FindBin(min); | |
828 | Int_t bin2 = h->FindBin(max); | |
829 | Float_t bin1Edge = GetLowestNotEmptyBinEdge (h); | |
830 | Float_t bin2Edge = GetHighestNotEmptyBinEdge(h); | |
831 | ||
832 | Double_t integralFromHistoError ; | |
833 | Double_t integralFromHisto = h->IntegralAndError(bin1,bin2,integralFromHistoError,"width"); | |
834 | ||
835 | Double_t integralBelow = min < bin1Edge ? f->Integral(min,bin1Edge) : 0; | |
836 | Double_t integralBelowError = min < bin1Edge ? f->IntegralError(min,bin1Edge) : 0; | |
837 | Double_t integralAbove = max > bin2Edge ? f->Integral(bin2Edge,max) : 0; | |
838 | Double_t integralAboveError = max > bin2Edge ? f->Integral(bin2Edge,max) : 0; | |
839 | ||
3beb22a2 | 840 | // cout << "GetYield INFO" << endl; |
841 | // cout << " " << bin1Edge << " " << bin2Edge << endl; | |
842 | // cout << " " << integralFromHisto << " " << integralBelow << " " << integralAbove << endl; | |
843 | // cout << " " << integralFromHistoError << " " << integralBelowError << " " << integralAboveError << endl; | |
4c0d7bc7 | 844 | |
845 | if(partialYields) { | |
846 | partialYields[0] = integralFromHisto; | |
847 | partialYields[1] = integralBelow; | |
848 | partialYields[2] = integralAbove; | |
849 | } | |
850 | if(partialYieldsErrors) { | |
851 | partialYieldsErrors[0] = integralFromHistoError; | |
852 | partialYieldsErrors[1] = integralBelowError; | |
853 | partialYieldsErrors[2] = integralAboveError; | |
854 | } | |
855 | yield = integralFromHisto+integralBelow+integralAbove; | |
856 | yieldError = TMath::Sqrt(integralFromHistoError*integralFromHistoError+ | |
857 | integralBelowError*integralBelowError+ | |
858 | integralAboveError*integralAboveError); | |
859 | ||
860 | } | |
861 | ||
3beb22a2 | 862 | TGraphErrors * AliBWTools::DivideGraphByFunc(const TGraphErrors * g, const TF1 * f, Bool_t invert){ |
4c0d7bc7 | 863 | |
864 | // Divides g/f. If invert == true => f/g | |
865 | ||
866 | TGraphErrors * gRatio = new TGraphErrors(); | |
867 | Int_t npoint = g->GetN(); | |
868 | for(Int_t ipoint = 0; ipoint < npoint; ipoint++){ | |
869 | Double_t x = g->GetX()[ipoint]; | |
870 | Double_t ratio = invert ? f->Eval(x)/g->GetY()[ipoint] :g->GetY()[ipoint]/f->Eval(x); | |
871 | gRatio->SetPoint (ipoint, x, ratio); | |
872 | gRatio->SetPointError(ipoint, 0, g->GetEY()[ipoint]/f->Eval(x)); | |
873 | // cout << x << " " << g->GetY()[ipoint] << " " << f->Eval(x) << endl; | |
874 | ||
875 | } | |
876 | gRatio->SetMarkerStyle(20); | |
877 | //gRatio->Print(); | |
878 | return gRatio; | |
879 | ||
880 | } | |
881 | ||
3beb22a2 | 882 | TGraphErrors * AliBWTools::DivideGraphByHisto(const TGraphErrors * g, TH1 * h, Bool_t invert){ |
4c0d7bc7 | 883 | |
884 | // Divides g/h. If invert == true => h/g | |
885 | ||
886 | ||
887 | TGraphErrors * gRatio = new TGraphErrors(); | |
888 | Int_t npoint = g->GetN(); | |
889 | for(Int_t ipoint = 0; ipoint < npoint; ipoint++){ | |
890 | Double_t xj = g->GetX()[ipoint]; | |
891 | Double_t yj = g->GetY()[ipoint]; | |
892 | Double_t yje = g->GetEY()[ipoint]; | |
893 | ||
894 | Int_t binData = h->FindBin(xj); | |
895 | Double_t yd = h->GetBinContent(binData); | |
896 | Double_t yde = h->GetBinError(binData); | |
897 | Double_t xd = h->GetBinCenter(binData); | |
898 | ||
4c0d7bc7 | 899 | |
900 | ||
3beb22a2 | 901 | if (!yd) continue; |
4c0d7bc7 | 902 | |
903 | if (TMath::Abs(xd-xj)/TMath::Abs(xd) > 0.01){ | |
904 | Printf( "WARNING: bin center (%f) and x graph (%f) are more than 1 %% away, skipping",xd,xj ); | |
905 | continue; | |
906 | ||
907 | } | |
908 | ||
909 | Double_t ratio = invert ? yd/yj : yj/yd; | |
910 | ||
911 | gRatio->SetPoint(ipoint, xj, ratio); | |
912 | gRatio->SetPointError(ipoint, 0, TMath::Sqrt(yde*yde/yd/yd + yje*yje/yj/yj)*ratio); | |
913 | // gRatio->SetPointError(ipoint, 0, yje/yj * ratio); | |
914 | } | |
915 | ||
916 | return gRatio; | |
917 | ||
918 | ||
919 | } | |
920 | ||
921 | TH1F * AliBWTools::DivideHistoByFunc(TH1F * h, TF1 * f, Bool_t invert){ | |
922 | ||
923 | // Divides h/f. If invert == true => f/g | |
924 | // Performs the integral of f on the bin range to perform the ratio | |
925 | // Returns a histo with the same binnig as h | |
926 | ||
927 | // Prepare histo for ratio | |
928 | TH1F * hRatio = (TH1F*) h->Clone(TString("hRatio_")+h->GetName()+"_"+f->GetName()); | |
929 | hRatio->Reset(); | |
930 | // Set y title | |
931 | if(!invert) hRatio->SetYTitle(TString(h->GetName())+"/"+f->GetName()); | |
932 | else hRatio->SetYTitle(TString(f->GetName())+"/"+h->GetName()); | |
933 | ||
934 | // Loop over all bins | |
935 | Int_t nbin = hRatio->GetNbinsX(); | |
936 | ||
937 | for(Int_t ibin = 1; ibin <= nbin; ibin++){ | |
938 | Double_t yhisto = h->GetBinContent(ibin); | |
939 | Double_t yerror = h->GetBinError(ibin); | |
940 | Double_t xmin = h->GetBinLowEdge(ibin); | |
941 | Double_t xmax = h->GetBinLowEdge(ibin+1); | |
942 | Double_t yfunc = f->Integral(xmin,xmax)/(xmax-xmin); | |
943 | Double_t ratio = invert ? yfunc/yhisto : yhisto/yfunc ; | |
944 | Double_t error = yerror/yfunc ; | |
945 | hRatio->SetBinContent(ibin,ratio); | |
946 | hRatio->SetBinError (ibin,error); | |
947 | } | |
948 | ||
949 | return hRatio; | |
950 | ||
951 | } | |
ee08b77d | 952 | |
953 | void AliBWTools::WeightedMean(Int_t npoints, const Double_t *x, const Double_t *xerr, Double_t &mean, Double_t &meanerr){ | |
954 | ||
3beb22a2 | 955 | // Performs the weighted mean of npoints numbers in x with errors in xerr |
ee08b77d | 956 | |
957 | mean = 0; | |
958 | meanerr = 0; | |
959 | ||
960 | Double_t sumweight = 0; | |
961 | ||
962 | for (Int_t ipoint = 0; ipoint < npoints; ipoint++){ | |
963 | ||
964 | Double_t xerr2 = xerr[ipoint]*xerr[ipoint]; | |
965 | if(xerr2>0){ | |
966 | // cout << "xe2 " << xerr2 << endl; | |
967 | Double_t weight = 1. / xerr2; | |
968 | sumweight += weight; | |
969 | mean += weight * x[ipoint]; | |
3725266c | 970 | }// else cout << " Skipping " << ipoint << endl; |
ee08b77d | 971 | |
972 | } | |
973 | ||
974 | ||
975 | if(sumweight){ | |
976 | mean /= sumweight; | |
977 | meanerr = TMath::Sqrt(1./ sumweight); | |
978 | } | |
979 | else { | |
3725266c | 980 | // cout << " No sumweight" << endl; |
ee08b77d | 981 | mean = 0; |
982 | meanerr = 0; | |
983 | } | |
984 | ||
985 | ||
986 | } | |
3725266c | 987 | |
058137a6 | 988 | void AliBWTools::GetValueAndError(TH1 * hdest, const TH1 * hvalue, const TH1 * herror, Bool_t isPercentError) { |
3725266c | 989 | |
990 | // Put into source, bin-by-bin, the values from hvalue and the | |
991 | // errors from content from herror. | |
992 | // Used mainly to combine histos of systemati errors with their spectra | |
993 | // Set isPercentError to kTRUE if the error is given in % | |
994 | ||
995 | if(hdest == NULL){ | |
996 | Printf("AliBWTools::GetValueAndError Errror: hdest is null"); | |
997 | return; | |
998 | } | |
999 | ||
1000 | ||
1001 | Int_t nbin = hdest->GetNbinsX(); | |
1002 | Int_t nBinSourceVal = hvalue->GetNbinsX(); | |
1003 | Int_t nBinSourceErr = herror->GetNbinsX(); | |
1004 | ||
1005 | for(Int_t iBinDest = 1; iBinDest <= nbin; iBinDest++){ | |
1006 | Float_t lowPtDest=hdest->GetBinLowEdge(iBinDest); | |
1007 | Float_t binWidDest=hdest->GetBinWidth(iBinDest); | |
1008 | // Loop over Source bins and find overlapping bins to Dest | |
1009 | // First value then error | |
1010 | // Value | |
1011 | Bool_t foundValue = kFALSE; | |
1012 | for(Int_t iBinSourceVal=1; iBinSourceVal<=nBinSourceVal; iBinSourceVal++){ | |
1013 | Float_t lowPtSource= hvalue->GetBinLowEdge(iBinSourceVal) ; | |
1014 | Float_t binWidSource= hvalue->GetBinWidth(iBinSourceVal); | |
1015 | if(TMath::Abs(lowPtDest-lowPtSource)<0.001 && TMath::Abs(binWidSource-binWidDest)<0.001){ | |
1016 | Double_t content = hvalue->GetBinContent(iBinSourceVal); | |
1017 | hdest->SetBinContent(iBinDest, content); | |
1018 | foundValue = kTRUE; | |
1019 | break; | |
1020 | } | |
1021 | } | |
1022 | // if (!foundValue){ | |
1023 | // Printf("AliBWTools::GetValueAndError: Error: cannot find matching value source bin for destination %d",iBinDest); | |
1024 | // } | |
1025 | ||
1026 | // Error | |
1027 | Bool_t foundError = kFALSE; | |
1028 | for(Int_t iBinSourceErr=1; iBinSourceErr<=nBinSourceErr; iBinSourceErr++){ | |
1029 | Float_t lowPtSource= herror->GetBinLowEdge(iBinSourceErr) ; | |
1030 | Float_t binWidSource= herror->GetBinWidth(iBinSourceErr); | |
1031 | if(TMath::Abs(lowPtDest-lowPtSource)<0.001 && TMath::Abs(binWidSource-binWidDest)<0.001){ | |
1032 | Double_t error = herror->GetBinContent(iBinSourceErr); | |
1033 | // cout << "-> " << iBinDest << " " << error << " " << hdest->GetBinContent(iBinDest) << endl; | |
1034 | ||
1035 | hdest->SetBinError(iBinDest, isPercentError ? error * hdest->GetBinContent(iBinDest) : error); | |
1036 | foundError=kTRUE; | |
1037 | break; | |
1038 | } | |
1039 | } | |
1040 | // if (!foundError ){ | |
1041 | // Printf("AliBWTools::GetValueAndError: Error: cannot find matching error source bin for destination %d",iBinDest); | |
1042 | // } | |
1043 | } | |
1044 | ||
1045 | ||
1046 | } | |
1047 | ||
058137a6 | 1048 | void AliBWTools::AddHisto(TH1 * hdest, const TH1* hsource, Bool_t getMirrorBins) { |
3725266c | 1049 | |
1050 | // Adds hsource to hdest bin by bin, even if they have a different | |
1051 | // binning If getMirrorBins is true, it takes the negative bins | |
1052 | // (Needed because sometimes the TPC uses the positive axis for | |
1053 | // negative particles and the possitive axis for positive | |
1054 | // particles). | |
1055 | ||
1056 | ||
1057 | if (hdest == NULL) { | |
1058 | Printf("Error: hdest is NULL\n"); | |
1059 | } | |
1060 | if (hsource == NULL) { | |
1061 | Printf("Error: hsource is NULL\n"); | |
1062 | } | |
1063 | ||
1064 | Int_t nBinSource = hsource->GetNbinsX(); | |
1065 | Int_t nBinDest = hdest->GetNbinsX(); | |
1066 | ||
1067 | // Loop over destination bins, | |
1068 | for(Int_t iBinDest=1; iBinDest<=nBinDest; iBinDest++){ | |
1069 | Float_t lowPtDest=hdest->GetBinLowEdge(iBinDest); | |
1070 | Float_t binWidDest=hdest->GetBinWidth(iBinDest); | |
1071 | // Loop over Source bins and find overlapping bins to Dest | |
1072 | Bool_t found = kFALSE; | |
1073 | for(Int_t iBinSource=1; iBinSource<=nBinSource; iBinSource++){ | |
1074 | Float_t lowPtSource= getMirrorBins ? -hsource->GetBinLowEdge(iBinSource)+hsource->GetBinWidth(iBinSource) : hsource->GetBinLowEdge(iBinSource) ; | |
1075 | Float_t binWidSource= hsource->GetBinWidth(iBinSource) ; | |
1076 | if(TMath::Abs(lowPtDest-lowPtSource)<0.001 && TMath::Abs(binWidSource-binWidDest)<0.001){ | |
1077 | Float_t dest=hdest->GetBinContent(iBinDest); | |
1078 | Float_t source=hsource->GetBinContent(iBinSource); | |
1079 | Float_t edest=hdest->GetBinError(iBinDest); | |
1080 | Float_t esource=hsource->GetBinError(iBinSource); | |
1081 | Double_t cont=dest+source; | |
1082 | Double_t econt=TMath::Sqrt(edest*edest+esource*esource); | |
1083 | hdest->SetBinContent(iBinDest,cont); | |
1084 | hdest->SetBinError (iBinDest,econt); | |
1085 | found = kTRUE; | |
1086 | ||
1087 | break; | |
1088 | } | |
1089 | } | |
1090 | if (!found){ | |
1091 | Printf("Error: cannot find matching source bin for destination %d",iBinDest); | |
1092 | } | |
1093 | } | |
1094 | ||
1095 | ||
1096 | } | |
1097 | ||
058137a6 | 1098 | void AliBWTools::GetHistoCombinedErrors(TH1 * hdest, const TH1 * h1) { |
1099 | ||
1100 | // Combine the errors of hdest with the errors of h1, summing in | |
1101 | // quadrature. Results are put in hdest. Histograms are assumed to | |
1102 | // have the same binning | |
1103 | ||
1104 | Int_t nbin = hdest->GetNbinsX(); | |
1105 | for(Int_t ibin = 0; ibin < nbin; ibin++){ | |
1106 | Double_t e1 = hdest->GetBinError(ibin); | |
1107 | Double_t e2 = h1->GetBinError(ibin); | |
1108 | hdest->SetBinError(ibin, TMath::Sqrt(e1*e1+e2*e2)); | |
1109 | } | |
1110 | ||
1111 | ||
1112 | } | |
d8e6677d | 1113 | |
1114 | TH1F * AliBWTools::DivideHistosDifferentBins(const TH1F* h1, const TH1F* h2) { | |
1115 | // Divides 2 histos even if they have a different binning. Finds | |
1116 | // overlapping bins and divides them | |
1117 | ||
1118 | // 1. clone histo | |
1119 | TH1F * hRatio = new TH1F(*h1); | |
1120 | Int_t nBinsH1=h1->GetNbinsX(); | |
1121 | Int_t nBinsH2=h2->GetNbinsX(); | |
1122 | // Loop over H1 bins, | |
1123 | for(Int_t iBin=1; iBin<=nBinsH1; iBin++){ | |
1124 | hRatio->SetBinContent(iBin,0.); | |
1125 | hRatio->SetBinContent(iBin,0.); | |
1126 | Float_t lowPtH1=h1->GetBinLowEdge(iBin); | |
1127 | Float_t binWidH1=h1->GetBinWidth(iBin); | |
1128 | // Loop over H2 bins and find overlapping bins to H1 | |
1129 | for(Int_t jBin=1; jBin<=nBinsH2; jBin++){ | |
1130 | Float_t lowPtH2=h2->GetBinLowEdge(jBin); | |
1131 | Float_t binWidH2=h2->GetBinWidth(jBin); | |
1132 | if(TMath::Abs(lowPtH1-lowPtH2)<0.001 && TMath::Abs(binWidH2-binWidH1)<0.001){ | |
1133 | Float_t numer=h1->GetBinContent(iBin); | |
1134 | Float_t denom=h2->GetBinContent(jBin); | |
1135 | Float_t enumer=h1->GetBinError(iBin); | |
1136 | Float_t edenom=h2->GetBinError(jBin); | |
1137 | Double_t ratio=0.; | |
1138 | Double_t eratio=0.; | |
1139 | if(numer>0. && denom>0.){ | |
1140 | ratio=numer/denom; | |
1141 | eratio=ratio*TMath::Sqrt((enumer/numer)*(enumer/numer)+(edenom/denom)*(edenom/denom)); | |
1142 | } | |
1143 | hRatio->SetBinContent(iBin,ratio); | |
1144 | hRatio->SetBinError(iBin,eratio); | |
1145 | break; | |
1146 | } | |
1147 | } | |
1148 | } | |
1149 | return hRatio; | |
1150 | } |