The treatment of the Bz field component is different in our package and in ALICE...
[u/mrichter/AliRoot.git] / EVE / Alieve / EventAlieve.cxx
CommitLineData
5a5a1232 1// $Header$
2
3#include "EventAlieve.h"
4#include <Reve/Reve.h>
d9e0d6c5 5#include <Reve/RGTopFrame.h>
5a5a1232 6
7#include <AliRunLoader.h>
93845f6c 8#include <AliRun.h>
5a5a1232 9#include <AliESD.h>
3aecaefc 10#include <AliESDfriend.h>
93845f6c 11#include <AliMagFMaps.h>
632d2b03 12#include <AliCDBManager.h>
13#include <AliHeader.h>
14#include <AliGeomManager.h>
5a5a1232 15
16#include <TFile.h>
17#include <TTree.h>
90fa773e 18#include <TObjString.h>
632d2b03 19#include <TError.h>
5a5a1232 20
21#include <TROOT.h>
22#include <TSystem.h>
90fa773e 23#include <TCint.h>
5a5a1232 24
25using namespace Reve;
26using namespace Alieve;
27
28//______________________________________________________________________
29// Event
30//
4852ff6f 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//
5a5a1232 37
38ClassImp(Event)
39
40Event* Alieve::gEvent = 0;
41
753fdd1e 42Bool_t Alieve::Event::fgUseRunLoader = kTRUE;
43Bool_t Alieve::Event::fgUseESDTree = kTRUE;
44Bool_t Alieve::Event::fgAvoidExcOnOpen = kTRUE;
5a5a1232 45
632d2b03 46TString Alieve::Event::fgCdbUri("local://$ALICE_ROOT");
47
93845f6c 48AliMagF* Alieve::Event::fgMagField = 0;
49
50
753fdd1e 51void Event::Initialize(Bool_t use_runloader, Bool_t use_esd,
52 Bool_t avoid_exc_on_open)
5a5a1232 53{
54 static const Exc_t eH("Event::Initialize ");
55
753fdd1e 56 fgUseRunLoader = use_runloader;
57 fgUseESDTree = use_esd;
58 fgAvoidExcOnOpen = avoid_exc_on_open;
5a5a1232 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
6f0b0040 75Event::Event() :
265ecb21 76 EventBase(),
77
78 fPath (), fEventId (0),
79 fRunLoader (0),
90fa773e 80 fESDFile (0), fESDTree (0), fESD (0),
2aa55da0 81 fESDfriend (0), fESDfriendExists(kFALSE),
82 fNewEventCommands()
265ecb21 83{}
5a5a1232 84
6f0b0040 85Event::Event(TString path, Int_t ev) :
86 EventBase("AliEVE Event"),
265ecb21 87
90fa773e 88 fPath (path), fEventId(-1),
265ecb21 89 fRunLoader (0),
90fa773e 90 fESDFile (0), fESDTree (0), fESD (0),
2aa55da0 91 fESDfriend (0), fESDfriendExists(kFALSE),
92 fNewEventCommands()
5a5a1232 93{
5a5a1232 94 Open();
90fa773e 95 if (ev >= 0) GotoEvent(ev);
5a5a1232 96}
97
98/**************************************************************************/
99
100void 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
632d2b03 108 Int_t runNo = -1;
109
90fa773e 110 if(fgUseRunLoader)
111 {
5a5a1232 112 TString ga_path(Form("%s/galice.root", fPath.Data()));
90fa773e 113 if(gSystem->AccessPathName(ga_path, kReadPermission))
114 {
753fdd1e 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 }
5a5a1232 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 }
1eaa5849 129 if(fRunLoader->LoadgAlice() != 0)
5a5a1232 130 throw(eH + "failed loading gAlice.");
cf94c7c2 131 if(fRunLoader->LoadHeader() != 0)
132 throw(eH + "failed loading header.");
632d2b03 133 runNo = fRunLoader->GetHeader()->GetRun();
5a5a1232 134 }
753fdd1e 135end_run_loader:
5a5a1232 136
90fa773e 137 if(fgUseESDTree)
138 {
5a5a1232 139 TString p(Form("%s/AliESDs.root", fPath.Data()));
90fa773e 140 if(gSystem->AccessPathName(p, kReadPermission))
141 {
753fdd1e 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 }
5a5a1232 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
43ce2ae3 155 fESD = new AliESD();
5a5a1232 156 fESDTree = (TTree*) fESDFile->Get("esdTree");
157 if(fESDTree == 0)
158 throw(eH + "failed getting the esdTree.");
43ce2ae3 159 fESD->ReadFromTree(fESDTree);
632d2b03 160 runNo = fESD->GetESDRun()->GetRunNumber();
3aecaefc 161
938142f5 162 // Check if ESDfriends exists and attach the branch
3aecaefc 163 p = Form("%s/AliESDfriends.root", fPath.Data());
90fa773e 164 if(gSystem->AccessPathName(p, kReadPermission) == kFALSE)
165 {
2cea771a 166 fESDfriendExists = kTRUE;
167 fESDTree->SetBranchStatus ("ESDfriend*", 1);
168 fESDTree->SetBranchAddress("ESDfriend.", &fESDfriend);
3aecaefc 169 }
5a5a1232 170 }
171
632d2b03 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
90fa773e 183end_esd_loader:
1eaa5849 184 SetName(Form("Event %d", fEventId));
5a5a1232 185 SetTitle(fPath);
186}
187
1eaa5849 188void Event::GotoEvent(Int_t event)
189{
190 static const Exc_t eH("Event::GotoEvent ");
191
1eaa5849 192 Int_t maxEvent = 0;
753fdd1e 193 if(fRunLoader)
1eaa5849 194 maxEvent = fRunLoader->GetNumberOfEvents() - 1;
753fdd1e 195 else if(fESDTree)
1eaa5849 196 maxEvent = fESDTree->GetEntries() - 1;
753fdd1e 197 else
198 throw(eH + "neither RunLoader nor ESD loaded.");
1eaa5849 199
200 if(event < 0 || event > maxEvent)
201 throw(eH + Form("event %d not present, available range [%d, %d].",
202 event, 0, maxEvent));
203
d9e0d6c5 204 RGTopFrame::RedrawDisabler rd(gReve);
541e09b4 205 gReve->Redraw3D(kFALSE, kTRUE); // Enforce drop of all logicals.
d9e0d6c5 206
1eaa5849 207 DestroyElements();
208 fEventId = event;
209 SetName(Form("Event %d", fEventId));
210 UpdateItems();
211
753fdd1e 212 if(fRunLoader) {
1eaa5849 213 if(fRunLoader->GetEvent(fEventId) != 0)
214 throw(eH + "failed getting required event.");
215 }
216
753fdd1e 217 if(fESDTree) {
1eaa5849 218 if(fESDTree->GetEntry(fEventId) <= 0)
219 throw(eH + "failed getting required event from ESD.");
220
2cea771a 221 if (fESDfriendExists)
1eaa5849 222 fESD->SetESDfriend(fESDfriend);
1eaa5849 223 }
90fa773e 224
225 AfterNewEventLoaded();
1eaa5849 226}
227
5a5a1232 228void Event::Close()
229{
2cea771a 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 }
5a5a1232 237}
238
90fa773e 239/**************************************************************************/
240
241void 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
252void Event::AddNewEventCommand(const Text_t* cmd)
253{
254 fNewEventCommands.Add(new TObjString(cmd));
255}
256
5a5a1232 257/**************************************************************************/
258/**************************************************************************/
259
260// Static convenience functions.
261
262AliRunLoader* 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
273AliESD* 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}
3aecaefc 283
284AliESDfriend* 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}
93845f6c 294
295AliMagF* 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}
632d2b03 306
307TGeoManager* 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}