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