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