]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/ESD/AliESDEvent.cxx
Complete implementation of pools, see #88914. From rev. 53550,53557,53568 (Ruben)
[u/mrichter/AliRoot.git] / STEER / ESD / 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 #include <TROOT.h>
39 #include <TInterpreter.h>
40
41 #include "AliESDEvent.h"
42 #include "AliESDfriend.h"
43 #include "AliESDVZERO.h"
44 #include "AliESDFMD.h"
45 #include "AliESD.h"
46 #include "AliESDMuonTrack.h"
47 #include "AliESDPmdTrack.h"
48 #include "AliESDTrdTrack.h"
49 #include "AliESDVertex.h"
50 #include "AliESDcascade.h"
51 #include "AliESDPmdTrack.h"
52 #include "AliESDTrdTrigger.h"
53 #include "AliESDTrdTrack.h"
54 #include "AliESDTrdTracklet.h"
55 #include "AliESDVertex.h"
56 #include "AliVertexerTracks.h"
57 #include "AliESDcascade.h"
58 #include "AliESDkink.h"
59 #include "AliESDtrack.h"
60 #include "AliESDHLTtrack.h"
61 #include "AliESDCaloCluster.h"
62 #include "AliESDCaloCells.h"
63 #include "AliESDv0.h"
64 #include "AliESDFMD.h"
65 #include "AliESDVZERO.h"
66 #include "AliMultiplicity.h"
67 #include "AliRawDataErrorLog.h"
68 #include "AliLog.h"
69 #include "AliESDACORDE.h"
70 #include "AliESDHLTDecision.h"
71 #include "AliCentrality.h"
72 #ifdef MFT_UPGRADE
73 #include "AliESDMFT.h"
74 #endif
75 #include "AliEventplane.h"
76
77
78 ClassImp(AliESDEvent)
79
80
81
82 // here we define the names, some classes are no TNamed, therefore the classnames 
83 // are the Names
84   const char* AliESDEvent::fgkESDListName[kESDListN] = {"AliESDRun",
85                                                         "AliESDHeader",
86                                                         "AliESDZDC",
87                                                         "AliESDFMD",
88                                                         "AliESDVZERO",
89                                                         "AliESDTZERO",
90                                                         "TPCVertex",
91                                                         "SPDVertex",
92                                                         "PrimaryVertex",
93                                                         "AliMultiplicity",
94                                                         "PHOSTrigger",
95                                                         "EMCALTrigger",
96                                                         "SPDPileupVertices",
97                                                         "TrkPileupVertices",
98                                                         "Tracks",
99                                                         "MuonTracks",
100                                                         "PmdTracks",
101                                                         "AliESDTrdTrigger",
102                                                         "TrdTracks",
103                                                         "TrdTracklets",
104                                                         "V0s",
105                                                         "Cascades",
106                                                         "Kinks",
107                                                         "CaloClusters",
108                                                         "EMCALCells",
109                                                         "PHOSCells",
110                                                         "AliRawDataErrorLogs",
111                                                         "AliESDACORDE",
112                                                         "AliTOFHeader"
113                                 #ifdef MFT_UPGRADE
114 //                              , "AliESDMFT"
115                                                         #endif
116   };
117
118 //______________________________________________________________________________
119 AliESDEvent::AliESDEvent():
120   AliVEvent(),
121   fESDObjects(new TList()),
122   fESDRun(0),
123   fHeader(0),
124   fESDZDC(0),
125   fESDFMD(0),
126   fESDVZERO(0),
127   fESDTZERO(0),
128   fTPCVertex(0),
129   fSPDVertex(0),
130   fPrimaryVertex(0),
131   fSPDMult(0),
132   fPHOSTrigger(0),
133   fEMCALTrigger(0),
134   fESDACORDE(0),
135   fTrdTrigger(0),
136   fSPDPileupVertices(0),
137   fTrkPileupVertices(0),
138   fTracks(0),
139   fMuonTracks(0),
140   fPmdTracks(0),
141   fTrdTracks(0),
142   fTrdTracklets(0),
143   fV0s(0),  
144   fCascades(0),
145   fKinks(0),
146   fCaloClusters(0),
147   fEMCALCells(0), fPHOSCells(0),
148   fErrorLogs(0),
149   fESDOld(0),
150   fESDFriendOld(0),
151   fConnected(kFALSE),
152   fUseOwnList(kFALSE),
153   fTOFHeader(0),
154   fCentrality(0),
155   fEventplane(0)
156   #ifdef MFT_UPGRADE
157 //  , fESDMFT(0)
158   #endif
159 {
160 }
161 //______________________________________________________________________________
162 AliESDEvent::AliESDEvent(const AliESDEvent& esd):
163   AliVEvent(esd),
164   fESDObjects(new TList()),
165   fESDRun(new AliESDRun(*esd.fESDRun)),
166   fHeader(new AliESDHeader(*esd.fHeader)),
167   fESDZDC(new AliESDZDC(*esd.fESDZDC)),
168   fESDFMD(new AliESDFMD(*esd.fESDFMD)),
169   fESDVZERO(new AliESDVZERO(*esd.fESDVZERO)),
170   fESDTZERO(new AliESDTZERO(*esd.fESDTZERO)),
171   fTPCVertex(new AliESDVertex(*esd.fTPCVertex)),
172   fSPDVertex(new AliESDVertex(*esd.fSPDVertex)),
173   fPrimaryVertex(new AliESDVertex(*esd.fPrimaryVertex)),
174   fSPDMult(new AliMultiplicity(*esd.fSPDMult)),
175   fPHOSTrigger(new AliESDCaloTrigger(*esd.fPHOSTrigger)),
176   fEMCALTrigger(new AliESDCaloTrigger(*esd.fEMCALTrigger)),
177   fESDACORDE(new AliESDACORDE(*esd.fESDACORDE)),
178   fTrdTrigger(new AliESDTrdTrigger(*esd.fTrdTrigger)),
179   fSPDPileupVertices(new TClonesArray(*esd.fSPDPileupVertices)),
180   fTrkPileupVertices(new TClonesArray(*esd.fTrkPileupVertices)),
181   fTracks(new TClonesArray(*esd.fTracks)),
182   fMuonTracks(new TClonesArray(*esd.fMuonTracks)),
183   fPmdTracks(new TClonesArray(*esd.fPmdTracks)),
184   fTrdTracks(new TClonesArray(*esd.fTrdTracks)),
185   fTrdTracklets(new TClonesArray(*esd.fTrdTracklets)),
186   fV0s(new TClonesArray(*esd.fV0s)),  
187   fCascades(new TClonesArray(*esd.fCascades)),
188   fKinks(new TClonesArray(*esd.fKinks)),
189   fCaloClusters(new TClonesArray(*esd.fCaloClusters)),
190   fEMCALCells(new AliESDCaloCells(*esd.fEMCALCells)),
191   fPHOSCells(new AliESDCaloCells(*esd.fPHOSCells)),
192   fErrorLogs(new TClonesArray(*esd.fErrorLogs)),
193   fESDOld(esd.fESDOld ? new AliESD(*esd.fESDOld) : 0),
194   fESDFriendOld(esd.fESDFriendOld ? new AliESDfriend(*esd.fESDFriendOld) : 0),
195   fConnected(esd.fConnected),
196   fUseOwnList(esd.fUseOwnList),
197   fTOFHeader(new AliTOFHeader(*esd.fTOFHeader)),
198   fCentrality(new AliCentrality(*esd.fCentrality)),
199   fEventplane(new AliEventplane(*esd.fEventplane))
200   #ifdef MFT_UPGRADE
201 //  , fESDMFT(new AliESDMFT(*esd.fESDMFT))
202   #endif
203
204
205 {
206   printf("copying ESD event...\n");   // AU
207   // CKB init in the constructor list and only add here ...
208   AddObject(fESDRun);
209   AddObject(fHeader);
210   AddObject(fESDZDC);
211   AddObject(fESDFMD);
212   AddObject(fESDVZERO);
213   AddObject(fESDTZERO);
214   AddObject(fTPCVertex);
215   AddObject(fSPDVertex);
216   AddObject(fPrimaryVertex);
217   AddObject(fSPDMult);
218   AddObject(fPHOSTrigger);
219   AddObject(fEMCALTrigger);
220   AddObject(fTrdTrigger);
221   AddObject(fSPDPileupVertices);
222   AddObject(fTrkPileupVertices);
223   AddObject(fTracks);
224   AddObject(fMuonTracks);
225   AddObject(fPmdTracks);
226   AddObject(fTrdTracks);
227   AddObject(fTrdTracklets);
228   AddObject(fV0s);
229   AddObject(fCascades);
230   AddObject(fKinks);
231   AddObject(fCaloClusters);
232   AddObject(fEMCALCells);
233   AddObject(fPHOSCells);
234   AddObject(fErrorLogs);
235   AddObject(fESDACORDE);
236   AddObject(fTOFHeader);
237   #ifdef MFT_UPGRADE
238 //  AddObject(fESDMFT);
239   #endif
240   GetStdContent();
241
242 }
243
244 //______________________________________________________________________________
245 AliESDEvent & AliESDEvent::operator=(const AliESDEvent& source) {
246
247   // Assignment operator
248
249   if(&source == this) return *this;
250   AliVEvent::operator=(source);
251
252   // This assumes that the list is already created
253   // and that the virtual void Copy(Tobject&) function
254   // is correctly implemented in the derived class
255   // otherwise only TObject::Copy() will be used
256
257
258
259   if((fESDObjects->GetSize()==0)&&(source.fESDObjects->GetSize()>=kESDListN)){
260     // We cover the case that we do not yet have the 
261     // standard content but the source has it
262     CreateStdContent();
263   }
264
265   TIter next(source.GetList());
266   TObject *its = 0;
267   TString name;
268   while ((its = next())) {
269     name.Form("%s", its->GetName());
270     TObject *mine = fESDObjects->FindObject(name.Data());
271     if(!mine){
272       TClass* pClass=TClass::GetClass(its->ClassName());
273       if (!pClass) {
274         AliWarning(Form("Can not find class description for entry %s (%s)\n",
275                         its->ClassName(), name.Data()));
276         continue;
277       }
278
279       mine=(TObject*)pClass->New();
280       if(!mine){
281       // not in this: can be added to list
282         AliWarning(Form("%s:%d Could not find %s for copying \n",
283                         (char*)__FILE__,__LINE__,name.Data()));
284         continue;
285       }  
286       if(mine->InheritsFrom("TNamed")){
287         ((TNamed*)mine)->SetName(name);
288       }
289       else if(mine->InheritsFrom("TCollection")){
290         if(mine->InheritsFrom("TClonesArray")) {
291           TClonesArray* tcits = dynamic_cast<TClonesArray*>(its);
292           if (tcits)
293             dynamic_cast<TClonesArray*>(mine)->SetClass(tcits->GetClass());
294         }
295         dynamic_cast<TCollection*>(mine)->SetName(name);
296       }
297       AliDebug(1, Form("adding object %s of type %s", mine->GetName(), mine->ClassName()));
298       AddObject(mine);
299     }  
300    
301     if(!its->InheritsFrom("TCollection")){
302       // simple objects
303       its->Copy(*mine);
304     }
305     else if(its->InheritsFrom("TClonesArray")){
306       // Create or expand the tclonesarray pointers
307       // so we can directly copy to the object
308       TClonesArray *its_tca = (TClonesArray*)its;
309       TClonesArray *mine_tca = (TClonesArray*)mine;
310
311       // this leaves the capacity of the TClonesArray the same
312       // except for a factor of 2 increase when size > capacity
313       // does not release any memory occupied by the tca
314       mine_tca->ExpandCreate(its_tca->GetEntriesFast());
315       for(int i = 0;i < its_tca->GetEntriesFast();++i){
316         // copy 
317         TObject *mine_tca_obj = mine_tca->At(i);
318         TObject *its_tca_obj = its_tca->At(i);
319         // no need to delete first
320         // pointers within the class should be handled by Copy()...
321         // Can there be Empty slots?
322         its_tca_obj->Copy(*mine_tca_obj);
323       }
324     }
325     else{
326       AliWarning(Form("%s:%d cannot copy TCollection \n",
327                       (char*)__FILE__,__LINE__));
328     }
329   }
330
331   fCentrality = source.fCentrality;
332   fEventplane = source.fEventplane;
333
334   fConnected  = source.fConnected;
335   fUseOwnList = source.fUseOwnList;
336
337   return *this;
338 }
339
340
341 //______________________________________________________________________________
342 AliESDEvent::~AliESDEvent()
343 {
344   //
345   // Standard destructor
346   //
347
348   // everthing on the list gets deleted automatically
349
350   
351   if(fESDObjects&&!fConnected)
352     {
353       delete fESDObjects;
354       fESDObjects = 0;
355     }
356   if (fCentrality) delete fCentrality;
357   if (fEventplane) delete fEventplane;
358   
359 }
360
361 void AliESDEvent::Copy(TObject &obj) const {
362
363   // interface to TOBject::Copy
364   // Copies the content of this into obj!
365   // bascially obj = *this
366
367   if(this==&obj)return;
368   AliESDEvent *robj = dynamic_cast<AliESDEvent*>(&obj);
369   if(!robj)return; // not an AliESEvent
370   *robj = *this;
371   return;
372 }
373
374 //______________________________________________________________________________
375 void AliESDEvent::Reset()
376 {
377
378   // Handle the cases
379   // Std content + Non std content
380
381   // Reset the standard contents
382   ResetStdContent(); 
383
384   //  reset for the old data without AliESDEvent...
385   if(fESDOld)fESDOld->Reset();
386   if(fESDFriendOld){
387     fESDFriendOld->~AliESDfriend();
388     new (fESDFriendOld) AliESDfriend();
389   }
390   // 
391
392   if(fESDObjects->GetSize()>kESDListN){
393     // we have non std content
394     // this also covers esdfriends
395     for(int i = kESDListN;i < fESDObjects->GetSize();++i){
396       TObject *pObject = fESDObjects->At(i);
397       // TClonesArrays
398       if(pObject->InheritsFrom(TClonesArray::Class())){
399         ((TClonesArray*)pObject)->Delete();
400       }
401       else if(!pObject->InheritsFrom(TCollection::Class())){
402         TClass *pClass = TClass::GetClass(pObject->ClassName());
403         if (pClass && pClass->GetListOfMethods()->FindObject("Clear")) {
404           AliDebug(1, Form("Clear for object %s class %s", pObject->GetName(), pObject->ClassName()));
405           pObject->Clear();
406         }
407         else {
408           AliDebug(1, Form("ResetWithPlacementNew for object %s class %s", pObject->GetName(), pObject->ClassName()));
409           ResetWithPlacementNew(pObject);
410         }
411       }
412       else{
413         AliWarning(Form("No reset for %s \n",
414                         pObject->ClassName()));
415       }
416     }
417   }
418
419 }
420
421 Bool_t AliESDEvent::ResetWithPlacementNew(TObject *pObject){
422   Long_t dtoronly = TObject::GetDtorOnly();
423   TClass *pClass = TClass::GetClass(pObject->ClassName()); 
424   TObject::SetDtorOnly(pObject);
425   delete pObject;
426   // Recreate with placement new
427   pClass->New(pObject);
428   // Restore the state.
429   TObject::SetDtorOnly((void*)dtoronly);
430   return kTRUE;
431 }
432
433 void AliESDEvent::ResetStdContent()
434 {
435   // Reset the standard contents
436   if(fESDRun) fESDRun->Reset();
437   if(fHeader) fHeader->Reset();
438   if(fCentrality) fCentrality->Reset();
439   if(fEventplane) fEventplane->Reset();
440   if(fESDZDC) fESDZDC->Reset();
441   if(fESDFMD) {
442     fESDFMD->Clear();
443   }
444   if(fESDVZERO){
445     // reset by callin d'to /c'tor keep the pointer
446     fESDVZERO->~AliESDVZERO();
447     new (fESDVZERO) AliESDVZERO();
448   }  
449   if(fESDACORDE){
450     fESDACORDE->~AliESDACORDE();
451     new (fESDACORDE) AliESDACORDE();    
452   } 
453   if(fESDTZERO) fESDTZERO->Reset(); 
454   // CKB no clear/reset implemented
455   if(fTPCVertex){
456     fTPCVertex->~AliESDVertex();
457     new (fTPCVertex) AliESDVertex();
458     fTPCVertex->SetName(fgkESDListName[kTPCVertex]);
459   }
460   if(fSPDVertex){
461     fSPDVertex->~AliESDVertex();
462     new (fSPDVertex) AliESDVertex();
463     fSPDVertex->SetName(fgkESDListName[kSPDVertex]);
464   }
465   if(fPrimaryVertex){
466     fPrimaryVertex->~AliESDVertex();
467     new (fPrimaryVertex) AliESDVertex();
468     fPrimaryVertex->SetName(fgkESDListName[kPrimaryVertex]);
469   }
470   if(fSPDMult){
471     fSPDMult->~AliMultiplicity();
472     new (fSPDMult) AliMultiplicity();
473   }
474   if(fTOFHeader){
475     fTOFHeader->~AliTOFHeader();
476     new (fTOFHeader) AliTOFHeader();
477     //fTOFHeader->SetName(fgkESDListName[kTOFHeader]);
478   }
479   if (fTrdTrigger) {
480     fTrdTrigger->~AliESDTrdTrigger();
481     new (fTrdTrigger) AliESDTrdTrigger();
482   }
483   #ifdef MFT_UPGRADE
484   //if(fESDMFT){
485 //      fESDMFT->~AliESDMFT();
486 //      new (fESDMFT) AliESDMFT();
487  // }  
488   #endif
489         
490   if(fPHOSTrigger)fPHOSTrigger->DeAllocate(); 
491   if(fEMCALTrigger)fEMCALTrigger->DeAllocate(); 
492   if(fSPDPileupVertices)fSPDPileupVertices->Delete();
493   if(fTrkPileupVertices)fTrkPileupVertices->Delete();
494   if(fTracks) fTracks->Clear("C"); // Delete();
495   if(fMuonTracks)fMuonTracks->Delete();
496   if(fPmdTracks)fPmdTracks->Delete();
497   if(fTrdTracks)fTrdTracks->Delete();
498   if(fTrdTracklets)fTrdTracklets->Delete();
499   if(fV0s)fV0s->Clear();  // not dynamic // Delete();
500   if(fCascades)fCascades->Clear(); // not dynamic // Delete();
501   if(fKinks)fKinks->Clear(); // not dynamyc //Delete();
502   if(fCaloClusters)fCaloClusters->Delete();
503   if(fPHOSCells)fPHOSCells->DeleteContainer();
504   if(fEMCALCells)fEMCALCells->DeleteContainer();
505   if(fErrorLogs) fErrorLogs->Delete();
506
507   // don't reset fconnected fConnected and the list
508
509 }
510
511
512 Int_t AliESDEvent::AddV0(const AliESDv0 *v) {
513   //
514   // Add V0
515   //
516   TClonesArray &fv = *fV0s;
517   Int_t idx=fV0s->GetEntriesFast();
518   new(fv[idx]) AliESDv0(*v);
519   return idx;
520 }  
521
522 //______________________________________________________________________________
523 void AliESDEvent::Print(Option_t *) const 
524 {
525   //
526   // Print header information of the event
527   //
528   printf("ESD run information\n");
529   printf("Event # in file %d Bunch crossing # %d Orbit # %d Period # %d Run # %d Trigger %lld Magnetic field %f \n",
530          GetEventNumberInFile(),
531          GetBunchCrossNumber(),
532          GetOrbitNumber(),
533          GetPeriodNumber(),
534          GetRunNumber(),
535          GetTriggerMask(),
536          GetMagneticField() );
537   if (fPrimaryVertex)
538     printf("Vertex: (%.4f +- %.4f, %.4f +- %.4f, %.4f +- %.4f) cm\n",
539            fPrimaryVertex->GetXv(), fPrimaryVertex->GetXRes(),
540            fPrimaryVertex->GetYv(), fPrimaryVertex->GetYRes(),
541            fPrimaryVertex->GetZv(), fPrimaryVertex->GetZRes());
542   printf("Mean vertex in RUN: X=%.4f Y=%.4f Z=%.4f cm\n",
543          GetDiamondX(),GetDiamondY(),GetDiamondZ());
544   if(fSPDMult)
545     printf("SPD Multiplicity. Number of tracklets %d \n",
546            fSPDMult->GetNumberOfTracklets());
547   printf("Number of pileup primary vertices reconstructed with SPD %d\n", 
548          GetNumberOfPileupVerticesSPD());
549   printf("Number of pileup primary vertices reconstructed using the tracks %d\n",
550          GetNumberOfPileupVerticesTracks());
551   printf("Number of tracks: \n");
552   printf("                 charged   %d\n", GetNumberOfTracks());
553   printf("                 muon      %d\n", GetNumberOfMuonTracks());
554   printf("                 pmd       %d\n", GetNumberOfPmdTracks());
555   printf("                 trd       %d\n", GetNumberOfTrdTracks());
556   printf("                 trd trkl  %d\n", GetNumberOfTrdTracklets());
557   printf("                 v0        %d\n", GetNumberOfV0s());
558   printf("                 cascades  %d\n", GetNumberOfCascades());
559   printf("                 kinks     %d\n", GetNumberOfKinks());
560   if(fPHOSCells)printf("                 PHOSCells %d\n", fPHOSCells->GetNumberOfCells());
561   else printf("                 PHOSCells not in the Event\n");
562   if(fEMCALCells)printf("                 EMCALCells %d\n", fEMCALCells->GetNumberOfCells());
563   else printf("                 EMCALCells not in the Event\n");
564   printf("                 CaloClusters %d\n", GetNumberOfCaloClusters());
565   printf("                 FMD       %s\n", (fESDFMD ? "yes" : "no"));
566   printf("                 VZERO     %s\n", (fESDVZERO ? "yes" : "no"));
567   #ifdef MFT_UPGRADE
568   //printf("                 MFT     %s\n", (fESDMFT ? "yes" : "no"));
569   #endif
570         
571         
572   TObject* pHLTDecision=GetHLTTriggerDecision();
573   printf("HLT trigger decision: %s\n", pHLTDecision?pHLTDecision->GetOption():"not available");
574   if (pHLTDecision) pHLTDecision->Print("compact");
575
576   return;
577 }
578
579 void AliESDEvent::SetESDfriend(const AliESDfriend *ev) const {
580   //
581   // Attaches the complementary info to the ESD
582   //
583   if (!ev) return;
584
585   // to be sure that we set the tracks also
586   // in case of old esds 
587   // if(fESDOld)CopyFromOldESD();
588
589   Int_t ntrk=ev->GetNumberOfTracks();
590  
591   for (Int_t i=0; i<ntrk; i++) {
592     const AliESDfriendTrack *f=ev->GetTrack(i);
593     GetTrack(i)->SetFriendTrack(f);
594   }
595 }
596
597 Bool_t  AliESDEvent::RemoveKink(Int_t rm) const {
598   // ---------------------------------------------------------
599   // Remove a kink candidate and references to it from ESD,
600   // if this candidate does not come from a reconstructed decay
601   // Not yet implemented...
602   // ---------------------------------------------------------
603   Int_t last=GetNumberOfKinks()-1;
604   if ((rm<0)||(rm>last)) return kFALSE;
605
606   return kTRUE;
607 }
608
609 Bool_t  AliESDEvent::RemoveV0(Int_t rm) const {
610   // ---------------------------------------------------------
611   // Remove a V0 candidate and references to it from ESD,
612   // if this candidate does not come from a reconstructed decay
613   // ---------------------------------------------------------
614   Int_t last=GetNumberOfV0s()-1;
615   if ((rm<0)||(rm>last)) return kFALSE;
616
617   AliESDv0 *v0=GetV0(rm);
618   Int_t idxP=v0->GetPindex(), idxN=v0->GetNindex();
619
620   v0=GetV0(last);
621   Int_t lastIdxP=v0->GetPindex(), lastIdxN=v0->GetNindex();
622
623   Int_t used=0;
624
625   // Check if this V0 comes from a reconstructed decay
626   Int_t ncs=GetNumberOfCascades();
627   for (Int_t n=0; n<ncs; n++) {
628     AliESDcascade *cs=GetCascade(n);
629
630     Int_t csIdxP=cs->GetPindex();
631     Int_t csIdxN=cs->GetNindex();
632
633     if (idxP==csIdxP)
634        if (idxN==csIdxN) return kFALSE;
635
636     if (csIdxP==lastIdxP)
637        if (csIdxN==lastIdxN) used++;
638   }
639
640   //Replace the removed V0 with the last V0 
641   TClonesArray &a=*fV0s;
642   delete a.RemoveAt(rm);
643
644   if (rm==last) return kTRUE;
645
646   //v0 is pointing to the last V0 candidate... 
647   new (a[rm]) AliESDv0(*v0);
648   delete a.RemoveAt(last);
649
650   if (!used) return kTRUE;
651   
652
653   // Remap the indices of the daughters of reconstructed decays
654   for (Int_t n=0; n<ncs; n++) {
655     AliESDcascade *cs=GetCascade(n);
656
657
658     Int_t csIdxP=cs->GetPindex();
659     Int_t csIdxN=cs->GetNindex();
660
661     if (csIdxP==lastIdxP)
662       if (csIdxN==lastIdxN) {
663          cs->AliESDv0::SetIndex(1,idxP);
664          cs->AliESDv0::SetIndex(0,idxN);
665          used--;
666          if (!used) return kTRUE;
667       }
668   }
669
670   return kTRUE;
671 }
672
673 Bool_t  AliESDEvent::RemoveTrack(Int_t rm) const {
674   // ---------------------------------------------------------
675   // Remove a track and references to it from ESD,
676   // if this track does not come from a reconstructed decay
677   // ---------------------------------------------------------
678   Int_t last=GetNumberOfTracks()-1;
679   if ((rm<0)||(rm>last)) return kFALSE;
680
681   Int_t used=0;
682
683   // Check if this track comes from the reconstructed primary vertices
684   if (fTPCVertex && fTPCVertex->GetStatus()) {
685      UShort_t *primIdx=fTPCVertex->GetIndices();
686      Int_t n=fTPCVertex->GetNIndices();
687      while (n--) {
688        Int_t idx=Int_t(primIdx[n]);
689        if (rm==idx) return kFALSE;
690        if (idx==last) used++; 
691      }
692   }
693   if (fPrimaryVertex && fPrimaryVertex->GetStatus()) {
694      UShort_t *primIdx=fPrimaryVertex->GetIndices();
695      Int_t n=fPrimaryVertex->GetNIndices();
696      while (n--) {
697        Int_t idx=Int_t(primIdx[n]);
698        if (rm==idx) return kFALSE;
699        if (idx==last) used++; 
700      }
701   }
702   
703   // Check if this track comes from a reconstructed decay
704   Int_t nv0=GetNumberOfV0s();
705   for (Int_t n=0; n<nv0; n++) {
706     AliESDv0 *v0=GetV0(n);
707
708     Int_t idx=v0->GetNindex();
709     if (rm==idx) return kFALSE;
710     if (idx==last) used++;
711
712     idx=v0->GetPindex();
713     if (rm==idx) return kFALSE;
714     if (idx==last) used++;
715   }
716
717   Int_t ncs=GetNumberOfCascades();
718   for (Int_t n=0; n<ncs; n++) {
719     AliESDcascade *cs=GetCascade(n);
720
721     Int_t idx=cs->GetIndex();
722     if (rm==idx) return kFALSE;
723     if (idx==last) used++;
724
725     AliESDv0 *v0=cs;
726     idx=v0->GetNindex();
727     if (rm==idx) return kFALSE;
728     if (idx==last) used++;
729
730     idx=v0->GetPindex();
731     if (rm==idx) return kFALSE;
732     if (idx==last) used++;
733   }
734
735   Int_t nkn=GetNumberOfKinks();
736   for (Int_t n=0; n<nkn; n++) {
737     AliESDkink *kn=GetKink(n);
738
739     Int_t idx=kn->GetIndex(0);
740     if (rm==idx) return kFALSE;
741     if (idx==last) used++;
742
743     idx=kn->GetIndex(1);
744     if (rm==idx) return kFALSE;
745     if (idx==last) used++;
746   }
747
748   // Check if this track is associated with a CaloCluster
749   Int_t ncl=GetNumberOfCaloClusters();
750   for (Int_t n=0; n<ncl; n++) {
751     AliESDCaloCluster *cluster=GetCaloCluster(n);
752     TArrayI *arr=cluster->GetTracksMatched();
753     Int_t s=arr->GetSize();
754     while (s--) {
755       Int_t idx=arr->At(s);
756       if (rm==idx) return kFALSE;
757       if (idx==last) used++;     
758     }
759   }
760
761   // Replace the removed track with the last track 
762   TClonesArray &a=*fTracks;
763   delete a.RemoveAt(rm);
764
765   if (rm==last) return kTRUE;
766
767   // RS: Attention: semi-shalow copy is used: the new track will steal the dynamic content of old one
768   AliESDtrack *t=GetTrack(last);
769   t->SetID(rm);
770   new (a[rm]) AliESDtrack(t, kTRUE); // detach dynamic content of the source
771   delete a.RemoveAt(last);
772
773
774   if (!used) return kTRUE;
775   
776
777   // Remap the indices of the tracks used for the primary vertex reconstruction
778   if (fTPCVertex && fTPCVertex->GetStatus()) {
779      UShort_t *primIdx=fTPCVertex->GetIndices();
780      Int_t n=fTPCVertex->GetNIndices();
781      while (n--) {
782        Int_t idx=Int_t(primIdx[n]);
783        if (idx==last) {
784           primIdx[n]=Short_t(rm); 
785           used--;
786           if (!used) return kTRUE;
787        }
788      }
789   }  
790   if (fPrimaryVertex && fPrimaryVertex->GetStatus()) {
791      UShort_t *primIdx=fPrimaryVertex->GetIndices();
792      Int_t n=fPrimaryVertex->GetNIndices();
793      while (n--) {
794        Int_t idx=Int_t(primIdx[n]);
795        if (idx==last) {
796           primIdx[n]=Short_t(rm); 
797           used--;
798           if (!used) return kTRUE;
799        }
800      }
801   }  
802
803   // Remap the indices of the daughters of reconstructed decays
804   for (Int_t n=0; n<nv0; n++) {
805     AliESDv0 *v0=GetV0(n);
806     if (v0->GetIndex(0)==last) {
807        v0->SetIndex(0,rm);
808        used--;
809        if (!used) return kTRUE;
810     }
811     if (v0->GetIndex(1)==last) {
812        v0->SetIndex(1,rm);
813        used--;
814        if (!used) return kTRUE;
815     }
816   }
817
818   for (Int_t n=0; n<ncs; n++) {
819     AliESDcascade *cs=GetCascade(n);
820     if (cs->GetIndex()==last) {
821        cs->SetIndex(rm);
822        used--;
823        if (!used) return kTRUE;
824     }
825     AliESDv0 *v0=cs;
826     if (v0->GetIndex(0)==last) {
827        v0->SetIndex(0,rm);
828        used--;
829        if (!used) return kTRUE;
830     }
831     if (v0->GetIndex(1)==last) {
832        v0->SetIndex(1,rm);
833        used--;
834        if (!used) return kTRUE;
835     }
836   }
837
838   for (Int_t n=0; n<nkn; n++) {
839     AliESDkink *kn=GetKink(n);
840     if (kn->GetIndex(0)==last) {
841        kn->SetIndex(rm,0);
842        used--;
843        if (!used) return kTRUE;
844     }
845     if (kn->GetIndex(1)==last) {
846        kn->SetIndex(rm,1);
847        used--;
848        if (!used) return kTRUE;
849     }
850   }
851
852   // Remap the indices of the tracks accosicated with CaloClusters
853   for (Int_t n=0; n<ncl; n++) {
854     AliESDCaloCluster *cluster=GetCaloCluster(n);
855     TArrayI *arr=cluster->GetTracksMatched();
856     Int_t s=arr->GetSize();
857     while (s--) {
858       Int_t idx=arr->At(s);
859       if (idx==last) {
860          arr->AddAt(rm,s);
861          used--; 
862          if (!used) return kTRUE;
863       }
864     }
865   }
866
867   return kTRUE;
868 }
869
870
871 Bool_t AliESDEvent::Clean(Float_t *cleanPars) {
872   //
873   // Remove the data which are not needed for the physics analysis.
874   //
875   // 1) Cleaning the V0 candidates
876   //    ---------------------------
877   //    If the cosine of the V0 pointing angle "csp" and 
878   //    the DCA between the daughter tracks "dca" does not satisfy 
879   //    the conditions 
880   //
881   //     csp > cleanPars[1] + dca/cleanPars[0]*(1.- cleanPars[1])
882   //
883   //    an attempt to remove this V0 candidate from ESD is made.
884   //
885   //    The V0 candidate gets removed if it does not belong to any 
886   //    recosntructed cascade decay
887   //
888   //    12.11.2007, optimal values: cleanPars[0]=0.5, cleanPars[1]=0.999
889   //
890   // 2) Cleaning the tracks
891   //    ----------------------
892   //    If track's transverse parameter is larger than cleanPars[2]
893   //                       OR
894   //    track's longitudinal parameter is larger than cleanPars[3]
895   //    an attempt to remove this track from ESD is made.
896   //
897   //    The track gets removed if it does not come 
898   //    from a reconstructed decay
899   //
900   Bool_t rc=kFALSE;
901
902   Float_t dcaMax=cleanPars[0];
903   Float_t cspMin=cleanPars[1];
904
905   Int_t nV0s=GetNumberOfV0s();
906   for (Int_t i=nV0s-1; i>=0; i--) {
907     AliESDv0 *v0=GetV0(i);
908
909     Float_t dca=v0->GetDcaV0Daughters();
910     Float_t csp=v0->GetV0CosineOfPointingAngle();
911     Float_t cspcut=cspMin + dca/dcaMax*(1.-cspMin);
912     if (csp > cspcut) continue;
913     if (RemoveV0(i)) rc=kTRUE;
914   }
915
916
917   Float_t dmax=cleanPars[2], zmax=cleanPars[3];
918
919   const AliESDVertex *vertex=GetPrimaryVertexSPD();
920   Bool_t vtxOK=vertex->GetStatus();
921   
922   Int_t nTracks=GetNumberOfTracks();
923   for (Int_t i=nTracks-1; i>=0; i--) {
924     AliESDtrack *track=GetTrack(i);
925     Float_t xy,z; track->GetImpactParameters(xy,z);
926     if ((TMath::Abs(xy) > dmax) || (vtxOK && (TMath::Abs(z) > zmax))) {
927       if (RemoveTrack(i)) rc=kTRUE;
928     }
929   }
930
931   return rc;
932 }
933
934 Char_t  AliESDEvent::AddPileupVertexSPD(const AliESDVertex *vtx) 
935 {
936     // Add a pileup primary vertex reconstructed with SPD
937     TClonesArray &ftr = *fSPDPileupVertices;
938     Char_t n=Char_t(ftr.GetEntriesFast());
939     AliESDVertex *vertex = new(ftr[n]) AliESDVertex(*vtx);
940     vertex->SetID(n);
941     return n;
942 }
943
944 Char_t  AliESDEvent::AddPileupVertexTracks(const AliESDVertex *vtx) 
945 {
946     // Add a pileup primary vertex reconstructed with SPD
947     TClonesArray &ftr = *fTrkPileupVertices;
948     Char_t n=Char_t(ftr.GetEntriesFast());
949     AliESDVertex *vertex = new(ftr[n]) AliESDVertex(*vtx);
950     vertex->SetID(n);
951     return n;
952 }
953
954 Int_t  AliESDEvent::AddTrack(const AliESDtrack *t) 
955 {
956     // Add track
957     TClonesArray &ftr = *fTracks;
958     AliESDtrack * track = new(ftr[fTracks->GetEntriesFast()])AliESDtrack(*t);
959     track->SetID(fTracks->GetEntriesFast()-1);
960     return  track->GetID();    
961 }
962
963 AliESDtrack*  AliESDEvent::NewTrack() 
964 {
965     // Add a new track
966     TClonesArray &ftr = *fTracks;
967     AliESDtrack * track = new(ftr[fTracks->GetEntriesFast()])AliESDtrack();
968     track->SetID(fTracks->GetEntriesFast()-1);
969     return  track;
970 }
971
972  void AliESDEvent::AddMuonTrack(const AliESDMuonTrack *t) 
973 {
974     TClonesArray &fmu = *fMuonTracks;
975     new(fmu[fMuonTracks->GetEntriesFast()]) AliESDMuonTrack(*t);
976 }
977
978 void AliESDEvent::AddPmdTrack(const AliESDPmdTrack *t) 
979 {
980   TClonesArray &fpmd = *fPmdTracks;
981   new(fpmd[fPmdTracks->GetEntriesFast()]) AliESDPmdTrack(*t);
982 }
983
984 void AliESDEvent::SetTrdTrigger(const AliESDTrdTrigger *t)
985 {
986   *fTrdTrigger = *t;
987 }
988
989 void AliESDEvent::AddTrdTrack(const AliESDTrdTrack *t) 
990 {
991   TClonesArray &ftrd = *fTrdTracks;
992   new(ftrd[fTrdTracks->GetEntriesFast()]) AliESDTrdTrack(*t);
993 }
994
995 void AliESDEvent::AddTrdTracklet(const AliESDTrdTracklet *trkl)
996 {
997   new ((*fTrdTracklets)[fTrdTracklets->GetEntriesFast()]) AliESDTrdTracklet(*trkl);
998 }
999
1000 Int_t AliESDEvent::AddKink(const AliESDkink *c) 
1001 {
1002     // Add kink
1003     TClonesArray &fk = *fKinks;
1004     AliESDkink * kink = new(fk[fKinks->GetEntriesFast()]) AliESDkink(*c);
1005     kink->SetID(fKinks->GetEntriesFast()); // CKB different from the other imps..
1006     return fKinks->GetEntriesFast()-1;
1007 }
1008
1009
1010 void AliESDEvent::AddCascade(const AliESDcascade *c) 
1011 {
1012   TClonesArray &fc = *fCascades;
1013   new(fc[fCascades->GetEntriesFast()]) AliESDcascade(*c);
1014 }
1015
1016
1017 Int_t AliESDEvent::AddCaloCluster(const AliESDCaloCluster *c) 
1018 {
1019     // Add calocluster
1020     TClonesArray &fc = *fCaloClusters;
1021     AliESDCaloCluster *clus = new(fc[fCaloClusters->GetEntriesFast()]) AliESDCaloCluster(*c);
1022     clus->SetID(fCaloClusters->GetEntriesFast()-1);
1023     return fCaloClusters->GetEntriesFast()-1;
1024   }
1025
1026
1027 void  AliESDEvent::AddRawDataErrorLog(const AliRawDataErrorLog *log) const {
1028   TClonesArray &errlogs = *fErrorLogs;
1029   new(errlogs[errlogs.GetEntriesFast()])  AliRawDataErrorLog(*log);
1030 }
1031
1032 void AliESDEvent::SetZDCData(AliESDZDC * obj)
1033
1034   // use already allocated space
1035   if(fESDZDC)
1036     *fESDZDC = *obj;
1037 }
1038
1039 void  AliESDEvent::SetPrimaryVertexTPC(const AliESDVertex *vertex) 
1040 {
1041   // Set the TPC vertex
1042   // use already allocated space
1043   if(fTPCVertex){
1044     *fTPCVertex = *vertex;
1045     fTPCVertex->SetName(fgkESDListName[kTPCVertex]);
1046   }
1047 }
1048
1049 void  AliESDEvent::SetPrimaryVertexSPD(const AliESDVertex *vertex) 
1050 {
1051   // Set the SPD vertex
1052   // use already allocated space
1053   if(fSPDVertex){
1054     *fSPDVertex = *vertex;
1055     fSPDVertex->SetName(fgkESDListName[kSPDVertex]);
1056   }
1057 }
1058
1059 void  AliESDEvent::SetPrimaryVertexTracks(const AliESDVertex *vertex) 
1060 {
1061   // Set the primary vertex reconstructed using he ESD tracks.
1062   // use already allocated space
1063   if(fPrimaryVertex){
1064     *fPrimaryVertex = *vertex;
1065     fPrimaryVertex->SetName(fgkESDListName[kPrimaryVertex]);
1066   }
1067 }
1068
1069 const AliESDVertex * AliESDEvent::GetPrimaryVertex() const 
1070 {
1071   //
1072   // Get the "best" available reconstructed primary vertex.
1073   //
1074   if(fPrimaryVertex){
1075     if (fPrimaryVertex->GetStatus()) return fPrimaryVertex;
1076   }
1077   if(fSPDVertex){
1078     if (fSPDVertex->GetStatus()) return fSPDVertex;
1079   }
1080   if(fTPCVertex) return fTPCVertex;
1081   
1082   AliWarning("No primary vertex available. Returning the \"default\"...");
1083   return fSPDVertex;
1084 }
1085
1086 AliESDVertex * AliESDEvent::PrimaryVertexTracksUnconstrained() const 
1087 {
1088   //
1089   // Removes diamond constraint from fPrimaryVertex (reconstructed with tracks)
1090   // Returns a AliESDVertex which has to be deleted by the user
1091   //
1092   if(!fPrimaryVertex) {
1093     AliWarning("No primary vertex from tracks available.");
1094     return 0;
1095   }
1096   if(!fPrimaryVertex->GetStatus()) {
1097     AliWarning("No primary vertex from tracks available.");
1098     return 0;
1099   }
1100
1101   AliVertexerTracks vertexer(GetMagneticField());
1102   Float_t diamondxyz[3]={(Float_t)GetDiamondX(),(Float_t)GetDiamondY(),0.};
1103   Float_t diamondcovxy[3]; GetDiamondCovXY(diamondcovxy);
1104   Float_t diamondcov[6]={diamondcovxy[0],diamondcovxy[1],diamondcovxy[2],0.,0.,7.};
1105   AliESDVertex *vertex = 
1106     (AliESDVertex*)vertexer.RemoveConstraintFromVertex(fPrimaryVertex,diamondxyz,diamondcov);
1107
1108   return vertex;
1109 }
1110
1111 void AliESDEvent::SetMultiplicity(const AliMultiplicity *mul) 
1112 {
1113   // Set the SPD Multiplicity
1114   if(fSPDMult){
1115     *fSPDMult = *mul;
1116   }
1117 }
1118
1119
1120 void AliESDEvent::SetFMDData(AliESDFMD * obj) 
1121
1122   // use already allocated space
1123   if(fESDFMD){
1124     *fESDFMD = *obj;
1125   }
1126 }
1127
1128 void AliESDEvent::SetVZEROData(AliESDVZERO * obj)
1129
1130   // use already allocated space
1131   if(fESDVZERO)
1132     *fESDVZERO = *obj;
1133 }
1134
1135 void AliESDEvent::SetTZEROData(AliESDTZERO * obj)
1136
1137   // use already allocated space
1138   if(fESDTZERO)
1139     *fESDTZERO = *obj;
1140 }
1141
1142 #ifdef MFT_UPGRADE
1143 //void AliESDEvent::SetMFTData(AliESDMFT * obj)
1144 //{ 
1145 //  if(fESDMFT)
1146 //      *fESDMFT = *obj;
1147 //}
1148 #endif
1149
1150 void AliESDEvent::SetACORDEData(AliESDACORDE * obj)
1151 {
1152   if(fESDACORDE)
1153     *fESDACORDE = *obj;
1154 }
1155
1156 void AliESDEvent::DetachFriends() const
1157 {
1158   // clean friend track pointers of ESDtracks
1159   Int_t ntrk=GetNumberOfTracks();
1160   for (Int_t i=0; i<ntrk; i++) {
1161     AliESDtrack *t=GetTrack(i);
1162     t->ReleaseESDfriendTrackGently();
1163   }
1164 }
1165
1166 void AliESDEvent::GetESDfriend(AliESDfriend *ev) const 
1167 {
1168   //
1169   // Extracts the complementary info from the ESD
1170   //
1171   if (!ev) return;
1172
1173   Int_t ntrk=GetNumberOfTracks();
1174
1175   for (Int_t i=0; i<ntrk; i++) {
1176     AliESDtrack *t=GetTrack(i);
1177     AliESDfriendTrack *f = (AliESDfriendTrack*)t->GetFriendTrack();
1178     ev->AddTrackShallow(f);
1179     t->ReleaseESDfriendTrackGently();// Not to have two copies of "friendTrack"
1180   }
1181
1182   AliESDfriend *fr = (AliESDfriend*)(const_cast<AliESDEvent*>(this)->FindListObject("AliESDfriend"));
1183   if (fr) ev->SetVZEROfriend(fr->GetVZEROfriend());
1184 }
1185
1186 void AliESDEvent::AddObject(TObject* obj) 
1187 {
1188   // Add an object to the list of object.
1189   // Please be aware that in order to increase performance you should
1190   // refrain from using TObjArrays (if possible). Use TClonesArrays, instead.
1191   fESDObjects->SetOwner(kTRUE);
1192   fESDObjects->AddLast(obj);
1193 }
1194
1195
1196 void AliESDEvent::GetStdContent() 
1197 {
1198   // set pointers for standard content
1199   // get by name much safer and not a big overhead since not called very often
1200  
1201   fESDRun = (AliESDRun*)fESDObjects->FindObject(fgkESDListName[kESDRun]);
1202   fHeader = (AliESDHeader*)fESDObjects->FindObject(fgkESDListName[kHeader]);
1203   fESDZDC = (AliESDZDC*)fESDObjects->FindObject(fgkESDListName[kESDZDC]);
1204   fESDFMD = (AliESDFMD*)fESDObjects->FindObject(fgkESDListName[kESDFMD]);
1205   fESDVZERO = (AliESDVZERO*)fESDObjects->FindObject(fgkESDListName[kESDVZERO]);
1206   fESDTZERO = (AliESDTZERO*)fESDObjects->FindObject(fgkESDListName[kESDTZERO]);
1207   fTPCVertex = (AliESDVertex*)fESDObjects->FindObject(fgkESDListName[kTPCVertex]);
1208   fSPDVertex = (AliESDVertex*)fESDObjects->FindObject(fgkESDListName[kSPDVertex]);
1209   fPrimaryVertex = (AliESDVertex*)fESDObjects->FindObject(fgkESDListName[kPrimaryVertex]);
1210   fSPDMult =       (AliMultiplicity*)fESDObjects->FindObject(fgkESDListName[kSPDMult]);
1211   fPHOSTrigger = (AliESDCaloTrigger*)fESDObjects->FindObject(fgkESDListName[kPHOSTrigger]);
1212   fEMCALTrigger = (AliESDCaloTrigger*)fESDObjects->FindObject(fgkESDListName[kEMCALTrigger]);
1213   fSPDPileupVertices = (TClonesArray*)fESDObjects->FindObject(fgkESDListName[kSPDPileupVertices]);
1214   fTrkPileupVertices = (TClonesArray*)fESDObjects->FindObject(fgkESDListName[kTrkPileupVertices]);
1215   fTracks = (TClonesArray*)fESDObjects->FindObject(fgkESDListName[kTracks]);
1216   fMuonTracks = (TClonesArray*)fESDObjects->FindObject(fgkESDListName[kMuonTracks]);
1217   fPmdTracks = (TClonesArray*)fESDObjects->FindObject(fgkESDListName[kPmdTracks]);
1218   fTrdTrigger = (AliESDTrdTrigger*)fESDObjects->FindObject(fgkESDListName[kTrdTrigger]);
1219   fTrdTracks = (TClonesArray*)fESDObjects->FindObject(fgkESDListName[kTrdTracks]);
1220   fTrdTracklets = (TClonesArray*)fESDObjects->FindObject(fgkESDListName[kTrdTracklets]);
1221   fV0s = (TClonesArray*)fESDObjects->FindObject(fgkESDListName[kV0s]);
1222   fCascades = (TClonesArray*)fESDObjects->FindObject(fgkESDListName[kCascades]);
1223   fKinks = (TClonesArray*)fESDObjects->FindObject(fgkESDListName[kKinks]);
1224   fCaloClusters = (TClonesArray*)fESDObjects->FindObject(fgkESDListName[kCaloClusters]);
1225   fEMCALCells = (AliESDCaloCells*)fESDObjects->FindObject(fgkESDListName[kEMCALCells]);
1226   fPHOSCells = (AliESDCaloCells*)fESDObjects->FindObject(fgkESDListName[kPHOSCells]);
1227   fErrorLogs = (TClonesArray*)fESDObjects->FindObject(fgkESDListName[kErrorLogs]);
1228   fESDACORDE = (AliESDACORDE*)fESDObjects->FindObject(fgkESDListName[kESDACORDE]);
1229   fTOFHeader = (AliTOFHeader*)fESDObjects->FindObject(fgkESDListName[kTOFHeader]);
1230   #ifdef MFT_UPGRADE
1231  // fESDMFT = (AliESDMFT*)fESDObjects->FindObject(fgkESDListName[kESDMFT]);
1232   #endif
1233 }
1234
1235 void AliESDEvent::SetStdNames(){
1236   // Set the names of the standard contents
1237   // 
1238   if(fESDObjects->GetEntries()>=kESDListN){
1239     for(int i = 0;i < fESDObjects->GetEntries() && i<kESDListN;i++){
1240       TObject *fObj = fESDObjects->At(i);
1241       if(fObj->InheritsFrom("TNamed")){
1242         ((TNamed*)fObj)->SetName(fgkESDListName[i]);
1243       }
1244       else if(fObj->InheritsFrom("TClonesArray")){
1245         ((TClonesArray*)fObj)->SetName(fgkESDListName[i]);
1246       }
1247     }
1248   }
1249   else{
1250      AliWarning("Std Entries missing");
1251   }
1252
1253
1254
1255 void AliESDEvent::CreateStdContent(Bool_t bUseThisList){
1256   fUseOwnList = bUseThisList;
1257   CreateStdContent();
1258 }
1259
1260 void AliESDEvent::CreateStdContent() 
1261 {
1262   // create the standard AOD content and set pointers
1263
1264   // create standard objects and add them to the TList of objects
1265   AddObject(new AliESDRun());
1266   AddObject(new AliESDHeader());
1267   AddObject(new AliESDZDC());
1268   AddObject(new AliESDFMD());
1269   AddObject(new AliESDVZERO());
1270   AddObject(new AliESDTZERO());
1271   AddObject(new AliESDVertex());
1272   AddObject(new AliESDVertex());
1273   AddObject(new AliESDVertex());
1274   AddObject(new AliMultiplicity());
1275   AddObject(new AliESDCaloTrigger());
1276   AddObject(new AliESDCaloTrigger());
1277   AddObject(new TClonesArray("AliESDVertex",0));
1278   AddObject(new TClonesArray("AliESDVertex",0));
1279   AddObject(new TClonesArray("AliESDtrack",0));
1280   AddObject(new TClonesArray("AliESDMuonTrack",0));
1281   AddObject(new TClonesArray("AliESDPmdTrack",0));
1282   AddObject(new AliESDTrdTrigger());
1283   AddObject(new TClonesArray("AliESDTrdTrack",0));
1284   AddObject(new TClonesArray("AliESDTrdTracklet",0));
1285   AddObject(new TClonesArray("AliESDv0",0));
1286   AddObject(new TClonesArray("AliESDcascade",0));
1287   AddObject(new TClonesArray("AliESDkink",0));
1288   AddObject(new TClonesArray("AliESDCaloCluster",0));
1289   AddObject(new AliESDCaloCells());
1290   AddObject(new AliESDCaloCells());
1291   AddObject(new TClonesArray("AliRawDataErrorLog",0));
1292   AddObject(new AliESDACORDE()); 
1293   AddObject(new AliTOFHeader());
1294   #ifdef MFT_UPGRADE
1295   //AddObject(new AliESDMFT());
1296   #endif
1297         
1298   // check the order of the indices against enum...
1299
1300   // set names
1301   SetStdNames();
1302   // read back pointers
1303   GetStdContent();
1304 }
1305
1306 TObject* AliESDEvent::FindListObject(const char *name) const {
1307 //
1308 // Find object with name "name" in the list of branches
1309 //
1310   if(fESDObjects){
1311     return fESDObjects->FindObject(name);
1312   }
1313   return 0;
1314
1315
1316 Int_t AliESDEvent::GetPHOSClusters(TRefArray *clusters) const
1317 {
1318   // fills the provided TRefArray with all found phos clusters
1319   
1320   clusters->Clear();
1321   
1322   AliESDCaloCluster *cl = 0;
1323   for (Int_t i = 0; i < GetNumberOfCaloClusters(); i++) {
1324     
1325     if ( (cl = GetCaloCluster(i)) ) {
1326       if (cl->IsPHOS()){
1327         clusters->Add(cl);
1328         AliDebug(1,Form("IsPHOS cluster %d Size: %d \n",i,clusters->GetEntriesFast()));
1329       }
1330     }
1331   }
1332   return clusters->GetEntriesFast();
1333 }
1334
1335 Int_t AliESDEvent::GetEMCALClusters(TRefArray *clusters) const
1336 {
1337   // fills the provided TRefArray with all found emcal clusters
1338
1339   clusters->Clear();
1340
1341   AliESDCaloCluster *cl = 0;
1342   for (Int_t i = 0; i < GetNumberOfCaloClusters(); i++) {
1343
1344     if ( (cl = GetCaloCluster(i)) ) {
1345       if (cl->IsEMCAL()){
1346         clusters->Add(cl);
1347         AliDebug(1,Form("IsEMCAL cluster %d Size: %d \n",i,clusters->GetEntriesFast()));
1348       }
1349     }
1350   }
1351   return clusters->GetEntriesFast();
1352 }
1353
1354 void AliESDEvent::WriteToTree(TTree* tree) const {
1355   // Book the branches as in TTree::Branch(TCollection*)
1356   // but add a "." at the end of top level branches which are
1357   // not a TClonesArray
1358
1359
1360   TString branchname;
1361   TIter next(fESDObjects);
1362   const Int_t kSplitlevel = 99; // default value in TTree::Branch()
1363   const Int_t kBufsize = 32000/4; // default value in TTree::Branch()
1364   TObject *obj = 0;
1365
1366   while ((obj = next())) {
1367     branchname.Form("%s", obj->GetName());
1368     if(branchname.CompareTo("AliESDfriend")==0)branchname = "ESDfriend.";
1369     if ((kSplitlevel > 1) &&  !obj->InheritsFrom(TClonesArray::Class())) {
1370       if(!branchname.EndsWith("."))branchname += ".";
1371     }
1372     if (!tree->FindBranch(branchname)) {
1373       // For the custom streamer to be called splitlevel
1374       // has to be negative, only needed for HLT
1375       Int_t splitLevel = (TString(obj->ClassName()) == "AliHLTGlobalTriggerDecision") ? -1 : kSplitlevel - 1;
1376       tree->Bronch(branchname, obj->ClassName(), fESDObjects->GetObjectRef(obj),kBufsize, splitLevel);
1377     }
1378   }
1379 }
1380
1381
1382 void AliESDEvent::ReadFromTree(TTree *tree, Option_t* opt){
1383 //
1384 // Connect the ESDEvent to a tree
1385 //
1386   if(!tree){
1387     AliWarning("AliESDEvent::ReadFromTree() Zero Pointer to Tree \n");
1388     return;
1389   }
1390   // load the TTree
1391   if(!tree->GetTree())tree->LoadTree(0);
1392
1393   // if we find the "ESD" branch on the tree we do have the old structure
1394   if(tree->GetBranch("ESD")) {
1395     char ** address  = (char **)(tree->GetBranch("ESD")->GetAddress());
1396     // do we have the friend branch
1397     TBranch * esdFB = tree->GetBranch("ESDfriend.");
1398     char ** addressF = 0;
1399     if(esdFB)addressF = (char **)(esdFB->GetAddress());
1400     if (!address) {
1401       AliInfo("AliESDEvent::ReadFromTree() Reading old Tree");
1402       tree->SetBranchAddress("ESD",       &fESDOld);
1403       if(esdFB){
1404         tree->SetBranchAddress("ESDfriend.",&fESDFriendOld);
1405       }
1406     } else {
1407       AliInfo("AliESDEvent::ReadFromTree() Reading old Tree");
1408       AliInfo("Branch already connected. Using existing branch address.");
1409       fESDOld       = (AliESD*)       (*address);
1410       // addressF can still be 0, since branch needs to switched on
1411       if(addressF)fESDFriendOld = (AliESDfriend*) (*addressF);
1412     }
1413                                        
1414     //  have already connected the old ESD structure... ?
1415     // reuse also the pointer of the AlliESDEvent
1416     // otherwise create new ones
1417     TList* connectedList = (TList*) (tree->GetUserInfo()->FindObject("ESDObjectsConnectedToTree"));
1418   
1419     if(connectedList){
1420       // If connected use the connected list of objects
1421       if(fESDObjects!= connectedList){
1422         // protect when called twice 
1423         fESDObjects->Delete();
1424         fESDObjects = connectedList;
1425       }
1426       GetStdContent(); 
1427
1428       
1429       // The pointer to the friend changes when called twice via InitIO
1430       // since AliESDEvent is deleted
1431       TObject* oldf = FindListObject("AliESDfriend");
1432       TObject* newf = 0;
1433       if(addressF){
1434         newf = (TObject*)*addressF;
1435       }
1436       if(newf!=0&&oldf!=newf){
1437         // remove the old reference
1438         // Should we also delete it? Or is this handled in TTree I/O
1439         // since it is created by the first SetBranchAddress
1440         fESDObjects->Remove(oldf);
1441         // add the new one 
1442         fESDObjects->Add(newf);
1443       }
1444       
1445       fConnected = true;
1446       return;
1447     }
1448     // else...    
1449     CreateStdContent(); // create for copy
1450     // if we have the esdfriend add it, so we always can access it via the userinfo
1451     if(fESDFriendOld)AddObject(fESDFriendOld);
1452     // we are not owner of the list objects 
1453     // must not delete it
1454     fESDObjects->SetOwner(kTRUE);
1455     fESDObjects->SetName("ESDObjectsConnectedToTree");
1456     tree->GetUserInfo()->Add(fESDObjects);
1457     fConnected = true;
1458     return;
1459   }
1460   
1461
1462     delete fESDOld;
1463     fESDOld = 0;
1464   // Try to find AliESDEvent
1465   AliESDEvent *esdEvent = 0;
1466   esdEvent = (AliESDEvent*)tree->GetTree()->GetUserInfo()->FindObject("AliESDEvent");
1467   if(esdEvent){   
1468       // Check if already connected to tree
1469     esdEvent->Reset();
1470     TList* connectedList = (TList*) (tree->GetUserInfo()->FindObject("ESDObjectsConnectedToTree"));
1471
1472     
1473     if (connectedList && (strcmp(opt, "reconnect"))) {
1474       // If connected use the connected list if objects
1475       fESDObjects->Delete();
1476       fESDObjects = connectedList;
1477       GetStdContent(); 
1478       fConnected = true;
1479       return;
1480     }
1481
1482     // Connect to tree
1483     // prevent a memory leak when reading back the TList
1484     // if (!(strcmp(opt, "reconnect"))) fESDObjects->Delete();
1485     
1486     if(!fUseOwnList){
1487       // create a new TList from the UserInfo TList... 
1488       // copy constructor does not work...
1489       fESDObjects = (TList*)(esdEvent->GetList()->Clone());
1490       fESDObjects->SetOwner(kTRUE);
1491     }
1492     else if ( fESDObjects->GetEntries()==0){
1493       // at least create the std content if we want to read to our list
1494       CreateStdContent(); 
1495     }
1496
1497     // in principle
1498     // we only need new things in the list if we do no already have it..
1499     // TODO just add new entries
1500
1501     if(fESDObjects->GetEntries()<kESDListN){
1502       AliWarning(Form("AliESDEvent::ReadFromTree() TList contains less than the standard contents %d < %d \n",
1503                       fESDObjects->GetEntries(),kESDListN));
1504     }
1505     // set the branch addresses
1506     TIter next(fESDObjects);
1507     TNamed *el;
1508     while((el=(TNamed*)next())){
1509       TString bname(el->GetName());
1510       if(bname.CompareTo("AliESDfriend")==0)
1511         {
1512           // AliESDfriend does not have a name ...
1513             TBranch *br = tree->GetBranch("ESDfriend.");
1514             if (br) tree->SetBranchAddress("ESDfriend.",fESDObjects->GetObjectRef(el));
1515         }
1516       else{
1517         // check if branch exists under this Name
1518         TBranch *br = tree->GetBranch(bname.Data());
1519         if(br){
1520           tree->SetBranchAddress(bname.Data(),fESDObjects->GetObjectRef(el));
1521         }
1522         else{
1523           br = tree->GetBranch(Form("%s.",bname.Data()));
1524           if(br){
1525             tree->SetBranchAddress(Form("%s.",bname.Data()),fESDObjects->GetObjectRef(el));
1526           }
1527           else{
1528             AliWarning(Form("AliESDEvent::ReadFromTree() No Branch found with Name %s or %s.",bname.Data(),bname.Data()));
1529           }
1530
1531         }
1532       }
1533     }
1534     GetStdContent();
1535     // when reading back we are not owner of the list 
1536     // must not delete it
1537     fESDObjects->SetOwner(kTRUE);
1538     fESDObjects->SetName("ESDObjectsConnectedToTree");
1539     // we are not owner of the list objects 
1540     // must not delete it
1541     tree->GetUserInfo()->Add(fESDObjects);
1542     tree->GetUserInfo()->SetOwner(kFALSE);
1543     fConnected = true;
1544   }// no esdEvent -->
1545   else {
1546     // we can't get the list from the user data, create standard content
1547     // and set it by hand (no ESDfriend at the moment
1548     CreateStdContent();
1549     TIter next(fESDObjects);
1550     TNamed *el;
1551     while((el=(TNamed*)next())){
1552       TString bname(el->GetName());    
1553       TBranch *br = tree->GetBranch(bname.Data());
1554       if(br){
1555         tree->SetBranchAddress(bname.Data(),fESDObjects->GetObjectRef(el));
1556       }
1557       else{
1558         br = tree->GetBranch(Form("%s.",bname.Data()));
1559         if(br){
1560           tree->SetBranchAddress(Form("%s.",bname.Data()),fESDObjects->GetObjectRef(el));
1561         }
1562       }
1563     }
1564     GetStdContent();
1565     // when reading back we are not owner of the list 
1566     // must not delete it
1567     fESDObjects->SetOwner(kTRUE);
1568   }
1569 }
1570
1571
1572 void AliESDEvent::CopyFromOldESD()
1573 {
1574   // Method which copies over everthing from the old esd structure to the 
1575   // new  
1576   if(fESDOld){
1577     ResetStdContent();
1578      // Run
1579     SetRunNumber(fESDOld->GetRunNumber());
1580     SetPeriodNumber(fESDOld->GetPeriodNumber());
1581     SetMagneticField(fESDOld->GetMagneticField());
1582   
1583     // leave out diamond ...
1584     // SetDiamond(const AliESDVertex *vertex) { fESDRun->SetDiamond(vertex);}
1585
1586     // header
1587     SetTriggerMask(fESDOld->GetTriggerMask());
1588     SetOrbitNumber(fESDOld->GetOrbitNumber());
1589     SetTimeStamp(fESDOld->GetTimeStamp());
1590     SetEventType(fESDOld->GetEventType());
1591     SetEventNumberInFile(fESDOld->GetEventNumberInFile());
1592     SetBunchCrossNumber(fESDOld->GetBunchCrossNumber());
1593     SetTriggerCluster(fESDOld->GetTriggerCluster());
1594
1595     // ZDC
1596
1597     SetZDC(fESDOld->GetZDCN1Energy(),
1598            fESDOld->GetZDCP1Energy(),
1599            fESDOld->GetZDCEMEnergy(),
1600            0,
1601            fESDOld->GetZDCN2Energy(),
1602            fESDOld->GetZDCP2Energy(),
1603            fESDOld->GetZDCParticipants(),
1604            0,
1605            0,
1606            0,
1607            0,
1608            0,
1609            0);
1610
1611     // FMD
1612     
1613     if(fESDOld->GetFMDData())SetFMDData(fESDOld->GetFMDData());
1614
1615     // T0
1616
1617     SetT0zVertex(fESDOld->GetT0zVertex());
1618     SetT0(fESDOld->GetT0());
1619     //  leave amps out
1620
1621     // VZERO
1622     if (fESDOld->GetVZEROData()) SetVZEROData(fESDOld->GetVZEROData());
1623
1624     if(fESDOld->GetVertex())SetPrimaryVertexSPD(fESDOld->GetVertex());
1625
1626     if(fESDOld->GetPrimaryVertex())SetPrimaryVertexTracks(fESDOld->GetPrimaryVertex());
1627
1628     if(fESDOld->GetMultiplicity())SetMultiplicity(fESDOld->GetMultiplicity());
1629
1630     for(int i = 0;i<fESDOld->GetNumberOfTracks();i++){
1631       AddTrack(fESDOld->GetTrack(i));
1632     }
1633
1634     for(int i = 0;i<fESDOld->GetNumberOfMuonTracks();i++){
1635       AddMuonTrack(fESDOld->GetMuonTrack(i));
1636     }
1637
1638     for(int i = 0;i<fESDOld->GetNumberOfPmdTracks();i++){
1639       AddPmdTrack(fESDOld->GetPmdTrack(i));
1640     }
1641
1642     for(int i = 0;i<fESDOld->GetNumberOfTrdTracks();i++){
1643       AddTrdTrack(fESDOld->GetTrdTrack(i));
1644     }
1645
1646     for(int i = 0;i<fESDOld->GetNumberOfV0s();i++){
1647       AddV0(fESDOld->GetV0(i));
1648     }
1649
1650     for(int i = 0;i<fESDOld->GetNumberOfCascades();i++){
1651       AddCascade(fESDOld->GetCascade(i));
1652     }
1653
1654     for(int i = 0;i<fESDOld->GetNumberOfKinks();i++){
1655       AddKink(fESDOld->GetKink(i));
1656     }
1657
1658
1659     for(int i = 0;i<fESDOld->GetNumberOfCaloClusters();i++){
1660       AddCaloCluster(fESDOld->GetCaloCluster(i));
1661     }
1662           
1663         #ifdef MFT_UPGRADE  
1664         // MFT
1665 //      if (fESDOld->GetMFTData()) SetMFTData(fESDOld->GetMFTData());
1666     #endif
1667
1668   }// if fesdold
1669 }
1670
1671 Bool_t AliESDEvent::IsEventSelected(const char *trigExpr) const
1672 {
1673   // Check if the event satisfies the trigger
1674   // selection expression trigExpr.
1675   // trigExpr can be any logical expression
1676   // of the trigger classes defined in AliESDRun
1677   // In case of wrong syntax return kTRUE.
1678
1679   TString expr(trigExpr);
1680   if (expr.IsNull()) return kTRUE;
1681
1682   ULong64_t mask = GetTriggerMask();
1683   for(Int_t itrig = 0; itrig < AliESDRun::kNTriggerClasses; itrig++) {
1684     if (mask & (1ull << itrig)) {
1685       expr.ReplaceAll(GetESDRun()->GetTriggerClass(itrig),"1");
1686     }
1687     else {
1688       expr.ReplaceAll(GetESDRun()->GetTriggerClass(itrig),"0");
1689     }
1690   }
1691
1692   Int_t error;
1693   if ((gROOT->ProcessLineFast(expr.Data(),&error) == 0) &&
1694       (error == TInterpreter::kNoError)) {
1695     return kFALSE;
1696   }
1697
1698   return kTRUE;
1699
1700 }
1701
1702 TObject*  AliESDEvent::GetHLTTriggerDecision() const
1703 {
1704   // get the HLT trigger decission object
1705
1706   // cast away const'nes because the FindListObject method
1707   // is not const
1708   AliESDEvent* pNonConst=const_cast<AliESDEvent*>(this);
1709   return pNonConst->FindListObject("HLTGlobalTrigger");
1710 }
1711
1712 TString   AliESDEvent::GetHLTTriggerDescription() const
1713 {
1714   // get the HLT trigger decission description
1715   TString description;
1716   TObject* pDecision=GetHLTTriggerDecision();
1717   if (pDecision) {
1718     description=pDecision->GetTitle();
1719   }
1720
1721   return description;
1722 }
1723
1724 Bool_t    AliESDEvent::IsHLTTriggerFired(const char* name) const
1725 {
1726   // get the HLT trigger decission description
1727   TObject* pDecision=GetHLTTriggerDecision();
1728   if (!pDecision) return kFALSE;
1729
1730   Option_t* option=pDecision->GetOption();
1731   if (option==NULL || *option!='1') return kFALSE;
1732
1733   if (name) {
1734     TString description=GetHLTTriggerDescription();
1735     Int_t index=description.Index(name);
1736     if (index<0) return kFALSE;
1737     index+=strlen(name);
1738     if (index>=description.Length()) return kFALSE;
1739     if (description[index]!=0 && description[index]!=' ') return kFALSE;
1740   }
1741   return kTRUE;
1742 }
1743
1744 //______________________________________________________________________________
1745 Bool_t  AliESDEvent::IsPileupFromSPD(Int_t minContributors, 
1746                                      Double_t minZdist, 
1747                                      Double_t nSigmaZdist, 
1748                                      Double_t nSigmaDiamXY, 
1749                                      Double_t nSigmaDiamZ) const{
1750   //
1751   // This function checks if there was a pile up
1752   // reconstructed with SPD
1753   //
1754   Int_t nc1=fSPDVertex->GetNContributors();
1755   if(nc1<1) return kFALSE;
1756   Int_t nPileVert=GetNumberOfPileupVerticesSPD();
1757   if(nPileVert==0) return kFALSE;
1758   
1759   for(Int_t i=0; i<nPileVert;i++){
1760     const AliESDVertex* pv=GetPileupVertexSPD(i);
1761     Int_t nc2=pv->GetNContributors();
1762     if(nc2>=minContributors){
1763       Double_t z1=fSPDVertex->GetZ();
1764       Double_t z2=pv->GetZ();
1765       Double_t distZ=TMath::Abs(z2-z1);
1766       Double_t distZdiam=TMath::Abs(z2-GetDiamondZ());
1767       Double_t cutZdiam=nSigmaDiamZ*TMath::Sqrt(GetSigma2DiamondZ());
1768       if(GetSigma2DiamondZ()<0.0001)cutZdiam=99999.; //protection for missing z diamond information
1769       if(distZ>minZdist && distZdiam<cutZdiam){
1770         Double_t x2=pv->GetX();
1771         Double_t y2=pv->GetY();
1772         Double_t distXdiam=TMath::Abs(x2-GetDiamondX());
1773         Double_t distYdiam=TMath::Abs(y2-GetDiamondY());
1774         Double_t cov1[6],cov2[6];       
1775         fSPDVertex->GetCovarianceMatrix(cov1);
1776         pv->GetCovarianceMatrix(cov2);
1777         Double_t errxDist=TMath::Sqrt(cov2[0]+GetSigma2DiamondX());
1778         Double_t erryDist=TMath::Sqrt(cov2[2]+GetSigma2DiamondY());
1779         Double_t errzDist=TMath::Sqrt(cov1[5]+cov2[5]);
1780         Double_t cutXdiam=nSigmaDiamXY*errxDist;
1781         if(GetSigma2DiamondX()<0.0001)cutXdiam=99999.; //protection for missing diamond information
1782         Double_t cutYdiam=nSigmaDiamXY*erryDist;
1783         if(GetSigma2DiamondY()<0.0001)cutYdiam=99999.; //protection for missing diamond information
1784         if( (distXdiam<cutXdiam) && (distYdiam<cutYdiam) && (distZ>nSigmaZdist*errzDist) ){
1785           return kTRUE;
1786         }
1787       }
1788     }
1789   }
1790   return kFALSE;
1791 }
1792
1793 //______________________________________________________________________________
1794 void AliESDEvent::EstimateMultiplicity(Int_t &tracklets, Int_t &trITSTPC, Int_t &trITSSApure, Double_t eta, Bool_t useDCAFlag,Bool_t useV0Flag) const
1795 {
1796   //
1797   // calculates 3 estimators for the multiplicity in the -eta:eta range
1798   // tracklets   : using SPD tracklets only
1799   // trITSTPC    : using TPC/ITS + complementary ITS SA tracks + tracklets from clusters not used by tracks
1800   // trITSSApure : using ITS standalone tracks + tracklets from clusters not used by tracks
1801   // if useDCAFlag is true: account for the ESDtrack flag marking the tracks with large DCA
1802   // if useV0Flag  is true: account for the ESDtrack flag marking conversion and K0's V0s
1803   tracklets = trITSSApure = trITSTPC = 0;
1804   int ntr = fSPDMult ? fSPDMult->GetNumberOfTracklets() : 0;
1805   //
1806   // count tracklets
1807   for (int itr=ntr;itr--;) { 
1808     if (TMath::Abs(fSPDMult->GetEta(itr))>eta) continue;
1809     tracklets++;
1810     if (fSPDMult->FreeClustersTracklet(itr,0)) trITSTPC++;    // not used in ITS/TPC or ITS_SA track
1811     if (fSPDMult->FreeClustersTracklet(itr,1)) trITSSApure++; // not used in ITS_SA_Pure track
1812   }
1813   //
1814   // count real tracks
1815   ntr = GetNumberOfTracks();
1816   for (int itr=ntr;itr--;) {
1817     AliESDtrack *t = GetTrack(itr);
1818     if (TMath::Abs(t->Eta())>eta) continue;
1819     if (!t->IsOn(AliESDtrack::kITSin)) continue;
1820     if (useDCAFlag && t->IsOn(AliESDtrack::kMultSec))  continue;
1821     if (useV0Flag  && t->IsOn(AliESDtrack::kMultInV0)) continue;    
1822     if (t->IsOn(AliESDtrack::kITSpureSA)) trITSSApure++;
1823     else                                  trITSTPC++;
1824   }
1825   //
1826 }
1827
1828 Bool_t AliESDEvent::IsPileupFromSPDInMultBins() const {
1829     Int_t nTracklets=GetMultiplicity()->GetNumberOfTracklets();
1830     if(nTracklets<20) return IsPileupFromSPD(3,0.8);
1831     else if(nTracklets<50) return IsPileupFromSPD(4,0.8);
1832     else return IsPileupFromSPD(5,0.8);
1833 }
1834
1835 void  AliESDEvent::SetTOFHeader(const AliTOFHeader *header)
1836 {
1837   //
1838   // Set the TOF event_time
1839   //
1840
1841   if (fTOFHeader) {
1842     *fTOFHeader=*header;
1843     //fTOFHeader->SetName(fgkESDListName[kTOFHeader]);
1844   }
1845   else {
1846     // for analysis of reconstructed events
1847     // when this information is not avaliable
1848     fTOFHeader = new AliTOFHeader(*header);
1849     //AddObject(fTOFHeader);
1850   }
1851
1852 }
1853
1854 AliCentrality* AliESDEvent::GetCentrality()
1855 {
1856     if (!fCentrality) fCentrality = new AliCentrality();
1857     return  fCentrality;
1858 }
1859
1860 AliEventplane* AliESDEvent::GetEventplane()
1861 {
1862     if (!fEventplane) fEventplane = new AliEventplane();
1863     return  fEventplane;
1864 }
1865
1866 Float_t AliESDEvent::GetVZEROEqMultiplicity(Int_t i) const
1867 {
1868   // Get VZERO Multiplicity for channel i
1869   // Themethod uses the equalization factors
1870   // stored in the ESD-run object in order to
1871   // get equal multiplicities within a VZERO rins (1/8 of VZERO)
1872   if (!fESDVZERO || !fESDRun) return -1;
1873
1874   Int_t ring = i/8;
1875   Float_t factorSum = 0;
1876   for(Int_t j = 8*ring; j < (8*ring+8); ++j) {
1877     factorSum += fESDRun->GetVZEROEqFactors(j);
1878   }
1879   Float_t factor = fESDRun->GetVZEROEqFactors(i)*8./factorSum;
1880
1881   return (fESDVZERO->GetMultiplicity(i)/factor);
1882 }