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>
18 #include <AliRawEventHeaderBase.h>
19 #include <AliRawReaderRoot.h>
20 #include <AliRawReaderFile.h>
21 #include <AliRawReaderDate.h>
22 #include <AliMagFMaps.h>
23 #include <AliCDBManager.h>
24 #include <AliHeader.h>
25 #include <AliGeomManager.h>
29 #include <TGeoManager.h>
32 //==============================================================================
33 //==============================================================================
35 //==============================================================================
37 //______________________________________________________________________________
39 // Provide interface for loading and navigating standard AliRoot data
40 // (AliRunLoader) and ESDs.
42 // Missing support for raw-data. For now this is handled individually
43 // by each sub-detector.
45 // Also provides interface to magnetic-field and geometry. Mostly
46 // intended as wrappers over standard AliRoot functionality for
47 // convenient use from visualizateion macros.
49 ClassImp(AliEveEventManager)
51 AliEveEventManager* gAliEveEvent = 0;
53 Bool_t AliEveEventManager::fgAssertRunLoader = kFALSE;
54 Bool_t AliEveEventManager::fgAssertESD = kFALSE;
55 Bool_t AliEveEventManager::fgAssertRaw = kFALSE;
57 TString AliEveEventManager::fgESDFileName("AliESDs.root");
58 TString AliEveEventManager::fgRawFileName("raw.root");
59 TString AliEveEventManager::fgCdbUri("local://$ALICE_ROOT");
61 AliMagF* AliEveEventManager::fgMagField = 0;
64 AliEveEventManager::AliEveEventManager() :
67 fPath ( ), fEventId (-1),
69 fESDFile (0), fESDTree (0), fESD (0),
70 fESDfriend (0), fESDfriendExists(kFALSE),
77 // Default constructor.
80 AliEveEventManager::AliEveEventManager(TString path, Int_t ev) :
81 TEveEventManager("AliEVE AliEveEventManager"),
83 fPath (path), fEventId(-1),
85 fESDFile (0), fESDTree (0), fESD (0),
86 fESDfriend (0), fESDfriendExists(kFALSE),
93 // Constructor with event-directory URL and event-id.
96 if (ev >= 0) GotoEvent(ev);
99 AliEveEventManager::~AliEveEventManager()
103 if (fAutoLoadTimer) delete fAutoLoadTimer;
104 // Somewhat unclear what to do here.
105 // In principle should close all data sources and deregister from
109 /******************************************************************************/
111 void AliEveEventManager::SetESDFileName(const Text_t* esd)
113 // Set file-name for opening ESD, default "AliESDs.root".
115 if (esd) fgESDFileName = esd;
118 void AliEveEventManager::SetRawFileName(const Text_t* raw)
120 // Set file-name for opening of raw-data, default "raw.root"
121 if (raw) fgRawFileName = raw;
124 void AliEveEventManager::SetCdbUri(const Text_t* cdb)
126 // Set path to CDB, default "local://$ALICE_ROOT".
128 if (cdb) fgCdbUri = cdb;
131 void AliEveEventManager::SetAssertElements(Bool_t assertRunloader,
135 // Set global flags that detrmine which parts of the event-data must
136 // be present when the event is opened.
138 fgAssertRunLoader = assertRunloader;
139 fgAssertESD = assertEsd;
140 fgAssertRaw = assertRaw;
143 /******************************************************************************/
145 void AliEveEventManager::Open()
147 // Open event-data from URL specified in fPath.
148 // Attempts to create AliRunLoader() and to open ESD with ESDfriends.
149 // Warning is reported if run-loader or ESD is not found.
150 // Global data-members fgAssertRunLoader and fgAssertESD can be set
151 // to throw exceptions instead.
153 static const TEveException kEH("AliEveEventManager::Open ");
155 gSystem->ExpandPathName(fPath);
156 // The following magick is required for ESDriends to be loaded properly
157 // from non-current directory.
158 if (fPath.IsNull() || fPath == ".")
160 fPath = gSystem->WorkingDirectory();
162 else if ( ! fPath.BeginsWith("file:/"))
164 TUrl url(fPath, kTRUE);
165 TString protocol(url.GetProtocol());
166 if (protocol == "file" && fPath[0] != '/')
167 fPath = Form("%s/%s", gSystem->WorkingDirectory(), fPath.Data());
172 // Open ESD and ESDfriends
174 TString esdPath(Form("%s/%s", fPath.Data(), fgESDFileName.Data()));
175 if ((fESDFile = TFile::Open(esdPath)))
177 fESD = new AliESDEvent();
178 fESDTree = (TTree*) fESDFile->Get("esdTree");
181 fESD->ReadFromTree(fESDTree);
182 fESDTree->GetEntry(0);
184 runNo = fESD->GetESDRun()->GetRunNumber();
186 // Check if ESDfriends exists and attach the branch
187 TString p(Form("%s/AliESDfriends.root", fPath.Data()));
188 if (gSystem->AccessPathName(p, kReadPermission) == kFALSE)
190 fESDfriendExists = kTRUE;
191 fESDTree->SetBranchStatus ("ESDfriend*", 1);
192 fESDTree->SetBranchAddress("ESDfriend.", &fESDfriend);
197 delete fESDFile; fESDFile = 0;
198 Warning(kEH, "failed getting the esdTree.");
201 else // esd not readable
203 Warning(kEH, "can not read ESD file '%s'.", esdPath.Data());
209 throw (kEH + "ESD not initialized. Its precence was requested.");
211 Warning(kEH, "ESD not initialized.");
215 // Open RunLoader from galice.root
217 TString gaPath(Form("%s/galice.root", fPath.Data()));
218 // If i use open directly, we get fatal.
219 // Is AccessPathName check ok for xrootd / alien? Yes, not for http.
220 if (gSystem->AccessPathName(gaPath, kReadPermission) == kFALSE)
222 fRunLoader = AliRunLoader::Open(gaPath);
225 TString alicePath = fPath + "/";
226 fRunLoader->SetDirName(alicePath);
228 if (fRunLoader->LoadgAlice() != 0)
229 Warning(kEH, "failed loading gAlice via run-loader.");
231 if (fRunLoader->LoadHeader() == 0)
234 runNo = fRunLoader->GetHeader()->GetRun();
238 Warning(kEH, "failed loading run-loader's header.");
243 else // run-loader open failed
245 Warning(kEH, "failed opening ALICE run-loader from '%s'.", gaPath.Data());
248 else // galice not readable
250 Warning(kEH, "can not read '%s'.", gaPath.Data());
254 if (fgAssertRunLoader)
255 throw (kEH + "Bootstraping of run-loader failed. Its precence was requested.");
257 Warning(kEH, "Bootstraping of run-loader failed.");
260 // Open raw-data file
262 TString rawPath(Form("%s/%s", fPath.Data(), fgRawFileName.Data()));
263 // If i use open directly, raw-reader reports an error but i have
264 // no way to detect it.
265 // Is this (AccessPathName check) ok for xrootd / alien?
266 if (gSystem->AccessPathName(rawPath, kReadPermission) == kFALSE)
268 if (fgRawFileName.EndsWith("/"))
270 fRawReader = new AliRawReaderFile(rawPath);
272 else if (fgRawFileName.EndsWith(".root"))
274 fRawReader = new AliRawReaderRoot(rawPath);
276 else if (!fgRawFileName.IsNull())
278 fRawReader = new AliRawReaderDate(rawPath);
286 throw (kEH + "raw-data not initialized. Its precence was requested.");
288 Warning(kEH, "raw-data not initialized.");
296 fRawReader->NextEvent();
297 runNo = fRawReader->GetRunNumber();
298 printf("Determining run-no from raw ... run=%d\n", runNo);
299 fRawReader->RewindEvents();
301 throw (kEH + "unknown run number.");
306 AliCDBManager* cdb = AliCDBManager::Instance();
307 cdb->SetDefaultStorage(fgCdbUri);
308 if (cdb->IsDefaultStorageSet() == kFALSE)
309 throw (kEH + "CDB initialization failed.");
313 SetName(Form("Event %d", fEventId));
317 void AliEveEventManager::SetEvent(AliRunLoader *runLoader, AliRawReader *rawReader, AliESDEvent *esd)
319 // Set an event from an external source
320 // The method is used in the online visualisation
321 fRunLoader = runLoader;
322 fRawReader = rawReader;
325 SetTitle("Online event in memory");
326 SetName("Online Event");
329 AfterNewEventLoaded();
332 Int_t AliEveEventManager::GetMaxEventId(Bool_t refreshESD) const
334 // Returns maximum available event id.
335 // If raw-data is the only data-source this can not be known
336 // and 10,000,000 is returned.
337 // If neither data-source is initialised an exception is thrown.
338 // If refresh_esd is true and ESD is the primary event-data source
339 // its header is re-read from disk.
341 static const TEveException kEH("AliEveEventManager::GetMaxEventId ");
345 return fRunLoader->GetNumberOfEvents() - 1;
351 return fESDTree->GetEntries() - 1;
359 throw (kEH + "neither RunLoader, ESD nor Raw loaded.");
363 void AliEveEventManager::GotoEvent(Int_t event)
365 // Load data for specified event.
366 // If event is out of range an exception is thrown and old state
368 // After successful loading of event, the virtual function
369 // AfterNewEventLoaded() is called. This executes commands that
370 // were registered via TEveEventManager::AddNewEventCommand().
372 // If event is negative, it is subtracted from the number of
373 // available events, thus passing -1 will load the last event.
374 // This is not supported when raw-data is the only data-source
375 // as the number of events is not known.
377 static const TEveException kEH("AliEveEventManager::GotoEvent ");
383 maxEvent = fESDTree->GetEntries() - 1;
385 event = fESDTree->GetEntries() + event;
389 maxEvent = fRunLoader->GetNumberOfEvents() - 1;
391 event = fRunLoader->GetNumberOfEvents() + event;
397 Error(kEH, "negative event id passed for raw-data as source. Operation not supported.");
400 Info(kEH, "number of events unknown for raw-data, setting max-event id to 10M.");
404 throw (kEH + "neither RunLoader, ESD nor Raw loaded.");
406 if (event < 0 || event > maxEvent)
408 throw (kEH + Form("event %d not present, available range [%d, %d].",
409 event, 0, maxEvent));
412 TEveManager::TRedrawDisabler rd(gEve);
413 gEve->Redraw3D(kFALSE, kTRUE); // Enforce drop of all logicals.
415 // !!! MT this is somewhat brutal; at least optionally, one could be
416 // a bit gentler, checking for objs owning their external refs and having
417 // additinal parents.
421 if (fESDTree->GetEntry(event) <= 0)
422 throw (kEH + "failed getting required event from ESD.");
424 if (fESDfriendExists)
425 fESD->SetESDfriend(fESDfriend);
429 if (fRunLoader->GetEvent(event) != 0)
430 throw (kEH + "failed getting required event.");
435 Int_t rawEv = fEventId;
438 fRawReader->RewindEvents();
442 while (rawEv < event)
444 if ( ! fRawReader->NextEvent())
446 fRawReader->RewindEvents();
448 throw (kEH + Form("Error going to next raw-event from event %d.", rawEv));
453 printf ("Loaded raw-event %d.\n", rawEv);
457 SetName(Form("Event %d", fEventId));
460 AfterNewEventLoaded();
463 void AliEveEventManager::NextEvent()
466 // either in automatic (online) or
470 if (fAutoLoadTimer) fAutoLoadTimer->Stop();
477 if (fEventId < GetMaxEventId(kTRUE))
478 GotoEvent(fEventId + 1);
481 StartStopAutoLoadTimer();
485 void AliEveEventManager::PrevEvent()
487 // Loads previous event
488 // only in case of manual mode
490 GotoEvent(fEventId - 1);
491 StartStopAutoLoadTimer();
495 void AliEveEventManager::Close()
497 // Close the event files.
498 // For the moment only ESD is closed. Needs to be investigated for
499 // AliRunLoader and Raw.
502 delete fESD; fESD = 0;
503 delete fESDfriend; fESDfriend = 0;
505 delete fESDTree; fESDTree = 0;
506 delete fESDFile; fESDFile = 0;
511 /******************************************************************************/
512 // Static convenience functions, mainly used from macros.
513 /******************************************************************************/
515 AliRunLoader* AliEveEventManager::AssertRunLoader()
517 // Make sure AliRunLoader is initialized and return it.
518 // Throws exception in case run-loader is not available.
519 // Static utility for macros.
521 static const TEveException kEH("AliEveEventManager::AssertRunLoader ");
523 if (gAliEveEvent == 0)
524 throw (kEH + "ALICE event not ready.");
525 if (gAliEveEvent->fRunLoader == 0)
526 throw (kEH + "AliRunLoader not initialised.");
527 return gAliEveEvent->fRunLoader;
530 AliESDEvent* AliEveEventManager::AssertESD()
532 // Make sure AliESDEvent is initialized and return it.
533 // Throws exception in case ESD is not available.
534 // Static utility for macros.
536 static const TEveException kEH("AliEveEventManager::AssertESD ");
538 if (gAliEveEvent == 0)
539 throw (kEH + "ALICE event not ready.");
540 if (gAliEveEvent->fESD == 0)
541 throw (kEH + "AliESD not initialised.");
542 return gAliEveEvent->fESD;
545 AliESDfriend* AliEveEventManager::AssertESDfriend()
547 // Make sure AliESDfriend is initialized and return it.
548 // Throws exception in case ESDfriend-loader is not available.
549 // Static utility for macros.
551 static const TEveException kEH("AliEveEventManager::AssertESDfriend ");
553 if (gAliEveEvent == 0)
554 throw (kEH + "ALICE event not ready.");
555 if (gAliEveEvent->fESDfriend == 0)
556 throw (kEH + "AliESDfriend not initialised.");
557 return gAliEveEvent->fESDfriend;
560 AliRawReader* AliEveEventManager::AssertRawReader()
562 // Make sure raw-reader is initialized and return it.
564 static const TEveException kEH("AliEveEventManager::AssertRawReader ");
566 if (gAliEveEvent == 0)
567 throw (kEH + "ALICE event not ready.");
568 if (gAliEveEvent->fRawReader == 0)
569 throw (kEH + "RawReader not ready.");
571 return gAliEveEvent->fRawReader;
574 AliMagF* AliEveEventManager::AssertMagField()
576 // Make sure AliMagF is initialized and return it.
577 // Throws exception in case magnetic field is not available.
578 // Static utility for macros.
582 if (gAliEveEvent && gAliEveEvent->fRunLoader && gAliEveEvent->fRunLoader->GetAliRun())
583 fgMagField = gAliEveEvent->fRunLoader->GetAliRun()->Field();
585 fgMagField = new AliMagFMaps("Maps","Maps", 1, 1., 10., AliMagFMaps::k5kG);
590 TGeoManager* AliEveEventManager::AssertGeometry()
592 // Make sure AliGeomManager is initialized and returns the
593 // corresponding TGeoManger.
594 // gGeoManager is set to the return value.
595 // Throws exception if geometry can not be loaded or if it is not
596 // available and the TGeoManager is locked.
597 // Static utility for macros.
599 static const TEveException kEH("AliEveEventManager::AssertGeometry ");
601 if (AliGeomManager::GetGeometry() == 0)
603 if (TGeoManager::IsLocked())
604 throw (kEH + "geometry is not loaded but TGeoManager is locked.");
607 AliGeomManager::LoadGeometry();
608 if ( ! AliGeomManager::GetGeometry())
610 throw (kEH + "can not load geometry.");
612 if ( ! AliGeomManager::ApplyAlignObjsFromCDB("ITS TPC TRD TOF PHOS HMPID EMCAL MUON FMD ZDC PMD T0 VZERO ACORDE"))
614 ::Warning(kEH, "mismatch of alignable volumes. Proceeding.");
615 // throw (kEH + "could not apply align objs.");
617 AliGeomManager::GetGeometry()->DefaultColors();
620 gGeoManager = AliGeomManager::GetGeometry();
624 void AliEveEventManager::SetAutoLoad(Bool_t autoLoad)
626 // Set the automatic event loading mode
627 fAutoLoad = autoLoad;
628 StartStopAutoLoadTimer();
631 void AliEveEventManager::SetAutoLoadTime(Double_t time)
633 // Set the auto-load time in seconds
634 fAutoLoadTime = time;
635 StartStopAutoLoadTimer();
638 void AliEveEventManager::StartStopAutoLoadTimer()
640 // Create if needed and start
641 // the automatic event loading timer
643 if (!fAutoLoadTimer) {
644 fAutoLoadTimer = new TTimer;
645 fAutoLoadTimer->Connect("Timeout()","AliEveEventManager",this,"NextEvent()");
647 fAutoLoadTimer->Start((Long_t)fAutoLoadTime*1000,kTRUE);
650 if (fAutoLoadTimer) fAutoLoadTimer->Stop();
654 const char* AliEveEventManager::GetEventInfo() const
656 // Dumps the event-header contents
658 static TString eventInfo;
661 eventInfo.Form("No raw-data event info is available!\n");
664 const UInt_t* attr = fRawReader->GetAttributes();
665 eventInfo.Form("Raw-data event info:\nRun#: %d\nEvent type: %d (%s)\nPeriod: %x\nOrbit: %x BC: %x\nTrigger: %llx\nDetectors: %x (%s)\nAttributes:%x-%x-%x\nTimestamp: %x\n",
666 fRawReader->GetRunNumber(),fRawReader->GetType(),AliRawEventHeaderBase::GetTypeName(fRawReader->GetType()),
667 fRawReader->GetPeriod(),fRawReader->GetOrbitID(),fRawReader->GetBCID(),
668 fRawReader->GetClassMask(),
669 *fRawReader->GetDetectorPattern(),AliDAQ::ListOfTriggeredDetectors(*fRawReader->GetDetectorPattern()),
670 attr[0],attr[1],attr[2],
671 fRawReader->GetTimestamp());
674 eventInfo.Append(Form("\nNo ESD event info is available!\n"));
677 TString acttrclasses = fESD->GetESDRun()->GetActiveTriggerClasses();
678 TString firedtrclasses = fESD->GetFiredTriggerClasses();
679 eventInfo.Append(Form("\nESD event info:\nRun#: %d\nActive trigger classes: %s\nEvent type: %d (%s)\nPeriod: %x\nOrbit: %x BC: %x\nTrigger: %llx (%s)\nEvent# in file:%d\nTimestamp: %x\n",
680 fESD->GetRunNumber(),
682 fESD->GetEventType(),AliRawEventHeaderBase::GetTypeName(fESD->GetEventType()),
683 fESD->GetPeriodNumber(),fESD->GetOrbitNumber(),fESD->GetBunchCrossNumber(),
684 fESD->GetTriggerMask(),firedtrclasses.Data(),
685 fESD->GetEventNumberInFile(),
686 fESD->GetTimeStamp()));
689 return eventInfo.Data();