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