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>
43 #include <THnSparse.h>
45 ClassImp(AliJetSpectrumUnfolding)
47 const Int_t AliJetSpectrumUnfolding::fgkNBINSE = 50;
48 const Int_t AliJetSpectrumUnfolding::fgkNBINSZ = 50;
49 const Int_t AliJetSpectrumUnfolding::fgkNEVENTS = 500000;
50 const Double_t AliJetSpectrumUnfolding::fgkaxisLowerLimitE = 0.;
51 const Double_t AliJetSpectrumUnfolding::fgkaxisLowerLimitZ = 0.;
52 const Double_t AliJetSpectrumUnfolding::fgkaxisUpperLimitE = 250.;
53 const Double_t AliJetSpectrumUnfolding::fgkaxisUpperLimitZ = 1.;
55 Float_t AliJetSpectrumUnfolding::fgBayesianSmoothing = 1; // smoothing parameter (0 = no smoothing)
56 Int_t AliJetSpectrumUnfolding::fgBayesianIterations = 100; // number of iterations in Bayesian method
58 //____________________________________________________________________
60 AliJetSpectrumUnfolding::AliJetSpectrumUnfolding() :
61 TNamed(), fCurrentRec(0), fCurrentCorrelation(0), fRecSpectrum(0), fGenSpectrum(0),
62 fUnfSpectrum(0), fCorrelation(0), fLastChi2MC(0), fLastChi2MCLimit(0), fLastChi2Residuals(0), fRatioAverage(0)
65 // default constructor
74 //____________________________________________________________________
75 AliJetSpectrumUnfolding::AliJetSpectrumUnfolding(const Char_t* name, const Char_t* title) :
76 TNamed(name, title), fCurrentRec(0), fCurrentCorrelation(0), fRecSpectrum(0),
77 fGenSpectrum(0), fUnfSpectrum(0), fCorrelation(0), fLastChi2MC(0), fLastChi2MCLimit(0), fLastChi2Residuals(0), fRatioAverage(0)
83 // do not add this hists to the directory
84 Bool_t oldStatus = TH1::AddDirectoryStatus();
85 TH1::AddDirectory(kFALSE);
86 fRecSpectrum = new TH2F("fRecSpectrum", "Reconstructed Spectrum;E^{jet}_{rec} [GeV];z^{lp}_{rec}",
87 fgkNBINSE, fgkaxisLowerLimitE, fgkaxisUpperLimitE,
88 fgkNBINSZ, fgkaxisLowerLimitZ, fgkaxisUpperLimitZ);
89 fGenSpectrum = new TH2F("fGenSpectrum", "Generated Spectrum;E^{jet}_{gen} [GeV];z^{lp}_{gen}",
90 fgkNBINSE, fgkaxisLowerLimitE, fgkaxisUpperLimitE,
91 fgkNBINSZ, fgkaxisLowerLimitZ, fgkaxisUpperLimitZ);
92 fUnfSpectrum = new TH2F("fUnfSpectrum", "Unfolded Spectrum;E^{jet} [GeV];z^{lp}",
93 fgkNBINSE, fgkaxisLowerLimitE, fgkaxisUpperLimitE,
94 fgkNBINSZ, fgkaxisLowerLimitZ, fgkaxisUpperLimitZ);
96 const Int_t nbin[4]={fgkNBINSE, fgkNBINSE, fgkNBINSZ, fgkNBINSZ};
97 //arrays for bin limits
98 Double_t lowEdge[4] = {fgkaxisLowerLimitE, fgkaxisLowerLimitE, fgkaxisLowerLimitZ, fgkaxisLowerLimitZ};
99 Double_t upEdge[4] = {fgkaxisUpperLimitE, fgkaxisUpperLimitE, fgkaxisUpperLimitZ, fgkaxisUpperLimitZ};
101 fCorrelation = new THnSparseF("fCorrelation", "Correlation Function", 4, nbin, lowEdge, upEdge);
103 TH1::AddDirectory(oldStatus);
106 //____________________________________________________________________
107 AliJetSpectrumUnfolding::~AliJetSpectrumUnfolding()
131 //____________________________________________________________________
132 Long64_t AliJetSpectrumUnfolding::Merge(TCollection* list)
134 // Merge a list of AliJetSpectrumUnfolding objects with this (needed for
136 // Returns the number of merged objects (including this).
144 TIterator* iter = list->MakeIterator();
147 // collections of all histograms
148 TList collections[4];
151 while ((obj = iter->Next())) {
153 AliJetSpectrumUnfolding* entry = dynamic_cast<AliJetSpectrumUnfolding*> (obj);
157 collections[0].Add(entry->fGenSpectrum);
158 collections[1].Add(entry->fRecSpectrum);
159 collections[2].Add(entry->fUnfSpectrum);
160 collections[3].Add(entry->fCorrelation);
165 fGenSpectrum->Merge(&collections[0]);
166 fRecSpectrum->Merge(&collections[1]);
167 fUnfSpectrum->Merge(&collections[2]);
168 fCorrelation->Merge(&collections[3]);
175 //____________________________________________________________________
176 Bool_t AliJetSpectrumUnfolding::LoadHistograms(const Char_t* dir)
179 // loads the histograms from a file
180 // if dir is empty a directory with the name of this object is taken (like in SaveHistogram)
186 if (!gDirectory->cd(dir))
189 Bool_t success = kTRUE;
191 // store old histograms to delete them later
193 oldHistograms.SetOwner(1);
195 if (fGenSpectrum) oldHistograms.Add(fGenSpectrum);
196 if (fRecSpectrum) oldHistograms.Add(fRecSpectrum);
197 if (fUnfSpectrum) oldHistograms.Add(fUnfSpectrum);
198 if (fCorrelation) oldHistograms.Add(fCorrelation);
200 // load new histograms
201 fGenSpectrum = dynamic_cast<TH2F*> (gDirectory->Get(fGenSpectrum->GetName()));
205 fRecSpectrum = dynamic_cast<TH2F*> (gDirectory->Get(fRecSpectrum->GetName()));
209 fUnfSpectrum = dynamic_cast<TH2F*> (gDirectory->Get(fUnfSpectrum->GetName()));
213 fCorrelation = dynamic_cast<THnSparseF*> (gDirectory->Get(fCorrelation->GetName()));
217 gDirectory->cd("..");
219 // delete old histograms
220 oldHistograms.Delete();
225 //____________________________________________________________________
226 void AliJetSpectrumUnfolding::SaveHistograms()
229 // saves the histograms
232 gDirectory->mkdir(GetName());
233 gDirectory->cd(GetName());
236 fGenSpectrum->Write();
239 fRecSpectrum->Write();
242 fUnfSpectrum->Write();
245 fCorrelation->Write();
247 gDirectory->cd("..");
250 //____________________________________________________________________
251 void AliJetSpectrumUnfolding::SetupCurrentHists(Bool_t createBigBin)
254 // resets fUnfSpectrum
257 fUnfSpectrum->Reset();
258 fUnfSpectrum->Sumw2();
260 fCurrentRec = (TH2F*)fRecSpectrum->Clone("fCurrentRec");
261 fCurrentRec->Sumw2();
263 fCurrentCorrelation = (THnSparseF*)fCorrelation->Clone("fCurrentCorrelation");
264 fCurrentCorrelation->Sumw2();
266 Printf("Correlation Matrix has %.0E filled bins", fCurrentCorrelation->GetNbins());
270 Int_t maxBinE = 0, maxBinZ = 0;
271 Float_t maxE = 0, maxZ = 0;
272 for (Int_t me=1; me<=fCurrentRec->GetNbinsX(); me++)
273 for (Int_t mz=1; mz<=fCurrentRec->GetNbinsY(); mz++)
275 if (fCurrentRec->GetBinContent(me,mz) <= 5 && me>fgkNBINSE/2 && mz>fgkNBINSZ/2)
279 maxE = fCurrentRec->GetXaxis()->GetBinCenter(me);
280 maxZ = fCurrentRec->GetYaxis()->GetBinCenter(mz);
285 if (maxBinE > 0 || maxBinZ > 0)
287 printf("Bin limit in measured spectrum is e = %d and z = %d.\n", maxBinE, maxBinZ);
288 fCurrentRec->SetBinContent(maxBinE, maxBinZ, fCurrentRec->Integral(maxBinE, fCurrentRec->GetNbinsX(), maxBinZ, fCurrentRec->GetNbinsY()));
289 for (Int_t me=maxBinE+1; me<=fCurrentRec->GetNbinsX(); me++)
290 for (Int_t mz=maxBinZ+1; mz<=fCurrentRec->GetNbinsY(); mz++)
292 fCurrentRec->SetBinContent(me, mz, 0);
293 fCurrentRec->SetBinError(me, mz, 0);
295 // the error is set to sqrt(N), better solution possible?, sum of relative errors of all contributions???
296 fCurrentRec->SetBinError(maxBinE, maxBinZ, TMath::Sqrt(fCurrentRec->GetBinContent(maxBinE, maxBinZ)));
298 printf("This bin has now %f +- %f entries\n", fCurrentRec->GetBinContent(maxBinE, maxBinZ), fCurrentRec->GetBinError(maxBinE, maxBinZ));
300 /* for (Int_t te=1; te<=NBINSE; te++)
302 for (Int_t tz=1; tz<=NBINSZ; tz++)
304 Int_t binMin[4] = {te, maxBinE, tz, maxBinZ};
305 Int_t binMax[4] = {NBINSE, NBINSE, NBINSZ, NBINSZ};
307 for (Int_t ite=te; ite<=NBINSE; ite++)
308 for (Int_t itz=tz; itz<=NBINSZ; itz++)
309 for (Int_t ime=maxBinE; ime<=NBINSE; ime++)
310 for (Int_t imz=maxBinZ; imz<=NBINSZ; imz++)
312 Int_t bin[4] = {ite, ime, itz, imz};
313 sum += fCurrentCorrelation->GetBinContent(bin);
315 fCurrentCorrelation->SetBinContent(binMin, sum);
316 fCurrentCorrelation->SetBinError(binMin, TMath::Sqrt(fCurrentCorrelation->GetBinContent(binMin)));
317 printf("create big bin1, nbins = %d, te = %d, tz = %d\n", NBINSE, te, tz);
318 for (Int_t me=maxBinE; me<=NBINSE; me++)
320 for (Int_t mz=maxBinZ; mz<=NBINSZ; mz++)
322 Int_t bin[4] = {te, me, tz, mz};
323 fCurrentCorrelation->SetBinContent(bin, 0.);
324 fCurrentCorrelation->SetBinError(bin, 0.);
325 printf("create big bin2\n");
331 for(Int_t idx = 0; idx<=fCurrentCorrelation->GetNbins(); idx++)
334 Float_t binContent = fCurrentCorrelation->GetBinContent(idx,bin);
335 Float_t binError = fCurrentCorrelation->GetBinError(idx);
336 Int_t binMin[4] = {bin[0], maxBinE, bin[2], maxBinZ};
337 if ( (bin[1]>maxBinE && bin[1]<=fgkNBINSE) && (bin[3]>maxBinZ && bin[3]<=fgkNBINSZ) )
339 fCurrentCorrelation->SetBinContent(binMin, binContent + fCurrentCorrelation->GetBinContent(binMin));
340 fCurrentCorrelation->SetBinError(binMin, binError + TMath::Sqrt(fCurrentCorrelation->GetBinContent(binMin)));
341 fCurrentCorrelation->SetBinContent(bin, 0.);
342 fCurrentCorrelation->SetBinError(bin, 0.);
344 printf("create big bin1, nbins = %d, te = %d, tz = %d\n", fgkNBINSE, bin[0], bin[1]);
347 printf("Adjusted correlation matrix!\n");
349 } // end Create Big Bin
353 //____________________________________________________________________
354 void AliJetSpectrumUnfolding::SetBayesianParameters(Float_t smoothing, Int_t nIterations)
357 // sets the parameters for Bayesian unfolding
360 fgBayesianSmoothing = smoothing;
361 fgBayesianIterations = nIterations;
363 printf("AliJetSpectrumUnfolding::SetBayesianParameters --> Paramaters set to %d iterations with smoothing %f\n", fgBayesianIterations, fgBayesianSmoothing);
366 //____________________________________________________________________
367 void AliJetSpectrumUnfolding::NormalizeToBinWidth(TH2* const hist)
370 // normalizes a 2-d histogram to its bin width (x width * y width)
373 for (Int_t i=1; i<=hist->GetNbinsX(); i++)
374 for (Int_t j=1; j<=hist->GetNbinsY(); j++)
376 Double_t factor = hist->GetXaxis()->GetBinWidth(i) * hist->GetYaxis()->GetBinWidth(j);
377 hist->SetBinContent(i, j, hist->GetBinContent(i, j) / factor);
378 hist->SetBinError(i, j, hist->GetBinError(i, j) / factor);
382 //____________________________________________________________________
383 void AliJetSpectrumUnfolding::DrawHistograms()
386 // draws the histograms of this class
389 gStyle->SetPalette(1);
391 TCanvas* canvas1 = new TCanvas("fRecSpectrum", "fRecSpectrum", 900, 600);
393 fRecSpectrum->DrawCopy("COLZ");
395 TCanvas* canvas2 = new TCanvas("fGenSpectrum", "fGenSpectrum", 900, 600);
398 fGenSpectrum->DrawCopy("COLZ");
400 TCanvas* canvas3 = new TCanvas("fUnfSpectrum", "fUnfSpectrum", 900, 600);
403 fUnfSpectrum->DrawCopy("COLZ");
405 TCanvas* canvas4 = new TCanvas("fCorrelation", "fCorrelation", 500, 500);
410 TH2D* h0 = fCorrelation->Projection(1,0);
411 h0->SetXTitle("E^{jet}_{gen} [GeV]");
412 h0->SetYTitle("E^{jet}_{rec} [GeV]");
413 h0->SetTitle("Projection: Jet Energy");
414 h0->DrawCopy("colz");
418 TH2D* h1 = fCorrelation->Projection(3,2);
419 h1->SetXTitle("z^{lp}_{gen}");
420 h1->SetYTitle("z^{lp}_{rec}");
421 h1->SetTitle("Projection: Leading Particle Fragmentation");
422 h1->DrawCopy("colz");
426 //____________________________________________________________________
427 void AliJetSpectrumUnfolding::DrawComparison(const char* name, TH2* const genHist)
430 // Draws the copmparison plot (gen,rec and unfolded distributions
433 if (fUnfSpectrum->Integral() == 0)
435 printf("ERROR. Unfolded histogram is empty\n");
439 //regain measured distribution used for unfolding, because the bins were modified in SetupCurrentHists
441 fCurrentRec = (TH2F*)fRecSpectrum->Clone();
442 fCurrentRec->Sumw2();
443 fCurrentRec->Scale(1.0 / fCurrentRec->Integral());
445 // normalize unfolded result to 1
446 fUnfSpectrum->Scale(1.0 / fUnfSpectrum->Integral());
448 // find bin with <= 100 entries. this is used as limit for the chi2 calculation
449 Int_t mcBinLimitE = 0, mcBinLimitZ = 0;
450 for (Int_t i=0; i<genHist->GetNbinsX(); ++i)
451 for (Int_t j=0; j<genHist->GetNbinsY(); ++j)
453 if (genHist->GetBinContent(i,j) > 100)
461 Printf("AliJetSpectrumUnfolding::DrawComparison: Gen bin limit is (x,y) = (%d,%d)", mcBinLimitE,mcBinLimitZ);
463 // scale to 1 true spectrum
465 genHist->Scale(1.0 / genHist->Integral());
467 // calculate residual
468 // for that we convolute the response matrix with the gathered result
469 TH2* tmpRecRecorrected = (TH2*) fUnfSpectrum->Clone("tmpRecRecorrected");
470 TH2* convoluted = CalculateRecSpectrum(tmpRecRecorrected);
471 if (convoluted->Integral() > 0)
472 convoluted->Scale(1.0 / convoluted->Integral());
474 printf("ERROR: convoluted is empty. Something went wrong calculating the convoluted histogram.\n");
476 TH2* residual = (TH2*) convoluted->Clone("residual");
477 residual->SetTitle("(R#otimesUnfolded - Reconstructed)/Reconstructed;E^{jet} [GeV]; z^{lp}");
479 fCurrentRec->Scale(1./fCurrentRec->Integral());
480 residual->Add(fCurrentRec, -1);
481 //residual->Divide(residual, fCurrentRec, 1, 1, "B");
484 TCanvas* canvas1 = new TCanvas(name, name, 1000, 1000);
485 canvas1->Divide(2, 3);
488 const Int_t nRGBs = 5;
489 const Int_t nCont = 500;
491 Double_t stops[nRGBs] = { 0.00, 0.34, 0.61, 0.84, 1.00 };
492 Double_t red[nRGBs] = { 0.00, 0.00, 0.87, 1.00, 0.51 };
493 Double_t green[nRGBs] = { 0.00, 0.81, 1.00, 0.20, 0.00 };
494 Double_t blue[nRGBs] = { 0.51, 1.00, 0.12, 0.00, 0.00 };
495 TColor::CreateGradientColorTable(nRGBs, stops, red, green, blue, nCont);
496 gStyle->SetNumberContours(nCont);
499 gStyle->SetPalette(style);
501 genHist->SetTitle("Generated Spectrum;E^{jet}_{gen} [GeV];z^{lp}");
502 genHist->SetStats(0);
503 genHist->DrawCopy("colz");
506 gStyle->SetPalette(style);
508 fUnfSpectrum->SetStats(0);
509 fUnfSpectrum->DrawCopy("colz");
512 gStyle->SetPalette(style);
514 fCurrentRec->SetTitle(fRecSpectrum->GetTitle());
515 fCurrentRec->SetStats(0);
516 fCurrentRec->DrawCopy("colz");
519 gStyle->SetPalette(style);
521 TH1D* projGenX = genHist->ProjectionX();
522 projGenX->SetTitle("Projection: Jet Energy; E^{jet} [GeV]");
523 TH1D* projUnfX = fUnfSpectrum->ProjectionX();
524 TH1D* projRecX = fCurrentRec->ProjectionX();
525 projGenX->SetStats(0);
526 projRecX->SetStats(0);
527 projUnfX->SetStats(0);
528 projGenX->SetLineColor(8);
529 projRecX->SetLineColor(2);
530 projGenX->DrawCopy();
531 projUnfX->DrawCopy("same");
532 projRecX->DrawCopy("same");
534 TLegend* legend = new TLegend(0.6, 0.85, 0.98, 0.98);
535 legend->AddEntry(projGenX, "Generated Spectrum");
536 legend->AddEntry(projUnfX, "Unfolded Spectrum");
537 legend->AddEntry(projRecX, "Reconstructed Spectrum");
538 //legend->SetFillColor(0);
539 legend->Draw("same");
543 gStyle->SetPalette(style);
544 TH1D* projGenY = genHist->ProjectionY();
545 projGenY->SetTitle("Projection: Leading Particle Fragmentation; z^{lp}");
546 TH1D* projUnfY = fUnfSpectrum->ProjectionY();
547 TH1D* projRecY = fCurrentRec->ProjectionY();
548 projGenY->SetStats(0);
549 projRecY->SetStats(0);
550 projUnfY->SetStats(0);
551 projGenY->SetLineColor(8);
552 projRecY->SetLineColor(2);
553 projGenY->DrawCopy();
554 projUnfY->DrawCopy("same");
555 projRecY->DrawCopy("same");
557 TLegend* legend1 = new TLegend(0.6, 0.85, 0.98, 0.98);
558 legend1->AddEntry(projGenY, "Generated Spectrum");
559 legend1->AddEntry(projUnfY, "Unfolded Spectrum");
560 legend1->AddEntry(projRecY, "Recontructed Spectrum");
561 //legend1->SetFillColor(0);
562 legend1->Draw("same");
566 gStyle->SetPalette(style);
568 residual->SetStats(0);
569 residual->DrawCopy("colz");
571 canvas1->SaveAs(Form("%s.png", canvas1->GetName()));
575 //____________________________________________________________________
576 void AliJetSpectrumUnfolding::GetComparisonResults(Float_t* const gen, Int_t* const genLimit, Float_t* const residuals, Float_t* const ratioAverage) const
578 // Returns the chi2 between the Generated and the unfolded Reconstructed spectrum as well as between the Reconstructed and the folded unfolded
579 // These values are computed during DrawComparison, thus this function picks up the
585 *genLimit = fLastChi2MCLimit;
587 *residuals = fLastChi2Residuals;
589 *ratioAverage = fRatioAverage;
592 //____________________________________________________________________
593 void AliJetSpectrumUnfolding::ApplyBayesianMethod(Float_t regPar, Int_t nIterations, TH2* const initialConditions, Bool_t determineError)
596 // correct spectrum using bayesian unfolding
599 // initialize seed with current time
602 printf("seting up current arrays and histograms...\n");
603 SetupCurrentHists(kFALSE); // kFALSE to not create big bin
605 // normalize Correlation Map to convert number of events into probabilities
606 /*for (Int_t te=1; te<=NBINSE; te++)
607 for (Int_t tz=1; tz<=NBINSZ; tz++)
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 sum += fCurrentCorrelation->GetBinContent(bin);
619 for (Int_t me = 1; me<=NBINSE; me++)
620 for (Int_t mz = 1; mz<=NBINSZ; mz++)
622 bin[0] = te; bin[1] = me;
623 bin[2] = tz; bin[3] = mz;
624 fCurrentCorrelation->SetBinContent(bin, fCurrentCorrelation->GetBinContent(bin)/sum);
625 fCurrentCorrelation->SetBinError(bin, fCurrentCorrelation->GetBinError(bin)/sum);
628 Float_t sum[fgkNBINSE+2][fgkNBINSZ+2];
629 memset(sum,0,sizeof(Float_t)*(fgkNBINSE+2)*(fgkNBINSZ+2));
631 for (Int_t idx=0; idx<=fCurrentCorrelation->GetNbins(); idx++)
634 Float_t binContent = fCurrentCorrelation->GetBinContent(idx, bin);
635 if ( (bin[1]>0 && bin[1]<=fgkNBINSE) && (bin[3]>0 && bin[3]<=fgkNBINSZ) )
636 sum[bin[0]][bin[2]] += binContent;
639 for (Int_t idx=0; idx<=fCurrentCorrelation->GetNbins(); idx++)
642 Float_t binContent = fCurrentCorrelation->GetBinContent(idx, bin);
643 Float_t binError = fCurrentCorrelation->GetBinError(bin);
644 if (sum[bin[0]][bin[2]]>0 && (bin[1]>0 && bin[1]<=fgkNBINSE) &&
645 (bin[3]>0 && bin[3]<=fgkNBINSZ) && (bin[0]>0 && bin[0]<=fgkNBINSE) && (bin[2]>0 && bin[2]<=fgkNBINSZ) )
647 fCurrentCorrelation->SetBinContent(bin, binContent/sum[bin[0]][bin[2]]);
648 fCurrentCorrelation->SetBinError(bin, binError/sum[bin[0]][bin[2]]);
652 printf("calling UnfoldWithBayesian\n");
653 Int_t success = UnfoldWithBayesian(fCurrentCorrelation, fCurrentRec, initialConditions, fUnfSpectrum, regPar, nIterations, kFALSE);
660 Printf("AliJetSpectrumUnfolding::ApplyBayesianMethod: WARNING: No errors calculated.");
664 // evaluate errors, this is done by randomizing the measured spectrum following Poission statistics
665 // this (new) measured spectrum is then unfolded and the different to the result from the "real" measured
666 // spectrum calculated. This is performed N times and the maximum difference is taken as the systematic
667 // error of the unfolding method itself.
669 TH2* maxError = (TH2*) fUnfSpectrum->Clone("maxError");
672 TH2* normalizedResult = (TH2*) fUnfSpectrum->Clone("normalizedResult");
673 normalizedResult->Scale(1.0 / normalizedResult->Integral());
675 const Int_t kErrorIterations = 20;
677 printf("Spectrum unfolded. Determining error (%d iterations)...\n", kErrorIterations);
679 TH2* randomized = (TH2*) fCurrentRec->Clone("randomized");
680 TH2* result2 = (TH2*) fUnfSpectrum->Clone("result2");
681 for (Int_t n=0; n<kErrorIterations; ++n)
683 // randomize the content of clone following a poisson with the mean = the value of that bin
684 for (Int_t x=1; x<=randomized->GetNbinsX(); x++)
685 for (Int_t y=1; y<=randomized->GetNbinsY(); y++)
687 Float_t randomValue = fCurrentRec->GetBinContent(x,y);
688 TF1* poisson = new TF1("poisson", "TMath::Poisson(x,[0])",randomValue*0.25, randomValue*1.25);
689 poisson->SetParameters(randomValue,0.);
690 randomValue = poisson->GetRandom();
691 //printf("%e --> %e\n", fCurrentRec->GetBinContent(x,y), (Double_t)randomValue);
692 randomized->SetBinContent(x, y, randomValue);
697 if (UnfoldWithBayesian(fCurrentCorrelation, randomized, initialConditions, result2, regPar, nIterations) != 0)
700 result2->Scale(1.0 / result2->Integral());
703 result2->Divide(normalizedResult);
705 //new TCanvas; result2->DrawCopy("HIST");
707 // find max. deviation
708 for (Int_t i=1; i<=result2->GetNbinsX(); i++)
709 for (Int_t j=1; j<=result2->GetNbinsY(); j++)
710 maxError->SetBinContent(i, j, TMath::Max(maxError->GetBinContent(i,j), TMath::Abs(1 - result2->GetBinContent(i,j))));
715 for (Int_t i=1; i<=fUnfSpectrum->GetNbinsX(); i++)
716 for (Int_t j=1; j<=fUnfSpectrum->GetNbinsY(); j++)
717 fUnfSpectrum->SetBinError(i, j, fUnfSpectrum->GetBinError(i,j) + maxError->GetBinContent(i,j)*fUnfSpectrum->GetBinContent(i,j));
720 delete normalizedResult;
723 //____________________________________________________________________
724 Int_t AliJetSpectrumUnfolding::UnfoldWithBayesian(THnSparseF* const correlation, TH2* const measured, TH2* const initialConditions, TH2* const aResult, Float_t regPar, Int_t nIterations, Bool_t calculateErrors)
727 // unfolds a spectrum
730 if (measured->Integral() <= 0)
732 Printf("AliJetSpectrumUnfolding::UnfoldWithBayesian: ERROR: The measured spectrum is empty");
735 const Int_t nFillesBins = correlation->GetNbins();
736 const Int_t kStartBin = 1;
738 const Int_t kMaxTZ = fgkNBINSZ; // max true axis fragmentation function
739 const Int_t kMaxMZ = fgkNBINSZ; // max measured axis fragmentation function
740 const Int_t kMaxTE = fgkNBINSE; // max true axis energy
741 const Int_t kMaxME = fgkNBINSE; // max measured axis energy
743 printf("NbinsE=%d - NbinsZ=%d\n", fgkNBINSE, fgkNBINSZ);
745 // store information in arrays, to increase processing speed
746 Double_t measuredCopy[kMaxME+1][kMaxMZ+1];
747 Double_t prior[kMaxTE+1][kMaxTZ+1];
748 Double_t errors[kMaxTE+1][kMaxTZ+1];
749 Double_t result[kMaxTE+1][kMaxTZ+1];
751 THnSparseF *inverseCorrelation;
752 inverseCorrelation = (THnSparseF*)correlation->Clone("inverseCorrelation");
753 inverseCorrelation->Reset();
755 Float_t inputDistIntegral = 1;
756 if (initialConditions)
758 printf("Using different starting conditions...\n");
759 inputDistIntegral = initialConditions->Integral();
761 Float_t measuredIntegral = measured->Integral();
762 for (Int_t me=1; me<=kMaxME; me++)
763 for (Int_t mz=1; mz<=kMaxMZ; mz++)
765 // normalization of the measured spectrum
766 measuredCopy[me][mz] = measured->GetBinContent(me,mz) / measuredIntegral;
767 errors[me][mz] = measured->GetBinError(me, mz) / measuredIntegral;
768 // pick prior distribution and normalize it
769 if (initialConditions)
770 prior[me][mz] = initialConditions->GetBinContent(me,mz) / inputDistIntegral;
772 prior[me][mz] = measured->GetBinContent(me,mz) / measuredIntegral;
776 for (Int_t i=0; i<nIterations; i++)
778 // calculate Inverse Correlation Map from Bayes theorem:
779 // IR_ji = R_ij * prior_i / sum_k(R_kj * prior_k)
781 for (Int_t me=1; me<=kMaxME; me++)
782 for (Int_t mz=1; mz<=kMaxMZ; mz++)
785 for (Int_t te=kStartBin; te<=kMaxTE; te++)
786 for (Int_t tz=kStartBin; tz<=kMaxTZ; tz++)
788 Int_t bin[4] = {te, me, tz, mz};
789 norm += correlation->GetBinContent(bin)*prior[te][tz];
792 for (Int_t te = kStartBin; te <= kMaxTE; te++)
793 for (Int_t tz = kStartBin; tz <= kMaxTZ; tz++)
795 Int_t bin[4] = {te, me, tz, mz};
796 inverseCorrelation->SetBinContent(bin, correlation->GetBinContent(bin)*prior[te][tz]/norm );
799 // inverse response set to '0' wich has been already done in line 2069
801 inverseCorrelation->Reset();
802 Float_t norm[kMaxTE+2][kMaxTZ+2];
803 for (Int_t te=0; te<(kMaxTE+2); te++)
804 for (Int_t tz=0; tz<(kMaxTZ+2); tz++)
806 for (Int_t idx=0; idx<=correlation->GetNbins(); idx++)
809 Float_t binContent = correlation->GetBinContent(idx, bin);
810 if (bin[1]>0 && bin[1]<=fgkNBINSE && bin[3]>0 && bin[3]<=fgkNBINSZ &&
811 bin[0]>0 && bin[0]<=fgkNBINSE && bin[2]>0 && bin[2]<=fgkNBINSZ)
812 norm[bin[1]][bin[3]] += binContent*prior[bin[0]][bin[2]];
814 Float_t chi2Measured=0, diff;
815 for (Int_t idx=0; idx<=correlation->GetNbins(); idx++)
818 Float_t binContent = correlation->GetBinContent(idx, bin);
819 if (norm[bin[1]][bin[3]]>0 && bin[1]>0 && bin[1]<=fgkNBINSE &&
820 bin[3]>0 && bin[3]<=fgkNBINSZ && bin[0]>0 && bin[2]>0 && bin[0]<=fgkNBINSE && bin[2]<=fgkNBINSZ)
822 inverseCorrelation->SetBinContent(bin, binContent*prior[bin[0]][bin[2]]/norm[bin[1]][bin[3]]);
823 if (errors[bin[1]][bin[3]]>0)
825 diff = ((measuredCopy[bin[1]][bin[3]]-norm[bin[1]][bin[3]])/(errors[bin[1]][bin[3]]));
826 chi2Measured += diff*diff;
831 // calculate "generated" spectrum
832 for (Int_t te = kStartBin; te<=kMaxTE; te++)
833 for (Int_t tz = kStartBin; tz<=kMaxTZ; tz++)
836 for (Int_t me=1; me<=kMaxME; me++)
837 for (Int_t mz=1; mz<=kMaxMZ; mz++)
839 Int_t bin[4] = {te, me, tz, mz};
840 value += inverseCorrelation->GetBinContent(bin)*measuredCopy[me][mz];
842 result[te][tz] = value;
843 //printf("%e\n", result[te][tz]);
846 // regularization (simple smoothing)
847 Float_t chi2LastIter = 0;
848 for (Int_t te=kStartBin; te<=kMaxTE; te++)
849 for (Int_t tz=kStartBin; tz<=kMaxTZ; tz++)
851 Float_t newValue = 0;
852 // 0 bin excluded from smoothing
853 if (( te >(kStartBin+1) && te<(kMaxTE-1) ) && ( tz > (kStartBin+1) && tz<(kMaxTZ-1) ))
855 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.;
857 // weight the average with the regularization parameter
858 newValue = (1 - regPar) * result[te][tz] + regPar * average;
861 newValue = result[te][tz];
862 if (prior[te][tz]>1.e-5)
864 diff = ((prior[te][tz]-newValue)/prior[te][tz]);
865 chi2LastIter = diff*diff;
867 prior[te][tz] = newValue;
869 //printf(" iteration %d - chi2LastIter = %e - chi2Measured = %e \n", i, chi2LastIter/((Float_t)kMaxTE*(Float_t)kMaxTZ), chi2Measured/((Float_t)kMaxTE*(Float_t)kMaxTZ));
870 if (chi2LastIter/((Float_t)kMaxTE*(Float_t)kMaxTZ)<5.e-6 && chi2Measured/((Float_t)kMaxTE*(Float_t)kMaxTZ)<5.e-3)
872 } // end of iterations
874 // propagate errors of the reconstructed distribution through the unfolding
875 for (Int_t te = kStartBin; te<=kMaxTE; te++)
876 for (Int_t tz = kStartBin; tz<=kMaxTZ; tz++)
878 Float_t valueError = 0;
879 // Float_t binError = 0;
880 for (Int_t me=1; me<=kMaxME; me++)
881 for (Int_t mz=1; mz<=kMaxMZ; mz++)
883 Int_t bin[4] = {te, me, tz, mz};
884 valueError += inverseCorrelation->GetBinContent(bin)*inverseCorrelation->GetBinContent(bin)*errors[me][mz]*errors[me][mz];
886 //if (errors[te][tz]!=0)printf("errors[%d][%d]=%e\n", te, tz, valueError);
887 aResult->SetBinContent(te, tz, prior[te][tz]);
888 aResult->SetBinError(te, tz, TMath::Sqrt(valueError));
891 // ***********************************************************************************************************
892 // Calculate the covariance matrix, all arguments are taken from G. D'Agostini (p.6-8)
895 printf("Covariance matrix will be calculated... this will take a lot of time (>1 day) ;)\n");
897 //Variables and Matrices that will be use along the calculation
898 const Int_t binsV[4] = {fgkNBINSE,fgkNBINSE, fgkNBINSZ, fgkNBINSZ};
899 const Double_t lowEdgeV[4] = {fgkaxisLowerLimitE, fgkaxisLowerLimitE, fgkaxisLowerLimitZ, fgkaxisLowerLimitZ};
900 const Double_t upEdgeV[4] = {fgkaxisUpperLimitE, fgkaxisUpperLimitE, fgkaxisUpperLimitZ, fgkaxisUpperLimitZ};
902 const Double_t nTrue = (Double_t)measured->Integral();
904 THnSparseF *v = new THnSparseF("V","",4, binsV, lowEdgeV, upEdgeV);
906 Double_t invCorrContent1, nt;
907 Double_t invCorrContent2, v11,v12, v2;
908 // calculate V1 and V2
909 for (Int_t idx1=0; idx1<=nFillesBins; idx1++)
911 printf("Covariance Matrix calculation: iteration idx1=%d of %d\n", idx1, nFillesBins);
912 for (Int_t idx2=0; idx2<=nFillesBins; idx2++)
916 invCorrContent1 = inverseCorrelation->GetBinContent(idx1, bin1);
917 invCorrContent2 = inverseCorrelation->GetBinContent(idx2, bin2);
919 if(bin1[0]>0 && bin1[0]<=fgkNBINSE && bin1[1]>0 && bin1[1]<=fgkNBINSE &&
920 bin1[2]>0 && bin1[2]<=fgkNBINSZ && bin1[3]>0 && bin1[3]<=fgkNBINSZ &&
921 bin2[0]>0 && bin2[0]<=fgkNBINSE && bin2[1]>0 && bin2[1]<=fgkNBINSE &&
922 bin2[2]>0 && bin2[2]<=fgkNBINSZ && bin2[3]>0 && bin2[3]<=fgkNBINSZ)
924 if (bin1[1]==bin2[1] && bin1[3]==bin2[3])
925 v11 = invCorrContent1*invCorrContent2*measuredCopy[bin1[1]][bin1[3]]
926 *(1. - measuredCopy[bin2[1]][bin2[3]]/nTrue);
928 v12 = invCorrContent1*invCorrContent2*measuredCopy[bin1[1]][bin1[3]]*
929 measuredCopy[bin2[1]][bin2[3]]/nTrue;
930 nt = (Double_t)prior[bin2[0]][bin2[2]];
931 v2 = measuredCopy[bin1[1]][bin1[3]]*measuredCopy[bin2[1]][bin2[3]]*
932 invCorrContent1*invCorrContent2*
933 BayesUncertaintyTerms(inverseCorrelation, correlation, bin1, bin2, nt);
934 Int_t binV[4] = {bin1[0],bin2[0],bin1[2],bin2[2]};
935 v->SetBinContent(binV,v11-v12 + v2);
940 for(Int_t te = 1; te<=fgkNBINSE; te++)
941 for(Int_t tz = 1; tz<=fgkNBINSZ; tz++)
943 Int_t binV[4] = {te,te,tz,tz};
944 aResult->SetBinError( te, tz, v->GetBinContent(binV) );
947 TFile* f = new TFile("Covariance_UnfSpectrum.root");
957 //____________________________________________________________________
958 Double_t AliJetSpectrumUnfolding::BayesUncertaintyTerms(THnSparseF* const M, THnSparseF* const C, Int_t* const binTM, Int_t* const binTM1, Double_t nt)
961 // helper function for the covariance matrix of the bayesian method
966 Int_t tmpBin[4], tmpBin1[4];
967 const Int_t nFilledBins = C->GetNbins();
972 Float_t invCorrContent;
974 tmpBin[0] =binTM[0]; tmpBin[1] =binTM[1]; tmpBin[2] =binTM[2]; tmpBin[3] =binTM[3];
975 tmpBin1[0]=binTM[0]; tmpBin1[1]=binTM1[1]; tmpBin1[2]=binTM[2]; tmpBin1[3]=binTM1[3];
976 if (C->GetBinContent(tmpBin)!=0 && C->GetBinContent(tmpBin1)!=0)
978 if (binTM[0]==binTM1[0] && binTM[2]==binTM1[2])
979 term[0] = BayesCov(M, C, tmpBin, tmpBin1)/
980 (C->GetBinContent(tmpBin)*C->GetBinContent(tmpBin1));
981 term[2] = term[0]*M->GetBinContent(tmpBin1);
989 tmpBin[0]=binTM1[0]; tmpBin[1]=binTM[1]; tmpBin[2]=binTM1[2]; tmpBin[3]=binTM[3];
990 tmpBin1[0]=binTM1[0]; tmpBin1[1]=binTM1[1]; tmpBin1[2]=binTM1[2]; tmpBin1[3]=binTM1[3];
991 if (C->GetBinContent(tmpBin)!=0 && C->GetBinContent(tmpBin1)!=0)
992 term[6] = BayesCov(M, C, tmpBin, tmpBin1)*
993 M->GetBinContent(tmpBin)/
994 (C->GetBinContent(tmpBin)*C->GetBinContent(tmpBin1));
998 for(Int_t idx1=0; idx1<=nFilledBins; idx1++)
1001 corrContent = C->GetBinContent(idx1, bin1);
1002 invCorrContent = M->GetBinContent(idx1, bin1);
1003 if(bin1[0]>0 && bin1[0]<=fgkNBINSE && bin1[1]>0 && bin1[1]<=fgkNBINSE &&
1004 bin1[2]>0 && bin1[2]<=fgkNBINSZ && bin1[3]>0 && bin1[3]<=fgkNBINSZ)
1006 tmpBin[0] =binTM[0]; tmpBin[1] =binTM[1]; tmpBin[2] =binTM[2]; tmpBin[3] =binTM[3];
1007 tmpBin1[0]=binTM[0]; tmpBin1[1]=bin1[1]; tmpBin1[2]=binTM[2]; tmpBin1[3]=bin1[3];
1008 if (C->GetBinContent(tmpBin)!=0 &&
1009 binTM[0]==binTM1[0] && binTM[2]==binTM1[2])
1010 term[1] = BayesCov(M, C, tmpBin, tmpBin1)/C->GetBinContent(tmpBin);
1014 tmpBin[0] =binTM[0]; tmpBin[1] =bin1[1]; tmpBin[2] =binTM[2]; tmpBin[3] =bin1[3];
1015 tmpBin1[0]=binTM[0]; tmpBin1[1]=binTM1[1]; tmpBin1[2]=binTM[2]; tmpBin1[3]=binTM1[3];
1016 if (C->GetBinContent(tmpBin1)!=0)
1018 if (binTM[0]==binTM1[0] && binTM[2]==binTM1[2])
1019 term[3] = BayesCov(M, C, tmpBin, tmpBin1)/
1020 C->GetBinContent(tmpBin1);
1021 term[5] = BayesCov(M, C, tmpBin, tmpBin1)*M->GetBinContent(tmpBin1)/
1022 C->GetBinContent(tmpBin1);
1030 tmpBin[0] =binTM1[0]; tmpBin[1] =binTM[1]; tmpBin[2] =binTM1[2]; tmpBin[3] =binTM[3];
1031 tmpBin1[0]=binTM1[0]; tmpBin1[1]=bin1[1]; tmpBin1[2]=binTM1[2]; tmpBin1[3]=bin1[3];
1032 if (C->GetBinContent(tmpBin)!=0)
1033 term[7] = BayesCov(M, C, tmpBin, tmpBin1)*M->GetBinContent(tmpBin)/
1034 C->GetBinContent(tmpBin);
1038 tmpBin[0] =bin1[0]; tmpBin[1] =binTM[1]; tmpBin[2] =bin1[2]; tmpBin[3] =binTM[3];
1039 tmpBin1[0]=bin1[0]; tmpBin1[1]=binTM1[1]; tmpBin1[2]=bin1[2]; tmpBin1[3]=binTM1[3];
1040 if (C->GetBinContent(tmpBin)!=0 && C->GetBinContent(tmpBin1)!=0)
1041 term[8] = BayesCov(M, C, tmpBin, tmpBin1)*
1042 M->GetBinContent(tmpBin)*M->GetBinContent(tmpBin)/
1043 (C->GetBinContent(tmpBin)*C->GetBinContent(tmpBin1));
1047 for (Int_t i=0; i<9; i++)
1048 result += term[i]/nt;
1055 //____________________________________________________________________
1056 Double_t AliJetSpectrumUnfolding::BayesCov(THnSparseF* const M, THnSparseF* const correlation, Int_t* const binTM, Int_t* const bin1)
1060 // get the covariance matrix
1064 Double_t result, result1, result2, result3;
1066 if (binTM[0]==bin1[0] && binTM[2]==bin1[2])
1068 if (correlation->GetBinContent(bin1)!=0)
1069 result1 = 1./correlation->GetBinContent(bin1);
1080 if (binTM[1]==bin1[1] && binTM[3]==bin1[3])
1082 Int_t tmpbin[4] = {bin1[0], binTM[1], bin1[2], binTM[3]};
1083 if(correlation->GetBinContent(tmpbin)!=0)
1084 result3 = M->GetBinContent(tmpbin)/correlation->GetBinContent(tmpbin);
1094 return result = result1 + result2 + result3;
1097 //____________________________________________________________________
1098 TH2F* AliJetSpectrumUnfolding::CalculateRecSpectrum(TH2* const inputGen)
1100 // runs the distribution given in inputGen through the correlation histogram identified by
1101 // fCorrelation and produces a reconstructed spectrum
1106 // normalize to convert number of events into probability
1107 /*for (Int_t te=1; te<=NBINSE; te++)
1108 for (Int_t tz=1; tz<=NBINSZ; tz++)
1112 for (Int_t me = 1; me<=NBINSE; me++)
1113 for (Int_t mz = 1; mz<=NBINSZ; mz++)
1115 bin[0] = te; bin[1] = me;
1116 bin[2] = tz; bin[3] = mz;
1117 sum += fCorrelation[correlationMap]->GetBinContent(bin);
1120 for (Int_t me = 1; me<=NBINSE; me++)
1121 for (Int_t mz = 1; mz<=NBINSZ; mz++)
1123 bin[0] = te; bin[1] = me;
1124 bin[2] = tz; bin[3] = mz;
1125 fCorrelation[correlationMap]->SetBinContent(bin, fCorrelation[correlationMap]->GetBinContent(bin)/sum);
1126 fCorrelation[correlationMap]->SetBinError(bin, fCorrelation[correlationMap]->GetBinError(bin)/sum);
1129 // normalize to convert number of events into probability (the following loop is much faster)
1130 Float_t sum[fgkNBINSE+2][fgkNBINSZ+2];
1131 memset(sum,0,sizeof(Float_t)*(fgkNBINSE+2)*(fgkNBINSZ+2));
1133 for (Int_t idx=0; idx<fCorrelation->GetNbins(); idx++)
1136 Float_t binContent = fCorrelation->GetBinContent(idx, bin);
1137 if (bin[1]>0 && bin[1]<=fgkNBINSE && bin[3]>0 && bin[3]<=fgkNBINSZ){
1138 sum[bin[0]][bin[2]] += binContent;
1142 for (Int_t idx=0; idx<fCorrelation->GetNbins(); idx++)
1145 Float_t binContent = fCorrelation->GetBinContent(idx, bin);
1146 Float_t binError = fCorrelation->GetBinError(bin);
1147 if (sum[bin[0]][bin[2]]>0 && bin[1]>0 && bin[1]<=fgkNBINSE &&
1148 bin[3]>0 && bin[3]<=fgkNBINSZ && bin[0]>0 && bin[2]>0 && bin[0]<=fgkNBINSE && bin[2]<=fgkNBINSZ)
1150 fCorrelation->SetBinContent(bin, binContent/sum[bin[0]][bin[2]]);
1151 fCorrelation->SetBinError(bin, binError/sum[bin[0]][bin[2]]);
1155 TH2F* target = dynamic_cast<TH2F*> (fRecSpectrum->Clone(Form("reconstructed_%s", inputGen->GetName())));
1158 for (Int_t me=1; me<=fgkNBINSE; ++me)
1159 for (Int_t mz=1; mz<=fgkNBINSZ; ++mz)
1161 Float_t measured = 0;
1164 for (Int_t te=1; te<=fgkNBINSE; ++te)
1165 for (Int_t tz=1; tz<=fgkNBINSZ; ++tz)
1167 Int_t bin[4] = {te, me, tz, mz};
1168 measured += inputGen->GetBinContent(te,tz) * fCorrelation->GetBinContent(bin);
1169 error += inputGen->GetBinError(te,tz) * fCorrelation->GetBinContent(bin);
1171 target->SetBinContent(me, mz, measured);
1172 target->SetBinError(me, mz, error);
1178 //__________________________________________________________________________________________________
1179 void AliJetSpectrumUnfolding::SetGenRecFromFunc(TF2* const inputGen)
1181 // uses the given function to fill the input Generated histogram and generates from that
1182 // the reconstructed histogram by applying the response histogram
1183 // this can be used to evaluate if the methods work indepedently of the input
1189 TH2F* histtmp = new TH2F("histtmp", "tmp",
1190 fgkNBINSE, fgkaxisLowerLimitE, fgkaxisUpperLimitE,
1191 fgkNBINSZ, fgkaxisLowerLimitZ, fgkaxisUpperLimitZ);
1193 TH2F* gen = fGenSpectrum;
1198 histtmp->FillRandom(inputGen->GetName(), fgkNEVENTS);
1200 for (Int_t i=1; i<=gen->GetNbinsX(); ++i)
1201 for (Int_t j=1; j<=gen->GetNbinsY(); ++j)
1203 gen->SetBinContent(i, j, histtmp->GetBinContent(i,j));
1204 gen->SetBinError(i, j, histtmp->GetBinError(i,j));
1210 //gStyle->SetPalette(1);
1212 //gen->Draw("COLZ");
1215 TH2 *recsave = fRecSpectrum;
1217 fRecSpectrum = CalculateRecSpectrum(gen);
1218 fRecSpectrum->SetName(recsave->GetName());
1223 //________________________________________________________________________________________