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