]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EVE/Alieve/AliEveEventManager.cxx
47e34f90947774adf4e04dd7c999830fb1024d06
[u/mrichter/AliRoot.git] / EVE / Alieve / 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::fgAssertESDTree   = 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
62 AliEveEventManager::AliEveEventManager(TString path, Int_t ev) :
63   TEveEventManager("AliEVE AliEveEventManager"),
64
65   fPath (path), fEventId(-1),
66   fRunLoader (0),
67   fESDFile   (0), fESDTree (0), fESD (0),
68   fESDfriend (0), fESDfriendExists(kFALSE)
69 {
70   Open();
71   if (ev >= 0) GotoEvent(ev);
72 }
73
74 /******************************************************************************/
75
76 void AliEveEventManager::Open()
77 {
78   static const TEveException eH("AliEveEventManager::Open ");
79
80   gSystem->ExpandPathName(fPath);
81   if (fPath[0] != '/')
82     fPath = Form("%s/%s", gSystem->WorkingDirectory(), fPath.Data());
83
84   Int_t runNo = -1;
85
86   TString ga_path(Form("%s/galice.root", fPath.Data()));
87   if (gSystem->AccessPathName(ga_path, kReadPermission) == kFALSE)
88   {
89     fRunLoader = AliRunLoader::Open(ga_path);
90     if (fRunLoader)
91     {
92       TString alice_path = fPath + "/";
93       fRunLoader->SetDirName(alice_path);
94
95       if (fRunLoader->LoadgAlice() != 0)
96         Warning(eH, "failed loading gAlice via run-loader.");
97
98       if (fRunLoader->LoadHeader() == 0)
99       {
100         runNo = fRunLoader->GetHeader()->GetRun();
101       }
102       else
103       {
104         Warning(eH, "failed loading run-loader's header.");
105         delete fRunLoader;
106         fRunLoader = 0;
107       }
108     }
109     else // run-loader open failed
110     {
111       Warning(eH, "failed opening ALICE run-loader from '%s'.", ga_path.Data());
112     }
113   }
114   else // galice not readable
115   {
116     Warning(eH, "can not read '%s'.", ga_path.Data());
117   }
118   if (fRunLoader == 0)
119   {
120     if (fgAssertRunLoader)
121       throw(eH + "Bootstraping of run-loader failed. Its precence was requested.");
122     else
123       Warning(eH, "Bootstraping of run-loader failed.");
124   }
125
126
127   TString esd_path(Form("%s/AliESDs.root", fPath.Data()));
128   if (gSystem->AccessPathName(esd_path, kReadPermission) == kFALSE)
129   {
130     fESDFile = new TFile(esd_path);
131     if (fESDFile->IsZombie() == kFALSE)
132     {
133       fESD = new AliESDEvent();
134       fESDTree = (TTree*) fESDFile->Get("esdTree");
135       if (fESDTree != 0)
136       {
137         fESD->ReadFromTree(fESDTree);
138         runNo = fESD->GetESDRun()->GetRunNumber();
139
140         // Check if ESDfriends exists and attach the branch
141         TString p = Form("%s/AliESDfriends.root", fPath.Data());
142         if (gSystem->AccessPathName(p, kReadPermission) == kFALSE)
143         {
144           fESDfriendExists = kTRUE;
145           fESDTree->SetBranchStatus ("ESDfriend*", 1);
146           fESDTree->SetBranchAddress("ESDfriend.", &fESDfriend);
147         }
148       }
149       else // esdtree == 0
150       {
151         delete fESDFile; fESDFile = 0;
152         Warning(eH, "failed getting the esdTree.");
153       }
154     }
155     else // esd tfile is zombie
156     {
157       delete fESDFile; fESDFile = 0;
158       Warning(eH, "failed opening ESD from '%s'.", esd_path.Data());
159     }
160   }
161   else // esd not readable
162   {
163     Warning(eH, "can not read ESD file '%s'.", esd_path.Data());
164   }
165   if (fESDTree == 0)
166   {
167     if (fgAssertESDTree)
168     {
169       throw(eH + "ESD not initialized. Its precence was requested.");
170     } else {
171       Warning(eH, "ESD not initialized.");
172     }
173   }
174
175   if (runNo < 0)
176     throw(eH + "invalid run number.");
177
178   {
179     AliCDBManager* cdb = AliCDBManager::Instance();
180     cdb->SetDefaultStorage(fgCdbUri);
181     if (cdb->IsDefaultStorageSet() == kFALSE)
182       throw(eH + "CDB initialization failed.");
183     cdb->SetRun(runNo);
184   }
185
186   SetName(Form("AliEveEventManager %d", fEventId));
187   SetTitle(fPath);
188 }
189
190 void AliEveEventManager::GotoEvent(Int_t event)
191 {
192   static const TEveException eH("AliEveEventManager::GotoEvent ");
193
194   Int_t maxEvent = 0;
195   if (fRunLoader)
196     maxEvent = fRunLoader->GetNumberOfEvents() - 1;
197   else if (fESDTree)
198     maxEvent = fESDTree->GetEntries() - 1;
199   else
200     throw(eH + "neither RunLoader nor ESD loaded.");
201
202   if (event < 0 || event > maxEvent)
203     throw(eH + Form("event %d not present, available range [%d, %d].",
204                     event, 0, maxEvent));
205
206   TEveManager::TRedrawDisabler rd(gEve);
207   gEve->Redraw3D(kFALSE, kTRUE); // Enforce drop of all logicals.
208
209   // !!! MT this is somewhat brutal; at least optionally, one could be
210   // a bit gentler, checking for objs owning their external refs and having
211   // additinal parents.
212   DestroyElements();
213   fEventId = event;
214   SetName(Form("AliEveEventManager %d", fEventId));
215   UpdateItems();
216
217   if (fRunLoader) {
218     if (fRunLoader->GetEvent(fEventId) != 0)
219       throw(eH + "failed getting required event.");
220   }
221
222   if (fESDTree) {
223     if (fESDTree->GetEntry(fEventId) <= 0)
224       throw(eH + "failed getting required event from ESD.");
225
226     if (fESDfriendExists)
227       fESD->SetESDfriend(fESDfriend);
228   }
229
230   AfterNewEventLoaded();
231 }
232
233 void AliEveEventManager::Close()
234 {
235   if (fESDTree) {
236     delete fESD;       fESD       = 0;
237     delete fESDfriend; fESDfriend = 0;
238
239     delete fESDTree; fESDTree = 0;
240     delete fESDFile; fESDFile = 0;
241   }
242 }
243
244
245 /******************************************************************************/
246 /******************************************************************************/
247
248 // Static convenience functions.
249
250 AliRunLoader* AliEveEventManager::AssertRunLoader()
251 {
252   static const TEveException eH("AliEveEventManager::AssertRunLoader ");
253
254   if (gEvent == 0)
255     throw(eH + "ALICE event not ready.");
256   if (gEvent->fRunLoader == 0)
257     throw(eH + "AliRunLoader not initialised.");
258   return gEvent->fRunLoader;
259 }
260
261 AliESDEvent* AliEveEventManager::AssertESD()
262 {
263   static const TEveException eH("AliEveEventManager::AssertESD ");
264
265   if (gEvent == 0)
266     throw(eH + "ALICE event not ready.");
267   if (gEvent->fESD == 0)
268     throw(eH + "AliESD not initialised.");
269   return gEvent->fESD;
270 }
271
272 AliESDfriend* AliEveEventManager::AssertESDfriend()
273 {
274   static const TEveException eH("AliEveEventManager::AssertESDfriend ");
275
276   if (gEvent == 0)
277     throw(eH + "ALICE event not ready.");
278   if (gEvent->fESDfriend == 0)
279     throw(eH + "AliESDfriend not initialised.");
280   return gEvent->fESDfriend;
281 }
282
283 AliMagF* AliEveEventManager::AssertMagField()
284 {
285   if (fgMagField == 0)
286   {
287     if (gEvent && gEvent->fRunLoader && gEvent->fRunLoader->GetAliRun())
288       fgMagField = gEvent->fRunLoader->GetAliRun()->Field();
289     else
290       fgMagField = new AliMagFMaps("Maps","Maps", 1, 1., 10., AliMagFMaps::k5kG);
291   }
292   return fgMagField;
293 }
294
295 TGeoManager* AliEveEventManager::AssertGeometry()
296 {
297   static const TEveException eH("AliEveEventManager::AssertGeometry ");
298
299   if (AliGeomManager::GetGeometry() == 0)
300   {
301     gGeoManager = 0;
302     AliGeomManager::LoadGeometry();
303     if ( ! AliGeomManager::GetGeometry())
304     {
305       throw(eH + "can not load geometry.");
306     }
307     if ( ! AliGeomManager::ApplyAlignObjsFromCDB("ITS TPC TRD TOF PHOS HMPID EMCAL MUON FMD ZDC PMD T0 VZERO ACORDE"))
308     {
309       ::Warning(eH, "mismatch of alignable volumes. Proceeding.");
310       // throw(eH + "could not apply align objs.");
311     }
312   }
313
314   return AliGeomManager::GetGeometry();
315 }