AliEveEventManager::AssertGeometry() - consolidate with changes in
[u/mrichter/AliRoot.git] / EVE / EveBase / AliEveEventManager.cxx
CommitLineData
d810d0de 1// $Id$
2// Main authors: Matevz Tadel & Alja Mrak-Tadel: 2006, 2007
5a5a1232 3
d810d0de 4/**************************************************************************
5 * Copyright(c) 1998-2008, ALICE Experiment at CERN, all rights reserved. *
6 * See http://aliceinfo.cern.ch/Offline/AliRoot/License.html for *
51346b82 7 * full copyright notice. *
d810d0de 8 **************************************************************************/
9
10#include "AliEveEventManager.h"
84aff7a4 11#include <TEveManager.h>
5a5a1232 12
13#include <AliRunLoader.h>
93845f6c 14#include <AliRun.h>
af885e0f 15#include <AliESDEvent.h>
3aecaefc 16#include <AliESDfriend.h>
c2c4b7a2 17#include <AliRawReaderRoot.h>
18#include <AliRawReaderFile.h>
19#include <AliRawReaderDate.h>
93845f6c 20#include <AliMagFMaps.h>
632d2b03 21#include <AliCDBManager.h>
22#include <AliHeader.h>
23#include <AliGeomManager.h>
5a5a1232 24
25#include <TFile.h>
26#include <TTree.h>
5a5a1232 27#include <TSystem.h>
d810d0de 28
a15e6d7d 29//==============================================================================
30//==============================================================================
31// AliEveEventManager
32//==============================================================================
5a5a1232 33
57ffa5fb 34//______________________________________________________________________________
5a5a1232 35//
4852ff6f 36// Provide interface for loading and navigating standard AliRoot data
37// (AliRunLoader) and ESDs.
38//
39// Missing support for raw-data. For now this is handled individually
40// by each sub-detector.
51346b82 41//
a15e6d7d 42// Also provides interface to magnetic-field and geometry. Mostly
43// intended as wrappers over standard AliRoot functionality for
44// convenient use from visualizateion macros.
5a5a1232 45
d810d0de 46ClassImp(AliEveEventManager)
5a5a1232 47
a15e6d7d 48AliEveEventManager* gAliEveEvent = 0;
5a5a1232 49
d810d0de 50Bool_t AliEveEventManager::fgAssertRunLoader = kFALSE;
c76ea574 51Bool_t AliEveEventManager::fgAssertESD = kFALSE;
c2c4b7a2 52Bool_t AliEveEventManager::fgAssertRaw = kFALSE;
5a5a1232 53
c2c4b7a2 54TString AliEveEventManager::fgESDFileName("AliESDs.root");
55TString AliEveEventManager::fgRawFileName("raw.root");
d810d0de 56TString AliEveEventManager::fgCdbUri("local://$ALICE_ROOT");
632d2b03 57
d810d0de 58AliMagF* AliEveEventManager::fgMagField = 0;
93845f6c 59
60
d810d0de 61AliEveEventManager::AliEveEventManager() :
84aff7a4 62 TEveEventManager(),
265ecb21 63
c2c4b7a2 64 fPath ( ), fEventId (-1),
265ecb21 65 fRunLoader (0),
90fa773e 66 fESDFile (0), fESDTree (0), fESD (0),
c2c4b7a2 67 fESDfriend (0), fESDfriendExists(kFALSE),
68 fRawReader (0)
c76ea574 69{
70 // Default constructor.
71}
5a5a1232 72
d810d0de 73AliEveEventManager::AliEveEventManager(TString path, Int_t ev) :
74 TEveEventManager("AliEVE AliEveEventManager"),
265ecb21 75
c76ea574 76 fPath (path), fEventId(-1),
265ecb21 77 fRunLoader (0),
90fa773e 78 fESDFile (0), fESDTree (0), fESD (0),
c2c4b7a2 79 fESDfriend (0), fESDfriendExists(kFALSE),
80 fRawReader (0)
5a5a1232 81{
c76ea574 82 // Constructor with event-directory URL and event-id.
83
5a5a1232 84 Open();
90fa773e 85 if (ev >= 0) GotoEvent(ev);
5a5a1232 86}
87
a15e6d7d 88AliEveEventManager::~AliEveEventManager()
89{
90 // Destructor.
91
92 // Somewhat unclear what to do here.
93 // In principle should close all data sources and deregister from
94 // TEveManager.
95}
96
57ffa5fb 97/******************************************************************************/
5a5a1232 98
c2c4b7a2 99void AliEveEventManager::SetESDFileName(const Text_t* esd)
100{
101 // Set file-name for opening ESD, default "AliESDs.root".
102
103 if (esd) fgESDFileName = esd;
104}
105
106void AliEveEventManager::SetRawFileName(const Text_t* raw)
107{
108 // Set file-name for opening of raw-data, default "raw.root"
109 if (raw) fgRawFileName = raw;
110}
111
112void AliEveEventManager::SetCdbUri(const Text_t* cdb)
113{
114 // Set path to CDB, default "local://$ALICE_ROOT".
115
116 if (cdb) fgCdbUri = cdb;
117}
118
119void AliEveEventManager::SetAssertElements(Bool_t assertRunloader,
120 Bool_t assertEsd,
121 Bool_t assertRaw)
122{
123 // Set global flags that detrmine which parts of the event-data must
124 // be present when the event is opened.
125
126 fgAssertRunLoader = assertRunloader;
127 fgAssertESD = assertEsd;
128 fgAssertRaw = assertRaw;
129}
130
131/******************************************************************************/
132
d810d0de 133void AliEveEventManager::Open()
5a5a1232 134{
c76ea574 135 // Open event-data from URL specified in fPath.
136 // Attempts to create AliRunLoader() and to open ESD with ESDfriends.
137 // Warning is reported if run-loader or ESD is not found.
138 // Global data-members fgAssertRunLoader and fgAssertESD can be set
139 // to throw exceptions instead.
140
a15e6d7d 141 static const TEveException kEH("AliEveEventManager::Open ");
5a5a1232 142
143 gSystem->ExpandPathName(fPath);
84aff7a4 144 if (fPath[0] != '/')
5a5a1232 145 fPath = Form("%s/%s", gSystem->WorkingDirectory(), fPath.Data());
146
632d2b03 147 Int_t runNo = -1;
148
a15e6d7d 149 TString gaPath(Form("%s/galice.root", fPath.Data()));
c2c4b7a2 150 // If i use open directly, we get fatal.
151 // Is this (AccessPathName check) ok for xrootd / alien?
a15e6d7d 152 if (gSystem->AccessPathName(gaPath, kReadPermission) == kFALSE)
90fa773e 153 {
a15e6d7d 154 fRunLoader = AliRunLoader::Open(gaPath);
a1896a82 155 if (fRunLoader)
5a5a1232 156 {
a15e6d7d 157 TString alicePath = fPath + "/";
158 fRunLoader->SetDirName(alicePath);
a1896a82 159
160 if (fRunLoader->LoadgAlice() != 0)
c2c4b7a2 161 Warning(kEH, "failed loading gAlice via run-loader.");
a1896a82 162
163 if (fRunLoader->LoadHeader() == 0)
164 {
c2c4b7a2 165 runNo = fRunLoader->GetHeader()->GetRun();
a1896a82 166 }
167 else
168 {
c2c4b7a2 169 Warning(kEH, "failed loading run-loader's header.");
170 delete fRunLoader;
171 fRunLoader = 0;
a1896a82 172 }
173 }
174 else // run-loader open failed
175 {
a15e6d7d 176 Warning(kEH, "failed opening ALICE run-loader from '%s'.", gaPath.Data());
5a5a1232 177 }
5a5a1232 178 }
a1896a82 179 else // galice not readable
180 {
a15e6d7d 181 Warning(kEH, "can not read '%s'.", gaPath.Data());
a1896a82 182 }
183 if (fRunLoader == 0)
184 {
84aff7a4 185 if (fgAssertRunLoader)
c2c4b7a2 186 throw (kEH + "Bootstraping of run-loader failed. Its precence was requested.");
a1896a82 187 else
a15e6d7d 188 Warning(kEH, "Bootstraping of run-loader failed.");
a1896a82 189 }
51346b82 190
5a5a1232 191
c2c4b7a2 192 TString esdPath(Form("%s/%s", fPath.Data(), fgESDFileName.Data()));
193 if ((fESDFile = TFile::Open(esdPath)))
90fa773e 194 {
c2c4b7a2 195 fESD = new AliESDEvent();
196 fESDTree = (TTree*) fESDFile->Get("esdTree");
197 if (fESDTree != 0)
90fa773e 198 {
c2c4b7a2 199 fESD->ReadFromTree(fESDTree);
200 runNo = fESD->GetESDRun()->GetRunNumber();
201
202 // Check if ESDfriends exists and attach the branch
203 TString p = Form("%s/AliESDfriends.root", fPath.Data());
204 if (gSystem->AccessPathName(p, kReadPermission) == kFALSE)
a1896a82 205 {
c2c4b7a2 206 fESDfriendExists = kTRUE;
207 fESDTree->SetBranchStatus ("ESDfriend*", 1);
208 fESDTree->SetBranchAddress("ESDfriend.", &fESDfriend);
753fdd1e 209 }
210 }
c2c4b7a2 211 else // esdtree == 0
a1896a82 212 {
5a5a1232 213 delete fESDFile; fESDFile = 0;
c2c4b7a2 214 Warning(kEH, "failed getting the esdTree.");
5a5a1232 215 }
a1896a82 216 }
217 else // esd not readable
218 {
a15e6d7d 219 Warning(kEH, "can not read ESD file '%s'.", esdPath.Data());
a1896a82 220 }
221 if (fESDTree == 0)
222 {
c76ea574 223 if (fgAssertESD)
90fa773e 224 {
c2c4b7a2 225 throw (kEH + "ESD not initialized. Its precence was requested.");
51346b82 226 } else {
a15e6d7d 227 Warning(kEH, "ESD not initialized.");
3aecaefc 228 }
5a5a1232 229 }
230
c2c4b7a2 231 TString rawPath(Form("%s/%s", fPath.Data(), fgRawFileName.Data()));
232 // If i use open directly, raw-reader reports an error but i have
233 // no way to detect it.
234 // Is this (AccessPathName check) ok for xrootd / alien?
235 if (gSystem->AccessPathName(rawPath, kReadPermission) == kFALSE)
236 {
237 if (fgRawFileName.EndsWith("/"))
238 {
239 fRawReader = new AliRawReaderFile(rawPath);
240 }
241 else if (fgRawFileName.EndsWith(".root"))
242 {
243 fRawReader = new AliRawReaderRoot(rawPath);
244 }
245 else if (!fgRawFileName.IsNull())
246 {
247 fRawReader = new AliRawReaderDate(rawPath);
248 }
249 }
250
251 if (fRawReader == 0)
252 {
253 if (fgAssertRaw)
254 {
255 throw (kEH + "raw-data not initialized. Its precence was requested.");
256 } else {
257 Warning(kEH, "raw-data not initialized.");
258 }
259 }
260
632d2b03 261 if (runNo < 0)
c2c4b7a2 262 {
263 if (fRawReader)
264 {
265 fRawReader->NextEvent();
266 runNo = fRawReader->GetRunNumber();
267 printf("Determining run-no from raw ... run=%d\n", runNo);
268 fRawReader->RewindEvents();
269 } else {
270 throw (kEH + "unknown run number.");
271 }
272 }
632d2b03 273
274 {
275 AliCDBManager* cdb = AliCDBManager::Instance();
276 cdb->SetDefaultStorage(fgCdbUri);
277 if (cdb->IsDefaultStorageSet() == kFALSE)
c2c4b7a2 278 throw (kEH + "CDB initialization failed.");
632d2b03 279 cdb->SetRun(runNo);
280 }
281
73c1c0ec 282 SetName(Form("Event %d", fEventId));
5a5a1232 283 SetTitle(fPath);
284}
285
d810d0de 286void AliEveEventManager::GotoEvent(Int_t event)
1eaa5849 287{
73c1c0ec 288 // Load data for specified event.
289 // If event is out of range an exception is thrown and old state
290 // is preserved.
291 // After successful loading of event, the virtual function
292 // AfterNewEventLoaded() is called. This executes commands that
293 // were registered via TEveEventManager::AddNewEventCommand().
294
a15e6d7d 295 static const TEveException kEH("AliEveEventManager::GotoEvent ");
1eaa5849 296
c2c4b7a2 297 if (event < 0) {
298 Error(kEH, "event must be non-negative.");
299 return;
300 }
301
1eaa5849 302 Int_t maxEvent = 0;
c2c4b7a2 303 if (fRunLoader) {
1eaa5849 304 maxEvent = fRunLoader->GetNumberOfEvents() - 1;
c2c4b7a2 305 } else if (fESDTree) {
1eaa5849 306 maxEvent = fESDTree->GetEntries() - 1;
c2c4b7a2 307 } else if (fRawReader) {
308 maxEvent = 10000000;
309 Info(kEH, "number of events unknown for raw-data, setting max-event id to 10M.");
310 } else {
311 throw (kEH + "neither RunLoader, ESD nor Raw loaded.");
312 }
84aff7a4 313 if (event < 0 || event > maxEvent)
c2c4b7a2 314 throw (kEH + Form("event %d not present, available range [%d, %d].",
315 event, 0, maxEvent));
1eaa5849 316
84aff7a4 317 TEveManager::TRedrawDisabler rd(gEve);
318 gEve->Redraw3D(kFALSE, kTRUE); // Enforce drop of all logicals.
d9e0d6c5 319
32e219c2 320 // !!! MT this is somewhat brutal; at least optionally, one could be
321 // a bit gentler, checking for objs owning their external refs and having
322 // additinal parents.
1eaa5849 323 DestroyElements();
1eaa5849 324
84aff7a4 325 if (fRunLoader) {
c2c4b7a2 326 if (fRunLoader->GetEvent(event) != 0)
327 throw (kEH + "failed getting required event.");
1eaa5849 328 }
329
84aff7a4 330 if (fESDTree) {
c2c4b7a2 331 if (fESDTree->GetEntry(event) <= 0)
332 throw (kEH + "failed getting required event from ESD.");
1eaa5849 333
2cea771a 334 if (fESDfriendExists)
1eaa5849 335 fESD->SetESDfriend(fESDfriend);
1eaa5849 336 }
90fa773e 337
c2c4b7a2 338 if (fRawReader)
339 {
340 Int_t rawEv = fEventId;
341 if (event < rawEv)
342 {
343 fRawReader->RewindEvents();
344 rawEv = -1;
345 }
346
347 while (rawEv < event)
348 {
349 if ( ! fRawReader->NextEvent())
350 {
351 fRawReader->RewindEvents();
352 fEventId = -1;
353 throw (kEH + Form("Error going to next raw-event from event %d.", rawEv));
354 }
355 ++rawEv;
356 }
357
358 printf ("Loaded raw-event %d.\n", rawEv);
359 }
360
361 fEventId = event;
362 SetName(Form("Event %d", fEventId));
363 UpdateItems();
364
90fa773e 365 AfterNewEventLoaded();
1eaa5849 366}
367
d810d0de 368void AliEveEventManager::Close()
5a5a1232 369{
73c1c0ec 370 // Close the event files.
371 // For the moment only ESD is closed. Needs to be investigated for
372 // AliRunLoader and Raw.
373
2cea771a 374 if (fESDTree) {
375 delete fESD; fESD = 0;
376 delete fESDfriend; fESDfriend = 0;
377
378 delete fESDTree; fESDTree = 0;
379 delete fESDFile; fESDFile = 0;
380 }
5a5a1232 381}
382
90fa773e 383
57ffa5fb 384/******************************************************************************/
c76ea574 385// Static convenience functions, mainly used from macros.
57ffa5fb 386/******************************************************************************/
5a5a1232 387
d810d0de 388AliRunLoader* AliEveEventManager::AssertRunLoader()
5a5a1232 389{
73c1c0ec 390 // Make sure AliRunLoader is initialized and return it.
391 // Throws exception in case run-loader is not available.
392 // Static utility for macros.
393
a15e6d7d 394 static const TEveException kEH("AliEveEventManager::AssertRunLoader ");
5a5a1232 395
a15e6d7d 396 if (gAliEveEvent == 0)
c2c4b7a2 397 throw (kEH + "ALICE event not ready.");
a15e6d7d 398 if (gAliEveEvent->fRunLoader == 0)
c2c4b7a2 399 throw (kEH + "AliRunLoader not initialised.");
a15e6d7d 400 return gAliEveEvent->fRunLoader;
5a5a1232 401}
402
d810d0de 403AliESDEvent* AliEveEventManager::AssertESD()
5a5a1232 404{
73c1c0ec 405 // Make sure AliESDEvent is initialized and return it.
406 // Throws exception in case ESD is not available.
407 // Static utility for macros.
408
a15e6d7d 409 static const TEveException kEH("AliEveEventManager::AssertESD ");
5a5a1232 410
a15e6d7d 411 if (gAliEveEvent == 0)
c2c4b7a2 412 throw (kEH + "ALICE event not ready.");
a15e6d7d 413 if (gAliEveEvent->fESD == 0)
c2c4b7a2 414 throw (kEH + "AliESD not initialised.");
a15e6d7d 415 return gAliEveEvent->fESD;
5a5a1232 416}
3aecaefc 417
d810d0de 418AliESDfriend* AliEveEventManager::AssertESDfriend()
3aecaefc 419{
73c1c0ec 420 // Make sure AliESDfriend is initialized and return it.
421 // Throws exception in case ESDfriend-loader is not available.
422 // Static utility for macros.
423
a15e6d7d 424 static const TEveException kEH("AliEveEventManager::AssertESDfriend ");
3aecaefc 425
a15e6d7d 426 if (gAliEveEvent == 0)
c2c4b7a2 427 throw (kEH + "ALICE event not ready.");
a15e6d7d 428 if (gAliEveEvent->fESDfriend == 0)
c2c4b7a2 429 throw (kEH + "AliESDfriend not initialised.");
a15e6d7d 430 return gAliEveEvent->fESDfriend;
3aecaefc 431}
93845f6c 432
c2c4b7a2 433AliRawReader* AliEveEventManager::AssertRawReader()
434{
435 // Make sure raw-reader is initialized and return it.
436
437 static const TEveException kEH("AliEveEventManager::AssertRawReader ");
438
439 if (gAliEveEvent == 0)
440 throw (kEH + "ALICE event not ready.");
441 if (gAliEveEvent->fRawReader == 0)
442 throw (kEH + "RawReader not ready.");
443
444 return gAliEveEvent->fRawReader;
445}
446
d810d0de 447AliMagF* AliEveEventManager::AssertMagField()
93845f6c 448{
73c1c0ec 449 // Make sure AliMagF is initialized and return it.
450 // Throws exception in case magnetic field is not available.
451 // Static utility for macros.
452
93845f6c 453 if (fgMagField == 0)
454 {
a15e6d7d 455 if (gAliEveEvent && gAliEveEvent->fRunLoader && gAliEveEvent->fRunLoader->GetAliRun())
456 fgMagField = gAliEveEvent->fRunLoader->GetAliRun()->Field();
93845f6c 457 else
458 fgMagField = new AliMagFMaps("Maps","Maps", 1, 1., 10., AliMagFMaps::k5kG);
459 }
460 return fgMagField;
461}
632d2b03 462
d810d0de 463TGeoManager* AliEveEventManager::AssertGeometry()
632d2b03 464{
73c1c0ec 465 // Make sure AliGeomManager is initialized and returns the
466 // corresponding TGeoManger.
647814a2 467 // gGeoManager is set to the return value.
468 // Throws exception if geometry can not be loaded or if it is not
469 // available and the TGeoManager is locked.
73c1c0ec 470 // Static utility for macros.
471
a15e6d7d 472 static const TEveException kEH("AliEveEventManager::AssertGeometry ");
632d2b03 473
474 if (AliGeomManager::GetGeometry() == 0)
475 {
647814a2 476 if (TGeoManager::IsLocked())
477 throw (kEH + "geometry is not loaded but TGeoManager is locked.");
478
af2e4ef5 479 gGeoManager = 0;
632d2b03 480 AliGeomManager::LoadGeometry();
481 if ( ! AliGeomManager::GetGeometry())
482 {
c2c4b7a2 483 throw (kEH + "can not load geometry.");
632d2b03 484 }
485 if ( ! AliGeomManager::ApplyAlignObjsFromCDB("ITS TPC TRD TOF PHOS HMPID EMCAL MUON FMD ZDC PMD T0 VZERO ACORDE"))
486 {
a15e6d7d 487 ::Warning(kEH, "mismatch of alignable volumes. Proceeding.");
c2c4b7a2 488 // throw (kEH + "could not apply align objs.");
632d2b03 489 }
c2c4b7a2 490
647814a2 491 // @@NEWROOT@@ Temporary fix.
c2c4b7a2 492 // In AliEve several simplified geometries can be loaded at a later stage.
647814a2 493 // Locking/unlocking is now handled properly in
494 // TEveManager::GetGeometry() but we're waiting for next root
495 // version due on 14.5.2008.
c2c4b7a2 496 TGeoManager::UnlockGeometry();
632d2b03 497 }
498
647814a2 499 gGeoManager = AliGeomManager::GetGeometry();
500 return gGeoManager;
632d2b03 501}