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