]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AOD/AliAODEvent.cxx
b2a6c9df84f351794b763747a3a18e4f3ec4fa06
[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
35 ClassImp(AliAODEvent)
36
37 // definition of std AOD member names
38   const char* AliAODEvent::fAODListName[kAODListN] = {"header",
39                                                       "tracks",
40                                                       "vertices",
41                                                       "v0s",
42                                                       "cascades",
43                                                       "tracklets",
44                                                       "jets",
45                                                       "emcalCells",
46                                                       "phosCells",
47                                                                         "caloClusters",
48                                                                         "emcalTrigger",
49                                                                         "phosTrigger",
50                                                       "fmdClusters",
51                                                       "pmdClusters",
52                                                       "dimuons",
53                                                       "AliAODTZERO",
54                                                       "AliAODVZERO",
55                                                       "AliAODZDC",
56                                                       "AliTOFHeader"
57 #ifdef MFT_UPGRADE        
58                                                       ,"AliAODMFT"
59 #endif                                                
60                                                       
61 };
62 //______________________________________________________________________________
63 AliAODEvent::AliAODEvent() :
64   AliVEvent(),
65   fAODObjects(0),
66   fAODFolder(0),
67   fConnected(kFALSE),
68   fHeader(0),
69   fTracks(0),
70   fVertices(0),
71   fV0s(0),
72   fCascades(0),
73   fTracklets(0),
74   fJets(0),
75   fEmcalCells(0),
76   fPhosCells(0),
77   fCaloClusters(0),
78   fEMCALTrigger(0),
79   fPHOSTrigger(0),
80   fFmdClusters(0),
81   fPmdClusters(0),
82   fDimuons(0),
83   fAODTZERO(0),
84   fAODVZERO(0),
85   fAODZDC(0),
86   fTOFHeader(0)
87 #ifdef MFT_UPGRADE
88   ,fAODMFT(0)
89 #endif
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   fHeader(new AliAODHeader(*aod.fHeader)),
102   fTracks(new TClonesArray(*aod.fTracks)),
103   fVertices(new TClonesArray(*aod.fVertices)),
104   fV0s(new TClonesArray(*aod.fV0s)),
105   fCascades(new TClonesArray(*aod.fCascades)),
106   fTracklets(new AliAODTracklets(*aod.fTracklets)),
107   fJets(new TClonesArray(*aod.fJets)),
108   fEmcalCells(new AliAODCaloCells(*aod.fEmcalCells)),
109   fPhosCells(new AliAODCaloCells(*aod.fPhosCells)),
110   fCaloClusters(new TClonesArray(*aod.fCaloClusters)),
111   fEMCALTrigger(new AliAODCaloTrigger(*aod.fEMCALTrigger)),
112   fPHOSTrigger(new AliAODCaloTrigger(*aod.fPHOSTrigger)),
113   fFmdClusters(new TClonesArray(*aod.fFmdClusters)),
114   fPmdClusters(new TClonesArray(*aod.fPmdClusters)),
115   fDimuons(new TClonesArray(*aod.fDimuons)),
116   fAODTZERO(new AliAODTZERO(*aod.fAODTZERO)),
117   fAODVZERO(new AliAODVZERO(*aod.fAODVZERO)),
118   fAODZDC(new AliAODZDC(*aod.fAODZDC)),
119   fTOFHeader(new AliTOFHeader(*aod.fTOFHeader))
120 #ifdef MFT_UPGRADE
121   ,fAODMFT(new AliAODMFT(*aod.fAODMFT))
122 #endif
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(fDimuons);
140   AddObject(fAODTZERO);
141   AddObject(fAODVZERO);
142   AddObject(fAODZDC);
143   AddObject(fTOFHeader);
144 #ifdef MFT_UPGRADE      
145   AddObject(fAODVZERO);
146 #endif
147   fConnected = aod.fConnected;
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   return *this;
245 }
246
247
248 //______________________________________________________________________________
249 AliAODEvent::~AliAODEvent() 
250 {
251 // destructor
252     delete fAODFolder;
253     fAODFolder = 0;
254     if(!fConnected) {
255 //       fAODObjects->Delete("slow");
256        delete fAODObjects;
257     }   
258 }
259
260 //______________________________________________________________________________
261 void AliAODEvent::AddObject(TObject* obj) 
262 {
263   // Add an object to the list of objects.
264   // Please be aware that in order to increase performance you should
265   // refrain from using TObjArrays (if possible). Use TClonesArrays, instead.
266   
267 //  if ( !fAODObjects ) {
268 //     fAODObjects = new TList();
269 //     fAODObjects->SetOwner();
270 //  }
271   if ( !fAODObjects->FindObject(obj) ) 
272   {
273     fAODObjects->AddLast(obj);
274   }
275 }
276
277 //______________________________________________________________________________
278 void AliAODEvent::RemoveObject(TObject* obj) 
279 {
280   // Removes an object from the list of objects.
281   
282   fAODObjects->Remove(obj);
283 }
284
285 //______________________________________________________________________________
286 TObject *AliAODEvent::FindListObject(const char *objName) const
287 {
288   // Return the pointer to the object with the given name.
289
290   return fAODObjects->FindObject(objName);
291 }
292
293 //______________________________________________________________________________
294 void AliAODEvent::CreateStdContent() 
295 {
296   // create the standard AOD content and set pointers
297
298   // create standard objects and add them to the TList of objects
299   AddObject(new AliAODHeader());
300   AddObject(new TClonesArray("AliAODTrack", 0));
301   AddObject(new TClonesArray("AliAODVertex", 0));
302   AddObject(new TClonesArray("AliAODv0", 0));
303   AddObject(new TClonesArray("AliAODcascade", 0));
304   AddObject(new AliAODTracklets());
305   AddObject(new TClonesArray("AliAODJet", 0));
306   AddObject(new AliAODCaloCells());
307   AddObject(new AliAODCaloCells());
308   AddObject(new TClonesArray("AliAODCaloCluster", 0));
309   AddObject(new AliAODCaloTrigger()); // EMCAL 
310   AddObject(new AliAODCaloTrigger()); // PHOS
311   AddObject(new TClonesArray("AliAODFmdCluster", 0));
312   AddObject(new TClonesArray("AliAODPmdCluster", 0));
313   AddObject(new TClonesArray("AliAODDimuon", 0));
314   AddObject(new AliAODTZERO());
315   AddObject(new AliAODVZERO());
316   AddObject(new AliAODZDC());
317   AddObject(new AliTOFHeader());
318 #ifdef MFT_UPGRADE
319   AddObject(new AliAODMFT());
320 #endif
321   // set names
322   SetStdNames();
323
324   // read back pointers
325   GetStdContent();
326   CreateStdFolders();
327   return;
328 }
329
330 void  AliAODEvent::MakeEntriesReferencable()
331 {
332     // Make all entries referencable in a subsequent process
333     //
334     TIter next(fAODObjects);
335     TObject* obj;
336     while ((obj = next()))
337     {
338         if(obj->InheritsFrom("TCollection"))
339             {
340                 AssignIDtoCollection((TCollection*)obj);
341             }
342     }
343 }
344
345 //______________________________________________________________________________
346 void AliAODEvent::SetStdNames()
347 {
348   // introduce the standard naming
349
350   if(fAODObjects->GetEntries()==kAODListN){
351     for(int i = 0;i < fAODObjects->GetEntries();i++){
352       TObject *fObj = fAODObjects->At(i);
353       if(fObj->InheritsFrom("TNamed")){
354         ((TNamed*)fObj)->SetName(fAODListName[i]);
355       }
356       else if(fObj->InheritsFrom("TClonesArray")){
357         ((TClonesArray*)fObj)->SetName(fAODListName[i]);
358       }
359     }
360   }
361   else{
362     printf("%s:%d SetStdNames() Wrong number of Std Entries \n",(char*)__FILE__,__LINE__);
363   }
364
365
366 void AliAODEvent::CreateStdFolders()
367 {
368     // Create the standard folder structure
369   if(fAODFolder)delete fAODFolder;
370     fAODFolder = gROOT->GetRootFolder()->AddFolder("AOD", "AOD");
371     if(fAODObjects->GetEntries()==kAODListN){
372         for(int i = 0;i < fAODObjects->GetEntries();i++){
373             TObject *fObj = fAODObjects->At(i);
374             if(fObj->InheritsFrom("TClonesArray")){
375                 fAODFolder->AddFolder(fAODListName[i], fAODListName[i], (TCollection*) fObj);
376             } else {
377                 fAODFolder->AddFolder(fAODListName[i], fAODListName[i], 0);
378             }
379         }
380     }
381     else{
382         printf("%s:%d CreateStdFolders() Wrong number of Std Entries \n",(char*)__FILE__,__LINE__);
383     }
384
385
386 //______________________________________________________________________________
387 void AliAODEvent::GetStdContent()
388 {
389   // set pointers for standard content
390
391   fHeader        = (AliAODHeader*)fAODObjects->FindObject("header");
392   fTracks        = (TClonesArray*)fAODObjects->FindObject("tracks");
393   fVertices      = (TClonesArray*)fAODObjects->FindObject("vertices");
394   fV0s           = (TClonesArray*)fAODObjects->FindObject("v0s");
395   fCascades      = (TClonesArray*)fAODObjects->FindObject("cascades");
396   fTracklets     = (AliAODTracklets*)fAODObjects->FindObject("tracklets");
397   fJets          = (TClonesArray*)fAODObjects->FindObject("jets");
398   fEmcalCells    = (AliAODCaloCells*)fAODObjects->FindObject("emcalCells");
399   fPhosCells     = (AliAODCaloCells*)fAODObjects->FindObject("phosCells");
400   fCaloClusters  = (TClonesArray*)fAODObjects->FindObject("caloClusters");
401         fEMCALTrigger  = (AliAODCaloTrigger*)fAODObjects->FindObject("emcalTrigger");
402         fPHOSTrigger   = (AliAODCaloTrigger*)fAODObjects->FindObject("phosTrigger");
403   fFmdClusters   = (TClonesArray*)fAODObjects->FindObject("fmdClusters");
404   fPmdClusters   = (TClonesArray*)fAODObjects->FindObject("pmdClusters");
405   fDimuons       = (TClonesArray*)fAODObjects->FindObject("dimuons");
406   fAODTZERO      = (AliAODTZERO*)fAODObjects->FindObject("AliAODTZERO");
407   fAODVZERO      = (AliAODVZERO*)fAODObjects->FindObject("AliAODVZERO");
408   fAODZDC        = (AliAODZDC*)fAODObjects->FindObject("AliAODZDC");
409   fTOFHeader     = (AliTOFHeader*)fAODObjects->FindObject("AliTOFHeader");
410 #ifdef MFT_UPGRADE
411   fAODMFT        = (AliAODMFT*)fAODObjects->FindObject("AliAODMFT");
412 #endif
413 }
414
415 //______________________________________________________________________________
416 void AliAODEvent::ResetStd(Int_t trkArrSize, 
417                            Int_t vtxArrSize, 
418                            Int_t v0ArrSize,
419                            Int_t cascadeArrSize,
420                            Int_t jetSize, 
421                            Int_t caloClusSize, 
422                            Int_t fmdClusSize, 
423                            Int_t pmdClusSize,
424                            Int_t dimuonArrSize
425                            )
426 {
427   // deletes content of standard arrays and resets size 
428   if (fTracks) {
429     fTracks->Delete();
430     if (trkArrSize > fTracks->GetSize()) 
431       fTracks->Expand(trkArrSize);
432   }
433   if (fVertices) {
434     fVertices->Delete();
435     if (vtxArrSize > fVertices->GetSize()) 
436       fVertices->Expand(vtxArrSize);
437   }
438   if (fV0s) {
439     fV0s->Delete();
440     if (v0ArrSize > fV0s->GetSize()) 
441       fV0s->Expand(v0ArrSize);
442   }
443   if (fCascades) {
444     fCascades->Delete();
445     if (cascadeArrSize > fCascades->GetSize()) 
446       fCascades->Expand(cascadeArrSize);
447   }
448   if (fJets) {
449     fJets->Delete();
450     if (jetSize > fJets->GetSize())
451       fJets->Expand(jetSize);
452   }
453   if (fCaloClusters) {
454     fCaloClusters->Delete();
455     if (caloClusSize > fCaloClusters->GetSize()) 
456       fCaloClusters->Expand(caloClusSize);
457   }
458   if (fFmdClusters) {
459     fFmdClusters->Delete();
460     if (fmdClusSize > fFmdClusters->GetSize()) 
461       fFmdClusters->Expand(fmdClusSize);
462   }
463   if (fPmdClusters) {
464     fPmdClusters->Delete();
465     if (pmdClusSize > fPmdClusters->GetSize()) 
466       fPmdClusters->Expand(pmdClusSize);
467   }
468   if (fDimuons) {
469     fDimuons->Delete();
470     if (dimuonArrSize > fDimuons->GetSize()) 
471       fDimuons->Expand(dimuonArrSize);
472   }
473   if (fTracklets)
474     fTracklets->DeleteContainer();
475   if (fPhosCells)
476     fPhosCells->DeleteContainer();  
477   if (fEmcalCells)
478     fEmcalCells->DeleteContainer();
479   
480   if (fEMCALTrigger)
481         fEMCALTrigger->DeAllocate();
482   if (fPHOSTrigger)
483         fPHOSTrigger->DeAllocate();
484
485 }
486
487 void AliAODEvent::ClearStd()
488 {
489   // clears the standard arrays
490   if (fHeader)
491     fHeader        ->Clear();
492   if (fTracks)
493     fTracks        ->Delete();
494   if (fVertices)
495     fVertices      ->Delete();
496   if (fV0s)
497     fV0s           ->Delete();
498   if (fCascades)
499     fCascades      ->Delete();
500   if (fTracklets)
501     fTracklets     ->DeleteContainer();
502   if (fJets)
503     fJets          ->Delete();
504   if (fEmcalCells)
505     fEmcalCells    ->DeleteContainer();
506   if (fPhosCells)
507     fPhosCells     ->DeleteContainer();
508   if (fCaloClusters)
509     fCaloClusters  ->Delete();
510   if (fFmdClusters)
511     fFmdClusters   ->Clear();
512   if (fPmdClusters)
513     fPmdClusters   ->Clear();
514   if (fDimuons)
515     fDimuons       ->Clear();
516         
517   if (fEMCALTrigger)
518         fEMCALTrigger->DeAllocate();
519   if (fPHOSTrigger)
520         fPHOSTrigger->DeAllocate();
521 }
522
523 //_________________________________________________________________
524 Int_t AliAODEvent::GetPHOSClusters(TRefArray *clusters) const
525 {
526   // fills the provided TRefArray with all found phos clusters
527   
528   clusters->Clear();
529   
530   AliAODCaloCluster *cl = 0;
531   Bool_t first = kTRUE;
532   for (Int_t i = 0; i < GetNumberOfCaloClusters() ; i++) {
533     if ( (cl = GetCaloCluster(i)) ) {
534       if (cl->IsPHOS()){
535         if(first) {
536           new (clusters) TRefArray(TProcessID::GetProcessWithUID(cl)); 
537           first=kFALSE;
538         }
539         clusters->Add(cl);
540         //printf("IsPHOS cluster %d, E %2.3f Size: %d \n",i,cl->E(),clusters->GetEntriesFast());
541       }
542     }
543   }
544   return clusters->GetEntriesFast();
545 }
546
547 //_________________________________________________________________
548 Int_t AliAODEvent::GetEMCALClusters(TRefArray *clusters) const
549 {
550   // fills the provided TRefArray with all found emcal clusters
551
552   clusters->Clear();
553   AliAODCaloCluster *cl = 0;
554   Bool_t first = kTRUE;
555   for (Int_t i = 0; i < GetNumberOfCaloClusters(); i++) {
556     if ( (cl = GetCaloCluster(i)) ) {
557       if (cl->IsEMCAL()){
558         if(first) {
559           new (clusters) TRefArray(TProcessID::GetProcessWithUID(cl)); 
560           first=kFALSE;
561         }
562         clusters->Add(cl);
563         //printf("IsEMCal cluster %d, E %2.3f Size: %d \n",i,cl->E(),clusters->GetEntriesFast());
564       }
565     }
566   }
567   return clusters->GetEntriesFast();
568 }
569
570
571 //______________________________________________________________________________
572 Int_t AliAODEvent::GetMuonTracks(TRefArray *muonTracks) const
573 {
574   // fills the provided TRefArray with all found muon tracks
575
576   muonTracks->Clear();
577
578   AliAODTrack *track = 0;
579   for (Int_t iTrack = 0; iTrack < GetNTracks(); iTrack++) {
580     track = GetTrack(iTrack);
581     if (track->IsMuonTrack()) {
582       muonTracks->Add(track);
583     }
584   }
585   
586   return muonTracks->GetEntriesFast();
587 }
588
589
590 //______________________________________________________________________________
591 Int_t AliAODEvent::GetNumberOfMuonTracks() const
592 {
593   // get number of muon tracks
594   Int_t nMuonTracks=0;
595   for (Int_t iTrack = 0; iTrack < GetNTracks(); iTrack++) {
596     if ((GetTrack(iTrack))->IsMuonTrack()) {
597        nMuonTracks++;
598     }
599   }
600   
601   return nMuonTracks;
602 }
603
604 //______________________________________________________________________________
605 void AliAODEvent::ReadFromTree(TTree *tree, Option_t* opt /*= ""*/)
606 {
607     // Connects aod event to tree
608   
609   if(!tree){
610     AliWarning("Zero Pointer to Tree \n");
611     return;
612   }
613     // load the TTree
614   if(!tree->GetTree())tree->LoadTree(0);
615   
616     // Try to find AliAODEvent
617   AliAODEvent *aodEvent = 0;
618   aodEvent = (AliAODEvent*)tree->GetTree()->GetUserInfo()->FindObject("AliAODEvent");
619   if(aodEvent){
620     // This event is connected to the tree by definition, just say so
621     aodEvent->SetConnected();
622       // Check if already connected to tree
623     TList* connectedList = (TList*) (tree->GetUserInfo()->FindObject("AODObjectsConnectedToTree"));
624     if (connectedList && (!strcmp(opt, "reconnect"))) {
625         // If connected use the connected list of objects
626         if (fAODObjects != connectedList) {
627            delete fAODObjects;
628            fAODObjects = connectedList;
629         }   
630         GetStdContent(); 
631         fConnected = kTRUE;
632         return;
633     } 
634       // Connect to tree
635       // prevent a memory leak when reading back the TList
636 //      if (!(strcmp(opt, "reconnect"))) fAODObjects->Delete();
637     
638       // create a new TList from the UserInfo TList... 
639       // copy constructor does not work...
640     //    fAODObjects = (TList*)(aodEvent->GetList()->Clone());
641     fAODObjects = (TList*)aodEvent->GetList();
642     fAODObjects->SetOwner(kTRUE);
643     if(fAODObjects->GetEntries()<kAODListN)
644     {
645       AliWarning(Form("AliAODEvent::ReadFromTree() TList contains less than the standard contents %d < %d"
646                       " That might be fine though (at least for filtered AODs)",fAODObjects->GetEntries(),kAODListN));
647     }
648       //
649       // Let's find out whether we have friends
650     TList* friendL = tree->GetTree()->GetListOfFriends();
651     if (friendL) 
652     {
653       TIter next(friendL);
654       TFriendElement* fe;
655       while ((fe = (TFriendElement*)next())){
656         aodEvent = (AliAODEvent*)(fe->GetTree()->GetUserInfo()->FindObject("AliAODEvent"));
657         if (!aodEvent) {
658           printf("No UserInfo on tree \n");
659         } else {
660           
661           //          TList* objL = (TList*)(aodEvent->GetList()->Clone());
662           TList* objL = (TList*)aodEvent->GetList();
663           printf("Get list of object from tree %d !!\n", objL->GetEntries());
664           TIter nextobject(objL);
665           TObject* obj =  0;
666           while((obj = nextobject()))
667           {
668             printf("Adding object from friend %s !\n", obj->GetName());
669             fAODObjects->Add(obj);
670           } // object "branch" loop
671         } // has userinfo  
672       } // friend loop
673     } // has friends    
674       // set the branch addresses
675     TIter next(fAODObjects);
676     TNamed *el;
677     while((el=(TNamed*)next())){
678       TString bname(el->GetName());
679         // check if branch exists under this Name
680       TBranch *br = tree->GetTree()->GetBranch(bname.Data());
681       if(br){
682         tree->SetBranchAddress(bname.Data(),fAODObjects->GetObjectRef(el));
683       } else {
684         br = tree->GetBranch(Form("%s.",bname.Data()));
685         if(br){
686           tree->SetBranchAddress(Form("%s.",bname.Data()),fAODObjects->GetObjectRef(el));
687         }
688         else{
689           printf("%s %d AliAODEvent::ReadFromTree() No Branch found with Name %s. \n",
690                  (char*)__FILE__,__LINE__,bname.Data());
691         }       
692       }
693     }
694     GetStdContent();
695       // when reading back we are not owner of the list 
696       // must not delete it
697     fAODObjects->SetOwner(kTRUE);
698     fAODObjects->SetName("AODObjectsConnectedToTree");
699       // we are not owner of the list objects 
700       // must not delete it
701     tree->GetUserInfo()->Add(fAODObjects);
702     fConnected = kTRUE;
703   }// no aodEvent
704   else {
705       // we can't get the list from the user data, create standard content
706       // and set it by hand
707     CreateStdContent();
708     TIter next(fAODObjects);
709     TNamed *el;
710     while((el=(TNamed*)next())){
711       TString bname(el->GetName());    
712       tree->SetBranchAddress(bname.Data(),fAODObjects->GetObjectRef(el));
713     }
714     GetStdContent();
715       // when reading back we are not owner of the list 
716       // must not delete it
717     fAODObjects->SetOwner(kTRUE);
718   }
719 }
720   //______________________________________________________________________________
721 Int_t  AliAODEvent::GetNumberOfPileupVerticesSPD() const{
722   // count number of SPD pileup vertices
723   Int_t nVertices=GetNumberOfVertices();
724   Int_t nPileupVertices=0;
725   for(Int_t iVert=0; iVert<nVertices; iVert++){
726     AliAODVertex *v=GetVertex(iVert);
727     if(v->GetType()==AliAODVertex::kPileupSPD) nPileupVertices++;
728   }
729   return nPileupVertices;
730 }
731 //______________________________________________________________________________
732 Int_t  AliAODEvent::GetNumberOfPileupVerticesTracks() const{
733   // count number of track pileup vertices
734   Int_t nVertices=GetNumberOfVertices();
735   Int_t nPileupVertices=0;
736   for(Int_t iVert=0; iVert<nVertices; iVert++){
737     AliAODVertex *v=GetVertex(iVert);
738     if(v->GetType()==AliAODVertex::kPileupTracks) nPileupVertices++;
739   }
740   return nPileupVertices;
741 }
742 //______________________________________________________________________________
743 AliAODVertex* AliAODEvent::GetPrimaryVertexSPD() const{
744   // Get SPD primary vertex
745   Int_t nVertices=GetNumberOfVertices();
746   for(Int_t iVert=0; iVert<nVertices; iVert++){
747     AliAODVertex *v=GetVertex(iVert);
748     if(v->GetType()==AliAODVertex::kMainSPD) return v;
749   }
750   return 0;
751 }
752 //______________________________________________________________________________
753 AliAODVertex* AliAODEvent::GetPileupVertexSPD(Int_t iV) const{
754   // Get pile-up vertex iV
755   Int_t nVertices=GetNumberOfVertices();
756   Int_t counter=0;
757   for(Int_t iVert=0; iVert<nVertices; iVert++){
758     AliAODVertex *v=GetVertex(iVert);
759     if(v->GetType()==AliAODVertex::kPileupSPD){
760       if(counter==iV) return v;
761       ++counter;
762     }
763   }
764   return 0;
765 }
766 //______________________________________________________________________________
767 AliAODVertex* AliAODEvent::GetPileupVertexTracks(Int_t iV) const{
768   // Get pile-up vertex iV
769   Int_t nVertices=GetNumberOfVertices();
770   Int_t counter=0;
771   for(Int_t iVert=0; iVert<nVertices; iVert++){
772     AliAODVertex *v=GetVertex(iVert);
773     if(v->GetType()==AliAODVertex::kPileupTracks){
774       if(counter==iV) return v;
775       ++counter;
776     }
777   }
778   return 0;
779 }
780 //______________________________________________________________________________
781 Bool_t  AliAODEvent::IsPileupFromSPD(Int_t minContributors, 
782                                      Double_t minZdist, 
783                                      Double_t nSigmaZdist, 
784                                      Double_t nSigmaDiamXY, 
785                                      Double_t nSigmaDiamZ) const{
786   //
787   // This function checks if there was a pile up
788   // reconstructed with SPD
789   //
790   AliAODVertex *mainV=GetPrimaryVertexSPD();
791   if(!mainV) return kFALSE;
792   Int_t nc1=mainV->GetNContributors();
793   if(nc1<1) return kFALSE;
794   Int_t nPileVert=GetNumberOfPileupVerticesSPD();
795   if(nPileVert==0) return kFALSE;
796   Int_t nVertices=GetNumberOfVertices();
797   
798   for(Int_t iVert=0; iVert<nVertices; iVert++){
799     AliAODVertex *pv=GetVertex(iVert);
800     if(pv->GetType()!=AliAODVertex::kPileupSPD) continue;
801     Int_t nc2=pv->GetNContributors();
802     if(nc2>=minContributors){
803       Double_t z1=mainV->GetZ();
804       Double_t z2=pv->GetZ();
805       Double_t distZ=TMath::Abs(z2-z1);
806       Double_t distZdiam=TMath::Abs(z2-GetDiamondZ());
807       Double_t cutZdiam=nSigmaDiamZ*TMath::Sqrt(GetSigma2DiamondZ());
808       if(GetSigma2DiamondZ()<0.0001)cutZdiam=99999.; //protection for missing z diamond information
809       if(distZ>minZdist && distZdiam<cutZdiam){
810         Double_t x2=pv->GetX();
811         Double_t y2=pv->GetY();
812         Double_t distXdiam=TMath::Abs(x2-GetDiamondX());
813         Double_t distYdiam=TMath::Abs(y2-GetDiamondY());
814         Double_t cov1[6],cov2[6];       
815         mainV->GetCovarianceMatrix(cov1);
816         pv->GetCovarianceMatrix(cov2);
817         Double_t errxDist=TMath::Sqrt(cov2[0]+GetSigma2DiamondX());
818         Double_t erryDist=TMath::Sqrt(cov2[2]+GetSigma2DiamondY());
819         Double_t errzDist=TMath::Sqrt(cov1[5]+cov2[5]);
820         Double_t cutXdiam=nSigmaDiamXY*errxDist;
821         if(GetSigma2DiamondX()<0.0001)cutXdiam=99999.; //protection for missing diamond information
822         Double_t cutYdiam=nSigmaDiamXY*erryDist;
823         if(GetSigma2DiamondY()<0.0001)cutYdiam=99999.; //protection for missing diamond information
824         if( (distXdiam<cutXdiam) && (distYdiam<cutYdiam) && (distZ>nSigmaZdist*errzDist) ){
825           return kTRUE;
826         }
827       }
828     }
829   }
830   return kFALSE;
831 }
832
833 //______________________________________________________________________________
834 void AliAODEvent::Print(Option_t *) const
835 {
836   // Print the names of the all branches
837   TIter next(fAODObjects);
838   TNamed *el;
839   Printf(">>>>>  AOD  Content <<<<<");    
840   while((el=(TNamed*)next())){
841     Printf(">> %s ",el->GetName());      
842   }
843   Printf(">>>>>                <<<<<");    
844   
845   return;
846 }
847
848 void AliAODEvent::AssignIDtoCollection(const TCollection* col)
849 {
850     // Static method which assigns a ID to each object in a collection
851     // In this way the objects are marked as referenced and written with 
852     // an ID. This has the advantage that TRefs to this objects can be 
853     // written by a subsequent process.
854     TIter next(col);
855     TObject* obj;
856     while ((obj = next()))
857         TProcessID::AssignID(obj);
858 }
859
860 Bool_t AliAODEvent::IsPileupFromSPDInMultBins() const {
861     Int_t nTracklets=GetTracklets()->GetNumberOfTracklets();
862     if(nTracklets<20) return IsPileupFromSPD(3,0.8);
863     else if(nTracklets<50) return IsPileupFromSPD(4,0.8);
864     else return IsPileupFromSPD(5,0.8);
865 }
866
867 Float_t AliAODEvent::GetVZEROEqMultiplicity(Int_t i) const
868 {
869   // Get VZERO Multiplicity for channel i
870   // Themethod uses the equalization factors
871   // stored in the ESD-run object in order to
872   // get equal multiplicities within a VZERO rins (1/8 of VZERO)
873   if (!fAODVZERO || !fHeader) return -1;
874
875   Int_t ring = i/8;
876   Float_t factorSum = 0;
877   for(Int_t j = 8*ring; j < (8*ring+8); ++j) {
878     factorSum += fHeader->GetVZEROEqFactors(j);
879   }
880   Float_t factor = fHeader->GetVZEROEqFactors(i)*8./factorSum;
881
882   return (fAODVZERO->GetMultiplicity(i)/factor);
883 }
884
885 //------------------------------------------------------------
886 void  AliAODEvent::SetTOFHeader(const AliTOFHeader *header)
887 {
888   //
889   // Set the TOF event_time
890   //
891
892   if (fTOFHeader) {
893     *fTOFHeader=*header;
894     //fTOFHeader->SetName(fgkESDListName[kTOFHeader]);
895   }
896   else {
897     // for analysis of reconstructed events
898     // when this information is not avaliable
899     fTOFHeader = new AliTOFHeader(*header);
900     //AddObject(fTOFHeader);
901   }
902
903 }