3 #include "TGraphErrors.h"
4 #include "TGraphAsymmErrors.h"
18 #include "SystematicErrorUtils.h"
20 const Int_t numSpecies = 5;
22 //________________________________________________________
23 TCanvas* calculateSystematics(TString canvName, TString canvTitle, TH1F** histos, Int_t numHistos, Int_t speciesID, Double_t /*nSigma*/,
24 const TString* systematicsHistosName, Int_t reference, TH1F** hSystematics, TGraphAsymmErrors** gr,
25 Bool_t ignoreSigmaErrors)
28 // Since the method with the root finding already takes into account the statistical error,
29 // there is no need to use nSigma > 0.
30 // If the statistical error is ignored, nevertheless don't use nSigma > 0 because this might
31 // give zero systematic error for high pT, which is usually not accepted by people, although
32 // the natural point of view "no systematic visible for given statistical error" is reasonable to me.
39 for (Int_t j = 0; j < numHistos; j++) {
40 hSystematics[j] = new TH1F(*histos[j]);
41 hSystematics[j]->SetName(Form("%s_%s", systematicsHistosName[j].Data(), AliPID::ParticleName(speciesID)));
42 hSystematics[j]->Reset();
43 hSystematics[j]->GetXaxis()->SetRange(0, -1);
45 for (Int_t bin = 1; bin <= histos[j]->GetNbinsX(); bin++) {
46 hSystematics[j]->SetBinContent(bin, histos[reference]->GetBinContent(bin) - histos[j]->GetBinContent(bin));
47 hSystematics[j]->SetBinError(bin, TMath::Sqrt(TMath::Abs(TMath::Power(histos[reference]->GetBinError(bin), 2) -
48 TMath::Power(histos[j]->GetBinError(bin), 2))));
50 if (hSystematics[j]->GetBinError(bin) == 0)
51 hSystematics[j]->SetBinError(bin, 1e-10);
52 Double_t temp = hSystematics[j]->GetBinContent(bin) + hSystematics[j]->GetBinError(bin);
56 temp = hSystematics[j]->GetBinContent(bin) - hSystematics[j]->GetBinError(bin);
62 TCanvas* canv = new TCanvas(canvName.Data(), canvTitle.Data(),100,10,1200,800);
65 hSystematics[reference]->Draw("e p");
66 hSystematics[reference]->GetYaxis()->SetRangeUser(ymin, ymax);
67 for (Int_t j = 0; j < numHistos; j++) {
71 hSystematics[j]->SetMarkerStyle(20 + j);
72 hSystematics[j]->Draw("e p same");
75 TLegend* legend = new TLegend(0.622126, 0.605932, 0.862069, 0.855932);
76 legend->SetBorderSize(0);
77 legend->SetFillColor(0);
79 for (Int_t j = 0; j < numHistos; j++) {
80 legend->AddEntry(hSystematics[j], Form("%s", systematicsHistosName[j].Data()), "p");
85 const Int_t nBins = histos[reference]->GetNbinsX();
89 Double_t yerrl[nBins];
90 Double_t yerrh[nBins];
92 Double_t meansForFit[numHistos];
93 Double_t sigmasForFit[numHistos];
95 for (Int_t bin = 0; bin < nBins; bin++) {
96 x[bin] = histos[reference]->GetBinCenter(bin + 1);
97 xerr[bin] = histos[reference]->GetBinWidth(bin + 1) / 2.;
98 y[bin] = histos[reference]->GetBinContent(bin + 1);
100 for (Int_t j = 0; j < numHistos; j++) {
101 meansForFit[j] = histos[j]->GetBinContent(bin + 1);
102 sigmasForFit[j] = histos[j]->GetBinError(bin + 1);
105 yerrl[bin] = yerrh[bin] = findSystematicError(numHistos, meansForFit, sigmasForFit, ignoreSigmaErrors);
108 TGraphAsymmErrors* gTemp = new TGraphAsymmErrors(nBins, x, y, xerr, xerr, yerrl, yerrh);
110 (*gr)->SetName(Form("systematicError_%s", AliPID::ParticleName(speciesID)));
111 (*gr)->SetLineColor(hSystematics[0]->GetMarkerColor());
112 //(*gr)->SetFillColor(kGray);
113 (*gr)->SetFillStyle(0);//3004 + reference);
120 //________________________________________________________
121 TCanvas* calculateSystematics(TString canvName, TString canvTitle, TH1F** histos, Int_t numHistos, Int_t speciesID, Double_t nSigma,
122 const TString* systematicsHistosName, Int_t reference, TH1F** hSystematics, TGraphAsymmErrors** gr)
127 for (Int_t j = 0; j < numHistos; j++) {
128 hSystematics[j] = new TH1F(*histos[j]);
129 hSystematics[j]->SetName(Form("%s_%s", systematicsHistosName[j].Data(), AliPID::ParticleName(speciesID)));
130 hSystematics[j]->Reset();
131 hSystematics[j]->GetXaxis()->SetRange(0, -1);
133 for (Int_t bin = 1; bin <= histos[j]->GetNbinsX(); bin++) {
134 hSystematics[j]->SetBinContent(bin, histos[reference]->GetBinContent(bin) - histos[j]->GetBinContent(bin));
135 hSystematics[j]->SetBinError(bin, TMath::Sqrt(TMath::Abs(TMath::Power(histos[reference]->GetBinError(bin), 2) -
136 TMath::Power(histos[j]->GetBinError(bin), 2))));
138 if (hSystematics[j]->GetBinError(bin) == 0)
139 hSystematics[j]->SetBinError(bin, 1e-10);
140 Double_t temp = hSystematics[j]->GetBinContent(bin) + hSystematics[j]->GetBinError(bin);
144 temp = hSystematics[j]->GetBinContent(bin) - hSystematics[j]->GetBinError(bin);
150 TCanvas* canv = new TCanvas(canvName.Data(), canvTitle.Data(),100,10,1200,800);
153 hSystematics[reference]->Draw("e p");
154 hSystematics[reference]->GetYaxis()->SetRangeUser(ymin, ymax);
155 for (Int_t j = 0; j < numHistos; j++) {
159 hSystematics[j]->SetMarkerStyle(20 + j);
160 hSystematics[j]->Draw("e p same");
163 TLegend* legend = new TLegend(0.622126, 0.605932, 0.862069, 0.855932);
164 legend->SetBorderSize(0);
165 legend->SetFillColor(0);
167 for (Int_t j = 0; j < numHistos; j++) {
168 legend->AddEntry(hSystematics[j], Form("%s", systematicsHistosName[j].Data()), "p");
173 const Int_t nBins = histos[reference]->GetNbinsX();
176 Double_t xerr[nBins];
177 Double_t yerrl[nBins];
178 Double_t yerrh[nBins];
180 for (Int_t bin = 0; bin < nBins; bin++) {
181 x[bin] = histos[reference]->GetBinCenter(bin + 1);
182 xerr[bin] = histos[reference]->GetBinWidth(bin + 1) / 2.;
183 y[bin] = histos[reference]->GetBinContent(bin + 1);
185 // Take all points that are more than nSigma sigma away from 0.
186 // If there are at least 2 such points, take the difference between
187 // the extreme values (i.e. maximum and minimum) as a measure of
190 Double_t deltaMin = 0;
191 Double_t deltaMax = 0;
193 for (Int_t j = 0; j < numHistos; j++) {
194 if (hSystematics[j]->GetBinError(bin + 1) == 0) // Per definition always true for reference histo
197 Double_t delta = hSystematics[j]->GetBinContent(bin + 1);
198 if (TMath::Abs(delta / hSystematics[j]->GetBinError(bin + 1)) > nSigma) {
204 if (delta < deltaMin)
206 if (delta > deltaMax)
214 // yerrh[bin] = deltaMax;
219 // yerrl[bin] = -deltaMin;
223 if (count < 1) // Reference histo is not counted. One can only do systematics if there is at least one other histogram
224 yerrl[bin] = yerrh[bin] = 0.;
226 yerrl[bin] = yerrh[bin] = (deltaMax - deltaMin) / TMath::Sqrt(2);
230 TGraphAsymmErrors* gTemp = new TGraphAsymmErrors(nBins, x, y, xerr, xerr, yerrl, yerrh);
232 (*gr)->SetName(Form("systematicError_%s", AliPID::ParticleName(speciesID)));
233 (*gr)->SetLineColor(hSystematics[0]->GetMarkerColor());
234 //(*gr)->SetFillColor(kGray);
235 (*gr)->SetFillStyle(0);//3004 + reference);
241 //________________________________________________________
242 TCanvas* DrawFractionHistos(TString canvName, TString canvTitle, Double_t pLow, Double_t pHigh, TH1F*** hist, Int_t reference,
243 TGraphAsymmErrors** gr)
245 TCanvas* canv = new TCanvas(canvName.Data(), canvTitle.Data(),100,10,1200,800);
249 for (Int_t i = 0; i < numSpecies; i++) {
250 hist[i][reference]->GetYaxis()->SetRangeUser(0.0, 1.0);
251 hist[i][reference]->GetYaxis()->SetTitle(canvTitle.Data());
252 hist[i][reference]->GetXaxis()->SetRangeUser(pLow, pHigh);
253 //hist[i][reference]->SetFillStyle(3004 + i);
254 //hist[i][reference]->SetFillColor(kGray);
255 hist[i][reference]->SetFillStyle(0);
256 hist[i][reference]->SetFillColor(hist[i][reference]->GetMarkerColor());
257 hist[i][reference]->SetLineColor(hist[i][reference]->GetMarkerColor());
259 hist[2][reference]->SetMarkerStyle(20);
260 hist[2][reference]->Draw("e p");
261 hist[0][reference]->SetMarkerStyle(21);
262 hist[0][reference]->Draw("e p same");
263 hist[1][reference]->SetMarkerStyle(22);
264 hist[1][reference]->Draw("e p same");
265 hist[3][reference]->SetMarkerStyle(29);
266 hist[3][reference]->Draw("e p same");
267 hist[4][reference]->SetMarkerStyle(30);
268 hist[4][reference]->Draw("e p same");
270 gr[0]->Draw("2 same");
271 gr[1]->Draw("2 same");
272 gr[2]->Draw("2 same");
273 gr[3]->Draw("2 same");
274 gr[4]->Draw("2 same");
276 TLegend* legend = new TLegend(0.622126, 0.605932, 0.862069, 0.855932);
277 legend->SetBorderSize(0);
278 legend->SetFillColor(0);
279 legend->AddEntry(hist[2][reference], "#pi", "flp");
280 legend->AddEntry(hist[0][reference], "e", "flp");
281 legend->AddEntry(hist[1][reference], "K", "flp");
282 legend->AddEntry(hist[3][reference], "p", "flp");
283 legend->AddEntry(hist[4][reference], "#mu", "flp");
290 //________________________________________________________
291 TH1F* loadHisto(const TString histName, TFile* f)
294 std::cout << "No file. Cannot load hist \"" << histName.Data() << "\n!" << std::endl;
298 TH1F* hTemp = dynamic_cast<TH1F*>(f->Get(histName.Data()));
300 std::cout << "Failed to load histo \"" << histName.Data() << "\"!" << std::endl;
308 //________________________________________________________
309 Int_t SystematicErrorEstimation(const TString path, const TString outFileTitle, const TString* fileNames, const TString* histTitles,
310 const Int_t numFiles, const Double_t nSigma, const Bool_t ignoreSigmaErrors)
312 if (!fileNames || numFiles < 1)
316 TH1F** hFractions[numSpecies];
317 for (Int_t i = 0; i < numSpecies; i++)
318 hFractions[i] = new TH1F*[numFiles];
320 const Int_t reference = 0;
321 TH1F* hYields[numSpecies]; // Only the reference yields
323 const TString histNames[numSpecies] = {"hFractionElectrons", "hFractionKaons", "hFractionPions", "hFractionProtons", "hFractionMuons" };
325 const TString histNamesYields[numSpecies] = {"hYieldElectrons", "hYieldKaons", "hYieldPions", "hYieldProtons", "hYieldMuons" };
327 for (Int_t iFile = 0; iFile < numFiles; iFile++) {
328 f[iFile] = TFile::Open(fileNames[iFile].Data());
330 std::cout << "Failed to open file \"" << fileNames[iFile].Data() << "\"!" << std::endl;
334 // Extract the data histograms
335 for (Int_t i = 0; i < numSpecies; i++) {
336 hFractions[i][iFile] = loadHisto(histNames[i], f[iFile]);
337 if (!hFractions[i][iFile])
340 if (iFile == reference) {
341 hYields[i] = loadHisto(histNamesYields[i], f[iFile]);
349 TGraphAsymmErrors* grSysErrors[numSpecies] = {0x0,};
350 TGraphAsymmErrors* grSysErrorsYields[numSpecies] = {0x0,};
352 TH1F* hSystematicsPions[numFiles];
353 TCanvas* cSystematicsPions = calculateSystematics("cSystematicsPions", "Systematics Pions", hFractions[2], numFiles,
354 AliPID::kPion, nSigma,
355 histTitles, reference, hSystematicsPions, &grSysErrors[2], ignoreSigmaErrors);
357 TH1F* hSystematicsElectrons[numFiles];
358 TCanvas* cSystematicsElectrons = calculateSystematics("cSystematicsElectrons", "Systematics Electrons", hFractions[0], numFiles,
360 nSigma, histTitles, reference, hSystematicsElectrons,
361 &grSysErrors[0], ignoreSigmaErrors);
363 TH1F* hSystematicsKaons[numFiles];
364 TCanvas* cSystematicsKaons = calculateSystematics("cSystematicsKaons", "Systematics Kaons", hFractions[1], numFiles, AliPID::kKaon, nSigma,
365 histTitles, reference, hSystematicsKaons, &grSysErrors[1], ignoreSigmaErrors);
367 TH1F* hSystematicsProtons[numFiles];
368 TCanvas* cSystematicsProtons = calculateSystematics("cSystematicsProtons", "Systematics Protons", hFractions[3], numFiles,
369 AliPID::kProton, nSigma,
370 histTitles, reference, hSystematicsProtons, &grSysErrors[3], ignoreSigmaErrors);
372 TH1F* hSystematicsMuons[numFiles];
373 TCanvas* cSystematicsMuons = calculateSystematics("cSystematicsMuons", "Systematics Muons", hFractions[4], numFiles,
374 AliPID::kMuon, nSigma,
375 histTitles, reference, hSystematicsMuons, &grSysErrors[4], ignoreSigmaErrors);
377 Double_t pLow = 0.15;
378 Double_t pHigh = 50.;
379 TCanvas* cFractionsWithSystematicError = DrawFractionHistos("cFractionsWithSystematicError", "Particle fractions", pLow, pHigh, hFractions, reference,
383 //TODO At the moment, the error of the fractions and the yield is just a constant factor (number of tracks in that bin)
384 // (-> But this can change in future (I have to think about it a little bit more carefully)).
385 // Thus, the relative errors are the same for fractions and yields and I can just use this fact to
386 // transform the errors from the fractions to those of the yields.
387 // However, this causes trouble in case of fraction = 0. Therefore, sum up the yields to the total yield and use this for scaling
388 for (Int_t i = 0; i < numSpecies; i++) {
389 grSysErrorsYields[i] = new TGraphAsymmErrors(*grSysErrors[i]);
390 TString name = grSysErrors[i]->GetName();
391 name.ReplaceAll("systematicError_", "systematicErrorYields_");
392 grSysErrorsYields[i]->SetName(name.Data());
394 for (Int_t ind = 0; ind < grSysErrorsYields[i]->GetN(); ind++) {
395 Double_t totalYield = 0;
396 for (Int_t j = 0; j < numSpecies; j++)
397 totalYield += hYields[j]->GetBinContent(ind + 1);
399 const Double_t yield = hYields[i]->GetBinContent(ind + 1);
400 const Double_t sysErrorLow = grSysErrors[i]->GetErrorYlow(ind);
401 const Double_t sysErrorHigh = grSysErrors[i]->GetErrorYhigh(ind);
403 grSysErrorsYields[i]->SetPoint(ind, grSysErrorsYields[i]->GetX()[ind], yield);
404 grSysErrorsYields[i]->SetPointEYhigh(ind, totalYield * sysErrorHigh);
405 grSysErrorsYields[i]->SetPointEYlow(ind, totalYield * sysErrorLow);
408 Double_t totalYield = 0;
409 for (Int_t j = 0; j < numSpecies; j++)
410 totalYield += hYields[j]->GetBinContent(ind + 1);
412 const Double_t yield = hYields[i]->GetBinContent(ind + 1);
413 const Double_t fraction = hFractions[i][reference]->GetBinContent(ind + 1);
414 const Double_t sysErrorLow = grSysErrors[i]->GetErrorYlow(ind);
415 const Double_t sysErrorHigh = grSysErrors[i]->GetErrorYhigh(ind);
417 if (fraction <= 0.) {
418 printf("Error: Fraction = 0 for species %d. Cannot transform error....\n", i);
421 const Double_t sysErrorLowRel = sysErrorLow / fraction;
422 const Double_t sysErrorHighRel = sysErrorHigh / fraction;
424 grSysErrorsYields[i]->SetPoint(ind, grSysErrorsYields[i]->GetX()[ind], yield);
425 grSysErrorsYields[i]->SetPointEYhigh(ind, sysErrorHighRel * yield);
426 grSysErrorsYields[i]->SetPointEYlow(ind, sysErrorLowRel * yield);
435 TString saveFileName;
437 saveFileName = Form("outputSystematics_%s_nSigma%.1f__%04d_%02d_%02d.root", outFileTitle.Data(), nSigma, daTime.GetYear(),
438 daTime.GetMonth(), daTime.GetDay());
440 fSave = TFile::Open(Form("%s/%s", path.Data(), saveFileName.Data()), "recreate");
442 std::cout << "Failed to open save file \"" << Form("%s/%s", path.Data(), saveFileName.Data()) << "\"!" << std::endl;
446 // Save final results
449 for (Int_t i = 0; i < numFiles; i++) {
450 if (hSystematicsElectrons[i])
451 hSystematicsElectrons[i]->Write();
453 if (hSystematicsPions[i])
454 hSystematicsPions[i]->Write();
456 if (hSystematicsKaons[i])
457 hSystematicsKaons[i]->Write();
459 if (hSystematicsProtons[i])
460 hSystematicsProtons[i]->Write();
461 if (hSystematicsMuons[i])
462 hSystematicsMuons[i]->Write();
465 if (cSystematicsElectrons)
466 cSystematicsElectrons->Write();
468 if (cSystematicsPions)
469 cSystematicsPions->Write();
471 if (cSystematicsKaons)
472 cSystematicsKaons->Write();
474 if (cSystematicsProtons)
475 cSystematicsProtons->Write();
477 if (cSystematicsMuons)
478 cSystematicsMuons->Write();
480 if (cFractionsWithSystematicError)
481 cFractionsWithSystematicError->Write();
483 for (Int_t i = 0; i < numSpecies; i++) {
484 if (hFractions[i][reference])
485 hFractions[i][reference]->Write();
488 grSysErrors[i]->Write();
493 if (grSysErrorsYields[i])
494 grSysErrorsYields[i]->Write();
497 // Save list of file names in output file
498 TString listOfFileNames = "";
499 for (Int_t i = 0; i < numFiles; i++) {
500 listOfFileNames.Append(Form("%s%d: %s", i == 0 ? "" : ", ", i, fileNames[i].Data()));
503 TNamed* settings = new TNamed(Form("Used files for systematics: %s\n", listOfFileNames.Data()),
504 Form("Used files for systematics: %s\n", listOfFileNames.Data()));
509 delete cSystematicsElectrons;
510 delete cSystematicsPions;
511 delete cSystematicsKaons;
512 delete cSystematicsMuons;
513 delete cSystematicsProtons;
514 delete cFractionsWithSystematicError;