]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGJE/EMCALJetTasks/UserTasks/AliAnalysisTaskChargedJetsPA.cxx
Fix for Root6
[u/mrichter/AliRoot.git] / PWGJE / EMCALJetTasks / UserTasks / AliAnalysisTaskChargedJetsPA.cxx
1 #ifndef ALIANALYSISTASKSE_H
2 #include <Riostream.h>
3 #include <TROOT.h>
4 #include <TFile.h>
5 #include <TChain.h>
6 #include <TTree.h>
7 #include <TKey.h>
8 #include <TProfile.h>
9 #include <TProfile2D.h>
10 #include <TH1F.h>
11 #include <TH2F.h>
12 #include <TCanvas.h>
13 #include <TList.h>
14 #include <TClonesArray.h>
15 #include <TObject.h>
16 #include <TMath.h>
17 #include <TSystem.h>
18 #include <TInterpreter.h>
19 #include <TH1.h>
20 #include "AliAnalysisTask.h"
21 #include "AliCentrality.h"
22 #include "AliStack.h"
23 #include "AliESDEvent.h"
24 #include "AliESDInputHandler.h"
25 #include "AliAODEvent.h"
26 #include "AliAODHandler.h"
27 #include "AliAnalysisManager.h"
28 #include "AliAnalysisTaskSE.h"
29 #endif
30
31 #include <time.h>
32 #include <TRandom3.h>
33 #include "AliGenPythiaEventHeader.h"
34 #include "AliAODMCHeader.h"
35 #include "AliMCEvent.h"
36 #include "AliLog.h"
37 #include <AliEmcalJet.h>
38 #include <AliPicoTrack.h>
39 #include "AliVEventHandler.h"
40 #include "AliVParticle.h"
41 #include "AliAODMCParticle.h"
42 #include "AliAnalysisUtils.h"
43 #include "AliRhoParameter.h"
44 #include "TVector3.h"
45
46 #include "AliAnalysisTaskChargedJetsPA.h"
47 using std::min;
48
49 //TODO: Not accessing the particles when using MC
50 //TODO: FillHistogram can be done better with virtual TH1(?)
51 ClassImp(AliAnalysisTaskChargedJetsPA)
52
53 // ######################################################################################## DEFINE HISTOGRAMS
54 void AliAnalysisTaskChargedJetsPA::Init()
55 {
56   #ifdef DEBUGMODE
57     AliInfo("Creating histograms.");
58   #endif
59
60   TH1D* tmpHisto = AddHistogram1D<TH1D>("hNumberEvents", "Number of events (0 = before cuts, 1 = after cuts)", "", 2, 0, 2, "stage","N^{Events}/cut");
61   tmpHisto->GetXaxis()->SetBinLabel(1, "Before cuts");
62   tmpHisto->GetXaxis()->SetBinLabel(2, "After cuts");
63
64   tmpHisto = AddHistogram1D<TH1D>("hEventAcceptance", "Accepted events (0 = before cuts, 1 = after pile up, 2 = after vertex)", "", 3, 0, 3, "stage","N^{Events}/cut");
65   tmpHisto->GetXaxis()->SetBinLabel(1, "Before cuts");
66   tmpHisto->GetXaxis()->SetBinLabel(2, "After pile up");
67   tmpHisto->GetXaxis()->SetBinLabel(3, "After vertex");
68
69   tmpHisto = AddHistogram1D<TH1D>("hTrackAcceptance", "Accepted tracks (0 = before cuts, 1 = after eta, 2 = after pT)", "", 3, 0, 3, "stage","N^{Tracks}/cut");
70   tmpHisto->GetXaxis()->SetBinLabel(1, "Before cuts");
71   tmpHisto->GetXaxis()->SetBinLabel(2, "After eta");
72   tmpHisto->GetXaxis()->SetBinLabel(3, "After p_{T}");
73
74   tmpHisto = AddHistogram1D<TH1D>("hJetAcceptance", "Accepted jets (0 = before cuts, 1 = after eta, 2 = after pT, 3 = after area)", "", 4, 0, 4, "stage","N^{Jets}/cut");
75   tmpHisto->GetXaxis()->SetBinLabel(1, "Before cuts");
76   tmpHisto->GetXaxis()->SetBinLabel(2, "After eta");
77   tmpHisto->GetXaxis()->SetBinLabel(3, "After p_{T}");
78   tmpHisto->GetXaxis()->SetBinLabel(4, "After area");
79
80   // NOTE: Jet histograms
81   if (fAnalyzeJets)
82   {
83     // ######## Jet spectra
84     AddHistogram1D<TH1D>("hRawJetPt", "Raw jets p_{T} distribution (before cuts)", "", 500, 0., 250., "p_{T} (GeV/c)", "dN^{Jets}/dp_{T}");
85     AddHistogram2D<TH2D>("hJetPt", "Jets p_{T} distribution", "", 500, -50., 200., fNumberOfCentralityBins, 0, 100, "p_{T} (GeV/c)","Centrality","dN^{Jets}/dp_{T}");
86     AddHistogram2D<TH2D>("hJetPtBgrdSubtractedKTImprovedCMS", "Jets p_{T} distribution, KT background (Improved CMS) subtracted", "", 500, -50., 200., fNumberOfCentralityBins, 0, 100, "p_{T} (GeV/c)","Centrality","dN^{Jets}/dp_{T}");    
87     AddHistogram2D<TH2D>("hJetPtBgrdSubtractedPP", "Jets p_{T} distribution, pp background subtracted", "", 500, -50., 200., fNumberOfCentralityBins, 0, 100, "p_{T} (GeV/c)","Centrality","dN^{Jets}/dp_{T}");
88
89
90
91     AddHistogram2D<TProfile2D>("hJetPtSubtractedRhoKTImprovedCMS", "Mean subtracted KT (CMS w/o signal) background from jets", "COLZ", 600, 0, 150, fNumberOfCentralityBins, 0, 100, "Jet p_{T}", "Centrality", "#rho mean");
92     AddHistogram2D<TH2D>("hJetPtSubtractedRhoKTImprovedCMS020", "Mean subtracted KT (CMS w/o signal) background from jets, 0-20", "COLZ", 600, 0, 150, 400,0.,40., "Jet p_{T} (GeV/c)", "#rho (GeV/c)", "dN^{Events}/dp_{T}#rho");
93
94     if(fAnalyzeDeprecatedBackgrounds)
95     {
96       AddHistogram2D<TH2D>("hJetPtBgrdSubtractedTR", "Jets p_{T} distribution, TR background (Cone R=0.6 around jets excluded) subtracted", "", 500, -50., 200., fNumberOfCentralityBins, 0, 100, "p_{T} (GeV/c)","Centrality","dN^{Jets}/dp_{T}");
97       AddHistogram2D<TH2D>("hJetPtBgrdSubtractedKTPbPb", "Jets p_{T} distribution, KT background (PbPb w/o ghosts) subtracted", "", 500, -50., 200., fNumberOfCentralityBins, 0, 100, "p_{T} (GeV/c)","Centrality","dN^{Jets}/dp_{T}");
98       AddHistogram2D<TH2D>("hJetPtBgrdSubtractedKTPbPbWithGhosts", "Jets p_{T} distribution, KT background (PbPb w/ ghosts) subtracted", "", 500, -50., 200., fNumberOfCentralityBins, 0, 100, "p_{T} (GeV/c)","Centrality","dN^{Jets}/dp_{T}");
99       AddHistogram2D<TH2D>("hJetPtBgrdSubtractedKTCMS", "Jets p_{T} distribution, KT background (CMS) subtracted", "", 500, -50., 200., fNumberOfCentralityBins, 0, 100, "p_{T} (GeV/c)","Centrality","dN^{Jets}/dp_{T}");    
100       AddHistogram2D<TH2D>("hJetPtBgrdSubtractedKTMean", "Jets p_{T} distribution, KT background (Mean) subtracted", "", 500, -50., 200., fNumberOfCentralityBins, 0, 100, "p_{T} (GeV/c)","Centrality","dN^{Jets}/dp_{T}");    
101       AddHistogram2D<TH2D>("hJetPtBgrdSubtractedKTTrackLike", "Jets p_{T} distribution, KT background (track-like) subtracted", "", 500, -50., 200., fNumberOfCentralityBins, 0, 100, "p_{T} (GeV/c)","Centrality","dN^{Jets}/dp_{T}");
102     }
103
104     // ######## Jet profiles
105     if(fAnalyzeJetProfile)
106     {
107       AddHistogram2D<TH2D>("hJetProfile10GeV", "Jet profile, cone p_{T}/jet p_{T} vs. jet radius, jet p_{T} > 10 GeV", "", 12, 0, 0.6,200, 0., 2., "Cone radius","dN^{Jets}/dR", "Ratio");
108       AddHistogram2D<TH2D>("hJetProfile20GeV", "Jet profile, cone p_{T}/jet p_{T} vs. jet radius, jet p_{T} > 20 GeV", "", 12, 0, 0.6,200, 0., 2., "Cone radius","dN^{Jets}/dR", "Ratio");
109       AddHistogram2D<TH2D>("hJetProfile30GeV", "Jet profile, cone p_{T}/jet p_{T} vs. jet radius, jet p_{T} > 30 GeV", "", 12, 0, 0.6,200, 0., 2., "Cone radius","dN^{Jets}/dR", "Ratio");
110       AddHistogram2D<TH2D>("hJetProfile40GeV", "Jet profile, cone p_{T}/jet p_{T} vs. jet radius, jet p_{T} > 40 GeV", "", 12, 0, 0.6,200, 0., 2., "Cone radius","dN^{Jets}/dR", "Ratio");
111       AddHistogram2D<TH2D>("hJetProfile50GeV", "Jet profile, cone p_{T}/jet p_{T} vs. jet radius, jet p_{T} > 50 GeV", "", 12, 0, 0.6,200, 0., 2., "Cone radius","dN^{Jets}/dR", "Ratio");
112       AddHistogram2D<TH2D>("hJetProfile60GeV", "Jet profile, cone p_{T}/jet p_{T} vs. jet radius, jet p_{T} > 60 GeV", "", 12, 0, 0.6,200, 0., 2., "Cone radius","dN^{Jets}/dR", "Ratio");
113       AddHistogram2D<TH2D>("hJetProfile70GeV", "Jet profile, cone p_{T}/jet p_{T} vs. jet radius, jet p_{T} > 70 GeV", "", 12, 0, 0.6,200, 0., 2., "Cone radius","dN^{Jets}/dR", "Ratio");
114     }
115     // ######## Jet stuff
116     AddHistogram2D<TH2D>("hJetConstituentPt", "Jet constituents p_{T} distribution", "", 500, -50., 200., fNumberOfCentralityBins, 0, 100, "p_{T} (GeV/c)","Centrality","dN^{Tracks}/dp_{T}");
117     AddHistogram1D<TH1D>("hJetCountAll", "Number of Jets", "", 200, 0., 200., "N jets","dN^{Events}/dN^{Jets}");
118     AddHistogram1D<TH1D>("hJetCountAccepted", "Number of accepted Jets", "", 200, 0., 200., "N jets","dN^{Events}/dN^{Jets}");
119     AddHistogram2D<TH2D>("hJetCount", "Correlation jets/accepted jets", "", 200, 0., 200., 200, 0., 200., "N jets","N jets accepted", "d^{2}N^{Events}/dN^{Jets dN^{Jets, acc}}");
120     AddHistogram1D<TH1D>("hLeadingJetPt", "Leading jet p_{T}", "", 500, -50., 200., "p_{T} (GeV/c)","dN^{Jets}/dp_{T}");
121     AddHistogram1D<TH1D>("hSecondLeadingJetPt", "Second leading jet p_{T}", "", 500, -50., 200., "p_{T} (GeV/c)","dN^{Jets}/dp_{T}");
122     AddHistogram1D<TH1D>("hCorrectedLeadingJetPt", "Corrected leading jet p_{T}", "", 500, -50., 200., "p_{T} (GeV/c)","dN^{Jets}/dp_{T}");
123     AddHistogram1D<TH1D>("hCorrectedSecondLeadingJetPt", "Corrected second leading jet p_{T}", "", 500, -50., 200., "p_{T} (GeV/c)","dN^{Jets}/dp_{T}");
124     AddHistogram1D<TH1D>("hJetDeltaPhi", "Jets combinatorial #Delta #phi", "", 250, 0., TMath::Pi(), "#Delta #phi","dN^{Jets}/d(#Delta #phi)");
125     AddHistogram1D<TH1D>("hLeadingJetDeltaPhi", "1st and 2nd leading jet #Delta #phi", "", 250, 0., TMath::Pi(), "#Delta #phi","dN^{Jets}/d(#Delta #phi)");
126
127     // ########## Dijet stuff
128     AddHistogram1D<TH1D>("hDijetLeadingJetPt", "Dijet leading jet p_{T} distribution", "", 500, 0., 100., "p_{T} (GeV/c)","dN^{Jets}/dp_{T}");
129     AddHistogram1D<TH1D>("hDijetConstituentsPt", "Dijet constituents p_{T} distribution", "", 500, 0., 100., "p_{T} (GeV/c)","dN^{Jets}/dp_{T}");
130     AddHistogram2D<TH2D>("hDijetPtCorrelation", "Dijet constituents p_{T} correlation", "COLZ", 500, 5., 100., 500, 5., 100., "1st leading jet p_{T} (GeV/c)","2nd leading jet p_{T} (GeV/c)","dN^{Dijets}/d^{2}p_{T}");
131   }
132
133   // NOTE: Jet background histograms
134   if (fAnalyzeBackground)
135   {
136     // ########## Default background estimates
137     AddHistogram2D<TH2D>("hKTBackgroundImprovedCMS", "KT background density (Improved CMS approach)", "LEGO2", 400, 0., 40., fNumberOfCentralityBins, 0, 100, "#rho (GeV/c)","Centrality", "dN^{Events}/d#rho");
138     AddHistogram2D<TH2D>("hKTBackgroundImprovedCMSExternal", "KT background density (Improved CMS approach from external task)", "LEGO2", 400, 0., 40., fNumberOfCentralityBins, 0, 100, "#rho (GeV/c)","Centrality", "dN^{Events}/d#rho");
139     AddHistogram2D<TH2D>("hDeltaPtKTImprovedCMS", "Background fluctuations #delta p_{T} (KT, Improved CMS-like)", "", 1201, -40.0, 40.0, fNumberOfCentralityBins, 0, 100, "#delta p_{T} (GeV/c)","Centrality","dN^{Jets}/d#delta p_{T}");
140     AddHistogram2D<TH2D>("hDeltaPtKTImprovedCMSPartialExclusion", "Background fluctuations #delta p_{T} (KT, Improved CMS-like, partial jet exclusion)", "", 1201, -40.0, 40.0, fNumberOfCentralityBins, 0, 100, "#delta p_{T} (GeV/c)","Centrality","dN^{Jets}/d#delta p_{T}");
141     AddHistogram2D<TH2D>("hDeltaPtKTImprovedCMSPartialExclusion_Signal", "Background fluctuations #delta p_{T} (KT, Improved CMS-like, partial jet exclusion w/ 1/N_{sig} probability)", "", 1201, -40.0, 40.0, fNumberOfCentralityBins, 0, 100, "#delta p_{T} (GeV/c)","Centrality","dN^{Jets}/d#delta p_{T}");
142     AddHistogram2D<TH2D>("hDeltaPtKTImprovedCMSFullExclusion", "Background fluctuations #delta p_{T} (KT, Improved CMS-like, full leading jet exclusion)", "", 1201, -40.0, 40.0, fNumberOfCentralityBins, 0, 100, "#delta p_{T} (GeV/c)","Centrality","dN^{Jets}/d#delta p_{T}");
143     AddHistogram2D<TH2D>("hDeltaPtNoBackground", "Background fluctuations #delta p_{T} (No background)", "", 1201, -40.0, 40.0, fNumberOfCentralityBins, 0, 100, "#delta p_{T} (GeV/c)","Centrality","dN^{Jets}/d#delta p_{T}");
144     AddHistogram2D<TH2D>("hDeltaPtNoBackgroundNoEmptyCones", "Background fluctuations #delta p_{T} (No background, no empty cones)", "", 1201, -40.0, 40.0, fNumberOfCentralityBins, 0, 100, "#delta p_{T} (GeV/c)","Centrality","dN^{Jets}/d#delta p_{T}");
145
146     AddHistogram1D<TProfile>("hKTMeanBackgroundImprovedCMS", "KT background mean (Improved CMS approach)", "", 100, 0, 100, "Centrality", "#rho mean");
147
148     AddHistogram2D<TH2D>("hDijetBackground", "Background density (dijets excluded)", "", 200, 0., 20., fNumberOfCentralityBins, 0, 100, "#rho (GeV/c)","Centrality", "dN^{Events}/d#rho");
149     AddHistogram2D<TH2D>("hDijetBackgroundPerpendicular", "Background density (dijets excluded)", "", 200, 0., 20., fNumberOfCentralityBins, 0, 100, "#rho (GeV/c)","Centrality", "dN^{Events}/d#rho");
150     AddHistogram2D<TH2D>("hDijetBackgroundPerpendicularDeltaPt", "Background fluctuations #delta p_{T} (dijets excluded)", "", 1201, -80.0, 80.0, fNumberOfCentralityBins, 0, 100, "#delta p_{T} (GeV/c)","Centrality","dN^{Jets}/d#delta p_{T}");
151
152     if(fAnalyzeDeprecatedBackgrounds)
153     {
154       // ########## Different background estimates
155       AddHistogram2D<TH2D>("hKTBackgroundPbPb", "KT background density (PbPb approach, no ghosts)", "LEGO2", 400, 0., 40., fNumberOfCentralityBins, 0, 100, "#rho (GeV/c)","Centrality", "dN^{Events}/d#rho");
156       AddHistogram2D<TH2D>("hKTBackgroundPbPbWithGhosts", "KT background density (PbPb approach w/ ghosts)", "LEGO2", 400, 0., 40., fNumberOfCentralityBins, 0, 100, "#rho (GeV/c)","Centrality", "dN^{Events}/d#rho");
157       AddHistogram2D<TH2D>("hKTBackgroundCMS", "KT background density (CMS approach)", "LEGO2", 400, 0., 40., fNumberOfCentralityBins, 0, 100, "#rho (GeV/c)","Centrality", "dN^{Events}/d#rho");
158       AddHistogram2D<TH2D>("hKTBackgroundMean", "KT background density (Mean approach)", "LEGO2", 400, 0., 40., fNumberOfCentralityBins, 0, 100, "#rho (GeV/c)","Centrality", "dN^{Events}/d#rho");
159       AddHistogram2D<TH2D>("hKTBackgroundTrackLike", "KT background density (Track-like approach)", "LEGO2", 400, 0., 40., fNumberOfCentralityBins, 0, 100, "#rho (GeV/c)","Centrality", "dN^{Events}/d#rho");
160
161       AddHistogram2D<TH2D>("hTRBackgroundNoExcl", "TR background density (No signal excluded)", "LEGO2", 400, 0., 40., fNumberOfCentralityBins, 0, 100, "#rho (GeV/c)","Centrality", "dN^{Events}/d#rho");
162       AddHistogram2D<TH2D>("hTRBackgroundCone02", "TR background density (Cones R=0.2 around signal jets excluded)", "LEGO2", 400, 0., 40., fNumberOfCentralityBins, 0, 100, "#rho (GeV/c)","Centrality", "dN^{Events}/d#rho");
163       AddHistogram2D<TH2D>("hTRBackgroundCone04", "TR background density (Cones R=0.4 around signal jets excluded)", "LEGO2", 400, 0., 40., fNumberOfCentralityBins, 0, 100, "#rho (GeV/c)","Centrality", "dN^{Events}/d#rho");
164       AddHistogram2D<TH2D>("hTRBackgroundCone06", "TR background density (Cones R=0.6 around signal jets excluded)", "LEGO2", 400, 0., 40., fNumberOfCentralityBins, 0, 100, "#rho (GeV/c)","Centrality", "dN^{Events}/d#rho");
165       AddHistogram2D<TH2D>("hTRBackgroundCone08", "TR background density (Cones R=0.8 around signal jets excluded)", "LEGO2", 400, 0., 40., fNumberOfCentralityBins, 0, 100, "#rho (GeV/c)","Centrality", "dN^{Events}/d#rho");
166       AddHistogram2D<TH2D>("hTRBackgroundExact",  "TR background density (signal jets exactly excluded)", "LEGO2", 400, 0., 40., fNumberOfCentralityBins, 0, 100, "#rho (GeV/c)","Centrality", "dN^{Events}/d#rho");
167
168       // ########## Delta Pt
169       AddHistogram2D<TH2D>("hDeltaPtKTPbPb", "Background fluctuations #delta p_{T} (KT, PbPb w/o ghosts)", "", 1201, -40.0, 40.0, fNumberOfCentralityBins, 0, 100, "#delta p_{T} (GeV/c)","Centrality","dN^{Jets}/d#delta p_{T}");
170       AddHistogram2D<TH2D>("hDeltaPtKTPbPbWithGhosts", "Background fluctuations #delta p_{T} (KT, PbPb w/ ghosts)", "", 1201, -40.0, 40.0, fNumberOfCentralityBins, 0, 100, "#delta p_{T} (GeV/c)","Centrality","dN^{Jets}/d#delta p_{T}");
171       AddHistogram2D<TH2D>("hDeltaPtKTCMS", "Background fluctuations #delta p_{T} (KT, CMS-like)", "", 1201, -40.0, 40.0, fNumberOfCentralityBins, 0, 100, "#delta p_{T} (GeV/c)","Centrality","dN^{Jets}/d#delta p_{T}");
172       AddHistogram2D<TH2D>("hDeltaPtKTMean", "Background fluctuations #delta p_{T} (KT, Mean)", "", 1201, -40.0, 40.0, fNumberOfCentralityBins, 0, 100, "#delta p_{T} (GeV/c)","Centrality","dN^{Jets}/d#delta p_{T}");
173       AddHistogram2D<TH2D>("hDeltaPtKTTrackLike", "Background fluctuations #delta p_{T} (KT, track-like)", "", 1201, -40.0, 40.0, fNumberOfCentralityBins, 0, 100, "#delta p_{T} (GeV/c)","Centrality","dN^{Jets}/d#delta p_{T}");
174       AddHistogram2D<TH2D>("hDeltaPtTR", "Background fluctuations #delta p_{T} (TR, cone R=0.6)", "", 1201, -40.0, 40.0, fNumberOfCentralityBins, 0, 100,  "#delta p_{T} (GeV/c)","Centrality","dN^{Jets}/d#delta p_{T}");
175
176       // ########## Profiles for background means vs. centrality
177       AddHistogram1D<TProfile>("hKTMeanBackgroundPbPb", "KT background mean (PbPb approach w/o ghosts)", "", fNumberOfCentralityBins, 0, 100, "Centrality", "#rho mean");
178       AddHistogram1D<TProfile>("hKTMeanBackgroundPbPbWithGhosts", "KT background mean (PbPb approach)", "", fNumberOfCentralityBins, 0, 100, "Centrality", "#rho mean");
179       AddHistogram1D<TProfile>("hKTMeanBackgroundCMS", "KT background mean (CMS approach)", "", fNumberOfCentralityBins, 0, 100, "Centrality", "#rho mean");
180       AddHistogram1D<TProfile>("hKTMeanBackgroundMean", "KT background mean (Mean approach)", "",  fNumberOfCentralityBins, 0, 100, "Centrality", "#rho mean");
181       AddHistogram1D<TProfile>("hKTMeanBackgroundTPC", "KT background mean (Track-like approach)", "", fNumberOfCentralityBins, 0, 100, "Centrality", "#rho mean");
182       AddHistogram1D<TProfile>("hTRMeanBackground", "TR background mean", "", fNumberOfCentralityBins, 0, 100, "Centrality", "#rho mean");
183     }
184   }
185   // NOTE: Jet constituent correlations
186   if(fAnalyzeMassCorrelation)
187   {
188     AddHistogram1D<TH1D>("hJetMassFromConstituents", "Jet mass by mass of charged constituents", "", 200, 0., 200., "N jets","dN^{Jets}/dN^{mass}");
189     AddHistogram1D<TH1D>("hJetMass", "Jet mass from fastjet", "", 200, 0., 200., "N jets","dN^{Jets}/dN^{mass}");
190
191     AddHistogram2D<TH2D>("hJetPtVsMass", "Correlation jet pt/summed constituent jet", "", 400, 0., 100., 400, 0., 100., "p_{T}","Mass", "d^{2}N}/dN^{p_{T}dM");
192     AddHistogram2D<TH2D>("hJetPtVsJetMass", "Correlation jet pt/summed jet", "", 400, 0., 100., 400, 0., 100., "p_{T}","Mass", "d^{2}N}/dN^{p_{T}dM");
193
194     AddHistogram2D<TH2D>("hJetPtVsProtonCount", "Correlation jet pt/amount of proton in jet", "", 400, 0., 100., 20, 0., 1., "p_{T}","Proton percentage", "d^{2}N}/dN^{p_{T}dPercentage");
195     AddHistogram2D<TH2D>("hJetPtVsPionCount", "Correlation jet pt/amount of pion in jet", "", 400, 0., 100., 20, 0., 1., "p_{T}","Pion percentage", "d^{2}N}/dN^{p_{T}dPercentage");
196     AddHistogram2D<TH2D>("hJetPtVsKaonCount", "Correlation jet pt/amount of kaon in jet", "", 400, 0., 100., 20, 0., 1., "p_{T}","Kaon percentage", "d^{2}N}/dN^{p_{T}dPercentage");
197     AddHistogram2D<TH2D>("hJetPtVsElectronCount", "Correlation jet pt/amount of proton in jet", "", 400, 0., 100., 20, 0., 1., "p_{T}","Electron percentage", "d^{2}N}/dN^{p_{T}dPercentage");
198     AddHistogram2D<TH2D>("hJetPtVsOthersCount", "Correlation jet pt/amount of other particles in jet", "", 400, 0., 100., 20, 0., 1., "p_{T}","Others percentage", "d^{2}N}/dN^{p_{T}dPercentage");
199     AddHistogram1D<TH1D>("hJetConstituentProtonCount", "Proton count in jets", "", 100, 0., 100., "N protons","dN^{Jets}/dN^{protons}");
200     AddHistogram1D<TH1D>("hJetConstituentPionCount", "Pion count in jets", "", 100, 0., 100., "N pions","dN^{Jets}/dN^{pions}");
201     AddHistogram1D<TH1D>("hJetConstituentKaonCount", "Kaon count in jets", "", 100, 0., 100., "N kaons","dN^{Jets}/dN^{kaons}");
202     AddHistogram1D<TH1D>("hJetConstituentElectronCount", "Electron count in jets", "", 100, 0., 100., "N electrons","dN^{Jets}/dN^{electrons}");
203     AddHistogram1D<TH1D>("hJetConstituentOthersCount", "Others count in jets", "", 100, 0., 100., "N others","dN^{Jets}/dN^{others}");
204
205     AddHistogram2D<TH2D>("hJetPtVsMass_6_14", "Correlation jet pt/summed constituent jet", "", 400, 0., 100., 400, 0., 100., "p_{T}","Mass", "d^{2}N}/dN^{p_{T}dM");
206     AddHistogram2D<TH2D>("hJetPtVsJetMass_6_14", "Correlation jet pt/summed jet", "", 400, 0., 100., 400, 0., 100., "p_{T}","Mass", "d^{2}N}/dN^{p_{T}dM");
207     AddHistogram2D<TH2D>("hJetPtVsProtonCount_6_14", "Correlation jet pt/amount of proton in jet", "", 400, 0., 100., 8, 6., 14., "p_{T}","Proton count", "d^{2}N}/dN^{p_{T}dN");
208     AddHistogram2D<TH2D>("hJetPtVsPionCount_6_14", "Correlation jet pt/amount of pion in jet", "", 400, 0., 100., 8, 6., 14., "p_{T}","Pion count", "d^{2}N}/dN^{p_{T}dN");
209     AddHistogram2D<TH2D>("hJetPtVsKaonCount_6_14", "Correlation jet pt/amount of kaon in jet", "", 400, 0., 100., 8, 6., 14., "p_{T}","Kaon count", "d^{2}N}/dN^{p_{T}dN");
210     AddHistogram2D<TH2D>("hJetPtVsElectronCount_6_14", "Correlation jet pt/amount of proton in jet", "", 400, 0., 100., 8, 6., 14., "p_{T}","Electron count", "d^{2}N}/dN^{p_{T}dN");
211     AddHistogram2D<TH2D>("hJetPtVsOthersCount_6_14", "Correlation jet pt/amount of other particles in jet", "", 400, 0., 100., 8, 6., 14., "p_{T}","Others count", "d^{2}N}/dN^{p_{T}dN");
212     AddHistogram1D<TH1D>("hJetConstituentProtonCount_6_14", "Proton count in jets", "", 100, 0., 100., "N protons","dN^{Jets}/dN^{protons}");
213     AddHistogram1D<TH1D>("hJetConstituentPionCount_6_14", "Pion count in jets", "", 100, 0., 100., "N pions","dN^{Jets}/dN^{pions}");
214     AddHistogram1D<TH1D>("hJetConstituentKaonCount_6_14", "Kaon count in jets", "", 100, 0., 100., "N kaons","dN^{Jets}/dN^{kaons}");
215     AddHistogram1D<TH1D>("hJetConstituentElectronCount_6_14", "Electron count in jets", "", 100, 0., 100., "N electrons","dN^{Jets}/dN^{electrons}");
216     AddHistogram1D<TH1D>("hJetConstituentOthersCount_6_14", "Others count in jets", "", 100, 0., 100., "N others","dN^{Jets}/dN^{others}");
217
218     AddHistogram2D<TH2D>("hJetPtVsMass_2_X", "Correlation jet pt/summed constituent jet", "", 400, 0., 100., 400, 0., 100., "p_{T}","Mass", "d^{2}N}/dN^{p_{T}dM");
219     AddHistogram2D<TH2D>("hJetPtVsJetMass_2_X", "Correlation jet pt/summed jet", "", 400, 0., 100., 400, 0., 100., "p_{T}","Mass", "d^{2}N}/dN^{p_{T}dM");
220     AddHistogram2D<TH2D>("hJetPtVsProtonCount_2_X", "Correlation jet pt/amount of proton in jet", "", 400, 0., 100., 68, 2., 70., "p_{T}","Proton count", "d^{2}N}/dN^{p_{T}dN");
221     AddHistogram2D<TH2D>("hJetPtVsPionCount_2_X", "Correlation jet pt/amount of pion in jet", "", 400, 0., 100., 68, 2., 70., "p_{T}","Pion count", "d^{2}N}/dN^{p_{T}dN");
222     AddHistogram2D<TH2D>("hJetPtVsKaonCount_2_X", "Correlation jet pt/amount of kaon in jet", "", 400, 0., 100., 68, 2., 70., "p_{T}","Kaon count", "d^{2}N}/dN^{p_{T}dN");
223     AddHistogram2D<TH2D>("hJetPtVsElectronCount_2_X", "Correlation jet pt/amount of proton in jet", "", 400, 0., 100., 68, 2., 70., "p_{T}","Electron count", "d^{2}N}/dN^{p_{T}dN");
224     AddHistogram2D<TH2D>("hJetPtVsOthersCount_2_X", "Correlation jet pt/amount of other particles in jet", "", 400, 0., 100., 68, 2., 70., "p_{T}","Others count", "d^{2}N}/dN^{p_{T}dN");
225     AddHistogram1D<TH1D>("hJetConstituentProtonCount_2_X", "Proton count in jets", "", 100, 0., 100., "N protons","dN^{Jets}/dN^{protons}");
226     AddHistogram1D<TH1D>("hJetConstituentPionCount_2_X", "Pion count in jets", "", 100, 0., 100., "N pions","dN^{Jets}/dN^{pions}");
227     AddHistogram1D<TH1D>("hJetConstituentKaonCount_2_X", "Kaon count in jets", "", 100, 0., 100., "N kaons","dN^{Jets}/dN^{kaons}");
228     AddHistogram1D<TH1D>("hJetConstituentElectronCount_2_X", "Electron count in jets", "", 100, 0., 100., "N electrons","dN^{Jets}/dN^{electrons}");
229     AddHistogram1D<TH1D>("hJetConstituentOthersCount_2_X", "Others count in jets", "", 100, 0., 100., "N others","dN^{Jets}/dN^{others}");
230
231     AddHistogram2D<TH2D>("hJetPtVsMass_2_7", "Correlation jet pt/summed constituent jet", "", 400, 0., 100., 400, 0., 100., "p_{T}","Mass", "d^{2}N}/dN^{p_{T}dM");
232     AddHistogram2D<TH2D>("hJetPtVsJetMass_2_7", "Correlation jet pt/summed jet", "", 400, 0., 100., 400, 0., 100., "p_{T}","Mass", "d^{2}N}/dN^{p_{T}dM");
233     AddHistogram2D<TH2D>("hJetPtVsProtonCount_2_7", "Correlation jet pt/amount of proton in jet", "", 400, 0., 100., 6, 2., 8., "p_{T}","Proton count", "d^{2}N}/dN^{p_{T}dN");
234     AddHistogram2D<TH2D>("hJetPtVsPionCount_2_7", "Correlation jet pt/amount of pion in jet", "", 400, 0., 100., 6, 2., 8., "p_{T}","Pion count", "d^{2}N}/dN^{p_{T}dN");
235     AddHistogram2D<TH2D>("hJetPtVsKaonCount_2_7", "Correlation jet pt/amount of kaon in jet", "", 400, 0., 100., 6, 2., 8., "p_{T}","Kaon count", "d^{2}N}/dN^{p_{T}dN");
236     AddHistogram2D<TH2D>("hJetPtVsElectronCount_2_7", "Correlation jet pt/amount of proton in jet", "", 400, 0., 100., 6, 2., 8., "p_{T}","Electron count", "d^{2}N}/dN^{p_{T}dN");
237     AddHistogram2D<TH2D>("hJetPtVsOthersCount_2_7", "Correlation jet pt/amount of other particles in jet", "", 400, 0., 100., 6, 2., 8., "p_{T}","Others count", "d^{2}N}/dN^{p_{T}dN");
238     AddHistogram1D<TH1D>("hJetConstituentProtonCount_2_7", "Proton count in jets", "", 100, 0., 100., "N protons","dN^{Jets}/dN^{protons}");
239     AddHistogram1D<TH1D>("hJetConstituentPionCount_2_7", "Pion count in jets", "", 100, 0., 100., "N pions","dN^{Jets}/dN^{pions}");
240     AddHistogram1D<TH1D>("hJetConstituentKaonCount_2_7", "Kaon count in jets", "", 100, 0., 100., "N kaons","dN^{Jets}/dN^{kaons}");
241     AddHistogram1D<TH1D>("hJetConstituentElectronCount_2_7", "Electron count in jets", "", 100, 0., 100., "N electrons","dN^{Jets}/dN^{electrons}");
242     AddHistogram1D<TH1D>("hJetConstituentOthersCount_2_7", "Others count in jets", "", 100, 0., 100., "N others","dN^{Jets}/dN^{others}");
243
244   }
245
246   // NOTE: Track & Cluster & QA histograms
247   if (fAnalyzeQA)
248   {
249     AddHistogram1D<TH1D>("hVertexX", "X distribution of the vertex", "", 2000, -1., 1., "#Delta x(cm)","dN^{Events}/dx");
250     AddHistogram1D<TH1D>("hVertexY", "Y distribution of the vertex", "", 2000, -1., 1., "#Delta y(cm)","dN^{Events}/dy");
251     AddHistogram2D<TH2D>("hVertexXY", "XY distribution of the vertex", "COLZ", 500, -1., 1., 500, -1., 1.,"#Delta x(cm)", "#Delta y(cm)","dN^{Events}/dxdy");
252     AddHistogram1D<TH1D>("hVertexZBeforeVertexCut", "Z distribution of the vertex (before std. vertex cut)", "", 200, -20., 20., "#Delta z(cm)","dN^{Events}/dz");
253     AddHistogram1D<TH1D>("hVertexZAfterVertexCut", "Z distribution of the vertex (after std. vertex cut)", "", 200, -20., 20., "#Delta z(cm)","dN^{Events}/dz");
254     AddHistogram1D<TH1D>("hVertexR", "R distribution of the vertex", "", 100, 0., 1., "#Delta r(cm)","dN^{Events}/dr");
255     AddHistogram1D<TH1D>("hCentralityV0M", "Centrality distribution V0M", "", fNumberOfCentralityBins, 0., 100., "Centrality","dN^{Events}");
256     AddHistogram1D<TH1D>("hCentralityV0A", "Centrality distribution V0A", "", fNumberOfCentralityBins, 0., 100., "Centrality","dN^{Events}");
257     AddHistogram1D<TH1D>("hCentralityV0C", "Centrality distribution V0C", "", fNumberOfCentralityBins, 0., 100., "Centrality","dN^{Events}");
258     AddHistogram1D<TH1D>("hCentrality", Form("Centrality distribution %s", fCentralityType.Data()), "", fNumberOfCentralityBins, 0., 100., "Centrality","dN^{Events}");
259
260
261     AddHistogram2D<TH2D>("hTrackCountAcc", "Number of tracks in acceptance vs. centrality", "LEGO2", 750, 0., 750., fNumberOfCentralityBins, 0, 100, "N tracks","Centrality", "dN^{Events}/dN^{Tracks}");
262     AddHistogram2D<TH2D>("hTrackPt", "Tracks p_{T} distribution", "", 1000, 0., 250., fNumberOfCentralityBins, 0, 100, "p_{T} (GeV/c)", "Centrality", "dN^{Tracks}/dp_{T}");
263     AddHistogram2D<TH2D>("hTrackPtNegEta", "Tracks p_{T} distribution (negative #eta)", "", 1000, 0., 250., fNumberOfCentralityBins, 0, 100, "p_{T} (GeV/c)","Centrality","dN^{Tracks}/dp_{T}");
264     AddHistogram2D<TH2D>("hTrackPtPosEta", "Tracks p_{T} distribution (positive #eta)", "", 1000, 0., 250., fNumberOfCentralityBins, 0, 100, "p_{T} (GeV/c)","Centrality","dN^{Tracks}/dp_{T}");
265     AddHistogram1D<TH1D>("hTrackCharge", "Charge", "", 11, -5, 5, "Charge (e)","dN^{Tracks}/dq");
266     AddHistogram1D<TH1D>("hTrackPhi", "Track #phi distribution", "", 360, 0, TMath::TwoPi(), "#phi","dN^{Tracks}/d#phi");
267     AddHistogram2D<TH2D>("hTrackPhiEta", "Track angular distribution", "LEGO2", 100, 0., 2*TMath::Pi(),100, -2.5, 2.5, "#phi","#eta","dN^{Tracks}/(d#phi d#eta)");
268
269     AddHistogram2D<TH2D>("hTrackPhiPtCut", "Track #phi distribution for different pT cuts", "LEGO2", 360, 0, TMath::TwoPi(), 20, 0, 20, "#phi", "p_{T} lower cut", "dN^{Tracks}/d#phi dp_{T}");
270     AddHistogram2D<TH2D>("hTrackPhiLabel", "Track #phi distribution for different labels", "LEGO2", 360, 0, TMath::TwoPi(), 3, 0, 3, "#phi", "Label", "dN^{Tracks}/d#phi");
271     AddHistogram2D<TH2D>("hTrackPhiTrackType", "Track #phi distribution for different track types", "LEGO2", 360, 0, TMath::TwoPi(), 3, 0, 3, "#phi", "Label", "dN^{Tracks}/d#phi");
272     AddHistogram2D<TH2D>("hTrackEta", "Track #eta distribution", "COLZ", 180, -fTrackEtaWindow, +fTrackEtaWindow, fNumberOfCentralityBins, 0., 100., "#eta", "Centrality", "dN^{Tracks}/d#eta");
273     if (fAnalyzeJets)
274     {
275       // ######## Jet QA
276       AddHistogram1D<TH1D>("hRawJetArea", "Jets area distribution w/o area cut", "", 200, 0., 2., "Area","dN^{Jets}/dA");
277       AddHistogram1D<TH1D>("hJetArea", "Jets area distribution", "", 200, 0., 2., "Area","dN^{Jets}/dA");
278       AddHistogram2D<TH2D>("hRawJetPhiEta", "Raw Jets angular distribution w/o #eta cut", "LEGO2", 360, 0., 2*TMath::Pi(),100, -1.0, 1.0, "#phi","#eta","dN^{Jets}/(d#phi d#eta)");
279       AddHistogram2D<TH2D>("hJetEta", "Jets #eta distribution", "COLZ", 180, -fTrackEtaWindow, +fTrackEtaWindow, fNumberOfCentralityBins, 0., 100., "#eta", "Centrality", "dN^{Jets}/d#eta");
280       AddHistogram2D<TH2D>("hJetEta2GeVTracks", "Jets #eta distribution, track p_{T} > 2 GeV", "COLZ", 180, -fTrackEtaWindow, +fTrackEtaWindow, fNumberOfCentralityBins, 0., 100., "#eta", "Centrality", "dN^{Jets}/d#eta");
281       AddHistogram2D<TH2D>("hJetEta4GeVTracks", "Jets #eta distribution, track p_{T} > 4 GeV", "COLZ", 180, -fTrackEtaWindow, +fTrackEtaWindow, fNumberOfCentralityBins, 0., 100., "#eta", "Centrality", "dN^{Jets}/d#eta");
282       AddHistogram2D<TH2D>("hJetPhiEta", "Jets angular distribution", "LEGO2", 360, 0., 2*TMath::Pi(),100, -1.0, 1.0, "#phi","#eta","dN^{Jets}/(d#phi d#eta)");
283       AddHistogram2D<TH2D>("hJetPtVsConstituentCount", "Jets number of constituents vs. jet p_{T}", "COLZ", 400, 0., 200., 100, 0., 100., "p_{T}","N^{Tracks}","dN^{Jets}/(dp_{T} dN^{tracks})");
284     }
285   }
286
287   // NOTE: Pythia histograms
288   if (fAnalyzePythia)
289   {
290     AddHistogram1D<TH1D>("hPythiaPtHard", "Pythia p_{T} hard distribution", "", 2000, 0, 400, "p_{T} hard","dN^{Events}/dp_{T,hard}");
291   }
292
293   // register Histograms
294   for (Int_t i = 0; i < fHistCount; i++)
295   {
296     fOutputList->Add(fHistList->At(i));
297   }
298   
299   PostData(1,fOutputList); // important for merging
300
301 }
302
303 //________________________________________________________________________
304 AliAnalysisTaskChargedJetsPA::AliAnalysisTaskChargedJetsPA(const char *name, const char* trackArrayName, const char* jetArrayName, const char* backgroundJetArrayName) : AliAnalysisTaskSE(name), fOutputList(0), fAnalyzeJets(1), fAnalyzeJetProfile(1), fAnalyzeQA(1), fAnalyzeBackground(1), fAnalyzeDeprecatedBackgrounds(1), fAnalyzePythia(0), fAnalyzeMassCorrelation(0), fHasTracks(0), fHasJets(0), fHasBackgroundJets(0), fIsKinematics(0), fUseDefaultVertexCut(1), fUsePileUpCut(1), fSetCentralityToOne(0), fPartialAnalysisNParts(1), fPartialAnalysisIndex(0), fJetArray(0), fTrackArray(0), fBackgroundJetArray(0), fJetArrayName(0), fTrackArrayName(0), fBackgroundJetArrayName(0), fNumPtHardBins(11), fUsePtHardBin(-1), fRhoTaskName(), fNcoll(6.88348), fRandConeRadius(0.4), fSignalJetRadius(0.4), fBackgroundJetRadius(0.4), fTRBackgroundConeRadius(0.6), fNumberRandCones(8), fNumberExcludedJets(-1), fDijetMaxAngleDeviation(10.0), fPhysicalJetRadius(0.6), fSignalJetEtaWindow(0.5), fBackgroundJetEtaWindow(0.5), fTrackEtaWindow(0.9), fMinTrackPt(0.150), fMinJetPt(0.15), fMinJetArea(0.5), fMinBackgroundJetPt(0.0), fMinDijetLeadingPt(10.0), fNumberOfCentralityBins(20), fCentralityType("V0A"), fFirstLeadingJet(0), fSecondLeadingJet(0), fNumberSignalJets(0), fCrossSection(0.0), fTrials(0.0), fRandom(0), fHelperClass(0), fInitialized(0), fTaskInstanceCounter(0), fHistList(0), fHistCount(0), fIsDEBUG(0), fEventCounter(0)
305 {
306   #ifdef DEBUGMODE
307     AliInfo("Calling constructor.");
308   #endif
309
310   // Every instance of this task gets his own number
311   static Int_t instance = 0;
312   fTaskInstanceCounter = instance;
313   instance++;
314
315   fTrackArrayName = new TString(trackArrayName);
316   if (fTrackArrayName->Contains("MCParticles") || fTrackArrayName->Contains("mcparticles"))
317     fIsKinematics = kTRUE;
318
319   fJetArrayName = new TString(jetArrayName);
320   if (strcmp(fJetArrayName->Data(),"") == 0)
321   {
322     fAnalyzeJets = kFALSE;
323     fAnalyzeJetProfile = kFALSE;
324   }
325   else
326     fAnalyzeJets = kTRUE;
327     
328   fBackgroundJetArrayName = new TString(backgroundJetArrayName);
329   if (strcmp(fBackgroundJetArrayName->Data(),"") == 0)
330     fAnalyzeBackground = kFALSE;
331   else
332     fAnalyzeBackground = kTRUE;
333
334   DefineOutput(1, TList::Class());
335  
336   fHistList = new TList();
337
338   for(Int_t i=0;i<1024;i++)
339     fSignalJets[i] = NULL;
340
341
342   #ifdef DEBUGMODE
343     AliInfo("Constructor done.");
344   #endif
345   
346 }
347
348 //________________________________________________________________________
349 inline Double_t AliAnalysisTaskChargedJetsPA::GetConePt(Double_t eta, Double_t phi, Double_t radius)
350 {
351   Double_t tmpConePt = 0.0;
352
353   for (Int_t i = 0; i < fTrackArray->GetEntries(); i++)
354   {
355     AliVTrack* tmpTrack = static_cast<AliVTrack*>(fTrackArray->At(i));
356     if (IsTrackInAcceptance(tmpTrack))
357       if(IsTrackInCone(tmpTrack, eta, phi, radius))
358         tmpConePt = tmpConePt + tmpTrack->Pt();
359   }
360   return tmpConePt;
361 }
362
363
364 //________________________________________________________________________
365 inline Double_t AliAnalysisTaskChargedJetsPA::GetPtHard()
366 {
367   #ifdef DEBUGMODE
368     AliInfo("Starting GetPtHard.");
369   #endif
370   AliGenPythiaEventHeader* pythiaHeader = dynamic_cast<AliGenPythiaEventHeader*>(MCEvent()->GenEventHeader());
371   if (MCEvent()) 
372     if (!pythiaHeader)
373     {
374       // Check if AOD
375       AliAODMCHeader* aodMCH = dynamic_cast<AliAODMCHeader*>(InputEvent()->FindListObject(AliAODMCHeader::StdBranchName()));
376
377       if (aodMCH)
378       {
379         for(UInt_t i = 0;i<aodMCH->GetNCocktailHeaders();i++)
380         {
381           pythiaHeader = dynamic_cast<AliGenPythiaEventHeader*>(aodMCH->GetCocktailHeader(i));
382           if (pythiaHeader) break;
383         }
384       }
385     }
386
387   #ifdef DEBUGMODE
388     AliInfo("Ending GetPtHard.");
389   #endif
390   if (pythiaHeader)
391     return pythiaHeader->GetPtHard();
392
393   AliWarning(Form("In task %s: GetPtHard() failed!", GetName()));
394   return -1.0;
395 }
396
397
398 //________________________________________________________________________
399 inline Double_t AliAnalysisTaskChargedJetsPA::GetPythiaTrials()
400 {
401   #ifdef DEBUGMODE
402     AliInfo("Starting GetPythiaTrials.");
403   #endif
404   AliGenPythiaEventHeader* pythiaHeader = dynamic_cast<AliGenPythiaEventHeader*>(MCEvent()->GenEventHeader());
405   if (MCEvent()) 
406     if (!pythiaHeader)
407     {
408       // Check if AOD
409       AliAODMCHeader* aodMCH = dynamic_cast<AliAODMCHeader*>(InputEvent()->FindListObject(AliAODMCHeader::StdBranchName()));
410
411       if (aodMCH)
412       {
413         for(UInt_t i = 0;i<aodMCH->GetNCocktailHeaders();i++)
414         {
415           pythiaHeader = dynamic_cast<AliGenPythiaEventHeader*>(aodMCH->GetCocktailHeader(i));
416           if (pythiaHeader) break;
417         }
418       }
419     }
420
421   #ifdef DEBUGMODE
422     AliInfo("Ending GetPythiaTrials.");
423   #endif
424   if (pythiaHeader)
425     return pythiaHeader->Trials();
426
427   AliWarning(Form("In task %s: GetPythiaTrials() failed!", GetName()));
428   return -1.0;
429 }
430
431
432
433 //________________________________________________________________________
434 inline Int_t AliAnalysisTaskChargedJetsPA::GetPtHardBin()
435 {
436   #ifdef DEBUGMODE
437     AliInfo("Starting GetPtHardBin.");
438   #endif
439   // ########## PT HARD BIN EDGES
440   const Int_t kPtHardLowerEdges[] =  { 0, 5,11,21,36,57, 84,117,152,191,234};
441   const Int_t kPtHardHigherEdges[] = { 5,11,21,36,57,84,117,152,191,234,1000000};
442
443   Int_t tmpPtHardBin = 0;
444   Double_t tmpPtHard = GetPtHard();
445  
446   for (tmpPtHardBin = 0; tmpPtHardBin <= fNumPtHardBins; tmpPtHardBin++)
447     if (tmpPtHard >= kPtHardLowerEdges[tmpPtHardBin] && tmpPtHard < kPtHardHigherEdges[tmpPtHardBin])
448       break;
449
450   #ifdef DEBUGMODE
451     AliInfo("Ending GetPtHardBin.");
452   #endif
453   return tmpPtHardBin;
454 }
455
456 //________________________________________________________________________
457 Double_t AliAnalysisTaskChargedJetsPA::GetExternalRho()
458 {
459   // Get rho from event.
460   AliRhoParameter *rho = 0;
461   if (!fRhoTaskName.IsNull()) {
462     rho = dynamic_cast<AliRhoParameter*>(InputEvent()->FindListObject(fRhoTaskName.Data()));
463     if (!rho) {
464       AliWarning(Form("%s: Could not retrieve rho with name %s!", GetName(), fRhoTaskName.Data())); 
465       return 0;
466     }
467   }
468   else
469     return 0;
470
471   return (rho->GetVal());
472 }
473
474
475 //________________________________________________________________________
476 inline Bool_t AliAnalysisTaskChargedJetsPA::IsTrackInCone(AliVTrack* track, Double_t eta, Double_t phi, Double_t radius)
477 {
478   // This is to use a full cone in phi even at the edges of phi (2pi -> 0) (0 -> 2pi)
479   Double_t trackPhi = 0.0;
480   if (track->Phi() > (TMath::TwoPi() - (radius-phi)))
481     trackPhi = track->Phi() - TMath::TwoPi();
482   else if (track->Phi() < (phi+radius - TMath::TwoPi()))
483     trackPhi = track->Phi() + TMath::TwoPi();
484   else
485     trackPhi = track->Phi();
486   
487   if ( TMath::Abs(trackPhi-phi)*TMath::Abs(trackPhi-phi) + TMath::Abs(track->Eta()-eta)*TMath::Abs(track->Eta()-eta) <= radius*radius)
488     return kTRUE;
489   
490   return kFALSE;
491 }
492
493 //________________________________________________________________________
494 inline Bool_t AliAnalysisTaskChargedJetsPA::IsTrackInJet(AliEmcalJet* jet, Int_t trackIndex)
495 {
496   for (Int_t i = 0; i < jet->GetNumberOfTracks(); ++i)
497   {
498     Int_t jetTrack = jet->TrackAt(i);
499     if (jetTrack == trackIndex)
500       return kTRUE;
501   }
502   return kFALSE;
503 }
504
505 //________________________________________________________________________
506 inline Bool_t AliAnalysisTaskChargedJetsPA::IsJetOverlapping(AliEmcalJet* jet1, AliEmcalJet* jet2)
507 {
508   for (Int_t i = 0; i < jet1->GetNumberOfTracks(); ++i)
509   {
510     Int_t jet1Track = jet1->TrackAt(i);
511     for (Int_t j = 0; j < jet2->GetNumberOfTracks(); ++j)
512     {
513       Int_t jet2Track = jet2->TrackAt(j);
514       if (jet1Track == jet2Track)
515         return kTRUE;
516     }
517   }
518   return kFALSE;
519 }
520
521 //________________________________________________________________________
522 inline Bool_t AliAnalysisTaskChargedJetsPA::IsEventInAcceptance(AliVEvent* event)
523 {
524   if (!event)
525     return kFALSE;
526
527   FillHistogram("hEventAcceptance", 0.5); // number of events before manual cuts
528   if(fUsePileUpCut)
529     if(fHelperClass->IsPileUpEvent(event))
530       return kFALSE;
531
532   FillHistogram("hEventAcceptance", 1.5); // number of events after pileup cuts
533
534   if(fAnalyzeQA)
535     FillHistogram("hVertexZBeforeVertexCut",event->GetPrimaryVertex()->GetZ());
536
537   if(fUseDefaultVertexCut)
538   {
539     if(!fHelperClass->IsVertexSelected2013pA(event))
540       return kFALSE;
541   }
542   else // Failsafe vertex cut
543   {
544     if(TMath::Abs(event->GetPrimaryVertex()->GetZ()) > 10.0)
545       return kFALSE;
546   }
547
548   if(fAnalyzeQA)
549     FillHistogram("hVertexZAfterVertexCut",event->GetPrimaryVertex()->GetZ());
550
551   FillHistogram("hEventAcceptance", 2.5); // number of events after vertex cut
552
553   return kTRUE;
554 }
555
556 //________________________________________________________________________
557 inline Bool_t AliAnalysisTaskChargedJetsPA::IsTrackInAcceptance(AliVParticle* track)
558 {
559   FillHistogram("hTrackAcceptance", 0.5);
560   if (track != 0)
561   {
562     if(fIsKinematics)
563     {
564       // TODO: Only working for AOD MC
565       if((!track->Charge()) || (!(static_cast<AliAODMCParticle*>(track))->IsPhysicalPrimary()) )
566         return kFALSE;
567     }
568     if (TMath::Abs(track->Eta()) <= fTrackEtaWindow)
569     {
570       FillHistogram("hTrackAcceptance", 1.5);
571       if (track->Pt() >= fMinTrackPt)
572       {
573         FillHistogram("hTrackAcceptance", 2.5);
574         return kTRUE;
575       }
576     }
577   }
578   return kFALSE;
579 }
580
581 //________________________________________________________________________
582 inline Bool_t AliAnalysisTaskChargedJetsPA::IsBackgroundJetInAcceptance(AliEmcalJet *jet)
583 {   
584   if (jet != 0)
585     if (TMath::Abs(jet->Eta()) <= fBackgroundJetEtaWindow)
586       if (jet->Pt() >= fMinBackgroundJetPt)
587         return kTRUE;
588
589   return kFALSE;
590 }
591
592 //________________________________________________________________________
593 inline Bool_t AliAnalysisTaskChargedJetsPA::IsSignalJetInAcceptance(AliEmcalJet *jet)
594 {   
595   FillHistogram("hJetAcceptance", 0.5);
596   if (jet != 0)
597     if (TMath::Abs(jet->Eta()) <= fSignalJetEtaWindow)
598     {
599       FillHistogram("hJetAcceptance", 1.5);
600       if (jet->Pt() >= fMinJetPt)
601       {
602         FillHistogram("hJetAcceptance", 2.5);
603         if (jet->Area() >= fMinJetArea)
604         {
605           FillHistogram("hJetAcceptance", 3.5);
606           return kTRUE;
607         }
608       }
609     }
610   return kFALSE;
611 }
612
613 //________________________________________________________________________
614 inline Bool_t AliAnalysisTaskChargedJetsPA::IsDijet(AliEmcalJet *jet1, AliEmcalJet *jet2)
615 {   
616   // Output from GetDeltaPhi is < pi in any case
617   if ((jet1 != 0) && (jet2 != 0))
618     if((TMath::Pi() - GetDeltaPhi(jet1->Phi(),jet2->Phi())) < fDijetMaxAngleDeviation)
619       if ((jet1->Pt() > fMinDijetLeadingPt) && (jet2->Pt() > fMinDijetLeadingPt)) //TODO: Introduce recoil jet?
620         return kTRUE;
621
622   return kFALSE;
623 }
624
625 //________________________________________________________________________
626 void AliAnalysisTaskChargedJetsPA::ExecOnce()
627 {
628   #ifdef DEBUGMODE
629     AliInfo("Starting ExecOnce.");
630   #endif
631   fInitialized = kTRUE;
632
633   // Check for track array
634   if (strcmp(fTrackArrayName->Data(), "") != 0)
635   {
636     fTrackArray = dynamic_cast<TClonesArray*>(InputEvent()->FindListObject(fTrackArrayName->Data()));
637     fHasTracks = kTRUE;
638     if (!fTrackArray) 
639     {
640       AliWarning(Form("%s: Could not retrieve tracks %s! This is OK, if tracks are not demanded.", GetName(), fTrackArrayName->Data())); 
641       fHasTracks = kFALSE;
642     } 
643     else
644     {
645       TClass *cl = fTrackArray->GetClass();
646       if (!cl->GetBaseClass("AliVParticle"))
647       {
648         AliError(Form("%s: Collection %s does not contain AliVParticle objects!", GetName(), fTrackArrayName->Data())); 
649         fTrackArray = 0;
650         fHasTracks = kFALSE;
651       }
652     }
653   }
654
655   // Check for jet array
656   if (strcmp(fJetArrayName->Data(), "") != 0)
657   {
658     fJetArray = dynamic_cast<TClonesArray*>(InputEvent()->FindListObject(fJetArrayName->Data()));
659     fHasJets = kTRUE;
660
661     if (!fJetArray) 
662     {
663       AliWarning(Form("%s: Could not retrieve jets %s! This is OK, if jets are not demanded.", GetName(), fJetArrayName->Data())); 
664       fHasJets = kFALSE;
665     } 
666     else
667     {
668       if (!fJetArray->GetClass()->GetBaseClass("AliEmcalJet")) 
669       {
670         AliError(Form("%s: Collection %s does not contain AliEmcalJet objects!", GetName(), fJetArrayName->Data())); 
671         fJetArray = 0;
672         fHasJets = kFALSE;
673       }
674     }
675   }
676
677   // Check for background object
678   if (strcmp(fBackgroundJetArrayName->Data(), "") != 0)
679   {
680     fBackgroundJetArray = dynamic_cast<TClonesArray*>(InputEvent()->FindListObject(fBackgroundJetArrayName->Data()));
681     fHasBackgroundJets = kTRUE;
682     if (!fBackgroundJetArray)
683     {
684       AliInfo(Form("%s: Could not retrieve background jets %s! This is OK, if background is not demanded.", GetName(), fBackgroundJetArrayName->Data())); 
685       fHasBackgroundJets = kFALSE;
686     }
687   }
688
689   // Look, if initialization is OK
690   if (!fHasTracks && fAnalyzeBackground)
691   {
692     AliError(Form("%s: Tracks NOT successfully casted although demanded! Deactivating background analysis",GetName()));
693     fAnalyzeBackground = kFALSE;
694   }
695   if ((!fHasJets && fAnalyzeJets) || (!fHasJets && fAnalyzeBackground))
696   {
697     AliError(Form("%s: Jets NOT successfully casted although demanded!  Deactivating jet- and background analysis",GetName()));
698     fAnalyzeJets = kFALSE;
699     fAnalyzeBackground = kFALSE;
700   }
701   if (!fHasBackgroundJets && fAnalyzeBackground)
702   {
703     AliError(Form("%s: Background NOT successfully casted although demanded!  Deactivating background analysis",GetName()));
704     fAnalyzeBackground = kFALSE;
705   }
706
707   // Initialize helper class (for vertex selection & pile up correction)
708   fHelperClass = new AliAnalysisUtils();
709   fHelperClass->SetCutOnZVertexSPD(kFALSE);
710   // Histogram init
711   Init();
712
713   #ifdef DEBUGMODE
714     AliInfo("ExecOnce done.");
715   #endif
716
717 }
718
719 //________________________________________________________________________
720 void AliAnalysisTaskChargedJetsPA::GetSignalJets()
721 {
722   // Reset vars
723   fFirstLeadingJet = NULL;
724   fSecondLeadingJet = NULL;
725   fNumberSignalJets = 0;
726
727   TList tmpJets;
728   for (Int_t i = 0; i < fJetArray->GetEntries(); i++)
729   {
730     AliEmcalJet* jet = static_cast<AliEmcalJet*>(fJetArray->At(i));
731     if (!jet)
732     {
733       AliError(Form("%s: Could not receive jet %d", GetName(), i));
734       continue;
735     }
736     if (!IsSignalJetInAcceptance(jet))
737       continue;
738
739     for (Int_t j = 0; j <= tmpJets.GetEntries(); j++)
740     {
741       if (j>tmpJets.GetEntries()-1) // When passed last item add the jet at the end
742       {
743         tmpJets.Add(jet);
744         break;
745       }
746
747       AliEmcalJet* listJet = static_cast<AliEmcalJet*>(tmpJets.At(j));
748      
749       if(jet->Pt() < listJet->Pt()) // Insert jet before that one in list if pt smaller
750       {
751         tmpJets.AddAt(jet, j);
752         break;
753       }
754     }
755   }
756
757   for (Int_t i = 0; i < tmpJets.GetEntries(); i++)
758   {
759     AliEmcalJet* jet = static_cast<AliEmcalJet*>(tmpJets.At(i));
760     fSignalJets[fNumberSignalJets] = jet;
761     fNumberSignalJets++;
762   }
763   
764   if (fNumberSignalJets > 0)
765     fFirstLeadingJet  = static_cast<AliEmcalJet*>(tmpJets.At(0));
766   if (fNumberSignalJets > 1)
767     fSecondLeadingJet = static_cast<AliEmcalJet*>(tmpJets.At(1));
768
769 }
770
771 //________________________________________________________________________
772 Int_t AliAnalysisTaskChargedJetsPA::GetLeadingJets(TClonesArray* jetArray, Int_t* jetIDArray, Bool_t isSignalJets)
773 {
774 // Writes first two leading jets into already registered array jetIDArray
775
776   if (!jetArray)
777   {
778     AliError("Could not get the jet array to get leading jets from it!");
779     return 0;
780   }
781
782   Float_t maxJetPts[] = {0, 0};
783   jetIDArray[0] = -1;
784   jetIDArray[1] = -1;
785
786   Int_t jetCount = jetArray->GetEntries();
787   Int_t jetCountAccepted = 0;
788
789   for (Int_t i = 0; i < jetCount; i++)
790   {
791     AliEmcalJet* jet = static_cast<AliEmcalJet*>(jetArray->At(i));
792     if (!jet) 
793     {
794       AliError(Form("%s: Could not receive jet %d", GetName(), i));
795       continue;
796     }
797
798     if(isSignalJets)
799     {
800       if (!IsSignalJetInAcceptance(jet)) continue;
801     }
802     else
803     {
804       if (!IsBackgroundJetInAcceptance(jet)) continue;
805     }    
806
807     if (jet->Pt() > maxJetPts[0]) 
808     {
809       maxJetPts[1] = maxJetPts[0];
810       jetIDArray[1] = jetIDArray[0];
811       maxJetPts[0] = jet->Pt();
812       jetIDArray[0] = i;
813     }
814     else if (jet->Pt() > maxJetPts[1]) 
815     {
816       maxJetPts[1] = jet->Pt();
817       jetIDArray[1] = i;
818     }
819     jetCountAccepted++;
820   }
821   return jetCountAccepted;
822 }
823
824
825 //________________________________________________________________________
826 Double_t AliAnalysisTaskChargedJetsPA::GetCorrectedJetPt(AliEmcalJet* jet, Double_t background)
827 {
828   #ifdef DEBUGMODE
829     AliInfo("Getting corrected jet spectra.");
830   #endif
831
832   if(!jet)
833   {
834     AliError("Jet pointer passed to GetCorrectedJetPt() not valid!");
835     return -1.0;
836   }
837
838   Double_t correctedPt = -1.0;
839
840   // if the passed background is not valid, do not subtract it
841   if(background < 0)
842     background = 0;
843
844   // Subtract background
845   correctedPt = jet->Pt() - background * jet->Area();
846
847   #ifdef DEBUGMODE
848     AliInfo("Got corrected jet spectra.");
849   #endif 
850
851   return correctedPt;
852 }
853
854
855
856 //________________________________________________________________________
857 Double_t AliAnalysisTaskChargedJetsPA::GetDeltaPt(Double_t rho, Double_t leadingJetExclusionProbability)
858 {
859   #ifdef DEBUGMODE
860     AliInfo("Getting Delta Pt.");
861   #endif
862
863   // Define an invalid delta pt
864   Double_t deltaPt = -10000.0;
865
866   // Define eta range
867   Double_t etaMin, etaMax;
868   etaMin = -(fTrackEtaWindow-fRandConeRadius);
869   etaMax = +(fTrackEtaWindow-fRandConeRadius);
870
871   // Define random cone
872   Bool_t coneValid = kTRUE;
873   Double_t tmpRandConeEta = etaMin + fRandom->Rndm()*(etaMax-etaMin);
874   Double_t tmpRandConePhi = fRandom->Rndm()*TMath::TwoPi();
875
876   // if there is a jet, check for overlap if demanded
877   if(leadingJetExclusionProbability)
878   {
879     AliEmcalJet* tmpLeading = dynamic_cast<AliEmcalJet*>(fJetArray->At(0));
880     // Get leading jet (regardless of pT)
881     for (Int_t i = 1; i<fJetArray->GetEntries(); i++)
882     {
883       AliEmcalJet* tmpJet = static_cast<AliEmcalJet*>(fJetArray->At(i));
884       // if jet is in acceptance and higher, take as new leading
885       if (tmpJet)
886         if ((TMath::Abs(tmpJet->Eta()) <= fSignalJetEtaWindow) && (tmpJet->Area() >= fMinJetArea))
887           if((!tmpLeading) || (tmpJet->Pt() > tmpLeading->Pt()))
888             tmpLeading = tmpJet;
889     }
890     if(tmpLeading)
891     {
892       Double_t excludedJetPhi = tmpLeading->Phi();
893       Double_t excludedJetEta = tmpLeading->Eta();
894       Double_t tmpDeltaPhi = GetDeltaPhi(tmpRandConePhi, excludedJetPhi);
895
896       // Check, if cone has overlap with jet
897       if ( tmpDeltaPhi*tmpDeltaPhi + TMath::Abs(tmpRandConeEta-excludedJetEta)*TMath::Abs(tmpRandConeEta-excludedJetEta) <= fRandConeRadius*fRandConeRadius)
898       {
899         // Define probability to exclude the RC
900         Double_t probability = leadingJetExclusionProbability;
901
902         // Only exclude cone with a given probability
903         if (fRandom->Rndm()<=probability)
904           coneValid = kFALSE;
905       }
906     }
907   }
908
909
910   // Get the cones' pt and calculate delta pt
911   if (coneValid)
912     deltaPt = GetConePt(tmpRandConeEta,tmpRandConePhi,fRandConeRadius) - (rho*fRandConeRadius*fRandConeRadius*TMath::Pi());
913
914   return deltaPt;
915   #ifdef DEBUGMODE
916     AliInfo("Got Delta Pt.");
917   #endif
918 }
919
920 //________________________________________________________________________
921 void AliAnalysisTaskChargedJetsPA::GetKTBackgroundDensityAll(Int_t numberExcludeLeadingJets, Double_t& rhoPbPb, Double_t& rhoPbPbWithGhosts, Double_t& rhoCMS, Double_t& rhoImprovedCMS, Double_t& rhoMean, Double_t& rhoTrackLike)
922 {
923   #ifdef DEBUGMODE
924     AliInfo("Getting ALL KT background density.");
925   #endif
926
927   static Double_t tmpRhoPbPb[1024];
928   static Double_t tmpRhoPbPbWithGhosts[1024];
929   static Double_t tmpRhoMean[1024];
930   static Double_t tmpRhoCMS[1024];
931   static Double_t tmpRhoImprovedCMS[1024];
932   Double_t tmpCoveredArea = 0.0;
933   Double_t tmpSummedArea = 0.0;
934   Double_t tmpPtTrackLike = 0.0;
935   Double_t tmpAreaTrackLike = 0.0;
936
937   // Setting invalid values
938   rhoPbPb = 0.0;
939   rhoPbPbWithGhosts = 0.0;
940   rhoCMS = 0.0;
941   rhoImprovedCMS = 0.0;
942   rhoMean = 0.0;
943   rhoTrackLike = 0.0;
944
945   Int_t rhoPbPbJetCount = 0;
946   Int_t rhoPbPbWithGhostsJetCount = 0;
947   Int_t rhoCMSJetCount = 0;
948   Int_t rhoImprovedCMSJetCount = 0;
949   Int_t rhoMeanJetCount = 0;
950
951
952   // Find 2 leading KT jets for the original PbPb approach
953   Int_t leadingKTJets[]   = {-1, -1};
954   GetLeadingJets(fBackgroundJetArray, &leadingKTJets[0], kFALSE);
955
956   // Exclude UP TO numberExcludeLeadingJets
957   if(numberExcludeLeadingJets==-1)
958     numberExcludeLeadingJets = fNumberSignalJets;
959   if (fNumberSignalJets < numberExcludeLeadingJets)
960     numberExcludeLeadingJets = fNumberSignalJets;
961
962   for (Int_t i = 0; i < fBackgroundJetArray->GetEntries(); i++)
963   {
964     AliEmcalJet* backgroundJet = static_cast<AliEmcalJet*>(fBackgroundJetArray->At(i));
965
966     if (!backgroundJet)
967     {
968       AliError(Form("%s: Could not receive jet %d", GetName(), i));
969       continue;
970     } 
971
972     if (!IsBackgroundJetInAcceptance(backgroundJet))
973       continue;
974
975     // Search for overlap with signal jets
976     Bool_t isOverlapping = kFALSE;
977     for(Int_t j=0;j<numberExcludeLeadingJets;j++)
978     {
979       AliEmcalJet* signalJet = fSignalJets[j];
980      
981       if(signalJet->Pt() >= 5.0)
982         if(IsJetOverlapping(signalJet, backgroundJet))
983         {
984           isOverlapping = kTRUE;
985           break;
986         }
987     }
988
989     tmpSummedArea += backgroundJet->Area();
990     if(backgroundJet->Pt() > 0.150)
991       tmpCoveredArea += backgroundJet->Area();
992
993     Double_t tmpRho = 0.0;
994     if(backgroundJet->Area())
995       tmpRho = backgroundJet->Pt() / backgroundJet->Area();
996
997     // PbPb approach (take ghosts into account)
998     if ((i != leadingKTJets[0]) && (i != leadingKTJets[1]))
999     {
1000       tmpRhoPbPbWithGhosts[rhoPbPbWithGhostsJetCount] = tmpRho;
1001       rhoPbPbWithGhostsJetCount++;
1002     }
1003
1004     if(backgroundJet->Pt() > 0.150)
1005     {
1006       // CMS approach: don't take ghosts into acount
1007       tmpRhoCMS[rhoCMSJetCount] = tmpRho;
1008       rhoCMSJetCount++;
1009
1010       // Improved CMS approach: like CMS but excluding signal
1011       if(!isOverlapping)
1012       {
1013         tmpRhoImprovedCMS[rhoImprovedCMSJetCount] = tmpRho;
1014         rhoImprovedCMSJetCount++;
1015       }
1016
1017       // PbPb w/o ghosts approach (just neglect ghosts)
1018       if ((i != leadingKTJets[0]) && (i != leadingKTJets[1]))
1019       {  
1020         tmpRhoPbPb[rhoPbPbJetCount] = tmpRho;
1021         rhoPbPbJetCount++;
1022       }
1023     }
1024
1025     // (no overlap with signal jets)
1026     if(!isOverlapping)
1027     {
1028       // Mean approach
1029       tmpRhoMean[rhoMeanJetCount] = tmpRho;
1030       rhoMeanJetCount++;
1031       
1032       // Track like approach approach
1033       tmpPtTrackLike += backgroundJet->Pt();
1034       tmpAreaTrackLike += backgroundJet->Area();
1035     }
1036
1037   }
1038
1039   if (tmpAreaTrackLike > 0)
1040     rhoTrackLike = tmpPtTrackLike/tmpAreaTrackLike;
1041   if (rhoPbPbJetCount > 0)
1042     rhoPbPb = TMath::Median(rhoPbPbJetCount, tmpRhoPbPb);
1043   if (rhoPbPbWithGhostsJetCount > 0)
1044     rhoPbPbWithGhosts = TMath::Median(rhoPbPbWithGhostsJetCount, tmpRhoPbPbWithGhosts);
1045   if (rhoCMSJetCount > 0)
1046     rhoCMS = TMath::Median(rhoCMSJetCount, tmpRhoCMS) * tmpCoveredArea/tmpSummedArea;
1047   if (rhoImprovedCMSJetCount > 0)
1048   {
1049     rhoImprovedCMS = TMath::Median(rhoImprovedCMSJetCount, tmpRhoImprovedCMS) * tmpCoveredArea/tmpSummedArea;
1050   }
1051   if (rhoMeanJetCount > 0)
1052     rhoMean = TMath::Mean(rhoMeanJetCount, tmpRhoMean);
1053
1054   #ifdef DEBUGMODE
1055     AliInfo("Got ALL KT background density.");
1056   #endif
1057 }
1058
1059 //________________________________________________________________________
1060 void AliAnalysisTaskChargedJetsPA::GetKTBackgroundDensity(Int_t numberExcludeLeadingJets, Double_t& rhoImprovedCMS)
1061 {
1062   #ifdef DEBUGMODE
1063     AliInfo("Getting KT background density.");
1064   #endif
1065
1066   static Double_t tmpRhoImprovedCMS[1024];
1067   Double_t tmpCoveredArea = 0.0;
1068   Double_t tmpSummedArea = 0.0;
1069
1070   // Setting invalid values
1071   rhoImprovedCMS = 0.0;
1072
1073   Int_t rhoImprovedCMSJetCount = 0;
1074
1075   // Exclude UP TO numberExcludeLeadingJets
1076   if(numberExcludeLeadingJets==-1)
1077     numberExcludeLeadingJets = fNumberSignalJets;
1078   if (fNumberSignalJets < numberExcludeLeadingJets)
1079     numberExcludeLeadingJets = fNumberSignalJets;
1080
1081   for (Int_t i = 0; i < fBackgroundJetArray->GetEntries(); i++)
1082   {
1083     AliEmcalJet* backgroundJet = static_cast<AliEmcalJet*>(fBackgroundJetArray->At(i));
1084
1085     if (!backgroundJet)
1086     {
1087       AliError(Form("%s: Could not receive jet %d", GetName(), i));
1088       continue;
1089     } 
1090
1091     // Search for overlap with signal jets
1092     Bool_t isOverlapping = kFALSE;
1093     for(Int_t j=0;j<numberExcludeLeadingJets;j++)
1094     {
1095       AliEmcalJet* signalJet = fSignalJets[j];
1096       if(signalJet->Pt() >= 5.0)     
1097         if(IsJetOverlapping(signalJet, backgroundJet))
1098         {
1099           isOverlapping = kTRUE;
1100           break;
1101         }
1102     }
1103
1104     tmpSummedArea += backgroundJet->Area();
1105     if(backgroundJet->Pt() > 0.150)
1106       tmpCoveredArea += backgroundJet->Area();
1107
1108     if (!IsBackgroundJetInAcceptance(backgroundJet))
1109       continue;
1110
1111     Double_t tmpRho = backgroundJet->Pt() / backgroundJet->Area();
1112
1113     if(backgroundJet->Pt() > 0.150)
1114       if(!isOverlapping)
1115       {
1116         tmpRhoImprovedCMS[rhoImprovedCMSJetCount] = tmpRho;
1117         rhoImprovedCMSJetCount++;
1118       }
1119   }
1120
1121   if (rhoImprovedCMSJetCount > 0)
1122   {
1123     rhoImprovedCMS = TMath::Median(rhoImprovedCMSJetCount, tmpRhoImprovedCMS) * tmpCoveredArea/tmpSummedArea;
1124   }
1125   #ifdef DEBUGMODE
1126     AliInfo("Got KT background density.");
1127   #endif
1128 }
1129
1130
1131 //________________________________________________________________________
1132 void AliAnalysisTaskChargedJetsPA::GetTRBackgroundDensity(Int_t numberExcludeLeadingJets, Double_t& rhoNoExclusion, Double_t& rhoConeExclusion02, Double_t& rhoConeExclusion04, Double_t& rhoConeExclusion06, Double_t& rhoConeExclusion08, Double_t& rhoExactExclusion)
1133 {
1134   #ifdef DEBUGMODE
1135     AliInfo("Getting TR background density.");
1136   #endif
1137
1138   Double_t summedTracksPtCone04 = 0.0;
1139   Double_t summedTracksPtCone02 = 0.0;
1140   Double_t summedTracksPtCone06 = 0.0;
1141   Double_t summedTracksPtCone08 = 0.0;
1142   Double_t summedTracksPtWithinJets = 0.0;
1143   Double_t summedTracksPt = 0.0;
1144   
1145   // Setting invalid values
1146   rhoNoExclusion = 0.0;
1147   rhoConeExclusion02 = 0.0;
1148   rhoConeExclusion04 = 0.0;
1149   rhoConeExclusion06 = 0.0;
1150   rhoConeExclusion08 = 0.0; 
1151   rhoExactExclusion  = 0.0;
1152
1153   // Exclude UP TO numberExcludeLeadingJets
1154   if(numberExcludeLeadingJets==-1)
1155     numberExcludeLeadingJets = fNumberSignalJets;
1156   if (fNumberSignalJets < numberExcludeLeadingJets)
1157     numberExcludeLeadingJets = fNumberSignalJets;
1158
1159   Int_t fSignalJetCount5GeV = 0;
1160   for(Int_t j=0;j<numberExcludeLeadingJets;j++)
1161   {
1162     AliEmcalJet* signalJet = fSignalJets[j];
1163     if(signalJet->Pt() < 5.0)
1164       continue;
1165     fSignalJetCount5GeV++;
1166   }
1167
1168   for (Int_t i = 0; i < fTrackArray->GetEntries(); i++)
1169   {
1170     AliVTrack* tmpTrack = static_cast<AliVTrack*>(fTrackArray->At(i));
1171     Bool_t trackWithinJet = kFALSE; Bool_t trackWithin02Cone = kFALSE; Bool_t trackWithin04Cone = kFALSE; Bool_t trackWithin06Cone = kFALSE; Bool_t trackWithin08Cone = kFALSE;
1172
1173     if (IsTrackInAcceptance(tmpTrack))
1174     {
1175       // Check if tracks overlaps with jet
1176       for(Int_t j=0;j<numberExcludeLeadingJets;j++)
1177       {
1178         AliEmcalJet* signalJet = fSignalJets[j];
1179
1180         if(signalJet->Pt() < 5.0)
1181           continue;
1182
1183         // Exact jet exclusion
1184         if (IsTrackInJet(signalJet, i))
1185           trackWithinJet = kTRUE;
1186
1187         // Cone exclusions
1188         if (IsTrackInCone(tmpTrack, signalJet->Eta(), signalJet->Phi(), 0.2))
1189         {
1190           trackWithin02Cone = kTRUE;
1191           trackWithin04Cone = kTRUE;
1192           trackWithin06Cone = kTRUE;
1193           trackWithin08Cone = kTRUE;
1194           break;
1195         }
1196         else if (IsTrackInCone(tmpTrack, signalJet->Eta(), signalJet->Phi(), 0.4))
1197         {
1198           trackWithin04Cone = kTRUE;
1199           trackWithin06Cone = kTRUE;
1200           trackWithin08Cone = kTRUE;
1201         }
1202         else if (IsTrackInCone(tmpTrack, signalJet->Eta(), signalJet->Phi(), 0.6))
1203         {
1204           trackWithin06Cone = kTRUE;
1205           trackWithin08Cone = kTRUE;
1206         }
1207         else if (IsTrackInCone(tmpTrack, signalJet->Eta(), signalJet->Phi(), 0.8))
1208         {
1209           trackWithin08Cone = kTRUE;
1210         }
1211       }
1212
1213       if(!trackWithin08Cone)
1214       {
1215         summedTracksPtCone08 += tmpTrack->Pt();
1216       }
1217       if(!trackWithin06Cone)
1218       {
1219         summedTracksPtCone06 += tmpTrack->Pt();
1220       }
1221       if(!trackWithin04Cone)
1222       {
1223         summedTracksPtCone04 += tmpTrack->Pt();
1224       }
1225       if(!trackWithin02Cone)
1226       {
1227         summedTracksPtCone02 += tmpTrack->Pt();
1228       }
1229       if(!trackWithinJet)
1230       {
1231         summedTracksPtWithinJets += tmpTrack->Pt();
1232       }
1233       summedTracksPt += tmpTrack->Pt();
1234
1235     }
1236   }
1237
1238   // Calculate the correct area where the tracks were taking from
1239
1240   Double_t tmpFullTPCArea = (2.0*fTrackEtaWindow) * TMath::TwoPi();
1241   Double_t tmpAreaCone02     = tmpFullTPCArea;
1242   Double_t tmpAreaCone04     = tmpFullTPCArea;
1243   Double_t tmpAreaCone06     = tmpFullTPCArea;
1244   Double_t tmpAreaCone08     = tmpFullTPCArea;
1245   Double_t tmpAreaWithinJets = tmpFullTPCArea;
1246   std::vector<Double_t> tmpEtas(fSignalJetCount5GeV);
1247   std::vector<Double_t> tmpPhis(fSignalJetCount5GeV);
1248
1249   Int_t iSignal = 0;
1250   for(Int_t i=0;i<numberExcludeLeadingJets;i++)
1251   {
1252     AliEmcalJet* tmpJet = fSignalJets[i];
1253
1254     if(tmpJet->Pt() < 5.0)
1255       continue;
1256
1257     tmpEtas[iSignal] = tmpJet->Eta();
1258     tmpPhis[iSignal] = tmpJet->Phi();
1259     tmpAreaWithinJets -= tmpJet->Area();
1260
1261     iSignal++;
1262   }
1263
1264   tmpAreaCone02 -= tmpFullTPCArea * MCGetOverlapMultipleCirclesRectancle(fSignalJetCount5GeV, tmpEtas, tmpPhis, 0.2, -fTrackEtaWindow, +fTrackEtaWindow, 0., TMath::TwoPi());
1265   tmpAreaCone04 -= tmpFullTPCArea * MCGetOverlapMultipleCirclesRectancle(fSignalJetCount5GeV, tmpEtas, tmpPhis, 0.4, -fTrackEtaWindow, +fTrackEtaWindow, 0., TMath::TwoPi());
1266   tmpAreaCone06 -= tmpFullTPCArea * MCGetOverlapMultipleCirclesRectancle(fSignalJetCount5GeV, tmpEtas, tmpPhis, 0.6, -fTrackEtaWindow, +fTrackEtaWindow, 0., TMath::TwoPi());
1267   tmpAreaCone08 -= tmpFullTPCArea * MCGetOverlapMultipleCirclesRectancle(fSignalJetCount5GeV, tmpEtas, tmpPhis, 0.8, -fTrackEtaWindow, +fTrackEtaWindow, 0., TMath::TwoPi());
1268  
1269   rhoConeExclusion02 = summedTracksPtCone02/tmpAreaCone02;
1270   rhoConeExclusion04 = summedTracksPtCone04/tmpAreaCone04;
1271   rhoConeExclusion06 = summedTracksPtCone06/tmpAreaCone06;
1272   rhoConeExclusion08 = summedTracksPtCone08/tmpAreaCone08;
1273   rhoExactExclusion  = summedTracksPtWithinJets/tmpAreaWithinJets;
1274   rhoNoExclusion     = summedTracksPt/tmpFullTPCArea;
1275
1276
1277   #ifdef DEBUGMODE
1278     AliInfo("Got TR background density.");
1279   #endif
1280 }
1281
1282 //________________________________________________________________________
1283 void AliAnalysisTaskChargedJetsPA::GetTRBackgroundDensity(Int_t numberExcludeLeadingJets, Double_t& rhoMean, Double_t& area, AliEmcalJet* excludeJet1, AliEmcalJet* excludeJet2, Bool_t doSearchPerpendicular)
1284 {
1285   #ifdef DEBUGMODE
1286     AliInfo("Getting TR background density.");
1287   #endif
1288
1289   // Setting invalid values
1290   Double_t summedTracksPt = 0.0;
1291   rhoMean = 0.0;
1292   area = -1.0;
1293
1294   Double_t tmpRadius = 0.0;
1295   if (doSearchPerpendicular)
1296     tmpRadius = 0.4*TMath::Pi(); // exclude 90 degrees around jets
1297   else
1298     tmpRadius = 0.8;
1299     
1300   numberExcludeLeadingJets = 2; // dijet is excluded here in any case
1301
1302
1303
1304   if (!fTrackArray || !fJetArray)
1305   {
1306     AliError("Could not get the track/jet array to calculate track rho!");
1307     return;
1308   }
1309
1310   Int_t   trackCount = fTrackArray->GetEntries();
1311   Int_t   trackCountAccepted = 0;
1312   for (Int_t i = 0; i < trackCount; i++)
1313   {
1314     AliVTrack* tmpTrack = static_cast<AliVTrack*>(fTrackArray->At(i));
1315     if (IsTrackInAcceptance(tmpTrack))
1316     {
1317       if (IsTrackInCone(tmpTrack, excludeJet1->Eta(), excludeJet1->Phi(), tmpRadius))
1318         continue;
1319
1320       if (numberExcludeLeadingJets > 1)
1321         if (IsTrackInCone(tmpTrack, excludeJet2->Eta(), excludeJet2->Phi(), tmpRadius))
1322           continue;
1323
1324         // Add track pt to array
1325         summedTracksPt = summedTracksPt + tmpTrack->Pt();
1326         trackCountAccepted++;
1327     }
1328   }
1329
1330   if (trackCountAccepted > 0)
1331   {
1332     Double_t tmpArea = 2.0*fTrackEtaWindow*TMath::TwoPi()  - 2*(tmpRadius*tmpRadius * TMath::Pi()); //TPC area - excluding jet area
1333     rhoMean = summedTracksPt/tmpArea;
1334     area = tmpArea;
1335   }
1336
1337   #ifdef DEBUGMODE
1338     AliInfo("Got TR background density.");
1339   #endif
1340 }
1341
1342 //________________________________________________________________________
1343 void AliAnalysisTaskChargedJetsPA::GetPPBackgroundDensity(Double_t& background, AliEmcalJet* jet)
1344 {
1345   // This is the background that was used for the pp 7 TeV ALICE paper
1346   Double_t jetMom[3] = { jet->Px(), jet->Py(), jet->Pz() };
1347   TVector3 jet3mom1(jetMom);
1348   TVector3 jet3mom2(jetMom);
1349   background = 0;
1350
1351   jet3mom1.RotateZ(TMath::Pi());
1352   jet3mom2.RotateZ(-TMath::Pi());
1353
1354   for (int i = 0; i < fTrackArray->GetEntries(); i++)
1355   {
1356     AliVTrack* track = static_cast<AliVTrack*>(fTrackArray->At(i));
1357
1358     Double_t trackMom[3] = { track->Px(), track->Py(), track->Pz() };
1359     TVector3 track3mom(trackMom);
1360
1361     Double_t dR1 = jet3mom1.DeltaR(track3mom);
1362     Double_t dR2 = jet3mom2.DeltaR(track3mom);
1363
1364     if (dR1 <= fSignalJetRadius || dR2 <= fSignalJetRadius)
1365       background += track3mom.Pt();
1366   }
1367
1368   background /= (2 * TMath::Pi() * fSignalJetRadius * fSignalJetRadius);
1369 }
1370
1371 //________________________________________________________________________
1372 void AliAnalysisTaskChargedJetsPA::Calculate(AliVEvent* event)
1373 {
1374   #ifdef DEBUGMODE
1375     AliInfo("Starting Calculate().");
1376   #endif
1377   ////////////////////// NOTE: initialization & casting
1378
1379   fEventCounter++;
1380
1381   // Check, if analysis should be done in pt hard bins
1382   if(fUsePtHardBin != -1)
1383     if(GetPtHardBin() != fUsePtHardBin)
1384       return;
1385
1386   // This is to take only every Nth event
1387   if((fEventCounter+fPartialAnalysisIndex) % fPartialAnalysisNParts != 0)
1388     return;
1389
1390   FillHistogram("hNumberEvents",0.5);
1391
1392   if(!IsEventInAcceptance(event))
1393     return;
1394
1395   FillHistogram("hNumberEvents",1.5);
1396
1397   #ifdef DEBUGMODE
1398     AliInfo("Calculate()::Init done.");
1399   #endif
1400
1401   ////////////////////// NOTE: Get Centrality, (Leading)Signal jets and Background
1402
1403   // Get centrality
1404   AliCentrality* tmpCentrality = NULL;
1405   tmpCentrality = event->GetCentrality();
1406   Double_t centralityPercentile = -1.0;
1407   Double_t centralityPercentileV0A = 0.0;
1408   Double_t centralityPercentileV0C = 0.0;
1409   Double_t centralityPercentileV0M = 0.0;
1410   if (tmpCentrality != NULL)
1411   {
1412     centralityPercentile = tmpCentrality->GetCentralityPercentile(fCentralityType.Data());
1413     centralityPercentileV0A = tmpCentrality->GetCentralityPercentile("V0A");
1414     centralityPercentileV0C = tmpCentrality->GetCentralityPercentile("V0C");
1415     centralityPercentileV0M = tmpCentrality->GetCentralityPercentile("V0M");
1416   }
1417
1418   if((centralityPercentile < 0.0) || (centralityPercentile > 100.0))
1419     AliWarning(Form("Centrality value not valid (c=%E)",centralityPercentile)); 
1420
1421   if(fSetCentralityToOne)
1422     centralityPercentile = 1.0;
1423
1424   // Get jets
1425   if (fAnalyzeBackground || fAnalyzeJets)
1426     GetSignalJets();
1427
1428   // Get background estimates
1429   Double_t              backgroundKTImprovedCMS = -1.0;
1430   Double_t              backgroundKTImprovedCMSExternal = -1.0;
1431   Double_t              backgroundDijet = -1.0;
1432   Double_t              backgroundDijetPerpendicular = -1.0;
1433   Double_t              deltaPtDijetPerpendicular = -10000.0;
1434   Double_t              backgroundKTPbPb = -1.0;
1435   Double_t              backgroundKTPbPbWithGhosts = -1.0;
1436   Double_t              backgroundKTCMS = -1.0;
1437   Double_t              backgroundKTMean = -1.0;
1438   Double_t              backgroundKTTrackLike = -1.0;
1439   Double_t              backgroundTRNoExcl = -1.0;
1440   Double_t              backgroundTRCone02 = -1.0;
1441   Double_t              backgroundTRCone04 = -1.0;
1442   Double_t              backgroundTRCone06 = -1.0;
1443   Double_t              backgroundTRCone08 = -1.0;
1444   Double_t              backgroundTRExact  = -1.0;
1445   Double_t              backgroundPP       = -1.0;
1446   // Calculate background for different jet exclusions
1447
1448   if (fAnalyzeBackground)
1449   {
1450
1451     if(fAnalyzeDeprecatedBackgrounds)
1452       GetKTBackgroundDensityAll    (fNumberExcludedJets, backgroundKTPbPb, backgroundKTPbPbWithGhosts, backgroundKTCMS, backgroundKTImprovedCMS, backgroundKTMean, backgroundKTTrackLike);
1453     else
1454       GetKTBackgroundDensity       (fNumberExcludedJets, backgroundKTImprovedCMS);
1455
1456     if(fAnalyzeDeprecatedBackgrounds)
1457       GetTRBackgroundDensity    (fNumberExcludedJets, backgroundTRNoExcl, backgroundTRCone02, backgroundTRCone04, backgroundTRCone06, backgroundTRCone08, backgroundTRExact);
1458
1459     backgroundKTImprovedCMSExternal = GetExternalRho();
1460   }
1461
1462   #ifdef DEBUGMODE
1463     AliInfo("Calculate()::Centrality&SignalJets&Background-Calculation done.");
1464   #endif
1465
1466   if (fAnalyzeQA)
1467   {
1468     FillHistogram("hVertexX",event->GetPrimaryVertex()->GetX());
1469     FillHistogram("hVertexY",event->GetPrimaryVertex()->GetY());
1470     FillHistogram("hVertexXY",event->GetPrimaryVertex()->GetX(), event->GetPrimaryVertex()->GetY());
1471     FillHistogram("hVertexR",TMath::Sqrt(event->GetPrimaryVertex()->GetX()*event->GetPrimaryVertex()->GetX() + event->GetPrimaryVertex()->GetY()*event->GetPrimaryVertex()->GetY()));
1472     FillHistogram("hCentralityV0M",centralityPercentileV0M);
1473     FillHistogram("hCentralityV0A",centralityPercentileV0A);
1474     FillHistogram("hCentralityV0C",centralityPercentileV0C);
1475     FillHistogram("hCentrality",centralityPercentile);
1476
1477     Int_t trackCountAcc = 0;
1478     Int_t nTracks = fTrackArray->GetEntries();
1479     for (Int_t i = 0; i < nTracks; i++)
1480     {
1481       AliVTrack* track = static_cast<AliVTrack*>(fTrackArray->At(i));
1482
1483       if (track != 0)
1484         if (track->Pt() >= fMinTrackPt)
1485           FillHistogram("hTrackPhiEta", track->Phi(),track->Eta(), 1);
1486
1487       if (IsTrackInAcceptance(track))
1488       {
1489         FillHistogram("hTrackPt", track->Pt(), centralityPercentile);
1490         if(track->Eta() >= 0)
1491           FillHistogram("hTrackPtPosEta", track->Pt(), centralityPercentile);
1492         else
1493           FillHistogram("hTrackPtNegEta", track->Pt(), centralityPercentile);
1494                 
1495         FillHistogram("hTrackEta", track->Eta(), centralityPercentile);
1496         FillHistogram("hTrackPhi", track->Phi());
1497         
1498         if(static_cast<AliPicoTrack*>(track))
1499         {
1500           FillHistogram("hTrackPhiTrackType", track->Phi(), (static_cast<AliPicoTrack*>(track))->GetTrackType());
1501           FillHistogram("hTrackPhiLabel", track->Phi(), (static_cast<AliPicoTrack*>(track))->GetLabel());
1502         }
1503         for(Int_t j=0;j<20;j++)
1504           if(track->Pt() > j)
1505             FillHistogram("hTrackPhiPtCut", track->Phi(), track->Pt());
1506
1507         FillHistogram("hTrackCharge", track->Charge());
1508         trackCountAcc++;
1509       }
1510     }
1511     FillHistogram("hTrackCountAcc", trackCountAcc, centralityPercentile);
1512
1513
1514 /*
1515     // This is code that is run locally to get some special events
1516     TFile* fileOutput = new TFile("SpecialEvents.root", "UPDATE");
1517     if(fSecondLeadingJet&&(fSecondLeadingJet->Pt()>10.))
1518     {
1519       cout << "Event found\n";
1520       TH2D* tmpEvent = new TH2D(Form("Event%lu", fEventCounter), "", 40, -0.9, 0.9, 40, 0., TMath::TwoPi());
1521       tmpEvent->GetXaxis()->SetTitle("#eta");
1522       tmpEvent->GetYaxis()->SetTitle("#phi");
1523       tmpEvent->SetOption("LEGO2");
1524       tmpEvent->Sumw2();
1525
1526       for (Int_t i = 0; i < nTracks; i++)
1527       {
1528         AliVTrack* track = static_cast<AliVTrack*>(fTrackArray->At(i));
1529
1530         if (IsTrackInAcceptance(track))
1531         {
1532           tmpEvent->Fill(track->Eta(), track->Phi(), track->Pt());
1533         }
1534       }
1535       tmpEvent->Write(0, kOverwrite);
1536     }
1537     fileOutput->Close();
1538 */
1539   }
1540   #ifdef DEBUGMODE
1541     AliInfo("Calculate()::QA done.");
1542   #endif
1543
1544   ////////////////////// NOTE: Jet analysis and calculations
1545
1546   if (fAnalyzeJets)
1547   {
1548     for (Int_t i = 0; i<fJetArray->GetEntries(); i++)
1549     {
1550       AliEmcalJet* tmpJet = static_cast<AliEmcalJet*>(fJetArray->At(i));
1551       if (!tmpJet)
1552         continue;
1553
1554       FillHistogram("hRawJetPt", tmpJet->Pt());
1555       if (tmpJet->Pt() >= 5.0)
1556       {
1557       // ### RAW JET ANALYSIS
1558         if (tmpJet->Area() >= fMinJetArea)
1559           FillHistogram("hRawJetPhiEta", tmpJet->Phi(), tmpJet->Eta());
1560         if (TMath::Abs(tmpJet->Eta()) <= fSignalJetEtaWindow)
1561           FillHistogram("hRawJetArea", tmpJet->Area());
1562       }
1563
1564       if(IsSignalJetInAcceptance(tmpJet))
1565       {
1566       // ### SIGNAL JET ANALYSIS
1567         // Jet spectra
1568         FillHistogram("hJetPt", tmpJet->Pt(), centralityPercentile);
1569         FillHistogram("hJetPtBgrdSubtractedKTImprovedCMS", GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS), centralityPercentile);
1570         FillHistogram("hJetPtSubtractedRhoKTImprovedCMS", tmpJet->Pt(), centralityPercentile, backgroundKTImprovedCMS);
1571         if(centralityPercentile<=20.0)
1572           FillHistogram("hJetPtSubtractedRhoKTImprovedCMS020", tmpJet->Pt(), backgroundKTImprovedCMS);
1573         
1574         // pp background
1575         GetPPBackgroundDensity(backgroundPP, tmpJet);
1576         FillHistogram("hJetPtBgrdSubtractedPP", GetCorrectedJetPt(tmpJet, backgroundPP), centralityPercentile);
1577
1578         if(fAnalyzeDeprecatedBackgrounds)
1579         {
1580           FillHistogram("hJetPtBgrdSubtractedTR", GetCorrectedJetPt(tmpJet, backgroundTRCone06), centralityPercentile);
1581           FillHistogram("hJetPtBgrdSubtractedKTPbPb", GetCorrectedJetPt(tmpJet, backgroundKTPbPb), centralityPercentile);
1582           FillHistogram("hJetPtBgrdSubtractedKTPbPbWithGhosts", GetCorrectedJetPt(tmpJet, backgroundKTPbPbWithGhosts), centralityPercentile);
1583           FillHistogram("hJetPtBgrdSubtractedKTCMS", GetCorrectedJetPt(tmpJet, backgroundKTCMS), centralityPercentile);
1584           FillHistogram("hJetPtBgrdSubtractedKTMean", GetCorrectedJetPt(tmpJet, backgroundKTMean), centralityPercentile);
1585           FillHistogram("hJetPtBgrdSubtractedKTTrackLike", GetCorrectedJetPt(tmpJet, backgroundKTTrackLike), centralityPercentile);
1586         }
1587
1588         // Jet profile analysis
1589         if(TMath::Abs(tmpJet->Eta()) <= 0.3)
1590         {
1591           if(tmpJet->Pt()>=70.0)
1592           {
1593             FillHistogram("hJetProfile70GeV", 0.05-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.05))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1594             FillHistogram("hJetProfile70GeV", 0.10-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.10))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1595             FillHistogram("hJetProfile70GeV", 0.15-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.15))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1596             FillHistogram("hJetProfile70GeV", 0.20-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.20))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1597             FillHistogram("hJetProfile70GeV", 0.25-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.25))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1598             FillHistogram("hJetProfile70GeV", 0.30-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.30))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1599             FillHistogram("hJetProfile70GeV", 0.35-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.35))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1600             FillHistogram("hJetProfile70GeV", 0.40-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.40))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1601             FillHistogram("hJetProfile70GeV", 0.45-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.45))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1602             FillHistogram("hJetProfile70GeV", 0.50-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.50))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1603             FillHistogram("hJetProfile70GeV", 0.55-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.55))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1604             FillHistogram("hJetProfile70GeV", 0.60-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.60))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1605           }
1606           else if(GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS)>=60.0)
1607           {
1608             FillHistogram("hJetProfile60GeV", 0.05-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.05))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1609             FillHistogram("hJetProfile60GeV", 0.10-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.10))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1610             FillHistogram("hJetProfile60GeV", 0.15-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.15))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1611             FillHistogram("hJetProfile60GeV", 0.20-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.20))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1612             FillHistogram("hJetProfile60GeV", 0.25-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.25))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1613             FillHistogram("hJetProfile60GeV", 0.30-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.30))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1614             FillHistogram("hJetProfile60GeV", 0.35-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.35))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1615             FillHistogram("hJetProfile60GeV", 0.40-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.40))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1616             FillHistogram("hJetProfile60GeV", 0.45-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.45))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1617             FillHistogram("hJetProfile60GeV", 0.50-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.50))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1618             FillHistogram("hJetProfile60GeV", 0.55-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.55))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1619             FillHistogram("hJetProfile60GeV", 0.60-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.60))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1620           }
1621           else if(GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS)>=50.0)
1622           {
1623             FillHistogram("hJetProfile50GeV", 0.05-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.05))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1624             FillHistogram("hJetProfile50GeV", 0.10-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.10))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1625             FillHistogram("hJetProfile50GeV", 0.15-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.15))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1626             FillHistogram("hJetProfile50GeV", 0.20-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.20))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1627             FillHistogram("hJetProfile50GeV", 0.25-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.25))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1628             FillHistogram("hJetProfile50GeV", 0.30-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.30))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1629             FillHistogram("hJetProfile50GeV", 0.35-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.35))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1630             FillHistogram("hJetProfile50GeV", 0.40-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.40))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1631             FillHistogram("hJetProfile50GeV", 0.45-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.45))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1632             FillHistogram("hJetProfile50GeV", 0.50-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.50))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1633             FillHistogram("hJetProfile50GeV", 0.55-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.55))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1634             FillHistogram("hJetProfile50GeV", 0.60-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.60))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1635           }
1636           else if(GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS)>=40.0)
1637           {
1638             FillHistogram("hJetProfile40GeV", 0.05-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.05))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1639             FillHistogram("hJetProfile40GeV", 0.10-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.10))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1640             FillHistogram("hJetProfile40GeV", 0.15-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.15))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1641             FillHistogram("hJetProfile40GeV", 0.20-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.20))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1642             FillHistogram("hJetProfile40GeV", 0.25-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.25))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1643             FillHistogram("hJetProfile40GeV", 0.30-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.30))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1644             FillHistogram("hJetProfile40GeV", 0.35-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.35))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1645             FillHistogram("hJetProfile40GeV", 0.40-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.40))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1646             FillHistogram("hJetProfile40GeV", 0.45-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.45))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1647             FillHistogram("hJetProfile40GeV", 0.50-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.50))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1648             FillHistogram("hJetProfile40GeV", 0.55-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.55))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1649             FillHistogram("hJetProfile40GeV", 0.60-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.60))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1650           }
1651           else if(GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS)>=30.0)
1652           {
1653             FillHistogram("hJetProfile30GeV", 0.05-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.05))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1654             FillHistogram("hJetProfile30GeV", 0.10-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.10))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1655             FillHistogram("hJetProfile30GeV", 0.15-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.15))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1656             FillHistogram("hJetProfile30GeV", 0.20-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.20))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1657             FillHistogram("hJetProfile30GeV", 0.25-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.25))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1658             FillHistogram("hJetProfile30GeV", 0.30-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.30))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1659             FillHistogram("hJetProfile30GeV", 0.35-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.35))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1660             FillHistogram("hJetProfile30GeV", 0.40-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.40))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1661             FillHistogram("hJetProfile30GeV", 0.45-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.45))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1662             FillHistogram("hJetProfile30GeV", 0.50-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.50))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1663             FillHistogram("hJetProfile30GeV", 0.55-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.55))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1664             FillHistogram("hJetProfile30GeV", 0.60-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.60))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1665           }
1666           else if(GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS)>=20.0)
1667           {
1668             FillHistogram("hJetProfile20GeV", 0.05-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.05))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1669             FillHistogram("hJetProfile20GeV", 0.10-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.10))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1670             FillHistogram("hJetProfile20GeV", 0.15-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.15))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1671             FillHistogram("hJetProfile20GeV", 0.20-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.20))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1672             FillHistogram("hJetProfile20GeV", 0.25-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.25))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1673             FillHistogram("hJetProfile20GeV", 0.30-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.30))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1674             FillHistogram("hJetProfile20GeV", 0.35-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.35))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1675             FillHistogram("hJetProfile20GeV", 0.40-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.40))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1676             FillHistogram("hJetProfile20GeV", 0.45-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.45))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1677             FillHistogram("hJetProfile20GeV", 0.50-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.50))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1678             FillHistogram("hJetProfile20GeV", 0.55-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.55))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1679             FillHistogram("hJetProfile20GeV", 0.60-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.60))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1680           }
1681           else if(GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS)>=10.0)
1682           {
1683             FillHistogram("hJetProfile10GeV", 0.05-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.05))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1684             FillHistogram("hJetProfile10GeV", 0.10-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.10))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1685             FillHistogram("hJetProfile10GeV", 0.15-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.15))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1686             FillHistogram("hJetProfile10GeV", 0.20-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.20))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1687             FillHistogram("hJetProfile10GeV", 0.25-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.25))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1688             FillHistogram("hJetProfile10GeV", 0.30-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.30))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1689             FillHistogram("hJetProfile10GeV", 0.35-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.35))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1690             FillHistogram("hJetProfile10GeV", 0.40-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.40))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1691             FillHistogram("hJetProfile10GeV", 0.45-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.45))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1692             FillHistogram("hJetProfile10GeV", 0.50-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.50))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1693             FillHistogram("hJetProfile10GeV", 0.55-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.55))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1694             FillHistogram("hJetProfile10GeV", 0.60-0.05/2, (-backgroundKTImprovedCMS+GetConePt(tmpJet->Eta(), tmpJet->Phi(), 0.60))/GetCorrectedJetPt(tmpJet, backgroundKTImprovedCMS));
1695           }
1696         }
1697         FillHistogram("hJetPtVsConstituentCount", tmpJet->Pt(),tmpJet->GetNumberOfTracks());
1698
1699         if((fAnalyzeQA) && (tmpJet->Pt() >= 5.0))
1700         {
1701           Double_t lowestTrackPt = 1e99;
1702           Double_t highestTrackPt = 0.0;
1703           for(Int_t j=0; j<tmpJet->GetNumberOfTracks(); j++)
1704           {
1705             FillHistogram("hJetConstituentPt", tmpJet->TrackAt(j, fTrackArray)->Pt(), centralityPercentile);
1706             // Find the lowest pT of a track in the jet
1707             if (tmpJet->TrackAt(j, fTrackArray)->Pt() < lowestTrackPt)
1708               lowestTrackPt = tmpJet->TrackAt(j, fTrackArray)->Pt();
1709             if (tmpJet->TrackAt(j, fTrackArray)->Pt() > highestTrackPt)
1710               highestTrackPt = tmpJet->TrackAt(j, fTrackArray)->Pt();
1711           }
1712           FillHistogram("hJetArea", tmpJet->Area());
1713           // Signal jet vs. signal jet - "Combinatorial"
1714           for (Int_t j = i+1; j<fNumberSignalJets; j++)
1715             FillHistogram("hJetDeltaPhi", GetDeltaPhi(tmpJet->Phi(), fSignalJets[j]->Phi()));
1716
1717           FillHistogram("hJetPhiEta", tmpJet->Phi(),tmpJet->Eta());
1718           FillHistogram("hJetEta", tmpJet->Eta(), centralityPercentile);
1719
1720           if(lowestTrackPt>=2.0)
1721             FillHistogram("hJetEta2GeVTracks", tmpJet->Eta(), centralityPercentile);
1722           if(lowestTrackPt>=4.0)
1723             FillHistogram("hJetEta4GeVTracks", tmpJet->Eta(), centralityPercentile);
1724         }
1725
1726
1727         // Jet constituent mass analyses (for PYTHIA)
1728         if (fAnalyzeMassCorrelation)
1729         {
1730           Double_t jetMass = 0.0;
1731           Int_t constituentCount = tmpJet->GetNumberOfTracks();
1732           Int_t electronCount = 0;
1733           Int_t pionCount = 0;
1734           Int_t protonCount = 0;
1735           Int_t kaonCount = 0;
1736           Int_t othersCount = 0;
1737
1738           for(Int_t j=0; j<tmpJet->GetNumberOfTracks(); j++)
1739           {
1740             AliVParticle* tmpParticle = tmpJet->TrackAt(j, fTrackArray);
1741
1742             jetMass += tmpParticle->M();
1743             if(TMath::Abs(tmpParticle->PdgCode()) == 11) // electron, positron
1744               electronCount++;
1745             else if(TMath::Abs(tmpParticle->PdgCode()) == 211) // p-, p+
1746               pionCount++;
1747             else if(TMath::Abs(tmpParticle->PdgCode()) == 2212) // p, pbar
1748               protonCount++;
1749             else if(TMath::Abs(tmpParticle->PdgCode()) == 321) // kaon+,kaon-
1750               kaonCount++;
1751             else
1752               othersCount++;
1753           }
1754
1755           FillHistogram("hJetMassFromConstituents", jetMass);
1756           FillHistogram("hJetMass", tmpJet->M());
1757
1758           FillHistogram("hJetPtVsMass", tmpJet->Pt(), jetMass);
1759           FillHistogram("hJetPtVsJetMass", tmpJet->Pt(), tmpJet->M());
1760
1761           FillHistogram("hJetPtVsProtonCount", tmpJet->Pt(), protonCount/(static_cast<Double_t>(constituentCount)));
1762           FillHistogram("hJetPtVsPionCount", tmpJet->Pt(), pionCount/(static_cast<Double_t>(constituentCount)));
1763           FillHistogram("hJetPtVsKaonCount", tmpJet->Pt(), kaonCount/(static_cast<Double_t>(constituentCount)));
1764           FillHistogram("hJetPtVsElectronCount", tmpJet->Pt(), electronCount/(static_cast<Double_t>(constituentCount)));
1765           FillHistogram("hJetPtVsOthersCount", tmpJet->Pt(), othersCount/(static_cast<Double_t>(constituentCount)));
1766           FillHistogram("hJetConstituentProtonCount", protonCount);
1767           FillHistogram("hJetConstituentPionCount", pionCount);
1768           FillHistogram("hJetConstituentKaonCount", kaonCount);
1769           FillHistogram("hJetConstituentElectronCount", electronCount);
1770           FillHistogram("hJetConstituentOthersCount", othersCount);
1771
1772           // Results for the "normal" jet (avoiding single particle jets)
1773           if((constituentCount>=6) && (constituentCount<=14))
1774           {
1775             FillHistogram("hJetPtVsMass_6_14", tmpJet->Pt(), jetMass);
1776             FillHistogram("hJetPtVsJetMass_6_14", tmpJet->Pt(), tmpJet->M());
1777
1778             FillHistogram("hJetPtVsProtonCount_6_14", tmpJet->Pt(), protonCount);
1779             FillHistogram("hJetPtVsPionCount_6_14", tmpJet->Pt(), pionCount);
1780             FillHistogram("hJetPtVsKaonCount_6_14", tmpJet->Pt(), kaonCount);
1781             FillHistogram("hJetPtVsElectronCount_6_14", tmpJet->Pt(), electronCount);
1782             FillHistogram("hJetPtVsOthersCount_6_14", tmpJet->Pt(), othersCount);
1783             FillHistogram("hJetConstituentProtonCount_6_14", protonCount);
1784             FillHistogram("hJetConstituentPionCount_6_14", pionCount);
1785             FillHistogram("hJetConstituentKaonCount_6_14", kaonCount);
1786             FillHistogram("hJetConstituentElectronCount_6_14", electronCount);
1787             FillHistogram("hJetConstituentOthersCount_6_14", othersCount);
1788           }
1789           if((constituentCount>=2))
1790           {
1791             FillHistogram("hJetPtVsMass_2_X", tmpJet->Pt(), jetMass);
1792             FillHistogram("hJetPtVsJetMass_2_X", tmpJet->Pt(), tmpJet->M());
1793
1794             FillHistogram("hJetPtVsProtonCount_2_X", tmpJet->Pt(), protonCount);
1795             FillHistogram("hJetPtVsPionCount_2_X", tmpJet->Pt(), pionCount);
1796             FillHistogram("hJetPtVsKaonCount_2_X", tmpJet->Pt(), kaonCount);
1797             FillHistogram("hJetPtVsElectronCount_2_X", tmpJet->Pt(), electronCount);
1798             FillHistogram("hJetPtVsOthersCount_2_X", tmpJet->Pt(), othersCount);
1799             FillHistogram("hJetConstituentProtonCount_2_X", protonCount);
1800             FillHistogram("hJetConstituentPionCount_2_X", pionCount);
1801             FillHistogram("hJetConstituentKaonCount_2_X", kaonCount);
1802             FillHistogram("hJetConstituentElectronCount_2_X", electronCount);
1803             FillHistogram("hJetConstituentOthersCount_2_X", othersCount);
1804           }
1805           if((constituentCount>=2) && (constituentCount<=7))
1806           {
1807             FillHistogram("hJetPtVsMass_2_7", tmpJet->Pt(), jetMass);
1808             FillHistogram("hJetPtVsJetMass_2_7", tmpJet->Pt(), tmpJet->M());
1809
1810             FillHistogram("hJetPtVsProtonCount_2_7", tmpJet->Pt(), protonCount);
1811             FillHistogram("hJetPtVsPionCount_2_7", tmpJet->Pt(), pionCount);
1812             FillHistogram("hJetPtVsKaonCount_2_7", tmpJet->Pt(), kaonCount);
1813             FillHistogram("hJetPtVsElectronCount_2_7", tmpJet->Pt(), electronCount);
1814             FillHistogram("hJetPtVsOthersCount_2_7", tmpJet->Pt(), othersCount);
1815             FillHistogram("hJetConstituentProtonCount_2_7", protonCount);
1816             FillHistogram("hJetConstituentPionCount_2_7", pionCount);
1817             FillHistogram("hJetConstituentKaonCount_2_7", kaonCount);
1818             FillHistogram("hJetConstituentElectronCount_2_7", electronCount);
1819             FillHistogram("hJetConstituentOthersCount_2_7", othersCount);
1820           }
1821         }
1822       }
1823     }
1824
1825     // ### DIJETS
1826     if(fNumberSignalJets >= 2)
1827     {
1828       FillHistogram("hLeadingJetDeltaPhi", GetDeltaPhi(fFirstLeadingJet->Phi(), fSecondLeadingJet->Phi()));
1829
1830       if (IsDijet(fFirstLeadingJet, fSecondLeadingJet))
1831       {
1832         FillHistogram("hDijetConstituentsPt", fFirstLeadingJet->Pt());
1833         FillHistogram("hDijetConstituentsPt", fSecondLeadingJet->Pt());
1834
1835         FillHistogram("hDijetLeadingJetPt", fFirstLeadingJet->Pt());
1836         FillHistogram("hDijetPtCorrelation", fFirstLeadingJet->Pt(), fSecondLeadingJet->Pt());
1837         Double_t dummyArea = 0;
1838         GetTRBackgroundDensity (2, backgroundDijet, dummyArea, fFirstLeadingJet, fSecondLeadingJet, kFALSE);
1839         GetTRBackgroundDensity (2, backgroundDijetPerpendicular, dummyArea, fFirstLeadingJet, fSecondLeadingJet, kTRUE);
1840         deltaPtDijetPerpendicular = GetDeltaPt(backgroundDijetPerpendicular);
1841       }
1842     }
1843
1844     // ### SOME JET PLOTS
1845     FillHistogram("hJetCountAll", fJetArray->GetEntries());
1846     FillHistogram("hJetCountAccepted", fNumberSignalJets);
1847     FillHistogram("hJetCount", fJetArray->GetEntries(), fNumberSignalJets);
1848     if (fFirstLeadingJet)
1849     {
1850       FillHistogram("hLeadingJetPt", fFirstLeadingJet->Pt());
1851       FillHistogram("hCorrectedLeadingJetPt", GetCorrectedJetPt(fFirstLeadingJet,backgroundKTImprovedCMS));
1852     }
1853     if (fSecondLeadingJet)
1854     {
1855       FillHistogram("hSecondLeadingJetPt", fSecondLeadingJet->Pt());
1856       FillHistogram("hCorrectedSecondLeadingJetPt", GetCorrectedJetPt(fSecondLeadingJet,backgroundKTImprovedCMS));
1857     }
1858   } //endif AnalyzeJets
1859
1860   #ifdef DEBUGMODE
1861     AliInfo("Calculate()::Jets done.");
1862   #endif
1863   ////////////////////// NOTE: Background analysis
1864
1865   if (fAnalyzeBackground)
1866   {
1867     // Calculate background in centrality classes
1868     FillHistogram("hKTBackgroundImprovedCMS", backgroundKTImprovedCMS, centralityPercentile);
1869
1870     FillHistogram("hKTBackgroundImprovedCMSExternal", backgroundKTImprovedCMSExternal, centralityPercentile);
1871
1872     FillHistogram("hKTMeanBackgroundImprovedCMS", centralityPercentile, backgroundKTImprovedCMS);
1873
1874     // In case of dijets -> look at the background
1875     if (backgroundDijet >= 0)
1876       FillHistogram("hDijetBackground", backgroundDijet, centralityPercentile); 
1877     if (backgroundDijetPerpendicular >= 0)
1878       FillHistogram("hDijetBackgroundPerpendicular", backgroundDijetPerpendicular, centralityPercentile); 
1879     if (deltaPtDijetPerpendicular > -10000.)
1880       FillHistogram("hDijetBackgroundPerpendicularDeltaPt", deltaPtDijetPerpendicular, centralityPercentile); 
1881
1882
1883     if(fAnalyzeDeprecatedBackgrounds)
1884     {
1885       FillHistogram("hKTBackgroundPbPb", backgroundKTPbPb, centralityPercentile);
1886       FillHistogram("hKTBackgroundPbPbWithGhosts", backgroundKTPbPbWithGhosts, centralityPercentile);
1887       FillHistogram("hKTBackgroundCMS", backgroundKTCMS, centralityPercentile);
1888       FillHistogram("hKTBackgroundMean", backgroundKTMean, centralityPercentile);
1889       FillHistogram("hKTBackgroundTrackLike", backgroundKTTrackLike, centralityPercentile);
1890
1891       FillHistogram("hTRBackgroundNoExcl", backgroundTRNoExcl, centralityPercentile);
1892       FillHistogram("hTRBackgroundCone02", backgroundTRCone02, centralityPercentile);
1893       FillHistogram("hTRBackgroundCone04", backgroundTRCone04, centralityPercentile);
1894       FillHistogram("hTRBackgroundCone06", backgroundTRCone06, centralityPercentile);
1895       FillHistogram("hTRBackgroundCone08", backgroundTRCone08, centralityPercentile);
1896       FillHistogram("hTRBackgroundExact", backgroundTRExact, centralityPercentile);
1897
1898       // Calculate background profiles in terms of centrality
1899       FillHistogram("hKTMeanBackgroundPbPb", centralityPercentile,  backgroundKTPbPb);
1900       FillHistogram("hKTMeanBackgroundPbPbWithGhosts", centralityPercentile,  backgroundKTPbPbWithGhosts);
1901       FillHistogram("hKTMeanBackgroundCMS", centralityPercentile, backgroundKTCMS);
1902       FillHistogram("hKTMeanBackgroundMean", centralityPercentile, backgroundKTMean);
1903       FillHistogram("hKTMeanBackgroundTPC", centralityPercentile, backgroundKTTrackLike);
1904       FillHistogram("hTRMeanBackground", centralityPercentile,  backgroundTRCone06);
1905     }
1906
1907
1908     // Calculate the delta pt
1909     Double_t tmpDeltaPtNoBackground = GetDeltaPt(0.0);
1910     Double_t tmpDeltaPtKTImprovedCMS = GetDeltaPt(backgroundKTImprovedCMS);
1911
1912     Double_t tmpDeltaPtKTImprovedCMSPartialExclusion = 0.0;
1913     if(fNcoll)
1914       tmpDeltaPtKTImprovedCMSPartialExclusion = GetDeltaPt(backgroundKTImprovedCMS, 1.0/fNcoll);
1915     else
1916       tmpDeltaPtKTImprovedCMSPartialExclusion = GetDeltaPt(backgroundKTImprovedCMS, 1.0);
1917
1918     Double_t tmpDeltaPtKTImprovedCMSPartialExclusion_Signal = 0.0;
1919     if(fNumberSignalJets)
1920       tmpDeltaPtKTImprovedCMSPartialExclusion_Signal = GetDeltaPt(backgroundKTImprovedCMS, 1.0/fNumberSignalJets);
1921     else
1922       tmpDeltaPtKTImprovedCMSPartialExclusion_Signal = GetDeltaPt(backgroundKTImprovedCMS, 1.0);
1923  
1924     Double_t tmpDeltaPtKTImprovedCMSFullExclusion = GetDeltaPt(backgroundKTImprovedCMS, 1.0);
1925
1926     Double_t tmpDeltaPtKTPbPb = 0;
1927     Double_t tmpDeltaPtKTPbPbWithGhosts = 0;
1928     Double_t tmpDeltaPtKTCMS = 0;
1929     Double_t tmpDeltaPtKTMean = 0;
1930     Double_t tmpDeltaPtKTTrackLike = 0;
1931     Double_t tmpDeltaPtTR = 0;
1932
1933     if(fAnalyzeDeprecatedBackgrounds)
1934     {
1935       tmpDeltaPtKTPbPb = GetDeltaPt(backgroundKTPbPb);
1936       tmpDeltaPtKTPbPbWithGhosts = GetDeltaPt(backgroundKTPbPbWithGhosts);
1937       tmpDeltaPtKTCMS = GetDeltaPt(backgroundKTCMS);
1938       tmpDeltaPtKTMean = GetDeltaPt(backgroundKTMean);
1939       tmpDeltaPtKTTrackLike = GetDeltaPt(backgroundKTTrackLike);
1940       tmpDeltaPtTR = GetDeltaPt(backgroundTRCone06);
1941     }
1942
1943     // If valid, fill the delta pt histograms
1944
1945     if(tmpDeltaPtKTImprovedCMS > -10000.0)
1946       FillHistogram("hDeltaPtKTImprovedCMS", tmpDeltaPtKTImprovedCMS, centralityPercentile);
1947     if(tmpDeltaPtKTImprovedCMSPartialExclusion > -10000.0)
1948       FillHistogram("hDeltaPtKTImprovedCMSPartialExclusion", tmpDeltaPtKTImprovedCMSPartialExclusion, centralityPercentile);
1949     if(tmpDeltaPtKTImprovedCMSPartialExclusion_Signal > -10000.0)
1950       FillHistogram("hDeltaPtKTImprovedCMSPartialExclusion_Signal", tmpDeltaPtKTImprovedCMSPartialExclusion_Signal, centralityPercentile);
1951     if(tmpDeltaPtKTImprovedCMSFullExclusion > -10000.0)
1952       FillHistogram("hDeltaPtKTImprovedCMSFullExclusion", tmpDeltaPtKTImprovedCMSFullExclusion, centralityPercentile);
1953
1954     if(tmpDeltaPtNoBackground > 0.000001)
1955       FillHistogram("hDeltaPtNoBackgroundNoEmptyCones", tmpDeltaPtNoBackground, centralityPercentile);
1956     else if(tmpDeltaPtNoBackground > -10000.0)
1957       FillHistogram("hDeltaPtNoBackground", tmpDeltaPtNoBackground, centralityPercentile);
1958
1959
1960     if(fAnalyzeDeprecatedBackgrounds)
1961     {
1962       if(tmpDeltaPtKTPbPb > -10000.0)
1963         FillHistogram("hDeltaPtKTPbPb", tmpDeltaPtKTPbPb, centralityPercentile);
1964       if(tmpDeltaPtKTPbPbWithGhosts > -10000.0)
1965         FillHistogram("hDeltaPtKTPbPbWithGhosts", tmpDeltaPtKTPbPbWithGhosts, centralityPercentile);
1966       if(tmpDeltaPtKTCMS > -10000.0)
1967         FillHistogram("hDeltaPtKTCMS", tmpDeltaPtKTCMS, centralityPercentile);
1968       if(tmpDeltaPtKTMean > -10000.0)
1969         FillHistogram("hDeltaPtKTMean", tmpDeltaPtKTMean, centralityPercentile);
1970       if(tmpDeltaPtKTTrackLike > -10000.0)
1971         FillHistogram("hDeltaPtKTTrackLike", tmpDeltaPtKTTrackLike, centralityPercentile);
1972
1973       if(tmpDeltaPtTR > -10000.0)
1974         FillHistogram("hDeltaPtTR", tmpDeltaPtTR, centralityPercentile);
1975     }
1976   }
1977   
1978   #ifdef DEBUGMODE
1979     AliInfo("Calculate()::Background done.");
1980   #endif
1981   
1982   ////////////////////// NOTE: Pythia histograms
1983   if(fAnalyzePythia)
1984   {
1985     FillHistogram("hPythiaPtHard", GetPtHard());
1986     FillHistogram("hPythiaNTrials", GetPtHardBin()+0.1, GetPythiaTrials());
1987     FillHistogram("hPythiaXSection", GetPtHardBin()+0.1, fCrossSection);
1988
1989         #ifdef DEBUGMODE
1990       AliInfo("Calculate()::Pythia done.");
1991     #endif
1992   }
1993   #ifdef DEBUGMODE
1994     AliInfo("Calculate() done.");
1995   #endif
1996 }
1997
1998 //________________________________________________________________________
1999 Bool_t AliAnalysisTaskChargedJetsPA::UserNotify()
2000 {
2001   // Implemented Notify() to read the cross sections
2002   // and number of trials from pyxsec.root
2003   // 
2004   #ifdef DEBUGMODE
2005     AliInfo("UserNotify started.");
2006   #endif
2007
2008   if(fAnalyzePythia)
2009   {
2010     TTree *tree = AliAnalysisManager::GetAnalysisManager()->GetTree();
2011     TFile *currFile = tree->GetCurrentFile();
2012
2013     TString file(currFile->GetName());
2014
2015     if(file.Contains("root_archive.zip#")){
2016       Ssiz_t pos1 = file.Index("root_archive",12,TString::kExact);
2017       Ssiz_t pos = file.Index("#",1,pos1,TString::kExact);
2018       file.Replace(pos+1,20,"");
2019     }
2020     else {
2021       // not an archive take the basename....
2022       file.ReplaceAll(gSystem->BaseName(file.Data()),"");
2023     }
2024    
2025     TFile *fxsec = TFile::Open(Form("%s%s",file.Data(),"pyxsec.root")); // problem that we cannot really test the existance of a file in a archive so we have to lvie with open error message from root
2026     if(!fxsec){
2027       // next trial fetch the histgram file
2028       fxsec = TFile::Open(Form("%s%s",file.Data(),"pyxsec_hists.root"));
2029       if(!fxsec){
2030           // not a severe condition but inciate that we have no information
2031         return kFALSE;
2032       }
2033       else{
2034         // find the tlist we want to be independtent of the name so use the Tkey
2035         TKey* key = (TKey*)fxsec->GetListOfKeys()->At(0); 
2036         if(!key){
2037           fxsec->Close();
2038           return kFALSE;
2039         }
2040         TList *list = dynamic_cast<TList*>(key->ReadObj());
2041         if(!list){
2042           fxsec->Close();
2043           return kFALSE;
2044         }
2045         fCrossSection = ((TProfile*)list->FindObject("h1Xsec"))->GetBinContent(1);
2046         fTrials  = ((TH1F*)list->FindObject("h1Trials"))->GetBinContent(1);
2047         fxsec->Close();
2048       }
2049     } // no tree pyxsec.root
2050     else {
2051       TTree *xtree = (TTree*)fxsec->Get("Xsection");
2052       if(!xtree){
2053         fxsec->Close();
2054         return kFALSE;
2055       }
2056       UInt_t   ntrials  = 0;
2057       Double_t  xsection  = 0;
2058       xtree->SetBranchAddress("xsection",&xsection);
2059       xtree->SetBranchAddress("ntrials",&ntrials);
2060       xtree->GetEntry(0);
2061       fTrials = ntrials;
2062       fCrossSection = xsection;
2063       fxsec->Close();
2064     }
2065   }
2066   #ifdef DEBUGMODE
2067     AliInfo("UserNotify ended.");
2068   #endif
2069   return kTRUE;
2070 }
2071
2072
2073 //________________________________________________________________________
2074 inline Double_t AliAnalysisTaskChargedJetsPA::EtaToTheta(Double_t arg)
2075   {return 2.*atan(exp(-arg));} 
2076 //________________________________________________________________________
2077 inline Double_t AliAnalysisTaskChargedJetsPA::ThetaToEta(Double_t arg)
2078 {
2079   if ((arg > TMath::Pi()) || (arg < 0.0))
2080   {
2081     AliError(Form("ThetaToEta got wrong input! (%f)", arg));
2082     return 0.0;
2083   }
2084   return -log(tan(arg/2.));
2085 }
2086 //________________________________________________________________________
2087 inline Double_t AliAnalysisTaskChargedJetsPA::GetDeltaPhi(Double_t phi1, Double_t phi2)
2088   {return min(TMath::Abs(phi1-phi2),TMath::TwoPi() - TMath::Abs(phi1-phi2));}
2089
2090 //________________________________________________________________________
2091 Double_t AliAnalysisTaskChargedJetsPA::MCGetOverlapCircleRectancle(Double_t cPosX, Double_t cPosY, Double_t cRadius, Double_t rPosXmin, Double_t rPosXmax, Double_t rPosYmin, Double_t rPosYmax)
2092 {
2093   const Int_t kTests = 1000;
2094   Int_t hits = 0;
2095   TRandom3 randomGen(0);
2096  
2097   // Loop over kTests-many tests
2098   for (Int_t i=0; i<kTests; i++)
2099   {
2100     //Choose random position in rectangle for the tester
2101     Double_t tmpTestX = randomGen.Uniform(rPosXmin, rPosXmax);
2102     Double_t tmpTestY = randomGen.Uniform(rPosYmin, rPosYmax);
2103
2104     //Check, if tester is in circle. If yes, increment circle counter.
2105     Double_t tmpDistance = TMath::Sqrt( (tmpTestX - cPosX)*(tmpTestX - cPosX) + (tmpTestY - cPosY)*(tmpTestY - cPosY) );
2106     if(tmpDistance < cRadius)
2107       hits++;
2108   }
2109
2110   // return ratio
2111   return (static_cast<Double_t>(hits)/static_cast<Double_t>(kTests));
2112 }
2113
2114 //________________________________________________________________________
2115 Double_t AliAnalysisTaskChargedJetsPA::MCGetOverlapMultipleCirclesRectancle(Int_t numCircles, std::vector<Double_t> cPosX, std::vector<Double_t> cPosY, Double_t cRadius, Double_t rPosXmin, Double_t rPosXmax, Double_t rPosYmin, Double_t rPosYmax)
2116 {
2117
2118   const Int_t kTests = 1000;
2119   Int_t hits = 0;
2120   TRandom3 randomGen(0);
2121  
2122   // Loop over kTests-many tests
2123   for (Int_t i=0; i<kTests; i++)
2124   {
2125     //Choose random position in rectangle for the tester
2126     Double_t tmpTestX = randomGen.Uniform(rPosXmin, rPosXmax);
2127     Double_t tmpTestY = randomGen.Uniform(rPosYmin, rPosYmax);
2128
2129     //Check, if tester is in one of the circles. If yes, increment circle counter.
2130     for(Int_t j=0; j<numCircles; j++)
2131     {
2132       Double_t tmpDistance = TMath::Sqrt( (tmpTestX - cPosX[j])*(tmpTestX - cPosX[j]) + (tmpTestY - cPosY[j])*(tmpTestY - cPosY[j]) );
2133       if(tmpDistance < cRadius)
2134       {
2135         hits++;
2136         break;
2137       }
2138     }
2139   }
2140
2141   // return ratio
2142   return (static_cast<Double_t>(hits)/static_cast<Double_t>(kTests));
2143
2144 }
2145
2146 //________________________________________________________________________
2147 inline void AliAnalysisTaskChargedJetsPA::FillHistogram(const char * key, Double_t x)
2148 {
2149   TH1* tmpHist = static_cast<TH1*>(fOutputList->FindObject(GetHistoName(key)));
2150   if(!tmpHist)
2151   {
2152     AliError(Form("Cannot find histogram <%s> ",key)) ;
2153     return;
2154   }
2155
2156   tmpHist->Fill(x);
2157 }
2158
2159 //________________________________________________________________________
2160 inline void AliAnalysisTaskChargedJetsPA::FillHistogram(const char * key, Double_t x, Double_t y)
2161 {
2162   TH1* tmpHist = static_cast<TH1*>(fOutputList->FindObject(GetHistoName(key)));
2163   if(!tmpHist)
2164   {
2165     AliError(Form("Cannot find histogram <%s> ",key));
2166     return;
2167   }
2168
2169   if (tmpHist->IsA()->GetBaseClass("TH1"))
2170     static_cast<TH1*>(tmpHist)->Fill(x,y); // Fill x with y
2171   else if (tmpHist->IsA()->GetBaseClass("TH2"))
2172     static_cast<TH2*>(tmpHist)->Fill(x,y); // Fill x,y with 1
2173 }
2174
2175 //________________________________________________________________________
2176 inline void AliAnalysisTaskChargedJetsPA::FillHistogram(const char * key, Double_t x, Double_t y, Double_t add)
2177 {
2178   TH2* tmpHist = static_cast<TH2*>(fOutputList->FindObject(GetHistoName(key)));
2179   if(!tmpHist)
2180   {
2181     AliError(Form("Cannot find histogram <%s> ",key));
2182     return;
2183   }
2184   
2185   tmpHist->Fill(x,y,add);
2186 }
2187 //________________________________________________________________________
2188 template <class T> T* AliAnalysisTaskChargedJetsPA::AddHistogram1D(const char* name, const char* title, const char* options, Int_t xBins, Double_t xMin, Double_t xMax, const char* xTitle, const char* yTitle)
2189 {
2190   T* tmpHist = new T(GetHistoName(name), GetHistoName(title), xBins, xMin, xMax);
2191
2192   tmpHist->GetXaxis()->SetTitle(xTitle);
2193   tmpHist->GetYaxis()->SetTitle(yTitle);
2194   tmpHist->SetOption(options);
2195   tmpHist->SetMarkerStyle(kFullCircle);
2196   tmpHist->Sumw2();
2197
2198   fHistList->Add(tmpHist);
2199   fHistCount++;
2200   
2201   return tmpHist;
2202 }
2203
2204 //________________________________________________________________________
2205 template <class T> T* AliAnalysisTaskChargedJetsPA::AddHistogram2D(const char* name, const char* title, const char* options, Int_t xBins, Double_t xMin, Double_t xMax, Int_t yBins, Double_t yMin, Double_t yMax, const char* xTitle, const char* yTitle, const char* zTitle)
2206 {
2207   T* tmpHist = new T(GetHistoName(name), GetHistoName(title), xBins, xMin, xMax, yBins, yMin, yMax);
2208   tmpHist->GetXaxis()->SetTitle(xTitle);
2209   tmpHist->GetYaxis()->SetTitle(yTitle);
2210   tmpHist->GetZaxis()->SetTitle(zTitle);
2211   tmpHist->SetOption(options);
2212   tmpHist->SetMarkerStyle(kFullCircle);
2213   tmpHist->Sumw2();
2214
2215   fHistList->Add(tmpHist);
2216   fHistCount++;
2217
2218   return tmpHist;
2219 }
2220
2221 //________________________________________________________________________
2222 void AliAnalysisTaskChargedJetsPA::Terminate(Option_t *)
2223 {
2224   PostData(1, fOutputList);
2225
2226   // Mandatory
2227   fOutputList = dynamic_cast<TList*> (GetOutputData(1)); // '1' refers to the output slot
2228   if (!fOutputList) {
2229     printf("ERROR: Output list not available\n");
2230     return;
2231   }
2232 }
2233
2234 //________________________________________________________________________
2235 AliAnalysisTaskChargedJetsPA::~AliAnalysisTaskChargedJetsPA()
2236 {
2237   // Destructor. Clean-up the output list, but not the histograms that are put inside
2238   // (the list is owner and will clean-up these histograms). Protect in PROOF case.
2239   if (fOutputList && !AliAnalysisManager::GetAnalysisManager()->IsProofMode()) {
2240     delete fOutputList;
2241   }
2242 }
2243
2244 //________________________________________________________________________
2245 void AliAnalysisTaskChargedJetsPA::UserCreateOutputObjects()
2246 {
2247   // called once to create user defined output objects like histograms, plots etc. 
2248   // and to put it on the output list.
2249   // Note: Saving to file with e.g. OpenFile(0) is must be before creating other objects.
2250
2251   fRandom = new TRandom3(0);
2252
2253
2254   fOutputList = new TList();
2255   fOutputList->SetOwner(); // otherwise it produces leaks in merging
2256
2257   // NOTE: Pythia histograms
2258   AddHistogram1D<TProfile>("hPythiaXSection", "Pythia cross section distribution", "", fNumPtHardBins+1, 0, fNumPtHardBins+1, "p_{T} hard bin","dN^{Events}/dp_{T,hard}");
2259   AddHistogram1D<TH1D>("hPythiaNTrials", "Pythia trials (no correction for manual cuts)", "", fNumPtHardBins+1, 0, fNumPtHardBins+1, "p_{T} hard bin", "Trials");
2260
2261
2262   PostData(1, fOutputList);
2263 }
2264
2265 //________________________________________________________________________
2266 void AliAnalysisTaskChargedJetsPA::UserExec(Option_t *) 
2267 {
2268   #ifdef DEBUGMODE
2269     AliInfo("UserExec() started.");
2270   #endif
2271
2272   if (!InputEvent())
2273   {
2274     AliError("??? Event pointer == 0 ???");
2275     return;
2276   }
2277
2278   if (!fInitialized)
2279     ExecOnce(); // Get tracks, jets, background from arrays if not already given + Init Histos
2280   
2281   Calculate(InputEvent());
2282         
2283   PostData(1, fOutputList);
2284 }