]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGJE/EMCALJetTasks/UserTasks/AliAnalysisTaskJetV2.cxx
Merge branch 'master' of https://git.cern.ch/reps/AliRoot
[u/mrichter/AliRoot.git] / PWGJE / EMCALJetTasks / UserTasks / AliAnalysisTaskJetV2.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
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  **************************************************************************/
15
16 /* 
17  * Jet V2 task
18  *
19  * this task is part of the emcal jet framework and should be run in the emcaljet train
20  * the following extensions to an accepted AliVEvent are expected:
21  *      - (anti-kt) jets
22  *      - background estimate rho
23  *      - pico tracks
24  *      aod's and esd's are handled transparently
25  * the task will attempt to estimate a phi-dependent background density rho 
26  * by fitting vn harmonics to the dpt/dphi distribution
27  *
28  * author: Redmer Alexander Bertens, Utrecht Univeristy, Utrecht, Netherlands
29  * rbertens@cern.ch, rbertens@nikhef.nl, r.a.bertens@uu.nl 
30  */
31
32 // root includes
33 #include <TStyle.h>
34 #include <TRandom3.h>
35 #include <TChain.h>
36 #include <TMath.h>
37 #include <TF1.h>
38 #include <TF2.h>
39 #include <TH1F.h>
40 #include <TH2F.h>
41 #include <TH3F.h>
42 #include <TProfile.h>
43 #include <TFile.h>
44 // aliroot includes
45 #include <AliAnalysisTask.h>
46 #include <AliAnalysisManager.h>
47 #include <AliCentrality.h>
48 #include <AliVVertex.h>
49 #include <AliVTrack.h>
50 #include <AliVVZERO.h>
51 #include <AliESDEvent.h>
52 #include <AliAODEvent.h>
53 #include <AliAODTrack.h>
54 #include <AliOADBContainer.h>
55 // emcal jet framework includes
56 #include <AliPicoTrack.h>
57 #include <AliEmcalJet.h>
58 #include <AliRhoParameter.h>
59 #include <AliLocalRhoParameter.h>
60 #include <AliAnalysisTaskJetV2.h>
61 #include <AliClusterContainer.h>
62
63 class AliAnalysisTaskJetV2;
64 using namespace std;
65
66 ClassImp(AliAnalysisTaskJetV2)
67
68 AliAnalysisTaskJetV2::AliAnalysisTaskJetV2() : AliAnalysisTaskEmcalJet("AliAnalysisTaskJetV2", kTRUE), 
69     fDebug(0), fRunToyMC(kFALSE), fLocalInit(0), fAttachToEvent(kTRUE), fFillHistograms(kTRUE), fFillQAHistograms(kTRUE), fReduceBinsXByFactor(-1.), fReduceBinsYByFactor(-1.), fNoEventWeightsForQC(kTRUE), fCentralityClasses(0), fExpectedRuns(0), fExpectedSemiGoodRuns(0), fUserSuppliedV2(0), fUserSuppliedV3(0), fUserSuppliedR2(0), fUserSuppliedR3(0), fTracksCont(0), fClusterCont(0), fJetsCont(0), fLeadingJet(0), fLeadingJetAfterSub(0), fNAcceptedTracks(0), fNAcceptedTracksQCn(0), fFitModulationType(kNoFit), fFitGoodnessTest(kChi2Poisson), fQCRecovery(kTryFit), fUsePtWeight(kTRUE), fUsePtWeightErrorPropagation(kTRUE), fDetectorType(kVZEROComb), fAnalysisType(kCharged), fFitModulationOptions("QWLI"), fRunModeType(kGrid), fDataType(kESD), fCollisionType(kPbPb), fRandom(0), fRunNumber(-1), fMappedRunNumber(0), fInCentralitySelection(-1), fFitModulation(0), fFitControl(0), fMinPvalue(0.01), fMaxPvalue(1), fNameSmallRho(""), fCachedRho(0), fSoftTrackMinPt(0.15), fSoftTrackMaxPt(5.), fSemiGoodJetMinPhi(0.), fSemiGoodJetMaxPhi(4.), fSemiGoodTrackMinPhi(0.), fSemiGoodTrackMaxPhi(4.), fAbsVertexZ(10), fHistCentrality(0), fHistVertexz(0), fHistRunnumbersPhi(0), fHistRunnumbersEta(0), fHistPvalueCDFROOT(0), fHistPvalueCDFROOTCent(0), fHistChi2ROOTCent(0), fHistPChi2Root(0),  fHistPvalueCDF(0), fHistPvalueCDFCent(0), fHistChi2Cent(0), fHistPChi2(0), fHistKolmogorovTest(0), fHistKolmogorovTestCent(0), fHistPKolmogorov(0), fHistRhoStatusCent(0), fHistUndeterminedRunQA(0), fMinDisanceRCtoLJ(0), fMaxCones(-1), fExcludeLeadingJetsFromFit(1.), fRebinSwapHistoOnTheFly(kTRUE), fPercentageOfFits(10.), fOutputList(0), fOutputListGood(0), fOutputListBad(0), fHistAnalysisSummary(0), fHistSwap(0), fProfV2(0), fProfV2Cumulant(0), fProfV3(0), fProfV3Cumulant(0), fHistPsiControl(0), fHistPsiSpread(0), fHistPsiVZEROA(0), fHistPsiVZEROC(0), fHistPsiVZERO(0), fHistPsiTPC(0), fHistPsiVZEROAV0M(0), fHistPsiVZEROCV0M(0), fHistPsiVZEROVV0M(0), fHistPsiTPCV0M(0), fHistPsiVZEROATRK(0), fHistPsiVZEROCTRK(0), fHistPsiVZEROTRK(0), fHistPsiTPCTRK(0), fHistRhoVsMult(0), fHistRhoVsCent(0), fHistRhoAVsMult(0), fHistRhoAVsCent(0), fVZEROgainEqualization(0x0), fVZEROgainEqualizationPerRing(kFALSE), fChi2A(0x0), fChi2C(0x0), fChi3A(0x0), fChi3C(0x0), fOADB(0x0)
70 {
71     for(Int_t i(0); i < 10; i++) {
72         fProfV2Resolution[i] = 0;
73         fProfV3Resolution[i] = 0;
74         fHistPicoTrackPt[i] = 0;
75         fHistPicoTrackMult[i] = 0;
76         fHistPicoCat1[i] = 0;
77         fHistPicoCat2[i] = 0;
78         fHistPicoCat3[i] = 0;
79         fHistClusterPt[i] = 0;
80         fHistClusterEtaPhi[i] = 0;
81         fHistClusterEtaPhiWeighted[i] = 0;
82         fHistPsiTPCLeadingJet[i] = 0;
83         fHistPsiVZEROALeadingJet[i] = 0;  
84         fHistPsiVZEROCLeadingJet[i] = 0;
85         fHistPsiVZEROCombLeadingJet[i] = 0;
86         fHistPsi2Correlation[i] = 0;
87         fHistLeadingJetBackground[i] = 0;
88         fHistRhoPackage[i] = 0;
89         fHistRho[i] = 0;
90         fHistRCPhiEta[i] = 0;
91         fHistRhoVsRCPt[i] = 0;
92         fHistRCPt[i] = 0;
93         fHistDeltaPtDeltaPhi2[i] = 0;
94         fHistDeltaPtDeltaPhi2Rho0[i] = 0;
95         fHistRCPhiEtaExLJ[i] = 0;
96         fHistRhoVsRCPtExLJ[i] = 0;
97         fHistRCPtExLJ[i] = 0;
98         fHistDeltaPtDeltaPhi2ExLJ[i] = 0;
99         fHistDeltaPtDeltaPhi2ExLJRho0[i] = 0;
100         fHistJetPtRaw[i] = 0;
101         fHistJetPt[i] = 0;
102         fHistJetEtaPhi[i] = 0;
103         fHistJetPtArea[i] = 0;
104         fHistJetPtEta[i] = 0;
105         fHistJetPtConstituents[i] = 0;
106         fHistJetEtaRho[i] = 0;
107         fHistJetPsi2Pt[i] = 0;
108         fHistJetPsi2PtRho0[i] = 0;
109    }
110    for(Int_t i(0); i < 9; i++) {
111        for(Int_t j(0); j < 2; j++) {
112            for(Int_t k(0); k < 2; k++) {
113                fMeanQ[i][j][k] = 0.; 
114                fWidthQ[i][j][k] = 0.;  
115                fMeanQv3[i][j][k] = 0.; 
116                fWidthQv3[i][j][k] = 0.;
117            }
118        }
119    }
120    for(Int_t i(0); i < 4; i++) {
121        fVZEROApol[i] = 0.;
122        fVZEROCpol[i] = 0.;
123    }
124    for(Int_t i(0); i < 8; i++) fUseVZERORing[i] = kTRUE;
125    // default constructor
126 }
127 //_____________________________________________________________________________
128 AliAnalysisTaskJetV2::AliAnalysisTaskJetV2(const char* name, runModeType type) : AliAnalysisTaskEmcalJet(name, kTRUE),
129   fDebug(0), fRunToyMC(kFALSE), fLocalInit(0), fAttachToEvent(kTRUE), fFillHistograms(kTRUE), fFillQAHistograms(kTRUE), fReduceBinsXByFactor(-1.), fReduceBinsYByFactor(-1.), fNoEventWeightsForQC(kTRUE), fCentralityClasses(0), fExpectedRuns(0), fExpectedSemiGoodRuns(0), fUserSuppliedV2(0), fUserSuppliedV3(0), fUserSuppliedR2(0), fUserSuppliedR3(0), fTracksCont(0), fClusterCont(0), fJetsCont(0), fLeadingJet(0), fLeadingJetAfterSub(0), fNAcceptedTracks(0), fNAcceptedTracksQCn(0), fFitModulationType(kNoFit), fFitGoodnessTest(kChi2Poisson), fQCRecovery(kTryFit), fUsePtWeight(kTRUE), fUsePtWeightErrorPropagation(kTRUE), fDetectorType(kVZEROComb), fAnalysisType(kCharged), fFitModulationOptions("QWLI"), fRunModeType(type), fDataType(kESD), fCollisionType(kPbPb), fRandom(0), fRunNumber(-1), fMappedRunNumber(0), fInCentralitySelection(-1), fFitModulation(0), fFitControl(0), fMinPvalue(0.01), fMaxPvalue(1), fNameSmallRho(""), fCachedRho(0), fSoftTrackMinPt(0.15), fSoftTrackMaxPt(5.), fSemiGoodJetMinPhi(0.), fSemiGoodJetMaxPhi(4.), fSemiGoodTrackMinPhi(0.), fSemiGoodTrackMaxPhi(4.), fAbsVertexZ(10), fHistCentrality(0), fHistVertexz(0), fHistRunnumbersPhi(0), fHistRunnumbersEta(0), fHistPvalueCDFROOT(0), fHistPvalueCDFROOTCent(0), fHistChi2ROOTCent(0), fHistPChi2Root(0),  fHistPvalueCDF(0), fHistPvalueCDFCent(0), fHistChi2Cent(0), fHistPChi2(0), fHistKolmogorovTest(0), fHistKolmogorovTestCent(0), fHistPKolmogorov(0), fHistRhoStatusCent(0), fHistUndeterminedRunQA(0), fMinDisanceRCtoLJ(0), fMaxCones(-1), fExcludeLeadingJetsFromFit(1.), fRebinSwapHistoOnTheFly(kTRUE), fPercentageOfFits(10.), fOutputList(0), fOutputListGood(0), fOutputListBad(0), fHistAnalysisSummary(0), fHistSwap(0), fProfV2(0), fProfV2Cumulant(0), fProfV3(0), fProfV3Cumulant(0), fHistPsiControl(0), fHistPsiSpread(0), fHistPsiVZEROA(0), fHistPsiVZEROC(0), fHistPsiVZERO(0), fHistPsiTPC(0), fHistPsiVZEROAV0M(0), fHistPsiVZEROCV0M(0), fHistPsiVZEROVV0M(0), fHistPsiTPCV0M(0), fHistPsiVZEROATRK(0), fHistPsiVZEROCTRK(0), fHistPsiVZEROTRK(0), fHistPsiTPCTRK(0), fHistRhoVsMult(0), fHistRhoVsCent(0), fHistRhoAVsMult(0), fHistRhoAVsCent(0), fVZEROgainEqualization(0x0), fVZEROgainEqualizationPerRing(kFALSE), fChi2A(0x0), fChi2C(0x0), fChi3A(0x0), fChi3C(0x0), fOADB(0x0)
130 {
131     for(Int_t i(0); i < 10; i++) {
132         fProfV2Resolution[i] = 0;
133         fProfV3Resolution[i] = 0;
134         fHistPicoTrackPt[i] = 0;
135         fHistPicoTrackMult[i] = 0;
136         fHistPicoCat1[i] = 0;
137         fHistPicoCat2[i] = 0;
138         fHistPicoCat3[i] = 0;
139         fHistClusterPt[i] = 0;
140         fHistClusterEtaPhi[i] = 0;
141         fHistClusterEtaPhiWeighted[i] = 0;
142         fHistPsiTPCLeadingJet[i] = 0;
143         fHistPsiVZEROALeadingJet[i] = 0;  
144         fHistPsiVZEROCLeadingJet[i] = 0;
145         fHistPsiVZEROCombLeadingJet[i] = 0;
146         fHistPsi2Correlation[i] = 0;
147         fHistLeadingJetBackground[i] = 0;
148         fHistRhoPackage[i] = 0;
149         fHistRho[i] = 0;
150         fHistRCPhiEta[i] = 0;
151         fHistRhoVsRCPt[i] = 0;
152         fHistRCPt[i] = 0;
153         fHistDeltaPtDeltaPhi2[i] = 0;
154         fHistDeltaPtDeltaPhi2Rho0[i] = 0;
155         fHistRCPhiEtaExLJ[i] = 0;
156         fHistRhoVsRCPtExLJ[i] = 0;
157         fHistRCPtExLJ[i] = 0;
158         fHistDeltaPtDeltaPhi2ExLJ[i] = 0;
159         fHistDeltaPtDeltaPhi2ExLJRho0[i] = 0;
160         fHistJetPtRaw[i] = 0;
161         fHistJetPt[i] = 0;
162         fHistJetEtaPhi[i] = 0;
163         fHistJetPtArea[i] = 0;
164         fHistJetPtEta[i] = 0;
165         fHistJetPtConstituents[i] = 0;
166         fHistJetEtaRho[i] = 0;
167         fHistJetPsi2Pt[i] = 0;
168         fHistJetPsi2PtRho0[i] = 0;
169    }
170    for(Int_t i(0); i < 9; i++) {
171        for(Int_t j(0); j < 2; j++) {
172            for(Int_t k(0); k < 2; k++) {
173                fMeanQ[i][j][k] = 0.; 
174                fWidthQ[i][j][k] = 0.;  
175                fMeanQv3[i][j][k] = 0.; 
176                fWidthQv3[i][j][k] = 0.;
177            }
178        }
179    }
180    for(Int_t i(0); i < 4; i++) {
181        fVZEROApol[i] = 0.;
182        fVZEROCpol[i] = 0.;
183    }
184    for(Int_t i(0); i < 8; i++) fUseVZERORing[i] = kTRUE;
185
186     // constructor
187     DefineInput(0, TChain::Class());
188     DefineOutput(1, TList::Class());
189     switch (fRunModeType) {
190         case kLocal : {
191             gStyle->SetOptFit(1);
192             DefineOutput(2, TList::Class());
193             DefineOutput(3, TList::Class());
194         } break;
195         default: fDebug = -1;   // suppress debug info explicitely when not running locally
196     }
197     switch (fCollisionType) {
198         case kPythia : {
199             fFitModulationType = kNoFit;
200         } break;
201         default : break;
202     }
203     if(fLocalRhoName=="") fLocalRhoName = Form("LocalRhoFrom_%s", GetName());
204 }
205 //_____________________________________________________________________________
206 AliAnalysisTaskJetV2::~AliAnalysisTaskJetV2()
207 {
208     // destructor
209     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
210     if(fOutputList)             {delete fOutputList;            fOutputList = 0x0;}
211     if(fOutputListGood)         {delete fOutputListGood;        fOutputListGood = 0x0;}
212     if(fOutputListBad)          {delete fOutputListBad;         fOutputListBad = 0x0;}
213     if(fFitModulation)          {delete fFitModulation;         fFitModulation = 0x0;}
214     if(fHistSwap)               {delete fHistSwap;              fHistSwap = 0x0;}
215     if(fCentralityClasses)      {delete fCentralityClasses;     fCentralityClasses = 0x0;}
216     if(fExpectedRuns)           {delete fExpectedRuns;          fExpectedRuns = 0x0;}
217     if(fExpectedSemiGoodRuns)   {delete fExpectedSemiGoodRuns;  fExpectedSemiGoodRuns = 0x0;}
218     if(fFitControl)             {delete fFitControl;            fFitControl = 0x0;}
219     if(fVZEROgainEqualization)  {delete fVZEROgainEqualization; fVZEROgainEqualization = 0x0;}
220     if(fChi2A)                  {delete fChi2A;                 fChi2A = 0x0;}
221     if(fChi2C)                  {delete fChi2C;                 fChi2C = 0x0;}
222     if(fChi3A)                  {delete fChi3A;                 fChi3A = 0x0;}
223     if(fChi3C)                  {delete fChi3C;                 fChi3C = 0x0;}
224     if(fOADB && !fOADB->IsZombie()) {
225         fOADB->Close();        fOADB = 0x0;
226     } else if (fOADB) fOADB = 0x0;
227 }
228 //_____________________________________________________________________________
229 void AliAnalysisTaskJetV2::ExecOnce()
230 {
231     // Init the analysis
232     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
233     fLocalRho = new AliLocalRhoParameter(fLocalRhoName.Data(), 0); 
234     if(fAttachToEvent) {
235         if(!(InputEvent()->FindListObject(fLocalRho->GetName()))) {
236             InputEvent()->AddObject(fLocalRho);
237         } else {
238             AliFatal(Form("%s: Container with name %s already present. Aborting", GetName(), fLocalRho->GetName()));
239         }
240     }
241     AliAnalysisTaskEmcalJet::ExecOnce();        // init the base class
242     AliAnalysisTaskEmcalJet::SetVzRange(-1.*fAbsVertexZ, fAbsVertexZ);
243     if(!GetJetContainer()) AliFatal(Form("%s: Couldn't find jet container. Aborting !", GetName()));
244 }
245 //_____________________________________________________________________________
246 Bool_t AliAnalysisTaskJetV2::Notify()
247 {
248     // determine the run number to see if the track and jet cuts should be refreshed for semi-good TPC runs
249     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
250     if(fRunNumber != InputEvent()->GetRunNumber()) {
251         fRunNumber = InputEvent()->GetRunNumber();        // set the current run number
252         if(fDebug > 0) printf("__FUNC__ %s > NEW RUNNUMBER DETECTED \n ", __func__);
253         // check if this is 10h or 11h data
254         switch (fCollisionType) {
255             case kPbPb10h : {
256                 if(fDebug > 0) printf(" LHC10h data, assuming full acceptance, reading VZERO calibration DB \n ");
257                 // for 10h data the vzero event plane calibration needs to be cached
258                 ReadVZEROCalibration2010h(); 
259                 // no need to change rho or acceptance for 10h, so we're done
260                 return kTRUE;
261             } break;
262             case kJetFlowMC : {
263                 return kTRUE;
264             } break;
265             default :  {
266                 if(fDebug > 0) printf(" checking runnumber to adjust acceptance on the fly \n");           
267             } break;
268         }
269         // reset the cuts. should be a pointless operation except for the case where the run number changes
270         // from semi-good back to good on one node, which is not a likely scenario (unless trains will
271         // run as one masterjob)
272         switch (fAnalysisType) {
273             case kCharged: {
274                 AliAnalysisTaskEmcalJet::SetJetPhiLimits(-10., 10.);   
275             } break;
276             case kFull: {
277                 AliAnalysisTaskEmcalJet::SetJetPhiLimits(1.405 + GetJetRadius(), 3.135 - GetJetRadius());
278             } break;
279             default: {
280                 AliAnalysisTaskEmcal::SetTrackPhiLimits(-10., 10.);
281             } break;
282         }
283         if(fCachedRho) {                // if there's a cached rho, it's the default, so switch back
284             if(fDebug > 0) printf("__FUNC__ %s > replacing rho with cached rho \n ", __func__);
285             fRho = fCachedRho;          // reset rho back to cached value. again, should be pointless
286         }
287         Bool_t flaggedAsSemiGood(kFALSE);       // not flagged as anything
288         for(Int_t i(0); i < fExpectedSemiGoodRuns->GetSize(); i++) {
289             if(fExpectedSemiGoodRuns->At(i) == fRunNumber) { // run is semi-good
290                if(fDebug > 0) printf("__FUNC__ %s > semi-good tpc run detected, adjusting acceptance \n ", __func__);
291                 flaggedAsSemiGood = kTRUE;
292                 switch (fAnalysisType) {
293                     // for full jets the jet acceptance does not have to be changed as emcal does not
294                     // cover the tpc low voltage readout strips
295                     case kCharged: {
296                         AliAnalysisTaskEmcalJet::SetJetPhiLimits(fSemiGoodJetMinPhi, fSemiGoodJetMaxPhi);       // just an acceptance cut, jets are obtained from full azimuth, so no edge effects
297                     } break;
298                     default: break;
299                 }
300                 AliAnalysisTaskEmcal::SetTrackPhiLimits(fSemiGoodTrackMinPhi, fSemiGoodTrackMaxPhi);    // only affects vn extraction, NOT jet finding
301                 // for semi-good runs, also try to get the 'small rho' estimate, if it is available
302                 AliRhoParameter* tempRho(dynamic_cast<AliRhoParameter*>(InputEvent()->FindListObject(fNameSmallRho.Data())));
303                 if(tempRho) {
304                     if(fDebug > 0) printf("__FUNC__ %s > switching to small rho, caching normal rho \n ", __func__);
305                     fHistAnalysisSummary->SetBinContent(54, 1.);        // bookkeep the fact that small rho is used
306                     fCachedRho = fRho;          // cache the original rho ...
307                     fRho = tempRho;             // ... and use the small rho
308                 }
309             }
310         }
311         if(!flaggedAsSemiGood) {
312             // in case the run is not a semi-good run, check if it is recognized as another run
313             // only done to catch unexpected runs
314             for(Int_t i(0); i < fExpectedRuns->GetSize(); i++) {
315                 if(fExpectedRuns->At(i) == fRunNumber) break; // run is known, break the loop else store the number in a random bin
316                 fHistUndeterminedRunQA->SetBinContent(TMath::Nint(10.*gRandom->Uniform(0.,.9))+1, fRunNumber);
317             }
318             fHistAnalysisSummary->SetBinContent(53, 1.);                // bookkeep which rho estimate is used 
319         }
320     }
321     return kTRUE;
322 }
323 //_____________________________________________________________________________
324 Bool_t AliAnalysisTaskJetV2::InitializeAnalysis() 
325 {
326     // initialize the anaysis
327     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
328     // if not set, estimate the number of cones that would fit into the selected acceptance
329     if(fMaxCones <= 0) fMaxCones = TMath::CeilNint((TMath::Abs(GetJetContainer()->GetJetEtaMax()-GetJetContainer()->GetJetEtaMin())*TMath::Abs(GetJetContainer()->GetJetPhiMax()-GetJetContainer()->GetJetPhiMin()))/(TMath::Pi()*GetJetRadius()*GetJetRadius()));
330     // manually 'override' the default acceptance cuts of the emcal framework (use with caution) 
331     if(fMinDisanceRCtoLJ==0) fMinDisanceRCtoLJ = GetJetRadius();
332     if(dynamic_cast<AliAODEvent*>(InputEvent())) fDataType = kAOD; // determine the datatype
333     else if(dynamic_cast<AliESDEvent*>(InputEvent())) fDataType = kESD;
334     fHistAnalysisSummary->SetBinContent(36, (int)fDataType);
335     if(!fRandom) fRandom = new TRandom3(0);  // set randomizer and random seed
336     switch (fFitModulationType)  {
337         case kNoFit : { SetModulationFit(new TF1("fix_kNoFit", "[0]", 0, TMath::TwoPi())); } break;
338         case kV2 : {
339             SetModulationFit(new TF1("fit_kV2", "[0]*([1]+[2]*[3]*TMath::Cos([2]*(x-[4])))", 0, TMath::TwoPi()));
340             fFitModulation->SetParameter(0, 0.);        // normalization
341             fFitModulation->SetParameter(3, 0.2);       // v2
342             fFitModulation->FixParameter(1, 1.);        // constant
343             fFitModulation->FixParameter(2, 2.);        // constant
344         } break;
345         case kV3: {
346             SetModulationFit(new TF1("fit_kV3", "[0]*([1]+[2]*[3]*TMath::Cos([2]*(x-[4])))", 0, TMath::TwoPi()));
347             fFitModulation->SetParameter(0, 0.);        // normalization
348             fFitModulation->SetParameter(3, 0.2);       // v3
349             fFitModulation->FixParameter(1, 1.);        // constant
350             fFitModulation->FixParameter(2, 3.);        // constant
351         } break;
352         default : { // for the combined fit, the 'direct fourier series' or the user supplied vn values we use v2 and v3
353              SetModulationFit(new TF1("fit_kCombined", "[0]*([1]+[2]*([3]*TMath::Cos([2]*(x-[4]))+[7]*TMath::Cos([5]*(x-[6]))))", 0, TMath::TwoPi()));
354              fFitModulation->SetParameter(0, 0.);       // normalization
355              fFitModulation->SetParameter(3, 0.2);      // v2
356              fFitModulation->FixParameter(1, 1.);       // constant
357              fFitModulation->FixParameter(2, 2.);       // constant
358              fFitModulation->FixParameter(5, 3.);       // constant
359              fFitModulation->SetParameter(7, 0.2);      // v3
360         } break;
361     }
362     switch (fRunModeType) {
363         case kGrid : { fFitModulationOptions += "N0"; } break;
364         default : break;
365     }
366     FillAnalysisSummaryHistogram();
367     return kTRUE;
368 }
369 //_____________________________________________________________________________
370 TH1F* AliAnalysisTaskJetV2::BookTH1F(const char* name, const char* x, Int_t bins, Double_t min, Double_t max, Int_t c, Bool_t append)
371 {
372     // book a TH1F and connect it to the output container
373     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
374     if(fReduceBinsXByFactor > 0 ) bins = TMath::Nint(bins/fReduceBinsXByFactor);
375     if(!fOutputList) return 0x0;
376     TString title(name);
377     if(c!=-1) { // format centrality dependent histograms accordingly
378         name = Form("%s_%i", name, c);
379         title += Form("_%i-%i", (int)(fCentralityClasses->At(c)), (int)(fCentralityClasses->At((1+c))));
380     }
381     title += Form(";%s;[counts]", x);
382     TH1F* histogram = new TH1F(name, title.Data(), bins, min, max);
383     histogram->Sumw2();
384     if(append) fOutputList->Add(histogram);
385     return histogram;   
386 }
387 //_____________________________________________________________________________
388 TH2F* AliAnalysisTaskJetV2::BookTH2F(const char* name, const char* x, const char* y, Int_t binsx, Double_t minx, Double_t maxx, Int_t binsy, Double_t miny, Double_t maxy, Int_t c, Bool_t append)
389 {
390     // book a TH2F and connect it to the output container
391     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
392     if(fReduceBinsXByFactor > 0 ) binsx = TMath::Nint(binsx/fReduceBinsXByFactor);
393     if(fReduceBinsYByFactor > 0 ) binsy = TMath::Nint(binsy/fReduceBinsYByFactor);
394     if(!fOutputList) return 0x0;
395     TString title(name);
396     if(c!=-1) { // format centrality dependent histograms accordingly
397         name = Form("%s_%i", name, c);
398         title += Form("_%i-%i", (int)fCentralityClasses->At(c), (int)(fCentralityClasses->At((1+c))));
399     }
400     title += Form(";%s;%s", x, y);
401     TH2F* histogram = new TH2F(name, title.Data(), binsx, minx, maxx, binsy, miny, maxy);
402     histogram->Sumw2();
403     if(append) fOutputList->Add(histogram);
404     return histogram;   
405 }
406 //_____________________________________________________________________________
407 TH3F* AliAnalysisTaskJetV2::BookTH3F(const char* name, const char* x, const char* y, const char* z, Int_t binsx, Double_t minx, Double_t maxx, Int_t binsy, Double_t miny, Double_t maxy, Int_t binsz, Double_t minz, Double_t maxz, Int_t c, Bool_t append)
408 {
409     // book a TH2F and connect it to the output container
410     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
411     if(fReduceBinsXByFactor > 0 ) {
412         binsx = TMath::Nint(binsx/fReduceBinsXByFactor);
413         binsy = TMath::Nint(binsy/fReduceBinsXByFactor);
414         binsz = TMath::Nint(binsz/fReduceBinsXByFactor);
415     }
416     if(!fOutputList) return 0x0;
417     TString title(name);
418     if(c!=-1) { // format centrality dependent histograms accordingly
419         name = Form("%s_%i", name, c);
420         title += Form("_%i-%i", (int)fCentralityClasses->At(c), (int)(fCentralityClasses->At((1+c))));
421     }
422     title += Form(";%s;%s;%s", x, y, z);
423     TH3F* histogram = new TH3F(name, title.Data(), binsx, minx, maxx, binsy, miny, maxy, binsz, minz, maxz);
424     histogram->Sumw2();
425     if(append) fOutputList->Add(histogram);
426     return histogram;   
427 }
428 //_____________________________________________________________________________
429 void AliAnalysisTaskJetV2::UserCreateOutputObjects()
430 {
431     // create output objects. also initializes some default values in case they aren't 
432     // loaded via the AddTask macro
433     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
434     fOutputList = new TList();
435     fOutputList->SetOwner(kTRUE);
436     if(!fCentralityClasses) {   // classes must be defined at this point
437         Double_t c[] = {0., 20., 40., 60., 80., 100.};
438         fCentralityClasses = new TArrayD(sizeof(c)/sizeof(c[0]), c);
439     }
440     if(!fExpectedRuns) {        // expected runs must be defined at this point
441         Int_t r[] =  {167813, 167988, 168066, 168068, 168069, 168076, 168104, 168212, 168311, 168322, 168325, 168341, 168361, 168362, 168458, 168460, 168461, 168992, 169091, 169094, 169138, 169143, 169167, 169417, 169835, 169837, 169838, 169846, 169855, 169858, 169859, 169923, 169956, 170027, 170036, 170081, /* up till here original good TPC list */169975, 169981, 170038, 170040, 170083, 170084, 170085, 170088, 170089, 170091, 170152, 170155, 170159, 170163, 170193, 170195, 170203, 170204, 170205, 170228, 170230, 170264, 170268, 170269, 170270, 170306, 170308, 170309, /* original semi-good tpc list */169415, 169411, 169035, 168988, 168984, 168826, 168777, 168512, 168511, 168467, 168464, 168342, 168310, 168115, 168108, 168107, 167987, 167915, 167903, /*new runs, good according to RCT */ 169238, 169160, 169156, 169148, 169145, 169144 /* run swith missing OROC 8 but seem ok in QA */};
442         fExpectedRuns = new TArrayI(sizeof(r)/sizeof(r[0]), r);
443     }
444     // set default semi-good runs only for 11h data
445     switch (fCollisionType) {
446         case kPbPb10h : break;
447         default : {
448             if(!fExpectedSemiGoodRuns) {
449                 Int_t r[] = {169975, 169981, 170038, 170040, 170083, 170084, 170085, 170088, 170089, 170091, 170152, 170155, 170159, 170163, 170193, 170195, 170203, 170204, 170205, 170228, 170230, 170264, 170268, 170269, 170270, 170306, 170308, 170309};
450                 fExpectedSemiGoodRuns = new TArrayI(sizeof(r)/sizeof(r[0]), r);
451             }
452         }
453     }
454
455     // global QA
456     fHistCentrality =           BookTH1F("fHistCentrality", "centrality", 102, -2, 100);
457     fHistVertexz =              BookTH1F("fHistVertexz", "vertex z (cm)", 100, -12, 12);
458
459     // for some histograms adjust the bounds according to analysis acceptance
460     Double_t etaMin(-1.), etaMax(1.), phiMin(0.), phiMax(TMath::TwoPi());
461     switch (fAnalysisType) {
462         case kFull : {
463            etaMin = -.7;       
464            etaMax = .7;
465            phiMin = 1.405;
466            phiMax = 3.135;
467         } break;
468         default : break;
469     }
470
471     // pico track and emcal cluster kinematics
472     for(Int_t i(0); i < fCentralityClasses->GetSize()-1; i++) { 
473         fHistPicoTrackPt[i] =           BookTH1F("fHistPicoTrackPt", "p_{t} [GeV/c]", 100, 0, 100, i);
474         fHistPicoTrackMult[i] =         BookTH1F("fHistPicoTrackMult", "multiplicity", 100, 0, 5000, i);
475         if(fFillQAHistograms) {
476             fHistPicoCat1[i] =          BookTH2F("fHistPicoCat1", "#eta", "#phi", 50, -1, 1, 50, 0, TMath::TwoPi(), i);
477             fHistPicoCat2[i] =          BookTH2F("fHistPicoCat2", "#eta", "#phi", 50, -1, 1, 50, 0, TMath::TwoPi(), i);
478             fHistPicoCat3[i] =          BookTH2F("fHistPicoCat3", "#eta", "#phi", 50, -1, 1, 50, 0, TMath::TwoPi(), i);
479             if(fAnalysisType == AliAnalysisTaskJetV2::kFull) {
480                 fHistClusterPt[i] =     BookTH1F("fHistClusterPt", "p_{t} [GeV/c]", 100, 0, 100, i);
481                 fHistClusterEtaPhi[i] = BookTH2F("fHistClusterEtaPhi", "#eta", "#phi", 100, etaMax, etaMax, 100, phiMin, phiMax, i);
482                 fHistClusterEtaPhiWeighted[i] = BookTH2F("fHistClusterEtaPhiWeighted", "#eta", "#phi", 100, etaMin, etaMax, 100, phiMin, phiMax, i);
483             }
484             fHistPsiTPCLeadingJet[i] =          BookTH3F("fHistPsiTPCLeadingJet", "p_{t} [GeV/c]", "#Psi_{TPC}", "#varphi_{jet}", 70, 0, 210, 50, -1.*TMath::Pi()/2., TMath::Pi()/2., 50, phiMin, phiMax, i);
485             fHistPsiVZEROALeadingJet[i] =       BookTH3F("fHistPsiVZEROALeadingJet", "p_{t} [GeV/c]", "#Psi_{VZEROA}", "#varphi_{jet}", 70, 0, 210, 50, -1.*TMath::Pi()/2., TMath::Pi()/2., 50, phiMin, phiMax, i);
486             fHistPsiVZEROCLeadingJet[i] =       BookTH3F("fHistPsiVZEROCLeadingJet", "p_{t} [GeV/c]", "#Psi_{VZEROC}", "#varphi_{jet}", 70, 0, 210, 50, -1.*TMath::Pi()/2., TMath::Pi()/2., 50, phiMin, phiMax, i);
487             fHistPsiVZEROCombLeadingJet[i] =    BookTH3F("fHistPsiVZEROCombLeadingJet", "p_{t} [GeV/c]", "#Psi_{VZEROComb}", "#varphi_{jet}", 70, 0, 210, 50, -1.*TMath::Pi()/2., TMath::Pi()/2., 50, phiMin, phiMax, i);
488             fHistPsi2Correlation[i] = BookTH3F("fHistPsi2Correlation", "#Psi_{TPC}", "#Psi_{VZEROA}", "#Psi_{VZEROC}",  20, -1.*TMath::Pi()/2., TMath::Pi()/2., 20, -1.*TMath::Pi()/2., TMath::Pi()/2., 20, -1.*TMath::Pi()/2., TMath::Pi()/2., i);
489             fHistLeadingJetBackground[i] =      BookTH2F("fHistLeadingJetBackground", "#Delta #eta (leading jet with, without sub)", "Delta #varphi (leading jet with, without sub)", 50, -2., 2, 50, 0., TMath::TwoPi(), i);
490         }
491     }
492
493     if(fFillQAHistograms) {
494         // event plane estimates and quality
495         fHistPsiControl =           new TProfile("fHistPsiControl", "fHistPsiControl", 10, 0, 10);
496         fHistPsiControl->Sumw2();
497         fHistPsiSpread =            new TProfile("fHistPsiSpread", "fHistPsiSpread", 4, 0, 4);
498         fHistPsiSpread->Sumw2();
499         fHistPsiControl->GetXaxis()->SetBinLabel(1, "<#Psi_{2, VZEROA}>");
500         fHistPsiControl->GetXaxis()->SetBinLabel(2, "<#Psi_{2, VZEROC}>");
501         fHistPsiControl->GetXaxis()->SetBinLabel(3, "<#Psi_{2, TPC}>");
502         fHistPsiControl->GetXaxis()->SetBinLabel(4, "<#Psi_{2, TPC, #eta < 0}>");
503         fHistPsiControl->GetXaxis()->SetBinLabel(5, "<#Psi_{2, TPC, #eta > 0}>");
504         fHistPsiControl->GetXaxis()->SetBinLabel(6, "<#Psi_{3, VZEROA}>");
505         fHistPsiControl->GetXaxis()->SetBinLabel(7, "<#Psi_{3, VZEROC}>");
506         fHistPsiControl->GetXaxis()->SetBinLabel(8, "<#Psi_{3, TPC}>");
507         fHistPsiControl->GetXaxis()->SetBinLabel(9, "<#Psi_{3, TPC, #eta < 0}>");
508         fHistPsiControl->GetXaxis()->SetBinLabel(10, "<#Psi_{3, TPC, #eta > 0}>");
509         fHistPsiSpread->GetXaxis()->SetBinLabel(1, "<#Psi_{2, VZEROA} - #Psi_{2, VZEROC}>");
510         fHistPsiSpread->GetXaxis()->SetBinLabel(2, "<#Psi_{2, VZEROC} - #Psi_{2, TPC}>");
511         fHistPsiSpread->GetXaxis()->SetBinLabel(3, "<#Psi_{2, VZEROC} - #Psi_{2, TPC}>");
512         fHistPsiSpread->GetXaxis()->SetBinLabel(4, "<#Psi_{2, TPC, #eta < 0} - #Psi_{2, TPC, #eta > 0}>");
513         fOutputList->Add(fHistPsiControl);
514         fOutputList->Add(fHistPsiSpread);
515         fHistPsiVZEROA =            BookTH1F("fHistPsiVZEROA", "#Psi_{VZEROA}", 40, -.5*TMath::Pi(), .5*TMath::Pi());
516         fHistPsiVZEROC =            BookTH1F("fHistPsiVZEROC", "#Psi_{VZEROC}", 40, -.5*TMath::Pi(), .5*TMath::Pi());
517         fHistPsiVZERO =             BookTH1F("fHistPsiVZERO", "#Psi_{VZERO}", 40, -.5*TMath::Pi(), .5*TMath::Pi());
518         fHistPsiTPC =               BookTH1F("fHistPsiTPC", "#Psi_{TPC}", 40, -.5*TMath::Pi(), .5*TMath::Pi());
519         fHistPsiVZEROAV0M =         BookTH2F("fHistPsiVZEROAV0M", "V0M", "#Psi_{2, VZEROA}", 60, 0, 60, 40, -.5*TMath::Pi(), .5*TMath::Pi());
520         fHistPsiVZEROCV0M =         BookTH2F("fHistPsiVZEROCV0M", "V0M", "#Psi_{2, VZEROC}", 60, 0, 60, 40, -.5*TMath::Pi(), .5*TMath::Pi());
521         fHistPsiVZEROVV0M =         BookTH2F("fHistPsiVZEROV0M", "V0M", "#Psi_{2, VZERO}", 60, 0, 60, 40, -.5*TMath::Pi(), .5*TMath::Pi());
522         fHistPsiTPCV0M =            BookTH2F("fHistPsiTPCV0M", "V0M", "#Psi_{2, TRK}", 60, 0, 60, 40, -.5*TMath::Pi(), .5*TMath::Pi());
523         fHistPsiVZEROATRK =         BookTH2F("fHistPsiVZEROATRK", "TRK", "#Psi_{2, VZEROA}", 60, 0, 60, 40, -.5*TMath::Pi(), .5*TMath::Pi());
524         fHistPsiVZEROCTRK =         BookTH2F("fHistPsiVZEROCTRK", "TRK", "#Psi_{2, VZEROC}", 60, 0, 60, 40, -.5*TMath::Pi(), .5*TMath::Pi());
525         fHistPsiVZEROTRK =          BookTH2F("fHistPsiVZEROTRK", "TRK", "#Psi_{2, VZERO}", 60, 0, 60, 40, -.5*TMath::Pi(), .5*TMath::Pi());
526         fHistPsiTPCTRK =            BookTH2F("fHistPsiTPCTRK", "TRK", "#Psi_{2, TRK}", 60, 0, 60, 40, -.5*TMath::Pi(), .5*TMath::Pi());
527     }
528     // background
529     for(Int_t i(0); i < fCentralityClasses->GetSize()-1; i ++) {
530         fHistRhoPackage[i] =           BookTH1F("fHistRhoPackage",  "#rho [GeV/c]", 100, 0, 150, i);
531         fHistRho[i] =                  BookTH1F("fHistRho", "#rho [GeV/c]", 100, 0, 150, i);
532     }
533     fHistRhoVsMult =            BookTH2F("fHistRhoVsMult", "multiplicity", "#rho [GeV/c]", 100, 0, 4000, 100, 0, 250);
534     fHistRhoVsCent =            BookTH2F("fHistRhoVsCent", "centrality", "#rho [GeV/c]", 100, 0, 100, 100, 0, 250);
535     fHistRhoAVsMult =           BookTH2F("fHistRhoAVsMult", "multiplicity", "#rho * A (jet) [GeV/c]", 100, 0, 4000, 100, 0, 50);
536     fHistRhoAVsCent =           BookTH2F("fHistRhoAVsCent", "centrality", "#rho * A (jet) [GeV/c]", 100, 0, 100, 100, 0, 50);
537
538     TString detector("");
539     switch (fDetectorType) {
540         case kTPC : detector+="TPC";
541             break;
542         case kVZEROA : detector+="VZEROA";
543             break;
544         case kVZEROC : detector+="VZEROC";
545             break;
546         case kVZEROComb : detector+="VZEROComb";
547             break; 
548         case kFixedEP : detector+="FixedEP";
549             break;
550         default: break;
551     }
552     // delta pt distributions
553     for(Int_t i(0); i < fCentralityClasses->GetSize()-1; i ++) {
554         if(fFillQAHistograms)   fHistRCPhiEta[i] = BookTH2F("fHistRCPhiEta", "#phi (RC)", "#eta (RC)", 40, phiMin, phiMax, 40, etaMin, etaMax, i);
555         fHistRhoVsRCPt[i] =            BookTH2F("fHistRhoVsRCPt", "p_{t} (RC) [GeV/c]", "#rho * A (RC) [GeV/c]", 100, 0, 300, 100, 0, 350, i);
556         fHistRCPt[i] =                 BookTH1F("fHistRCPt", "p_{t} (RC) [GeV/c]", 130, -20, 150, i);
557         if(fFillQAHistograms)   fHistRCPhiEtaExLJ[i] = BookTH2F("fHistRCPhiEtaExLJ", "#phi (RC)", "#eta (RC)", 40, phiMin, phiMax, 40, etaMin, etaMax, i);
558         fHistDeltaPtDeltaPhi2[i] =  BookTH2F("fHistDeltaPtDeltaPhi2", Form("#phi - #Psi_{2, %s}", detector.Data()), "#delta p_{t} [GeV/c]", 40, 0, TMath::Pi(), 400, -70, 130, i);
559         fHistDeltaPtDeltaPhi2Rho0[i] =  BookTH2F("fHistDeltaPtDeltaPhi2Rho0", Form("#phi - #Psi_{2, %s}", detector.Data()), "#delta p_{t} [GeV/c]", 40, 0, TMath::Pi(), 400, -70, 130, i);
560         fHistRhoVsRCPtExLJ[i] =        BookTH2F("fHistRhoVsRCPtExLJ", "p_{t} (RC) [GeV/c]", "#rho * A (RC) [GeV/c]", 100, 0, 300, 100, 0, 350, i);
561         fHistRCPtExLJ[i] =             BookTH1F("fHistRCPtExLJ", "p_{t} (RC) [GeV/c]", 130, -20, 150, i);
562         fHistDeltaPtDeltaPhi2ExLJ[i] = BookTH2F("fHistDeltaPtDeltaPhi2ExLJ", Form("#phi - #Psi_{2, %s}", detector.Data()),  "#delta p_{t} [GeV/c]", 40, 0, TMath::Pi(), 400, -70, 130, i);
563         fHistDeltaPtDeltaPhi2ExLJRho0[i] = BookTH2F("fHistDeltaPtDeltaPhi2ExLJRho0", Form("#phi - #Psi_{2, %s}", detector.Data()),  "#delta p_{t} [GeV/c]", 40, 0, TMath::Pi(), 400, -70, 130, i);
564         // jet histograms (after kinematic cuts)
565         fHistJetPtRaw[i] =             BookTH1F("fHistJetPtRaw", "p_{t, jet} RAW [GeV/c]", 200, -50, 150, i);
566         fHistJetPt[i] =                BookTH1F("fHistJetPt", "p_{t, jet} [GeV/c]", 350, -100, 250, i);
567         if(fFillQAHistograms)   fHistJetEtaPhi[i] =            BookTH2F("fHistJetEtaPhi", "#eta", "#phi", 100, etaMin, etaMax, 100, phiMin, phiMax, i);
568         fHistJetPtArea[i] =            BookTH2F("fHistJetPtArea", "p_{t, jet} [GeV/c]", "Area", 175, -100, 250, 30, 0, 0.9, i);
569         fHistJetPtEta[i] =             BookTH2F("fHistJetPtEta", "p_{t, jet} [GeV/c]", "Eta", 175, -100, 250, 30, etaMin, etaMax, i);
570         fHistJetPtConstituents[i] =    BookTH2F("fHistJetPtConstituents", "p_{t, jet} [GeV/c]", "no. of constituents", 350, -100, 250, 60, 0, 150, i);
571         fHistJetEtaRho[i] =            BookTH2F("fHistJetEtaRho", "#eta", "#rho", 100, etaMin, etaMax, 100, 0, 300, i);
572         // in plane and out of plane spectra
573         fHistJetPsi2Pt[i] =            BookTH2F("fHistJetPsi2Pt", Form("#phi_{jet} - #Psi_{2, %s}", detector.Data()), "p_{t, jet} [GeV/c]", 40, 0., TMath::Pi(), 350, -100, 250, i);
574         fHistJetPsi2PtRho0[i] =        BookTH2F("fHistJetPsi2PtRho0", Form("#phi_{jet} - #Psi_{2, %s}", detector.Data()), "p_{t, jet} [GeV/c]", 40, 0., TMath::Pi(), 350, -100, 250, i);
575         // profiles for all correlator permutations which are necessary to calculate each second and third order event plane resolution
576         fProfV2Resolution[i] = new TProfile(Form("fProfV2Resolution_%i", i), Form("fProfV2Resolution_%i", i), 11, -0.5, 10.5);
577         fProfV2Resolution[i]->GetXaxis()->SetBinLabel(3, "<cos(2(#Psi_{VZEROA} - #Psi_{VZEROC}))>");
578         fProfV2Resolution[i]->GetXaxis()->SetBinLabel(4, "<cos(2(#Psi_{VZEROC} - #Psi_{VZEROA}))>");
579         fProfV2Resolution[i]->GetXaxis()->SetBinLabel(5, "<cos(2(#Psi_{VZEROA} - #Psi_{TPC}))>");
580         fProfV2Resolution[i]->GetXaxis()->SetBinLabel(6, "<cos(2(#Psi_{TPC} - #Psi_{VZEROA}))>");
581         fProfV2Resolution[i]->GetXaxis()->SetBinLabel(7, "<cos(2(#Psi_{VZEROC} - #Psi_{TPC}))>");
582         fProfV2Resolution[i]->GetXaxis()->SetBinLabel(8, "<cos(2(#Psi_{TPC} - #Psi_{VZEROC}))>");
583         fProfV2Resolution[i]->GetXaxis()->SetBinLabel(9, "<cos(2(#Psi_{VZERO} - #Psi_{TPC_A}))>");
584         fProfV2Resolution[i]->GetXaxis()->SetBinLabel(10, "<cos(2(#Psi_{VZERO} - #Psi_{TPC_B}))>");
585         fProfV2Resolution[i]->GetXaxis()->SetBinLabel(11, "<cos(2(#Psi_{TPC_A} - #Psi_{TPC_B}))>");
586         fOutputList->Add(fProfV2Resolution[i]); 
587         fProfV3Resolution[i] = new TProfile(Form("fProfV3Resolution_%i", i), Form("fProfV3Resolution_%i", i), 11, -0.5, 10.5);
588         fProfV3Resolution[i]->GetXaxis()->SetBinLabel(3, "<cos(3(#Psi_{VZEROA} - #Psi_{VZEROC}))>");
589         fProfV3Resolution[i]->GetXaxis()->SetBinLabel(4, "<cos(3(#Psi_{VZEROC} - #Psi_{VZEROA}))>");
590         fProfV3Resolution[i]->GetXaxis()->SetBinLabel(5, "<cos(3(#Psi_{VZEROA} - #Psi_{TPC}))>");
591         fProfV3Resolution[i]->GetXaxis()->SetBinLabel(6, "<cos(3(#Psi_{TPC} - #Psi_{VZEROA}))>");
592         fProfV3Resolution[i]->GetXaxis()->SetBinLabel(7, "<cos(3(#Psi_{VZEROC} - #Psi_{TPC}))>");
593         fProfV3Resolution[i]->GetXaxis()->SetBinLabel(8, "<cos(3(#Psi_{TPC} - #Psi_{VZEROC}))>");
594         fProfV3Resolution[i]->GetXaxis()->SetBinLabel(9, "<cos(3(#Psi_{VZERO} - #Psi_{TPC_A}))>");
595         fProfV3Resolution[i]->GetXaxis()->SetBinLabel(10, "<cos(3(#Psi_{VZERO} - #Psi_{TPC_B}))>");
596         fProfV3Resolution[i]->GetXaxis()->SetBinLabel(11, "<cos(3(#Psi_{TPC_A} - #Psi_{TPC_B}))>");
597         fOutputList->Add(fProfV3Resolution[i]); 
598     }
599    // vn profile
600     Float_t temp[fCentralityClasses->GetSize()];
601     for(Int_t i(0); i < fCentralityClasses->GetSize(); i++) temp[i] = fCentralityClasses->At(i);
602     fProfV2 = new TProfile("fProfV2", "fProfV2", fCentralityClasses->GetSize()-1, temp);
603     fProfV3 = new TProfile("fProfV3", "fProfV3", fCentralityClasses->GetSize()-1, temp);
604     fOutputList->Add(fProfV2);
605     fOutputList->Add(fProfV3);
606     switch (fFitModulationType) {
607         case kQC2 : {
608             fProfV2Cumulant = new TProfile("fProfV2Cumulant", "fProfV2Cumulant", fCentralityClasses->GetSize()-1, temp);
609             fProfV3Cumulant = new TProfile("fProfV3Cumulant", "fProfV3Cumulant", fCentralityClasses->GetSize()-1, temp);
610             fOutputList->Add(fProfV2Cumulant);
611             fOutputList->Add(fProfV3Cumulant);
612         } break;
613         case kQC4 : {
614             fProfV2Cumulant = new TProfile("fProfV2Cumulant", "fProfV2Cumulant", fCentralityClasses->GetSize()-1, temp);
615             fProfV3Cumulant = new TProfile("fProfV3Cumulant", "fProfV3Cumulant", fCentralityClasses->GetSize()-1, temp);
616             fOutputList->Add(fProfV2Cumulant);
617             fOutputList->Add(fProfV3Cumulant);
618         } break;
619         default : break;
620     }
621     // for the histograms initialized below, binning is fixed to runnumbers or flags
622     fReduceBinsXByFactor = 1;
623     fReduceBinsYByFactor = 1;
624     if(fFillQAHistograms) {
625         fHistRunnumbersEta = new TH2F("fHistRunnumbersEta", "fHistRunnumbersEta", fExpectedRuns->GetSize()+1, -.5, fExpectedRuns->GetSize()+.5, 100, -1.1, 1.1);
626         fHistRunnumbersEta->Sumw2();
627         fOutputList->Add(fHistRunnumbersEta);
628         fHistRunnumbersPhi = new TH2F("fHistRunnumbersPhi", "fHistRunnumbersPhi", fExpectedRuns->GetSize()+1, -.5, fExpectedRuns->GetSize()+.5, 100, -0.2, TMath::TwoPi()+0.2);
629         fHistRunnumbersPhi->Sumw2();
630         fOutputList->Add(fHistRunnumbersPhi);
631         for(Int_t i(0); i < fExpectedRuns->GetSize(); i++) { 
632             fHistRunnumbersPhi->GetXaxis()->SetBinLabel(i+1, Form("%i", fExpectedRuns->At(i)));
633             fHistRunnumbersEta->GetXaxis()->SetBinLabel(i+1, Form("%i", fExpectedRuns->At(i)));
634         }
635         fHistRunnumbersPhi->GetXaxis()->SetBinLabel(fExpectedRuns->GetSize()+1, "undetermined");
636         fHistRunnumbersEta->GetXaxis()->SetBinLabel(fExpectedRuns->GetSize()+1, "undetermined");
637     }
638     fHistAnalysisSummary = BookTH1F("fHistAnalysisSummary", "flag", 54, -0.5, 54.5);
639     fHistSwap = new TH1F("fHistSwap", "fHistSwap", 20, 0, TMath::TwoPi());
640     if(fUsePtWeight) fHistSwap->Sumw2();
641
642     if(fUserSuppliedV2) fOutputList->Add(fUserSuppliedV2);
643     if(fUserSuppliedV3) fOutputList->Add(fUserSuppliedV3);
644     if(fUserSuppliedR2) fOutputList->Add(fUserSuppliedR2);
645     if(fUserSuppliedR3) fOutputList->Add(fUserSuppliedR3);
646     // increase readability of output list
647     fOutputList->Sort();
648     // cdf and pdf of chisquare distribution
649     fHistPvalueCDF = BookTH1F("fHistPvalueCDF", "CDF #chi^{2}", 50, 0, 1);
650     fHistPvalueCDFCent = BookTH2F("fHistPvalueCDFCent", "centrality", "p-value", 40, 0, 100, 40, 0, 1);
651     fHistChi2Cent = BookTH2F("fHistChi2Cent", "centrality", "#tilde{#chi^{2}}", 100, 0, 100, 100, 0, 5);
652     fHistPChi2 = BookTH2F("fHistPChi2", "p-value", "#tilde{#chi^{2}}", 1000, 0, 1, 100, 0, 5);
653     fHistKolmogorovTest = BookTH1F("fHistKolmogorovTest", "KolmogorovTest", 50, 0, 1);
654     fHistKolmogorovTestCent = BookTH2F("fHistKolmogorovTestCent", "centrality", "Kolmogorov p", 40, 0, 100, 45, 0, 1); 
655     fHistPvalueCDFROOT = BookTH1F("fHistPvalueCDFROOT", "CDF #chi^{2} ROOT", 50, 0, 1);
656     fHistPvalueCDFROOTCent = BookTH2F("fHistPvalueCDFROOTCent", "centrality", "p-value ROOT", 40, 0, 100, 45, 0, 1);
657     fHistChi2ROOTCent = BookTH2F("fHistChi2ROOTCent", "centrality", "#tilde{#chi^{2}}", 40, 0, 100, 45, 0, 5);
658     fHistPChi2Root = BookTH2F("fHistPChi2Root", "p-value", "#tilde{#chi^{2}} ROOT", 1000, 0, 1, 100, 0, 5);
659     fHistPKolmogorov = BookTH2F("fHistPKolmogorov", "p-value", "kolmogorov p",40, 0, 1, 40, 0, 1);
660     fHistRhoStatusCent = BookTH2F("fHistRhoStatusCent", "centrality", "status [-1=lin was better, 0=ok, 1 = failed]", 101, -1, 100, 3, -1.5, 1.5);
661     fHistUndeterminedRunQA = BookTH1F("fHistUndeterminedRunQA", "runnumber", 10, 0, 10);
662  
663     PostData(1, fOutputList);
664
665     switch (fRunModeType) {
666         case kLocal : {
667             fOutputListGood = new TList();
668             fOutputListGood->SetOwner(kTRUE);
669             fOutputListBad = new TList();
670             fOutputListBad->SetOwner(kTRUE);
671             PostData(2, fOutputListGood);
672             PostData(3, fOutputListBad);
673         } break;
674         default: break;
675     }
676
677     // get the containers
678     fTracksCont = GetParticleContainer("Tracks");
679     fClusterCont = GetClusterContainer(0);      // get the default cluster container
680     fJetsCont = GetJetContainer("Jets");
681 }
682 //_____________________________________________________________________________
683 Bool_t AliAnalysisTaskJetV2::Run()
684 {
685     // called for each accepted event (call made from user exec of parent class)
686     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
687     if(!fTracks||!fJets||!fRho) {
688         if(!fTracks) printf(" > Failed to retrieve fTracks ! < \n");
689         if(!fJets) printf(" > Failed to retrieve fJets ! < \n");
690         if(!fRho) printf(" > Failed to retrieve fRho ! < \n");
691         return kFALSE;
692     }
693     if(!fLocalInit) fLocalInit = InitializeAnalysis();
694     // reject the event if expected data is missing
695     if(!PassesCuts(InputEvent())) return kFALSE;
696     // cache the leading jet within acceptance
697     fLeadingJet = GetLeadingJet();
698     // set the rho value 
699     fLocalRho->SetVal(fRho->GetVal());
700     // place holder arrays for the event planes
701     //
702     // [0][0] psi2a     [1,0]   psi2c
703     // [0][1] psi3a     [1,1]   psi3c
704     Double_t vzero[2][2];
705     /* for the combined vzero event plane
706      * [0] psi2         [1] psi3
707      * not fully implmemented yet, use with caution ! */
708     Double_t vzeroComb[2];
709     // [0] psi2         [1] psi3
710     Double_t tpc[2];
711     // evaluate the actual event planes
712     switch (fDetectorType) {
713         case kFixedEP : {
714             // for fixed, fix all ep's to default values
715             tpc[0] = 0.;         tpc[1] = 1.;
716             vzero[0][0] = 0.;    vzero[0][1] = 1.;
717             vzero[1][0] = 0.;    vzero[1][1] = 1.;
718             vzeroComb[0] = 0.;   vzeroComb[1] = 1.;
719         } break;
720         default : {
721             // else grab the actual data
722             CalculateEventPlaneVZERO(vzero);
723             CalculateEventPlaneCombinedVZERO(vzeroComb);
724             CalculateEventPlaneTPC(tpc);
725         } break;
726     }
727     Double_t psi2(-1), psi3(-1);
728     // arrays which will hold the fit parameters
729     switch (fDetectorType) {    // determine the detector type for the rho fit
730         case kTPC :     { psi2 = tpc[0];         psi3 = tpc[1]; }       break;
731         case kVZEROA :  { psi2 = vzero[0][0];    psi3 = vzero[0][1]; }  break;  
732         case kVZEROC :  { psi2 = vzero[1][0];    psi3 = vzero[1][1]; }  break;
733         case kVZEROComb : { psi2 = vzeroComb[0]; psi3 = vzeroComb[1];}  break;
734         case kFixedEP : { psi2 = 0.;             psi3 = 1.;}            break;
735         default : break;
736     }
737     switch (fFitModulationType) { // do the fits
738         case kNoFit : { 
739              switch (fCollisionType) {
740                  case kPythia : { // background is zero for pp jets
741                      fFitModulation->FixParameter(0, 0);
742                      fLocalRho->SetVal(0);
743                  } break;
744                  default :  {
745                      fFitModulation->FixParameter(0, fLocalRho->GetVal()); 
746                  } break;
747              }
748         } break;
749         case kV2 : {    // only v2
750             if(CorrectRho(psi2, psi3)) {
751                 fProfV2->Fill(fCent, fFitModulation->GetParameter(3));
752                 if(fUserSuppliedR2) {
753                     Double_t r(fUserSuppliedR2->GetBinContent(fUserSuppliedR2->GetXaxis()->FindBin(fCent)));
754                     if(r > 0) fFitModulation->SetParameter(3, fFitModulation->GetParameter(3)/r);
755                 }
756                 CalculateEventPlaneResolution(vzero, vzeroComb, tpc);
757             }
758         } break;
759         case kV3 : {    // only v3
760             if(CorrectRho(psi2, psi3)) {
761                 if(fUserSuppliedR3) {
762                     Double_t r(fUserSuppliedR3->GetBinContent(fUserSuppliedR3->GetXaxis()->FindBin(fCent)));
763                     if(r > 0) fFitModulation->SetParameter(3, fFitModulation->GetParameter(3)/r);
764                 }
765                 fProfV3->Fill(fCent, fFitModulation->GetParameter(3));
766                 CalculateEventPlaneResolution(vzero, vzeroComb, tpc);
767             }
768         } break;
769         case kQC2 : {   // qc2 analysis
770             if(CorrectRho(psi2, psi3)) {
771                 if(fUserSuppliedR2 && fUserSuppliedR3) {
772                     // note for the qc method, resolution is REVERSED to go back to v2obs
773                     Double_t r2(fUserSuppliedR2->GetBinContent(fUserSuppliedR2->GetXaxis()->FindBin(fCent)));
774                     Double_t r3(fUserSuppliedR3->GetBinContent(fUserSuppliedR3->GetXaxis()->FindBin(fCent)));
775                     if(r2 > 0) fFitModulation->SetParameter(3, fFitModulation->GetParameter(3)*r2);
776                     if(r3 > 0) fFitModulation->SetParameter(7, fFitModulation->GetParameter(7)*r3);
777                 }
778                 if (fUsePtWeight) { // use weighted weights
779                     Double_t dQCnM11 = (fNoEventWeightsForQC) ? 1. : QCnM11();
780                     fProfV2->Fill(fCent, fFitModulation->GetParameter(3), dQCnM11);
781                     fProfV3->Fill(fCent, fFitModulation->GetParameter(7), dQCnM11); 
782                 } else {
783                     Double_t dQCnM = (fNoEventWeightsForQC) ? 2. : QCnM();
784                     fProfV2->Fill(fCent, fFitModulation->GetParameter(3), dQCnM*(dQCnM-1));
785                     fProfV3->Fill(fCent, fFitModulation->GetParameter(7), dQCnM*(dQCnM-1));
786                 }
787                 CalculateEventPlaneResolution(vzero, vzeroComb, tpc);
788             }
789         } break;
790         case kQC4 : {
791             if(CorrectRho(psi2, psi3)) {
792                 if(fUserSuppliedR2 && fUserSuppliedR3) {
793                     // note for the qc method, resolution is REVERSED to go back to v2obs   
794                     Double_t r2(fUserSuppliedR2->GetBinContent(fUserSuppliedR2->GetXaxis()->FindBin(fCent)));
795                     Double_t r3(fUserSuppliedR3->GetBinContent(fUserSuppliedR3->GetXaxis()->FindBin(fCent)));
796                     if(r2 > 0) fFitModulation->SetParameter(3, fFitModulation->GetParameter(3)*r2);
797                     if(r3 > 0) fFitModulation->SetParameter(7, fFitModulation->GetParameter(7)*r3);
798                 }
799                 if (fUsePtWeight) { // use weighted weights
800                     fProfV2->Fill(fCent, TMath::Power(fFitModulation->GetParameter(3),0.5)/*, QCnM1111()*/);
801                     fProfV3->Fill(fCent, TMath::Power(fFitModulation->GetParameter(7),0.5)/*, QCnM1111()*/); 
802                 } else {
803                     fProfV2->Fill(fCent, TMath::Power(fFitModulation->GetParameter(3),0.5)/*, QCnM()*(QCnM()-1)*(QCnM()-2)*(QCnM()-3)*/);
804                     fProfV3->Fill(fCent, TMath::Power(fFitModulation->GetParameter(7),0.5)/*, QCnM()*(QCnM()-1)*(QCnM()-2)*(QCnM()-3)*/);
805                 }
806             }
807             CalculateEventPlaneResolution(vzero, vzeroComb, tpc);
808         } break;
809         default : {
810             if(CorrectRho(psi2, psi3)) {
811                 if(fUserSuppliedR2 && fUserSuppliedR3) {
812                     Double_t r2(fUserSuppliedR2->GetBinContent(fUserSuppliedR2->GetXaxis()->FindBin(fCent)));
813                     Double_t r3(fUserSuppliedR3->GetBinContent(fUserSuppliedR3->GetXaxis()->FindBin(fCent)));
814                     if(r2 > 0) fFitModulation->SetParameter(3, fFitModulation->GetParameter(3)/r2);
815                     if(r3 > 0) fFitModulation->SetParameter(7, fFitModulation->GetParameter(7)/r3);
816                 }
817                 fProfV2->Fill(fCent, fFitModulation->GetParameter(3));
818                 fProfV3->Fill(fCent, fFitModulation->GetParameter(7));
819                 CalculateEventPlaneResolution(vzero, vzeroComb, tpc);
820             }
821         } break;
822     }
823     // if all went well, update the local rho parameter
824     fLocalRho->SetLocalRho(fFitModulation);
825     // and only at this point can the leading jet after rho subtraction be evaluated
826     if(fFillQAHistograms) fLeadingJetAfterSub = GetLeadingJet(fLocalRho);
827     // fill a number of histograms. event qa needs to be filled first as it also determines the runnumber for the track qa 
828     if(fFillQAHistograms)       FillQAHistograms(InputEvent());
829     if(fFillHistograms)         FillHistogramsAfterSubtraction(psi2, vzero, vzeroComb, tpc);
830     // send the output to the connected output container
831     PostData(1, fOutputList);
832     switch (fRunModeType) {
833         case kLocal : {
834             PostData(2, fOutputListGood);
835             PostData(3, fOutputListBad);
836         } break;
837         default: break;
838     }
839     return kTRUE;
840 }
841 //_____________________________________________________________________________
842 void AliAnalysisTaskJetV2::Exec(Option_t* c)
843 {
844     // for stand alone, avoid framework event setup
845     switch (fCollisionType) {
846         case kJetFlowMC : {
847             // need to call ExecOnce as it is not loaded otherwise
848             if(!fLocalRho) AliAnalysisTaskJetV2::ExecOnce();
849             AliAnalysisTaskJetV2::Run();
850         } break;
851         default : {
852             AliAnalysisTaskSE::Exec(c);
853         } break;
854     }
855 }  
856 //_____________________________________________________________________________
857 Double_t AliAnalysisTaskJetV2::CalculateEventPlaneChi(Double_t res)
858 {
859     // return chi for given resolution to combine event plane estimates from two subevents
860     // see Phys. Rev. C no. CS6346 (http://arxiv.org/abs/nucl-ex/9805001)
861     Double_t chi(2.), delta(1.), con((TMath::Sqrt(TMath::Pi()))/(2.*TMath::Sqrt(2)));
862     for (Int_t i(0); i < 15; i++) {
863         chi = ((con*chi*TMath::Exp(-chi*chi/4.)*(TMath::BesselI0(chi*chi/4.)+TMath::BesselI1(chi*chi/4.))) < res) ? chi + delta : chi - delta;
864         delta = delta / 2.;
865     }
866     return chi;
867 }
868 //_____________________________________________________________________________
869 void AliAnalysisTaskJetV2::CalculateEventPlaneVZERO(Double_t vzero[2][2]) const 
870 {
871     // get the vzero event plane (a and c separately)
872     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
873     switch (fCollisionType) {
874         case kPbPb10h : {
875             // for 10h data, get the calibrated q-vector from the database
876             Double_t QA2[] = {-999., -999.};
877             Double_t QA3[] = {-999., -999.};
878             Double_t QC2[] = {-999., -999.};
879             Double_t QC3[] = {-999., -999.};
880             CalculateQvectorVZERO(QA2, QA3, QC2, QC3);
881             vzero[0][0] = .5*TMath::ATan2(QA2[1], QA2[0]);
882             vzero[1][0] = .5*TMath::ATan2(QC2[1], QC2[0]);
883             vzero[0][1] = (1./3.)*TMath::ATan2(QA3[1], QA3[0]);
884             vzero[1][1] = (1./3.)*TMath::ATan2(QC3[1], QC3[0]);
885         } break;
886         default: {
887             // by default use the ep from the event header (make sure EP selection task is enabeled!)
888             Double_t a(0), b(0), c(0), d(0), e(0), f(0), g(0), h(0);
889             vzero[0][0] = InputEvent()->GetEventplane()->CalculateVZEROEventPlane(InputEvent(), 8, 2, a, b);
890             vzero[1][0] = InputEvent()->GetEventplane()->CalculateVZEROEventPlane(InputEvent(), 9, 2, c, d);
891             vzero[0][1] = InputEvent()->GetEventplane()->CalculateVZEROEventPlane(InputEvent(), 8, 3, e, f);
892             vzero[1][1] = InputEvent()->GetEventplane()->CalculateVZEROEventPlane(InputEvent(), 9, 3, g, h);
893             return;
894         }
895     }
896 }
897 //_____________________________________________________________________________
898 void AliAnalysisTaskJetV2::CalculateEventPlaneCombinedVZERO(Double_t* comb) const
899 {
900     // return the combined vzero event plane
901     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
902     switch (fCollisionType) {
903         // for 10h data call calibration info
904         case kPbPb10h : {
905             // get the calibrated q-vectors
906             Double_t Q2[] = {-999., -999.};            
907             Double_t Q3[] = {-999., -999.};
908             // return if something isn't ok from the calibration side
909             CalculateQvectorCombinedVZERO(Q2, Q3);
910             comb[0] = .5*TMath::ATan2(Q2[1], Q2[0]);
911             comb[1] = (1./3.)*TMath::ATan2(Q3[1], Q3[0]);
912         } break;
913         default : {
914             // for all other types use calibrated event plane from the event header
915             Double_t a(0), b(0), c(0), d(0);
916             comb[0] = InputEvent()->GetEventplane()->CalculateVZEROEventPlane(InputEvent(), 10, 2, a, b);
917             comb[1] = InputEvent()->GetEventplane()->CalculateVZEROEventPlane(InputEvent(), 10, 3, c, d);
918         } break;
919     }
920 }
921 //_____________________________________________________________________________
922 void AliAnalysisTaskJetV2::CalculateEventPlaneTPC(Double_t* tpc)
923 {
924    // grab the TPC event plane
925    if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
926    fNAcceptedTracks = 0;                // reset the track counter
927    Double_t qx2(0), qy2(0);     // for psi2
928    Double_t qx3(0), qy3(0);     // for psi3
929    if(fTracksCont) {
930        Float_t excludeInEta = -999;
931        if(fExcludeLeadingJetsFromFit > 0 ) {    // remove the leading jet from ep estimate
932            if(fLeadingJet) excludeInEta = fLeadingJet->Eta();
933        }
934        for(Int_t iTPC(0); iTPC < fTracksCont->GetNEntries(); iTPC++) {
935            AliVParticle* track = fTracksCont->GetParticle(iTPC);
936            if(!PassesCuts(track) || track->Pt() < fSoftTrackMinPt || track->Pt() > fSoftTrackMaxPt) continue;
937            if(fExcludeLeadingJetsFromFit > 0 &&( (TMath::Abs(track->Eta() - excludeInEta) < GetJetContainer()->GetJetRadius()*fExcludeLeadingJetsFromFit ) || (TMath::Abs(track->Eta()) - GetJetContainer()->GetJetRadius() - GetJetContainer()->GetJetEtaMax() ) > 0 )) continue;
938            fNAcceptedTracks++;
939            qx2+= TMath::Cos(2.*track->Phi());
940            qy2+= TMath::Sin(2.*track->Phi());
941            qx3+= TMath::Cos(3.*track->Phi());
942            qy3+= TMath::Sin(3.*track->Phi());
943        }
944    }
945    tpc[0] = .5*TMath::ATan2(qy2, qx2);
946    tpc[1] = (1./3.)*TMath::ATan2(qy3, qx3);
947 }
948 //_____________________________________________________________________________
949 void AliAnalysisTaskJetV2::CalculateEventPlaneResolution(Double_t vzero[2][2], Double_t* vzeroComb, Double_t* tpc)
950 {
951     // fill the profiles for the resolution parameters
952     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
953     fProfV2Resolution[fInCentralitySelection]->Fill(2., TMath::Cos(2.*(vzero[0][0] - vzero[1][0])));
954     fProfV2Resolution[fInCentralitySelection]->Fill(3., TMath::Cos(2.*(vzero[1][0] - vzero[0][0])));
955     fProfV2Resolution[fInCentralitySelection]->Fill(4., TMath::Cos(2.*(vzero[0][0] - tpc[0])));
956     fProfV2Resolution[fInCentralitySelection]->Fill(5., TMath::Cos(2.*(tpc[0] - vzero[0][0])));
957     fProfV2Resolution[fInCentralitySelection]->Fill(6., TMath::Cos(2.*(vzero[1][0] - tpc[0])));
958     fProfV2Resolution[fInCentralitySelection]->Fill(7., TMath::Cos(2.*(tpc[0] - vzero[1][0])));
959     fProfV3Resolution[fInCentralitySelection]->Fill(2., TMath::Cos(3.*(vzero[0][0] - vzero[1][0])));
960     fProfV3Resolution[fInCentralitySelection]->Fill(3., TMath::Cos(3.*(vzero[1][0] - vzero[0][0])));
961     fProfV3Resolution[fInCentralitySelection]->Fill(4., TMath::Cos(3.*(vzero[0][0] - tpc[0])));
962     fProfV3Resolution[fInCentralitySelection]->Fill(5., TMath::Cos(3.*(tpc[0] - vzero[0][0])));
963     fProfV3Resolution[fInCentralitySelection]->Fill(6., TMath::Cos(3.*(vzero[1][0] - tpc[0])));
964     fProfV3Resolution[fInCentralitySelection]->Fill(7., TMath::Cos(3.*(tpc[0] - vzero[1][0])));
965     // for the resolution of the combined vzero event plane, use two tpc halves as uncorrelated subdetectors
966     Double_t qx2a(0), qy2a(0);     // for psi2a, negative eta
967     Double_t qx3a(0), qy3a(0);     // for psi3a, negative eta
968     Double_t qx2b(0), qy2b(0);     // for psi2a, positive eta
969     Double_t qx3b(0), qy3b(0);     // for psi3a, positive eta
970     if(fTracks) {
971        Int_t iTracks(fTracks->GetEntriesFast());
972        for(Int_t iTPC(0); iTPC < iTracks; iTPC++) {
973            AliVTrack* track = static_cast<AliVTrack*>(fTracks->At(iTPC));
974            if(!PassesCuts(track) || track->Pt() < fSoftTrackMinPt || track->Pt() > fSoftTrackMaxPt) continue;
975            if(track->Eta() < 0 ) {
976                qx2a+= TMath::Cos(2.*track->Phi());
977                qy2a+= TMath::Sin(2.*track->Phi());
978                qx3a+= TMath::Cos(3.*track->Phi());
979                qy3a+= TMath::Sin(3.*track->Phi());
980            } else if (track->Eta() > 0) {
981                qx2b+= TMath::Cos(2.*track->Phi());
982                qy2b+= TMath::Sin(2.*track->Phi());
983                qx3b+= TMath::Cos(3.*track->Phi());
984                qy3b+= TMath::Sin(3.*track->Phi());
985            }
986        }
987    }
988    Double_t tpca2(.5*TMath::ATan2(qy2a, qx2a));
989    Double_t tpca3((1./3.)*TMath::ATan2(qy3a, qx3a));
990    Double_t tpcb2(.5*TMath::ATan2(qy2b, qx2b));
991    Double_t tpcb3((1./3.)*TMath::ATan2(qy3b, qx3b));
992    fProfV2Resolution[fInCentralitySelection]->Fill(8., TMath::Cos(2.*(vzeroComb[0] - tpca2)));
993    fProfV2Resolution[fInCentralitySelection]->Fill(9., TMath::Cos(2.*(vzeroComb[0] - tpcb2)));
994    fProfV2Resolution[fInCentralitySelection]->Fill(10., TMath::Cos(2.*(tpca2 - tpcb2))); 
995    fProfV3Resolution[fInCentralitySelection]->Fill(8., TMath::Cos(3.*(vzeroComb[1] - tpca3)));
996    fProfV3Resolution[fInCentralitySelection]->Fill(9., TMath::Cos(3.*(vzeroComb[1] - tpcb3)));
997    fProfV3Resolution[fInCentralitySelection]->Fill(10., TMath::Cos(3.*(tpca3 - tpcb3))); 
998 }   
999 //_____________________________________________________________________________
1000 void AliAnalysisTaskJetV2::CalculateQvectorVZERO(Double_t Qa2[2], Double_t Qc2[2], Double_t Qa3[2], Double_t Qc3[2]) const
1001 {
1002     // return the calibrated 2nd and 3rd order q-vectors for vzeroa and vzeroc
1003     // function takes arrays as arguments, which correspond to vzero info in the following way
1004     // 
1005     // Qa2[0] = Qx2 for vzero A         Qa2[1] = Qy2 for vzero A (etc)
1006     
1007     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
1008     // placeholders for geometric information
1009     Double_t phi(-999.), weight(-999.); 
1010     // reset placeholders for Q-vector components
1011     Qa2[0] = 0.;    Qc2[0] = 0.;    Qa3[0] = 0.;    Qc3[0] = 0.;
1012     Qa2[1] = 0.;    Qc2[1] = 0.;    Qa3[1] = 0.;    Qc3[1] = 0.;
1013     
1014     for(Int_t i(0); i < 64; i++) {
1015         // loop over all scintillators, construct Q-vectors in the same loop
1016         phi     = TMath::PiOver4()*(0.5+i%8);
1017         weight  = 0.;
1018         // note that disabled rings have already been excluded in ReadVZEROCalibration2010h
1019         if(i<32) {    // v0c side
1020             if(i < 8) weight = InputEvent()->GetVZEROData()->GetMultiplicity(i)*fVZEROCpol[0]/fVZEROgainEqualization->GetBinContent(1+i);
1021             else if (i < 16 ) weight = InputEvent()->GetVZEROData()->GetMultiplicity(i)*fVZEROCpol[1]/fVZEROgainEqualization->GetBinContent(1+i);
1022             else if (i < 24 ) weight = InputEvent()->GetVZEROData()->GetMultiplicity(i)*fVZEROCpol[2]/fVZEROgainEqualization->GetBinContent(1+i);
1023             else if (i < 32 ) weight = InputEvent()->GetVZEROData()->GetMultiplicity(i)*fVZEROCpol[3]/fVZEROgainEqualization->GetBinContent(1+i);
1024             // fill Q-vectors for v0c side
1025             Qc2[0]+=weight*TMath::Cos(2.*phi);
1026             Qc3[0]+=weight*TMath::Cos(3.*phi);
1027             Qc2[1]+=weight*TMath::Sin(2.*phi);
1028             Qc3[1]+=weight*TMath::Sin(3.*phi);
1029         } else {       // v0a side
1030             if( i < 40) weight = InputEvent()->GetVZEROData()->GetMultiplicity(i)*fVZEROApol[0]/fVZEROgainEqualization->GetBinContent(1+i);
1031             else if ( i < 48 ) weight = InputEvent()->GetVZEROData()->GetMultiplicity(i)*fVZEROApol[1]/fVZEROgainEqualization->GetBinContent(1+i);
1032             else if ( i < 56 ) weight = InputEvent()->GetVZEROData()->GetMultiplicity(i)*fVZEROApol[2]/fVZEROgainEqualization->GetBinContent(1+i);
1033             else if ( i < 64 ) weight = InputEvent()->GetVZEROData()->GetMultiplicity(i)*fVZEROApol[3]/fVZEROgainEqualization->GetBinContent(1+i);
1034             // fill Q-vectors for v0a side
1035             Qa2[0]+=weight*TMath::Cos(2.*phi);
1036             Qa3[0]+=weight*TMath::Cos(3.*phi);
1037             Qa2[1]+=weight*TMath::Sin(2.*phi);
1038             Qa3[1]+=weight*TMath::Sin(3.*phi);
1039         }
1040     }
1041     // get the cache index and read the correction terms from the cache
1042     Int_t VZEROcentralityBin(GetVZEROCentralityBin());
1043     Double_t Qx2amean = fMeanQ[VZEROcentralityBin][1][0];
1044     Double_t Qx2arms  = fWidthQ[VZEROcentralityBin][1][0];
1045     Double_t Qy2amean = fMeanQ[VZEROcentralityBin][1][1];
1046     Double_t Qy2arms  = fWidthQ[VZEROcentralityBin][1][1];
1047
1048     Double_t Qx2cmean = fMeanQ[VZEROcentralityBin][0][0];
1049     Double_t Qx2crms  = fWidthQ[VZEROcentralityBin][0][0];
1050     Double_t Qy2cmean = fMeanQ[VZEROcentralityBin][0][1];
1051     Double_t Qy2crms  = fWidthQ[VZEROcentralityBin][0][1];      
1052
1053     Double_t Qx3amean = fMeanQv3[VZEROcentralityBin][1][0];
1054     Double_t Qx3arms  = fWidthQv3[VZEROcentralityBin][1][0];
1055     Double_t Qy3amean = fMeanQv3[VZEROcentralityBin][1][1];
1056     Double_t Qy3arms  = fWidthQv3[VZEROcentralityBin][1][1];
1057
1058     Double_t Qx3cmean = fMeanQv3[VZEROcentralityBin][0][0];
1059     Double_t Qx3crms  = fWidthQv3[VZEROcentralityBin][0][0];
1060     Double_t Qy3cmean = fMeanQv3[VZEROcentralityBin][0][1];
1061     Double_t Qy3crms  = fWidthQv3[VZEROcentralityBin][0][1];    
1062
1063     // update the weighted q-vectors with the re-centered values
1064     Qa2[0] = (Qa2[0] - Qx2amean)/Qx2arms;
1065     Qa2[1] = (Qa2[1] - Qy2amean)/Qy2arms;
1066     Qc2[0] = (Qc2[0] - Qx2cmean)/Qx2crms;
1067     Qc2[1] = (Qc2[1] - Qy2cmean)/Qy2crms;
1068
1069     Qa3[0] = (Qa3[0] - Qx3amean)/Qx3arms;
1070     Qa3[1] = (Qa3[1] - Qy3amean)/Qy3arms;
1071     Qc3[0] = (Qc3[0] - Qx3cmean)/Qx3crms;
1072     Qc3[1] = (Qc3[0] - Qy3cmean)/Qy3crms;
1073 }
1074 //_____________________________________________________________________________
1075 void AliAnalysisTaskJetV2::CalculateQvectorCombinedVZERO(Double_t Q2[2], Double_t Q3[2]) const
1076 {
1077     // calculate calibrated q-vector of the combined vzeroa, vzeroc system
1078     // this is somewhat ugly as CalculateQvectorCombinedVZERO is called more than once per event
1079     // but for now it will have to do ...
1080     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
1081
1082     // first step: retrieve the q-vectors component-wise per vzero detector
1083     Double_t QA2[] = {-999., -999.};
1084     Double_t QA3[] = {-999., -999.};
1085     Double_t QC2[] = {-999., -999.};
1086     Double_t QC3[] = {-999., -999.};
1087     CalculateQvectorVZERO(QA2, QA3, QC2, QC3);
1088
1089     // get cache index and retrieve the chi weights for this centrality
1090     Int_t VZEROcentralityBin(GetVZEROCentralityBin());
1091     Double_t chi2A(fChi2A->At(VZEROcentralityBin));
1092     Double_t chi2C(fChi2C->At(VZEROcentralityBin));
1093     Double_t chi3A(fChi3A->At(VZEROcentralityBin));
1094     Double_t chi3C(fChi3C->At(VZEROcentralityBin));
1095
1096     // combine the vzera and vzeroc signal
1097     Q2[0] = chi2A*chi2A*QA2[0]+chi2C*chi2C*QC2[0];
1098     Q2[1] = chi2A*chi2A*QA2[1]+chi2C*chi2C*QC2[1];
1099     Q3[0] = chi3A*chi3A*QA3[0]+chi3C*chi3C*QC3[0];
1100     Q3[1] = chi3A*chi3A*QC3[1]+chi3C*chi3C*QC3[1];
1101 }
1102 //_____________________________________________________________________________
1103 void AliAnalysisTaskJetV2::CalculateRandomCone(Float_t &pt, Float_t &eta, Float_t &phi, 
1104         AliParticleContainer* tracksCont, AliClusterContainer* clusterCont, AliEmcalJet* jet) const
1105 {
1106     // get a random cone
1107     if(fDebug > 1) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
1108     pt = 0; eta = 0; phi = 0;
1109     Float_t etaJet(999), phiJet(999), dJet(999);        // no jet: same as jet very far away
1110     if(jet) { // if a leading jet is given, use its kinematic properties to exclude it
1111         etaJet = jet->Eta();
1112         phiJet = jet->Phi();
1113     }
1114     // the random cone acceptance has to equal the jet acceptance
1115     // this also insures safety when runnnig on the semi-good tpc runs for 11h data,
1116     // where jet acceptance is adjusted to reduced acceptance - hence random cone acceptance as well
1117     Float_t minPhi(GetJetContainer()->GetJetPhiMin()), maxPhi(GetJetContainer()->GetJetPhiMax());
1118     if(maxPhi > TMath::TwoPi()) maxPhi = TMath::TwoPi();
1119     if(minPhi < 0 ) minPhi = 0.;
1120     // construct a random cone and see if it's far away enough from the leading jet
1121     Int_t attempts(1000);
1122     while(kTRUE) {
1123         attempts--;
1124         eta = gRandom->Uniform(GetJetContainer()->GetJetEtaMin(), GetJetContainer()->GetJetEtaMax());
1125         phi = gRandom->Uniform(minPhi, maxPhi);
1126
1127         dJet = TMath::Sqrt((etaJet-eta)*(etaJet-eta)+(phiJet-phi)*(phiJet-phi));
1128         if(dJet > fMinDisanceRCtoLJ) break;
1129         else if (attempts == 0) {
1130             printf(" > No random cone after 1000 tries, giving up ... !\n");
1131             return;
1132         }
1133     }
1134     // get the charged energy (if tracks are provided)
1135     if(tracksCont) {
1136         AliVParticle* track = tracksCont->GetNextAcceptParticle(0);
1137         while(track) {
1138             Float_t etaTrack(track->Eta()), phiTrack(track->Phi());
1139             // get distance from cone
1140             if(TMath::Abs(phiTrack-phi) > TMath::Abs(phiTrack - phi + TMath::TwoPi())) phiTrack+=TMath::TwoPi();
1141             if(TMath::Abs(phiTrack-phi) > TMath::Abs(phiTrack - phi - TMath::TwoPi())) phiTrack-=TMath::TwoPi();
1142             if(TMath::Sqrt(TMath::Abs((etaTrack-eta)*(etaTrack-eta)+(phiTrack-phi)*(phiTrack-phi))) <= GetJetRadius()) pt += track->Pt();
1143             track = tracksCont->GetNextAcceptParticle();
1144         }
1145     }
1146     // get the neutral energy (if clusters are provided)
1147     if(clusterCont) {
1148         TLorentzVector momentum;
1149         AliVCluster* cluster = clusterCont->GetNextAcceptCluster(0);
1150         while(cluster) {
1151             cluster->GetMomentum(momentum, const_cast<Double_t*>(fVertex));
1152             Float_t etaClus(momentum.Eta()), phiClus(momentum.Phi());
1153             // get distance from cone
1154             if(TMath::Abs(phiClus-phi) > TMath::Abs(phiClus - phi + TMath::TwoPi())) phiClus+=TMath::TwoPi();
1155             if(TMath::Abs(phiClus-phi) > TMath::Abs(phiClus - phi - TMath::TwoPi())) phiClus-=TMath::TwoPi();
1156             if(TMath::Sqrt(TMath::Abs((etaClus-eta)*(etaClus-eta)+(phiClus-phi)*(phiClus-phi))) <= GetJetRadius()) pt += momentum.Pt();
1157             cluster = clusterCont->GetNextAcceptCluster();
1158         }
1159     }
1160 }
1161 //_____________________________________________________________________________
1162 Double_t AliAnalysisTaskJetV2::CalculateQC2(Int_t harm) {
1163     // get the second order q-cumulant, a -999 return will be caught in the qa routine of CorrectRho
1164     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
1165     Double_t reQ(0), imQ(0), modQ(0), M11(0), M(0);
1166     if(fUsePtWeight) {  // for the weighted 2-nd order q-cumulant
1167         QCnQnk(harm, 1, reQ, imQ);      // get the weighted 2-nd order q-vectors
1168         modQ = reQ*reQ+imQ*imQ;         // get abs Q-squared
1169         M11 = QCnM11();                 // equals S2,1 - S1,2
1170         return (M11 > 0) ? ((modQ - QCnS(1,2))/M11) : -999;
1171     } // else return the non-weighted 2-nd order q-cumulant
1172     QCnQnk(harm, 0, reQ, imQ);          // get the non-weighted 2-nd order q-vectors
1173     modQ = reQ*reQ+imQ*imQ;             // get abs Q-squared
1174     M = QCnM();
1175     return (M > 1) ? (modQ - M)/(M*(M-1)) : -999;
1176 }
1177 //_____________________________________________________________________________
1178 Double_t AliAnalysisTaskJetV2::CalculateQC4(Int_t harm) {
1179     // get the fourth order q-cumulant, a -999 return will be caught in the qa routine of CorrectRho
1180     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
1181     Double_t reQn1(0), imQn1(0), reQ2n2(0), imQ2n2(0), reQn3(0), imQn3(0), M1111(0), M(0);
1182     Double_t a(0), b(0), c(0), d(0), e(0), f(0), g(0);  // terms of the calculation
1183     if(fUsePtWeight) {  // for the weighted 4-th order q-cumulant
1184         QCnQnk(harm, 1, reQn1, imQn1);
1185         QCnQnk(harm*2, 2, reQ2n2, imQ2n2);
1186         QCnQnk(harm, 3, reQn3, imQn3);
1187         // fill in the terms ...
1188         a = (reQn1*reQn1+imQn1*imQn1)*(reQn1*reQn1+imQn1*imQn1);
1189         b = reQ2n2*reQ2n2 + imQ2n2*imQ2n2;
1190         c = -2.*(reQ2n2*reQn1*reQn1-reQ2n2*imQn1*imQn1+2.*imQ2n2*reQn1*imQn1);
1191         d = 8.*(reQn3*reQn1+imQn3*imQn1);
1192         e = -4.*QCnS(1,2)*(reQn1*reQn1+imQn1*imQn1);
1193         f = -6.*QCnS(1,4);
1194         g = 2.*QCnS(2,2);
1195         M1111 = QCnM1111();
1196         return (M1111 > 0) ? (a+b+c+d+e+f+g)/M1111 : -999;
1197     }   // else return the unweighted case
1198     Double_t reQn(0), imQn(0), reQ2n(0), imQ2n(0);
1199     QCnQnk(harm, 0, reQn, imQn);
1200     QCnQnk(harm*2, 0, reQ2n, imQ2n);
1201     // fill in the terms ...
1202     M = QCnM();
1203     if(M < 4) return -999;
1204     a = (reQn*reQn+imQn*imQn)*(reQn*reQn+imQn*imQn);
1205     b = reQ2n*reQ2n + imQ2n*imQ2n;
1206     c = -2.*(reQ2n*reQn*reQn-reQ2n*imQn*imQn+2.*imQ2n*reQn*imQn);
1207     e = -4.*(M-2)*(reQn*reQn+imQn*imQn);
1208     f = 2.*M*(M-3);
1209     return (a+b+c+e+f)/(M*(M-1)*(M-2)*(M-3));
1210 }
1211 //_____________________________________________________________________________
1212 void AliAnalysisTaskJetV2::QCnQnk(Int_t n, Int_t k, Double_t &reQ, Double_t &imQ) {
1213     // get the weighted n-th order q-vector, pass real and imaginary part as reference
1214     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
1215     if(!fTracks) return;
1216     fNAcceptedTracksQCn = 0;
1217     Int_t iTracks(fTracks->GetEntriesFast());
1218     for(Int_t iTPC(0); iTPC < iTracks; iTPC++) {
1219         AliVTrack* track = static_cast<AliVTrack*>(fTracks->At(iTPC));
1220         if(!PassesCuts(track) || track->Pt() < fSoftTrackMinPt || track->Pt() > fSoftTrackMaxPt) continue;
1221         fNAcceptedTracksQCn++;
1222         // for the unweighted case, k equals zero and the weight doesn't contribute to the equation below
1223         reQ += TMath::Power(track->Pt(), k) * TMath::Cos(((double)n)*track->Phi());
1224         imQ += TMath::Power(track->Pt(), k) * TMath::Sin(((double)n)*track->Phi());
1225     }
1226 }
1227 //_____________________________________________________________________________
1228 void AliAnalysisTaskJetV2::QCnDiffentialFlowVectors(
1229         TClonesArray* pois, TArrayD* ptBins, Bool_t vpart, Double_t* repn, Double_t* impn, 
1230         Double_t *mp, Double_t *reqn, Double_t *imqn, Double_t* mq, Int_t n) 
1231 {
1232      if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
1233    // get  unweighted differential flow vectors
1234     Int_t iPois(pois->GetEntriesFast());
1235     if(vpart) {
1236         for(Int_t i(0); i < iPois; i++) {
1237             for(Int_t ptBin(0); ptBin < ptBins->GetSize()-1; ptBin++) {
1238                 AliVTrack* poi = static_cast<AliVTrack*>(pois->At(i));
1239                 if(PassesCuts(poi)) {
1240                     if(poi->Pt() >= ptBins->At(ptBin) && poi->Pt() < ptBins->At(ptBin+1)) {
1241                             // fill the flow vectors assuming that all poi's are in the rp selection (true by design)
1242                             repn[ptBin]+=TMath::Cos(((double)n)*poi->Phi());
1243                             impn[ptBin]+=TMath::Sin(((double)n)*poi->Phi());
1244                             mp[ptBin]++;
1245                             reqn[ptBin]+=TMath::Cos(((double)n)*poi->Phi());
1246                             imqn[ptBin]+=TMath::Sin(((double)n)*poi->Phi());
1247                             mq[ptBin]++;
1248                     }
1249                 }
1250             }
1251         }
1252     } else {
1253         for(Int_t i(0); i < iPois; i++) {
1254             for(Int_t ptBin(0); ptBin < ptBins->GetSize()-1; ptBin++) {
1255                 AliEmcalJet* poi = static_cast<AliEmcalJet*>(pois->At(i));
1256                 if(PassesCuts(poi)) {    
1257                     Double_t pt(poi->Pt()-poi->Area()*fLocalRho->GetLocalVal(poi->Phi(), GetJetContainer()->GetJetRadius(), fLocalRho->GetVal()));
1258                     if(pt >= ptBins->At(ptBin) && pt < ptBins->At(ptBin+1)) {    
1259                             repn[ptBin]+=TMath::Cos(((double)n)*poi->Phi());
1260                             impn[ptBin]+=TMath::Sin(((double)n)*poi->Phi());
1261                             mp[ptBin]++;        // qn isn't filled, no overlap between poi's and rp's
1262                     }
1263                 }
1264             }
1265         }
1266     }
1267 }
1268 //_____________________________________________________________________________
1269 Double_t AliAnalysisTaskJetV2::QCnS(Int_t i, Int_t j) {
1270     // get the weighted ij-th order autocorrelation correction
1271     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
1272     if(!fTracks || i <= 0 || j <= 0) return -999;
1273     Int_t iTracks(fTracks->GetEntriesFast());
1274     Double_t Sij(0);
1275     for(Int_t iTPC(0); iTPC < iTracks; iTPC++) {
1276         AliVTrack* track = static_cast<AliVTrack*>(fTracks->At(iTPC));
1277         if(!PassesCuts(track) || track->Pt() < fSoftTrackMinPt || track->Pt() > fSoftTrackMaxPt) continue;
1278         Sij+=TMath::Power(track->Pt(), j);
1279     }
1280     return TMath::Power(Sij, i);
1281 }
1282 //_____________________________________________________________________________
1283 Double_t AliAnalysisTaskJetV2::QCnM() {
1284     // get multiplicity for unweighted q-cumulants. function QCnQnk should be called first
1285     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
1286     return (Double_t) fNAcceptedTracksQCn;
1287 }
1288 //_____________________________________________________________________________
1289 Double_t AliAnalysisTaskJetV2::QCnM11() {
1290     // get multiplicity weights for the weighted two particle cumulant
1291     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
1292     return (QCnS(2,1) - QCnS(1,2));
1293 }
1294 //_____________________________________________________________________________
1295 Double_t AliAnalysisTaskJetV2::QCnM1111() {
1296     // get multiplicity weights for the weighted four particle cumulant
1297     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
1298     return (QCnS(4,1)-6*QCnS(1,2)*QCnS(2,1)+8*QCnS(1,3)*QCnS(1,1)+3*QCnS(2,2)-6*QCnS(1,4));
1299 }
1300 //_____________________________________________________________________________
1301 Bool_t AliAnalysisTaskJetV2::QCnRecovery(Double_t psi2, Double_t psi3) {
1302     // decides how to deal with the situation where c2 or c3 is negative 
1303     // returns kTRUE depending on whether or not a modulated rho is used for the jet background
1304     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
1305     if(TMath::AreEqualAbs(fFitModulation->GetParameter(3), .0, 1e-10) && TMath::AreEqualAbs(fFitModulation->GetParameter(7), .0,1e-10)) {
1306         fFitModulation->SetParameter(7, 0);
1307         fFitModulation->SetParameter(3, 0);
1308         fFitModulation->SetParameter(0, fLocalRho->GetVal());
1309         return kTRUE;   // v2 and v3 have physical null values
1310     }
1311     switch (fQCRecovery) {
1312         case kFixedRho : {      // roll back to the original rho
1313            fFitModulation->SetParameter(7, 0);
1314            fFitModulation->SetParameter(3, 0);
1315            fFitModulation->SetParameter(0, fLocalRho->GetVal());
1316            return kFALSE;       // rho is forced to be fixed
1317         }
1318         case kNegativeVn : {
1319            Double_t c2(fFitModulation->GetParameter(3));
1320            Double_t c3(fFitModulation->GetParameter(7));
1321            if( c2 < 0 ) c2 = -1.*TMath::Sqrt(-1.*c2);
1322            if( c3 < 0 ) c3 = -1.*TMath::Sqrt(-1.*c3);
1323            fFitModulation->SetParameter(3, c2);
1324            fFitModulation->SetParameter(7, c3);
1325            return kTRUE;        // is this a physical quantity ?
1326         }
1327         case kTryFit : {
1328            fitModulationType tempType(fFitModulationType);  // store temporarily
1329            fFitModulationType = kCombined;
1330            fFitModulation->SetParameter(7, 0);
1331            fFitModulation->SetParameter(3, 0);
1332            Bool_t pass(CorrectRho(psi2, psi3));         // do the fit and all quality checks
1333            fFitModulationType = tempType;               // roll back for next event
1334            return pass;
1335         }
1336         default : return kFALSE;
1337     }
1338     return kFALSE;
1339 }
1340 //_____________________________________________________________________________
1341 Bool_t AliAnalysisTaskJetV2::CorrectRho(Double_t psi2, Double_t psi3) 
1342 {
1343     // get rho' -> rho(phi)
1344     // two routines are available, both can be used with or without pt weights
1345     //  [1] get vn from q-cumulants or as an integrated value from a user supplied histogram
1346     //      in case of cumulants, both cumulants and vn values are stored. in both cases, v2 and v3
1347     //      are expected. a check is performed to see if rho has no negative local minimum
1348     //      for full description, see Phys. Rev. C 83, 044913
1349     //      since the cn distribution has negative values, vn = sqrt(cn) can be imaginary sometimes
1350     //      in this case one can either roll back to the 'original' rixed rho, do a fit for vn or take use
1351     //      vn = - sqrt(|cn|) 
1352     //  [2] fitting a fourier expansion to the de/dphi distribution
1353     //      the fit can be done with either v2, v3 or a combination.
1354     //      in all cases, a cut can be made on the p-value of the chi-squared value of the fit
1355     //      and a check can be performed to see if rho has no negative local minimum
1356     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
1357     Int_t freeParams(2);                // free parameters of the fit (for NDF) 
1358     switch (fFitModulationType) {       // for approaches where no fitting is required
1359         case kQC2 : {
1360             fFitModulation->FixParameter(4, psi2); 
1361             fFitModulation->FixParameter(6, psi3);
1362             fFitModulation->FixParameter(3, CalculateQC2(2));   // set here with cn, vn = sqrt(cn)
1363             fFitModulation->FixParameter(7, CalculateQC2(3));
1364             // first fill the histos of the raw cumulant distribution
1365             if (fUsePtWeight) { // use weighted weights
1366                 Double_t dQCnM11 = (fNoEventWeightsForQC) ? 1. : QCnM11();
1367                 fProfV2Cumulant->Fill(fCent, fFitModulation->GetParameter(3), dQCnM11);
1368                 fProfV3Cumulant->Fill(fCent, fFitModulation->GetParameter(7), dQCnM11);
1369             } else {
1370                 Double_t dQCnM = (fNoEventWeightsForQC) ? 2. : QCnM();
1371                 fProfV2Cumulant->Fill(fCent, fFitModulation->GetParameter(3), dQCnM*(dQCnM-1));
1372                 fProfV3Cumulant->Fill(fCent, fFitModulation->GetParameter(7), dQCnM*(dQCnM-1));
1373             }
1374             // then see if one of the cn value is larger than zero and vn is readily available
1375             if(fFitModulation->GetParameter(3) > 0 && fFitModulation->GetParameter(7) > 0) {
1376                 fFitModulation->FixParameter(3, TMath::Sqrt(fFitModulation->GetParameter(3)));
1377                 fFitModulation->FixParameter(7, TMath::Sqrt(fFitModulation->GetParameter(7)));
1378             } else if (!QCnRecovery(psi2, psi3)) return kFALSE;  // try to recover the cumulant, this will set v2 and v3
1379             if(fFitModulation->GetMinimum(0, TMath::TwoPi()) < 0) {  // general check 
1380                 fFitModulation->SetParameter(7, 0);
1381                 fFitModulation->SetParameter(3, 0);
1382                 fFitModulation->SetParameter(0, fLocalRho->GetVal());
1383                 return kFALSE;
1384             }
1385             return kTRUE;
1386         } break;
1387         case kQC4 : {
1388             fFitModulation->FixParameter(4, psi2); 
1389             fFitModulation->FixParameter(6, psi3);
1390             fFitModulation->FixParameter(3, CalculateQC4(2));   // set here with cn, vn = sqrt(cn)
1391             fFitModulation->FixParameter(7, CalculateQC4(3));
1392             // first fill the histos of the raw cumulant distribution
1393             if (fUsePtWeight) { // use weighted weights
1394                 fProfV2Cumulant->Fill(fCent, fFitModulation->GetParameter(3)/*, QCnM1111()*/);
1395                 fProfV3Cumulant->Fill(fCent, fFitModulation->GetParameter(7)/*, QCnM1111()*/);
1396             } else {
1397                 fProfV2Cumulant->Fill(fCent, fFitModulation->GetParameter(3)/*, QCnM1111()*/);
1398                 fProfV3Cumulant->Fill(fCent, fFitModulation->GetParameter(7)/*, QCnM1111()*/);
1399             }
1400             // then see if one of the cn value is larger than zero and vn is readily available
1401             if(fFitModulation->GetParameter(3) > 0 && fFitModulation->GetParameter(7) > 0) {
1402                 fFitModulation->FixParameter(3, TMath::Sqrt(fFitModulation->GetParameter(3)));
1403                 fFitModulation->FixParameter(7, TMath::Sqrt(fFitModulation->GetParameter(7)));
1404             } else if (!QCnRecovery(psi2, psi3)) return kFALSE;  // try to recover the cumulant, this will set v2 and v3
1405             if(fFitModulation->GetMinimum(0, TMath::TwoPi()) < 0) {  // general check 
1406                 fFitModulation->SetParameter(7, 0);
1407                 fFitModulation->SetParameter(3, 0);
1408                 fFitModulation->SetParameter(0, fLocalRho->GetVal());
1409                 return kFALSE;
1410             }
1411         } break;
1412         case kIntegratedFlow : {
1413             // use v2 and v3 values from an earlier iteration over the data
1414             fFitModulation->FixParameter(3, fUserSuppliedV2->GetBinContent(fUserSuppliedV2->GetXaxis()->FindBin(fCent)));
1415             fFitModulation->FixParameter(4, psi2);
1416             fFitModulation->FixParameter(6, psi3);
1417             fFitModulation->FixParameter(7, fUserSuppliedV3->GetBinContent(fUserSuppliedV3->GetXaxis()->FindBin(fCent)));
1418             if(fFitModulation->GetMinimum(0, TMath::TwoPi()) < 0) { 
1419                 fFitModulation->SetParameter(7, 0);
1420                 fFitModulation->SetParameter(3, 0);
1421                 fFitModulation->SetParameter(0, fLocalRho->GetVal());
1422                 return kFALSE;
1423             }
1424             return kTRUE;
1425         }
1426         default : break;
1427     }
1428     TString detector("");
1429     switch (fDetectorType) {
1430         case kTPC : detector+="TPC";
1431             break;
1432         case kVZEROA : detector+="VZEROA";
1433             break;
1434         case kVZEROC : detector+="VZEROC";
1435             break;
1436         case kVZEROComb : detector+="VZEROComb";
1437             break; 
1438         case kFixedEP : detector+="FixedEP";
1439             break;
1440         default: break;
1441     }
1442     Int_t iTracks(fTracks->GetEntriesFast());
1443     Double_t excludeInEta = -999;
1444     Double_t excludeInPhi = -999;
1445     Double_t excludeInPt  = -999;
1446     if(iTracks <= 0 || fLocalRho->GetVal() <= 0 ) return kFALSE;   // no use fitting an empty event ...
1447     if(fExcludeLeadingJetsFromFit > 0 ) {
1448         if(fLeadingJet) {
1449             excludeInEta = fLeadingJet->Eta();
1450             excludeInPhi = fLeadingJet->Phi();
1451             excludeInPt = fLeadingJet->Pt();
1452         }
1453     }
1454     // check the acceptance of the track selection that will be used
1455     // if one uses e.g. semi-good tpc tracks, accepance in phi is reduced to 0 < phi < 4
1456     // the defaults (-10 < phi < 10) which accept all, are then overwritten
1457     Double_t lowBound(0.), upBound(TMath::TwoPi());     // bounds for fit
1458     if(GetParticleContainer()->GetParticlePhiMin() > lowBound) lowBound = GetParticleContainer()->GetParticlePhiMin();
1459     if(GetParticleContainer()->GetParticlePhiMax() < upBound) upBound = GetParticleContainer()->GetParticlePhiMax();
1460     fHistSwap->Reset(); // clear the histogram
1461     TH1F _tempSwap;     // on stack for quick access
1462     TH1F _tempSwapN;    // on stack for quick access, bookkeeping histogram
1463     if(fRebinSwapHistoOnTheFly) {
1464         if(fNAcceptedTracks < 49) fNAcceptedTracks = 49;       // avoid aliasing effects
1465         _tempSwap = TH1F("_tempSwap", "_tempSwap", TMath::CeilNint(TMath::Sqrt(fNAcceptedTracks)), lowBound, upBound);
1466         if(fUsePtWeightErrorPropagation) _tempSwapN = TH1F("_tempSwapN", "_tempSwapN", TMath::CeilNint(TMath::Sqrt(fNAcceptedTracks)), lowBound, upBound);
1467         if(fUsePtWeight) _tempSwap.Sumw2();
1468     }
1469     else _tempSwap = *fHistSwap;         // now _tempSwap holds the desired histo
1470     // non poissonian error when using pt weights
1471     Double_t totalpts(0.), totalptsquares(0.), totalns(0.);
1472     for(Int_t i(0); i < iTracks; i++) {
1473         AliVTrack* track = static_cast<AliVTrack*>(fTracks->At(i));
1474         if(fExcludeLeadingJetsFromFit > 0 &&( (TMath::Abs(track->Eta() - excludeInEta) < GetJetContainer()->GetJetRadius()*fExcludeLeadingJetsFromFit ) || (TMath::Abs(track->Eta()) - GetJetContainer()->GetJetRadius() - GetJetContainer()->GetJetEtaMax() ) > 0 )) continue;
1475         if(!PassesCuts(track) || track->Pt() > fSoftTrackMaxPt || track->Pt() < fSoftTrackMinPt) continue;
1476         if(fUsePtWeight) {
1477             _tempSwap.Fill(track->Phi(), track->Pt());
1478             if(fUsePtWeightErrorPropagation) {
1479                 totalpts += track->Pt();
1480                 totalptsquares += track->Pt()*track->Pt();
1481                 totalns += 1;
1482                 _tempSwapN.Fill(track->Phi());
1483             }
1484         }
1485         else _tempSwap.Fill(track->Phi());
1486     }
1487     if(fUsePtWeight && fUsePtWeightErrorPropagation) {
1488         // in the case of pt weights overwrite the poissonian error estimate which is assigned by root by a more sophisticated appraoch
1489         // the assumption here is that the bin error will be dominated by the uncertainty in the mean pt in a bin and in the uncertainty
1490         // of the number of tracks in a bin, the first of which will be estimated from the sample standard deviation of all tracks in the 
1491         // event, for the latter use a poissonian estimate. the two contrubitions are assumed to be uncorrelated
1492         if(totalns < 2) return kFALSE; // not one track passes the cuts > 2 avoids possible division by 0 later on
1493         for(Int_t l = 0; l < _tempSwap.GetNbinsX(); l++) {
1494             if(_tempSwapN.GetBinContent(l+1) == 0) {
1495                 _tempSwap.SetBinContent(l+1,0);
1496                 _tempSwap.SetBinError(l+1,0);
1497             }
1498             else {
1499                 Double_t vartimesnsq = totalptsquares*totalns - totalpts*totalpts;
1500                 Double_t variance = vartimesnsq/(totalns*(totalns-1.));
1501                 Double_t SDOMSq = variance / _tempSwapN.GetBinContent(l+1);
1502                 Double_t SDOMSqOverMeanSq = SDOMSq * _tempSwapN.GetBinContent(l+1) * _tempSwapN.GetBinContent(l+1) / (_tempSwapN.GetBinContent(l+1) * _tempSwapN.GetBinContent(l+1));
1503                 Double_t poissonfrac = 1./_tempSwapN.GetBinContent(l+1);
1504                 Double_t vartotalfrac = SDOMSqOverMeanSq + poissonfrac;
1505                 Double_t vartotal = vartotalfrac * _tempSwap.GetBinContent(l+1) * _tempSwap.GetBinContent(l+1);
1506                 if(vartotal > 0.0001) _tempSwap.SetBinError(l+1,TMath::Sqrt(vartotal));
1507                 else {
1508                     _tempSwap.SetBinContent(l+1,0);
1509                     _tempSwap.SetBinError(l+1,0);
1510                 }
1511             }
1512         }
1513     }
1514     fFitModulation->SetParameter(0, fLocalRho->GetVal());
1515     switch (fFitModulationType) {
1516         case kNoFit : { 
1517             fFitModulation->FixParameter(0, fLocalRho->GetVal() ); 
1518             freeParams = 0;
1519         } break;
1520         case kV2 : { 
1521             fFitModulation->FixParameter(4, psi2); 
1522             freeParams = 1;
1523         } break;
1524         case kV3 : { 
1525             fFitModulation->FixParameter(4, psi3); 
1526             freeParams = 1;
1527         } break;
1528         case kCombined : {
1529             fFitModulation->FixParameter(4, psi2); 
1530             fFitModulation->FixParameter(6, psi3);
1531             freeParams = 2;
1532         } break;
1533         case kFourierSeries : {
1534             // in this approach, an explicit calculation will be made of vn = sqrt(xn^2+yn^2)
1535             // where x[y] = Integrate[r(phi)cos[sin](n phi)dphi, 0, 2pi]
1536             Double_t cos2(0), sin2(0), cos3(0), sin3(0), sumPt(0);
1537             for(Int_t i(0); i < iTracks; i++) {
1538                 AliVTrack* track = static_cast<AliVTrack*>(fTracks->At(i));
1539                 if(!PassesCuts(track) || track->Pt() > fSoftTrackMaxPt || track->Pt() < fSoftTrackMinPt) continue;
1540                 sumPt += track->Pt();
1541                 cos2 += track->Pt()*TMath::Cos(2*PhaseShift(track->Phi()-psi2)); 
1542                 sin2 += track->Pt()*TMath::Sin(2*PhaseShift(track->Phi()-psi2));
1543                 cos3 += track->Pt()*TMath::Cos(3*PhaseShift(track->Phi()-psi3)); 
1544                 sin3 += track->Pt()*TMath::Sin(3*PhaseShift(track->Phi()-psi3));
1545             }
1546             fFitModulation->SetParameter(3, TMath::Sqrt(cos2*cos2+sin2*sin2)/fLocalRho->GetVal());
1547             fFitModulation->SetParameter(4, psi2);
1548             fFitModulation->SetParameter(6, psi3);
1549             fFitModulation->SetParameter(7, TMath::Sqrt(cos3*cos3+sin3*sin3)/fLocalRho->GetVal());
1550         } break;
1551         default : break;
1552     }
1553     if(fRunToyMC) {
1554         // toy mc, just here to check procedure, azimuthal profile is filled from hypothesis so p-value distribution should be flat
1555         Int_t _bins = _tempSwap.GetXaxis()->GetNbins();
1556         TF1* _tempFit = new TF1("temp_fit_kCombined", "[0]*([1]+[2]*([3]*TMath::Cos([2]*(x-[4]))+[7]*TMath::Cos([5]*(x-[6]))))", 0, TMath::TwoPi());
1557         _tempFit->SetParameter(0, fFitModulation->GetParameter(0));       // normalization
1558         _tempFit->SetParameter(3, 0.1);      // v2
1559         _tempFit->FixParameter(1, 1.);       // constant
1560         _tempFit->FixParameter(2, 2.);       // constant
1561         _tempFit->FixParameter(5, 3.);       // constant
1562         _tempFit->FixParameter(4, fFitModulation->GetParameter(4));
1563         _tempFit->FixParameter(6, fFitModulation->GetParameter(6));
1564         _tempFit->SetParameter(7, 0.1);      // v3
1565         _tempSwap.Reset();                   // rese bin content
1566         for(int _binsI = 0; _binsI < _bins*_bins; _binsI++)  _tempSwap.Fill(_tempFit->GetRandom());
1567     }
1568     _tempSwap.Fit(fFitModulation, fFitModulationOptions.Data(), "", lowBound, upBound);
1569     // the quality of the fit is evaluated from 1 - the cdf of the chi square distribution
1570     // three methods are available, all with their drawbacks. all are stored, one is selected to do the cut
1571     Int_t NDF(_tempSwap.GetXaxis()->GetNbins()-freeParams);
1572     if(NDF == 0 || (float)NDF <= 0.) return kFALSE;
1573     Double_t CDF(1.-ChiSquareCDF(NDF, ChiSquare(_tempSwap, fFitModulation)));
1574     Double_t CDFROOT(1.-ChiSquareCDF(NDF, fFitModulation->GetChisquare()));
1575     Double_t CDFKolmogorov(KolmogorovTest(_tempSwap, fFitModulation));
1576     // fill the values and centrality correlation (redundant but easy on the eyes)
1577     fHistPvalueCDF->Fill(CDF);
1578     fHistPvalueCDFCent->Fill(fCent, CDF);
1579     fHistPvalueCDFROOT->Fill(CDFROOT);
1580     fHistPvalueCDFROOTCent->Fill(fCent, CDFROOT);
1581     fHistKolmogorovTest->Fill(CDFKolmogorov);
1582     fHistChi2ROOTCent->Fill(fCent, fFitModulation->GetChisquare()/((float)NDF));
1583     fHistChi2Cent->Fill(fCent, ChiSquare(_tempSwap, fFitModulation)/((float)NDF));
1584     fHistKolmogorovTestCent->Fill(fCent, CDFKolmogorov);
1585     fHistPChi2Root->Fill(CDFROOT, fFitModulation->GetChisquare()/((float)NDF));
1586     fHistPChi2->Fill(CDF, ChiSquare(_tempSwap, fFitModulation)/((float)NDF));
1587     fHistPKolmogorov->Fill(CDF, CDFKolmogorov);
1588
1589     // variable CDF is used for making cuts, so we fill it with the selected p-value
1590     switch (fFitGoodnessTest) {
1591         case kChi2ROOT : {
1592             CDF = CDFROOT; 
1593         } break;
1594         case kChi2Poisson : break;      // CDF is already CDF
1595         case kKolmogorov : {
1596             CDF = CDFKolmogorov; 
1597         } break;
1598         default: break;
1599     }
1600
1601     if(fFitControl) {
1602         // as an additional quality check, see if fitting a control fit has a higher significance
1603         _tempSwap.Fit(fFitControl, fFitModulationOptions.Data(), "", lowBound, upBound);
1604         Double_t CDFControl(-1.);
1605         switch (fFitGoodnessTest) {
1606             case kChi2ROOT : {
1607                 CDFControl = 1.-ChiSquareCDF(fFitControl->GetNDF(), fFitModulation->GetChisquare());
1608             } break;
1609             case kChi2Poisson : {
1610                 CDFControl = 1.-ChiSquareCDF(fFitControl->GetNDF(), ChiSquare(_tempSwap, fFitModulation));
1611             } break;
1612             case kKolmogorov : {
1613                 CDFControl = KolmogorovTest(_tempSwap, fFitControl); 
1614             } break;
1615             default: break;
1616         }
1617         if(CDFControl > CDF) {
1618             CDF = -1.; // control fit is more significant, so throw out the 'old' fit
1619             fHistRhoStatusCent->Fill(fCent, -1);
1620         }
1621     }
1622     if(CDF >= fMinPvalue && CDF <= fMaxPvalue && ( fFitModulation->GetMinimum(0, TMath::TwoPi()) > 0)) {       
1623         // fit quality. not that although with limited acceptance the fit is performed on just
1624         // part of phase space, the requirement that energy desntiy is larger than zero is applied
1625         // to the FULL spectrum
1626         fHistRhoStatusCent->Fill(fCent, 0.);
1627         // for LOCAL didactic purposes, save the  best and the worst fits
1628         // this routine can produce a lot of output histograms (it's not memory 'safe') and will not work on GRID 
1629         // since the output will become unmergeable (i.e. different nodes may produce conflicting output)
1630         switch (fRunModeType) {
1631             case kLocal : {
1632                 if(fRandom->Uniform(0, 100) > fPercentageOfFits) break;
1633                 static Int_t didacticCounterBest(0);
1634                 TProfile* didacticProfile = (TProfile*)_tempSwap.Clone(Form("Fit_%i_1-CDF_%.3f_cen_%i_%s", didacticCounterBest, CDF, fInCentralitySelection, detector.Data()));
1635                 TF1* didacticFit = (TF1*)fFitModulation->Clone(Form("fit_%i_CDF_%.3f_cen_%i_%s", didacticCounterBest, CDF, fInCentralitySelection, detector.Data()));
1636                 switch(fFitModulationType) { 
1637                     case kCombined : {
1638                         // to make a nice picture also plot the separate components (v2 and v3) of the fit
1639                         // only done for cobined fit where there are actually components to split ...
1640                         TF1* v0(new TF1("dfit_kV2", "[0]", 0, TMath::TwoPi()));
1641                         v0->SetParameter(0, didacticFit->GetParameter(0));        // normalization
1642                         v0->SetLineColor(kMagenta);
1643                         v0->SetLineStyle(7);
1644                         didacticProfile->GetListOfFunctions()->Add(v0);
1645                         TF1* v2(new TF1("dfit_kV2", "[0]*([1]+[2]*[3]*TMath::Cos([2]*(x-[4])))", 0, TMath::TwoPi()));
1646                         v2->SetParameter(0, didacticFit->GetParameter(0));        // normalization
1647                         v2->SetParameter(3, didacticFit->GetParameter(3));        // v2
1648                         v2->FixParameter(1, 1.);        // constant
1649                         v2->FixParameter(2, 2.);        // constant
1650                         v2->FixParameter(4, didacticFit->GetParameter(4));        // psi2
1651                         v2->SetLineColor(kGreen);
1652                         didacticProfile->GetListOfFunctions()->Add(v2);
1653                         TF1* v3(new TF1("dfit_kV3", "[0]*([1]+[2]*[3]*TMath::Cos([5]*(x-[4])))", 0, TMath::TwoPi()));
1654                         v3->SetParameter(0, didacticFit->GetParameter(0));        // normalization
1655                         v3->SetParameter(3, didacticFit->GetParameter(7));        // v3
1656                         v3->FixParameter(1, 1.);        // constant
1657                         v3->FixParameter(2, 2.);        // constant
1658                         v3->FixParameter(4, didacticFit->GetParameter(6));        // psi3
1659                         v3->FixParameter(5, 3.);        // constant
1660                         v3->SetLineColor(kCyan);
1661                         didacticProfile->GetListOfFunctions()->Add(v3);
1662                     }
1663                     default : break;
1664                 }
1665                 didacticProfile->GetListOfFunctions()->Add(didacticFit);
1666                 didacticProfile->GetYaxis()->SetTitle("#frac{d #sum #it{p}_{T}}{d #varphi} [GeV/#it{c}]");
1667                 didacticProfile->GetXaxis()->SetTitle("#varphi");
1668                 fOutputListGood->Add(didacticProfile);
1669                 didacticCounterBest++;
1670                 TH2F* didacticSurface = BookTH2F(Form("surface_%s", didacticProfile->GetName()), "#phi", "#eta", 50, 0, TMath::TwoPi(), 50, -1, 1, -1, kFALSE);
1671                 for(Int_t i(0); i < iTracks; i++) {
1672                     AliVTrack* track = static_cast<AliVTrack*>(fTracks->At(i));
1673                     if(PassesCuts(track)) {
1674                         if(fUsePtWeight) didacticSurface->Fill(track->Phi(), track->Eta(), track->Pt());
1675                         else didacticSurface->Fill(track->Phi(), track->Eta());
1676                     }
1677                 }
1678                 if(fExcludeLeadingJetsFromFit) {       // visualize the excluded region
1679                     TF2 *f2 = new TF2(Form("%s_LJ", didacticSurface->GetName()),"[0]*TMath::Gaus(x,[1],[2])*TMath::Gaus(y,[3],[4])", 0, TMath::TwoPi(), -1, 1);
1680                     f2->SetParameters(excludeInPt/3.,excludeInPhi,.1,excludeInEta,.1);
1681                     didacticSurface->GetListOfFunctions()->Add(f2);
1682                 }
1683                 fOutputListGood->Add(didacticSurface);
1684             } break;
1685             default : break;
1686         }
1687     } else {    // if the fit is of poor quality revert to the original rho estimate
1688         switch (fRunModeType) { // again see if we want to save the fit
1689             case kLocal : {
1690                 static Int_t didacticCounterWorst(0);
1691                 if(fRandom->Uniform(0, 100) > fPercentageOfFits) break;
1692                 TProfile* didacticProfile = (TProfile*)_tempSwap.Clone(Form("Fit_%i_1-CDF_%.3f_cen_%i_%s", didacticCounterWorst, CDF, fInCentralitySelection, detector.Data() ));
1693                 TF1* didacticFit = (TF1*)fFitModulation->Clone(Form("fit_%i_p_%.3f_cen_%i_%s", didacticCounterWorst, CDF, fInCentralitySelection, detector.Data()));
1694                 didacticProfile->GetListOfFunctions()->Add(didacticFit);
1695                 fOutputListBad->Add(didacticProfile);
1696                 didacticCounterWorst++;
1697                 } break;
1698             default : break;
1699         }
1700         switch (fFitModulationType) {
1701             case kNoFit : break;        // nothing to do
1702             case kCombined : fFitModulation->SetParameter(7, 0);        // no break
1703             case kFourierSeries : fFitModulation->SetParameter(7, 0);   // no break
1704             default : { // needs to be done if there was a poor fit
1705                  fFitModulation->SetParameter(3, 0);
1706                  fFitModulation->SetParameter(0, fLocalRho->GetVal());
1707             } break;
1708         }
1709         if(CDF > -.5) fHistRhoStatusCent->Fill(fCent, 1.);
1710         return kFALSE;  // return false if the fit is rejected
1711     }
1712     return kTRUE;
1713 }
1714 //_____________________________________________________________________________
1715 Bool_t AliAnalysisTaskJetV2::PassesCuts(AliVEvent* event)
1716 {
1717     // event cuts
1718     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
1719     switch (fCollisionType) {
1720         case kJetFlowMC : {
1721             fInCentralitySelection = 0;
1722             return kTRUE;
1723     } break;
1724         default : break;
1725     }
1726     if(!event || !AliAnalysisTaskEmcal::IsEventSelected()) return kFALSE;
1727     if(TMath::Abs(InputEvent()->GetPrimaryVertex()->GetZ()) > 10.) return kFALSE;
1728     // aod and esd specific checks
1729     switch (fDataType) {
1730        case kESD: {
1731             AliESDEvent* esdEvent = static_cast<AliESDEvent*>(InputEvent());
1732             if( (!esdEvent) || (TMath::Abs(esdEvent->GetPrimaryVertexSPD()->GetZ() - esdEvent->GetPrimaryVertex()->GetZ()) > .5) ) return kFALSE; 
1733        } break;
1734        case kAOD: {
1735             AliAODEvent* aodEvent = static_cast<AliAODEvent*>(InputEvent());
1736             if( (!aodEvent) || (TMath::Abs(aodEvent->GetPrimaryVertexSPD()->GetZ() - aodEvent->GetPrimaryVertex()->GetZ()) > .5) ) return kFALSE; 
1737        } break;
1738        default: break;
1739     }
1740     fCent = InputEvent()->GetCentrality()->GetCentralityPercentile("V0M");
1741     if(fCent <= fCentralityClasses->At(0) || fCent >= fCentralityClasses->At(fCentralityClasses->GetSize()-1) || TMath::Abs(fCent-InputEvent()->GetCentrality()->GetCentralityPercentile("TRK")) > 5.) return kFALSE;
1742     // determine centrality class
1743     fInCentralitySelection = -1;
1744     for(Int_t i(0); i < fCentralityClasses->GetSize()-1; i++) {
1745         if(fCent >= fCentralityClasses->At(i) && fCent <= fCentralityClasses->At(1+i)) {
1746             fInCentralitySelection = i;
1747             break; }
1748     } 
1749     if(fInCentralitySelection<0) return kFALSE;     // should be null op
1750     // see if input containers are filled
1751     if(fTracks->GetEntries() < 1) return kFALSE;
1752     if(fRho->GetVal() <= 0 ) return kFALSE;
1753     if(fAnalysisType == AliAnalysisTaskJetV2::kFull && !fClusterCont) return kFALSE;
1754     return kTRUE;
1755 }
1756 //_____________________________________________________________________________
1757 void AliAnalysisTaskJetV2::FillHistogramsAfterSubtraction(Double_t psi2, Double_t vzero[2][2], Double_t* vzeroComb, Double_t* tpc)
1758 {
1759     // fill histograms 
1760     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
1761     FillTrackHistograms();
1762     if(fAnalysisType == AliAnalysisTaskJetV2::kFull) FillClusterHistograms();
1763     FillJetHistograms(psi2); 
1764     if(fFillQAHistograms) FillEventPlaneHistograms(vzero, vzeroComb, tpc);
1765     FillRhoHistograms();
1766     FillDeltaPtHistograms(psi2);
1767 }
1768 //_____________________________________________________________________________
1769 void AliAnalysisTaskJetV2::FillTrackHistograms() const
1770 {
1771     // fill track histograms
1772     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
1773     Int_t iTracks(fTracks->GetEntriesFast()), iAcceptedTracks(0);
1774     for(Int_t i(0); i < iTracks; i++) {
1775         AliVTrack* track = static_cast<AliVTrack*>(fTracks->At(i));
1776         if(!PassesCuts(track)) continue;
1777         iAcceptedTracks++;
1778         fHistPicoTrackPt[fInCentralitySelection]->Fill(track->Pt());
1779         if(fFillQAHistograms) FillQAHistograms(track);
1780     }
1781     fHistPicoTrackMult[fInCentralitySelection]->Fill(iAcceptedTracks);
1782 }
1783 //_____________________________________________________________________________
1784 void AliAnalysisTaskJetV2::FillClusterHistograms() const
1785 {
1786     // fill cluster histograms
1787     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
1788     if(!fClusterCont) return;
1789     Int_t iClusters(fClusterCont->GetNClusters());
1790     TLorentzVector clusterLorentzVector;
1791     for(Int_t i(0); i < iClusters; i++) {
1792         AliVCluster* cluster = fClusterCont->GetCluster(i);
1793         if (!PassesCuts(cluster)) continue;
1794         cluster->GetMomentum(clusterLorentzVector, const_cast<Double_t*>(fVertex));
1795         fHistClusterPt[fInCentralitySelection]->Fill(clusterLorentzVector.Pt());
1796         fHistClusterEtaPhi[fInCentralitySelection]->Fill(clusterLorentzVector.Eta(), clusterLorentzVector.Phi());
1797         fHistClusterEtaPhiWeighted[fInCentralitySelection]->Fill(clusterLorentzVector.Eta(), clusterLorentzVector.Phi(), clusterLorentzVector.Pt());
1798     }
1799     return;
1800 }
1801 //_____________________________________________________________________________
1802 void AliAnalysisTaskJetV2::FillEventPlaneHistograms(Double_t vzero[2][2], Double_t* vzeroComb, Double_t* tpc) const
1803 {
1804     // fill event plane histograms, only called in qa mode
1805     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
1806     fHistPsiControl->Fill(0.5, vzero[0][0]);    // vzero a psi2
1807     fHistPsiControl->Fill(1.5, vzero[1][0]);    // vzero c psi2
1808     fHistPsiControl->Fill(2.5, tpc[0]);         // tpc psi 2
1809     fHistPsiControl->Fill(5.5, vzero[0][1]);    // vzero a psi3
1810     fHistPsiControl->Fill(6.5, vzero[1][1]);    // vzero b psi3
1811     fHistPsiControl->Fill(7.5, tpc[1]);         // tpc psi 3
1812     fHistPsiVZEROA->Fill(vzero[0][0]);
1813     fHistPsiVZEROC->Fill(vzero[1][0]);
1814     fHistPsiVZERO->Fill(vzeroComb[0]);
1815     fHistPsiTPC->Fill(tpc[0]);
1816     fHistPsiSpread->Fill(0.5, TMath::Abs(vzero[0][0]-vzero[1][0]));
1817     fHistPsiSpread->Fill(1.5, TMath::Abs(vzero[0][0]-tpc[0]));
1818     fHistPsiSpread->Fill(2.5, TMath::Abs(vzero[1][0]-tpc[0]));
1819     // event plane vs centrality QA histo's to check recentering
1820     Double_t TRK(InputEvent()->GetCentrality()->GetCentralityPercentile("TRK"));
1821     Double_t V0M(InputEvent()->GetCentrality()->GetCentralityPercentile("V0M"));
1822     fHistPsiVZEROAV0M->Fill(V0M, vzero[0][0]);
1823     fHistPsiVZEROCV0M->Fill(V0M, vzero[1][0]);
1824     fHistPsiVZEROVV0M->Fill(V0M, vzeroComb[0]);
1825     fHistPsiTPCV0M->Fill(V0M, tpc[0]);
1826     fHistPsiVZEROATRK->Fill(TRK, vzero[0][0]);
1827     fHistPsiVZEROCTRK->Fill(TRK, vzero[1][0]);
1828     fHistPsiVZEROTRK->Fill(TRK, vzeroComb[0]);
1829     fHistPsiTPCTRK->Fill(TRK, tpc[0]);
1830     // leading jet vs event plane bias
1831     if(fLeadingJet) {
1832         Double_t rho(fLocalRho->GetLocalVal(fLeadingJet->Phi(), GetJetContainer()->GetJetRadius(), fLocalRho->GetVal()));
1833         Double_t pt(fLeadingJet->Pt() - fLeadingJet->Area()*rho);
1834         fHistPsiTPCLeadingJet[fInCentralitySelection]->Fill(pt, tpc[0], fLeadingJet->Phi());
1835         fHistPsiVZEROALeadingJet[fInCentralitySelection]->Fill(pt, vzero[0][0], fLeadingJet->Phi());
1836         fHistPsiVZEROCLeadingJet[fInCentralitySelection]->Fill(pt, vzero[1][0], fLeadingJet->Phi());
1837         fHistPsiVZEROCombLeadingJet[fInCentralitySelection]->Fill(pt, vzeroComb[0], fLeadingJet->Phi());
1838     }
1839     // correlation of event planes
1840     fHistPsi2Correlation[fInCentralitySelection]->Fill(tpc[0], vzero[0][0], vzero[1][0]);
1841 }
1842 //_____________________________________________________________________________
1843 void AliAnalysisTaskJetV2::FillRhoHistograms()
1844 {
1845     // fill rho histograms
1846     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
1847     fHistRhoPackage[fInCentralitySelection]->Fill(fLocalRho->GetVal());    // save the rho estimate from the emcal jet package
1848     // get multiplicity FIXME inefficient
1849     Int_t iJets(fJets->GetEntriesFast());
1850     Double_t rho(fLocalRho->GetLocalVal(TMath::Pi(), TMath::Pi(), fLocalRho->GetVal()));
1851     fHistRho[fInCentralitySelection]->Fill(rho);
1852     fHistRhoVsMult->Fill(fTracks->GetEntries(), rho);
1853     fHistRhoVsCent->Fill(fCent, rho);
1854     for(Int_t i(0); i < iJets; i++) {
1855         AliEmcalJet* jet = static_cast<AliEmcalJet*>(fJets->At(i));
1856         if(!PassesCuts(jet)) continue;
1857         fHistRhoAVsMult->Fill(fTracks->GetEntries(), rho * jet->Area());
1858         fHistRhoAVsCent->Fill(fCent, rho * jet->Area());
1859     }
1860 }
1861 //_____________________________________________________________________________
1862 void AliAnalysisTaskJetV2::FillDeltaPtHistograms(Double_t psi2) const
1863 {
1864     // fill delta pt histograms
1865     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
1866     Int_t i(0);
1867     const Float_t areaRC = GetJetRadius()*GetJetRadius()*TMath::Pi();
1868     // we're retrieved the leading jet, now get a random cone
1869     for(i = 0; i < fMaxCones; i++) {
1870        Float_t pt(0), eta(0), phi(0);
1871        // get a random cone without constraints on leading jet position
1872        CalculateRandomCone(pt, eta, phi, fTracksCont, fClusterCont, 0x0);
1873        if(pt > 0) {
1874            if(fFillQAHistograms) fHistRCPhiEta[fInCentralitySelection]->Fill(phi, eta);
1875            fHistRhoVsRCPt[fInCentralitySelection]->Fill(pt, fLocalRho->GetLocalVal(phi, GetJetContainer()->GetJetRadius(), fLocalRho->GetVal())*areaRC);
1876            fHistRCPt[fInCentralitySelection]->Fill(pt);
1877            fHistDeltaPtDeltaPhi2[fInCentralitySelection]->Fill(PhaseShift(phi-psi2, 2.), pt - areaRC*fLocalRho->GetLocalVal(phi, GetJetContainer()->GetJetRadius(), fLocalRho->GetVal()));
1878            fHistDeltaPtDeltaPhi2Rho0[fInCentralitySelection]->Fill(PhaseShift(phi-psi2, 2.), pt - areaRC*fLocalRho->GetVal());
1879
1880        }
1881        // get a random cone excluding leading jet area
1882        CalculateRandomCone(pt, eta, phi, fTracksCont, fClusterCont, fLeadingJet);
1883        if(pt > 0) {
1884            if(fFillQAHistograms) fHistRCPhiEtaExLJ[fInCentralitySelection]->Fill(phi, eta);
1885            fHistRhoVsRCPtExLJ[fInCentralitySelection]->Fill(pt, fLocalRho->GetLocalVal(phi, GetJetContainer()->GetJetRadius(), fLocalRho->GetVal())*areaRC);
1886            fHistRCPtExLJ[fInCentralitySelection]->Fill(pt);
1887            fHistDeltaPtDeltaPhi2ExLJ[fInCentralitySelection]->Fill(PhaseShift(phi-psi2, 2.), pt - areaRC*fLocalRho->GetLocalVal(phi, GetJetContainer()->GetJetRadius(), fLocalRho->GetVal()));
1888            fHistDeltaPtDeltaPhi2ExLJRho0[fInCentralitySelection]->Fill(PhaseShift(phi-psi2, 2.), pt - areaRC*fLocalRho->GetVal());
1889        }
1890     } 
1891 }
1892 //_____________________________________________________________________________
1893 void AliAnalysisTaskJetV2::FillJetHistograms(Double_t psi2)
1894 {
1895     // fill jet histograms
1896     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
1897     Int_t iJets(fJets->GetEntriesFast());
1898     for(Int_t i(0); i < iJets; i++) {
1899         AliEmcalJet* jet = static_cast<AliEmcalJet*>(fJets->At(i));
1900         if(PassesCuts(jet)) {
1901             Double_t pt(jet->Pt()), area(jet->Area()), eta(jet->Eta()), phi(jet->Phi());
1902             Double_t rho(fLocalRho->GetLocalVal(phi, GetJetContainer()->GetJetRadius(), fLocalRho->GetVal()));
1903             fHistJetPtRaw[fInCentralitySelection]->Fill(pt);
1904             fHistJetPt[fInCentralitySelection]->Fill(pt-area*rho);
1905             if(fFillQAHistograms) fHistJetEtaPhi[fInCentralitySelection]->Fill(eta, phi);
1906             fHistJetPtArea[fInCentralitySelection]->Fill(pt-area*rho, area);
1907             fHistJetPtEta[fInCentralitySelection]->Fill(pt-area*rho, eta);
1908             fHistJetPsi2Pt[fInCentralitySelection]->Fill(PhaseShift(phi-psi2, 2.), pt-area*rho);
1909             fHistJetPsi2PtRho0[fInCentralitySelection]->Fill(PhaseShift(phi-psi2, 2.), pt-area*fLocalRho->GetVal());
1910             fHistJetPtConstituents[fInCentralitySelection]->Fill(pt-area*rho, jet->GetNumberOfConstituents());
1911             fHistJetEtaRho[fInCentralitySelection]->Fill(eta, pt/area);
1912         } 
1913     }
1914 }
1915 //_____________________________________________________________________________
1916 void AliAnalysisTaskJetV2::FillQAHistograms(AliVTrack* vtrack) const
1917 {
1918     // fill qa histograms for pico tracks
1919     if(fDebug > 1) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
1920     if(!vtrack) return;
1921     AliPicoTrack* track = static_cast<AliPicoTrack*>(vtrack);
1922     fHistRunnumbersPhi->Fill(fMappedRunNumber, track->Phi());
1923     fHistRunnumbersEta->Fill(fMappedRunNumber, track->Eta());
1924     Int_t type((int)(track->GetTrackType()));
1925     switch (type) {
1926         case 0:
1927            fHistPicoCat1[fInCentralitySelection]->Fill(track->Eta(), track->Phi()); 
1928            break;
1929         case 1:
1930            fHistPicoCat2[fInCentralitySelection]->Fill(track->Eta(), track->Phi()); 
1931            break;
1932         case 2:
1933            fHistPicoCat3[fInCentralitySelection]->Fill(track->Eta(), track->Phi()); 
1934            break;
1935         default: break;
1936     }
1937 }
1938 //_____________________________________________________________________________
1939 void AliAnalysisTaskJetV2::FillQAHistograms(AliVEvent* vevent) 
1940 {
1941     // fill qa histograms for events
1942     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
1943     if(!vevent) return;
1944     fHistVertexz->Fill(vevent->GetPrimaryVertex()->GetZ());
1945     fHistCentrality->Fill(fCent);
1946     Int_t runNumber(InputEvent()->GetRunNumber());
1947     for(fMappedRunNumber = 0; fMappedRunNumber < fExpectedRuns->GetSize(); fMappedRunNumber++) {
1948         if(fExpectedRuns->At(fMappedRunNumber) == runNumber) return;
1949     }
1950     if(fDebug > 0) printf("\n > TASK %s CANNOT IDENTIFY RUN - CONFIGURATION COULD BE INCORRECT < \n", GetName());
1951     if(fLeadingJet && fLeadingJetAfterSub) fHistLeadingJetBackground[fInCentralitySelection]->Fill(TMath::Abs(fLeadingJet->Eta()-fLeadingJetAfterSub->Eta()), PhaseShift(fLeadingJet->Phi()-fLeadingJetAfterSub->Phi()));
1952 }
1953 //_____________________________________________________________________________
1954 void AliAnalysisTaskJetV2::FillAnalysisSummaryHistogram() const
1955 {
1956     // fill the analysis summary histrogram, saves all relevant analysis settigns
1957     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
1958     fHistAnalysisSummary->GetXaxis()->SetBinLabel(2, "fJetRadius");
1959     fHistAnalysisSummary->SetBinContent(2, GetJetContainer()->GetJetRadius());
1960     fHistAnalysisSummary->GetXaxis()->SetBinLabel(3, "fJetEtaMin");
1961     fHistAnalysisSummary->SetBinContent(3, GetJetContainer()->GetJetEtaMin());
1962     fHistAnalysisSummary->GetXaxis()->SetBinLabel(4, "fJetEtaMax");
1963     fHistAnalysisSummary->SetBinContent(4, GetJetContainer()->GetJetEtaMax());
1964     fHistAnalysisSummary->GetXaxis()->SetBinLabel(5, "fJetPhiMin");
1965     fHistAnalysisSummary->SetBinContent(5, GetJetContainer()->GetJetPhiMin());
1966     fHistAnalysisSummary->GetXaxis()->SetBinLabel(6, "fJetPhiMax");
1967     fHistAnalysisSummary->SetBinContent(6, GetJetContainer()->GetJetPhiMin());
1968     fHistAnalysisSummary->GetXaxis()->SetBinLabel(16, "fForceBeamType");
1969     fHistAnalysisSummary->SetBinContent(16, fForceBeamType);
1970     fHistAnalysisSummary->GetXaxis()->SetBinLabel(17, "fMinCent");
1971     fHistAnalysisSummary->SetBinContent(17, fMinCent);
1972     fHistAnalysisSummary->GetXaxis()->SetBinLabel(18, "fMaxCent");
1973     fHistAnalysisSummary->SetBinContent(18, fMaxCent);
1974     fHistAnalysisSummary->GetXaxis()->SetBinLabel(19, "fMinVz");
1975     fHistAnalysisSummary->SetBinContent(19, fMinVz);
1976     fHistAnalysisSummary->GetXaxis()->SetBinLabel(20, "fMaxVz");
1977     fHistAnalysisSummary->SetBinContent(20, fMaxVz);
1978     fHistAnalysisSummary->GetXaxis()->SetBinLabel(21, "fOffTrigger");
1979     fHistAnalysisSummary->SetBinContent(21, fOffTrigger);
1980     fHistAnalysisSummary->GetXaxis()->SetBinLabel(34, "fitModulationType");
1981     fHistAnalysisSummary->SetBinContent(34, (int)fFitModulationType);
1982     fHistAnalysisSummary->GetXaxis()->SetBinLabel(35, "runModeType");
1983     fHistAnalysisSummary->SetBinContent(35, (int)fRunModeType);
1984     fHistAnalysisSummary->GetXaxis()->SetBinLabel(36, "data type");
1985     fHistAnalysisSummary->SetBinContent(36, (int)fDataType);
1986     fHistAnalysisSummary->GetXaxis()->SetBinLabel(37, "iterator");
1987     fHistAnalysisSummary->SetBinContent(37, 1.);
1988     fHistAnalysisSummary->GetXaxis()->SetBinLabel(38, "fMinPvalue");
1989     fHistAnalysisSummary->SetBinContent(38, fMinPvalue);
1990     fHistAnalysisSummary->GetXaxis()->SetBinLabel(39, "fMaxPvalue");
1991     fHistAnalysisSummary->SetBinContent(39, fMaxPvalue);
1992     fHistAnalysisSummary->GetXaxis()->SetBinLabel(40, "fExcludeLeadingJetsFromFit");
1993     fHistAnalysisSummary->SetBinContent(40, fExcludeLeadingJetsFromFit);
1994     fHistAnalysisSummary->GetXaxis()->SetBinLabel(41, "fRebinSwapHistoOnTheFly");
1995     fHistAnalysisSummary->SetBinContent(41, (int)fRebinSwapHistoOnTheFly);
1996     fHistAnalysisSummary->GetXaxis()->SetBinLabel(42, "fUsePtWeight");
1997     fHistAnalysisSummary->SetBinContent(42, (int)fUsePtWeight);
1998     fHistAnalysisSummary->GetXaxis()->SetBinLabel(44, "fSoftTrackMinPt");
1999     fHistAnalysisSummary->SetBinContent(44, fSoftTrackMinPt);
2000     fHistAnalysisSummary->GetXaxis()->SetBinLabel(45, "fSoftTrackMaxPt");
2001     fHistAnalysisSummary->SetBinContent(45, fSoftTrackMaxPt);
2002     fHistAnalysisSummary->GetXaxis()->SetBinLabel(46, "fMaxCones");
2003     fHistAnalysisSummary->SetBinContent(46, fMaxCones);
2004     fHistAnalysisSummary->GetXaxis()->SetBinLabel(47, "used rho");
2005     fHistAnalysisSummary->GetXaxis()->SetBinLabel(48, "used small rho");
2006 }
2007 //_____________________________________________________________________________
2008 void AliAnalysisTaskJetV2::Terminate(Option_t *)
2009 {
2010     // terminate
2011     switch (fRunModeType) {
2012         case kLocal : {
2013         if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
2014         AliAnalysisTaskJetV2::Dump();
2015         for(Int_t i(0); i < fHistAnalysisSummary->GetXaxis()->GetNbins(); i++) printf( " > flag: %s \t content %.2f \n", fHistAnalysisSummary->GetXaxis()->GetBinLabel(1+i), fHistAnalysisSummary->GetBinContent(1+i));
2016         } break;
2017         default : break;
2018     }
2019 }
2020 //_____________________________________________________________________________
2021 void AliAnalysisTaskJetV2::SetModulationFit(TF1* fit) 
2022 {
2023     // set modulation fit
2024     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
2025     if (fFitModulation) delete fFitModulation;
2026     fFitModulation = fit; 
2027 }
2028 //_____________________________________________________________________________
2029 void AliAnalysisTaskJetV2::SetUseControlFit(Bool_t c)
2030 {
2031     // set control fit
2032     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
2033     if (fFitControl) delete fFitControl;
2034     if (c) {
2035         fFitControl = new TF1("controlFit", "pol0", 0, TMath::TwoPi());
2036     } else fFitControl = 0x0;
2037 }
2038 //_____________________________________________________________________________
2039 TH1F* AliAnalysisTaskJetV2::GetResolutionFromOuptutFile(detectorType det, Int_t h, TArrayD* cen)
2040 {
2041     // INTERFACE METHOD FOR OUTPUTFILE
2042     // get the detector resolution, user has ownership of the returned histogram
2043     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
2044     if(!fOutputList) {
2045         printf(" > Please add fOutputList first < \n");
2046         return 0x0;
2047     }
2048     TH1F* r(0x0);
2049     (cen) ? r = new TH1F("R", "R", cen->GetSize()-1, cen->GetArray()) : r = new TH1F("R", "R", 10, 0, 10);
2050     if(!cen) r->GetXaxis()->SetTitle("number of centrality bin");
2051     r->GetYaxis()->SetTitle(Form("Resolution #Psi_{%i}", h));
2052     for(Int_t i(0); i < 10; i++) {
2053         TProfile* temp((TProfile*)fOutputList->FindObject(Form("fProfV%iResolution_%i", h, i)));
2054         if(!temp) break;
2055         Double_t a(temp->GetBinContent(3)), b(temp->GetBinContent(5)), c(temp->GetBinContent(7));
2056         Double_t d(temp->GetBinContent(9)), e(temp->GetBinContent(10)), f(temp->GetBinContent(11));
2057         Double_t _a(temp->GetBinError(3)), _b(temp->GetBinError(5)), _c(temp->GetBinError(7));
2058         Double_t _d(temp->GetBinError(9)), _e(temp->GetBinError(10)), _f(temp->GetBinError(11));
2059         if(a <= 0 || b <= 0 || c <= 0 || d <= 0 || e <= 0 || f <= 0) continue;
2060         switch (det) {
2061             case kVZEROA : {
2062                 r->SetBinContent(1+i, TMath::Sqrt((a*b)/c));
2063                 if(i==0) r->SetNameTitle("VZEROA resolution", "VZEROA resolution");
2064                 r->SetBinError(1+i, TMath::Sqrt(_a*_a+_b*_b+_c*_c));
2065             } break;
2066             case kVZEROC : {
2067                 r->SetBinContent(1+i, TMath::Sqrt((a*c)/b));
2068                 if(i==0) r->SetNameTitle("VZEROC resolution", "VZEROC resolution");
2069                 r->SetBinError(1+i, TMath::Sqrt(_a*_a+_b*_b+_c*_c));
2070             } break;
2071             case kTPC : {
2072                 r->SetBinContent(1+i, TMath::Sqrt((b*c)/a));
2073                 if(i==0) r->SetNameTitle("TPC resolution", "TPC resolution");
2074                 r->SetBinError(1+i, TMath::Sqrt(_a*_a+_b*_b+_c*_c));
2075             } break;
2076             case kVZEROComb : {
2077                 r->SetBinContent(1+i, TMath::Sqrt((d*e)/f));
2078                 if(i==0) r->SetNameTitle("VZEROComb resolution", "VZEROComb resolution");
2079                 r->SetBinError(1+i, TMath::Sqrt(_d*_d+_e*_e+_f*_f));
2080             } break;
2081             default : break;
2082         }
2083     }
2084     return r;
2085 }
2086 //_____________________________________________________________________________
2087 TH1F* AliAnalysisTaskJetV2::CorrectForResolutionDiff(TH1F* v, detectorType det, TArrayD* cen, Int_t c, Int_t h)
2088 {
2089     // INTERFACE METHOD FOR OUTPUT FILE
2090     // correct the supplied differential vn histogram v for detector resolution
2091     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
2092     TH1F* r(GetResolutionFromOuptutFile(det, h, cen));
2093     if(!r) {
2094         printf(" > Couldn't find resolution < \n");
2095         return 0x0;
2096     }
2097     Double_t res(1./r->GetBinContent(1+r->FindBin(c)));
2098     TF1* line = new TF1("line", "pol0", 0, 200);
2099     line->SetParameter(0, res);
2100     v->Multiply(line);
2101     return v;
2102 }
2103 //_____________________________________________________________________________
2104 TH1F* AliAnalysisTaskJetV2::CorrectForResolutionInt(TH1F* v, detectorType det, TArrayD* cen, Int_t h)
2105 {
2106     // INTERFACE METHOD FOR OUTPUT FILE
2107     // correct the supplied intetrated vn histogram v for detector resolution
2108     // integrated vn must have the same centrality binning as the resolotion correction
2109     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
2110     TH1F* r(GetResolutionFromOuptutFile(det, h, cen));
2111     v->Divide(v, r);
2112     return v;
2113 }
2114 //_____________________________________________________________________________
2115 TH1F* AliAnalysisTaskJetV2::GetDifferentialQC(TProfile* refCumulants, TProfile* diffCumlants, TArrayD* ptBins, Int_t h)
2116 {
2117     // get differential QC
2118     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
2119     Double_t r(refCumulants->GetBinContent(h-1)); // v2 reference flow
2120     if(r > 0) r = TMath::Sqrt(r);
2121     TH1F* qc = new TH1F(Form("QC2v%i", h), Form("QC2v%i", h), ptBins->GetSize()-1, ptBins->GetArray());
2122     Double_t a(0), b(0), c(0);  // dummy variables
2123     for(Int_t i(0); i < ptBins->GetSize(); i++) {
2124         if(r > 0) {
2125             a = diffCumlants->GetBinContent(1+i);
2126             b = diffCumlants->GetBinError(1+i);
2127             c = a/r;
2128             qc->SetBinContent(1+i, c);
2129             (a <= 0 || b <= 0) ? qc->SetBinError(1+i, b) : qc->SetBinError(1+i, TMath::Sqrt(c*c*b*b/(a*a)));
2130         }
2131     }
2132     return qc;
2133 }
2134
2135 //_____________________________________________________________________________
2136 void AliAnalysisTaskJetV2::ReadVZEROCalibration2010h()
2137 {
2138     // necessary for calibration of 10h vzero event plane. code copied from flow package 
2139     // (duplicate, but i didn't want to introduce an ulgy dependency )
2140     // this function is only called when the runnumber changes 
2141     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
2142
2143     // 1) check if the proper chi weights for merging vzero a and vzero c ep are present
2144     // if not, use sane defaults. centrality binning is equal to that given in the fVZEROcentralityBin snippet
2145     //
2146     // chi values can be calculated using the static helper function 
2147     // AliAnalysisTaskJetV2::CalculateEventPlaneChi(Double_t res) where res is the event plane
2148     // resolution in a given centrality bin
2149     //
2150     // the resolutions that were used for these defaults are
2151     // this might need a bit of updating as they were read 'by-eye' from a performance plot ..
2152     // Double_t R2VZEROA[] = {.35, .40, .48, .50, .48, .45, .38, .26, .16};
2153     // Double_t R2VZEROC[] = {.45, .60, .70, .73, .68, .60, .40, .36, .17};
2154     // Double_t R3VZEROA[] = {.22, .23, .22, .19, .15, .12, .08, .00, .00};
2155     // Double_t R3VZEROC[] = {.30, .30, .28, .25, .22, .17, .11, .00, .00};
2156
2157     Double_t chiC2[] = {0.771423, 1.10236, 1.38116, 1.48077, 1.31964, 1.10236, 0.674622, 0.600403, 0.273865};
2158     Double_t chiA2[] = {0.582214, 0.674622, 0.832214, 0.873962, 0.832214, 0.771423, 0.637146, 0.424255, 0.257385};
2159     Double_t chiC3[] = {0.493347, 0.493347, 0.458557, 0.407166, 0.356628, 0.273865, 0.176208, 6.10352e-05, 6.10352e-05};
2160     Double_t chiA3[] = {0.356628, 0.373474, 0.356628, 0.306702, 0.24115, 0.192322, 0.127869, 6.10352e-05, 6.10352e-05};
2161
2162     if(!fChi2A) fChi2A = new TArrayD(9, chiA2);
2163     if(!fChi2C) fChi2C = new TArrayD(9, chiC2);
2164     if(!fChi3A) fChi3A = new TArrayD(9, chiA3);
2165     if(!fChi3C) fChi3C = new TArrayD(9, chiC3);
2166
2167     // 2) open database file
2168     fOADB = TFile::Open("$ALICE_ROOT/OADB/PWGCF/VZERO/VZEROcalibEP.root");
2169     if(fOADB->IsZombie()){
2170         printf("OADB file $ALICE_ROOT/OADB/PWGCF/VZERO/VZEROcalibEP.root cannot be opened, CALIBRATION FAILED !");
2171         return;
2172     }
2173
2174     AliOADBContainer *cont = (AliOADBContainer*) fOADB->Get("hMultV0BefCorr");
2175     if(!cont){
2176         // see if database is readable
2177         printf("OADB object hMultV0BefCorr is not available in the file\n");
2178         return; 
2179     }
2180     Int_t run(fRunNumber);
2181     if(!(cont->GetObject(run))){
2182         // if the run isn't recognized fall back to a default run
2183         printf("OADB object hMultVZEROBefCorr is not available for run %i (used default run 137366)\n",run);
2184         run = 137366;
2185     }
2186     // step 3) get the proper multiplicity weights from the vzero signal
2187     fVZEROgainEqualization = ((TH2F*)cont->GetObject(run))->ProfileX();
2188     if(!fVZEROgainEqualization) {
2189         AliFatal(Form("%s: Fatal error, couldn't read fVZEROgainEqualization from OADB object < \n", GetName()));
2190         return;
2191     }
2192
2193     TF1* fpol0 = new TF1("fpol0","pol0"); 
2194     if(fVZEROgainEqualizationPerRing) {
2195         // do the calibration per ring
2196         // start with the vzero c rings (segments 0 through 31)
2197         fVZEROgainEqualization->Fit(fpol0, "", "", 0, 8);
2198         (fUseVZERORing[0]) ? SetVZEROCpol(0, fpol0->GetParameter(0)) : SetVZEROCpol(0, 0.);
2199         fVZEROgainEqualization->Fit(fpol0, "", "", 8, 16);
2200         (fUseVZERORing[1]) ? SetVZEROCpol(1, fpol0->GetParameter(0)) : SetVZEROCpol(1, 0.);
2201         fVZEROgainEqualization->Fit(fpol0, "", "", 16, 24);
2202         (fUseVZERORing[2]) ? SetVZEROCpol(2, fpol0->GetParameter(0)) : SetVZEROCpol(2, 0.);
2203         fVZEROgainEqualization->Fit(fpol0, "", "", 24, 32);
2204         (fUseVZERORing[3]) ? SetVZEROCpol(3, fpol0->GetParameter(0)) : SetVZEROCpol(3, 0.);
2205         // same thing for vero A
2206         fVZEROgainEqualization->Fit(fpol0, "", "", 32, 40);
2207         (fUseVZERORing[4]) ? SetVZEROApol(0, fpol0->GetParameter(0)) : SetVZEROApol(0, 0.);
2208         fVZEROgainEqualization->Fit(fpol0, "", "", 40, 48);
2209         (fUseVZERORing[5]) ? SetVZEROApol(1, fpol0->GetParameter(0)) : SetVZEROApol(1, 0.);
2210         fVZEROgainEqualization->Fit(fpol0, "", "", 48, 56);
2211         (fUseVZERORing[6]) ? SetVZEROApol(2, fpol0->GetParameter(0)) : SetVZEROApol(2, 0.);
2212         fVZEROgainEqualization->Fit(fpol0, "", "", 56, 64);
2213         (fUseVZERORing[7]) ? SetVZEROApol(3, fpol0->GetParameter(0)) : SetVZEROApol(3, 0.);
2214     } else {
2215         // do the calibration in one go. the calibration will still be 
2216         // stored per ring, but each ring has the same weight now
2217         // this should be the default for the analysis as the database is tuned to this configuration
2218        fVZEROgainEqualization->Fit(fpol0,"","",0,31);
2219        for(Int_t i(0); i < 4; i++) SetVZEROCpol(i, fpol0->GetParameter(0));
2220        fVZEROgainEqualization->Fit(fpol0,"","",32,64);
2221        for(Int_t i(0); i < 4; i++) SetVZEROApol(i, fpol0->GetParameter(0));
2222     }
2223
2224     // step 4) extract the information to re-weight the q-vectors 
2225     for(Int_t iside=0;iside<2;iside++){
2226         for(Int_t icoord=0;icoord<2;icoord++){
2227             for(Int_t i=0;i  < 9;i++){
2228                 char namecont[100];
2229                 if(iside==0 && icoord==0)
2230                   snprintf(namecont,100,"hQxc2_%i",i);
2231                 else if(iside==1 && icoord==0)
2232                   snprintf(namecont,100,"hQxa2_%i",i);
2233                 else if(iside==0 && icoord==1)
2234                   snprintf(namecont,100,"hQyc2_%i",i);
2235                 else if(iside==1 && icoord==1)
2236                   snprintf(namecont,100,"hQya2_%i",i);
2237
2238                 cont = (AliOADBContainer*) fOADB->Get(namecont);
2239                 if(!cont){
2240                     printf("OADB object %s is not available in the file\n",namecont);
2241                     return;     
2242                 }
2243         
2244                 if(!(cont->GetObject(run))){
2245                     printf("OADB object %s is not available for run %i (used run 137366)\n",namecont,run);
2246                     run = 137366;
2247                 }
2248
2249                 // store info for all centralities to cache
2250                 fMeanQ[i][iside][icoord] = ((TH1F *) cont->GetObject(run))->GetMean();
2251                 fWidthQ[i][iside][icoord] = ((TH1F *) cont->GetObject(run))->GetRMS();
2252
2253                 //for v3
2254                 if(iside==0 && icoord==0)
2255                   snprintf(namecont,100,"hQxc3_%i",i);
2256                 else if(iside==1 && icoord==0)
2257                   snprintf(namecont,100,"hQxa3_%i",i);
2258                 else if(iside==0 && icoord==1)
2259                   snprintf(namecont,100,"hQyc3_%i",i);
2260                 else if(iside==1 && icoord==1)
2261                   snprintf(namecont,100,"hQya3_%i",i);
2262
2263                 cont = (AliOADBContainer*) fOADB->Get(namecont);
2264                 if(!cont){
2265                     printf("OADB object %s is not available in the file\n",namecont);
2266                     return;     
2267                 }
2268                 
2269                 if(!(cont->GetObject(run))){
2270                     printf("OADB object %s is not available for run %i (used run 137366)\n",namecont,run);
2271                     run = 137366;
2272                 }
2273                 // store info for all centralities to cache
2274                 fMeanQv3[i][iside][icoord] = ((TH1F *) cont->GetObject(run))->GetMean();
2275                 fWidthQv3[i][iside][icoord] = ((TH1F *) cont->GetObject(run))->GetRMS();
2276             }
2277         }
2278     }
2279     // cleanup. the opened file is closed in the destructor, otherwise fVZEROgainEqualization is no longer available
2280     delete fpol0;
2281 }
2282 //_____________________________________________________________________________
2283 Int_t AliAnalysisTaskJetV2::GetVZEROCentralityBin() const
2284 {
2285     // return cache index number corresponding to the event centrality
2286     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
2287     Float_t v0Centr(InputEvent()->GetCentrality()->GetCentralityPercentile("V0M"));
2288     if(v0Centr < 5) return 0;
2289     else if(v0Centr < 10) return 1;
2290     else if(v0Centr < 20) return  2;
2291     else if(v0Centr < 30) return  3;
2292     else if(v0Centr < 40) return  4;
2293     else if(v0Centr < 50) return  5;
2294     else if(v0Centr < 60) return  6;
2295     else if(v0Centr < 70) return  7;
2296     else return 8;
2297 }
2298 //_____________________________________________________________________________
2299 AliEmcalJet* AliAnalysisTaskJetV2::GetLeadingJet(AliLocalRhoParameter* localRho) {
2300     // return pointer to the highest pt jet (before background subtraction) within acceptance
2301     // only rudimentary cuts are applied on this level, hence the implementation outside of
2302     // the framework
2303     Int_t iJets(fJets->GetEntriesFast());
2304     Double_t pt(0);
2305     AliEmcalJet* leadingJet(0x0);
2306     if(!localRho) {
2307         for(Int_t i(0); i < iJets; i++) {
2308             AliEmcalJet* jet = static_cast<AliEmcalJet*>(fJets->At(i));
2309             if(!PassesSimpleCuts(jet)) continue;
2310             if(jet->Pt() > pt) {
2311                leadingJet = jet;
2312                pt = leadingJet->Pt();
2313             }
2314         }
2315         return leadingJet;
2316     } else {
2317         // return leading jet after background subtraction
2318         Double_t rho(0);
2319         for(Int_t i(0); i < iJets; i++) {
2320             AliEmcalJet* jet = static_cast<AliEmcalJet*>(fJets->At(i));
2321             if(!PassesSimpleCuts(jet)) continue;
2322             rho = localRho->GetLocalVal(jet->Phi(), GetJetContainer()->GetJetRadius(), localRho->GetVal());
2323             if((jet->Pt()-jet->Area()*rho) > pt) {
2324                leadingJet = jet;
2325                pt = (leadingJet->Pt()-jet->Area()*rho);
2326             }
2327         }
2328         return leadingJet;
2329
2330     }
2331     return 0x0;
2332 }
2333 //_____________________________________________________________________________