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