9d76fcb4cde19fcb8ed7246fefe93070b0d6c5d4
[u/mrichter/AliRoot.git] / ANALYSIS / AliAnalysisTaskESDfilter.cxx
1 /**************************************************************************\r
2  * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *\r
3  *                                                                        *\r
4  * Author: The ALICE Off-line Project.                                    *\r
5  * Contributors are mentioned in the code where appropriate.              *\r
6  *                                                                        *\r
7  * Permission to use, copy, modify and distribute this software and its   *\r
8  * documentation strictly for non-commercial purposes is hereby granted   *\r
9  * without fee, provided that the above copyright notice appears in all   *\r
10  * copies and that both the copyright notice and this permission notice   *\r
11  * appear in the supporting documentation. The authors make no claims     *\r
12  * about the suitability of this software for any purpose. It is          *\r
13  * provided "as is" without express or implied warranty.                  *\r
14  **************************************************************************/\r
15 \r
16 /* $Id: AliAnalysisTaskESDfilter.cxx 24535 2008-03-16 22:43:30Z fca $ */\r
17  \r
18 #include <TChain.h>\r
19 #include <TTree.h>\r
20 #include <TList.h>\r
21 #include <TArrayI.h>\r
22 #include <TRandom.h>\r
23 #include <TParticle.h>\r
24 #include <TFile.h>\r
25 \r
26 #include "AliAnalysisTaskESDfilter.h"\r
27 #include "AliAnalysisManager.h"\r
28 #include "AliESDEvent.h"\r
29 #include "AliESDRun.h"\r
30 #include "AliStack.h"\r
31 #include "AliAODEvent.h"\r
32 #include "AliMCEvent.h"\r
33 #include "AliMCEventHandler.h"\r
34 #include "AliESDInputHandler.h"\r
35 #include "AliAODHandler.h"\r
36 #include "AliAODMCParticle.h"\r
37 #include "AliAnalysisFilter.h"\r
38 #include "AliESDMuonTrack.h"\r
39 #include "AliESDVertex.h"\r
40 #include "AliCentrality.h"\r
41 #include "AliESDv0.h"\r
42 #include "AliESDkink.h"\r
43 #include "AliESDcascade.h"\r
44 #include "AliESDPmdTrack.h"\r
45 #include "AliESDCaloCluster.h"\r
46 #include "AliESDCaloCells.h"\r
47 #include "AliMultiplicity.h"\r
48 #include "AliLog.h"\r
49 #include "AliTrackerBase.h"\r
50 #include "AliESDtrackCuts.h"\r
51 #include "AliPID.h"\r
52  \r
53 ClassImp(AliAnalysisTaskESDfilter)\r
54 \r
55 ////////////////////////////////////////////////////////////////////////\r
56 \r
57 AliAnalysisTaskESDfilter::AliAnalysisTaskESDfilter():\r
58     AliAnalysisTaskSE(),\r
59     fTrackFilter(0x0),\r
60     fKinkFilter(0x0),\r
61     fV0Filter(0x0),\r
62     fCascadeFilter(0x0),\r
63     fTPCOnlyFilterMask(0),\r
64     fHighPthreshold(0),\r
65     fPtshape(0x0),\r
66     fEnableFillAOD(kTRUE)\r
67 {\r
68   // Default constructor\r
69 }\r
70 \r
71 AliAnalysisTaskESDfilter::AliAnalysisTaskESDfilter(const char* name):\r
72     AliAnalysisTaskSE(name),\r
73     fTrackFilter(0x0),\r
74     fKinkFilter(0x0),\r
75     fV0Filter(0x0),\r
76     fCascadeFilter(0x0),\r
77     fTPCOnlyFilterMask(0),\r
78     fHighPthreshold(0),\r
79     fPtshape(0x0),\r
80     fEnableFillAOD(kTRUE)\r
81 {\r
82   // Constructor\r
83 }\r
84 \r
85 void AliAnalysisTaskESDfilter::UserCreateOutputObjects()\r
86 {\r
87   //\r
88   // Create Output Objects conenct filter to outputtree\r
89   // \r
90   if(OutputTree()){\r
91     OutputTree()->GetUserInfo()->Add(fTrackFilter);\r
92   }\r
93   else{\r
94     AliError("No OutputTree() for adding the track filter");\r
95   }\r
96 }\r
97 \r
98 void AliAnalysisTaskESDfilter::Init()\r
99 {\r
100     // Initialization\r
101     if (fDebug > 1) AliInfo("Init() \n");\r
102     // Call configuration file\r
103 }\r
104 \r
105 \r
106 void AliAnalysisTaskESDfilter::UserExec(Option_t */*option*/)\r
107 {\r
108 // Execute analysis for current event\r
109 //\r
110                                             \r
111   Long64_t ientry = Entry();\r
112   \r
113   if (fDebug > 0) {\r
114       printf("Filter: Analysing event # %5d\n", (Int_t) ientry);\r
115       if (fHighPthreshold == 0) AliInfo("detector PID signals are stored in each track");\r
116       if (!fPtshape) AliInfo("detector PID signals are not stored below the pt threshold");\r
117   }\r
118   // Filters must explicitely enable AOD filling in their UserExec (AG)\r
119   if (!AliAnalysisManager::GetAnalysisManager()->GetOutputEventHandler()) AliFatal("Cannot run ESD filter without an output event handler");\r
120   if(fEnableFillAOD) AliAnalysisManager::GetAnalysisManager()->GetOutputEventHandler()->SetFillAOD(kTRUE);\r
121 \r
122   ConvertESDtoAOD();\r
123 }\r
124 \r
125 void AliAnalysisTaskESDfilter::ConvertESDtoAOD() {\r
126     // ESD Filter analysis task executed for each event\r
127 \r
128     AliESDEvent* esd = dynamic_cast<AliESDEvent*>(InputEvent());\r
129     if(!esd)return;\r
130     AliESD*      old = esd->GetAliESDOld();\r
131 \r
132     // Fetch Stack for debuggging if available \r
133     AliMCEventHandler *mcH = 0;\r
134     if(MCEvent()){\r
135       mcH = (AliMCEventHandler*) ((AliAnalysisManager::GetAnalysisManager())->GetMCtruthEventHandler()); \r
136     }\r
137     // set arrays and pointers\r
138     Float_t posF[3];\r
139     Double_t pos[3];\r
140     Double_t p[3];\r
141     Double_t momPos[3]; \r
142     Double_t momNeg[3];\r
143     Double_t momBach[3];\r
144     Double_t momPosAtV0vtx[3];\r
145     Double_t momNegAtV0vtx[3];\r
146     Double_t momBachAtCascadeVtx[3];\r
147     Double_t covVtx[6];\r
148     Double_t covTr[21];\r
149     Double_t pid[10];\r
150 \r
151     for (Int_t i = 0; i < 6; i++)  covVtx[i] = 0.;\r
152     for (Int_t i = 0; i < 21; i++) covTr [i] = 0.;\r
153 \r
154     \r
155     // loop over events and fill them\r
156     // Multiplicity information needed by the header (to be revised!)\r
157     Int_t nTracks    = esd->GetNumberOfTracks();\r
158     for (Int_t iTrack=0; iTrack<nTracks; ++iTrack) esd->GetTrack(iTrack)->SetESDEvent(esd);\r
159 \r
160     //    if (fDebug > 0) printf("-------------------Bo: Number of ESD tracks %d \n",nTracks);\r
161 \r
162     Int_t nPosTracks = 0;\r
163     \r
164     // Update the header\r
165 \r
166     AliAODHeader* header = AODEvent()->GetHeader();\r
167     \r
168     header->SetRunNumber(esd->GetRunNumber());\r
169     header->SetOfflineTrigger(fInputHandler->IsEventSelected()); // propagate the decision of the physics selection\r
170     TTree* tree = fInputHandler->GetTree();\r
171     if (tree) {\r
172         TFile* file = tree->GetCurrentFile();\r
173         if (file) header->SetESDFileName(file->GetName());\r
174     }\r
175     \r
176     if (old) {\r
177         header->SetBunchCrossNumber(0);\r
178         header->SetOrbitNumber(0);\r
179         header->SetPeriodNumber(0);\r
180         header->SetEventType(0);\r
181         header->SetMuonMagFieldScale(-999.);\r
182         header->SetCentrality(0);       \r
183     } else {\r
184         header->SetBunchCrossNumber(esd->GetBunchCrossNumber());\r
185         header->SetOrbitNumber(esd->GetOrbitNumber());\r
186         header->SetPeriodNumber(esd->GetPeriodNumber());\r
187         header->SetEventType(esd->GetEventType());\r
188 \r
189         header->SetEventNumberESDFile(esd->GetHeader()->GetEventNumberInFile());\r
190         if(esd->GetCentrality()){\r
191           header->SetCentrality(new AliCentrality(*(esd->GetCentrality())));\r
192         }\r
193         else{\r
194           header->SetCentrality(0);\r
195         }\r
196     }\r
197     \r
198     // Trigger\r
199     header->SetFiredTriggerClasses(esd->GetFiredTriggerClasses());\r
200     header->SetTriggerMask(esd->GetTriggerMask()); \r
201     header->SetTriggerCluster(esd->GetTriggerCluster());\r
202     header->SetL0TriggerInputs(esd->GetHeader()->GetL0TriggerInputs());    \r
203     header->SetL1TriggerInputs(esd->GetHeader()->GetL1TriggerInputs());    \r
204     header->SetL2TriggerInputs(esd->GetHeader()->GetL2TriggerInputs());    \r
205 \r
206     header->SetMagneticField(esd->GetMagneticField());\r
207     header->SetMuonMagFieldScale(esd->GetCurrentDip()/6000.);\r
208     header->SetZDCN1Energy(esd->GetZDCN1Energy());\r
209     header->SetZDCP1Energy(esd->GetZDCP1Energy());\r
210     header->SetZDCN2Energy(esd->GetZDCN2Energy());\r
211     header->SetZDCP2Energy(esd->GetZDCP2Energy());\r
212     header->SetZDCEMEnergy(esd->GetZDCEMEnergy(0),esd->GetZDCEMEnergy(1));\r
213 \r
214     \r
215     Float_t diamxy[2]={esd->GetDiamondX(),esd->GetDiamondY()};\r
216     Float_t diamcov[3]; esd->GetDiamondCovXY(diamcov);\r
217     header->SetDiamond(diamxy,diamcov);\r
218     header->SetDiamondZ(esd->GetDiamondZ(),esd->GetSigma2DiamondZ());\r
219 //\r
220 //\r
221     Int_t nV0s      = esd->GetNumberOfV0s();\r
222     Int_t nCascades = esd->GetNumberOfCascades();\r
223     Int_t nKinks    = esd->GetNumberOfKinks();\r
224     Int_t nVertices = nV0s + nCascades /*V0 wihtin cascade already counted*/+ nKinks + 1 /* = prim. vtx*/;\r
225     Int_t nPileSPDVertices=1+esd->GetNumberOfPileupVerticesSPD(); // also SPD main vertex\r
226     Int_t nPileTrkVertices=esd->GetNumberOfPileupVerticesTracks();\r
227     nVertices+=nPileSPDVertices;\r
228     nVertices+=nPileTrkVertices;\r
229     Int_t nJets     = 0;\r
230     Int_t nCaloClus = esd->GetNumberOfCaloClusters();\r
231     Int_t nFmdClus  = 0;\r
232     Int_t nPmdClus  = esd->GetNumberOfPmdTracks();\r
233     \r
234     if (fDebug > 0) \r
235         printf("   NV0=%d  NCASCADES=%d  NKINKS=%d\n", nV0s, nCascades, nKinks);\r
236        \r
237     AODEvent()->ResetStd(nTracks, nVertices, nV0s, nCascades, nJets, nCaloClus, nFmdClus, nPmdClus);\r
238 \r
239 \r
240     AliAODTrack   *aodTrack       = 0x0;\r
241     AliAODPid     *detpid         = 0x0;\r
242     //    Double_t      timezero        = 0;   //not needed anymore \r
243     AliAODVertex  *vV0FromCascade = 0x0;\r
244     AliAODv0      *aodV0          = 0x0;\r
245     AliAODcascade *aodCascade     = 0x0;\r
246     \r
247     // RefArray to store the mapping between esd track number and newly created AOD-Track\r
248     TRefArray   *aodTrackRefs = NULL;\r
249     if (nTracks > 0) aodTrackRefs = new TRefArray(nTracks);\r
250 \r
251     // RefArray to store a mapping between esd V0 number and newly created AOD-Vertex V0\r
252     TRefArray   *aodV0VtxRefs = NULL;\r
253     if (nV0s > 0) aodV0VtxRefs = new TRefArray(nV0s);\r
254 \r
255     // RefArray to store the mapping between esd V0 number and newly created AOD-V0\r
256     TRefArray   *aodV0Refs = NULL;\r
257     if (nV0s > 0) aodV0Refs = new TRefArray(nV0s);\r
258 \r
259     \r
260 \r
261 \r
262 \r
263     // Array to take into account the tracks already added to the AOD\r
264     Bool_t * usedTrack = NULL;\r
265     if (nTracks>0) {\r
266         usedTrack = new Bool_t[nTracks];\r
267         for (Int_t iTrack=0; iTrack<nTracks; ++iTrack) usedTrack[iTrack]=kFALSE;\r
268     }\r
269     // Array to take into account the V0s already added to the AOD (V0 within cascades)\r
270     Bool_t * usedV0 = NULL;\r
271     if (nV0s>0) {\r
272         usedV0 = new Bool_t[nV0s];\r
273         for (Int_t iV0=0; iV0<nV0s; ++iV0) usedV0[iV0]=kFALSE;\r
274     }\r
275     // Array to take into account the kinks already added to the AOD\r
276     Bool_t * usedKink = NULL;\r
277     if (nKinks>0) {\r
278         usedKink = new Bool_t[nKinks];\r
279         for (Int_t iKink=0; iKink<nKinks; ++iKink) usedKink[iKink]=kFALSE;\r
280     }\r
281     \r
282     // Access to the AOD container of vertices\r
283     TClonesArray &vertices = *(AODEvent()->GetVertices());\r
284     Int_t jVertices=0;\r
285     \r
286     // Access to the AOD container of tracks\r
287     TClonesArray &tracks = *(AODEvent()->GetTracks());\r
288     Int_t jTracks=0; \r
289     \r
290     // Access to the AOD container of Cascades\r
291     TClonesArray &cascades = *(AODEvent()->GetCascades());\r
292     Int_t jCascades=0;\r
293 \r
294     // Access to the AOD container of V0s\r
295     TClonesArray &v0s = *(AODEvent()->GetV0s());\r
296     Int_t jV0s=0;\r
297     \r
298     // Add primary vertex. The primary tracks will be defined\r
299     // after the loops on the composite objects (V0, cascades, kinks)\r
300     const AliESDVertex *vtx = esd->GetPrimaryVertex();\r
301     \r
302     vtx->GetXYZ(pos); // position\r
303     vtx->GetCovMatrix(covVtx); //covariance matrix\r
304     \r
305     AliAODVertex * primary = new(vertices[jVertices++])\r
306         AliAODVertex(pos, covVtx, vtx->GetChi2toNDF(), NULL, -1, AliAODVertex::kPrimary);\r
307     primary->SetName(vtx->GetName());\r
308     primary->SetTitle(vtx->GetTitle());\r
309 \r
310     TString vtitle = vtx->GetTitle();\r
311     if (!vtitle.Contains("VertexerTracks")) \r
312       primary->SetNContributors(vtx->GetNContributors());\r
313 \r
314     if (fDebug > 0) primary->Print();\r
315 \r
316 \r
317     // Add SPD "main" vertex \r
318     const AliESDVertex *vtxS = esd->GetPrimaryVertexSPD();\r
319     vtxS->GetXYZ(pos); // position\r
320     vtxS->GetCovMatrix(covVtx); //covariance matrix\r
321     AliAODVertex * mVSPD = new(vertices[jVertices++])\r
322       AliAODVertex(pos, covVtx, vtxS->GetChi2toNDF(), NULL, -1, AliAODVertex::kMainSPD);\r
323     mVSPD->SetName(vtxS->GetName());\r
324     mVSPD->SetTitle(vtxS->GetTitle());\r
325     mVSPD->SetNContributors(vtxS->GetNContributors()); \r
326 \r
327     // Add SPD pileup vertices\r
328     for(Int_t iV=0; iV<nPileSPDVertices-1; iV++){\r
329       const AliESDVertex *vtxP = esd->GetPileupVertexSPD(iV);\r
330       vtxP->GetXYZ(pos); // position\r
331       vtxP->GetCovMatrix(covVtx); //covariance matrix\r
332       AliAODVertex * pVSPD =  new(vertices[jVertices++])\r
333         AliAODVertex(pos, covVtx, vtxP->GetChi2toNDF(), NULL, -1, AliAODVertex::kPileupSPD);\r
334       pVSPD->SetName(vtxP->GetName());\r
335       pVSPD->SetTitle(vtxP->GetTitle());\r
336       pVSPD->SetNContributors(vtxP->GetNContributors()); \r
337     }\r
338 \r
339     // Add TRK pileup vertices\r
340     for(Int_t iV=0; iV<nPileTrkVertices; iV++){\r
341       const AliESDVertex *vtxP = esd->GetPileupVertexTracks(iV);\r
342       vtxP->GetXYZ(pos); // position\r
343       vtxP->GetCovMatrix(covVtx); //covariance matrix\r
344       AliAODVertex * pVTRK = new(vertices[jVertices++])\r
345         AliAODVertex(pos, covVtx, vtxP->GetChi2toNDF(), NULL, -1, AliAODVertex::kPileupTracks);\r
346       pVTRK->SetName(vtxP->GetName());\r
347       pVTRK->SetTitle(vtxP->GetTitle());\r
348       pVTRK->SetNContributors(vtxP->GetNContributors());\r
349     }\r
350 \r
351     // Create vertices starting from the most complex objects\r
352     Double_t chi2 = 0.;\r
353 \r
354 \r
355     //setting best TOF PID\r
356     AliESDpid *esdPid = new AliESDpid;\r
357     \r
358     if(!esd->GetTOFHeader()){\r
359       Float_t *t0spread= new Float_t[10];\r
360       Float_t intrinsicTOFres=100; //ps ok for LHC10b,c,d pass2!! \r
361       for (Int_t i=0; i<10; i++) t0spread[i] = (TMath::Sqrt(esd->GetSigma2DiamondZ()))/0.03; //0.03 to convert from cm to ps\r
362       \r
363       esdPid->GetTOFResponse().SetT0resolution(t0spread);\r
364       esdPid->GetTOFResponse().SetTimeResolution(intrinsicTOFres);\r
365       delete[] t0spread;\r
366       t0spread=0x0;\r
367     }\r
368     else{\r
369       esdPid->SetTOFResponse(esd, AliESDpid::kBest_T0); //TOF_T0 uses the T0 computed with TOF, kBest_T0 uses the best information available \r
370     }\r
371     esdPid->MakePID(esd, kFALSE);\r
372     \r
373     \r
374     // Cascades (Modified by A.Maire - February 2009)\r
375     for (Int_t nCascade = 0; nCascade < nCascades; ++nCascade) {\r
376 \r
377         if (fDebug > 1) \r
378         printf("\n ******** Cascade number : %d/%d *********\n", nCascade, nCascades);\r
379 \r
380         // 0- Preparation\r
381         //\r
382         AliESDcascade *esdCascade = esd->GetCascade(nCascade);\r
383                 Int_t  idxPosFromV0Dghter  = esdCascade->GetPindex();\r
384                 Int_t  idxNegFromV0Dghter  = esdCascade->GetNindex();\r
385                 Int_t  idxBachFromCascade  = esdCascade->GetBindex();\r
386         \r
387         AliESDtrack  *esdCascadePos  = esd->GetTrack( idxPosFromV0Dghter);\r
388         AliESDtrack  *esdCascadeNeg  = esd->GetTrack( idxNegFromV0Dghter);\r
389         AliESDtrack  *esdCascadeBach = esd->GetTrack( idxBachFromCascade);\r
390 \r
391         // Identification of the V0 within the esdCascade (via both daughter track indices)\r
392         AliESDv0 * currentV0   = 0x0;\r
393         Int_t      idxV0FromCascade = -1;\r
394         \r
395         for (Int_t iV0=0; iV0<nV0s; ++iV0) {\r
396         \r
397                          currentV0 = esd->GetV0(iV0);\r
398                 Int_t posCurrentV0 = currentV0->GetPindex();\r
399                 Int_t negCurrentV0 = currentV0->GetNindex();\r
400         \r
401                 if (posCurrentV0==idxPosFromV0Dghter && negCurrentV0==idxNegFromV0Dghter) {\r
402                 idxV0FromCascade = iV0;\r
403                 break;\r
404                 }\r
405         }\r
406 \r
407         if(idxV0FromCascade < 0){\r
408                 printf("Cascade - no matching for the V0 (index V0 = -1) ! Skip ... \n");\r
409                 continue;\r
410         }// a priori, useless check, but safer ... in case of pb with tracks "out of bounds"\r
411 \r
412         if (fDebug > 1) \r
413                 printf("Cascade %d - V0fromCascade ind : %d/%d\n", nCascade, idxV0FromCascade, nV0s);\r
414 \r
415         AliESDv0 *esdV0FromCascade   = esd->GetV0(idxV0FromCascade);\r
416         \r
417 \r
418         // 1 - Cascade selection \r
419         \r
420         //      AliESDVertex *esdPrimVtx = new AliESDVertex(*(esd->GetPrimaryVertex()));\r
421         //      TList cascadeObjects;\r
422         //      cascadeObjects.AddAt(esdV0FromCascade, 0);\r
423         //      cascadeObjects.AddAt(esdCascadePos,    1);\r
424         //      cascadeObjects.AddAt(esdCascadeNeg,    2);\r
425         //      cascadeObjects.AddAt(esdCascade,       3);\r
426         //      cascadeObjects.AddAt(esdCascadeBach,   4);\r
427         //      cascadeObjects.AddAt(esdPrimVtx,       5);\r
428         // \r
429         //      UInt_t selectCascade = 0;\r
430         //      if (fCascadeFilter) {\r
431         //        // selectCascade = fCascadeFilter->IsSelected(&cascadeObjects); \r
432         //              // FIXME AliESDCascadeCuts to be implemented ...\r
433         // \r
434         //              // Here we may encounter a moot point at the V0 level \r
435         //              // between the cascade selections and the V0 ones :\r
436         //              // the V0 selected along with the cascade (secondary V0) may \r
437         //              // usually be removed from the dedicated V0 selections (prim V0) ...\r
438         //              // -> To be discussed !\r
439         // \r
440         //        // this is a little awkward but otherwise the \r
441         //        // list wants to access the pointer (delete it) \r
442         //        // again when going out of scope\r
443         //        delete cascadeObjects.RemoveAt(5); // esdPrimVtx created via copy construct\r
444         //        esdPrimVtx = 0;\r
445         //        if (!selectCascade) \r
446         //          continue;\r
447         //      }\r
448         //      else{\r
449         //        delete cascadeObjects.RemoveAt(5); // esdPrimVtx created via copy construct\r
450         //        esdPrimVtx = 0;\r
451         //      }\r
452 \r
453         // 2 - Add the cascade vertex\r
454         \r
455         esdCascade->GetXYZcascade(pos[0], pos[1], pos[2]);\r
456         esdCascade->GetPosCovXi(covVtx);\r
457         chi2 = esdCascade->GetChi2Xi(); \r
458         \r
459         AliAODVertex *vCascade = new(vertices[jVertices++]) AliAODVertex( pos,\r
460                                                                           covVtx,\r
461                                                                           chi2, // FIXME = Chi2/NDF will be needed\r
462                                                                           primary,\r
463                                                                           nCascade, // id\r
464                                                                           AliAODVertex::kCascade);\r
465         primary->AddDaughter(vCascade);\r
466 \r
467         if (fDebug > 2) {\r
468                 printf("---- Cascade / Cascade Vertex (AOD) : \n");\r
469                 vCascade->Print();\r
470         }\r
471 \r
472 \r
473         // 3 - Add the bachelor track from the cascade\r
474         \r
475         if (!usedTrack[idxBachFromCascade]) {\r
476 \r
477         esdCascadeBach->GetPxPyPz(momBach);\r
478         esdCascadeBach->GetXYZ(pos);\r
479         esdCascadeBach->GetCovarianceXYZPxPyPz(covTr);\r
480         esdCascadeBach->GetESDpid(pid);\r
481 \r
482             usedTrack[idxBachFromCascade] = kTRUE;\r
483             UInt_t selectInfo = 0;\r
484             if (fTrackFilter) selectInfo = fTrackFilter->IsSelected(esdCascadeBach);\r
485             if(mcH)mcH->SelectParticle(esdCascadeBach->GetLabel());\r
486             aodTrack = new(tracks[jTracks++]) AliAODTrack(esdCascadeBach->GetID(),\r
487                                                           esdCascadeBach->GetLabel(), \r
488                                                           momBach, \r
489                                                           kTRUE,\r
490                                                           pos,\r
491                                                           kFALSE, // Why kFALSE for "isDCA" ? FIXME\r
492                                                           covTr, \r
493                                                           (Short_t)esdCascadeBach->GetSign(),\r
494                                                           esdCascadeBach->GetITSClusterMap(), \r
495                                                           pid,\r
496                                                           vCascade,\r
497                                                           kTRUE,  // usedForVtxFit = kFALSE ? FIXME\r
498                                                           vtx->UsesTrack(esdCascadeBach->GetID()),\r
499                                                           AliAODTrack::kSecondary,\r
500                                                           selectInfo);\r
501             aodTrack->SetTPCClusterMap(esdCascadeBach->GetTPCClusterMap());\r
502             aodTrack->SetTPCSharedMap (esdCascadeBach->GetTPCSharedMap());\r
503             aodTrack->SetChi2perNDF(Chi2perNDF(esdCascadeBach));\r
504             aodTrackRefs->AddAt(aodTrack,idxBachFromCascade);\r
505             \r
506             if (esdCascadeBach->GetSign() > 0) nPosTracks++;\r
507             aodTrack->ConvertAliPIDtoAODPID();\r
508             aodTrack->SetFlags(esdCascadeBach->GetStatus());\r
509             SetAODPID(esdCascadeBach,aodTrack,detpid,esd->GetMagneticField(), esdPid);\r
510         }\r
511         else {\r
512             aodTrack = dynamic_cast<AliAODTrack*>( aodTrackRefs->At(idxBachFromCascade) );\r
513         }\r
514 \r
515         vCascade->AddDaughter(aodTrack);\r
516 \r
517         if (fDebug > 4) {\r
518                 printf("---- Cascade / bach dghter : \n");\r
519                 aodTrack->Print();\r
520         }\r
521         \r
522 \r
523         // 4 - Add the V0 from the cascade. \r
524         // = V0vtx + both pos and neg daughter tracks + the aodV0 itself\r
525         //\r
526 \r
527         if ( !usedV0[idxV0FromCascade] ) {\r
528         // 4.A - if VO structure hasn't been created yet\r
529                 \r
530                 // 4.A.1 - Create the V0 vertex of the cascade\r
531         \r
532                 esdV0FromCascade->GetXYZ(pos[0], pos[1], pos[2]);\r
533                 esdV0FromCascade->GetPosCov(covVtx);\r
534                 chi2 = esdV0FromCascade->GetChi2V0();  // = chi2/NDF since NDF = 2*2-3 ?\r
535                         \r
536                 vV0FromCascade = new(vertices[jVertices++]) AliAODVertex(pos,\r
537                                                                         covVtx,\r
538                                                                         chi2,\r
539                                                                         vCascade,\r
540                                                                         idxV0FromCascade, //id of ESDv0\r
541                                                                         AliAODVertex::kV0);\r
542                 // Note:\r
543                 //    one V0 can be used by several cascades.\r
544                 // So, one AOD V0 vtx can have several parent vtx.\r
545                 // This is not directly allowed by AliAODvertex.\r
546                 // Setting the parent vtx (here = param "vCascade") doesn't lead to a crash\r
547                 // but to a problem of consistency within AODEvent.\r
548                 // -> See below paragraph 4.B, for the proposed treatment of such a case.\r
549 \r
550                 // Add the vV0FromCascade to the aodVOVtxRefs\r
551                 aodV0VtxRefs->AddAt(vV0FromCascade,idxV0FromCascade);\r
552                 \r
553                 \r
554                 // 4.A.2 - Add the positive tracks from the V0\r
555                 \r
556                 esdCascadePos->GetPxPyPz(momPos);\r
557                 esdCascadePos->GetXYZ(pos);\r
558                 esdCascadePos->GetCovarianceXYZPxPyPz(covTr);\r
559                 esdCascadePos->GetESDpid(pid);\r
560                 \r
561         \r
562                 if (!usedTrack[idxPosFromV0Dghter]) {\r
563                 usedTrack[idxPosFromV0Dghter] = kTRUE;\r
564         \r
565                 UInt_t selectInfo = 0;\r
566                 if (fTrackFilter) selectInfo = fTrackFilter->IsSelected(esdCascadePos);\r
567                 if(mcH) mcH->SelectParticle(esdCascadePos->GetLabel());\r
568                 aodTrack = new(tracks[jTracks++]) AliAODTrack(  esdCascadePos->GetID(),\r
569                                                                 esdCascadePos->GetLabel(), \r
570                                                                 momPos, \r
571                                                                 kTRUE,\r
572                                                                 pos,\r
573                                                                 kFALSE, // Why kFALSE for "isDCA" ? FIXME\r
574                                                                 covTr, \r
575                                                                 (Short_t)esdCascadePos->GetSign(),\r
576                                                                 esdCascadePos->GetITSClusterMap(), \r
577                                                                 pid,\r
578                                                                 vV0FromCascade,\r
579                                                                 kTRUE,  // usedForVtxFit = kFALSE ? FIXME\r
580                                                                 vtx->UsesTrack(esdCascadePos->GetID()),\r
581                                                                 AliAODTrack::kSecondary,\r
582                                                                 selectInfo);\r
583                 aodTrack->SetTPCClusterMap(esdCascadePos->GetTPCClusterMap());\r
584                 aodTrack->SetTPCSharedMap (esdCascadePos->GetTPCSharedMap());\r
585                 aodTrack->SetChi2perNDF(Chi2perNDF(esdCascadePos));\r
586                 aodTrackRefs->AddAt(aodTrack,idxPosFromV0Dghter);\r
587 \r
588                 if (esdCascadePos->GetSign() > 0) nPosTracks++;\r
589                 aodTrack->ConvertAliPIDtoAODPID();\r
590                 aodTrack->SetFlags(esdCascadePos->GetStatus());\r
591                 SetAODPID(esdCascadePos,aodTrack,detpid,esd->GetMagneticField(), esdPid);\r
592                 }\r
593                 else {\r
594                         aodTrack = dynamic_cast<AliAODTrack*>(aodTrackRefs->At(idxPosFromV0Dghter));\r
595                 }\r
596                 vV0FromCascade->AddDaughter(aodTrack);\r
597         \r
598         \r
599                 // 4.A.3 - Add the negative tracks from the V0\r
600                 \r
601                 esdCascadeNeg->GetPxPyPz(momNeg);\r
602                 esdCascadeNeg->GetXYZ(pos);\r
603                 esdCascadeNeg->GetCovarianceXYZPxPyPz(covTr);\r
604                 esdCascadeNeg->GetESDpid(pid);\r
605                 \r
606                 \r
607                 if (!usedTrack[idxNegFromV0Dghter]) {\r
608                 usedTrack[idxNegFromV0Dghter] = kTRUE;\r
609         \r
610                 UInt_t selectInfo = 0;\r
611                 if (fTrackFilter) selectInfo = fTrackFilter->IsSelected(esdCascadeNeg);\r
612                 if(mcH)mcH->SelectParticle(esdCascadeNeg->GetLabel());\r
613                 aodTrack = new(tracks[jTracks++]) AliAODTrack(  esdCascadeNeg->GetID(),\r
614                                                                 esdCascadeNeg->GetLabel(),\r
615                                                                 momNeg,\r
616                                                                 kTRUE,\r
617                                                                 pos,\r
618                                                                 kFALSE, // Why kFALSE for "isDCA" ? FIXME\r
619                                                                 covTr, \r
620                                                                 (Short_t)esdCascadeNeg->GetSign(),\r
621                                                                 esdCascadeNeg->GetITSClusterMap(), \r
622                                                                 pid,\r
623                                                                 vV0FromCascade,\r
624                                                                 kTRUE,  // usedForVtxFit = kFALSE ? FIXME\r
625                                                                 vtx->UsesTrack(esdCascadeNeg->GetID()),\r
626                                                                 AliAODTrack::kSecondary,\r
627                                                                 selectInfo);\r
628                 aodTrack->SetTPCClusterMap(esdCascadeNeg->GetTPCClusterMap());\r
629                 aodTrack->SetTPCSharedMap (esdCascadeNeg->GetTPCSharedMap());\r
630                 aodTrack->SetChi2perNDF(Chi2perNDF(esdCascadeNeg));\r
631                 aodTrackRefs->AddAt(aodTrack,idxNegFromV0Dghter);\r
632                 \r
633                 if (esdCascadeNeg->GetSign() > 0) nPosTracks++;\r
634                 aodTrack->ConvertAliPIDtoAODPID();\r
635                 aodTrack->SetFlags(esdCascadeNeg->GetStatus());\r
636                 SetAODPID(esdCascadeNeg,aodTrack,detpid,esd->GetMagneticField(),esdPid);\r
637                 }\r
638                 else {\r
639                         aodTrack = dynamic_cast<AliAODTrack*>(aodTrackRefs->At(idxNegFromV0Dghter));\r
640                 }\r
641         \r
642                 vV0FromCascade->AddDaughter(aodTrack);\r
643         \r
644                         \r
645                 // 4.A.4 - Add the V0 from cascade to the V0 array\r
646 \r
647                 Double_t  dcaV0Daughters      = esdV0FromCascade->GetDcaV0Daughters();\r
648                 Double_t  dcaV0ToPrimVertex   = esdV0FromCascade->GetD( esd->GetPrimaryVertex()->GetX(),\r
649                                                                         esd->GetPrimaryVertex()->GetY(),\r
650                                                                         esd->GetPrimaryVertex()->GetZ() );\r
651                 esdV0FromCascade->GetPPxPyPz( momPosAtV0vtx[0],momPosAtV0vtx[1],momPosAtV0vtx[2] ); \r
652                 esdV0FromCascade->GetNPxPyPz( momNegAtV0vtx[0],momNegAtV0vtx[1],momNegAtV0vtx[2] ); \r
653         \r
654                 Double_t dcaDaughterToPrimVertex[2] = { 999., 999.}; // ..[0] = DCA in (x,y) for Pos and ..[1] = Neg\r
655                 dcaDaughterToPrimVertex[0] = TMath::Abs(esdCascadePos->GetD(    esd->GetPrimaryVertex()->GetX(),\r
656                                                                                 esd->GetPrimaryVertex()->GetY(),\r
657                                                                                 esd->GetMagneticField())        );\r
658                 dcaDaughterToPrimVertex[1] = TMath::Abs(esdCascadeNeg->GetD(    esd->GetPrimaryVertex()->GetX(),\r
659                                                                                 esd->GetPrimaryVertex()->GetY(),\r
660                                                                                 esd->GetMagneticField())        );\r
661                 \r
662                 aodV0 = new(v0s[jV0s++]) AliAODv0( vV0FromCascade, \r
663                                                    dcaV0Daughters,\r
664                                                    dcaV0ToPrimVertex, \r
665                                                    momPosAtV0vtx, \r
666                                                    momNegAtV0vtx, \r
667                                                    dcaDaughterToPrimVertex); \r
668                 // set the aod v0 on-the-fly status\r
669                 aodV0->SetOnFlyStatus(esdV0FromCascade->GetOnFlyStatus());\r
670 \r
671                 // Add the aodV0 to the aodVORefs\r
672                 aodV0Refs->AddAt(aodV0,idxV0FromCascade);\r
673                 \r
674         usedV0[idxV0FromCascade] = kTRUE;\r
675 \r
676         } else { \r
677         // 4.B - if V0 structure already used\r
678 \r
679                 // Note :\r
680                 //    one V0 can be used by several cascades (frequent in PbPb evts) : \r
681                 // same V0 which used but attached to different bachelor tracks\r
682                 // -> aodVORefs and aodV0VtxRefs are needed.\r
683                 // Goal : avoid a redundancy of the info in "Vertices" and "v0s" clones array.\r
684         \r
685                 vV0FromCascade = dynamic_cast<AliAODVertex*>( aodV0VtxRefs->At(idxV0FromCascade) );\r
686                 aodV0          = dynamic_cast<AliAODv0*>    ( aodV0Refs   ->At(idxV0FromCascade) );\r
687                  \r
688                 // - Treatment of the parent for such a "re-used" V0 :\r
689                 // Insert the cascade that reuses the V0 vertex in the lineage chain\r
690                 // Before : vV0 -> vCascade1 -> vPrimary\r
691                 //  - Hyp : cascade2 uses the same V0 as cascade1\r
692                 //  After :  vV0 -> vCascade2 -> vCascade1 -> vPrimary\r
693                 \r
694                 AliAODVertex *vCascadePreviousParent = dynamic_cast<AliAODVertex*> (vV0FromCascade->GetParent());\r
695                         vV0FromCascade->SetParent(vCascade);\r
696                         vCascade      ->SetParent(vCascadePreviousParent);\r
697 \r
698                 if(fDebug > 2)  \r
699                         printf("---- Cascade / Lineage insertion\n"\r
700                                 "Parent of V0 vtx                 = Cascade vtx %p\n"\r
701                                 "Parent of the cascade vtx        = Cascade vtx %p\n"\r
702                                 "Parent of the parent cascade vtx = Cascade vtx %p\n", \r
703                                                 static_cast<void*> (vV0FromCascade->GetParent()),\r
704                                                 static_cast<void*> (vCascade->GetParent()),\r
705                                                 static_cast<void*> (vCascadePreviousParent->GetParent()) );\r
706                 \r
707         }// end if V0 structure already used\r
708 \r
709         if (fDebug > 2) {\r
710                 printf("---- Cascade / V0 vertex: \n");\r
711                 vV0FromCascade->Print();\r
712         }\r
713 \r
714         if (fDebug > 4) {\r
715                 printf("---- Cascade / pos dghter : \n");\r
716                         aodTrack->Print();\r
717                 printf("---- Cascade / neg dghter : \n");\r
718                         aodTrack->Print();\r
719                 printf("---- Cascade / aodV0 : \n");\r
720                         aodV0->Print();\r
721         }\r
722 \r
723         // In any case (used V0 or not), add the V0 vertex to the cascade one.\r
724         vCascade->AddDaughter(vV0FromCascade);  \r
725         \r
726                 \r
727         // 5 - Add the primary track of the cascade (if any)\r
728 \r
729 \r
730         // 6 - Add the cascade to the AOD array of cascades\r
731 \r
732         Double_t dcaBachToPrimVertexXY = TMath::Abs(esdCascadeBach->GetD(esd->GetPrimaryVertex()->GetX(),\r
733                                                                          esd->GetPrimaryVertex()->GetY(),\r
734                                                                          esd->GetMagneticField())        );\r
735 \r
736         esdCascade->GetBPxPyPz(momBachAtCascadeVtx[0], momBachAtCascadeVtx[1], momBachAtCascadeVtx[2]);\r
737 \r
738         aodCascade = new(cascades[jCascades++]) AliAODcascade(  vCascade,\r
739                                                                 esdCascade->Charge(),\r
740                                                                 esdCascade->GetDcaXiDaughters(),\r
741                                                                 -999.,\r
742                 // DCAXiToPrimVtx -> needs to be calculated   ----|\r
743                 // doesn't exist at ESD level;\r
744                 // See AODcascade::DcaXiToPrimVertex(Double, Double, Double)\r
745                                                                 dcaBachToPrimVertexXY,\r
746                                                                 momBachAtCascadeVtx,\r
747                                                                 *aodV0);\r
748         \r
749         if (fDebug > 3) {\r
750                 printf("---- Cascade / AOD cascade : \n\n");\r
751                 aodCascade->PrintXi(primary->GetX(), primary->GetY(), primary->GetZ());\r
752         }\r
753 \r
754     } // end of the loop on cascades\r
755 \r
756     cascades.Expand(jCascades);\r
757 \r
758 \r
759     //\r
760     // V0s\r
761     //\r
762     \r
763     for (Int_t nV0 = 0; nV0 < nV0s; ++nV0) {\r
764         \r
765         if (usedV0[nV0]) continue; // skip if already added to the AOD\r
766         \r
767         AliESDv0 *v0 = esd->GetV0(nV0);\r
768         Int_t posFromV0 = v0->GetPindex();\r
769         Int_t negFromV0 = v0->GetNindex();\r
770         \r
771         // V0 selection \r
772         //\r
773         AliESDVertex *esdVtx   = new AliESDVertex(*(esd->GetPrimaryVertex()));\r
774         AliESDtrack  *esdV0Pos = esd->GetTrack(posFromV0);\r
775         AliESDtrack  *esdV0Neg = esd->GetTrack(negFromV0);\r
776         TList v0objects;\r
777         v0objects.AddAt(v0,                      0);\r
778         v0objects.AddAt(esdV0Pos,                1);\r
779         v0objects.AddAt(esdV0Neg,                2);\r
780         v0objects.AddAt(esdVtx,                  3);\r
781         UInt_t selectV0 = 0;\r
782         if (fV0Filter) {\r
783           selectV0 = fV0Filter->IsSelected(&v0objects);\r
784           // this is a little awkward but otherwise the \r
785           // list wants to access the pointer (delete it) \r
786           // again when going out of scope\r
787           delete v0objects.RemoveAt(3); // esdVtx created via copy construct\r
788           esdVtx = 0;\r
789           if (!selectV0) \r
790             continue;\r
791         }\r
792         else{\r
793           delete v0objects.RemoveAt(3); // esdVtx created via copy construct\r
794           esdVtx = 0;\r
795         }\r
796     \r
797         v0->GetXYZ(pos[0], pos[1], pos[2]);\r
798 \r
799         if (!old) {\r
800             chi2 = v0->GetChi2V0(); // = chi2/NDF since NDF = 2*2-3\r
801             v0->GetPosCov(covVtx);\r
802         } else {\r
803             chi2 = -999.;\r
804             for (Int_t i = 0; i < 6; i++)  covVtx[i] = 0.;\r
805         }\r
806 \r
807 \r
808         AliAODVertex * vV0 = \r
809           new(vertices[jVertices++]) AliAODVertex(pos,\r
810                                                   covVtx,\r
811                                                   chi2,\r
812                                                   primary,\r
813                                                   nV0,\r
814                                                   AliAODVertex::kV0);\r
815         primary->AddDaughter(vV0);\r
816         \r
817 \r
818         // Add the positive tracks from the V0\r
819         \r
820         esdV0Pos->GetPxPyPz(momPos);\r
821         esdV0Pos->GetXYZ(pos);\r
822         esdV0Pos->GetCovarianceXYZPxPyPz(covTr);\r
823         esdV0Pos->GetESDpid(pid);\r
824         \r
825         if (!usedTrack[posFromV0]) {\r
826             usedTrack[posFromV0] = kTRUE;\r
827             UInt_t selectInfo = 0;\r
828             if (fTrackFilter) selectInfo = fTrackFilter->IsSelected(esdV0Pos);\r
829             if(mcH)mcH->SelectParticle(esdV0Pos->GetLabel());\r
830             aodTrack = new(tracks[jTracks++]) AliAODTrack(esdV0Pos->GetID(),\r
831                                                           esdV0Pos->GetLabel(), \r
832                                                           momPos, \r
833                                                           kTRUE,\r
834                                                           pos,\r
835                                                           kFALSE,\r
836                                                           covTr, \r
837                                                           (Short_t)esdV0Pos->GetSign(),\r
838                                                           esdV0Pos->GetITSClusterMap(), \r
839                                                           pid,\r
840                                                           vV0,\r
841                                                           kTRUE,  // check if this is right\r
842                                                           vtx->UsesTrack(esdV0Pos->GetID()),\r
843                                                           AliAODTrack::kSecondary,\r
844                                                           selectInfo);\r
845             aodTrack->SetTPCClusterMap(esdV0Pos->GetTPCClusterMap());\r
846             aodTrack->SetTPCSharedMap (esdV0Pos->GetTPCSharedMap());\r
847             aodTrack->SetChi2perNDF(Chi2perNDF(esdV0Pos));\r
848             aodTrackRefs->AddAt(aodTrack,posFromV0);\r
849             //      if (fDebug > 0) printf("-------------------Bo: pos track from original pt %.3f \n",aodTrack->Pt());\r
850             if (esdV0Pos->GetSign() > 0) nPosTracks++;\r
851             aodTrack->ConvertAliPIDtoAODPID();\r
852             aodTrack->SetFlags(esdV0Pos->GetStatus());\r
853             SetAODPID(esdV0Pos,aodTrack,detpid,esd->GetMagneticField(),esdPid);\r
854         }\r
855         else {\r
856             aodTrack = dynamic_cast<AliAODTrack*>(aodTrackRefs->At(posFromV0));\r
857             //      if (fDebug > 0) printf("-------------------Bo pos track from refArray pt %.3f \n",aodTrack->Pt());\r
858         }\r
859         vV0->AddDaughter(aodTrack);\r
860     \r
861         // Add the negative tracks from the V0\r
862         \r
863         esdV0Neg->GetPxPyPz(momNeg);\r
864         esdV0Neg->GetXYZ(pos);\r
865         esdV0Neg->GetCovarianceXYZPxPyPz(covTr);\r
866         esdV0Neg->GetESDpid(pid);\r
867         \r
868         if (!usedTrack[negFromV0]) {\r
869             usedTrack[negFromV0] = kTRUE;\r
870             UInt_t selectInfo = 0;\r
871             if (fTrackFilter) selectInfo = fTrackFilter->IsSelected(esdV0Neg);\r
872             if(mcH)mcH->SelectParticle(esdV0Neg->GetLabel());\r
873             aodTrack = new(tracks[jTracks++]) AliAODTrack(esdV0Neg->GetID(),\r
874                                                           esdV0Neg->GetLabel(),\r
875                                                           momNeg,\r
876                                                           kTRUE,\r
877                                                           pos,\r
878                                                           kFALSE,\r
879                                                           covTr, \r
880                                                           (Short_t)esdV0Neg->GetSign(),\r
881                                                           esdV0Neg->GetITSClusterMap(), \r
882                                                           pid,\r
883                                                           vV0,\r
884                                                           kTRUE,  // check if this is right\r
885                                                           vtx->UsesTrack(esdV0Neg->GetID()),\r
886                                                           AliAODTrack::kSecondary,\r
887                                                           selectInfo);\r
888             aodTrack->SetTPCClusterMap(esdV0Neg->GetTPCClusterMap());\r
889             aodTrack->SetTPCSharedMap (esdV0Neg->GetTPCSharedMap());\r
890             aodTrack->SetChi2perNDF(Chi2perNDF(esdV0Neg));\r
891             \r
892             aodTrackRefs->AddAt(aodTrack,negFromV0);\r
893             //      if (fDebug > 0) printf("-------------------Bo: neg track from original pt %.3f \n",aodTrack->Pt());\r
894             if (esdV0Neg->GetSign() > 0) nPosTracks++;\r
895             aodTrack->ConvertAliPIDtoAODPID();\r
896             aodTrack->SetFlags(esdV0Neg->GetStatus());\r
897             SetAODPID(esdV0Neg,aodTrack,detpid,esd->GetMagneticField(),esdPid);\r
898         }\r
899         else {\r
900             aodTrack = dynamic_cast<AliAODTrack*>(aodTrackRefs->At(negFromV0));\r
901             //      if (fDebug > 0) printf("-------------------Bo neg track from refArray pt %.3f \n",aodTrack->Pt());\r
902         }\r
903         vV0->AddDaughter(aodTrack);\r
904 \r
905 \r
906         // Add the V0 the V0 array as well\r
907         \r
908         Double_t  dcaV0Daughters      = v0->GetDcaV0Daughters();\r
909         Double_t  dcaV0ToPrimVertex   = v0->GetD(esd->GetPrimaryVertex()->GetX(),\r
910                                                  esd->GetPrimaryVertex()->GetY(),\r
911                                                  esd->GetPrimaryVertex()->GetZ());\r
912         v0->GetPPxPyPz(momPosAtV0vtx[0],momPosAtV0vtx[1],momPosAtV0vtx[2]); \r
913         v0->GetNPxPyPz(momNegAtV0vtx[0],momNegAtV0vtx[1],momNegAtV0vtx[2]); \r
914         \r
915         Double_t dcaDaughterToPrimVertex[2] = { 999., 999.}; // ..[0] = DCA in (x,y) for Pos and ..[1] = Neg\r
916         dcaDaughterToPrimVertex[0] = TMath::Abs(esdV0Pos->GetD(  esd->GetPrimaryVertex()->GetX(),\r
917                                                                  esd->GetPrimaryVertex()->GetY(),\r
918                                                                         esd->GetMagneticField()) );\r
919         dcaDaughterToPrimVertex[1] = TMath::Abs(esdV0Neg->GetD(  esd->GetPrimaryVertex()->GetX(),\r
920                                                                  esd->GetPrimaryVertex()->GetY(),\r
921                                                                         esd->GetMagneticField()) );\r
922         \r
923         aodV0 = new(v0s[jV0s++]) AliAODv0(vV0, \r
924                                         dcaV0Daughters,\r
925                                         dcaV0ToPrimVertex,\r
926                                         momPosAtV0vtx,\r
927                                         momNegAtV0vtx,\r
928                                         dcaDaughterToPrimVertex);\r
929 \r
930         // set the aod v0 on-the-fly status\r
931         aodV0->SetOnFlyStatus(v0->GetOnFlyStatus());\r
932     }//End of loop on V0s \r
933 \r
934     v0s.Expand(jV0s);    \r
935 \r
936     if (fDebug > 0)   printf("   NAODCascades=%d / NAODV0s=%d\n", jCascades, jV0s);\r
937     // end of V0 parts\r
938 \r
939 \r
940     // Kinks: it is a big mess the access to the information in the kinks\r
941     // The loop is on the tracks in order to find the mother and daugther of each kink\r
942     \r
943     \r
944     for (Int_t iTrack=0; iTrack<nTracks; ++iTrack) {\r
945         \r
946         AliESDtrack * esdTrack = esd->GetTrack(iTrack);\r
947         \r
948         Int_t ikink = esdTrack->GetKinkIndex(0);\r
949         \r
950         if (ikink && nKinks) {\r
951             // Negative kink index: mother, positive: daughter\r
952             \r
953             // Search for the second track of the kink\r
954             \r
955             for (Int_t jTrack = iTrack+1; jTrack<nTracks; ++jTrack) {\r
956                 \r
957                 AliESDtrack * esdTrack1 = esd->GetTrack(jTrack);\r
958                 \r
959                 Int_t jkink = esdTrack1->GetKinkIndex(0);\r
960                 \r
961                 if ( TMath::Abs(ikink)==TMath::Abs(jkink) ) {\r
962                     \r
963                     // The two tracks are from the same kink\r
964                     \r
965                     if (usedKink[TMath::Abs(ikink)-1]) continue; // skip used kinks\r
966                     \r
967                     Int_t imother = -1;\r
968                     Int_t idaughter = -1;\r
969                     \r
970                     if (ikink<0 && jkink>0) {\r
971                         \r
972                         imother = iTrack;\r
973                         idaughter = jTrack;\r
974                     }\r
975                     else if (ikink>0 && jkink<0) {\r
976                         \r
977                         imother = jTrack;\r
978                         idaughter = iTrack;\r
979                     }\r
980                     else {\r
981 //                      cerr << "Error: Wrong combination of kink indexes: "\r
982 //                           << ikink << " " << jkink << endl;\r
983                         continue;\r
984                     }\r
985                     \r
986                     // Add the mother track if it passed primary track selection cuts\r
987                     \r
988                     AliAODTrack * mother = NULL;\r
989                     \r
990                     UInt_t selectInfo = 0;\r
991                     if (fTrackFilter) {\r
992                         selectInfo = fTrackFilter->IsSelected(esd->GetTrack(imother));\r
993                         if (!selectInfo) continue;\r
994                     }\r
995                     \r
996                     if (!usedTrack[imother]) {\r
997                         \r
998                         usedTrack[imother] = kTRUE;\r
999                         \r
1000                         AliESDtrack *esdTrackM = esd->GetTrack(imother);\r
1001                         esdTrackM->GetPxPyPz(p);\r
1002                         esdTrackM->GetXYZ(pos);\r
1003                         esdTrackM->GetCovarianceXYZPxPyPz(covTr);\r
1004                         esdTrackM->GetESDpid(pid);\r
1005                         if(mcH)mcH->SelectParticle(esdTrackM->GetLabel());\r
1006                         mother = \r
1007                             new(tracks[jTracks++]) AliAODTrack(esdTrackM->GetID(),\r
1008                                                                esdTrackM->GetLabel(),\r
1009                                                                p,\r
1010                                                                kTRUE,\r
1011                                                                pos,\r
1012                                                                kFALSE,\r
1013                                                                covTr, \r
1014                                                                (Short_t)esdTrackM->GetSign(),\r
1015                                                                esdTrackM->GetITSClusterMap(), \r
1016                                                                pid,\r
1017                                                                primary,\r
1018                                                                kTRUE, // check if this is right\r
1019                                                                vtx->UsesTrack(esdTrack->GetID()),\r
1020                                                                AliAODTrack::kPrimary,\r
1021                                                                selectInfo);\r
1022                         mother->SetTPCClusterMap(esdTrackM->GetTPCClusterMap());\r
1023                         mother->SetTPCSharedMap (esdTrackM->GetTPCSharedMap());\r
1024                         mother->SetChi2perNDF(Chi2perNDF(esdTrackM));\r
1025 \r
1026                         aodTrackRefs->AddAt(mother, imother);\r
1027                         \r
1028                         if (esdTrackM->GetSign() > 0) nPosTracks++;\r
1029                         mother->SetFlags(esdTrackM->GetStatus());\r
1030                         mother->ConvertAliPIDtoAODPID();\r
1031                         primary->AddDaughter(mother);\r
1032                         mother->ConvertAliPIDtoAODPID();\r
1033                         SetAODPID(esdTrackM,mother,detpid,esd->GetMagneticField(),esdPid);\r
1034                     }\r
1035                     else {\r
1036 //                      cerr << "Error: event " << esd->GetEventNumberInFile() << " kink " << TMath::Abs(ikink)-1\r
1037 //                           << " track " << imother << " has already been used!" << endl;\r
1038                   }\r
1039                     \r
1040                     // Add the kink vertex\r
1041                     AliESDkink * kink = esd->GetKink(TMath::Abs(ikink)-1);\r
1042                     \r
1043                     AliAODVertex * vkink = \r
1044                         new(vertices[jVertices++]) AliAODVertex(kink->GetPosition(),\r
1045                                                                 NULL,\r
1046                                                                 0.,\r
1047                                                                 mother,\r
1048                                                                 esdTrack->GetID(),  // This is the track ID of the mother's track!\r
1049                                                                 AliAODVertex::kKink);\r
1050                     // Add the daughter track\r
1051                   \r
1052                     AliAODTrack * daughter = NULL;\r
1053                     \r
1054                     if (!usedTrack[idaughter]) {\r
1055                         \r
1056                         usedTrack[idaughter] = kTRUE;\r
1057                         \r
1058                         AliESDtrack *esdTrackD = esd->GetTrack(idaughter);\r
1059                         esdTrackD->GetPxPyPz(p);\r
1060                         esdTrackD->GetXYZ(pos);\r
1061                         esdTrackD->GetCovarianceXYZPxPyPz(covTr);\r
1062                         esdTrackD->GetESDpid(pid);\r
1063                         selectInfo = 0;\r
1064                         if (fTrackFilter) selectInfo = fTrackFilter->IsSelected(esdTrackD);\r
1065                         if(mcH)mcH->SelectParticle(esdTrackD->GetLabel());\r
1066                         daughter = \r
1067                             new(tracks[jTracks++]) AliAODTrack(esdTrackD->GetID(),\r
1068                                                                esdTrackD->GetLabel(),\r
1069                                                                p,\r
1070                                                                kTRUE,\r
1071                                                                pos,\r
1072                                                                kFALSE,\r
1073                                                                covTr, \r
1074                                                                (Short_t)esdTrackD->GetSign(),\r
1075                                                                esdTrackD->GetITSClusterMap(), \r
1076                                                                pid,\r
1077                                                                vkink,\r
1078                                                                kTRUE, // check if this is right\r
1079                                                                vtx->UsesTrack(esdTrack->GetID()),\r
1080                                                                AliAODTrack::kSecondary,\r
1081                                                                selectInfo);\r
1082                         daughter->SetTPCClusterMap(esdTrackD->GetTPCClusterMap());\r
1083                         daughter->SetTPCSharedMap (esdTrackD->GetTPCSharedMap());\r
1084                         aodTrackRefs->AddAt(daughter, idaughter);\r
1085                         \r
1086                         if (esdTrackD->GetSign() > 0) nPosTracks++;\r
1087                         daughter->SetFlags(esdTrackD->GetStatus());\r
1088                         daughter->ConvertAliPIDtoAODPID();\r
1089                         vkink->AddDaughter(daughter);\r
1090                         daughter->ConvertAliPIDtoAODPID();\r
1091                         SetAODPID(esdTrackD,daughter,detpid,esd->GetMagneticField(),esdPid);\r
1092                     }\r
1093                     else {\r
1094 //                      cerr << "Error: event " << esd->GetEventNumberInFile() << " kink " << TMath::Abs(ikink)-1\r
1095 //                           << " track " << idaughter << " has already been used!" << endl;\r
1096                     }\r
1097                 }\r
1098             }\r
1099         }      \r
1100     }\r
1101     \r
1102   \r
1103     // Tracks (primary and orphan)\r
1104 \r
1105     if (fDebug > 0) printf("NUMBER OF ESD TRACKS %5d\n", nTracks);\r
1106     \r
1107     for (Int_t nTrack = 0; nTrack < nTracks; ++nTrack) {\r
1108         \r
1109         \r
1110       if (usedTrack[nTrack]){\r
1111         continue;\r
1112       }\r
1113         AliESDtrack *esdTrack = esd->GetTrack(nTrack);\r
1114         UInt_t selectInfo = 0;\r
1115         //\r
1116         // Track selection\r
1117         if (fTrackFilter) {\r
1118             selectInfo = fTrackFilter->IsSelected(esdTrack);\r
1119             if (!selectInfo && !vtx->UsesTrack(esdTrack->GetID())) continue;\r
1120         }\r
1121         \r
1122         //\r
1123         esdTrack->GetPxPyPz(p);\r
1124         esdTrack->GetXYZ(pos);\r
1125         esdTrack->GetCovarianceXYZPxPyPz(covTr);\r
1126         esdTrack->GetESDpid(pid);\r
1127         if(mcH)mcH->SelectParticle(esdTrack->GetLabel());\r
1128         primary->AddDaughter(aodTrack =\r
1129                              new(tracks[jTracks++]) AliAODTrack(esdTrack->GetID(),\r
1130                                                                 esdTrack->GetLabel(),\r
1131                                                                 p,\r
1132                                                                 kTRUE,\r
1133                                                                 pos,\r
1134                                                                 kFALSE,\r
1135                                                                 covTr, \r
1136                                                                 (Short_t)esdTrack->GetSign(),\r
1137                                                                 esdTrack->GetITSClusterMap(), \r
1138                                                                 pid,\r
1139                                                                 primary,\r
1140                                                                 kTRUE, // check if this is right\r
1141                                                                 vtx->UsesTrack(esdTrack->GetID()),\r
1142                                                                 AliAODTrack::kPrimary, \r
1143                                                                 selectInfo)\r
1144                              );\r
1145         aodTrack->SetTPCClusterMap(esdTrack->GetTPCClusterMap());\r
1146         aodTrack->SetTPCSharedMap (esdTrack->GetTPCSharedMap());\r
1147         aodTrack->SetChi2perNDF(Chi2perNDF(esdTrack));\r
1148 \r
1149         aodTrackRefs->AddAt(aodTrack, nTrack);\r
1150 \r
1151         \r
1152         if (esdTrack->GetSign() > 0) nPosTracks++;\r
1153         aodTrack->SetFlags(esdTrack->GetStatus());\r
1154         aodTrack->ConvertAliPIDtoAODPID();\r
1155         SetAODPID(esdTrack,aodTrack,detpid,esd->GetMagneticField(),esdPid);\r
1156     } // end of loop on tracks\r
1157     \r
1158     // Update number of AOD tracks in header at the end of track loop (M.G.)\r
1159     header->SetRefMultiplicity(jTracks);\r
1160     header->SetRefMultiplicityPos(nPosTracks);\r
1161     header->SetRefMultiplicityNeg(jTracks - nPosTracks);\r
1162     if (fDebug > 0) \r
1163       printf("   NAODTRACKS=%d  NPOS=%d  NNEG=%d\n", jTracks, nPosTracks, jTracks - nPosTracks);\r
1164 \r
1165 \r
1166     // Do not shrink the array of tracks - other filters may add to it (M.G)\r
1167     //   tracks.Expand(jTracks); // remove 'empty slots' due to unwritten tracks\r
1168     \r
1169     if(fTPCOnlyFilterMask){\r
1170     // Loop over the tracks and extract and mask out all aod tracks that pass the selections for AODt racks\r
1171       for(int it = 0;it < jTracks;++it){\r
1172         AliAODTrack *tr = (AliAODTrack*)tracks.At(it);\r
1173         if(!tr)continue;\r
1174         UInt_t map = tr->GetFilterMap();\r
1175         if(map&fTPCOnlyFilterMask){\r
1176           // we only reset the track select ionfo, no deletion...\r
1177           tr->SetFilterMap(map&~fTPCOnlyFilterMask);\r
1178         }\r
1179       }\r
1180       // Loop over the ESD trcks and pick out the tracks passing TPC only cuts\r
1181       \r
1182       \r
1183      const AliESDVertex *vtxSPD = esd->GetPrimaryVertexSPD();\r
1184       for (Int_t nTrack = 0; nTrack < nTracks; ++nTrack) {\r
1185         AliESDtrack* esdTrack = esd->GetTrack(nTrack); //carefull do not modify it othwise  need to work with a copy \r
1186         \r
1187         UInt_t selectInfo = 0;\r
1188         //\r
1189         // Track selection\r
1190         if (fTrackFilter) {\r
1191           selectInfo = fTrackFilter->IsSelected(esdTrack);\r
1192         }\r
1193         selectInfo &= fTPCOnlyFilterMask;\r
1194         if (!selectInfo)continue;\r
1195 \r
1196         // create a tpc only tracl\r
1197         AliESDtrack *track = AliESDtrackCuts::GetTPCOnlyTrack(esd,esdTrack->GetID());\r
1198         if(!track) continue;\r
1199 \r
1200 \r
1201 \r
1202         if(track->Pt()>0.){\r
1203           // only constrain tracks above threshold\r
1204           AliExternalTrackParam exParam;\r
1205           // take the B-feild from the ESD, no 3D fieldMap available at this point\r
1206           Bool_t relate = false;\r
1207           relate = track->RelateToVertex(vtxSPD,esd->GetMagneticField(),kVeryBig,&exParam);\r
1208           if(!relate){\r
1209             delete track;\r
1210             continue;\r
1211           }\r
1212           track->Set(exParam.GetX(),exParam.GetAlpha(),exParam.GetParameter(),exParam.GetCovariance());\r
1213         }\r
1214 \r
1215         track->GetPxPyPz(p);\r
1216         track->GetXYZ(pos);\r
1217         track->GetCovarianceXYZPxPyPz(covTr);\r
1218         track->GetESDpid(pid);\r
1219         if(mcH)mcH->SelectParticle(esdTrack->GetLabel());\r
1220         aodTrack = new(tracks[jTracks++]) AliAODTrack(track->GetID(),\r
1221                                                       track->GetLabel(),\r
1222                                                       p,\r
1223                                                       kTRUE,\r
1224                                                       pos,\r
1225                                                       kFALSE,\r
1226                                                       covTr, \r
1227                                                       (Short_t)track->GetSign(),\r
1228                                                       track->GetITSClusterMap(), \r
1229                                                       pid,\r
1230                                                       primary,\r
1231                                                       kTRUE, // check if this is right\r
1232                                                       vtx->UsesTrack(track->GetID()),\r
1233                                                       AliAODTrack::kPrimary, \r
1234                                                       selectInfo);\r
1235         aodTrack->SetTPCClusterMap(track->GetTPCClusterMap());\r
1236         aodTrack->SetTPCSharedMap (track->GetTPCSharedMap());\r
1237         aodTrack->SetChi2perNDF(Chi2perNDF(track));\r
1238         \r
1239         aodTrackRefs->AddAt(aodTrack, nTrack);\r
1240         \r
1241         delete track;\r
1242       } // end of loop on tracks\r
1243       \r
1244     }//end if(fTPCOnlyFilterMask\r
1245     \r
1246     // Access to the AOD container of PMD clusters\r
1247     TClonesArray &pmdClusters = *(AODEvent()->GetPmdClusters());\r
1248     Int_t jPmdClusters=0;\r
1249   \r
1250     for (Int_t iPmd = 0; iPmd < nPmdClus; ++iPmd) {\r
1251       // file pmd clusters, to be revised!\r
1252       AliESDPmdTrack *pmdTrack = esd->GetPmdTrack(iPmd);\r
1253       Int_t nLabel = 0;\r
1254       Int_t *label = 0x0;\r
1255       Double_t posPmd[3] = { pmdTrack->GetClusterX(), pmdTrack->GetClusterY(), pmdTrack->GetClusterZ()};\r
1256       Double_t pidPmd[13] = { 0.}; // to be revised!\r
1257       // type not set!\r
1258       // assoc cluster not set\r
1259       new(pmdClusters[jPmdClusters++]) AliAODPmdCluster(iPmd, nLabel, label, pmdTrack->GetClusterADC(), posPmd, pidPmd);\r
1260     }\r
1261 \r
1262     // Access to the AOD container of clusters\r
1263     TClonesArray &caloClusters = *(AODEvent()->GetCaloClusters());\r
1264     Int_t jClusters=0;\r
1265  \r
1266     for (Int_t iClust=0; iClust<nCaloClus; ++iClust) {\r
1267 \r
1268       AliESDCaloCluster * cluster = esd->GetCaloCluster(iClust);\r
1269 \r
1270       Int_t  id        = cluster->GetID();\r
1271       Int_t  nLabel    = cluster->GetNLabels();\r
1272       Int_t *labels    = cluster->GetLabels();\r
1273       if(labels){ \r
1274                   for(int i = 0;i < nLabel;++i){\r
1275                           if(mcH)mcH->SelectParticle(labels[i]);\r
1276                   }\r
1277           }             \r
1278 \r
1279       Float_t energy = cluster->E();\r
1280       cluster->GetPosition(posF);\r
1281 \r
1282       AliAODCaloCluster *caloCluster = new(caloClusters[jClusters++]) AliAODCaloCluster(id,\r
1283                                                                                         nLabel,\r
1284                                                                                         labels,\r
1285                                                                                         energy,\r
1286                                                                                         posF,\r
1287                                                                                         NULL,\r
1288                                                                                         cluster->GetType(),0);\r
1289       \r
1290       caloCluster->SetCaloCluster(cluster->GetDistanceToBadChannel(),\r
1291                                   cluster->GetDispersion(),\r
1292                                   cluster->GetM20(), cluster->GetM02(),\r
1293                                   cluster->GetEmcCpvDistance(),  \r
1294                                   cluster->GetNExMax(),cluster->GetTOF()) ;\r
1295 \r
1296       caloCluster->SetPIDFromESD(cluster->GetPID());\r
1297       caloCluster->SetNCells(cluster->GetNCells());\r
1298       caloCluster->SetCellsAbsId(cluster->GetCellsAbsId());\r
1299       caloCluster->SetCellsAmplitudeFraction(cluster->GetCellsAmplitudeFraction());\r
1300 \r
1301       TArrayI* matchedT =       cluster->GetTracksMatched();\r
1302       if (nTracks>0 && matchedT && cluster->GetTrackMatchedIndex() >= 0) {      \r
1303         for (Int_t im = 0; im < matchedT->GetSize(); im++) {\r
1304             Int_t iESDtrack = matchedT->At(im);;\r
1305             if (aodTrackRefs->At(iESDtrack) != 0) {\r
1306                 caloCluster->AddTrackMatched((AliAODTrack*)aodTrackRefs->At(iESDtrack));\r
1307             }\r
1308         }\r
1309       }\r
1310       \r
1311     } \r
1312     caloClusters.Expand(jClusters); // resize TObjArray to 'remove' slots for pseudo clusters    \r
1313     // end of loop on calo clusters\r
1314 \r
1315     // fill EMCAL cell info\r
1316     if (esd->GetEMCALCells()) { // protection against missing ESD information\r
1317       AliESDCaloCells &esdEMcells = *(esd->GetEMCALCells());\r
1318       Int_t nEMcell = esdEMcells.GetNumberOfCells() ;\r
1319       \r
1320       AliAODCaloCells &aodEMcells = *(AODEvent()->GetEMCALCells());\r
1321       aodEMcells.CreateContainer(nEMcell);\r
1322       aodEMcells.SetType(AliAODCaloCells::kEMCALCell);\r
1323       for (Int_t iCell = 0; iCell < nEMcell; iCell++) {      \r
1324         aodEMcells.SetCell(iCell,esdEMcells.GetCellNumber(iCell),esdEMcells.GetAmplitude(iCell));\r
1325       }\r
1326       aodEMcells.Sort();\r
1327     }\r
1328 \r
1329     // fill PHOS cell info\r
1330     if (esd->GetPHOSCells()) { // protection against missing ESD information\r
1331       AliESDCaloCells &esdPHcells = *(esd->GetPHOSCells());\r
1332       Int_t nPHcell = esdPHcells.GetNumberOfCells() ;\r
1333       \r
1334       AliAODCaloCells &aodPHcells = *(AODEvent()->GetPHOSCells());\r
1335       aodPHcells.CreateContainer(nPHcell);\r
1336       aodPHcells.SetType(AliAODCaloCells::kPHOSCell);\r
1337       for (Int_t iCell = 0; iCell < nPHcell; iCell++) {      \r
1338         aodPHcells.SetCell(iCell,esdPHcells.GetCellNumber(iCell),esdPHcells.GetAmplitude(iCell));\r
1339       }\r
1340       aodPHcells.Sort();\r
1341     }\r
1342 \r
1343     // tracklets    \r
1344     AliAODTracklets &SPDTracklets = *(AODEvent()->GetTracklets());\r
1345     const AliMultiplicity *mult = esd->GetMultiplicity();\r
1346     if (mult) {\r
1347       if (mult->GetNumberOfTracklets()>0) {\r
1348         SPDTracklets.CreateContainer(mult->GetNumberOfTracklets());\r
1349 \r
1350         for (Int_t n=0; n<mult->GetNumberOfTracklets(); n++) {\r
1351           if(mcH){\r
1352             mcH->SelectParticle(mult->GetLabel(n, 0));\r
1353             mcH->SelectParticle(mult->GetLabel(n, 1));\r
1354           }\r
1355           SPDTracklets.SetTracklet(n, mult->GetTheta(n), mult->GetPhi(n), mult->GetDeltaPhi(n), mult->GetLabel(n, 0),mult->GetLabel(n, 1));\r
1356         }\r
1357       }\r
1358     } else {\r
1359       //Printf("ERROR: AliMultiplicity could not be retrieved from ESD");\r
1360     }\r
1361 \r
1362     delete [] usedKink;\r
1363     delete [] usedV0;\r
1364     delete [] usedTrack;    \r
1365     delete    aodV0Refs;\r
1366     delete    aodV0VtxRefs;\r
1367     delete    aodTrackRefs;\r
1368     delete    esdPid;\r
1369     return;\r
1370 }\r
1371 \r
1372 \r
1373 void AliAnalysisTaskESDfilter::SetAODPID(AliESDtrack *esdtrack, AliAODTrack *aodtrack, AliAODPid *detpid, Double_t bfield, AliESDpid *esdpid)\r
1374 {\r
1375   //\r
1376   // Setter for the raw PID detector signals\r
1377   //\r
1378 \r
1379   // Save PID object for candidate electrons\r
1380     Bool_t pidSave = kFALSE;\r
1381     if (fTrackFilter) {\r
1382         Bool_t selectInfo = fTrackFilter->IsSelected((char*) "Electrons");\r
1383         if (selectInfo)  pidSave = kTRUE;\r
1384     }\r
1385 \r
1386 \r
1387     // Tracks passing pt cut \r
1388     if(esdtrack->Pt()>fHighPthreshold) {\r
1389         pidSave = kTRUE;\r
1390     } else {\r
1391         if(fPtshape){\r
1392             if(esdtrack->Pt()> fPtshape->GetXmin()){\r
1393                 Double_t y = fPtshape->Eval(esdtrack->Pt())/fPtshape->Eval(fHighPthreshold);\r
1394                 if(gRandom->Rndm(0)<1./y){\r
1395                     pidSave = kTRUE;\r
1396                 }//end rndm\r
1397             }//end if p < pmin\r
1398         }//end if p function\r
1399     }// end else\r
1400 \r
1401     if (pidSave) {\r
1402       if(!aodtrack->GetDetPid()){// prevent memory leak when calling SetAODPID twice for the same track\r
1403         detpid = new AliAODPid();\r
1404         SetDetectorRawSignals(detpid,esdtrack, bfield, esdpid);\r
1405         aodtrack->SetDetPID(detpid);\r
1406       }\r
1407     }\r
1408 }\r
1409 \r
1410 void AliAnalysisTaskESDfilter::SetDetectorRawSignals(AliAODPid *aodpid, AliESDtrack *track, Double_t bfield, AliESDpid *esdpid)\r
1411 {\r
1412 //\r
1413 //assignment of the detector signals (AliXXXesdPID inspired)\r
1414 //\r
1415  if(!track){\r
1416  AliInfo("no ESD track found. .....exiting");\r
1417  return;\r
1418  }\r
1419  // TPC momentum\r
1420  const AliExternalTrackParam *in=track->GetInnerParam();\r
1421  if (in) {\r
1422    aodpid->SetTPCmomentum(in->GetP());\r
1423  }else{\r
1424    aodpid->SetTPCmomentum(-1.);\r
1425  }\r
1426 \r
1427 \r
1428  aodpid->SetITSsignal(track->GetITSsignal());\r
1429  aodpid->SetTPCsignal(track->GetTPCsignal());\r
1430  aodpid->SetTPCsignalN(track->GetTPCsignalN());\r
1431 \r
1432  //n TRD planes = 6\r
1433  Int_t nslices = track->GetNumberOfTRDslices()*6;\r
1434  Double_t *trdslices = new Double_t[nslices];\r
1435  for(Int_t iSl =0; iSl < track->GetNumberOfTRDslices(); iSl++) {\r
1436    for(Int_t iPl =0; iPl<6; iPl++) trdslices[iPl*track->GetNumberOfTRDslices()+iSl] = track->GetTRDslice(iPl,iSl);\r
1437  }\r
1438  \r
1439 //TRD momentum\r
1440  for(Int_t iPl=0;iPl<6;iPl++){\r
1441    Double_t trdmom=track->GetTRDmomentum(iPl);\r
1442    aodpid->SetTRDmomentum(iPl,trdmom);\r
1443  }\r
1444 \r
1445  aodpid->SetTRDsignal(track->GetNumberOfTRDslices()*6,trdslices);\r
1446 \r
1447  //TOF PID  \r
1448  Double_t times[AliAODPid::kSPECIES]; track->GetIntegratedTimes(times);\r
1449  aodpid->SetIntegratedTimes(times);\r
1450  Float_t tzeroTrack = esdpid->GetTOFResponse().GetStartTime(track->P());\r
1451  aodpid->SetTOFsignal(track->GetTOFsignal()-tzeroTrack);\r
1452 \r
1453  Double_t tofRes[5];\r
1454  for (Int_t iMass=0; iMass<5; iMass++){\r
1455    tofRes[iMass]=(Double_t)esdpid->GetTOFResponse().GetExpectedSigma(track->P(), times[iMass], AliPID::ParticleMass(iMass));\r
1456  }\r
1457  aodpid->SetTOFpidResolution(tofRes);\r
1458  \r
1459  aodpid->SetHMPIDsignal(track->GetHMPIDsignal());\r
1460 \r
1461  //Extrapolate track to EMCAL surface for AOD-level track-cluster matching\r
1462  Double_t emcpos[3] = {0.,0.,0.};\r
1463  Double_t emcmom[3] = {0.,0.,0.};\r
1464  aodpid->SetEMCALPosition(emcpos);\r
1465  aodpid->SetEMCALMomentum(emcmom);\r
1466 \r
1467  AliExternalTrackParam *outerparam = (AliExternalTrackParam*)track->GetOuterParam();\r
1468  if(!outerparam) return;\r
1469 \r
1470  //To be replaced by call to AliEMCALGeoUtils when the class becomes available\r
1471  Double_t radius = 441.0; //[cm] EMCAL radius +13cm\r
1472 \r
1473  Bool_t okpos = outerparam->GetXYZAt(radius,bfield,emcpos);\r
1474  Bool_t okmom = outerparam->GetPxPyPzAt(radius,bfield,emcmom);\r
1475  if(!(okpos && okmom)) return;\r
1476 \r
1477  aodpid->SetEMCALPosition(emcpos);\r
1478  aodpid->SetEMCALMomentum(emcmom);\r
1479 \r
1480 }\r
1481 \r
1482 Double_t  AliAnalysisTaskESDfilter::Chi2perNDF(AliESDtrack* track)\r
1483 {\r
1484     // Calculate chi2 per ndf for track\r
1485     Int_t  nClustersTPC = track->GetTPCNcls();\r
1486 \r
1487     if ( nClustersTPC > 5) {\r
1488        return (track->GetTPCchi2()/Float_t(nClustersTPC - 5));\r
1489     } else {\r
1490        return (-1.);\r
1491     }\r
1492  }\r
1493 \r
1494 \r
1495 \r
1496 void AliAnalysisTaskESDfilter::Terminate(Option_t */*option*/)\r
1497 {\r
1498 // Terminate analysis\r
1499 //\r
1500     if (fDebug > 1) printf("AnalysisESDfilter: Terminate() \n");\r
1501 }\r
1502 \r
1503 void  AliAnalysisTaskESDfilter::PrintMCInfo(AliStack *pStack,Int_t label){\r
1504   if(!pStack)return;\r
1505   label = TMath::Abs(label);\r
1506   TParticle *part = pStack->Particle(label);\r
1507   Printf("########################");\r
1508   Printf("%s:%d %d UniqueID %d PDG %d P %3.3f",(char*)__FILE__,__LINE__,label,part->GetUniqueID(),part->GetPdgCode(),part->P());\r
1509   part->Print();\r
1510   TParticle* mother = part;\r
1511   Int_t imo = part->GetFirstMother();\r
1512   Int_t nprim = pStack->GetNprimary();\r
1513   //  while((imo >= nprim) && (mother->GetUniqueID() == 4)) {\r
1514   while((imo >= nprim)) {\r
1515     mother =  pStack->Particle(imo);\r
1516     Printf("Mother %s:%d Label %d UniqueID %d PDG %d P %3.3f",(char*)__FILE__,__LINE__,imo,mother->GetUniqueID(),mother->GetPdgCode(),mother->P());\r
1517     mother->Print();\r
1518     imo =  mother->GetFirstMother();\r
1519   }\r
1520   Printf("########################");\r
1521 }\r