2 // Main authors: Matevz Tadel & Alja Mrak-Tadel: 2006, 2007
4 /**************************************************************************
5 * Copyright(c) 1998-2008, ALICE Experiment at CERN, all rights reserved. *
6 * See http://aliceinfo.cern.ch/Offline/AliRoot/License.html for *
7 * full copyright notice. *
8 **************************************************************************/
10 #include "AliEveEventManager.h"
11 #include <TEveManager.h>
13 #include <AliRunLoader.h>
15 #include <AliESDEvent.h>
16 #include <AliESDfriend.h>
17 #include <AliRawReaderRoot.h>
18 #include <AliRawReaderFile.h>
19 #include <AliRawReaderDate.h>
20 #include <AliMagFMaps.h>
21 #include <AliCDBManager.h>
22 #include <AliHeader.h>
23 #include <AliGeomManager.h>
27 #include <TGeoManager.h>
30 //==============================================================================
31 //==============================================================================
33 //==============================================================================
35 //______________________________________________________________________________
37 // Provide interface for loading and navigating standard AliRoot data
38 // (AliRunLoader) and ESDs.
40 // Missing support for raw-data. For now this is handled individually
41 // by each sub-detector.
43 // Also provides interface to magnetic-field and geometry. Mostly
44 // intended as wrappers over standard AliRoot functionality for
45 // convenient use from visualizateion macros.
47 ClassImp(AliEveEventManager)
49 AliEveEventManager* gAliEveEvent = 0;
51 Bool_t AliEveEventManager::fgAssertRunLoader = kFALSE;
52 Bool_t AliEveEventManager::fgAssertESD = kFALSE;
53 Bool_t AliEveEventManager::fgAssertRaw = kFALSE;
55 TString AliEveEventManager::fgESDFileName("AliESDs.root");
56 TString AliEveEventManager::fgRawFileName("raw.root");
57 TString AliEveEventManager::fgCdbUri("local://$ALICE_ROOT");
59 AliMagF* AliEveEventManager::fgMagField = 0;
62 AliEveEventManager::AliEveEventManager() :
65 fPath ( ), fEventId (-1),
67 fESDFile (0), fESDTree (0), fESD (0),
68 fESDfriend (0), fESDfriendExists(kFALSE),
75 // Default constructor.
78 AliEveEventManager::AliEveEventManager(TString path, Int_t ev) :
79 TEveEventManager("AliEVE AliEveEventManager"),
81 fPath (path), fEventId(-1),
83 fESDFile (0), fESDTree (0), fESD (0),
84 fESDfriend (0), fESDfriendExists(kFALSE),
91 // Constructor with event-directory URL and event-id.
94 if (ev >= 0) GotoEvent(ev);
97 AliEveEventManager::~AliEveEventManager()
101 if (fAutoLoadTimer) delete fAutoLoadTimer;
102 // Somewhat unclear what to do here.
103 // In principle should close all data sources and deregister from
107 /******************************************************************************/
109 void AliEveEventManager::SetESDFileName(const Text_t* esd)
111 // Set file-name for opening ESD, default "AliESDs.root".
113 if (esd) fgESDFileName = esd;
116 void AliEveEventManager::SetRawFileName(const Text_t* raw)
118 // Set file-name for opening of raw-data, default "raw.root"
119 if (raw) fgRawFileName = raw;
122 void AliEveEventManager::SetCdbUri(const Text_t* cdb)
124 // Set path to CDB, default "local://$ALICE_ROOT".
126 if (cdb) fgCdbUri = cdb;
129 void AliEveEventManager::SetAssertElements(Bool_t assertRunloader,
133 // Set global flags that detrmine which parts of the event-data must
134 // be present when the event is opened.
136 fgAssertRunLoader = assertRunloader;
137 fgAssertESD = assertEsd;
138 fgAssertRaw = assertRaw;
141 /******************************************************************************/
143 void AliEveEventManager::Open()
145 // Open event-data from URL specified in fPath.
146 // Attempts to create AliRunLoader() and to open ESD with ESDfriends.
147 // Warning is reported if run-loader or ESD is not found.
148 // Global data-members fgAssertRunLoader and fgAssertESD can be set
149 // to throw exceptions instead.
151 static const TEveException kEH("AliEveEventManager::Open ");
153 gSystem->ExpandPathName(fPath);
155 fPath = Form("%s/%s", gSystem->WorkingDirectory(), fPath.Data());
159 TString gaPath(Form("%s/galice.root", fPath.Data()));
160 // If i use open directly, we get fatal.
161 // Is this (AccessPathName check) ok for xrootd / alien?
162 if (gSystem->AccessPathName(gaPath, kReadPermission) == kFALSE)
164 fRunLoader = AliRunLoader::Open(gaPath);
167 TString alicePath = fPath + "/";
168 fRunLoader->SetDirName(alicePath);
170 if (fRunLoader->LoadgAlice() != 0)
171 Warning(kEH, "failed loading gAlice via run-loader.");
173 if (fRunLoader->LoadHeader() == 0)
175 runNo = fRunLoader->GetHeader()->GetRun();
179 Warning(kEH, "failed loading run-loader's header.");
184 else // run-loader open failed
186 Warning(kEH, "failed opening ALICE run-loader from '%s'.", gaPath.Data());
189 else // galice not readable
191 Warning(kEH, "can not read '%s'.", gaPath.Data());
195 if (fgAssertRunLoader)
196 throw (kEH + "Bootstraping of run-loader failed. Its precence was requested.");
198 Warning(kEH, "Bootstraping of run-loader failed.");
202 TString esdPath(Form("%s/%s", fPath.Data(), fgESDFileName.Data()));
203 if ((fESDFile = TFile::Open(esdPath)))
205 fESD = new AliESDEvent();
206 fESDTree = (TTree*) fESDFile->Get("esdTree");
209 fESD->ReadFromTree(fESDTree);
210 fESDTree->GetEntry(0);
211 runNo = fESD->GetESDRun()->GetRunNumber();
213 // Check if ESDfriends exists and attach the branch
214 TString p = Form("%s/AliESDfriends.root", fPath.Data());
215 if (gSystem->AccessPathName(p, kReadPermission) == kFALSE)
217 fESDfriendExists = kTRUE;
218 fESDTree->SetBranchStatus ("ESDfriend*", 1);
219 fESDTree->SetBranchAddress("ESDfriend.", &fESDfriend);
224 delete fESDFile; fESDFile = 0;
225 Warning(kEH, "failed getting the esdTree.");
228 else // esd not readable
230 Warning(kEH, "can not read ESD file '%s'.", esdPath.Data());
236 throw (kEH + "ESD not initialized. Its precence was requested.");
238 Warning(kEH, "ESD not initialized.");
242 TString rawPath(Form("%s/%s", fPath.Data(), fgRawFileName.Data()));
243 // If i use open directly, raw-reader reports an error but i have
244 // no way to detect it.
245 // Is this (AccessPathName check) ok for xrootd / alien?
246 if (gSystem->AccessPathName(rawPath, kReadPermission) == kFALSE)
248 if (fgRawFileName.EndsWith("/"))
250 fRawReader = new AliRawReaderFile(rawPath);
252 else if (fgRawFileName.EndsWith(".root"))
254 fRawReader = new AliRawReaderRoot(rawPath);
256 else if (!fgRawFileName.IsNull())
258 fRawReader = new AliRawReaderDate(rawPath);
266 throw (kEH + "raw-data not initialized. Its precence was requested.");
268 Warning(kEH, "raw-data not initialized.");
276 fRawReader->NextEvent();
277 runNo = fRawReader->GetRunNumber();
278 printf("Determining run-no from raw ... run=%d\n", runNo);
279 fRawReader->RewindEvents();
281 throw (kEH + "unknown run number.");
286 AliCDBManager* cdb = AliCDBManager::Instance();
287 cdb->SetDefaultStorage(fgCdbUri);
288 if (cdb->IsDefaultStorageSet() == kFALSE)
289 throw (kEH + "CDB initialization failed.");
293 SetName(Form("Event %d", fEventId));
297 void AliEveEventManager::SetEvent(AliRunLoader *runLoader, AliRawReader *rawReader, AliESDEvent *esd)
299 // Set an event from an external source
300 // The method is used in the online visualisation
301 fRunLoader = runLoader;
302 fRawReader = rawReader;
305 SetTitle("Online event in memory");
306 SetName("Online Event");
309 AfterNewEventLoaded();
312 void AliEveEventManager::GotoEvent(Int_t event)
314 // Load data for specified event.
315 // If event is out of range an exception is thrown and old state
317 // After successful loading of event, the virtual function
318 // AfterNewEventLoaded() is called. This executes commands that
319 // were registered via TEveEventManager::AddNewEventCommand().
321 static const TEveException kEH("AliEveEventManager::GotoEvent ");
324 Error(kEH, "event must be non-negative.");
330 maxEvent = fRunLoader->GetNumberOfEvents() - 1;
331 } else if (fESDTree) {
332 maxEvent = fESDTree->GetEntries() - 1;
333 } else if (fRawReader) {
335 Info(kEH, "number of events unknown for raw-data, setting max-event id to 10M.");
337 throw (kEH + "neither RunLoader, ESD nor Raw loaded.");
339 if (event < 0 || event > maxEvent)
340 throw (kEH + Form("event %d not present, available range [%d, %d].",
341 event, 0, maxEvent));
343 TEveManager::TRedrawDisabler rd(gEve);
344 gEve->Redraw3D(kFALSE, kTRUE); // Enforce drop of all logicals.
346 // !!! MT this is somewhat brutal; at least optionally, one could be
347 // a bit gentler, checking for objs owning their external refs and having
348 // additinal parents.
352 if (fRunLoader->GetEvent(event) != 0)
353 throw (kEH + "failed getting required event.");
357 if (fESDTree->GetEntry(event) <= 0)
358 throw (kEH + "failed getting required event from ESD.");
360 if (fESDfriendExists)
361 fESD->SetESDfriend(fESDfriend);
366 Int_t rawEv = fEventId;
369 fRawReader->RewindEvents();
373 while (rawEv < event)
375 if ( ! fRawReader->NextEvent())
377 fRawReader->RewindEvents();
379 throw (kEH + Form("Error going to next raw-event from event %d.", rawEv));
384 printf ("Loaded raw-event %d.\n", rawEv);
388 SetName(Form("Event %d", fEventId));
391 AfterNewEventLoaded();
394 void AliEveEventManager::Close()
396 // Close the event files.
397 // For the moment only ESD is closed. Needs to be investigated for
398 // AliRunLoader and Raw.
401 delete fESD; fESD = 0;
402 delete fESDfriend; fESDfriend = 0;
404 delete fESDTree; fESDTree = 0;
405 delete fESDFile; fESDFile = 0;
410 /******************************************************************************/
411 // Static convenience functions, mainly used from macros.
412 /******************************************************************************/
414 AliRunLoader* AliEveEventManager::AssertRunLoader()
416 // Make sure AliRunLoader is initialized and return it.
417 // Throws exception in case run-loader is not available.
418 // Static utility for macros.
420 static const TEveException kEH("AliEveEventManager::AssertRunLoader ");
422 if (gAliEveEvent == 0)
423 throw (kEH + "ALICE event not ready.");
424 if (gAliEveEvent->fRunLoader == 0)
425 throw (kEH + "AliRunLoader not initialised.");
426 return gAliEveEvent->fRunLoader;
429 AliESDEvent* AliEveEventManager::AssertESD()
431 // Make sure AliESDEvent is initialized and return it.
432 // Throws exception in case ESD is not available.
433 // Static utility for macros.
435 static const TEveException kEH("AliEveEventManager::AssertESD ");
437 if (gAliEveEvent == 0)
438 throw (kEH + "ALICE event not ready.");
439 if (gAliEveEvent->fESD == 0)
440 throw (kEH + "AliESD not initialised.");
441 return gAliEveEvent->fESD;
444 AliESDfriend* AliEveEventManager::AssertESDfriend()
446 // Make sure AliESDfriend is initialized and return it.
447 // Throws exception in case ESDfriend-loader is not available.
448 // Static utility for macros.
450 static const TEveException kEH("AliEveEventManager::AssertESDfriend ");
452 if (gAliEveEvent == 0)
453 throw (kEH + "ALICE event not ready.");
454 if (gAliEveEvent->fESDfriend == 0)
455 throw (kEH + "AliESDfriend not initialised.");
456 return gAliEveEvent->fESDfriend;
459 AliRawReader* AliEveEventManager::AssertRawReader()
461 // Make sure raw-reader is initialized and return it.
463 static const TEveException kEH("AliEveEventManager::AssertRawReader ");
465 if (gAliEveEvent == 0)
466 throw (kEH + "ALICE event not ready.");
467 if (gAliEveEvent->fRawReader == 0)
468 throw (kEH + "RawReader not ready.");
470 return gAliEveEvent->fRawReader;
473 AliMagF* AliEveEventManager::AssertMagField()
475 // Make sure AliMagF is initialized and return it.
476 // Throws exception in case magnetic field is not available.
477 // Static utility for macros.
481 if (gAliEveEvent && gAliEveEvent->fRunLoader && gAliEveEvent->fRunLoader->GetAliRun())
482 fgMagField = gAliEveEvent->fRunLoader->GetAliRun()->Field();
484 fgMagField = new AliMagFMaps("Maps","Maps", 1, 1., 10., AliMagFMaps::k5kG);
489 TGeoManager* AliEveEventManager::AssertGeometry()
491 // Make sure AliGeomManager is initialized and returns the
492 // corresponding TGeoManger.
493 // gGeoManager is set to the return value.
494 // Throws exception if geometry can not be loaded or if it is not
495 // available and the TGeoManager is locked.
496 // Static utility for macros.
498 static const TEveException kEH("AliEveEventManager::AssertGeometry ");
500 if (AliGeomManager::GetGeometry() == 0)
502 if (TGeoManager::IsLocked())
503 throw (kEH + "geometry is not loaded but TGeoManager is locked.");
506 AliGeomManager::LoadGeometry();
507 if ( ! AliGeomManager::GetGeometry())
509 throw (kEH + "can not load geometry.");
511 if ( ! AliGeomManager::ApplyAlignObjsFromCDB("ITS TPC TRD TOF PHOS HMPID EMCAL MUON FMD ZDC PMD T0 VZERO ACORDE"))
513 ::Warning(kEH, "mismatch of alignable volumes. Proceeding.");
514 // throw (kEH + "could not apply align objs.");
516 AliGeomManager::GetGeometry()->DefaultColors();
519 gGeoManager = AliGeomManager::GetGeometry();
523 void AliEveEventManager::SetAutoLoad(Bool_t autoLoad)
525 // Set the automatic event loading mode
526 fAutoLoad = autoLoad;
527 StartStopAutoLoadTimer();
530 void AliEveEventManager::SetAutoLoadTime(Double_t time)
532 // Set the auto-load time in seconds
533 fAutoLoadTime = time;
534 StartStopAutoLoadTimer();
537 void AliEveEventManager::StartStopAutoLoadTimer()
539 // Create if needed and start
540 // the automatic event loading timer
542 if (!fAutoLoadTimer) {
543 fAutoLoadTimer = new TTimer;
544 fAutoLoadTimer->Connect("Timeout()","AliEveEventManager",this,"NextEvent()");
546 fAutoLoadTimer->Start((Long_t)fAutoLoadTime*1000,kTRUE);
549 if (fAutoLoadTimer) fAutoLoadTimer->Stop();
553 void AliEveEventManager::PrevEvent()
555 // Loads previous event
556 // only in case of manual mode
558 GotoEvent(fEventId - 1);
559 StartStopAutoLoadTimer();
563 void AliEveEventManager::NextEvent()
566 // either in automatic (online) or
570 if (fAutoLoadTimer) fAutoLoadTimer->Stop();
577 GotoEvent(fEventId + 1);
578 StartStopAutoLoadTimer();
582 const char* AliEveEventManager::GetEventInfo() const
584 // Dumps the event-header contents
586 static TString eventInfo;
588 if (!fRawReader) return "No event information is available";
590 const UInt_t* id = fRawReader->GetEventId();
591 const UInt_t* pattern = fRawReader->GetTriggerPattern();
592 const UInt_t* attr = fRawReader->GetAttributes();
593 eventInfo.Form("Run#: %d\nEvent type: %d\nPeriod: %x\nOrbit: %x\nBC: %x\nTrigger: %x-%x\nDetectors: %x\nAttributes:%x-%x-%x",
594 fRawReader->GetRunNumber(),fRawReader->GetType(),
595 (((id)[0]>>4)&0x0fffffff),((((id)[0]<<20)&0xf00000)|(((id)[1]>>12)&0xfffff)),((id)[1]&0x00000fff),
596 pattern[0],pattern[1],
597 *fRawReader->GetDetectorPattern(),
598 attr[0],attr[1],attr[2]);
600 return eventInfo.Data();