]> git.uio.no Git - u/mrichter/AliRoot.git/blob - ANALYSIS/AliAnalysisTaskESDfilter.cxx
We found a bug in ANALYSIS/AliAnalysisTaskESDfilter.cxx
[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     fHighPthreshold(0),
57     fPtshape(0x0)
58 {
59   // Default constructor
60 }
61
62 AliAnalysisTaskESDfilter::AliAnalysisTaskESDfilter(const char* name):
63     AliAnalysisTaskSE(name),
64     fTrackFilter(0x0),
65     fKinkFilter(0x0),
66     fV0Filter(0x0),
67     fHighPthreshold(0),
68     fPtshape(0x0)
69 {
70   // Constructor
71 }
72
73 void AliAnalysisTaskESDfilter::UserCreateOutputObjects()
74 {
75 // Create the output container
76     OutputTree()->GetUserInfo()->Add(fTrackFilter);
77 }
78
79 void AliAnalysisTaskESDfilter::Init()
80 {
81     // Initialization
82     if (fDebug > 1) AliInfo("Init() \n");
83     // Call configuration file
84 }
85
86
87 void AliAnalysisTaskESDfilter::UserExec(Option_t */*option*/)
88 {
89 // Execute analysis for current event
90 //
91                                             
92   Long64_t ientry = Entry();
93   if (fDebug > 0) printf("Filter: Analysing event # %5d\n", (Int_t) ientry);
94   if (fHighPthreshold == 0) AliInfo("detector PID signals are stored in each track");
95   if (!fPtshape) AliInfo("detector PID signals are not stored below the pt threshold");
96
97   ConvertESDtoAOD();
98 }
99
100 void AliAnalysisTaskESDfilter::ConvertESDtoAOD() {
101     // ESD Filter analysis task executed for each event
102
103     AliESDEvent* esd = dynamic_cast<AliESDEvent*>(InputEvent());
104     AliESD* old = esd->GetAliESDOld();
105
106     // Fetch Stack for debuggging if available 
107     AliStack *pStack = 0;
108     AliMCEventHandler *mcH = 0;
109     if(MCEvent()){
110       pStack = MCEvent()->Stack();
111       mcH = (AliMCEventHandler*) ((AliAnalysisManager::GetAnalysisManager())->GetMCtruthEventHandler()); 
112     }
113     // set arrays and pointers
114     Float_t posF[3];
115     Double_t pos[3];
116     Double_t p[3];
117     Double_t p_pos[3];
118     Double_t p_neg[3];
119     Double_t p_pos_atv0[3];
120     Double_t p_neg_atv0[3];
121     Double_t covVtx[6];
122     Double_t covTr[21];
123     Double_t pid[10];
124
125     for (Int_t i = 0; i < 6; i++)  covVtx[i] = 0.;
126     for (Int_t i = 0; i < 21; i++) covTr [i] = 0.;
127
128     
129     // loop over events and fill them
130     
131     // Multiplicity information needed by the header (to be revised!)
132     Int_t nTracks    = esd->GetNumberOfTracks();
133     //    if (fDebug > 0) printf("-------------------Bo: Number of ESD tracks %d \n",nTracks);
134
135     Int_t nPosTracks = 0;
136 //    for (Int_t iTrack = 0; iTrack < nTracks; ++iTrack) 
137 //      if (esd->GetTrack(iTrack)->GetSign()> 0) nPosTracks++;
138     
139     // Update the header
140
141     AliAODHeader* header = AODEvent()->GetHeader();
142     header->SetRunNumber(esd->GetRunNumber());
143     if (old) {
144         header->SetBunchCrossNumber(0);
145         header->SetOrbitNumber(0);
146         header->SetPeriodNumber(0);
147         header->SetEventType(0);
148         header->SetMuonMagFieldScale(-999.); // FIXME
149         header->SetCentrality(-999.);        // FIXME
150     } else {
151         header->SetBunchCrossNumber(esd->GetBunchCrossNumber());
152         header->SetOrbitNumber(esd->GetOrbitNumber());
153         header->SetPeriodNumber(esd->GetPeriodNumber());
154         header->SetEventType(esd->GetEventType());
155         header->SetMuonMagFieldScale(-999.); // FIXME
156         header->SetCentrality(-999.);        // FIXME
157     }
158     
159     header->SetTriggerMask(esd->GetTriggerMask()); 
160     header->SetTriggerCluster(esd->GetTriggerCluster());
161     header->SetMagneticField(esd->GetMagneticField());
162     header->SetZDCN1Energy(esd->GetZDCN1Energy());
163     header->SetZDCP1Energy(esd->GetZDCP1Energy());
164     header->SetZDCN2Energy(esd->GetZDCN2Energy());
165     header->SetZDCP2Energy(esd->GetZDCP2Energy());
166     header->SetZDCEMEnergy(esd->GetZDCEMEnergy(0),esd->GetZDCEMEnergy(1));
167     Float_t diamxy[2]={esd->GetDiamondX(),esd->GetDiamondY()};
168     Float_t diamcov[3]; esd->GetDiamondCovXY(diamcov);
169     header->SetDiamond(diamxy,diamcov);
170 //
171 //    
172     Int_t nV0s      = esd->GetNumberOfV0s();
173     Int_t nCascades = esd->GetNumberOfCascades();
174     Int_t nKinks    = esd->GetNumberOfKinks();
175     Int_t nVertices = nV0s + 2*nCascades /*could lead to two vertices, one V0 and the Xi */+ nKinks + 1 /* = prim. vtx*/;    
176     Int_t nJets     = 0;
177     Int_t nCaloClus = esd->GetNumberOfCaloClusters();
178     Int_t nFmdClus  = 0;
179     Int_t nPmdClus  = esd->GetNumberOfPmdTracks();
180     
181     if (fDebug > 0) 
182         printf("   NV0=%d  NCASCADES=%d  NKINKS=%d\n", nV0s, nCascades, nKinks);
183
184     AODEvent()->ResetStd(nTracks, nVertices, nV0s+nCascades, nJets, nCaloClus, nFmdClus, nPmdClus);
185
186     AliAODTrack *aodTrack = 0x0;
187     AliAODPid   *detpid   = 0x0;
188     Double_t timezero = 0; //TO BE FIXED
189     AliAODv0    *aodV0    = 0x0;
190
191     // RefArray to store the mapping between esd track number and newly created AOD-Track
192     TRefArray   *aodRefs = NULL;
193     if (nTracks > 0) aodRefs = new TRefArray(nTracks);
194
195     // Array to take into account the tracks already added to the AOD
196     Bool_t * usedTrack = NULL;
197     if (nTracks>0) {
198         usedTrack = new Bool_t[nTracks];
199         for (Int_t iTrack=0; iTrack<nTracks; ++iTrack) usedTrack[iTrack]=kFALSE;
200     }
201     // Array to take into account the V0s already added to the AOD
202     Bool_t * usedV0 = NULL;
203     if (nV0s>0) {
204         usedV0 = new Bool_t[nV0s];
205         for (Int_t iV0=0; iV0<nV0s; ++iV0) usedV0[iV0]=kFALSE;
206     }
207     // Array to take into account the kinks already added to the AOD
208     Bool_t * usedKink = NULL;
209     if (nKinks>0) {
210         usedKink = new Bool_t[nKinks];
211         for (Int_t iKink=0; iKink<nKinks; ++iKink) usedKink[iKink]=kFALSE;
212     }
213     
214     // Access to the AOD container of vertices
215     TClonesArray &vertices = *(AODEvent()->GetVertices());
216     Int_t jVertices=0;
217     
218     // Access to the AOD container of tracks
219     TClonesArray &tracks = *(AODEvent()->GetTracks());
220     Int_t jTracks=0; 
221     
222     // Access to the AOD container of V0s
223     TClonesArray &V0s = *(AODEvent()->GetV0s());
224     Int_t jV0s=0;
225     
226     // Add primary vertex. The primary tracks will be defined
227     // after the loops on the composite objects (V0, cascades, kinks)
228     const AliESDVertex *vtx = esd->GetPrimaryVertex();
229     
230     vtx->GetXYZ(pos); // position
231     vtx->GetCovMatrix(covVtx); //covariance matrix
232     
233     AliAODVertex * primary = new(vertices[jVertices++])
234         AliAODVertex(pos, covVtx, vtx->GetChi2toNDF(), NULL, -1, AliAODVertex::kPrimary);
235     primary->SetName(vtx->GetName());
236     primary->SetTitle(vtx->GetTitle());
237
238     if (fDebug > 0) primary->Print();
239
240     // Create vertices starting from the most complex objects
241     Double_t chi2 = 0.;
242     
243     // Cascades
244     for (Int_t nCascade = 0; nCascade < nCascades; ++nCascade) {
245         AliESDcascade *cascade = esd->GetCascade(nCascade);
246         
247         cascade->GetXYZ(pos[0], pos[1], pos[2]);
248
249         if (!old) {
250             chi2 = cascade->GetChi2Xi(); // = chi2/NDF since NDF = 2*2-3
251             cascade->GetPosCovXi(covVtx);
252         } else {
253             chi2 = -999.;
254             for (Int_t i = 0; i < 6; i++)  covVtx[i] = 0.;
255         }
256         // Add the cascade vertex
257         AliAODVertex * vcascade = new(vertices[jVertices++]) AliAODVertex(pos,
258                                                                           covVtx,
259                                                                           chi2,
260                                                                           primary,
261                                                                           nCascade,
262                                                                           AliAODVertex::kCascade);
263         
264         primary->AddDaughter(vcascade);
265         
266         // Add the V0 from the cascade. The ESD class have to be optimized...
267         // Now we have to search for the corresponding Vo in the list of V0s
268         // using the indeces of the positive and negative tracks
269         
270         Int_t posFromV0 = cascade->GetPindex();
271         Int_t negFromV0 = cascade->GetNindex();
272         
273       
274         AliESDv0 * v0 = 0x0;
275         Int_t indV0 = -1;
276         
277         for (Int_t iV0=0; iV0<nV0s; ++iV0) {
278             
279             v0 = esd->GetV0(iV0);
280             Int_t posV0 = v0->GetPindex();
281             Int_t negV0 = v0->GetNindex();
282             
283             if (posV0==posFromV0 && negV0==negFromV0) {
284                 indV0 = iV0;
285                 break;
286             }
287         }
288         
289         AliAODVertex * vV0FromCascade = 0x0;
290         
291         if (indV0>-1 && !usedV0[indV0] ) {
292             
293             // the V0 exists in the array of V0s and is not used
294             
295             usedV0[indV0] = kTRUE;
296             
297             v0->GetXYZ(pos[0], pos[1], pos[2]);
298             if (!old) {
299                 chi2 = v0->GetChi2V0();  // = chi2/NDF since NDF = 2*2-3                             
300                 v0->GetPosCov(covVtx);
301             } else {
302                 chi2 = -999.;
303                 for (Int_t i = 0; i < 6; i++)  covVtx[i] = 0.;
304             }
305
306             vV0FromCascade = new(vertices[jVertices++]) AliAODVertex(pos,
307                                                                      covVtx,
308                                                                      chi2,
309                                                                      vcascade,
310                                                                      indV0,
311                                                                      AliAODVertex::kV0);
312         } else {
313             
314             // the V0 doesn't exist in the array of V0s or was used
315 //          cerr << "Error: event " << esd->GetEventNumberInFile() << " cascade " << nCascade
316 //               << " The V0 " << indV0 
317 //               << " doesn't exist in the array of V0s or was used!" << endl;
318             
319             cascade->GetXYZ(pos[0], pos[1], pos[2]);
320             
321             if (!old) {
322                 chi2 = v0->GetChi2V0();
323                 cascade->GetPosCov(covVtx);
324             } else {
325                 chi2 = -999.;
326                 for (Int_t i = 0; i < 6; i++)  covVtx[i] = 0.;
327             }
328
329             vV0FromCascade = new(vertices[jVertices++]) AliAODVertex(pos,
330                                                                      covVtx,
331                                                                      chi2, // = chi2/NDF since NDF = 2*2-3 (AM)
332                                                                      vcascade,
333                                                                      indV0,
334                                                                      AliAODVertex::kV0);
335             vcascade->AddDaughter(vV0FromCascade);
336         }
337         
338         // Add the positive tracks from the V0
339         
340         if (posFromV0>-1 && !usedTrack[posFromV0]) {
341             
342             usedTrack[posFromV0] = kTRUE;
343             
344             AliESDtrack *esdTrack = esd->GetTrack(posFromV0);
345             esdTrack->GetPxPyPz(p_pos);
346             esdTrack->GetXYZ(pos);
347             esdTrack->GetCovarianceXYZPxPyPz(covTr);
348             esdTrack->GetESDpid(pid);
349             UInt_t selectInfo = 0;
350             if (fTrackFilter) {
351                 selectInfo = fTrackFilter->IsSelected(esdTrack);
352             }
353             
354             if(mcH)mcH->SelectParticle(esdTrack->GetLabel());
355             vV0FromCascade->AddDaughter(aodTrack =
356                                         new(tracks[jTracks++]) AliAODTrack(esdTrack->GetID(),
357                                                                            esdTrack->GetLabel(), 
358                                                                            p_pos, 
359                                                                            kTRUE,
360                                                                            pos,
361                                                                            kFALSE,
362                                                                            covTr, 
363                                                                            (Short_t)esdTrack->GetSign(),
364                                                                            esdTrack->GetITSClusterMap(), 
365                                                                            pid,
366                                                                            vV0FromCascade,
367                                                                            kTRUE,  // check if this is right
368                                                                            vtx->UsesTrack(esdTrack->GetID()),
369                                                                            AliAODTrack::kSecondary,
370                                                                            selectInfo)
371                                         );
372             aodRefs->AddAt(aodTrack, posFromV0);
373             
374        if (esdTrack->GetSign() > 0) nPosTracks++;
375             aodTrack->ConvertAliPIDtoAODPID();
376             aodTrack->SetFlags(esdTrack->GetStatus());
377             SetAODPID(esdTrack,aodTrack,detpid,timezero);
378         }
379         else {
380 //          cerr << "Error: event " << esd->GetEventNumberInFile() << " cascade " << nCascade
381 //               << " track " << posFromV0 << " has already been used!" << endl;
382         }
383         
384         // Add the negative tracks from the V0
385         
386         if (negFromV0>-1 && !usedTrack[negFromV0]) {
387             
388             usedTrack[negFromV0] = kTRUE;
389             
390             AliESDtrack *esdTrack = esd->GetTrack(negFromV0);
391             esdTrack->GetPxPyPz(p_neg);
392             esdTrack->GetXYZ(pos);
393             esdTrack->GetCovarianceXYZPxPyPz(covTr);
394             esdTrack->GetESDpid(pid);
395             UInt_t selectInfo = 0;
396             if (fTrackFilter) selectInfo = fTrackFilter->IsSelected(esdTrack);      
397             if(mcH)mcH->SelectParticle(esdTrack->GetLabel());
398             vV0FromCascade->AddDaughter(aodTrack =
399                                         new(tracks[jTracks++]) AliAODTrack(esdTrack->GetID(),
400                                                                            esdTrack->GetLabel(),
401                                                                            p_neg,
402                                                                            kTRUE,
403                                                                            pos,
404                                                                            kFALSE,
405                                                                            covTr, 
406                                                                            (Short_t)esdTrack->GetSign(),
407                                                                            esdTrack->GetITSClusterMap(), 
408                                                                            pid,
409                                                                            vV0FromCascade,
410                                                                            kTRUE,  // check if this is right
411                                                                            vtx->UsesTrack(esdTrack->GetID()),
412                                                                            AliAODTrack::kSecondary,
413                                                                            selectInfo)
414                                         );
415             aodRefs->AddAt(aodTrack, negFromV0);
416
417        if (esdTrack->GetSign() > 0) nPosTracks++;
418             aodTrack->ConvertAliPIDtoAODPID();
419             aodTrack->SetFlags(esdTrack->GetStatus());      
420             SetAODPID(esdTrack,aodTrack,detpid,timezero);
421         }
422         else {
423 //          cerr << "Error: event " << esd->GetEventNumberInFile() << " cascade " << nCascade
424 //               << " track " << negFromV0 << " has already been used!" << endl;
425         }
426         
427         // add it to the V0 array as well
428         Double_t d0[2] = { -999., -99.};
429         // counting is probably wrong
430         new(V0s[jV0s++]) AliAODv0(vV0FromCascade, -999., -99., p_pos, p_neg, d0); // to be refined
431
432         // Add the bachelor track from the cascade
433         
434         Int_t bachelor = cascade->GetBindex();
435         
436         if(bachelor>-1 && !usedTrack[bachelor]) {
437             
438             usedTrack[bachelor] = kTRUE;
439             
440             AliESDtrack *esdTrack = esd->GetTrack(bachelor);
441             esdTrack->GetPxPyPz(p);
442             esdTrack->GetXYZ(pos);
443             esdTrack->GetCovarianceXYZPxPyPz(covTr);
444             esdTrack->GetESDpid(pid);
445             UInt_t selectInfo = 0;
446             if (fTrackFilter) selectInfo = fTrackFilter->IsSelected(esdTrack);
447
448             if(mcH)mcH->SelectParticle(esdTrack->GetLabel());
449             vcascade->AddDaughter(aodTrack =
450                                   new(tracks[jTracks++]) AliAODTrack(esdTrack->GetID(),
451                                                                      esdTrack->GetLabel(),
452                                                                      p,
453                                                                      kTRUE,
454                                                                      pos,
455                                                                      kFALSE,
456                                                                      covTr, 
457                                                                      (Short_t)esdTrack->GetSign(),
458                                                                      esdTrack->GetITSClusterMap(), 
459                                                                      pid,
460                                                                      vcascade,
461                                                                      kTRUE,  // check if this is right
462                                                                      vtx->UsesTrack(esdTrack->GetID()),
463                                                                      AliAODTrack::kSecondary,
464                                                                      selectInfo)
465                                   );
466             aodRefs->AddAt(aodTrack, bachelor);
467        if (esdTrack->GetSign() > 0) nPosTracks++;
468             aodTrack->ConvertAliPIDtoAODPID();
469             aodTrack->SetFlags(esdTrack->GetStatus());
470             SetAODPID(esdTrack,aodTrack,detpid,timezero);
471         }
472         else {
473 //          cerr << "Error: event " << esd->GetEventNumberInFile() << " cascade " << nCascade
474 //               << " track " << bachelor << " has already been used!" << endl;
475         }
476         
477         // Add the primary track of the cascade (if any)
478         
479     } // end of the loop on cascades
480
481     //
482     // V0s
483     //
484     
485     for (Int_t nV0 = 0; nV0 < nV0s; ++nV0) {
486         
487         if (usedV0[nV0]) continue; // skip if already added to the AOD
488         
489         AliESDv0 *v0 = esd->GetV0(nV0);
490         Int_t posFromV0 = v0->GetPindex();
491         Int_t negFromV0 = v0->GetNindex();
492         if (posFromV0 < 0 || negFromV0 < 0) continue;
493
494         // V0 selection 
495         //
496         AliESDVertex *esdVtx = new AliESDVertex(*(esd->GetPrimaryVertex()));
497         
498         AliESDtrack *esdV0Pos = esd->GetTrack(posFromV0);
499         AliESDtrack *esdV0Neg = esd->GetTrack(negFromV0);
500         TList v0objects;
501         v0objects.AddAt(v0,                      0);
502         v0objects.AddAt(esdV0Pos,                1);
503         v0objects.AddAt(esdV0Neg,                2);
504         v0objects.AddAt(esdVtx,                  3);
505         UInt_t selectV0 = 0;
506         if (fV0Filter) {
507           selectV0 = fV0Filter->IsSelected(&v0objects);
508           // this is a little awkward but otherwise the 
509           // list wants to access the pointer again when going out of scope
510           delete v0objects.RemoveAt(3);
511           if (!selectV0) 
512             continue;
513         }
514         else{
515           delete v0objects.RemoveAt(3);
516         }
517     
518         v0->GetXYZ(pos[0], pos[1], pos[2]);
519
520         if (!old) {
521             chi2 = v0->GetChi2V0(); // = chi2/NDF since NDF = 2*2-3
522             v0->GetPosCov(covVtx);
523         } else {
524             chi2 = -999.;
525             for (Int_t i = 0; i < 6; i++)  covVtx[i] = 0.;
526         }
527
528
529         AliAODVertex * vV0 = 
530           new(vertices[jVertices++]) AliAODVertex(pos,
531                                                   covVtx,
532                                                   chi2,
533                                                   primary,
534                                                   nV0,
535                                                   AliAODVertex::kV0);
536         primary->AddDaughter(vV0);
537         
538
539         Float_t  dcaPosToPrimVertexXYZ[2]   = { 999., 999.}; // ..[0] = in XY plane and ..[1] = in Z
540         Float_t  dcaNegToPrimVertexXYZ[2]   = { 999., 999.}; // ..[0] = in XY plane and ..[1] = in Z
541         Double_t dcaDaughterToPrimVertex[2] = { 999., 999.}; // ..[0] = Pos and ..[1] = Neg
542         
543         Double_t  dcaV0Daughters      = v0->GetDcaV0Daughters();
544         Double_t  dcaV0ToPrimVertex   = v0->GetD(esdVtx->GetX(),esdVtx->GetY(),esdVtx->GetZ());
545         v0->GetPPxPyPz(p_pos_atv0[0],p_pos_atv0[1],p_pos_atv0[2]); 
546         v0->GetNPxPyPz(p_neg_atv0[0],p_neg_atv0[1],p_neg_atv0[2]); 
547
548         // Add the positive tracks from the V0
549         
550
551         esdV0Pos->GetPxPyPz(p_pos);
552         esdV0Pos->GetXYZ(pos);
553         esdV0Pos->GetCovarianceXYZPxPyPz(covTr);
554         esdV0Pos->GetESDpid(pid);
555         esdV0Pos->GetImpactParameters(dcaPosToPrimVertexXYZ[0],dcaPosToPrimVertexXYZ[1]);
556         if (!usedTrack[posFromV0]) {
557             usedTrack[posFromV0] = kTRUE;
558             UInt_t selectInfo = 0;
559             if (fTrackFilter) selectInfo = fTrackFilter->IsSelected(esdV0Pos);
560             if(mcH)mcH->SelectParticle(esdV0Pos->GetLabel());
561             aodTrack = new(tracks[jTracks++]) AliAODTrack(esdV0Pos->GetID(),
562                                                           esdV0Pos->GetLabel(), 
563                                                           p_pos, 
564                                                           kTRUE,
565                                                           pos,
566                                                           kFALSE,
567                                                           covTr, 
568                                                           (Short_t)esdV0Pos->GetSign(),
569                                                           esdV0Pos->GetITSClusterMap(), 
570                                                           pid,
571                                                           vV0,
572                                                           kTRUE,  // check if this is right
573                                                           vtx->UsesTrack(esdV0Pos->GetID()),
574                                                           AliAODTrack::kSecondary,
575                                                           selectInfo);
576             aodRefs->AddAt(aodTrack,posFromV0);
577             //      if (fDebug > 0) printf("-------------------Bo: pos track from original pt %.3f \n",aodTrack->Pt());
578             if (esdV0Pos->GetSign() > 0) nPosTracks++;
579             aodTrack->ConvertAliPIDtoAODPID();
580             aodTrack->SetFlags(esdV0Pos->GetStatus());
581             SetAODPID(esdV0Pos,aodTrack,detpid,timezero);
582         }
583         else {
584             aodTrack = dynamic_cast<AliAODTrack*>(aodRefs->At(posFromV0));
585             //      if (fDebug > 0) printf("-------------------Bo pos track from refArray pt %.3f \n",aodTrack->Pt());
586         }
587         vV0->AddDaughter(aodTrack);
588     
589         // Add the negative tracks from the V0
590         
591         esdV0Neg->GetPxPyPz(p_neg);
592         esdV0Neg->GetXYZ(pos);
593         esdV0Neg->GetCovarianceXYZPxPyPz(covTr);
594         esdV0Neg->GetESDpid(pid);
595         esdV0Neg->GetImpactParameters(dcaNegToPrimVertexXYZ[0],dcaNegToPrimVertexXYZ[1]);
596         
597         if (!usedTrack[negFromV0]) {
598             usedTrack[negFromV0] = kTRUE;
599             UInt_t selectInfo = 0;
600             if (fTrackFilter) selectInfo = fTrackFilter->IsSelected(esdV0Neg);
601             if(mcH)mcH->SelectParticle(esdV0Neg->GetLabel());
602             aodTrack = new(tracks[jTracks++]) AliAODTrack(esdV0Neg->GetID(),
603                                                           esdV0Neg->GetLabel(),
604                                                           p_neg,
605                                                           kTRUE,
606                                                           pos,
607                                                           kFALSE,
608                                                           covTr, 
609                                                           (Short_t)esdV0Neg->GetSign(),
610                                                           esdV0Neg->GetITSClusterMap(), 
611                                                           pid,
612                                                           vV0,
613                                                           kTRUE,  // check if this is right
614                                                           vtx->UsesTrack(esdV0Neg->GetID()),
615                                                           AliAODTrack::kSecondary,
616                                                           selectInfo);
617             
618             aodRefs->AddAt(aodTrack,negFromV0);
619             //      if (fDebug > 0) printf("-------------------Bo: neg track from original pt %.3f \n",aodTrack->Pt());
620             if (esdV0Neg->GetSign() > 0) nPosTracks++;
621             aodTrack->ConvertAliPIDtoAODPID();
622             aodTrack->SetFlags(esdV0Neg->GetStatus());
623             SetAODPID(esdV0Neg,aodTrack,detpid,timezero);
624         }
625         else {
626             aodTrack = dynamic_cast<AliAODTrack*>(aodRefs->At(negFromV0));
627             //      if (fDebug > 0) printf("-------------------Bo neg track from refArray pt %.3f \n",aodTrack->Pt());
628         }
629         vV0->AddDaughter(aodTrack);
630         dcaDaughterToPrimVertex[0] = 
631             TMath::Sqrt(dcaPosToPrimVertexXYZ[0]*dcaPosToPrimVertexXYZ[0]
632                         +dcaPosToPrimVertexXYZ[1]*dcaPosToPrimVertexXYZ[1]);
633         dcaDaughterToPrimVertex[1] = 
634             TMath::Sqrt(dcaNegToPrimVertexXYZ[0]*dcaNegToPrimVertexXYZ[0]
635                         +dcaNegToPrimVertexXYZ[1]*dcaNegToPrimVertexXYZ[1]);
636         // add it to the V0 array as well
637         aodV0 = new(V0s[jV0s++]) 
638             AliAODv0(vV0, dcaV0Daughters, dcaV0ToPrimVertex, p_pos_atv0, p_neg_atv0, dcaDaughterToPrimVertex); // to be refined
639         // set the aod v0 on-the-fly status
640         aodV0->SetOnFlyStatus(v0->GetOnFlyStatus());
641     } 
642     V0s.Expand(jV0s);    
643     // end of the loop on V0s
644     
645     // Kinks: it is a big mess the access to the information in the kinks
646     // The loop is on the tracks in order to find the mother and daugther of each kink
647     
648     
649     for (Int_t iTrack=0; iTrack<nTracks; ++iTrack) {
650         
651         AliESDtrack * esdTrack = esd->GetTrack(iTrack);
652         
653         Int_t ikink = esdTrack->GetKinkIndex(0);
654         
655         if (ikink && nKinks) {
656             // Negative kink index: mother, positive: daughter
657             
658             // Search for the second track of the kink
659             
660             for (Int_t jTrack = iTrack+1; jTrack<nTracks; ++jTrack) {
661                 
662                 AliESDtrack * esdTrack1 = esd->GetTrack(jTrack);
663                 
664                 Int_t jkink = esdTrack1->GetKinkIndex(0);
665                 
666                 if ( TMath::Abs(ikink)==TMath::Abs(jkink) ) {
667                     
668                     // The two tracks are from the same kink
669                     
670                     if (usedKink[TMath::Abs(ikink)-1]) continue; // skip used kinks
671                     
672                     Int_t imother = -1;
673                     Int_t idaughter = -1;
674                     
675                     if (ikink<0 && jkink>0) {
676                         
677                         imother = iTrack;
678                         idaughter = jTrack;
679                     }
680                     else if (ikink>0 && jkink<0) {
681                         
682                         imother = jTrack;
683                         idaughter = iTrack;
684                     }
685                     else {
686 //                      cerr << "Error: Wrong combination of kink indexes: "
687 //                           << ikink << " " << jkink << endl;
688                         continue;
689                     }
690                     
691                     // Add the mother track if it passed primary track selection cuts
692                     
693                     AliAODTrack * mother = NULL;
694                     
695                     UInt_t selectInfo = 0;
696                     if (fTrackFilter) {
697                         selectInfo = fTrackFilter->IsSelected(esd->GetTrack(imother));
698                         if (!selectInfo) continue;
699                     }
700                     
701                     if (!usedTrack[imother]) {
702                         
703                         usedTrack[imother] = kTRUE;
704                         
705                         AliESDtrack *esdTrackM = esd->GetTrack(imother);
706                         esdTrackM->GetPxPyPz(p);
707                         esdTrackM->GetXYZ(pos);
708                         esdTrackM->GetCovarianceXYZPxPyPz(covTr);
709                         esdTrackM->GetESDpid(pid);
710                         if(mcH)mcH->SelectParticle(esdTrackM->GetLabel());
711                         mother = 
712                             new(tracks[jTracks++]) AliAODTrack(esdTrackM->GetID(),
713                                                                esdTrackM->GetLabel(),
714                                                                p,
715                                                                kTRUE,
716                                                                pos,
717                                                                kFALSE,
718                                                                covTr, 
719                                                                (Short_t)esdTrackM->GetSign(),
720                                                                esdTrackM->GetITSClusterMap(), 
721                                                                pid,
722                                                                primary,
723                                                                kTRUE, // check if this is right
724                                                                vtx->UsesTrack(esdTrack->GetID()),
725                                                                AliAODTrack::kPrimary,
726                                                                selectInfo);
727                         aodRefs->AddAt(mother, imother);
728                         
729                         if (esdTrackM->GetSign() > 0) nPosTracks++;
730                         mother->SetFlags(esdTrackM->GetStatus());
731                         mother->ConvertAliPIDtoAODPID();
732                         primary->AddDaughter(mother);
733                         mother->ConvertAliPIDtoAODPID();
734                         SetAODPID(esdTrackM,mother,detpid,timezero);
735                     }
736                     else {
737 //                      cerr << "Error: event " << esd->GetEventNumberInFile() << " kink " << TMath::Abs(ikink)-1
738 //                           << " track " << imother << " has already been used!" << endl;
739                   }
740                     
741                     // Add the kink vertex
742                     AliESDkink * kink = esd->GetKink(TMath::Abs(ikink)-1);
743                     
744                     AliAODVertex * vkink = 
745                         new(vertices[jVertices++]) AliAODVertex(kink->GetPosition(),
746                                                                 NULL,
747                                                                 0.,
748                                                                 mother,
749                                                                 esdTrack->GetID(),  // This is the track ID of the mother's track!
750                                                                 AliAODVertex::kKink);
751                     // Add the daughter track
752                   
753                     AliAODTrack * daughter = NULL;
754                     
755                     if (!usedTrack[idaughter]) {
756                         
757                         usedTrack[idaughter] = kTRUE;
758                         
759                         AliESDtrack *esdTrackD = esd->GetTrack(idaughter);
760                         esdTrackD->GetPxPyPz(p);
761                         esdTrackD->GetXYZ(pos);
762                         esdTrackD->GetCovarianceXYZPxPyPz(covTr);
763                         esdTrackD->GetESDpid(pid);
764                         selectInfo = 0;
765                         if (fTrackFilter) selectInfo = fTrackFilter->IsSelected(esdTrackD);
766                         if(mcH)mcH->SelectParticle(esdTrackD->GetLabel());
767                         daughter = 
768                             new(tracks[jTracks++]) AliAODTrack(esdTrackD->GetID(),
769                                                                esdTrackD->GetLabel(),
770                                                                p,
771                                                                kTRUE,
772                                                                pos,
773                                                                kFALSE,
774                                                                covTr, 
775                                                                (Short_t)esdTrackD->GetSign(),
776                                                                esdTrackD->GetITSClusterMap(), 
777                                                                pid,
778                                                                vkink,
779                                                                kTRUE, // check if this is right
780                                                                vtx->UsesTrack(esdTrack->GetID()),
781                                                                AliAODTrack::kSecondary,
782                                                                selectInfo);
783                         
784                         aodRefs->AddAt(daughter, idaughter);
785                         
786                         if (esdTrackD->GetSign() > 0) nPosTracks++;
787                         daughter->SetFlags(esdTrackD->GetStatus());
788                         daughter->ConvertAliPIDtoAODPID();
789                         vkink->AddDaughter(daughter);
790                         daughter->ConvertAliPIDtoAODPID();
791                        SetAODPID(esdTrackD,daughter,detpid,timezero);
792                     }
793                     else {
794 //                      cerr << "Error: event " << esd->GetEventNumberInFile() << " kink " << TMath::Abs(ikink)-1
795 //                           << " track " << idaughter << " has already been used!" << endl;
796                     }
797                 }
798             }
799         }      
800     }
801     
802   
803     // Tracks (primary and orphan)
804
805     if (fDebug > 0) printf("NUMBER OF ESD TRACKS %5d\n", nTracks);
806     
807     for (Int_t nTrack = 0; nTrack < nTracks; ++nTrack) {
808         
809         
810         if (usedTrack[nTrack]) continue;
811
812         AliESDtrack *esdTrack = esd->GetTrack(nTrack);
813         UInt_t selectInfo = 0;
814         //
815         // Track selection
816         if (fTrackFilter) {
817             selectInfo = fTrackFilter->IsSelected(esdTrack);
818             if (!selectInfo && !vtx->UsesTrack(esdTrack->GetID())) continue;
819         }
820         
821         //
822         esdTrack->GetPxPyPz(p);
823         esdTrack->GetXYZ(pos);
824         esdTrack->GetCovarianceXYZPxPyPz(covTr);
825         esdTrack->GetESDpid(pid);
826         if(mcH)mcH->SelectParticle(esdTrack->GetLabel());
827         primary->AddDaughter(aodTrack =
828                              new(tracks[jTracks++]) AliAODTrack(esdTrack->GetID(),
829                                                                 esdTrack->GetLabel(),
830                                                                 p,
831                                                                 kTRUE,
832                                                                 pos,
833                                                                 kFALSE,
834                                                                 covTr, 
835                                                                 (Short_t)esdTrack->GetSign(),
836                                                                 esdTrack->GetITSClusterMap(), 
837                                                                 pid,
838                                                                 primary,
839                                                                 kTRUE, // check if this is right
840                                                                 vtx->UsesTrack(esdTrack->GetID()),
841                                                                 AliAODTrack::kPrimary, 
842                                                                 selectInfo)
843                              );
844         aodRefs->AddAt(aodTrack, nTrack);
845         
846         if (esdTrack->GetSign() > 0) nPosTracks++;
847         aodTrack->SetFlags(esdTrack->GetStatus());
848         aodTrack->ConvertAliPIDtoAODPID();
849         SetAODPID(esdTrack,aodTrack,detpid,timezero);
850     } // end of loop on tracks
851     
852     // Update number of AOD tracks in header at the end of track loop (M.G.)
853     header->SetRefMultiplicity(jTracks);
854     header->SetRefMultiplicityPos(nPosTracks);
855     header->SetRefMultiplicityNeg(jTracks - nPosTracks);
856     if (fDebug > 0) 
857       printf("   NAODTRACKS=%d  NPOS=%d  NNEG=%d\n", jTracks, nPosTracks, jTracks - nPosTracks);
858     // Do not shrink the array of tracks - other filters may add to it (M.G)
859 //    tracks.Expand(jTracks); // remove 'empty slots' due to unwritten tracks
860   
861     // Access to the AOD container of PMD clusters
862     TClonesArray &pmdClusters = *(AODEvent()->GetPmdClusters());
863     Int_t jPmdClusters=0;
864   
865     for (Int_t iPmd = 0; iPmd < nPmdClus; ++iPmd) {
866       // file pmd clusters, to be revised!
867       AliESDPmdTrack *pmdTrack = esd->GetPmdTrack(iPmd);
868       Int_t nLabel = 0;
869       Int_t *label = 0x0;
870       Double_t posPmd[3] = { pmdTrack->GetClusterX(), pmdTrack->GetClusterY(), pmdTrack->GetClusterZ()};
871       Double_t pidPmd[9] = { 0., 0., 0., 0., 0., 0., 0., 0., 0. }; // to be revised!
872       // type not set!
873       // assoc cluster not set
874       new(pmdClusters[jPmdClusters++]) AliAODPmdCluster(iPmd, nLabel, label, pmdTrack->GetClusterADC(), posPmd, pidPmd);
875     }
876
877     // Access to the AOD container of clusters
878     TClonesArray &caloClusters = *(AODEvent()->GetCaloClusters());
879     Int_t jClusters=0;
880  
881     for (Int_t iClust=0; iClust<nCaloClus; ++iClust) {
882
883       AliESDCaloCluster * cluster = esd->GetCaloCluster(iClust);
884
885       Int_t id        = cluster->GetID();
886       Int_t nLabel    = cluster->GetNLabels();
887       TArrayI* labels = cluster->GetLabels();
888       Int_t *label = 0;
889       if (labels){
890         label = (cluster->GetLabels())->GetArray();
891         for(int i = 0;i < labels->GetSize();++i){
892           if(mcH)mcH->SelectParticle(label[i]);
893         }
894       }     
895
896       Float_t energy = cluster->E();
897       cluster->GetPosition(posF);
898       Char_t ttype = AliAODCluster::kUndef; 
899
900       if (cluster->GetClusterType() == AliESDCaloCluster::kPHOSCluster) {
901         ttype=AliAODCluster::kPHOSNeutral;
902       } 
903       else if (cluster->GetClusterType() == AliESDCaloCluster::kEMCALClusterv1) {
904         ttype = AliAODCluster::kEMCALClusterv1;
905       }
906
907       
908       AliAODCaloCluster *caloCluster = new(caloClusters[jClusters++]) AliAODCaloCluster(id,
909                                                                                         nLabel,
910                                                                                         label,
911                                                                                         energy,
912                                                                                         posF,
913                                                                                         NULL,
914                                                                                         ttype);
915       
916       caloCluster->SetCaloCluster(cluster->GetDistanceToBadChannel(),
917                                   cluster->GetClusterDisp(),
918                                   cluster->GetM20(), cluster->GetM02(),
919                                   cluster->GetEmcCpvDistance(),  
920                                   cluster->GetNExMax(),cluster->GetTOF()) ;
921
922       caloCluster->SetPIDFromESD(cluster->GetPid());
923       caloCluster->SetNCells(cluster->GetNCells());
924       caloCluster->SetCellsAbsId(cluster->GetCellsAbsId());
925       caloCluster->SetCellsAmplitudeFraction(cluster->GetCellsAmplitudeFraction());
926
927       TArrayI* matchedT =       cluster->GetTracksMatched();
928       if (matchedT && cluster->GetTrackMatched() >= 0) {        
929         for (Int_t im = 0; im < matchedT->GetSize(); im++) {
930             Int_t iESDtrack = matchedT->At(im);;
931             if (aodRefs->At(iESDtrack) != 0) {
932                 caloCluster->AddTrackMatched((AliAODTrack*)aodRefs->At(iESDtrack));
933             }
934         }
935       }
936       
937     } 
938     caloClusters.Expand(jClusters); // resize TObjArray to 'remove' slots for pseudo clusters    
939     // end of loop on calo clusters
940
941     // fill EMCAL cell info
942     if (esd->GetEMCALCells()) { // protection against missing ESD information
943       AliESDCaloCells &esdEMcells = *(esd->GetEMCALCells());
944       Int_t nEMcell = esdEMcells.GetNumberOfCells() ;
945       
946       AliAODCaloCells &aodEMcells = *(AODEvent()->GetEMCALCells());
947       aodEMcells.CreateContainer(nEMcell);
948       aodEMcells.SetType(AliAODCaloCells::kEMCAL);
949       for (Int_t iCell = 0; iCell < nEMcell; iCell++) {      
950         aodEMcells.SetCell(iCell,esdEMcells.GetCellNumber(iCell),esdEMcells.GetAmplitude(iCell));
951       }
952       aodEMcells.Sort();
953     }
954
955     // fill PHOS cell info
956     if (esd->GetPHOSCells()) { // protection against missing ESD information
957       AliESDCaloCells &esdPHcells = *(esd->GetPHOSCells());
958       Int_t nPHcell = esdPHcells.GetNumberOfCells() ;
959       
960       AliAODCaloCells &aodPHcells = *(AODEvent()->GetPHOSCells());
961       aodPHcells.CreateContainer(nPHcell);
962       aodPHcells.SetType(AliAODCaloCells::kPHOS);
963       for (Int_t iCell = 0; iCell < nPHcell; iCell++) {      
964         aodPHcells.SetCell(iCell,esdPHcells.GetCellNumber(iCell),esdPHcells.GetAmplitude(iCell));
965       }
966       aodPHcells.Sort();
967     }
968
969     // tracklets    
970     AliAODTracklets &SPDTracklets = *(AODEvent()->GetTracklets());
971     const AliMultiplicity *mult = esd->GetMultiplicity();
972     if (mult) {
973       if (mult->GetNumberOfTracklets()>0) {
974         SPDTracklets.CreateContainer(mult->GetNumberOfTracklets());
975
976         for (Int_t n=0; n<mult->GetNumberOfTracklets(); n++) {
977           if(mcH){
978             mcH->SelectParticle(mult->GetLabel(n, 0));
979             mcH->SelectParticle(mult->GetLabel(n, 1));
980           }
981           SPDTracklets.SetTracklet(n, mult->GetTheta(n), mult->GetPhi(n), mult->GetDeltaPhi(n), mult->GetLabel(n, 0),mult->GetLabel(n, 1));
982         }
983       }
984     } else {
985       //Printf("ERROR: AliMultiplicity could not be retrieved from ESD");
986     }
987
988     delete [] usedTrack;
989     delete [] usedV0;
990     delete [] usedKink;
991     delete    aodRefs;
992
993     return;
994 }
995
996
997 void AliAnalysisTaskESDfilter::SetAODPID(AliESDtrack *esdtrack, AliAODTrack *aodtrack, AliAODPid *detpid, Double_t timezero)
998 {
999   //
1000   // Setter for the raw PID detector signals
1001   //
1002
1003   if(esdtrack->Pt()>fHighPthreshold) {
1004     detpid = new AliAODPid();
1005     SetDetectorRawSignals(detpid,esdtrack,timezero);
1006     aodtrack->SetDetPID(detpid);
1007   } else {
1008     if(fPtshape){
1009       if(esdtrack->Pt()> fPtshape->GetXmin()){
1010         Double_t y = fPtshape->Eval(esdtrack->Pt())/fPtshape->Eval(fHighPthreshold);
1011         if(gRandom->Rndm(0)<1./y){
1012           detpid = new AliAODPid();
1013           SetDetectorRawSignals(detpid,esdtrack,timezero);
1014           aodtrack->SetDetPID(detpid);
1015         }//end rndm
1016       }//end if p < pmin
1017     }//end if p function
1018   }// end else
1019 }
1020
1021 void AliAnalysisTaskESDfilter::SetDetectorRawSignals(AliAODPid *aodpid, AliESDtrack *track, Double_t timezero)
1022 {
1023 //
1024 //assignment of the detector signals (AliXXXesdPID inspired)
1025 //
1026  if(!track){
1027  AliInfo("no ESD track found. .....exiting");
1028  return;
1029  }
1030
1031  aodpid->SetITSsignal(track->GetITSsignal());
1032  aodpid->SetTPCsignal(track->GetTPCsignal());
1033  //n TRD planes = 6
1034
1035  Int_t nslices = track->GetNumberOfTRDslices()*6;
1036  Double_t *trdslices = new Double_t[nslices];
1037  for(Int_t iSl =0; iSl < track->GetNumberOfTRDslices(); iSl++) {
1038      for(Int_t iPl =0; iPl<6; iPl++) trdslices[iPl*track->GetNumberOfTRDslices()+iSl] = track->GetTRDslice(iPl,iSl);
1039     }
1040
1041
1042  aodpid->SetTRDsignal(track->GetNumberOfTRDslices()*6,trdslices);
1043  Double_t times[AliAODPid::kSPECIES]; track->GetIntegratedTimes(times);
1044  aodpid->SetIntegratedTimes(times);
1045
1046  aodpid->SetTOFsignal(track->GetTOFsignal()-timezero); // to be fixed
1047  aodpid->SetHMPIDsignal(track->GetHMPIDsignal());
1048
1049 }
1050
1051 void AliAnalysisTaskESDfilter::Terminate(Option_t */*option*/)
1052 {
1053 // Terminate analysis
1054 //
1055     if (fDebug > 1) printf("AnalysisESDfilter: Terminate() \n");
1056 }
1057
1058 void  AliAnalysisTaskESDfilter::PrintMCInfo(AliStack *pStack,Int_t label){
1059   if(!pStack)return;
1060   label = TMath::Abs(label);
1061   TParticle *part = pStack->Particle(label);
1062   Printf("########################");
1063   Printf("%s:%d %d UniqueID %d PDG %d P %3.3f",(char*)__FILE__,__LINE__,label,part->GetUniqueID(),part->GetPdgCode(),part->P());
1064   part->Print();
1065   TParticle* mother = part;
1066   Int_t imo = part->GetFirstMother();
1067   Int_t nprim = pStack->GetNprimary();
1068   //  while((imo >= nprim) && (mother->GetUniqueID() == 4)) {
1069   while((imo >= nprim)) {
1070     mother =  pStack->Particle(imo);
1071     Printf("Mother %s:%d Label %d UniqueID %d PDG %d P %3.3f",(char*)__FILE__,__LINE__,imo,mother->GetUniqueID(),mother->GetPdgCode(),mother->P());
1072     mother->Print();
1073     imo =  mother->GetFirstMother();
1074   }
1075   Printf("########################");
1076 }