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