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