- ESDfilter moved from PWG0base to ANALYSISalice
[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         }
332         else {
333 //          cerr << "Error: event " << esd->GetEventNumberInFile() << " cascade " << nCascade
334 //               << " track " << posFromV0 << " has already been used!" << endl;
335         }
336         
337         // Add the negative tracks from the V0
338         
339         if (negFromV0>-1 && !usedTrack[negFromV0]) {
340             
341             usedTrack[negFromV0] = kTRUE;
342             
343             AliESDtrack *esdTrack = esd->GetTrack(negFromV0);
344             esdTrack->GetPxPyPz(p_neg);
345             esdTrack->GetXYZ(pos);
346             esdTrack->GetCovarianceXYZPxPyPz(covTr);
347             esdTrack->GetESDpid(pid);
348             
349             vV0FromCascade->AddDaughter(aodTrack =
350                                         new(tracks[jTracks++]) AliAODTrack(esdTrack->GetID(),
351                                                                            esdTrack->GetLabel(),
352                                                                            p_neg,
353                                                                            kTRUE,
354                                                                            pos,
355                                                                            kFALSE,
356                                                                            covTr, 
357                                                                            (Short_t)esdTrack->GetSign(),
358                                                                            esdTrack->GetITSClusterMap(), 
359                                                                            pid,
360                                                                            vV0FromCascade,
361                                                                            kTRUE,  // check if this is right
362                                                                            kFALSE, // check if this is right
363                                                                            AliAODTrack::kSecondary)
364                 );
365             aodTrack->ConvertAliPIDtoAODPID();
366         }
367         else {
368 //          cerr << "Error: event " << esd->GetEventNumberInFile() << " cascade " << nCascade
369 //               << " track " << negFromV0 << " has already been used!" << endl;
370         }
371         
372         // add it to the V0 array as well
373         Double_t d0[2] = { -999., -99.};
374         // counting is probably wrong
375         new(V0s[jV0s++]) AliAODv0(vV0FromCascade, -999., -99., p_pos, p_neg, d0); // to be refined
376
377         // Add the bachelor track from the cascade
378         
379         Int_t bachelor = cascade->GetBindex();
380         
381         if(bachelor>-1 && !usedTrack[bachelor]) {
382             
383             usedTrack[bachelor] = kTRUE;
384             
385             AliESDtrack *esdTrack = esd->GetTrack(bachelor);
386             esdTrack->GetPxPyPz(p);
387             esdTrack->GetXYZ(pos);
388             esdTrack->GetCovarianceXYZPxPyPz(covTr);
389             esdTrack->GetESDpid(pid);
390             
391             vcascade->AddDaughter(aodTrack =
392                                   new(tracks[jTracks++]) AliAODTrack(esdTrack->GetID(),
393                                                                      esdTrack->GetLabel(),
394                                                                      p,
395                                                                      kTRUE,
396                                                                      pos,
397                                                                      kFALSE,
398                                                                      covTr, 
399                                                                      (Short_t)esdTrack->GetSign(),
400                                                                      esdTrack->GetITSClusterMap(), 
401                                                                      pid,
402                                                                      vcascade,
403                                                                      kTRUE,  // check if this is right
404                                                                      kFALSE, // check if this is right
405                                                                      AliAODTrack::kSecondary)
406                 );
407             aodTrack->ConvertAliPIDtoAODPID();
408         }
409         else {
410 //          cerr << "Error: event " << esd->GetEventNumberInFile() << " cascade " << nCascade
411 //               << " track " << bachelor << " has already been used!" << endl;
412         }
413         
414         // Add the primary track of the cascade (if any)
415         
416     } // end of the loop on cascades
417    
418     // V0s
419     
420     for (Int_t nV0 = 0; nV0 < nV0s; ++nV0) {
421         
422         if (usedV0[nV0]) continue; // skip if aready added to the AOD
423         
424         AliESDv0 *v0 = esd->GetV0(nV0);
425         
426         v0->GetXYZ(pos[0], pos[1], pos[2]);
427
428         if (!old) {
429             chi2 = v0->GetChi2V0(); // = chi2/NDF since NDF = 2*2-3
430             v0->GetPosCov(covVtx);
431         } else {
432             chi2 = -999.;
433             for (Int_t i = 0; i < 6; i++)  covVtx[i] = 0.;
434         }
435
436
437         AliAODVertex * vV0 = 
438           new(vertices[jVertices++]) AliAODVertex(pos,
439                                                   covVtx,
440                                                   chi2,
441                                                   primary,
442                                                   nV0,
443                                                   AliAODVertex::kV0);
444         primary->AddDaughter(vV0);
445         
446         Int_t posFromV0 = v0->GetPindex();
447         Int_t negFromV0 = v0->GetNindex();
448         
449         // Add the positive tracks from the V0
450         
451         if (posFromV0>-1 && !usedTrack[posFromV0]) {
452             
453             usedTrack[posFromV0] = kTRUE;
454             
455             AliESDtrack *esdTrack = esd->GetTrack(posFromV0);
456             esdTrack->GetPxPyPz(p_pos);
457             esdTrack->GetXYZ(pos);
458             esdTrack->GetCovarianceXYZPxPyPz(covTr);
459             esdTrack->GetESDpid(pid);
460             
461             vV0->AddDaughter(aodTrack =
462                              new(tracks[jTracks++]) AliAODTrack(esdTrack->GetID(),
463                                                                 esdTrack->GetLabel(), 
464                                                                 p_pos, 
465                                                                 kTRUE,
466                                                                 pos,
467                                                                 kFALSE,
468                                                                 covTr, 
469                                                                 (Short_t)esdTrack->GetSign(),
470                                                                 esdTrack->GetITSClusterMap(), 
471                                                                 pid,
472                                                                 vV0,
473                                                                 kTRUE,  // check if this is right
474                                                                 kFALSE, // check if this is right
475                                                                 AliAODTrack::kSecondary)
476                 );
477             aodTrack->ConvertAliPIDtoAODPID();
478         }
479         else {
480 //          cerr << "Error: event " << esd->GetEventNumberInFile() << " V0 " << nV0
481 //               << " track " << posFromV0 << " has already been used!" << endl;
482         }
483         
484         // Add the negative tracks from the V0
485         
486         if (negFromV0>-1 && !usedTrack[negFromV0]) {
487             
488             usedTrack[negFromV0] = kTRUE;
489             
490             AliESDtrack *esdTrack = esd->GetTrack(negFromV0);
491             esdTrack->GetPxPyPz(p_neg);
492             esdTrack->GetXYZ(pos);
493             esdTrack->GetCovarianceXYZPxPyPz(covTr);
494             esdTrack->GetESDpid(pid);
495             
496             vV0->AddDaughter(aodTrack =
497                              new(tracks[jTracks++]) AliAODTrack(esdTrack->GetID(),
498                                                                 esdTrack->GetLabel(),
499                                                                 p_neg,
500                                                                 kTRUE,
501                                                                 pos,
502                                                                 kFALSE,
503                                                                 covTr, 
504                                                                 (Short_t)esdTrack->GetSign(),
505                                                                 esdTrack->GetITSClusterMap(), 
506                                                                 pid,
507                                                                 vV0,
508                                                                 kTRUE,  // check if this is right
509                                                                 kFALSE, // check if this is right
510                                                                 AliAODTrack::kSecondary)
511                 );
512             aodTrack->ConvertAliPIDtoAODPID();
513         }
514         else {
515 //          cerr << "Error: event " << esd->GetEventNumberInFile() << " V0 " << nV0
516 //               << " track " << negFromV0 << " has already been used!" << endl;
517         }
518
519         // add it to the V0 array as well
520         Double_t d0[2] = { 999., 99.};
521         new(V0s[jV0s++]) AliAODv0(vV0, 999., 99., p_pos, p_neg, d0); // to be refined
522     } 
523     V0s.Expand(jV0s);    
524     // end of the loop on V0s
525     
526     // Kinks: it is a big mess the access to the information in the kinks
527     // The loop is on the tracks in order to find the mother and daugther of each kink
528     
529     
530     for (Int_t iTrack=0; iTrack<nTracks; ++iTrack) {
531         
532         AliESDtrack * esdTrack = esd->GetTrack(iTrack);
533         
534         Int_t ikink = esdTrack->GetKinkIndex(0);
535         
536         if (ikink && nKinks) {
537             // Negative kink index: mother, positive: daughter
538             
539             // Search for the second track of the kink
540             
541             for (Int_t jTrack = iTrack+1; jTrack<nTracks; ++jTrack) {
542                 
543                 AliESDtrack * esdTrack1 = esd->GetTrack(jTrack);
544                 
545                 Int_t jkink = esdTrack1->GetKinkIndex(0);
546                 
547                 if ( TMath::Abs(ikink)==TMath::Abs(jkink) ) {
548                     
549                     // The two tracks are from the same kink
550                     
551                     if (usedKink[TMath::Abs(ikink)-1]) continue; // skip used kinks
552                     
553                     Int_t imother = -1;
554                     Int_t idaughter = -1;
555                     
556                     if (ikink<0 && jkink>0) {
557                         
558                         imother = iTrack;
559                         idaughter = jTrack;
560                     }
561                     else if (ikink>0 && jkink<0) {
562                         
563                         imother = jTrack;
564                         idaughter = iTrack;
565                     }
566                     else {
567 //                      cerr << "Error: Wrong combination of kink indexes: "
568 //                           << ikink << " " << jkink << endl;
569                         continue;
570                     }
571                     
572                     // Add the mother track
573                     
574                     AliAODTrack * mother = NULL;
575                     
576                     if (!usedTrack[imother]) {
577                         
578                         usedTrack[imother] = kTRUE;
579                         
580                         AliESDtrack *esdTrack = esd->GetTrack(imother);
581                         esdTrack->GetPxPyPz(p);
582                         esdTrack->GetXYZ(pos);
583                         esdTrack->GetCovarianceXYZPxPyPz(covTr);
584                         esdTrack->GetESDpid(pid);
585                         
586                         mother = 
587                             new(tracks[jTracks++]) AliAODTrack(esdTrack->GetID(),
588                                                                esdTrack->GetLabel(),
589                                                                p,
590                                                                kTRUE,
591                                                                pos,
592                                                                kFALSE,
593                                                                covTr, 
594                                                                (Short_t)esdTrack->GetSign(),
595                                                                esdTrack->GetITSClusterMap(), 
596                                                                pid,
597                                                                primary,
598                                                                kTRUE, // check if this is right
599                                                                kTRUE, // check if this is right
600                                                                AliAODTrack::kPrimary);
601                         primary->AddDaughter(mother);
602                         mother->ConvertAliPIDtoAODPID();
603                     }
604                     else {
605 //                      cerr << "Error: event " << esd->GetEventNumberInFile() << " kink " << TMath::Abs(ikink)-1
606 //                           << " track " << imother << " has already been used!" << endl;
607                   }
608                     
609                     // Add the kink vertex
610                     AliESDkink * kink = esd->GetKink(TMath::Abs(ikink)-1);
611                     
612                     AliAODVertex * vkink = 
613                         new(vertices[jVertices++]) AliAODVertex(kink->GetPosition(),
614                                                                 NULL,
615                                                                 0.,
616                                                                 mother,
617                                                                 esdTrack->GetID(),  // This is the track ID of the mother's track!
618                                                                 AliAODVertex::kKink);
619                     // Add the daughter track
620                   
621                     AliAODTrack * daughter = NULL;
622                     
623                     if (!usedTrack[idaughter]) {
624                         
625                         usedTrack[idaughter] = kTRUE;
626                         
627                         AliESDtrack *esdTrack = esd->GetTrack(idaughter);
628                         esdTrack->GetPxPyPz(p);
629                         esdTrack->GetXYZ(pos);
630                         esdTrack->GetCovarianceXYZPxPyPz(covTr);
631                         esdTrack->GetESDpid(pid);
632                         
633                         daughter = 
634                             new(tracks[jTracks++]) AliAODTrack(esdTrack->GetID(),
635                                                                esdTrack->GetLabel(),
636                                                                p,
637                                                                kTRUE,
638                                                                pos,
639                                                                kFALSE,
640                                                                covTr, 
641                                                                (Short_t)esdTrack->GetSign(),
642                                                                esdTrack->GetITSClusterMap(), 
643                                                                pid,
644                                                                vkink,
645                                                                kTRUE, // check if this is right
646                                                                kTRUE, // check if this is right
647                                                                AliAODTrack::kPrimary);
648                         vkink->AddDaughter(daughter);
649                         daughter->ConvertAliPIDtoAODPID();
650                     }
651                     else {
652 //                      cerr << "Error: event " << esd->GetEventNumberInFile() << " kink " << TMath::Abs(ikink)-1
653 //                           << " track " << idaughter << " has already been used!" << endl;
654                     }
655                 }
656             }
657         }      
658     }
659     
660   
661     // Tracks (primary and orphan)
662
663     printf("NUMBER OF TRACKS %5d\n", nTracks);
664     
665     for (Int_t nTrack = 0; nTrack < nTracks; ++nTrack) {
666         
667         
668         if (usedTrack[nTrack]) continue;
669
670         AliESDtrack *esdTrack = esd->GetTrack(nTrack);
671         UInt_t selectInfo = 0;
672         //
673         // Track selection
674         if (fTrackFilter) {
675             selectInfo = fTrackFilter->IsSelected(esdTrack);
676             if (!selectInfo) continue;
677         }
678         
679         //
680         esdTrack->GetPxPyPz(p);
681         esdTrack->GetXYZ(pos);
682         esdTrack->GetCovarianceXYZPxPyPz(covTr);
683         esdTrack->GetESDpid(pid);
684         
685         Float_t impactXY, impactZ;
686         
687         esdTrack->GetImpactParameters(impactXY,impactZ);
688         
689         if (impactXY<3) {
690             // track inside the beam pipe
691             
692             primary->AddDaughter(aodTrack =
693                                  new(tracks[jTracks++]) AliAODTrack(esdTrack->GetID(),
694                                                                     esdTrack->GetLabel(),
695                                                                     p,
696                                                                     kTRUE,
697                                                                     pos,
698                                                                     kFALSE,
699                                                                     covTr, 
700                                                                     (Short_t)esdTrack->GetSign(),
701                                                                     esdTrack->GetITSClusterMap(), 
702                                                                     pid,
703                                                                     primary,
704                                                                     kTRUE, // check if this is right
705                                                                     kTRUE, // check if this is right
706                                                                     AliAODTrack::kPrimary, 
707                                                                     selectInfo)
708                 );
709             aodTrack->ConvertAliPIDtoAODPID();
710         }
711         else {
712           // outside the beam pipe: orphan track
713           // Don't write them anymore!
714           continue;
715           /*
716           aodTrack =
717             new(tracks[jTracks++]) AliAODTrack(esdTrack->GetID(),
718                                                esdTrack->GetLabel(),
719                                                p,
720                                                kTRUE,
721                                                pos,
722                                                kFALSE,
723                                                covTr, 
724                                                (Short_t)esdTrack->GetSign(),
725                                                esdTrack->GetITSClusterMap(), 
726                                                pid,
727                                                NULL,
728                                                kFALSE, // check if this is right
729                                                kFALSE, // check if this is right
730                                                AliAODTrack::kOrphan,
731                                                selectInfo);
732           aodTrack->ConvertAliPIDtoAODPID();
733           */
734         }       
735     } // end of loop on tracks
736     
737     // muon tracks
738     Int_t nMuTracks = esd->GetNumberOfMuonTracks();
739     for (Int_t nMuTrack = 0; nMuTrack < nMuTracks; ++nMuTrack) {
740         
741         AliESDMuonTrack *esdMuTrack = esd->GetMuonTrack(nMuTrack);     
742         p[0] = esdMuTrack->Px(); 
743         p[1] = esdMuTrack->Py(); 
744         p[2] = esdMuTrack->Pz();
745         pos[0] = primary->GetX(); 
746         pos[1] = primary->GetY(); 
747         pos[2] = primary->GetZ();
748         
749         // has to be changed once the muon pid is provided by the ESD
750         for (Int_t i = 0; i < 10; pid[i++] = 0.); pid[AliAODTrack::kMuon]=1.;
751         
752         primary->AddDaughter(
753             new(tracks[jTracks++]) AliAODTrack(0, // no ID provided
754                                                0, // no label provided
755                                                p,
756                                                kTRUE,
757                                                pos,
758                                                kFALSE,
759                                                NULL, // no covariance matrix provided
760                                                (Short_t)-99, // no charge provided
761                                                0, // no ITSClusterMap
762                                                pid,
763                                                primary,
764                                                kTRUE,  // check if this is right
765                                                kTRUE,  // not used for vertex fit
766                                                AliAODTrack::kPrimary)
767             );
768   
769         aodTrack->SetHitsPatternInTrigCh(esdMuTrack->GetHitsPatternInTrigCh());
770         Int_t track2Trigger = esdMuTrack->GetMatchTrigger();
771         aodTrack->SetMatchTrigger(track2Trigger);
772         if (track2Trigger) 
773           aodTrack->SetChi2MatchTrigger(esdMuTrack->GetChi2MatchTrigger());
774         else 
775           aodTrack->SetChi2MatchTrigger(0.);
776     }
777     tracks.Expand(jTracks); // remove 'empty slots' due to unwritten tracks
778   
779     // Access to the AOD container of PMD clusters
780     TClonesArray &pmdClusters = *(AODEvent()->GetPmdClusters());
781     Int_t jPmdClusters=0;
782   
783     for (Int_t iPmd = 0; iPmd < nPmdClus; ++iPmd) {
784       // file pmd clusters, to be revised!
785       AliESDPmdTrack *pmdTrack = esd->GetPmdTrack(iPmd);
786       Int_t nLabel = 0;
787       Int_t *label = 0x0;
788       Double_t pos[3] = { pmdTrack->GetClusterX(), pmdTrack->GetClusterY(), pmdTrack->GetClusterZ() };
789       Double_t pid[9] = { 0., 0., 0., 0., 0., 0., 0., 0., 0. }; // to be revised!
790       // type not set!
791       // assoc cluster not set
792       new(pmdClusters[jPmdClusters++]) AliAODPmdCluster(iPmd, nLabel, label, pmdTrack->GetClusterADC(), pos, pid);
793     }
794
795     // Access to the AOD container of clusters
796     TClonesArray &caloClusters = *(AODEvent()->GetCaloClusters());
797     Int_t jClusters=0;
798  
799     for (Int_t iClust=0; iClust<nCaloClus; ++iClust) {
800
801       AliESDCaloCluster * cluster = esd->GetCaloCluster(iClust);
802
803       Int_t id = cluster->GetID();
804       Int_t nLabel = 0;
805       Int_t *label = 0x0;
806       Float_t energy = cluster->E();
807       cluster->GetPosition(posF);
808       Char_t ttype=AliAODCluster::kUndef;
809
810       if (cluster->GetClusterType() == AliESDCaloCluster::kPHOSCluster) {
811         ttype=AliAODCluster::kPHOSNeutral;
812       } 
813       else if (cluster->GetClusterType() == AliESDCaloCluster::kEMCALClusterv1) {
814         ttype = AliAODCluster::kEMCALClusterv1;
815       }
816
817       
818       AliAODCaloCluster *caloCluster = new(caloClusters[jClusters++]) AliAODCaloCluster(id,
819                                                                                         nLabel,
820                                                                                         label,
821                                                                                         energy,
822                                                                                         pos,
823                                                                                         NULL,
824                                                                                         ttype);
825       
826       caloCluster->SetCaloCluster(); // to be refined!
827
828     } 
829     caloClusters.Expand(jClusters); // resize TObjArray to 'remove' slots for pseudo clusters    
830     // end of loop on calo clusters
831
832     // fill EMCAL cell info
833     if (esd->GetEMCALCells()) { // protection against missing ESD information
834       AliESDCaloCells &esdEMcells = *(esd->GetEMCALCells());
835       Int_t nEMcell = esdEMcells.GetNumberOfCells() ;
836       
837       AliAODCaloCells &aodEMcells = *(AODEvent()->GetEMCALCells());
838       aodEMcells.CreateContainer(nEMcell);
839       aodEMcells.SetType(AliAODCaloCells::kEMCAL);
840       for (Int_t iCell = 0; iCell < nEMcell; iCell++) {      
841         aodEMcells.SetCell(iCell,esdEMcells.GetCellNumber(iCell),esdEMcells.GetAmplitude(iCell));
842       }
843       aodEMcells.Sort();
844     }
845
846     // fill PHOS cell info
847     if (esd->GetPHOSCells()) { // protection against missing ESD information
848       AliESDCaloCells &esdPHcells = *(esd->GetPHOSCells());
849       Int_t nPHcell = esdPHcells.GetNumberOfCells() ;
850       
851       AliAODCaloCells &aodPHcells = *(AODEvent()->GetPHOSCells());
852       aodPHcells.CreateContainer(nPHcell);
853       aodPHcells.SetType(AliAODCaloCells::kPHOS);
854       for (Int_t iCell = 0; iCell < nPHcell; iCell++) {      
855         aodPHcells.SetCell(iCell,esdPHcells.GetCellNumber(iCell),esdPHcells.GetAmplitude(iCell));
856       }
857       aodPHcells.Sort();
858     }
859
860     // tracklets    
861     AliAODTracklets &SPDTracklets = *(AODEvent()->GetTracklets());
862     const AliMultiplicity *mult = esd->GetMultiplicity();
863     if (mult) {
864       if (mult->GetNumberOfTracklets()>0) {
865         SPDTracklets.CreateContainer(mult->GetNumberOfTracklets());
866
867         for (Int_t n=0; n<mult->GetNumberOfTracklets(); n++) {
868           SPDTracklets.SetTracklet(n, mult->GetTheta(n), mult->GetPhi(n), mult->GetDeltaPhi(n), mult->GetLabel(n));
869         }
870       }
871     } else {
872       //Printf("ERROR: AliMultiplicity could not be retrieved from ESD");
873     }
874
875     delete [] usedTrack;
876     delete [] usedV0;
877     delete [] usedKink;
878
879     return;
880 }
881
882 void AliAnalysisTaskESDfilter::Terminate(Option_t */*option*/)
883 {
884 // Terminate analysis
885 //
886     if (fDebug > 1) printf("AnalysisESDfilter: Terminate() \n");
887 }
888