]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EVE/Alieve/EventAlieve.cxx
b071372b5278e8846bb10eea7fbebfb5de0b5bdf
[u/mrichter/AliRoot.git] / EVE / Alieve / EventAlieve.cxx
1 // $Header$
2
3 #include "EventAlieve.h"
4 #include <Reve/Reve.h>
5 #include <Reve/RGTopFrame.h>
6
7 #include <AliRunLoader.h>
8 #include <AliRun.h>
9 #include <AliESD.h>
10 #include <AliESDfriend.h>
11 #include <AliMagFMaps.h>
12 #include <AliCDBManager.h>
13 #include <AliHeader.h>
14 #include <AliGeomManager.h>
15
16 #include <TFile.h>
17 #include <TTree.h>
18 #include <TObjString.h>
19 #include <TError.h>
20
21 #include <TROOT.h>
22 #include <TSystem.h>
23 #include <TCint.h>
24
25 using namespace Reve;
26 using namespace Alieve;
27
28 //______________________________________________________________________
29 // Event
30 //
31 // Provide interface for loading and navigating standard AliRoot data
32 // (AliRunLoader) and ESDs.
33 //
34 // Missing support for raw-data. For now this is handled individually
35 // by each sub-detector.
36 // 
37
38 ClassImp(Event)
39
40 Event* Alieve::gEvent = 0;
41
42 Bool_t Alieve::Event::fgUseRunLoader   = kTRUE;
43 Bool_t Alieve::Event::fgUseESDTree     = kTRUE;
44 Bool_t Alieve::Event::fgAvoidExcOnOpen = kTRUE;
45
46 TString  Alieve::Event::fgCdbUri("local://$ALICE_ROOT");
47
48 AliMagF* Alieve::Event::fgMagField = 0;
49
50
51 void Event::Initialize(Bool_t use_runloader, Bool_t use_esd,
52                        Bool_t avoid_exc_on_open)
53 {
54   static const Exc_t eH("Event::Initialize ");
55
56   fgUseRunLoader   = use_runloader;
57   fgUseESDTree     = use_esd;
58   fgAvoidExcOnOpen = avoid_exc_on_open;
59
60   /*
61   if(fgUseRunLoader == false && fgUseESDTree == false)
62     throw(eH + "should use at least one data source.");
63
64   if(fgUseRunLoader) {
65     AssertMacro("loadlibs.C");
66   }
67   else if(fgUseESDTree) {
68     gSystem->Load("libESD.so");
69   }
70   */
71 }
72
73 /**************************************************************************/
74
75 Event::Event() :
76   EventBase(),
77
78   fPath (), fEventId   (0),
79   fRunLoader (0),
80   fESDFile   (0), fESDTree (0), fESD (0),
81   fESDfriend (0), fESDfriendExists(kFALSE),
82   fNewEventCommands()
83 {}
84
85 Event::Event(TString path, Int_t ev) :
86   EventBase("AliEVE Event"),
87
88   fPath (path), fEventId(-1),
89   fRunLoader (0),
90   fESDFile   (0), fESDTree (0), fESD (0),
91   fESDfriend (0), fESDfriendExists(kFALSE),
92   fNewEventCommands()
93 {
94   Open();
95   if (ev >= 0) GotoEvent(ev);
96 }
97
98 /**************************************************************************/
99
100 void Event::Open()
101 {
102   static const Exc_t eH("Event::Open ");
103
104   gSystem->ExpandPathName(fPath);
105   if(fPath[0] != '/')
106     fPath = Form("%s/%s", gSystem->WorkingDirectory(), fPath.Data());
107
108   Int_t runNo = -1;
109
110   if(fgUseRunLoader)
111   {
112     TString ga_path(Form("%s/galice.root", fPath.Data()));
113     if(gSystem->AccessPathName(ga_path, kReadPermission))
114     {
115       if (fgAvoidExcOnOpen) {
116         Warning(eH, "RunLoader not initialized.");
117         goto end_run_loader;
118       } else {
119         throw(eH + "can not read '" + ga_path + "'.");
120       }
121     }
122     fRunLoader = AliRunLoader::Open(ga_path);
123     if(!fRunLoader)
124       throw(eH + "failed opening ALICE run loader from '" + ga_path + "'.");
125     {
126       TString alice_path = fPath + "/";
127       fRunLoader->SetDirName(alice_path);
128     }
129     if(fRunLoader->LoadgAlice() != 0)
130       throw(eH + "failed loading gAlice.");
131     if(fRunLoader->LoadHeader() != 0)
132       throw(eH + "failed loading header.");
133     runNo = fRunLoader->GetHeader()->GetRun();
134   }
135 end_run_loader:
136
137   if(fgUseESDTree)
138   {
139     TString p(Form("%s/AliESDs.root", fPath.Data()));
140     if(gSystem->AccessPathName(p, kReadPermission))
141     {
142       if (fgAvoidExcOnOpen) {
143         Warning(eH, "ESD not initialized.");
144         goto end_esd_loader;
145       } else { 
146         throw(eH + "can not read '" + p + "'.");
147       }
148     }
149     fESDFile = new TFile(p);
150     if(fESDFile->IsZombie()) {
151       delete fESDFile; fESDFile = 0;
152       throw(eH + "failed opening ALICE ESD from '" + p + "'.");
153     }
154
155     fESD = new AliESD();
156     fESDTree = (TTree*) fESDFile->Get("esdTree");
157     if(fESDTree == 0)
158       throw(eH + "failed getting the esdTree.");
159     fESD->ReadFromTree(fESDTree);
160     runNo = fESD->GetESDRun()->GetRunNumber();
161
162     // Check if ESDfriends exists and attach the branch
163     p = Form("%s/AliESDfriends.root", fPath.Data());
164     if(gSystem->AccessPathName(p, kReadPermission) == kFALSE)
165     {
166       fESDfriendExists = kTRUE;
167       fESDTree->SetBranchStatus ("ESDfriend*", 1);
168       fESDTree->SetBranchAddress("ESDfriend.", &fESDfriend);
169     }
170   }
171
172   if (runNo < 0)
173     throw(eH + "invalid run number.");
174
175   {
176     AliCDBManager* cdb = AliCDBManager::Instance();
177     cdb->SetDefaultStorage(fgCdbUri);
178     if (cdb->IsDefaultStorageSet() == kFALSE)
179       throw(eH + "CDB initialization failed.");
180     cdb->SetRun(runNo);
181   }
182
183 end_esd_loader:
184   SetName(Form("Event %d", fEventId));
185   SetTitle(fPath);
186 }
187
188 void Event::GotoEvent(Int_t event)
189 {
190   static const Exc_t eH("Event::GotoEvent ");
191
192   Int_t maxEvent = 0;
193   if(fRunLoader)
194     maxEvent = fRunLoader->GetNumberOfEvents() - 1;
195   else if(fESDTree)
196     maxEvent = fESDTree->GetEntries() - 1;
197   else
198     throw(eH + "neither RunLoader nor ESD loaded.");
199
200   if(event < 0 || event > maxEvent)
201     throw(eH + Form("event %d not present, available range [%d, %d].",
202                     event, 0, maxEvent));
203
204   RGTopFrame::RedrawDisabler rd(gReve);
205   gReve->Redraw3D(kFALSE, kTRUE); // Enforce drop of all logicals.
206
207   DestroyElements();
208   fEventId = event;
209   SetName(Form("Event %d", fEventId));
210   UpdateItems();
211
212   if(fRunLoader) {
213     if(fRunLoader->GetEvent(fEventId) != 0)
214       throw(eH + "failed getting required event.");
215   }
216
217   if(fESDTree) {
218     if(fESDTree->GetEntry(fEventId) <= 0)
219       throw(eH + "failed getting required event from ESD.");
220
221     if (fESDfriendExists)
222       fESD->SetESDfriend(fESDfriend);
223   }
224
225   AfterNewEventLoaded();
226 }
227
228 void Event::Close()
229 {
230   if (fESDTree) {
231     delete fESD;       fESD       = 0;
232     delete fESDfriend; fESDfriend = 0;
233
234     delete fESDTree; fESDTree = 0;
235     delete fESDFile; fESDFile = 0;
236   }
237 }
238
239 /**************************************************************************/
240
241 void Event::AfterNewEventLoaded()
242 {
243   TIter next(&fNewEventCommands);
244   TObject* o;
245   while ((o = next())) {
246     TObjString* s = dynamic_cast<TObjString*>(o);
247     if (s)
248       gInterpreter->ProcessLine(s->String());
249   }
250 }
251
252 void Event::AddNewEventCommand(const Text_t* cmd)
253 {
254   fNewEventCommands.Add(new TObjString(cmd));
255 }
256
257 /**************************************************************************/
258 /**************************************************************************/
259
260 // Static convenience functions.
261
262 AliRunLoader* Event::AssertRunLoader()
263 {
264   static const Exc_t eH("Event::AssertRunLoader ");
265
266   if(gEvent == 0)
267     throw(eH + "ALICE event not ready.");
268   if(gEvent->fRunLoader == 0)
269     throw(eH + "AliRunLoader not initialised.");
270   return gEvent->fRunLoader;
271 }
272
273 AliESD* Event::AssertESD()
274 {
275   static const Exc_t eH("Event::AssertESD ");
276
277   if(gEvent == 0)
278     throw(eH + "ALICE event not ready.");
279   if(gEvent->fESD == 0)
280     throw(eH + "AliESD not initialised.");
281   return gEvent->fESD;
282 }
283
284 AliESDfriend* Event::AssertESDfriend()
285 {
286   static const Exc_t eH("Event::AssertESDfriend ");
287
288   if(gEvent == 0)
289     throw(eH + "ALICE event not ready.");
290   if(gEvent->fESDfriend == 0)
291     throw(eH + "AliESDfriend not initialised.");
292   return gEvent->fESDfriend;
293 }
294
295 AliMagF* Event::AssertMagField()
296 {
297   if (fgMagField == 0)
298   {
299     if (gEvent && gEvent->fRunLoader && gEvent->fRunLoader->GetAliRun())
300       fgMagField = gEvent->fRunLoader->GetAliRun()->Field();
301     else
302       fgMagField = new AliMagFMaps("Maps","Maps", 1, 1., 10., AliMagFMaps::k5kG);
303   }
304   return fgMagField;
305 }
306
307 TGeoManager* Event::AssertGeometry()
308 {
309   static const Exc_t eH("Event::AssertGeometry ");
310
311   if (AliGeomManager::GetGeometry() == 0)
312   {
313     AliGeomManager::LoadGeometry();
314     if ( ! AliGeomManager::GetGeometry())
315     {
316       throw(eH + "can not load geometry.");
317     }
318     if ( ! AliGeomManager::ApplyAlignObjsFromCDB("ITS TPC TRD TOF PHOS HMPID EMCAL MUON FMD ZDC PMD T0 VZERO ACORDE"))
319     {
320       ::Warning(eH, "mismatch of alignable volumes. Proceeding.");
321       // throw(eH + "could not apply align objs.");
322     }
323   }
324
325   return AliGeomManager::GetGeometry();
326 }