]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliAODEvent.cxx
8b2a1f44c47af75c565afc57c302cc0d221c21ca
[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   AliAODCaloCluster *cl = 0;
392   Bool_t first = kTRUE;
393   for (Int_t i = 0; i < GetNCaloClusters(); i++) {
394     if ( (cl = GetCaloCluster(i)) ) {
395       if (cl->IsEMCALCluster()){
396         if(first) {
397           new (clusters) TRefArray(TProcessID::GetProcessWithUID(cl)); 
398           first=kFALSE;
399         }
400         clusters->Add(cl);
401         //printf("IsEMCal cluster %d, E %2.3f Size: %d \n",i,cl->E(),clusters->GetEntriesFast());
402       }
403     }
404   }
405   return clusters->GetEntriesFast();
406 }
407
408
409 //______________________________________________________________________________
410 Int_t AliAODEvent::GetMuonTracks(TRefArray *muonTracks) const
411 {
412   // fills the provided TRefArray with all found muon tracks
413
414   muonTracks->Clear();
415
416   AliAODTrack *track = 0;
417   for (Int_t iTrack = 0; iTrack < GetNTracks(); iTrack++) {
418     if ((track = GetTrack(iTrack))->IsMuonTrack()) {
419       muonTracks->Add(track);
420     }
421   }
422   
423   return muonTracks->GetEntriesFast();
424 }
425
426
427 void AliAODEvent::ReadFromTree(TTree *tree, Option_t* opt /*= ""*/)
428 {
429   // Connects aod event to tree
430   
431   if(!tree){
432     Printf("%s %d AliAODEvent::ReadFromTree() Zero Pointer to Tree \n",(char*)__FILE__,__LINE__);
433     return;
434   }
435   // load the TTree
436   if(!tree->GetTree())tree->LoadTree(0);
437
438   // Try to find AliAODEvent
439   AliAODEvent *aodEvent = 0;
440   aodEvent = (AliAODEvent*)tree->GetTree()->GetUserInfo()->FindObject("AliAODEvent");
441   if(aodEvent){
442     // Check if already connected to tree
443     TList* connectedList = (TList*) (tree->GetUserInfo()->FindObject("AODObjectsConnectedToTree"));
444     if (connectedList && (strcmp(opt, "reconnect"))) {
445         // If connected use the connected list of objects
446         printf("Delete and reconnect \n");
447         
448         fAODObjects->Delete();
449         fAODObjects = connectedList;
450         GetStdContent(); 
451         fConnected = kTRUE;
452         return;
453     } 
454     // Connect to tree
455     // prevent a memory leak when reading back the TList
456     if (!(strcmp(opt, "reconnect"))) fAODObjects->Delete();
457     delete fAODObjects;
458     fAODObjects = 0;
459     // create a new TList from the UserInfo TList... 
460     // copy constructor does not work...
461     fAODObjects = (TList*)(aodEvent->GetList()->Clone());
462     fAODObjects->SetOwner(kFALSE);
463     if(fAODObjects->GetEntries()<kAODListN){
464       printf("%s %d AliAODEvent::ReadFromTree() TList contains less than the standard contents %d < %d \n",
465              (char*)__FILE__,__LINE__,fAODObjects->GetEntries(),kAODListN);
466     }
467     //
468     // Let's find out whether we have friends
469     TList* friendL = tree->GetTree()->GetListOfFriends();
470     if (friendL) 
471     {
472         TIter next(friendL);
473         TFriendElement* fe;
474         while ((fe = (TFriendElement*)next())){
475             aodEvent = (AliAODEvent*)(fe->GetTree()->GetUserInfo()->FindObject("AliAODEvent"));
476             if (!aodEvent) {
477                 printf("No UserInfo on tree \n");
478             } else {
479
480                 TList* objL = (TList*)(aodEvent->GetList()->Clone());
481                 printf("Get list of object from tree %d !!\n", objL->GetEntries());
482                 TIter nextobject(objL);
483                 TObject* obj =  0;
484                 while((obj = nextobject()))
485                 {
486                     printf("Adding object from friend %s !\n", obj->GetName());
487                     fAODObjects->Add(obj);
488                 } // object "branch" loop
489             } // has userinfo  
490         } // friend loop
491     } // has friends    
492             
493
494 // set the branch addresses
495     TIter next(fAODObjects);
496     TNamed *el;
497     while((el=(TNamed*)next())){
498       TString bname(el->GetName());
499       // check if branch exists under this Name
500       TBranch *br = tree->GetTree()->GetBranch(bname.Data());
501       if(br){
502         tree->SetBranchAddress(bname.Data(),fAODObjects->GetObjectRef(el));
503       } else {
504           br = tree->GetBranch(Form("%s.",bname.Data()));
505           if(br){
506               tree->SetBranchAddress(Form("%s.",bname.Data()),fAODObjects->GetObjectRef(el));
507           }
508           else{
509               printf("%s %d AliAODEvent::ReadFromTree() No Branch found with Name %s. \n",
510                      (char*)__FILE__,__LINE__,bname.Data());
511           }     
512       }
513     }
514     GetStdContent();
515     // when reading back we are not owner of the list 
516     // must not delete it
517     fAODObjects->SetOwner(kFALSE);
518     fAODObjects->SetName("AODObjectsConnectedToTree");
519     // we are not owner of the list objects 
520     // must not delete it
521     tree->GetUserInfo()->Add(fAODObjects);
522     fConnected = kTRUE;
523   }// no aodEvent
524   else {
525     // we can't get the list from the user data, create standard content
526     // and set it by hand
527     CreateStdContent();
528     TIter next(fAODObjects);
529     TNamed *el;
530     while((el=(TNamed*)next())){
531       TString bname(el->GetName());    
532       tree->SetBranchAddress(bname.Data(),fAODObjects->GetObjectRef(el));
533     }
534     GetStdContent();
535     // when reading back we are not owner of the list 
536     // must not delete it
537     fAODObjects->SetOwner(kFALSE);
538   }
539 }
540
541 //______________________________________________________________________________
542 void AliAODEvent::Print(Option_t *) const
543 {
544   // Something meaningful should be implemented here.
545   
546   return;
547 }
548
549 void AliAODEvent::AssignIDtoCollection(TCollection* col)
550 {
551     // Static method which assigns a ID to each object in a collection
552     // In this way the objects are marked as referenced and written with 
553     // an ID. This has the advantage that TRefs to this objects can be 
554     // written by a subsequent process.
555     TIter next(col);
556     TObject* obj;
557     while ((obj = next()))
558         TProcessID::AssignID(obj);
559 }