Updated SSD raw data QA code and first implementation of the SSD rec points QA code...
[u/mrichter/AliRoot.git] / PWG0 / base / 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$ */
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 "AliLog.h"
35
36 ClassImp(AliAnalysisTaskESDfilter)
37
38 ////////////////////////////////////////////////////////////////////////
39
40 AliAnalysisTaskESDfilter::AliAnalysisTaskESDfilter():
41     fDebug(0),
42     fTree(0x0),
43     fESD(0x0),
44     fAOD(0x0),
45     fTreeA(0x0),
46     fTrackFilter(0x0),
47     fKinkFilter(0x0),
48     fV0Filter(0x0)
49 {
50   // Default constructor
51 }
52
53 AliAnalysisTaskESDfilter::AliAnalysisTaskESDfilter(const char* name):
54     AliAnalysisTask(name, "AnalysisTaskESDfilter"),
55     fDebug(0),
56     fTree(0x0),
57     fESD(0x0),
58     fAOD(0x0),
59     fTreeA(0x0),
60     fTrackFilter(0x0),
61     fKinkFilter(0x0),
62     fV0Filter(0x0)
63 {
64   // Default constructor
65     DefineInput (0, TChain::Class());
66     DefineOutput(0, TTree::Class());
67 }
68
69 void AliAnalysisTaskESDfilter::CreateOutputObjects()
70 {
71 // Create the output container
72     if (fDebug > 1) AliInfo("CreateOutPutData() \n");
73     AliAODHandler* handler = (AliAODHandler*) ((AliAnalysisManager::GetAnalysisManager())->GetOutputEventHandler());
74     
75     fAOD   = handler->GetAOD();
76     fTreeA = handler->GetTree();
77     fTreeA->GetUserInfo()->Add(fTrackFilter);
78 }
79
80 void AliAnalysisTaskESDfilter::Init()
81 {
82     // Initialization
83     if (fDebug > 1) AliInfo("Init() \n");
84     // Call configuration file
85 }
86
87 void AliAnalysisTaskESDfilter::ConnectInputData(Option_t */*option*/)
88 {
89 // Connect the input data
90 //
91     if (fDebug > 1) AliInfo("ConnectInputData() \n");
92     // Input 
93     AliESDInputHandler* esdH = (AliESDInputHandler*) 
94         ((AliAnalysisManager::GetAnalysisManager())->GetInputEventHandler());
95     fESD = (AliESDEvent*) esdH->GetEvent();
96     fTree = esdH->GetTree();
97 }
98
99 void AliAnalysisTaskESDfilter::Exec(Option_t */*option*/)
100 {
101 // Execute analysis for current event
102 //
103     // ESD Filter analysis task executed for each event
104     AliESD* old = fESD->GetAliESDOld();
105     
106     Long64_t ientry = fTree->GetReadEntry();
107     printf("Filter: Analysing event # %5d\n", (Int_t) ientry);
108
109     // set arrays and pointers
110     Double_t pos[3];
111     Double_t p[3];
112     Double_t covVtx[6];
113     Double_t covTr[21];
114     Double_t pid[10];
115
116     for (Int_t i = 0; i < 6; i++)  covVtx[i] = 0.;
117     for (Int_t i = 0; i < 21; i++) covTr [i] = 0.;
118
119     
120   // loop over events and fill them
121   
122   // Multiplicity information needed by the header (to be revised!)
123     Int_t nTracks    = fESD->GetNumberOfTracks();
124     Int_t nPosTracks = 0;
125     for (Int_t iTrack = 0; iTrack < nTracks; ++iTrack) 
126         if (fESD->GetTrack(iTrack)->GetSign()> 0) nPosTracks++;
127     
128     // Update the header
129
130     AliAODHeader* header = fAOD->GetHeader();
131     header->SetRunNumber(fESD->GetRunNumber());
132     if (old) {
133         header->SetBunchCrossNumber(0);
134         header->SetOrbitNumber(0);
135         header->SetPeriodNumber(0);
136         header->SetEventType(0);
137         header->SetMuonMagFieldScale(-999.);
138         header->SetCentrality(-999.);
139     } else {
140         header->SetBunchCrossNumber(fESD->GetBunchCrossNumber());
141         header->SetOrbitNumber(fESD->GetOrbitNumber());
142         header->SetPeriodNumber(fESD->GetPeriodNumber());
143         header->SetEventType(fESD->GetEventType());
144         header->SetMuonMagFieldScale(-999.);
145         header->SetCentrality(-999.);
146     }
147     
148     header->SetTriggerMask(fESD->GetTriggerMask()); 
149     header->SetTriggerCluster(fESD->GetTriggerCluster());
150     header->SetMagneticField(fESD->GetMagneticField());
151     header->SetZDCN1Energy(fESD->GetZDCN1Energy());
152     header->SetZDCP1Energy(fESD->GetZDCP1Energy());
153     header->SetZDCN2Energy(fESD->GetZDCN2Energy());
154     header->SetZDCP2Energy(fESD->GetZDCP2Energy());
155     header->SetZDCEMEnergy(fESD->GetZDCEMEnergy(0),fESD->GetZDCEMEnergy(1));
156     header->SetRefMultiplicity(nTracks);
157     header->SetRefMultiplicityPos(nPosTracks);
158     header->SetRefMultiplicityNeg(nTracks - nPosTracks);
159 //
160 //    
161     Int_t nV0s      = fESD->GetNumberOfV0s();
162     Int_t nCascades = fESD->GetNumberOfCascades();
163     Int_t nKinks    = fESD->GetNumberOfKinks();
164     //    Int_t nVertices = nV0s + nCascades + nKinks + 1 /* = prim. vtx*/;
165     // Int_t nJets     = 0;
166     //Int_t nCaloClus = fESD->GetNumberOfCaloClusters();
167     //Int_t nFmdClus  = 0;
168     //Int_t nPmdClus  = fESD->GetNumberOfPmdTracks();
169     
170     printf("   NV0=%d  NCASCADES=%d  NKINKS=%d\n", nV0s, nCascades, nKinks);
171
172 //    fAOD->ResetStd(nTracks, nVertices, nV0s+nCascades, nJets, nCaloClus, nFmdClus, nPmdClus);
173
174     AliAODTrack *aodTrack;
175     
176     
177     // Array to take into account the tracks already added to the AOD
178     Bool_t * usedTrack = NULL;
179     if (nTracks>0) {
180         usedTrack = new Bool_t[nTracks];
181         for (Int_t iTrack=0; iTrack<nTracks; ++iTrack) usedTrack[iTrack]=kFALSE;
182     }
183     // Array to take into account the V0s already added to the AOD
184     Bool_t * usedV0 = NULL;
185     if (nV0s>0) {
186         usedV0 = new Bool_t[nV0s];
187         for (Int_t iV0=0; iV0<nV0s; ++iV0) usedV0[iV0]=kFALSE;
188     }
189     // Array to take into account the kinks already added to the AOD
190     Bool_t * usedKink = NULL;
191     if (nKinks>0) {
192         usedKink = new Bool_t[nKinks];
193         for (Int_t iKink=0; iKink<nKinks; ++iKink) usedKink[iKink]=kFALSE;
194     }
195     
196     // Access to the AOD container of vertices
197     TClonesArray &vertices = *(fAOD->GetVertices());
198     Int_t jVertices=0;
199     
200     // Access to the AOD container of tracks
201     TClonesArray &tracks = *(fAOD->GetTracks());
202     Int_t jTracks=0; 
203     
204     // Add primary vertex. The primary tracks will be defined
205     // after the loops on the composite objects (V0, cascades, kinks)
206     const AliESDVertex *vtx = fESD->GetPrimaryVertex();
207     
208     vtx->GetXYZ(pos); // position
209     vtx->GetCovMatrix(covVtx); //covariance matrix
210     
211     AliAODVertex * primary = new(vertices[jVertices++])
212         AliAODVertex(pos, covVtx, vtx->GetChi2toNDF(), NULL, AliAODVertex::kPrimary);
213     primary->Print();
214
215
216     // Create vertices starting from the most complex objects
217     Double_t chi2 = 0.;
218     
219     // Cascades
220     for (Int_t nCascade = 0; nCascade < nCascades; ++nCascade) {
221         AliESDcascade *cascade = fESD->GetCascade(nCascade);
222         
223         cascade->GetXYZ(pos[0], pos[1], pos[2]);
224
225         if (!old) {
226             chi2 = vtx->GetChi2toNDF();
227             cascade->GetPosCovXi(covVtx);
228         } else {
229             chi2 = -999.;
230         }
231         // Add the cascade vertex
232         AliAODVertex * vcascade = new(vertices[jVertices++]) AliAODVertex(pos,
233                                                                           covVtx,
234                                                                           chi2, // = chi2/NDF since NDF = 2*2-3 (AM)
235                                                                           primary,
236                                                                           AliAODVertex::kCascade);
237         
238         primary->AddDaughter(vcascade);
239         
240         // Add the V0 from the cascade. The ESD class have to be optimized...
241         // Now we have to search for the corresponding Vo in the list of V0s
242         // using the indeces of the positive and negative tracks
243         
244         Int_t posFromV0 = cascade->GetPindex();
245         Int_t negFromV0 = cascade->GetNindex();
246         
247       
248         AliESDv0 * v0 = 0x0;
249         Int_t indV0 = -1;
250         
251         for (Int_t iV0=0; iV0<nV0s; ++iV0) {
252             
253             v0 = fESD->GetV0(iV0);
254             Int_t posV0 = v0->GetPindex();
255             Int_t negV0 = v0->GetNindex();
256             
257             if (posV0==posFromV0 && negV0==negFromV0) {
258                 indV0 = iV0;
259                 break;
260             }
261         }
262         
263         AliAODVertex * vV0FromCascade = 0x0;
264         
265         if (indV0>-1 && !usedV0[indV0] ) {
266             
267             // the V0 exists in the array of V0s and is not used
268             
269             usedV0[indV0] = kTRUE;
270             
271             v0->GetXYZ(pos[0], pos[1], pos[2]);
272             if (!old) {
273                 chi2 = v0->GetChi2V0();
274                 v0->GetPosCov(covVtx);
275             } else {
276                 chi2 = -999.;
277             }
278
279             vV0FromCascade = new(vertices[jVertices++]) AliAODVertex(pos,
280                                                                      covVtx,
281                                                                      chi2, // = chi2/NDF since NDF = 2*2-3 (AM)
282                                                                      vcascade,
283                                                                      AliAODVertex::kV0);
284         } else {
285             
286             // the V0 doesn't exist in the array of V0s or was used
287 //          cerr << "Error: event " << fESD->GetEventNumberInFile() << " cascade " << nCascade
288 //               << " The V0 " << indV0 
289 //               << " doesn't exist in the array of V0s or was used!" << endl;
290             
291             cascade->GetXYZ(pos[0], pos[1], pos[2]);
292             
293             if (!old) {
294                 chi2 = v0->GetChi2V0();
295                 cascade->GetPosCov(covVtx);
296             } else {
297                 chi2 = -999.;
298             }
299
300             vV0FromCascade = new(vertices[jVertices++]) AliAODVertex(pos,
301                                                                      covVtx,
302                                                                      chi2, // = chi2/NDF since NDF = 2*2-3 (AM)
303                                                                      vcascade,
304                                                                      AliAODVertex::kV0);
305             vcascade->AddDaughter(vV0FromCascade);
306         }
307         
308         // Add the positive tracks from the V0
309         
310         if (posFromV0>-1 && !usedTrack[posFromV0]) {
311             
312             usedTrack[posFromV0] = kTRUE;
313             
314             AliESDtrack *esdTrack = fESD->GetTrack(posFromV0);
315             esdTrack->GetPxPyPz(p);
316             esdTrack->GetXYZ(pos);
317             esdTrack->GetCovarianceXYZPxPyPz(covTr);
318             esdTrack->GetESDpid(pid);
319             
320             vV0FromCascade->AddDaughter(aodTrack =
321                                         new(tracks[jTracks++]) AliAODTrack(esdTrack->GetID(),
322                                                                            esdTrack->GetLabel(), 
323                                                                            p, 
324                                                                            kTRUE,
325                                                                            pos,
326                                                                            kFALSE,
327                                                                            covTr, 
328                                                                            (Short_t)esdTrack->GetSign(),
329                                                                            esdTrack->GetITSClusterMap(), 
330                                                                            pid,
331                                                                            vV0FromCascade,
332                                                                            kTRUE,  // check if this is right
333                                                                            kFALSE, // check if this is right
334                                                                            AliAODTrack::kSecondary)
335                 );
336             aodTrack->ConvertAliPIDtoAODPID();
337         }
338         else {
339 //          cerr << "Error: event " << fESD->GetEventNumberInFile() << " cascade " << nCascade
340 //               << " track " << posFromV0 << " has already been used!" << endl;
341         }
342         
343         // Add the negative tracks from the V0
344         
345         if (negFromV0>-1 && !usedTrack[negFromV0]) {
346             
347             usedTrack[negFromV0] = kTRUE;
348             
349             AliESDtrack *esdTrack = fESD->GetTrack(negFromV0);
350             esdTrack->GetPxPyPz(p);
351             esdTrack->GetXYZ(pos);
352             esdTrack->GetCovarianceXYZPxPyPz(covTr);
353             esdTrack->GetESDpid(pid);
354             
355             vV0FromCascade->AddDaughter(aodTrack =
356                                         new(tracks[jTracks++]) AliAODTrack(esdTrack->GetID(),
357                                                                            esdTrack->GetLabel(),
358                                                                            p,
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                                                                            kFALSE, // check if this is right
369                                                                            AliAODTrack::kSecondary)
370                 );
371             aodTrack->ConvertAliPIDtoAODPID();
372         }
373         else {
374 //          cerr << "Error: event " << fESD->GetEventNumberInFile() << " cascade " << nCascade
375 //               << " track " << negFromV0 << " has already been used!" << endl;
376         }
377         
378         // Add the bachelor track from the cascade
379         
380         Int_t bachelor = cascade->GetBindex();
381         
382         if(bachelor>-1 && !usedTrack[bachelor]) {
383             
384             usedTrack[bachelor] = kTRUE;
385             
386             AliESDtrack *esdTrack = fESD->GetTrack(bachelor);
387             esdTrack->GetPxPyPz(p);
388             esdTrack->GetXYZ(pos);
389             esdTrack->GetCovarianceXYZPxPyPz(covTr);
390             esdTrack->GetESDpid(pid);
391             
392             vcascade->AddDaughter(aodTrack =
393                                   new(tracks[jTracks++]) AliAODTrack(esdTrack->GetID(),
394                                                                      esdTrack->GetLabel(),
395                                                                      p,
396                                                                      kTRUE,
397                                                                      pos,
398                                                                      kFALSE,
399                                                                      covTr, 
400                                                                      (Short_t)esdTrack->GetSign(),
401                                                                      esdTrack->GetITSClusterMap(), 
402                                                                      pid,
403                                                                      vcascade,
404                                                                      kTRUE,  // check if this is right
405                                                                      kFALSE, // check if this is right
406                                                                      AliAODTrack::kSecondary)
407                 );
408             aodTrack->ConvertAliPIDtoAODPID();
409         }
410         else {
411 //          cerr << "Error: event " << fESD->GetEventNumberInFile() << " cascade " << nCascade
412 //               << " track " << bachelor << " has already been used!" << endl;
413         }
414         
415         // Add the primary track of the cascade (if any)
416         
417     } // end of the loop on cascades
418    
419     // V0s
420     
421     for (Int_t nV0 = 0; nV0 < nV0s; ++nV0) {
422         
423         if (usedV0[nV0]) continue; // skip if aready added to the AOD
424         
425         AliESDv0 *v0 = fESD->GetV0(nV0);
426         
427         v0->GetXYZ(pos[0], pos[1], pos[2]);
428
429         if (!old) {
430             chi2 = v0->GetChi2V0();
431             v0->GetPosCov(covVtx);
432         } else {
433             chi2 = -999.;
434         }
435
436
437         AliAODVertex * vV0 = 
438             new(vertices[jVertices++]) AliAODVertex(pos,
439                                                     covVtx,
440                                                     chi2, // = chi2/NDF since NDF = 2*2-3
441                                                     primary,
442                                                     AliAODVertex::kV0);
443         primary->AddDaughter(vV0);
444         
445         Int_t posFromV0 = v0->GetPindex();
446         Int_t negFromV0 = v0->GetNindex();
447         
448         // Add the positive tracks from the V0
449         
450         if (posFromV0>-1 && !usedTrack[posFromV0]) {
451             
452             usedTrack[posFromV0] = kTRUE;
453             
454             AliESDtrack *esdTrack = fESD->GetTrack(posFromV0);
455             esdTrack->GetPxPyPz(p);
456             esdTrack->GetXYZ(pos);
457             esdTrack->GetCovarianceXYZPxPyPz(covTr);
458             esdTrack->GetESDpid(pid);
459             
460             vV0->AddDaughter(aodTrack =
461                              new(tracks[jTracks++]) AliAODTrack(esdTrack->GetID(),
462                                                                 esdTrack->GetLabel(), 
463                                                                 p, 
464                                                                 kTRUE,
465                                                                 pos,
466                                                                 kFALSE,
467                                                                 covTr, 
468                                                                 (Short_t)esdTrack->GetSign(),
469                                                                 esdTrack->GetITSClusterMap(), 
470                                                                 pid,
471                                                                 vV0,
472                                                                 kTRUE,  // check if this is right
473                                                                 kFALSE, // check if this is right
474                                                                 AliAODTrack::kSecondary)
475                 );
476             aodTrack->ConvertAliPIDtoAODPID();
477         }
478         else {
479 //          cerr << "Error: event " << fESD->GetEventNumberInFile() << " V0 " << nV0
480 //               << " track " << posFromV0 << " has already been used!" << endl;
481         }
482         
483         // Add the negative tracks from the V0
484         
485         if (negFromV0>-1 && !usedTrack[negFromV0]) {
486             
487             usedTrack[negFromV0] = kTRUE;
488             
489             AliESDtrack *esdTrack = fESD->GetTrack(negFromV0);
490             esdTrack->GetPxPyPz(p);
491             esdTrack->GetXYZ(pos);
492             esdTrack->GetCovarianceXYZPxPyPz(covTr);
493             esdTrack->GetESDpid(pid);
494             
495             vV0->AddDaughter(aodTrack =
496                              new(tracks[jTracks++]) AliAODTrack(esdTrack->GetID(),
497                                                                 esdTrack->GetLabel(),
498                                                                 p,
499                                                                 kTRUE,
500                                                                 pos,
501                                                                 kFALSE,
502                                                                 covTr, 
503                                                                 (Short_t)esdTrack->GetSign(),
504                                                                 esdTrack->GetITSClusterMap(), 
505                                                                 pid,
506                                                                 vV0,
507                                                                 kTRUE,  // check if this is right
508                                                                 kFALSE, // check if this is right
509                                                                 AliAODTrack::kSecondary)
510                 );
511             aodTrack->ConvertAliPIDtoAODPID();
512         }
513         else {
514 //          cerr << "Error: event " << fESD->GetEventNumberInFile() << " V0 " << nV0
515 //               << " track " << negFromV0 << " has already been used!" << endl;
516         }
517         
518     } // end of the loop on V0s
519     
520     // Kinks: it is a big mess the access to the information in the kinks
521     // The loop is on the tracks in order to find the mother and daugther of each kink
522     
523     
524     for (Int_t iTrack=0; iTrack<nTracks; ++iTrack) {
525         
526         
527         AliESDtrack * esdTrack = fESD->GetTrack(iTrack);
528         
529         Int_t ikink = esdTrack->GetKinkIndex(0);
530         
531         if (ikink && nKinks) {
532             // Negative kink index: mother, positive: daughter
533             
534             // Search for the second track of the kink
535             
536             for (Int_t jTrack = iTrack+1; jTrack<nTracks; ++jTrack) {
537                 
538                 AliESDtrack * esdTrack1 = fESD->GetTrack(jTrack);
539                 
540                 Int_t jkink = esdTrack1->GetKinkIndex(0);
541                 
542                 if ( TMath::Abs(ikink)==TMath::Abs(jkink) ) {
543                     
544                     // The two tracks are from the same kink
545                     
546                     if (usedKink[TMath::Abs(ikink)-1]) continue; // skip used kinks
547                     
548                     Int_t imother = -1;
549                     Int_t idaughter = -1;
550                     
551                     if (ikink<0 && jkink>0) {
552                         
553                         imother = iTrack;
554                         idaughter = jTrack;
555                     }
556                     else if (ikink>0 && jkink<0) {
557                         
558                         imother = jTrack;
559                         idaughter = iTrack;
560                     }
561                     else {
562 //                      cerr << "Error: Wrong combination of kink indexes: "
563 //                           << ikink << " " << jkink << endl;
564                         continue;
565                     }
566                     
567                     // Add the mother track
568                     
569                     AliAODTrack * mother = NULL;
570                     
571                     if (!usedTrack[imother]) {
572                         
573                         usedTrack[imother] = kTRUE;
574                         
575                         AliESDtrack *esdTrack = fESD->GetTrack(imother);
576                         esdTrack->GetPxPyPz(p);
577                         esdTrack->GetXYZ(pos);
578                         esdTrack->GetCovarianceXYZPxPyPz(covTr);
579                         esdTrack->GetESDpid(pid);
580                         
581                         mother = 
582                             new(tracks[jTracks++]) AliAODTrack(esdTrack->GetID(),
583                                                                esdTrack->GetLabel(),
584                                                                p,
585                                                                kTRUE,
586                                                                pos,
587                                                                kFALSE,
588                                                                covTr, 
589                                                                (Short_t)esdTrack->GetSign(),
590                                                                esdTrack->GetITSClusterMap(), 
591                                                                pid,
592                                                                primary,
593                                                                kTRUE, // check if this is right
594                                                                kTRUE, // check if this is right
595                                                                AliAODTrack::kPrimary);
596                         primary->AddDaughter(mother);
597                         mother->ConvertAliPIDtoAODPID();
598                     }
599                     else {
600 //                      cerr << "Error: event " << fESD->GetEventNumberInFile() << " kink " << TMath::Abs(ikink)-1
601 //                           << " track " << imother << " has already been used!" << endl;
602                   }
603                     
604                     // Add the kink vertex
605                     AliESDkink * kink = fESD->GetKink(TMath::Abs(ikink)-1);
606                     
607                     AliAODVertex * vkink = 
608                         new(vertices[jVertices++]) AliAODVertex(kink->GetPosition(),
609                                                                 NULL,
610                                                                 0.,
611                                                                 mother,
612                                                                 AliAODVertex::kKink);
613                     // Add the daughter track
614                   
615                     AliAODTrack * daughter = NULL;
616                     
617                     if (!usedTrack[idaughter]) {
618                         
619                         usedTrack[idaughter] = kTRUE;
620                         
621                         AliESDtrack *esdTrack = fESD->GetTrack(idaughter);
622                         esdTrack->GetPxPyPz(p);
623                         esdTrack->GetXYZ(pos);
624                         esdTrack->GetCovarianceXYZPxPyPz(covTr);
625                         esdTrack->GetESDpid(pid);
626                         
627                         daughter = 
628                             new(tracks[jTracks++]) AliAODTrack(esdTrack->GetID(),
629                                                                esdTrack->GetLabel(),
630                                                                p,
631                                                                kTRUE,
632                                                                pos,
633                                                                kFALSE,
634                                                                covTr, 
635                                                                (Short_t)esdTrack->GetSign(),
636                                                                esdTrack->GetITSClusterMap(), 
637                                                                pid,
638                                                                vkink,
639                                                                kTRUE, // check if this is right
640                                                                kTRUE, // check if this is right
641                                                                AliAODTrack::kPrimary);
642                         vkink->AddDaughter(daughter);
643                         daughter->ConvertAliPIDtoAODPID();
644                     }
645                     else {
646 //                      cerr << "Error: event " << fESD->GetEventNumberInFile() << " kink " << TMath::Abs(ikink)-1
647 //                           << " track " << idaughter << " has already been used!" << endl;
648                     }
649                 }
650             }
651         }      
652     }
653     
654   
655     // Tracks (primary and orphan)
656
657     printf("NUMBER OF TRACKS %5d\n", nTracks);
658     
659     for (Int_t nTrack = 0; nTrack < nTracks; ++nTrack) {
660         
661         
662         if (usedTrack[nTrack]) continue;
663         AliESDtrack *esdTrack = fESD->GetTrack(nTrack);
664         UInt_t selectInfo = 0;
665         //
666         // Track selection
667         if (fTrackFilter) {
668             selectInfo = fTrackFilter->IsSelected(esdTrack);
669             if (!selectInfo) continue;
670         }
671         
672         //
673         esdTrack->GetPxPyPz(p);
674         esdTrack->GetXYZ(pos);
675         esdTrack->GetCovarianceXYZPxPyPz(covTr);
676         esdTrack->GetESDpid(pid);
677         
678         Float_t impactXY, impactZ;
679         
680         esdTrack->GetImpactParameters(impactXY,impactZ);
681         
682         if (impactXY<3) {
683             // track inside the beam pipe
684             
685             primary->AddDaughter(aodTrack =
686                                  new(tracks[jTracks++]) AliAODTrack(esdTrack->GetID(),
687                                                                     esdTrack->GetLabel(),
688                                                                     p,
689                                                                     kTRUE,
690                                                                     pos,
691                                                                     kFALSE,
692                                                                     covTr, 
693                                                                     (Short_t)esdTrack->GetSign(),
694                                                                     esdTrack->GetITSClusterMap(), 
695                                                                     pid,
696                                                                     primary,
697                                                                     kTRUE, // check if this is right
698                                                                     kTRUE, // check if this is right
699                                                                     AliAODTrack::kPrimary, 
700                                                                     selectInfo)
701                 );
702             aodTrack->ConvertAliPIDtoAODPID();
703         }
704         else {
705             // outside the beam pipe: orphan track
706             aodTrack =
707                 new(tracks[jTracks++]) AliAODTrack(esdTrack->GetID(),
708                                                    esdTrack->GetLabel(),
709                                                    p,
710                                                    kTRUE,
711                                                    pos,
712                                                    kFALSE,
713                                                    covTr, 
714                                                    (Short_t)esdTrack->GetSign(),
715                                                    esdTrack->GetITSClusterMap(), 
716                                                    pid,
717                                                    NULL,
718                                                    kFALSE, // check if this is right
719                                                    kFALSE, // check if this is right
720                                                    AliAODTrack::kOrphan,
721                                                    selectInfo);
722             aodTrack->ConvertAliPIDtoAODPID();
723         }       
724     } // end of loop on tracks
725     
726     // muon tracks
727     Int_t nMuTracks = fESD->GetNumberOfMuonTracks();
728     for (Int_t nMuTrack = 0; nMuTrack < nMuTracks; ++nMuTrack) {
729         
730         AliESDMuonTrack *esdMuTrack = fESD->GetMuonTrack(nMuTrack);     
731         p[0] = esdMuTrack->Px(); 
732         p[1] = esdMuTrack->Py(); 
733         p[2] = esdMuTrack->Pz();
734         pos[0] = primary->GetX(); 
735         pos[1] = primary->GetY(); 
736         pos[2] = primary->GetZ();
737         
738         // has to be changed once the muon pid is provided by the ESD
739         for (Int_t i = 0; i < 10; pid[i++] = 0.); pid[AliAODTrack::kMuon]=1.;
740         
741         primary->AddDaughter(
742             new(tracks[jTracks++]) AliAODTrack(0, // no ID provided
743                                                0, // no label provided
744                                                p,
745                                                kTRUE,
746                                                pos,
747                                                kFALSE,
748                                                NULL, // no covariance matrix provided
749                                                (Short_t)-99, // no charge provided
750                                                0, // no ITSClusterMap
751                                                pid,
752                                                primary,
753                                                kTRUE,  // check if this is right
754                                                kTRUE,  // not used for vertex fit
755                                                AliAODTrack::kPrimary)
756             );
757     }
758   
759     delete [] usedTrack;
760     delete [] usedV0;
761     delete [] usedKink;
762
763
764     //
765     PostData(0, fTreeA);
766     return;
767 }
768
769 void AliAnalysisTaskESDfilter::Terminate(Option_t */*option*/)
770 {
771 // Terminate analysis
772 //
773     if (fDebug > 1) printf("AnalysisESDfilter: Terminate() \n");
774 }
775