1 /* Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
2 * See cxx source for full Copyright notice */
5 /* Author: Redmer Alexander Bertens, rbertens@cern.ch, rbertens@nikhef.nl, r.a.bertens@uu.nl
6 * see implementation for additional information */
8 #ifndef AliJetFlowTools_H
9 #define AliJetFlowTools_H
11 // root forward declarations
21 // aliroot forward declarations
22 class AliAnaChargedJetResponseMaker;
27 #include "TDirectoryFile.h"
30 #include "TVirtualPad.h"
31 #include "TPaveText.h"
33 //_____________________________________________________________________________
34 class AliJetFlowTools {
38 ~AliJetFlowTools(); // not implemented (deliberately). object ownership is a bit messy in this class
39 // since most (or all) of the objects are owned by the input and output files
42 enum unfoldingAlgorithm { // type of unfolding alrogithm
43 kChi2, // chi^2 unfolding, implemented in AliUnfolding
44 kBayesian, // Bayesian unfolding, implemented in RooUnfold
45 kBayesianAli, // Bayesian unfolding, implemented in AliUnfolding
46 kSVD, // SVD unfolding, implemented in RooUnfold
47 kNone }; // no unfolding
48 enum prior { // prior that is used for unfolding
49 kPriorChi2, // prior from chi^2 method
50 kPriorMeasured, // use measured spectrum as prior
51 kPriorPythia }; // use pythia spectrum as prior
52 enum histoType { // histogram identifier, only used internally
53 kInPlaneSpectrum, // default style for spectrum
58 kBar, // default style for bar histogram
59 kRatio, // default style for ratio
60 kV2, // default style for v2
61 kEmpty }; // default style
62 // setters, interface to the class
63 void SetSaveFull(Bool_t b) {fSaveFull = b;}
64 void SetInputList(TList* list) {
66 fRefreshInput = kTRUE;
68 void SetOutputFileName(TString name) {fOutputFileName = name;}
69 void CreateOutputList(TString name) {
70 // create a new output list and add it to the full output
71 if(!fOutputFile) fOutputFile = new TFile(fOutputFileName.Data(), "RECREATE");
72 fOutputFile->cd(); // avoid nested dirs
73 if(name.EqualTo(fActiveString)) {
74 printf(Form(" > Warning, duplicate output list, renaming %s to %s_2 ! < \n", name.Data(), name.Data()));
78 fActiveDir = new TDirectoryFile(fActiveString.Data(), fActiveString.Data());
81 void SetCentralityBin(Int_t bin) {
82 // in case of one centraltiy
83 fCentralityArray = new TArrayI(1);
84 fCentralityArray->AddAt(bin, 0);
85 // for one centrality there's no need for weights
86 fCentralityWeights = new TArrayD(1);
87 fCentralityWeights->AddAt(1., 0);
89 void SetCentralityBin(TArrayI* bins) {
90 fCentralityArray = bins;
92 void SetCentralityWeight(TArrayD* weights) {
93 fCentralityWeights = weights;
94 if(!fCentralityArray) printf(" > Warning: centrality weights set, but bins are not defined! \n");
96 void SetDetectorResponse(TH2D* dr) {fDetectorResponse = dr;}
97 void SetJetFindingEfficiency(TH1D* e) {fJetFindingEff = e;}
98 void SetBinsTrue(TArrayD* bins) {fBinsTrue = bins;}
99 void SetBinsRec(TArrayD* bins) {fBinsRec = bins;}
100 void SetBinsTruePrior(TArrayD* bins) {fBinsTruePrior = bins;}
101 void SetBinsRecPrior(TArrayD* bins) {fBinsRecPrior = bins;}
102 void SetSVDReg(Int_t r) {fSVDRegIn = r; fSVDRegOut = r;}
103 void SetSVDReg(Int_t in, Int_t out) {fSVDRegIn = in; fSVDRegOut = out;}
104 void SetSVDToy(Bool_t b, Float_t r) {fSVDToy = b; fJetRadius = r;}
105 void SetBeta(Double_t b) {fBetaIn = b; fBetaOut = b;}
106 void SetBeta(Double_t i, Double_t o) {fBetaIn = i; fBetaOut = o;}
107 void SetBayesianIter(Int_t i) {fBayesianIterIn = i; fBayesianIterOut = i;}
108 void SetBayesianIter(Int_t i, Int_t o) {fBayesianIterIn = i; fBayesianIterOut = o;}
109 void SetBayesianSmooth(Float_t s) {fBayesianSmoothIn = s; fBayesianSmoothOut = s;}
110 void SetBayesianSmooth(Float_t i, Float_t o) {fBayesianSmoothIn = i; fBayesianSmoothOut = o;}
111 void SetAvoidRoundingError(Bool_t r) {fAvoidRoundingError = r;}
112 void SetUnfoldingAlgorithm(unfoldingAlgorithm ua) {fUnfoldingAlgorithm = ua;}
113 void SetPrior(prior p) {fPrior = p;}
114 void SetPrior(prior p, TH1D* spectrum) {fPrior = p; fPriorUser = spectrum;}
115 void SetNormalizeSpectra(Bool_t b) {fNormalizeSpectra = b;}
116 void SetNormalizeSpectra(Int_t e) { // use to normalize to this no of events
118 fNormalizeSpectra = kFALSE;
120 void SetSmoothenPrior(Bool_t b, Float_t min = 50., Float_t max = 100., Float_t start= 75., Bool_t counts = kTRUE) {
125 fSmoothenCounts = counts;
127 void SetTestMode(Bool_t t) {fTestMode = t;}
128 void SetEventPlaneResolution(Double_t r) {fEventPlaneRes = r;}
129 void SetUseDetectorResponse(Bool_t r) {fUseDetectorResponse = r;}
130 void SetUseDptResponse(Bool_t r) {fUseDptResponse = r;}
131 void SetTrainPowerFit(Bool_t t) {fTrainPower = t;}
132 void SetDphiUnfolding(Bool_t i) {fDphiUnfolding = i;}
133 void SetDphiDptUnfolding(Bool_t i) {fDphiDptUnfolding = i;}
134 void SetExLJDpt(Bool_t i) {fExLJDpt = i;}
135 void SetWeightFunction(TF1* w) {fResponseMaker->SetRMMergeWeightFunction(w);}
137 void MakeAU(); // test function, use with caution (09012014)
140 if(fRMSSpectrumIn) fRMSSpectrumIn->Write();
141 if(fRMSSpectrumOut) fRMSSpectrumOut->Write();
142 if(fRMSRatio) fRMSRatio->Write();
143 fOutputFile->Close();}
147 Float_t rangeLow = 20,
148 Float_t rangeUp = 80,
149 TString in = "UnfoldedSpectra.root",
150 TString out = "ProcessedSpectra.root") const;
151 void GetNominalValues(
156 TString inFile = "UnfoldedSpectra.root",
157 TString outFile = "Nominal.root") const;
158 void GetCorrelatedUncertainty(
159 TGraphAsymmErrors*& corrRatio,
160 TGraphAsymmErrors*& corrV2,
161 TArrayI* variationsIn,
162 TArrayI* variationsOut,
163 TArrayI* variantions2ndIn,
164 TArrayI* variantions2ndOut,
168 Float_t rangeLow = 20,
169 Float_t rangeUp = 80,
171 TString in = "UnfoldedSpectra.root",
172 TString out = "CorrelatedUncertainty.root") const;
173 void GetShapeUncertainty(
174 TGraphAsymmErrors*& shapeRatio,
175 TGraphAsymmErrors*& shapeV2,
176 TArrayI* regularizationIn,
177 TArrayI* regularizationOut,
178 TArrayI* trueBinIn = 0x0,
179 TArrayI* trueBinOut = 0x0,
180 TArrayI* recBinIn = 0x0,
181 TArrayI* recBinOut = 0x0,
182 TArrayI* methodIn = 0x0,
183 TArrayI* methodOut = 0x0,
185 Float_t rangeLow = 20,
186 Float_t rangeUp = 80,
187 TString in = "UnfoldedSpectra.root",
188 TString out = "ShapeUncertainty.root") const;
190 TH2D* detectorResponse, // detector response matrix
191 TH1D* jetPtIn, // in plane jet spectrum
192 TH1D* jetPtOut, // out of plane jet spectrum
193 TH1D* dptIn, // in plane delta pt distribution
194 TH1D* dptOut, // out of plane delta pt distribution
195 Int_t eventCount = 0); // event count (optional)
196 // static const helper functions, mainly histogram manipulation
197 static TH1D* ResizeXaxisTH1D(TH1D* histo, Int_t low, Int_t up, TString suffix = "");
198 static TH2D* ResizeYaxisTH2D(TH2D* histo, TArrayD* x, TArrayD* y, TString suffix = "");
199 static TH2D* NormalizeTH2D(TH2D* histo, Bool_t noError = kTRUE);
200 static TH1D* RebinTH1D(TH1D* histo, TArrayD* bins, TString suffix = "", Bool_t kill = kTRUE);
201 TH2D* RebinTH2D(TH2D* histo, TArrayD* binsTrue, TArrayD* binsRec, TString suffix = "");
202 static TH2D* MatrixMultiplication(TH2D* a, TH2D* b, TString name = "CombinedResponse");
203 static TH1D* NormalizeTH1D(TH1D* histo, Double_t scale = 1.);
204 static TGraphErrors* GetRatio(TH1 *h1 = 0x0, TH1* h2 = 0x0, TString name = "", Bool_t appendFit = kFALSE, Int_t xmax = -1);
205 static TGraphErrors* GetV2(TH1* h1 = 0x0, TH1* h2 = 0x0, Double_t r = 0., TString name = "");
206 TGraphAsymmErrors* GetV2WithSystematicErrors(
207 TH1* h1, TH1* h2, Double_t r, TString name,
208 TH1* relativeErrorInUp,
209 TH1* relativeErrorInLow,
210 TH1* relativeErrorOutUp,
211 TH1* relativeErrorOutLow,
212 Float_t rho = 0.) const;
213 static void WriteObject(TObject* object, TString suffix = "", Bool_t kill = kTRUE);
214 static TH2D* ConstructDPtResponseFromTH1D(TH1D* dpt, Bool_t AvoidRoundingError);
215 static TH2D* GetUnityResponse(TArrayD* binsTrue, TArrayD* binsRec, TString suffix = "");
216 void SaveConfiguration(Bool_t convergedIn, Bool_t convergedOut) const;
217 static TMatrixD* CalculatePearsonCoefficients(TMatrixD* covmat);
218 static TH1D* SmoothenPrior(TH1D* spectrum, TF1* function, Double_t min, Double_t max, Double_t start, Bool_t kill = kTRUE, Bool_t counts = kTRUE);
220 void SetTitleFontSize(Double_t s) {fTitleFontSize = s;}
222 static void Style(TCanvas* c, TString style = "PEARSON");
223 static void Style(TVirtualPad* c, TString style = "SPECTRUM");
224 static void Style(TLegend* l);
225 static void Style(TH1* h, EColor col = kBlue, histoType = kEmpty);
226 static void Style(TGraph* h, EColor col = kBlue, histoType = kEmpty);
227 static TLegend* AddLegend(TVirtualPad* p, Bool_t style = kFALSE) {
228 if(!style) return p->BuildLegend(.565, .663, .882, .883);
230 TLegend* l = AddLegend(p, kFALSE);
235 static TPaveText* AddTPaveText(TString text, Int_t r = 2) {
236 TPaveText* t(new TPaveText(.35, .27, .76, .33,"NDC"));
237 // t->SetFillStyle(0);
240 t->AddText(0.,0.,text.Data());
241 t->AddText(0., 0., Form("#it{R} = 0.%i #it{k}_{T} charged jets", r));
242 t->SetTextColor(kBlack);
243 // t->SetTextSize(0.03);
248 static void SavePadToPDF(TVirtualPad* pad) {pad->SaveAs(Form("%s.pdf", pad->GetName()));}
249 // interface to AliUnfolding, not necessary but nice to have all parameters in one place
250 static void SetMinuitStepSize(Float_t s) {AliUnfolding::SetMinuitStepSize(s);}
251 static void SetMinuitPrecision(Float_t s) {AliUnfolding::SetMinuitPrecision(s);}
252 static void SetMinuitPrecision(Int_t i) {AliUnfolding::SetMinuitMaxIterations(i);}
253 static void SetMinuitStrategy(Double_t s) {AliUnfolding::SetMinuitStrategy(s);}
254 static void SetDebug(Int_t d) {AliUnfolding::SetDebug(d);}
256 Bool_t PrepareForUnfolding();
257 Bool_t PrepareForUnfolding(Int_t low, Int_t up);
258 TH1D* GetPrior( const TH1D* measuredJetSpectrum,
259 const TH2D* resizedResponse,
260 const TH1D* kinematicEfficiency,
261 const TH1D* measuredJetSpectrumTrueBins,
262 const TString suffix,
263 const TH1D* jetFindingEfficiency);
264 TH1D* UnfoldWrapper( const TH1D* measuredJetSpectrum,
265 const TH2D* resizedResponse,
266 const TH1D* kinematicEfficiency,
267 const TH1D* measuredJetSpectrumTrueBins,
268 const TString suffix,
269 const TH1D* jetFindingEfficiency = 0x0);
270 TH1D* UnfoldSpectrumChi2( const TH1D* measuredJetSpectrum,
271 const TH2D* resizedResponse,
272 const TH1D* kinematicEfficiency,
273 const TH1D* measuredJetSpectrumTrueBins,
274 const TString suffix,
275 const TH1D* jetFindingEfficiency = 0x0);
276 TH1D* UnfoldSpectrumSVD( const TH1D* measuredJetSpectrum,
277 const TH2D* resizedResponse,
278 const TH1D* kinematicEfficiency,
279 const TH1D* measuredJetSpectrumTrueBins,
280 const TString suffix,
281 const TH1D* jetFindingEfficiency = 0x0);
282 TH1D* UnfoldSpectrumBayesianAli( const TH1D* measuredJetSpectrum,
283 const TH2D* resizedResponse,
284 const TH1D* kinematicEfficiency,
285 const TH1D* measuredJetSpectrumTrueBins,
286 const TString suffix,
287 const TH1D* jetFindingEfficiency = 0x0);
288 TH1D* UnfoldSpectrumBayesian( const TH1D* measuredJetSpectrum,
289 const TH2D* resizedResponse,
290 const TH1D* kinematicEfficiency,
291 const TH1D* measuredJetSpectrumTrueBins,
292 const TString suffix,
293 const TH1D* jetFindingEfficiency = 0x0);
294 void DoIntermediateSystematics(
295 TArrayI* variationsIn,
296 TArrayI* variationsOut,
297 TH1D*& relativeErrorInUp,
298 TH1D*& relativeErrorInLow,
299 TH1D*& relativeErrorOutUp,
300 TH1D*& relativeErrorOutLow,
301 TH1D*& relativeSystematicIn,
302 TH1D*& relativeSystematicOut,
310 TString source = "") const;
311 static void ResetAliUnfolding();
312 // give object a unique name via the 'protect heap' functions.
313 // may seem redundant, but some internal functions of root (e.g.
314 // ProjectionY()) check for existing objects by name and re-use them
315 TH1D* ProtectHeap(TH1D* protect, Bool_t kill = kTRUE, TString suffix = "") const;
316 TH2D* ProtectHeap(TH2D* protect, Bool_t kill = kTRUE, TString suffix = "") const;
317 TGraphErrors* ProtectHeap(TGraphErrors* protect, Bool_t kill = kTRUE, TString suffix = "") const;
318 // members, accessible via setters
319 AliAnaChargedJetResponseMaker* fResponseMaker; // utility object
320 TF1* fPower; // smoothening fit
321 Bool_t fSaveFull; // save all generated histograms to file
322 TString fActiveString; // identifier of active output
323 TDirectoryFile* fActiveDir; // active directory
324 TList* fInputList; // input list
325 Bool_t fRefreshInput; // re-read the input (called automatically if input list changes)
326 TString fOutputFileName; // output file name
327 TFile* fOutputFile; // output file
328 TArrayI* fCentralityArray; // array of bins that are merged
329 TArrayD* fCentralityWeights; // array of centrality weights
330 TH2D* fDetectorResponse; // detector response
331 TH1D* fJetFindingEff; // jet finding efficiency
332 Double_t fBetaIn; // regularization strength, in plane unfolding
333 Double_t fBetaOut; // regularization strength, out of plane unfoldign
334 Int_t fBayesianIterIn; // bayesian regularization parameter, in plane unfolding
335 Int_t fBayesianIterOut; // bayesian regularization parameter, out plane unfolding
336 Float_t fBayesianSmoothIn; // bayesian smoothening parameter (AliUnfolding)
337 Float_t fBayesianSmoothOut; // bayesian smoothening parameter (AliUnfolding)
338 Bool_t fAvoidRoundingError; // set dpt to zero for small values far from the diagonal
339 unfoldingAlgorithm fUnfoldingAlgorithm; // algorithm used for unfolding
340 prior fPrior; // prior for unfolding
341 TH1D* fPriorUser; // user supplied prior (e.g. pythia spectrum)
342 TArrayD* fBinsTrue; // pt true bins
343 TArrayD* fBinsRec; // pt rec bins
344 TArrayD* fBinsTruePrior; // holds true bins for the chi2 prior for SVD. setting this is optional
345 TArrayD* fBinsRecPrior; // holds rec bins for the chi2 prior for SVD. setting this is optional
346 Int_t fSVDRegIn; // svd regularization (a good starting point is half of the number of bins)
347 Int_t fSVDRegOut; // svd regularization out of plane
348 Bool_t fSVDToy; // use toy to estimate coveriance matrix for SVD method
349 Float_t fJetRadius; // jet radius (for SVD toy)
350 Int_t fEventCount; // number of events
351 Bool_t fNormalizeSpectra; // normalize spectra to event count
352 Bool_t fSmoothenPrior; // smoothen the tail of the measured spectrum using a powerlaw fit
353 Float_t fFitMin; // lower bound of smoothening fit
354 Float_t fFitMax; // upper bound of smoothening fit
355 Float_t fFitStart; // from this value, use smoothening
356 Bool_t fSmoothenCounts; // fill smoothened spectrum with counts
357 Bool_t fTestMode; // unfold with unity response for testing
358 Bool_t fRawInputProvided; // input histograms provided, not read from file
359 Double_t fEventPlaneRes; // event plane resolution for current centrality
360 Bool_t fUseDetectorResponse; // add detector response to unfolding
361 Bool_t fUseDptResponse; // add dpt response to unfolding
362 Bool_t fTrainPower; // don't clear the params of fPower for call to Make
363 // might give more stable results, but possibly introduces
364 // a bias / dependency on previous iterations
365 Bool_t fDphiUnfolding; // do the unfolding in in and out of plane orientation
366 Bool_t fDphiDptUnfolding; // do the unfolding in dphi and dpt bins (to fit v2)
367 Bool_t fExLJDpt; // exclude randon cones with leading jet
368 Double_t fTitleFontSize; // title font size
369 // members, set internally
370 TProfile* fRMSSpectrumIn; // rms of in plane spectra of converged unfoldings
371 TProfile* fRMSSpectrumOut; // rms of out of plane spectra of converged unfoldings
372 TProfile* fRMSRatio; // rms of ratio of converged unfolded spectra
373 TProfile* fRMSV2; // rms of v2 of converged unfolded spectra
374 TH2D* fDeltaPtDeltaPhi; // delta pt delta phi distribution
375 TH2D* fJetPtDeltaPhi; // jet pt delta phi distribution
376 TH1D* fSpectrumIn; // in plane jet pt spectrum
377 TH1D* fSpectrumOut; // out of plane jet pt spectrum
378 TH1D* fDptInDist; // in plane dpt distribution
379 TH1D* fDptOutDist; // out of plane dpt distribution
380 TH2D* fDptIn; // in plane dpt matrix
381 TH2D* fDptOut; // out plane dpt matrix
382 TH2D* fFullResponseIn; // full response matrix, in plane
383 TH2D* fFullResponseOut; // full response matrix, out of plane
384 // copy and assignment
385 AliJetFlowTools(const AliJetFlowTools&); // not implemented
386 AliJetFlowTools& operator=(const AliJetFlowTools&); // not implemented
389 //_____________________________________________________________________________