]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AOD/AliAODEvent.cxx
Merge branch 'TPCdev' of /hera/alice/akalweit/software/aliroot/AliRoot_TPCDEV2 into...
[u/mrichter/AliRoot.git] / STEER / AOD / AliAODEvent.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-2007, 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 //     AOD base class
20 //     Author: Markus Oldenburg, CERN
21 //-------------------------------------------------------------------------
22
23 #include <TROOT.h>
24 #include <TTree.h>
25 #include <TFolder.h>
26 #include <TFriendElement.h>
27 #include <TProcessID.h>
28 #include <TCollection.h>
29 #include "Riostream.h"
30 #include "AliAODEvent.h"
31 #include "AliAODHeader.h"
32 #include "AliAODTrack.h"
33 #include "AliAODDimuon.h"
34 #include "AliAODTrdTrack.h"
35
36 ClassImp(AliAODEvent)
37
38 // definition of std AOD member names
39   const char* AliAODEvent::fAODListName[kAODListN] = {"header",
40                                                       "tracks",
41                                                       "vertices",
42                                                       "v0s",
43                                                       "cascades",
44                                                       "tracklets",
45                                                       "jets",
46                                                       "emcalCells",
47                                                       "phosCells",
48                                                                         "caloClusters",
49                                                                         "emcalTrigger",
50                                                                         "phosTrigger",
51                                                       "fmdClusters",
52                                                       "pmdClusters",
53                                                       "hmpidRings",
54                                                       "dimuons",
55                                                       "AliAODTZERO",
56                                                       "AliAODVZERO",
57                                                       "AliAODZDC",
58                                                       "AliTOFHeader",
59                                                       "trdTracks"
60                                                                               
61 };
62 //______________________________________________________________________________
63 AliAODEvent::AliAODEvent() :
64   AliVEvent(),
65   fAODObjects(0),
66   fAODFolder(0),
67   fConnected(kFALSE),
68   fTracksConnected(kFALSE),
69   fHeader(0),
70   fTracks(0),
71   fVertices(0),
72   fV0s(0),
73   fCascades(0),
74   fTracklets(0),
75   fJets(0),
76   fEmcalCells(0),
77   fPhosCells(0),
78   fCaloClusters(0),
79   fEMCALTrigger(0),
80   fPHOSTrigger(0),
81   fFmdClusters(0),
82   fPmdClusters(0),
83   fHMPIDrings(0),
84   fDimuons(0),
85   fAODTZERO(0),
86   fAODVZERO(0),
87   fAODZDC(0),
88   fTOFHeader(0),
89   fTrdTracks(0)
90 {
91   // default constructor
92   if (TClass::IsCallingNew() != TClass::kDummyNew) fAODObjects = new TList();
93 }
94
95 //______________________________________________________________________________
96 AliAODEvent::AliAODEvent(const AliAODEvent& aod):
97   AliVEvent(aod),
98   fAODObjects(new TList()),
99   fAODFolder(0),
100   fConnected(kFALSE),
101   fTracksConnected(kFALSE),
102   fHeader(new AliAODHeader(*aod.fHeader)),
103   fTracks(new TClonesArray(*aod.fTracks)),
104   fVertices(new TClonesArray(*aod.fVertices)),
105   fV0s(new TClonesArray(*aod.fV0s)),
106   fCascades(new TClonesArray(*aod.fCascades)),
107   fTracklets(new AliAODTracklets(*aod.fTracklets)),
108   fJets(new TClonesArray(*aod.fJets)),
109   fEmcalCells(new AliAODCaloCells(*aod.fEmcalCells)),
110   fPhosCells(new AliAODCaloCells(*aod.fPhosCells)),
111   fCaloClusters(new TClonesArray(*aod.fCaloClusters)),
112   fEMCALTrigger(new AliAODCaloTrigger(*aod.fEMCALTrigger)),
113   fPHOSTrigger(new AliAODCaloTrigger(*aod.fPHOSTrigger)),
114   fFmdClusters(new TClonesArray(*aod.fFmdClusters)),
115   fPmdClusters(new TClonesArray(*aod.fPmdClusters)),
116   fHMPIDrings(new TClonesArray(*aod.fHMPIDrings)),
117   fDimuons(new TClonesArray(*aod.fDimuons)),
118   fAODTZERO(new AliAODTZERO(*aod.fAODTZERO)),
119   fAODVZERO(new AliAODVZERO(*aod.fAODVZERO)),
120   fAODZDC(new AliAODZDC(*aod.fAODZDC)),
121   fTOFHeader(new AliTOFHeader(*aod.fTOFHeader)),
122   fTrdTracks(new TClonesArray(*aod.fTrdTracks))
123 {
124   // Copy constructor
125   AddObject(fHeader);
126   AddObject(fTracks);
127   AddObject(fVertices);
128   AddObject(fV0s);
129   AddObject(fCascades);
130   AddObject(fTracklets);
131   AddObject(fJets);
132   AddObject(fEmcalCells);
133   AddObject(fPhosCells);
134   AddObject(fCaloClusters);
135   AddObject(fEMCALTrigger);
136   AddObject(fPHOSTrigger);
137   AddObject(fFmdClusters);
138   AddObject(fPmdClusters);
139   AddObject(fHMPIDrings);
140   AddObject(fDimuons);
141   AddObject(fAODTZERO);
142   AddObject(fAODVZERO);
143   AddObject(fAODZDC);
144   AddObject(fTOFHeader);
145   AddObject(fTrdTracks);
146   fConnected = aod.fConnected;
147   ConnectTracks();
148   GetStdContent();
149   CreateStdFolders();
150 }
151
152 //______________________________________________________________________________
153 AliAODEvent & AliAODEvent::operator=(const AliAODEvent& aod) {
154
155     // Assignment operator
156
157   if(&aod == this) return *this;
158   AliVEvent::operator=(aod);
159
160   // This assumes that the list is already created
161   // and that the virtual void Copy(Tobject&) function
162   // is correctly implemented in the derived class
163   // otherwise only TObject::Copy() will be used
164
165   if((fAODObjects->GetSize()==0)&&(aod.fAODObjects->GetSize()>=kAODListN)){
166     // We cover the case that we do not yet have the 
167     // standard content but the source has it
168     CreateStdContent();
169   }
170   
171   // Here we have the standard content without user additions, but the content is 
172   // not matching the aod source.
173   
174   // Iterate the list of source objects
175   TIter next(aod.GetList());
176   TObject *its = 0;
177   TString name;
178   while ((its = next())) {
179     name = its->GetName();
180     // Check if we have this object type in out list
181     TObject *mine = fAODObjects->FindObject(name);    
182     if(!mine) {
183       // We have to create the same type of object.
184       TClass* pClass=TClass::GetClass(its->ClassName());     
185       if (!pClass) {
186         AliWarning(Form("Can not find class description for entry %s (%s)\n",
187                    its->ClassName(), name.Data()));
188         continue;
189       }
190       mine=(TObject*)pClass->New();
191       if(!mine){
192         // not in this: can be added to list
193         AliWarning(Form("%s:%d Could not find %s for copying \n",
194                    (char*)__FILE__,__LINE__,name.Data()));
195         continue;
196       }  
197       if(mine->InheritsFrom("TNamed"))  {
198         ((TNamed*)mine)->SetName(name);
199       } else if(mine->InheritsFrom("TCollection")){
200         if(mine->InheritsFrom("TClonesArray")) {
201           TClonesArray *itscl = dynamic_cast<TClonesArray*>(its);
202           if (!itscl) {
203             AliWarning(Form("Class description for entry %s (%s) not TClonesArray\n",
204                    its->ClassName(), name.Data()));
205             continue;
206           
207           }
208                dynamic_cast<TClonesArray*>(mine)->SetClass(itscl->GetClass(), itscl->GetSize());
209         }
210         dynamic_cast<TCollection*>(mine)->SetName(name);
211       }
212       AliDebug(1, Form("adding object %s of type %s", mine->GetName(), mine->ClassName()));
213       AddObject(mine);
214     }
215     // Now we have an object of the same type and name, but different content.        
216     if(!its->InheritsFrom("TCollection")){
217       // simple objects (do they have a Copy method that calls operator= ?)
218       its->Copy(*mine);
219     } else if (its->InheritsFrom("TClonesArray")) {
220       // Create or expand the tclonesarray pointers
221       // so we can directly copy to the object
222       TClonesArray *its_tca = (TClonesArray*)its;
223       TClonesArray *mine_tca = (TClonesArray*)mine;
224       // this leaves the capacity of the TClonesArray the same
225       // except for a factor of 2 increase when size > capacity
226       // does not release any memory occupied by the tca
227       Int_t its_entries = its_tca->GetEntriesFast();
228       mine_tca->ExpandCreate(its_entries);
229       for(int i=0; i<its_entries; i++){
230         // copy 
231         TObject *mine_tca_obj = mine_tca->At(i);
232         TObject *its_tca_obj = its_tca->At(i);
233         // no need to delete first
234         // pointers within the class should be handled by Copy()...
235         // Can there be Empty slots?
236         its_tca_obj->Copy(*mine_tca_obj);
237       }
238     } else {
239       AliWarning(Form("%s:%d cannot copy TCollection \n",
240                       (char*)__FILE__,__LINE__));
241     }
242   }  
243   fConnected = aod.fConnected;
244   fTracksConnected = kFALSE;
245   ConnectTracks();
246   return *this;
247 }
248
249
250 //______________________________________________________________________________
251 AliAODEvent::~AliAODEvent() 
252 {
253 // destructor
254     delete fAODFolder;
255     fAODFolder = 0;
256     if(!fConnected) {
257 //       fAODObjects->Delete("slow");
258        delete fAODObjects;
259     }   
260 }
261
262 //______________________________________________________________________________
263 void AliAODEvent::AddObject(TObject* obj) 
264 {
265   // Add an object to the list of objects.
266   // Please be aware that in order to increase performance you should
267   // refrain from using TObjArrays (if possible). Use TClonesArrays, instead.
268   
269 //  if ( !fAODObjects ) {
270 //     fAODObjects = new TList();
271 //     fAODObjects->SetOwner();
272 //  }
273   if ( !fAODObjects->FindObject(obj) ) 
274   {
275     fAODObjects->AddLast(obj);
276   }
277 }
278
279 //______________________________________________________________________________
280 Int_t AliAODEvent::AddTrack(const AliAODTrack* trk)
281 {
282 // Add new AOD track. Make sure to set the event if needed.
283   AliAODTrack *track =  new((*fTracks)[fTracks->GetEntriesFast()]) AliAODTrack(*trk);
284   track->SetAODEvent(this);
285   return fTracks->GetEntriesFast()-1;
286 }  
287
288 //______________________________________________________________________________
289 void AliAODEvent::RemoveObject(TObject* obj) 
290 {
291   // Removes an object from the list of objects.
292   
293   fAODObjects->Remove(obj);
294 }
295
296 //______________________________________________________________________________
297 TObject *AliAODEvent::FindListObject(const char *objName) const
298 {
299   // Return the pointer to the object with the given name.
300
301   return fAODObjects->FindObject(objName);
302 }
303
304 //______________________________________________________________________________
305 void AliAODEvent::CreateStdContent() 
306 {
307   // create the standard AOD content and set pointers
308
309   // create standard objects and add them to the TList of objects
310   AddObject(new AliAODHeader());
311   AddObject(new TClonesArray("AliAODTrack", 0));
312   AddObject(new TClonesArray("AliAODVertex", 0));
313   AddObject(new TClonesArray("AliAODv0", 0));
314   AddObject(new TClonesArray("AliAODcascade", 0));
315   AddObject(new AliAODTracklets());
316   AddObject(new TClonesArray("AliAODJet", 0));
317   AddObject(new AliAODCaloCells());
318   AddObject(new AliAODCaloCells());
319   AddObject(new TClonesArray("AliAODCaloCluster", 0));
320   AddObject(new AliAODCaloTrigger()); // EMCAL 
321   AddObject(new AliAODCaloTrigger()); // PHOS
322   AddObject(new TClonesArray("AliAODFmdCluster", 0));
323   AddObject(new TClonesArray("AliAODPmdCluster", 0));
324   AddObject(new TClonesArray("AliAODHMPIDrings", 0));
325   AddObject(new TClonesArray("AliAODDimuon", 0));
326   AddObject(new AliAODTZERO());
327   AddObject(new AliAODVZERO());
328   AddObject(new AliAODZDC());
329   AddObject(new AliTOFHeader());
330   AddObject(new TClonesArray("AliAODTrdTrack", 0));
331   // set names
332   SetStdNames();
333
334   // read back pointers
335   GetStdContent();
336   CreateStdFolders();
337   return;
338 }
339
340 void  AliAODEvent::MakeEntriesReferencable()
341 {
342     // Make all entries referencable in a subsequent process
343     //
344     TIter next(fAODObjects);
345     TObject* obj;
346     while ((obj = next()))
347     {
348         if(obj->InheritsFrom("TCollection"))
349             {
350                 AssignIDtoCollection((TCollection*)obj);
351             }
352     }
353 }
354
355 //______________________________________________________________________________
356 void AliAODEvent::SetStdNames()
357 {
358   // introduce the standard naming
359
360   if(fAODObjects->GetEntries()==kAODListN){
361     for(int i = 0;i < fAODObjects->GetEntries();i++){
362       TObject *fObj = fAODObjects->At(i);
363       if(fObj->InheritsFrom("TNamed")){
364         ((TNamed*)fObj)->SetName(fAODListName[i]);
365       }
366       else if(fObj->InheritsFrom("TClonesArray")){
367         ((TClonesArray*)fObj)->SetName(fAODListName[i]);
368       }
369     }
370   }
371   else{
372     printf("%s:%d SetStdNames() Wrong number of Std Entries \n",(char*)__FILE__,__LINE__);
373   }
374
375
376 //______________________________________________________________________________
377 void AliAODEvent::CreateStdFolders()
378 {
379     // Create the standard folder structure
380   if(fAODFolder)delete fAODFolder;
381     fAODFolder = gROOT->GetRootFolder()->AddFolder("AOD", "AOD");
382     if(fAODObjects->GetEntries()==kAODListN){
383         for(int i = 0;i < fAODObjects->GetEntries();i++){
384             TObject *fObj = fAODObjects->At(i);
385             if(fObj->InheritsFrom("TClonesArray")){
386                 fAODFolder->AddFolder(fAODListName[i], fAODListName[i], (TCollection*) fObj);
387             } else {
388                 fAODFolder->AddFolder(fAODListName[i], fAODListName[i], 0);
389             }
390         }
391     }
392     else{
393         printf("%s:%d CreateStdFolders() Wrong number of Std Entries \n",(char*)__FILE__,__LINE__);
394     }
395
396
397 //______________________________________________________________________________
398 void AliAODEvent::GetStdContent()
399 {
400   // set pointers for standard content
401
402   fHeader        = (AliAODHeader*)fAODObjects->FindObject("header");
403   fTracks        = (TClonesArray*)fAODObjects->FindObject("tracks");
404   fVertices      = (TClonesArray*)fAODObjects->FindObject("vertices");
405   fV0s           = (TClonesArray*)fAODObjects->FindObject("v0s");
406   fCascades      = (TClonesArray*)fAODObjects->FindObject("cascades");
407   fTracklets     = (AliAODTracklets*)fAODObjects->FindObject("tracklets");
408   fJets          = (TClonesArray*)fAODObjects->FindObject("jets");
409   fEmcalCells    = (AliAODCaloCells*)fAODObjects->FindObject("emcalCells");
410   fPhosCells     = (AliAODCaloCells*)fAODObjects->FindObject("phosCells");
411   fCaloClusters  = (TClonesArray*)fAODObjects->FindObject("caloClusters");
412   fEMCALTrigger  = (AliAODCaloTrigger*)fAODObjects->FindObject("emcalTrigger");
413   fPHOSTrigger   = (AliAODCaloTrigger*)fAODObjects->FindObject("phosTrigger");
414   fFmdClusters   = (TClonesArray*)fAODObjects->FindObject("fmdClusters");
415   fPmdClusters   = (TClonesArray*)fAODObjects->FindObject("pmdClusters");
416   fHMPIDrings    = (TClonesArray*)fAODObjects->FindObject("hmpidRings");  
417   fDimuons       = (TClonesArray*)fAODObjects->FindObject("dimuons");
418   fAODTZERO      = (AliAODTZERO*)fAODObjects->FindObject("AliAODTZERO");
419   fAODVZERO      = (AliAODVZERO*)fAODObjects->FindObject("AliAODVZERO");
420   fAODZDC        = (AliAODZDC*)fAODObjects->FindObject("AliAODZDC");
421   fTOFHeader     = (AliTOFHeader*)fAODObjects->FindObject("AliTOFHeader");
422   fTrdTracks     = (TClonesArray*)fAODObjects->FindObject("trdTracks");
423 }
424
425 //______________________________________________________________________________
426 void AliAODEvent::ResetStd(Int_t trkArrSize, 
427             Int_t vtxArrSize, 
428             Int_t v0ArrSize,
429             Int_t cascadeArrSize,
430             Int_t jetSize, 
431             Int_t caloClusSize, 
432             Int_t fmdClusSize, 
433             Int_t pmdClusSize,
434             Int_t hmpidRingsSize,
435             Int_t dimuonArrSize,
436             Int_t nTrdTracks
437                            )
438 {
439   // deletes content of standard arrays and resets size 
440   fTracksConnected = kFALSE;
441   if (fTracks) {
442     fTracks->Delete();
443     if (trkArrSize > fTracks->GetSize()) 
444       fTracks->Expand(trkArrSize);
445   }
446   if (fVertices) {
447     fVertices->Delete();
448     if (vtxArrSize > fVertices->GetSize()) 
449       fVertices->Expand(vtxArrSize);
450   }
451   if (fV0s) {
452     fV0s->Delete();
453     if (v0ArrSize > fV0s->GetSize()) 
454       fV0s->Expand(v0ArrSize);
455   }
456   if (fCascades) {
457     fCascades->Delete();
458     if (cascadeArrSize > fCascades->GetSize()) 
459       fCascades->Expand(cascadeArrSize);
460   }
461   if (fJets) {
462     fJets->Delete();
463     if (jetSize > fJets->GetSize())
464       fJets->Expand(jetSize);
465   }
466   if (fCaloClusters) {
467     fCaloClusters->Delete();
468     if (caloClusSize > fCaloClusters->GetSize()) 
469       fCaloClusters->Expand(caloClusSize);
470   }
471   if (fFmdClusters) {
472     fFmdClusters->Delete();
473     if (fmdClusSize > fFmdClusters->GetSize()) 
474       fFmdClusters->Expand(fmdClusSize);
475   }
476   if (fPmdClusters) {
477     fPmdClusters->Delete();
478     if (pmdClusSize > fPmdClusters->GetSize()) 
479       fPmdClusters->Expand(pmdClusSize);
480   }
481   if (fHMPIDrings) {
482      fHMPIDrings->Delete();
483     if (hmpidRingsSize > fHMPIDrings->GetSize()) 
484       fHMPIDrings->Expand(hmpidRingsSize);
485   }
486   if (fDimuons) {
487     fDimuons->Delete();
488     if (dimuonArrSize > fDimuons->GetSize()) 
489       fDimuons->Expand(dimuonArrSize);
490   }
491   if (fTrdTracks) {
492     // no pointers in there, so cheaper Clear suffices
493     fTrdTracks->Clear("C");
494     if (nTrdTracks > fTrdTracks->GetSize())
495       fTrdTracks->Expand(nTrdTracks);
496   }
497
498   if (fTracklets)
499     fTracklets->DeleteContainer();
500   if (fPhosCells)
501     fPhosCells->DeleteContainer();  
502   if (fEmcalCells)
503     fEmcalCells->DeleteContainer();
504   
505   if (fEMCALTrigger)
506         fEMCALTrigger->DeAllocate();
507   if (fPHOSTrigger)
508         fPHOSTrigger->DeAllocate();
509
510 }
511
512 //______________________________________________________________________________
513 void AliAODEvent::ClearStd()
514 {
515   // clears the standard arrays
516   if (fHeader)
517     fHeader        ->Clear();
518   fTracksConnected = kFALSE;
519   if (fTracks)
520     fTracks        ->Delete();
521   if (fVertices)
522     fVertices      ->Delete();
523   if (fV0s)
524     fV0s           ->Delete();
525   if (fCascades)
526     fCascades      ->Delete();
527   if (fTracklets)
528     fTracklets     ->DeleteContainer();
529   if (fJets)
530     fJets          ->Delete();
531   if (fEmcalCells)
532     fEmcalCells    ->DeleteContainer();
533   if (fPhosCells)
534     fPhosCells     ->DeleteContainer();
535   if (fCaloClusters)
536     fCaloClusters  ->Delete();
537   if (fFmdClusters)
538     fFmdClusters   ->Clear();
539   if (fPmdClusters)
540     fPmdClusters   ->Clear();  
541   if (fHMPIDrings) 
542      fHMPIDrings   ->Clear();    
543   if (fDimuons)
544     fDimuons       ->Clear();
545   if (fTrdTracks)
546     fTrdTracks     ->Clear();
547         
548   if (fEMCALTrigger)
549         fEMCALTrigger->DeAllocate();
550   if (fPHOSTrigger)
551         fPHOSTrigger->DeAllocate();
552 }
553
554 //_________________________________________________________________
555 Int_t AliAODEvent::GetPHOSClusters(TRefArray *clusters) const
556 {
557   // fills the provided TRefArray with all found phos clusters
558   
559   clusters->Clear();
560   
561   AliAODCaloCluster *cl = 0;
562   Bool_t first = kTRUE;
563   for (Int_t i = 0; i < GetNumberOfCaloClusters() ; i++) {
564     if ( (cl = GetCaloCluster(i)) ) {
565       if (cl->IsPHOS()){
566         if(first) {
567           new (clusters) TRefArray(TProcessID::GetProcessWithUID(cl)); 
568           first=kFALSE;
569         }
570         clusters->Add(cl);
571         //printf("IsPHOS cluster %d, E %2.3f Size: %d \n",i,cl->E(),clusters->GetEntriesFast());
572       }
573     }
574   }
575   return clusters->GetEntriesFast();
576 }
577
578 //_________________________________________________________________
579 Int_t AliAODEvent::GetEMCALClusters(TRefArray *clusters) const
580 {
581   // fills the provided TRefArray with all found emcal clusters
582
583   clusters->Clear();
584   AliAODCaloCluster *cl = 0;
585   Bool_t first = kTRUE;
586   for (Int_t i = 0; i < GetNumberOfCaloClusters(); i++) {
587     if ( (cl = GetCaloCluster(i)) ) {
588       if (cl->IsEMCAL()){
589         if(first) {
590           new (clusters) TRefArray(TProcessID::GetProcessWithUID(cl)); 
591           first=kFALSE;
592         }
593         clusters->Add(cl);
594         //printf("IsEMCal cluster %d, E %2.3f Size: %d \n",i,cl->E(),clusters->GetEntriesFast());
595       }
596     }
597   }
598   return clusters->GetEntriesFast();
599 }
600
601
602 //______________________________________________________________________________
603 Int_t AliAODEvent::GetMuonTracks(TRefArray *muonTracks) const
604 {
605   // fills the provided TRefArray with all found muon tracks
606
607   muonTracks->Clear();
608
609   AliAODTrack *track = 0;
610   for (Int_t iTrack = 0; iTrack < GetNTracks(); iTrack++) {
611     track = GetTrack(iTrack);
612     if (track->IsMuonTrack()) {
613       muonTracks->Add(track);
614     }
615   }
616   
617   return muonTracks->GetEntriesFast();
618 }
619
620
621 //______________________________________________________________________________
622 Int_t AliAODEvent::GetNumberOfMuonTracks() const
623 {
624   // get number of muon tracks
625   Int_t nMuonTracks=0;
626   for (Int_t iTrack = 0; iTrack < GetNTracks(); iTrack++) {
627     if ((GetTrack(iTrack))->IsMuonTrack()) {
628        nMuonTracks++;
629     }
630   }
631   
632   return nMuonTracks;
633 }
634
635 //______________________________________________________________________________
636 void AliAODEvent::ReadFromTree(TTree *tree, Option_t* opt /*= ""*/)
637 {
638     // Connects aod event to tree
639   
640   if(!tree){
641     AliWarning("Zero Pointer to Tree \n");
642     return;
643   }
644     // load the TTree
645   if(!tree->GetTree())tree->LoadTree(0);
646   
647     // Try to find AliAODEvent
648   AliAODEvent *aodEvent = 0;
649   aodEvent = (AliAODEvent*)tree->GetTree()->GetUserInfo()->FindObject("AliAODEvent");
650   if(aodEvent){
651     // This event is connected to the tree by definition, just say so
652     aodEvent->SetConnected();
653       // Check if already connected to tree
654     TList* connectedList = (TList*) (tree->GetUserInfo()->FindObject("AODObjectsConnectedToTree"));
655     if (connectedList && (!strcmp(opt, "reconnect"))) {
656         // If connected use the connected list of objects
657         if (fAODObjects != connectedList) {
658            delete fAODObjects;
659            fAODObjects = connectedList;
660         }   
661         GetStdContent(); 
662         fConnected = kTRUE;
663         return;
664     } 
665       // Connect to tree
666       // prevent a memory leak when reading back the TList
667 //      if (!(strcmp(opt, "reconnect"))) fAODObjects->Delete();
668     
669       // create a new TList from the UserInfo TList... 
670       // copy constructor does not work...
671     //    fAODObjects = (TList*)(aodEvent->GetList()->Clone());
672     fAODObjects = (TList*)aodEvent->GetList();
673     fAODObjects->SetOwner(kTRUE);
674     if(fAODObjects->GetEntries()<kAODListN)
675     {
676       AliWarning(Form("AliAODEvent::ReadFromTree() TList contains less than the standard contents %d < %d"
677                       " That might be fine though (at least for filtered AODs)",fAODObjects->GetEntries(),kAODListN));
678     }
679       //
680       // Let's find out whether we have friends
681     TList* friendL = tree->GetTree()->GetListOfFriends();
682     if (friendL) 
683     {
684       TIter next(friendL);
685       TFriendElement* fe;
686       while ((fe = (TFriendElement*)next())){
687         aodEvent = (AliAODEvent*)(fe->GetTree()->GetUserInfo()->FindObject("AliAODEvent"));
688         if (!aodEvent) {
689           printf("No UserInfo on tree \n");
690         } else {
691           
692           //          TList* objL = (TList*)(aodEvent->GetList()->Clone());
693           TList* objL = (TList*)aodEvent->GetList();
694           printf("Get list of object from tree %d !!\n", objL->GetEntries());
695           TIter nextobject(objL);
696           TObject* obj =  0;
697           while((obj = nextobject()))
698           {
699             printf("Adding object from friend %s !\n", obj->GetName());
700             fAODObjects->Add(obj);
701           } // object "branch" loop
702         } // has userinfo  
703       } // friend loop
704     } // has friends    
705       // set the branch addresses
706     TIter next(fAODObjects);
707     TNamed *el;
708     while((el=(TNamed*)next())){
709       TString bname(el->GetName());
710         // check if branch exists under this Name
711       TBranch *br = tree->GetTree()->GetBranch(bname.Data());
712       if(br){
713         tree->SetBranchAddress(bname.Data(),fAODObjects->GetObjectRef(el));
714       } else {
715         br = tree->GetBranch(Form("%s.",bname.Data()));
716         if(br){
717           tree->SetBranchAddress(Form("%s.",bname.Data()),fAODObjects->GetObjectRef(el));
718         }
719         else{
720           printf("%s %d AliAODEvent::ReadFromTree() No Branch found with Name %s. \n",
721                  (char*)__FILE__,__LINE__,bname.Data());
722         }       
723       }
724     }
725     GetStdContent();
726       // when reading back we are not owner of the list 
727       // must not delete it
728     fAODObjects->SetOwner(kTRUE);
729     fAODObjects->SetName("AODObjectsConnectedToTree");
730       // we are not owner of the list objects 
731       // must not delete it
732     tree->GetUserInfo()->Add(fAODObjects);
733     fConnected = kTRUE;
734   }// no aodEvent
735   else {
736       // we can't get the list from the user data, create standard content
737       // and set it by hand
738     CreateStdContent();
739     TIter next(fAODObjects);
740     TNamed *el;
741     while((el=(TNamed*)next())){
742       TString bname(el->GetName());    
743       tree->SetBranchAddress(bname.Data(),fAODObjects->GetObjectRef(el));
744     }
745     GetStdContent();
746       // when reading back we are not owner of the list 
747       // must not delete it
748     fAODObjects->SetOwner(kTRUE);
749   }
750 }
751   //______________________________________________________________________________
752 Int_t  AliAODEvent::GetNumberOfPileupVerticesSPD() const{
753   // count number of SPD pileup vertices
754   Int_t nVertices=GetNumberOfVertices();
755   Int_t nPileupVertices=0;
756   for(Int_t iVert=0; iVert<nVertices; iVert++){
757     AliAODVertex *v=GetVertex(iVert);
758     if(v->GetType()==AliAODVertex::kPileupSPD) nPileupVertices++;
759   }
760   return nPileupVertices;
761 }
762 //______________________________________________________________________________
763 Int_t  AliAODEvent::GetNumberOfPileupVerticesTracks() const{
764   // count number of track pileup vertices
765   Int_t nVertices=GetNumberOfVertices();
766   Int_t nPileupVertices=0;
767   for(Int_t iVert=0; iVert<nVertices; iVert++){
768     AliAODVertex *v=GetVertex(iVert);
769     if(v->GetType()==AliAODVertex::kPileupTracks) nPileupVertices++;
770   }
771   return nPileupVertices;
772 }
773 //______________________________________________________________________________
774 AliAODVertex* AliAODEvent::GetPrimaryVertexSPD() const{
775   // Get SPD primary vertex
776   Int_t nVertices=GetNumberOfVertices();
777   for(Int_t iVert=0; iVert<nVertices; iVert++){
778     AliAODVertex *v=GetVertex(iVert);
779     if(v->GetType()==AliAODVertex::kMainSPD) return v;
780   }
781   return 0;
782 }
783 //______________________________________________________________________________
784 AliAODVertex* AliAODEvent::GetPileupVertexSPD(Int_t iV) const{
785   // Get pile-up vertex iV
786   Int_t nVertices=GetNumberOfVertices();
787   Int_t counter=0;
788   for(Int_t iVert=0; iVert<nVertices; iVert++){
789     AliAODVertex *v=GetVertex(iVert);
790     if(v->GetType()==AliAODVertex::kPileupSPD){
791       if(counter==iV) return v;
792       ++counter;
793     }
794   }
795   return 0;
796 }
797 //______________________________________________________________________________
798 AliAODVertex* AliAODEvent::GetPileupVertexTracks(Int_t iV) const{
799   // Get pile-up vertex iV
800   Int_t nVertices=GetNumberOfVertices();
801   Int_t counter=0;
802   for(Int_t iVert=0; iVert<nVertices; iVert++){
803     AliAODVertex *v=GetVertex(iVert);
804     if(v->GetType()==AliAODVertex::kPileupTracks){
805       if(counter==iV) return v;
806       ++counter;
807     }
808   }
809   return 0;
810 }
811 //______________________________________________________________________________
812 Bool_t  AliAODEvent::IsPileupFromSPD(Int_t minContributors, 
813                                      Double_t minZdist, 
814                                      Double_t nSigmaZdist, 
815                                      Double_t nSigmaDiamXY, 
816                                      Double_t nSigmaDiamZ) const{
817   //
818   // This function checks if there was a pile up
819   // reconstructed with SPD
820   //
821   AliAODVertex *mainV=GetPrimaryVertexSPD();
822   if(!mainV) return kFALSE;
823   Int_t nc1=mainV->GetNContributors();
824   if(nc1<1) return kFALSE;
825   Int_t nPileVert=GetNumberOfPileupVerticesSPD();
826   if(nPileVert==0) return kFALSE;
827   Int_t nVertices=GetNumberOfVertices();
828   
829   for(Int_t iVert=0; iVert<nVertices; iVert++){
830     AliAODVertex *pv=GetVertex(iVert);
831     if(pv->GetType()!=AliAODVertex::kPileupSPD) continue;
832     Int_t nc2=pv->GetNContributors();
833     if(nc2>=minContributors){
834       Double_t z1=mainV->GetZ();
835       Double_t z2=pv->GetZ();
836       Double_t distZ=TMath::Abs(z2-z1);
837       Double_t distZdiam=TMath::Abs(z2-GetDiamondZ());
838       Double_t cutZdiam=nSigmaDiamZ*TMath::Sqrt(GetSigma2DiamondZ());
839       if(GetSigma2DiamondZ()<0.0001)cutZdiam=99999.; //protection for missing z diamond information
840       if(distZ>minZdist && distZdiam<cutZdiam){
841         Double_t x2=pv->GetX();
842         Double_t y2=pv->GetY();
843         Double_t distXdiam=TMath::Abs(x2-GetDiamondX());
844         Double_t distYdiam=TMath::Abs(y2-GetDiamondY());
845         Double_t cov1[6],cov2[6];       
846         mainV->GetCovarianceMatrix(cov1);
847         pv->GetCovarianceMatrix(cov2);
848         Double_t errxDist=TMath::Sqrt(cov2[0]+GetSigma2DiamondX());
849         Double_t erryDist=TMath::Sqrt(cov2[2]+GetSigma2DiamondY());
850         Double_t errzDist=TMath::Sqrt(cov1[5]+cov2[5]);
851         Double_t cutXdiam=nSigmaDiamXY*errxDist;
852         if(GetSigma2DiamondX()<0.0001)cutXdiam=99999.; //protection for missing diamond information
853         Double_t cutYdiam=nSigmaDiamXY*erryDist;
854         if(GetSigma2DiamondY()<0.0001)cutYdiam=99999.; //protection for missing diamond information
855         if( (distXdiam<cutXdiam) && (distYdiam<cutYdiam) && (distZ>nSigmaZdist*errzDist) ){
856           return kTRUE;
857         }
858       }
859     }
860   }
861   return kFALSE;
862 }
863
864 //______________________________________________________________________________
865 void AliAODEvent::Print(Option_t *) const
866 {
867   // Print the names of the all branches
868   TIter next(fAODObjects);
869   TNamed *el;
870   Printf(">>>>>  AOD  Content <<<<<");    
871   while((el=(TNamed*)next())){
872     Printf(">> %s ",el->GetName());      
873   }
874   Printf(">>>>>                <<<<<");    
875   
876   return;
877 }
878
879 //______________________________________________________________________________
880 void AliAODEvent::AssignIDtoCollection(const TCollection* col)
881 {
882     // Static method which assigns a ID to each object in a collection
883     // In this way the objects are marked as referenced and written with 
884     // an ID. This has the advantage that TRefs to this objects can be 
885     // written by a subsequent process.
886     TIter next(col);
887     TObject* obj;
888     while ((obj = next()))
889         TProcessID::AssignID(obj);
890 }
891
892 //______________________________________________________________________________
893 Bool_t AliAODEvent::IsPileupFromSPDInMultBins() const {
894     Int_t nTracklets=GetTracklets()->GetNumberOfTracklets();
895     if(nTracklets<20) return IsPileupFromSPD(3,0.8);
896     else if(nTracklets<50) return IsPileupFromSPD(4,0.8);
897     else return IsPileupFromSPD(5,0.8);
898 }
899
900 //______________________________________________________________________________
901 void AliAODEvent::Reset()
902 {
903   // Handle the cases
904   // Std content + Non std content
905
906   ClearStd();
907   if(fAODObjects->GetSize()>kAODListN){
908     // we have non std content
909     // this also covers aodfriends
910     for(int i = kAODListN;i < fAODObjects->GetSize();++i){
911       TObject *pObject = fAODObjects->At(i);
912       // TClonesArrays
913       if(pObject->InheritsFrom(TClonesArray::Class())){
914        ((TClonesArray*)pObject)->Delete();
915       }
916       else if(!pObject->InheritsFrom(TCollection::Class())){
917        TClass *pClass = TClass::GetClass(pObject->ClassName());
918        if (pClass && pClass->GetListOfMethods()->FindObject("Clear")) {
919          AliDebug(1, Form("Clear for object %s class %s", pObject->GetName(), pObject->ClassName()));
920          pObject->Clear();
921        }
922        else {
923          AliDebug(1, Form("ResetWithPlacementNew for object %s class %s", pObject->GetName(), pObject->ClassName()));
924           Long_t dtoronly = TObject::GetDtorOnly();
925           TObject::SetDtorOnly(pObject);
926           delete pObject;
927           pClass->New(pObject);
928           TObject::SetDtorOnly((void*)dtoronly);
929        }
930       }
931       else{
932        AliWarning(Form("No reset for %s \n",
933                        pObject->ClassName()));
934       }
935     }
936   }
937 }
938
939 Float_t AliAODEvent::GetVZEROEqMultiplicity(Int_t i) const
940 {
941   // Get VZERO Multiplicity for channel i
942   // Themethod uses the equalization factors
943   // stored in the ESD-run object in order to
944   // get equal multiplicities within a VZERO rins (1/8 of VZERO)
945   if (!fAODVZERO || !fHeader) return -1;
946
947   Int_t ring = i/8;
948   Float_t factorSum = 0;
949   for(Int_t j = 8*ring; j < (8*ring+8); ++j) {
950     factorSum += fHeader->GetVZEROEqFactors(j);
951   }
952   Float_t factor = fHeader->GetVZEROEqFactors(i)*8./factorSum;
953
954   return (fAODVZERO->GetMultiplicity(i)/factor);
955 }
956
957 //------------------------------------------------------------
958 void  AliAODEvent::SetTOFHeader(const AliTOFHeader *header)
959 {
960   //
961   // Set the TOF event_time
962   //
963
964   if (fTOFHeader) {
965     *fTOFHeader=*header;
966     //fTOFHeader->SetName(fgkESDListName[kTOFHeader]);
967   }
968   else {
969     // for analysis of reconstructed events
970     // when this information is not avaliable
971     fTOFHeader = new AliTOFHeader(*header);
972     //AddObject(fTOFHeader);
973   }
974
975 }
976 //------------------------------------------------------------
977 AliAODHMPIDrings *AliAODEvent::GetHMPIDringForTrackID(Int_t trackID) const
978 {
979   //
980   // Returns the HMPID object if any for a given track ID
981   //
982   if(GetHMPIDrings())
983   {
984     for(Int_t ien = 0 ; ien < GetNHMPIDrings(); ien++)
985     {
986       if( GetHMPIDring(ien)->GetHmpTrkID() == trackID ) return GetHMPIDring(ien);      
987     }//rings loop  
988   }
989   return 0;
990 }
991 //------------------------------------------------------------
992 Int_t AliAODEvent::GetNHMPIDrings() const   
993
994   //
995   // If there is a list of HMPID rings in the given AOD event, return their number
996   //
997   if ( fHMPIDrings) return fHMPIDrings->GetEntriesFast(); 
998   else return -1;
999
1000 //------------------------------------------------------------
1001 AliAODHMPIDrings *AliAODEvent::GetHMPIDring(Int_t nRings) const
1002
1003   //
1004   // If there is a list of HMPID rings in the given AOD event, return corresponding ring
1005   //
1006   if(fHMPIDrings) {
1007     if(   (AliAODHMPIDrings*)fHMPIDrings->UncheckedAt(nRings) ) {
1008       return (AliAODHMPIDrings*)fHMPIDrings->UncheckedAt(nRings);
1009     }
1010     else return 0x0;
1011   }
1012   else return 0x0;  
1013 }
1014 //------------------------------------------------------------
1015 AliAODTrdTrack& AliAODEvent::AddTrdTrack(const AliVTrdTrack *track) {
1016   return *(new ((*fTrdTracks)[fTrdTracks->GetEntriesFast()]) AliAODTrdTrack(*track));
1017 }
1018
1019 //______________________________________________________________________________
1020 void AliAODEvent::ConnectTracks() {
1021 // Connect tracks to this event
1022   if (fTracksConnected || !fTracks || !fTracks->GetEntriesFast()) return;
1023   AliAODTrack *track;
1024   TIter next(fTracks);
1025   while ((track=(AliAODTrack*)next())) track->SetAODEvent(this);
1026   fTracksConnected = kTRUE;
1027 }
1028