move ESDfilter to separate library + copy calo info for all tracks
[u/mrichter/AliRoot.git] / ANALYSIS / ESDfilter / AliAnalysisTaskESDfilter.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15
16 /* $Id$ */
17  
18 #include <Riostream.h>
19 #include <TChain.h>
20 #include <TTree.h>
21 #include <TList.h>
22 #include <TArrayI.h>
23 #include <TParameter.h>
24 #include <TRandom.h>
25 #include <TParticle.h>
26 #include <TFile.h>
27 #include <TVector3.h>
28
29 #include "AliAnalysisTaskESDfilter.h"
30 #include "AliAnalysisManager.h"
31 #include "AliESDEvent.h"
32 #include "AliESDRun.h"
33 #include "AliStack.h"
34 #include "AliAODEvent.h"
35 #include "AliMCEvent.h"
36 #include "AliMCEventHandler.h"
37 #include "AliESDInputHandler.h"
38 #include "AliAODHandler.h"
39 #include "AliAODMCParticle.h"
40 #include "AliAnalysisFilter.h"
41 #include "AliESDMuonTrack.h"
42 #include "AliESDVertex.h"
43 #include "AliCentrality.h"
44 #include "AliEventplane.h"
45 #include "AliESDv0.h"
46 #include "AliESDkink.h"
47 #include "AliESDcascade.h"
48 #include "AliESDPmdTrack.h"
49 #include "AliESDCaloCluster.h"
50 #include "AliESDCaloCells.h"
51 #include "AliMultiplicity.h"
52 #include "AliLog.h"
53 #include "AliCodeTimer.h"
54 #include "AliESDtrackCuts.h"
55 #include "AliESDpid.h"
56 #include "AliAODHMPIDrings.h"
57 #include "AliV0vertexer.h"
58 #include "AliCascadeVertexer.h"
59 #include "AliExternalTrackParam.h"
60 #include "AliTrackerBase.h"
61 #include "AliTPCdEdxInfo.h"
62
63 #include "AliESDTrdTrack.h"
64 #include "AliESDTrdTracklet.h"
65 #include "AliAODTrdTrack.h"
66 #include "AliAODTrdTracklet.h"
67 #include "AliEMCALRecoUtils.h"
68
69 using std::cout;
70 using std::endl;
71 ClassImp(AliAnalysisTaskESDfilter)
72
73 ////////////////////////////////////////////////////////////////////////
74
75 AliAnalysisTaskESDfilter::AliAnalysisTaskESDfilter():
76   AliAnalysisTaskSE(),
77   fTrackFilter(0x0),
78   fKinkFilter(0x0),
79   fV0Filter(0x0),
80   fCascadeFilter(0x0),
81   fHighPthreshold(0),
82   fPtshape(0x0),
83   fEnableFillAOD(kTRUE),
84   fUsedTrack(0x0),
85   fUsedTrackCopy(0x0),
86   fUsedKink(0x0),
87   fUsedV0(0x0),
88   fAODTrackRefs(0x0),
89   fAODV0VtxRefs(0x0),
90   fAODV0Refs(0x0),
91   fMChandler(0x0),
92   fNumberOfTracks(0),
93   fNumberOfPositiveTracks(0),
94   fNumberOfV0s(0),
95   fNumberOfVertices(0),
96   fNumberOfCascades(0),
97   fNumberOfKinks(0),
98   fOldESDformat(kFALSE),
99   fPrimaryVertex(0x0),
100   fTPCConstrainedFilterMask(0),
101   fHybridFilterMaskTPCCG(0),
102   fWriteHybridTPCCOnly(kFALSE),
103   fGlobalConstrainedFilterMask(0),
104   fHybridFilterMaskGCG(0),
105   fWriteHybridGCOnly(kFALSE),
106   fIsVZEROEnabled(kTRUE),
107   fIsTZEROEnabled(kTRUE),
108   fIsZDCEnabled(kTRUE),
109   fIsHMPIDEnabled(kTRUE), 
110   fIsV0CascadeRecoEnabled(kFALSE),
111   fAreCascadesEnabled(kTRUE),
112   fAreV0sEnabled(kTRUE),
113   fAreKinksEnabled(kTRUE),
114   fAreTracksEnabled(kTRUE),
115   fArePmdClustersEnabled(kTRUE),
116   fAreCaloClustersEnabled(kTRUE),
117   fAreEMCALCellsEnabled(kTRUE),
118   fArePHOSCellsEnabled(kTRUE),
119   fAreEMCALTriggerEnabled(kTRUE),
120   fArePHOSTriggerEnabled(kTRUE),
121   fAreTrackletsEnabled(kTRUE),
122   fIsTRDEnabled(kTRUE),
123   fESDpid(0x0),
124   fIsPidOwner(kFALSE),
125   fTPCaloneTrackCuts(0),
126   fDoPropagateTrackToEMCal(kTRUE),
127   fEMCalSurfaceDistance(440)
128 {
129   // Default constructor
130     fV0Cuts[0] =  33.   ;   // max allowed chi2
131     fV0Cuts[1] =   0.1  ;   // min allowed impact parameter for the 1st daughter
132     fV0Cuts[2] =   0.1  ;   // min allowed impact parameter for the 2nd daughter
133     fV0Cuts[3] =   1.   ;   // max allowed DCA between the daughter tracks
134     fV0Cuts[4] =    .998;   // min allowed cosine of V0's pointing angle
135     fV0Cuts[5] =   0.9  ;   // min radius of the fiducial volume
136     fV0Cuts[6] = 100.   ;   // max radius of the fiducial volume
137
138     fCascadeCuts[0] =  33.   ; // max allowed chi2 (same as PDC07)
139     fCascadeCuts[1] =   0.05 ; // min allowed V0 impact parameter
140     fCascadeCuts[2] =   0.008; // "window" around the Lambda mass
141     fCascadeCuts[3] =   0.03 ; // min allowed bachelor's impact parameter
142     fCascadeCuts[4] =   0.3  ; // max allowed DCA between the V0 and the bachelor
143     fCascadeCuts[5] =   0.999; // min allowed cosine of the cascade pointing angle
144     fCascadeCuts[6] =   0.9  ; // min radius of the fiducial volume
145     fCascadeCuts[7] = 100.   ; // max radius of the fiducial volume
146 }
147
148 //______________________________________________________________________________
149 AliAnalysisTaskESDfilter::AliAnalysisTaskESDfilter(const char* name):
150     AliAnalysisTaskSE(name),
151     fTrackFilter(0x0),
152     fKinkFilter(0x0),
153     fV0Filter(0x0),
154     fCascadeFilter(0x0),
155     fHighPthreshold(0),
156     fPtshape(0x0),
157     fEnableFillAOD(kTRUE),
158     fUsedTrack(0x0),
159     fUsedTrackCopy(0x0),
160     fUsedKink(0x0),
161     fUsedV0(0x0),
162     fAODTrackRefs(0x0),
163     fAODV0VtxRefs(0x0),
164     fAODV0Refs(0x0),
165     fMChandler(0x0),
166     fNumberOfTracks(0),
167     fNumberOfPositiveTracks(0),
168     fNumberOfV0s(0),
169     fNumberOfVertices(0),
170     fNumberOfCascades(0),
171     fNumberOfKinks(0),
172     fOldESDformat(kFALSE),
173     fPrimaryVertex(0x0),
174   fTPCConstrainedFilterMask(0),
175   fHybridFilterMaskTPCCG(0),
176   fWriteHybridTPCCOnly(kFALSE),
177   fGlobalConstrainedFilterMask(0),
178   fHybridFilterMaskGCG(0),
179   fWriteHybridGCOnly(kFALSE),
180     fIsVZEROEnabled(kTRUE),
181     fIsTZEROEnabled(kTRUE),
182     fIsZDCEnabled(kTRUE),
183     fIsHMPIDEnabled(kTRUE), 
184     fIsV0CascadeRecoEnabled(kFALSE),
185     fAreCascadesEnabled(kTRUE),
186     fAreV0sEnabled(kTRUE),
187     fAreKinksEnabled(kTRUE),
188     fAreTracksEnabled(kTRUE),
189     fArePmdClustersEnabled(kTRUE),
190     fAreCaloClustersEnabled(kTRUE),
191     fAreEMCALCellsEnabled(kTRUE),
192     fArePHOSCellsEnabled(kTRUE),
193                 fAreEMCALTriggerEnabled(kTRUE),
194                 fArePHOSTriggerEnabled(kTRUE),
195                 fAreTrackletsEnabled(kTRUE),
196     fIsTRDEnabled(kTRUE),
197     fESDpid(0x0),
198     fIsPidOwner(kFALSE),
199     fTPCaloneTrackCuts(0),
200     fDoPropagateTrackToEMCal(kTRUE),
201     fEMCalSurfaceDistance(440)
202 {
203   // Constructor
204
205     fV0Cuts[0] =  33.   ;   // max allowed chi2
206     fV0Cuts[1] =   0.1  ;   // min allowed impact parameter for the 1st daughter
207     fV0Cuts[2] =   0.1  ;   // min allowed impact parameter for the 2nd daughter
208     fV0Cuts[3] =   1.   ;   // max allowed DCA between the daughter tracks
209     fV0Cuts[4] =    .998;   // min allowed cosine of V0's pointing angle
210     fV0Cuts[5] =   0.9  ;   // min radius of the fiducial volume
211     fV0Cuts[6] = 100.   ;   // max radius of the fiducial volume
212
213     fCascadeCuts[0] =  33.   ; // max allowed chi2 (same as PDC07)
214     fCascadeCuts[1] =   0.05 ; // min allowed V0 impact parameter
215     fCascadeCuts[2] =   0.008; // "window" around the Lambda mass
216     fCascadeCuts[3] =   0.03 ; // min allowed bachelor's impact parameter
217     fCascadeCuts[4] =   0.3  ; // max allowed DCA between the V0 and the bachelor
218     fCascadeCuts[5] =   0.999; // min allowed cosine of the cascade pointing angle
219     fCascadeCuts[6] =   0.9  ; // min radius of the fiducial volume
220     fCascadeCuts[7] = 100.   ; // max radius of the fiducial volume
221
222
223
224 }
225 AliAnalysisTaskESDfilter::~AliAnalysisTaskESDfilter(){
226     if(fIsPidOwner) delete fESDpid;
227 }
228 //______________________________________________________________________________
229 void AliAnalysisTaskESDfilter::UserCreateOutputObjects()
230 {
231   //
232   // Create Output Objects conenct filter to outputtree
233   // 
234   if(OutputTree())
235   {
236     OutputTree()->GetUserInfo()->Add(fTrackFilter);
237   }
238   else
239   {
240     AliError("No OutputTree() for adding the track filter");
241   }
242   fTPCaloneTrackCuts = AliESDtrackCuts::GetStandardTPCOnlyTrackCuts();
243 }
244
245 //______________________________________________________________________________
246 void AliAnalysisTaskESDfilter::Init()
247 {
248   // Initialization
249   if (fDebug > 1) AliInfo("Init() \n");
250   // Call configuration file
251 }
252
253 //______________________________________________________________________________
254 Bool_t AliAnalysisTaskESDfilter::Notify()
255 {
256 // Notify method.
257    AddMetadataToUserInfo();
258    return kTRUE;
259 }   
260
261 //______________________________________________________________________________
262 Bool_t AliAnalysisTaskESDfilter::AddMetadataToUserInfo()
263 {
264 // Copy metadata to AOD user info.
265   static Bool_t copyFirst = kFALSE;
266   if (!copyFirst) {
267     AliAnalysisManager *mgr = AliAnalysisManager::GetAnalysisManager();
268     if (!mgr) {
269        AliError("AliAnalysisTaskESDfilter::AddMetadataToUserInfo() : No analysis manager !");
270        return kFALSE;
271     }   
272     TTree *esdTree = mgr->GetTree()->GetTree();
273     if (!esdTree) return kFALSE;
274     TNamed *alirootVersion = (TNamed*)esdTree->GetUserInfo()->FindObject("alirootVersion");
275     if (!alirootVersion) return kFALSE;    
276     AliAODHandler *aodHandler = dynamic_cast<AliAODHandler*>(mgr->GetOutputEventHandler());
277          if (!aodHandler) return kFALSE;
278     TTree *aodTree = aodHandler->GetTree();
279     if (!aodTree) return kFALSE;
280     aodTree->GetUserInfo()->Add(new TNamed(*alirootVersion));
281     copyFirst = kTRUE;
282   }
283   return kTRUE;
284 }
285
286 //______________________________________________________________________________
287 void AliAnalysisTaskESDfilter::PrintTask(Option_t *option, Int_t indent) const
288 {
289 // Print selection task information
290   AliInfo("");
291   
292   AliAnalysisTaskSE::PrintTask(option,indent);
293   
294   TString spaces(' ',indent+3);
295   
296         cout << spaces.Data() << Form("Cascades       are %s",fAreCascadesEnabled ? "ENABLED":"DISABLED") << endl;
297         cout << spaces.Data() << Form("V0s            are %s",fAreV0sEnabled ? "ENABLED":"DISABLED") << endl;
298         cout << spaces.Data() << Form("Kinks          are %s",fAreKinksEnabled ? "ENABLED":"DISABLED") << endl;
299         cout << spaces.Data() << Form("Tracks         are %s",fAreTracksEnabled ? "ENABLED":"DISABLED") << endl;
300         cout << spaces.Data() << Form("PmdClusters    are %s",fArePmdClustersEnabled ? "ENABLED":"DISABLED") << endl;
301         cout << spaces.Data() << Form("CaloClusters   are %s",fAreCaloClustersEnabled ? "ENABLED":"DISABLED") << endl;
302   cout << spaces.Data() << Form("EMCAL cells    are %s",fAreEMCALCellsEnabled ? "ENABLED":"DISABLED") << endl;
303         cout << spaces.Data() << Form("EMCAL triggers are %s",fAreEMCALTriggerEnabled ? "ENABLED":"DISABLED") << endl;
304         cout << spaces.Data() << Form("PHOS triggers  are %s",fArePHOSTriggerEnabled ? "ENABLED":"DISABLED") << endl;
305         cout << spaces.Data() << Form("Tracklets      are %s",fAreTrackletsEnabled ? "ENABLED":"DISABLED") << endl;  
306         cout << spaces.Data() << Form("PropagateTrackToEMCal  is %s", fDoPropagateTrackToEMCal ? "ENABLED":"DISABLED") << endl; 
307 }
308
309 //______________________________________________________________________________
310 void AliAnalysisTaskESDfilter::UserExec(Option_t */*option*/)
311 {
312 // Execute analysis for current event
313 //
314                                             
315   Long64_t ientry = Entry();
316   
317   if (fDebug > 0) {
318       printf("Filter: Analysing event # %5d\n", (Int_t) ientry);
319       if (fHighPthreshold == 0) AliInfo("detector PID signals are stored in each track");
320       if (!fPtshape) AliInfo("detector PID signals are not stored below the pt threshold");
321   }
322   // Filters must explicitely enable AOD filling in their UserExec (AG)
323   if (!AliAnalysisManager::GetAnalysisManager()->GetOutputEventHandler()) AliFatal("Cannot run ESD filter without an output event handler");
324   if(fEnableFillAOD) {
325      AliAnalysisManager::GetAnalysisManager()->GetOutputEventHandler()->SetFillAOD(kTRUE);
326      AliAnalysisManager::GetAnalysisManager()->GetOutputEventHandler()->SetFillExtension(kTRUE);
327   }   
328   ConvertESDtoAOD();
329 }
330
331 //______________________________________________________________________________
332 TClonesArray& AliAnalysisTaskESDfilter::Cascades()
333 {
334   return *(AODEvent()->GetCascades());
335 }
336
337 //______________________________________________________________________________
338 TClonesArray& AliAnalysisTaskESDfilter::Tracks()
339 {
340   return *(AODEvent()->GetTracks());
341 }
342
343 //______________________________________________________________________________
344 TClonesArray& AliAnalysisTaskESDfilter::V0s()
345 {
346   return *(AODEvent()->GetV0s());
347 }
348
349 //______________________________________________________________________________
350 TClonesArray& AliAnalysisTaskESDfilter::Vertices()
351 {
352   return *(AODEvent()->GetVertices());
353 }
354
355 //______________________________________________________________________________
356 AliAODHeader* AliAnalysisTaskESDfilter::ConvertHeader(const AliESDEvent& esd)
357 {
358 // Convert header information
359
360   AliCodeTimerAuto("",0);
361   
362   AliAODHeader* header = AODEvent()->GetHeader();
363   
364   header->SetRunNumber(esd.GetRunNumber());
365   header->SetOfflineTrigger(fInputHandler->IsEventSelected()); // propagate the decision of the physics selection
366   header->SetNumberOfESDTracks(esd.GetNumberOfTracks());
367
368   TTree* tree = fInputHandler->GetTree();
369   if (tree) {
370     TFile* file = tree->GetCurrentFile();
371     if (file) header->SetESDFileName(file->GetName());
372   }
373   
374   if (fOldESDformat) {
375     header->SetBunchCrossNumber(0);
376     header->SetOrbitNumber(0);
377     header->SetPeriodNumber(0);
378     header->SetEventType(0);
379     header->SetMuonMagFieldScale(-999.);
380     header->SetCentrality(0);       
381     header->SetEventplane(0);
382   } else {
383     header->SetBunchCrossNumber(esd.GetBunchCrossNumber());
384     header->SetOrbitNumber(esd.GetOrbitNumber());
385     header->SetPeriodNumber(esd.GetPeriodNumber());
386     header->SetEventType(esd.GetEventType());
387     
388     header->SetEventNumberESDFile(esd.GetHeader()->GetEventNumberInFile());
389     if(const_cast<AliESDEvent&>(esd).GetCentrality()){
390       header->SetCentrality(const_cast<AliESDEvent&>(esd).GetCentrality());
391     }
392     else{
393       header->SetCentrality(0);
394     }
395     if(const_cast<AliESDEvent&>(esd).GetEventplane()){
396       header->SetEventplane(const_cast<AliESDEvent&>(esd).GetEventplane());
397     }
398     else{
399       header->SetEventplane(0);
400     }
401   }
402   
403   // Trigger
404   header->SetFiredTriggerClasses(esd.GetFiredTriggerClasses());
405   header->SetTriggerMask(esd.GetTriggerMask()); 
406   header->SetTriggerCluster(esd.GetTriggerCluster());
407   header->SetL0TriggerInputs(esd.GetHeader()->GetL0TriggerInputs());    
408   header->SetL1TriggerInputs(esd.GetHeader()->GetL1TriggerInputs());    
409   header->SetL2TriggerInputs(esd.GetHeader()->GetL2TriggerInputs());    
410   
411   header->SetMagneticField(esd.GetMagneticField());
412   header->SetMuonMagFieldScale(esd.GetCurrentDip()/6000.);
413   header->SetZDCN1Energy(esd.GetZDCN1Energy());
414   header->SetZDCP1Energy(esd.GetZDCP1Energy());
415   header->SetZDCN2Energy(esd.GetZDCN2Energy());
416   header->SetZDCP2Energy(esd.GetZDCP2Energy());
417   header->SetZDCEMEnergy(esd.GetZDCEMEnergy(0),esd.GetZDCEMEnergy(1));
418
419   header->SetIRInt2InteractionMap(esd.GetHeader()->GetIRInt2InteractionMap());
420   header->SetIRInt1InteractionMap(esd.GetHeader()->GetIRInt1InteractionMap());
421   
422   // ITS Cluster Multiplicty
423   const AliMultiplicity *mult = esd.GetMultiplicity();
424   for (Int_t ilay = 0; ilay < 6; ilay++) header->SetITSClusters(ilay, mult->GetNumberOfITSClusters(ilay));
425   
426   // TPC only Reference Multiplicty
427   Int_t refMult  = fTPCaloneTrackCuts ? (Short_t)fTPCaloneTrackCuts->GetReferenceMultiplicity(&esd, kTRUE) : -1;
428   header->SetTPConlyRefMultiplicity(refMult);
429   //
430   AliESDtrackCuts::MultEstTrackType estType = esd.GetPrimaryVertexTracks()->GetStatus() ? AliESDtrackCuts::kTrackletsITSTPC : AliESDtrackCuts::kTracklets;
431   header->SetRefMultiplicityComb05(AliESDtrackCuts::GetReferenceMultiplicity(&esd,estType,0.5));
432   header->SetRefMultiplicityComb08(AliESDtrackCuts::GetReferenceMultiplicity(&esd,estType,0.8));
433   //
434   Float_t diamxy[2]={esd.GetDiamondX(),esd.GetDiamondY()};
435   Float_t diamcov[3]; 
436   esd.GetDiamondCovXY(diamcov);
437   header->SetDiamond(diamxy,diamcov);
438   header->SetDiamondZ(esd.GetDiamondZ(),esd.GetSigma2DiamondZ());
439   
440   // VZERO channel equalization factors for event-plane reconstruction   
441   header->SetVZEROEqFactors(esd.GetVZEROEqFactors());
442
443   // T0 Resolution information                                                                                                                                          
444   const AliESDRun* esdRun = esd.GetESDRun();
445   for (Int_t i=0;i<AliESDRun::kT0spreadSize;i++) header->SetT0spread(i,esdRun->GetT0spread(i));
446
447   return header;
448 }
449
450 //______________________________________________________________________________
451 void AliAnalysisTaskESDfilter::ConvertCascades(const AliESDEvent& esd) 
452 {
453
454   // Convert the cascades part of the ESD.
455   // Return the number of cascades
456  
457   AliCodeTimerAuto("",0);
458   
459   // Create vertices starting from the most complex objects
460   Double_t chi2 = 0.;
461   
462   const AliESDVertex* vtx = esd.GetPrimaryVertex();
463   Double_t pos[3] = { 0. };
464   Double_t covVtx[6] = { 0. };
465   Double_t momBach[3]={0.};
466   Double_t covTr[21]={0.};
467   Double_t pid[10]={0.};
468   AliAODPid* detpid(0x0);
469   AliAODVertex* vV0FromCascade(0x0);
470   AliAODv0* aodV0(0x0);
471   AliAODcascade* aodCascade(0x0);
472   AliAODTrack* aodTrack(0x0);
473   Double_t momPos[3]={0.};
474   Double_t momNeg[3] = { 0. };
475   Double_t momPosAtV0vtx[3]={0.};
476   Double_t momNegAtV0vtx[3]={0.};
477   Int_t    tofLabel[3] = {0};
478   TClonesArray& verticesArray = Vertices();
479   TClonesArray& tracksArray = Tracks();
480   TClonesArray& cascadesArray = Cascades();
481   
482   // Cascades (Modified by A.Maire - February 2009)
483   for (Int_t nCascade = 0; nCascade < esd.GetNumberOfCascades(); ++nCascade) {
484     
485     // 0- Preparation
486     //
487     AliESDcascade *esdCascade = esd.GetCascade(nCascade);
488                 Int_t  idxPosFromV0Dghter  = esdCascade->GetPindex();
489                 Int_t  idxNegFromV0Dghter  = esdCascade->GetNindex();
490                 Int_t  idxBachFromCascade  = esdCascade->GetBindex();
491     
492     AliESDtrack  *esdCascadePos  = esd.GetTrack( idxPosFromV0Dghter);
493     AliESDtrack  *esdCascadeNeg  = esd.GetTrack( idxNegFromV0Dghter);
494     AliESDtrack  *esdCascadeBach = esd.GetTrack( idxBachFromCascade);
495     
496     // Identification of the V0 within the esdCascade (via both daughter track indices)
497     AliESDv0 * currentV0   = 0x0;
498     Int_t      idxV0FromCascade = -1;
499     
500     for (Int_t iV0=0; iV0<esd.GetNumberOfV0s(); ++iV0) {
501       
502       currentV0 = esd.GetV0(iV0);
503       Int_t posCurrentV0 = currentV0->GetPindex();
504       Int_t negCurrentV0 = currentV0->GetNindex();
505       
506       if (posCurrentV0==idxPosFromV0Dghter && negCurrentV0==idxNegFromV0Dghter) {
507         idxV0FromCascade = iV0;
508         break;
509       }
510     }
511     
512     if(idxV0FromCascade < 0){
513       printf("Cascade - no matching for the V0 (index V0 = -1) ! Skip ... \n");
514       continue;
515     }// a priori, useless check, but safer ... in case of pb with tracks "out of bounds"
516     
517     AliESDv0 *esdV0FromCascade   = esd.GetV0(idxV0FromCascade);
518         
519     // 1 - Cascade selection 
520     
521     //  AliESDVertex *esdPrimVtx = new AliESDVertex(*(esd.GetPrimaryVertex()));
522     //  TList cascadeObjects;
523     //  cascadeObjects.AddAt(esdV0FromCascade, 0);
524     //  cascadeObjects.AddAt(esdCascadePos,    1);
525     //  cascadeObjects.AddAt(esdCascadeNeg,    2);
526     //  cascadeObjects.AddAt(esdCascade,       3);
527     //  cascadeObjects.AddAt(esdCascadeBach,   4);
528     //  cascadeObjects.AddAt(esdPrimVtx,       5);
529     // 
530     //  UInt_t selectCascade = 0;
531     //  if (fCascadeFilter) {
532     //    // selectCascade = fCascadeFilter->IsSelected(&cascadeObjects); 
533     //          // FIXME AliESDCascadeCuts to be implemented ...
534     // 
535     //          // Here we may encounter a moot point at the V0 level 
536     //          // between the cascade selections and the V0 ones :
537     //          // the V0 selected along with the cascade (secondary V0) may 
538     //          // usually be removed from the dedicated V0 selections (prim V0) ...
539     //          // -> To be discussed !
540     // 
541     //    // this is a little awkward but otherwise the 
542     //    // list wants to access the pointer (delete it) 
543     //    // again when going out of scope
544     //    delete cascadeObjects.RemoveAt(5); // esdPrimVtx created via copy construct
545     //    esdPrimVtx = 0;
546     //    if (!selectCascade) 
547     //      continue;
548     //  }
549     //  else{
550     //    delete cascadeObjects.RemoveAt(5); // esdPrimVtx created via copy construct
551     //    esdPrimVtx = 0;
552     //  }
553     
554     // 2 - Add the cascade vertex
555     
556     esdCascade->GetXYZcascade(pos[0], pos[1], pos[2]);
557     esdCascade->GetPosCovXi(covVtx);
558     chi2 = esdCascade->GetChi2Xi(); 
559     
560     AliAODVertex *vCascade = new(verticesArray[fNumberOfVertices++]) AliAODVertex( pos,
561                                                                      covVtx,
562                                                                      chi2, // FIXME = Chi2/NDF will be needed
563                                                                      fPrimaryVertex,
564                                                                      nCascade, // id
565                                                                      AliAODVertex::kCascade);
566     fPrimaryVertex->AddDaughter(vCascade);
567     
568 //    if (fDebug > 2) {
569 //      printf("---- Cascade / Cascade Vertex (AOD) : \n");
570 //      vCascade->Print();
571 //    }
572     
573 //    if(esd.GetTOFHeader() && fIsPidOwner) fESDpid->SetTOFResponse(const_cast<AliESDEvent*>(&esd), (AliESDpid::EStartTimeType_t)fTimeZeroType); //in case of AOD production starting form LHC10e without Tender. 
574
575
576     // 3 - Add the bachelor track from the cascade
577     
578     if (!fUsedTrack[idxBachFromCascade]) {
579       
580       esdCascadeBach->GetPxPyPz(momBach);
581       esdCascadeBach->GetXYZ(pos);
582       esdCascadeBach->GetCovarianceXYZPxPyPz(covTr);
583       esdCascadeBach->GetESDpid(pid);
584       esdCascadeBach->GetTOFLabel(tofLabel);
585
586             fUsedTrack[idxBachFromCascade] = kTRUE;
587             UInt_t selectInfo = 0;
588             if (fTrackFilter) selectInfo = fTrackFilter->IsSelected(esdCascadeBach);
589             if (fMChandler) fMChandler->SelectParticle(esdCascadeBach->GetLabel());
590             aodTrack = new(tracksArray[fNumberOfTracks++]) AliAODTrack(esdCascadeBach->GetID(),
591                                                                            esdCascadeBach->GetLabel(), 
592                                                                            momBach, 
593                                                                            kTRUE,
594                                                                            pos,
595                                                                            kFALSE, // Why kFALSE for "isDCA" ? FIXME
596                                                                            covTr, 
597                                                                            (Short_t)esdCascadeBach->GetSign(),
598                                                                            esdCascadeBach->GetITSClusterMap(), 
599                                                                            pid,
600                                                                            vCascade,
601                                                                            kTRUE,  // usedForVtxFit = kFALSE ? FIXME
602                                                                            vtx->UsesTrack(esdCascadeBach->GetID()),
603                                                                            AliAODTrack::kSecondary,
604                                                                            selectInfo);
605             aodTrack->SetTPCFitMap(esdCascadeBach->GetTPCFitMap());
606             aodTrack->SetTPCClusterMap(esdCascadeBach->GetTPCClusterMap());
607             aodTrack->SetTPCSharedMap (esdCascadeBach->GetTPCSharedMap());
608             aodTrack->SetChi2perNDF(Chi2perNDF(esdCascadeBach));
609             aodTrack->SetTPCPointsF(esdCascadeBach->GetTPCNclsF());
610             aodTrack->SetTPCNCrossedRows(UShort_t(esdCascadeBach->GetTPCCrossedRows()));
611             aodTrack->SetIntegratedLength(esdCascadeBach->GetIntegratedLength());
612             aodTrack->SetTOFLabel(tofLabel);
613             fAODTrackRefs->AddAt(aodTrack,idxBachFromCascade);
614             
615             if (esdCascadeBach->GetSign() > 0) ++fNumberOfPositiveTracks;
616             aodTrack->ConvertAliPIDtoAODPID();
617             aodTrack->SetFlags(esdCascadeBach->GetStatus());
618             SetAODPID(esdCascadeBach,aodTrack,detpid);
619     }
620     else {
621             aodTrack = static_cast<AliAODTrack*>( fAODTrackRefs->At(idxBachFromCascade) );
622     }
623     
624     vCascade->AddDaughter(aodTrack);
625     
626 //    if (fDebug > 4) {
627 //      printf("---- Cascade / bach dghter : \n");
628 //      aodTrack->Print();
629 //    }
630     
631     
632     // 4 - Add the V0 from the cascade. 
633     // = V0vtx + both pos and neg daughter tracks + the aodV0 itself
634     //
635     
636     if ( !fUsedV0[idxV0FromCascade] ) {
637       // 4.A - if VO structure hasn't been created yet
638       
639       // 4.A.1 - Create the V0 vertex of the cascade
640       
641       esdV0FromCascade->GetXYZ(pos[0], pos[1], pos[2]);
642       esdV0FromCascade->GetPosCov(covVtx);
643       chi2 = esdV0FromCascade->GetChi2V0();  // = chi2/NDF since NDF = 2*2-3 ?
644                         
645       vV0FromCascade = new(verticesArray[fNumberOfVertices++]) AliAODVertex(pos,
646                                                                          covVtx,
647                                                                          chi2,
648                                                                          vCascade,
649                                                                          idxV0FromCascade, //id of ESDv0
650                                                                          AliAODVertex::kV0);
651       // Note:
652       //    one V0 can be used by several cascades.
653       // So, one AOD V0 vtx can have several parent vtx.
654       // This is not directly allowed by AliAODvertex.
655       // Setting the parent vtx (here = param "vCascade") doesn't lead to a crash
656       // but to a problem of consistency within AODEvent.
657       // -> See below paragraph 4.B, for the proposed treatment of such a case.
658       
659       // Add the vV0FromCascade to the aodVOVtxRefs
660       fAODV0VtxRefs->AddAt(vV0FromCascade,idxV0FromCascade);
661       
662       
663       // 4.A.2 - Add the positive tracks from the V0
664       
665       esdCascadePos->GetPxPyPz(momPos);
666       esdCascadePos->GetXYZ(pos);
667       esdCascadePos->GetCovarianceXYZPxPyPz(covTr);
668       esdCascadePos->GetESDpid(pid);
669       esdCascadePos->GetTOFLabel(tofLabel);      
670       
671       if (!fUsedTrack[idxPosFromV0Dghter]) {
672         fUsedTrack[idxPosFromV0Dghter] = kTRUE;
673         
674         UInt_t selectInfo = 0;
675         if (fTrackFilter) selectInfo = fTrackFilter->IsSelected(esdCascadePos);
676         if(fMChandler) fMChandler->SelectParticle(esdCascadePos->GetLabel());
677         aodTrack = new(tracksArray[fNumberOfTracks++]) 
678         AliAODTrack(  esdCascadePos->GetID(),
679                     esdCascadePos->GetLabel(), 
680                     momPos, 
681                     kTRUE,
682                     pos,
683                     kFALSE, // Why kFALSE for "isDCA" ? FIXME
684                     covTr, 
685                     (Short_t)esdCascadePos->GetSign(),
686                     esdCascadePos->GetITSClusterMap(), 
687                     pid,
688                     vV0FromCascade,
689                     kTRUE,  // usedForVtxFit = kFALSE ? FIXME
690                     vtx->UsesTrack(esdCascadePos->GetID()),
691                     AliAODTrack::kSecondary,
692                     selectInfo);
693         aodTrack->SetTPCFitMap(esdCascadePos->GetTPCFitMap());
694         aodTrack->SetTPCClusterMap(esdCascadePos->GetTPCClusterMap());
695         aodTrack->SetTPCSharedMap (esdCascadePos->GetTPCSharedMap());
696         aodTrack->SetChi2perNDF(Chi2perNDF(esdCascadePos));
697         aodTrack->SetTPCPointsF(esdCascadePos->GetTPCNclsF());
698         aodTrack->SetTPCNCrossedRows(UShort_t(esdCascadePos->GetTPCCrossedRows()));
699         aodTrack->SetIntegratedLength(esdCascadePos->GetIntegratedLength());
700         aodTrack->SetTOFLabel(tofLabel);
701         fAODTrackRefs->AddAt(aodTrack,idxPosFromV0Dghter);
702         
703         if (esdCascadePos->GetSign() > 0) ++fNumberOfPositiveTracks;
704         aodTrack->ConvertAliPIDtoAODPID();
705         aodTrack->SetFlags(esdCascadePos->GetStatus());
706         SetAODPID(esdCascadePos,aodTrack,detpid);
707       }
708       else {
709         aodTrack = static_cast<AliAODTrack*>(fAODTrackRefs->At(idxPosFromV0Dghter));
710       }
711       vV0FromCascade->AddDaughter(aodTrack);
712       
713       
714       // 4.A.3 - Add the negative tracks from the V0
715       
716       esdCascadeNeg->GetPxPyPz(momNeg);
717       esdCascadeNeg->GetXYZ(pos);
718       esdCascadeNeg->GetCovarianceXYZPxPyPz(covTr);
719       esdCascadeNeg->GetESDpid(pid);
720       esdCascadeNeg->GetTOFLabel(tofLabel);
721       
722       
723       if (!fUsedTrack[idxNegFromV0Dghter]) {
724         fUsedTrack[idxNegFromV0Dghter] = kTRUE;
725         
726         UInt_t selectInfo = 0;
727         if (fTrackFilter) selectInfo = fTrackFilter->IsSelected(esdCascadeNeg);
728         if(fMChandler)fMChandler->SelectParticle(esdCascadeNeg->GetLabel());
729         aodTrack = new(tracksArray[fNumberOfTracks++]) AliAODTrack(  esdCascadeNeg->GetID(),
730                                                       esdCascadeNeg->GetLabel(),
731                                                       momNeg,
732                                                       kTRUE,
733                                                       pos,
734                                                       kFALSE, // Why kFALSE for "isDCA" ? FIXME
735                                                       covTr, 
736                                                       (Short_t)esdCascadeNeg->GetSign(),
737                                                       esdCascadeNeg->GetITSClusterMap(), 
738                                                       pid,
739                                                       vV0FromCascade,
740                                                       kTRUE,  // usedForVtxFit = kFALSE ? FIXME
741                                                       vtx->UsesTrack(esdCascadeNeg->GetID()),
742                                                       AliAODTrack::kSecondary,
743                                                       selectInfo);
744         aodTrack->SetTPCFitMap(esdCascadeNeg->GetTPCFitMap());
745         aodTrack->SetTPCClusterMap(esdCascadeNeg->GetTPCClusterMap());
746         aodTrack->SetTPCSharedMap (esdCascadeNeg->GetTPCSharedMap());
747         aodTrack->SetChi2perNDF(Chi2perNDF(esdCascadeNeg));
748         aodTrack->SetTPCPointsF(esdCascadeNeg->GetTPCNclsF());
749         aodTrack->SetTPCNCrossedRows(UShort_t(esdCascadeNeg->GetTPCCrossedRows()));
750         aodTrack->SetIntegratedLength(esdCascadeNeg->GetIntegratedLength());
751         aodTrack->SetTOFLabel(tofLabel);
752         fAODTrackRefs->AddAt(aodTrack,idxNegFromV0Dghter);
753         
754         if (esdCascadeNeg->GetSign() > 0) ++fNumberOfPositiveTracks;
755         aodTrack->ConvertAliPIDtoAODPID();
756         aodTrack->SetFlags(esdCascadeNeg->GetStatus());
757         SetAODPID(esdCascadeNeg,aodTrack,detpid);
758       }
759       else {
760         aodTrack = static_cast<AliAODTrack*>(fAODTrackRefs->At(idxNegFromV0Dghter));
761       }
762       
763       vV0FromCascade->AddDaughter(aodTrack);
764       
765                         
766       // 4.A.4 - Add the V0 from cascade to the V0 array
767       
768       Double_t  dcaV0Daughters      = esdV0FromCascade->GetDcaV0Daughters();
769       Double_t  dcaV0ToPrimVertex   = esdV0FromCascade->GetD( esd.GetPrimaryVertex()->GetX(),
770                                                              esd.GetPrimaryVertex()->GetY(),
771                                                              esd.GetPrimaryVertex()->GetZ() );
772       esdV0FromCascade->GetPPxPyPz( momPosAtV0vtx[0],momPosAtV0vtx[1],momPosAtV0vtx[2] ); 
773       esdV0FromCascade->GetNPxPyPz( momNegAtV0vtx[0],momNegAtV0vtx[1],momNegAtV0vtx[2] ); 
774       
775       Double_t dcaDaughterToPrimVertex[2] = { 999., 999.}; // ..[0] = DCA in (x,y) for Pos and ..[1] = Neg
776       dcaDaughterToPrimVertex[0] = TMath::Abs(esdCascadePos->GetD(      esd.GetPrimaryVertex()->GetX(),
777                                                                   esd.GetPrimaryVertex()->GetY(),
778                                                                   esd.GetMagneticField())        );
779       dcaDaughterToPrimVertex[1] = TMath::Abs(esdCascadeNeg->GetD(      esd.GetPrimaryVertex()->GetX(),
780                                                                   esd.GetPrimaryVertex()->GetY(),
781                                                                   esd.GetMagneticField())        );
782       
783       aodV0 = new(V0s()[fNumberOfV0s++]) AliAODv0( vV0FromCascade, 
784                                                   dcaV0Daughters,
785                                                   dcaV0ToPrimVertex, 
786                                                   momPosAtV0vtx, 
787                                                   momNegAtV0vtx, 
788                                                   dcaDaughterToPrimVertex); 
789       // set the aod v0 on-the-fly status
790       aodV0->SetOnFlyStatus(esdV0FromCascade->GetOnFlyStatus());
791       
792       // Add the aodV0 to the aodVORefs
793       fAODV0Refs->AddAt(aodV0,idxV0FromCascade);
794       
795       fUsedV0[idxV0FromCascade] = kTRUE;
796       
797     } else { 
798       // 4.B - if V0 structure already used
799       
800       // Note :
801       //    one V0 can be used by several cascades (frequent in PbPb evts) : 
802       // same V0 which used but attached to different bachelor tracks
803       // -> aodVORefs and fAODV0VtxRefs are needed.
804       // Goal : avoid a redundancy of the info in "Vertices" and "v0s" clones array.
805       
806       vV0FromCascade = static_cast<AliAODVertex*>( fAODV0VtxRefs->At(idxV0FromCascade) );
807       aodV0          = static_cast<AliAODv0*>    ( fAODV0Refs   ->At(idxV0FromCascade) );
808       
809       // - Treatment of the parent for such a "re-used" V0 :
810       // Insert the cascade that reuses the V0 vertex in the lineage chain
811       // Before : vV0 -> vCascade1 -> vPrimary
812       //  - Hyp : cascade2 uses the same V0 as cascade1
813       //  After :  vV0 -> vCascade2 -> vCascade1 -> vPrimary
814       
815       AliAODVertex *vCascadePreviousParent = static_cast<AliAODVertex*> (vV0FromCascade->GetParent());
816                         vV0FromCascade->SetParent(vCascade);
817                         vCascade      ->SetParent(vCascadePreviousParent);
818       
819 //      if(fDebug > 2)  
820 //        printf("---- Cascade / Lineage insertion\n"
821 //               "Parent of V0 vtx                 = Cascade vtx %p\n"
822 //               "Parent of the cascade vtx        = Cascade vtx %p\n"
823 //               "Parent of the parent cascade vtx = Cascade vtx %p\n", 
824 //               static_cast<void*> (vV0FromCascade->GetParent()),
825 //               static_cast<void*> (vCascade->GetParent()),
826 //               static_cast<void*> (vCascadePreviousParent->GetParent()) );
827       
828     }// end if V0 structure already used
829     
830 //    if (fDebug > 2) {
831 //      printf("---- Cascade / V0 vertex: \n");
832 //      vV0FromCascade->Print();
833 //    }
834 //    
835 //    if (fDebug > 4) {
836 //      printf("---- Cascade / pos dghter : \n");
837 //                      aodTrack->Print();
838 //      printf("---- Cascade / neg dghter : \n");
839 //                      aodTrack->Print();
840 //      printf("---- Cascade / aodV0 : \n");
841 //                      aodV0->Print();
842 //    }
843     
844     // In any case (used V0 or not), add the V0 vertex to the cascade one.
845     vCascade->AddDaughter(vV0FromCascade);      
846     
847                 
848     // 5 - Add the primary track of the cascade (if any)
849     
850     
851     // 6 - Add the cascade to the AOD array of cascades
852     
853     Double_t dcaBachToPrimVertexXY = TMath::Abs(esdCascadeBach->GetD(esd.GetPrimaryVertex()->GetX(),
854                                                                      esd.GetPrimaryVertex()->GetY(),
855                                                                      esd.GetMagneticField())        );
856     
857     Double_t momBachAtCascadeVtx[3]={0.};
858
859     esdCascade->GetBPxPyPz(momBachAtCascadeVtx[0], momBachAtCascadeVtx[1], momBachAtCascadeVtx[2]);
860     
861     aodCascade = new(cascadesArray[fNumberOfCascades++]) AliAODcascade(  vCascade,
862                                                           esdCascade->Charge(),
863                                                           esdCascade->GetDcaXiDaughters(),
864                                                           -999.,
865                                                           // DCAXiToPrimVtx -> needs to be calculated   ----|
866                                                           // doesn't exist at ESD level;
867                                                           // See AODcascade::DcaXiToPrimVertex(Double, Double, Double)
868                                                           dcaBachToPrimVertexXY,
869                                                           momBachAtCascadeVtx,
870                                                           *aodV0);
871     
872     if (fDebug > 10) {
873       printf("---- Cascade / AOD cascade : \n\n");
874       aodCascade->PrintXi(fPrimaryVertex->GetX(), fPrimaryVertex->GetY(), fPrimaryVertex->GetZ());
875     }
876     
877   } // end of the loop on cascades
878   
879   Cascades().Expand(fNumberOfCascades);
880 }
881
882 //______________________________________________________________________________
883 void AliAnalysisTaskESDfilter::ConvertV0s(const AliESDEvent& esd)
884 {
885   // Access to the AOD container of V0s
886   
887   AliCodeTimerAuto("",0);
888
889   //
890   // V0s
891   //
892   
893   Double_t pos[3] = { 0. };      
894   Double_t chi2(0.0);
895   Double_t covVtx[6] = { 0. };
896   Double_t momPos[3]={0.};
897   Double_t covTr[21]={0.};
898   Double_t pid[10]={0.};
899   AliAODTrack* aodTrack(0x0);
900   AliAODPid* detpid(0x0);
901   Double_t momNeg[3]={0.};
902   Double_t momPosAtV0vtx[3]={0.};
903   Double_t momNegAtV0vtx[3]={0.};
904   Int_t    tofLabel[3] = {0};
905   for (Int_t nV0 = 0; nV0 < esd.GetNumberOfV0s(); ++nV0) 
906   {
907     if (fUsedV0[nV0]) continue; // skip if already added to the AOD
908     
909     AliESDv0 *v0 = esd.GetV0(nV0);
910     Int_t posFromV0 = v0->GetPindex();
911     Int_t negFromV0 = v0->GetNindex();
912     
913     // V0 selection 
914     //
915     AliESDVertex *esdVtx   = new AliESDVertex(*(esd.GetPrimaryVertex()));
916     AliESDtrack  *esdV0Pos = esd.GetTrack(posFromV0);
917     AliESDtrack  *esdV0Neg = esd.GetTrack(negFromV0);
918     TList v0objects;
919     v0objects.AddAt(v0,                      0);
920     v0objects.AddAt(esdV0Pos,                1);
921     v0objects.AddAt(esdV0Neg,                2);
922     v0objects.AddAt(esdVtx,                  3);
923     UInt_t selectV0 = 0;
924     if (fV0Filter) {
925       selectV0 = fV0Filter->IsSelected(&v0objects);
926       // this is a little awkward but otherwise the 
927       // list wants to access the pointer (delete it) 
928       // again when going out of scope
929       delete v0objects.RemoveAt(3); // esdVtx created via copy construct
930       esdVtx = 0;
931       if (!selectV0) 
932         continue;
933     }
934     else{
935       delete v0objects.RemoveAt(3); // esdVtx created via copy construct
936       esdVtx = 0;
937     }
938     
939     v0->GetXYZ(pos[0], pos[1], pos[2]);
940     
941     if (!fOldESDformat) {
942             chi2 = v0->GetChi2V0(); // = chi2/NDF since NDF = 2*2-3
943             v0->GetPosCov(covVtx);
944     } else {
945             chi2 = -999.;
946             for (Int_t i = 0; i < 6; i++)  covVtx[i] = 0.;
947     }
948     
949     
950     AliAODVertex * vV0 = 
951           new(Vertices()[fNumberOfVertices++]) AliAODVertex(pos,
952                                                       covVtx,
953                                                       chi2,
954                                                       fPrimaryVertex,
955                                                       nV0,
956                                                       AliAODVertex::kV0);
957     fPrimaryVertex->AddDaughter(vV0);
958     
959     
960     // Add the positive tracks from the V0
961     
962
963     esdV0Pos->GetPxPyPz(momPos);
964     esdV0Pos->GetXYZ(pos);
965     esdV0Pos->GetCovarianceXYZPxPyPz(covTr);
966     esdV0Pos->GetESDpid(pid);
967     esdV0Pos->GetTOFLabel(tofLabel);
968     
969     const AliESDVertex *vtx = esd.GetPrimaryVertex();
970
971     if (!fUsedTrack[posFromV0]) {
972             fUsedTrack[posFromV0] = kTRUE;
973             UInt_t selectInfo = 0;
974             if (fTrackFilter) selectInfo = fTrackFilter->IsSelected(esdV0Pos);
975             if(fMChandler)fMChandler->SelectParticle(esdV0Pos->GetLabel());
976             aodTrack = new(Tracks()[fNumberOfTracks++]) AliAODTrack(esdV0Pos->GetID(),
977                                                     esdV0Pos->GetLabel(), 
978                                                     momPos, 
979                                                     kTRUE,
980                                                     pos,
981                                                     kFALSE,
982                                                     covTr, 
983                                                     (Short_t)esdV0Pos->GetSign(),
984                                                     esdV0Pos->GetITSClusterMap(), 
985                                                     pid,
986                                                     vV0,
987                                                     kTRUE,  // check if this is right
988                                                     vtx->UsesTrack(esdV0Pos->GetID()),
989                                                     AliAODTrack::kSecondary,
990                                                     selectInfo);
991             aodTrack->SetTPCFitMap(esdV0Pos->GetTPCFitMap());
992             aodTrack->SetTPCClusterMap(esdV0Pos->GetTPCClusterMap());
993             aodTrack->SetTPCSharedMap (esdV0Pos->GetTPCSharedMap());
994             aodTrack->SetChi2perNDF(Chi2perNDF(esdV0Pos));
995             aodTrack->SetTPCPointsF(esdV0Pos->GetTPCNclsF());
996             aodTrack->SetTPCNCrossedRows(UShort_t(esdV0Pos->GetTPCCrossedRows()));
997             aodTrack->SetIntegratedLength(esdV0Pos->GetIntegratedLength());
998             aodTrack->SetTOFLabel(tofLabel);
999             CopyCaloProps(esdV0Pos,aodTrack);
1000             fAODTrackRefs->AddAt(aodTrack,posFromV0);
1001             //      if (fDebug > 0) printf("-------------------Bo: pos track from original pt %.3f \n",aodTrack->Pt());
1002             if (esdV0Pos->GetSign() > 0) ++fNumberOfPositiveTracks;
1003             aodTrack->ConvertAliPIDtoAODPID();
1004             aodTrack->SetFlags(esdV0Pos->GetStatus());
1005             SetAODPID(esdV0Pos,aodTrack,detpid);
1006     }
1007     else {
1008             aodTrack = static_cast<AliAODTrack*>(fAODTrackRefs->At(posFromV0));
1009             //      if (fDebug > 0) printf("-------------------Bo pos track from refArray pt %.3f \n",aodTrack->Pt());
1010     }
1011     vV0->AddDaughter(aodTrack);
1012     
1013     // Add the negative tracks from the V0
1014     
1015     esdV0Neg->GetPxPyPz(momNeg);
1016     esdV0Neg->GetXYZ(pos);
1017     esdV0Neg->GetCovarianceXYZPxPyPz(covTr);
1018     esdV0Neg->GetESDpid(pid);
1019     esdV0Neg->GetTOFLabel(tofLabel);
1020     
1021     if (!fUsedTrack[negFromV0]) {
1022             fUsedTrack[negFromV0] = kTRUE;
1023             UInt_t selectInfo = 0;
1024             if (fTrackFilter) selectInfo = fTrackFilter->IsSelected(esdV0Neg);
1025             if(fMChandler)fMChandler->SelectParticle(esdV0Neg->GetLabel());
1026             aodTrack = new(Tracks()[fNumberOfTracks++]) AliAODTrack(esdV0Neg->GetID(),
1027                                                     esdV0Neg->GetLabel(),
1028                                                     momNeg,
1029                                                     kTRUE,
1030                                                     pos,
1031                                                     kFALSE,
1032                                                     covTr, 
1033                                                     (Short_t)esdV0Neg->GetSign(),
1034                                                     esdV0Neg->GetITSClusterMap(), 
1035                                                     pid,
1036                                                     vV0,
1037                                                     kTRUE,  // check if this is right
1038                                                     vtx->UsesTrack(esdV0Neg->GetID()),
1039                                                     AliAODTrack::kSecondary,
1040                                                     selectInfo);
1041             aodTrack->SetTPCFitMap(esdV0Neg->GetTPCFitMap());
1042             aodTrack->SetTPCClusterMap(esdV0Neg->GetTPCClusterMap());
1043             aodTrack->SetTPCSharedMap (esdV0Neg->GetTPCSharedMap());
1044             aodTrack->SetChi2perNDF(Chi2perNDF(esdV0Neg));
1045             aodTrack->SetTPCPointsF(esdV0Neg->GetTPCNclsF());
1046             aodTrack->SetTPCNCrossedRows(UShort_t(esdV0Neg->GetTPCCrossedRows()));
1047             aodTrack->SetIntegratedLength(esdV0Neg->GetIntegratedLength());
1048             aodTrack->SetTOFLabel(tofLabel);
1049             CopyCaloProps(esdV0Neg,aodTrack);
1050             fAODTrackRefs->AddAt(aodTrack,negFromV0);
1051             //      if (fDebug > 0) printf("-------------------Bo: neg track from original pt %.3f \n",aodTrack->Pt());
1052             if (esdV0Neg->GetSign() > 0) ++fNumberOfPositiveTracks;
1053             aodTrack->ConvertAliPIDtoAODPID();
1054             aodTrack->SetFlags(esdV0Neg->GetStatus());
1055             SetAODPID(esdV0Neg,aodTrack,detpid);
1056     }
1057     else {
1058             aodTrack = static_cast<AliAODTrack*>(fAODTrackRefs->At(negFromV0));
1059             //      if (fDebug > 0) printf("-------------------Bo neg track from refArray pt %.3f \n",aodTrack->Pt());
1060     }
1061     vV0->AddDaughter(aodTrack);
1062     
1063     
1064     // Add the V0 the V0 array as well
1065     
1066     Double_t  dcaV0Daughters      = v0->GetDcaV0Daughters();
1067     Double_t  dcaV0ToPrimVertex   = v0->GetD(esd.GetPrimaryVertex()->GetX(),
1068                                              esd.GetPrimaryVertex()->GetY(),
1069                                              esd.GetPrimaryVertex()->GetZ());
1070     
1071     v0->GetPPxPyPz(momPosAtV0vtx[0],momPosAtV0vtx[1],momPosAtV0vtx[2]); 
1072     v0->GetNPxPyPz(momNegAtV0vtx[0],momNegAtV0vtx[1],momNegAtV0vtx[2]); 
1073     
1074     Double_t dcaDaughterToPrimVertex[2] = { 999., 999.}; // ..[0] = DCA in (x,y) for Pos and ..[1] = Neg
1075     dcaDaughterToPrimVertex[0] = TMath::Abs(esdV0Pos->GetD(  esd.GetPrimaryVertex()->GetX(),
1076                                                            esd.GetPrimaryVertex()->GetY(),
1077                                                            esd.GetMagneticField()) );
1078     dcaDaughterToPrimVertex[1] = TMath::Abs(esdV0Neg->GetD(  esd.GetPrimaryVertex()->GetX(),
1079                                                            esd.GetPrimaryVertex()->GetY(),
1080                                                            esd.GetMagneticField()) );
1081     
1082     AliAODv0* aodV0 = new(V0s()[fNumberOfV0s++]) AliAODv0(vV0, 
1083                                                 dcaV0Daughters,
1084                                                 dcaV0ToPrimVertex,
1085                                                 momPosAtV0vtx,
1086                                                 momNegAtV0vtx,
1087                                                 dcaDaughterToPrimVertex);
1088     
1089     // set the aod v0 on-the-fly status
1090     aodV0->SetOnFlyStatus(v0->GetOnFlyStatus());
1091   }//End of loop on V0s 
1092   
1093   V0s().Expand(fNumberOfV0s);    
1094 }
1095
1096 //______________________________________________________________________________
1097 void AliAnalysisTaskESDfilter::ConvertTPCOnlyTracks(const AliESDEvent& esd)
1098 {
1099   // Convert TPC only tracks
1100   // Here we have wo hybrid appraoch to remove fakes
1101   // ******* ITSTPC ********
1102   // Uses a cut on the ITS properties to select global tracks
1103   // which are than marked as HybdridITSTPC for the remainder 
1104   // the TPC only tracks are flagged as HybridITSTPConly. 
1105   // Note, in order not to get fakes back in the TPC cuts, one needs 
1106   // two "ITS" cuts one tight (1) (to throw out fakes) and one lose (2) (to NOT flag the trakcs in the TPC only)
1107   // using cut number (3)
1108   // so fHybridFilterMask == (1)|(2) fTPCFilterMask = (3), Usercode needs to slect with mask = (1)|(3) and track->IsHybridITSTPC()
1109   // ******* TPC ********
1110   // Here, only TPC tracks are flagged that pass the tight ITS cuts and tracks that pass the TPC cuts and NOT the loose ITS cuts
1111   // the ITS cuts neeed to be added to the filter as extra cuts, since here the selections info is reset in the global and put to the TPC only track
1112
1113   AliCodeTimerAuto("",0);
1114   
1115   // Loop over the tracks and extract and mask out all aod tracks that pass the selections for AODt racks
1116   for(int it = 0;it < fNumberOfTracks;++it)
1117   {
1118     AliAODTrack *tr = (AliAODTrack*)(Tracks().At(it));
1119     if(!tr)continue;
1120     UInt_t map = tr->GetFilterMap();
1121     if(map&fTPCConstrainedFilterMask){
1122       // we only reset the track select ionfo, no deletion...
1123       tr->SetFilterMap(map&~fTPCConstrainedFilterMask);
1124     }
1125     if(map&fHybridFilterMaskTPCCG){
1126       // this is one part of the hybrid tracks
1127       // the others not passing the selection will be TPC only selected below
1128       tr->SetIsHybridTPCConstrainedGlobal(kTRUE);
1129     }
1130   }
1131   // Loop over the ESD trcks and pick out the tracks passing TPC only cuts
1132   
1133   
1134   const AliESDVertex *vtxSPD = esd.GetPrimaryVertexSPD();
1135   const AliESDVertex *vtx = esd.GetPrimaryVertex();
1136
1137   Double_t pos[3] = { 0. };      
1138   Double_t covTr[21]={0.};
1139   Double_t pid[10]={0.};  
1140
1141
1142   Double_t p[3] = { 0. };
1143
1144   Double_t pDCA[3] = { 0. }; // momentum at DCA
1145   Double_t rDCA[3] = { 0. }; // position at DCA
1146   Float_t  dDCA[2] = {0.};    // DCA to the vertex d and z
1147   Float_t  cDCA[3] = {0.};    // covariance of impact parameters
1148   Int_t    tofLabel[3] = {0};
1149
1150   AliAODTrack* aodTrack(0x0);
1151   //  AliAODPid* detpid(0x0);
1152
1153   // account for change in pT after the constraint
1154   Float_t ptMax = 1E10;
1155   Float_t ptMin = 0;
1156   for(int i = 0;i<32;i++){
1157     if(fTPCConstrainedFilterMask&(1<<i)){
1158       AliESDtrackCuts*cuts = (AliESDtrackCuts*)fTrackFilter->GetCuts()->At(i);
1159       Float_t tmp1= 0,tmp2 = 0;
1160       cuts->GetPtRange(tmp1,tmp2);
1161       if(tmp1>ptMin)ptMin=tmp1;
1162       if(tmp2<ptMax)ptMax=tmp2;
1163     }
1164   } 
1165
1166   for (Int_t nTrack = 0; nTrack < esd.GetNumberOfTracks(); ++nTrack) 
1167   {
1168     AliESDtrack* esdTrack = esd.GetTrack(nTrack); //carefull do not modify it othwise  need to work with a copy 
1169     
1170     UInt_t selectInfo = 0;
1171     Bool_t isHybridITSTPC = false;
1172     //
1173     // Track selection
1174     if (fTrackFilter) {
1175       selectInfo = fTrackFilter->IsSelected(esdTrack);
1176     }
1177
1178     if(!(selectInfo&fHybridFilterMaskTPCCG)){
1179       // not already selected tracks, use second part of hybrid tracks
1180       isHybridITSTPC = true;
1181       // too save space one could only store these...
1182     }
1183
1184     selectInfo &= fTPCConstrainedFilterMask;
1185     if (!selectInfo)continue;
1186     if (fWriteHybridTPCCOnly&&!isHybridITSTPC)continue; // write only complementary tracks
1187     // create a tpc only tracl
1188     AliESDtrack *track = AliESDtrackCuts::GetTPCOnlyTrack(const_cast<AliESDEvent*>(&esd),esdTrack->GetID());
1189     if(!track) continue;
1190     
1191     if(track->Pt()>0.)
1192     {
1193       // only constrain tracks above threshold
1194       AliExternalTrackParam exParam;
1195       // take the B-field from the ESD, no 3D fieldMap available at this point
1196       Bool_t relate = false;
1197       relate = track->RelateToVertexTPC(vtxSPD,esd.GetMagneticField(),kVeryBig,&exParam);
1198       if(!relate){
1199         delete track;
1200         continue;
1201       }
1202       // fetch the track parameters at the DCA (unconstraint)
1203       if(track->GetTPCInnerParam()){
1204         track->GetTPCInnerParam()->GetPxPyPz(pDCA);
1205         track->GetTPCInnerParam()->GetXYZ(rDCA);
1206       }
1207       // get the DCA to the vertex:
1208       track->GetImpactParametersTPC(dDCA,cDCA);
1209       // set the constrained parameters to the track
1210       track->Set(exParam.GetX(),exParam.GetAlpha(),exParam.GetParameter(),exParam.GetCovariance());
1211     }
1212     
1213     track->GetPxPyPz(p);
1214
1215     Float_t pT = track->Pt();
1216     if(pT<ptMin||pT>ptMax){
1217       delete track;
1218       continue;
1219     }
1220
1221     // 
1222
1223
1224     track->GetXYZ(pos);
1225     track->GetCovarianceXYZPxPyPz(covTr);
1226     esdTrack->GetESDpid(pid);// original PID
1227     esdTrack->GetTOFLabel(tofLabel);
1228     if(fMChandler)fMChandler->SelectParticle(esdTrack->GetLabel());
1229     fUsedTrackCopy[nTrack] |= selectInfo;
1230     aodTrack = new(Tracks()[fNumberOfTracks++]) AliAODTrack((track->GetID()+1)*-1,
1231                                                             track->GetLabel(),
1232                                                             p,
1233                                                             kTRUE,
1234                                                             pos,
1235                                                             kFALSE,
1236                                                             covTr, 
1237                                                             (Short_t)track->GetSign(),
1238                                                             track->GetITSClusterMap(), 
1239                                                             pid,
1240                                                             fPrimaryVertex,
1241                                                             kTRUE, // check if this is right
1242                                                             vtx->UsesTrack(track->GetID()),
1243                                                             AliAODTrack::kPrimary, 
1244                                                             selectInfo);
1245     aodTrack->SetIsHybridTPCConstrainedGlobal(isHybridITSTPC);    
1246     aodTrack->SetTPCFitMap(track->GetTPCFitMap());
1247     aodTrack->SetTPCClusterMap(track->GetTPCClusterMap());
1248     aodTrack->SetTPCSharedMap (track->GetTPCSharedMap());
1249     aodTrack->SetIsTPCConstrained(kTRUE);    
1250     aodTrack->SetChi2perNDF(Chi2perNDF(esdTrack)); // original track
1251     // set the DCA values to the AOD track
1252     aodTrack->SetPxPyPzAtDCA(pDCA[0],pDCA[1],pDCA[2]);
1253     aodTrack->SetXYAtDCA(rDCA[0],rDCA[1]);
1254     aodTrack->SetDCA(dDCA[0],dDCA[1]);
1255
1256     aodTrack->SetFlags(track->GetStatus());
1257     aodTrack->SetTPCPointsF(track->GetTPCNclsF());
1258     aodTrack->SetTPCNCrossedRows(UShort_t(track->GetTPCCrossedRows()));
1259     aodTrack->SetIntegratedLength(track->GetIntegratedLength());
1260     aodTrack->SetTOFLabel(tofLabel);
1261     CopyCaloProps(track,aodTrack);
1262   
1263     // do not duplicate PID information 
1264     //    aodTrack->ConvertAliPIDtoAODPID();
1265     //    SetAODPID(esdTrack,aodTrack,detpid);
1266
1267     delete track;
1268   } // end of loop on tracks
1269   
1270 }
1271
1272 //______________________________________________________________________________
1273 void AliAnalysisTaskESDfilter::ConvertGlobalConstrainedTracks(const AliESDEvent& esd)
1274 {
1275
1276   // Here we have the option to store the complement from global constraint information
1277   // to tracks passing tight cuts (1) in order not to get fakes back in, one needs 
1278   // two sets of cuts one tight (1) (to throw out fakes) and one lose (2) (fakes/bad tracks would pass (2) but not (1))
1279   // using cut number (3) selects the tracks that complement (1) e.g. tracks witout ITS refit or cluster requirement
1280
1281
1282   AliCodeTimerAuto("",0);
1283   
1284   // Loop over the tracks and extract and mask out all aod tracks that pass the selections for AODt racks
1285   for(int it = 0;it < fNumberOfTracks;++it)
1286   {
1287     AliAODTrack *tr = (AliAODTrack*)(Tracks().At(it));
1288     if(!tr)continue;
1289     UInt_t map = tr->GetFilterMap();
1290     if(map&fGlobalConstrainedFilterMask){
1291       // we only reset the track select info, no deletion...
1292       // mask reset mask in case track is already taken
1293       tr->SetFilterMap(map&~fGlobalConstrainedFilterMask);
1294     }
1295     if(map&fHybridFilterMaskGCG){
1296       // this is one part of the hybrid tracks
1297       // the others not passing the selection will be the ones selected below
1298       tr->SetIsHybridGlobalConstrainedGlobal(kTRUE);
1299     }
1300   }
1301   // Loop over the ESD trcks and pick out the tracks passing the GlobalConstraint cuts
1302  
1303
1304   Double_t pos[3] = { 0. };      
1305   Double_t covTr[21]={0.};
1306   Double_t pid[10]={0.};  
1307   Double_t p[3] = { 0. };
1308
1309   Double_t pDCA[3] = { 0. }; // momentum at DCA
1310   Double_t rDCA[3] = { 0. }; // position at DCA
1311   Float_t  dDCA[2] = {0.};    // DCA to the vertex d and z
1312   Float_t  cDCA[3] = {0.};    // covariance of impact parameters
1313   Int_t    tofLabel[3] = {0};
1314
1315
1316   AliAODTrack* aodTrack(0x0);
1317   AliAODPid* detpid(0x0);
1318   const AliESDVertex *vtx = esd.GetPrimaryVertex();
1319
1320   // account for change in pT after the constraint
1321   Float_t ptMax = 1E10;
1322   Float_t ptMin = 0;
1323   for(int i = 0;i<32;i++){
1324     if(fGlobalConstrainedFilterMask&(1<<i)){
1325       AliESDtrackCuts*cuts = (AliESDtrackCuts*)fTrackFilter->GetCuts()->At(i);
1326       Float_t tmp1= 0,tmp2 = 0;
1327       cuts->GetPtRange(tmp1,tmp2);
1328       if(tmp1>ptMin)ptMin=tmp1;
1329       if(tmp2<ptMax)ptMax=tmp2;
1330     }
1331   } 
1332
1333
1334
1335  for (Int_t nTrack = 0; nTrack < esd.GetNumberOfTracks(); ++nTrack) 
1336   {
1337     AliESDtrack* esdTrack = esd.GetTrack(nTrack); //carefull do not modify it othwise  need to work with a copy 
1338     const AliExternalTrackParam * exParamGC = esdTrack->GetConstrainedParam();
1339     if(!exParamGC)continue;
1340
1341     UInt_t selectInfo = 0;
1342     Bool_t isHybridGC = false;
1343
1344     //
1345     // Track selection
1346     if (fTrackFilter) {
1347       selectInfo = fTrackFilter->IsSelected(esdTrack);
1348     }
1349
1350
1351     if(!(selectInfo&fHybridFilterMaskGCG))isHybridGC = true;
1352     if (fWriteHybridGCOnly&&!isHybridGC)continue; // write only complementary tracks
1353
1354     selectInfo &= fGlobalConstrainedFilterMask;
1355     if (!selectInfo)continue;
1356     // fetch the track parameters at the DCA (unconstrained)
1357     esdTrack->GetPxPyPz(pDCA);
1358     esdTrack->GetXYZ(rDCA);
1359     // get the DCA to the vertex:
1360     esdTrack->GetImpactParameters(dDCA,cDCA);
1361
1362     if (!esdTrack->GetConstrainedPxPyPz(p)) continue;
1363
1364
1365     Float_t pT = exParamGC->Pt();
1366     if(pT<ptMin||pT>ptMax){
1367       continue;
1368     }
1369
1370
1371     esdTrack->GetConstrainedXYZ(pos);
1372     exParamGC->GetCovarianceXYZPxPyPz(covTr);
1373     esdTrack->GetESDpid(pid);
1374     esdTrack->GetTOFLabel(tofLabel); 
1375     if(fMChandler)fMChandler->SelectParticle(esdTrack->GetLabel());
1376     fUsedTrackCopy[nTrack] |= selectInfo;
1377     aodTrack = new(Tracks()[fNumberOfTracks++]) AliAODTrack((esdTrack->GetID()+1)*-1,
1378                                                             esdTrack->GetLabel(),
1379                                                             p,
1380                                                             kTRUE,
1381                                                             pos,
1382                                                             kFALSE,
1383                                                             covTr, 
1384                                                             (Short_t)esdTrack->GetSign(),
1385                                                             esdTrack->GetITSClusterMap(), 
1386                                                             pid,
1387                                                             fPrimaryVertex,
1388                                                             kTRUE, // check if this is right
1389                                                             vtx->UsesTrack(esdTrack->GetID()),
1390                                                             AliAODTrack::kPrimary, 
1391                                                             selectInfo);
1392     aodTrack->SetIsHybridGlobalConstrainedGlobal(isHybridGC);    
1393     aodTrack->SetIsGlobalConstrained(kTRUE);    
1394     aodTrack->SetTPCFitMap(esdTrack->GetTPCFitMap());
1395     aodTrack->SetTPCClusterMap(esdTrack->GetTPCClusterMap());
1396     aodTrack->SetTPCSharedMap (esdTrack->GetTPCSharedMap());
1397     aodTrack->SetChi2perNDF(Chi2perNDF(esdTrack));
1398
1399     // set the DCA values to the AOD track
1400     aodTrack->SetPxPyPzAtDCA(pDCA[0],pDCA[1],pDCA[2]);
1401     aodTrack->SetXYAtDCA(rDCA[0],rDCA[1]);
1402     aodTrack->SetDCA(dDCA[0],dDCA[1]);
1403     aodTrack->SetFlags(esdTrack->GetStatus());
1404     aodTrack->SetTPCPointsF(esdTrack->GetTPCNclsF());
1405     aodTrack->SetTPCNCrossedRows(UShort_t(esdTrack->GetTPCCrossedRows()));
1406     aodTrack->SetIntegratedLength(esdTrack->GetIntegratedLength());
1407     aodTrack->SetTOFLabel(tofLabel);
1408     CopyCaloProps(esdTrack,aodTrack);
1409     if(isHybridGC){
1410       // only copy AOD information for hybrid, no duplicate information
1411       aodTrack->ConvertAliPIDtoAODPID();
1412       SetAODPID(esdTrack,aodTrack,detpid);
1413     }
1414   } // end of loop on tracks
1415   
1416 }
1417
1418
1419 //______________________________________________________________________________
1420 void AliAnalysisTaskESDfilter::ConvertTracks(const AliESDEvent& esd)
1421 {
1422   // Tracks (primary and orphan)
1423
1424   AliCodeTimerAuto("",0);
1425   
1426   AliDebug(1,Form("NUMBER OF ESD TRACKS %5d\n", esd.GetNumberOfTracks()));
1427   
1428   const AliESDVertex *vtx = esd.GetPrimaryVertex();
1429   Double_t p[3] = { 0. };
1430   Double_t pos[3] = { 0. };
1431   Double_t covTr[21] = { 0. };
1432   Double_t pid[10] = { 0. };
1433   Int_t    tofLabel[3] = {0};
1434   AliAODTrack* aodTrack(0x0);
1435   AliAODPid* detpid(0x0);
1436   
1437   for (Int_t nTrack = 0; nTrack < esd.GetNumberOfTracks(); ++nTrack) 
1438   {
1439     if (fUsedTrack[nTrack]) continue;
1440     
1441     AliESDtrack *esdTrack = esd.GetTrack(nTrack);
1442     UInt_t selectInfo = 0;
1443     //
1444     // Track selection
1445     if (fTrackFilter) {
1446             selectInfo = fTrackFilter->IsSelected(esdTrack);
1447             if (!selectInfo && !vtx->UsesTrack(esdTrack->GetID())) continue;
1448     }
1449     
1450     
1451     esdTrack->GetPxPyPz(p);
1452     esdTrack->GetXYZ(pos);
1453     esdTrack->GetCovarianceXYZPxPyPz(covTr);
1454     esdTrack->GetESDpid(pid);
1455     esdTrack->GetTOFLabel(tofLabel);
1456     if(fMChandler)fMChandler->SelectParticle(esdTrack->GetLabel());
1457     fUsedTrack[nTrack] = kTRUE;
1458     fPrimaryVertex->AddDaughter(aodTrack =
1459                          new(Tracks()[fNumberOfTracks++]) AliAODTrack(esdTrack->GetID(),
1460                                                             esdTrack->GetLabel(),
1461                                                             p,
1462                                                             kTRUE,
1463                                                             pos,
1464                                                             kFALSE,
1465                                                             covTr, 
1466                                                             (Short_t)esdTrack->GetSign(),
1467                                                             esdTrack->GetITSClusterMap(), 
1468                                                             pid,
1469                                                             fPrimaryVertex,
1470                                                             kTRUE, // check if this is right
1471                                                             vtx->UsesTrack(esdTrack->GetID()),
1472                                                             AliAODTrack::kPrimary, 
1473                                                             selectInfo)
1474                          );
1475     aodTrack->SetTPCFitMap(esdTrack->GetTPCFitMap());
1476     aodTrack->SetTPCClusterMap(esdTrack->GetTPCClusterMap());
1477     aodTrack->SetTPCSharedMap (esdTrack->GetTPCSharedMap());
1478     aodTrack->SetChi2perNDF(Chi2perNDF(esdTrack));
1479     aodTrack->SetTPCPointsF(esdTrack->GetTPCNclsF());
1480     aodTrack->SetTPCNCrossedRows(UShort_t(esdTrack->GetTPCCrossedRows()));
1481     aodTrack->SetIntegratedLength(esdTrack->GetIntegratedLength());
1482     aodTrack->SetTOFLabel(tofLabel);
1483     CopyCaloProps(esdTrack,aodTrack);
1484
1485     fAODTrackRefs->AddAt(aodTrack, nTrack);
1486     
1487     if (esdTrack->GetSign() > 0) ++fNumberOfPositiveTracks;
1488     aodTrack->SetFlags(esdTrack->GetStatus());
1489     aodTrack->ConvertAliPIDtoAODPID();
1490     SetAODPID(esdTrack,aodTrack,detpid);
1491   } // end of loop on tracks
1492 }
1493
1494 //______________________________________________________________________________
1495 void AliAnalysisTaskESDfilter::ConvertPmdClusters(const AliESDEvent& esd)
1496 {
1497 // Convert PMD Clusters 
1498   AliCodeTimerAuto("",0);
1499   Int_t jPmdClusters=0;
1500   // Access to the AOD container of PMD clusters
1501   TClonesArray &pmdClusters = *(AODEvent()->GetPmdClusters());
1502   for (Int_t iPmd = 0; iPmd < esd.GetNumberOfPmdTracks(); ++iPmd) {
1503     // file pmd clusters, to be revised!
1504     AliESDPmdTrack *pmdTrack = esd.GetPmdTrack(iPmd);
1505     Int_t nLabel = 0;
1506     Int_t *label = 0x0;
1507     Double_t posPmd[3] = { pmdTrack->GetClusterX(), pmdTrack->GetClusterY(), pmdTrack->GetClusterZ()};
1508     Double_t pidPmd[13] = { 0.}; // to be revised!
1509     // type not set!
1510     // assoc cluster not set
1511     new(pmdClusters[jPmdClusters++]) AliAODPmdCluster(iPmd, nLabel, label, pmdTrack->GetClusterADC(), posPmd, pidPmd);
1512   }
1513 }
1514
1515
1516 //______________________________________________________________________________
1517 void AliAnalysisTaskESDfilter::ConvertCaloClusters(const AliESDEvent& esd)
1518 {
1519 // Convert Calorimeter Clusters
1520   AliCodeTimerAuto("",0);
1521   
1522   // Access to the AOD container of clusters
1523   TClonesArray &caloClusters = *(AODEvent()->GetCaloClusters());
1524   Int_t jClusters(0);
1525   
1526   for (Int_t iClust=0; iClust<esd.GetNumberOfCaloClusters(); ++iClust) {
1527     
1528     AliESDCaloCluster * cluster = esd.GetCaloCluster(iClust);
1529     
1530     Int_t  id        = cluster->GetID();
1531     Int_t  nLabel    = cluster->GetNLabels();
1532     Int_t *labels    = cluster->GetLabels();
1533     if(labels){ 
1534                   for(int i = 0;i < nLabel;++i){
1535                           if(fMChandler)fMChandler->SelectParticle(labels[i]);
1536                   }
1537           }             
1538     
1539     Float_t energy = cluster->E();
1540     Float_t posF[3] = { 0.};
1541     cluster->GetPosition(posF);
1542     
1543     AliAODCaloCluster *caloCluster = new(caloClusters[jClusters++]) AliAODCaloCluster(id,
1544                                                                                       nLabel,
1545                                                                                       labels,
1546                                                                                       energy,
1547                                                                                       posF,
1548                                                                                       NULL,
1549                                                                                       cluster->GetType(),0);
1550     
1551     caloCluster->SetCaloCluster(cluster->GetDistanceToBadChannel(),
1552                                 cluster->GetDispersion(),
1553                                 cluster->GetM20(), cluster->GetM02(),
1554                                 cluster->GetEmcCpvDistance(),  
1555                                 cluster->GetNExMax(),cluster->GetTOF()) ;
1556     
1557     caloCluster->SetPIDFromESD(cluster->GetPID());
1558     caloCluster->SetNCells(cluster->GetNCells());
1559     caloCluster->SetCellsAbsId(cluster->GetCellsAbsId());
1560     caloCluster->SetCellsAmplitudeFraction(cluster->GetCellsAmplitudeFraction());
1561
1562     caloCluster->SetTrackDistance(cluster->GetTrackDx(), cluster->GetTrackDz());
1563     
1564     Int_t nMatchCount = 0;
1565     TArrayI* matchedT =         cluster->GetTracksMatched();
1566     if (fNumberOfTracks>0 && matchedT && cluster->GetTrackMatchedIndex() >= 0) {        
1567       for (Int_t im = 0; im < matchedT->GetSize(); im++) {
1568         Int_t iESDtrack = matchedT->At(im);;
1569         if (fAODTrackRefs->At(iESDtrack) != 0) {
1570           caloCluster->AddTrackMatched((AliAODTrack*)fAODTrackRefs->At(iESDtrack));
1571           nMatchCount++;
1572         }
1573       }
1574     }
1575     if(nMatchCount==0)
1576       caloCluster->SetTrackDistance(-999,-999);
1577     
1578   } 
1579   caloClusters.Expand(jClusters); // resize TObjArray to 'remove' slots for pseudo clusters      
1580 }
1581
1582 //______________________________________________________________________________
1583 void AliAnalysisTaskESDfilter::ConvertCaloTrigger(TString calo, const AliESDEvent& esd)
1584 {
1585         AliCodeTimerAuto("",0);
1586                 
1587         if (calo == "PHOS") 
1588         {
1589           AliAODCaloTrigger &aodTrigger = *(AODEvent()->GetCaloTrigger(calo));
1590           AliESDCaloTrigger &esdTrigger = *(esd.GetCaloTrigger(calo));
1591
1592           aodTrigger.Allocate(esdTrigger.GetEntries());
1593           esdTrigger.Reset();
1594
1595           Float_t a;
1596           Int_t tmod,tabsId;
1597
1598           while (esdTrigger.Next()) {
1599             esdTrigger.GetPosition(tmod,tabsId);
1600             esdTrigger.GetAmplitude(a);
1601             aodTrigger.Add(tmod,tabsId,a,0.,(Int_t*)NULL,0,0,0);
1602           }
1603
1604           return;
1605         }
1606                         
1607         AliAODHandler *aodHandler = dynamic_cast<AliAODHandler*>(AliAnalysisManager::GetAnalysisManager()->GetOutputEventHandler()); 
1608                         
1609         if (aodHandler)
1610         {
1611                 TTree *aodTree = aodHandler->GetTree();
1612                                         
1613                 if (aodTree)
1614                 {
1615                         Int_t *type = esd.GetCaloTriggerType();
1616                                                         
1617                         for (Int_t i = 0; i < 15; i++) 
1618                         {
1619                                 aodTree->GetUserInfo()->Add(new TParameter<int>(Form("EMCALCaloTrigger%d",i), type[i]));
1620                         }
1621                 }
1622         }
1623                                                 
1624         AliAODCaloTrigger &aodTrigger = *(AODEvent()->GetCaloTrigger(calo));
1625                                                 
1626         AliESDCaloTrigger &esdTrigger = *(esd.GetCaloTrigger(calo));
1627                                                 
1628         aodTrigger.Allocate(esdTrigger.GetEntries());
1629                                                 
1630         esdTrigger.Reset();
1631         while (esdTrigger.Next())
1632         {         
1633                 Int_t px, py, ts, nTimes, times[10], b; 
1634                 Float_t a, t;
1635                                                                 
1636                 esdTrigger.GetPosition(px, py);
1637                                                 
1638                 esdTrigger.GetAmplitude(a);
1639                 esdTrigger.GetTime(t);
1640                                                                 
1641                 esdTrigger.GetL0Times(times);
1642                 esdTrigger.GetNL0Times(nTimes);
1643                                                                 
1644                 esdTrigger.GetL1TimeSum(ts);
1645                                                                 
1646                 esdTrigger.GetTriggerBits(b);
1647                                                                 
1648                 aodTrigger.Add(px, py, a, t, times, nTimes, ts, b);
1649         }
1650                                                         
1651         for (int i = 0; i < 4; i++) aodTrigger.SetL1Threshold(i, esdTrigger.GetL1Threshold(i));
1652                                                         
1653         Int_t v0[2] = 
1654         {
1655                 esdTrigger.GetL1V0(0),
1656                 esdTrigger.GetL1V0(1)
1657         };              
1658                                                                 
1659         aodTrigger.SetL1V0(v0); 
1660         aodTrigger.SetL1FrameMask(esdTrigger.GetL1FrameMask());
1661 }
1662
1663 //______________________________________________________________________________
1664 void AliAnalysisTaskESDfilter::ConvertEMCALCells(const AliESDEvent& esd)
1665 {
1666 // Convert EMCAL Cells
1667   AliCodeTimerAuto("",0);
1668   // fill EMCAL cell info
1669   if (esd.GetEMCALCells()) { // protection against missing ESD information
1670     AliESDCaloCells &esdEMcells = *(esd.GetEMCALCells());
1671     Int_t nEMcell = esdEMcells.GetNumberOfCells() ;
1672     
1673     AliAODCaloCells &aodEMcells = *(AODEvent()->GetEMCALCells());
1674     aodEMcells.CreateContainer(nEMcell);
1675     aodEMcells.SetType(AliAODCaloCells::kEMCALCell);
1676     for (Int_t iCell = 0; iCell < nEMcell; iCell++) {      
1677       aodEMcells.SetCell(iCell,esdEMcells.GetCellNumber(iCell),esdEMcells.GetAmplitude(iCell),
1678                          esdEMcells.GetTime(iCell), esdEMcells.GetMCLabel(iCell), esdEMcells.GetEFraction(iCell));
1679     }
1680     aodEMcells.Sort();
1681   }
1682 }
1683
1684 //______________________________________________________________________________
1685 void AliAnalysisTaskESDfilter::ConvertPHOSCells(const AliESDEvent& esd)
1686 {
1687 // Convert PHOS Cells
1688   AliCodeTimerAuto("",0);
1689   // fill PHOS cell info
1690   if (esd.GetPHOSCells()) { // protection against missing ESD information
1691     AliESDCaloCells &esdPHcells = *(esd.GetPHOSCells());
1692     Int_t nPHcell = esdPHcells.GetNumberOfCells() ;
1693     
1694     AliAODCaloCells &aodPHcells = *(AODEvent()->GetPHOSCells());
1695     aodPHcells.CreateContainer(nPHcell);
1696     aodPHcells.SetType(AliAODCaloCells::kPHOSCell);
1697     for (Int_t iCell = 0; iCell < nPHcell; iCell++) {      
1698       aodPHcells.SetCell(iCell,esdPHcells.GetCellNumber(iCell),esdPHcells.GetAmplitude(iCell),
1699                          esdPHcells.GetTime(iCell), esdPHcells.GetMCLabel(iCell), esdPHcells.GetEFraction(iCell));
1700     }
1701     aodPHcells.Sort();
1702   }
1703 }
1704
1705 //______________________________________________________________________________
1706 void AliAnalysisTaskESDfilter::ConvertTracklets(const AliESDEvent& esd)
1707 {
1708   // tracklets    
1709   AliCodeTimerAuto("",0);
1710
1711   AliAODTracklets &SPDTracklets = *(AODEvent()->GetTracklets());
1712   const AliMultiplicity *mult = esd.GetMultiplicity();
1713   if (mult) {
1714     if (mult->GetNumberOfTracklets()>0) {
1715       SPDTracklets.CreateContainer(mult->GetNumberOfTracklets());
1716       
1717       for (Int_t n=0; n<mult->GetNumberOfTracklets(); n++) {
1718         if(fMChandler){
1719           fMChandler->SelectParticle(mult->GetLabel(n, 0));
1720           fMChandler->SelectParticle(mult->GetLabel(n, 1));
1721         }
1722         SPDTracklets.SetTracklet(n, mult->GetTheta(n), mult->GetPhi(n), mult->GetDeltaPhi(n), mult->GetLabel(n, 0),mult->GetLabel(n, 1));
1723       }
1724     }
1725   } else {
1726     //Printf("ERROR: AliMultiplicity could not be retrieved from ESD");
1727   }
1728 }
1729
1730 //______________________________________________________________________________
1731 void AliAnalysisTaskESDfilter::ConvertKinks(const AliESDEvent& esd)
1732 {
1733   AliCodeTimerAuto("",0);
1734   
1735   // Kinks: it is a big mess the access to the information in the kinks
1736   // The loop is on the tracks in order to find the mother and daugther of each kink
1737   
1738   Double_t covTr[21]={0.};
1739   Double_t pid[10]={0.};
1740   AliAODPid* detpid(0x0);
1741   Int_t tofLabel[3] = {0};
1742   
1743   fNumberOfKinks = esd.GetNumberOfKinks();
1744
1745   const AliESDVertex* vtx = esd.GetPrimaryVertex();
1746   
1747   for (Int_t iTrack=0; iTrack<esd.GetNumberOfTracks(); ++iTrack) 
1748   {
1749     AliESDtrack * esdTrack = esd.GetTrack(iTrack);
1750     
1751     Int_t ikink = esdTrack->GetKinkIndex(0);
1752     
1753     if (ikink && fNumberOfKinks) {
1754             // Negative kink index: mother, positive: daughter
1755             
1756             // Search for the second track of the kink
1757             
1758             for (Int_t jTrack = iTrack+1; jTrack<esd.GetNumberOfTracks(); ++jTrack) {
1759         
1760         AliESDtrack * esdTrack1 = esd.GetTrack(jTrack);
1761         
1762         Int_t jkink = esdTrack1->GetKinkIndex(0);
1763         
1764         if ( TMath::Abs(ikink)==TMath::Abs(jkink) ) {
1765           
1766           // The two tracks are from the same kink
1767           
1768           if (fUsedKink[TMath::Abs(ikink)-1]) continue; // skip used kinks
1769           
1770           Int_t imother = -1;
1771           Int_t idaughter = -1;
1772           
1773           if (ikink<0 && jkink>0) {
1774             
1775             imother = iTrack;
1776             idaughter = jTrack;
1777           }
1778           else if (ikink>0 && jkink<0) {
1779             
1780             imother = jTrack;
1781             idaughter = iTrack;
1782           }
1783           else {
1784             //                  cerr << "Error: Wrong combination of kink indexes: "
1785             //                       << ikink << " " << jkink << endl;
1786             continue;
1787           }
1788           
1789           // Add the mother track if it passed primary track selection cuts
1790           
1791           AliAODTrack * mother = NULL;
1792           
1793           UInt_t selectInfo = 0;
1794           if (fTrackFilter) {
1795             selectInfo = fTrackFilter->IsSelected(esd.GetTrack(imother));
1796             if (!selectInfo) continue;
1797           }
1798           
1799           if (!fUsedTrack[imother]) {
1800             
1801             fUsedTrack[imother] = kTRUE;
1802             
1803             AliESDtrack *esdTrackM = esd.GetTrack(imother);
1804             Double_t p[3] = { 0. };
1805             Double_t pos[3] = { 0. };
1806             esdTrackM->GetPxPyPz(p);
1807             esdTrackM->GetXYZ(pos);
1808             esdTrackM->GetCovarianceXYZPxPyPz(covTr);
1809             esdTrackM->GetESDpid(pid);
1810             esdTrackM->GetTOFLabel(tofLabel);
1811             if(fMChandler)fMChandler->SelectParticle(esdTrackM->GetLabel());
1812             mother = 
1813             new(Tracks()[fNumberOfTracks++]) AliAODTrack(esdTrackM->GetID(),
1814                                                esdTrackM->GetLabel(),
1815                                                p,
1816                                                kTRUE,
1817                                                pos,
1818                                                kFALSE,
1819                                                covTr, 
1820                                                (Short_t)esdTrackM->GetSign(),
1821                                                esdTrackM->GetITSClusterMap(), 
1822                                                pid,
1823                                                fPrimaryVertex,
1824                                                kTRUE, // check if this is right
1825                                                vtx->UsesTrack(esdTrack->GetID()),
1826                                                AliAODTrack::kPrimary,
1827                                                selectInfo);
1828             mother->SetTPCFitMap(esdTrackM->GetTPCFitMap());
1829             mother->SetTPCClusterMap(esdTrackM->GetTPCClusterMap());
1830             mother->SetTPCSharedMap (esdTrackM->GetTPCSharedMap());
1831             mother->SetChi2perNDF(Chi2perNDF(esdTrackM));
1832             mother->SetTPCPointsF(esdTrackM->GetTPCNclsF());
1833             mother->SetTPCNCrossedRows(UShort_t(esdTrackM->GetTPCCrossedRows()));
1834             mother->SetIntegratedLength(esdTrackM->GetIntegratedLength());
1835             mother->SetTOFLabel(tofLabel);
1836             CopyCaloProps(esdTrackM,mother);
1837             fAODTrackRefs->AddAt(mother, imother);
1838             
1839             if (esdTrackM->GetSign() > 0) ++fNumberOfPositiveTracks;
1840             mother->SetFlags(esdTrackM->GetStatus());
1841             mother->ConvertAliPIDtoAODPID();
1842             fPrimaryVertex->AddDaughter(mother);
1843             mother->ConvertAliPIDtoAODPID();
1844             SetAODPID(esdTrackM,mother,detpid);
1845           }
1846           else {
1847             //                  cerr << "Error: event " << esd.GetEventNumberInFile() << " kink " << TMath::Abs(ikink)-1
1848             //                       << " track " << imother << " has already been used!" << endl;
1849           }
1850           
1851           // Add the kink vertex
1852           AliESDkink * kink = esd.GetKink(TMath::Abs(ikink)-1);
1853           
1854           AliAODVertex * vkink = 
1855           new(Vertices()[fNumberOfVertices++]) AliAODVertex(kink->GetPosition(),
1856                                                   NULL,
1857                                                   0.,
1858                                                   mother,
1859                                                   esdTrack->GetID(),  // This is the track ID of the mother's track!
1860                                                   AliAODVertex::kKink);
1861           // Add the daughter track
1862           
1863           AliAODTrack * daughter = NULL;
1864           
1865           if (!fUsedTrack[idaughter]) {
1866             
1867             fUsedTrack[idaughter] = kTRUE;
1868             
1869             AliESDtrack *esdTrackD = esd.GetTrack(idaughter);
1870             Double_t p[3] = { 0. };
1871             Double_t pos[3] = { 0. };
1872
1873             esdTrackD->GetPxPyPz(p);
1874             esdTrackD->GetXYZ(pos);
1875             esdTrackD->GetCovarianceXYZPxPyPz(covTr);
1876             esdTrackD->GetESDpid(pid);
1877             esdTrackD->GetTOFLabel(tofLabel);
1878             selectInfo = 0;
1879             if (fTrackFilter) selectInfo = fTrackFilter->IsSelected(esdTrackD);
1880             if(fMChandler)fMChandler->SelectParticle(esdTrackD->GetLabel());
1881             daughter = 
1882             new(Tracks()[fNumberOfTracks++]) AliAODTrack(esdTrackD->GetID(),
1883                                                esdTrackD->GetLabel(),
1884                                                p,
1885                                                kTRUE,
1886                                                pos,
1887                                                kFALSE,
1888                                                covTr, 
1889                                                (Short_t)esdTrackD->GetSign(),
1890                                                esdTrackD->GetITSClusterMap(), 
1891                                                pid,
1892                                                vkink,
1893                                                kTRUE, // check if this is right
1894                                                vtx->UsesTrack(esdTrack->GetID()),
1895                                                AliAODTrack::kSecondary,
1896                                                selectInfo);
1897             daughter->SetTPCFitMap(esdTrackD->GetTPCFitMap());
1898             daughter->SetTPCClusterMap(esdTrackD->GetTPCClusterMap());
1899             daughter->SetTPCSharedMap (esdTrackD->GetTPCSharedMap());
1900             daughter->SetTPCPointsF(esdTrackD->GetTPCNclsF());
1901             daughter->SetTPCNCrossedRows(UShort_t(esdTrackD->GetTPCCrossedRows()));
1902             daughter->SetIntegratedLength(esdTrackD->GetIntegratedLength());
1903             daughter->SetTOFLabel(tofLabel);
1904             CopyCaloProps(esdTrackD,daughter);
1905             fAODTrackRefs->AddAt(daughter, idaughter);
1906             
1907             if (esdTrackD->GetSign() > 0) ++fNumberOfPositiveTracks;
1908             daughter->SetFlags(esdTrackD->GetStatus());
1909             daughter->ConvertAliPIDtoAODPID();
1910             vkink->AddDaughter(daughter);
1911             daughter->ConvertAliPIDtoAODPID();
1912             SetAODPID(esdTrackD,daughter,detpid);
1913           }
1914           else {
1915             //                  cerr << "Error: event " << esd.GetEventNumberInFile() << " kink " << TMath::Abs(ikink)-1
1916             //                       << " track " << idaughter << " has already been used!" << endl;
1917           }
1918         }
1919             }
1920     }      
1921   }
1922 }
1923
1924 //______________________________________________________________________________
1925 void AliAnalysisTaskESDfilter::ConvertPrimaryVertices(const AliESDEvent& esd)
1926 {
1927   AliCodeTimerAuto("",0);
1928   
1929   // Access to the AOD container of vertices
1930   fNumberOfVertices = 0;
1931   
1932   Double_t pos[3] = { 0. };
1933   Double_t covVtx[6] = { 0. };
1934
1935   // Add primary vertex. The primary tracks will be defined
1936   // after the loops on the composite objects (V0, cascades, kinks)
1937   const AliESDVertex *vtx = esd.GetPrimaryVertex();
1938   
1939   vtx->GetXYZ(pos); // position
1940   vtx->GetCovMatrix(covVtx); //covariance matrix
1941   
1942   fPrimaryVertex = new(Vertices()[fNumberOfVertices++])
1943   AliAODVertex(pos, covVtx, vtx->GetChi2toNDF(), NULL, -1, AliAODVertex::kPrimary);
1944   fPrimaryVertex->SetName(vtx->GetName());
1945   fPrimaryVertex->SetTitle(vtx->GetTitle());
1946   fPrimaryVertex->SetBC(vtx->GetBC());
1947   
1948   TString vtitle = vtx->GetTitle();
1949   if (!vtitle.Contains("VertexerTracks")) 
1950     fPrimaryVertex->SetNContributors(vtx->GetNContributors());
1951   
1952   if (fDebug > 0) fPrimaryVertex->Print();  
1953   
1954   // Add SPD "main" vertex 
1955   const AliESDVertex *vtxS = esd.GetPrimaryVertexSPD();
1956   vtxS->GetXYZ(pos); // position
1957   vtxS->GetCovMatrix(covVtx); //covariance matrix
1958   AliAODVertex * mVSPD = new(Vertices()[fNumberOfVertices++])
1959   AliAODVertex(pos, covVtx, vtxS->GetChi2toNDF(), NULL, -1, AliAODVertex::kMainSPD);
1960   mVSPD->SetName(vtxS->GetName());
1961   mVSPD->SetTitle(vtxS->GetTitle());
1962   mVSPD->SetNContributors(vtxS->GetNContributors()); 
1963   
1964   // Add SPD pileup vertices
1965   for(Int_t iV=0; iV<esd.GetNumberOfPileupVerticesSPD(); ++iV)
1966   {
1967     const AliESDVertex *vtxP = esd.GetPileupVertexSPD(iV);
1968     vtxP->GetXYZ(pos); // position
1969     vtxP->GetCovMatrix(covVtx); //covariance matrix
1970     AliAODVertex * pVSPD =  new(Vertices()[fNumberOfVertices++])
1971     AliAODVertex(pos, covVtx, vtxP->GetChi2toNDF(), NULL, -1, AliAODVertex::kPileupSPD);
1972     pVSPD->SetName(vtxP->GetName());
1973     pVSPD->SetTitle(vtxP->GetTitle());
1974     pVSPD->SetNContributors(vtxP->GetNContributors()); 
1975     pVSPD->SetBC(vtxP->GetBC());
1976   }
1977   
1978   // Add TRK pileup vertices
1979   for(Int_t iV=0; iV<esd.GetNumberOfPileupVerticesTracks(); ++iV)
1980   {
1981     const AliESDVertex *vtxP = esd.GetPileupVertexTracks(iV);
1982     vtxP->GetXYZ(pos); // position
1983     vtxP->GetCovMatrix(covVtx); //covariance matrix
1984     AliAODVertex * pVTRK = new(Vertices()[fNumberOfVertices++])
1985     AliAODVertex(pos, covVtx, vtxP->GetChi2toNDF(), NULL, -1, AliAODVertex::kPileupTracks);
1986     pVTRK->SetName(vtxP->GetName());
1987     pVTRK->SetTitle(vtxP->GetTitle());
1988     pVTRK->SetNContributors(vtxP->GetNContributors());
1989     pVTRK->SetBC(vtxP->GetBC());
1990   }
1991
1992   // Add TPC "main" vertex 
1993   const AliESDVertex *vtxT = esd.GetPrimaryVertexTPC();
1994   vtxT->GetXYZ(pos); // position
1995   vtxT->GetCovMatrix(covVtx); //covariance matrix
1996   AliAODVertex * mVTPC = new(Vertices()[fNumberOfVertices++])
1997   AliAODVertex(pos, covVtx, vtxT->GetChi2toNDF(), NULL, -1, AliAODVertex::kMainTPC);
1998   mVTPC->SetName(vtxT->GetName());
1999   mVTPC->SetTitle(vtxT->GetTitle());
2000   mVTPC->SetNContributors(vtxT->GetNContributors()); 
2001
2002
2003 }
2004
2005 //______________________________________________________________________________
2006 void AliAnalysisTaskESDfilter::ConvertVZERO(const AliESDEvent& esd)
2007 {
2008   // Convert VZERO data
2009   AliAODVZERO* vzeroData = AODEvent()->GetVZEROData();
2010   *vzeroData = *(esd.GetVZEROData());
2011 }
2012
2013 //______________________________________________________________________________
2014 void AliAnalysisTaskESDfilter::ConvertTZERO(const AliESDEvent& esd)
2015 {
2016   // Convert TZERO data
2017   const AliESDTZERO* esdTzero = esd.GetESDTZERO(); 
2018   AliAODTZERO* aodTzero = AODEvent()->GetTZEROData();
2019
2020   for (Int_t icase=0; icase<3; icase++){ 
2021     aodTzero->SetT0TOF(    icase, esdTzero->GetT0TOF(icase));
2022     aodTzero->SetT0TOFbest(icase, esdTzero->GetT0TOFbest(icase)); 
2023   }
2024   aodTzero->SetBackgroundFlag(esdTzero->GetBackgroundFlag());
2025   aodTzero->SetPileupFlag(esdTzero->GetPileupFlag());
2026   aodTzero->SetSatelliteFlag(esdTzero->GetSatellite()); 
2027
2028   Float_t rawTime[24];
2029   for(Int_t ipmt=0; ipmt<24; ipmt++)
2030     rawTime[ipmt] = esdTzero->GetTimeFull(ipmt,0);
2031    
2032   Int_t idxOfFirstPmtA = -1,       idxOfFirstPmtC = -1;
2033   Float_t timeOfFirstPmtA = 9999, timeOfFirstPmtC = 9999;
2034   for(int ipmt=0;  ipmt<12; ipmt++){
2035     if( rawTime[ipmt] > -200 && rawTime[ipmt] < timeOfFirstPmtC && rawTime[ipmt]!=0){
2036       timeOfFirstPmtC = rawTime[ipmt];
2037       idxOfFirstPmtC  = ipmt;
2038     }
2039   }
2040   for(int ipmt=12; ipmt<24; ipmt++){
2041     if( rawTime[ipmt] > -200 && rawTime[ipmt] < timeOfFirstPmtA && rawTime[ipmt]!=0 ){
2042       timeOfFirstPmtA = rawTime[ipmt];
2043       idxOfFirstPmtA  = ipmt;
2044     }
2045   }
2046
2047   if(idxOfFirstPmtA != -1 && idxOfFirstPmtC != -1){
2048     //speed of light in cm/ns   TMath::C()*1e-7 
2049     Float_t vertexraw = TMath::C()*1e-7 * (rawTime[idxOfFirstPmtA] - rawTime[idxOfFirstPmtC])/2;
2050     aodTzero->SetT0VertexRaw( vertexraw );
2051   }else{
2052     aodTzero->SetT0VertexRaw(99999);
2053   }
2054
2055   aodTzero->SetT0zVertex(esdTzero->GetT0zVertex());
2056   //amplitude
2057
2058   const Double32_t *amp=esdTzero->GetT0amplitude();
2059   for(int ipmt=0;  ipmt<24; ipmt++)
2060     aodTzero->SetAmp(ipmt, amp[ipmt]);
2061   aodTzero->SetAmp(24,esdTzero->GetMultC() );
2062   aodTzero->SetAmp(25,esdTzero->GetMultA() );
2063
2064 }
2065
2066
2067 //______________________________________________________________________________
2068 void AliAnalysisTaskESDfilter::ConvertZDC(const AliESDEvent& esd)
2069 {
2070   // Convert ZDC data
2071   AliESDZDC* esdZDC = esd.GetZDCData();
2072   
2073   const Double_t zem1Energy = esdZDC->GetZEM1Energy();
2074   const Double_t zem2Energy = esdZDC->GetZEM2Energy();
2075    
2076   const Double_t *towZNC = esdZDC->GetZNCTowerEnergy();
2077   const Double_t *towZPC = esdZDC->GetZPCTowerEnergy();
2078   const Double_t *towZNA = esdZDC->GetZNATowerEnergy();
2079   const Double_t *towZPA = esdZDC->GetZPATowerEnergy();
2080   const Double_t *towZNCLG = esdZDC->GetZNCTowerEnergyLR();
2081   const Double_t *towZNALG = esdZDC->GetZNATowerEnergyLR();
2082   
2083   AliAODZDC* zdcAOD = AODEvent()->GetZDCData();
2084
2085   zdcAOD->SetZEM1Energy(zem1Energy);
2086   zdcAOD->SetZEM2Energy(zem2Energy);
2087   zdcAOD->SetZNCTowers(towZNC, towZNCLG);
2088   zdcAOD->SetZNATowers(towZNA, towZNALG);
2089   zdcAOD->SetZPCTowers(towZPC);
2090   zdcAOD->SetZPATowers(towZPA);
2091   
2092   zdcAOD->SetZDCParticipants(esdZDC->GetZDCParticipants(), esdZDC->GetZDCPartSideA(), esdZDC->GetZDCPartSideC());
2093   zdcAOD->SetZDCImpactParameter(esdZDC->GetImpactParameter(), esdZDC->GetImpactParamSideA(), 
2094         esdZDC->GetImpactParamSideC());
2095   zdcAOD->SetZDCTDCSum(esdZDC->GetZNTDCSum(0)); 
2096   zdcAOD->SetZDCTDCDiff(esdZDC->GetZNTDCDiff(0));       
2097   if(esdZDC->IsZNChit()) zdcAOD->SetZNCTDC(esdZDC->GetZDCTDCCorrected(10,0));
2098   if(esdZDC->IsZNAhit()) zdcAOD->SetZNATDC(esdZDC->GetZDCTDCCorrected(12,0));
2099 }
2100
2101 //_______________________________________________________________________________________________________________________________________
2102 Int_t AliAnalysisTaskESDfilter::ConvertHMPID(const AliESDEvent& esd) // clm
2103 {
2104   //
2105   // Convtert ESD HMPID info to AOD and return the number of good tracks with HMPID signal.
2106   // We need to return an int since there is no signal counter in the ESD.
2107   //
2108   
2109   AliCodeTimerAuto("",0);
2110   
2111   Int_t cntHmpidGoodTracks = 0;
2112   
2113   Float_t  xMip = 0;
2114   Float_t  yMip = 0;
2115   Int_t    qMip = 0;
2116   Int_t    nphMip = 0;
2117   
2118   Float_t xTrk = 0;
2119   Float_t yTrk = 0;
2120   Float_t thetaTrk = 0;
2121   Float_t phiTrk = 0;
2122   
2123   Double_t hmpPid[5]={0};
2124   Double_t hmpMom[3]={0};
2125   
2126   TClonesArray &hmpidRings = *(AODEvent()->GetHMPIDrings());
2127   
2128  for (Int_t iTrack=0; iTrack<esd.GetNumberOfTracks(); ++iTrack) 
2129   {
2130     if(! esd.GetTrack(iTrack) ) continue;
2131       
2132     if(esd.GetTrack(iTrack)->GetHMPIDsignal() > -20 ) {                  // 
2133        
2134       (esd.GetTrack(iTrack))->GetHMPIDmip(xMip, yMip, qMip, nphMip);     // Get MIP properties
2135       (esd.GetTrack(iTrack))->GetHMPIDtrk(xTrk,yTrk,thetaTrk,phiTrk);
2136       (esd.GetTrack(iTrack))->GetHMPIDpid(hmpPid);
2137       if((esd.GetTrack(iTrack))->GetOuterHmpParam()) (esd.GetTrack(iTrack))->GetOuterHmpPxPyPz(hmpMom);
2138       
2139      if(esd.GetTrack(iTrack)->GetHMPIDsignal() == 0 && thetaTrk == 0 && qMip == 0 && nphMip ==0 ) continue;  //
2140       
2141      new(hmpidRings[cntHmpidGoodTracks++]) AliAODHMPIDrings(
2142                                                                  (esd.GetTrack(iTrack))->GetID(),             // Unique track id to attach the ring to
2143                                                                  1000000*nphMip+qMip,                         // MIP charge and number of photons
2144                                                                  (esd.GetTrack(iTrack))->GetHMPIDcluIdx(),    // 1000000*chamber id + cluster idx of the assigned MIP cluster  
2145                                                                  thetaTrk,                                    // track inclination angle theta
2146                                                                  phiTrk,                                      // track inclination angle phi
2147                                                                  (esd.GetTrack(iTrack))->GetHMPIDsignal(),    // Cherenkov angle
2148                                                                  (esd.GetTrack(iTrack))->GetHMPIDoccupancy(), // Occupancy claculated for the given chamber 
2149                                                                  (esd.GetTrack(iTrack))->GetHMPIDchi2(),      // Ring resolution squared
2150                                                                   xTrk,                                       // Track x coordinate (LORS)
2151                                                                   yTrk,                                       // Track y coordinate (LORS)
2152                                                                   xMip,                                       // MIP x coordinate (LORS)
2153                                                                   yMip,                                       // MIP y coordinate (LORS)
2154                                                                   hmpPid,                                     // PID probablities from ESD, remove later once it is in CombinedPid
2155                                                                   hmpMom                                      // Track momentum in HMPID at ring reconstruction
2156                                                                );  
2157      
2158       //  Printf(Form("+++++++++ yes/no: %d  %lf %lf %lf %lf %lf %lf ",(esd.GetTrack(iTrack))->IsHMPID(),thetaTrk, (esd.GetTrack(iTrack))->GetHMPIDchi2(),xTrk, yTrk , xMip, yMip));
2159      
2160                                                                 
2161    }// HMPID signal > -20
2162   }//___esd track loop
2163   
2164   return cntHmpidGoodTracks;
2165 }
2166
2167 void AliAnalysisTaskESDfilter::ConvertTRD(const AliESDEvent& esd)
2168 {
2169   // fill TRD on-line tracks with assiocated tracklets
2170   // as used for the TRD level-1 triggers
2171
2172   const Int_t nTrdTracks = esd.GetNumberOfTrdTracks();
2173   const Int_t nLayers = 6;
2174
2175   for (Int_t iTrdTrack = 0; iTrdTrack < nTrdTracks; ++iTrdTrack) {
2176     // copy information from ESD track to AOD track
2177     const AliESDTrdTrack *esdTrdTrk = esd.GetTrdTrack(iTrdTrack);
2178     AliAODTrdTrack &aodTrdTrk = AODEvent()->AddTrdTrack(esdTrdTrk);
2179
2180     // copy the contributing tracklets
2181     for (Int_t iTracklet = 0; iTracklet < nLayers; ++iTracklet) {
2182       if (const AliESDTrdTracklet *esdTrdTrkl = esdTrdTrk->GetTracklet(iTracklet))
2183         aodTrdTrk.AddTracklet(*esdTrdTrkl, iTracklet);
2184     }
2185
2186     // add the reference to the matched global track
2187     AliAODTrack *aodTrkMatch = 0x0;
2188     AliESDtrack *esdTrkMatch = (AliESDtrack*) esdTrdTrk->GetTrackMatch();
2189     if (esdTrkMatch) {
2190       Int_t idx = esdTrkMatch->GetID();
2191
2192       if (idx < 0)
2193         AliError("track has a matched track that was not found");
2194       else if (esdTrkMatch != esd.GetTrack(idx))
2195         AliError("wrong track found for ESD track index");
2196       else {
2197         UInt_t selectInfo = fTrackFilter ? fTrackFilter->IsSelected(esdTrkMatch) : 0;
2198
2199         if (fUsedTrack[idx]) {
2200           aodTrkMatch = (AliAODTrack*) (*fAODTrackRefs)[idx];
2201           AliDebug(2, Form("event %lld: existing track (idx %i, pt = %f) matched to TRD track (idx %i, pt = %f), cut flags: 0x%08x",
2202                            Entry(), idx, esdTrkMatch->Pt(), iTrdTrack, esdTrdTrk->Pt(),
2203                            selectInfo));
2204         }
2205         else {
2206           if (selectInfo & fUsedTrackCopy[idx]) {
2207             // mask filter bits already used in track copies
2208             selectInfo &= ~fUsedTrackCopy[idx];
2209             AliWarning(Form("event %lld: copied track (idx %i, pt = %f) matched to TRD track (idx %i, pt = %f), cut flags: 0x%08x -> 0x%08x",
2210                             Entry(), idx, esdTrkMatch->Pt(), iTrdTrack, esdTrdTrk->Pt(),
2211                             fTrackFilter->IsSelected(esdTrkMatch), selectInfo));
2212           }
2213           AliDebug(2, Form("event %lld: unused track (idx %i, pt = %f) matched to TRD track (idx %i, pt = %f), cut flags: 0x%08x -> 0x%08x",
2214                            Entry(), idx, esdTrkMatch->Pt(), iTrdTrack, esdTrdTrk->Pt(),
2215                            fTrackFilter->IsSelected(esdTrkMatch), selectInfo));
2216
2217           Double_t mom[3]={0.};
2218           Double_t pos[3]={0.};
2219           Double_t covTr[21]={0.};
2220           Double_t pid[10]={0.};
2221
2222           esdTrkMatch->GetPxPyPz(mom);
2223           esdTrkMatch->GetXYZ(pos);
2224           esdTrkMatch->GetCovarianceXYZPxPyPz(covTr);
2225           esdTrkMatch->GetESDpid(pid);
2226           const AliESDVertex* vtx = esd.GetPrimaryVertex();
2227
2228           fUsedTrack[idx] = kTRUE;
2229           if(fMChandler) fMChandler->SelectParticle(esdTrkMatch->GetLabel());
2230
2231           aodTrkMatch = new(Tracks()[fNumberOfTracks++])
2232             AliAODTrack(esdTrkMatch->GetID(),
2233                         esdTrkMatch->GetLabel(),
2234                         mom,
2235                         kTRUE,
2236                         pos,
2237                         kFALSE,
2238                         covTr,
2239                         (Short_t)esdTrkMatch->GetSign(),
2240                         esdTrkMatch->GetITSClusterMap(),
2241                         pid,
2242                         fPrimaryVertex,
2243                         kTRUE,
2244                         vtx->UsesTrack(esdTrkMatch->GetID()),
2245                         AliAODTrack::kUndef,
2246                         selectInfo);
2247
2248           aodTrkMatch->SetTPCFitMap(esdTrkMatch->GetTPCFitMap());
2249           aodTrkMatch->SetTPCClusterMap(esdTrkMatch->GetTPCClusterMap());
2250           aodTrkMatch->SetTPCSharedMap (esdTrkMatch->GetTPCSharedMap());
2251           aodTrkMatch->SetChi2perNDF(Chi2perNDF(esdTrkMatch));
2252           aodTrkMatch->SetTPCPointsF(esdTrkMatch->GetTPCNclsF());
2253           aodTrkMatch->SetTPCNCrossedRows(UShort_t(esdTrkMatch->GetTPCCrossedRows()));
2254           aodTrkMatch->SetIntegratedLength(esdTrkMatch->GetIntegratedLength());
2255           CopyCaloProps(esdTrkMatch,aodTrkMatch);
2256           fAODTrackRefs->AddAt(aodTrkMatch,idx);
2257           if (esdTrkMatch->GetSign() > 0) ++fNumberOfPositiveTracks;
2258           aodTrkMatch->ConvertAliPIDtoAODPID();
2259           aodTrkMatch->SetFlags(esdTrkMatch->GetStatus());
2260         }
2261       }
2262     }
2263     aodTrdTrk.SetTrackMatchReference(aodTrkMatch);
2264   }
2265 }
2266
2267 //______________________________________________________________________________
2268 void AliAnalysisTaskESDfilter::ConvertESDtoAOD() 
2269 {
2270   // ESD Filter analysis task executed for each event
2271   
2272   AliESDEvent* esd = dynamic_cast<AliESDEvent*>(InputEvent());
2273   
2274   if(!esd)return;
2275
2276   AliCodeTimerAuto("",0);
2277   
2278   fOldESDformat = ( esd->GetAliESDOld() != 0x0 );
2279  
2280       // Reconstruct cascades and V0 here
2281   if (fIsV0CascadeRecoEnabled) {
2282     esd->ResetCascades();
2283     esd->ResetV0s();
2284
2285     AliV0vertexer lV0vtxer;
2286     AliCascadeVertexer lCascVtxer;
2287
2288     lV0vtxer.SetCuts(fV0Cuts);
2289     lCascVtxer.SetCuts(fCascadeCuts);
2290
2291
2292     lV0vtxer.Tracks2V0vertices(esd);
2293     lCascVtxer.V0sTracks2CascadeVertices(esd);
2294   }
2295
2296   // Perform progagation of tracks if needed
2297   if (fDoPropagateTrackToEMCal) {
2298     const Int_t ntrack = esd->GetNumberOfTracks();
2299     for (Int_t i=0;i<ntrack;++i) {
2300       AliESDtrack *t = esd->GetTrack(i);
2301       AliEMCALRecoUtils::ExtrapolateTrackToEMCalSurface(t,fEMCalSurfaceDistance);
2302     }
2303   }
2304  
2305   fNumberOfTracks = 0;
2306   fNumberOfPositiveTracks = 0;
2307   fNumberOfV0s = 0;
2308   fNumberOfVertices = 0;
2309   fNumberOfCascades = 0;
2310   fNumberOfKinks = 0;
2311     
2312   AliAODHeader* header = ConvertHeader(*esd);
2313
2314   if ( fIsVZEROEnabled ) ConvertVZERO(*esd);
2315   if ( fIsTZEROEnabled ) ConvertTZERO(*esd);
2316   
2317   // Fetch Stack for debuggging if available 
2318   fMChandler=0x0;
2319   if(MCEvent())
2320   {
2321     fMChandler = (AliMCEventHandler*) ((AliAnalysisManager::GetAnalysisManager())->GetMCtruthEventHandler()); 
2322   }
2323   
2324   // loop over events and fill them
2325   // Multiplicity information needed by the header (to be revised!)
2326   Int_t nTracks    = esd->GetNumberOfTracks();
2327   for (Int_t iTrack=0; iTrack<nTracks; ++iTrack) esd->GetTrack(iTrack)->SetESDEvent(esd);
2328
2329   // Update the header
2330
2331   Int_t nV0s      = esd->GetNumberOfV0s();
2332   Int_t nCascades = esd->GetNumberOfCascades();
2333   Int_t nKinks    = esd->GetNumberOfKinks();
2334   Int_t nVertices = nV0s + nCascades /*V0 wihtin cascade already counted*/+ nKinks + 1 /* = prim. vtx*/;
2335   Int_t nPileSPDVertices=1+esd->GetNumberOfPileupVerticesSPD(); // also SPD main vertex
2336   Int_t nPileTrkVertices=esd->GetNumberOfPileupVerticesTracks();
2337   nVertices+=nPileSPDVertices;
2338   nVertices+=nPileTrkVertices;
2339   Int_t nJets     = 0;
2340   Int_t nCaloClus = esd->GetNumberOfCaloClusters();
2341   Int_t nFmdClus  = 0;
2342   Int_t nPmdClus  = esd->GetNumberOfPmdTracks();
2343   Int_t nHmpidRings = 0;  
2344     
2345   AliDebug(1,Form("   NV0=%d  NCASCADES=%d  NKINKS=%d", nV0s, nCascades, nKinks));
2346        
2347   AODEvent()->ResetStd(nTracks, nVertices, nV0s, nCascades, nJets, nCaloClus, nFmdClus, nPmdClus,nHmpidRings);
2348
2349   if (nV0s > 0) 
2350   {
2351     // RefArray to store a mapping between esd V0 number and newly created AOD-Vertex V0
2352     fAODV0VtxRefs = new TRefArray(nV0s);
2353     // RefArray to store the mapping between esd V0 number and newly created AOD-V0
2354     fAODV0Refs = new TRefArray(nV0s); 
2355     // Array to take into account the V0s already added to the AOD (V0 within cascades)
2356     fUsedV0 = new Bool_t[nV0s];
2357     for (Int_t iV0=0; iV0<nV0s; ++iV0) fUsedV0[iV0]=kFALSE;
2358   }
2359   
2360   if (nTracks>0) 
2361   {
2362     // RefArray to store the mapping between esd track number and newly created AOD-Track
2363     
2364     fAODTrackRefs = new TRefArray(nTracks);
2365
2366     // Array to take into account the tracks already added to the AOD    
2367     fUsedTrack = new Bool_t[nTracks];
2368     fUsedTrackCopy = new UInt_t[nTracks];
2369     for (Int_t iTrack=0; iTrack<nTracks; ++iTrack) {
2370       fUsedTrack[iTrack]=kFALSE;
2371       fUsedTrackCopy[iTrack] = 0;
2372     }
2373   }
2374   
2375   // Array to take into account the kinks already added to the AOD
2376   if (nKinks>0) 
2377   {
2378     fUsedKink = new Bool_t[nKinks];
2379     for (Int_t iKink=0; iKink<nKinks; ++iKink) fUsedKink[iKink]=kFALSE;
2380   }
2381     
2382   ConvertPrimaryVertices(*esd);
2383
2384   //setting best TOF PID
2385   AliESDInputHandler* esdH = dynamic_cast<AliESDInputHandler*>(AliAnalysisManager::GetAnalysisManager()->GetInputEventHandler());
2386   if (esdH)
2387       fESDpid = esdH->GetESDpid();
2388
2389   if (fIsPidOwner && fESDpid){
2390     delete fESDpid;
2391     fESDpid = 0;
2392   }
2393   if(!fESDpid)
2394   { //in case of no Tender attached 
2395     fESDpid = new AliESDpid;
2396     fIsPidOwner = kTRUE;
2397   }
2398   
2399   if(!esd->GetTOFHeader())
2400   { //protection in case the pass2 LHC10b,c,d have been processed without tender. 
2401     Float_t t0spread[10];
2402     Float_t intrinsicTOFres=100; //ps ok for LHC10b,c,d pass2!! 
2403     for (Int_t i=0; i<10; i++) t0spread[i] = (TMath::Sqrt(esd->GetSigma2DiamondZ()))/0.03; //0.03 to convert from cm to ps
2404     fESDpid->GetTOFResponse().SetT0resolution(t0spread);
2405     fESDpid->GetTOFResponse().SetTimeResolution(intrinsicTOFres);
2406       //    fESDpid->SetTOFResponse(esd, (AliESDpid::EStartTimeType_t)fTimeZeroType);    
2407     AliTOFHeader tmpTOFHeader(0,t0spread[0],0,NULL,NULL,NULL,intrinsicTOFres,t0spread[0]);   
2408     AODEvent()->SetTOFHeader(&tmpTOFHeader);         // write dummy TOF header in AOD
2409   } else {
2410     AODEvent()->SetTOFHeader(esd->GetTOFHeader());    // write TOF header in AOD
2411   }
2412   
2413   //  if(esd->GetTOFHeader() && fIsPidOwner) fESDpid->SetTOFResponse(esd, (AliESDpid::EStartTimeType_t)fTimeZeroType); //in case of AOD production strating form LHC10e without Tender. 
2414   
2415   if ( fAreCascadesEnabled ) ConvertCascades(*esd);
2416
2417   if ( fAreV0sEnabled ) ConvertV0s(*esd);
2418   
2419   if ( fAreKinksEnabled ) ConvertKinks(*esd);
2420   
2421   if ( fAreTracksEnabled ) ConvertTracks(*esd);
2422   
2423   // Update number of AOD tracks in header at the end of track loop (M.G.)
2424   header->SetRefMultiplicity(fNumberOfTracks);
2425   header->SetRefMultiplicityPos(fNumberOfPositiveTracks);
2426   header->SetRefMultiplicityNeg(fNumberOfTracks - fNumberOfPositiveTracks);
2427
2428   if ( fTPCConstrainedFilterMask ) ConvertTPCOnlyTracks(*esd);
2429   if( fGlobalConstrainedFilterMask) ConvertGlobalConstrainedTracks(*esd);  
2430
2431   if ( fArePmdClustersEnabled ) ConvertPmdClusters(*esd);
2432   
2433   if ( fAreCaloClustersEnabled ) ConvertCaloClusters(*esd);
2434   
2435   if ( fAreEMCALCellsEnabled )ConvertEMCALCells(*esd);
2436   
2437   if ( fArePHOSCellsEnabled )ConvertPHOSCells(*esd);
2438         
2439         if ( fAreEMCALTriggerEnabled )ConvertCaloTrigger(TString("EMCAL"), *esd);
2440
2441         if ( fArePHOSTriggerEnabled )ConvertCaloTrigger(TString("PHOS"), *esd);
2442   
2443   if ( fAreTrackletsEnabled ) ConvertTracklets(*esd);
2444   if ( fIsZDCEnabled ) ConvertZDC(*esd);
2445   
2446   if(fIsHMPIDEnabled) nHmpidRings = ConvertHMPID(*esd); 
2447
2448   if (fIsTRDEnabled) ConvertTRD(*esd);
2449
2450   delete fAODTrackRefs; fAODTrackRefs=0x0;
2451   delete fAODV0VtxRefs; fAODV0VtxRefs=0x0;
2452   delete fAODV0Refs; fAODV0Refs=0x0;
2453   
2454   delete[] fUsedTrack; fUsedTrack=0x0;
2455   delete[] fUsedTrackCopy; fUsedTrackCopy=0x0;
2456   delete[] fUsedV0; fUsedV0=0x0;
2457   delete[] fUsedKink; fUsedKink=0x0;
2458
2459   if ( fIsPidOwner){
2460     delete fESDpid;
2461     fESDpid = 0x0;
2462   }
2463
2464
2465 }
2466
2467
2468 //______________________________________________________________________________
2469 void AliAnalysisTaskESDfilter::SetAODPID(AliESDtrack *esdtrack, AliAODTrack *aodtrack, AliAODPid *detpid)
2470 {
2471   //
2472   // Setter for the raw PID detector signals
2473   //
2474
2475   // Save PID object for candidate electrons
2476     Bool_t pidSave = kFALSE;
2477     if (fTrackFilter) {
2478         Bool_t selectInfo = fTrackFilter->IsSelected((char*) "Electrons");
2479         if (selectInfo)  pidSave = kTRUE;
2480     }
2481
2482
2483     // Tracks passing pt cut 
2484     if(esdtrack->Pt()>fHighPthreshold) {
2485         pidSave = kTRUE;
2486     } else {
2487         if(fPtshape){
2488             if(esdtrack->Pt()> fPtshape->GetXmin()){
2489                 Double_t y = fPtshape->Eval(esdtrack->Pt())/fPtshape->Eval(fHighPthreshold);
2490                 if(gRandom->Rndm(0)<1./y){
2491                     pidSave = kTRUE;
2492                 }//end rndm
2493             }//end if p < pmin
2494         }//end if p function
2495     }// end else
2496
2497     if (pidSave) {
2498       if(!aodtrack->GetDetPid()){// prevent memory leak when calling SetAODPID twice for the same track
2499         detpid = new AliAODPid();
2500         SetDetectorRawSignals(detpid,esdtrack);
2501         aodtrack->SetDetPID(detpid);
2502       }
2503     }
2504 }
2505
2506 //______________________________________________________________________________
2507 void AliAnalysisTaskESDfilter::SetDetectorRawSignals(AliAODPid *aodpid, AliESDtrack *track)
2508 {
2509 //
2510 //assignment of the detector signals (AliXXXesdPID inspired)
2511 //
2512  if(!track){
2513  AliInfo("no ESD track found. .....exiting");
2514  return;
2515  }
2516  // TPC momentum
2517  aodpid->SetTPCmomentum(track->GetTPCmomentum());
2518  aodpid->SetTPCTgl(track->GetTPCTgl());
2519 //  const AliExternalTrackParam *in=track->GetInnerParam();
2520 //  if (in) {
2521 //    aodpid->SetTPCmomentum(in->GetP());
2522 //  }else{
2523 //    aodpid->SetTPCmomentum(-1.);
2524 //  }
2525
2526
2527  aodpid->SetITSsignal(track->GetITSsignal());
2528  Double_t itsdedx[4]; // dE/dx samples for individual ITS layers
2529  track->GetITSdEdxSamples(itsdedx);
2530  aodpid->SetITSdEdxSamples(itsdedx);
2531
2532  aodpid->SetTPCsignal(track->GetTPCsignal());
2533  aodpid->SetTPCsignalN(track->GetTPCsignalN());
2534  if(track->GetTPCdEdxInfo()) aodpid->SetTPCdEdxInfo(track->GetTPCdEdxInfo());
2535
2536  //n TRD planes = 6
2537  Int_t nslices = track->GetNumberOfTRDslices()*6;
2538  TArrayD trdslices(nslices);
2539  for(Int_t iSl =0; iSl < track->GetNumberOfTRDslices(); iSl++) {
2540    for(Int_t iPl =0; iPl<6; iPl++) trdslices[iPl*track->GetNumberOfTRDslices()+iSl] = track->GetTRDslice(iPl,iSl);
2541  }
2542  
2543 //TRD momentum
2544  for(Int_t iPl=0;iPl<6;iPl++){
2545    Double_t trdmom=track->GetTRDmomentum(iPl);
2546    aodpid->SetTRDmomentum(iPl,trdmom);
2547  }
2548
2549  aodpid->SetTRDslices(track->GetNumberOfTRDslices()*6,trdslices.GetArray());
2550  aodpid->SetTRDsignal(track->GetTRDsignal());
2551
2552  //TRD clusters and tracklets
2553  aodpid->SetTRDncls(track->GetTRDncls());
2554  aodpid->SetTRDntrackletsPID(track->GetTRDntrackletsPID());
2555  
2556  aodpid->SetTRDChi2(track->GetTRDchi2());
2557
2558  //TOF PID  
2559  Double_t times[AliPID::kSPECIES]; track->GetIntegratedTimes(times);
2560  aodpid->SetIntegratedTimes(times);
2561
2562    //  Float_t tzeroTrack = fESDpid->GetTOFResponse().GetStartTime(track->P());
2563    //  aodpid->SetTOFsignal(track->GetTOFsignal()-tzeroTrack);
2564    aodpid->SetTOFsignal(track->GetTOFsignal());
2565   
2566   Double_t tofRes[5];
2567   for (Int_t iMass=0; iMass<5; iMass++){
2568     //    tofRes[iMass]=(Double_t)fESDpid->GetTOFResponse().GetExpectedSigma(track->P(), times[iMass], AliPID::ParticleMass(iMass));
2569     tofRes[iMass]=0; //backward compatibility
2570   }
2571   aodpid->SetTOFpidResolution(tofRes);
2572
2573 //  aodpid->SetHMPIDsignal(0); // set to zero for compression but it will be removed later
2574
2575 }
2576
2577 Double_t  AliAnalysisTaskESDfilter::Chi2perNDF(AliESDtrack* track)
2578 {
2579     // Calculate chi2 per ndf for track
2580     Int_t  nClustersTPC = track->GetTPCNcls();
2581
2582     if ( nClustersTPC > 5) {
2583        return (track->GetTPCchi2()/Float_t(nClustersTPC - 5));
2584     } else {
2585        return (-1.);
2586     }
2587  }
2588
2589
2590 //______________________________________________________________________________
2591 void AliAnalysisTaskESDfilter::Terminate(Option_t */*option*/)
2592 {
2593 // Terminate analysis
2594 //
2595     if (fDebug > 1) printf("AnalysisESDfilter: Terminate() \n");
2596 }
2597
2598 //______________________________________________________________________________
2599 void  AliAnalysisTaskESDfilter::PrintMCInfo(AliStack *pStack,Int_t label){
2600 // Print MC info
2601   if(!pStack)return;
2602   label = TMath::Abs(label);
2603   TParticle *part = pStack->Particle(label);
2604   Printf("########################");
2605   Printf("%s:%d %d UniqueID %d PDG %d P %3.3f",(char*)__FILE__,__LINE__,label,part->GetUniqueID(),part->GetPdgCode(),part->P());
2606   part->Print();
2607   TParticle* mother = part;
2608   Int_t imo = part->GetFirstMother();
2609   Int_t nprim = pStack->GetNprimary();
2610   //  while((imo >= nprim) && (mother->GetUniqueID() == 4)) {
2611   while((imo >= nprim)) {
2612     mother =  pStack->Particle(imo);
2613     Printf("Mother %s:%d Label %d UniqueID %d PDG %d P %3.3f",(char*)__FILE__,__LINE__,imo,mother->GetUniqueID(),mother->GetPdgCode(),mother->P());
2614     mother->Print();
2615     imo =  mother->GetFirstMother();
2616   }
2617   Printf("########################");
2618 }
2619
2620 //______________________________________________________________________________
2621 void  AliAnalysisTaskESDfilter::CopyCaloProps(AliESDtrack *tre, AliAODTrack *tra) {
2622   //Copy calo properties from ESD track to AOD track
2623
2624   tra->SetTrackPhiEtaPtOnEMCal(tre->GetTrackPhiOnEMCal(),tre->GetTrackEtaOnEMCal(),tre->GetTrackPtOnEMCal());
2625   if(tre->IsEMCAL()) tra->SetEMCALcluster(tre->GetEMCALcluster());
2626   if(tre->IsPHOS())  tra->SetPHOScluster(tre->GetPHOScluster());
2627
2628 }
2629
2630 //______________________________________________________________________________
2631
2632