]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWGCF/Correlations/DPhi/AliAnalysisTaskPhiCorrelations.cxx
allowing to skip filling of steps
[u/mrichter/AliRoot.git] / PWGCF / Correlations / DPhi / AliAnalysisTaskPhiCorrelations.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /* $Id:$ */
17
18 #include <TROOT.h>
19 #include <TChain.h>
20 #include <TFile.h>
21 #include <TList.h>
22 #include <TMath.h>
23 #include <TTree.h>
24 #include <TH2F.h>
25 #include <TRandom.h>
26
27 #include "AliAnalysisTaskPhiCorrelations.h"
28 #include "AliAnalyseLeadingTrackUE.h"
29 #include "AliUEHistograms.h"
30 #include "AliUEHist.h"
31
32 #include "AliAnalysisManager.h"
33 #include "AliAODHandler.h"
34 #include "AliAODInputHandler.h"
35 #include "AliAODMCParticle.h"
36 #include "AliInputEventHandler.h"
37 #include "AliLog.h"
38 #include "AliMCEventHandler.h"
39 #include "AliVParticle.h"
40 #include "AliCFContainer.h"
41
42 #include "AliESDEvent.h"
43 #include "AliESDInputHandler.h"
44 #include "AliMultiplicity.h"
45 #include "AliCentrality.h"
46 #include "AliStack.h"
47 #include "AliAODMCHeader.h"
48
49 #include "AliEventPoolManager.h"
50
51
52 ////////////////////////////////////////////////////////////////////////
53 //
54 // Analysis class for azimuthal correlation studies
55 // Based on the UE task from Sara Vallero and Jan Fiete
56 //
57 // This class needs input AODs.
58 // The output is a list of analysis-specific containers.
59 //
60 // The AOD can be either connected to the InputEventHandler  
61 // for a chain of AOD files 
62 // or 
63 // to the OutputEventHandler
64 // for a chain of ESD files,
65 // in this case the class should be in the train after the jet-finder
66 //
67 //    Authors:
68 //    Jan Fiete Grosse-Oetringhaus
69 // 
70 ////////////////////////////////////////////////////////////////////////
71
72
73 ClassImp( AliAnalysisTaskPhiCorrelations )
74 ClassImp( AliDPhiBasicParticle )
75
76 //____________________________________________________________________
77 AliAnalysisTaskPhiCorrelations:: AliAnalysisTaskPhiCorrelations(const char* name):
78 AliAnalysisTask(name,""),
79 // general configuration
80 fDebug(0),
81 fMode(0),
82 fReduceMemoryFootprint(kFALSE),
83 fFillMixed(kTRUE),
84 fMixingTracks(50000),
85 fCompareCentralities(kFALSE),
86 fTwoTrackEfficiencyStudy(kFALSE),
87 fTwoTrackEfficiencyCut(kFALSE),
88 fUseVtxAxis(kFALSE),
89 fSkipTrigger(kFALSE),
90 // pointers to UE classes
91 fAnalyseUE(0x0),
92 fHistos(0x0),
93 fHistosMixed(0),
94 fkTrackingEfficiency(0x0),
95 // handlers and events
96 fAOD(0x0),
97 fESD(0x0),
98 fArrayMC(0x0),
99 fInputHandler(0x0),
100 fMcEvent(0x0),
101 fMcHandler(0x0),
102 fPoolMgr(0x0),
103 // histogram settings
104 fListOfHistos(0x0), 
105 // event QA
106 fnTracksVertex(1),  // QA tracks pointing to principal vertex (= 3 default) 
107 fZVertex(7.),
108 fCentralityMethod("V0M"),
109 // track cuts
110 fTrackEtaCut(0.8),
111 fPtMin(0.5),
112 fFilterBit(0xFF),
113 fSelectBit(0),
114 fUseChargeHadrons(kFALSE),
115 fSelectCharge(0),
116 fTriggerRestrictEta(-1),
117 fEtaOrdering(kFALSE),
118 fCutConversions(kFALSE),
119 fCutResonances(kFALSE),
120 fFillOnlyStep0(kFALSE),
121 fSkipStep6(kFALSE),
122 fFillpT(kFALSE)
123 {
124   // Default constructor
125   // Define input and output slots here
126   // Input slot #0 works with a TChain
127   DefineInput(0, TChain::Class());
128   // Output slot #0 writes into a TList container
129   DefineOutput(0, TList::Class());
130 }
131
132 AliAnalysisTaskPhiCorrelations::~AliAnalysisTaskPhiCorrelations() 
133
134   // destructor
135   
136   if (fListOfHistos  && !AliAnalysisManager::GetAnalysisManager()->IsProofMode()) 
137     delete fListOfHistos;
138 }
139
140 //____________________________________________________________________
141 void AliAnalysisTaskPhiCorrelations::ConnectInputData(Option_t* /*option*/)
142 {
143   
144   // Connect the input data  
145   if (fDebug > 1) AliInfo("ConnectInputData() ");
146   
147   // Since AODs can either be connected to the InputEventHandler
148   // or to the OutputEventHandler ( the AOD is created by a previus task in the train )
149   // we need to get the pointer to the AODEvent correctly.
150   
151   // Delta AODs are also accepted.
152   
153   TObject* handler = AliAnalysisManager::GetAnalysisManager()->GetInputEventHandler();
154   
155   if( handler && handler->InheritsFrom("AliAODInputHandler") ) 
156   { // input AOD
157     fAOD = ((AliAODInputHandler*)handler)->GetEvent();
158     if (fDebug > 1) AliInfo(" ==== Tracks and Jets from AliAODInputHandler");
159   } 
160   else 
161   {  //output AOD
162     handler = AliAnalysisManager::GetAnalysisManager()->GetOutputEventHandler();
163     if (handler && handler->InheritsFrom("AliAODHandler") ) 
164     {
165       fAOD = ((AliAODHandler*)handler)->GetAOD();
166       if (fDebug > 1) AliInfo(" ==== Tracks and Jets from AliAODHandler");
167     } 
168     else 
169     {  // no AOD
170       AliWarning("I can't get any AOD Event Handler");
171     }
172   }
173   
174   if (handler && handler->InheritsFrom("AliESDInputHandler") ) 
175   { // input ESD
176     // pointer received per event in ::Exec
177     if (fDebug > 1) AliInfo(" ==== Tracks and Jets from AliESDInputHandler");
178   } 
179   
180   // Initialize common pointers
181   Initialize();
182 }
183
184 //____________________________________________________________________
185 void  AliAnalysisTaskPhiCorrelations::CreateOutputObjects()
186 {
187   // Create the output container
188   
189   if (fDebug > 1) AliInfo("CreateOutputObjects()");
190    
191   // Initialize class with main algorithms, event and track selection. 
192   fAnalyseUE = new AliAnalyseLeadingTrackUE();
193   fAnalyseUE->SetParticleSelectionCriteria(fFilterBit, fUseChargeHadrons, fTrackEtaCut, fPtMin);
194   fAnalyseUE->SetDebug(fDebug); 
195   fAnalyseUE->DefineESDCuts(fFilterBit);
196
197   // Initialize output list of containers
198   if (fListOfHistos != NULL){
199         delete fListOfHistos;
200         fListOfHistos = NULL;
201         }
202   if (!fListOfHistos){
203         fListOfHistos = new TList();
204         fListOfHistos->SetOwner(kTRUE); 
205         }
206
207   // Initialize class to handle histograms 
208   const char* histType = "4";
209   if (fUseVtxAxis)
210     histType = "5";
211   fHistos = new AliUEHistograms("AliUEHistogramsSame", histType);
212   fHistosMixed = new AliUEHistograms("AliUEHistogramsMixed", histType);
213   
214   fHistos->SetSelectCharge(fSelectCharge);
215   fHistosMixed->SetSelectCharge(fSelectCharge);
216   
217   fHistos->SetTriggerRestrictEta(fTriggerRestrictEta);
218   fHistosMixed->SetTriggerRestrictEta(fTriggerRestrictEta);
219   
220   fHistos->SetEtaOrdering(fEtaOrdering);
221   fHistosMixed->SetEtaOrdering(fEtaOrdering);
222
223   fHistos->SetPairCuts(fCutConversions, fCutResonances);
224   fHistosMixed->SetPairCuts(fCutConversions, fCutResonances);
225   
226   // add histograms to list
227   fListOfHistos->Add(fHistos);
228   fListOfHistos->Add(fHistosMixed);
229   
230   fListOfHistos->Add(new TH2F("trackletsVsV0Cent", ";L1 clusters;v0 centrality", 100, -0.5, 9999.5, 101, 0, 101));
231   fListOfHistos->Add(new TH2F("processIDs", ";#Delta#phi;process id", 100, -0.5 * TMath::Pi(), 1.5 * TMath::Pi(), kPNoProcess + 1, -0.5, kPNoProcess + 0.5));
232   fListOfHistos->Add(new TH1F("eventStat", ";;events", 4, -0.5, 3.5));
233   fListOfHistos->Add(new TH2F("mixedDist", ";centrality;tracks;events", 101, 0, 101, 200, 0, fMixingTracks * 1.5));
234   fListOfHistos->Add(new TH1F("pids", ";pdg;tracks", 2001, -1000.5, 1000.5));
235   
236   PostData(0,fListOfHistos);
237   
238   // Add task configuration to output list 
239   AddSettingsTree();
240
241   // event mixing
242   Int_t trackDepth = fMixingTracks; 
243   Int_t poolsize   = 1000;  // Maximum number of events, ignored in the present implemented of AliEventPool
244    
245   Int_t nCentralityBins  = fHistos->GetUEHist(2)->GetEventHist()->GetNBins(1);
246   Double_t* centralityBins = (Double_t*) fHistos->GetUEHist(2)->GetEventHist()->GetAxis(1, 0)->GetXbins()->GetArray();
247   
248   Int_t nZvtxBins  = 7+1+7;
249   // bins for second buffer are shifted by 100 cm
250   Double_t vertexBins[] = { -7, -5, -3, -1, 1, 3, 5, 7, 93, 95, 97, 99, 101, 103, 105, 107 };
251   Double_t* zvtxbin = vertexBins;
252
253   if (fHistos->GetUEHist(2)->GetEventHist()->GetNVar() > 2)
254   {
255     nZvtxBins = fHistos->GetUEHist(2)->GetEventHist()->GetNBins(2);
256     zvtxbin = (Double_t*) fHistos->GetUEHist(2)->GetEventHist()->GetAxis(2, 0)->GetXbins()->GetArray();
257   }
258
259   fPoolMgr = new AliEventPoolManager(poolsize, trackDepth, nCentralityBins, centralityBins, nZvtxBins, zvtxbin);
260 }
261
262 //____________________________________________________________________
263 void  AliAnalysisTaskPhiCorrelations::Exec(Option_t */*option*/)
264 {
265   // receive ESD pointer if we are not running AOD analysis
266   if (!fAOD)
267   {
268     AliVEventHandler* handler = AliAnalysisManager::GetAnalysisManager()->GetInputEventHandler();
269     if (handler && handler->InheritsFrom("AliESDInputHandler"))
270       fESD = ((AliESDInputHandler*)handler)->GetEvent();
271   }
272
273   if (fMode)
274   {
275     // correction mode
276     
277     if (fMcHandler)
278       fMcEvent = fMcHandler->MCEvent();
279
280     if (fAOD)
281     {
282       // array of MC particles
283       fArrayMC = dynamic_cast<TClonesArray*>(fAOD->FindListObject(AliAODMCParticle::StdBranchName()));
284       if (!fArrayMC)
285         AliFatal("No array of MC particles found !!!");
286     }
287     
288     AnalyseCorrectionMode();
289   }
290   else AnalyseDataMode();
291 }
292
293 /******************** ANALYSIS METHODS *****************************/
294
295 //____________________________________________________________________
296 void  AliAnalysisTaskPhiCorrelations::AddSettingsTree()
297 {
298   //Write settings to output list
299   TTree *settingsTree   = new TTree("UEAnalysisSettings","Analysis Settings in UE estimation");
300   settingsTree->Branch("fnTracksVertex", &fnTracksVertex,"nTracksVertex/I");
301   settingsTree->Branch("fZVertex", &fZVertex,"ZVertex/D");
302   //settingsTree->Branch("fCentralityMethod", fCentralityMethod.Data(),"CentralityMethod/C");
303   settingsTree->Branch("fTrackEtaCut", &fTrackEtaCut, "TrackEtaCut/D");
304   settingsTree->Branch("fPtMin", &fPtMin, "PtMin/D");
305   settingsTree->Branch("fFilterBit", &fFilterBit,"FilterBit/I");
306   settingsTree->Branch("fSelectBit", &fSelectBit,"EventSelectionBit/I");
307   settingsTree->Branch("fUseChargeHadrons", &fUseChargeHadrons,"UseChHadrons/O");
308   settingsTree->Branch("fSelectCharge", &fSelectCharge,"SelectCharge/I");
309   settingsTree->Branch("fTriggerRestrictEta", &fTriggerRestrictEta,"TriggerRestrictEta/D");
310   settingsTree->Branch("fEtaOrdering", &fEtaOrdering,"EtaOrdering/O");
311   settingsTree->Branch("fCutConversions", &fCutConversions,"CutConversions/O");
312   settingsTree->Branch("fCutResonances", &fCutResonances,"CutResonances/O");
313   settingsTree->Branch("fFillpT", &fFillpT,"FillpT/O");
314   settingsTree->Branch("fkTrackingEfficiency", "TH1D", &fkTrackingEfficiency);
315   settingsTree->Branch("fMixingTracks", &fMixingTracks,"MixingTracks/I");
316   settingsTree->Branch("fSkipTrigger", &fSkipTrigger,"SkipTrigger/O");
317   
318   settingsTree->Fill();
319   fListOfHistos->Add(settingsTree);
320 }  
321
322 //____________________________________________________________________
323 void  AliAnalysisTaskPhiCorrelations::AnalyseCorrectionMode()
324 {
325   // Run the analysis on MC to get the correction maps
326   //
327  
328   if ( fDebug > 3 ) AliInfo( " Processing event in Corrections mode ..." );
329   
330   Double_t centrality = 0;
331   
332   if (fCentralityMethod.Length() > 0)
333   {
334     AliCentrality *centralityObj = 0;
335     if (fAOD)
336       centralityObj = fAOD->GetHeader()->GetCentralityP();
337     else if (fESD)
338       centralityObj = fESD->GetCentrality();
339     
340     if (centralityObj)
341     {
342       centrality = centralityObj->GetCentralityPercentileUnchecked(fCentralityMethod);
343       AliInfo(Form("Centrality is %f", centrality));
344     }
345     else
346     {
347       Printf("WARNING: Centrality object is 0");
348       centrality = -1;
349      }
350   }
351   
352   // Support for ESD and AOD based analysis
353   AliVEvent* inputEvent = fAOD;
354   if (!inputEvent)
355     inputEvent = fESD;
356   
357   fHistos->SetRunNumber(inputEvent->GetRunNumber());
358
359   TObject* mc = fArrayMC;
360   if (!mc)
361     mc = fMcEvent;
362   
363   // count all events
364   fHistos->FillEvent(centrality, -1);
365   
366   // Only consider MC events within the vtx-z region used also as cut on the reconstructed vertex
367   TObject* vertexSupplier = fMcEvent;
368   if (fAOD) // AOD
369     vertexSupplier = fAOD->FindListObject(AliAODMCHeader::StdBranchName());
370     
371   if (!fAnalyseUE->VertexSelection(vertexSupplier, 0, fZVertex)) 
372     return;
373   
374   Float_t zVtx = 0;
375   if (fAOD)
376     zVtx = ((AliAODMCHeader*) vertexSupplier)->GetVtxZ();
377   else
378     zVtx = fMcEvent->GetPrimaryVertex()->GetZ();
379   Float_t weight = 1;
380   if (fFillpT)
381     weight = -1;
382     
383   // Get MC primaries
384   TObjArray* tracksMC = fAnalyseUE->GetAcceptedParticles(mc, 0, kTRUE, -1, kTRUE);
385   
386   if (fAOD)
387   {
388     for (Int_t i=0; i<fArrayMC->GetEntriesFast(); i++)
389       ((TH1F*) fListOfHistos->FindObject("pids"))->Fill(((AliAODMCParticle*) fArrayMC->At(i))->PdgCode());
390   }
391   else
392   {
393     for (Int_t i=0; i<fMcEvent->GetNumberOfTracks(); i++)
394       ((TH1F*) fListOfHistos->FindObject("pids"))->Fill(fMcEvent->GetTrack(i)->PdgCode());
395   }
396   
397   // (MC-true all particles)
398   // STEP 0
399   fHistos->FillCorrelations(centrality, zVtx, AliUEHist::kCFStepAll, tracksMC, 0, weight);
400   
401   // mixed event
402   if (fFillMixed)
403   {
404     AliEventPool* pool = fPoolMgr->GetEventPool(centrality, zVtx);
405     //pool->PrintInfo();
406     if (pool->IsReady() || pool->NTracksInPool() > fMixingTracks / 10 || pool->GetCurrentNEvents() >= 5) 
407       for (Int_t jMix=0; jMix<pool->GetCurrentNEvents(); jMix++) 
408         fHistosMixed->FillCorrelations(centrality, zVtx, AliUEHist::kCFStepAll, tracksMC, pool->GetEvent(jMix), 1.0 / pool->GetCurrentNEvents(), (jMix == 0));
409     pool->UpdatePool(CloneAndReduceTrackList(tracksMC));
410   }
411   
412 //   Printf("trigger: %d", ((AliInputEventHandler*)fInputHandler)->IsEventSelected());
413   
414   // Trigger selection ************************************************
415   if (!fFillOnlyStep0 && (fSkipTrigger || fAnalyseUE->TriggerSelection(fInputHandler)))
416   {  
417     // (MC-true all particles)
418     // STEP 1
419     if (!fReduceMemoryFootprint)
420       fHistos->FillCorrelations(centrality, zVtx, AliUEHist::kCFStepTriggered, tracksMC, 0, weight);
421     else
422       fHistos->FillEvent(centrality, AliUEHist::kCFStepTriggered);
423       
424     if (!inputEvent) {
425       AliFatal("UNEXPECTED: inputEvent is 0. Trigger selection should have failed");
426       return;
427     }
428     
429     // Vertex selection *************************************************
430     if (fAnalyseUE->VertexSelection(inputEvent, fnTracksVertex, fZVertex))
431     {
432       // fill here for tracking efficiency
433       // loop over particle species
434       
435       for (Int_t particleSpecies = 0; particleSpecies < 4; particleSpecies++)
436       {
437         TObjArray* primMCParticles = fAnalyseUE->GetAcceptedParticles(mc, 0x0, kTRUE, particleSpecies, kTRUE);
438         TObjArray* primRecoTracksMatched = fAnalyseUE->GetAcceptedParticles(inputEvent, mc, kTRUE, particleSpecies, kTRUE);
439         TObjArray* allRecoTracksMatched = fAnalyseUE->GetAcceptedParticles(inputEvent, mc, kFALSE, particleSpecies, kTRUE);
440       
441         fHistos->FillTrackingEfficiency(primMCParticles, primRecoTracksMatched, allRecoTracksMatched, particleSpecies, centrality);
442         
443 //      Printf("%d --> %d %d %d", particleSpecies, primMCParticles->GetEntries(), primRecoTracksMatched->GetEntries(), allRecoTracksMatched->GetEntries());
444
445         delete primMCParticles;
446         delete primRecoTracksMatched;
447         delete allRecoTracksMatched;
448       }
449     
450       // (MC-true all particles)
451       // STEP 2
452       if (!fReduceMemoryFootprint)
453         fHistos->FillCorrelations(centrality, zVtx, AliUEHist::kCFStepVertex, tracksMC, 0, weight);
454       else
455         fHistos->FillEvent(centrality, AliUEHist::kCFStepVertex);
456       
457       // Get MC primaries that match reconstructed track
458       TObjArray* tracksRecoMatchedPrim = fAnalyseUE->GetAcceptedParticles(inputEvent, mc, kTRUE, -1, kTRUE);
459       
460       // (RECO-matched (quantities from MC particle) primary particles)
461       // STEP 4
462       fHistos->FillCorrelations(centrality, zVtx, AliUEHist::kCFStepTrackedOnlyPrim, tracksRecoMatchedPrim, 0, weight);
463       
464       // Get MC primaries + secondaries that match reconstructed track
465       TObjArray* tracksRecoMatchedAll = fAnalyseUE->GetAcceptedParticles(inputEvent, mc, kFALSE, -1, kTRUE);
466       
467       // (RECO-matched (quantities from MC particle) all particles)
468       // STEP 5
469       fHistos->FillCorrelations(centrality, zVtx, AliUEHist::kCFStepTracked, tracksRecoMatchedAll, 0, weight);
470       
471       // Get RECO tracks
472       TObjArray* tracks = fAnalyseUE->GetAcceptedParticles(inputEvent, 0, kTRUE, -1, kTRUE);
473       
474       // (RECO all tracks)
475       // STEP 6
476       if (!fSkipStep6)
477         fHistos->FillCorrelations(centrality, zVtx, AliUEHist::kCFStepReconstructed, tracks, 0, weight);
478       
479       // mixed event
480       if (fFillMixed && !fSkipStep6)
481       {
482         AliEventPool* pool2 = fPoolMgr->GetEventPool(centrality, zVtx + 100);
483         //pool2->PrintInfo();
484         if (pool2->IsReady() || pool2->NTracksInPool() > fMixingTracks / 10 || pool2->GetCurrentNEvents() >= 5) 
485           for (Int_t jMix=0; jMix<pool2->GetCurrentNEvents(); jMix++) 
486             fHistosMixed->FillCorrelations(centrality, zVtx, AliUEHist::kCFStepReconstructed, tracks, pool2->GetEvent(jMix), 1.0 / pool2->GetCurrentNEvents(), (jMix == 0));
487         pool2->UpdatePool(CloneAndReduceTrackList(tracks));
488       }
489       
490       if (0 && !fReduceMemoryFootprint)
491       {
492         // make list of secondaries (matched with MC)
493         TObjArray* tracksRecoMatchedSecondaries = new TObjArray;
494         for (Int_t i=0; i<tracksRecoMatchedAll->GetEntriesFast(); i++)
495           if (((AliAODMCParticle*)tracksRecoMatchedAll->At(i))->IsPhysicalPrimary() == kFALSE)
496             tracksRecoMatchedSecondaries->Add(tracksRecoMatchedAll->At(i));
497       
498         // Study: Use only secondaries as trigger particles and plot the correlation vs. all particles; store in step 9
499         fHistos->FillCorrelations(centrality, zVtx, AliUEHist::kCFStepBiasStudy2, tracksRecoMatchedSecondaries, tracksRecoMatchedAll, weight);
500         
501         // Study: Use only primaries as trigger particles and plot the correlation vs. secondaries; store in step 8
502         fHistos->FillCorrelations(centrality, zVtx, AliUEHist::kCFStepBiasStudy, tracksRecoMatchedPrim, tracksRecoMatchedSecondaries, weight);
503       
504         // plot delta phi vs process id of secondaries
505         // trigger particles: primaries in 4 < pT < 10
506         // associated particles: secondaries in 1 < pT < 10
507         
508         for (Int_t i=0; i<tracksRecoMatchedPrim->GetEntriesFast(); i++)
509         {
510           AliVParticle* triggerParticle = (AliVParticle*) tracksRecoMatchedPrim->At(i);
511           
512           if (triggerParticle->Pt() < 4 || triggerParticle->Pt() > 10)
513             continue;
514           
515           for (Int_t j=0; j<tracksRecoMatchedSecondaries->GetEntriesFast(); j++)
516           {
517             AliAODMCParticle* particle = (AliAODMCParticle*) tracksRecoMatchedSecondaries->At(j);
518             
519             if (particle->Pt() < 1 || particle->Pt() > 10)
520               continue;
521             
522             if (particle->Pt() > triggerParticle->Pt())
523               continue;
524               
525             Double_t deltaPhi = triggerParticle->Phi() - particle->Phi();
526             if (deltaPhi > 1.5 * TMath::Pi()) 
527               deltaPhi -= TMath::TwoPi();
528             if (deltaPhi < -0.5 * TMath::Pi())
529               deltaPhi += TMath::TwoPi();
530               
531             Int_t processID = fMcEvent->Stack()->Particle(particle->GetLabel())->GetUniqueID();
532               
533             ((TH2F*) fListOfHistos->FindObject("processIDs"))->Fill(deltaPhi, processID);
534           }
535         }
536         
537         delete tracksRecoMatchedSecondaries;
538       }
539   
540       delete tracksRecoMatchedPrim;
541       delete tracksRecoMatchedAll;
542       delete tracks;
543     }
544   }
545   
546   delete tracksMC;
547 }
548
549 //____________________________________________________________________
550 void  AliAnalysisTaskPhiCorrelations::AnalyseDataMode()
551 {
552
553   // Run the analysis on DATA or MC to get raw distributions
554  
555   if ( fDebug > 3 ) AliInfo( " Processing event in Data mode ..." );
556
557   ((TH1F*) fListOfHistos->FindObject("eventStat"))->Fill(0);
558
559   // skip not selected events here (the AOD is not updated for those)
560   if (!fInputHandler)
561     return;
562     
563   if (!(fInputHandler->IsEventSelected() & (AliVEvent::kMB | AliVEvent::kUserDefined)))
564     return;
565
566   Double_t centrality = 0;
567   
568   AliCentrality *centralityObj = 0;
569   if (fCentralityMethod.Length() > 0)
570   {
571     if (fAOD)
572       centralityObj = fAOD->GetHeader()->GetCentralityP();
573     else if (fESD)
574       centralityObj = fESD->GetCentrality();
575     
576     if (centralityObj)
577       centrality = centralityObj->GetCentralityPercentile(fCentralityMethod);
578       //centrality = centralityObj->GetCentralityPercentileUnchecked(fCentralityMethod);
579     else
580       centrality = -1;
581
582     if (fAOD)
583     {
584       // remove outliers
585       if (centrality == 0)
586       {
587         if (fAOD->GetVZEROData())
588         {
589           Float_t multV0 = 0;
590           for (Int_t i=0; i<64; i++)
591             multV0 += fAOD->GetVZEROData()->GetMultiplicity(i);
592           if (multV0 < 19500)
593           {
594             centrality = -1;
595             AliInfo("Rejecting event due to too small V0 multiplicity");
596           }
597         }
598       }
599     }
600     
601     AliInfo(Form("Centrality is %f", centrality));
602   }
603   
604   // Support for ESD and AOD based analysis
605   AliVEvent* inputEvent = fAOD;
606   if (!inputEvent)
607     inputEvent = fESD;
608
609   Float_t bSign = (inputEvent->GetMagneticField() > 0) ? 1 : -1;
610
611   fHistos->SetRunNumber(inputEvent->GetRunNumber());
612   
613   // Fill the "event-counting-container", it is needed to get the number of events remaining after each event-selection cut
614   fHistos->FillEvent(centrality, AliUEHist::kCFStepAll);
615   
616   // Trigger selection ************************************************
617   if (!fAnalyseUE->TriggerSelection(fInputHandler)) return;
618   
619   // Fill the "event-counting-container", it is needed to get the number of events remaining after each event-selection cut
620   fHistos->FillEvent(centrality, AliUEHist::kCFStepTriggered);
621   
622   // Vertex selection *************************************************
623   if(!fAnalyseUE->VertexSelection(inputEvent, fnTracksVertex, fZVertex)) return;
624   
625   // Fill the "event-counting-container", it is needed to get the number of events remaining after each event-selection cut
626   fHistos->FillEvent(centrality, AliUEHist::kCFStepVertex);
627  
628   // optimization
629   if (centrality < 0 && !fCompareCentralities)
630     return;
631
632   TObjArray* tracks = fAnalyseUE->GetAcceptedParticles(inputEvent, 0, kTRUE, -1, kTRUE);
633   // create a list of reduced objects. This speeds up processing and reduces memory consumption for the event pool
634   TObjArray* tracksClone = CloneAndReduceTrackList(tracks);
635   delete tracks;
636   
637   //Printf("Accepted %d tracks", tracks->GetEntries());
638   
639   const AliVVertex* vertex = inputEvent->GetPrimaryVertex();
640   Double_t zVtx = vertex->GetZ();
641   
642   Float_t weight = 1;
643   if (fFillpT)
644     weight = -1;
645
646   // fill two different centralities (syst study)
647   // the zvtx axis is used to distinguish the results of both centralities: configured centrality in zvtx = 0, SPD in zvtx = 2
648   if (fCompareCentralities)
649     zVtx = 0;
650   
651   // Fill containers at STEP 6 (reconstructed)
652   if (centrality >= 0)
653   {
654     if (!fSkipStep6)
655       fHistos->FillCorrelations(centrality, zVtx, AliUEHist::kCFStepReconstructed, tracksClone, 0, weight);
656     
657     ((TH1F*) fListOfHistos->FindObject("eventStat"))->Fill(1);
658     
659     if (fTwoTrackEfficiencyCut)
660       fHistos->FillCorrelations(centrality, zVtx, AliUEHist::kCFStepBiasStudy, tracksClone, 0, weight, kTRUE, kTRUE, bSign);
661   }
662
663   // fill second time with SPD centrality
664   if (fCompareCentralities && centralityObj)
665   {
666     centrality = centralityObj->GetCentralityPercentile("CL1");
667     if (centrality >= 0 && !fSkipStep6)
668       fHistos->FillCorrelations(centrality, 2, AliUEHist::kCFStepReconstructed, tracksClone, 0, weight);
669   }
670   
671   if (fFillMixed)
672   {
673     // event mixing
674     
675     // 1. First get an event pool corresponding in mult (cent) and
676     //    zvertex to the current event. Once initialized, the pool
677     //    should contain nMix (reduced) events. This routine does not
678     //    pre-scan the chain. The first several events of every chain
679     //    will be skipped until the needed pools are filled to the
680     //    specified depth. If the pool categories are not too rare, this
681     //    should not be a problem. If they are rare, you could lose
682     //    statistics.
683
684     // 2. Collect the whole pool's content of tracks into one TObjArray
685     //    (bgTracks), which is effectively a single background super-event.
686
687     // 3. The reduced and bgTracks arrays must both be passed into
688     //    FillCorrelations(). Also nMix should be passed in, so a weight
689     //    of 1./nMix can be applied.
690
691     AliEventPool* pool = fPoolMgr->GetEventPool(centrality, zVtx);
692     
693     if (!pool)
694       AliFatal(Form("No pool found for centrality = %f, zVtx = %f", centrality, zVtx));
695     
696     //pool->SetDebug(1);
697      
698     if (pool->IsReady() || pool->NTracksInPool() > fMixingTracks / 10 || pool->GetCurrentNEvents() >= 5) 
699     {
700       
701       Int_t nMix = pool->GetCurrentNEvents();
702 //       cout << "nMix = " << nMix << " tracks in pool = " << pool->NTracksInPool() << endl;
703       
704       ((TH1F*) fListOfHistos->FindObject("eventStat"))->Fill(2);
705       ((TH2F*) fListOfHistos->FindObject("mixedDist"))->Fill(centrality, pool->NTracksInPool());
706       if (pool->IsReady())
707         ((TH1F*) fListOfHistos->FindObject("eventStat"))->Fill(3);
708     
709       // Fill mixed-event histos here  
710       for (Int_t jMix=0; jMix<nMix; jMix++) 
711       {
712         TObjArray* bgTracks = pool->GetEvent(jMix);
713         
714         if (!fSkipStep6)
715           fHistosMixed->FillCorrelations(centrality, zVtx, AliUEHist::kCFStepReconstructed, tracksClone, bgTracks, 1.0 / nMix, (jMix == 0));
716
717         if (fTwoTrackEfficiencyCut)
718           fHistosMixed->FillCorrelations(centrality, zVtx, AliUEHist::kCFStepBiasStudy, tracksClone, bgTracks, 1.0 / nMix, (jMix == 0), kTRUE, bSign);
719       }
720     }
721     
722     // ownership is with the pool now
723     pool->UpdatePool(tracksClone);
724     //pool->PrintInfo();
725   }
726   else
727     delete tracksClone;
728 }
729
730 TObjArray* AliAnalysisTaskPhiCorrelations::CloneAndReduceTrackList(TObjArray* tracks)
731 {
732   // clones a track list by using AliDPhiBasicParticle which uses much less memory (used for event mixing)
733   
734   TObjArray* tracksClone = new TObjArray;
735   tracksClone->SetOwner(kTRUE);
736   
737   for (Int_t i=0; i<tracks->GetEntriesFast(); i++)
738   {
739     AliVParticle* particle = (AliVParticle*) tracks->At(i);
740     tracksClone->Add(new AliDPhiBasicParticle(particle->Eta(), particle->Phi(), particle->Pt(), particle->Charge()));
741   }
742   
743   return tracksClone;
744 }
745
746 //____________________________________________________________________
747 void  AliAnalysisTaskPhiCorrelations::Initialize()
748 {
749   // input handler
750   fInputHandler = (AliInputEventHandler*)
751          ((AliAnalysisManager::GetAnalysisManager())->GetInputEventHandler());
752   // MC handler
753   fMcHandler = dynamic_cast<AliMCEventHandler*> (AliAnalysisManager::GetAnalysisManager()->GetMCtruthEventHandler());
754 }