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