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