]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EVE/EveBase/AliEveEventManager.cxx
Some more effc++ warnings.
[u/mrichter/AliRoot.git] / EVE / EveBase / AliEveEventManager.cxx
1 // $Id$
2 // Main authors: Matevz Tadel & Alja Mrak-Tadel: 2006, 2007
3
4 /**************************************************************************
5  * Copyright(c) 1998-2008, ALICE Experiment at CERN, all rights reserved. *
6  * See http://aliceinfo.cern.ch/Offline/AliRoot/License.html for          *
7  * full copyright notice.                                                 *
8  **************************************************************************/
9
10 #include "AliEveEventManager.h"
11 #include <TEveUtil.h>
12 #include <TEveManager.h>
13
14 #include <AliRunLoader.h>
15 #include <AliRun.h>
16 #include <AliESDEvent.h>
17 #include <AliESDfriend.h>
18 #include <AliMagFMaps.h>
19 #include <AliCDBManager.h>
20 #include <AliHeader.h>
21 #include <AliGeomManager.h>
22
23 #include <TFile.h>
24 #include <TTree.h>
25 #include <TError.h>
26
27 #include <TROOT.h>
28 #include <TSystem.h>
29
30
31 //______________________________________________________________________________
32 // AliEveEventManager
33 //
34 // Provide interface for loading and navigating standard AliRoot data
35 // (AliRunLoader) and ESDs.
36 //
37 // Missing support for raw-data. For now this is handled individually
38 // by each sub-detector.
39 //
40
41 ClassImp(AliEveEventManager)
42
43 AliEveEventManager* gEvent = 0;
44
45 Bool_t AliEveEventManager::fgAssertRunLoader = kFALSE;
46 Bool_t AliEveEventManager::fgAssertESD       = kFALSE;
47
48 TString  AliEveEventManager::fgCdbUri("local://$ALICE_ROOT");
49
50 AliMagF* AliEveEventManager::fgMagField = 0;
51
52
53 AliEveEventManager::AliEveEventManager() :
54   TEveEventManager(),
55
56   fPath       (), fEventId (0),
57   fRunLoader (0),
58   fESDFile   (0), fESDTree (0), fESD (0),
59   fESDfriend (0), fESDfriendExists(kFALSE)
60 {
61   // Default constructor.
62 }
63
64 AliEveEventManager::AliEveEventManager(TString path, Int_t ev) :
65   TEveEventManager("AliEVE AliEveEventManager"),
66
67   fPath   (path), fEventId(-1),
68   fRunLoader (0),
69   fESDFile   (0), fESDTree (0), fESD (0),
70   fESDfriend (0), fESDfriendExists(kFALSE)
71 {
72   // Constructor with event-directory URL and event-id.
73
74   Open();
75   if (ev >= 0) GotoEvent(ev);
76 }
77
78 /******************************************************************************/
79
80 void AliEveEventManager::Open()
81 {
82   // Open event-data from URL specified in fPath.
83   // Attempts to create AliRunLoader() and to open ESD with ESDfriends.
84   // Warning is reported if run-loader or ESD is not found.
85   // Global data-members fgAssertRunLoader and fgAssertESD can be set
86   // to throw exceptions instead.
87
88   static const TEveException eH("AliEveEventManager::Open ");
89
90   gSystem->ExpandPathName(fPath);
91   if (fPath[0] != '/')
92     fPath = Form("%s/%s", gSystem->WorkingDirectory(), fPath.Data());
93
94   Int_t runNo = -1;
95
96   TString ga_path(Form("%s/galice.root", fPath.Data()));
97   if (gSystem->AccessPathName(ga_path, kReadPermission) == kFALSE)
98   {
99     fRunLoader = AliRunLoader::Open(ga_path);
100     if (fRunLoader)
101     {
102       TString alice_path = fPath + "/";
103       fRunLoader->SetDirName(alice_path);
104
105       if (fRunLoader->LoadgAlice() != 0)
106         Warning(eH, "failed loading gAlice via run-loader.");
107
108       if (fRunLoader->LoadHeader() == 0)
109       {
110         runNo = fRunLoader->GetHeader()->GetRun();
111       }
112       else
113       {
114         Warning(eH, "failed loading run-loader's header.");
115         delete fRunLoader;
116         fRunLoader = 0;
117       }
118     }
119     else // run-loader open failed
120     {
121       Warning(eH, "failed opening ALICE run-loader from '%s'.", ga_path.Data());
122     }
123   }
124   else // galice not readable
125   {
126     Warning(eH, "can not read '%s'.", ga_path.Data());
127   }
128   if (fRunLoader == 0)
129   {
130     if (fgAssertRunLoader)
131       throw(eH + "Bootstraping of run-loader failed. Its precence was requested.");
132     else
133       Warning(eH, "Bootstraping of run-loader failed.");
134   }
135
136
137   TString esd_path(Form("%s/AliESDs.root", fPath.Data()));
138   if (gSystem->AccessPathName(esd_path, kReadPermission) == kFALSE)
139   {
140     fESDFile = new TFile(esd_path);
141     if (fESDFile->IsZombie() == kFALSE)
142     {
143       fESD = new AliESDEvent();
144       fESDTree = (TTree*) fESDFile->Get("esdTree");
145       if (fESDTree != 0)
146       {
147         fESD->ReadFromTree(fESDTree);
148         runNo = fESD->GetESDRun()->GetRunNumber();
149
150         // Check if ESDfriends exists and attach the branch
151         TString p = Form("%s/AliESDfriends.root", fPath.Data());
152         if (gSystem->AccessPathName(p, kReadPermission) == kFALSE)
153         {
154           fESDfriendExists = kTRUE;
155           fESDTree->SetBranchStatus ("ESDfriend*", 1);
156           fESDTree->SetBranchAddress("ESDfriend.", &fESDfriend);
157         }
158       }
159       else // esdtree == 0
160       {
161         delete fESDFile; fESDFile = 0;
162         Warning(eH, "failed getting the esdTree.");
163       }
164     }
165     else // esd tfile is zombie
166     {
167       delete fESDFile; fESDFile = 0;
168       Warning(eH, "failed opening ESD from '%s'.", esd_path.Data());
169     }
170   }
171   else // esd not readable
172   {
173     Warning(eH, "can not read ESD file '%s'.", esd_path.Data());
174   }
175   if (fESDTree == 0)
176   {
177     if (fgAssertESD)
178     {
179       throw(eH + "ESD not initialized. Its precence was requested.");
180     } else {
181       Warning(eH, "ESD not initialized.");
182     }
183   }
184
185   if (runNo < 0)
186     throw(eH + "invalid run number.");
187
188   {
189     AliCDBManager* cdb = AliCDBManager::Instance();
190     cdb->SetDefaultStorage(fgCdbUri);
191     if (cdb->IsDefaultStorageSet() == kFALSE)
192       throw(eH + "CDB initialization failed.");
193     cdb->SetRun(runNo);
194   }
195
196   SetName(Form("AliEveEventManager %d", fEventId));
197   SetTitle(fPath);
198 }
199
200 void AliEveEventManager::GotoEvent(Int_t event)
201 {
202   static const TEveException eH("AliEveEventManager::GotoEvent ");
203
204   Int_t maxEvent = 0;
205   if (fRunLoader)
206     maxEvent = fRunLoader->GetNumberOfEvents() - 1;
207   else if (fESDTree)
208     maxEvent = fESDTree->GetEntries() - 1;
209   else
210     throw(eH + "neither RunLoader nor ESD loaded.");
211
212   if (event < 0 || event > maxEvent)
213     throw(eH + Form("event %d not present, available range [%d, %d].",
214                     event, 0, maxEvent));
215
216   TEveManager::TRedrawDisabler rd(gEve);
217   gEve->Redraw3D(kFALSE, kTRUE); // Enforce drop of all logicals.
218
219   // !!! MT this is somewhat brutal; at least optionally, one could be
220   // a bit gentler, checking for objs owning their external refs and having
221   // additinal parents.
222   DestroyElements();
223   fEventId = event;
224   SetName(Form("AliEveEventManager %d", fEventId));
225   UpdateItems();
226
227   if (fRunLoader) {
228     if (fRunLoader->GetEvent(fEventId) != 0)
229       throw(eH + "failed getting required event.");
230   }
231
232   if (fESDTree) {
233     if (fESDTree->GetEntry(fEventId) <= 0)
234       throw(eH + "failed getting required event from ESD.");
235
236     if (fESDfriendExists)
237       fESD->SetESDfriend(fESDfriend);
238   }
239
240   AfterNewEventLoaded();
241 }
242
243 void AliEveEventManager::Close()
244 {
245   if (fESDTree) {
246     delete fESD;       fESD       = 0;
247     delete fESDfriend; fESDfriend = 0;
248
249     delete fESDTree; fESDTree = 0;
250     delete fESDFile; fESDFile = 0;
251   }
252 }
253
254
255 /******************************************************************************/
256 // Static convenience functions, mainly used from macros.
257 /******************************************************************************/
258
259 AliRunLoader* AliEveEventManager::AssertRunLoader()
260 {
261   static const TEveException eH("AliEveEventManager::AssertRunLoader ");
262
263   if (gEvent == 0)
264     throw(eH + "ALICE event not ready.");
265   if (gEvent->fRunLoader == 0)
266     throw(eH + "AliRunLoader not initialised.");
267   return gEvent->fRunLoader;
268 }
269
270 AliESDEvent* AliEveEventManager::AssertESD()
271 {
272   static const TEveException eH("AliEveEventManager::AssertESD ");
273
274   if (gEvent == 0)
275     throw(eH + "ALICE event not ready.");
276   if (gEvent->fESD == 0)
277     throw(eH + "AliESD not initialised.");
278   return gEvent->fESD;
279 }
280
281 AliESDfriend* AliEveEventManager::AssertESDfriend()
282 {
283   static const TEveException eH("AliEveEventManager::AssertESDfriend ");
284
285   if (gEvent == 0)
286     throw(eH + "ALICE event not ready.");
287   if (gEvent->fESDfriend == 0)
288     throw(eH + "AliESDfriend not initialised.");
289   return gEvent->fESDfriend;
290 }
291
292 AliMagF* AliEveEventManager::AssertMagField()
293 {
294   if (fgMagField == 0)
295   {
296     if (gEvent && gEvent->fRunLoader && gEvent->fRunLoader->GetAliRun())
297       fgMagField = gEvent->fRunLoader->GetAliRun()->Field();
298     else
299       fgMagField = new AliMagFMaps("Maps","Maps", 1, 1., 10., AliMagFMaps::k5kG);
300   }
301   return fgMagField;
302 }
303
304 TGeoManager* AliEveEventManager::AssertGeometry()
305 {
306   static const TEveException eH("AliEveEventManager::AssertGeometry ");
307
308   if (AliGeomManager::GetGeometry() == 0)
309   {
310     gGeoManager = 0;
311     AliGeomManager::LoadGeometry();
312     if ( ! AliGeomManager::GetGeometry())
313     {
314       throw(eH + "can not load geometry.");
315     }
316     if ( ! AliGeomManager::ApplyAlignObjsFromCDB("ITS TPC TRD TOF PHOS HMPID EMCAL MUON FMD ZDC PMD T0 VZERO ACORDE"))
317     {
318       ::Warning(eH, "mismatch of alignable volumes. Proceeding.");
319       // throw(eH + "could not apply align objs.");
320     }
321   }
322
323   return AliGeomManager::GetGeometry();
324 }