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