]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliESDEvent.cxx
Second ZDC module. More ZDC information included in the ESD and AOD classes (Chiara)
[u/mrichter/AliRoot.git] / STEER / 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
39 #include "AliESDEvent.h"
40 #include "AliESDfriend.h"
41 #include "AliESDVZERO.h"
42 #include "AliESDFMD.h"
43 #include "AliESD.h"
44 #include "AliESDMuonTrack.h"
45 #include "AliESDPmdTrack.h"
46 #include "AliESDTrdTrack.h"
47 #include "AliESDVertex.h"
48 #include "AliESDcascade.h"
49 #include "AliESDPmdTrack.h"
50 #include "AliESDTrdTrack.h"
51 #include "AliESDVertex.h"
52 #include "AliESDcascade.h"
53 #include "AliESDkink.h"
54 #include "AliESDtrack.h"
55 #include "AliESDHLTtrack.h"
56 #include "AliESDCaloCluster.h"
57 #include "AliESDv0.h"
58 #include "AliESDFMD.h"
59 #include "AliESDVZERO.h"
60 #include "AliMultiplicity.h"
61 #include "AliRawDataErrorLog.h"
62
63
64 ClassImp(AliESDEvent)
65
66
67
68 // here we define the names, some classes are no TNamed, therefore the classnames 
69 // are the Names
70   const char* AliESDEvent::fgkESDListName[kESDListN] = {"AliESDRun",
71                                                        "AliESDHeader",
72                                                        "AliESDZDC",
73                                                        "AliESDFMD",
74                                                        "AliESDVZERO",
75                                                        "AliESDTZERO",
76                                                        "SPDVertex",
77                                                        "PrimaryVertex",
78                                                        "AliMultiplicity",
79                                                        "PHOSTrigger",
80                                                        "EMCALTrigger",
81                                                        "Tracks",
82                                                        "MuonTracks",
83                                                        "PmdTracks",
84                                                        "TrdTracks",
85                                                        "V0s",
86                                                        "Cascades",
87                                                        "Kinks",
88                                                        "CaloClusters",
89                                                        "AliRawDataErrorLogs"};
90 //______________________________________________________________________________
91 AliESDEvent::AliESDEvent():
92   AliVEvent(),
93   fESDObjects(new TList()),
94   fESDRun(0),
95   fHeader(0),
96   fESDZDC(0),
97   fESDFMD(0),
98   fESDVZERO(0),
99   fESDTZERO(0),
100   fSPDVertex(0),
101   fPrimaryVertex(0),
102   fSPDMult(0),
103   fPHOSTrigger(0),
104   fEMCALTrigger(0),
105   fTracks(0),
106   fMuonTracks(0),
107   fPmdTracks(0),
108   fTrdTracks(0),
109   fV0s(0),  
110   fCascades(0),
111   fKinks(0),
112   fCaloClusters(0),
113   fErrorLogs(0),
114   fESDOld(0),
115   fESDFriendOld(0),
116   fConnected(kFALSE),
117   fEMCALClusters(0), 
118   fFirstEMCALCluster(-1),
119   fPHOSClusters(0), 
120   fFirstPHOSCluster(-1)
121 {
122 }
123 //______________________________________________________________________________
124 AliESDEvent::AliESDEvent(const AliESDEvent& esd):
125   AliVEvent(esd),
126   fESDObjects(new TList()),
127   fESDRun(new AliESDRun(*esd.fESDRun)),
128   fHeader(new AliESDHeader(*esd.fHeader)),
129   fESDZDC(new AliESDZDC(*esd.fESDZDC)),
130   fESDFMD(new AliESDFMD(*esd.fESDFMD)),
131   fESDVZERO(new AliESDVZERO(*esd.fESDVZERO)),
132   fESDTZERO(new AliESDTZERO(*esd.fESDTZERO)),
133   fSPDVertex(new AliESDVertex(*esd.fSPDVertex)),
134   fPrimaryVertex(new AliESDVertex(*esd.fPrimaryVertex)),
135   fSPDMult(new AliMultiplicity(*esd.fSPDMult)),
136   fPHOSTrigger(new AliESDCaloTrigger(*esd.fPHOSTrigger)),
137   fEMCALTrigger(new AliESDCaloTrigger(*esd.fEMCALTrigger)),
138   fTracks(new TClonesArray(*esd.fTracks)),
139   fMuonTracks(new TClonesArray(*esd.fMuonTracks)),
140   fPmdTracks(new TClonesArray(*esd.fPmdTracks)),
141   fTrdTracks(new TClonesArray(*esd.fTrdTracks)),
142   fV0s(new TClonesArray(*esd.fV0s)),  
143   fCascades(new TClonesArray(*esd.fCascades)),
144   fKinks(new TClonesArray(*esd.fKinks)),
145   fCaloClusters(new TClonesArray(*esd.fCaloClusters)),
146   fErrorLogs(new TClonesArray(*esd.fErrorLogs)),
147   fESDOld(new AliESD(*esd.fESDOld)),
148   fESDFriendOld(new AliESDfriend(*esd.fESDFriendOld)),
149   fConnected(esd.fConnected),
150   fEMCALClusters(esd.fEMCALClusters), 
151   fFirstEMCALCluster(esd.fFirstEMCALCluster),
152   fPHOSClusters(esd.fPHOSClusters), 
153   fFirstPHOSCluster(esd.fFirstPHOSCluster)
154
155 {
156   // CKB init in the constructor list and only add here ...
157   AddObject(fESDRun);
158   AddObject(fHeader);
159   AddObject(fESDZDC);
160   AddObject(fESDFMD);
161   AddObject(fESDVZERO);
162   AddObject(fESDTZERO);
163   AddObject(fSPDVertex);
164   AddObject(fPrimaryVertex);
165   AddObject(fSPDMult);
166   AddObject(fPHOSTrigger);
167   AddObject(fEMCALTrigger);
168   AddObject(fTracks);
169   AddObject(fMuonTracks);
170   AddObject(fPmdTracks);
171   AddObject(fTrdTracks);
172   AddObject(fV0s);
173   AddObject(fCascades);
174   AddObject(fKinks);
175   AddObject(fCaloClusters);
176   AddObject(fErrorLogs);
177
178   GetStdContent();
179
180 }
181
182 //______________________________________________________________________________
183 AliESDEvent & AliESDEvent::operator=(const AliESDEvent& source) {
184
185   // Assignment operator
186
187   if(&source == this) return *this;
188   AliVEvent::operator=(source);
189
190   fESDRun = new AliESDRun(*source.fESDRun);
191   fHeader = new AliESDHeader(*source.fHeader);
192   fESDZDC = new AliESDZDC(*source.fESDZDC);
193   fESDFMD = new AliESDFMD(*source.fESDFMD);
194   fESDVZERO = new AliESDVZERO(*source.fESDVZERO);
195   fESDTZERO = new AliESDTZERO(*source.fESDTZERO);
196   fSPDVertex = new AliESDVertex(*source.fSPDVertex);
197   fPrimaryVertex = new AliESDVertex(*source.fPrimaryVertex);
198   fSPDMult = new AliMultiplicity(*source.fSPDMult);
199   fPHOSTrigger = new AliESDCaloTrigger(*source.fPHOSTrigger);
200   fEMCALTrigger = new AliESDCaloTrigger(*source.fEMCALTrigger);
201   fTracks = new TClonesArray(*source.fTracks);
202   fMuonTracks = new TClonesArray(*source.fMuonTracks);
203   fPmdTracks = new TClonesArray(*source.fPmdTracks);
204   fTrdTracks = new TClonesArray(*source.fTrdTracks);
205   fV0s = new TClonesArray(*source.fV0s);
206   fCascades = new TClonesArray(*source.fCascades);
207   fKinks = new TClonesArray(*source.fKinks);
208   fCaloClusters = new TClonesArray(*source.fCaloClusters);
209   fErrorLogs = new TClonesArray(*source.fErrorLogs);
210   fESDOld       = new AliESD(*source.fESDOld);
211   fESDFriendOld = new AliESDfriend(*source.fESDFriendOld);
212   // CKB this way?? or 
213   // or AddObject(  fESDZDC = new AliESDZDC(*source.fESDZDC));
214
215   fESDObjects = new TList();
216   AddObject(fESDRun);
217   AddObject(fHeader);
218   AddObject(fESDZDC);
219   AddObject(fESDFMD);
220   AddObject(fESDVZERO);
221   AddObject(fESDTZERO);
222   AddObject(fSPDVertex);
223   AddObject(fPrimaryVertex);
224   AddObject(fSPDMult);
225   AddObject(fPHOSTrigger);
226   AddObject(fEMCALTrigger);
227   AddObject(fTracks);
228   AddObject(fMuonTracks);
229   AddObject(fPmdTracks);
230   AddObject(fTrdTracks);
231   AddObject(fV0s);
232   AddObject(fCascades);
233   AddObject(fKinks);
234   AddObject(fCaloClusters);
235   AddObject(fErrorLogs);
236
237   fConnected = source.fConnected;
238   fEMCALClusters = source.fEMCALClusters;
239   fFirstEMCALCluster = source.fFirstEMCALCluster;
240   fPHOSClusters = source.fPHOSClusters;
241   fFirstPHOSCluster = source.fFirstPHOSCluster;
242
243
244
245   return *this;
246
247 }
248
249
250 //______________________________________________________________________________
251 AliESDEvent::~AliESDEvent()
252 {
253   //
254   // Standard destructor
255   //
256
257   // everthing on the list gets deleted automatically
258
259   
260   if(fESDObjects&&!fConnected)
261     {
262       delete fESDObjects;
263       fESDObjects = 0;
264     }
265
266   
267 }
268
269 //______________________________________________________________________________
270 void AliESDEvent::Reset()
271 {
272
273   
274   // Reset the standard contents
275   ResetStdContent(); 
276   if(fESDOld)fESDOld->Reset();
277   // call reset for user supplied data?
278 }
279
280 void AliESDEvent::ResetStdContent()
281 {
282   // Reset the standard contents
283   if(fESDRun) fESDRun->Reset();
284   if(fHeader) fHeader->Reset();
285   if(fESDZDC) fESDZDC->Reset();
286   if(fESDFMD) fESDFMD->Clear(); // why clear.... need consistend names
287   if(fESDVZERO){
288     // reset by callin d'to /c'tor keep the pointer
289     fESDVZERO->~AliESDVZERO();
290     new (fESDVZERO) AliESDVZERO();
291   }  
292   if(fESDTZERO) fESDTZERO->Reset(); 
293   // CKB no clear/reset implemented
294   if(fSPDVertex){
295     fSPDVertex->~AliESDVertex();
296     new (fSPDVertex) AliESDVertex();
297     fSPDVertex->SetName(fgkESDListName[kSPDVertex]);
298   }
299   if(fPrimaryVertex){
300     fPrimaryVertex->~AliESDVertex();
301     new (fPrimaryVertex) AliESDVertex();
302     fPrimaryVertex->SetName(fgkESDListName[kPrimaryVertex]);
303   }
304   if(fSPDMult){
305     fSPDMult->~AliMultiplicity();
306     new (fSPDMult) AliMultiplicity();
307   }
308   if(fPHOSTrigger)fPHOSTrigger->Reset(); 
309   if(fEMCALTrigger)fEMCALTrigger->Reset(); 
310   if(fTracks)fTracks->Delete();
311   if(fMuonTracks)fMuonTracks->Delete();
312   if(fPmdTracks)fPmdTracks->Delete();
313   if(fTrdTracks)fTrdTracks->Delete();
314   if(fV0s)fV0s->Delete();
315   if(fCascades)fCascades->Delete();
316   if(fKinks)fKinks->Delete();
317   if(fCaloClusters)fCaloClusters->Delete();
318   if(fErrorLogs) fErrorLogs->Delete();
319
320   // don't reset fconnected fConnected ;
321
322   fEMCALClusters=0; 
323   fFirstEMCALCluster=-1; 
324   fPHOSClusters=0; 
325   fFirstPHOSCluster=-1; 
326 }
327
328
329 Int_t AliESDEvent::AddV0(const AliESDv0 *v) {
330   //
331   // Add V0
332   //
333   TClonesArray &fv = *fV0s;
334   Int_t idx=fV0s->GetEntriesFast();
335   new(fv[idx]) AliESDv0(*v);
336   return idx;
337 }  
338
339 //______________________________________________________________________________
340 void AliESDEvent::Print(Option_t *) const 
341 {
342   //
343   // Print header information of the event
344   //
345   printf("ESD run information\n");
346   printf("Event # in file %d Bunch crossing # %d Orbit # %d Period # %d Run # %d Trigger %lld Magnetic field %f \n",
347          GetEventNumberInFile(),
348          GetBunchCrossNumber(),
349          GetOrbitNumber(),
350          GetPeriodNumber(),
351          GetRunNumber(),
352          GetTriggerMask(),
353          GetMagneticField() );
354   printf("Vertex: (%.4f +- %.4f, %.4f +- %.4f, %.4f +- %.4f) cm\n",
355            fPrimaryVertex->GetXv(), fPrimaryVertex->GetXRes(),
356            fPrimaryVertex->GetYv(), fPrimaryVertex->GetYRes(),
357            fPrimaryVertex->GetZv(), fPrimaryVertex->GetZRes());
358     printf("Mean vertex in RUN: X=%.4f Y=%.4f cm\n",
359            GetDiamondX(),GetDiamondY());
360     printf("SPD Multiplicity. Number of tracklets %d \n",
361            fSPDMult->GetNumberOfTracklets());
362   printf("Number of tracks: \n");
363   printf("                 charged   %d\n", GetNumberOfTracks());
364   printf("                 muon      %d\n", GetNumberOfMuonTracks());
365   printf("                 pmd       %d\n", GetNumberOfPmdTracks());
366   printf("                 trd       %d\n", GetNumberOfTrdTracks());
367   printf("                 v0        %d\n", GetNumberOfV0s());
368   printf("                 cascades  %d\n", GetNumberOfCascades());
369   printf("                 kinks     %d\n", GetNumberOfKinks());
370   printf("                 CaloClusters %d\n", GetNumberOfCaloClusters());
371   printf("                 phos      %d\n", GetNumberOfPHOSClusters());
372   printf("                 emcal     %d\n", GetNumberOfEMCALClusters());
373   printf("                 FMD       %s\n", (fESDFMD ? "yes" : "no"));
374   printf("                 VZERO     %s\n", (fESDVZERO ? "yes" : "no"));
375
376   return;
377 }
378
379 void AliESDEvent::SetESDfriend(const AliESDfriend *ev) {
380   //
381   // Attaches the complementary info to the ESD
382   //
383   if (!ev) return;
384
385   // to be sure that we set the tracks also
386   // in case of old esds 
387   // if(fESDOld)CopyFromOldESD();
388
389   Int_t ntrk=ev->GetNumberOfTracks();
390  
391   for (Int_t i=0; i<ntrk; i++) {
392     const AliESDfriendTrack *f=ev->GetTrack(i);
393     GetTrack(i)->SetFriendTrack(f);
394   }
395 }
396
397 Bool_t  AliESDEvent::RemoveKink(Int_t rm) {
398   // ---------------------------------------------------------
399   // Remove a kink candidate and references to it from ESD,
400   // if this candidate does not come from a reconstructed decay
401   // Not yet implemented...
402   // ---------------------------------------------------------
403   Int_t last=GetNumberOfKinks()-1;
404   if ((rm<0)||(rm>last)) return kFALSE;
405
406   return kTRUE;
407 }
408
409 Bool_t  AliESDEvent::RemoveV0(Int_t rm) {
410   // ---------------------------------------------------------
411   // Remove a V0 candidate and references to it from ESD,
412   // if this candidate does not come from a reconstructed decay
413   // ---------------------------------------------------------
414   Int_t last=GetNumberOfV0s()-1;
415   if ((rm<0)||(rm>last)) return kFALSE;
416
417   AliESDv0 *v0=GetV0(rm);
418   Int_t idxP=v0->GetPindex(), idxN=v0->GetNindex();
419
420   v0=GetV0(last);
421   Int_t lastIdxP=v0->GetPindex(), lastIdxN=v0->GetNindex();
422
423   Int_t used=0;
424
425   // Check if this V0 comes from a reconstructed decay
426   Int_t ncs=GetNumberOfCascades();
427   for (Int_t n=0; n<ncs; n++) {
428     AliESDcascade *cs=GetCascade(n);
429
430     Int_t csIdxP=cs->GetPindex();
431     Int_t csIdxN=cs->GetNindex();
432
433     if (idxP==csIdxP)
434        if (idxN==csIdxN) return kFALSE;
435
436     if (csIdxP==lastIdxP)
437        if (csIdxN==lastIdxN) used++;
438   }
439
440   //Replace the removed V0 with the last V0 
441   TClonesArray &a=*fV0s;
442   delete a.RemoveAt(rm);
443
444   if (rm==last) return kTRUE;
445
446   //v0 is pointing to the last V0 candidate... 
447   new (a[rm]) AliESDv0(*v0);
448   delete a.RemoveAt(last);
449
450   if (!used) return kTRUE;
451   
452
453   // Remap the indices of the daughters of reconstructed decays
454   for (Int_t n=0; n<ncs; n++) {
455     AliESDcascade *cs=GetCascade(n);
456
457
458     Int_t csIdxP=cs->GetPindex();
459     Int_t csIdxN=cs->GetNindex();
460
461     if (csIdxP==lastIdxP)
462       if (csIdxN==lastIdxN) {
463          cs->AliESDv0::SetIndex(1,idxP);
464          cs->AliESDv0::SetIndex(0,idxN);
465          used--;
466          if (!used) return kTRUE;
467       }
468   }
469
470   return kTRUE;
471 }
472
473 Bool_t  AliESDEvent::RemoveTrack(Int_t rm) {
474   // ---------------------------------------------------------
475   // Remove a track and references to it from ESD,
476   // if this track does not come from a reconstructed decay
477   // ---------------------------------------------------------
478   Int_t last=GetNumberOfTracks()-1;
479   if ((rm<0)||(rm>last)) return kFALSE;
480
481   Int_t used=0;
482
483   // Check if this track comes from a reconstructed decay
484   Int_t nv0=GetNumberOfV0s();
485   for (Int_t n=0; n<nv0; n++) {
486     AliESDv0 *v0=GetV0(n);
487
488     Int_t idx=v0->GetNindex();
489     if (rm==idx) return kFALSE;
490     if (idx==last) used++;
491
492     idx=v0->GetPindex();
493     if (rm==idx) return kFALSE;
494     if (idx==last) used++;
495   }
496
497   Int_t ncs=GetNumberOfCascades();
498   for (Int_t n=0; n<ncs; n++) {
499     AliESDcascade *cs=GetCascade(n);
500
501     Int_t idx=cs->GetIndex();
502     if (rm==idx) return kFALSE;
503     if (idx==last) used++;
504   }
505
506   Int_t nkn=GetNumberOfKinks();
507   for (Int_t n=0; n<nkn; n++) {
508     AliESDkink *kn=GetKink(n);
509
510     Int_t idx=kn->GetIndex(0);
511     if (rm==idx) return kFALSE;
512     if (idx==last) used++;
513
514     idx=kn->GetIndex(1);
515     if (rm==idx) return kFALSE;
516     if (idx==last) used++;
517   }
518
519
520   //Replace the removed track with the last track 
521   TClonesArray &a=*fTracks;
522   delete a.RemoveAt(rm);
523
524   if (rm==last) return kTRUE;
525
526   AliESDtrack *t=GetTrack(last);
527   t->SetID(rm);
528   new (a[rm]) AliESDtrack(*t);
529   delete a.RemoveAt(last);
530
531   if (!used) return kTRUE;
532   
533
534   // Remap the indices of the daughters of reconstructed decays
535   for (Int_t n=0; n<nv0; n++) {
536     AliESDv0 *v0=GetV0(n);
537     if (v0->GetIndex(0)==last) {
538        v0->SetIndex(0,rm);
539        used--;
540        if (!used) return kTRUE;
541     }
542     if (v0->GetIndex(1)==last) {
543        v0->SetIndex(1,rm);
544        used--;
545        if (!used) return kTRUE;
546     }
547   }
548
549   for (Int_t n=0; n<ncs; n++) {
550     AliESDcascade *cs=GetCascade(n);
551     if (cs->GetIndex()==last) {
552        cs->SetIndex(rm);
553        used--;
554        if (!used) return kTRUE;
555     }
556   }
557
558   for (Int_t n=0; n<nkn; n++) {
559     AliESDkink *kn=GetKink(n);
560     if (kn->GetIndex(0)==last) {
561        kn->SetIndex(rm,0);
562        used--;
563        if (!used) return kTRUE;
564     }
565     if (kn->GetIndex(1)==last) {
566        kn->SetIndex(rm,1);
567        used--;
568        if (!used) return kTRUE;
569     }
570   }
571
572   return kTRUE;
573 }
574
575
576 Bool_t AliESDEvent::Clean(Float_t *cleanPars) {
577   //
578   // Remove the data which are not needed for the physics analysis.
579   //
580   // 1) Cleaning the V0 candidates
581   //    ---------------------------
582   //    If the cosine of the V0 pointing angle "csp" and 
583   //    the DCA between the daughter tracks "dca" does not satisfy 
584   //    the conditions 
585   //
586   //     csp > cleanPars[1] + dca/cleanPars[0]*(1.- cleanPars[1])
587   //
588   //    an attempt to remove this V0 candidate from ESD is made.
589   //
590   //    The V0 candidate gets removed if it does not belong to any 
591   //    recosntructed cascade decay
592   //
593   //    12.11.2007, optimal values: cleanPars[0]=0.5, cleanPars[1]=0.999
594   //
595   // 2) Cleaning the tracks
596   //    ----------------------
597   //    If track's transverse parameter is larger than cleanPars[2]
598   //                       OR
599   //    track's longitudinal parameter is larger than cleanPars[3]
600   //    an attempt to remove this track from ESD is made.
601   //
602   //    The track gets removed if it does not come 
603   //    from a reconstructed decay
604   //
605   Bool_t rc=kFALSE;
606
607   Float_t dcaMax=cleanPars[0];
608   Float_t cspMin=cleanPars[1];
609
610   Int_t nV0s=GetNumberOfV0s();
611   for (Int_t i=nV0s-1; i>=0; i--) {
612     AliESDv0 *v0=GetV0(i);
613
614     Float_t dca=v0->GetDcaV0Daughters();
615     Float_t csp=v0->GetV0CosineOfPointingAngle();
616     Float_t cspcut=cspMin + dca/dcaMax*(1.-cspMin);
617     if (csp > cspcut) continue;
618
619     if (RemoveV0(i)) rc=kTRUE;
620   }
621
622
623   Float_t dmax=cleanPars[2], zmax=cleanPars[3];
624
625   const AliESDVertex *vertex=GetVertex();
626   Bool_t vtxOK=vertex->GetStatus();
627   
628   Int_t nTracks=GetNumberOfTracks();
629   for (Int_t i=nTracks-1; i>=0; i--) {
630     AliESDtrack *track=GetTrack(i);
631     Float_t xy,z; track->GetImpactParameters(xy,z);
632     if ((TMath::Abs(xy) > dmax) || (vtxOK && (TMath::Abs(z) > zmax))) {
633       if (RemoveTrack(i)) rc=kTRUE;
634     }
635   }
636
637   return rc;
638 }
639
640 Int_t  AliESDEvent::AddTrack(const AliESDtrack *t) 
641 {
642     // Add track
643     TClonesArray &ftr = *fTracks;
644     AliESDtrack * track = new(ftr[fTracks->GetEntriesFast()])AliESDtrack(*t);
645     track->SetID(fTracks->GetEntriesFast()-1);
646     return  track->GetID();    
647 }
648
649  void AliESDEvent::AddMuonTrack(const AliESDMuonTrack *t) 
650 {
651     TClonesArray &fmu = *fMuonTracks;
652     new(fmu[fMuonTracks->GetEntriesFast()]) AliESDMuonTrack(*t);
653 }
654
655 void AliESDEvent::AddPmdTrack(const AliESDPmdTrack *t) 
656 {
657   TClonesArray &fpmd = *fPmdTracks;
658   new(fpmd[fPmdTracks->GetEntriesFast()]) AliESDPmdTrack(*t);
659 }
660
661 void AliESDEvent::AddTrdTrack(const AliESDTrdTrack *t) 
662 {
663   TClonesArray &ftrd = *fTrdTracks;
664   new(ftrd[fTrdTracks->GetEntriesFast()]) AliESDTrdTrack(*t);
665 }
666
667
668
669
670 Int_t AliESDEvent::AddKink(const AliESDkink *c) 
671 {
672     // Add kink
673     TClonesArray &fk = *fKinks;
674     AliESDkink * kink = new(fk[fKinks->GetEntriesFast()]) AliESDkink(*c);
675     kink->SetID(fKinks->GetEntriesFast()); // CKB different from the other imps..
676     return fKinks->GetEntriesFast()-1;
677 }
678
679
680 void AliESDEvent::AddCascade(const AliESDcascade *c) 
681 {
682   TClonesArray &fc = *fCascades;
683   new(fc[fCascades->GetEntriesFast()]) AliESDcascade(*c);
684 }
685
686
687 Int_t AliESDEvent::AddCaloCluster(const AliESDCaloCluster *c) 
688 {
689     // Add calocluster
690     TClonesArray &fc = *fCaloClusters;
691     AliESDCaloCluster *clus = new(fc[fCaloClusters->GetEntriesFast()]) AliESDCaloCluster(*c);
692     clus->SetID(fCaloClusters->GetEntriesFast()-1);
693     return fCaloClusters->GetEntriesFast()-1;
694   }
695
696
697 void  AliESDEvent::AddRawDataErrorLog(const AliRawDataErrorLog *log) {
698   TClonesArray &errlogs = *fErrorLogs;
699   new(errlogs[errlogs.GetEntriesFast()])  AliRawDataErrorLog(*log);
700 }
701
702 void  AliESDEvent::SetVertex(const AliESDVertex *vertex) 
703 {
704   // Set the SPD vertex
705   // use already allocated space
706   if(fSPDVertex){
707     *fSPDVertex = *vertex;
708     fSPDVertex->SetName(fgkESDListName[kSPDVertex]);
709   }
710 }
711
712 void  AliESDEvent::SetPrimaryVertex(const AliESDVertex *vertex) 
713 {
714   // Set the primary vertex
715   // use already allocated space
716   if(fPrimaryVertex){
717     *fPrimaryVertex = *vertex;
718     fPrimaryVertex->SetName(fgkESDListName[kPrimaryVertex]);
719   }
720 }
721
722 void AliESDEvent::SetMultiplicity(const AliMultiplicity *mul) 
723 {
724   // Set the SPD Multiplicity
725   if(fSPDMult){
726     *fSPDMult = *mul;
727   }
728 }
729
730
731 void AliESDEvent::SetFMDData(AliESDFMD * obj) 
732
733   // use already allocated space
734   if(fESDFMD){
735     *fESDFMD = *obj;
736   }
737 }
738
739 void AliESDEvent::SetVZEROData(AliESDVZERO * obj)
740
741   // use already allocated space
742   if(fESDVZERO)
743     *fESDVZERO = *obj;
744 }
745
746 void AliESDEvent::GetESDfriend(AliESDfriend *ev) const 
747 {
748   //
749   // Extracts the complementary info from the ESD
750   //
751   if (!ev) return;
752
753   Int_t ntrk=GetNumberOfTracks();
754
755   for (Int_t i=0; i<ntrk; i++) {
756     AliESDtrack *t=GetTrack(i);
757     const AliESDfriendTrack *f=t->GetFriendTrack();
758     ev->AddTrack(f);
759
760     t->ReleaseESDfriendTrack();// Not to have two copies of "friendTrack"
761
762   }
763 }
764
765
766 void AliESDEvent::AddObject(TObject* obj) 
767 {
768   // Add an object to the list of object.
769   // Please be aware that in order to increase performance you should
770   // refrain from using TObjArrays (if possible). Use TClonesArrays, instead.
771   fESDObjects->SetOwner(kTRUE);
772   fESDObjects->AddLast(obj);
773 }
774
775
776 void AliESDEvent::GetStdContent() 
777 {
778   // set pointers for standard content
779   // get by name much safer and not a big overhead since not called very often
780  
781   fESDRun = (AliESDRun*)fESDObjects->FindObject(fgkESDListName[kESDRun]);
782   fHeader = (AliESDHeader*)fESDObjects->FindObject(fgkESDListName[kHeader]);
783   fESDZDC = (AliESDZDC*)fESDObjects->FindObject(fgkESDListName[kESDZDC]);
784   fESDFMD = (AliESDFMD*)fESDObjects->FindObject(fgkESDListName[kESDFMD]);
785   fESDVZERO = (AliESDVZERO*)fESDObjects->FindObject(fgkESDListName[kESDVZERO]);
786   fESDTZERO = (AliESDTZERO*)fESDObjects->FindObject(fgkESDListName[kESDTZERO]);
787   fSPDVertex = (AliESDVertex*)fESDObjects->FindObject(fgkESDListName[kSPDVertex]);
788   fPrimaryVertex = (AliESDVertex*)fESDObjects->FindObject(fgkESDListName[kPrimaryVertex]);
789   fSPDMult =       (AliMultiplicity*)fESDObjects->FindObject(fgkESDListName[kSPDMult]);
790   fPHOSTrigger = (AliESDCaloTrigger*)fESDObjects->FindObject(fgkESDListName[kPHOSTrigger]);
791   fEMCALTrigger = (AliESDCaloTrigger*)fESDObjects->FindObject(fgkESDListName[kEMCALTrigger]);
792   fTracks = (TClonesArray*)fESDObjects->FindObject(fgkESDListName[kTracks]);
793   fMuonTracks = (TClonesArray*)fESDObjects->FindObject(fgkESDListName[kMuonTracks]);
794   fPmdTracks = (TClonesArray*)fESDObjects->FindObject(fgkESDListName[kPmdTracks]);
795   fTrdTracks = (TClonesArray*)fESDObjects->FindObject(fgkESDListName[kTrdTracks]);
796   fV0s = (TClonesArray*)fESDObjects->FindObject(fgkESDListName[kV0s]);
797   fCascades = (TClonesArray*)fESDObjects->FindObject(fgkESDListName[kCascades]);
798   fKinks = (TClonesArray*)fESDObjects->FindObject(fgkESDListName[kKinks]);
799   fCaloClusters = (TClonesArray*)fESDObjects->FindObject(fgkESDListName[kCaloClusters]);
800   fErrorLogs = (TClonesArray*)fESDObjects->FindObject(fgkESDListName[kErrorLogs]);
801
802 }
803
804 void AliESDEvent::SetStdNames(){
805   // Set the names of the standard contents
806   // 
807   if(fESDObjects->GetEntries()==kESDListN){
808     for(int i = 0;i < fESDObjects->GetEntries();i++){
809       TObject *fObj = fESDObjects->At(i);
810       if(fObj->InheritsFrom("TNamed")){
811         ((TNamed*)fObj)->SetName(fgkESDListName[i]);
812       }
813       else if(fObj->InheritsFrom("TClonesArray")){
814         ((TClonesArray*)fObj)->SetName(fgkESDListName[i]);
815       }
816     }
817   }
818   else{
819     printf("%s:%d SetStdNames() Wrong number of Std Entries \n",(char*)__FILE__,__LINE__);
820   }
821
822
823 void AliESDEvent::CreateStdContent() 
824 {
825   // create the standard AOD content and set pointers
826
827   // create standard objects and add them to the TList of objects
828   AddObject(new AliESDRun());
829   AddObject(new AliESDHeader());
830   AddObject(new AliESDZDC());
831   AddObject(new AliESDFMD());
832   AddObject(new AliESDVZERO());
833   AddObject(new AliESDTZERO());
834   AddObject(new AliESDVertex());
835   AddObject(new AliESDVertex());
836   AddObject(new AliMultiplicity());
837   AddObject(new AliESDCaloTrigger());
838   AddObject(new AliESDCaloTrigger());
839   AddObject(new TClonesArray("AliESDtrack",0));
840   AddObject(new TClonesArray("AliESDMuonTrack",0));
841   AddObject(new TClonesArray("AliESDPmdTrack",0));
842   AddObject(new TClonesArray("AliESDTrdTrack",0));
843   AddObject(new TClonesArray("AliESDv0",0));
844   AddObject(new TClonesArray("AliESDcascade",0));
845   AddObject(new TClonesArray("AliESDkink",0));
846   AddObject(new TClonesArray("AliESDCaloCluster",0));
847   AddObject(new TClonesArray("AliRawDataErrorLog",0));
848
849   // check the order of the indices against enum...
850
851   // set names
852   SetStdNames();
853   // read back pointers
854   GetStdContent();
855 }
856
857 TObject* AliESDEvent::FindListObject(const char *name){
858   if(fESDObjects)return fESDObjects->FindObject(name);
859   return 0;
860
861
862 Int_t AliESDEvent::GetPHOSClusters(TRefArray *clusters) const
863 {
864   // fills the provided TRefArray with all found phos clusters
865
866   clusters->Clear();
867
868   AliESDCaloCluster *cl = 0;
869   for (Int_t i = 0; i < GetNumberOfCaloClusters(); i++) {
870
871     if ( (cl = GetCaloCluster(i))) {
872       if (cl->IsPHOS()){
873         clusters->Add(cl);
874         printf("IsPHOS %d Size: %d \n",i,clusters->GetEntriesFast());
875       }
876     }
877   }
878   return clusters->GetEntriesFast();
879 }
880
881 Int_t AliESDEvent::GetEMCALClusters(TRefArray *clusters) const
882 {
883   // fills the provided TRefArray with all found phos clusters
884
885   clusters->Clear();
886
887   AliESDCaloCluster *cl = 0;
888   for (Int_t i = 0; i < GetNumberOfCaloClusters(); i++) {
889
890     if ( (cl = GetCaloCluster(i))) {
891       if (cl->IsEMCAL()){
892         clusters->Add(cl);
893         printf("IsEMCAL %d Size: %d \n",i,clusters->GetEntriesFast());
894       }
895     }
896   }
897   return clusters->GetEntriesFast();
898 }
899
900
901 void AliESDEvent::ReadFromTree(TTree *tree){
902
903   if(!tree){
904     Printf("%s %d AliESDEvent::ReadFromTree() Zero Pointer to Tree \n",(char*)__FILE__,__LINE__);
905     return;
906   }
907
908   // load the TTree
909   if(!tree->GetTree())tree->LoadTree(0);
910
911   // if we find the "ESD" branch on the tree we do have the old structure
912   if(tree->GetBranch("ESD")) {
913     char ** address  = (char **)(tree->GetBranch("ESD")->GetAddress());
914     // do we have the friend branch
915     TBranch * esdFB = tree->GetBranch("ESDfriend.");
916     char ** addressF = 0;
917     if(esdFB)addressF = (char **)(esdFB->GetAddress());
918     if (!address) {
919       printf("%s %d AliESDEvent::ReadFromTree() Reading old Tree \n",(char*)__FILE__,__LINE__);
920       tree->SetBranchAddress("ESD",       &fESDOld);
921       if(esdFB){
922         tree->SetBranchAddress("ESDfriend.",&fESDFriendOld);
923       }
924     } else {
925       printf("%s %d AliESDEvent::ReadFromTree() Reading old Tree \n",(char*)__FILE__,__LINE__);
926       printf("%s %d Branch already connected. Using existing branch address. \n",(char*)__FILE__,__LINE__);
927       fESDOld       = (AliESD*)       (*address);
928       // addressF can still be 0, since branch needs to switched on
929       if(addressF)fESDFriendOld = (AliESDfriend*) (*addressF);
930     }
931                                        
932     //  have already connected the old ESD structure... ?
933     // reuse also the pointer of the AlliESDEvent
934     // otherwise create new ones
935
936     TList* connectedList = (TList*) (tree->GetUserInfo()->FindObject("ESDObjectsConnectedToTree"));
937   
938     if(connectedList){
939       // If connected use the connected list of objects
940       if(fESDObjects!= connectedList){
941         // protect when called twice 
942         fESDObjects->Delete();
943         fESDObjects = connectedList;
944       }
945       GetStdContent(); 
946
947       // The pointer to the friend changes when called twice via InitIO. Is this connected to 
948       // the change TChain -> TTree
949       
950       TObject* oldf = FindListObject("AliESDfriend");
951       TObject* newf = 0;
952       if(addressF){
953         newf = (TObject*)*addressF;
954       }
955       if(newf!=0&&oldf!=newf){
956         // remove the old reference
957         // Should we also delete it? Or is this handled in TTree I/O
958         // since it is created by the first SetBranchAddress
959         fESDObjects->Remove(oldf);
960         // add the new one 
961         fESDObjects->Add(newf);
962       }
963       
964       fConnected = true;
965       return;
966     }
967     // else...    
968     CreateStdContent(); // create for copy
969     // if we have the esdfriend add it, so we always can access it via the userinfo
970     if(fESDFriendOld)AddObject(fESDFriendOld);
971     // we are not owner of the list objects 
972     // must not delete it
973     fESDObjects->SetOwner(kFALSE);
974     fESDObjects->SetName("ESDObjectsConnectedToTree");
975     tree->GetUserInfo()->Add(fESDObjects);
976     fConnected = true;
977     return;
978   }
979   
980   delete fESDOld;
981   fESDOld = 0;
982   // Try to find AliESDEvent
983   AliESDEvent *esdEvent = 0;
984   esdEvent = (AliESDEvent*)tree->GetTree()->GetUserInfo()->FindObject("AliESDEvent");
985   if(esdEvent){   
986       // Check if already connected to tree
987     TList* connectedList = (TList*) (tree->GetUserInfo()->FindObject("ESDObjectsConnectedToTree"));
988     if (connectedList) {
989       // If connected use the connected list if objects
990       fESDObjects->Delete();
991       fESDObjects = connectedList;
992       GetStdContent(); 
993       fConnected = true;
994       return;
995     }
996     // Connect to tree
997     // prevent a memory leak when reading back the TList
998     delete fESDObjects;
999     fESDObjects = 0;
1000     // create a new TList from the UserInfo TList... 
1001     // copy constructor does not work...
1002     fESDObjects = (TList*)(esdEvent->GetList()->Clone());
1003     fESDObjects->SetOwner(kFALSE);
1004     if(fESDObjects->GetEntries()<kESDListN){
1005       printf("%s %d AliESDEvent::ReadFromTree() TList contains less than the standard contents %d < %d \n",
1006              (char*)__FILE__,__LINE__,fESDObjects->GetEntries(),kESDListN);
1007     }
1008     // set the branch addresses
1009     TIter next(fESDObjects);
1010     TNamed *el;
1011     while((el=(TNamed*)next())){
1012       TString bname(el->GetName());
1013       if(bname.CompareTo("AliESDfriend")==0)
1014         {
1015           // AliESDfriend does not have a name ...
1016           tree->SetBranchAddress("ESDfriend.",fESDObjects->GetObjectRef(el));
1017         }
1018       else{
1019         tree->SetBranchAddress(bname.Data(),fESDObjects->GetObjectRef(el));
1020       }
1021     }
1022     GetStdContent();
1023     // when reading back we are not owner of the list 
1024     // must not delete it
1025     fESDObjects->SetOwner(kFALSE);
1026     fESDObjects->SetName("ESDObjectsConnectedToTree");
1027     // we are not owner of the list objects 
1028     // must not delete it
1029     tree->GetUserInfo()->Add(fESDObjects);
1030     fConnected = true;
1031   }// no esdEvent
1032   else {
1033     // we can't get the list from the user data, create standard content
1034     // and set it by hand (no ESDfriend at the moment
1035     CreateStdContent();
1036     TIter next(fESDObjects);
1037     TNamed *el;
1038     while((el=(TNamed*)next())){
1039       TString bname(el->GetName());    
1040       tree->SetBranchAddress(bname.Data(),fESDObjects->GetObjectRef(el));
1041     }
1042     GetStdContent();
1043     // when reading back we are not owner of the list 
1044     // must not delete it
1045     fESDObjects->SetOwner(kFALSE);
1046   }
1047 }
1048
1049
1050 void AliESDEvent::CopyFromOldESD()
1051 {
1052   // Method which copies over everthing from the old esd structure to the 
1053   // new  
1054   if(fESDOld){
1055     ResetStdContent();
1056      // Run
1057     SetRunNumber(fESDOld->GetRunNumber());
1058     SetPeriodNumber(fESDOld->GetPeriodNumber());
1059     SetMagneticField(fESDOld->GetMagneticField());
1060   
1061     // leave out diamond ...
1062     // SetDiamond(const AliESDVertex *vertex) { fESDRun->SetDiamond(vertex);}
1063
1064     // header
1065     SetTriggerMask(fESDOld->GetTriggerMask());
1066     SetOrbitNumber(fESDOld->GetOrbitNumber());
1067     SetTimeStamp(fESDOld->GetTimeStamp());
1068     SetEventType(fESDOld->GetEventType());
1069     SetEventNumberInFile(fESDOld->GetEventNumberInFile());
1070     SetBunchCrossNumber(fESDOld->GetBunchCrossNumber());
1071     SetTriggerCluster(fESDOld->GetTriggerCluster());
1072
1073     // ZDC
1074
1075     SetZDC(fESDOld->GetZDCN1Energy(),
1076            fESDOld->GetZDCP1Energy(),
1077            fESDOld->GetZDCEMEnergy(),
1078            0,
1079            fESDOld->GetZDCN2Energy(),
1080            fESDOld->GetZDCP2Energy(),
1081            fESDOld->GetZDCParticipants());
1082
1083     // FMD
1084     
1085     if(fESDOld->GetFMDData())SetFMDData(fESDOld->GetFMDData());
1086
1087     // T0
1088
1089     SetT0zVertex(fESDOld->GetT0zVertex());
1090     SetT0(fESDOld->GetT0());
1091     //  leave amps out
1092
1093     // VZERO
1094     if (fESDOld->GetVZEROData()) SetVZEROData(fESDOld->GetVZEROData());
1095
1096     if(fESDOld->GetVertex())SetVertex(fESDOld->GetVertex());
1097
1098     if(fESDOld->GetPrimaryVertex())SetPrimaryVertex(fESDOld->GetPrimaryVertex());
1099
1100     if(fESDOld->GetMultiplicity())SetMultiplicity(fESDOld->GetMultiplicity());
1101
1102     for(int i = 0;i<fESDOld->GetNumberOfTracks();i++){
1103       AddTrack(fESDOld->GetTrack(i));
1104     }
1105
1106     for(int i = 0;i<fESDOld->GetNumberOfMuonTracks();i++){
1107       AddMuonTrack(fESDOld->GetMuonTrack(i));
1108     }
1109
1110     for(int i = 0;i<fESDOld->GetNumberOfPmdTracks();i++){
1111       AddPmdTrack(fESDOld->GetPmdTrack(i));
1112     }
1113
1114     for(int i = 0;i<fESDOld->GetNumberOfTrdTracks();i++){
1115       AddTrdTrack(fESDOld->GetTrdTrack(i));
1116     }
1117
1118     for(int i = 0;i<fESDOld->GetNumberOfV0s();i++){
1119       AddV0(fESDOld->GetV0(i));
1120     }
1121
1122     for(int i = 0;i<fESDOld->GetNumberOfCascades();i++){
1123       AddCascade(fESDOld->GetCascade(i));
1124     }
1125
1126     for(int i = 0;i<fESDOld->GetNumberOfKinks();i++){
1127       AddKink(fESDOld->GetKink(i));
1128     }
1129
1130
1131     for(int i = 0;i<fESDOld->GetNumberOfCaloClusters();i++){
1132       AddCaloCluster(fESDOld->GetCaloCluster(i));
1133     }
1134
1135   }// if fesdold
1136 }
1137
1138
1139