]> git.uio.no Git - u/mrichter/AliRoot.git/blob - PWG/CaloTrackCorrBase/AliCaloTrackReader.cxx
Add pile up clusters counting recalculation in case of input is a reclusterized list...
[u/mrichter/AliRoot.git] / PWG / CaloTrackCorrBase / AliCaloTrackReader.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 //_________________________________________________________________________
17 // Base class for reading data: MonteCarlo, ESD or AOD, of PHOS EMCAL and 
18 // Central Barrel Tracking detectors (CTS).
19 // Not all MC particles/tracks/clusters are kept, some kinematical/fiducial restrictions are done.
20 // Mother class of : AliCaloTrackESDReader: Fills ESD data in 3 TObjArrays (PHOS, EMCAL, CTS)
21 //                 : AliCaloTrackMCReader : Fills Kinematics data in 3 TObjArrays (PHOS, EMCAL, CTS)
22 //                 : AliCaloTrackAODReader: Fills AOD data in 3 TObjArrays (PHOS, EMCAL, CTS) 
23 //-- Author: Gustavo Conesa (LNF-INFN) 
24 //////////////////////////////////////////////////////////////////////////////
25
26
27 // --- ROOT system ---
28 #include <TFile.h>
29 #include <TGeoManager.h>
30
31 // ---- ANALYSIS system ----
32 #include "AliMCEvent.h"
33 #include "AliAODMCHeader.h"
34 #include "AliGenPythiaEventHeader.h"
35 #include "AliESDEvent.h"
36 #include "AliAODEvent.h"
37 #include "AliVTrack.h"
38 #include "AliVParticle.h"
39 #include "AliMixedEvent.h"
40 #include "AliESDtrack.h"
41 #include "AliESDtrackCuts.h"
42 #include "AliTriggerAnalysis.h"
43 #include "AliESDVZERO.h"
44 #include "AliVCaloCells.h"
45 #include "AliAnalysisManager.h"
46 #include "AliInputEventHandler.h"
47
48 // ---- Detectors ----
49 #include "AliPHOSGeoUtils.h"
50 #include "AliEMCALGeometry.h"
51 #include "AliEMCALRecoUtils.h"
52
53 // ---- CaloTrackCorr ---
54 #include "AliCalorimeterUtils.h"
55 #include "AliCaloTrackReader.h"
56
57 ClassImp(AliCaloTrackReader)
58   
59   
60 //________________________________________
61 AliCaloTrackReader::AliCaloTrackReader() : 
62 TObject(),                   fEventNumber(-1), //fCurrentFileName(""),
63 fDataType(0),                fDebug(0), 
64 fFiducialCut(0x0),           fCheckFidCut(kFALSE), 
65 fComparePtHardAndJetPt(0),   fPtHardAndJetPtFactor(0),
66 fComparePtHardAndClusterPt(0),fPtHardAndClusterPtFactor(0),
67 fCTSPtMin(0),                fEMCALPtMin(0),                  fPHOSPtMin(0), 
68 fCTSPtMax(0),                fEMCALPtMax(0),                  fPHOSPtMax(0), 
69 fEMCALTimeCutMin(-10000),    fEMCALTimeCutMax(10000),
70 fEMCALParamTimeCutMin(),     fEMCALParamTimeCutMax(),
71 fUseParamTimeCut(kFALSE),
72 fAODBranchList(0x0),
73 fCTSTracks(0x0),             fEMCALClusters(0x0),             fPHOSClusters(0x0),
74 fEMCALCells(0x0),            fPHOSCells(0x0),
75 fInputEvent(0x0),            fOutputEvent(0x0),fMC(0x0),
76 fFillCTS(0),                 fFillEMCAL(0),                   fFillPHOS(0),
77 fFillEMCALCells(0),          fFillPHOSCells(0), 
78 fRecalculateClusters(kFALSE),fSelectEmbeddedClusters(kFALSE),
79 fTrackStatus(0),             fTrackFilterMask(0),             
80 fESDtrackCuts(0),            fConstrainTrack(kFALSE),
81 fSelectHybridTracks(0),      fSelectSPDHitTracks(kFALSE),
82 fTrackMult(0),               fTrackMultEtaCut(0.8),
83 fReadStack(kFALSE),          fReadAODMCParticles(kFALSE), 
84 fDeltaAODFileName(""),       fFiredTriggerClassName(""),      
85 fEventTriggerMask(0),        fMixEventTriggerMask(0),         fEventTriggerAtSE(0), 
86 fAnaLED(kFALSE),
87 fTaskName(""),               fCaloUtils(0x0), 
88 fMixedEvent(NULL),           fNMixedEvent(0),                 fVertex(NULL), 
89 fListMixedTracksEvents(),    fListMixedCaloEvents(),
90 fLastMixedTracksEvent(-1),   fLastMixedCaloEvent(-1),
91 fWriteOutputDeltaAOD(kFALSE),fOldAOD(kFALSE),                 fCaloFilterPatch(kFALSE),
92 fEMCALClustersListName(""),  fZvtxCut(0.),                    
93 fAcceptFastCluster(kFALSE),  fRemoveLEDEvents(kTRUE), 
94 fDoEventSelection(kFALSE),   fDoV0ANDEventSelection(kFALSE),  fUseEventsWithPrimaryVertex(kFALSE),
95 fTriggerAnalysis (0x0),
96 fNPileUpClusters(-1),        fNNonPileUpClusters(-1),         fNPileUpClustersCut(3),
97 fCentralityClass(""),        fCentralityOpt(0),
98 fEventPlaneMethod(""),       fImportGeometryFromFile(kFALSE), fImportGeometryFilePath("")
99 {
100   //Ctor
101
102   //Initialize parameters
103   InitParameters();
104 }
105
106 //_______________________________________
107 AliCaloTrackReader::~AliCaloTrackReader() 
108 {
109   //Dtor
110   
111   delete fFiducialCut ;
112         
113   if(fAODBranchList)
114   {
115     fAODBranchList->Delete();
116     delete fAODBranchList ;
117   }  
118   
119   if(fCTSTracks)
120   {
121     if(fDataType!=kMC)fCTSTracks->Clear() ; 
122     else              fCTSTracks->Delete() ; 
123     delete fCTSTracks ;
124   }
125   
126   if(fEMCALClusters)
127   {
128     if(fDataType!=kMC)fEMCALClusters->Clear("C") ; 
129     else              fEMCALClusters->Delete() ; 
130     delete fEMCALClusters ;
131   }
132   
133   if(fPHOSClusters)
134   {
135     if(fDataType!=kMC)fPHOSClusters->Clear("C") ; 
136     else              fPHOSClusters->Delete() ; 
137     delete fPHOSClusters ;
138   }
139   
140   if(fVertex)
141   {
142     for (Int_t i = 0; i < fNMixedEvent; i++) 
143     {
144       delete [] fVertex[i] ;
145       
146     }
147     delete [] fVertex ;
148         }
149   
150   delete fESDtrackCuts;
151   delete fTriggerAnalysis;
152   
153   //  Pointers not owned, done by the analysis frame
154   //  if(fInputEvent)  delete fInputEvent ;
155   //  if(fOutputEvent) delete fOutputEvent ;
156   //  if(fMC)          delete fMC ;  
157   //  Pointer not owned, deleted by maker
158   //  if (fCaloUtils) delete fCaloUtils ;
159   
160 }
161
162 //________________________________________________
163 Bool_t AliCaloTrackReader::ComparePtHardAndJetPt()
164 {
165   // Check the event, if the requested ptHard is much smaller than the jet pT, then there is a problem.
166   // Only for PYTHIA.
167   
168   //printf("AliCaloTrackReader::ComparePtHardAndJetPt() - GenHeaderName : %s\n",GetGenEventHeader()->ClassName());
169   
170   if(!strcmp(GetGenEventHeader()->ClassName(), "AliGenPythiaEventHeader"))
171   {
172     TParticle * jet =  0;
173     AliGenPythiaEventHeader* pygeh= (AliGenPythiaEventHeader*) GetGenEventHeader();
174     Int_t nTriggerJets =  pygeh->NTriggerJets();
175     Float_t ptHard = pygeh->GetPtHard();
176     
177     if(fDebug > 1) 
178       printf("AliCaloTrackReader::ComparePtHardAndJetPt() - Njets: %d, pT Hard %f\n",nTriggerJets, ptHard);
179     
180     Float_t tmpjet[]={0,0,0,0};
181     for(Int_t ijet = 0; ijet< nTriggerJets; ijet++)
182     {
183       pygeh->TriggerJet(ijet, tmpjet);
184       jet = new TParticle(94, 21, -1, -1, -1, -1, tmpjet[0],tmpjet[1],tmpjet[2],tmpjet[3], 0,0,0,0);
185       
186       if(fDebug > 1) 
187         printf("AliCaloTrackReader::ComparePtHardAndJetPt() - jet %d; pycell jet pT %f\n",ijet, jet->Pt());
188       
189       //Compare jet pT and pt Hard
190       if(jet->Pt() > fPtHardAndJetPtFactor * ptHard)
191       {
192         printf("AliCaloTrackReader::ComparePtHardAndJetPt() - Reject jet event with : pT Hard %2.2f, pycell jet pT %2.2f, rejection factor %1.1f\n",
193                 ptHard, jet->Pt(), fPtHardAndJetPtFactor);
194         return kFALSE;
195       }
196     }
197     
198     if(jet) delete jet; 
199   }
200   
201   return kTRUE ;
202   
203 }
204
205 //____________________________________________________________________
206 Bool_t AliCaloTrackReader::ComparePtHardAndClusterPt()
207 {
208   // Check the event, if the requested ptHard is smaller than the calorimeter cluster E, then there is a problem.
209   // Only for PYTHIA.
210   
211   if(!strcmp(GetGenEventHeader()->ClassName(), "AliGenPythiaEventHeader"))
212   {
213     AliGenPythiaEventHeader* pygeh= (AliGenPythiaEventHeader*) GetGenEventHeader();
214     Float_t ptHard = pygeh->GetPtHard();
215     
216     Int_t nclusters = fInputEvent->GetNumberOfCaloClusters();
217     for (Int_t iclus =  0; iclus <  nclusters; iclus++) 
218     {
219       AliVCluster * clus = fInputEvent->GetCaloCluster(iclus) ; 
220       Float_t ecluster = clus->E();
221       
222       if(ecluster > fPtHardAndClusterPtFactor*ptHard) 
223       {
224         printf("AliCaloTrackReader::ComparePtHardAndClusterPt() - Reject : ecluster %2.2f, calo %d, factor %2.2f, ptHard %f\n",ecluster,clus->GetType(),fPtHardAndClusterPtFactor,ptHard);
225
226         return kFALSE;
227       }
228     }
229     
230   }
231   
232   return kTRUE ;
233   
234 }
235
236 //____________________________________________
237 AliStack* AliCaloTrackReader::GetStack() const 
238 {
239   //Return pointer to stack
240   if(fMC)
241     return fMC->Stack();
242   else
243   {
244     if(fDebug > 1) printf("AliCaloTrackReader::GetStack() - Stack is not available\n"); 
245     return 0x0 ;
246   }
247 }
248
249 //__________________________________________________
250 TString AliCaloTrackReader::GetFiredTriggerClasses() 
251
252   // List of triggered classes in a TString
253
254   AliESDEvent* esdevent = dynamic_cast<AliESDEvent*> (GetInputEvent());
255   AliAODEvent* aodevent = dynamic_cast<AliAODEvent*> (GetInputEvent());
256     
257   if     (esdevent) return esdevent->GetFiredTriggerClasses();
258   else if(aodevent) return aodevent->GetFiredTriggerClasses();
259   else              return ""; // Mixed Event, MC event, does not have this trigger info
260
261 }
262
263 //______________________________________________
264 AliHeader* AliCaloTrackReader::GetHeader() const 
265 {
266   //Return pointer to header
267   if(fMC)
268   {
269     return fMC->Header();
270   }
271   else
272   {
273     printf("AliCaloTrackReader::Header is not available\n"); 
274     return 0x0 ;
275   }
276 }
277
278 //______________________________________________________________
279 AliGenEventHeader* AliCaloTrackReader::GetGenEventHeader() const 
280 {
281   //Return pointer to Generated event header
282   if     (ReadStack() && fMC)
283   {
284     return fMC->GenEventHeader();
285   }
286   else if(ReadAODMCParticles() && GetAODMCHeader())
287   {
288     //printf("AliCaloTrackReader::GetGenEventHeader() - N headers %d\n",GetAODMCHeader()->GetNCocktailHeaders());
289     if( GetAODMCHeader()->GetNCocktailHeaders() > 0)
290       return GetAODMCHeader()->GetCocktailHeader(0) ;
291     else 
292       return 0x0;
293   }
294   else 
295   {
296     //printf("AliCaloTrackReader::GetGenEventHeader() - MC header not available! \n");
297     return 0;
298   }
299 }
300
301 //____________________________________________________________________
302 TClonesArray* AliCaloTrackReader::GetAODMCParticles(Int_t input) const 
303 {
304   //Return list of particles in AOD. Do it for the corresponding input event.
305   
306   TClonesArray * rv = NULL ; 
307   if(fDataType == kAOD)
308   {
309     if(input == 0)
310     {
311       //Normal input AOD
312       AliAODEvent * evt = dynamic_cast<AliAODEvent*> (fInputEvent) ;
313       if(evt)
314         rv = (TClonesArray*)evt->FindListObject("mcparticles");
315       else  
316         printf("AliCaloTrackReader::GetAODMCParticles() - wrong AOD input index? %d, or non existing tree? \n",input); 
317     }  
318     
319   } 
320   else 
321   {
322     printf("AliCaloTrackReader::GetAODMCParticles() - Input are not AODs\n"); 
323   }
324   
325   return rv ; 
326 }
327
328 //________________________________________________________
329 AliAODMCHeader* AliCaloTrackReader::GetAODMCHeader() const 
330 {
331   //Return MC header in AOD. Do it for the corresponding input event.
332   
333   AliAODMCHeader *mch = NULL;
334   
335   if(fDataType == kAOD)
336   {
337     AliAODEvent * aod = dynamic_cast<AliAODEvent*> (fInputEvent);
338     if(aod) mch = dynamic_cast<AliAODMCHeader*>(aod->FindListObject("mcHeader"));
339   }
340   else 
341   {
342     printf("AliCaloTrackReader::GetAODMCHeader() - Input are not AODs\n");
343   }
344   
345   return mch;
346 }
347
348 //_____________________________
349 void AliCaloTrackReader::Init()
350 {
351   //Init reader. Method to be called in AliAnaPartCorrMaker
352
353   //printf(" AliCaloTrackReader::Init() %p \n",gGeoManager);
354
355   if(fReadStack && fReadAODMCParticles)
356   {
357     printf("AliCaloTrackReader::Init() - Cannot access stack and mcparticles at the same time, change them \n");
358     fReadStack          = kFALSE;
359     fReadAODMCParticles = kFALSE;
360   }
361   
362   // Init geometry, I do not like much to do it like this ...
363   if(fImportGeometryFromFile && !gGeoManager) 
364   {
365     if(fImportGeometryFilePath=="") // If not specified, set a default location
366     fImportGeometryFilePath = "$ALICE_ROOT/OADB/EMCAL/geometry_2011.root"; // "$ALICE_ROOT/EVE/alice-data/default_geo.root"
367
368     printf("AliCaloTrackReader::Init() - Import %s\n",fImportGeometryFilePath.Data());
369     TGeoManager::Import(fImportGeometryFilePath) ; // default need file "geometry.root" in local dir!!!!
370   }
371
372 }
373
374 //_______________________________________
375 void AliCaloTrackReader::InitParameters()
376 {
377   //Initialize the parameters of the analysis.
378   fDataType   = kESD ;
379   fCTSPtMin   = 0.1 ;
380   fEMCALPtMin = 0.1 ;
381   fPHOSPtMin  = 0.1 ;
382   fCTSPtMax   = 1000. ;
383   fEMCALPtMax = 1000. ;
384   fPHOSPtMax  = 1000. ;
385   
386   //Do not filter the detectors input by default.
387   fFillEMCAL      = kFALSE;
388   fFillPHOS       = kFALSE;
389   fFillCTS        = kFALSE;
390   fFillEMCALCells = kFALSE;
391   fFillPHOSCells  = kFALSE;
392   
393   fReadStack             = kFALSE; // Check in the constructor of the other readers if it was set or in the configuration file
394   fReadAODMCParticles    = kFALSE; // Check in the constructor of the other readers if it was set or in the configuration file
395   fDeltaAODFileName      = "deltaAODPartCorr.root";
396   fFiredTriggerClassName = "";
397   fEventTriggerMask      = AliVEvent::kAny;
398   fMixEventTriggerMask   = AliVEvent::kAnyINT;
399   fEventTriggerAtSE      = kTRUE; // Use only events that pass event selection at SE base class
400   
401   fAcceptFastCluster = kTRUE;
402   fAnaLED            = kFALSE;
403   
404   //We want tracks fitted in the detectors:
405   //fTrackStatus=AliESDtrack::kTPCrefit;
406   //fTrackStatus|=AliESDtrack::kITSrefit; 
407   fTrackStatus     = 0;
408   fTrackFilterMask = 128; //For AODs, but what is the difference between fTrackStatus and fTrackFilterMask?
409   
410   fESDtrackCuts = AliESDtrackCuts::GetStandardTPCOnlyTrackCuts(); //initialize with TPC only tracks 
411   
412   fConstrainTrack = kFALSE ; // constrain tracks to vertex
413   
414   fV0ADC[0] = 0;   fV0ADC[1] = 0; 
415   fV0Mul[0] = 0;   fV0Mul[1] = 0; 
416   
417   fZvtxCut   = 10.;
418   
419   fNMixedEvent = 1;
420   
421   fPtHardAndJetPtFactor     = 7.;
422   fPtHardAndClusterPtFactor = 1.;
423
424   //Centrality
425   fCentralityClass  = "V0M";
426   fCentralityOpt    = 10;
427   fCentralityBin[0] = fCentralityBin[1]=-1;
428   
429   fEventPlaneMethod = "V0";
430
431   // Allocate memory (not sure this is the right place)
432   fCTSTracks       = new TObjArray();
433   fEMCALClusters   = new TObjArray();
434   fPHOSClusters    = new TObjArray(); 
435   fTriggerAnalysis = new AliTriggerAnalysis;
436   fAODBranchList   = new TList ;
437
438   fImportGeometryFromFile = kFALSE;
439   
440   fPileUpParamSPD[0] = 3   ; fPileUpParamSPD[1] = 0.8 ;
441   fPileUpParamSPD[2] = 3.0 ; fPileUpParamSPD[3] = 2.0 ; fPileUpParamSPD[4] = 5.0;
442   
443   // Parametrized time cut (LHC11d)
444   fEMCALParamTimeCutMin[0] =-5; fEMCALParamTimeCutMin[1] =-1 ; fEMCALParamTimeCutMin[2] = 3.5 ; fEMCALParamTimeCutMin[3] = 1.  ;   
445   fEMCALParamTimeCutMax[0] = 5; fEMCALParamTimeCutMax[1] = 50; fEMCALParamTimeCutMax[2] = 0.45; fEMCALParamTimeCutMax[3] = 1.25;   
446
447   // Parametrized time cut (LHC11c)
448   //fEMCALParamTimeCutMin[0] =-5;   fEMCALParamTimeCutMin[1] =-1 ; fEMCALParamTimeCutMin[2] = 1.87; fEMCALParamTimeCutMin[3] = 0.4;   
449   //fEMCALParamTimeCutMax[0] = 3.5; fEMCALParamTimeCutMax[1] = 50; fEMCALParamTimeCutMax[2] = 0.15; fEMCALParamTimeCutMax[3] = 1.6;   
450 }
451
452 //___________________________________________________________
453 Bool_t AliCaloTrackReader::IsInTimeWindow(const Double_t tof, const Float_t energy) const
454 {
455   // Cluster time selection window
456   
457   // Parametrized cut depending on E
458   if(fUseParamTimeCut)
459   {
460     Float_t minCut= fEMCALParamTimeCutMin[0]+fEMCALParamTimeCutMin[1]*TMath::Exp(-(energy-fEMCALParamTimeCutMin[2])/fEMCALParamTimeCutMin[3]);
461     Float_t maxCut= fEMCALParamTimeCutMax[0]+fEMCALParamTimeCutMax[1]*TMath::Exp(-(energy-fEMCALParamTimeCutMax[2])/fEMCALParamTimeCutMax[3]);
462     //printf("tof %f, minCut %f, maxCut %f\n",tof,minCut,maxCut);
463     if( tof < minCut || tof > maxCut )  return kFALSE ;
464   }
465   
466   //In any case, the time should to be larger than the fixed window ...
467   if( tof < fEMCALTimeCutMin  || tof > fEMCALTimeCutMax )  return kFALSE ;
468   
469   return kTRUE ;
470 }
471
472 //________________________________________________
473 Bool_t AliCaloTrackReader::IsPileUpFromSPD() const
474 {
475   // Check if event is from pile-up determined by SPD
476   // Default values: (3, 0.8, 3., 2., 5.)
477   return fInputEvent->IsPileupFromSPD((Int_t) fPileUpParamSPD[0] , fPileUpParamSPD[1] , 
478                                               fPileUpParamSPD[2] , fPileUpParamSPD[3] , fPileUpParamSPD[4] ); 
479   //printf("Param : %d, %2.2f, %2.2f, %2.2f, %2.2f\n",(Int_t) fPileUpParamSPD[0], fPileUpParamSPD[1], fPileUpParamSPD[2], fPileUpParamSPD[3], fPileUpParamSPD[4]);
480
481 }
482
483 //__________________________________________________
484 Bool_t AliCaloTrackReader::IsPileUpFromEMCal() const
485 {
486   // Check if event is from pile-up determined by EMCal
487   if(fNPileUpClusters > fNPileUpClustersCut) return kTRUE ;
488   else                                       return kFALSE;
489 }
490
491 //________________________________________________________
492 Bool_t AliCaloTrackReader::IsPileUpFromSPDAndEMCal() const
493 {
494   // Check if event is from pile-up determined by SPD and EMCal
495   if( IsPileUpFromSPD() && IsPileUpFromEMCal()) return kTRUE ;
496   else                                          return kFALSE;
497 }
498
499 //_______________________________________________________
500 Bool_t AliCaloTrackReader::IsPileUpFromSPDOrEMCal() const
501 {
502   // Check if event is from pile-up determined by SPD or EMCal
503   if( IsPileUpFromSPD() || IsPileUpFromEMCal()) return kTRUE ;
504   else                                          return kFALSE;
505 }
506
507 //___________________________________________________________
508 Bool_t AliCaloTrackReader::IsPileUpFromSPDAndNotEMCal() const
509 {
510   // Check if event is from pile-up determined by SPD and not by EMCal
511   if( IsPileUpFromSPD() && !IsPileUpFromEMCal()) return kTRUE ;
512   else                                          return kFALSE;
513 }
514
515 //___________________________________________________________
516 Bool_t AliCaloTrackReader::IsPileUpFromEMCalAndNotSPD() const
517 {
518   // Check if event is from pile-up determined by EMCal, not by SPD
519   if( !IsPileUpFromSPD() && IsPileUpFromEMCal()) return kTRUE ;
520   else                                           return kFALSE;
521 }
522
523 //______________________________________________________________
524 Bool_t AliCaloTrackReader::IsPileUpFromNotSPDAndNotEMCal() const
525 {
526   // Check if event not from pile-up determined neither by SPD nor by EMCal
527   if( !IsPileUpFromSPD() && !IsPileUpFromEMCal()) return kTRUE ;
528   else                                            return kFALSE;
529 }
530
531 //________________________________________________________
532 void AliCaloTrackReader::Print(const Option_t * opt) const
533 {
534   
535   //Print some relevant parameters set for the analysis
536   if(! opt)
537     return;
538   
539   printf("***** Print: %s %s ******\n",    GetName(), GetTitle() ) ;
540   printf("Task name      : %s\n",          fTaskName.Data()) ;
541   printf("Data type      : %d\n",          fDataType) ;
542   printf("CTS Min pT     : %2.1f GeV/c\n", fCTSPtMin) ;
543   printf("EMCAL Min pT   : %2.1f GeV/c\n", fEMCALPtMin) ;
544   printf("PHOS Min pT    : %2.1f GeV/c\n", fPHOSPtMin) ;
545   printf("CTS Max pT     : %2.1f GeV/c\n", fCTSPtMax) ;
546   printf("EMCAL Max pT   : %2.1f GeV/c\n", fEMCALPtMax) ;
547   printf("PHOS Max pT    : %2.1f GeV/c\n", fPHOSPtMax) ;
548   printf("EMCAL Time Cut: %3.1f < TOF  < %3.1f\n", fEMCALTimeCutMin, fEMCALTimeCutMax);
549   printf("Use CTS         =     %d\n",     fFillCTS) ;
550   printf("Use EMCAL       =     %d\n",     fFillEMCAL) ;
551   printf("Use PHOS        =     %d\n",     fFillPHOS) ;
552   printf("Use EMCAL Cells =     %d\n",     fFillEMCALCells) ;
553   printf("Use PHOS  Cells =     %d\n",     fFillPHOSCells) ;
554   printf("Track status    =     %d\n", (Int_t) fTrackStatus) ;
555   printf("AODs Track filter mask  =  %d or hybrid %d, SPD hit %d\n", (Int_t) fTrackFilterMask,fSelectHybridTracks,fSelectSPDHitTracks) ;
556   printf("Track Mult Eta Cut =  %d\n", (Int_t) fTrackMultEtaCut) ;
557   printf("Write delta AOD =     %d\n",     fWriteOutputDeltaAOD) ;
558   printf("Recalculate Clusters = %d\n",    fRecalculateClusters) ;
559   
560   printf("Use Triggers selected in SE base class %d; If not what trigger Mask? %d; Trigger max for mixed %d \n", 
561          fEventTriggerAtSE, fEventTriggerMask,fMixEventTriggerMask);
562   
563   if(fComparePtHardAndClusterPt)
564           printf("Compare jet pt and pt hard to accept event, factor = %2.2f",fPtHardAndJetPtFactor);
565   
566   if(fComparePtHardAndClusterPt)
567           printf("Compare cluster pt and pt hard to accept event, factor = %2.2f",fPtHardAndClusterPtFactor);
568   
569   printf("Read Kine from, stack? %d, AOD ? %d \n", fReadStack, fReadAODMCParticles) ;
570   printf("Delta AOD File Name =     %s\n", fDeltaAODFileName.Data()) ;
571   printf("Centrality: Class %s, Option %d, Bin [%d,%d] \n", fCentralityClass.Data(),fCentralityOpt,fCentralityBin[0], fCentralityBin[1]) ;
572   
573   printf("    \n") ;
574   
575
576
577 //_________________________________________________________________________
578 Bool_t AliCaloTrackReader::FillInputEvent(const Int_t iEntry, 
579                                           const char * /*currentFileName*/) 
580 {
581   //Fill the event counter and input lists that are needed, called by the analysis maker.
582     
583   fEventNumber = iEntry;
584   //fCurrentFileName = TString(currentFileName);
585   if(!fInputEvent)
586   {
587           if(fDebug >= 0) printf("AliCaloTrackReader::FillInputEvent() - Input event not available, skip event analysis\n");
588           return kFALSE;
589   }
590   
591   //Select events only fired by a certain trigger configuration if it is provided
592   Int_t eventType = 0;
593   if(fInputEvent->GetHeader())
594           eventType = ((AliVHeader*)fInputEvent->GetHeader())->GetEventType();
595   
596   if (GetFiredTriggerClasses().Contains("FAST")  && !GetFiredTriggerClasses().Contains("ALL") && !fAcceptFastCluster) 
597   {
598     if(fDebug > 0)  printf("AliCaloTrackReader::FillInputEvent - Do not count events from fast cluster, trigger name %s\n",fFiredTriggerClassName.Data());
599     return kFALSE;
600   }
601   
602   //-------------------------------------------------------------------------------------
603   // Reject event if large clusters with large energy
604   // Use only for LHC11a data for the moment, and if input is clusterizer V1 or V1+unfolding
605   // If clusterzer NxN or V2 it does not help
606   //-------------------------------------------------------------------------------------
607   Int_t run = fInputEvent->GetRunNumber();
608   if( fRemoveLEDEvents && run > 146857  && run < 146861 )
609   {
610     //printf("Event %d\n",GetEventNumber());
611     
612     // Count number of cells with energy larger than 0.1 in SM3, cut on this number
613     Int_t ncellsSM3 = 0;
614     for(Int_t icell = 0; icell < fInputEvent->GetEMCALCells()->GetNumberOfCells(); icell++)
615     {
616       Int_t absID = fInputEvent->GetEMCALCells()->GetCellNumber(icell);
617       Int_t sm    = GetCaloUtils()->GetEMCALGeometry()->GetSuperModuleNumber(absID);
618       if(fInputEvent->GetEMCALCells()->GetAmplitude(icell) > 0.1 && sm==3) ncellsSM3++;
619     }
620     
621     Int_t ncellcut = 21;
622     if(fFiredTriggerClassName.Contains("EMC")) ncellcut = 35;
623     
624     if(ncellsSM3 >= ncellcut)
625     {
626       if(fDebug > 0) printf(" AliCaloTrackReader::FillInputEvent() - reject event with ncells in SM3 %d\n",ncellsSM3);
627       return kFALSE;
628     }
629   }// Remove LED events
630   
631   // Reject pure LED events?
632   if( fFiredTriggerClassName  !="" && !fAnaLED)
633   {
634     //printf("Event type %d\n",eventType);
635     if(eventType!=7)
636       return kFALSE; //Only physics event, do not use for simulated events!!!
637     
638     if(fDebug > 0) 
639       printf("AliCaloTrackReader::FillInputEvent() - FiredTriggerClass <%s>, selected class <%s>, compare name %d\n",
640              GetFiredTriggerClasses().Data(),fFiredTriggerClassName.Data(), GetFiredTriggerClasses().Contains(fFiredTriggerClassName));
641     
642     if( !GetFiredTriggerClasses().Contains(fFiredTriggerClassName) ) return kFALSE;
643     else if(fDebug > 0) printf("AliCaloTrackReader::FillInputEvent() - Accepted triggered event\n");
644   }
645   else if(fAnaLED)
646   {
647     //    kStartOfRun =       1,    // START_OF_RUN
648     //    kEndOfRun =         2,    // END_OF_RUN
649     //    kStartOfRunFiles =  3,    // START_OF_RUN_FILES
650     //    kEndOfRunFiles =    4,    // END_OF_RUN_FILES
651     //    kStartOfBurst =     5,    // START_OF_BURST
652     //    kEndOfBurst =       6,    // END_OF_BURST
653     //    kPhysicsEvent =     7,    // PHYSICS_EVENT
654     //    kCalibrationEvent = 8,    // CALIBRATION_EVENT
655     //    kFormatError =      9,    // EVENT_FORMAT_ERROR
656     //    kStartOfData =      10,   // START_OF_DATA
657     //    kEndOfData =        11,   // END_OF_DATA
658     //    kSystemSoftwareTriggerEvent   = 12, // SYSTEM_SOFTWARE_TRIGGER_EVENT
659     //    kDetectorSoftwareTriggerEvent = 13  // DETECTOR_SOFTWARE_TRIGGER_EVENT
660     
661           if(eventType!=7 && fDebug > 1 )printf("AliCaloTrackReader::FillInputEvent() - DO LED, Event Type <%d>, 8 Calibration \n",  eventType);
662           if(eventType!=8)return kFALSE;
663   }
664   
665   //In case of analysis of events with jets, skip those with jet pt > 5 pt hard 
666   if(fComparePtHardAndJetPt) 
667   {
668     if(!ComparePtHardAndJetPt()) return kFALSE ;
669   }
670   
671   if(fComparePtHardAndClusterPt) 
672   {
673     if(!ComparePtHardAndClusterPt()) return kFALSE ;
674   }
675   
676   //Fill Vertex array
677   FillVertexArray();
678   //Reject events with Z vertex too large, only for SE analysis, if not, cut on the analysis code
679   if(!GetMixedEvent() && TMath::Abs(fVertex[0][2]) > fZvtxCut) return kFALSE;  
680   
681   //------------------------------------------------------
682   //Event rejection depending on vertex, pileup, v0and
683   //------------------------------------------------------
684
685   if(fUseEventsWithPrimaryVertex)
686   {
687     if( !CheckForPrimaryVertex() )              return kFALSE;
688     if( TMath::Abs(fVertex[0][0] ) < 1.e-6 && 
689         TMath::Abs(fVertex[0][1] ) < 1.e-6 && 
690         TMath::Abs(fVertex[0][2] ) < 1.e-6    ) return kFALSE;
691   }
692   
693   //printf("Reader : IsPileUp %d, Multi %d\n",IsPileUpFromSPD(),fInputEvent->IsPileupFromSPDInMultBins());
694   
695   if(fDoEventSelection)
696   {
697     if(!fCaloFilterPatch)
698     {
699       // Do not analyze events with pileup
700       Bool_t bPileup = IsPileUpFromSPD();
701       //IsPileupFromSPDInMultBins() // method to try
702       //printf("pile-up %d, %d, %2.2f, %2.2f, %2.2f, %2.2f\n",bPileup, (Int_t) fPileUpParamSPD[0], fPileUpParamSPD[1], fPileUpParamSPD[2], fPileUpParamSPD[3], fPileUpParamSPD[4]);
703       if(bPileup) return kFALSE;
704       
705       if(fDoV0ANDEventSelection)
706       {
707         Bool_t bV0AND = kTRUE; 
708         AliESDEvent* esd = dynamic_cast<AliESDEvent*> (fInputEvent);
709         if(esd) 
710           bV0AND = fTriggerAnalysis->IsOfflineTriggerFired(esd, AliTriggerAnalysis::kV0AND);
711         //else bV0AND = //FIXME FOR AODs
712         if(!bV0AND) return kFALSE;
713       }
714     }//CaloFilter patch
715     else
716     { 
717       if(fInputEvent->GetNumberOfCaloClusters() > 0) 
718       {
719         AliVCluster * calo = fInputEvent->GetCaloCluster(0);
720         if(calo->GetNLabels() == 4)
721         {
722           Int_t * selection = calo->GetLabels();
723           Bool_t bPileup = selection[0];
724           if(bPileup) return kFALSE;
725           
726           Bool_t bGoodV = selection[1]; 
727           if(fUseEventsWithPrimaryVertex && !bGoodV) return kFALSE;
728           
729           if(fDoV0ANDEventSelection)
730           {
731             Bool_t bV0AND = selection[2]; 
732             if(!bV0AND) return kFALSE;
733           }
734           
735           fTrackMult = selection[3];
736           if(fTrackMult == 0) return kFALSE;
737         } 
738         else 
739         {
740           //First filtered AODs, track multiplicity stored there.  
741           fTrackMult = (Int_t) ((AliAODHeader*)fInputEvent->GetHeader())->GetCentrality();
742           if(fTrackMult == 0) return kFALSE;          
743         }
744       }//at least one cluster
745       else 
746       {
747         //printf("AliCaloTrackReader::FillInputEvent() - No clusters in event\n");
748         //Remove events with  vertex (0,0,0), bad vertex reconstruction
749         if(fUseEventsWithPrimaryVertex && TMath::Abs(fVertex[0][0]) < 1.e-6 && TMath::Abs(fVertex[0][1]) < 1.e-6 && TMath::Abs(fVertex[0][2]) < 1.e-6) return kFALSE;
750         
751         //First filtered AODs, track multiplicity stored there.  
752         fTrackMult = (Int_t) ((AliAODHeader*)fInputEvent->GetHeader())->GetCentrality();
753         if(fTrackMult == 0) return kFALSE;
754       }// no cluster
755     }// CaloFileter patch
756   }// Event selection
757   //------------------------------------------------------
758   
759   //Check if there is a centrality value, PbPb analysis, and if a centrality bin selection is requested
760   //If we need a centrality bin, we select only those events in the corresponding bin.
761   if(GetCentrality() && fCentralityBin[0]>=0 && fCentralityBin[1]>=0 && fCentralityOpt==100)
762   {
763     Int_t cen = GetEventCentrality();
764     if(cen > fCentralityBin[1] || cen < fCentralityBin[0]) return kFALSE; //reject events out of bin.
765   }
766   
767   //Fill the arrays with cluster/tracks/cells data
768   
769   if(fFillEMCALCells) 
770     FillInputEMCALCells();
771   
772   if(fFillPHOSCells)  
773     FillInputPHOSCells();
774   
775   FillInputVZERO();
776   
777   if(fEventTriggerAtSE)
778   {
779     if(fFillCTS)
780     {   
781       FillInputCTS();
782       //Accept events with at least one track
783       if(fTrackMult == 0 && fDoEventSelection) return kFALSE;
784     }
785     if(fFillEMCAL) 
786       FillInputEMCAL();
787     if(fFillPHOS)  
788       FillInputPHOS();
789   }
790   else 
791   {
792     // In case of mixing analysis, all triggers accepted, but trigger particles selected 
793     // only for the specific trigered events selected here. Mixing done only for MB events,
794     // tracks array filled also for those events and not the others.
795
796     AliAnalysisManager *manager = AliAnalysisManager::GetAnalysisManager();
797     AliInputEventHandler *inputHandler = dynamic_cast<AliInputEventHandler*>(manager->GetInputEventHandler());
798     
799     if(!inputHandler) return kFALSE ;  // to content coverity
800     
801     UInt_t isTrigger = inputHandler->IsEventSelected() & fEventTriggerMask;
802     UInt_t isMB      = inputHandler->IsEventSelected() & fMixEventTriggerMask;
803     
804     if(fFillCTS && (isMB || isTrigger))
805     {   
806       FillInputCTS();
807       //Accept events with at least one track
808       if(fTrackMult == 0 && fDoEventSelection) return kFALSE;
809     }
810     
811     if(isTrigger)
812     {
813       if(fFillEMCAL) 
814         FillInputEMCAL();
815       if(fFillPHOS)  
816         FillInputPHOS();
817     } 
818   }
819   
820   return kTRUE ;
821 }
822
823 //___________________________________
824 void AliCaloTrackReader::ResetLists() 
825 {
826   //  Reset lists, called by the analysis maker 
827   
828   if(fCTSTracks)       fCTSTracks     -> Clear();
829   if(fEMCALClusters)   fEMCALClusters -> Clear("C");
830   if(fPHOSClusters)    fPHOSClusters  -> Clear("C");
831   
832   fV0ADC[0] = 0;   fV0ADC[1] = 0; 
833   fV0Mul[0] = 0;   fV0Mul[1] = 0; 
834   
835 }
836
837 //____________________________________________________________
838 void AliCaloTrackReader::SetInputEvent(AliVEvent* const input)  
839 {
840   fInputEvent  = input;
841   fMixedEvent = dynamic_cast<AliMixedEvent*>(GetInputEvent()) ; 
842   if (fMixedEvent) 
843     fNMixedEvent = fMixedEvent->GetNumberOfEvents() ; 
844   
845   //Delete previous vertex
846   if(fVertex)
847   {
848     for (Int_t i = 0; i < fNMixedEvent; i++) 
849     {
850       delete [] fVertex[i] ; 
851     }
852     delete [] fVertex ;
853   }
854   
855   fVertex = new Double_t*[fNMixedEvent] ; 
856   for (Int_t i = 0; i < fNMixedEvent; i++) 
857   {
858     fVertex[i] = new Double_t[3] ; 
859     fVertex[i][0] = 0.0 ; 
860     fVertex[i][1] = 0.0 ; 
861     fVertex[i][2] = 0.0 ; 
862   }
863 }
864
865 //__________________________________________________
866 Int_t AliCaloTrackReader::GetEventCentrality() const 
867 {
868   //Return current event centrality
869   
870   if(GetCentrality())
871   {
872     if     (fCentralityOpt==100) return (Int_t) GetCentrality()->GetCentralityPercentile(fCentralityClass); // 100 bins max
873     else if(fCentralityOpt==10)  return GetCentrality()->GetCentralityClass10(fCentralityClass);// 10 bins max
874     else if(fCentralityOpt==20)  return GetCentrality()->GetCentralityClass5(fCentralityClass); // 20 bins max
875     else 
876     {
877       printf("AliCaloTrackReader::GetEventCentrality() - Unknown centrality option %d, use 10, 20 or 100\n",fCentralityOpt);
878       return -1;
879     } 
880   }
881   else return -1;
882   
883 }
884
885 //_____________________________________________________
886 Double_t AliCaloTrackReader::GetEventPlaneAngle() const 
887 {
888   //Return current event centrality
889   
890   if(GetEventPlane())
891   {
892     Float_t ep =  GetEventPlane()->GetEventplane(GetEventPlaneMethod(), GetInputEvent());
893     
894     if(GetEventPlaneMethod()=="Q" && (ep < 0 || ep > TMath::Pi())) 
895     {
896       if(fDebug > 0 ) printf("AliCaloTrackReader::GetEventPlaneAngle() -  Bad EP for <Q> method : %f\n",ep);
897       return -1000;
898     }
899     else if(GetEventPlaneMethod().Contains("V0")  ) 
900     {
901       if((ep > TMath::Pi()/2 || ep < -TMath::Pi()/2))
902       {
903         if(fDebug > 0 ) printf("AliCaloTrackReader::GetEventPlaneAngle() -  Bad EP for <%s> method : %f\n",GetEventPlaneMethod().Data(), ep);
904         return -1000;
905       }
906       
907       ep+=TMath::Pi()/2; // put same range as for <Q> method
908       
909     }
910   
911     //printf("AliCaloTrackReader::GetEventPlaneAngle() = %f\n",ep);
912     if(fDebug > 0 )
913     {
914       if     (ep > TMath::Pi()) printf("AliCaloTrackReader::GetEventPlaneAngle() - Too large angle = %f\n",ep);
915       else if(ep < 0          ) printf("AliCaloTrackReader::GetEventPlaneAngle() - Negative angle = %f\n" ,ep);
916     }
917     
918     return ep;
919   }
920   else
921   {
922     if(fDataType!=kMC && fDebug > 0) printf("AliCaloTrackReader::GetEventPlaneAngle() -  No EP pointer\n");
923     return -1000;
924   } 
925   
926 }
927
928 //__________________________________________________________
929 void AliCaloTrackReader::GetVertex(Double_t vertex[3]) const 
930 {
931   //Return vertex position to be used for single event analysis
932   vertex[0]=fVertex[0][0];  
933   vertex[1]=fVertex[0][1];  
934   vertex[2]=fVertex[0][2];
935 }
936
937 //____________________________________________________________
938 void AliCaloTrackReader::GetVertex(Double_t vertex[3], 
939                                    const Int_t evtIndex) const 
940 {
941   //Return vertex position for mixed event, recover the vertex in a particular event.
942   
943   vertex[0]=fVertex[evtIndex][0];  vertex[1]=fVertex[evtIndex][1];  vertex[2]=fVertex[evtIndex][2];
944   
945 }
946
947 //________________________________________
948 void AliCaloTrackReader::FillVertexArray() 
949 {
950   
951   //Fill data member with vertex
952   //In case of Mixed event, multiple vertices
953   
954   //Delete previous vertex
955   if(fVertex)
956   {
957     for (Int_t i = 0; i < fNMixedEvent; i++) 
958     {
959       delete [] fVertex[i] ; 
960     }
961     delete [] fVertex ;  
962   }
963   
964   fVertex = new Double_t*[fNMixedEvent] ; 
965   for (Int_t i = 0; i < fNMixedEvent; i++) 
966   {
967     fVertex[i] = new Double_t[3] ; 
968     fVertex[i][0] = 0.0 ; 
969     fVertex[i][1] = 0.0 ; 
970     fVertex[i][2] = 0.0 ; 
971   }          
972   
973   if (!fMixedEvent) 
974   { //Single event analysis
975     if(fDataType!=kMC)
976     {
977       
978       if(fInputEvent->GetPrimaryVertex())
979       {
980         fInputEvent->GetPrimaryVertex()->GetXYZ(fVertex[0]); 
981       }
982       else 
983       {
984         printf("AliCaloTrackReader::FillVertexArray() - NULL primary vertex\n");
985         fVertex[0][0]=0.;   fVertex[0][1]=0.;   fVertex[0][2]=0.;
986       }//Primary vertex pointer do not exist
987       
988     } else
989     {//MC read event 
990       fVertex[0][0]=0.;   fVertex[0][1]=0.;   fVertex[0][2]=0.;
991     }
992     
993     if(fDebug > 1)
994       printf("AliCaloTrackReader::FillVertexArray() - Single Event Vertex : %f,%f,%f\n",fVertex[0][0],fVertex[0][1],fVertex[0][2]);
995     
996   } else 
997   { // MultiEvent analysis
998     for (Int_t iev = 0; iev < fNMixedEvent; iev++) 
999     {
1000       if (fMixedEvent->GetVertexOfEvent(iev))
1001         fMixedEvent->GetVertexOfEvent(iev)->GetXYZ(fVertex[iev]);
1002       else
1003       { // no vertex found !!!!
1004         AliWarning("No vertex found");
1005       }
1006       
1007       if(fDebug > 1)
1008         printf("AliCaloTrackReader::FillVertexArray() - Multi Event %d Vertex : %f,%f,%f\n",iev,fVertex[iev][0],fVertex[iev][1],fVertex[iev][2]);
1009       
1010     }
1011   }
1012   
1013 }
1014
1015 //_____________________________________
1016 void AliCaloTrackReader::FillInputCTS() 
1017 {
1018   //Return array with Central Tracking System (CTS) tracks
1019   
1020   if(fDebug > 2 ) printf("AliCaloTrackReader::FillInputCTS()\n");
1021   
1022   Double_t pTrack[3] = {0,0,0};
1023   
1024   Int_t nTracks = fInputEvent->GetNumberOfTracks() ;
1025   fTrackMult    = 0;
1026   Int_t nstatus = 0;
1027   
1028   for (Int_t itrack =  0; itrack <  nTracks; itrack++) 
1029   {////////////// track loop
1030     AliVTrack * track = (AliVTrack*)fInputEvent->GetTrack(itrack) ; // retrieve track from esd
1031     
1032     //Select tracks under certain conditions, TPCrefit, ITSrefit ... check the set bits
1033     if (fTrackStatus && !((track->GetStatus() & fTrackStatus) == fTrackStatus)) 
1034       continue ;
1035     
1036     nstatus++;
1037     
1038     if     (fDataType==kESD)
1039     {
1040       AliESDtrack* esdTrack = dynamic_cast<AliESDtrack*> (track);
1041       
1042       if(esdTrack && fESDtrackCuts->AcceptTrack(esdTrack))
1043       {
1044         track->GetPxPyPz(pTrack) ;
1045
1046         if(fConstrainTrack)
1047         {
1048           if(esdTrack->GetConstrainedParam())
1049           {
1050             const AliExternalTrackParam* constrainParam = esdTrack->GetConstrainedParam();
1051             esdTrack->Set(constrainParam->GetX(),constrainParam->GetAlpha(),constrainParam->GetParameter(),constrainParam->GetCovariance());
1052             esdTrack->GetConstrainedPxPyPz(pTrack);
1053           }
1054           else continue;
1055           
1056         } // use constrained tracks
1057         
1058         if(fSelectSPDHitTracks)
1059         {//Not much sense to use with TPC only or Hybrid tracks
1060           if(!esdTrack->HasPointOnITSLayer(0) && !esdTrack->HasPointOnITSLayer(1)) continue ;
1061         }
1062       }
1063       else continue;
1064       
1065     } // ESD
1066     else if(fDataType==kAOD)
1067     {
1068       AliAODTrack *aodtrack = dynamic_cast <AliAODTrack*>(track);
1069       
1070       if(aodtrack)
1071       {
1072        if(fDebug > 2 ) printf("AliCaloTrackReader::FillInputCTS():AOD track type: %d (primary %d), hybrid? %d \n",
1073                               aodtrack->GetType(),AliAODTrack::kPrimary,
1074                               aodtrack->IsHybridGlobalConstrainedGlobal());
1075         
1076         
1077         if (fSelectHybridTracks)
1078         {
1079           if (!aodtrack->IsHybridGlobalConstrainedGlobal())       continue ;
1080         }
1081         else 
1082         {
1083           if ( aodtrack->TestFilterBit(fTrackFilterMask)==kFALSE) continue ;
1084         }
1085         
1086         if(fSelectSPDHitTracks)
1087         {//Not much sense to use with TPC only or Hybrid tracks
1088           if(!aodtrack->HasPointOnITSLayer(0) && !aodtrack->HasPointOnITSLayer(1)) continue ;
1089         }
1090         
1091         if (aodtrack->GetType()!= AliAODTrack::kPrimary)          continue ;
1092         
1093         if (fDebug > 2 ) printf("AliCaloTrackReader::FillInputCTS(): \t accepted track! \n");
1094         
1095         track->GetPxPyPz(pTrack) ;
1096         
1097       } // aod track exists
1098       else continue ;
1099       
1100     } // AOD
1101     
1102     //Count the tracks in eta < 0.9
1103     //printf("Eta %f cut  %f\n",TMath::Abs(track->Eta()),fTrackMultEtaCut);
1104     if(TMath::Abs(track->Eta())< fTrackMultEtaCut) fTrackMult++;
1105     
1106     TLorentzVector momentum(pTrack[0],pTrack[1],pTrack[2],0);
1107     
1108     if(fCTSPtMin < momentum.Pt() && fCTSPtMax > momentum.Pt())
1109     {
1110       if(fCheckFidCut && !fFiducialCut->IsInFiducialCut(momentum,"CTS")) 
1111         continue;
1112       
1113       if(fDebug > 2 && momentum.Pt() > 0.1) 
1114         printf("AliCaloTrackReader::FillInputCTS() - Selected tracks E %3.2f, pt %3.2f, phi %3.2f, eta %3.2f\n",
1115                momentum.E(),momentum.Pt(),momentum.Phi()*TMath::RadToDeg(),momentum.Eta());
1116       
1117       if (fMixedEvent) 
1118       {
1119         track->SetID(itrack);
1120       }
1121       
1122       fCTSTracks->Add(track);        
1123       
1124     }//Pt and Fiducial cut passed. 
1125   }// track loop
1126         
1127   if(fDebug > 1) 
1128     printf("AliCaloTrackReader::FillInputCTS()   - aod entries %d, input tracks %d, pass status %d, multipliticy %d\n", fCTSTracks->GetEntriesFast(), nTracks, nstatus, fTrackMult);//fCTSTracksNormalInputEntries);
1129   
1130 }
1131
1132 //__________________________________________________________________
1133 void AliCaloTrackReader::FillInputEMCALAlgorithm(AliVCluster * clus, 
1134                                                  const Int_t iclus) 
1135 {
1136   //Fill the EMCAL data in the array, do it
1137     
1138   Int_t vindex = 0 ;  
1139   if (fMixedEvent) 
1140     vindex = fMixedEvent->EventIndexForCaloCluster(iclus);
1141   
1142   //Reject clusters with bad channels, close to borders and exotic;
1143   if(!GetCaloUtils()->GetEMCALRecoUtils()->IsGoodCluster(clus,GetCaloUtils()->GetEMCALGeometry(),GetEMCALCells(),fInputEvent->GetBunchCrossNumber())) return;
1144   
1145   //Mask all cells in collumns facing ALICE thick material if requested
1146   if(GetCaloUtils()->GetNMaskCellColumns())
1147   {
1148     Int_t absId   = -1;
1149     Int_t iSupMod = -1;
1150     Int_t iphi    = -1;
1151     Int_t ieta    = -1;
1152     Bool_t shared = kFALSE;
1153     GetCaloUtils()->GetEMCALRecoUtils()->GetMaxEnergyCell(GetCaloUtils()->GetEMCALGeometry(), GetEMCALCells(),clus,absId,iSupMod,ieta,iphi,shared);
1154     if(GetCaloUtils()->MaskFrameCluster(iSupMod, ieta)) return;
1155   }
1156   
1157   if(fSelectEmbeddedClusters)
1158   {
1159     if(clus->GetNLabels()==0 || clus->GetLabel() < 0) return;
1160     //else printf("Embedded cluster,  %d, n label %d label %d  \n",iclus,clus->GetNLabels(),clus->GetLabel());
1161   }
1162   
1163   //Float_t pos[3];
1164   //clus->GetPosition(pos);
1165   //printf("Before Corrections: e %f, x %f, y %f, z %f\n",clus->E(),pos[0],pos[1],pos[2]);
1166   
1167   if(fRecalculateClusters)
1168   {
1169     //Recalibrate the cluster energy 
1170     if(GetCaloUtils()->IsRecalibrationOn())
1171     {
1172       Float_t energy = GetCaloUtils()->RecalibrateClusterEnergy(clus, GetEMCALCells());
1173       
1174       clus->SetE(energy);
1175       //printf("Recalibrated Energy %f\n",clus->E());  
1176       
1177       GetCaloUtils()->RecalculateClusterShowerShapeParameters(GetEMCALCells(),clus);
1178       GetCaloUtils()->RecalculateClusterPID(clus);
1179       
1180     } // recalculate E
1181     
1182     //Recalculate distance to bad channels, if new list of bad channels provided
1183     GetCaloUtils()->RecalculateClusterDistanceToBadChannel(GetEMCALCells(),clus);
1184     
1185     //Recalculate cluster position
1186     if(GetCaloUtils()->IsRecalculationOfClusterPositionOn())
1187     {
1188       GetCaloUtils()->RecalculateClusterPosition(GetEMCALCells(),clus); 
1189       //clus->GetPosition(pos);
1190       //printf("After  Corrections: e %f, x %f, y %f, z %f\n",clus->E(),pos[0],pos[1],pos[2]);
1191     }
1192     
1193     // Recalculate TOF
1194     if(GetCaloUtils()->GetEMCALRecoUtils()->IsTimeRecalibrationOn()) 
1195     {
1196       Double_t tof      = clus->GetTOF();
1197       Float_t  frac     =-1;
1198       Int_t    absIdMax = GetCaloUtils()->GetMaxEnergyCell(fEMCALCells, clus,frac);
1199       
1200       if(fDataType==AliCaloTrackReader::kESD)
1201       { 
1202         tof = fEMCALCells->GetCellTime(absIdMax);
1203       }
1204       
1205       GetCaloUtils()->GetEMCALRecoUtils()->RecalibrateCellTime(absIdMax,fInputEvent->GetBunchCrossNumber(),tof);
1206       
1207       clus->SetTOF(tof);
1208       
1209     }// Time recalibration    
1210   }
1211   
1212   //Correct non linearity
1213   if(GetCaloUtils()->IsCorrectionOfClusterEnergyOn())
1214   {
1215     GetCaloUtils()->CorrectClusterEnergy(clus) ;
1216     //printf("Linearity Corrected Energy %f\n",clus->E());  
1217     
1218     //In case of MC analysis, to match resolution/calibration in real data
1219     Float_t rdmEnergy = GetCaloUtils()->GetEMCALRecoUtils()->SmearClusterEnergy(clus);
1220     // printf("\t Energy %f, smeared %f\n", clus->E(),rdmEnergy);
1221     clus->SetE(rdmEnergy);
1222   }
1223   
1224   TLorentzVector momentum ;
1225   
1226   clus->GetMomentum(momentum, fVertex[vindex]);      
1227   
1228   if(fCheckFidCut && !fFiducialCut->IsInFiducialCut(momentum,"EMCAL")) return ;
1229   
1230   if(fEMCALPtMin > momentum.E() || fEMCALPtMax < momentum.E())         return ;
1231   
1232   Double_t tof = clus->GetTOF()*1e9;
1233
1234   if(!IsInTimeWindow(tof,momentum.E()))
1235   {
1236     fNPileUpClusters++ ;
1237     return ;
1238   }
1239   else
1240     fNNonPileUpClusters++;
1241
1242   if(fDebug > 2 && momentum.E() > 0.1) 
1243     printf("AliCaloTrackReader::FillInputEMCAL() - Selected clusters E %3.2f, pt %3.2f, phi %3.2f, eta %3.2f\n",
1244            momentum.E(),momentum.Pt(),momentum.Phi()*TMath::RadToDeg(),momentum.Eta());
1245   
1246   if (fMixedEvent) 
1247     clus->SetID(iclus) ; 
1248   
1249   fEMCALClusters->Add(clus);    
1250     
1251 }
1252
1253 //_______________________________________
1254 void AliCaloTrackReader::FillInputEMCAL() 
1255 {
1256   //Return array with EMCAL clusters in aod format
1257   
1258   if(fDebug > 2 ) printf("AliCaloTrackReader::FillInputEMCAL()\n");
1259   
1260   // First recalibrate cells, time or energy
1261   //  if(GetCaloUtils()->IsRecalibrationOn())
1262   //    GetCaloUtils()->GetEMCALRecoUtils()->RecalibrateCells(GetCaloUtils()->GetEMCALGeometry(), 
1263   //                                                          GetEMCALCells(), 
1264   //                                                          fInputEvent->GetBunchCrossNumber());
1265   
1266   fNPileUpClusters    = 0; // Init counter
1267   fNNonPileUpClusters = 0; // Init counter
1268   
1269   //Loop to select clusters in fiducial cut and fill container with aodClusters
1270   if(fEMCALClustersListName=="")
1271   {
1272     Int_t nclusters = fInputEvent->GetNumberOfCaloClusters();
1273     for (Int_t iclus =  0; iclus <  nclusters; iclus++) 
1274     {
1275       AliVCluster * clus = 0;
1276       if ( (clus = fInputEvent->GetCaloCluster(iclus)) ) 
1277       {
1278         if (IsEMCALCluster(clus))
1279         {          
1280           FillInputEMCALAlgorithm(clus, iclus);
1281         }//EMCAL cluster
1282       }// cluster exists
1283     }// cluster loop
1284     
1285     //Recalculate track matching
1286     GetCaloUtils()->RecalculateClusterTrackMatching(fInputEvent);
1287     
1288   }//Get the clusters from the input event
1289   else
1290   {
1291     TClonesArray * clusterList = 0x0; 
1292     
1293     if      (fInputEvent->FindListObject(fEMCALClustersListName))
1294     {
1295       clusterList = dynamic_cast<TClonesArray*> (fInputEvent->FindListObject(fEMCALClustersListName));
1296     }
1297     else if(fOutputEvent)
1298     {
1299       clusterList = dynamic_cast<TClonesArray*> (fOutputEvent->FindListObject(fEMCALClustersListName));
1300     }
1301     
1302     if(!clusterList)
1303     {
1304         printf("AliCaloTrackReader::FillInputEMCAL() - Wrong name of list with clusters?  <%s>\n",fEMCALClustersListName.Data());
1305         return;
1306     }
1307     
1308     Int_t nclusters = clusterList->GetEntriesFast();
1309     for (Int_t iclus =  0; iclus <  nclusters; iclus++) 
1310     {
1311       AliVCluster * clus = dynamic_cast<AliVCluster*> (clusterList->At(iclus));
1312       //printf("E %f\n",clus->E());
1313       if (clus) FillInputEMCALAlgorithm(clus, iclus);
1314       else printf("AliCaloTrackReader::FillInputEMCAL() - Null cluster in list!\n");
1315     }// cluster loop
1316     
1317     // Recalculate the pile-up time, in case long time clusters removed during clusterization
1318     //printf("Input event INIT : Pile-up clusters %d, NO pile-up %d\n",fNPileUpClusters,fNNonPileUpClusters);
1319
1320     fNPileUpClusters    = 0; // Init counter
1321     fNNonPileUpClusters = 0; // Init counter
1322     
1323     for (Int_t iclus =  0; iclus < fInputEvent->GetNumberOfCaloClusters(); iclus++)
1324     {
1325       AliVCluster * clus = 0;
1326       
1327       if ( (clus = fInputEvent->GetCaloCluster(iclus)) )
1328       {
1329         if (IsEMCALCluster(clus))
1330         {
1331           
1332           if(fEMCALPtMin > clus->E() || fEMCALPtMax < clus->E()) continue ;
1333
1334           Float_t  frac     =-1;
1335           Int_t    absIdMax = GetCaloUtils()->GetMaxEnergyCell(fEMCALCells, clus,frac);
1336           Double_t tof = clus->GetTOF();
1337           GetCaloUtils()->GetEMCALRecoUtils()->RecalibrateCellTime(absIdMax,fInputEvent->GetBunchCrossNumber(),tof);
1338           tof*=1e9;
1339
1340           //printf("Input event cluster : AbsIdMax %d, E %2.2f, time %2.2f \n", absIdMax,clus->E(),tof);
1341
1342           //Reject clusters with bad channels, close to borders and exotic;
1343           if(!GetCaloUtils()->GetEMCALRecoUtils()->IsGoodCluster(clus,GetCaloUtils()->GetEMCALGeometry(),GetEMCALCells(),fInputEvent->GetBunchCrossNumber()))  continue;
1344
1345           if(!IsInTimeWindow(tof,clus->E()))
1346           {
1347             fNPileUpClusters++ ;
1348           }
1349           else
1350             fNNonPileUpClusters++;
1351         }
1352       }
1353     }
1354     
1355     //printf("Input event : Pile-up clusters %d, NO pile-up %d\n",fNPileUpClusters,fNNonPileUpClusters);
1356     
1357     // Recalculate track matching, not necessary if already done in the reclusterization task.
1358     // in case it was not done ...
1359     GetCaloUtils()->RecalculateClusterTrackMatching(fInputEvent,clusterList);
1360     
1361   }
1362     
1363   if(fDebug > 1) printf("AliCaloTrackReader::FillInputEMCAL() - aod entries %d, n pile-up clusters %d, n non pile-up %d \n",  fEMCALClusters->GetEntriesFast(),fNPileUpClusters,fNNonPileUpClusters);
1364   
1365 }
1366
1367 //______________________________________
1368 void AliCaloTrackReader::FillInputPHOS() 
1369 {
1370   //Return array with PHOS clusters in aod format
1371   
1372   if(fDebug > 2 ) printf("AliCaloTrackReader::FillInputPHOS()\n");
1373   
1374   //Loop to select clusters in fiducial cut and fill container with aodClusters
1375   Int_t nclusters = fInputEvent->GetNumberOfCaloClusters();
1376   for (Int_t iclus = 0; iclus < nclusters; iclus++) 
1377   {
1378     AliVCluster * clus = 0;
1379     if ( (clus = fInputEvent->GetCaloCluster(iclus)) ) 
1380     {
1381       if (IsPHOSCluster(clus))
1382       {
1383         //Check if the cluster contains any bad channel and if close to calorimeter borders
1384         Int_t vindex = 0 ;  
1385         if (fMixedEvent) 
1386           vindex = fMixedEvent->EventIndexForCaloCluster(iclus);
1387         if( GetCaloUtils()->ClusterContainsBadChannel("PHOS",clus->GetCellsAbsId(), clus->GetNCells())) 
1388           continue;
1389         if(!GetCaloUtils()->CheckCellFiducialRegion(clus, fInputEvent->GetPHOSCells(), fInputEvent, vindex)) 
1390           continue;
1391         
1392         if(fRecalculateClusters)
1393         {
1394           //Recalibrate the cluster energy 
1395           if(GetCaloUtils()->IsRecalibrationOn()) 
1396           {
1397             Float_t energy = GetCaloUtils()->RecalibrateClusterEnergy(clus, (AliAODCaloCells*)GetPHOSCells());
1398             clus->SetE(energy);
1399           }
1400         }
1401         
1402         TLorentzVector momentum ;
1403         
1404         clus->GetMomentum(momentum, fVertex[vindex]);      
1405         
1406         if(fCheckFidCut && !fFiducialCut->IsInFiducialCut(momentum,"PHOS")) continue;
1407         
1408         if(fPHOSPtMin > momentum.E() || fPHOSPtMax < momentum.E())          continue;
1409         
1410         if(fDebug > 2 && momentum.E() > 0.1) 
1411           printf("AliCaloTrackReader::FillInputPHOS() - Selected clusters E %3.2f, pt %3.2f, phi %3.2f, eta %3.2f\n",
1412                  momentum.E(),momentum.Pt(),momentum.Phi()*TMath::RadToDeg(),momentum.Eta());        
1413         
1414         
1415         if (fMixedEvent) 
1416         {
1417           clus->SetID(iclus) ; 
1418         }              
1419         
1420         fPHOSClusters->Add(clus);       
1421         
1422       }//PHOS cluster
1423     }//cluster exists
1424   }//esd cluster loop
1425   
1426   if(fDebug > 1) printf("AliCaloTrackReader::FillInputPHOS()  - aod entries %d\n",  fPHOSClusters->GetEntriesFast());
1427   
1428 }
1429
1430 //____________________________________________
1431 void AliCaloTrackReader::FillInputEMCALCells() 
1432 {
1433   //Return array with EMCAL cells in aod format
1434   
1435   fEMCALCells = fInputEvent->GetEMCALCells(); 
1436   
1437 }
1438
1439 //___________________________________________
1440 void AliCaloTrackReader::FillInputPHOSCells() 
1441 {
1442   //Return array with PHOS cells in aod format
1443   
1444   fPHOSCells = fInputEvent->GetPHOSCells(); 
1445   
1446 }
1447
1448 //_______________________________________
1449 void AliCaloTrackReader::FillInputVZERO()
1450 {
1451   //Fill VZERO information in data member, add all the channels information.
1452   AliVVZERO* v0 = fInputEvent->GetVZEROData();
1453   //printf("Init V0: ADC (%d,%d), Multiplicity (%d,%d) \n",fV0ADC[0],fV0ADC[1],fV0Mul[0],fV0Mul[1]);
1454   
1455   if (v0) 
1456   {
1457     AliESDVZERO* esdV0 = dynamic_cast<AliESDVZERO*> (v0);
1458     for (Int_t i = 0; i < 32; i++)
1459     {
1460       if(esdV0)
1461       {//Only available in ESDs
1462         fV0ADC[0] += (Int_t)esdV0->GetAdcV0C(i);
1463         fV0ADC[1] += (Int_t)esdV0->GetAdcV0A(i);
1464       }
1465       
1466       fV0Mul[0] += (Int_t)v0->GetMultiplicityV0C(i);
1467       fV0Mul[1] += (Int_t)v0->GetMultiplicityV0A(i);
1468     }
1469     if(fDebug > 0)
1470       printf("V0: ADC (%d,%d), Multiplicity (%d,%d) \n",fV0ADC[0],fV0ADC[1],fV0Mul[0],fV0Mul[1]);
1471   }
1472   else
1473   {
1474     if(fDebug > 0)
1475       printf("Cannot retrieve V0 ESD! Run w/ null V0 charges\n ");
1476   }
1477 }
1478
1479
1480 //___________________________________________________________________
1481 Bool_t AliCaloTrackReader::IsEMCALCluster(AliVCluster* cluster) const 
1482 {
1483   // Check if it is a cluster from EMCAL. For old AODs cluster type has
1484   // different number and need to patch here
1485   
1486   if(fDataType==kAOD && fOldAOD)
1487   {
1488     if (cluster->GetType() == 2) return kTRUE;
1489     else                         return kFALSE;
1490   }
1491   else 
1492   {
1493     return cluster->IsEMCAL();
1494   }
1495   
1496 }
1497
1498 //___________________________________________________________________
1499 Bool_t AliCaloTrackReader::IsPHOSCluster(AliVCluster * cluster) const 
1500 {
1501   //Check if it is a cluster from PHOS.For old AODs cluster type has
1502   // different number and need to patch here
1503   
1504   if(fDataType==kAOD && fOldAOD)
1505   {
1506     Int_t type = cluster->GetType();
1507     if (type == 0 || type == 1) return kTRUE;
1508     else                        return kFALSE;
1509   }
1510   else 
1511   {
1512     return cluster->IsPHOS();
1513   }
1514   
1515 }
1516
1517 //________________________________________________
1518 Bool_t AliCaloTrackReader::CheckForPrimaryVertex()
1519 {
1520   //Check if the vertex was well reconstructed, copy from V0Reader of conversion group
1521   //Only for ESDs ...
1522   
1523   AliESDEvent * event = dynamic_cast<AliESDEvent*> (fInputEvent);
1524   if(!event) return kTRUE;
1525   
1526   if(event->GetPrimaryVertexTracks()->GetNContributors() > 0) 
1527   {
1528     return kTRUE;
1529   }
1530   
1531   if(event->GetPrimaryVertexTracks()->GetNContributors() < 1) 
1532   {
1533     // SPD vertex
1534     if(event->GetPrimaryVertexSPD()->GetNContributors() > 0) 
1535     {
1536       //cout<<"spd vertex type::"<< fESDEvent->GetPrimaryVertex()->GetName() << endl;
1537       return kTRUE;
1538       
1539     }
1540     if(event->GetPrimaryVertexSPD()->GetNContributors() < 1) 
1541     {
1542       //      cout<<"bad vertex type::"<< event->GetPrimaryVertex()->GetName() << endl;
1543       return kFALSE;
1544     }
1545   }
1546   
1547   return kFALSE;  
1548   
1549 }
1550
1551 //____________________________________________________________
1552 void  AliCaloTrackReader::SetTrackCuts(AliESDtrackCuts * cuts) 
1553
1554   // Set Track cuts
1555   
1556   if(fESDtrackCuts) delete fESDtrackCuts ;
1557   
1558   fESDtrackCuts = cuts ; 
1559   
1560 }                 
1561