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>
29 //==============================================================================
30 //==============================================================================
32 //==============================================================================
34 //______________________________________________________________________________
36 // Provide interface for loading and navigating standard AliRoot data
37 // (AliRunLoader) and ESDs.
39 // Missing support for raw-data. For now this is handled individually
40 // by each sub-detector.
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.
46 ClassImp(AliEveEventManager)
48 AliEveEventManager* gAliEveEvent = 0;
50 Bool_t AliEveEventManager::fgAssertRunLoader = kFALSE;
51 Bool_t AliEveEventManager::fgAssertESD = kFALSE;
52 Bool_t AliEveEventManager::fgAssertRaw = kFALSE;
54 TString AliEveEventManager::fgESDFileName("AliESDs.root");
55 TString AliEveEventManager::fgRawFileName("raw.root");
56 TString AliEveEventManager::fgCdbUri("local://$ALICE_ROOT");
58 AliMagF* AliEveEventManager::fgMagField = 0;
61 AliEveEventManager::AliEveEventManager() :
64 fPath ( ), fEventId (-1),
66 fESDFile (0), fESDTree (0), fESD (0),
67 fESDfriend (0), fESDfriendExists(kFALSE),
74 // Default constructor.
77 AliEveEventManager::AliEveEventManager(TString path, Int_t ev) :
78 TEveEventManager("AliEVE AliEveEventManager"),
80 fPath (path), fEventId(-1),
82 fESDFile (0), fESDTree (0), fESD (0),
83 fESDfriend (0), fESDfriendExists(kFALSE),
90 // Constructor with event-directory URL and event-id.
93 if (ev >= 0) GotoEvent(ev);
96 AliEveEventManager::~AliEveEventManager()
100 if (fAutoLoadTimer) delete fAutoLoadTimer;
101 // Somewhat unclear what to do here.
102 // In principle should close all data sources and deregister from
106 /******************************************************************************/
108 void AliEveEventManager::SetESDFileName(const Text_t* esd)
110 // Set file-name for opening ESD, default "AliESDs.root".
112 if (esd) fgESDFileName = esd;
115 void AliEveEventManager::SetRawFileName(const Text_t* raw)
117 // Set file-name for opening of raw-data, default "raw.root"
118 if (raw) fgRawFileName = raw;
121 void AliEveEventManager::SetCdbUri(const Text_t* cdb)
123 // Set path to CDB, default "local://$ALICE_ROOT".
125 if (cdb) fgCdbUri = cdb;
128 void AliEveEventManager::SetAssertElements(Bool_t assertRunloader,
132 // Set global flags that detrmine which parts of the event-data must
133 // be present when the event is opened.
135 fgAssertRunLoader = assertRunloader;
136 fgAssertESD = assertEsd;
137 fgAssertRaw = assertRaw;
140 /******************************************************************************/
142 void AliEveEventManager::Open()
144 // Open event-data from URL specified in fPath.
145 // Attempts to create AliRunLoader() and to open ESD with ESDfriends.
146 // Warning is reported if run-loader or ESD is not found.
147 // Global data-members fgAssertRunLoader and fgAssertESD can be set
148 // to throw exceptions instead.
150 static const TEveException kEH("AliEveEventManager::Open ");
152 gSystem->ExpandPathName(fPath);
154 fPath = Form("%s/%s", gSystem->WorkingDirectory(), fPath.Data());
158 TString gaPath(Form("%s/galice.root", fPath.Data()));
159 // If i use open directly, we get fatal.
160 // Is this (AccessPathName check) ok for xrootd / alien?
161 if (gSystem->AccessPathName(gaPath, kReadPermission) == kFALSE)
163 fRunLoader = AliRunLoader::Open(gaPath);
166 TString alicePath = fPath + "/";
167 fRunLoader->SetDirName(alicePath);
169 if (fRunLoader->LoadgAlice() != 0)
170 Warning(kEH, "failed loading gAlice via run-loader.");
172 if (fRunLoader->LoadHeader() == 0)
174 runNo = fRunLoader->GetHeader()->GetRun();
178 Warning(kEH, "failed loading run-loader's header.");
183 else // run-loader open failed
185 Warning(kEH, "failed opening ALICE run-loader from '%s'.", gaPath.Data());
188 else // galice not readable
190 Warning(kEH, "can not read '%s'.", gaPath.Data());
194 if (fgAssertRunLoader)
195 throw (kEH + "Bootstraping of run-loader failed. Its precence was requested.");
197 Warning(kEH, "Bootstraping of run-loader failed.");
201 TString esdPath(Form("%s/%s", fPath.Data(), fgESDFileName.Data()));
202 if ((fESDFile = TFile::Open(esdPath)))
204 fESD = new AliESDEvent();
205 fESDTree = (TTree*) fESDFile->Get("esdTree");
208 fESD->ReadFromTree(fESDTree);
209 runNo = fESD->GetESDRun()->GetRunNumber();
211 // Check if ESDfriends exists and attach the branch
212 TString p = Form("%s/AliESDfriends.root", fPath.Data());
213 if (gSystem->AccessPathName(p, kReadPermission) == kFALSE)
215 fESDfriendExists = kTRUE;
216 fESDTree->SetBranchStatus ("ESDfriend*", 1);
217 fESDTree->SetBranchAddress("ESDfriend.", &fESDfriend);
222 delete fESDFile; fESDFile = 0;
223 Warning(kEH, "failed getting the esdTree.");
226 else // esd not readable
228 Warning(kEH, "can not read ESD file '%s'.", esdPath.Data());
234 throw (kEH + "ESD not initialized. Its precence was requested.");
236 Warning(kEH, "ESD not initialized.");
240 TString rawPath(Form("%s/%s", fPath.Data(), fgRawFileName.Data()));
241 // If i use open directly, raw-reader reports an error but i have
242 // no way to detect it.
243 // Is this (AccessPathName check) ok for xrootd / alien?
244 if (gSystem->AccessPathName(rawPath, kReadPermission) == kFALSE)
246 if (fgRawFileName.EndsWith("/"))
248 fRawReader = new AliRawReaderFile(rawPath);
250 else if (fgRawFileName.EndsWith(".root"))
252 fRawReader = new AliRawReaderRoot(rawPath);
254 else if (!fgRawFileName.IsNull())
256 fRawReader = new AliRawReaderDate(rawPath);
264 throw (kEH + "raw-data not initialized. Its precence was requested.");
266 Warning(kEH, "raw-data not initialized.");
274 fRawReader->NextEvent();
275 runNo = fRawReader->GetRunNumber();
276 printf("Determining run-no from raw ... run=%d\n", runNo);
277 fRawReader->RewindEvents();
279 throw (kEH + "unknown run number.");
284 AliCDBManager* cdb = AliCDBManager::Instance();
285 cdb->SetDefaultStorage(fgCdbUri);
286 if (cdb->IsDefaultStorageSet() == kFALSE)
287 throw (kEH + "CDB initialization failed.");
291 SetName(Form("Event %d", fEventId));
295 void AliEveEventManager::SetEvent(AliRunLoader *runLoader, AliRawReader *rawReader, AliESDEvent *esd)
297 // Set an event from an external source
298 // The method is used in the online visualisation
299 fRunLoader = runLoader;
300 fRawReader = rawReader;
303 SetTitle("Online event in memory");
304 SetName("Online Event");
307 AfterNewEventLoaded();
310 void AliEveEventManager::GotoEvent(Int_t event)
312 // Load data for specified event.
313 // If event is out of range an exception is thrown and old state
315 // After successful loading of event, the virtual function
316 // AfterNewEventLoaded() is called. This executes commands that
317 // were registered via TEveEventManager::AddNewEventCommand().
319 static const TEveException kEH("AliEveEventManager::GotoEvent ");
322 Error(kEH, "event must be non-negative.");
328 maxEvent = fRunLoader->GetNumberOfEvents() - 1;
329 } else if (fESDTree) {
330 maxEvent = fESDTree->GetEntries() - 1;
331 } else if (fRawReader) {
333 Info(kEH, "number of events unknown for raw-data, setting max-event id to 10M.");
335 throw (kEH + "neither RunLoader, ESD nor Raw loaded.");
337 if (event < 0 || event > maxEvent)
338 throw (kEH + Form("event %d not present, available range [%d, %d].",
339 event, 0, maxEvent));
341 TEveManager::TRedrawDisabler rd(gEve);
342 gEve->Redraw3D(kFALSE, kTRUE); // Enforce drop of all logicals.
344 // !!! MT this is somewhat brutal; at least optionally, one could be
345 // a bit gentler, checking for objs owning their external refs and having
346 // additinal parents.
350 if (fRunLoader->GetEvent(event) != 0)
351 throw (kEH + "failed getting required event.");
355 if (fESDTree->GetEntry(event) <= 0)
356 throw (kEH + "failed getting required event from ESD.");
358 if (fESDfriendExists)
359 fESD->SetESDfriend(fESDfriend);
364 Int_t rawEv = fEventId;
367 fRawReader->RewindEvents();
371 while (rawEv < event)
373 if ( ! fRawReader->NextEvent())
375 fRawReader->RewindEvents();
377 throw (kEH + Form("Error going to next raw-event from event %d.", rawEv));
382 printf ("Loaded raw-event %d.\n", rawEv);
386 SetName(Form("Event %d", fEventId));
389 AfterNewEventLoaded();
392 void AliEveEventManager::Close()
394 // Close the event files.
395 // For the moment only ESD is closed. Needs to be investigated for
396 // AliRunLoader and Raw.
399 delete fESD; fESD = 0;
400 delete fESDfriend; fESDfriend = 0;
402 delete fESDTree; fESDTree = 0;
403 delete fESDFile; fESDFile = 0;
408 /******************************************************************************/
409 // Static convenience functions, mainly used from macros.
410 /******************************************************************************/
412 AliRunLoader* AliEveEventManager::AssertRunLoader()
414 // Make sure AliRunLoader is initialized and return it.
415 // Throws exception in case run-loader is not available.
416 // Static utility for macros.
418 static const TEveException kEH("AliEveEventManager::AssertRunLoader ");
420 if (gAliEveEvent == 0)
421 throw (kEH + "ALICE event not ready.");
422 if (gAliEveEvent->fRunLoader == 0)
423 throw (kEH + "AliRunLoader not initialised.");
424 return gAliEveEvent->fRunLoader;
427 AliESDEvent* AliEveEventManager::AssertESD()
429 // Make sure AliESDEvent is initialized and return it.
430 // Throws exception in case ESD is not available.
431 // Static utility for macros.
433 static const TEveException kEH("AliEveEventManager::AssertESD ");
435 if (gAliEveEvent == 0)
436 throw (kEH + "ALICE event not ready.");
437 if (gAliEveEvent->fESD == 0)
438 throw (kEH + "AliESD not initialised.");
439 return gAliEveEvent->fESD;
442 AliESDfriend* AliEveEventManager::AssertESDfriend()
444 // Make sure AliESDfriend is initialized and return it.
445 // Throws exception in case ESDfriend-loader is not available.
446 // Static utility for macros.
448 static const TEveException kEH("AliEveEventManager::AssertESDfriend ");
450 if (gAliEveEvent == 0)
451 throw (kEH + "ALICE event not ready.");
452 if (gAliEveEvent->fESDfriend == 0)
453 throw (kEH + "AliESDfriend not initialised.");
454 return gAliEveEvent->fESDfriend;
457 AliRawReader* AliEveEventManager::AssertRawReader()
459 // Make sure raw-reader is initialized and return it.
461 static const TEveException kEH("AliEveEventManager::AssertRawReader ");
463 if (gAliEveEvent == 0)
464 throw (kEH + "ALICE event not ready.");
465 if (gAliEveEvent->fRawReader == 0)
466 throw (kEH + "RawReader not ready.");
468 return gAliEveEvent->fRawReader;
471 AliMagF* AliEveEventManager::AssertMagField()
473 // Make sure AliMagF is initialized and return it.
474 // Throws exception in case magnetic field is not available.
475 // Static utility for macros.
479 if (gAliEveEvent && gAliEveEvent->fRunLoader && gAliEveEvent->fRunLoader->GetAliRun())
480 fgMagField = gAliEveEvent->fRunLoader->GetAliRun()->Field();
482 fgMagField = new AliMagFMaps("Maps","Maps", 1, 1., 10., AliMagFMaps::k5kG);
487 TGeoManager* AliEveEventManager::AssertGeometry()
489 // Make sure AliGeomManager is initialized and returns the
490 // corresponding TGeoManger.
491 // gGeoManager is set to the return value.
492 // Throws exception if geometry can not be loaded or if it is not
493 // available and the TGeoManager is locked.
494 // Static utility for macros.
496 static const TEveException kEH("AliEveEventManager::AssertGeometry ");
498 if (AliGeomManager::GetGeometry() == 0)
500 if (TGeoManager::IsLocked())
501 throw (kEH + "geometry is not loaded but TGeoManager is locked.");
504 AliGeomManager::LoadGeometry();
505 if ( ! AliGeomManager::GetGeometry())
507 throw (kEH + "can not load geometry.");
509 if ( ! AliGeomManager::ApplyAlignObjsFromCDB("ITS TPC TRD TOF PHOS HMPID EMCAL MUON FMD ZDC PMD T0 VZERO ACORDE"))
511 ::Warning(kEH, "mismatch of alignable volumes. Proceeding.");
512 // throw (kEH + "could not apply align objs.");
515 // @@NEWROOT@@ Temporary fix.
516 // In AliEve several simplified geometries can be loaded at a later stage.
517 // Locking/unlocking is now handled properly in
518 // TEveManager::GetGeometry() but we're waiting for next root
519 // version due on 14.5.2008.
520 TGeoManager::UnlockGeometry();
523 gGeoManager = AliGeomManager::GetGeometry();
527 void AliEveEventManager::SetAutoLoad(Bool_t autoLoad)
529 // Set the automatic event loading mode
530 fAutoLoad = autoLoad;
531 StartStopAutoLoadTimer();
534 void AliEveEventManager::SetAutoLoadTime(Double_t time)
536 // Set the auto-load time in seconds
537 fAutoLoadTime = time;
538 StartStopAutoLoadTimer();
541 void AliEveEventManager::StartStopAutoLoadTimer()
543 // Create if needed and start
544 // the automatic event loading timer
546 if (!fAutoLoadTimer) {
547 fAutoLoadTimer = new TTimer;
548 fAutoLoadTimer->Connect("Timeout()","AliEveEventManager",this,"NextEvent()");
550 fAutoLoadTimer->Start((Long_t)fAutoLoadTime*1000,kTRUE);
553 if (fAutoLoadTimer) fAutoLoadTimer->Stop();
557 void AliEveEventManager::PrevEvent()
559 // Loads previous event
560 // only in case of manual mode
562 GotoEvent(fEventId - 1);
563 StartStopAutoLoadTimer();
567 void AliEveEventManager::NextEvent()
570 // either in automatic (online) or
574 if (fAutoLoadTimer) fAutoLoadTimer->Stop();
581 GotoEvent(fEventId + 1);
582 StartStopAutoLoadTimer();
586 const char* AliEveEventManager::GetEventInfo() const
588 // Dumps the event-header contents
590 static TString eventInfo;
592 if (!fRawReader) return "No event information is available";
594 const UInt_t* id = fRawReader->GetEventId();
595 const UInt_t* pattern = fRawReader->GetTriggerPattern();
596 const UInt_t* attr = fRawReader->GetAttributes();
597 eventInfo.Form("Run#: %d\nEvent type: %d\nPeriod: %x\nOrbit: %x\nBC: %x\nTrigger: %x-%x\nDetectors: %x\nAttributes:%x-%x-%x",
598 fRawReader->GetRunNumber(),fRawReader->GetType(),
599 (((id)[0]>>4)&0x0fffffff),((((id)[0]<<20)&0xf00000)|(((id)[1]>>12)&0xfffff)),((id)[1]&0x00000fff),
600 pattern[0],pattern[1],
601 *fRawReader->GetDetectorPattern(),
602 attr[0],attr[1],attr[2]);
604 return eventInfo.Data();