]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliAODEvent.cxx
Virtual method declared constant.
[u/mrichter/AliRoot.git] / STEER / 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
34 ClassImp(AliAODEvent)
35
36 // definition of std AOD member names
37   const char* AliAODEvent::fAODListName[kAODListN] = {"header",
38                                                       "tracks",
39                                                       "vertices",
40                                                       "v0s",
41                                                       "cascades",
42                                                       "tracklets",
43                                                       "jets",
44                                                       "emcalCells",
45                                                       "phosCells",
46                                                       "caloClusters",
47                                                       "fmdClusters",
48                                                       "pmdClusters"
49                                                       
50 };
51 //______________________________________________________________________________
52 AliAODEvent::AliAODEvent() :
53   AliVEvent(),
54   fAODObjects(new TList()),
55   fAODFolder(0),
56   fConnected(kFALSE),
57   fHeader(0),
58   fTracks(0),
59   fVertices(0),
60   fV0s(0),
61   fCascades(0),
62   fTracklets(0),
63   fJets(0),
64   fEmcalCells(0),
65   fPhosCells(0),
66   fCaloClusters(0),
67   fFmdClusters(0),
68   fPmdClusters(0)
69 {
70   // default constructor
71 }
72
73 //______________________________________________________________________________
74 AliAODEvent::AliAODEvent(const AliAODEvent& aod):
75   AliVEvent(aod),
76   fAODObjects(new TList()),
77   fAODFolder(new TFolder()),
78   fConnected(kFALSE),
79   fHeader(new AliAODHeader(*aod.fHeader)),
80   fTracks(new TClonesArray(*aod.fTracks)),
81   fVertices(new TClonesArray(*aod.fVertices)),
82   fV0s(new TClonesArray(*aod.fV0s)),
83   fCascades(new TClonesArray(*aod.fCascades)),
84   fTracklets(new AliAODTracklets(*aod.fTracklets)),
85   fJets(new TClonesArray(*aod.fJets)),
86   fEmcalCells(new AliAODCaloCells(*aod.fEmcalCells)),
87   fPhosCells(new AliAODCaloCells(*aod.fPhosCells)),
88   fCaloClusters(new TClonesArray(*aod.fCaloClusters)),
89   fFmdClusters(new TClonesArray(*aod.fFmdClusters)),
90   fPmdClusters(new TClonesArray(*aod.fPmdClusters))
91 {
92   // Copy constructor
93   AddObject(fHeader);
94   AddObject(fTracks);
95   AddObject(fVertices);
96   AddObject(fV0s);
97   AddObject(fCascades);
98   AddObject(fTracklets);
99   AddObject(fJets);
100   AddObject(fEmcalCells);
101   AddObject(fPhosCells);
102   AddObject(fCaloClusters);
103   AddObject(fFmdClusters);
104   AddObject(fPmdClusters);
105   fConnected = aod.fConnected;
106   GetStdContent();
107 }
108
109 //______________________________________________________________________________
110 AliAODEvent & AliAODEvent::operator=(const AliAODEvent& aod) {
111
112     // Assignment operator
113
114     if(&aod == this) return *this;
115     AliVEvent::operator=(aod);
116
117     fAODObjects      = new TList();
118     fAODFolder       = new TFolder();
119     fConnected       = aod.fConnected;
120     fHeader          = new AliAODHeader(*aod.fHeader);
121     fTracks          = new TClonesArray(*aod.fTracks);
122     fVertices        = new TClonesArray(*aod.fVertices);
123     fV0s             = new TClonesArray(*aod.fV0s);
124     fCascades        = new TClonesArray(*aod.fCascades);
125     fTracklets       = new AliAODTracklets(*aod.fTracklets);
126     fJets            = new TClonesArray(*aod.fJets);
127     fEmcalCells      = new AliAODCaloCells(*aod.fEmcalCells);
128     fPhosCells       = new AliAODCaloCells(*aod.fPhosCells);
129     fCaloClusters    = new TClonesArray(*aod.fCaloClusters);
130     fFmdClusters     = new TClonesArray(*aod.fFmdClusters);
131     fPmdClusters     = new TClonesArray(*aod.fPmdClusters);
132     
133     fAODObjects = new TList();
134     
135     AddObject(fHeader);
136     AddObject(fTracks);
137     AddObject(fVertices);
138     AddObject(fV0s);
139     AddObject(fCascades);
140     AddObject(fTracklets);
141     AddObject(fJets);
142     AddObject(fEmcalCells);
143     AddObject(fPhosCells);
144     AddObject(fCaloClusters);
145     AddObject(fFmdClusters);
146     AddObject(fPmdClusters);
147     GetStdContent();
148     return *this;
149 }
150
151
152 //______________________________________________________________________________
153 AliAODEvent::~AliAODEvent() 
154 {
155 // destructor
156     if(fAODObjects&&!fConnected)
157     {
158         delete fAODObjects;
159         fAODObjects = 0;
160     }
161
162     delete fAODFolder;
163 }
164
165 //______________________________________________________________________________
166 void AliAODEvent::AddObject(TObject* obj) 
167 {
168   // Add an object to the list of objects.
169   // Please be aware that in order to increase performance you should
170   // refrain from using TObjArrays (if possible). Use TClonesArrays, instead.
171   
172   fAODObjects->AddLast(obj);
173 }
174
175 //______________________________________________________________________________
176 void AliAODEvent::RemoveObject(TObject* obj) 
177 {
178   // Removes an object from the list of objects.
179   
180   fAODObjects->Remove(obj);
181 }
182
183 //______________________________________________________________________________
184 TObject *AliAODEvent::FindListObject(const char *objName)
185 {
186   // Return the pointer to the object with the given name.
187
188   return fAODObjects->FindObject(objName);
189 }
190
191 //______________________________________________________________________________
192 void AliAODEvent::CreateStdContent() 
193 {
194   // create the standard AOD content and set pointers
195
196   // create standard objects and add them to the TList of objects
197   AddObject(new AliAODHeader());
198   AddObject(new TClonesArray("AliAODTrack", 0));
199   AddObject(new TClonesArray("AliAODVertex", 0));
200   AddObject(new TClonesArray("AliAODv0", 0));
201   AddObject(new TClonesArray("AliAODcascade", 0));
202   AddObject(new AliAODTracklets());
203   AddObject(new TClonesArray("AliAODJet", 0));
204   AddObject(new AliAODCaloCells());
205   AddObject(new AliAODCaloCells());
206   AddObject(new TClonesArray("AliAODCaloCluster", 0));
207   AddObject(new TClonesArray("AliAODFmdCluster", 0));
208   AddObject(new TClonesArray("AliAODPmdCluster", 0));
209   // set names
210   SetStdNames();
211
212   // read back pointers
213   GetStdContent();
214   CreateStdFolders();
215   return;
216 }
217
218 void  AliAODEvent::MakeEntriesReferencable()
219 {
220     // Make all entries referencable in a subsequent process
221     //
222     TIter next(fAODObjects);
223     TObject* obj;
224     while ((obj = next()))
225     {
226         if(obj->InheritsFrom("TCollection"))
227             {
228                 AssignIDtoCollection((TCollection*)obj);
229             }
230     }
231 }
232
233 //______________________________________________________________________________
234 void AliAODEvent::SetStdNames()
235 {
236   // introduce the standard naming
237
238   if(fAODObjects->GetEntries()==kAODListN){
239     for(int i = 0;i < fAODObjects->GetEntries();i++){
240       TObject *fObj = fAODObjects->At(i);
241       if(fObj->InheritsFrom("TNamed")){
242         ((TNamed*)fObj)->SetName(fAODListName[i]);
243       }
244       else if(fObj->InheritsFrom("TClonesArray")){
245         ((TClonesArray*)fObj)->SetName(fAODListName[i]);
246       }
247     }
248   }
249   else{
250     printf("%s:%d SetStdNames() Wrong number of Std Entries \n",(char*)__FILE__,__LINE__);
251   }
252
253
254 void AliAODEvent::CreateStdFolders()
255 {
256     // Create the standard folder structure
257     fAODFolder = gROOT->GetRootFolder()->AddFolder("AOD", "AOD");
258     if(fAODObjects->GetEntries()==kAODListN){
259         for(int i = 0;i < fAODObjects->GetEntries();i++){
260             TObject *fObj = fAODObjects->At(i);
261             if(fObj->InheritsFrom("TClonesArray")){
262                 fAODFolder->AddFolder(fAODListName[i], fAODListName[i], (TCollection*) fObj);
263             } else {
264                 fAODFolder->AddFolder(fAODListName[i], fAODListName[i], 0);
265             }
266         }
267     }
268     else{
269         printf("%s:%d CreateStdFolders() Wrong number of Std Entries \n",(char*)__FILE__,__LINE__);
270     }
271
272
273 //______________________________________________________________________________
274 void AliAODEvent::GetStdContent()
275 {
276   // set pointers for standard content
277
278   fHeader        = (AliAODHeader*)fAODObjects->FindObject("header");
279   fTracks        = (TClonesArray*)fAODObjects->FindObject("tracks");
280   fVertices      = (TClonesArray*)fAODObjects->FindObject("vertices");
281   fV0s           = (TClonesArray*)fAODObjects->FindObject("v0s");
282   fCascades      = (TClonesArray*)fAODObjects->FindObject("cascades");
283   fTracklets     = (AliAODTracklets*)fAODObjects->FindObject("tracklets");
284   fJets          = (TClonesArray*)fAODObjects->FindObject("jets");
285   fEmcalCells    = (AliAODCaloCells*)fAODObjects->FindObject("emcalCells");
286   fPhosCells     = (AliAODCaloCells*)fAODObjects->FindObject("phosCells");
287   fCaloClusters  = (TClonesArray*)fAODObjects->FindObject("caloClusters");
288   fFmdClusters   = (TClonesArray*)fAODObjects->FindObject("fmdClusters");
289   fPmdClusters   = (TClonesArray*)fAODObjects->FindObject("pmdClusters");
290 }
291
292 //______________________________________________________________________________
293 void AliAODEvent::ResetStd(Int_t trkArrSize, 
294                            Int_t vtxArrSize, 
295                            Int_t v0ArrSize,
296                            Int_t cascadeArrSize,
297                            Int_t jetSize, 
298                            Int_t caloClusSize, 
299                            Int_t fmdClusSize, 
300                            Int_t pmdClusSize
301                            )
302 {
303   // deletes content of standard arrays and resets size 
304   
305   fTracks->Delete();
306   if (trkArrSize > fTracks->GetSize()) 
307     fTracks->Expand(trkArrSize);
308
309   fVertices->Delete();
310   if (vtxArrSize > fVertices->GetSize()) 
311     fVertices->Expand(vtxArrSize);
312          
313   fV0s->Delete();
314   if (v0ArrSize > fV0s->GetSize()) 
315     fV0s->Expand(v0ArrSize);
316   
317   fCascades->Delete();
318   if (cascadeArrSize > fCascades->GetSize()) 
319     fCascades->Expand(cascadeArrSize);
320   
321   fJets->Delete();
322   if (jetSize > fJets->GetSize())
323     fJets->Expand(jetSize);
324
325   fCaloClusters->Delete();
326   if (caloClusSize > fCaloClusters->GetSize()) 
327     fCaloClusters->Expand(caloClusSize);
328
329   fFmdClusters->Delete();
330   if (fmdClusSize > fFmdClusters->GetSize()) 
331     fFmdClusters->Expand(fmdClusSize);
332
333   fPmdClusters->Delete();
334   if (pmdClusSize > fPmdClusters->GetSize()) 
335     fPmdClusters->Expand(pmdClusSize);
336
337   // Reset the tracklets
338   fTracklets->DeleteContainer();
339   fPhosCells->DeleteContainer();  
340   fEmcalCells->DeleteContainer();
341
342 }
343
344 void AliAODEvent::ClearStd()
345 {
346   // clears the standard arrays
347   fHeader        ->RemoveQTheta();
348   fTracks        ->Delete();
349   fVertices      ->Delete();
350   fV0s           ->Delete();
351   fCascades      ->Delete();
352   fTracklets     ->DeleteContainer();
353   fJets          ->Delete();
354   fEmcalCells    ->DeleteContainer();
355   fPhosCells     ->DeleteContainer();
356   fCaloClusters  ->Delete();
357   fFmdClusters   ->Clear();
358   fPmdClusters   ->Clear();
359 }
360
361 //_________________________________________________________________
362 Int_t AliAODEvent::GetPHOSClusters(TRefArray *clusters) const
363 {
364   // fills the provided TRefArray with all found phos clusters
365   
366   clusters->Clear();
367   
368   AliAODCaloCluster *cl = 0;
369   Bool_t first = kTRUE;
370   for (Int_t i = 0; i < GetNCaloClusters() ; i++) {
371     if ( (cl = GetCaloCluster(i)) ) {
372       if (cl->IsPHOSCluster()){
373         if(first) {
374           new (clusters) TRefArray(TProcessID::GetProcessWithUID(cl)); 
375           first=kFALSE;
376         }
377         clusters->Add(cl);
378         //printf("IsPHOS cluster %d, E %2.3f Size: %d \n",i,cl->E(),clusters->GetEntriesFast());
379       }
380     }
381   }
382   return clusters->GetEntriesFast();
383 }
384
385 //_________________________________________________________________
386 Int_t AliAODEvent::GetEMCALClusters(TRefArray *clusters) const
387 {
388   // fills the provided TRefArray with all found emcal clusters
389
390   clusters->Clear();
391   cout<<"AOD event 1: nclus "<<GetNCaloClusters()<<endl;
392   AliAODCaloCluster *cl = 0;
393   Bool_t first = kTRUE;
394   for (Int_t i = 0; i < GetNCaloClusters(); i++) {
395     if ( (cl = GetCaloCluster(i)) ) {
396       if (cl->IsEMCALCluster()){
397         if(first) {
398           new (clusters) TRefArray(TProcessID::GetProcessWithUID(cl)); 
399           first=kFALSE;
400         }
401         clusters->Add(cl);
402         //printf("IsEMCal cluster %d, E %2.3f Size: %d \n",i,cl->E(),clusters->GetEntriesFast());
403       }
404     }
405   }
406   return clusters->GetEntriesFast();
407 }
408
409
410 //______________________________________________________________________________
411 Int_t AliAODEvent::GetMuonTracks(TRefArray *muonTracks) const
412 {
413   // fills the provided TRefArray with all found muon tracks
414
415   muonTracks->Clear();
416
417   AliAODTrack *track = 0;
418   for (Int_t iTrack = 0; iTrack < GetNTracks(); iTrack++) {
419     if ((track = GetTrack(iTrack))->IsMuonTrack()) {
420       muonTracks->Add(track);
421     }
422   }
423   
424   return muonTracks->GetEntriesFast();
425 }
426
427
428 void AliAODEvent::ReadFromTree(TTree *tree, Option_t* opt /*= ""*/)
429 {
430   // Connects aod event to tree
431   
432   if(!tree){
433     Printf("%s %d AliAODEvent::ReadFromTree() Zero Pointer to Tree \n",(char*)__FILE__,__LINE__);
434     return;
435   }
436   // load the TTree
437   if(!tree->GetTree())tree->LoadTree(0);
438
439   // Try to find AliAODEvent
440   AliAODEvent *aodEvent = 0;
441   aodEvent = (AliAODEvent*)tree->GetTree()->GetUserInfo()->FindObject("AliAODEvent");
442   if(aodEvent){
443     // Check if already connected to tree
444     TList* connectedList = (TList*) (tree->GetUserInfo()->FindObject("AODObjectsConnectedToTree"));
445     if (connectedList && (strcmp(opt, "reconnect"))) {
446         // If connected use the connected list of objects
447         printf("Delete and reconnect \n");
448         
449         fAODObjects->Delete();
450         fAODObjects = connectedList;
451         GetStdContent(); 
452         fConnected = kTRUE;
453         return;
454     } 
455     // Connect to tree
456     // prevent a memory leak when reading back the TList
457     if (!(strcmp(opt, "reconnect"))) fAODObjects->Delete();
458     delete fAODObjects;
459     fAODObjects = 0;
460     // create a new TList from the UserInfo TList... 
461     // copy constructor does not work...
462     fAODObjects = (TList*)(aodEvent->GetList()->Clone());
463     fAODObjects->SetOwner(kFALSE);
464     if(fAODObjects->GetEntries()<kAODListN){
465       printf("%s %d AliAODEvent::ReadFromTree() TList contains less than the standard contents %d < %d \n",
466              (char*)__FILE__,__LINE__,fAODObjects->GetEntries(),kAODListN);
467     }
468     //
469     // Let's find out whether we have friends
470     TList* friendL = tree->GetTree()->GetListOfFriends();
471     if (friendL) 
472     {
473         TIter next(friendL);
474         TFriendElement* fe;
475         while ((fe = (TFriendElement*)next())){
476             aodEvent = (AliAODEvent*)(fe->GetTree()->GetUserInfo()->FindObject("AliAODEvent"));
477             if (!aodEvent) {
478                 printf("No UserInfo on tree \n");
479             } else {
480
481                 TList* objL = (TList*)(aodEvent->GetList()->Clone());
482                 printf("Get list of object from tree %d !!\n", objL->GetEntries());
483                 TIter nextobject(objL);
484                 TObject* obj =  0;
485                 while((obj = nextobject()))
486                 {
487                     printf("Adding object from friend %s !\n", obj->GetName());
488                     fAODObjects->Add(obj);
489                 } // object "branch" loop
490             } // has userinfo  
491         } // friend loop
492     } // has friends    
493             
494
495 // set the branch addresses
496     TIter next(fAODObjects);
497     TNamed *el;
498     while((el=(TNamed*)next())){
499       TString bname(el->GetName());
500       // check if branch exists under this Name
501       TBranch *br = tree->GetTree()->GetBranch(bname.Data());
502       if(br){
503         tree->SetBranchAddress(bname.Data(),fAODObjects->GetObjectRef(el));
504       } else {
505           br = tree->GetBranch(Form("%s.",bname.Data()));
506           if(br){
507               tree->SetBranchAddress(Form("%s.",bname.Data()),fAODObjects->GetObjectRef(el));
508           }
509           else{
510               printf("%s %d AliAODEvent::ReadFromTree() No Branch found with Name %s. \n",
511                      (char*)__FILE__,__LINE__,bname.Data());
512           }     
513       }
514     }
515     GetStdContent();
516     // when reading back we are not owner of the list 
517     // must not delete it
518     fAODObjects->SetOwner(kFALSE);
519     fAODObjects->SetName("AODObjectsConnectedToTree");
520     // we are not owner of the list objects 
521     // must not delete it
522     tree->GetUserInfo()->Add(fAODObjects);
523     fConnected = kTRUE;
524   }// no aodEvent
525   else {
526     // we can't get the list from the user data, create standard content
527     // and set it by hand
528     CreateStdContent();
529     TIter next(fAODObjects);
530     TNamed *el;
531     while((el=(TNamed*)next())){
532       TString bname(el->GetName());    
533       tree->SetBranchAddress(bname.Data(),fAODObjects->GetObjectRef(el));
534     }
535     GetStdContent();
536     // when reading back we are not owner of the list 
537     // must not delete it
538     fAODObjects->SetOwner(kFALSE);
539   }
540 }
541
542 //______________________________________________________________________________
543 void AliAODEvent::Print(Option_t *) const
544 {
545   // Something meaningful should be implemented here.
546   
547   return;
548 }
549
550 void AliAODEvent::AssignIDtoCollection(TCollection* col)
551 {
552     // Static method which assigns a ID to each object in a collection
553     // In this way the objects are marked as referenced and written with 
554     // an ID. This has the advantage that TRefs to this objects can be 
555     // written by a subsequent process.
556     TIter next(col);
557     TObject* obj;
558     while ((obj = next()))
559         TProcessID::AssignID(obj);
560 }