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