1 /**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
16 // This class is used to store correlation maps, generated and reconstructed data of the jet spectrum
17 // It also contains functions to correct the spectrum using the bayesian unfolding
20 #include "AliJetSpectrumUnfolding.h"
26 #include <TDirectory.h>
27 #include <TVirtualFitter.h>
33 #include <TCollection.h>
38 #include <TProfile2D.h>
42 #include <THnSparse.h>
44 ClassImp(AliJetSpectrumUnfolding)
46 const Int_t NBINSE = 50;
47 const Int_t NBINSZ = 50;
48 const Int_t NEVENTS = 500000;
49 const Double_t axisLowerLimitE = 0.;
50 const Double_t axisLowerLimitZ = 0.;
51 const Double_t axisUpperLimitE = 250.;
52 const Double_t axisUpperLimitZ = 1.;
54 Float_t AliJetSpectrumUnfolding::fgBayesianSmoothing = 1; // smoothing parameter (0 = no smoothing)
55 Int_t AliJetSpectrumUnfolding::fgBayesianIterations = 100; // number of iterations in Bayesian method
57 //____________________________________________________________________
59 AliJetSpectrumUnfolding::AliJetSpectrumUnfolding() :
60 TNamed(), fCurrentRec(0), fCurrentCorrelation(0), fGenSpectrum(0),
61 fRecSpectrum(0), fUnfSpectrum(0), fCorrelation(0), fLastChi2MC(0), fLastChi2MCLimit(0), fLastChi2Residuals(0), fRatioAverage(0)
64 // default constructor
73 //____________________________________________________________________
74 AliJetSpectrumUnfolding::AliJetSpectrumUnfolding(const Char_t* name, const Char_t* title) :
75 TNamed(name, title), fCurrentRec(0), fCurrentCorrelation(0), fGenSpectrum(0),
76 fRecSpectrum(0), fUnfSpectrum(0), fCorrelation(0), fLastChi2MC(0), fLastChi2MCLimit(0), fLastChi2Residuals(0), fRatioAverage(0)
82 // do not add this hists to the directory
83 Bool_t oldStatus = TH1::AddDirectoryStatus();
84 TH1::AddDirectory(kFALSE);
86 #define ZBINNING NBINSZ, axisLowerLimitZ, axisUpperLimitZ // fragmentation of leading particle
87 #define EBINNING NBINSE, axisLowerLimitE, axisUpperLimitE // energy of the jet
89 fRecSpectrum = new TH2F("fRecSpectrum", "Reconstructed Spectrum;E^{jet}_{rec} [GeV];z^{lp}_{rec}", EBINNING, ZBINNING);
90 fGenSpectrum = new TH2F("fGenSpectrum", "Generated Spectrum;E^{jet}_{gen} [GeV];z^{lp}_{gen}", EBINNING, ZBINNING);
91 fUnfSpectrum = new TH2F("fUnfSpectrum", "Unfolded Spectrum;E^{jet} [GeV];z^{lp}", EBINNING, ZBINNING);
93 const Int_t nbin[4]={NBINSE, NBINSE, NBINSZ, NBINSZ};
94 //arrays for bin limits
95 Double_t LowEdge[4] = {axisLowerLimitE, axisLowerLimitE, axisLowerLimitZ, axisLowerLimitZ};
96 Double_t UpEdge[4] = {axisUpperLimitE, axisUpperLimitE, axisUpperLimitZ, axisUpperLimitZ};
98 fCorrelation = new THnSparseF("fCorrelation", "Correlation Function", 4, nbin, LowEdge, UpEdge);
100 TH1::AddDirectory(oldStatus);
103 //____________________________________________________________________
104 AliJetSpectrumUnfolding::~AliJetSpectrumUnfolding()
128 //____________________________________________________________________
129 Long64_t AliJetSpectrumUnfolding::Merge(TCollection* list)
131 // Merge a list of AliJetSpectrumUnfolding objects with this (needed for
133 // Returns the number of merged objects (including this).
141 TIterator* iter = list->MakeIterator();
144 // collections of all histograms
145 TList collections[4];
148 while ((obj = iter->Next())) {
150 AliJetSpectrumUnfolding* entry = dynamic_cast<AliJetSpectrumUnfolding*> (obj);
154 collections[0].Add(entry->fGenSpectrum);
155 collections[1].Add(entry->fRecSpectrum);
156 collections[2].Add(entry->fUnfSpectrum);
157 collections[3].Add(entry->fCorrelation);
162 fGenSpectrum->Merge(&collections[0]);
163 fRecSpectrum->Merge(&collections[1]);
164 fUnfSpectrum->Merge(&collections[2]);
165 fCorrelation->Merge(&collections[3]);
172 //____________________________________________________________________
173 Bool_t AliJetSpectrumUnfolding::LoadHistograms(const Char_t* dir)
176 // loads the histograms from a file
177 // if dir is empty a directory with the name of this object is taken (like in SaveHistogram)
183 if (!gDirectory->cd(dir))
186 Bool_t success = kTRUE;
188 // store old histograms to delete them later
190 oldHistograms.SetOwner(1);
192 if (fGenSpectrum) oldHistograms.Add(fGenSpectrum);
193 if (fRecSpectrum) oldHistograms.Add(fRecSpectrum);
194 if (fUnfSpectrum) oldHistograms.Add(fUnfSpectrum);
195 if (fCorrelation) oldHistograms.Add(fCorrelation);
197 // load new histograms
198 fGenSpectrum = dynamic_cast<TH2F*> (gDirectory->Get(fGenSpectrum->GetName()));
202 fRecSpectrum = dynamic_cast<TH2F*> (gDirectory->Get(fRecSpectrum->GetName()));
206 fUnfSpectrum = dynamic_cast<TH2F*> (gDirectory->Get(fUnfSpectrum->GetName()));
210 fCorrelation = dynamic_cast<THnSparseF*> (gDirectory->Get(fCorrelation->GetName()));
214 gDirectory->cd("..");
216 // delete old histograms
217 oldHistograms.Delete();
222 //____________________________________________________________________
223 void AliJetSpectrumUnfolding::SaveHistograms()
226 // saves the histograms
229 gDirectory->mkdir(GetName());
230 gDirectory->cd(GetName());
233 fGenSpectrum->Write();
236 fRecSpectrum->Write();
239 fUnfSpectrum->Write();
242 fCorrelation->Write();
244 gDirectory->cd("..");
247 //____________________________________________________________________
248 void AliJetSpectrumUnfolding::SetupCurrentHists(Bool_t createBigBin)
251 // resets fUnfSpectrum
254 fUnfSpectrum->Reset();
255 fUnfSpectrum->Sumw2();
257 fCurrentRec = (TH2F*)fRecSpectrum->Clone("fCurrentRec");
258 fCurrentRec->Sumw2();
260 fCurrentCorrelation = (THnSparseF*)fCorrelation->Clone("fCurrentCorrelation");
261 fCurrentCorrelation->Sumw2();
263 Printf("Correlation Matrix has %.0E filled bins", fCurrentCorrelation->GetNbins());
267 Int_t maxBinE = 0, maxBinZ = 0;
268 Float_t maxE = 0, maxZ = 0;
269 for (Int_t me=1; me<=fCurrentRec->GetNbinsX(); me++)
270 for (Int_t mz=1; mz<=fCurrentRec->GetNbinsY(); mz++)
272 if (fCurrentRec->GetBinContent(me,mz) <= 5 && me>NBINSE/2 && mz>NBINSZ/2)
276 maxE = fCurrentRec->GetXaxis()->GetBinCenter(me);
277 maxZ = fCurrentRec->GetYaxis()->GetBinCenter(mz);
282 if (maxBinE > 0 || maxBinZ > 0)
284 printf("Bin limit in measured spectrum is e = %d and z = %d.\n", maxBinE, maxBinZ);
285 fCurrentRec->SetBinContent(maxBinE, maxBinZ, fCurrentRec->Integral(maxBinE, fCurrentRec->GetNbinsX(), maxBinZ, fCurrentRec->GetNbinsY()));
286 for (Int_t me=maxBinE+1; me<=fCurrentRec->GetNbinsX(); me++)
287 for (Int_t mz=maxBinZ+1; mz<=fCurrentRec->GetNbinsY(); mz++)
289 fCurrentRec->SetBinContent(me, mz, 0);
290 fCurrentRec->SetBinError(me, mz, 0);
292 // the error is set to sqrt(N), better solution possible?, sum of relative errors of all contributions???
293 fCurrentRec->SetBinError(maxBinE, maxBinZ, TMath::Sqrt(fCurrentRec->GetBinContent(maxBinE, maxBinZ)));
295 printf("This bin has now %f +- %f entries\n", fCurrentRec->GetBinContent(maxBinE, maxBinZ), fCurrentRec->GetBinError(maxBinE, maxBinZ));
297 /* for (Int_t te=1; te<=NBINSE; te++)
299 for (Int_t tz=1; tz<=NBINSZ; tz++)
301 Int_t binMin[4] = {te, maxBinE, tz, maxBinZ};
302 Int_t binMax[4] = {NBINSE, NBINSE, NBINSZ, NBINSZ};
304 for (Int_t ite=te; ite<=NBINSE; ite++)
305 for (Int_t itz=tz; itz<=NBINSZ; itz++)
306 for (Int_t ime=maxBinE; ime<=NBINSE; ime++)
307 for (Int_t imz=maxBinZ; imz<=NBINSZ; imz++)
309 Int_t bin[4] = {ite, ime, itz, imz};
310 sum += fCurrentCorrelation->GetBinContent(bin);
312 fCurrentCorrelation->SetBinContent(binMin, sum);
313 fCurrentCorrelation->SetBinError(binMin, TMath::Sqrt(fCurrentCorrelation->GetBinContent(binMin)));
314 printf("create big bin1, nbins = %d, te = %d, tz = %d\n", NBINSE, te, tz);
315 for (Int_t me=maxBinE; me<=NBINSE; me++)
317 for (Int_t mz=maxBinZ; mz<=NBINSZ; mz++)
319 Int_t bin[4] = {te, me, tz, mz};
320 fCurrentCorrelation->SetBinContent(bin, 0.);
321 fCurrentCorrelation->SetBinError(bin, 0.);
322 printf("create big bin2\n");
328 for(Int_t idx = 0; idx<=fCurrentCorrelation->GetNbins(); idx++)
331 Float_t binContent = fCurrentCorrelation->GetBinContent(idx,bin);
332 Float_t binError = fCurrentCorrelation->GetBinError(idx);
333 Int_t binMin[4] = {bin[0], maxBinE, bin[2], maxBinZ};
334 if ( (bin[1]>maxBinE && bin[1]<=NBINSE) && (bin[3]>maxBinZ && bin[3]<=NBINSZ) )
336 fCurrentCorrelation->SetBinContent(binMin, binContent + fCurrentCorrelation->GetBinContent(binMin));
337 fCurrentCorrelation->SetBinError(binMin, binError + TMath::Sqrt(fCurrentCorrelation->GetBinContent(binMin)));
338 fCurrentCorrelation->SetBinContent(bin, 0.);
339 fCurrentCorrelation->SetBinError(bin, 0.);
341 printf("create big bin1, nbins = %d, te = %d, tz = %d\n", NBINSE, bin[0], bin[1]);
344 printf("Adjusted correlation matrix!\n");
346 } // end Create Big Bin
350 //____________________________________________________________________
351 void AliJetSpectrumUnfolding::SetBayesianParameters(Float_t smoothing, Int_t nIterations)
354 // sets the parameters for Bayesian unfolding
357 fgBayesianSmoothing = smoothing;
358 fgBayesianIterations = nIterations;
360 printf("AliJetSpectrumUnfolding::SetBayesianParameters --> Paramaters set to %d iterations with smoothing %f\n", fgBayesianIterations, fgBayesianSmoothing);
363 //____________________________________________________________________
364 void AliJetSpectrumUnfolding::NormalizeToBinWidth(TH2* hist)
367 // normalizes a 2-d histogram to its bin width (x width * y width)
370 for (Int_t i=1; i<=hist->GetNbinsX(); i++)
371 for (Int_t j=1; j<=hist->GetNbinsY(); j++)
373 Double_t factor = hist->GetXaxis()->GetBinWidth(i) * hist->GetYaxis()->GetBinWidth(j);
374 hist->SetBinContent(i, j, hist->GetBinContent(i, j) / factor);
375 hist->SetBinError(i, j, hist->GetBinError(i, j) / factor);
379 //____________________________________________________________________
380 void AliJetSpectrumUnfolding::DrawHistograms()
383 // draws the histograms of this class
386 gStyle->SetPalette(1);
388 TCanvas* canvas1 = new TCanvas("fRecSpectrum", "fRecSpectrum", 900, 600);
390 fRecSpectrum->DrawCopy("COLZ");
392 TCanvas* canvas2 = new TCanvas("fGenSpectrum", "fGenSpectrum", 900, 600);
394 fGenSpectrum->DrawCopy("COLZ");
396 TCanvas* canvas3 = new TCanvas("fUnfSpectrum", "fUnfSpectrum", 900, 600);
398 fUnfSpectrum->DrawCopy("COLZ");
400 TCanvas* canvas4 = new TCanvas("fCorrelation", "fCorrelation", 500, 500);
405 TH2D* h0 = fCorrelation->Projection(1,0);
406 h0->SetXTitle("E^{jet}_{gen} [GeV]");
407 h0->SetYTitle("E^{jet}_{rec} [GeV]");
408 h0->SetTitle("Projection: Jet Energy");
409 h0->DrawCopy("colz");
413 TH2D* h1 = fCorrelation->Projection(3,2);
414 h1->SetXTitle("z^{lp}_{gen}");
415 h1->SetYTitle("z^{lp}_{rec}");
416 h1->SetTitle("Projection: Leading Particle Fragmentation");
417 h1->DrawCopy("colz");
421 //____________________________________________________________________
422 void AliJetSpectrumUnfolding::DrawComparison(const char* name, TH2* genHist)
425 if (fUnfSpectrum->Integral() == 0)
427 printf("ERROR. Unfolded histogram is empty\n");
431 //regain measured distribution used for unfolding, because the bins were modified in SetupCurrentHists
433 fCurrentRec = (TH2F*)fRecSpectrum->Clone();
434 fCurrentRec->Sumw2();
435 fCurrentRec->Scale(1.0 / fCurrentRec->Integral());
437 // normalize unfolded result to 1
438 fUnfSpectrum->Scale(1.0 / fUnfSpectrum->Integral());
440 // find bin with <= 100 entries. this is used as limit for the chi2 calculation
441 Int_t mcBinLimitE = 0, mcBinLimitZ = 0;
442 for (Int_t i=0; i<genHist->GetNbinsX(); ++i)
443 for (Int_t j=0; j<genHist->GetNbinsY(); ++j)
445 if (genHist->GetBinContent(i,j) > 100)
453 Printf("AliJetSpectrumUnfolding::DrawComparison: Gen bin limit is (x,y) = (%d,%d)", mcBinLimitE,mcBinLimitZ);
455 // scale to 1 true spectrum
457 genHist->Scale(1.0 / genHist->Integral());
459 // calculate residual
460 // for that we convolute the response matrix with the gathered result
461 TH2* tmpRecRecorrected = (TH2*) fUnfSpectrum->Clone("tmpRecRecorrected");
462 TH2* convoluted = CalculateRecSpectrum(tmpRecRecorrected);
463 if (convoluted->Integral() > 0)
464 convoluted->Scale(1.0 / convoluted->Integral());
466 printf("ERROR: convoluted is empty. Something went wrong calculating the convoluted histogram.\n");
468 TH2* residual = (TH2*) convoluted->Clone("residual");
469 residual->SetTitle("(R#otimesUnfolded - Reconstructed)/Reconstructed;E^{jet} [GeV]; z^{lp}");
471 fCurrentRec->Scale(1./fCurrentRec->Integral());
472 residual->Add(fCurrentRec, -1);
473 //residual->Divide(residual, fCurrentRec, 1, 1, "B");
476 TCanvas* canvas1 = new TCanvas(name, name, 1000, 1000);
477 canvas1->Divide(2, 3);
480 const Int_t NRGBs = 5;
481 const Int_t NCont = 500;
483 Double_t stops[NRGBs] = { 0.00, 0.34, 0.61, 0.84, 1.00 };
484 Double_t red[NRGBs] = { 0.00, 0.00, 0.87, 1.00, 0.51 };
485 Double_t green[NRGBs] = { 0.00, 0.81, 1.00, 0.20, 0.00 };
486 Double_t blue[NRGBs] = { 0.51, 1.00, 0.12, 0.00, 0.00 };
487 TColor::CreateGradientColorTable(NRGBs, stops, red, green, blue, NCont);
488 gStyle->SetNumberContours(NCont);
491 gStyle->SetPalette(style);
493 genHist->SetTitle("Generated Spectrum;E^{jet}_{gen} [GeV];z^{lp}");
494 genHist->SetStats(0);
495 genHist->DrawCopy("colz");
498 gStyle->SetPalette(style);
500 fUnfSpectrum->SetStats(0);
501 fUnfSpectrum->DrawCopy("colz");
504 gStyle->SetPalette(style);
506 fCurrentRec->SetTitle(fRecSpectrum->GetTitle());
507 fCurrentRec->SetStats(0);
508 fCurrentRec->DrawCopy("colz");
511 gStyle->SetPalette(style);
513 TH1D* projGenX = genHist->ProjectionX();
514 projGenX->SetTitle("Projection: Jet Energy; E^{jet} [GeV]");
515 TH1D* projUnfX = fUnfSpectrum->ProjectionX();
516 TH1D* projRecX = fCurrentRec->ProjectionX();
517 projGenX->SetStats(0);
518 projRecX->SetStats(0);
519 projUnfX->SetStats(0);
520 projGenX->SetLineColor(8);
521 projRecX->SetLineColor(2);
522 projGenX->DrawCopy();
523 projUnfX->DrawCopy("same");
524 projRecX->DrawCopy("same");
526 TLegend* legend = new TLegend(0.6, 0.85, 0.98, 0.98);
527 legend->AddEntry(projGenX, "Generated Spectrum");
528 legend->AddEntry(projUnfX, "Unfolded Spectrum");
529 legend->AddEntry(projRecX, "Reconstructed Spectrum");
530 //legend->SetFillColor(0);
531 legend->Draw("same");
535 gStyle->SetPalette(style);
536 TH1D* projGenY = genHist->ProjectionY();
537 projGenY->SetTitle("Projection: Leading Particle Fragmentation; z^{lp}");
538 TH1D* projUnfY = fUnfSpectrum->ProjectionY();
539 TH1D* projRecY = fCurrentRec->ProjectionY();
540 projGenY->SetStats(0);
541 projRecY->SetStats(0);
542 projUnfY->SetStats(0);
543 projGenY->SetLineColor(8);
544 projRecY->SetLineColor(2);
545 projGenY->DrawCopy();
546 projUnfY->DrawCopy("same");
547 projRecY->DrawCopy("same");
549 TLegend* legend1 = new TLegend(0.6, 0.85, 0.98, 0.98);
550 legend1->AddEntry(projGenY, "Generated Spectrum");
551 legend1->AddEntry(projUnfY, "Unfolded Spectrum");
552 legend1->AddEntry(projRecY, "Recontructed Spectrum");
553 //legend1->SetFillColor(0);
554 legend1->Draw("same");
558 gStyle->SetPalette(style);
560 residual->SetStats(0);
561 residual->DrawCopy("colz");
563 canvas1->SaveAs(Form("%s.png", canvas1->GetName()));
567 //____________________________________________________________________
568 void AliJetSpectrumUnfolding::GetComparisonResults(Float_t* gen, Int_t* genLimit, Float_t* residuals, Float_t* ratioAverage) const
570 // Returns the chi2 between the Generated and the unfolded Reconstructed spectrum as well as between the Reconstructed and the folded unfolded
571 // These values are computed during DrawComparison, thus this function picks up the
577 *genLimit = fLastChi2MCLimit;
579 *residuals = fLastChi2Residuals;
581 *ratioAverage = fRatioAverage;
584 //____________________________________________________________________
585 void AliJetSpectrumUnfolding::ApplyBayesianMethod(Float_t regPar, Int_t nIterations, TH2* initialConditions, Bool_t determineError)
588 // correct spectrum using bayesian unfolding
591 // initialize seed with current time
594 printf("seting up current arrays and histograms...\n");
595 SetupCurrentHists(kFALSE); // kFALSE to not create big bin
597 // normalize Correlation Map to convert number of events into probabilities
598 /*for (Int_t te=1; te<=NBINSE; te++)
599 for (Int_t tz=1; tz<=NBINSZ; tz++)
603 for (Int_t me = 1; me<=NBINSE; me++)
604 for (Int_t mz = 1; mz<=NBINSZ; mz++)
606 bin[0] = te; bin[1] = me;
607 bin[2] = tz; bin[3] = mz;
608 sum += fCurrentCorrelation->GetBinContent(bin);
611 for (Int_t me = 1; me<=NBINSE; me++)
612 for (Int_t mz = 1; mz<=NBINSZ; mz++)
614 bin[0] = te; bin[1] = me;
615 bin[2] = tz; bin[3] = mz;
616 fCurrentCorrelation->SetBinContent(bin, fCurrentCorrelation->GetBinContent(bin)/sum);
617 fCurrentCorrelation->SetBinError(bin, fCurrentCorrelation->GetBinError(bin)/sum);
620 Float_t sum[NBINSE+2][NBINSZ+2];
621 memset(sum,0,sizeof(Float_t)*(NBINSE+2)*(NBINSZ+2));
623 for (Int_t idx=0; idx<=fCurrentCorrelation->GetNbins(); idx++)
626 Float_t binContent = fCurrentCorrelation->GetBinContent(idx, bin);
627 if ( (bin[1]>0 && bin[1]<=NBINSE) && (bin[3]>0 && bin[3]<=NBINSZ) )
628 sum[bin[0]][bin[2]] += binContent;
631 for (Int_t idx=0; idx<=fCurrentCorrelation->GetNbins(); idx++)
634 Float_t binContent = fCurrentCorrelation->GetBinContent(idx, bin);
635 Float_t binError = fCurrentCorrelation->GetBinError(bin);
636 if (sum[bin[0]][bin[2]]>0 && (bin[1]>0 && bin[1]<=NBINSE) &&
637 (bin[3]>0 && bin[3]<=NBINSZ) && (bin[0]>0 && bin[0]<=NBINSE) && (bin[2]>0 && bin[2]<=NBINSZ) )
639 fCurrentCorrelation->SetBinContent(bin, binContent/sum[bin[0]][bin[2]]);
640 fCurrentCorrelation->SetBinError(bin, binError/sum[bin[0]][bin[2]]);
644 printf("calling UnfoldWithBayesian\n");
645 Int_t success = UnfoldWithBayesian(fCurrentCorrelation, fCurrentRec, initialConditions, fUnfSpectrum, regPar, nIterations, kFALSE);
652 Printf("AliJetSpectrumUnfolding::ApplyBayesianMethod: WARNING: No errors calculated.");
656 // evaluate errors, this is done by randomizing the measured spectrum following Poission statistics
657 // this (new) measured spectrum is then unfolded and the different to the result from the "real" measured
658 // spectrum calculated. This is performed N times and the maximum difference is taken as the systematic
659 // error of the unfolding method itself.
661 TH2* maxError = (TH2*) fUnfSpectrum->Clone("maxError");
664 TH2* normalizedResult = (TH2*) fUnfSpectrum->Clone("normalizedResult");
665 normalizedResult->Scale(1.0 / normalizedResult->Integral());
667 const Int_t kErrorIterations = 20;
669 printf("Spectrum unfolded. Determining error (%d iterations)...\n", kErrorIterations);
671 TH2* randomized = (TH2*) fCurrentRec->Clone("randomized");
672 TH2* result2 = (TH2*) fUnfSpectrum->Clone("result2");
673 for (Int_t n=0; n<kErrorIterations; ++n)
675 // randomize the content of clone following a poisson with the mean = the value of that bin
676 for (Int_t x=1; x<=randomized->GetNbinsX(); x++)
677 for (Int_t y=1; y<=randomized->GetNbinsY(); y++)
679 Float_t randomValue = fCurrentRec->GetBinContent(x,y);
680 TF1* poisson = new TF1("poisson", "TMath::Poisson(x,[0])",randomValue*0.25, randomValue*1.25);
681 poisson->SetParameters(randomValue,0.);
682 randomValue = poisson->GetRandom();
683 //printf("%e --> %e\n", fCurrentRec->GetBinContent(x,y), (Double_t)randomValue);
684 randomized->SetBinContent(x, y, randomValue);
689 if (UnfoldWithBayesian(fCurrentCorrelation, randomized, initialConditions, result2, regPar, nIterations) != 0)
692 result2->Scale(1.0 / result2->Integral());
695 result2->Divide(normalizedResult);
697 //new TCanvas; result2->DrawCopy("HIST");
699 // find max. deviation
700 for (Int_t i=1; i<=result2->GetNbinsX(); i++)
701 for (Int_t j=1; j<=result2->GetNbinsY(); j++)
702 maxError->SetBinContent(i, j, TMath::Max(maxError->GetBinContent(i,j), TMath::Abs(1 - result2->GetBinContent(i,j))));
707 for (Int_t i=1; i<=fUnfSpectrum->GetNbinsX(); i++)
708 for (Int_t j=1; j<=fUnfSpectrum->GetNbinsY(); j++)
709 fUnfSpectrum->SetBinError(i, j, fUnfSpectrum->GetBinError(i,j) + maxError->GetBinContent(i,j)*fUnfSpectrum->GetBinContent(i,j));
712 delete normalizedResult;
715 //____________________________________________________________________
716 Int_t AliJetSpectrumUnfolding::UnfoldWithBayesian(THnSparseF* correlation, TH2* measured, TH2* initialConditions, TH2* aResult, Float_t regPar, Int_t nIterations, Bool_t calculateErrors)
719 // unfolds a spectrum
722 if (measured->Integral() <= 0)
724 Printf("AliJetSpectrumUnfolding::UnfoldWithBayesian: ERROR: The measured spectrum is empty");
727 const Int_t NFilledBins = correlation->GetNbins();
728 const Int_t kStartBin = 1;
730 const Int_t kMaxTZ = NBINSZ; // max true axis fragmentation function
731 const Int_t kMaxMZ = NBINSZ; // max measured axis fragmentation function
732 const Int_t kMaxTE = NBINSE; // max true axis energy
733 const Int_t kMaxME = NBINSE; // max measured axis energy
735 printf("NbinsE=%d - NbinsZ=%d\n", NBINSE, NBINSZ);
737 // store information in arrays, to increase processing speed
738 Double_t measuredCopy[kMaxME+1][kMaxMZ+1];
739 Double_t prior[kMaxTE+1][kMaxTZ+1];
740 Double_t errors[kMaxTE+1][kMaxTZ+1];
741 Double_t result[kMaxTE+1][kMaxTZ+1];
743 THnSparseF *inverseCorrelation;
744 inverseCorrelation = (THnSparseF*)correlation->Clone("inverseCorrelation");
745 inverseCorrelation->Reset();
747 Float_t inputDistIntegral = 1;
748 if (initialConditions)
750 printf("Using different starting conditions...\n");
751 inputDistIntegral = initialConditions->Integral();
753 Float_t measuredIntegral = measured->Integral();
754 for (Int_t me=1; me<=kMaxME; me++)
755 for (Int_t mz=1; mz<=kMaxMZ; mz++)
757 // normalization of the measured spectrum
758 measuredCopy[me][mz] = measured->GetBinContent(me,mz) / measuredIntegral;
759 errors[me][mz] = measured->GetBinError(me, mz) / measuredIntegral;
760 // pick prior distribution and normalize it
761 if (initialConditions)
762 prior[me][mz] = initialConditions->GetBinContent(me,mz) / inputDistIntegral;
764 prior[me][mz] = measured->GetBinContent(me,mz) / measuredIntegral;
768 for (Int_t i=0; i<nIterations; i++)
770 // calculate Inverse Correlation Map from Bayes theorem:
771 // IR_ji = R_ij * prior_i / sum_k(R_kj * prior_k)
773 for (Int_t me=1; me<=kMaxME; me++)
774 for (Int_t mz=1; mz<=kMaxMZ; mz++)
777 for (Int_t te=kStartBin; te<=kMaxTE; te++)
778 for (Int_t tz=kStartBin; tz<=kMaxTZ; tz++)
780 Int_t bin[4] = {te, me, tz, mz};
781 norm += correlation->GetBinContent(bin)*prior[te][tz];
784 for (Int_t te = kStartBin; te <= kMaxTE; te++)
785 for (Int_t tz = kStartBin; tz <= kMaxTZ; tz++)
787 Int_t bin[4] = {te, me, tz, mz};
788 inverseCorrelation->SetBinContent(bin, correlation->GetBinContent(bin)*prior[te][tz]/norm );
791 // inverse response set to '0' wich has been already done in line 2069
793 inverseCorrelation->Reset();
794 Float_t norm[kMaxTE+2][kMaxTZ+2];
795 for (Int_t te=0; te<(kMaxTE+2); te++)
796 for (Int_t tz=0; tz<(kMaxTZ+2); tz++)
798 for (Int_t idx=0; idx<=correlation->GetNbins(); idx++)
801 Float_t binContent = correlation->GetBinContent(idx, bin);
802 if (bin[1]>0 && bin[1]<=NBINSE && bin[3]>0 && bin[3]<=NBINSZ &&
803 bin[0]>0 && bin[0]<=NBINSE && bin[2]>0 && bin[2]<=NBINSZ)
804 norm[bin[1]][bin[3]] += binContent*prior[bin[0]][bin[2]];
806 Float_t chi2Measured=0, diff;
807 for (Int_t idx=0; idx<=correlation->GetNbins(); idx++)
810 Float_t binContent = correlation->GetBinContent(idx, bin);
811 if (norm[bin[1]][bin[3]]>0 && bin[1]>0 && bin[1]<=NBINSE &&
812 bin[3]>0 && bin[3]<=NBINSZ && bin[0]>0 && bin[2]>0 && bin[0]<=NBINSE && bin[2]<=NBINSZ)
814 inverseCorrelation->SetBinContent(bin, binContent*prior[bin[0]][bin[2]]/norm[bin[1]][bin[3]]);
815 if (errors[bin[1]][bin[3]]>0)
817 diff = ((measuredCopy[bin[1]][bin[3]]-norm[bin[1]][bin[3]])/(errors[bin[1]][bin[3]]));
818 chi2Measured += diff*diff;
823 // calculate "generated" spectrum
824 for (Int_t te = kStartBin; te<=kMaxTE; te++)
825 for (Int_t tz = kStartBin; tz<=kMaxTZ; tz++)
828 for (Int_t me=1; me<=kMaxME; me++)
829 for (Int_t mz=1; mz<=kMaxMZ; mz++)
831 Int_t bin[4] = {te, me, tz, mz};
832 value += inverseCorrelation->GetBinContent(bin)*measuredCopy[me][mz];
834 result[te][tz] = value;
835 //printf("%e\n", result[te][tz]);
838 // regularization (simple smoothing)
839 Float_t chi2LastIter = 0;
840 for (Int_t te=kStartBin; te<=kMaxTE; te++)
841 for (Int_t tz=kStartBin; tz<=kMaxTZ; tz++)
843 Float_t newValue = 0;
844 // 0 bin excluded from smoothing
845 if (( te >(kStartBin+1) && te<(kMaxTE-1) ) && ( tz > (kStartBin+1) && tz<(kMaxTZ-1) ))
847 Float_t average = ((result[te-1][tz-1] + result[te-1][tz] + result[te-1][tz+1])+(result[te][tz-1] + result[te][tz] + result[te][tz+1])+(result[te+1][tz-1] + result[te+1][tz] + result[te+1][tz+1]))/9.;
849 // weight the average with the regularization parameter
850 newValue = (1 - regPar) * result[te][tz] + regPar * average;
853 newValue = result[te][tz];
854 if (prior[te][tz]>1.e-5)
856 diff = ((prior[te][tz]-newValue)/prior[te][tz]);
857 chi2LastIter = diff*diff;
859 prior[te][tz] = newValue;
861 //printf(" iteration %d - chi2LastIter = %e - chi2Measured = %e \n", i, chi2LastIter/((Float_t)kMaxTE*(Float_t)kMaxTZ), chi2Measured/((Float_t)kMaxTE*(Float_t)kMaxTZ));
862 if (chi2LastIter/((Float_t)kMaxTE*(Float_t)kMaxTZ)<5.e-6 && chi2Measured/((Float_t)kMaxTE*(Float_t)kMaxTZ)<5.e-3)
864 } // end of iterations
866 // propagate errors of the reconstructed distribution through the unfolding
867 for (Int_t te = kStartBin; te<=kMaxTE; te++)
868 for (Int_t tz = kStartBin; tz<=kMaxTZ; tz++)
870 Float_t valueError = 0;
871 Float_t binError = 0;
872 for (Int_t me=1; me<=kMaxME; me++)
873 for (Int_t mz=1; mz<=kMaxMZ; mz++)
875 Int_t bin[4] = {te, me, tz, mz};
876 valueError += inverseCorrelation->GetBinContent(bin)*inverseCorrelation->GetBinContent(bin)*errors[me][mz]*errors[me][mz];
878 //if (errors[te][tz]!=0)printf("errors[%d][%d]=%e\n", te, tz, valueError);
879 aResult->SetBinContent(te, tz, prior[te][tz]);
880 aResult->SetBinError(te, tz, TMath::Sqrt(valueError));
883 // ***********************************************************************************************************
884 // Calculate the covariance matrix, all arguments are taken from G. D'Agostini (p.6-8)
887 printf("Covariance matrix will be calculated... this will take a lot of time (>1 day) ;)\n");
889 //Variables and Matrices that will be use along the calculation
890 const Int_t binsV[4] = {NBINSE,NBINSE, NBINSZ, NBINSZ};
891 const Double_t LowEdgeV[4] = {axisLowerLimitE, axisLowerLimitE, axisLowerLimitZ, axisLowerLimitZ};
892 const Double_t UpEdgeV[4] = {axisUpperLimitE, axisUpperLimitE, axisUpperLimitZ, axisUpperLimitZ};
894 const Double_t Ntrue = (Double_t)measured->Integral();
896 THnSparseF *V = new THnSparseF("V","",4, binsV, LowEdgeV, UpEdgeV);
898 Double_t invCorrContent1, Nt;
899 Double_t invCorrContent2, v11, v12, v2;
900 // calculate V1 and V2
901 for (Int_t idx1=0; idx1<=NFilledBins; idx1++)
903 printf("Covariance Matrix calculation: iteration idx1=%d of %d\n", idx1, NFilledBins);
904 for (Int_t idx2=0; idx2<=NFilledBins; idx2++)
908 invCorrContent1 = inverseCorrelation->GetBinContent(idx1, bin1);
909 invCorrContent2 = inverseCorrelation->GetBinContent(idx2, bin2);
911 if(bin1[0]>0 && bin1[0]<=NBINSE && bin1[1]>0 && bin1[1]<=NBINSE &&
912 bin1[2]>0 && bin1[2]<=NBINSZ && bin1[3]>0 && bin1[3]<=NBINSZ &&
913 bin2[0]>0 && bin2[0]<=NBINSE && bin2[1]>0 && bin2[1]<=NBINSE &&
914 bin2[2]>0 && bin2[2]<=NBINSZ && bin2[3]>0 && bin2[3]<=NBINSZ)
916 if (bin1[1]==bin2[1] && bin1[3]==bin2[3])
917 v11 = invCorrContent1*invCorrContent2*measuredCopy[bin1[1]][bin1[3]]
918 *(1. - measuredCopy[bin2[1]][bin2[3]]/Ntrue);
920 v12 = invCorrContent1*invCorrContent2*measuredCopy[bin1[1]][bin1[3]]*
921 measuredCopy[bin2[1]][bin2[3]]/Ntrue;
922 Nt = (Double_t)prior[bin2[0]][bin2[2]];
923 v2 = measuredCopy[bin1[1]][bin1[3]]*measuredCopy[bin2[1]][bin2[3]]*
924 invCorrContent1*invCorrContent2*
925 BayesUncertaintyTerms(inverseCorrelation, correlation, bin1, bin2, Nt);
926 Int_t binV[4] = {bin1[0],bin2[0],bin1[2],bin2[2]};
927 V->SetBinContent(binV,v11-v12 + v2);
932 for(Int_t te = 1; te<=NBINSE; te++)
933 for(Int_t tz = 1; tz<=NBINSZ; tz++)
935 Int_t binV[4] = {te,te,tz,tz};
936 aResult->SetBinError( te, tz, V->GetBinContent(binV) );
939 TFile* f = new TFile("Covariance_UnfSpectrum.root");
949 //____________________________________________________________________
950 Double_t AliJetSpectrumUnfolding::BayesUncertaintyTerms(THnSparseF *M, THnSparseF *C, Int_t* binTM, Int_t* binTM1, Double_t Nt)
953 // helper function for the covariance matrix of the bayesian method
958 Int_t tmpBin[4], tmpBin1[4];
959 const Int_t nFilledBins = C->GetNbins();
964 Float_t InvCorrContent;
966 tmpBin[0] =binTM[0]; tmpBin[1] =binTM[1]; tmpBin[2] =binTM[2]; tmpBin[3] =binTM[3];
967 tmpBin1[0]=binTM[0]; tmpBin1[1]=binTM1[1]; tmpBin1[2]=binTM[2]; tmpBin1[3]=binTM1[3];
968 if (C->GetBinContent(tmpBin)!=0 && C->GetBinContent(tmpBin1)!=0)
970 if (binTM[0]==binTM1[0] && binTM[2]==binTM1[2])
971 term[0] = BayesCov(M, C, tmpBin, tmpBin1)/
972 (C->GetBinContent(tmpBin)*C->GetBinContent(tmpBin1));
973 term[2] = term[0]*M->GetBinContent(tmpBin1);
981 tmpBin[0]=binTM1[0]; tmpBin[1]=binTM[1]; tmpBin[2]=binTM1[2]; tmpBin[3]=binTM[3];
982 tmpBin1[0]=binTM1[0]; tmpBin1[1]=binTM1[1]; tmpBin1[2]=binTM1[2]; tmpBin1[3]=binTM1[3];
983 if (C->GetBinContent(tmpBin)!=0 && C->GetBinContent(tmpBin1)!=0)
984 term[6] = BayesCov(M, C, tmpBin, tmpBin1)*
985 M->GetBinContent(tmpBin)/
986 (C->GetBinContent(tmpBin)*C->GetBinContent(tmpBin1));
990 for(Int_t idx1=0; idx1<=nFilledBins; idx1++)
993 CorrContent = C->GetBinContent(idx1, bin1);
994 InvCorrContent = M->GetBinContent(idx1, bin1);
995 if(bin1[0]>0 && bin1[0]<=NBINSE && bin1[1]>0 && bin1[1]<=NBINSE &&
996 bin1[2]>0 && bin1[2]<=NBINSZ && bin1[3]>0 && bin1[3]<=NBINSZ)
998 tmpBin[0] =binTM[0]; tmpBin[1] =binTM[1]; tmpBin[2] =binTM[2]; tmpBin[3] =binTM[3];
999 tmpBin1[0]=binTM[0]; tmpBin1[1]=bin1[1]; tmpBin1[2]=binTM[2]; tmpBin1[3]=bin1[3];
1000 if (C->GetBinContent(tmpBin)!=0 &&
1001 binTM[0]==binTM1[0] && binTM[2]==binTM1[2])
1002 term[1] = BayesCov(M, C, tmpBin, tmpBin1)/C->GetBinContent(tmpBin);
1006 tmpBin[0] =binTM[0]; tmpBin[1] =bin1[1]; tmpBin[2] =binTM[2]; tmpBin[3] =bin1[3];
1007 tmpBin1[0]=binTM[0]; tmpBin1[1]=binTM1[1]; tmpBin1[2]=binTM[2]; tmpBin1[3]=binTM1[3];
1008 if (C->GetBinContent(tmpBin1)!=0)
1010 if (binTM[0]==binTM1[0] && binTM[2]==binTM1[2])
1011 term[3] = BayesCov(M, C, tmpBin, tmpBin1)/
1012 C->GetBinContent(tmpBin1);
1013 term[5] = BayesCov(M, C, tmpBin, tmpBin1)*M->GetBinContent(tmpBin1)/
1014 C->GetBinContent(tmpBin1);
1022 tmpBin[0] =binTM1[0]; tmpBin[1] =binTM[1]; tmpBin[2] =binTM1[2]; tmpBin[3] =binTM[3];
1023 tmpBin1[0]=binTM1[0]; tmpBin1[1]=bin1[1]; tmpBin1[2]=binTM1[2]; tmpBin1[3]=bin1[3];
1024 if (C->GetBinContent(tmpBin)!=0)
1025 term[7] = BayesCov(M, C, tmpBin, tmpBin1)*M->GetBinContent(tmpBin)/
1026 C->GetBinContent(tmpBin);
1030 tmpBin[0] =bin1[0]; tmpBin[1] =binTM[1]; tmpBin[2] =bin1[2]; tmpBin[3] =binTM[3];
1031 tmpBin1[0]=bin1[0]; tmpBin1[1]=binTM1[1]; tmpBin1[2]=bin1[2]; tmpBin1[3]=binTM1[3];
1032 if (C->GetBinContent(tmpBin)!=0 && C->GetBinContent(tmpBin1)!=0)
1033 term[8] = BayesCov(M, C, tmpBin, tmpBin1)*
1034 M->GetBinContent(tmpBin)*M->GetBinContent(tmpBin)/
1035 (C->GetBinContent(tmpBin)*C->GetBinContent(tmpBin1));
1039 for (Int_t i=0; i<9; i++)
1040 result += term[i]/Nt;
1047 //____________________________________________________________________
1048 Double_t AliJetSpectrumUnfolding::BayesCov(THnSparseF *M, THnSparseF *correlation, Int_t* binTM, Int_t* bin1)
1050 Double_t result, result1, result2, result3;
1052 if (binTM[0]==bin1[0] && binTM[2]==bin1[2])
1054 if (correlation->GetBinContent(bin1)!=0)
1055 result1 = 1./correlation->GetBinContent(bin1);
1066 if (binTM[1]==bin1[1] && binTM[3]==bin1[3])
1068 Int_t tmpbin[4] = {bin1[0], binTM[1], bin1[2], binTM[3]};
1069 if(correlation->GetBinContent(tmpbin)!=0)
1070 result3 = M->GetBinContent(tmpbin)/correlation->GetBinContent(tmpbin);
1080 return result = result1 + result2 + result3;
1083 //____________________________________________________________________
1084 TH2F* AliJetSpectrumUnfolding::CalculateRecSpectrum(TH2* inputGen)
1086 // runs the distribution given in inputGen through the correlation histogram identified by
1087 // fCorrelation and produces a reconstructed spectrum
1092 // normalize to convert number of events into probability
1093 /*for (Int_t te=1; te<=NBINSE; te++)
1094 for (Int_t tz=1; tz<=NBINSZ; tz++)
1098 for (Int_t me = 1; me<=NBINSE; me++)
1099 for (Int_t mz = 1; mz<=NBINSZ; mz++)
1101 bin[0] = te; bin[1] = me;
1102 bin[2] = tz; bin[3] = mz;
1103 sum += fCorrelation[correlationMap]->GetBinContent(bin);
1106 for (Int_t me = 1; me<=NBINSE; me++)
1107 for (Int_t mz = 1; mz<=NBINSZ; mz++)
1109 bin[0] = te; bin[1] = me;
1110 bin[2] = tz; bin[3] = mz;
1111 fCorrelation[correlationMap]->SetBinContent(bin, fCorrelation[correlationMap]->GetBinContent(bin)/sum);
1112 fCorrelation[correlationMap]->SetBinError(bin, fCorrelation[correlationMap]->GetBinError(bin)/sum);
1115 // normalize to convert number of events into probability (the following loop is much faster)
1116 Float_t sum[NBINSE+2][NBINSZ+2];
1117 memset(sum,0,sizeof(Float_t)*(NBINSE+2)*(NBINSZ+2));
1119 for (Int_t idx=0; idx<fCorrelation->GetNbins(); idx++)
1122 Float_t binContent = fCorrelation->GetBinContent(idx, bin);
1123 if (bin[1]>0 && bin[1]<=NBINSE && bin[3]>0 && bin[3]<=NBINSZ){
1124 sum[bin[0]][bin[2]] += binContent;
1128 for (Int_t idx=0; idx<fCorrelation->GetNbins(); idx++)
1131 Float_t binContent = fCorrelation->GetBinContent(idx, bin);
1132 Float_t binError = fCorrelation->GetBinError(bin);
1133 if (sum[bin[0]][bin[2]]>0 && bin[1]>0 && bin[1]<=NBINSE &&
1134 bin[3]>0 && bin[3]<=NBINSZ && bin[0]>0 && bin[2]>0 && bin[0]<=NBINSE && bin[2]<=NBINSZ)
1136 fCorrelation->SetBinContent(bin, binContent/sum[bin[0]][bin[2]]);
1137 fCorrelation->SetBinError(bin, binError/sum[bin[0]][bin[2]]);
1141 TH2F* target = dynamic_cast<TH2F*> (fRecSpectrum->Clone(Form("reconstructed_%s", inputGen->GetName())));
1144 for (Int_t me=1; me<=NBINSE; ++me)
1145 for (Int_t mz=1; mz<=NBINSZ; ++mz)
1147 Float_t measured = 0;
1150 for (Int_t te=1; te<=NBINSE; ++te)
1151 for (Int_t tz=1; tz<=NBINSZ; ++tz)
1153 Int_t bin[4] = {te, me, tz, mz};
1154 measured += inputGen->GetBinContent(te,tz) * fCorrelation->GetBinContent(bin);
1155 error += inputGen->GetBinError(te,tz) * fCorrelation->GetBinContent(bin);
1157 target->SetBinContent(me, mz, measured);
1158 target->SetBinError(me, mz, error);
1164 //__________________________________________________________________________________________________
1165 void AliJetSpectrumUnfolding::SetGenRecFromFunc(TF2* inputGen)
1167 // uses the given function to fill the input Generated histogram and generates from that
1168 // the reconstructed histogram by applying the response histogram
1169 // this can be used to evaluate if the methods work indepedently of the input
1175 TH2F* histtmp = new TH2F("histtmp", "tmp", EBINNING, ZBINNING);
1176 TH2F* gen = fGenSpectrum;
1181 histtmp->FillRandom(inputGen->GetName(), NEVENTS);
1183 for (Int_t i=1; i<=gen->GetNbinsX(); ++i)
1184 for (Int_t j=1; j<=gen->GetNbinsY(); ++j)
1186 gen->SetBinContent(i, j, histtmp->GetBinContent(i,j));
1187 gen->SetBinError(i, j, histtmp->GetBinError(i,j));
1193 //gStyle->SetPalette(1);
1195 //gen->Draw("COLZ");
1198 TH2 *recsave = fRecSpectrum;
1200 fRecSpectrum = CalculateRecSpectrum(gen);
1201 fRecSpectrum->SetName(recsave->GetName());
1206 //________________________________________________________________________________________