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 "AliEveMacroExecutor.h"
12 #include <TEveManager.h>
14 #include <AliRunLoader.h>
16 #include <AliESDEvent.h>
17 #include <AliESDfriend.h>
19 #include <AliRawEventHeaderBase.h>
20 #include <AliRawReaderRoot.h>
21 #include <AliRawReaderFile.h>
22 #include <AliRawReaderDate.h>
23 #include <AliMagFMaps.h>
24 #include <AliCDBManager.h>
25 #include <AliHeader.h>
26 #include <AliGeomManager.h>
30 #include <TGeoManager.h>
32 #include <TTimeStamp.h>
34 //==============================================================================
35 //==============================================================================
37 //==============================================================================
39 //______________________________________________________________________________
41 // Provide interface for loading and navigating standard AliRoot data
42 // (AliRunLoader) and ESDs.
44 // Missing support for raw-data. For now this is handled individually
45 // by each sub-detector.
47 // Also provides interface to magnetic-field and geometry. Mostly
48 // intended as wrappers over standard AliRoot functionality for
49 // convenient use from visualizateion macros.
51 ClassImp(AliEveEventManager)
53 AliEveEventManager* gAliEveEvent = 0;
55 Bool_t AliEveEventManager::fgAssertRunLoader = kFALSE;
56 Bool_t AliEveEventManager::fgAssertESD = kFALSE;
57 Bool_t AliEveEventManager::fgAssertRaw = kFALSE;
59 TString AliEveEventManager::fgESDFileName("AliESDs.root");
60 TString AliEveEventManager::fgRawFileName("raw.root");
61 TString AliEveEventManager::fgCdbUri("local://$ALICE_ROOT");
63 AliMagF* AliEveEventManager::fgMagField = 0;
66 AliEveEventManager::AliEveEventManager() :
69 fPath ( ), fEventId (-1),
71 fESDFile (0), fESDTree (0), fESD (0),
72 fESDfriend (0), fESDfriendExists(kFALSE),
78 fExecutor(new AliEveMacroExecutor)
80 // Default constructor.
83 AliEveEventManager::AliEveEventManager(TString path, Int_t ev) :
84 TEveEventManager("AliEVE AliEveEventManager"),
86 fPath (path), fEventId(-1),
88 fESDFile (0), fESDTree (0), fESD (0),
89 fESDfriend (0), fESDfriendExists(kFALSE),
95 fExecutor(new AliEveMacroExecutor)
97 // Constructor with event-directory URL and event-id.
100 if (ev >= 0) GotoEvent(ev);
103 AliEveEventManager::~AliEveEventManager()
107 if (fAutoLoadTimer) delete fAutoLoadTimer;
108 // Somewhat unclear what to do here.
109 // In principle should close all data sources and deregister from
113 /******************************************************************************/
115 void AliEveEventManager::SetESDFileName(const Text_t* esd)
117 // Set file-name for opening ESD, default "AliESDs.root".
119 if (esd) fgESDFileName = esd;
122 void AliEveEventManager::SetRawFileName(const Text_t* raw)
124 // Set file-name for opening of raw-data, default "raw.root"
125 if (raw) fgRawFileName = raw;
128 void AliEveEventManager::SetCdbUri(const Text_t* cdb)
130 // Set path to CDB, default "local://$ALICE_ROOT".
132 if (cdb) fgCdbUri = cdb;
135 void AliEveEventManager::SetAssertElements(Bool_t assertRunloader,
139 // Set global flags that detrmine which parts of the event-data must
140 // be present when the event is opened.
142 fgAssertRunLoader = assertRunloader;
143 fgAssertESD = assertEsd;
144 fgAssertRaw = assertRaw;
147 /******************************************************************************/
149 void AliEveEventManager::Open()
151 // Open event-data from URL specified in fPath.
152 // Attempts to create AliRunLoader() and to open ESD with ESDfriends.
153 // Warning is reported if run-loader or ESD is not found.
154 // Global data-members fgAssertRunLoader and fgAssertESD can be set
155 // to throw exceptions instead.
157 static const TEveException kEH("AliEveEventManager::Open ");
159 gSystem->ExpandPathName(fPath);
160 // The following magick is required for ESDriends to be loaded properly
161 // from non-current directory.
162 if (fPath.IsNull() || fPath == ".")
164 fPath = gSystem->WorkingDirectory();
166 else if ( ! fPath.BeginsWith("file:/"))
168 TUrl url(fPath, kTRUE);
169 TString protocol(url.GetProtocol());
170 if (protocol == "file" && fPath[0] != '/')
171 fPath = Form("%s/%s", gSystem->WorkingDirectory(), fPath.Data());
176 // Open ESD and ESDfriends
178 TString esdPath(Form("%s/%s", fPath.Data(), fgESDFileName.Data()));
179 if ((fESDFile = TFile::Open(esdPath)))
181 fESD = new AliESDEvent();
182 fESDTree = (TTree*) fESDFile->Get("esdTree");
185 // Check if ESDfriends exists and attach the branch
186 TString p(Form("%s/AliESDfriends.root", fPath.Data()));
187 TFile *esdFriendFile = TFile::Open(p);
189 if (!esdFriendFile->IsZombie())
191 esdFriendFile->Close();
192 delete esdFriendFile;
193 fESDfriendExists = kTRUE;
194 fESDTree->SetBranchStatus ("ESDfriend*", 1);
195 fESDTree->SetBranchAddress("ESDfriend.", &fESDfriend);
199 esdFriendFile->Close();
200 delete esdFriendFile;
204 fESD->ReadFromTree(fESDTree);
205 if (!fESDfriendExists) fESDTree->SetBranchStatus ("ESDfriend*", 0);
206 if (fESDTree->GetEntry(0) <= 0)
208 delete fESDFile; fESDFile = 0;
209 delete fESD; fESD = 0;
210 Warning(kEH, "failed getting the first entry from esdTree.");
215 runNo = fESD->GetESDRun()->GetRunNumber();
220 delete fESDFile; fESDFile = 0;
221 delete fESD; fESD = 0;
222 Warning(kEH, "failed getting the esdTree.");
225 else // esd not readable
227 Warning(kEH, "can not read ESD file '%s'.", esdPath.Data());
233 throw (kEH + "ESD not initialized. Its precence was requested.");
235 Warning(kEH, "ESD not initialized.");
239 // Open RunLoader from galice.root
241 TString gaPath(Form("%s/galice.root", fPath.Data()));
242 // If i use open directly, we get fatal.
243 // Is AccessPathName check ok for xrootd / alien? Yes, not for http.
244 if (gSystem->AccessPathName(gaPath, kReadPermission) == kFALSE)
246 fRunLoader = AliRunLoader::Open(gaPath);
249 TString alicePath = fPath + "/";
250 fRunLoader->SetDirName(alicePath);
252 if (fRunLoader->LoadgAlice() != 0)
253 Warning(kEH, "failed loading gAlice via run-loader.");
255 if (fRunLoader->LoadHeader() == 0)
258 runNo = fRunLoader->GetHeader()->GetRun();
262 Warning(kEH, "failed loading run-loader's header.");
267 else // run-loader open failed
269 Warning(kEH, "failed opening ALICE run-loader from '%s'.", gaPath.Data());
272 else // galice not readable
274 Warning(kEH, "can not read '%s'.", gaPath.Data());
278 if (fgAssertRunLoader)
279 throw (kEH + "Bootstraping of run-loader failed. Its precence was requested.");
281 Warning(kEH, "Bootstraping of run-loader failed.");
284 // Open raw-data file
286 TString rawPath(Form("%s/%s", fPath.Data(), fgRawFileName.Data()));
287 // If i use open directly, raw-reader reports an error but i have
288 // no way to detect it.
289 // Is this (AccessPathName check) ok for xrootd / alien? Yes, not for http.
290 if (gSystem->AccessPathName(rawPath, kReadPermission) == kFALSE)
292 fRawReader = AliRawReader::Create(rawPath);
296 fRawReader = AliRawReader::Create(fgRawFileName);
302 throw (kEH + "raw-data not initialized. Its precence was requested.");
304 Warning(kEH, "raw-data not initialized.");
312 fRawReader->NextEvent();
313 runNo = fRawReader->GetRunNumber();
314 Info(kEH, "Determining run-no from raw ... run=%d.", runNo);
315 fRawReader->RewindEvents();
317 throw (kEH + "unknown run number.");
322 AliCDBManager* cdb = AliCDBManager::Instance();
323 cdb->SetDefaultStorage(fgCdbUri);
324 if (cdb->IsDefaultStorageSet() == kFALSE)
325 throw (kEH + "CDB initialization failed.");
329 SetName(Form("Event %d", fEventId));
333 void AliEveEventManager::SetEvent(AliRunLoader *runLoader, AliRawReader *rawReader, AliESDEvent *esd)
335 // Set an event from an external source
336 // The method is used in the online visualisation
337 fRunLoader = runLoader;
338 fRawReader = rawReader;
341 SetTitle("Online event in memory");
342 SetName("Online Event");
345 AfterNewEventLoaded();
348 Int_t AliEveEventManager::GetMaxEventId(Bool_t /*refreshESD*/) const
350 // Returns maximum available event id.
351 // If raw-data is the only data-source this can not be known
352 // and 10,000,000 is returned.
353 // If neither data-source is initialised an exception is thrown.
354 // If refresh_esd is true and ESD is the primary event-data source
355 // its header is re-read from disk.
357 static const TEveException kEH("AliEveEventManager::GetMaxEventId ");
361 // Refresh crashes with root-5.21.1-alice.
362 // Fixed by Philippe 5.8.2008 r25053, can be reactivated
363 // when we move to a newer root.
365 // fESDTree->Refresh();
366 return fESDTree->GetEntries() - 1;
370 return fRunLoader->GetNumberOfEvents() - 1;
374 Int_t n = fRawReader->GetNumberOfEvents() - 1;
375 return n > -1 ? n : 10000000;
379 throw (kEH + "neither RunLoader, ESD nor Raw loaded.");
383 void AliEveEventManager::GotoEvent(Int_t event)
385 // Load data for specified event.
386 // If event is out of range an exception is thrown and old state
388 // After successful loading of event, the virtual function
389 // AfterNewEventLoaded() is called. This executes commands that
390 // were registered via TEveEventManager::AddNewEventCommand().
392 // If event is negative, it is subtracted from the number of
393 // available events, thus passing -1 will load the last event.
394 // This is not supported when raw-data is the only data-source
395 // as the number of events is not known.
397 static const TEveException kEH("AliEveEventManager::GotoEvent ");
402 // Refresh crashes with root-5.21.1-alice.
403 // Fixed by Philippe 5.8.2008 r25053, can be reactivated
404 // when we move to a newer root.
405 // fESDTree->Refresh();
406 maxEvent = fESDTree->GetEntries() - 1;
408 event = fESDTree->GetEntries() + event;
412 maxEvent = fRunLoader->GetNumberOfEvents() - 1;
414 event = fRunLoader->GetNumberOfEvents() + event;
418 maxEvent = fRawReader->GetNumberOfEvents() - 1;
423 Error(kEH, "current raw-data source does not support direct event access.");
426 Info(kEH, "number of events unknown for current raw-data source, setting max-event id to 10M.");
431 event = fRawReader->GetNumberOfEvents() + event;
436 throw (kEH + "neither RunLoader, ESD nor Raw loaded.");
438 if (event < 0 || event > maxEvent)
440 throw (kEH + Form("event %d not present, available range [%d, %d].",
441 event, 0, maxEvent));
444 TEveManager::TRedrawDisabler rd(gEve);
445 gEve->Redraw3D(kFALSE, kTRUE); // Enforce drop of all logicals.
447 // !!! MT this is somewhat brutal; at least optionally, one could be
448 // a bit gentler, checking for objs owning their external refs and having
449 // additinal parents.
453 if (fESDTree->GetEntry(event) <= 0)
454 throw (kEH + "failed getting required event from ESD.");
456 if (fESDfriendExists)
457 fESD->SetESDfriend(fESDfriend);
461 if (fRunLoader->GetEvent(event) != 0)
462 throw (kEH + "failed getting required event.");
467 // AliRawReader::GotoEvent(Int_t) works for AliRawReaderRoot/Chain.
468 if (fRawReader->GotoEvent(event) == kFALSE)
470 // Use fallback method - iteration with NextEvent().
471 Int_t rawEv = fEventId;
474 fRawReader->RewindEvents();
478 while (rawEv < event)
480 if ( ! fRawReader->NextEvent())
482 fRawReader->RewindEvents();
484 throw (kEH + Form("Error going to next raw-event from event %d.", rawEv));
488 Warning(kEH, "Loaded raw-event %d with fallback method.\n", rawEv);
493 SetName(Form("Event %d", fEventId));
496 AfterNewEventLoaded();
499 void AliEveEventManager::NextEvent()
502 // either in automatic (online) or
507 if (fAutoLoadTimer) fAutoLoadTimer->Stop();
515 if (fEventId < GetMaxEventId(kTRUE))
516 GotoEvent(fEventId + 1);
519 StartStopAutoLoadTimer();
523 void AliEveEventManager::PrevEvent()
525 // Loads previous event
526 // only in case of manual mode
528 GotoEvent(fEventId - 1);
529 StartStopAutoLoadTimer();
533 void AliEveEventManager::Close()
535 // Close the event data-files and delete ESD, ESDfriend, run-loader
539 delete fESD; fESD = 0;
540 delete fESDfriend; fESDfriend = 0;
542 delete fESDTree; fESDTree = 0;
543 delete fESDFile; fESDFile = 0;
547 delete fRunLoader; fRunLoader = 0;
551 delete fRawReader; fRawReader = 0;
556 /******************************************************************************/
557 // Static convenience functions, mainly used from macros.
558 /******************************************************************************/
560 Bool_t AliEveEventManager::HasRunLoader()
562 // Check if AliRunLoader is initialized.
564 return gAliEveEvent && gAliEveEvent->fRunLoader;
567 Bool_t AliEveEventManager::HasESD()
569 // Check if AliESDEvent is initialized.
571 return gAliEveEvent && gAliEveEvent->fESD;
574 Bool_t AliEveEventManager::HasESDfriend()
576 // Check if AliESDfriend is initialized.
578 return gAliEveEvent && gAliEveEvent->fESDfriend;
581 Bool_t AliEveEventManager::HasRawReader()
583 // Check if raw-reader is initialized.
585 return gAliEveEvent && gAliEveEvent->fRawReader;
588 AliRunLoader* AliEveEventManager::AssertRunLoader()
590 // Make sure AliRunLoader is initialized and return it.
591 // Throws exception in case run-loader is not available.
592 // Static utility for macros.
594 static const TEveException kEH("AliEveEventManager::AssertRunLoader ");
596 if (gAliEveEvent == 0)
597 throw (kEH + "ALICE event not ready.");
598 if (gAliEveEvent->fRunLoader == 0)
599 throw (kEH + "AliRunLoader not initialised.");
600 return gAliEveEvent->fRunLoader;
603 AliESDEvent* AliEveEventManager::AssertESD()
605 // Make sure AliESDEvent is initialized and return it.
606 // Throws exception in case ESD is not available.
607 // Static utility for macros.
609 static const TEveException kEH("AliEveEventManager::AssertESD ");
611 if (gAliEveEvent == 0)
612 throw (kEH + "ALICE event not ready.");
613 if (gAliEveEvent->fESD == 0)
614 throw (kEH + "AliESD not initialised.");
615 return gAliEveEvent->fESD;
618 AliESDfriend* AliEveEventManager::AssertESDfriend()
620 // Make sure AliESDfriend is initialized and return it.
621 // Throws exception in case ESDfriend-loader is not available.
622 // Static utility for macros.
624 static const TEveException kEH("AliEveEventManager::AssertESDfriend ");
626 if (gAliEveEvent == 0)
627 throw (kEH + "ALICE event not ready.");
628 if (gAliEveEvent->fESDfriend == 0)
629 throw (kEH + "AliESDfriend not initialised.");
630 return gAliEveEvent->fESDfriend;
633 AliRawReader* AliEveEventManager::AssertRawReader()
635 // Make sure raw-reader is initialized and return it.
637 static const TEveException kEH("AliEveEventManager::AssertRawReader ");
639 if (gAliEveEvent == 0)
640 throw (kEH + "ALICE event not ready.");
641 if (gAliEveEvent->fRawReader == 0)
642 throw (kEH + "RawReader not ready.");
644 return gAliEveEvent->fRawReader;
647 AliMagF* AliEveEventManager::AssertMagField()
649 // Make sure AliMagF is initialized and return it.
650 // Throws exception in case magnetic field is not available.
651 // Static utility for macros.
655 if (gAliEveEvent && gAliEveEvent->fRunLoader && gAliEveEvent->fRunLoader->GetAliRun())
656 fgMagField = gAliEveEvent->fRunLoader->GetAliRun()->Field();
658 fgMagField = new AliMagFMaps("Maps","Maps", 1, 1., 10., AliMagFMaps::k5kG);
663 TGeoManager* AliEveEventManager::AssertGeometry()
665 // Make sure AliGeomManager is initialized and returns the
666 // corresponding TGeoManger.
667 // gGeoManager is set to the return value.
668 // Throws exception if geometry can not be loaded or if it is not
669 // available and the TGeoManager is locked.
670 // Static utility for macros.
672 static const TEveException kEH("AliEveEventManager::AssertGeometry ");
674 if (AliGeomManager::GetGeometry() == 0)
676 if (TGeoManager::IsLocked())
677 throw (kEH + "geometry is not loaded but TGeoManager is locked.");
680 AliGeomManager::LoadGeometry();
681 if ( ! AliGeomManager::GetGeometry())
683 throw (kEH + "can not load geometry.");
685 if ( ! AliGeomManager::ApplyAlignObjsFromCDB("ITS TPC TRD TOF PHOS HMPID EMCAL MUON FMD ZDC PMD T0 VZERO ACORDE"))
687 ::Warning(kEH, "mismatch of alignable volumes. Proceeding.");
688 // throw (kEH + "could not apply align objs.");
690 AliGeomManager::GetGeometry()->DefaultColors();
693 gGeoManager = AliGeomManager::GetGeometry();
697 void AliEveEventManager::SetAutoLoad(Bool_t autoLoad)
699 // Set the automatic event loading mode
700 fAutoLoad = autoLoad;
701 StartStopAutoLoadTimer();
704 void AliEveEventManager::SetAutoLoadTime(Double_t time)
706 // Set the auto-load time in seconds
707 fAutoLoadTime = time;
708 StartStopAutoLoadTimer();
711 void AliEveEventManager::StartStopAutoLoadTimer()
713 // Create if needed and start
714 // the automatic event loading timer
719 fAutoLoadTimer = new TTimer;
720 fAutoLoadTimer->Connect("Timeout()","AliEveEventManager",this,"NextEvent()");
722 fAutoLoadTimer->Start((Long_t)fAutoLoadTime*1000,kTRUE);
726 if (fAutoLoadTimer) fAutoLoadTimer->Stop();
730 void AliEveEventManager::AfterNewEventLoaded()
732 // Execute registered macros and commands.
733 // At the end emit NewEventLoaded signal.
735 // Virtual from TEveEventManager.
738 fExecutor->ExecMacros();
740 TEveEventManager::AfterNewEventLoaded();
745 void AliEveEventManager::NewEventLoaded()
747 // Emit NewEventLoaded signal.
749 Emit("NewEventLoaded()");
752 //==============================================================================
754 TString AliEveEventManager::GetEventInfoHorizontal() const
756 // Dumps the event-header contents in vertical formatting.
758 TString rawInfo, esdInfo;
762 rawInfo = "No raw-data event info is available!\n";
766 const UInt_t* attr = fRawReader->GetAttributes();
767 TTimeStamp ts(fRawReader->GetTimestamp());
768 rawInfo.Form("RAW event info: Run#: %d Event type: %d (%s) Period: %x Orbit: %x BC: %x\n"
769 "Trigger: %llx\nDetectors: %x (%s)\nAttributes:%x-%x-%x Timestamp: %s\n",
770 fRawReader->GetRunNumber(),fRawReader->GetType(),AliRawEventHeaderBase::GetTypeName(fRawReader->GetType()),
771 fRawReader->GetPeriod(),fRawReader->GetOrbitID(),fRawReader->GetBCID(),
772 fRawReader->GetClassMask(),
773 *fRawReader->GetDetectorPattern(),AliDAQ::ListOfTriggeredDetectors(*fRawReader->GetDetectorPattern()),
774 attr[0],attr[1],attr[2], ts.AsString("s"));
779 esdInfo = "No ESD event info is available!";
783 TString acttrclasses = fESD->GetESDRun()->GetActiveTriggerClasses();
784 TString firedtrclasses = fESD->GetFiredTriggerClasses();
785 TTimeStamp ts(fESD->GetTimeStamp());
786 esdInfo.Form("ESD event info: Run#: %d Event type: %d (%s) Period: %x Orbit: %x BC: %x\n"
787 "Active trigger classes: %s\nTrigger: %llx (%s)\nEvent# in file: %d Timestamp: %s",
788 fESD->GetRunNumber(),
789 fESD->GetEventType(),AliRawEventHeaderBase::GetTypeName(fESD->GetEventType()),
790 fESD->GetPeriodNumber(),fESD->GetOrbitNumber(),fESD->GetBunchCrossNumber(),
792 fESD->GetTriggerMask(),firedtrclasses.Data(),
793 fESD->GetEventNumberInFile(), ts.AsString("s"));
796 return rawInfo + esdInfo;
799 TString AliEveEventManager::GetEventInfoVertical() const
801 // Dumps the event-header contents in vertical formatting.
803 TString rawInfo, esdInfo;
807 rawInfo = "No raw-data event info is available!\n";
811 const UInt_t* attr = fRawReader->GetAttributes();
812 rawInfo.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",
813 fRawReader->GetRunNumber(),fRawReader->GetType(),AliRawEventHeaderBase::GetTypeName(fRawReader->GetType()),
814 fRawReader->GetPeriod(),fRawReader->GetOrbitID(),fRawReader->GetBCID(),
815 fRawReader->GetClassMask(),
816 *fRawReader->GetDetectorPattern(),AliDAQ::ListOfTriggeredDetectors(*fRawReader->GetDetectorPattern()),
817 attr[0],attr[1],attr[2],
818 fRawReader->GetTimestamp());
823 esdInfo = "No ESD event info is available!\n";
827 TString acttrclasses = fESD->GetESDRun()->GetActiveTriggerClasses();
828 TString firedtrclasses = fESD->GetFiredTriggerClasses();
829 esdInfo.Form("ESD 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",
830 fESD->GetRunNumber(),
832 fESD->GetEventType(),AliRawEventHeaderBase::GetTypeName(fESD->GetEventType()),
833 fESD->GetPeriodNumber(),fESD->GetOrbitNumber(),fESD->GetBunchCrossNumber(),
834 fESD->GetTriggerMask(),firedtrclasses.Data(),
835 fESD->GetEventNumberInFile(),
836 fESD->GetTimeStamp());
839 return rawInfo + "\n" + esdInfo;