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