]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliESDEvent.cxx
Summary of all changes:
[u/mrichter/AliRoot.git] / STEER / AliESDEvent.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 //-----------------------------------------------------------------
19 //           Implementation of the AliESDEvent class
20 //   This is the class to deal with during the physics analysis of data.
21 //   It also ensures the backward compatibility with the old ESD format.
22 /*
23    AliESDEvent *ev= new AliESDEvent();
24    ev->ReadFromTree(esdTree);
25    ...
26     for (Int_t i=0; i<nev; i++) {
27       esdTree->GetEntry(i);
28       if(ev->GetAliESDOld())ev->CopyFromOldESD();
29 */
30 //   The AliESDInputHAndler does this automatically for you
31 //
32 // Origin: Christian Klein-Boesing, CERN, Christian.Klein-Boesing@cern.ch
33 //-----------------------------------------------------------------
34
35 #include "TList.h"
36 #include "TRefArray.h"
37 #include <TNamed.h>
38
39 #include "AliESDEvent.h"
40 #include "AliESDfriend.h"
41 #include "AliESDVZERO.h"
42 #include "AliESDFMD.h"
43 #include "AliESD.h"
44 #include "AliESDMuonTrack.h"
45 #include "AliESDPmdTrack.h"
46 #include "AliESDTrdTrack.h"
47 #include "AliESDVertex.h"
48 #include "AliESDcascade.h"
49 #include "AliESDPmdTrack.h"
50 #include "AliESDTrdTrack.h"
51 #include "AliESDVertex.h"
52 #include "AliESDcascade.h"
53 #include "AliESDkink.h"
54 #include "AliESDtrack.h"
55 #include "AliESDHLTtrack.h"
56 #include "AliESDCaloCluster.h"
57 #include "AliESDCaloCells.h"
58 #include "AliESDv0.h"
59 #include "AliESDFMD.h"
60 #include "AliESDVZERO.h"
61 #include "AliMultiplicity.h"
62 #include "AliRawDataErrorLog.h"
63 #include "AliLog.h"
64
65 ClassImp(AliESDEvent)
66
67
68
69 // here we define the names, some classes are no TNamed, therefore the classnames 
70 // are the Names
71   const char* AliESDEvent::fgkESDListName[kESDListN] = {"AliESDRun",
72                                                        "AliESDHeader",
73                                                        "AliESDZDC",
74                                                        "AliESDFMD",
75                                                        "AliESDVZERO",
76                                                        "AliESDTZERO",
77                                                        "SPDVertex",
78                                                        "PrimaryVertex",
79                                                        "AliMultiplicity",
80                                                        "PHOSTrigger",
81                                                        "EMCALTrigger",
82                                                        "Tracks",
83                                                        "MuonTracks",
84                                                        "PmdTracks",
85                                                        "TrdTracks",
86                                                        "V0s",
87                                                        "Cascades",
88                                                        "Kinks",
89                                                        "CaloClusters",
90                                                       "EMCALCells",
91                                                       "PHOSCells",
92                                                        "AliRawDataErrorLogs"};
93 //______________________________________________________________________________
94 AliESDEvent::AliESDEvent():
95   AliVEvent(),
96   fESDObjects(new TList()),
97   fESDRun(0),
98   fHeader(0),
99   fESDZDC(0),
100   fESDFMD(0),
101   fESDVZERO(0),
102   fESDTZERO(0),
103   fSPDVertex(0),
104   fPrimaryVertex(0),
105   fSPDMult(0),
106   fPHOSTrigger(0),
107   fEMCALTrigger(0),
108   fTracks(0),
109   fMuonTracks(0),
110   fPmdTracks(0),
111   fTrdTracks(0),
112   fV0s(0),  
113   fCascades(0),
114   fKinks(0),
115   fCaloClusters(0),
116   fEMCALCells(0), fPHOSCells(0),
117   fErrorLogs(0),
118   fESDOld(0),
119   fESDFriendOld(0),
120   fConnected(kFALSE),
121   fEMCALClusters(0), 
122   fFirstEMCALCluster(-1),
123   fPHOSClusters(0), 
124   fFirstPHOSCluster(-1)
125 {
126 }
127 //______________________________________________________________________________
128 AliESDEvent::AliESDEvent(const AliESDEvent& esd):
129   AliVEvent(esd),
130   fESDObjects(new TList()),
131   fESDRun(new AliESDRun(*esd.fESDRun)),
132   fHeader(new AliESDHeader(*esd.fHeader)),
133   fESDZDC(new AliESDZDC(*esd.fESDZDC)),
134   fESDFMD(new AliESDFMD(*esd.fESDFMD)),
135   fESDVZERO(new AliESDVZERO(*esd.fESDVZERO)),
136   fESDTZERO(new AliESDTZERO(*esd.fESDTZERO)),
137   fSPDVertex(new AliESDVertex(*esd.fSPDVertex)),
138   fPrimaryVertex(new AliESDVertex(*esd.fPrimaryVertex)),
139   fSPDMult(new AliMultiplicity(*esd.fSPDMult)),
140   fPHOSTrigger(new AliESDCaloTrigger(*esd.fPHOSTrigger)),
141   fEMCALTrigger(new AliESDCaloTrigger(*esd.fEMCALTrigger)),
142   fTracks(new TClonesArray(*esd.fTracks)),
143   fMuonTracks(new TClonesArray(*esd.fMuonTracks)),
144   fPmdTracks(new TClonesArray(*esd.fPmdTracks)),
145   fTrdTracks(new TClonesArray(*esd.fTrdTracks)),
146   fV0s(new TClonesArray(*esd.fV0s)),  
147   fCascades(new TClonesArray(*esd.fCascades)),
148   fKinks(new TClonesArray(*esd.fKinks)),
149   fCaloClusters(new TClonesArray(*esd.fCaloClusters)),
150   fEMCALCells(new AliESDCaloCells(*esd.fEMCALCells)),
151   fPHOSCells(new AliESDCaloCells(*esd.fPHOSCells)),
152   fErrorLogs(new TClonesArray(*esd.fErrorLogs)),
153   fESDOld(new AliESD(*esd.fESDOld)),
154   fESDFriendOld(new AliESDfriend(*esd.fESDFriendOld)),
155   fConnected(esd.fConnected),
156   fEMCALClusters(esd.fEMCALClusters), 
157   fFirstEMCALCluster(esd.fFirstEMCALCluster),
158   fPHOSClusters(esd.fPHOSClusters), 
159   fFirstPHOSCluster(esd.fFirstPHOSCluster)
160
161 {
162   // CKB init in the constructor list and only add here ...
163   AddObject(fESDRun);
164   AddObject(fHeader);
165   AddObject(fESDZDC);
166   AddObject(fESDFMD);
167   AddObject(fESDVZERO);
168   AddObject(fESDTZERO);
169   AddObject(fSPDVertex);
170   AddObject(fPrimaryVertex);
171   AddObject(fSPDMult);
172   AddObject(fPHOSTrigger);
173   AddObject(fEMCALTrigger);
174   AddObject(fTracks);
175   AddObject(fMuonTracks);
176   AddObject(fPmdTracks);
177   AddObject(fTrdTracks);
178   AddObject(fV0s);
179   AddObject(fCascades);
180   AddObject(fKinks);
181   AddObject(fCaloClusters);
182   AddObject(fEMCALCells);
183   AddObject(fPHOSCells);
184   AddObject(fErrorLogs);
185
186   GetStdContent();
187
188 }
189
190 //______________________________________________________________________________
191 AliESDEvent & AliESDEvent::operator=(const AliESDEvent& source) {
192
193   // Assignment operator
194
195   if(&source == this) return *this;
196   AliVEvent::operator=(source);
197
198   fESDRun = new AliESDRun(*source.fESDRun);
199   fHeader = new AliESDHeader(*source.fHeader);
200   fESDZDC = new AliESDZDC(*source.fESDZDC);
201   fESDFMD = new AliESDFMD(*source.fESDFMD);
202   fESDVZERO = new AliESDVZERO(*source.fESDVZERO);
203   fESDTZERO = new AliESDTZERO(*source.fESDTZERO);
204   fSPDVertex = new AliESDVertex(*source.fSPDVertex);
205   fPrimaryVertex = new AliESDVertex(*source.fPrimaryVertex);
206   fSPDMult = new AliMultiplicity(*source.fSPDMult);
207   fPHOSTrigger = new AliESDCaloTrigger(*source.fPHOSTrigger);
208   fEMCALTrigger = new AliESDCaloTrigger(*source.fEMCALTrigger);
209   fTracks = new TClonesArray(*source.fTracks);
210   fMuonTracks = new TClonesArray(*source.fMuonTracks);
211   fPmdTracks = new TClonesArray(*source.fPmdTracks);
212   fTrdTracks = new TClonesArray(*source.fTrdTracks);
213   fV0s = new TClonesArray(*source.fV0s);
214   fCascades = new TClonesArray(*source.fCascades);
215   fKinks = new TClonesArray(*source.fKinks);
216   fCaloClusters = new TClonesArray(*source.fCaloClusters);
217   fEMCALCells = new AliESDCaloCells(*source.fEMCALCells);
218   fPHOSCells = new AliESDCaloCells(*source.fPHOSCells);
219   fErrorLogs = new TClonesArray(*source.fErrorLogs);
220   fESDOld       = new AliESD(*source.fESDOld);
221   fESDFriendOld = new AliESDfriend(*source.fESDFriendOld);
222   // CKB this way?? or 
223   // or AddObject(  fESDZDC = new AliESDZDC(*source.fESDZDC));
224
225   fESDObjects = new TList();
226   AddObject(fESDRun);
227   AddObject(fHeader);
228   AddObject(fESDZDC);
229   AddObject(fESDFMD);
230   AddObject(fESDVZERO);
231   AddObject(fESDTZERO);
232   AddObject(fSPDVertex);
233   AddObject(fPrimaryVertex);
234   AddObject(fSPDMult);
235   AddObject(fPHOSTrigger);
236   AddObject(fEMCALTrigger);
237   AddObject(fTracks);
238   AddObject(fMuonTracks);
239   AddObject(fPmdTracks);
240   AddObject(fTrdTracks);
241   AddObject(fV0s);
242   AddObject(fCascades);
243   AddObject(fKinks);
244   AddObject(fCaloClusters);
245   AddObject(fEMCALCells);
246   AddObject(fPHOSCells);
247   AddObject(fErrorLogs);
248
249   fConnected = source.fConnected;
250   fEMCALClusters = source.fEMCALClusters;
251   fFirstEMCALCluster = source.fFirstEMCALCluster;
252   fPHOSClusters = source.fPHOSClusters;
253   fFirstPHOSCluster = source.fFirstPHOSCluster;
254
255
256
257   return *this;
258
259 }
260
261
262 //______________________________________________________________________________
263 AliESDEvent::~AliESDEvent()
264 {
265   //
266   // Standard destructor
267   //
268
269   // everthing on the list gets deleted automatically
270
271   
272   if(fESDObjects&&!fConnected)
273     {
274       delete fESDObjects;
275       fESDObjects = 0;
276     }
277
278   
279 }
280
281 //______________________________________________________________________________
282 void AliESDEvent::Reset()
283 {
284
285   
286   // Reset the standard contents
287   ResetStdContent(); 
288   if(fESDOld)fESDOld->Reset();
289   // call reset for user supplied data?
290 }
291
292 void AliESDEvent::ResetStdContent()
293 {
294   // Reset the standard contents
295   if(fESDRun) fESDRun->Reset();
296   if(fHeader) fHeader->Reset();
297   if(fESDZDC) fESDZDC->Reset();
298   if(fESDFMD) fESDFMD->Clear(); // why clear.... need consistend names
299   if(fESDVZERO){
300     // reset by callin d'to /c'tor keep the pointer
301     fESDVZERO->~AliESDVZERO();
302     new (fESDVZERO) AliESDVZERO();
303   }  
304   if(fESDTZERO) fESDTZERO->Reset(); 
305   // CKB no clear/reset implemented
306   if(fSPDVertex){
307     fSPDVertex->~AliESDVertex();
308     new (fSPDVertex) AliESDVertex();
309     fSPDVertex->SetName(fgkESDListName[kSPDVertex]);
310   }
311   if(fPrimaryVertex){
312     fPrimaryVertex->~AliESDVertex();
313     new (fPrimaryVertex) AliESDVertex();
314     fPrimaryVertex->SetName(fgkESDListName[kPrimaryVertex]);
315   }
316   if(fSPDMult){
317     fSPDMult->~AliMultiplicity();
318     new (fSPDMult) AliMultiplicity();
319   }
320   if(fPHOSTrigger)fPHOSTrigger->Reset(); 
321   if(fEMCALTrigger)fEMCALTrigger->Reset(); 
322   if(fTracks)fTracks->Delete();
323   if(fMuonTracks)fMuonTracks->Delete();
324   if(fPmdTracks)fPmdTracks->Delete();
325   if(fTrdTracks)fTrdTracks->Delete();
326   if(fV0s)fV0s->Delete();
327   if(fCascades)fCascades->Delete();
328   if(fKinks)fKinks->Delete();
329   if(fCaloClusters)fCaloClusters->Delete();
330   if(fPHOSCells)fPHOSCells->DeleteContainer();
331   if(fEMCALCells)fEMCALCells->DeleteContainer();
332   if(fErrorLogs) fErrorLogs->Delete();
333
334   // don't reset fconnected fConnected ;
335
336   fEMCALClusters=0; 
337   fFirstEMCALCluster=-1; 
338   fPHOSClusters=0; 
339   fFirstPHOSCluster=-1; 
340 }
341
342
343 Int_t AliESDEvent::AddV0(const AliESDv0 *v) {
344   //
345   // Add V0
346   //
347   TClonesArray &fv = *fV0s;
348   Int_t idx=fV0s->GetEntriesFast();
349   new(fv[idx]) AliESDv0(*v);
350   return idx;
351 }  
352
353 //______________________________________________________________________________
354 void AliESDEvent::Print(Option_t *) const 
355 {
356   //
357   // Print header information of the event
358   //
359   printf("ESD run information\n");
360   printf("Event # in file %d Bunch crossing # %d Orbit # %d Period # %d Run # %d Trigger %lld Magnetic field %f \n",
361          GetEventNumberInFile(),
362          GetBunchCrossNumber(),
363          GetOrbitNumber(),
364          GetPeriodNumber(),
365          GetRunNumber(),
366          GetTriggerMask(),
367          GetMagneticField() );
368   printf("Vertex: (%.4f +- %.4f, %.4f +- %.4f, %.4f +- %.4f) cm\n",
369            fPrimaryVertex->GetXv(), fPrimaryVertex->GetXRes(),
370            fPrimaryVertex->GetYv(), fPrimaryVertex->GetYRes(),
371            fPrimaryVertex->GetZv(), fPrimaryVertex->GetZRes());
372     printf("Mean vertex in RUN: X=%.4f Y=%.4f cm\n",
373            GetDiamondX(),GetDiamondY());
374     printf("SPD Multiplicity. Number of tracklets %d \n",
375            fSPDMult->GetNumberOfTracklets());
376   printf("Number of tracks: \n");
377   printf("                 charged   %d\n", GetNumberOfTracks());
378   printf("                 muon      %d\n", GetNumberOfMuonTracks());
379   printf("                 pmd       %d\n", GetNumberOfPmdTracks());
380   printf("                 trd       %d\n", GetNumberOfTrdTracks());
381   printf("                 v0        %d\n", GetNumberOfV0s());
382   printf("                 cascades  %d\n", GetNumberOfCascades());
383   printf("                 kinks     %d\n", GetNumberOfKinks());
384   printf("                 PHOSCells %d\n", fPHOSCells->GetNumberOfCells());
385   printf("                 EMCALCells %d\n", fEMCALCells->GetNumberOfCells());
386   printf("                 CaloClusters %d\n", GetNumberOfCaloClusters());
387   printf("                 phos      %d\n", GetNumberOfPHOSClusters());
388   printf("                 emcal     %d\n", GetNumberOfEMCALClusters());
389   printf("                 FMD       %s\n", (fESDFMD ? "yes" : "no"));
390   printf("                 VZERO     %s\n", (fESDVZERO ? "yes" : "no"));
391
392   return;
393 }
394
395 void AliESDEvent::SetESDfriend(const AliESDfriend *ev) {
396   //
397   // Attaches the complementary info to the ESD
398   //
399   if (!ev) return;
400
401   // to be sure that we set the tracks also
402   // in case of old esds 
403   // if(fESDOld)CopyFromOldESD();
404
405   Int_t ntrk=ev->GetNumberOfTracks();
406  
407   for (Int_t i=0; i<ntrk; i++) {
408     const AliESDfriendTrack *f=ev->GetTrack(i);
409     GetTrack(i)->SetFriendTrack(f);
410   }
411 }
412
413 Bool_t  AliESDEvent::RemoveKink(Int_t rm) {
414   // ---------------------------------------------------------
415   // Remove a kink candidate and references to it from ESD,
416   // if this candidate does not come from a reconstructed decay
417   // Not yet implemented...
418   // ---------------------------------------------------------
419   Int_t last=GetNumberOfKinks()-1;
420   if ((rm<0)||(rm>last)) return kFALSE;
421
422   return kTRUE;
423 }
424
425 Bool_t  AliESDEvent::RemoveV0(Int_t rm) {
426   // ---------------------------------------------------------
427   // Remove a V0 candidate and references to it from ESD,
428   // if this candidate does not come from a reconstructed decay
429   // ---------------------------------------------------------
430   Int_t last=GetNumberOfV0s()-1;
431   if ((rm<0)||(rm>last)) return kFALSE;
432
433   AliESDv0 *v0=GetV0(rm);
434   Int_t idxP=v0->GetPindex(), idxN=v0->GetNindex();
435
436   v0=GetV0(last);
437   Int_t lastIdxP=v0->GetPindex(), lastIdxN=v0->GetNindex();
438
439   Int_t used=0;
440
441   // Check if this V0 comes from a reconstructed decay
442   Int_t ncs=GetNumberOfCascades();
443   for (Int_t n=0; n<ncs; n++) {
444     AliESDcascade *cs=GetCascade(n);
445
446     Int_t csIdxP=cs->GetPindex();
447     Int_t csIdxN=cs->GetNindex();
448
449     if (idxP==csIdxP)
450        if (idxN==csIdxN) return kFALSE;
451
452     if (csIdxP==lastIdxP)
453        if (csIdxN==lastIdxN) used++;
454   }
455
456   //Replace the removed V0 with the last V0 
457   TClonesArray &a=*fV0s;
458   delete a.RemoveAt(rm);
459
460   if (rm==last) return kTRUE;
461
462   //v0 is pointing to the last V0 candidate... 
463   new (a[rm]) AliESDv0(*v0);
464   delete a.RemoveAt(last);
465
466   if (!used) return kTRUE;
467   
468
469   // Remap the indices of the daughters of reconstructed decays
470   for (Int_t n=0; n<ncs; n++) {
471     AliESDcascade *cs=GetCascade(n);
472
473
474     Int_t csIdxP=cs->GetPindex();
475     Int_t csIdxN=cs->GetNindex();
476
477     if (csIdxP==lastIdxP)
478       if (csIdxN==lastIdxN) {
479          cs->AliESDv0::SetIndex(1,idxP);
480          cs->AliESDv0::SetIndex(0,idxN);
481          used--;
482          if (!used) return kTRUE;
483       }
484   }
485
486   return kTRUE;
487 }
488
489 Bool_t  AliESDEvent::RemoveTrack(Int_t rm) {
490   // ---------------------------------------------------------
491   // Remove a track and references to it from ESD,
492   // if this track does not come from a reconstructed decay
493   // ---------------------------------------------------------
494   Int_t last=GetNumberOfTracks()-1;
495   if ((rm<0)||(rm>last)) return kFALSE;
496
497   Int_t used=0;
498
499   // Check if this track comes from a reconstructed decay
500   Int_t nv0=GetNumberOfV0s();
501   for (Int_t n=0; n<nv0; n++) {
502     AliESDv0 *v0=GetV0(n);
503
504     Int_t idx=v0->GetNindex();
505     if (rm==idx) return kFALSE;
506     if (idx==last) used++;
507
508     idx=v0->GetPindex();
509     if (rm==idx) return kFALSE;
510     if (idx==last) used++;
511   }
512
513   Int_t ncs=GetNumberOfCascades();
514   for (Int_t n=0; n<ncs; n++) {
515     AliESDcascade *cs=GetCascade(n);
516
517     Int_t idx=cs->GetIndex();
518     if (rm==idx) return kFALSE;
519     if (idx==last) used++;
520   }
521
522   Int_t nkn=GetNumberOfKinks();
523   for (Int_t n=0; n<nkn; n++) {
524     AliESDkink *kn=GetKink(n);
525
526     Int_t idx=kn->GetIndex(0);
527     if (rm==idx) return kFALSE;
528     if (idx==last) used++;
529
530     idx=kn->GetIndex(1);
531     if (rm==idx) return kFALSE;
532     if (idx==last) used++;
533   }
534
535
536   //Replace the removed track with the last track 
537   TClonesArray &a=*fTracks;
538   delete a.RemoveAt(rm);
539
540   if (rm==last) return kTRUE;
541
542   AliESDtrack *t=GetTrack(last);
543   t->SetID(rm);
544   new (a[rm]) AliESDtrack(*t);
545   delete a.RemoveAt(last);
546
547   if (!used) return kTRUE;
548   
549
550   // Remap the indices of the daughters of reconstructed decays
551   for (Int_t n=0; n<nv0; n++) {
552     AliESDv0 *v0=GetV0(n);
553     if (v0->GetIndex(0)==last) {
554        v0->SetIndex(0,rm);
555        used--;
556        if (!used) return kTRUE;
557     }
558     if (v0->GetIndex(1)==last) {
559        v0->SetIndex(1,rm);
560        used--;
561        if (!used) return kTRUE;
562     }
563   }
564
565   for (Int_t n=0; n<ncs; n++) {
566     AliESDcascade *cs=GetCascade(n);
567     if (cs->GetIndex()==last) {
568        cs->SetIndex(rm);
569        used--;
570        if (!used) return kTRUE;
571     }
572   }
573
574   for (Int_t n=0; n<nkn; n++) {
575     AliESDkink *kn=GetKink(n);
576     if (kn->GetIndex(0)==last) {
577        kn->SetIndex(rm,0);
578        used--;
579        if (!used) return kTRUE;
580     }
581     if (kn->GetIndex(1)==last) {
582        kn->SetIndex(rm,1);
583        used--;
584        if (!used) return kTRUE;
585     }
586   }
587
588   return kTRUE;
589 }
590
591
592 Bool_t AliESDEvent::Clean(Float_t *cleanPars) {
593   //
594   // Remove the data which are not needed for the physics analysis.
595   //
596   // 1) Cleaning the V0 candidates
597   //    ---------------------------
598   //    If the cosine of the V0 pointing angle "csp" and 
599   //    the DCA between the daughter tracks "dca" does not satisfy 
600   //    the conditions 
601   //
602   //     csp > cleanPars[1] + dca/cleanPars[0]*(1.- cleanPars[1])
603   //
604   //    an attempt to remove this V0 candidate from ESD is made.
605   //
606   //    The V0 candidate gets removed if it does not belong to any 
607   //    recosntructed cascade decay
608   //
609   //    12.11.2007, optimal values: cleanPars[0]=0.5, cleanPars[1]=0.999
610   //
611   // 2) Cleaning the tracks
612   //    ----------------------
613   //    If track's transverse parameter is larger than cleanPars[2]
614   //                       OR
615   //    track's longitudinal parameter is larger than cleanPars[3]
616   //    an attempt to remove this track from ESD is made.
617   //
618   //    The track gets removed if it does not come 
619   //    from a reconstructed decay
620   //
621   Bool_t rc=kFALSE;
622
623   Float_t dcaMax=cleanPars[0];
624   Float_t cspMin=cleanPars[1];
625
626   Int_t nV0s=GetNumberOfV0s();
627   for (Int_t i=nV0s-1; i>=0; i--) {
628     AliESDv0 *v0=GetV0(i);
629
630     Float_t dca=v0->GetDcaV0Daughters();
631     Float_t csp=v0->GetV0CosineOfPointingAngle();
632     Float_t cspcut=cspMin + dca/dcaMax*(1.-cspMin);
633     if (csp > cspcut) continue;
634
635     if (RemoveV0(i)) rc=kTRUE;
636   }
637
638
639   Float_t dmax=cleanPars[2], zmax=cleanPars[3];
640
641   const AliESDVertex *vertex=GetVertex();
642   Bool_t vtxOK=vertex->GetStatus();
643   
644   Int_t nTracks=GetNumberOfTracks();
645   for (Int_t i=nTracks-1; i>=0; i--) {
646     AliESDtrack *track=GetTrack(i);
647     Float_t xy,z; track->GetImpactParameters(xy,z);
648     if ((TMath::Abs(xy) > dmax) || (vtxOK && (TMath::Abs(z) > zmax))) {
649       if (RemoveTrack(i)) rc=kTRUE;
650     }
651   }
652
653   return rc;
654 }
655
656 Int_t  AliESDEvent::AddTrack(const AliESDtrack *t) 
657 {
658     // Add track
659     TClonesArray &ftr = *fTracks;
660     AliESDtrack * track = new(ftr[fTracks->GetEntriesFast()])AliESDtrack(*t);
661     track->SetID(fTracks->GetEntriesFast()-1);
662     return  track->GetID();    
663 }
664
665  void AliESDEvent::AddMuonTrack(const AliESDMuonTrack *t) 
666 {
667     TClonesArray &fmu = *fMuonTracks;
668     new(fmu[fMuonTracks->GetEntriesFast()]) AliESDMuonTrack(*t);
669 }
670
671 void AliESDEvent::AddPmdTrack(const AliESDPmdTrack *t) 
672 {
673   TClonesArray &fpmd = *fPmdTracks;
674   new(fpmd[fPmdTracks->GetEntriesFast()]) AliESDPmdTrack(*t);
675 }
676
677 void AliESDEvent::AddTrdTrack(const AliESDTrdTrack *t) 
678 {
679   TClonesArray &ftrd = *fTrdTracks;
680   new(ftrd[fTrdTracks->GetEntriesFast()]) AliESDTrdTrack(*t);
681 }
682
683
684
685
686 Int_t AliESDEvent::AddKink(const AliESDkink *c) 
687 {
688     // Add kink
689     TClonesArray &fk = *fKinks;
690     AliESDkink * kink = new(fk[fKinks->GetEntriesFast()]) AliESDkink(*c);
691     kink->SetID(fKinks->GetEntriesFast()); // CKB different from the other imps..
692     return fKinks->GetEntriesFast()-1;
693 }
694
695
696 void AliESDEvent::AddCascade(const AliESDcascade *c) 
697 {
698   TClonesArray &fc = *fCascades;
699   new(fc[fCascades->GetEntriesFast()]) AliESDcascade(*c);
700 }
701
702
703 Int_t AliESDEvent::AddCaloCluster(const AliESDCaloCluster *c) 
704 {
705     // Add calocluster
706     TClonesArray &fc = *fCaloClusters;
707     AliESDCaloCluster *clus = new(fc[fCaloClusters->GetEntriesFast()]) AliESDCaloCluster(*c);
708     clus->SetID(fCaloClusters->GetEntriesFast()-1);
709     return fCaloClusters->GetEntriesFast()-1;
710   }
711
712
713 void  AliESDEvent::AddRawDataErrorLog(const AliRawDataErrorLog *log) {
714   TClonesArray &errlogs = *fErrorLogs;
715   new(errlogs[errlogs.GetEntriesFast()])  AliRawDataErrorLog(*log);
716 }
717
718 void  AliESDEvent::SetVertex(const AliESDVertex *vertex) 
719 {
720   // Set the SPD vertex
721   // use already allocated space
722   if(fSPDVertex){
723     *fSPDVertex = *vertex;
724     fSPDVertex->SetName(fgkESDListName[kSPDVertex]);
725   }
726 }
727
728 void  AliESDEvent::SetPrimaryVertex(const AliESDVertex *vertex) 
729 {
730   // Set the primary vertex
731   // use already allocated space
732   if(fPrimaryVertex){
733     *fPrimaryVertex = *vertex;
734     fPrimaryVertex->SetName(fgkESDListName[kPrimaryVertex]);
735   }
736 }
737
738 void AliESDEvent::SetMultiplicity(const AliMultiplicity *mul) 
739 {
740   // Set the SPD Multiplicity
741   if(fSPDMult){
742     *fSPDMult = *mul;
743   }
744 }
745
746
747 void AliESDEvent::SetFMDData(AliESDFMD * obj) 
748
749   // use already allocated space
750   if(fESDFMD){
751     *fESDFMD = *obj;
752   }
753 }
754
755 void AliESDEvent::SetVZEROData(AliESDVZERO * obj)
756
757   // use already allocated space
758   if(fESDVZERO)
759     *fESDVZERO = *obj;
760 }
761
762 void AliESDEvent::GetESDfriend(AliESDfriend *ev) const 
763 {
764   //
765   // Extracts the complementary info from the ESD
766   //
767   if (!ev) return;
768
769   Int_t ntrk=GetNumberOfTracks();
770
771   for (Int_t i=0; i<ntrk; i++) {
772     AliESDtrack *t=GetTrack(i);
773     const AliESDfriendTrack *f=t->GetFriendTrack();
774     ev->AddTrack(f);
775
776     t->ReleaseESDfriendTrack();// Not to have two copies of "friendTrack"
777
778   }
779 }
780
781
782 void AliESDEvent::AddObject(TObject* obj) 
783 {
784   // Add an object to the list of object.
785   // Please be aware that in order to increase performance you should
786   // refrain from using TObjArrays (if possible). Use TClonesArrays, instead.
787   fESDObjects->SetOwner(kTRUE);
788   fESDObjects->AddLast(obj);
789 }
790
791
792 void AliESDEvent::GetStdContent() 
793 {
794   // set pointers for standard content
795   // get by name much safer and not a big overhead since not called very often
796  
797   fESDRun = (AliESDRun*)fESDObjects->FindObject(fgkESDListName[kESDRun]);
798   fHeader = (AliESDHeader*)fESDObjects->FindObject(fgkESDListName[kHeader]);
799   fESDZDC = (AliESDZDC*)fESDObjects->FindObject(fgkESDListName[kESDZDC]);
800   fESDFMD = (AliESDFMD*)fESDObjects->FindObject(fgkESDListName[kESDFMD]);
801   fESDVZERO = (AliESDVZERO*)fESDObjects->FindObject(fgkESDListName[kESDVZERO]);
802   fESDTZERO = (AliESDTZERO*)fESDObjects->FindObject(fgkESDListName[kESDTZERO]);
803   fSPDVertex = (AliESDVertex*)fESDObjects->FindObject(fgkESDListName[kSPDVertex]);
804   fPrimaryVertex = (AliESDVertex*)fESDObjects->FindObject(fgkESDListName[kPrimaryVertex]);
805   fSPDMult =       (AliMultiplicity*)fESDObjects->FindObject(fgkESDListName[kSPDMult]);
806   fPHOSTrigger = (AliESDCaloTrigger*)fESDObjects->FindObject(fgkESDListName[kPHOSTrigger]);
807   fEMCALTrigger = (AliESDCaloTrigger*)fESDObjects->FindObject(fgkESDListName[kEMCALTrigger]);
808   fTracks = (TClonesArray*)fESDObjects->FindObject(fgkESDListName[kTracks]);
809   fMuonTracks = (TClonesArray*)fESDObjects->FindObject(fgkESDListName[kMuonTracks]);
810   fPmdTracks = (TClonesArray*)fESDObjects->FindObject(fgkESDListName[kPmdTracks]);
811   fTrdTracks = (TClonesArray*)fESDObjects->FindObject(fgkESDListName[kTrdTracks]);
812   fV0s = (TClonesArray*)fESDObjects->FindObject(fgkESDListName[kV0s]);
813   fCascades = (TClonesArray*)fESDObjects->FindObject(fgkESDListName[kCascades]);
814   fKinks = (TClonesArray*)fESDObjects->FindObject(fgkESDListName[kKinks]);
815   fCaloClusters = (TClonesArray*)fESDObjects->FindObject(fgkESDListName[kCaloClusters]);
816   fEMCALCells = (AliESDCaloCells*)fESDObjects->FindObject(fgkESDListName[kEMCALCells]);
817   fPHOSCells = (AliESDCaloCells*)fESDObjects->FindObject(fgkESDListName[kPHOSCells]);
818   fErrorLogs = (TClonesArray*)fESDObjects->FindObject(fgkESDListName[kErrorLogs]);
819
820 }
821
822 void AliESDEvent::SetStdNames(){
823   // Set the names of the standard contents
824   // 
825   if(fESDObjects->GetEntries()==kESDListN){
826     for(int i = 0;i < fESDObjects->GetEntries();i++){
827       TObject *fObj = fESDObjects->At(i);
828       if(fObj->InheritsFrom("TNamed")){
829         ((TNamed*)fObj)->SetName(fgkESDListName[i]);
830       }
831       else if(fObj->InheritsFrom("TClonesArray")){
832         ((TClonesArray*)fObj)->SetName(fgkESDListName[i]);
833       }
834     }
835   }
836   else{
837     printf("%s:%d SetStdNames() Wrong number of Std Entries \n",(char*)__FILE__,__LINE__);
838   }
839
840
841 void AliESDEvent::CreateStdContent() 
842 {
843   // create the standard AOD content and set pointers
844
845   // create standard objects and add them to the TList of objects
846   AddObject(new AliESDRun());
847   AddObject(new AliESDHeader());
848   AddObject(new AliESDZDC());
849   AddObject(new AliESDFMD());
850   AddObject(new AliESDVZERO());
851   AddObject(new AliESDTZERO());
852   AddObject(new AliESDVertex());
853   AddObject(new AliESDVertex());
854   AddObject(new AliMultiplicity());
855   AddObject(new AliESDCaloTrigger());
856   AddObject(new AliESDCaloTrigger());
857   AddObject(new TClonesArray("AliESDtrack",0));
858   AddObject(new TClonesArray("AliESDMuonTrack",0));
859   AddObject(new TClonesArray("AliESDPmdTrack",0));
860   AddObject(new TClonesArray("AliESDTrdTrack",0));
861   AddObject(new TClonesArray("AliESDv0",0));
862   AddObject(new TClonesArray("AliESDcascade",0));
863   AddObject(new TClonesArray("AliESDkink",0));
864   AddObject(new TClonesArray("AliESDCaloCluster",0));
865   AddObject(new AliESDCaloCells());
866   AddObject(new AliESDCaloCells());
867   AddObject(new TClonesArray("AliRawDataErrorLog",0));
868
869   // check the order of the indices against enum...
870
871   // set names
872   SetStdNames();
873   // read back pointers
874   GetStdContent();
875 }
876
877 TObject* AliESDEvent::FindListObject(const char *name){
878   if(fESDObjects)return fESDObjects->FindObject(name);
879   return 0;
880
881
882 Int_t AliESDEvent::GetPHOSClusters(TRefArray *clusters) const
883 {
884   // fills the provided TRefArray with all found phos clusters
885   
886   clusters->Clear();
887   
888   AliESDCaloCluster *cl = 0;
889   for (Int_t i = 0; i < GetNumberOfCaloClusters(); i++) {
890     
891     if ( (cl = GetCaloCluster(i)) ) {
892       if (cl->IsPHOS()){
893         clusters->Add(cl);
894         AliDebug(1,Form("IsPHOS cluster %d Size: %d \n",i,clusters->GetEntriesFast()));
895       }
896     }
897   }
898   return clusters->GetEntriesFast();
899 }
900
901 Int_t AliESDEvent::GetEMCALClusters(TRefArray *clusters) const
902 {
903   // fills the provided TRefArray with all found emcal clusters
904
905   clusters->Clear();
906
907   AliESDCaloCluster *cl = 0;
908   for (Int_t i = 0; i < GetNumberOfCaloClusters(); i++) {
909
910     if ( (cl = GetCaloCluster(i)) ) {
911       if (cl->IsEMCAL()){
912         clusters->Add(cl);
913         AliDebug(1,Form("IsEMCAL cluster %d Size: %d \n",i,clusters->GetEntriesFast()));
914       }
915     }
916   }
917   return clusters->GetEntriesFast();
918 }
919
920
921 void AliESDEvent::ReadFromTree(TTree *tree){
922
923   if(!tree){
924     Printf("%s %d AliESDEvent::ReadFromTree() Zero Pointer to Tree \n",(char*)__FILE__,__LINE__);
925     return;
926   }
927
928   // load the TTree
929   if(!tree->GetTree())tree->LoadTree(0);
930
931   // if we find the "ESD" branch on the tree we do have the old structure
932   if(tree->GetBranch("ESD")) {
933     char ** address  = (char **)(tree->GetBranch("ESD")->GetAddress());
934     // do we have the friend branch
935     TBranch * esdFB = tree->GetBranch("ESDfriend.");
936     char ** addressF = 0;
937     if(esdFB)addressF = (char **)(esdFB->GetAddress());
938     if (!address) {
939       printf("%s %d AliESDEvent::ReadFromTree() Reading old Tree \n",(char*)__FILE__,__LINE__);
940       tree->SetBranchAddress("ESD",       &fESDOld);
941       if(esdFB){
942         tree->SetBranchAddress("ESDfriend.",&fESDFriendOld);
943       }
944     } else {
945       printf("%s %d AliESDEvent::ReadFromTree() Reading old Tree \n",(char*)__FILE__,__LINE__);
946       printf("%s %d Branch already connected. Using existing branch address. \n",(char*)__FILE__,__LINE__);
947       fESDOld       = (AliESD*)       (*address);
948       // addressF can still be 0, since branch needs to switched on
949       if(addressF)fESDFriendOld = (AliESDfriend*) (*addressF);
950     }
951                                        
952     //  have already connected the old ESD structure... ?
953     // reuse also the pointer of the AlliESDEvent
954     // otherwise create new ones
955
956     TList* connectedList = (TList*) (tree->GetUserInfo()->FindObject("ESDObjectsConnectedToTree"));
957   
958     if(connectedList){
959       // If connected use the connected list of objects
960       if(fESDObjects!= connectedList){
961         // protect when called twice 
962         fESDObjects->Delete();
963         fESDObjects = connectedList;
964       }
965       GetStdContent(); 
966
967       // The pointer to the friend changes when called twice via InitIO. Is this connected to 
968       // the change TChain -> TTree
969       
970       TObject* oldf = FindListObject("AliESDfriend");
971       TObject* newf = 0;
972       if(addressF){
973         newf = (TObject*)*addressF;
974       }
975       if(newf!=0&&oldf!=newf){
976         // remove the old reference
977         // Should we also delete it? Or is this handled in TTree I/O
978         // since it is created by the first SetBranchAddress
979         fESDObjects->Remove(oldf);
980         // add the new one 
981         fESDObjects->Add(newf);
982       }
983       
984       fConnected = true;
985       return;
986     }
987     // else...    
988     CreateStdContent(); // create for copy
989     // if we have the esdfriend add it, so we always can access it via the userinfo
990     if(fESDFriendOld)AddObject(fESDFriendOld);
991     // we are not owner of the list objects 
992     // must not delete it
993     fESDObjects->SetOwner(kFALSE);
994     fESDObjects->SetName("ESDObjectsConnectedToTree");
995     tree->GetUserInfo()->Add(fESDObjects);
996     fConnected = true;
997     return;
998   }
999   
1000   delete fESDOld;
1001   fESDOld = 0;
1002   // Try to find AliESDEvent
1003   AliESDEvent *esdEvent = 0;
1004   esdEvent = (AliESDEvent*)tree->GetTree()->GetUserInfo()->FindObject("AliESDEvent");
1005   if(esdEvent){   
1006       // Check if already connected to tree
1007     TList* connectedList = (TList*) (tree->GetUserInfo()->FindObject("ESDObjectsConnectedToTree"));
1008     if (connectedList) {
1009       // If connected use the connected list if objects
1010       fESDObjects->Delete();
1011       fESDObjects = connectedList;
1012       GetStdContent(); 
1013       fConnected = true;
1014       return;
1015     }
1016     // Connect to tree
1017     // prevent a memory leak when reading back the TList
1018     delete fESDObjects;
1019     fESDObjects = 0;
1020     // create a new TList from the UserInfo TList... 
1021     // copy constructor does not work...
1022     fESDObjects = (TList*)(esdEvent->GetList()->Clone());
1023     fESDObjects->SetOwner(kFALSE);
1024     if(fESDObjects->GetEntries()<kESDListN){
1025       printf("%s %d AliESDEvent::ReadFromTree() TList contains less than the standard contents %d < %d \n",
1026              (char*)__FILE__,__LINE__,fESDObjects->GetEntries(),kESDListN);
1027     }
1028     // set the branch addresses
1029     TIter next(fESDObjects);
1030     TNamed *el;
1031     while((el=(TNamed*)next())){
1032       TString bname(el->GetName());
1033       if(bname.CompareTo("AliESDfriend")==0)
1034         {
1035           // AliESDfriend does not have a name ...
1036           tree->SetBranchAddress("ESDfriend.",fESDObjects->GetObjectRef(el));
1037         }
1038       else{
1039         tree->SetBranchAddress(bname.Data(),fESDObjects->GetObjectRef(el));
1040       }
1041     }
1042     GetStdContent();
1043     // when reading back we are not owner of the list 
1044     // must not delete it
1045     fESDObjects->SetOwner(kFALSE);
1046     fESDObjects->SetName("ESDObjectsConnectedToTree");
1047     // we are not owner of the list objects 
1048     // must not delete it
1049     tree->GetUserInfo()->Add(fESDObjects);
1050     fConnected = true;
1051   }// no esdEvent
1052   else {
1053     // we can't get the list from the user data, create standard content
1054     // and set it by hand (no ESDfriend at the moment
1055     CreateStdContent();
1056     TIter next(fESDObjects);
1057     TNamed *el;
1058     while((el=(TNamed*)next())){
1059       TString bname(el->GetName());    
1060       tree->SetBranchAddress(bname.Data(),fESDObjects->GetObjectRef(el));
1061     }
1062     GetStdContent();
1063     // when reading back we are not owner of the list 
1064     // must not delete it
1065     fESDObjects->SetOwner(kFALSE);
1066   }
1067 }
1068
1069
1070 void AliESDEvent::CopyFromOldESD()
1071 {
1072   // Method which copies over everthing from the old esd structure to the 
1073   // new  
1074   if(fESDOld){
1075     ResetStdContent();
1076      // Run
1077     SetRunNumber(fESDOld->GetRunNumber());
1078     SetPeriodNumber(fESDOld->GetPeriodNumber());
1079     SetMagneticField(fESDOld->GetMagneticField());
1080   
1081     // leave out diamond ...
1082     // SetDiamond(const AliESDVertex *vertex) { fESDRun->SetDiamond(vertex);}
1083
1084     // header
1085     SetTriggerMask(fESDOld->GetTriggerMask());
1086     SetOrbitNumber(fESDOld->GetOrbitNumber());
1087     SetTimeStamp(fESDOld->GetTimeStamp());
1088     SetEventType(fESDOld->GetEventType());
1089     SetEventNumberInFile(fESDOld->GetEventNumberInFile());
1090     SetBunchCrossNumber(fESDOld->GetBunchCrossNumber());
1091     SetTriggerCluster(fESDOld->GetTriggerCluster());
1092
1093     // ZDC
1094
1095     SetZDC(fESDOld->GetZDCN1Energy(),
1096            fESDOld->GetZDCP1Energy(),
1097            fESDOld->GetZDCEMEnergy(),
1098            0,
1099            fESDOld->GetZDCN2Energy(),
1100            fESDOld->GetZDCP2Energy(),
1101            fESDOld->GetZDCParticipants());
1102
1103     // FMD
1104     
1105     if(fESDOld->GetFMDData())SetFMDData(fESDOld->GetFMDData());
1106
1107     // T0
1108
1109     SetT0zVertex(fESDOld->GetT0zVertex());
1110     SetT0(fESDOld->GetT0());
1111     //  leave amps out
1112
1113     // VZERO
1114     if (fESDOld->GetVZEROData()) SetVZEROData(fESDOld->GetVZEROData());
1115
1116     if(fESDOld->GetVertex())SetVertex(fESDOld->GetVertex());
1117
1118     if(fESDOld->GetPrimaryVertex())SetPrimaryVertex(fESDOld->GetPrimaryVertex());
1119
1120     if(fESDOld->GetMultiplicity())SetMultiplicity(fESDOld->GetMultiplicity());
1121
1122     for(int i = 0;i<fESDOld->GetNumberOfTracks();i++){
1123       AddTrack(fESDOld->GetTrack(i));
1124     }
1125
1126     for(int i = 0;i<fESDOld->GetNumberOfMuonTracks();i++){
1127       AddMuonTrack(fESDOld->GetMuonTrack(i));
1128     }
1129
1130     for(int i = 0;i<fESDOld->GetNumberOfPmdTracks();i++){
1131       AddPmdTrack(fESDOld->GetPmdTrack(i));
1132     }
1133
1134     for(int i = 0;i<fESDOld->GetNumberOfTrdTracks();i++){
1135       AddTrdTrack(fESDOld->GetTrdTrack(i));
1136     }
1137
1138     for(int i = 0;i<fESDOld->GetNumberOfV0s();i++){
1139       AddV0(fESDOld->GetV0(i));
1140     }
1141
1142     for(int i = 0;i<fESDOld->GetNumberOfCascades();i++){
1143       AddCascade(fESDOld->GetCascade(i));
1144     }
1145
1146     for(int i = 0;i<fESDOld->GetNumberOfKinks();i++){
1147       AddKink(fESDOld->GetKink(i));
1148     }
1149
1150
1151     for(int i = 0;i<fESDOld->GetNumberOfCaloClusters();i++){
1152       AddCaloCluster(fESDOld->GetCaloCluster(i));
1153     }
1154
1155   }// if fesdold
1156 }
1157
1158
1159