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