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