fFlag set in AliAODtrack. (M. Oldenburg)
[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 <TFile.h>
20
21 #include "AliAnalysisTaskESDfilter.h"
22 #include "AliAnalysisManager.h"
23 #include "AliESDEvent.h"
24 #include "AliAODEvent.h"
25 #include "AliESDInputHandler.h"
26 #include "AliAODHandler.h"
27 #include "AliAnalysisFilter.h"
28 #include "AliESDtrack.h"
29 #include "AliESDMuonTrack.h"
30 #include "AliESDVertex.h"
31 #include "AliESDv0.h"
32 #include "AliESDkink.h"
33 #include "AliESDcascade.h"
34 #include "AliESDPmdTrack.h"
35 #include "AliESDCaloCluster.h"
36 #include "AliESDCaloCells.h"
37 #include "AliMultiplicity.h"
38 #include "AliLog.h"
39
40 ClassImp(AliAnalysisTaskESDfilter)
41
42 ////////////////////////////////////////////////////////////////////////
43
44 AliAnalysisTaskESDfilter::AliAnalysisTaskESDfilter():
45     AliAnalysisTaskSE(),
46     fTrackFilter(0x0),
47     fKinkFilter(0x0),
48     fV0Filter(0x0)
49 {
50   // Default constructor
51 }
52
53 AliAnalysisTaskESDfilter::AliAnalysisTaskESDfilter(const char* name):
54     AliAnalysisTaskSE(name),
55     fTrackFilter(0x0),
56     fKinkFilter(0x0),
57     fV0Filter(0x0)
58 {
59   // Constructor
60 }
61
62 void AliAnalysisTaskESDfilter::UserCreateOutputObjects()
63 {
64 // Create the output container
65     OutputTree()->GetUserInfo()->Add(fTrackFilter);
66 }
67
68 void AliAnalysisTaskESDfilter::Init()
69 {
70     // Initialization
71     if (fDebug > 1) AliInfo("Init() \n");
72     // Call configuration file
73 }
74
75
76 void AliAnalysisTaskESDfilter::UserExec(Option_t */*option*/)
77 {
78 // Execute analysis for current event
79 //
80                                             
81   Long64_t ientry = Entry();
82   printf("Filter: Analysing event # %5d\n", (Int_t) ientry);
83
84   ConvertESDtoAOD();
85 }
86
87 void AliAnalysisTaskESDfilter::ConvertESDtoAOD() {
88     // ESD Filter analysis task executed for each event
89     AliESDEvent* esd = dynamic_cast<AliESDEvent*>(InputEvent());
90     AliESD* old = esd->GetAliESDOld();
91     
92     // set arrays and pointers
93     Float_t posF[3];
94     Double_t pos[3];
95     Double_t p[3];
96     Double_t p_pos[3];
97     Double_t p_neg[3];
98     Double_t covVtx[6];
99     Double_t covTr[21];
100     Double_t pid[10];
101
102     for (Int_t i = 0; i < 6; i++)  covVtx[i] = 0.;
103     for (Int_t i = 0; i < 21; i++) covTr [i] = 0.;
104
105     
106   // loop over events and fill them
107   
108   // Multiplicity information needed by the header (to be revised!)
109     Int_t nTracks    = esd->GetNumberOfTracks();
110     Int_t nPosTracks = 0;
111     for (Int_t iTrack = 0; iTrack < nTracks; ++iTrack) 
112         if (esd->GetTrack(iTrack)->GetSign()> 0) nPosTracks++;
113     
114     // Update the header
115
116     AliAODHeader* header = AODEvent()->GetHeader();
117     header->SetRunNumber(esd->GetRunNumber());
118     if (old) {
119         header->SetBunchCrossNumber(0);
120         header->SetOrbitNumber(0);
121         header->SetPeriodNumber(0);
122         header->SetEventType(0);
123         header->SetMuonMagFieldScale(-999.); // FIXME
124         header->SetCentrality(-999.);        // FIXME
125     } else {
126         header->SetBunchCrossNumber(esd->GetBunchCrossNumber());
127         header->SetOrbitNumber(esd->GetOrbitNumber());
128         header->SetPeriodNumber(esd->GetPeriodNumber());
129         header->SetEventType(esd->GetEventType());
130         header->SetMuonMagFieldScale(-999.); // FIXME
131         header->SetCentrality(-999.);        // FIXME
132     }
133     
134     header->SetTriggerMask(esd->GetTriggerMask()); 
135     header->SetTriggerCluster(esd->GetTriggerCluster());
136     header->SetMagneticField(esd->GetMagneticField());
137     header->SetZDCN1Energy(esd->GetZDCN1Energy());
138     header->SetZDCP1Energy(esd->GetZDCP1Energy());
139     header->SetZDCN2Energy(esd->GetZDCN2Energy());
140     header->SetZDCP2Energy(esd->GetZDCP2Energy());
141     header->SetZDCEMEnergy(esd->GetZDCEMEnergy(0),esd->GetZDCEMEnergy(1));
142     header->SetRefMultiplicity(nTracks);
143     header->SetRefMultiplicityPos(nPosTracks);
144     header->SetRefMultiplicityNeg(nTracks - nPosTracks);
145 //
146 //    
147     Int_t nV0s      = esd->GetNumberOfV0s();
148     Int_t nCascades = esd->GetNumberOfCascades();
149     Int_t nKinks    = esd->GetNumberOfKinks();
150     Int_t nVertices = nV0s + 2*nCascades /*could lead to two vertices, one V0 and the Xi */+ nKinks + 1 /* = prim. vtx*/;    
151     Int_t nJets     = 0;
152     Int_t nCaloClus = esd->GetNumberOfCaloClusters();
153     Int_t nFmdClus  = 0;
154     Int_t nPmdClus  = esd->GetNumberOfPmdTracks();
155     
156     printf("   NV0=%d  NCASCADES=%d  NKINKS=%d\n", nV0s, nCascades, nKinks);
157
158     AODEvent()->ResetStd(nTracks, nVertices, nV0s+nCascades, nJets, nCaloClus, nFmdClus, nPmdClus);
159
160     AliAODTrack *aodTrack = 0x0;
161     
162     // Array to take into account the tracks already added to the AOD
163     Bool_t * usedTrack = NULL;
164     if (nTracks>0) {
165         usedTrack = new Bool_t[nTracks];
166         for (Int_t iTrack=0; iTrack<nTracks; ++iTrack) usedTrack[iTrack]=kFALSE;
167     }
168     // Array to take into account the V0s already added to the AOD
169     Bool_t * usedV0 = NULL;
170     if (nV0s>0) {
171         usedV0 = new Bool_t[nV0s];
172         for (Int_t iV0=0; iV0<nV0s; ++iV0) usedV0[iV0]=kFALSE;
173     }
174     // Array to take into account the kinks already added to the AOD
175     Bool_t * usedKink = NULL;
176     if (nKinks>0) {
177         usedKink = new Bool_t[nKinks];
178         for (Int_t iKink=0; iKink<nKinks; ++iKink) usedKink[iKink]=kFALSE;
179     }
180     
181     // Access to the AOD container of vertices
182     TClonesArray &vertices = *(AODEvent()->GetVertices());
183     Int_t jVertices=0;
184     
185     // Access to the AOD container of tracks
186     TClonesArray &tracks = *(AODEvent()->GetTracks());
187     Int_t jTracks=0; 
188     
189     // Access to the AOD container of V0s
190     TClonesArray &V0s = *(AODEvent()->GetV0s());
191     Int_t jV0s=0;
192     
193     // Add primary vertex. The primary tracks will be defined
194     // after the loops on the composite objects (V0, cascades, kinks)
195     const AliESDVertex *vtx = esd->GetPrimaryVertex();
196     
197     vtx->GetXYZ(pos); // position
198     vtx->GetCovMatrix(covVtx); //covariance matrix
199     
200     AliAODVertex * primary = new(vertices[jVertices++])
201         AliAODVertex(pos, covVtx, vtx->GetChi2toNDF(), NULL, AliAODVertex::kPrimary);
202     primary->Print();
203
204     // Create vertices starting from the most complex objects
205     Double_t chi2 = 0.;
206     
207     // Cascades
208     for (Int_t nCascade = 0; nCascade < nCascades; ++nCascade) {
209         AliESDcascade *cascade = esd->GetCascade(nCascade);
210         
211         cascade->GetXYZ(pos[0], pos[1], pos[2]);
212
213         if (!old) {
214             chi2 = cascade->GetChi2Xi(); // = chi2/NDF since NDF = 2*2-3
215             cascade->GetPosCovXi(covVtx);
216         } else {
217             chi2 = -999.;
218             for (Int_t i = 0; i < 6; i++)  covVtx[i] = 0.;
219         }
220         // Add the cascade vertex
221         AliAODVertex * vcascade = new(vertices[jVertices++]) AliAODVertex(pos,
222                                                                           covVtx,
223                                                                           chi2,
224                                                                           primary,
225                                                                           nCascade,
226                                                                           AliAODVertex::kCascade);
227         
228         primary->AddDaughter(vcascade);
229         
230         // Add the V0 from the cascade. The ESD class have to be optimized...
231         // Now we have to search for the corresponding Vo in the list of V0s
232         // using the indeces of the positive and negative tracks
233         
234         Int_t posFromV0 = cascade->GetPindex();
235         Int_t negFromV0 = cascade->GetNindex();
236         
237       
238         AliESDv0 * v0 = 0x0;
239         Int_t indV0 = -1;
240         
241         for (Int_t iV0=0; iV0<nV0s; ++iV0) {
242             
243             v0 = esd->GetV0(iV0);
244             Int_t posV0 = v0->GetPindex();
245             Int_t negV0 = v0->GetNindex();
246             
247             if (posV0==posFromV0 && negV0==negFromV0) {
248                 indV0 = iV0;
249                 break;
250             }
251         }
252         
253         AliAODVertex * vV0FromCascade = 0x0;
254         
255         if (indV0>-1 && !usedV0[indV0] ) {
256             
257             // the V0 exists in the array of V0s and is not used
258             
259             usedV0[indV0] = kTRUE;
260             
261             v0->GetXYZ(pos[0], pos[1], pos[2]);
262             if (!old) {
263                 chi2 = v0->GetChi2V0();  // = chi2/NDF since NDF = 2*2-3                             
264                 v0->GetPosCov(covVtx);
265             } else {
266                 chi2 = -999.;
267                 for (Int_t i = 0; i < 6; i++)  covVtx[i] = 0.;
268             }
269
270             vV0FromCascade = new(vertices[jVertices++]) AliAODVertex(pos,
271                                                                      covVtx,
272                                                                      chi2,
273                                                                      vcascade,
274                                                                      indV0,
275                                                                      AliAODVertex::kV0);
276         } else {
277             
278             // the V0 doesn't exist in the array of V0s or was used
279 //          cerr << "Error: event " << esd->GetEventNumberInFile() << " cascade " << nCascade
280 //               << " The V0 " << indV0 
281 //               << " doesn't exist in the array of V0s or was used!" << endl;
282             
283             cascade->GetXYZ(pos[0], pos[1], pos[2]);
284             
285             if (!old) {
286                 chi2 = v0->GetChi2V0();
287                 cascade->GetPosCov(covVtx);
288             } else {
289                 chi2 = -999.;
290                 for (Int_t i = 0; i < 6; i++)  covVtx[i] = 0.;
291             }
292
293             vV0FromCascade = new(vertices[jVertices++]) AliAODVertex(pos,
294                                                                      covVtx,
295                                                                      chi2, // = chi2/NDF since NDF = 2*2-3 (AM)
296                                                                      vcascade,
297                                                                      indV0,
298                                                                      AliAODVertex::kV0);
299             vcascade->AddDaughter(vV0FromCascade);
300         }
301         
302         // Add the positive tracks from the V0
303         
304         if (posFromV0>-1 && !usedTrack[posFromV0]) {
305             
306             usedTrack[posFromV0] = kTRUE;
307             
308             AliESDtrack *esdTrack = esd->GetTrack(posFromV0);
309             esdTrack->GetPxPyPz(p_pos);
310             esdTrack->GetXYZ(pos);
311             esdTrack->GetCovarianceXYZPxPyPz(covTr);
312             esdTrack->GetESDpid(pid);
313             
314             vV0FromCascade->AddDaughter(aodTrack =
315                                         new(tracks[jTracks++]) AliAODTrack(esdTrack->GetID(),
316                                                                            esdTrack->GetLabel(), 
317                                                                            p_pos, 
318                                                                            kTRUE,
319                                                                            pos,
320                                                                            kFALSE,
321                                                                            covTr, 
322                                                                            (Short_t)esdTrack->GetSign(),
323                                                                            esdTrack->GetITSClusterMap(), 
324                                                                            pid,
325                                                                            vV0FromCascade,
326                                                                            kTRUE,  // check if this is right
327                                                                            kFALSE, // check if this is right
328                                                                            AliAODTrack::kSecondary)
329                 );
330             aodTrack->ConvertAliPIDtoAODPID();
331             aodTrack->SetFlags(esdTrack->GetStatus());
332         }
333         else {
334 //          cerr << "Error: event " << esd->GetEventNumberInFile() << " cascade " << nCascade
335 //               << " track " << posFromV0 << " has already been used!" << endl;
336         }
337         
338         // Add the negative tracks from the V0
339         
340         if (negFromV0>-1 && !usedTrack[negFromV0]) {
341             
342             usedTrack[negFromV0] = kTRUE;
343             
344             AliESDtrack *esdTrack = esd->GetTrack(negFromV0);
345             esdTrack->GetPxPyPz(p_neg);
346             esdTrack->GetXYZ(pos);
347             esdTrack->GetCovarianceXYZPxPyPz(covTr);
348             esdTrack->GetESDpid(pid);
349             
350             vV0FromCascade->AddDaughter(aodTrack =
351                                         new(tracks[jTracks++]) AliAODTrack(esdTrack->GetID(),
352                                                                            esdTrack->GetLabel(),
353                                                                            p_neg,
354                                                                            kTRUE,
355                                                                            pos,
356                                                                            kFALSE,
357                                                                            covTr, 
358                                                                            (Short_t)esdTrack->GetSign(),
359                                                                            esdTrack->GetITSClusterMap(), 
360                                                                            pid,
361                                                                            vV0FromCascade,
362                                                                            kTRUE,  // check if this is right
363                                                                            kFALSE, // check if this is right
364                                                                            AliAODTrack::kSecondary)
365                 );
366             aodTrack->ConvertAliPIDtoAODPID();
367             aodTrack->SetFlags(esdTrack->GetStatus());      
368         }
369         else {
370 //          cerr << "Error: event " << esd->GetEventNumberInFile() << " cascade " << nCascade
371 //               << " track " << negFromV0 << " has already been used!" << endl;
372         }
373         
374         // add it to the V0 array as well
375         Double_t d0[2] = { -999., -99.};
376         // counting is probably wrong
377         new(V0s[jV0s++]) AliAODv0(vV0FromCascade, -999., -99., p_pos, p_neg, d0); // to be refined
378
379         // Add the bachelor track from the cascade
380         
381         Int_t bachelor = cascade->GetBindex();
382         
383         if(bachelor>-1 && !usedTrack[bachelor]) {
384             
385             usedTrack[bachelor] = kTRUE;
386             
387             AliESDtrack *esdTrack = esd->GetTrack(bachelor);
388             esdTrack->GetPxPyPz(p);
389             esdTrack->GetXYZ(pos);
390             esdTrack->GetCovarianceXYZPxPyPz(covTr);
391             esdTrack->GetESDpid(pid);
392             
393             vcascade->AddDaughter(aodTrack =
394                                   new(tracks[jTracks++]) AliAODTrack(esdTrack->GetID(),
395                                                                      esdTrack->GetLabel(),
396                                                                      p,
397                                                                      kTRUE,
398                                                                      pos,
399                                                                      kFALSE,
400                                                                      covTr, 
401                                                                      (Short_t)esdTrack->GetSign(),
402                                                                      esdTrack->GetITSClusterMap(), 
403                                                                      pid,
404                                                                      vcascade,
405                                                                      kTRUE,  // check if this is right
406                                                                      kFALSE, // check if this is right
407                                                                      AliAODTrack::kSecondary)
408                 );
409             aodTrack->ConvertAliPIDtoAODPID();
410             aodTrack->SetFlags(esdTrack->GetStatus());
411         }
412         else {
413 //          cerr << "Error: event " << esd->GetEventNumberInFile() << " cascade " << nCascade
414 //               << " track " << bachelor << " has already been used!" << endl;
415         }
416         
417         // Add the primary track of the cascade (if any)
418         
419     } // end of the loop on cascades
420    
421     // V0s
422     
423     for (Int_t nV0 = 0; nV0 < nV0s; ++nV0) {
424         
425         if (usedV0[nV0]) continue; // skip if aready added to the AOD
426         
427         AliESDv0 *v0 = esd->GetV0(nV0);
428         
429         v0->GetXYZ(pos[0], pos[1], pos[2]);
430
431         if (!old) {
432             chi2 = v0->GetChi2V0(); // = chi2/NDF since NDF = 2*2-3
433             v0->GetPosCov(covVtx);
434         } else {
435             chi2 = -999.;
436             for (Int_t i = 0; i < 6; i++)  covVtx[i] = 0.;
437         }
438
439
440         AliAODVertex * vV0 = 
441           new(vertices[jVertices++]) AliAODVertex(pos,
442                                                   covVtx,
443                                                   chi2,
444                                                   primary,
445                                                   nV0,
446                                                   AliAODVertex::kV0);
447         primary->AddDaughter(vV0);
448         
449         Int_t posFromV0 = v0->GetPindex();
450         Int_t negFromV0 = v0->GetNindex();
451         
452         // Add the positive tracks from the V0
453         
454         if (posFromV0>-1 && !usedTrack[posFromV0]) {
455             
456             usedTrack[posFromV0] = kTRUE;
457             
458             AliESDtrack *esdTrack = esd->GetTrack(posFromV0);
459             esdTrack->GetPxPyPz(p_pos);
460             esdTrack->GetXYZ(pos);
461             esdTrack->GetCovarianceXYZPxPyPz(covTr);
462             esdTrack->GetESDpid(pid);
463             
464             vV0->AddDaughter(aodTrack =
465                              new(tracks[jTracks++]) AliAODTrack(esdTrack->GetID(),
466                                                                 esdTrack->GetLabel(), 
467                                                                 p_pos, 
468                                                                 kTRUE,
469                                                                 pos,
470                                                                 kFALSE,
471                                                                 covTr, 
472                                                                 (Short_t)esdTrack->GetSign(),
473                                                                 esdTrack->GetITSClusterMap(), 
474                                                                 pid,
475                                                                 vV0,
476                                                                 kTRUE,  // check if this is right
477                                                                 kFALSE, // check if this is right
478                                                                 AliAODTrack::kSecondary)
479                 );
480             aodTrack->ConvertAliPIDtoAODPID();
481             aodTrack->SetFlags(esdTrack->GetStatus());
482         }
483         else {
484 //          cerr << "Error: event " << esd->GetEventNumberInFile() << " V0 " << nV0
485 //               << " track " << posFromV0 << " has already been used!" << endl;
486         }
487         
488         // Add the negative tracks from the V0
489         
490         if (negFromV0>-1 && !usedTrack[negFromV0]) {
491             
492             usedTrack[negFromV0] = kTRUE;
493             
494             AliESDtrack *esdTrack = esd->GetTrack(negFromV0);
495             esdTrack->GetPxPyPz(p_neg);
496             esdTrack->GetXYZ(pos);
497             esdTrack->GetCovarianceXYZPxPyPz(covTr);
498             esdTrack->GetESDpid(pid);
499             
500             vV0->AddDaughter(aodTrack =
501                              new(tracks[jTracks++]) AliAODTrack(esdTrack->GetID(),
502                                                                 esdTrack->GetLabel(),
503                                                                 p_neg,
504                                                                 kTRUE,
505                                                                 pos,
506                                                                 kFALSE,
507                                                                 covTr, 
508                                                                 (Short_t)esdTrack->GetSign(),
509                                                                 esdTrack->GetITSClusterMap(), 
510                                                                 pid,
511                                                                 vV0,
512                                                                 kTRUE,  // check if this is right
513                                                                 kFALSE, // check if this is right
514                                                                 AliAODTrack::kSecondary)
515                 );
516             aodTrack->ConvertAliPIDtoAODPID();
517             aodTrack->SetFlags(esdTrack->GetStatus());
518         }
519         else {
520 //          cerr << "Error: event " << esd->GetEventNumberInFile() << " V0 " << nV0
521 //               << " track " << negFromV0 << " has already been used!" << endl;
522         }
523
524         // add it to the V0 array as well
525         Double_t d0[2] = { 999., 99.};
526         new(V0s[jV0s++]) AliAODv0(vV0, 999., 99., p_pos, p_neg, d0); // to be refined
527     } 
528     V0s.Expand(jV0s);    
529     // end of the loop on V0s
530     
531     // Kinks: it is a big mess the access to the information in the kinks
532     // The loop is on the tracks in order to find the mother and daugther of each kink
533     
534     
535     for (Int_t iTrack=0; iTrack<nTracks; ++iTrack) {
536         
537         AliESDtrack * esdTrack = esd->GetTrack(iTrack);
538         
539         Int_t ikink = esdTrack->GetKinkIndex(0);
540         
541         if (ikink && nKinks) {
542             // Negative kink index: mother, positive: daughter
543             
544             // Search for the second track of the kink
545             
546             for (Int_t jTrack = iTrack+1; jTrack<nTracks; ++jTrack) {
547                 
548                 AliESDtrack * esdTrack1 = esd->GetTrack(jTrack);
549                 
550                 Int_t jkink = esdTrack1->GetKinkIndex(0);
551                 
552                 if ( TMath::Abs(ikink)==TMath::Abs(jkink) ) {
553                     
554                     // The two tracks are from the same kink
555                     
556                     if (usedKink[TMath::Abs(ikink)-1]) continue; // skip used kinks
557                     
558                     Int_t imother = -1;
559                     Int_t idaughter = -1;
560                     
561                     if (ikink<0 && jkink>0) {
562                         
563                         imother = iTrack;
564                         idaughter = jTrack;
565                     }
566                     else if (ikink>0 && jkink<0) {
567                         
568                         imother = jTrack;
569                         idaughter = iTrack;
570                     }
571                     else {
572 //                      cerr << "Error: Wrong combination of kink indexes: "
573 //                           << ikink << " " << jkink << endl;
574                         continue;
575                     }
576                     
577                     // Add the mother track
578                     
579                     AliAODTrack * mother = NULL;
580                     
581                     if (!usedTrack[imother]) {
582                         
583                         usedTrack[imother] = kTRUE;
584                         
585                         AliESDtrack *esdTrack = esd->GetTrack(imother);
586                         esdTrack->GetPxPyPz(p);
587                         esdTrack->GetXYZ(pos);
588                         esdTrack->GetCovarianceXYZPxPyPz(covTr);
589                         esdTrack->GetESDpid(pid);
590                         
591                         mother = 
592                             new(tracks[jTracks++]) AliAODTrack(esdTrack->GetID(),
593                                                                esdTrack->GetLabel(),
594                                                                p,
595                                                                kTRUE,
596                                                                pos,
597                                                                kFALSE,
598                                                                covTr, 
599                                                                (Short_t)esdTrack->GetSign(),
600                                                                esdTrack->GetITSClusterMap(), 
601                                                                pid,
602                                                                primary,
603                                                                kTRUE, // check if this is right
604                                                                kTRUE, // check if this is right
605                                                                AliAODTrack::kPrimary);
606                         aodTrack->SetFlags(esdTrack->GetStatus());
607                         aodTrack->ConvertAliPIDtoAODPID();
608                         primary->AddDaughter(mother);
609                         mother->ConvertAliPIDtoAODPID();
610                     }
611                     else {
612 //                      cerr << "Error: event " << esd->GetEventNumberInFile() << " kink " << TMath::Abs(ikink)-1
613 //                           << " track " << imother << " has already been used!" << endl;
614                   }
615                     
616                     // Add the kink vertex
617                     AliESDkink * kink = esd->GetKink(TMath::Abs(ikink)-1);
618                     
619                     AliAODVertex * vkink = 
620                         new(vertices[jVertices++]) AliAODVertex(kink->GetPosition(),
621                                                                 NULL,
622                                                                 0.,
623                                                                 mother,
624                                                                 esdTrack->GetID(),  // This is the track ID of the mother's track!
625                                                                 AliAODVertex::kKink);
626                     // Add the daughter track
627                   
628                     AliAODTrack * daughter = NULL;
629                     
630                     if (!usedTrack[idaughter]) {
631                         
632                         usedTrack[idaughter] = kTRUE;
633                         
634                         AliESDtrack *esdTrack = esd->GetTrack(idaughter);
635                         esdTrack->GetPxPyPz(p);
636                         esdTrack->GetXYZ(pos);
637                         esdTrack->GetCovarianceXYZPxPyPz(covTr);
638                         esdTrack->GetESDpid(pid);
639                         
640                         daughter = 
641                             new(tracks[jTracks++]) AliAODTrack(esdTrack->GetID(),
642                                                                esdTrack->GetLabel(),
643                                                                p,
644                                                                kTRUE,
645                                                                pos,
646                                                                kFALSE,
647                                                                covTr, 
648                                                                (Short_t)esdTrack->GetSign(),
649                                                                esdTrack->GetITSClusterMap(), 
650                                                                pid,
651                                                                vkink,
652                                                                kTRUE, // check if this is right
653                                                                kTRUE, // check if this is right
654                                                                AliAODTrack::kPrimary);
655                         aodTrack->SetFlags(esdTrack->GetStatus());
656                         aodTrack->ConvertAliPIDtoAODPID();
657                         vkink->AddDaughter(daughter);
658                         daughter->ConvertAliPIDtoAODPID();
659                     }
660                     else {
661 //                      cerr << "Error: event " << esd->GetEventNumberInFile() << " kink " << TMath::Abs(ikink)-1
662 //                           << " track " << idaughter << " has already been used!" << endl;
663                     }
664                 }
665             }
666         }      
667     }
668     
669   
670     // Tracks (primary and orphan)
671
672     printf("NUMBER OF TRACKS %5d\n", nTracks);
673     
674     for (Int_t nTrack = 0; nTrack < nTracks; ++nTrack) {
675         
676         
677         if (usedTrack[nTrack]) continue;
678
679         AliESDtrack *esdTrack = esd->GetTrack(nTrack);
680         UInt_t selectInfo = 0;
681         //
682         // Track selection
683         if (fTrackFilter) {
684             selectInfo = fTrackFilter->IsSelected(esdTrack);
685             if (!selectInfo) continue;
686         }
687         
688         //
689         esdTrack->GetPxPyPz(p);
690         esdTrack->GetXYZ(pos);
691         esdTrack->GetCovarianceXYZPxPyPz(covTr);
692         esdTrack->GetESDpid(pid);
693         
694         Float_t impactXY, impactZ;
695         
696         esdTrack->GetImpactParameters(impactXY,impactZ);
697         
698         if (impactXY<3) {
699             // track inside the beam pipe
700             
701             primary->AddDaughter(aodTrack =
702                                  new(tracks[jTracks++]) AliAODTrack(esdTrack->GetID(),
703                                                                     esdTrack->GetLabel(),
704                                                                     p,
705                                                                     kTRUE,
706                                                                     pos,
707                                                                     kFALSE,
708                                                                     covTr, 
709                                                                     (Short_t)esdTrack->GetSign(),
710                                                                     esdTrack->GetITSClusterMap(), 
711                                                                     pid,
712                                                                     primary,
713                                                                     kTRUE, // check if this is right
714                                                                     kTRUE, // check if this is right
715                                                                     AliAODTrack::kPrimary, 
716                                                                     selectInfo)
717                 );
718             aodTrack->SetFlags(esdTrack->GetStatus());
719             aodTrack->ConvertAliPIDtoAODPID();
720         }
721         else {
722           // outside the beam pipe: orphan track
723           // Don't write them anymore!
724           continue;
725           /*
726           aodTrack =
727             new(tracks[jTracks++]) AliAODTrack(esdTrack->GetID(),
728                                                esdTrack->GetLabel(),
729                                                p,
730                                                kTRUE,
731                                                pos,
732                                                kFALSE,
733                                                covTr, 
734                                                (Short_t)esdTrack->GetSign(),
735                                                esdTrack->GetITSClusterMap(), 
736                                                pid,
737                                                NULL,
738                                                kFALSE, // check if this is right
739                                                kFALSE, // check if this is right
740                                                AliAODTrack::kOrphan,
741                                                selectInfo);
742           aodTrack->SetFlags(esdTrack->GetStatus());
743           aodTrack->ConvertAliPIDtoAODPID();
744           */
745         }       
746     } // end of loop on tracks
747     
748     // muon tracks
749     Int_t nMuTracks = esd->GetNumberOfMuonTracks();
750     for (Int_t nMuTrack = 0; nMuTrack < nMuTracks; ++nMuTrack) {
751         
752         AliESDMuonTrack *esdMuTrack = esd->GetMuonTrack(nMuTrack);     
753         p[0] = esdMuTrack->Px(); 
754         p[1] = esdMuTrack->Py(); 
755         p[2] = esdMuTrack->Pz();
756         pos[0] = primary->GetX(); 
757         pos[1] = primary->GetY(); 
758         pos[2] = primary->GetZ();
759         
760         // has to be changed once the muon pid is provided by the ESD
761         for (Int_t i = 0; i < 10; pid[i++] = 0.); pid[AliAODTrack::kMuon]=1.;
762         
763         primary->AddDaughter(
764             new(tracks[jTracks++]) AliAODTrack(0, // no ID provided
765                                                0, // no label provided
766                                                p,
767                                                kTRUE,
768                                                pos,
769                                                kFALSE,
770                                                NULL, // no covariance matrix provided
771                                                (Short_t)-99, // no charge provided
772                                                0, // no ITSClusterMap
773                                                pid,
774                                                primary,
775                                                kTRUE,  // check if this is right
776                                                kTRUE,  // not used for vertex fit
777                                                AliAODTrack::kPrimary)
778             );
779   
780         aodTrack->ConvertAliPIDtoAODPID();
781         aodTrack->SetHitsPatternInTrigCh(esdMuTrack->GetHitsPatternInTrigCh());
782         Int_t track2Trigger = esdMuTrack->GetMatchTrigger();
783         aodTrack->SetMatchTrigger(track2Trigger);
784         if (track2Trigger) 
785           aodTrack->SetChi2MatchTrigger(esdMuTrack->GetChi2MatchTrigger());
786         else 
787           aodTrack->SetChi2MatchTrigger(0.);
788     }
789     tracks.Expand(jTracks); // remove 'empty slots' due to unwritten tracks
790   
791     // Access to the AOD container of PMD clusters
792     TClonesArray &pmdClusters = *(AODEvent()->GetPmdClusters());
793     Int_t jPmdClusters=0;
794   
795     for (Int_t iPmd = 0; iPmd < nPmdClus; ++iPmd) {
796       // file pmd clusters, to be revised!
797       AliESDPmdTrack *pmdTrack = esd->GetPmdTrack(iPmd);
798       Int_t nLabel = 0;
799       Int_t *label = 0x0;
800       Double_t pos[3] = { pmdTrack->GetClusterX(), pmdTrack->GetClusterY(), pmdTrack->GetClusterZ() };
801       Double_t pid[9] = { 0., 0., 0., 0., 0., 0., 0., 0., 0. }; // to be revised!
802       // type not set!
803       // assoc cluster not set
804       new(pmdClusters[jPmdClusters++]) AliAODPmdCluster(iPmd, nLabel, label, pmdTrack->GetClusterADC(), pos, pid);
805     }
806
807     // Access to the AOD container of clusters
808     TClonesArray &caloClusters = *(AODEvent()->GetCaloClusters());
809     Int_t jClusters=0;
810  
811     for (Int_t iClust=0; iClust<nCaloClus; ++iClust) {
812
813       AliESDCaloCluster * cluster = esd->GetCaloCluster(iClust);
814
815       Int_t id = cluster->GetID();
816       Int_t nLabel = 0;
817       Int_t *label = 0x0;
818       Float_t energy = cluster->E();
819       cluster->GetPosition(posF);
820       Char_t ttype=AliAODCluster::kUndef;
821
822       if (cluster->GetClusterType() == AliESDCaloCluster::kPHOSCluster) {
823         ttype=AliAODCluster::kPHOSNeutral;
824       } 
825       else if (cluster->GetClusterType() == AliESDCaloCluster::kEMCALClusterv1) {
826         ttype = AliAODCluster::kEMCALClusterv1;
827       }
828
829       
830       AliAODCaloCluster *caloCluster = new(caloClusters[jClusters++]) AliAODCaloCluster(id,
831                                                                                         nLabel,
832                                                                                         label,
833                                                                                         energy,
834                                                                                         pos,
835                                                                                         NULL,
836                                                                                         ttype);
837       
838       caloCluster->SetCaloCluster(); // to be refined!
839
840     } 
841     caloClusters.Expand(jClusters); // resize TObjArray to 'remove' slots for pseudo clusters    
842     // end of loop on calo clusters
843
844     // fill EMCAL cell info
845     if (esd->GetEMCALCells()) { // protection against missing ESD information
846       AliESDCaloCells &esdEMcells = *(esd->GetEMCALCells());
847       Int_t nEMcell = esdEMcells.GetNumberOfCells() ;
848       
849       AliAODCaloCells &aodEMcells = *(AODEvent()->GetEMCALCells());
850       aodEMcells.CreateContainer(nEMcell);
851       aodEMcells.SetType(AliAODCaloCells::kEMCAL);
852       for (Int_t iCell = 0; iCell < nEMcell; iCell++) {      
853         aodEMcells.SetCell(iCell,esdEMcells.GetCellNumber(iCell),esdEMcells.GetAmplitude(iCell));
854       }
855       aodEMcells.Sort();
856     }
857
858     // fill PHOS cell info
859     if (esd->GetPHOSCells()) { // protection against missing ESD information
860       AliESDCaloCells &esdPHcells = *(esd->GetPHOSCells());
861       Int_t nPHcell = esdPHcells.GetNumberOfCells() ;
862       
863       AliAODCaloCells &aodPHcells = *(AODEvent()->GetPHOSCells());
864       aodPHcells.CreateContainer(nPHcell);
865       aodPHcells.SetType(AliAODCaloCells::kPHOS);
866       for (Int_t iCell = 0; iCell < nPHcell; iCell++) {      
867         aodPHcells.SetCell(iCell,esdPHcells.GetCellNumber(iCell),esdPHcells.GetAmplitude(iCell));
868       }
869       aodPHcells.Sort();
870     }
871
872     // tracklets    
873     AliAODTracklets &SPDTracklets = *(AODEvent()->GetTracklets());
874     const AliMultiplicity *mult = esd->GetMultiplicity();
875     if (mult) {
876       if (mult->GetNumberOfTracklets()>0) {
877         SPDTracklets.CreateContainer(mult->GetNumberOfTracklets());
878
879         for (Int_t n=0; n<mult->GetNumberOfTracklets(); n++) {
880           SPDTracklets.SetTracklet(n, mult->GetTheta(n), mult->GetPhi(n), mult->GetDeltaPhi(n), mult->GetLabel(n));
881         }
882       }
883     } else {
884       //Printf("ERROR: AliMultiplicity could not be retrieved from ESD");
885     }
886
887     delete [] usedTrack;
888     delete [] usedV0;
889     delete [] usedKink;
890
891     return;
892 }
893
894 void AliAnalysisTaskESDfilter::Terminate(Option_t */*option*/)
895 {
896 // Terminate analysis
897 //
898     if (fDebug > 1) printf("AnalysisESDfilter: Terminate() \n");
899 }
900