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