]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/ESD/AliESDEvent.cxx
Changes for #90436: Misuse of TClonesArray containing AliESDMuonCluster
[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 new MUON containers if missing (for backward compatibility)
1494   if (!fESDObjects->FindObject(fgkESDListName[kMuonClusters])) {
1495     TClonesArray* muonClusters = new TClonesArray("AliESDMuonCluster",0);
1496     muonClusters->SetName(fgkESDListName[kMuonClusters]);
1497     fESDObjects->AddAt(muonClusters, kMuonClusters);
1498     fESDObjects->SetOwner(kTRUE);
1499   }
1500   if (!fESDObjects->FindObject(fgkESDListName[kMuonPads])) {
1501     TClonesArray* muonPads = new TClonesArray("AliESDMuonPad",0);
1502     muonPads->SetName(fgkESDListName[kMuonPads]);
1503     fESDObjects->AddAt(muonPads, kMuonPads);
1504     fESDObjects->SetOwner(kTRUE);
1505   }
1506 }
1507
1508 TObject* AliESDEvent::FindListObject(const char *name) const {
1509 //
1510 // Find object with name "name" in the list of branches
1511 //
1512   if(fESDObjects){
1513     return fESDObjects->FindObject(name);
1514   }
1515   return 0;
1516
1517
1518 Int_t AliESDEvent::GetPHOSClusters(TRefArray *clusters) const
1519 {
1520   // fills the provided TRefArray with all found phos clusters
1521   
1522   clusters->Clear();
1523   
1524   AliESDCaloCluster *cl = 0;
1525   for (Int_t i = 0; i < GetNumberOfCaloClusters(); i++) {
1526     
1527     if ( (cl = GetCaloCluster(i)) ) {
1528       if (cl->IsPHOS()){
1529         clusters->Add(cl);
1530         AliDebug(1,Form("IsPHOS cluster %d Size: %d \n",i,clusters->GetEntriesFast()));
1531       }
1532     }
1533   }
1534   return clusters->GetEntriesFast();
1535 }
1536
1537 Int_t AliESDEvent::GetEMCALClusters(TRefArray *clusters) const
1538 {
1539   // fills the provided TRefArray with all found emcal clusters
1540
1541   clusters->Clear();
1542
1543   AliESDCaloCluster *cl = 0;
1544   for (Int_t i = 0; i < GetNumberOfCaloClusters(); i++) {
1545
1546     if ( (cl = GetCaloCluster(i)) ) {
1547       if (cl->IsEMCAL()){
1548         clusters->Add(cl);
1549         AliDebug(1,Form("IsEMCAL cluster %d Size: %d \n",i,clusters->GetEntriesFast()));
1550       }
1551     }
1552   }
1553   return clusters->GetEntriesFast();
1554 }
1555
1556 void AliESDEvent::WriteToTree(TTree* tree) const {
1557   // Book the branches as in TTree::Branch(TCollection*)
1558   // but add a "." at the end of top level branches which are
1559   // not a TClonesArray
1560
1561
1562   TString branchname;
1563   TIter next(fESDObjects);
1564   const Int_t kSplitlevel = 99; // default value in TTree::Branch()
1565   const Int_t kBufsize = 32000; // default value in TTree::Branch()
1566   TObject *obj = 0;
1567
1568   while ((obj = next())) {
1569     branchname.Form("%s", obj->GetName());
1570     if(branchname.CompareTo("AliESDfriend")==0)branchname = "ESDfriend.";
1571     if ((kSplitlevel > 1) &&  !obj->InheritsFrom(TClonesArray::Class())) {
1572       if(!branchname.EndsWith("."))branchname += ".";
1573     }
1574     if (!tree->FindBranch(branchname)) {
1575       // For the custom streamer to be called splitlevel
1576       // has to be negative, only needed for HLT
1577       Int_t splitLevel = (TString(obj->ClassName()) == "AliHLTGlobalTriggerDecision") ? -1 : kSplitlevel - 1;
1578       tree->Bronch(branchname, obj->ClassName(), fESDObjects->GetObjectRef(obj),kBufsize, splitLevel);
1579     }
1580   }
1581 }
1582
1583
1584 void AliESDEvent::ReadFromTree(TTree *tree, Option_t* opt){
1585 //
1586 // Connect the ESDEvent to a tree
1587 //
1588   if(!tree){
1589     AliWarning("AliESDEvent::ReadFromTree() Zero Pointer to Tree \n");
1590     return;
1591   }
1592   // load the TTree
1593   if(!tree->GetTree())tree->LoadTree(0);
1594
1595   // if we find the "ESD" branch on the tree we do have the old structure
1596   if(tree->GetBranch("ESD")) {
1597     fOldMuonStructure = kFALSE;
1598     char ** address  = (char **)(tree->GetBranch("ESD")->GetAddress());
1599     // do we have the friend branch
1600     TBranch * esdFB = tree->GetBranch("ESDfriend.");
1601     char ** addressF = 0;
1602     if(esdFB)addressF = (char **)(esdFB->GetAddress());
1603     if (!address) {
1604       AliInfo("AliESDEvent::ReadFromTree() Reading old Tree");
1605       tree->SetBranchAddress("ESD",       &fESDOld);
1606       if(esdFB){
1607         tree->SetBranchAddress("ESDfriend.",&fESDFriendOld);
1608       }
1609     } else {
1610       AliInfo("AliESDEvent::ReadFromTree() Reading old Tree");
1611       AliInfo("Branch already connected. Using existing branch address.");
1612       fESDOld       = (AliESD*)       (*address);
1613       // addressF can still be 0, since branch needs to switched on
1614       if(addressF)fESDFriendOld = (AliESDfriend*) (*addressF);
1615     }
1616                                        
1617     //  have already connected the old ESD structure... ?
1618     // reuse also the pointer of the AlliESDEvent
1619     // otherwise create new ones
1620     TList* connectedList = (TList*) (tree->GetUserInfo()->FindObject("ESDObjectsConnectedToTree"));
1621   
1622     if(connectedList){
1623       // If connected use the connected list of objects
1624       if(fESDObjects!= connectedList){
1625         // protect when called twice 
1626         fESDObjects->Delete();
1627         fESDObjects = connectedList;
1628       }
1629       GetStdContent(); 
1630
1631       
1632       // The pointer to the friend changes when called twice via InitIO
1633       // since AliESDEvent is deleted
1634       TObject* oldf = FindListObject("AliESDfriend");
1635       TObject* newf = 0;
1636       if(addressF){
1637         newf = (TObject*)*addressF;
1638       }
1639       if(newf!=0&&oldf!=newf){
1640         // remove the old reference
1641         // Should we also delete it? Or is this handled in TTree I/O
1642         // since it is created by the first SetBranchAddress
1643         fESDObjects->Remove(oldf);
1644         // add the new one 
1645         fESDObjects->Add(newf);
1646       }
1647       
1648       fConnected = true;
1649       return;
1650     }
1651     // else...    
1652     CreateStdContent(); // create for copy
1653     // if we have the esdfriend add it, so we always can access it via the userinfo
1654     if(fESDFriendOld)AddObject(fESDFriendOld);
1655     // we are not owner of the list objects 
1656     // must not delete it
1657     fESDObjects->SetOwner(kTRUE);
1658     fESDObjects->SetName("ESDObjectsConnectedToTree");
1659     tree->GetUserInfo()->Add(fESDObjects);
1660     fConnected = true;
1661     return;
1662   }
1663   
1664
1665     delete fESDOld;
1666     fESDOld = 0;
1667   // Try to find AliESDEvent
1668   AliESDEvent *esdEvent = 0;
1669   esdEvent = (AliESDEvent*)tree->GetTree()->GetUserInfo()->FindObject("AliESDEvent");
1670   if(esdEvent){   
1671       // Check if already connected to tree
1672     esdEvent->Reset();
1673     TList* connectedList = (TList*) (tree->GetUserInfo()->FindObject("ESDObjectsConnectedToTree"));
1674
1675     
1676     if (connectedList && (strcmp(opt, "reconnect"))) {
1677       // If connected use the connected list if objects
1678       fESDObjects->Delete();
1679       fESDObjects = connectedList;
1680       GetStdContent(); 
1681       fOldMuonStructure = fESDObjects->TestBit(BIT(23));
1682       fConnected = true;
1683       return;
1684     }
1685
1686     // Connect to tree
1687     // prevent a memory leak when reading back the TList
1688     // if (!(strcmp(opt, "reconnect"))) fESDObjects->Delete();
1689     
1690     if(!fUseOwnList){
1691       // create a new TList from the UserInfo TList... 
1692       // copy constructor does not work...
1693       fESDObjects = (TList*)(esdEvent->GetList()->Clone());
1694       fESDObjects->SetOwner(kTRUE);
1695     }
1696     else if ( fESDObjects->GetEntries()==0){
1697       // at least create the std content if we want to read to our list
1698       CreateStdContent(); 
1699     }
1700
1701     // in principle
1702     // we only need new things in the list if we do no already have it..
1703     // TODO just add new entries
1704     CompleteStdContent();
1705
1706     if(fESDObjects->GetEntries()<kESDListN){
1707       AliWarning(Form("AliESDEvent::ReadFromTree() TList contains less than the standard contents %d < %d \n",
1708                       fESDObjects->GetEntries(),kESDListN));
1709     }
1710     // set the branch addresses
1711     fOldMuonStructure = kFALSE;
1712     TIter next(fESDObjects);
1713     TNamed *el;
1714     while((el=(TNamed*)next())){
1715       TString bname(el->GetName());
1716       if(bname.CompareTo("AliESDfriend")==0)
1717         {
1718           // AliESDfriend does not have a name ...
1719             TBranch *br = tree->GetBranch("ESDfriend.");
1720             if (br) tree->SetBranchAddress("ESDfriend.",fESDObjects->GetObjectRef(el));
1721         }
1722       else{
1723         // check if branch exists under this Name
1724         TBranch *br = tree->GetBranch(bname.Data());
1725         if(br){
1726           tree->SetBranchAddress(bname.Data(),fESDObjects->GetObjectRef(el));
1727         }
1728         else{
1729           br = tree->GetBranch(Form("%s.",bname.Data()));
1730           if(br){
1731             tree->SetBranchAddress(Form("%s.",bname.Data()),fESDObjects->GetObjectRef(el));
1732           }
1733           else{
1734             AliWarning(Form("AliESDEvent::ReadFromTree() No Branch found with Name %s or %s.",bname.Data(),bname.Data()));
1735             if (bname == fgkESDListName[kMuonClusters]) {
1736               fOldMuonStructure = kTRUE;
1737             }
1738           }
1739
1740         }
1741       }
1742     }
1743     GetStdContent();
1744     // when reading back we are not owner of the list 
1745     // must not delete it
1746     fESDObjects->SetOwner(kTRUE);
1747     fESDObjects->SetName("ESDObjectsConnectedToTree");
1748     fESDObjects->SetBit(BIT(23), fOldMuonStructure);
1749     // we are not owner of the list objects 
1750     // must not delete it
1751     tree->GetUserInfo()->Add(fESDObjects);
1752     tree->GetUserInfo()->SetOwner(kFALSE);
1753     fConnected = true;
1754   }// no esdEvent -->
1755   else {
1756     // we can't get the list from the user data, create standard content
1757     // and set it by hand (no ESDfriend at the moment
1758     CreateStdContent();
1759     fOldMuonStructure = kFALSE;
1760     TIter next(fESDObjects);
1761     TNamed *el;
1762     while((el=(TNamed*)next())){
1763       TString bname(el->GetName());    
1764       TBranch *br = tree->GetBranch(bname.Data());
1765       if(br){
1766         tree->SetBranchAddress(bname.Data(),fESDObjects->GetObjectRef(el));
1767       }
1768       else{
1769         br = tree->GetBranch(Form("%s.",bname.Data()));
1770         if(br){
1771           tree->SetBranchAddress(Form("%s.",bname.Data()),fESDObjects->GetObjectRef(el));
1772         }
1773         else if (bname == fgkESDListName[kMuonClusters]) {
1774           fOldMuonStructure = kTRUE;
1775         }
1776       }
1777     }
1778     GetStdContent();
1779     // when reading back we are not owner of the list 
1780     // must not delete it
1781     fESDObjects->SetOwner(kTRUE);
1782   }
1783 }
1784
1785
1786 void AliESDEvent::CopyFromOldESD()
1787 {
1788   // Method which copies over everthing from the old esd structure to the 
1789   // new  
1790   if(fESDOld){
1791     ResetStdContent();
1792      // Run
1793     SetRunNumber(fESDOld->GetRunNumber());
1794     SetPeriodNumber(fESDOld->GetPeriodNumber());
1795     SetMagneticField(fESDOld->GetMagneticField());
1796   
1797     // leave out diamond ...
1798     // SetDiamond(const AliESDVertex *vertex) { fESDRun->SetDiamond(vertex);}
1799
1800     // header
1801     SetTriggerMask(fESDOld->GetTriggerMask());
1802     SetOrbitNumber(fESDOld->GetOrbitNumber());
1803     SetTimeStamp(fESDOld->GetTimeStamp());
1804     SetEventType(fESDOld->GetEventType());
1805     SetEventNumberInFile(fESDOld->GetEventNumberInFile());
1806     SetBunchCrossNumber(fESDOld->GetBunchCrossNumber());
1807     SetTriggerCluster(fESDOld->GetTriggerCluster());
1808
1809     // ZDC
1810
1811     SetZDC(fESDOld->GetZDCN1Energy(),
1812            fESDOld->GetZDCP1Energy(),
1813            fESDOld->GetZDCEMEnergy(),
1814            0,
1815            fESDOld->GetZDCN2Energy(),
1816            fESDOld->GetZDCP2Energy(),
1817            fESDOld->GetZDCParticipants(),
1818            0,
1819            0,
1820            0,
1821            0,
1822            0,
1823            0);
1824
1825     // FMD
1826     
1827     if(fESDOld->GetFMDData())SetFMDData(fESDOld->GetFMDData());
1828
1829     // T0
1830
1831     SetT0zVertex(fESDOld->GetT0zVertex());
1832     SetT0(fESDOld->GetT0());
1833     //  leave amps out
1834
1835     // VZERO
1836     if (fESDOld->GetVZEROData()) SetVZEROData(fESDOld->GetVZEROData());
1837
1838     if(fESDOld->GetVertex())SetPrimaryVertexSPD(fESDOld->GetVertex());
1839
1840     if(fESDOld->GetPrimaryVertex())SetPrimaryVertexTracks(fESDOld->GetPrimaryVertex());
1841
1842     if(fESDOld->GetMultiplicity())SetMultiplicity(fESDOld->GetMultiplicity());
1843
1844     for(int i = 0;i<fESDOld->GetNumberOfTracks();i++){
1845       AddTrack(fESDOld->GetTrack(i));
1846     }
1847
1848     for(int i = 0;i<fESDOld->GetNumberOfMuonTracks();i++){
1849       AddMuonTrack(fESDOld->GetMuonTrack(i));
1850     }
1851
1852     for(int i = 0;i<fESDOld->GetNumberOfPmdTracks();i++){
1853       AddPmdTrack(fESDOld->GetPmdTrack(i));
1854     }
1855
1856     for(int i = 0;i<fESDOld->GetNumberOfTrdTracks();i++){
1857       AddTrdTrack(fESDOld->GetTrdTrack(i));
1858     }
1859
1860     for(int i = 0;i<fESDOld->GetNumberOfV0s();i++){
1861       AddV0(fESDOld->GetV0(i));
1862     }
1863
1864     for(int i = 0;i<fESDOld->GetNumberOfCascades();i++){
1865       AddCascade(fESDOld->GetCascade(i));
1866     }
1867
1868     for(int i = 0;i<fESDOld->GetNumberOfKinks();i++){
1869       AddKink(fESDOld->GetKink(i));
1870     }
1871
1872
1873     for(int i = 0;i<fESDOld->GetNumberOfCaloClusters();i++){
1874       AddCaloCluster(fESDOld->GetCaloCluster(i));
1875     }
1876           
1877         #ifdef MFT_UPGRADE  
1878         // MFT
1879 //      if (fESDOld->GetMFTData()) SetMFTData(fESDOld->GetMFTData());
1880     #endif
1881
1882   }// if fesdold
1883 }
1884
1885 Bool_t AliESDEvent::IsEventSelected(const char *trigExpr) const
1886 {
1887   // Check if the event satisfies the trigger
1888   // selection expression trigExpr.
1889   // trigExpr can be any logical expression
1890   // of the trigger classes defined in AliESDRun
1891   // In case of wrong syntax return kTRUE.
1892
1893   TString expr(trigExpr);
1894   if (expr.IsNull()) return kTRUE;
1895
1896   ULong64_t mask = GetTriggerMask();
1897   for(Int_t itrig = 0; itrig < AliESDRun::kNTriggerClasses; itrig++) {
1898     if (mask & (1ull << itrig)) {
1899       expr.ReplaceAll(GetESDRun()->GetTriggerClass(itrig),"1");
1900     }
1901     else {
1902       expr.ReplaceAll(GetESDRun()->GetTriggerClass(itrig),"0");
1903     }
1904   }
1905
1906   Int_t error;
1907   if ((gROOT->ProcessLineFast(expr.Data(),&error) == 0) &&
1908       (error == TInterpreter::kNoError)) {
1909     return kFALSE;
1910   }
1911
1912   return kTRUE;
1913
1914 }
1915
1916 TObject*  AliESDEvent::GetHLTTriggerDecision() const
1917 {
1918   // get the HLT trigger decission object
1919
1920   // cast away const'nes because the FindListObject method
1921   // is not const
1922   AliESDEvent* pNonConst=const_cast<AliESDEvent*>(this);
1923   return pNonConst->FindListObject("HLTGlobalTrigger");
1924 }
1925
1926 TString   AliESDEvent::GetHLTTriggerDescription() const
1927 {
1928   // get the HLT trigger decission description
1929   TString description;
1930   TObject* pDecision=GetHLTTriggerDecision();
1931   if (pDecision) {
1932     description=pDecision->GetTitle();
1933   }
1934
1935   return description;
1936 }
1937
1938 Bool_t    AliESDEvent::IsHLTTriggerFired(const char* name) const
1939 {
1940   // get the HLT trigger decission description
1941   TObject* pDecision=GetHLTTriggerDecision();
1942   if (!pDecision) return kFALSE;
1943
1944   Option_t* option=pDecision->GetOption();
1945   if (option==NULL || *option!='1') return kFALSE;
1946
1947   if (name) {
1948     TString description=GetHLTTriggerDescription();
1949     Int_t index=description.Index(name);
1950     if (index<0) return kFALSE;
1951     index+=strlen(name);
1952     if (index>=description.Length()) return kFALSE;
1953     if (description[index]!=0 && description[index]!=' ') return kFALSE;
1954   }
1955   return kTRUE;
1956 }
1957
1958 //______________________________________________________________________________
1959 Bool_t  AliESDEvent::IsPileupFromSPD(Int_t minContributors, 
1960                                      Double_t minZdist, 
1961                                      Double_t nSigmaZdist, 
1962                                      Double_t nSigmaDiamXY, 
1963                                      Double_t nSigmaDiamZ) const{
1964   //
1965   // This function checks if there was a pile up
1966   // reconstructed with SPD
1967   //
1968   Int_t nc1=fSPDVertex->GetNContributors();
1969   if(nc1<1) return kFALSE;
1970   Int_t nPileVert=GetNumberOfPileupVerticesSPD();
1971   if(nPileVert==0) return kFALSE;
1972   
1973   for(Int_t i=0; i<nPileVert;i++){
1974     const AliESDVertex* pv=GetPileupVertexSPD(i);
1975     Int_t nc2=pv->GetNContributors();
1976     if(nc2>=minContributors){
1977       Double_t z1=fSPDVertex->GetZ();
1978       Double_t z2=pv->GetZ();
1979       Double_t distZ=TMath::Abs(z2-z1);
1980       Double_t distZdiam=TMath::Abs(z2-GetDiamondZ());
1981       Double_t cutZdiam=nSigmaDiamZ*TMath::Sqrt(GetSigma2DiamondZ());
1982       if(GetSigma2DiamondZ()<0.0001)cutZdiam=99999.; //protection for missing z diamond information
1983       if(distZ>minZdist && distZdiam<cutZdiam){
1984         Double_t x2=pv->GetX();
1985         Double_t y2=pv->GetY();
1986         Double_t distXdiam=TMath::Abs(x2-GetDiamondX());
1987         Double_t distYdiam=TMath::Abs(y2-GetDiamondY());
1988         Double_t cov1[6],cov2[6];       
1989         fSPDVertex->GetCovarianceMatrix(cov1);
1990         pv->GetCovarianceMatrix(cov2);
1991         Double_t errxDist=TMath::Sqrt(cov2[0]+GetSigma2DiamondX());
1992         Double_t erryDist=TMath::Sqrt(cov2[2]+GetSigma2DiamondY());
1993         Double_t errzDist=TMath::Sqrt(cov1[5]+cov2[5]);
1994         Double_t cutXdiam=nSigmaDiamXY*errxDist;
1995         if(GetSigma2DiamondX()<0.0001)cutXdiam=99999.; //protection for missing diamond information
1996         Double_t cutYdiam=nSigmaDiamXY*erryDist;
1997         if(GetSigma2DiamondY()<0.0001)cutYdiam=99999.; //protection for missing diamond information
1998         if( (distXdiam<cutXdiam) && (distYdiam<cutYdiam) && (distZ>nSigmaZdist*errzDist) ){
1999           return kTRUE;
2000         }
2001       }
2002     }
2003   }
2004   return kFALSE;
2005 }
2006
2007 //______________________________________________________________________________
2008 void AliESDEvent::EstimateMultiplicity(Int_t &tracklets, Int_t &trITSTPC, Int_t &trITSSApure, Double_t eta, Bool_t useDCAFlag,Bool_t useV0Flag) const
2009 {
2010   //
2011   // calculates 3 estimators for the multiplicity in the -eta:eta range
2012   // tracklets   : using SPD tracklets only
2013   // trITSTPC    : using TPC/ITS + complementary ITS SA tracks + tracklets from clusters not used by tracks
2014   // trITSSApure : using ITS standalone tracks + tracklets from clusters not used by tracks
2015   // if useDCAFlag is true: account for the ESDtrack flag marking the tracks with large DCA
2016   // if useV0Flag  is true: account for the ESDtrack flag marking conversion and K0's V0s
2017
2018   AliWarning("This obsolete method will be eliminated soon. Use AliESDtrackCuts::GetReferenceMultiplicity");
2019
2020   tracklets = trITSSApure = trITSTPC = 0;
2021   int ntr = fSPDMult ? fSPDMult->GetNumberOfTracklets() : 0;
2022   //
2023   // count tracklets
2024   for (int itr=ntr;itr--;) { 
2025     if (TMath::Abs(fSPDMult->GetEta(itr))>eta) continue;
2026     tracklets++;
2027     if (fSPDMult->FreeClustersTracklet(itr,0)) trITSTPC++;    // not used in ITS/TPC or ITS_SA track
2028     if (fSPDMult->FreeClustersTracklet(itr,1)) trITSSApure++; // not used in ITS_SA_Pure track
2029   }
2030   //
2031   // count real tracks
2032   ntr = GetNumberOfTracks();
2033   for (int itr=ntr;itr--;) {
2034     AliESDtrack *t = GetTrack(itr);
2035     if (!t) {AliFatal(Form("NULL pointer for ESD track %d",itr));}
2036     if (TMath::Abs(t->Eta())>eta) continue;
2037     if (!t->IsOn(AliESDtrack::kITSin)) continue;
2038     if (useDCAFlag && t->IsOn(AliESDtrack::kMultSec))  continue;
2039     if (useV0Flag  && t->IsOn(AliESDtrack::kMultInV0)) continue;    
2040     if (t->IsOn(AliESDtrack::kITSpureSA)) trITSSApure++;
2041     else                                  trITSTPC++;
2042   }
2043   //
2044 }
2045
2046 Bool_t AliESDEvent::IsPileupFromSPDInMultBins() const {
2047     Int_t nTracklets=GetMultiplicity()->GetNumberOfTracklets();
2048     if(nTracklets<20) return IsPileupFromSPD(3,0.8);
2049     else if(nTracklets<50) return IsPileupFromSPD(4,0.8);
2050     else return IsPileupFromSPD(5,0.8);
2051 }
2052
2053 void  AliESDEvent::SetTOFHeader(const AliTOFHeader *header)
2054 {
2055   //
2056   // Set the TOF event_time
2057   //
2058
2059   if (fTOFHeader) {
2060     *fTOFHeader=*header;
2061     //fTOFHeader->SetName(fgkESDListName[kTOFHeader]);
2062   }
2063   else {
2064     // for analysis of reconstructed events
2065     // when this information is not avaliable
2066     fTOFHeader = new AliTOFHeader(*header);
2067     //AddObject(fTOFHeader);
2068   }
2069
2070 }
2071
2072 AliCentrality* AliESDEvent::GetCentrality()
2073 {
2074     if (!fCentrality) fCentrality = new AliCentrality();
2075     return  fCentrality;
2076 }
2077
2078 AliEventplane* AliESDEvent::GetEventplane()
2079 {
2080     if (!fEventplane) fEventplane = new AliEventplane();
2081     return  fEventplane;
2082 }
2083
2084 Float_t AliESDEvent::GetVZEROEqMultiplicity(Int_t i) const
2085 {
2086   // Get VZERO Multiplicity for channel i
2087   // Themethod uses the equalization factors
2088   // stored in the ESD-run object in order to
2089   // get equal multiplicities within a VZERO rins (1/8 of VZERO)
2090   if (!fESDVZERO || !fESDRun) return -1;
2091
2092   Int_t ring = i/8;
2093   Float_t factorSum = 0;
2094   for(Int_t j = 8*ring; j < (8*ring+8); ++j) {
2095     factorSum += fESDRun->GetVZEROEqFactors(j);
2096   }
2097   Float_t factor = fESDRun->GetVZEROEqFactors(i)*8./factorSum;
2098
2099   return (fESDVZERO->GetMultiplicity(i)/factor);
2100 }