]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGJE/EMCALJetTasks/UserTasks/AliAnalysisTaskJetV2.cxx
prototype for weights
[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), fEventPlaneWeights(0), fAcceptanceWeights(kFALSE), fEventPlaneWeight(1.), 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), fHistCentralityPercIn(0), fHistCentralityPercOut(0), fHistCentralityPercLost(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), fEventPlaneWeights(0), fAcceptanceWeights(kFALSE), fEventPlaneWeight(1.), 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), fHistCentralityPercIn(0), fHistCentralityPercOut(0), fHistCentralityPercLost(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     if(fAcceptanceWeights) {
459         fHistCentralityPercIn =         new TProfile("fHistCentralityPercIn", "fHistCentralityPercIn", 102, -2, 100);
460         fHistCentralityPercOut =        new TProfile("fHistCentralityPercOut", "fHistCentralityPercOut", 102, -2, 100);
461         fHistCentralityPercLost =       new TProfile("fHistCentralityPercLost", "fHistCentralityPercLost", 102, -2, 100);
462     }
463
464     // for some histograms adjust the bounds according to analysis acceptance
465     Double_t etaMin(-1.), etaMax(1.), phiMin(0.), phiMax(TMath::TwoPi());
466     switch (fAnalysisType) {
467         case kFull : {
468            etaMin = -.7;       
469            etaMax = .7;
470            phiMin = 1.405;
471            phiMax = 3.135;
472         } break;
473         default : break;
474     }
475
476     // pico track and emcal cluster kinematics
477     for(Int_t i(0); i < fCentralityClasses->GetSize()-1; i++) { 
478         fHistPicoTrackPt[i] =           BookTH1F("fHistPicoTrackPt", "p_{t} [GeV/c]", 100, 0, 100, i);
479         fHistPicoTrackMult[i] =         BookTH1F("fHistPicoTrackMult", "multiplicity", 100, 0, 5000, i);
480         if(fFillQAHistograms) {
481             fHistPicoCat1[i] =          BookTH2F("fHistPicoCat1", "#eta", "#phi", 50, -1, 1, 50, 0, TMath::TwoPi(), i);
482             fHistPicoCat2[i] =          BookTH2F("fHistPicoCat2", "#eta", "#phi", 50, -1, 1, 50, 0, TMath::TwoPi(), i);
483             fHistPicoCat3[i] =          BookTH2F("fHistPicoCat3", "#eta", "#phi", 50, -1, 1, 50, 0, TMath::TwoPi(), i);
484             if(fAnalysisType == AliAnalysisTaskJetV2::kFull) {
485                 fHistClusterPt[i] =     BookTH1F("fHistClusterPt", "p_{t} [GeV/c]", 100, 0, 100, i);
486                 fHistClusterEtaPhi[i] = BookTH2F("fHistClusterEtaPhi", "#eta", "#phi", 100, etaMax, etaMax, 100, phiMin, phiMax, i);
487                 fHistClusterEtaPhiWeighted[i] = BookTH2F("fHistClusterEtaPhiWeighted", "#eta", "#phi", 100, etaMin, etaMax, 100, phiMin, phiMax, i);
488             }
489             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);
490             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);
491             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);
492             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);
493             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);
494             fHistLeadingJetBackground[i] =      BookTH2F("fHistLeadingJetBackground", "#Delta #eta (leading jet with, without sub)", "Delta #varphi (leading jet with, without sub)", 50, 0., 2, 50, 0., TMath::TwoPi(), i);
495         }
496     }
497
498     if(fFillQAHistograms) {
499         // event plane estimates and quality
500         fHistPsiControl =           new TProfile("fHistPsiControl", "fHistPsiControl", 10, 0, 10);
501         fHistPsiControl->Sumw2();
502         fHistPsiSpread =            new TProfile("fHistPsiSpread", "fHistPsiSpread", 4, 0, 4);
503         fHistPsiSpread->Sumw2();
504         fHistPsiControl->GetXaxis()->SetBinLabel(1, "<#Psi_{2, VZEROA}>");
505         fHistPsiControl->GetXaxis()->SetBinLabel(2, "<#Psi_{2, VZEROC}>");
506         fHistPsiControl->GetXaxis()->SetBinLabel(3, "<#Psi_{2, TPC}>");
507         fHistPsiControl->GetXaxis()->SetBinLabel(4, "<#Psi_{2, TPC, #eta < 0}>");
508         fHistPsiControl->GetXaxis()->SetBinLabel(5, "<#Psi_{2, TPC, #eta > 0}>");
509         fHistPsiControl->GetXaxis()->SetBinLabel(6, "<#Psi_{3, VZEROA}>");
510         fHistPsiControl->GetXaxis()->SetBinLabel(7, "<#Psi_{3, VZEROC}>");
511         fHistPsiControl->GetXaxis()->SetBinLabel(8, "<#Psi_{3, TPC}>");
512         fHistPsiControl->GetXaxis()->SetBinLabel(9, "<#Psi_{3, TPC, #eta < 0}>");
513         fHistPsiControl->GetXaxis()->SetBinLabel(10, "<#Psi_{3, TPC, #eta > 0}>");
514         fHistPsiSpread->GetXaxis()->SetBinLabel(1, "<#Psi_{2, VZEROA} - #Psi_{2, VZEROC}>");
515         fHistPsiSpread->GetXaxis()->SetBinLabel(2, "<#Psi_{2, VZEROC} - #Psi_{2, TPC}>");
516         fHistPsiSpread->GetXaxis()->SetBinLabel(3, "<#Psi_{2, VZEROC} - #Psi_{2, TPC}>");
517         fHistPsiSpread->GetXaxis()->SetBinLabel(4, "<#Psi_{2, TPC, #eta < 0} - #Psi_{2, TPC, #eta > 0}>");
518         fOutputList->Add(fHistPsiControl);
519         fOutputList->Add(fHistPsiSpread);
520         fHistPsiVZEROA =            BookTH1F("fHistPsiVZEROA", "#Psi_{VZEROA}", 40, -.5*TMath::Pi(), .5*TMath::Pi());
521         fHistPsiVZEROC =            BookTH1F("fHistPsiVZEROC", "#Psi_{VZEROC}", 40, -.5*TMath::Pi(), .5*TMath::Pi());
522         fHistPsiVZERO =             BookTH1F("fHistPsiVZERO", "#Psi_{VZERO}", 40, -.5*TMath::Pi(), .5*TMath::Pi());
523         fHistPsiTPC =               BookTH1F("fHistPsiTPC", "#Psi_{TPC}", 40, -.5*TMath::Pi(), .5*TMath::Pi());
524         fHistPsiVZEROAV0M =         BookTH2F("fHistPsiVZEROAV0M", "V0M", "#Psi_{2, VZEROA}", 60, 0, 60, 40, -.5*TMath::Pi(), .5*TMath::Pi());
525         fHistPsiVZEROCV0M =         BookTH2F("fHistPsiVZEROCV0M", "V0M", "#Psi_{2, VZEROC}", 60, 0, 60, 40, -.5*TMath::Pi(), .5*TMath::Pi());
526         fHistPsiVZEROVV0M =         BookTH2F("fHistPsiVZEROV0M", "V0M", "#Psi_{2, VZERO}", 60, 0, 60, 40, -.5*TMath::Pi(), .5*TMath::Pi());
527         fHistPsiTPCV0M =            BookTH2F("fHistPsiTPCV0M", "V0M", "#Psi_{2, TRK}", 60, 0, 60, 40, -.5*TMath::Pi(), .5*TMath::Pi());
528         fHistPsiVZEROATRK =         BookTH2F("fHistPsiVZEROATRK", "TRK", "#Psi_{2, VZEROA}", 60, 0, 60, 40, -.5*TMath::Pi(), .5*TMath::Pi());
529         fHistPsiVZEROCTRK =         BookTH2F("fHistPsiVZEROCTRK", "TRK", "#Psi_{2, VZEROC}", 60, 0, 60, 40, -.5*TMath::Pi(), .5*TMath::Pi());
530         fHistPsiVZEROTRK =          BookTH2F("fHistPsiVZEROTRK", "TRK", "#Psi_{2, VZERO}", 60, 0, 60, 40, -.5*TMath::Pi(), .5*TMath::Pi());
531         fHistPsiTPCTRK =            BookTH2F("fHistPsiTPCTRK", "TRK", "#Psi_{2, TRK}", 60, 0, 60, 40, -.5*TMath::Pi(), .5*TMath::Pi());
532     }
533     // background
534     for(Int_t i(0); i < fCentralityClasses->GetSize()-1; i ++) {
535         fHistRhoPackage[i] =           BookTH1F("fHistRhoPackage",  "#rho [GeV/c]", 100, 0, 150, i);
536         fHistRho[i] =                  BookTH1F("fHistRho", "#rho [GeV/c]", 100, 0, 150, i);
537     }
538     fHistRhoVsMult =            BookTH2F("fHistRhoVsMult", "multiplicity", "#rho [GeV/c]", 100, 0, 4000, 100, 0, 250);
539     fHistRhoVsCent =            BookTH2F("fHistRhoVsCent", "centrality", "#rho [GeV/c]", 100, 0, 100, 100, 0, 250);
540     fHistRhoAVsMult =           BookTH2F("fHistRhoAVsMult", "multiplicity", "#rho * A (jet) [GeV/c]", 100, 0, 4000, 100, 0, 50);
541     fHistRhoAVsCent =           BookTH2F("fHistRhoAVsCent", "centrality", "#rho * A (jet) [GeV/c]", 100, 0, 100, 100, 0, 50);
542
543     TString detector("");
544     switch (fDetectorType) {
545         case kTPC : detector+="TPC";
546             break;
547         case kVZEROA : detector+="VZEROA";
548             break;
549         case kVZEROC : detector+="VZEROC";
550             break;
551         case kVZEROComb : detector+="VZEROComb";
552             break; 
553         case kFixedEP : detector+="FixedEP";
554             break;
555         default: break;
556     }
557     // delta pt distributions
558     for(Int_t i(0); i < fCentralityClasses->GetSize()-1; i ++) {
559         if(fFillQAHistograms)   fHistRCPhiEta[i] = BookTH2F("fHistRCPhiEta", "#phi (RC)", "#eta (RC)", 40, phiMin, phiMax, 40, etaMin, etaMax, i);
560         fHistRhoVsRCPt[i] =            BookTH2F("fHistRhoVsRCPt", "p_{t} (RC) [GeV/c]", "#rho * A (RC) [GeV/c]", 100, 0, 300, 100, 0, 350, i);
561         fHistRCPt[i] =                 BookTH1F("fHistRCPt", "p_{t} (RC) [GeV/c]", 130, -20, 150, i);
562         if(fFillQAHistograms)   fHistRCPhiEtaExLJ[i] = BookTH2F("fHistRCPhiEtaExLJ", "#phi (RC)", "#eta (RC)", 40, phiMin, phiMax, 40, etaMin, etaMax, i);
563         fHistDeltaPtDeltaPhi2[i] =  BookTH2F("fHistDeltaPtDeltaPhi2", Form("#phi - #Psi_{2, %s}", detector.Data()), "#delta p_{t} [GeV/c]", 40, 0, TMath::Pi(), 400, -70, 130, i);
564         fHistDeltaPtDeltaPhi2Rho0[i] =  BookTH2F("fHistDeltaPtDeltaPhi2Rho0", Form("#phi - #Psi_{2, %s}", detector.Data()), "#delta p_{t} [GeV/c]", 40, 0, TMath::Pi(), 400, -70, 130, i);
565         fHistRhoVsRCPtExLJ[i] =        BookTH2F("fHistRhoVsRCPtExLJ", "p_{t} (RC) [GeV/c]", "#rho * A (RC) [GeV/c]", 100, 0, 300, 100, 0, 350, i);
566         fHistRCPtExLJ[i] =             BookTH1F("fHistRCPtExLJ", "p_{t} (RC) [GeV/c]", 130, -20, 150, i);
567         fHistDeltaPtDeltaPhi2ExLJ[i] = BookTH2F("fHistDeltaPtDeltaPhi2ExLJ", Form("#phi - #Psi_{2, %s}", detector.Data()),  "#delta p_{t} [GeV/c]", 40, 0, TMath::Pi(), 400, -70, 130, i);
568         fHistDeltaPtDeltaPhi2ExLJRho0[i] = BookTH2F("fHistDeltaPtDeltaPhi2ExLJRho0", Form("#phi - #Psi_{2, %s}", detector.Data()),  "#delta p_{t} [GeV/c]", 40, 0, TMath::Pi(), 400, -70, 130, i);
569         // jet histograms (after kinematic cuts)
570         fHistJetPtRaw[i] =             BookTH1F("fHistJetPtRaw", "p_{t, jet} RAW [GeV/c]", 200, -50, 150, i);
571         fHistJetPt[i] =                BookTH1F("fHistJetPt", "p_{t, jet} [GeV/c]", 350, -100, 250, i);
572         if(fFillQAHistograms)   fHistJetEtaPhi[i] =            BookTH2F("fHistJetEtaPhi", "#eta", "#phi", 100, etaMin, etaMax, 100, phiMin, phiMax, i);
573         fHistJetPtArea[i] =            BookTH2F("fHistJetPtArea", "p_{t, jet} [GeV/c]", "Area", 175, -100, 250, 30, 0, 0.9, i);
574         fHistJetPtEta[i] =             BookTH2F("fHistJetPtEta", "p_{t, jet} [GeV/c]", "Eta", 175, -100, 250, 30, etaMin, etaMax, i);
575         fHistJetPtConstituents[i] =    BookTH2F("fHistJetPtConstituents", "p_{t, jet} [GeV/c]", "no. of constituents", 350, -100, 250, 60, 0, 150, i);
576         fHistJetEtaRho[i] =            BookTH2F("fHistJetEtaRho", "#eta", "#rho", 100, etaMin, etaMax, 100, 0, 300, i);
577         // in plane and out of plane spectra
578         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);
579         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);
580         // profiles for all correlator permutations which are necessary to calculate each second and third order event plane resolution
581         fProfV2Resolution[i] = new TProfile(Form("fProfV2Resolution_%i", i), Form("fProfV2Resolution_%i", i), 11, -0.5, 10.5);
582         fProfV2Resolution[i]->GetXaxis()->SetBinLabel(3, "<cos(2(#Psi_{VZEROA} - #Psi_{VZEROC}))>");
583         fProfV2Resolution[i]->GetXaxis()->SetBinLabel(4, "<cos(2(#Psi_{VZEROC} - #Psi_{VZEROA}))>");
584         fProfV2Resolution[i]->GetXaxis()->SetBinLabel(5, "<cos(2(#Psi_{VZEROA} - #Psi_{TPC}))>");
585         fProfV2Resolution[i]->GetXaxis()->SetBinLabel(6, "<cos(2(#Psi_{TPC} - #Psi_{VZEROA}))>");
586         fProfV2Resolution[i]->GetXaxis()->SetBinLabel(7, "<cos(2(#Psi_{VZEROC} - #Psi_{TPC}))>");
587         fProfV2Resolution[i]->GetXaxis()->SetBinLabel(8, "<cos(2(#Psi_{TPC} - #Psi_{VZEROC}))>");
588         fProfV2Resolution[i]->GetXaxis()->SetBinLabel(9, "<cos(2(#Psi_{VZERO} - #Psi_{TPC_A}))>");
589         fProfV2Resolution[i]->GetXaxis()->SetBinLabel(10, "<cos(2(#Psi_{VZERO} - #Psi_{TPC_B}))>");
590         fProfV2Resolution[i]->GetXaxis()->SetBinLabel(11, "<cos(2(#Psi_{TPC_A} - #Psi_{TPC_B}))>");
591         fOutputList->Add(fProfV2Resolution[i]); 
592         fProfV3Resolution[i] = new TProfile(Form("fProfV3Resolution_%i", i), Form("fProfV3Resolution_%i", i), 11, -0.5, 10.5);
593         fProfV3Resolution[i]->GetXaxis()->SetBinLabel(3, "<cos(3(#Psi_{VZEROA} - #Psi_{VZEROC}))>");
594         fProfV3Resolution[i]->GetXaxis()->SetBinLabel(4, "<cos(3(#Psi_{VZEROC} - #Psi_{VZEROA}))>");
595         fProfV3Resolution[i]->GetXaxis()->SetBinLabel(5, "<cos(3(#Psi_{VZEROA} - #Psi_{TPC}))>");
596         fProfV3Resolution[i]->GetXaxis()->SetBinLabel(6, "<cos(3(#Psi_{TPC} - #Psi_{VZEROA}))>");
597         fProfV3Resolution[i]->GetXaxis()->SetBinLabel(7, "<cos(3(#Psi_{VZEROC} - #Psi_{TPC}))>");
598         fProfV3Resolution[i]->GetXaxis()->SetBinLabel(8, "<cos(3(#Psi_{TPC} - #Psi_{VZEROC}))>");
599         fProfV3Resolution[i]->GetXaxis()->SetBinLabel(9, "<cos(3(#Psi_{VZERO} - #Psi_{TPC_A}))>");
600         fProfV3Resolution[i]->GetXaxis()->SetBinLabel(10, "<cos(3(#Psi_{VZERO} - #Psi_{TPC_B}))>");
601         fProfV3Resolution[i]->GetXaxis()->SetBinLabel(11, "<cos(3(#Psi_{TPC_A} - #Psi_{TPC_B}))>");
602         fOutputList->Add(fProfV3Resolution[i]); 
603     }
604    // vn profile
605     Float_t temp[fCentralityClasses->GetSize()];
606     for(Int_t i(0); i < fCentralityClasses->GetSize(); i++) temp[i] = fCentralityClasses->At(i);
607     fProfV2 = new TProfile("fProfV2", "fProfV2", fCentralityClasses->GetSize()-1, temp);
608     fProfV3 = new TProfile("fProfV3", "fProfV3", fCentralityClasses->GetSize()-1, temp);
609     fOutputList->Add(fProfV2);
610     fOutputList->Add(fProfV3);
611     switch (fFitModulationType) {
612         case kQC2 : {
613             fProfV2Cumulant = new TProfile("fProfV2Cumulant", "fProfV2Cumulant", fCentralityClasses->GetSize()-1, temp);
614             fProfV3Cumulant = new TProfile("fProfV3Cumulant", "fProfV3Cumulant", fCentralityClasses->GetSize()-1, temp);
615             fOutputList->Add(fProfV2Cumulant);
616             fOutputList->Add(fProfV3Cumulant);
617         } break;
618         case kQC4 : {
619             fProfV2Cumulant = new TProfile("fProfV2Cumulant", "fProfV2Cumulant", fCentralityClasses->GetSize()-1, temp);
620             fProfV3Cumulant = new TProfile("fProfV3Cumulant", "fProfV3Cumulant", fCentralityClasses->GetSize()-1, temp);
621             fOutputList->Add(fProfV2Cumulant);
622             fOutputList->Add(fProfV3Cumulant);
623         } break;
624         default : break;
625     }
626     // for the histograms initialized below, binning is fixed to runnumbers or flags
627     fReduceBinsXByFactor = 1;
628     fReduceBinsYByFactor = 1;
629     if(fFillQAHistograms) {
630         fHistRunnumbersEta = new TH2F("fHistRunnumbersEta", "fHistRunnumbersEta", fExpectedRuns->GetSize()+1, -.5, fExpectedRuns->GetSize()+.5, 100, -1.1, 1.1);
631         fHistRunnumbersEta->Sumw2();
632         fOutputList->Add(fHistRunnumbersEta);
633         fHistRunnumbersPhi = new TH2F("fHistRunnumbersPhi", "fHistRunnumbersPhi", fExpectedRuns->GetSize()+1, -.5, fExpectedRuns->GetSize()+.5, 100, -0.2, TMath::TwoPi()+0.2);
634         fHistRunnumbersPhi->Sumw2();
635         fOutputList->Add(fHistRunnumbersPhi);
636         for(Int_t i(0); i < fExpectedRuns->GetSize(); i++) { 
637             fHistRunnumbersPhi->GetXaxis()->SetBinLabel(i+1, Form("%i", fExpectedRuns->At(i)));
638             fHistRunnumbersEta->GetXaxis()->SetBinLabel(i+1, Form("%i", fExpectedRuns->At(i)));
639         }
640         fHistRunnumbersPhi->GetXaxis()->SetBinLabel(fExpectedRuns->GetSize()+1, "undetermined");
641         fHistRunnumbersEta->GetXaxis()->SetBinLabel(fExpectedRuns->GetSize()+1, "undetermined");
642     }
643     fHistAnalysisSummary = BookTH1F("fHistAnalysisSummary", "flag", 54, -0.5, 54.5);
644     fHistSwap = new TH1F("fHistSwap", "fHistSwap", 20, 0, TMath::TwoPi());
645     if(fUsePtWeight) fHistSwap->Sumw2();
646
647     if(fUserSuppliedV2)         fOutputList->Add(fUserSuppliedV2);
648     if(fUserSuppliedV3)         fOutputList->Add(fUserSuppliedV3);
649     if(fUserSuppliedR2)         fOutputList->Add(fUserSuppliedR2);
650     if(fUserSuppliedR3)         fOutputList->Add(fUserSuppliedR3);
651     if(fEventPlaneWeights)      fOutputList->Add(fEventPlaneWeights);
652     // increase readability of output list
653     fOutputList->Sort();
654     // cdf and pdf of chisquare distribution
655     fHistPvalueCDF = BookTH1F("fHistPvalueCDF", "CDF #chi^{2}", 50, 0, 1);
656     fHistPvalueCDFCent = BookTH2F("fHistPvalueCDFCent", "centrality", "p-value", 40, 0, 100, 40, 0, 1);
657     fHistChi2Cent = BookTH2F("fHistChi2Cent", "centrality", "#tilde{#chi^{2}}", 100, 0, 100, 100, 0, 5);
658     fHistPChi2 = BookTH2F("fHistPChi2", "p-value", "#tilde{#chi^{2}}", 1000, 0, 1, 100, 0, 5);
659     fHistKolmogorovTest = BookTH1F("fHistKolmogorovTest", "KolmogorovTest", 50, 0, 1);
660     fHistKolmogorovTestCent = BookTH2F("fHistKolmogorovTestCent", "centrality", "Kolmogorov p", 40, 0, 100, 45, 0, 1); 
661     fHistPvalueCDFROOT = BookTH1F("fHistPvalueCDFROOT", "CDF #chi^{2} ROOT", 50, 0, 1);
662     fHistPvalueCDFROOTCent = BookTH2F("fHistPvalueCDFROOTCent", "centrality", "p-value ROOT", 40, 0, 100, 45, 0, 1);
663     fHistChi2ROOTCent = BookTH2F("fHistChi2ROOTCent", "centrality", "#tilde{#chi^{2}}", 40, 0, 100, 45, 0, 5);
664     fHistPChi2Root = BookTH2F("fHistPChi2Root", "p-value", "#tilde{#chi^{2}} ROOT", 1000, 0, 1, 100, 0, 5);
665     fHistPKolmogorov = BookTH2F("fHistPKolmogorov", "p-value", "kolmogorov p",40, 0, 1, 40, 0, 1);
666     fHistRhoStatusCent = BookTH2F("fHistRhoStatusCent", "centrality", "status [-1=lin was better, 0=ok, 1 = failed]", 101, -1, 100, 3, -1.5, 1.5);
667     fHistUndeterminedRunQA = BookTH1F("fHistUndeterminedRunQA", "runnumber", 10, 0, 10);
668  
669     PostData(1, fOutputList);
670
671     switch (fRunModeType) {
672         case kLocal : {
673             fOutputListGood = new TList();
674             fOutputListGood->SetOwner(kTRUE);
675             fOutputListBad = new TList();
676             fOutputListBad->SetOwner(kTRUE);
677             PostData(2, fOutputListGood);
678             PostData(3, fOutputListBad);
679         } break;
680         default: break;
681     }
682
683     // get the containers
684     fTracksCont = GetParticleContainer("Tracks");
685     fClusterCont = GetClusterContainer(0);      // get the default cluster container
686     fJetsCont = GetJetContainer("Jets");
687 }
688 //_____________________________________________________________________________
689 Bool_t AliAnalysisTaskJetV2::Run()
690 {
691     // called for each accepted event (call made from user exec of parent class)
692     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
693     if(!fTracks||!fJets||!fRho) {
694         if(!fTracks) printf(" > Failed to retrieve fTracks ! < \n");
695         if(!fJets) printf(" > Failed to retrieve fJets ! < \n");
696         if(!fRho) printf(" > Failed to retrieve fRho ! < \n");
697         return kFALSE;
698     }
699     if(!fLocalInit) fLocalInit = InitializeAnalysis();
700     // reject the event if expected data is missing
701     if(!PassesCuts(InputEvent())) return kFALSE;
702     // cache the leading jet within acceptance
703     fLeadingJet = GetLeadingJet();
704     // set the rho value 
705     fLocalRho->SetVal(fRho->GetVal());
706     // place holder arrays for the event planes
707     //
708     // [0][0] psi2a     [1,0]   psi2c
709     // [0][1] psi3a     [1,1]   psi3c
710     Double_t vzero[2][2];
711     /* for the combined vzero event plane
712      * [0] psi2         [1] psi3
713      * not fully implmemented yet, use with caution ! */
714     Double_t vzeroComb[2];
715     // [0] psi2         [1] psi3
716     Double_t tpc[2];
717     // evaluate the actual event planes
718     switch (fDetectorType) {
719         case kFixedEP : {
720             // for fixed, fix all ep's to default values
721             tpc[0] = 0.;         tpc[1] = 1.;
722             vzero[0][0] = 0.;    vzero[0][1] = 1.;
723             vzero[1][0] = 0.;    vzero[1][1] = 1.;
724             vzeroComb[0] = 0.;   vzeroComb[1] = 1.;
725         } break;
726         default : {
727             // else grab the actual data
728             CalculateEventPlaneVZERO(vzero);
729             CalculateEventPlaneCombinedVZERO(vzeroComb);
730             CalculateEventPlaneTPC(tpc);
731         } break;
732     }
733     Double_t psi2(-1), psi3(-1);
734     // arrays which will hold the fit parameters
735     switch (fDetectorType) {    // determine the detector type for the rho fit
736         case kTPC :     { psi2 = tpc[0];         psi3 = tpc[1]; }       break;
737         case kVZEROA :  { psi2 = vzero[0][0];    psi3 = vzero[0][1]; }  break;  
738         case kVZEROC :  { psi2 = vzero[1][0];    psi3 = vzero[1][1]; }  break;
739         case kVZEROComb : { psi2 = vzeroComb[0]; psi3 = vzeroComb[1];}  break;
740         case kFixedEP : { psi2 = 0.;             psi3 = 1.;}            break;
741         default : break;
742     }
743     // if requested extract the event plane weight
744     if(fEventPlaneWeights) {
745         fEventPlaneWeight = fEventPlaneWeights->GetBinContent(fEventPlaneWeights->FindBin(psi2));
746     } 
747     // if requested store the acceptance weights
748     if(fAcceptanceWeights) {
749         Double_t percIn(0.), percOut(0.), percLost(0.);
750         NumericalOverlap(GetJetContainer()->GetJetEtaMin(), GetJetContainer()->GetJetEtaMax(), 
751                 psi2, percIn, percOut, percLost);
752         fHistCentralityPercIn->Fill(fCent, percIn);
753         fHistCentralityPercOut->Fill(fCent, percOut);
754         fHistCentralityPercLost->Fill(fCent, percLost);
755     }
756     switch (fFitModulationType) { // do the fits
757         case kNoFit : { 
758              switch (fCollisionType) {
759                  case kPythia : { // background is zero for pp jets
760                      fFitModulation->FixParameter(0, 0);
761                      fLocalRho->SetVal(0);
762                  } break;
763                  default :  {
764                      fFitModulation->FixParameter(0, fLocalRho->GetVal()); 
765                  } break;
766              }
767         } break;
768         case kV2 : {    // only v2
769             if(CorrectRho(psi2, psi3)) {
770                 fProfV2->Fill(fCent, fFitModulation->GetParameter(3));
771                 if(fUserSuppliedR2) {
772                     Double_t r(fUserSuppliedR2->GetBinContent(fUserSuppliedR2->GetXaxis()->FindBin(fCent)));
773                     if(r > 0) fFitModulation->SetParameter(3, fFitModulation->GetParameter(3)/r);
774                 }
775                 CalculateEventPlaneResolution(vzero, vzeroComb, tpc);
776             }
777         } break;
778         case kV3 : {    // only v3
779             if(CorrectRho(psi2, psi3)) {
780                 if(fUserSuppliedR3) {
781                     Double_t r(fUserSuppliedR3->GetBinContent(fUserSuppliedR3->GetXaxis()->FindBin(fCent)));
782                     if(r > 0) fFitModulation->SetParameter(3, fFitModulation->GetParameter(3)/r);
783                 }
784                 fProfV3->Fill(fCent, fFitModulation->GetParameter(3));
785                 CalculateEventPlaneResolution(vzero, vzeroComb, tpc);
786             }
787         } break;
788         case kQC2 : {   // qc2 analysis
789             if(CorrectRho(psi2, psi3)) {
790                 if(fUserSuppliedR2 && fUserSuppliedR3) {
791                     // note for the qc method, resolution is REVERSED to go back to v2obs
792                     Double_t r2(fUserSuppliedR2->GetBinContent(fUserSuppliedR2->GetXaxis()->FindBin(fCent)));
793                     Double_t r3(fUserSuppliedR3->GetBinContent(fUserSuppliedR3->GetXaxis()->FindBin(fCent)));
794                     if(r2 > 0) fFitModulation->SetParameter(3, fFitModulation->GetParameter(3)*r2);
795                     if(r3 > 0) fFitModulation->SetParameter(7, fFitModulation->GetParameter(7)*r3);
796                 }
797                 if (fUsePtWeight) { // use weighted weights
798                     Double_t dQCnM11 = (fNoEventWeightsForQC) ? 1. : QCnM11();
799                     fProfV2->Fill(fCent, fFitModulation->GetParameter(3), dQCnM11);
800                     fProfV3->Fill(fCent, fFitModulation->GetParameter(7), dQCnM11); 
801                 } else {
802                     Double_t dQCnM = (fNoEventWeightsForQC) ? 2. : QCnM();
803                     fProfV2->Fill(fCent, fFitModulation->GetParameter(3), dQCnM*(dQCnM-1));
804                     fProfV3->Fill(fCent, fFitModulation->GetParameter(7), dQCnM*(dQCnM-1));
805                 }
806                 CalculateEventPlaneResolution(vzero, vzeroComb, tpc);
807             }
808         } break;
809         case kQC4 : {
810             if(CorrectRho(psi2, psi3)) {
811                 if(fUserSuppliedR2 && fUserSuppliedR3) {
812                     // note for the qc method, resolution is REVERSED to go back to v2obs   
813                     Double_t r2(fUserSuppliedR2->GetBinContent(fUserSuppliedR2->GetXaxis()->FindBin(fCent)));
814                     Double_t r3(fUserSuppliedR3->GetBinContent(fUserSuppliedR3->GetXaxis()->FindBin(fCent)));
815                     if(r2 > 0) fFitModulation->SetParameter(3, fFitModulation->GetParameter(3)*r2);
816                     if(r3 > 0) fFitModulation->SetParameter(7, fFitModulation->GetParameter(7)*r3);
817                 }
818                 if (fUsePtWeight) { // use weighted weights
819                     fProfV2->Fill(fCent, TMath::Power(fFitModulation->GetParameter(3),0.5)/*, QCnM1111()*/);
820                     fProfV3->Fill(fCent, TMath::Power(fFitModulation->GetParameter(7),0.5)/*, QCnM1111()*/); 
821                 } else {
822                     fProfV2->Fill(fCent, TMath::Power(fFitModulation->GetParameter(3),0.5)/*, QCnM()*(QCnM()-1)*(QCnM()-2)*(QCnM()-3)*/);
823                     fProfV3->Fill(fCent, TMath::Power(fFitModulation->GetParameter(7),0.5)/*, QCnM()*(QCnM()-1)*(QCnM()-2)*(QCnM()-3)*/);
824                 }
825             }
826             CalculateEventPlaneResolution(vzero, vzeroComb, tpc);
827         } break;
828         default : {
829             if(CorrectRho(psi2, psi3)) {
830                 if(fUserSuppliedR2 && fUserSuppliedR3) {
831                     Double_t r2(fUserSuppliedR2->GetBinContent(fUserSuppliedR2->GetXaxis()->FindBin(fCent)));
832                     Double_t r3(fUserSuppliedR3->GetBinContent(fUserSuppliedR3->GetXaxis()->FindBin(fCent)));
833                     if(r2 > 0) fFitModulation->SetParameter(3, fFitModulation->GetParameter(3)/r2);
834                     if(r3 > 0) fFitModulation->SetParameter(7, fFitModulation->GetParameter(7)/r3);
835                 }
836                 fProfV2->Fill(fCent, fFitModulation->GetParameter(3));
837                 fProfV3->Fill(fCent, fFitModulation->GetParameter(7));
838                 CalculateEventPlaneResolution(vzero, vzeroComb, tpc);
839             }
840         } break;
841     }
842     // if all went well, update the local rho parameter
843     fLocalRho->SetLocalRho(fFitModulation);
844     // and only at this point can the leading jet after rho subtraction be evaluated
845     if(fFillQAHistograms) fLeadingJetAfterSub = GetLeadingJet(fLocalRho);
846     // fill a number of histograms. event qa needs to be filled first as it also determines the runnumber for the track qa 
847     if(fFillQAHistograms)       {
848         if(fEventPlaneWeights) FillWeightedQAHistograms(InputEvent());
849         else FillQAHistograms(InputEvent());
850     }
851     if(fFillHistograms)         FillHistogramsAfterSubtraction(psi2, vzero, vzeroComb, tpc);
852     // send the output to the connected output container
853     PostData(1, fOutputList);
854     switch (fRunModeType) {
855         case kLocal : {
856             PostData(2, fOutputListGood);
857             PostData(3, fOutputListBad);
858         } break;
859         default: break;
860     }
861     return kTRUE;
862 }
863 //_____________________________________________________________________________
864 void AliAnalysisTaskJetV2::Exec(Option_t* c)
865 {
866     // for stand alone, avoid framework event setup
867     switch (fCollisionType) {
868         case kJetFlowMC : {
869             // need to call ExecOnce as it is not loaded otherwise
870             if(!fLocalRho) AliAnalysisTaskJetV2::ExecOnce();
871             AliAnalysisTaskJetV2::Run();
872         } break;
873         default : {
874             AliAnalysisTaskSE::Exec(c);
875         } break;
876     }
877 }  
878 //_____________________________________________________________________________
879 void AliAnalysisTaskJetV2::NumericalOverlap(Double_t x1, Double_t x2, Double_t psi2, Double_t &percIn, Double_t &percOut, Double_t &percLost)
880 {
881    // numerically integrate with finite resolution
882    // idea is the following:
883    // 1) choose a vector phi
884    // 2) see if it is in a region of overlap between detector and in/out of plane spectrum
885    // 3) bookkeep percentages over overlap
886    Double_t a(psi2 - TMath::Pi()/4.);
887    // poor man's appproach: fix the frame
888    if(a < 0) a += TMath::Pi();
889    // set the rest of the event
890    Double_t b(a + TMath::Pi()/2.);
891    Double_t c(b + TMath::Pi()/2.);
892    Double_t d(c + TMath::Pi()/2.);
893    Double_t e(d + TMath::Pi()/2.);      // may seem mysterious but here for good reasons
894    // get percetnages
895    Double_t interval(TMath::TwoPi() / 1000.);
896    percIn = 0.;
897    percOut = 0.;
898    percLost = 0.;
899    Int_t status(-1);
900    // automagically do the integration
901    for(Double_t i = a; i < a+TMath::TwoPi()-interval; i += interval) {
902        status = OverlapsWithPlane(x1, x2, a, b, c, d, e, i);
903        if(status == 0 ) percLost += .001;
904        else if(status == 1 ) percIn += 0.001;
905        else if(status == 2 ) percOut += 0.001;
906    }
907 }
908 //_____________________________________________________________________________
909 Int_t AliAnalysisTaskJetV2::OverlapsWithPlane (
910         Double_t x1, Double_t x2,                                       // detector geometry relative to ep
911         Double_t a, Double_t b, Double_t c, Double_t d, Double_t e,     // in-plane, out-of-plane boundaries (see comments)
912         Double_t phi)                                                   // variable
913 {
914     // 'numerical integration' of geometric overlap
915     //
916     // works as follows: for a given vector phi determines whether
917     // or not this vector points towards an overlap region of 
918     // detector geometry and plane (in or out)
919     //
920     // returns
921     // 1) if overlap with in plane
922     // 2) if overlap with out of plane
923     // 0) if no overlap at all
924     Int_t overlap(0);
925     // check for condition in-plane
926     // conditions are always checked as
927     // 1) is the angle within in-plane sector?
928     // 2) is the angle also within detector acceptance?
929     if(phi > a && phi < b && phi > x1 && phi < x2) overlap = 1;
930     if(phi > c && phi < d && phi > x1 && phi < x2) overlap = 1;
931     // likewise for out-of-plane
932     if(phi > b && phi < c && phi > x1 && phi < x2) overlap = 2;
933     if(phi > d && phi < e && phi > x1 && phi < x2) overlap = 2;
934
935     // life would be so much easier if the detector was flat instead of cylindrical ....
936     x1+=TMath::TwoPi();
937     x2+=TMath::TwoPi();
938
939     if(phi > a && phi < b && phi > x1 && phi < x2) overlap = 1;
940     if(phi > c && phi < d && phi > x1 && phi < x2) overlap = 1;
941     // likewise for out-of-plane
942     if(phi > b && phi < c && phi > x1 && phi < x2) overlap = 2;
943     if(phi > d && phi < e && phi > x1 && phi < x2) overlap = 2;
944
945     return overlap;
946 }
947 //_____________________________________________________________________________
948 Double_t AliAnalysisTaskJetV2::CalculateEventPlaneChi(Double_t res)
949 {
950     // return chi for given resolution to combine event plane estimates from two subevents
951     // see Phys. Rev. C no. CS6346 (http://arxiv.org/abs/nucl-ex/9805001)
952     Double_t chi(2.), delta(1.), con((TMath::Sqrt(TMath::Pi()))/(2.*TMath::Sqrt(2)));
953     for (Int_t i(0); i < 15; i++) {
954         chi = ((con*chi*TMath::Exp(-chi*chi/4.)*(TMath::BesselI0(chi*chi/4.)+TMath::BesselI1(chi*chi/4.))) < res) ? chi + delta : chi - delta;
955         delta = delta / 2.;
956     }
957     return chi;
958 }
959 //_____________________________________________________________________________
960 void AliAnalysisTaskJetV2::CalculateEventPlaneVZERO(Double_t vzero[2][2]) const 
961 {
962     // get the vzero event plane (a and c separately)
963     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
964     switch (fCollisionType) {
965         case kPbPb10h : {
966             // for 10h data, get the calibrated q-vector from the database
967             Double_t QA2[] = {-999., -999.};
968             Double_t QA3[] = {-999., -999.};
969             Double_t QC2[] = {-999., -999.};
970             Double_t QC3[] = {-999., -999.};
971             CalculateQvectorVZERO(QA2, QA3, QC2, QC3);
972             vzero[0][0] = .5*TMath::ATan2(QA2[1], QA2[0]);
973             vzero[1][0] = .5*TMath::ATan2(QC2[1], QC2[0]);
974             vzero[0][1] = (1./3.)*TMath::ATan2(QA3[1], QA3[0]);
975             vzero[1][1] = (1./3.)*TMath::ATan2(QC3[1], QC3[0]);
976         } break;
977         default: {
978             // by default use the ep from the event header (make sure EP selection task is enabeled!)
979             Double_t a(0), b(0), c(0), d(0), e(0), f(0), g(0), h(0);
980             vzero[0][0] = InputEvent()->GetEventplane()->CalculateVZEROEventPlane(InputEvent(), 8, 2, a, b);
981             vzero[1][0] = InputEvent()->GetEventplane()->CalculateVZEROEventPlane(InputEvent(), 9, 2, c, d);
982             vzero[0][1] = InputEvent()->GetEventplane()->CalculateVZEROEventPlane(InputEvent(), 8, 3, e, f);
983             vzero[1][1] = InputEvent()->GetEventplane()->CalculateVZEROEventPlane(InputEvent(), 9, 3, g, h);
984             return;
985         }
986     }
987 }
988 //_____________________________________________________________________________
989 void AliAnalysisTaskJetV2::CalculateEventPlaneCombinedVZERO(Double_t* comb) const
990 {
991     // return the combined vzero event plane
992     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
993     switch (fCollisionType) {
994         // for 10h data call calibration info
995         case kPbPb10h : {
996             // get the calibrated q-vectors
997             Double_t Q2[] = {-999., -999.};            
998             Double_t Q3[] = {-999., -999.};
999             // return if something isn't ok from the calibration side
1000             CalculateQvectorCombinedVZERO(Q2, Q3);
1001             comb[0] = .5*TMath::ATan2(Q2[1], Q2[0]);
1002             comb[1] = (1./3.)*TMath::ATan2(Q3[1], Q3[0]);
1003         } break;
1004         default : {
1005             // for all other types use calibrated event plane from the event header
1006             Double_t a(0), b(0), c(0), d(0);
1007             comb[0] = InputEvent()->GetEventplane()->CalculateVZEROEventPlane(InputEvent(), 10, 2, a, b);
1008             comb[1] = InputEvent()->GetEventplane()->CalculateVZEROEventPlane(InputEvent(), 10, 3, c, d);
1009         } break;
1010     }
1011 }
1012 //_____________________________________________________________________________
1013 void AliAnalysisTaskJetV2::CalculateEventPlaneTPC(Double_t* tpc)
1014 {
1015    // grab the TPC event plane
1016    if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
1017    fNAcceptedTracks = 0;                // reset the track counter
1018    Double_t qx2(0), qy2(0);     // for psi2
1019    Double_t qx3(0), qy3(0);     // for psi3
1020    if(fTracksCont) {
1021        Float_t excludeInEta = -999;
1022        if(fExcludeLeadingJetsFromFit > 0 ) {    // remove the leading jet from ep estimate
1023            if(fLeadingJet) excludeInEta = fLeadingJet->Eta();
1024        }
1025        for(Int_t iTPC(0); iTPC < fTracksCont->GetNEntries(); iTPC++) {
1026            AliVParticle* track = fTracksCont->GetParticle(iTPC);
1027            if(!PassesCuts(track) || track->Pt() < fSoftTrackMinPt || track->Pt() > fSoftTrackMaxPt) continue;
1028            if(fExcludeLeadingJetsFromFit > 0 &&( (TMath::Abs(track->Eta() - excludeInEta) < GetJetContainer()->GetJetRadius()*fExcludeLeadingJetsFromFit ) || (TMath::Abs(track->Eta()) - GetJetContainer()->GetJetRadius() - GetJetContainer()->GetJetEtaMax() ) > 0 )) continue;
1029            fNAcceptedTracks++;
1030            qx2+= TMath::Cos(2.*track->Phi());
1031            qy2+= TMath::Sin(2.*track->Phi());
1032            qx3+= TMath::Cos(3.*track->Phi());
1033            qy3+= TMath::Sin(3.*track->Phi());
1034        }
1035    }
1036    tpc[0] = .5*TMath::ATan2(qy2, qx2);
1037    tpc[1] = (1./3.)*TMath::ATan2(qy3, qx3);
1038 }
1039 //_____________________________________________________________________________
1040 void AliAnalysisTaskJetV2::CalculateEventPlaneResolution(Double_t vzero[2][2], Double_t* vzeroComb, Double_t* tpc)
1041 {
1042     // fill the profiles for the resolution parameters
1043     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
1044     fProfV2Resolution[fInCentralitySelection]->Fill(2., TMath::Cos(2.*(vzero[0][0] - vzero[1][0])));
1045     fProfV2Resolution[fInCentralitySelection]->Fill(3., TMath::Cos(2.*(vzero[1][0] - vzero[0][0])));
1046     fProfV2Resolution[fInCentralitySelection]->Fill(4., TMath::Cos(2.*(vzero[0][0] - tpc[0])));
1047     fProfV2Resolution[fInCentralitySelection]->Fill(5., TMath::Cos(2.*(tpc[0] - vzero[0][0])));
1048     fProfV2Resolution[fInCentralitySelection]->Fill(6., TMath::Cos(2.*(vzero[1][0] - tpc[0])));
1049     fProfV2Resolution[fInCentralitySelection]->Fill(7., TMath::Cos(2.*(tpc[0] - vzero[1][0])));
1050     fProfV3Resolution[fInCentralitySelection]->Fill(2., TMath::Cos(3.*(vzero[0][0] - vzero[1][0])));
1051     fProfV3Resolution[fInCentralitySelection]->Fill(3., TMath::Cos(3.*(vzero[1][0] - vzero[0][0])));
1052     fProfV3Resolution[fInCentralitySelection]->Fill(4., TMath::Cos(3.*(vzero[0][0] - tpc[0])));
1053     fProfV3Resolution[fInCentralitySelection]->Fill(5., TMath::Cos(3.*(tpc[0] - vzero[0][0])));
1054     fProfV3Resolution[fInCentralitySelection]->Fill(6., TMath::Cos(3.*(vzero[1][0] - tpc[0])));
1055     fProfV3Resolution[fInCentralitySelection]->Fill(7., TMath::Cos(3.*(tpc[0] - vzero[1][0])));
1056     // for the resolution of the combined vzero event plane, use two tpc halves as uncorrelated subdetectors
1057     Double_t qx2a(0), qy2a(0);     // for psi2a, negative eta
1058     Double_t qx3a(0), qy3a(0);     // for psi3a, negative eta
1059     Double_t qx2b(0), qy2b(0);     // for psi2a, positive eta
1060     Double_t qx3b(0), qy3b(0);     // for psi3a, positive eta
1061     if(fTracks) {
1062        Int_t iTracks(fTracks->GetEntriesFast());
1063        for(Int_t iTPC(0); iTPC < iTracks; iTPC++) {
1064            AliVTrack* track = static_cast<AliVTrack*>(fTracks->At(iTPC));
1065            if(!PassesCuts(track) || track->Pt() < fSoftTrackMinPt || track->Pt() > fSoftTrackMaxPt) continue;
1066            if(track->Eta() < 0 ) {
1067                qx2a+= TMath::Cos(2.*track->Phi());
1068                qy2a+= TMath::Sin(2.*track->Phi());
1069                qx3a+= TMath::Cos(3.*track->Phi());
1070                qy3a+= TMath::Sin(3.*track->Phi());
1071            } else if (track->Eta() > 0) {
1072                qx2b+= TMath::Cos(2.*track->Phi());
1073                qy2b+= TMath::Sin(2.*track->Phi());
1074                qx3b+= TMath::Cos(3.*track->Phi());
1075                qy3b+= TMath::Sin(3.*track->Phi());
1076            }
1077        }
1078    }
1079    Double_t tpca2(.5*TMath::ATan2(qy2a, qx2a));
1080    Double_t tpca3((1./3.)*TMath::ATan2(qy3a, qx3a));
1081    Double_t tpcb2(.5*TMath::ATan2(qy2b, qx2b));
1082    Double_t tpcb3((1./3.)*TMath::ATan2(qy3b, qx3b));
1083    fProfV2Resolution[fInCentralitySelection]->Fill(8., TMath::Cos(2.*(vzeroComb[0] - tpca2)));
1084    fProfV2Resolution[fInCentralitySelection]->Fill(9., TMath::Cos(2.*(vzeroComb[0] - tpcb2)));
1085    fProfV2Resolution[fInCentralitySelection]->Fill(10., TMath::Cos(2.*(tpca2 - tpcb2))); 
1086    fProfV3Resolution[fInCentralitySelection]->Fill(8., TMath::Cos(3.*(vzeroComb[1] - tpca3)));
1087    fProfV3Resolution[fInCentralitySelection]->Fill(9., TMath::Cos(3.*(vzeroComb[1] - tpcb3)));
1088    fProfV3Resolution[fInCentralitySelection]->Fill(10., TMath::Cos(3.*(tpca3 - tpcb3))); 
1089 }   
1090 //_____________________________________________________________________________
1091 void AliAnalysisTaskJetV2::CalculateQvectorVZERO(Double_t Qa2[2], Double_t Qc2[2], Double_t Qa3[2], Double_t Qc3[2]) const
1092 {
1093     // return the calibrated 2nd and 3rd order q-vectors for vzeroa and vzeroc
1094     // function takes arrays as arguments, which correspond to vzero info in the following way
1095     // 
1096     // Qa2[0] = Qx2 for vzero A         Qa2[1] = Qy2 for vzero A (etc)
1097     
1098     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
1099     // placeholders for geometric information
1100     Double_t phi(-999.), weight(-999.); 
1101     // reset placeholders for Q-vector components
1102     Qa2[0] = 0.;    Qc2[0] = 0.;    Qa3[0] = 0.;    Qc3[0] = 0.;
1103     Qa2[1] = 0.;    Qc2[1] = 0.;    Qa3[1] = 0.;    Qc3[1] = 0.;
1104     
1105     for(Int_t i(0); i < 64; i++) {
1106         // loop over all scintillators, construct Q-vectors in the same loop
1107         phi     = TMath::PiOver4()*(0.5+i%8);
1108         weight  = 0.;
1109         // note that disabled rings have already been excluded in ReadVZEROCalibration2010h
1110         if(i<32) {    // v0c side
1111             if(i < 8) weight = InputEvent()->GetVZEROData()->GetMultiplicity(i)*fVZEROCpol[0]/fVZEROgainEqualization->GetBinContent(1+i);
1112             else if (i < 16 ) weight = InputEvent()->GetVZEROData()->GetMultiplicity(i)*fVZEROCpol[1]/fVZEROgainEqualization->GetBinContent(1+i);
1113             else if (i < 24 ) weight = InputEvent()->GetVZEROData()->GetMultiplicity(i)*fVZEROCpol[2]/fVZEROgainEqualization->GetBinContent(1+i);
1114             else if (i < 32 ) weight = InputEvent()->GetVZEROData()->GetMultiplicity(i)*fVZEROCpol[3]/fVZEROgainEqualization->GetBinContent(1+i);
1115             // fill Q-vectors for v0c side
1116             Qc2[0]+=weight*TMath::Cos(2.*phi);
1117             Qc3[0]+=weight*TMath::Cos(3.*phi);
1118             Qc2[1]+=weight*TMath::Sin(2.*phi);
1119             Qc3[1]+=weight*TMath::Sin(3.*phi);
1120         } else {       // v0a side
1121             if( i < 40) weight = InputEvent()->GetVZEROData()->GetMultiplicity(i)*fVZEROApol[0]/fVZEROgainEqualization->GetBinContent(1+i);
1122             else if ( i < 48 ) weight = InputEvent()->GetVZEROData()->GetMultiplicity(i)*fVZEROApol[1]/fVZEROgainEqualization->GetBinContent(1+i);
1123             else if ( i < 56 ) weight = InputEvent()->GetVZEROData()->GetMultiplicity(i)*fVZEROApol[2]/fVZEROgainEqualization->GetBinContent(1+i);
1124             else if ( i < 64 ) weight = InputEvent()->GetVZEROData()->GetMultiplicity(i)*fVZEROApol[3]/fVZEROgainEqualization->GetBinContent(1+i);
1125             // fill Q-vectors for v0a side
1126             Qa2[0]+=weight*TMath::Cos(2.*phi);
1127             Qa3[0]+=weight*TMath::Cos(3.*phi);
1128             Qa2[1]+=weight*TMath::Sin(2.*phi);
1129             Qa3[1]+=weight*TMath::Sin(3.*phi);
1130         }
1131     }
1132     // get the cache index and read the correction terms from the cache
1133     Int_t VZEROcentralityBin(GetVZEROCentralityBin());
1134     Double_t Qx2amean = fMeanQ[VZEROcentralityBin][1][0];
1135     Double_t Qx2arms  = fWidthQ[VZEROcentralityBin][1][0];
1136     Double_t Qy2amean = fMeanQ[VZEROcentralityBin][1][1];
1137     Double_t Qy2arms  = fWidthQ[VZEROcentralityBin][1][1];
1138
1139     Double_t Qx2cmean = fMeanQ[VZEROcentralityBin][0][0];
1140     Double_t Qx2crms  = fWidthQ[VZEROcentralityBin][0][0];
1141     Double_t Qy2cmean = fMeanQ[VZEROcentralityBin][0][1];
1142     Double_t Qy2crms  = fWidthQ[VZEROcentralityBin][0][1];      
1143
1144     Double_t Qx3amean = fMeanQv3[VZEROcentralityBin][1][0];
1145     Double_t Qx3arms  = fWidthQv3[VZEROcentralityBin][1][0];
1146     Double_t Qy3amean = fMeanQv3[VZEROcentralityBin][1][1];
1147     Double_t Qy3arms  = fWidthQv3[VZEROcentralityBin][1][1];
1148
1149     Double_t Qx3cmean = fMeanQv3[VZEROcentralityBin][0][0];
1150     Double_t Qx3crms  = fWidthQv3[VZEROcentralityBin][0][0];
1151     Double_t Qy3cmean = fMeanQv3[VZEROcentralityBin][0][1];
1152     Double_t Qy3crms  = fWidthQv3[VZEROcentralityBin][0][1];    
1153
1154     // update the weighted q-vectors with the re-centered values
1155     Qa2[0] = (Qa2[0] - Qx2amean)/Qx2arms;
1156     Qa2[1] = (Qa2[1] - Qy2amean)/Qy2arms;
1157     Qc2[0] = (Qc2[0] - Qx2cmean)/Qx2crms;
1158     Qc2[1] = (Qc2[1] - Qy2cmean)/Qy2crms;
1159
1160     Qa3[0] = (Qa3[0] - Qx3amean)/Qx3arms;
1161     Qa3[1] = (Qa3[1] - Qy3amean)/Qy3arms;
1162     Qc3[0] = (Qc3[0] - Qx3cmean)/Qx3crms;
1163     Qc3[1] = (Qc3[0] - Qy3cmean)/Qy3crms;
1164 }
1165 //_____________________________________________________________________________
1166 void AliAnalysisTaskJetV2::CalculateQvectorCombinedVZERO(Double_t Q2[2], Double_t Q3[2]) const
1167 {
1168     // calculate calibrated q-vector of the combined vzeroa, vzeroc system
1169     // this is somewhat ugly as CalculateQvectorCombinedVZERO is called more than once per event
1170     // but for now it will have to do ...
1171     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
1172
1173     // first step: retrieve the q-vectors component-wise per vzero detector
1174     Double_t QA2[] = {-999., -999.};
1175     Double_t QA3[] = {-999., -999.};
1176     Double_t QC2[] = {-999., -999.};
1177     Double_t QC3[] = {-999., -999.};
1178     CalculateQvectorVZERO(QA2, QA3, QC2, QC3);
1179
1180     // get cache index and retrieve the chi weights for this centrality
1181     Int_t VZEROcentralityBin(GetVZEROCentralityBin());
1182     Double_t chi2A(fChi2A->At(VZEROcentralityBin));
1183     Double_t chi2C(fChi2C->At(VZEROcentralityBin));
1184     Double_t chi3A(fChi3A->At(VZEROcentralityBin));
1185     Double_t chi3C(fChi3C->At(VZEROcentralityBin));
1186
1187     // combine the vzera and vzeroc signal
1188     Q2[0] = chi2A*chi2A*QA2[0]+chi2C*chi2C*QC2[0];
1189     Q2[1] = chi2A*chi2A*QA2[1]+chi2C*chi2C*QC2[1];
1190     Q3[0] = chi3A*chi3A*QA3[0]+chi3C*chi3C*QC3[0];
1191     Q3[1] = chi3A*chi3A*QC3[1]+chi3C*chi3C*QC3[1];
1192 }
1193 //_____________________________________________________________________________
1194 void AliAnalysisTaskJetV2::CalculateRandomCone(Float_t &pt, Float_t &eta, Float_t &phi, 
1195         AliParticleContainer* tracksCont, AliClusterContainer* clusterCont, AliEmcalJet* jet) const
1196 {
1197     // get a random cone
1198     if(fDebug > 1) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
1199     pt = 0; eta = 0; phi = 0;
1200     Float_t etaJet(999), phiJet(999), dJet(999);        // no jet: same as jet very far away
1201     if(jet) { // if a leading jet is given, use its kinematic properties to exclude it
1202         etaJet = jet->Eta();
1203         phiJet = jet->Phi();
1204     }
1205     // the random cone acceptance has to equal the jet acceptance
1206     // this also insures safety when runnnig on the semi-good tpc runs for 11h data,
1207     // where jet acceptance is adjusted to reduced acceptance - hence random cone acceptance as well
1208     Float_t minPhi(GetJetContainer()->GetJetPhiMin()), maxPhi(GetJetContainer()->GetJetPhiMax());
1209     if(maxPhi > TMath::TwoPi()) maxPhi = TMath::TwoPi();
1210     if(minPhi < 0 ) minPhi = 0.;
1211     // construct a random cone and see if it's far away enough from the leading jet
1212     Int_t attempts(1000);
1213     while(kTRUE) {
1214         attempts--;
1215         eta = gRandom->Uniform(GetJetContainer()->GetJetEtaMin(), GetJetContainer()->GetJetEtaMax());
1216         phi = gRandom->Uniform(minPhi, maxPhi);
1217
1218         dJet = TMath::Sqrt((etaJet-eta)*(etaJet-eta)+(phiJet-phi)*(phiJet-phi));
1219         if(dJet > fMinDisanceRCtoLJ) break;
1220         else if (attempts == 0) {
1221             printf(" > No random cone after 1000 tries, giving up ... !\n");
1222             return;
1223         }
1224     }
1225     // get the charged energy (if tracks are provided)
1226     if(tracksCont) {
1227         AliVParticle* track = tracksCont->GetNextAcceptParticle(0);
1228         while(track) {
1229             Float_t etaTrack(track->Eta()), phiTrack(track->Phi());
1230             // get distance from cone
1231             if(TMath::Abs(phiTrack-phi) > TMath::Abs(phiTrack - phi + TMath::TwoPi())) phiTrack+=TMath::TwoPi();
1232             if(TMath::Abs(phiTrack-phi) > TMath::Abs(phiTrack - phi - TMath::TwoPi())) phiTrack-=TMath::TwoPi();
1233             if(TMath::Sqrt(TMath::Abs((etaTrack-eta)*(etaTrack-eta)+(phiTrack-phi)*(phiTrack-phi))) <= GetJetRadius()) pt += track->Pt();
1234             track = tracksCont->GetNextAcceptParticle();
1235         }
1236     }
1237     // get the neutral energy (if clusters are provided)
1238     if(clusterCont) {
1239         TLorentzVector momentum;
1240         AliVCluster* cluster = clusterCont->GetNextAcceptCluster(0);
1241         while(cluster) {
1242             cluster->GetMomentum(momentum, const_cast<Double_t*>(fVertex));
1243             Float_t etaClus(momentum.Eta()), phiClus(momentum.Phi());
1244             // get distance from cone
1245             if(TMath::Abs(phiClus-phi) > TMath::Abs(phiClus - phi + TMath::TwoPi())) phiClus+=TMath::TwoPi();
1246             if(TMath::Abs(phiClus-phi) > TMath::Abs(phiClus - phi - TMath::TwoPi())) phiClus-=TMath::TwoPi();
1247             if(TMath::Sqrt(TMath::Abs((etaClus-eta)*(etaClus-eta)+(phiClus-phi)*(phiClus-phi))) <= GetJetRadius()) pt += momentum.Pt();
1248             cluster = clusterCont->GetNextAcceptCluster();
1249         }
1250     }
1251 }
1252 //_____________________________________________________________________________
1253 Double_t AliAnalysisTaskJetV2::CalculateQC2(Int_t harm) {
1254     // get the second order q-cumulant, a -999 return will be caught in the qa routine of CorrectRho
1255     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
1256     Double_t reQ(0), imQ(0), modQ(0), M11(0), M(0);
1257     if(fUsePtWeight) {  // for the weighted 2-nd order q-cumulant
1258         QCnQnk(harm, 1, reQ, imQ);      // get the weighted 2-nd order q-vectors
1259         modQ = reQ*reQ+imQ*imQ;         // get abs Q-squared
1260         M11 = QCnM11();                 // equals S2,1 - S1,2
1261         return (M11 > 0) ? ((modQ - QCnS(1,2))/M11) : -999;
1262     } // else return the non-weighted 2-nd order q-cumulant
1263     QCnQnk(harm, 0, reQ, imQ);          // get the non-weighted 2-nd order q-vectors
1264     modQ = reQ*reQ+imQ*imQ;             // get abs Q-squared
1265     M = QCnM();
1266     return (M > 1) ? (modQ - M)/(M*(M-1)) : -999;
1267 }
1268 //_____________________________________________________________________________
1269 Double_t AliAnalysisTaskJetV2::CalculateQC4(Int_t harm) {
1270     // get the fourth order q-cumulant, a -999 return will be caught in the qa routine of CorrectRho
1271     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
1272     Double_t reQn1(0), imQn1(0), reQ2n2(0), imQ2n2(0), reQn3(0), imQn3(0), M1111(0), M(0);
1273     Double_t a(0), b(0), c(0), d(0), e(0), f(0), g(0);  // terms of the calculation
1274     if(fUsePtWeight) {  // for the weighted 4-th order q-cumulant
1275         QCnQnk(harm, 1, reQn1, imQn1);
1276         QCnQnk(harm*2, 2, reQ2n2, imQ2n2);
1277         QCnQnk(harm, 3, reQn3, imQn3);
1278         // fill in the terms ...
1279         a = (reQn1*reQn1+imQn1*imQn1)*(reQn1*reQn1+imQn1*imQn1);
1280         b = reQ2n2*reQ2n2 + imQ2n2*imQ2n2;
1281         c = -2.*(reQ2n2*reQn1*reQn1-reQ2n2*imQn1*imQn1+2.*imQ2n2*reQn1*imQn1);
1282         d = 8.*(reQn3*reQn1+imQn3*imQn1);
1283         e = -4.*QCnS(1,2)*(reQn1*reQn1+imQn1*imQn1);
1284         f = -6.*QCnS(1,4);
1285         g = 2.*QCnS(2,2);
1286         M1111 = QCnM1111();
1287         return (M1111 > 0) ? (a+b+c+d+e+f+g)/M1111 : -999;
1288     }   // else return the unweighted case
1289     Double_t reQn(0), imQn(0), reQ2n(0), imQ2n(0);
1290     QCnQnk(harm, 0, reQn, imQn);
1291     QCnQnk(harm*2, 0, reQ2n, imQ2n);
1292     // fill in the terms ...
1293     M = QCnM();
1294     if(M < 4) return -999;
1295     a = (reQn*reQn+imQn*imQn)*(reQn*reQn+imQn*imQn);
1296     b = reQ2n*reQ2n + imQ2n*imQ2n;
1297     c = -2.*(reQ2n*reQn*reQn-reQ2n*imQn*imQn+2.*imQ2n*reQn*imQn);
1298     e = -4.*(M-2)*(reQn*reQn+imQn*imQn);
1299     f = 2.*M*(M-3);
1300     return (a+b+c+e+f)/(M*(M-1)*(M-2)*(M-3));
1301 }
1302 //_____________________________________________________________________________
1303 void AliAnalysisTaskJetV2::QCnQnk(Int_t n, Int_t k, Double_t &reQ, Double_t &imQ) {
1304     // get the weighted n-th order q-vector, pass real and imaginary part as reference
1305     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
1306     if(!fTracks) return;
1307     fNAcceptedTracksQCn = 0;
1308     Int_t iTracks(fTracks->GetEntriesFast());
1309     for(Int_t iTPC(0); iTPC < iTracks; iTPC++) {
1310         AliVTrack* track = static_cast<AliVTrack*>(fTracks->At(iTPC));
1311         if(!PassesCuts(track) || track->Pt() < fSoftTrackMinPt || track->Pt() > fSoftTrackMaxPt) continue;
1312         fNAcceptedTracksQCn++;
1313         // for the unweighted case, k equals zero and the weight doesn't contribute to the equation below
1314         reQ += TMath::Power(track->Pt(), k) * TMath::Cos(((double)n)*track->Phi());
1315         imQ += TMath::Power(track->Pt(), k) * TMath::Sin(((double)n)*track->Phi());
1316     }
1317 }
1318 //_____________________________________________________________________________
1319 void AliAnalysisTaskJetV2::QCnDiffentialFlowVectors(
1320         TClonesArray* pois, TArrayD* ptBins, Bool_t vpart, Double_t* repn, Double_t* impn, 
1321         Double_t *mp, Double_t *reqn, Double_t *imqn, Double_t* mq, Int_t n) 
1322 {
1323      if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
1324    // get  unweighted differential flow vectors
1325     Int_t iPois(pois->GetEntriesFast());
1326     if(vpart) {
1327         for(Int_t i(0); i < iPois; i++) {
1328             for(Int_t ptBin(0); ptBin < ptBins->GetSize()-1; ptBin++) {
1329                 AliVTrack* poi = static_cast<AliVTrack*>(pois->At(i));
1330                 if(PassesCuts(poi)) {
1331                     if(poi->Pt() >= ptBins->At(ptBin) && poi->Pt() < ptBins->At(ptBin+1)) {
1332                             // fill the flow vectors assuming that all poi's are in the rp selection (true by design)
1333                             repn[ptBin]+=TMath::Cos(((double)n)*poi->Phi());
1334                             impn[ptBin]+=TMath::Sin(((double)n)*poi->Phi());
1335                             mp[ptBin]++;
1336                             reqn[ptBin]+=TMath::Cos(((double)n)*poi->Phi());
1337                             imqn[ptBin]+=TMath::Sin(((double)n)*poi->Phi());
1338                             mq[ptBin]++;
1339                     }
1340                 }
1341             }
1342         }
1343     } else {
1344         for(Int_t i(0); i < iPois; i++) {
1345             for(Int_t ptBin(0); ptBin < ptBins->GetSize()-1; ptBin++) {
1346                 AliEmcalJet* poi = static_cast<AliEmcalJet*>(pois->At(i));
1347                 if(PassesCuts(poi)) {    
1348                     Double_t pt(poi->Pt()-poi->Area()*fLocalRho->GetLocalVal(poi->Phi(), GetJetContainer()->GetJetRadius(), fLocalRho->GetVal()));
1349                     if(pt >= ptBins->At(ptBin) && pt < ptBins->At(ptBin+1)) {    
1350                             repn[ptBin]+=TMath::Cos(((double)n)*poi->Phi());
1351                             impn[ptBin]+=TMath::Sin(((double)n)*poi->Phi());
1352                             mp[ptBin]++;        // qn isn't filled, no overlap between poi's and rp's
1353                     }
1354                 }
1355             }
1356         }
1357     }
1358 }
1359 //_____________________________________________________________________________
1360 Double_t AliAnalysisTaskJetV2::QCnS(Int_t i, Int_t j) {
1361     // get the weighted ij-th order autocorrelation correction
1362     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
1363     if(!fTracks || i <= 0 || j <= 0) return -999;
1364     Int_t iTracks(fTracks->GetEntriesFast());
1365     Double_t Sij(0);
1366     for(Int_t iTPC(0); iTPC < iTracks; iTPC++) {
1367         AliVTrack* track = static_cast<AliVTrack*>(fTracks->At(iTPC));
1368         if(!PassesCuts(track) || track->Pt() < fSoftTrackMinPt || track->Pt() > fSoftTrackMaxPt) continue;
1369         Sij+=TMath::Power(track->Pt(), j);
1370     }
1371     return TMath::Power(Sij, i);
1372 }
1373 //_____________________________________________________________________________
1374 Double_t AliAnalysisTaskJetV2::QCnM() {
1375     // get multiplicity for unweighted q-cumulants. function QCnQnk should be called first
1376     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
1377     return (Double_t) fNAcceptedTracksQCn;
1378 }
1379 //_____________________________________________________________________________
1380 Double_t AliAnalysisTaskJetV2::QCnM11() {
1381     // get multiplicity weights for the weighted two particle cumulant
1382     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
1383     return (QCnS(2,1) - QCnS(1,2));
1384 }
1385 //_____________________________________________________________________________
1386 Double_t AliAnalysisTaskJetV2::QCnM1111() {
1387     // get multiplicity weights for the weighted four particle cumulant
1388     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
1389     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));
1390 }
1391 //_____________________________________________________________________________
1392 Bool_t AliAnalysisTaskJetV2::QCnRecovery(Double_t psi2, Double_t psi3) {
1393     // decides how to deal with the situation where c2 or c3 is negative 
1394     // returns kTRUE depending on whether or not a modulated rho is used for the jet background
1395     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
1396     if(TMath::AreEqualAbs(fFitModulation->GetParameter(3), .0, 1e-10) && TMath::AreEqualAbs(fFitModulation->GetParameter(7), .0,1e-10)) {
1397         fFitModulation->SetParameter(7, 0);
1398         fFitModulation->SetParameter(3, 0);
1399         fFitModulation->SetParameter(0, fLocalRho->GetVal());
1400         return kTRUE;   // v2 and v3 have physical null values
1401     }
1402     switch (fQCRecovery) {
1403         case kFixedRho : {      // roll back to the original rho
1404            fFitModulation->SetParameter(7, 0);
1405            fFitModulation->SetParameter(3, 0);
1406            fFitModulation->SetParameter(0, fLocalRho->GetVal());
1407            return kFALSE;       // rho is forced to be fixed
1408         }
1409         case kNegativeVn : {
1410            Double_t c2(fFitModulation->GetParameter(3));
1411            Double_t c3(fFitModulation->GetParameter(7));
1412            if( c2 < 0 ) c2 = -1.*TMath::Sqrt(-1.*c2);
1413            if( c3 < 0 ) c3 = -1.*TMath::Sqrt(-1.*c3);
1414            fFitModulation->SetParameter(3, c2);
1415            fFitModulation->SetParameter(7, c3);
1416            return kTRUE;        // is this a physical quantity ?
1417         }
1418         case kTryFit : {
1419            fitModulationType tempType(fFitModulationType);  // store temporarily
1420            fFitModulationType = kCombined;
1421            fFitModulation->SetParameter(7, 0);
1422            fFitModulation->SetParameter(3, 0);
1423            Bool_t pass(CorrectRho(psi2, psi3));         // do the fit and all quality checks
1424            fFitModulationType = tempType;               // roll back for next event
1425            return pass;
1426         }
1427         default : return kFALSE;
1428     }
1429     return kFALSE;
1430 }
1431 //_____________________________________________________________________________
1432 Bool_t AliAnalysisTaskJetV2::CorrectRho(Double_t psi2, Double_t psi3) 
1433 {
1434     // get rho' -> rho(phi)
1435     // two routines are available, both can be used with or without pt weights
1436     //  [1] get vn from q-cumulants or as an integrated value from a user supplied histogram
1437     //      in case of cumulants, both cumulants and vn values are stored. in both cases, v2 and v3
1438     //      are expected. a check is performed to see if rho has no negative local minimum
1439     //      for full description, see Phys. Rev. C 83, 044913
1440     //      since the cn distribution has negative values, vn = sqrt(cn) can be imaginary sometimes
1441     //      in this case one can either roll back to the 'original' rixed rho, do a fit for vn or take use
1442     //      vn = - sqrt(|cn|) 
1443     //  [2] fitting a fourier expansion to the de/dphi distribution
1444     //      the fit can be done with either v2, v3 or a combination.
1445     //      in all cases, a cut can be made on the p-value of the chi-squared value of the fit
1446     //      and a check can be performed to see if rho has no negative local minimum
1447     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
1448     Int_t freeParams(2);                // free parameters of the fit (for NDF) 
1449     switch (fFitModulationType) {       // for approaches where no fitting is required
1450         case kQC2 : {
1451             fFitModulation->FixParameter(4, psi2); 
1452             fFitModulation->FixParameter(6, psi3);
1453             fFitModulation->FixParameter(3, CalculateQC2(2));   // set here with cn, vn = sqrt(cn)
1454             fFitModulation->FixParameter(7, CalculateQC2(3));
1455             // first fill the histos of the raw cumulant distribution
1456             if (fUsePtWeight) { // use weighted weights
1457                 Double_t dQCnM11 = (fNoEventWeightsForQC) ? 1. : QCnM11();
1458                 fProfV2Cumulant->Fill(fCent, fFitModulation->GetParameter(3), dQCnM11);
1459                 fProfV3Cumulant->Fill(fCent, fFitModulation->GetParameter(7), dQCnM11);
1460             } else {
1461                 Double_t dQCnM = (fNoEventWeightsForQC) ? 2. : QCnM();
1462                 fProfV2Cumulant->Fill(fCent, fFitModulation->GetParameter(3), dQCnM*(dQCnM-1));
1463                 fProfV3Cumulant->Fill(fCent, fFitModulation->GetParameter(7), dQCnM*(dQCnM-1));
1464             }
1465             // then see if one of the cn value is larger than zero and vn is readily available
1466             if(fFitModulation->GetParameter(3) > 0 && fFitModulation->GetParameter(7) > 0) {
1467                 fFitModulation->FixParameter(3, TMath::Sqrt(fFitModulation->GetParameter(3)));
1468                 fFitModulation->FixParameter(7, TMath::Sqrt(fFitModulation->GetParameter(7)));
1469             } else if (!QCnRecovery(psi2, psi3)) return kFALSE;  // try to recover the cumulant, this will set v2 and v3
1470             if(fFitModulation->GetMinimum(0, TMath::TwoPi()) < 0) {  // general check 
1471                 fFitModulation->SetParameter(7, 0);
1472                 fFitModulation->SetParameter(3, 0);
1473                 fFitModulation->SetParameter(0, fLocalRho->GetVal());
1474                 return kFALSE;
1475             }
1476             return kTRUE;
1477         } break;
1478         case kQC4 : {
1479             fFitModulation->FixParameter(4, psi2); 
1480             fFitModulation->FixParameter(6, psi3);
1481             fFitModulation->FixParameter(3, CalculateQC4(2));   // set here with cn, vn = sqrt(cn)
1482             fFitModulation->FixParameter(7, CalculateQC4(3));
1483             // first fill the histos of the raw cumulant distribution
1484             if (fUsePtWeight) { // use weighted weights
1485                 fProfV2Cumulant->Fill(fCent, fFitModulation->GetParameter(3)/*, QCnM1111()*/);
1486                 fProfV3Cumulant->Fill(fCent, fFitModulation->GetParameter(7)/*, QCnM1111()*/);
1487             } else {
1488                 fProfV2Cumulant->Fill(fCent, fFitModulation->GetParameter(3)/*, QCnM1111()*/);
1489                 fProfV3Cumulant->Fill(fCent, fFitModulation->GetParameter(7)/*, QCnM1111()*/);
1490             }
1491             // then see if one of the cn value is larger than zero and vn is readily available
1492             if(fFitModulation->GetParameter(3) > 0 && fFitModulation->GetParameter(7) > 0) {
1493                 fFitModulation->FixParameter(3, TMath::Sqrt(fFitModulation->GetParameter(3)));
1494                 fFitModulation->FixParameter(7, TMath::Sqrt(fFitModulation->GetParameter(7)));
1495             } else if (!QCnRecovery(psi2, psi3)) return kFALSE;  // try to recover the cumulant, this will set v2 and v3
1496             if(fFitModulation->GetMinimum(0, TMath::TwoPi()) < 0) {  // general check 
1497                 fFitModulation->SetParameter(7, 0);
1498                 fFitModulation->SetParameter(3, 0);
1499                 fFitModulation->SetParameter(0, fLocalRho->GetVal());
1500                 return kFALSE;
1501             }
1502         } break;
1503         case kIntegratedFlow : {
1504             // use v2 and v3 values from an earlier iteration over the data
1505             fFitModulation->FixParameter(3, fUserSuppliedV2->GetBinContent(fUserSuppliedV2->GetXaxis()->FindBin(fCent)));
1506             fFitModulation->FixParameter(4, psi2);
1507             fFitModulation->FixParameter(6, psi3);
1508             fFitModulation->FixParameter(7, fUserSuppliedV3->GetBinContent(fUserSuppliedV3->GetXaxis()->FindBin(fCent)));
1509             if(fFitModulation->GetMinimum(0, TMath::TwoPi()) < 0) { 
1510                 fFitModulation->SetParameter(7, 0);
1511                 fFitModulation->SetParameter(3, 0);
1512                 fFitModulation->SetParameter(0, fLocalRho->GetVal());
1513                 return kFALSE;
1514             }
1515             return kTRUE;
1516         }
1517         default : break;
1518     }
1519     TString detector("");
1520     switch (fDetectorType) {
1521         case kTPC : detector+="TPC";
1522             break;
1523         case kVZEROA : detector+="VZEROA";
1524             break;
1525         case kVZEROC : detector+="VZEROC";
1526             break;
1527         case kVZEROComb : detector+="VZEROComb";
1528             break; 
1529         case kFixedEP : detector+="FixedEP";
1530             break;
1531         default: break;
1532     }
1533     Int_t iTracks(fTracks->GetEntriesFast());
1534     Double_t excludeInEta = -999;
1535     Double_t excludeInPhi = -999;
1536     Double_t excludeInPt  = -999;
1537     if(iTracks <= 0 || fLocalRho->GetVal() <= 0 ) return kFALSE;   // no use fitting an empty event ...
1538     if(fExcludeLeadingJetsFromFit > 0 ) {
1539         if(fLeadingJet) {
1540             excludeInEta = fLeadingJet->Eta();
1541             excludeInPhi = fLeadingJet->Phi();
1542             excludeInPt = fLeadingJet->Pt();
1543         }
1544     }
1545     // check the acceptance of the track selection that will be used
1546     // if one uses e.g. semi-good tpc tracks, accepance in phi is reduced to 0 < phi < 4
1547     // the defaults (-10 < phi < 10) which accept all, are then overwritten
1548     Double_t lowBound(0.), upBound(TMath::TwoPi());     // bounds for fit
1549     if(GetParticleContainer()->GetParticlePhiMin() > lowBound) lowBound = GetParticleContainer()->GetParticlePhiMin();
1550     if(GetParticleContainer()->GetParticlePhiMax() < upBound) upBound = GetParticleContainer()->GetParticlePhiMax();
1551     fHistSwap->Reset(); // clear the histogram
1552     TH1F _tempSwap;     // on stack for quick access
1553     TH1F _tempSwapN;    // on stack for quick access, bookkeeping histogram
1554     if(fRebinSwapHistoOnTheFly) {
1555         if(fNAcceptedTracks < 49) fNAcceptedTracks = 49;       // avoid aliasing effects
1556         _tempSwap = TH1F("_tempSwap", "_tempSwap", TMath::CeilNint(TMath::Sqrt(fNAcceptedTracks)), lowBound, upBound);
1557         if(fUsePtWeightErrorPropagation) _tempSwapN = TH1F("_tempSwapN", "_tempSwapN", TMath::CeilNint(TMath::Sqrt(fNAcceptedTracks)), lowBound, upBound);
1558         if(fUsePtWeight) _tempSwap.Sumw2();
1559     }
1560     else _tempSwap = *fHistSwap;         // now _tempSwap holds the desired histo
1561     // non poissonian error when using pt weights
1562     Double_t totalpts(0.), totalptsquares(0.), totalns(0.);
1563     for(Int_t i(0); i < iTracks; i++) {
1564         AliVTrack* track = static_cast<AliVTrack*>(fTracks->At(i));
1565         if(fExcludeLeadingJetsFromFit > 0 &&( (TMath::Abs(track->Eta() - excludeInEta) < GetJetContainer()->GetJetRadius()*fExcludeLeadingJetsFromFit ) || (TMath::Abs(track->Eta()) - GetJetContainer()->GetJetRadius() - GetJetContainer()->GetJetEtaMax() ) > 0 )) continue;
1566         if(!PassesCuts(track) || track->Pt() > fSoftTrackMaxPt || track->Pt() < fSoftTrackMinPt) continue;
1567         if(fUsePtWeight) {
1568             _tempSwap.Fill(track->Phi(), track->Pt());
1569             if(fUsePtWeightErrorPropagation) {
1570                 totalpts += track->Pt();
1571                 totalptsquares += track->Pt()*track->Pt();
1572                 totalns += 1;
1573                 _tempSwapN.Fill(track->Phi());
1574             }
1575         }
1576         else _tempSwap.Fill(track->Phi());
1577     }
1578     if(fUsePtWeight && fUsePtWeightErrorPropagation) {
1579         // in the case of pt weights overwrite the poissonian error estimate which is assigned by root by a more sophisticated appraoch
1580         // 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
1581         // 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 
1582         // event, for the latter use a poissonian estimate. the two contrubitions are assumed to be uncorrelated
1583         if(totalns < 2) return kFALSE; // not one track passes the cuts > 2 avoids possible division by 0 later on
1584         for(Int_t l = 0; l < _tempSwap.GetNbinsX(); l++) {
1585             if(_tempSwapN.GetBinContent(l+1) == 0) {
1586                 _tempSwap.SetBinContent(l+1,0);
1587                 _tempSwap.SetBinError(l+1,0);
1588             }
1589             else {
1590                 Double_t vartimesnsq = totalptsquares*totalns - totalpts*totalpts;
1591                 Double_t variance = vartimesnsq/(totalns*(totalns-1.));
1592                 Double_t SDOMSq = variance / _tempSwapN.GetBinContent(l+1);
1593                 Double_t SDOMSqOverMeanSq = SDOMSq * _tempSwapN.GetBinContent(l+1) * _tempSwapN.GetBinContent(l+1) / (_tempSwapN.GetBinContent(l+1) * _tempSwapN.GetBinContent(l+1));
1594                 Double_t poissonfrac = 1./_tempSwapN.GetBinContent(l+1);
1595                 Double_t vartotalfrac = SDOMSqOverMeanSq + poissonfrac;
1596                 Double_t vartotal = vartotalfrac * _tempSwap.GetBinContent(l+1) * _tempSwap.GetBinContent(l+1);
1597                 if(vartotal > 0.0001) _tempSwap.SetBinError(l+1,TMath::Sqrt(vartotal));
1598                 else {
1599                     _tempSwap.SetBinContent(l+1,0);
1600                     _tempSwap.SetBinError(l+1,0);
1601                 }
1602             }
1603         }
1604     }
1605     fFitModulation->SetParameter(0, fLocalRho->GetVal());
1606     switch (fFitModulationType) {
1607         case kNoFit : { 
1608             fFitModulation->FixParameter(0, fLocalRho->GetVal() ); 
1609             freeParams = 0;
1610         } break;
1611         case kV2 : { 
1612             fFitModulation->FixParameter(4, psi2); 
1613             freeParams = 1;
1614         } break;
1615         case kV3 : { 
1616             fFitModulation->FixParameter(4, psi3); 
1617             freeParams = 1;
1618         } break;
1619         case kCombined : {
1620             fFitModulation->FixParameter(4, psi2); 
1621             fFitModulation->FixParameter(6, psi3);
1622             freeParams = 2;
1623         } break;
1624         case kFourierSeries : {
1625             // in this approach, an explicit calculation will be made of vn = sqrt(xn^2+yn^2)
1626             // where x[y] = Integrate[r(phi)cos[sin](n phi)dphi, 0, 2pi]
1627             Double_t cos2(0), sin2(0), cos3(0), sin3(0), sumPt(0);
1628             for(Int_t i(0); i < iTracks; i++) {
1629                 AliVTrack* track = static_cast<AliVTrack*>(fTracks->At(i));
1630                 if(!PassesCuts(track) || track->Pt() > fSoftTrackMaxPt || track->Pt() < fSoftTrackMinPt) continue;
1631                 sumPt += track->Pt();
1632                 cos2 += track->Pt()*TMath::Cos(2*PhaseShift(track->Phi()-psi2)); 
1633                 sin2 += track->Pt()*TMath::Sin(2*PhaseShift(track->Phi()-psi2));
1634                 cos3 += track->Pt()*TMath::Cos(3*PhaseShift(track->Phi()-psi3)); 
1635                 sin3 += track->Pt()*TMath::Sin(3*PhaseShift(track->Phi()-psi3));
1636             }
1637             fFitModulation->SetParameter(3, TMath::Sqrt(cos2*cos2+sin2*sin2)/fLocalRho->GetVal());
1638             fFitModulation->SetParameter(4, psi2);
1639             fFitModulation->SetParameter(6, psi3);
1640             fFitModulation->SetParameter(7, TMath::Sqrt(cos3*cos3+sin3*sin3)/fLocalRho->GetVal());
1641         } break;
1642         default : break;
1643     }
1644     if(fRunToyMC) {
1645         // toy mc, just here to check procedure, azimuthal profile is filled from hypothesis so p-value distribution should be flat
1646         Int_t _bins = _tempSwap.GetXaxis()->GetNbins();
1647         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());
1648         _tempFit->SetParameter(0, fFitModulation->GetParameter(0));       // normalization
1649         _tempFit->SetParameter(3, 0.1);      // v2
1650         _tempFit->FixParameter(1, 1.);       // constant
1651         _tempFit->FixParameter(2, 2.);       // constant
1652         _tempFit->FixParameter(5, 3.);       // constant
1653         _tempFit->FixParameter(4, fFitModulation->GetParameter(4));
1654         _tempFit->FixParameter(6, fFitModulation->GetParameter(6));
1655         _tempFit->SetParameter(7, 0.1);      // v3
1656         _tempSwap.Reset();                   // rese bin content
1657         for(int _binsI = 0; _binsI < _bins*_bins; _binsI++)  _tempSwap.Fill(_tempFit->GetRandom());
1658     }
1659     _tempSwap.Fit(fFitModulation, fFitModulationOptions.Data(), "", lowBound, upBound);
1660     // the quality of the fit is evaluated from 1 - the cdf of the chi square distribution
1661     // three methods are available, all with their drawbacks. all are stored, one is selected to do the cut
1662     Int_t NDF(_tempSwap.GetXaxis()->GetNbins()-freeParams);
1663     if(NDF == 0 || (float)NDF <= 0.) return kFALSE;
1664     Double_t CDF(1.-ChiSquareCDF(NDF, ChiSquare(_tempSwap, fFitModulation)));
1665     Double_t CDFROOT(1.-ChiSquareCDF(NDF, fFitModulation->GetChisquare()));
1666     Double_t CDFKolmogorov(KolmogorovTest(_tempSwap, fFitModulation));
1667     // fill the values and centrality correlation (redundant but easy on the eyes)
1668     fHistPvalueCDF->Fill(CDF);
1669     fHistPvalueCDFCent->Fill(fCent, CDF);
1670     fHistPvalueCDFROOT->Fill(CDFROOT);
1671     fHistPvalueCDFROOTCent->Fill(fCent, CDFROOT);
1672     fHistKolmogorovTest->Fill(CDFKolmogorov);
1673     fHistChi2ROOTCent->Fill(fCent, fFitModulation->GetChisquare()/((float)NDF));
1674     fHistChi2Cent->Fill(fCent, ChiSquare(_tempSwap, fFitModulation)/((float)NDF));
1675     fHistKolmogorovTestCent->Fill(fCent, CDFKolmogorov);
1676     fHistPChi2Root->Fill(CDFROOT, fFitModulation->GetChisquare()/((float)NDF));
1677     fHistPChi2->Fill(CDF, ChiSquare(_tempSwap, fFitModulation)/((float)NDF));
1678     fHistPKolmogorov->Fill(CDF, CDFKolmogorov);
1679
1680     // variable CDF is used for making cuts, so we fill it with the selected p-value
1681     switch (fFitGoodnessTest) {
1682         case kChi2ROOT : {
1683             CDF = CDFROOT; 
1684         } break;
1685         case kChi2Poisson : break;      // CDF is already CDF
1686         case kKolmogorov : {
1687             CDF = CDFKolmogorov; 
1688         } break;
1689         default: break;
1690     }
1691
1692     if(fFitControl) {
1693         // as an additional quality check, see if fitting a control fit has a higher significance
1694         _tempSwap.Fit(fFitControl, fFitModulationOptions.Data(), "", lowBound, upBound);
1695         Double_t CDFControl(-1.);
1696         switch (fFitGoodnessTest) {
1697             case kChi2ROOT : {
1698                 CDFControl = 1.-ChiSquareCDF(fFitControl->GetNDF(), fFitModulation->GetChisquare());
1699             } break;
1700             case kChi2Poisson : {
1701                 CDFControl = 1.-ChiSquareCDF(fFitControl->GetNDF(), ChiSquare(_tempSwap, fFitModulation));
1702             } break;
1703             case kKolmogorov : {
1704                 CDFControl = KolmogorovTest(_tempSwap, fFitControl); 
1705             } break;
1706             default: break;
1707         }
1708         if(CDFControl > CDF) {
1709             CDF = -1.; // control fit is more significant, so throw out the 'old' fit
1710             fHistRhoStatusCent->Fill(fCent, -1);
1711         }
1712     }
1713     if(CDF >= fMinPvalue && CDF <= fMaxPvalue && ( fFitModulation->GetMinimum(0, TMath::TwoPi()) > 0)) {       
1714         // fit quality. not that although with limited acceptance the fit is performed on just
1715         // part of phase space, the requirement that energy desntiy is larger than zero is applied
1716         // to the FULL spectrum
1717         fHistRhoStatusCent->Fill(fCent, 0.);
1718         // for LOCAL didactic purposes, save the  best and the worst fits
1719         // this routine can produce a lot of output histograms (it's not memory 'safe') and will not work on GRID 
1720         // since the output will become unmergeable (i.e. different nodes may produce conflicting output)
1721         switch (fRunModeType) {
1722             case kLocal : {
1723                 if(fRandom->Uniform(0, 100) > fPercentageOfFits) break;
1724                 static Int_t didacticCounterBest(0);
1725                 TProfile* didacticProfile = (TProfile*)_tempSwap.Clone(Form("Fit_%i_1-CDF_%.3f_cen_%i_%s", didacticCounterBest, CDF, fInCentralitySelection, detector.Data()));
1726                 TF1* didacticFit = (TF1*)fFitModulation->Clone(Form("fit_%i_CDF_%.3f_cen_%i_%s", didacticCounterBest, CDF, fInCentralitySelection, detector.Data()));
1727                 switch(fFitModulationType) { 
1728                     case kCombined : {
1729                         // to make a nice picture also plot the separate components (v2 and v3) of the fit
1730                         // only done for cobined fit where there are actually components to split ...
1731                         TF1* v0(new TF1("dfit_kV2", "[0]", 0, TMath::TwoPi()));
1732                         v0->SetParameter(0, didacticFit->GetParameter(0));        // normalization
1733                         v0->SetLineColor(kMagenta);
1734                         v0->SetLineStyle(7);
1735                         didacticProfile->GetListOfFunctions()->Add(v0);
1736                         TF1* v2(new TF1("dfit_kV2", "[0]*([1]+[2]*[3]*TMath::Cos([2]*(x-[4])))", 0, TMath::TwoPi()));
1737                         v2->SetParameter(0, didacticFit->GetParameter(0));        // normalization
1738                         v2->SetParameter(3, didacticFit->GetParameter(3));        // v2
1739                         v2->FixParameter(1, 1.);        // constant
1740                         v2->FixParameter(2, 2.);        // constant
1741                         v2->FixParameter(4, didacticFit->GetParameter(4));        // psi2
1742                         v2->SetLineColor(kGreen);
1743                         didacticProfile->GetListOfFunctions()->Add(v2);
1744                         TF1* v3(new TF1("dfit_kV3", "[0]*([1]+[2]*[3]*TMath::Cos([5]*(x-[4])))", 0, TMath::TwoPi()));
1745                         v3->SetParameter(0, didacticFit->GetParameter(0));        // normalization
1746                         v3->SetParameter(3, didacticFit->GetParameter(7));        // v3
1747                         v3->FixParameter(1, 1.);        // constant
1748                         v3->FixParameter(2, 2.);        // constant
1749                         v3->FixParameter(4, didacticFit->GetParameter(6));        // psi3
1750                         v3->FixParameter(5, 3.);        // constant
1751                         v3->SetLineColor(kCyan);
1752                         didacticProfile->GetListOfFunctions()->Add(v3);
1753                     }
1754                     default : break;
1755                 }
1756                 didacticProfile->GetListOfFunctions()->Add(didacticFit);
1757                 didacticProfile->GetYaxis()->SetTitle("#frac{d #sum #it{p}_{T}}{d #varphi} [GeV/#it{c}]");
1758                 didacticProfile->GetXaxis()->SetTitle("#varphi");
1759                 fOutputListGood->Add(didacticProfile);
1760                 didacticCounterBest++;
1761                 TH2F* didacticSurface = BookTH2F(Form("surface_%s", didacticProfile->GetName()), "#phi", "#eta", 50, 0, TMath::TwoPi(), 50, -1, 1, -1, kFALSE);
1762                 for(Int_t i(0); i < iTracks; i++) {
1763                     AliVTrack* track = static_cast<AliVTrack*>(fTracks->At(i));
1764                     if(PassesCuts(track)) {
1765                         if(fUsePtWeight) didacticSurface->Fill(track->Phi(), track->Eta(), track->Pt());
1766                         else didacticSurface->Fill(track->Phi(), track->Eta());
1767                     }
1768                 }
1769                 if(fExcludeLeadingJetsFromFit) {       // visualize the excluded region
1770                     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);
1771                     f2->SetParameters(excludeInPt/3.,excludeInPhi,.1,excludeInEta,.1);
1772                     didacticSurface->GetListOfFunctions()->Add(f2);
1773                 }
1774                 fOutputListGood->Add(didacticSurface);
1775             } break;
1776             default : break;
1777         }
1778     } else {    // if the fit is of poor quality revert to the original rho estimate
1779         switch (fRunModeType) { // again see if we want to save the fit
1780             case kLocal : {
1781                 static Int_t didacticCounterWorst(0);
1782                 if(fRandom->Uniform(0, 100) > fPercentageOfFits) break;
1783                 TProfile* didacticProfile = (TProfile*)_tempSwap.Clone(Form("Fit_%i_1-CDF_%.3f_cen_%i_%s", didacticCounterWorst, CDF, fInCentralitySelection, detector.Data() ));
1784                 TF1* didacticFit = (TF1*)fFitModulation->Clone(Form("fit_%i_p_%.3f_cen_%i_%s", didacticCounterWorst, CDF, fInCentralitySelection, detector.Data()));
1785                 didacticProfile->GetListOfFunctions()->Add(didacticFit);
1786                 fOutputListBad->Add(didacticProfile);
1787                 didacticCounterWorst++;
1788                 } break;
1789             default : break;
1790         }
1791         switch (fFitModulationType) {
1792             case kNoFit : break;        // nothing to do
1793             case kCombined : fFitModulation->SetParameter(7, 0);        // no break
1794             case kFourierSeries : fFitModulation->SetParameter(7, 0);   // no break
1795             default : { // needs to be done if there was a poor fit
1796                  fFitModulation->SetParameter(3, 0);
1797                  fFitModulation->SetParameter(0, fLocalRho->GetVal());
1798             } break;
1799         }
1800         if(CDF > -.5) fHistRhoStatusCent->Fill(fCent, 1.);
1801         return kFALSE;  // return false if the fit is rejected
1802     }
1803     return kTRUE;
1804 }
1805 //_____________________________________________________________________________
1806 Bool_t AliAnalysisTaskJetV2::PassesCuts(AliVEvent* event)
1807 {
1808     // event cuts
1809     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
1810     switch (fCollisionType) {
1811         case kJetFlowMC : {
1812             fInCentralitySelection = 0;
1813             return kTRUE;
1814     } break;
1815         default : break;
1816     }
1817     if(!event || !AliAnalysisTaskEmcal::IsEventSelected()) return kFALSE;
1818     if(TMath::Abs(InputEvent()->GetPrimaryVertex()->GetZ()) > 10.) return kFALSE;
1819     // aod and esd specific checks
1820     switch (fDataType) {
1821        case kESD: {
1822             AliESDEvent* esdEvent = static_cast<AliESDEvent*>(InputEvent());
1823             if( (!esdEvent) || (TMath::Abs(esdEvent->GetPrimaryVertexSPD()->GetZ() - esdEvent->GetPrimaryVertex()->GetZ()) > .5) ) return kFALSE; 
1824        } break;
1825        case kAOD: {
1826             AliAODEvent* aodEvent = static_cast<AliAODEvent*>(InputEvent());
1827             if( (!aodEvent) || (TMath::Abs(aodEvent->GetPrimaryVertexSPD()->GetZ() - aodEvent->GetPrimaryVertex()->GetZ()) > .5) ) return kFALSE; 
1828        } break;
1829        default: break;
1830     }
1831     fCent = InputEvent()->GetCentrality()->GetCentralityPercentile("V0M");
1832     if(fCent <= fCentralityClasses->At(0) || fCent >= fCentralityClasses->At(fCentralityClasses->GetSize()-1) || TMath::Abs(fCent-InputEvent()->GetCentrality()->GetCentralityPercentile("TRK")) > 5.) return kFALSE;
1833     // determine centrality class
1834     fInCentralitySelection = -1;
1835     for(Int_t i(0); i < fCentralityClasses->GetSize()-1; i++) {
1836         if(fCent >= fCentralityClasses->At(i) && fCent <= fCentralityClasses->At(1+i)) {
1837             fInCentralitySelection = i;
1838             break;
1839         }
1840     } 
1841     if(fInCentralitySelection<0) return kFALSE;     // should be null op
1842     // see if input containers are filled
1843     if(fTracks->GetEntries() < 1) return kFALSE;
1844     if(fRho->GetVal() <= 0 ) return kFALSE;
1845     if(fAnalysisType == AliAnalysisTaskJetV2::kFull && !fClusterCont) return kFALSE;
1846     return kTRUE;
1847 }
1848 //_____________________________________________________________________________
1849 void AliAnalysisTaskJetV2::FillHistogramsAfterSubtraction(Double_t psi2, Double_t vzero[2][2], Double_t* vzeroComb, Double_t* tpc)
1850 {
1851     // fill histograms 
1852     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
1853     if(fEventPlaneWeights) {
1854         FillWeightedTrackHistograms();
1855         if(fAnalysisType == AliAnalysisTaskJetV2::kFull) FillWeightedClusterHistograms();
1856         FillWeightedJetHistograms(psi2); 
1857         if(fFillQAHistograms) FillWeightedEventPlaneHistograms(vzero, vzeroComb, tpc);
1858         FillWeightedRhoHistograms();
1859         FillWeightedDeltaPtHistograms(psi2);
1860     } else {
1861         FillTrackHistograms();
1862         if(fAnalysisType == AliAnalysisTaskJetV2::kFull) FillClusterHistograms();
1863         FillJetHistograms(psi2); 
1864         if(fFillQAHistograms) FillEventPlaneHistograms(vzero, vzeroComb, tpc);
1865         FillRhoHistograms();
1866         FillDeltaPtHistograms(psi2);
1867     }
1868 }
1869 //_____________________________________________________________________________
1870 void AliAnalysisTaskJetV2::FillTrackHistograms() const
1871 {
1872     // fill track histograms
1873     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
1874     Int_t iTracks(fTracks->GetEntriesFast()), iAcceptedTracks(0);
1875     for(Int_t i(0); i < iTracks; i++) {
1876         AliVTrack* track = static_cast<AliVTrack*>(fTracks->At(i));
1877         if(!PassesCuts(track)) continue;
1878         iAcceptedTracks++;
1879         fHistPicoTrackPt[fInCentralitySelection]->Fill(track->Pt());
1880         if(fFillQAHistograms) FillQAHistograms(track);
1881     }
1882     fHistPicoTrackMult[fInCentralitySelection]->Fill(iAcceptedTracks);
1883 }
1884 //_____________________________________________________________________________
1885 void AliAnalysisTaskJetV2::FillClusterHistograms() const
1886 {
1887     // fill cluster histograms
1888     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
1889     if(!fClusterCont) return;
1890     Int_t iClusters(fClusterCont->GetNClusters());
1891     TLorentzVector clusterLorentzVector;
1892     for(Int_t i(0); i < iClusters; i++) {
1893         AliVCluster* cluster = fClusterCont->GetCluster(i);
1894         if (!PassesCuts(cluster)) continue;
1895         cluster->GetMomentum(clusterLorentzVector, const_cast<Double_t*>(fVertex));
1896         fHistClusterPt[fInCentralitySelection]->Fill(clusterLorentzVector.Pt());
1897         fHistClusterEtaPhi[fInCentralitySelection]->Fill(clusterLorentzVector.Eta(), clusterLorentzVector.Phi());
1898         fHistClusterEtaPhiWeighted[fInCentralitySelection]->Fill(clusterLorentzVector.Eta(), clusterLorentzVector.Phi(), clusterLorentzVector.Pt());
1899     }
1900     return;
1901 }
1902 //_____________________________________________________________________________
1903 void AliAnalysisTaskJetV2::FillEventPlaneHistograms(Double_t vzero[2][2], Double_t* vzeroComb, Double_t* tpc) const
1904 {
1905     // fill event plane histograms, only called in qa mode
1906     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
1907     fHistPsiControl->Fill(0.5, vzero[0][0]);    // vzero a psi2
1908     fHistPsiControl->Fill(1.5, vzero[1][0]);    // vzero c psi2
1909     fHistPsiControl->Fill(2.5, tpc[0]);         // tpc psi 2
1910     fHistPsiControl->Fill(5.5, vzero[0][1]);    // vzero a psi3
1911     fHistPsiControl->Fill(6.5, vzero[1][1]);    // vzero b psi3
1912     fHistPsiControl->Fill(7.5, tpc[1]);         // tpc psi 3
1913     fHistPsiVZEROA->Fill(vzero[0][0]);
1914     fHistPsiVZEROC->Fill(vzero[1][0]);
1915     fHistPsiVZERO->Fill(vzeroComb[0]);
1916     fHistPsiTPC->Fill(tpc[0]);
1917     fHistPsiSpread->Fill(0.5, TMath::Abs(vzero[0][0]-vzero[1][0]));
1918     fHistPsiSpread->Fill(1.5, TMath::Abs(vzero[0][0]-tpc[0]));
1919     fHistPsiSpread->Fill(2.5, TMath::Abs(vzero[1][0]-tpc[0]));
1920     // event plane vs centrality QA histo's to check recentering
1921     Double_t TRK(InputEvent()->GetCentrality()->GetCentralityPercentile("TRK"));
1922     Double_t V0M(InputEvent()->GetCentrality()->GetCentralityPercentile("V0M"));
1923     fHistPsiVZEROAV0M->Fill(V0M, vzero[0][0]);
1924     fHistPsiVZEROCV0M->Fill(V0M, vzero[1][0]);
1925     fHistPsiVZEROVV0M->Fill(V0M, vzeroComb[0]);
1926     fHistPsiTPCV0M->Fill(V0M, tpc[0]);
1927     fHistPsiVZEROATRK->Fill(TRK, vzero[0][0]);
1928     fHistPsiVZEROCTRK->Fill(TRK, vzero[1][0]);
1929     fHistPsiVZEROTRK->Fill(TRK, vzeroComb[0]);
1930     fHistPsiTPCTRK->Fill(TRK, tpc[0]);
1931     // leading jet vs event plane bias
1932     if(fLeadingJet) {
1933         Double_t rho(fLocalRho->GetLocalVal(fLeadingJet->Phi(), GetJetContainer()->GetJetRadius(), fLocalRho->GetVal()));
1934         Double_t pt(fLeadingJet->Pt() - fLeadingJet->Area()*rho);
1935         fHistPsiTPCLeadingJet[fInCentralitySelection]->Fill(pt, tpc[0], fLeadingJet->Phi());
1936         fHistPsiVZEROALeadingJet[fInCentralitySelection]->Fill(pt, vzero[0][0], fLeadingJet->Phi());
1937         fHistPsiVZEROCLeadingJet[fInCentralitySelection]->Fill(pt, vzero[1][0], fLeadingJet->Phi());
1938         fHistPsiVZEROCombLeadingJet[fInCentralitySelection]->Fill(pt, vzeroComb[0], fLeadingJet->Phi());
1939     }
1940     // correlation of event planes
1941     fHistPsi2Correlation[fInCentralitySelection]->Fill(tpc[0], vzero[0][0], vzero[1][0]);
1942 }
1943 //_____________________________________________________________________________
1944 void AliAnalysisTaskJetV2::FillRhoHistograms()
1945 {
1946     // fill rho histograms
1947     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
1948     fHistRhoPackage[fInCentralitySelection]->Fill(fLocalRho->GetVal());    // save the rho estimate from the emcal jet package
1949     // get multiplicity FIXME inefficient
1950     Int_t iJets(fJets->GetEntriesFast());
1951     Double_t rho(fLocalRho->GetLocalVal(TMath::Pi(), TMath::Pi(), fLocalRho->GetVal()));
1952     fHistRho[fInCentralitySelection]->Fill(rho);
1953     fHistRhoVsMult->Fill(fTracks->GetEntries(), rho);
1954     fHistRhoVsCent->Fill(fCent, rho);
1955     for(Int_t i(0); i < iJets; i++) {
1956         AliEmcalJet* jet = static_cast<AliEmcalJet*>(fJets->At(i));
1957         if(!PassesCuts(jet)) continue;
1958         fHistRhoAVsMult->Fill(fTracks->GetEntries(), rho * jet->Area());
1959         fHistRhoAVsCent->Fill(fCent, rho * jet->Area());
1960     }
1961 }
1962 //_____________________________________________________________________________
1963 void AliAnalysisTaskJetV2::FillDeltaPtHistograms(Double_t psi2) const
1964 {
1965     // fill delta pt histograms
1966     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
1967     Int_t i(0);
1968     const Float_t areaRC = GetJetRadius()*GetJetRadius()*TMath::Pi();
1969     // we're retrieved the leading jet, now get a random cone
1970     for(i = 0; i < fMaxCones; i++) {
1971        Float_t pt(0), eta(0), phi(0);
1972        // get a random cone without constraints on leading jet position
1973        CalculateRandomCone(pt, eta, phi, fTracksCont, fClusterCont, 0x0);
1974        if(pt > 0) {
1975            if(fFillQAHistograms) fHistRCPhiEta[fInCentralitySelection]->Fill(phi, eta);
1976            fHistRhoVsRCPt[fInCentralitySelection]->Fill(pt, fLocalRho->GetLocalVal(phi, GetJetContainer()->GetJetRadius(), fLocalRho->GetVal())*areaRC);
1977            fHistRCPt[fInCentralitySelection]->Fill(pt);
1978            fHistDeltaPtDeltaPhi2[fInCentralitySelection]->Fill(PhaseShift(phi-psi2, 2.), pt - areaRC*fLocalRho->GetLocalVal(phi, GetJetContainer()->GetJetRadius(), fLocalRho->GetVal()));
1979            fHistDeltaPtDeltaPhi2Rho0[fInCentralitySelection]->Fill(PhaseShift(phi-psi2, 2.), pt - areaRC*fLocalRho->GetVal());
1980
1981        }
1982        // get a random cone excluding leading jet area
1983        CalculateRandomCone(pt, eta, phi, fTracksCont, fClusterCont, fLeadingJet);
1984        if(pt > 0) {
1985            if(fFillQAHistograms) fHistRCPhiEtaExLJ[fInCentralitySelection]->Fill(phi, eta);
1986            fHistRhoVsRCPtExLJ[fInCentralitySelection]->Fill(pt, fLocalRho->GetLocalVal(phi, GetJetContainer()->GetJetRadius(), fLocalRho->GetVal())*areaRC);
1987            fHistRCPtExLJ[fInCentralitySelection]->Fill(pt);
1988            fHistDeltaPtDeltaPhi2ExLJ[fInCentralitySelection]->Fill(PhaseShift(phi-psi2, 2.), pt - areaRC*fLocalRho->GetLocalVal(phi, GetJetContainer()->GetJetRadius(), fLocalRho->GetVal()));
1989            fHistDeltaPtDeltaPhi2ExLJRho0[fInCentralitySelection]->Fill(PhaseShift(phi-psi2, 2.), pt - areaRC*fLocalRho->GetVal());
1990        }
1991     } 
1992 }
1993 //_____________________________________________________________________________
1994 void AliAnalysisTaskJetV2::FillJetHistograms(Double_t psi2)
1995 {
1996     // fill jet histograms
1997     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
1998     Int_t iJets(fJets->GetEntriesFast());
1999     for(Int_t i(0); i < iJets; i++) {
2000         AliEmcalJet* jet = static_cast<AliEmcalJet*>(fJets->At(i));
2001         if(PassesCuts(jet)) {
2002             Double_t pt(jet->Pt()), area(jet->Area()), eta(jet->Eta()), phi(jet->Phi());
2003             Double_t rho(fLocalRho->GetLocalVal(phi, GetJetContainer()->GetJetRadius(), fLocalRho->GetVal()));
2004             fHistJetPtRaw[fInCentralitySelection]->Fill(pt);
2005             fHistJetPt[fInCentralitySelection]->Fill(pt-area*rho);
2006             if(fFillQAHistograms) fHistJetEtaPhi[fInCentralitySelection]->Fill(eta, phi);
2007             fHistJetPtArea[fInCentralitySelection]->Fill(pt-area*rho, area);
2008             fHistJetPtEta[fInCentralitySelection]->Fill(pt-area*rho, eta);
2009             fHistJetPsi2Pt[fInCentralitySelection]->Fill(PhaseShift(phi-psi2, 2.), pt-area*rho);
2010             fHistJetPsi2PtRho0[fInCentralitySelection]->Fill(PhaseShift(phi-psi2, 2.), pt-area*fLocalRho->GetVal());
2011             fHistJetPtConstituents[fInCentralitySelection]->Fill(pt-area*rho, jet->GetNumberOfConstituents());
2012             fHistJetEtaRho[fInCentralitySelection]->Fill(eta, pt/area);
2013         } 
2014     }
2015 }
2016 //_____________________________________________________________________________
2017 void AliAnalysisTaskJetV2::FillQAHistograms(AliVTrack* vtrack) const
2018 {
2019     // fill qa histograms for pico tracks
2020     if(fDebug > 1) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
2021     if(!vtrack) return;
2022     AliPicoTrack* track = static_cast<AliPicoTrack*>(vtrack);
2023     fHistRunnumbersPhi->Fill(fMappedRunNumber, track->Phi());
2024     fHistRunnumbersEta->Fill(fMappedRunNumber, track->Eta());
2025     Int_t type((int)(track->GetTrackType()));
2026     switch (type) {
2027         case 0:
2028            fHistPicoCat1[fInCentralitySelection]->Fill(track->Eta(), track->Phi()); 
2029            break;
2030         case 1:
2031            fHistPicoCat2[fInCentralitySelection]->Fill(track->Eta(), track->Phi()); 
2032            break;
2033         case 2:
2034            fHistPicoCat3[fInCentralitySelection]->Fill(track->Eta(), track->Phi()); 
2035            break;
2036         default: break;
2037     }
2038 }
2039 //_____________________________________________________________________________
2040 void AliAnalysisTaskJetV2::FillQAHistograms(AliVEvent* vevent) 
2041 {
2042     // fill qa histograms for events
2043     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
2044     if(!vevent) return;
2045     fHistVertexz->Fill(vevent->GetPrimaryVertex()->GetZ());
2046     fHistCentrality->Fill(fCent);
2047     Int_t runNumber(InputEvent()->GetRunNumber());
2048     if(fLeadingJet && fLeadingJetAfterSub) fHistLeadingJetBackground[fInCentralitySelection]->Fill(TMath::Abs(fLeadingJet->Eta()-fLeadingJetAfterSub->Eta()), PhaseShift(fLeadingJet->Phi()-fLeadingJetAfterSub->Phi()));
2049     for(fMappedRunNumber = 0; fMappedRunNumber < fExpectedRuns->GetSize(); fMappedRunNumber++) {
2050         if(fExpectedRuns->At(fMappedRunNumber) == runNumber) return;
2051     }
2052     if(fDebug > 0) printf("\n > TASK %s CANNOT IDENTIFY RUN - CONFIGURATION COULD BE INCORRECT < \n", GetName());
2053 }
2054 //_____________________________________________________________________________
2055 void AliAnalysisTaskJetV2::FillWeightedTrackHistograms() const
2056 {
2057     // fill track histograms
2058     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
2059     Int_t iTracks(fTracks->GetEntriesFast()), iAcceptedTracks(0);
2060     for(Int_t i(0); i < iTracks; i++) {
2061         AliVTrack* track = static_cast<AliVTrack*>(fTracks->At(i));
2062         if(!PassesCuts(track)) continue;
2063         iAcceptedTracks++;
2064         fHistPicoTrackPt[fInCentralitySelection]->Fill(track->Pt(), fEventPlaneWeight);
2065         if(fFillQAHistograms) FillQAHistograms(track);
2066     }
2067     fHistPicoTrackMult[fInCentralitySelection]->Fill(iAcceptedTracks, fEventPlaneWeight);
2068 }
2069 //_____________________________________________________________________________
2070 void AliAnalysisTaskJetV2::FillWeightedClusterHistograms() const
2071 {
2072     // fill cluster histograms
2073     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
2074     if(!fClusterCont) return;
2075     Int_t iClusters(fClusterCont->GetNClusters());
2076     TLorentzVector clusterLorentzVector;
2077     for(Int_t i(0); i < iClusters; i++) {
2078         AliVCluster* cluster = fClusterCont->GetCluster(i);
2079         if (!PassesCuts(cluster)) continue;
2080         cluster->GetMomentum(clusterLorentzVector, const_cast<Double_t*>(fVertex));
2081         fHistClusterPt[fInCentralitySelection]->Fill(clusterLorentzVector.Pt(), fEventPlaneWeight);
2082         fHistClusterEtaPhi[fInCentralitySelection]->Fill(clusterLorentzVector.Eta(), clusterLorentzVector.Phi(), fEventPlaneWeight);
2083         fHistClusterEtaPhiWeighted[fInCentralitySelection]->Fill(clusterLorentzVector.Eta(), clusterLorentzVector.Phi(), clusterLorentzVector.Pt()*fEventPlaneWeight);
2084     }
2085     return;
2086 }
2087 //_____________________________________________________________________________
2088 void AliAnalysisTaskJetV2::FillWeightedEventPlaneHistograms(Double_t vzero[2][2], Double_t* vzeroComb, Double_t* tpc) const
2089 {
2090     // fill event plane histograms, only called in qa mode
2091     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
2092     fHistPsiControl->Fill(0.5, vzero[0][0], fEventPlaneWeight);    // vzero a psi2
2093     fHistPsiControl->Fill(1.5, vzero[1][0], fEventPlaneWeight);    // vzero c psi2
2094     fHistPsiControl->Fill(2.5, tpc[0], fEventPlaneWeight);         // tpc psi 2
2095     fHistPsiControl->Fill(5.5, vzero[0][1], fEventPlaneWeight);    // vzero a psi3
2096     fHistPsiControl->Fill(6.5, vzero[1][1], fEventPlaneWeight);    // vzero b psi3
2097     fHistPsiControl->Fill(7.5, tpc[1], fEventPlaneWeight);         // tpc psi 3
2098     fHistPsiVZEROA->Fill(vzero[0][0], fEventPlaneWeight);
2099     fHistPsiVZEROC->Fill(vzero[1][0], fEventPlaneWeight);
2100     fHistPsiVZERO->Fill(vzeroComb[0], fEventPlaneWeight);
2101     fHistPsiTPC->Fill(tpc[0], fEventPlaneWeight);
2102     fHistPsiSpread->Fill(0.5, TMath::Abs(vzero[0][0]-vzero[1][0]), fEventPlaneWeight);
2103     fHistPsiSpread->Fill(1.5, TMath::Abs(vzero[0][0]-tpc[0]), fEventPlaneWeight);
2104     fHistPsiSpread->Fill(2.5, TMath::Abs(vzero[1][0]-tpc[0]), fEventPlaneWeight);
2105     // event plane vs centrality QA histo's to check recentering
2106     Double_t TRK(InputEvent()->GetCentrality()->GetCentralityPercentile("TRK"));
2107     Double_t V0M(InputEvent()->GetCentrality()->GetCentralityPercentile("V0M"));
2108     fHistPsiVZEROAV0M->Fill(V0M, vzero[0][0], fEventPlaneWeight);
2109     fHistPsiVZEROCV0M->Fill(V0M, vzero[1][0], fEventPlaneWeight);
2110     fHistPsiVZEROVV0M->Fill(V0M, vzeroComb[0], fEventPlaneWeight);
2111     fHistPsiTPCV0M->Fill(V0M, tpc[0], fEventPlaneWeight);
2112     fHistPsiVZEROATRK->Fill(TRK, vzero[0][0], fEventPlaneWeight);
2113     fHistPsiVZEROCTRK->Fill(TRK, vzero[1][0], fEventPlaneWeight);
2114     fHistPsiVZEROTRK->Fill(TRK, vzeroComb[0], fEventPlaneWeight);
2115     fHistPsiTPCTRK->Fill(TRK, tpc[0], fEventPlaneWeight);
2116     // leading jet vs event plane bias
2117     if(fLeadingJet) {
2118         Double_t rho(fLocalRho->GetLocalVal(fLeadingJet->Phi(), GetJetContainer()->GetJetRadius(), fLocalRho->GetVal()));
2119         Double_t pt(fLeadingJet->Pt() - fLeadingJet->Area()*rho);
2120         fHistPsiTPCLeadingJet[fInCentralitySelection]->Fill(pt, tpc[0], fLeadingJet->Phi(), fEventPlaneWeight);
2121         fHistPsiVZEROALeadingJet[fInCentralitySelection]->Fill(pt, vzero[0][0], fLeadingJet->Phi(), fEventPlaneWeight);
2122         fHistPsiVZEROCLeadingJet[fInCentralitySelection]->Fill(pt, vzero[1][0], fLeadingJet->Phi(), fEventPlaneWeight);
2123         fHistPsiVZEROCombLeadingJet[fInCentralitySelection]->Fill(pt, vzeroComb[0], fLeadingJet->Phi(), fEventPlaneWeight);
2124     }
2125     // correlation of event planes
2126     fHistPsi2Correlation[fInCentralitySelection]->Fill(tpc[0], vzero[0][0], vzero[1][0], fEventPlaneWeight);
2127 }
2128 //_____________________________________________________________________________
2129 void AliAnalysisTaskJetV2::FillWeightedRhoHistograms()
2130 {
2131     // fill rho histograms
2132     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
2133     fHistRhoPackage[fInCentralitySelection]->Fill(fLocalRho->GetVal(), fEventPlaneWeight);    // save the rho estimate from the emcal jet package
2134     // get multiplicity FIXME inefficient
2135     Int_t iJets(fJets->GetEntriesFast());
2136     Double_t rho(fLocalRho->GetLocalVal(TMath::Pi(), TMath::Pi(), fLocalRho->GetVal()));
2137     fHistRho[fInCentralitySelection]->Fill(rho, fEventPlaneWeight);
2138     fHistRhoVsMult->Fill(fTracks->GetEntries(), rho, fEventPlaneWeight);
2139     fHistRhoVsCent->Fill(fCent, rho, fEventPlaneWeight);
2140     for(Int_t i(0); i < iJets; i++) {
2141         AliEmcalJet* jet = static_cast<AliEmcalJet*>(fJets->At(i));
2142         if(!PassesCuts(jet)) continue;
2143         fHistRhoAVsMult->Fill(fTracks->GetEntries(), rho * jet->Area(), fEventPlaneWeight);
2144         fHistRhoAVsCent->Fill(fCent, rho * jet->Area(), fEventPlaneWeight);
2145     }
2146 }
2147 //_____________________________________________________________________________
2148 void AliAnalysisTaskJetV2::FillWeightedDeltaPtHistograms(Double_t psi2) const
2149 {
2150     // fill delta pt histograms
2151     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
2152     Int_t i(0);
2153     const Float_t areaRC = GetJetRadius()*GetJetRadius()*TMath::Pi();
2154     // we're retrieved the leading jet, now get a random cone
2155     for(i = 0; i < fMaxCones; i++) {
2156        Float_t pt(0), eta(0), phi(0);
2157        // get a random cone without constraints on leading jet position
2158        CalculateRandomCone(pt, eta, phi, fTracksCont, fClusterCont, 0x0);
2159        if(pt > 0) {
2160            if(fFillQAHistograms) fHistRCPhiEta[fInCentralitySelection]->Fill(phi, eta, fEventPlaneWeight);
2161            fHistRhoVsRCPt[fInCentralitySelection]->Fill(pt, fLocalRho->GetLocalVal(phi, GetJetContainer()->GetJetRadius(), fLocalRho->GetVal())*areaRC, fEventPlaneWeight);
2162            fHistRCPt[fInCentralitySelection]->Fill(pt, fEventPlaneWeight);
2163            fHistDeltaPtDeltaPhi2[fInCentralitySelection]->Fill(PhaseShift(phi-psi2, 2.), pt - areaRC*fLocalRho->GetLocalVal(phi, GetJetContainer()->GetJetRadius(), fLocalRho->GetVal()), fEventPlaneWeight);
2164            fHistDeltaPtDeltaPhi2Rho0[fInCentralitySelection]->Fill(PhaseShift(phi-psi2, 2.), pt - areaRC*fLocalRho->GetVal(), fEventPlaneWeight);
2165
2166        }
2167        // get a random cone excluding leading jet area
2168        CalculateRandomCone(pt, eta, phi, fTracksCont, fClusterCont, fLeadingJet);
2169        if(pt > 0) {
2170            if(fFillQAHistograms) fHistRCPhiEtaExLJ[fInCentralitySelection]->Fill(phi, eta, fEventPlaneWeight);
2171            fHistRhoVsRCPtExLJ[fInCentralitySelection]->Fill(pt, fLocalRho->GetLocalVal(phi, GetJetContainer()->GetJetRadius(), fLocalRho->GetVal())*areaRC, fEventPlaneWeight);
2172            fHistRCPtExLJ[fInCentralitySelection]->Fill(pt, fEventPlaneWeight);
2173            fHistDeltaPtDeltaPhi2ExLJ[fInCentralitySelection]->Fill(PhaseShift(phi-psi2, 2.), pt - areaRC*fLocalRho->GetLocalVal(phi, GetJetContainer()->GetJetRadius(), fLocalRho->GetVal()), fEventPlaneWeight);
2174            fHistDeltaPtDeltaPhi2ExLJRho0[fInCentralitySelection]->Fill(PhaseShift(phi-psi2, 2.), pt - areaRC*fLocalRho->GetVal(), fEventPlaneWeight);
2175        }
2176     } 
2177 }
2178 //_____________________________________________________________________________
2179 void AliAnalysisTaskJetV2::FillWeightedJetHistograms(Double_t psi2)
2180 {
2181     // fill jet histograms
2182     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
2183     Int_t iJets(fJets->GetEntriesFast());
2184     for(Int_t i(0); i < iJets; i++) {
2185         AliEmcalJet* jet = static_cast<AliEmcalJet*>(fJets->At(i));
2186         if(PassesCuts(jet)) {
2187             Double_t pt(jet->Pt()), area(jet->Area()), eta(jet->Eta()), phi(jet->Phi());
2188             Double_t rho(fLocalRho->GetLocalVal(phi, GetJetContainer()->GetJetRadius(), fLocalRho->GetVal()));
2189             fHistJetPtRaw[fInCentralitySelection]->Fill(pt, fEventPlaneWeight);
2190             fHistJetPt[fInCentralitySelection]->Fill(pt-area*rho, fEventPlaneWeight);
2191             if(fFillQAHistograms) fHistJetEtaPhi[fInCentralitySelection]->Fill(eta, phi, fEventPlaneWeight);
2192             fHistJetPtArea[fInCentralitySelection]->Fill(pt-area*rho, area, fEventPlaneWeight);
2193             fHistJetPtEta[fInCentralitySelection]->Fill(pt-area*rho, eta, fEventPlaneWeight);
2194             fHistJetPsi2Pt[fInCentralitySelection]->Fill(PhaseShift(phi-psi2, 2.), pt-area*rho, fEventPlaneWeight);
2195             fHistJetPsi2PtRho0[fInCentralitySelection]->Fill(PhaseShift(phi-psi2, 2.), pt-area*fLocalRho->GetVal(), fEventPlaneWeight);
2196             fHistJetPtConstituents[fInCentralitySelection]->Fill(pt-area*rho, jet->GetNumberOfConstituents(), fEventPlaneWeight);
2197             fHistJetEtaRho[fInCentralitySelection]->Fill(eta, pt/area, fEventPlaneWeight);
2198         } 
2199     }
2200 }
2201 //_____________________________________________________________________________
2202 void AliAnalysisTaskJetV2::FillWeightedQAHistograms(AliVTrack* vtrack) const
2203 {
2204     // fill qa histograms for pico tracks
2205     if(fDebug > 1) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
2206     if(!vtrack) return;
2207     AliPicoTrack* track = static_cast<AliPicoTrack*>(vtrack);
2208     fHistRunnumbersPhi->Fill(fMappedRunNumber, track->Phi(), fEventPlaneWeight);
2209     fHistRunnumbersEta->Fill(fMappedRunNumber, track->Eta(), fEventPlaneWeight);
2210     Int_t type((int)(track->GetTrackType()));
2211     switch (type) {
2212         case 0:
2213            fHistPicoCat1[fInCentralitySelection]->Fill(track->Eta(), track->Phi(), fEventPlaneWeight); 
2214            break;
2215         case 1:
2216            fHistPicoCat2[fInCentralitySelection]->Fill(track->Eta(), track->Phi(), fEventPlaneWeight); 
2217            break;
2218         case 2:
2219            fHistPicoCat3[fInCentralitySelection]->Fill(track->Eta(), track->Phi(), fEventPlaneWeight); 
2220            break;
2221         default: break;
2222     }
2223 }
2224 //_____________________________________________________________________________
2225 void AliAnalysisTaskJetV2::FillWeightedQAHistograms(AliVEvent* vevent) 
2226 {
2227     // fill qa histograms for events
2228     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
2229     if(!vevent) return;
2230     fHistVertexz->Fill(vevent->GetPrimaryVertex()->GetZ());
2231     fHistCentrality->Fill(fCent);
2232     Int_t runNumber(InputEvent()->GetRunNumber());
2233     if(fLeadingJet && fLeadingJetAfterSub) fHistLeadingJetBackground[fInCentralitySelection]->Fill(TMath::Abs(fLeadingJet->Eta()-fLeadingJetAfterSub->Eta()), PhaseShift(fLeadingJet->Phi()-fLeadingJetAfterSub->Phi()), fEventPlaneWeight);
2234     for(fMappedRunNumber = 0; fMappedRunNumber < fExpectedRuns->GetSize(); fMappedRunNumber++) {
2235         if(fExpectedRuns->At(fMappedRunNumber) == runNumber) return;
2236     }
2237     if(fDebug > 0) printf("\n > TASK %s CANNOT IDENTIFY RUN - CONFIGURATION COULD BE INCORRECT < \n", GetName());
2238 }
2239 //_____________________________________________________________________________
2240 void AliAnalysisTaskJetV2::FillAnalysisSummaryHistogram() const
2241 {
2242     // fill the analysis summary histrogram, saves all relevant analysis settigns
2243     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
2244     fHistAnalysisSummary->GetXaxis()->SetBinLabel(2, "fJetRadius");
2245     fHistAnalysisSummary->SetBinContent(2, GetJetContainer()->GetJetRadius());
2246     fHistAnalysisSummary->GetXaxis()->SetBinLabel(3, "fJetEtaMin");
2247     fHistAnalysisSummary->SetBinContent(3, GetJetContainer()->GetJetEtaMin());
2248     fHistAnalysisSummary->GetXaxis()->SetBinLabel(4, "fJetEtaMax");
2249     fHistAnalysisSummary->SetBinContent(4, GetJetContainer()->GetJetEtaMax());
2250     fHistAnalysisSummary->GetXaxis()->SetBinLabel(5, "fJetPhiMin");
2251     fHistAnalysisSummary->SetBinContent(5, GetJetContainer()->GetJetPhiMin());
2252     fHistAnalysisSummary->GetXaxis()->SetBinLabel(6, "fJetPhiMax");
2253     fHistAnalysisSummary->SetBinContent(6, GetJetContainer()->GetJetPhiMin());
2254     fHistAnalysisSummary->GetXaxis()->SetBinLabel(16, "fForceBeamType");
2255     fHistAnalysisSummary->SetBinContent(16, fForceBeamType);
2256     fHistAnalysisSummary->GetXaxis()->SetBinLabel(17, "fMinCent");
2257     fHistAnalysisSummary->SetBinContent(17, fMinCent);
2258     fHistAnalysisSummary->GetXaxis()->SetBinLabel(18, "fMaxCent");
2259     fHistAnalysisSummary->SetBinContent(18, fMaxCent);
2260     fHistAnalysisSummary->GetXaxis()->SetBinLabel(19, "fMinVz");
2261     fHistAnalysisSummary->SetBinContent(19, fMinVz);
2262     fHistAnalysisSummary->GetXaxis()->SetBinLabel(20, "fMaxVz");
2263     fHistAnalysisSummary->SetBinContent(20, fMaxVz);
2264     fHistAnalysisSummary->GetXaxis()->SetBinLabel(21, "fOffTrigger");
2265     fHistAnalysisSummary->SetBinContent(21, fOffTrigger);
2266     fHistAnalysisSummary->GetXaxis()->SetBinLabel(34, "fitModulationType");
2267     fHistAnalysisSummary->SetBinContent(34, (int)fFitModulationType);
2268     fHistAnalysisSummary->GetXaxis()->SetBinLabel(35, "runModeType");
2269     fHistAnalysisSummary->SetBinContent(35, (int)fRunModeType);
2270     fHistAnalysisSummary->GetXaxis()->SetBinLabel(36, "data type");
2271     fHistAnalysisSummary->SetBinContent(36, (int)fDataType);
2272     fHistAnalysisSummary->GetXaxis()->SetBinLabel(37, "iterator");
2273     fHistAnalysisSummary->SetBinContent(37, 1.);
2274     fHistAnalysisSummary->GetXaxis()->SetBinLabel(38, "fMinPvalue");
2275     fHistAnalysisSummary->SetBinContent(38, fMinPvalue);
2276     fHistAnalysisSummary->GetXaxis()->SetBinLabel(39, "fMaxPvalue");
2277     fHistAnalysisSummary->SetBinContent(39, fMaxPvalue);
2278     fHistAnalysisSummary->GetXaxis()->SetBinLabel(40, "fExcludeLeadingJetsFromFit");
2279     fHistAnalysisSummary->SetBinContent(40, fExcludeLeadingJetsFromFit);
2280     fHistAnalysisSummary->GetXaxis()->SetBinLabel(41, "fRebinSwapHistoOnTheFly");
2281     fHistAnalysisSummary->SetBinContent(41, (int)fRebinSwapHistoOnTheFly);
2282     fHistAnalysisSummary->GetXaxis()->SetBinLabel(42, "fUsePtWeight");
2283     fHistAnalysisSummary->SetBinContent(42, (int)fUsePtWeight);
2284     fHistAnalysisSummary->GetXaxis()->SetBinLabel(44, "fSoftTrackMinPt");
2285     fHistAnalysisSummary->SetBinContent(44, fSoftTrackMinPt);
2286     fHistAnalysisSummary->GetXaxis()->SetBinLabel(45, "fSoftTrackMaxPt");
2287     fHistAnalysisSummary->SetBinContent(45, fSoftTrackMaxPt);
2288     fHistAnalysisSummary->GetXaxis()->SetBinLabel(46, "fMaxCones");
2289     fHistAnalysisSummary->SetBinContent(46, fMaxCones);
2290     fHistAnalysisSummary->GetXaxis()->SetBinLabel(47, "used rho");
2291     fHistAnalysisSummary->GetXaxis()->SetBinLabel(48, "used small rho");
2292 }
2293 //_____________________________________________________________________________
2294 void AliAnalysisTaskJetV2::Terminate(Option_t *)
2295 {
2296     // terminate
2297     switch (fRunModeType) {
2298         case kLocal : {
2299         if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
2300         AliAnalysisTaskJetV2::Dump();
2301         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));
2302         } break;
2303         default : break;
2304     }
2305 }
2306 //_____________________________________________________________________________
2307 void AliAnalysisTaskJetV2::SetModulationFit(TF1* fit) 
2308 {
2309     // set modulation fit
2310     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
2311     if (fFitModulation) delete fFitModulation;
2312     fFitModulation = fit; 
2313 }
2314 //_____________________________________________________________________________
2315 void AliAnalysisTaskJetV2::SetUseControlFit(Bool_t c)
2316 {
2317     // set control fit
2318     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
2319     if (fFitControl) delete fFitControl;
2320     if (c) {
2321         fFitControl = new TF1("controlFit", "pol0", 0, TMath::TwoPi());
2322     } else fFitControl = 0x0;
2323 }
2324 //_____________________________________________________________________________
2325 TH1F* AliAnalysisTaskJetV2::GetResolutionFromOuptutFile(detectorType det, Int_t h, TArrayD* cen)
2326 {
2327     // INTERFACE METHOD FOR OUTPUTFILE
2328     // get the detector resolution, user has ownership of the returned histogram
2329     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
2330     if(!fOutputList) {
2331         printf(" > Please add fOutputList first < \n");
2332         return 0x0;
2333     }
2334     TH1F* r(0x0);
2335     (cen) ? r = new TH1F("R", "R", cen->GetSize()-1, cen->GetArray()) : r = new TH1F("R", "R", 10, 0, 10);
2336     if(!cen) r->GetXaxis()->SetTitle("number of centrality bin");
2337     r->GetYaxis()->SetTitle(Form("Resolution #Psi_{%i}", h));
2338     for(Int_t i(0); i < 10; i++) {
2339         TProfile* temp((TProfile*)fOutputList->FindObject(Form("fProfV%iResolution_%i", h, i)));
2340         if(!temp) break;
2341         Double_t a(temp->GetBinContent(3)), b(temp->GetBinContent(5)), c(temp->GetBinContent(7));
2342         Double_t d(temp->GetBinContent(9)), e(temp->GetBinContent(10)), f(temp->GetBinContent(11));
2343         Double_t _a(temp->GetBinError(3)), _b(temp->GetBinError(5)), _c(temp->GetBinError(7));
2344         Double_t _d(temp->GetBinError(9)), _e(temp->GetBinError(10)), _f(temp->GetBinError(11));
2345         if(a <= 0 || b <= 0 || c <= 0 || d <= 0 || e <= 0 || f <= 0) continue;
2346         switch (det) {
2347             case kVZEROA : {
2348                 r->SetBinContent(1+i, TMath::Sqrt((a*b)/c));
2349                 if(i==0) r->SetNameTitle("VZEROA resolution", "VZEROA resolution");
2350                 r->SetBinError(1+i, TMath::Sqrt(_a*_a+_b*_b+_c*_c));
2351             } break;
2352             case kVZEROC : {
2353                 r->SetBinContent(1+i, TMath::Sqrt((a*c)/b));
2354                 if(i==0) r->SetNameTitle("VZEROC resolution", "VZEROC resolution");
2355                 r->SetBinError(1+i, TMath::Sqrt(_a*_a+_b*_b+_c*_c));
2356             } break;
2357             case kTPC : {
2358                 r->SetBinContent(1+i, TMath::Sqrt((b*c)/a));
2359                 if(i==0) r->SetNameTitle("TPC resolution", "TPC resolution");
2360                 r->SetBinError(1+i, TMath::Sqrt(_a*_a+_b*_b+_c*_c));
2361             } break;
2362             case kVZEROComb : {
2363                 r->SetBinContent(1+i, TMath::Sqrt((d*e)/f));
2364                 if(i==0) r->SetNameTitle("VZEROComb resolution", "VZEROComb resolution");
2365                 r->SetBinError(1+i, TMath::Sqrt(_d*_d+_e*_e+_f*_f));
2366             } break;
2367             default : break;
2368         }
2369     }
2370     return r;
2371 }
2372 //_____________________________________________________________________________
2373 TH1F* AliAnalysisTaskJetV2::CorrectForResolutionDiff(TH1F* v, detectorType det, TArrayD* cen, Int_t c, Int_t h)
2374 {
2375     // INTERFACE METHOD FOR OUTPUT FILE
2376     // correct the supplied differential vn histogram v for detector resolution
2377     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
2378     TH1F* r(GetResolutionFromOuptutFile(det, h, cen));
2379     if(!r) {
2380         printf(" > Couldn't find resolution < \n");
2381         return 0x0;
2382     }
2383     Double_t res(1./r->GetBinContent(1+r->FindBin(c)));
2384     TF1* line = new TF1("line", "pol0", 0, 200);
2385     line->SetParameter(0, res);
2386     v->Multiply(line);
2387     return v;
2388 }
2389 //_____________________________________________________________________________
2390 TH1F* AliAnalysisTaskJetV2::CorrectForResolutionInt(TH1F* v, detectorType det, TArrayD* cen, Int_t h)
2391 {
2392     // INTERFACE METHOD FOR OUTPUT FILE
2393     // correct the supplied intetrated vn histogram v for detector resolution
2394     // integrated vn must have the same centrality binning as the resolotion correction
2395     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
2396     TH1F* r(GetResolutionFromOuptutFile(det, h, cen));
2397     v->Divide(v, r);
2398     return v;
2399 }
2400 //_____________________________________________________________________________
2401 TH1F* AliAnalysisTaskJetV2::GetDifferentialQC(TProfile* refCumulants, TProfile* diffCumlants, TArrayD* ptBins, Int_t h)
2402 {
2403     // get differential QC
2404     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
2405     Double_t r(refCumulants->GetBinContent(h-1)); // v2 reference flow
2406     if(r > 0) r = TMath::Sqrt(r);
2407     TH1F* qc = new TH1F(Form("QC2v%i", h), Form("QC2v%i", h), ptBins->GetSize()-1, ptBins->GetArray());
2408     Double_t a(0), b(0), c(0);  // dummy variables
2409     for(Int_t i(0); i < ptBins->GetSize(); i++) {
2410         if(r > 0) {
2411             a = diffCumlants->GetBinContent(1+i);
2412             b = diffCumlants->GetBinError(1+i);
2413             c = a/r;
2414             qc->SetBinContent(1+i, c);
2415             (a <= 0 || b <= 0) ? qc->SetBinError(1+i, b) : qc->SetBinError(1+i, TMath::Sqrt(c*c*b*b/(a*a)));
2416         }
2417     }
2418     return qc;
2419 }
2420 //_____________________________________________________________________________
2421 void AliAnalysisTaskJetV2::ReadVZEROCalibration2010h()
2422 {
2423     // necessary for calibration of 10h vzero event plane. code copied from flow package 
2424     // (duplicate, but i didn't want to introduce an ulgy dependency )
2425     // this function is only called when the runnumber changes 
2426     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
2427
2428     // 1) check if the proper chi weights for merging vzero a and vzero c ep are present
2429     // if not, use sane defaults. centrality binning is equal to that given in the fVZEROcentralityBin snippet
2430     //
2431     // chi values can be calculated using the static helper function 
2432     // AliAnalysisTaskJetV2::CalculateEventPlaneChi(Double_t res) where res is the event plane
2433     // resolution in a given centrality bin
2434     //
2435     // the resolutions that were used for these defaults are
2436     // this might need a bit of updating as they were read 'by-eye' from a performance plot ..
2437     // Double_t R2VZEROA[] = {.35, .40, .48, .50, .48, .45, .38, .26, .16};
2438     // Double_t R2VZEROC[] = {.45, .60, .70, .73, .68, .60, .40, .36, .17};
2439     // Double_t R3VZEROA[] = {.22, .23, .22, .19, .15, .12, .08, .00, .00};
2440     // Double_t R3VZEROC[] = {.30, .30, .28, .25, .22, .17, .11, .00, .00};
2441
2442     Double_t chiC2[] = {0.771423, 1.10236, 1.38116, 1.48077, 1.31964, 1.10236, 0.674622, 0.600403, 0.273865};
2443     Double_t chiA2[] = {0.582214, 0.674622, 0.832214, 0.873962, 0.832214, 0.771423, 0.637146, 0.424255, 0.257385};
2444     Double_t chiC3[] = {0.493347, 0.493347, 0.458557, 0.407166, 0.356628, 0.273865, 0.176208, 6.10352e-05, 6.10352e-05};
2445     Double_t chiA3[] = {0.356628, 0.373474, 0.356628, 0.306702, 0.24115, 0.192322, 0.127869, 6.10352e-05, 6.10352e-05};
2446
2447     if(!fChi2A) fChi2A = new TArrayD(9, chiA2);
2448     if(!fChi2C) fChi2C = new TArrayD(9, chiC2);
2449     if(!fChi3A) fChi3A = new TArrayD(9, chiA3);
2450     if(!fChi3C) fChi3C = new TArrayD(9, chiC3);
2451
2452     // 2) open database file
2453     fOADB = TFile::Open("$ALICE_ROOT/OADB/PWGCF/VZERO/VZEROcalibEP.root");
2454     if(fOADB->IsZombie()){
2455         printf("OADB file $ALICE_ROOT/OADB/PWGCF/VZERO/VZEROcalibEP.root cannot be opened, CALIBRATION FAILED !");
2456         return;
2457     }
2458
2459     AliOADBContainer *cont = (AliOADBContainer*) fOADB->Get("hMultV0BefCorr");
2460     if(!cont){
2461         // see if database is readable
2462         printf("OADB object hMultV0BefCorr is not available in the file\n");
2463         return; 
2464     }
2465     Int_t run(fRunNumber);
2466     if(!(cont->GetObject(run))){
2467         // if the run isn't recognized fall back to a default run
2468         printf("OADB object hMultVZEROBefCorr is not available for run %i (used default run 137366)\n",run);
2469         run = 137366;
2470     }
2471     // step 3) get the proper multiplicity weights from the vzero signal
2472     fVZEROgainEqualization = ((TH2F*)cont->GetObject(run))->ProfileX();
2473     if(!fVZEROgainEqualization) {
2474         AliFatal(Form("%s: Fatal error, couldn't read fVZEROgainEqualization from OADB object < \n", GetName()));
2475         return;
2476     }
2477
2478     TF1* fpol0 = new TF1("fpol0","pol0"); 
2479     if(fVZEROgainEqualizationPerRing) {
2480         // do the calibration per ring
2481         // start with the vzero c rings (segments 0 through 31)
2482         fVZEROgainEqualization->Fit(fpol0, "", "", 0, 8);
2483         (fUseVZERORing[0]) ? SetVZEROCpol(0, fpol0->GetParameter(0)) : SetVZEROCpol(0, 0.);
2484         fVZEROgainEqualization->Fit(fpol0, "", "", 8, 16);
2485         (fUseVZERORing[1]) ? SetVZEROCpol(1, fpol0->GetParameter(0)) : SetVZEROCpol(1, 0.);
2486         fVZEROgainEqualization->Fit(fpol0, "", "", 16, 24);
2487         (fUseVZERORing[2]) ? SetVZEROCpol(2, fpol0->GetParameter(0)) : SetVZEROCpol(2, 0.);
2488         fVZEROgainEqualization->Fit(fpol0, "", "", 24, 32);
2489         (fUseVZERORing[3]) ? SetVZEROCpol(3, fpol0->GetParameter(0)) : SetVZEROCpol(3, 0.);
2490         // same thing for vero A
2491         fVZEROgainEqualization->Fit(fpol0, "", "", 32, 40);
2492         (fUseVZERORing[4]) ? SetVZEROApol(0, fpol0->GetParameter(0)) : SetVZEROApol(0, 0.);
2493         fVZEROgainEqualization->Fit(fpol0, "", "", 40, 48);
2494         (fUseVZERORing[5]) ? SetVZEROApol(1, fpol0->GetParameter(0)) : SetVZEROApol(1, 0.);
2495         fVZEROgainEqualization->Fit(fpol0, "", "", 48, 56);
2496         (fUseVZERORing[6]) ? SetVZEROApol(2, fpol0->GetParameter(0)) : SetVZEROApol(2, 0.);
2497         fVZEROgainEqualization->Fit(fpol0, "", "", 56, 64);
2498         (fUseVZERORing[7]) ? SetVZEROApol(3, fpol0->GetParameter(0)) : SetVZEROApol(3, 0.);
2499     } else {
2500         // do the calibration in one go. the calibration will still be 
2501         // stored per ring, but each ring has the same weight now
2502         // this should be the default for the analysis as the database is tuned to this configuration
2503        fVZEROgainEqualization->Fit(fpol0,"","",0,31);
2504        for(Int_t i(0); i < 4; i++) SetVZEROCpol(i, fpol0->GetParameter(0));
2505        fVZEROgainEqualization->Fit(fpol0,"","",32,64);
2506        for(Int_t i(0); i < 4; i++) SetVZEROApol(i, fpol0->GetParameter(0));
2507     }
2508
2509     // step 4) extract the information to re-weight the q-vectors 
2510     for(Int_t iside=0;iside<2;iside++){
2511         for(Int_t icoord=0;icoord<2;icoord++){
2512             for(Int_t i=0;i  < 9;i++){
2513                 char namecont[100];
2514                 if(iside==0 && icoord==0)
2515                   snprintf(namecont,100,"hQxc2_%i",i);
2516                 else if(iside==1 && icoord==0)
2517                   snprintf(namecont,100,"hQxa2_%i",i);
2518                 else if(iside==0 && icoord==1)
2519                   snprintf(namecont,100,"hQyc2_%i",i);
2520                 else if(iside==1 && icoord==1)
2521                   snprintf(namecont,100,"hQya2_%i",i);
2522
2523                 cont = (AliOADBContainer*) fOADB->Get(namecont);
2524                 if(!cont){
2525                     printf("OADB object %s is not available in the file\n",namecont);
2526                     return;     
2527                 }
2528         
2529                 if(!(cont->GetObject(run))){
2530                     printf("OADB object %s is not available for run %i (used run 137366)\n",namecont,run);
2531                     run = 137366;
2532                 }
2533
2534                 // store info for all centralities to cache
2535                 fMeanQ[i][iside][icoord] = ((TH1F *) cont->GetObject(run))->GetMean();
2536                 fWidthQ[i][iside][icoord] = ((TH1F *) cont->GetObject(run))->GetRMS();
2537
2538                 //for v3
2539                 if(iside==0 && icoord==0)
2540                   snprintf(namecont,100,"hQxc3_%i",i);
2541                 else if(iside==1 && icoord==0)
2542                   snprintf(namecont,100,"hQxa3_%i",i);
2543                 else if(iside==0 && icoord==1)
2544                   snprintf(namecont,100,"hQyc3_%i",i);
2545                 else if(iside==1 && icoord==1)
2546                   snprintf(namecont,100,"hQya3_%i",i);
2547
2548                 cont = (AliOADBContainer*) fOADB->Get(namecont);
2549                 if(!cont){
2550                     printf("OADB object %s is not available in the file\n",namecont);
2551                     return;     
2552                 }
2553                 
2554                 if(!(cont->GetObject(run))){
2555                     printf("OADB object %s is not available for run %i (used run 137366)\n",namecont,run);
2556                     run = 137366;
2557                 }
2558                 // store info for all centralities to cache
2559                 fMeanQv3[i][iside][icoord] = ((TH1F *) cont->GetObject(run))->GetMean();
2560                 fWidthQv3[i][iside][icoord] = ((TH1F *) cont->GetObject(run))->GetRMS();
2561             }
2562         }
2563     }
2564     // cleanup. the opened file is closed in the destructor, otherwise fVZEROgainEqualization is no longer available
2565     delete fpol0;
2566 }
2567 //_____________________________________________________________________________
2568 Int_t AliAnalysisTaskJetV2::GetVZEROCentralityBin() const
2569 {
2570     // return cache index number corresponding to the event centrality
2571     if(fDebug > 0) printf("__FILE__ = %s \n __LINE __ %i , __FUNC__ %s \n ", __FILE__, __LINE__, __func__);
2572     Float_t v0Centr(InputEvent()->GetCentrality()->GetCentralityPercentile("V0M"));
2573     if(v0Centr < 5) return 0;
2574     else if(v0Centr < 10) return 1;
2575     else if(v0Centr < 20) return  2;
2576     else if(v0Centr < 30) return  3;
2577     else if(v0Centr < 40) return  4;
2578     else if(v0Centr < 50) return  5;
2579     else if(v0Centr < 60) return  6;
2580     else if(v0Centr < 70) return  7;
2581     else return 8;
2582 }
2583 //_____________________________________________________________________________
2584 AliEmcalJet* AliAnalysisTaskJetV2::GetLeadingJet(AliLocalRhoParameter* localRho) {
2585     // return pointer to the highest pt jet (before background subtraction) within acceptance
2586     // only rudimentary cuts are applied on this level, hence the implementation outside of
2587     // the framework
2588     Int_t iJets(fJets->GetEntriesFast());
2589     Double_t pt(0);
2590     AliEmcalJet* leadingJet(0x0);
2591     if(!localRho) {
2592         for(Int_t i(0); i < iJets; i++) {
2593             AliEmcalJet* jet = static_cast<AliEmcalJet*>(fJets->At(i));
2594             if(!PassesSimpleCuts(jet)) continue;
2595             if(jet->Pt() > pt) {
2596                leadingJet = jet;
2597                pt = leadingJet->Pt();
2598             }
2599         }
2600         return leadingJet;
2601     } else {
2602         // return leading jet after background subtraction
2603         Double_t rho(0);
2604         for(Int_t i(0); i < iJets; i++) {
2605             AliEmcalJet* jet = static_cast<AliEmcalJet*>(fJets->At(i));
2606             if(!PassesSimpleCuts(jet)) continue;
2607             rho = localRho->GetLocalVal(jet->Phi(), GetJetContainer()->GetJetRadius(), localRho->GetVal());
2608             if((jet->Pt()-jet->Area()*rho) > pt) {
2609                leadingJet = jet;
2610                pt = (leadingJet->Pt()-jet->Area()*rho);
2611             }
2612         }
2613         return leadingJet;
2614
2615     }
2616     return 0x0;
2617 }
2618 //_____________________________________________________________________________