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