]> git.uio.no Git - u/mrichter/AliRoot.git/blob - EVE/EveBase/AliEveEventManager.cxx
Generator for dimuon decays of low masses resonances in AliRoot.
[u/mrichter/AliRoot.git] / EVE / EveBase / AliEveEventManager.cxx
1 // $Id$
2 // Main authors: Matevz Tadel & Alja Mrak-Tadel: 2006, 2007
3
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  **************************************************************************/
9
10 #include "AliEveEventManager.h"
11 #include "AliEveEventSelector.h"
12 #include "AliEveMacroExecutor.h"
13 #include <TEveElement.h>
14 #include <TEveManager.h>
15 #include <TEveViewer.h>
16
17 #include <AliRunLoader.h>
18 #include <AliRun.h>
19 #include <AliESDRun.h>
20 #include <AliESDEvent.h>
21 #include <AliESDfriend.h>
22 #include <AliAODEvent.h>
23
24 #include <AliDAQ.h>
25 #include <AliRawEventHeaderBase.h>
26 #include <AliRawReaderRoot.h>
27 #include <AliRawReaderFile.h>
28 #include <AliRawReaderDate.h>
29 #include <AliMagF.h>
30 #include <AliCDBManager.h>
31 #include <AliCDBStorage.h>
32 #include <AliGRPObject.h>
33 #include <AliHeader.h>
34 #include <AliGeomManager.h>
35 #include <AliGRPManager.h>
36 #include <AliSysInfo.h>
37
38 #include <TFile.h>
39 #include <TTree.h>
40 #include <TGeoManager.h>
41 #include <TGeoGlobalMagField.h>
42 #include <TSystem.h>
43 #include <TTimeStamp.h>
44 #include <TPRegexp.h>
45 #include <TError.h>
46 #include <TEnv.h>
47 #include <TString.h>
48 #include <TMap.h>
49
50 //==============================================================================
51 //==============================================================================
52 // AliEveEventManager
53 //==============================================================================
54
55 //______________________________________________________________________________
56 //
57 // Provides interface for loading and navigating standard AliRoot data
58 // (AliRunLoader), ESD, AOD and RAW.
59 //
60 // ESDfriend is attached automatically, if the file is found.
61 //
62 // AODfriends are not attached automatically as there are several
63 // possible files involved. To have a specific AODfriend attached, call
64 // static method
65 //   AliEveEventManager::AddAODfriend("AliAOD.VertexingHF.root");
66 // before initializing the event-manager.
67 //
68 // Also provides interface to magnetic-field and geometry. Mostly
69 // intended as wrappers over standard AliRoot functionality for
70 // convenient use from visualizateion macros.
71 //
72 // There can be a single main event-manger, it is stored in private
73 // data member fgMaster and can be accessed via static member function
74 // GetMaster().
75 //
76 // For event overlaying and embedding one can instantiate additional
77 // event-managers via static method AddDependentManager(const TString& path).
78 // This interface is under development.
79
80 ClassImp(AliEveEventManager)
81
82 Bool_t AliEveEventManager::fgAssertRunLoader = kFALSE;
83 Bool_t AliEveEventManager::fgAssertESD       = kFALSE;
84 Bool_t AliEveEventManager::fgAssertAOD       = kFALSE;
85 Bool_t AliEveEventManager::fgAssertRaw       = kFALSE;
86
87 TString  AliEveEventManager::fgESDFileName("AliESDs.root");
88 TString  AliEveEventManager::fgAODFileName("AliAOD.root");
89 TString  AliEveEventManager::fgRawFileName("raw.root");
90 TString  AliEveEventManager::fgCdbUri;
91
92 TList*   AliEveEventManager::fgAODfriends = 0;
93
94 Bool_t   AliEveEventManager::fgRawFromStandardLoc = kFALSE;
95
96 Bool_t   AliEveEventManager::fgGRPLoaded    = kFALSE;
97 AliMagF* AliEveEventManager::fgMagField     = 0;
98 Bool_t   AliEveEventManager::fgUniformField = kFALSE;
99
100 AliEveEventManager* AliEveEventManager::fgMaster  = 0;
101 AliEveEventManager* AliEveEventManager::fgCurrent = 0;
102
103 void AliEveEventManager::InitInternals()
104 {
105   // Initialize internal members.
106
107   static const TEveException kEH("AliEveEventManager::InitInternals ");
108
109   if (fgCurrent != 0)
110   {
111     throw(kEH + "Dependent event-managers should be created via static method AddDependentManager().");
112   }
113
114   if (fgMaster == 0)
115   {
116     fgMaster = this;
117   }
118
119   fgCurrent = this;
120
121   fAutoLoadTimer = new TTimer;
122   fAutoLoadTimer->Connect("Timeout()", "AliEveEventManager", this, "AutoLoadNextEvent()");
123
124   fExecutor = new AliEveMacroExecutor;
125
126   fTransients = new TEveElementList("Transients", "Transient per-event elements.");
127   fTransients->IncDenyDestroy();
128   gEve->AddToListTree(fTransients, kFALSE);
129
130   fTransientLists = new TEveElementList("Transient Lists", "Containers of transient elements.");
131   fTransientLists->IncDenyDestroy();
132   gEve->AddToListTree(fTransientLists, kFALSE);
133
134   fPEventSelector = new AliEveEventSelector(this);
135
136   fGlobal = new TMap; fGlobal->SetOwnerKeyValue();
137 }
138
139 AliEveEventManager::AliEveEventManager(const TString& name) :
140   TEveEventManager(name),
141
142   fPath      ( ), fEventId (-1),
143   fRunLoader (0),
144   fESDFile   (0), fESDTree (0), fESD (0),
145   fESDfriend (0), fESDfriendExists(kFALSE),
146   fAODFile   (0), fAODTree (0), fAOD (0),
147   fRawReader (0),
148   fAutoLoad  (kFALSE), fAutoLoadTime (5.),     fAutoLoadTimer(0),
149   fIsOpen    (kFALSE), fHasEvent     (kFALSE), fExternalCtrl (kFALSE),
150   fGlobal    (0), fGlobalReplace (kTRUE), fGlobalUpdate (kTRUE),
151   fExecutor    (0), fTransients(0), fTransientLists(0),
152   fPEventSelector(0),
153   fSubManagers (0),
154   fAutoLoadTimerRunning(kFALSE)
155 {
156   // Default constructor.
157
158   InitInternals();
159 }
160
161 AliEveEventManager::AliEveEventManager(const TString& name, const TString& path, Int_t ev) :
162   TEveEventManager(name, path),
163
164   fPath   (path), fEventId(-1),
165   fRunLoader (0),
166   fESDFile   (0), fESDTree (0), fESD (0),
167   fESDfriend (0), fESDfriendExists(kFALSE),
168   fAODFile   (0), fAODTree (0), fAOD (0),
169   fRawReader (0),
170   fAutoLoad  (kFALSE), fAutoLoadTime (5),      fAutoLoadTimer(0),
171   fIsOpen    (kFALSE), fHasEvent     (kFALSE), fExternalCtrl (kFALSE),
172   fGlobal    (0), fGlobalReplace (kTRUE), fGlobalUpdate (kTRUE),
173   fExecutor    (0), fTransients(0), fTransientLists(0),
174   fPEventSelector(0),
175   fSubManagers (0),
176   fAutoLoadTimerRunning(kFALSE)
177 {
178   // Constructor with event-directory URL and event-id.
179
180   InitInternals();
181
182   Open();
183   if (ev >= 0)
184   {
185     GotoEvent(ev);
186   }
187 }
188
189 AliEveEventManager::~AliEveEventManager()
190 {
191   // Destructor.
192
193   delete fSubManagers;
194
195   if (fIsOpen)
196   {
197     Close();
198   }
199
200   fTransients->DecDenyDestroy();
201   fTransients->Destroy();
202
203   fTransientLists->DecDenyDestroy();
204   fTransientLists->Destroy();
205 }
206
207 /******************************************************************************/
208
209 void AliEveEventManager::SetESDFileName(const TString& esd)
210 {
211   // Set file-name for opening ESD, default "AliESDs.root".
212
213   if ( ! esd.IsNull()) fgESDFileName = esd;
214 }
215
216 void AliEveEventManager::SetAODFileName(const TString& aod)
217 {
218   // Set file-name for opening AOD, default "AliAOD.root".
219
220   if ( ! aod.IsNull()) fgAODFileName = aod;
221 }
222
223 void AliEveEventManager::AddAODfriend(const TString& friendFileName)
224 {
225   // Add new AOD friend file-name to be attached when opening AOD.
226   // This should include '.root', as in 'AliAOD.VertexingHF.root'.
227
228   if (fgAODfriends == 0)
229   {
230     fgAODfriends = new TList;
231     fgAODfriends->SetOwner(kTRUE);
232   }
233   if (fgAODfriends->FindObject(friendFileName) == 0)
234   {
235     fgAODfriends->Add(new TObjString(friendFileName));
236   }
237 }
238
239 void AliEveEventManager::SetRawFileName(const TString& raw)
240 {
241   // Set file-name for opening of raw-data, default "raw.root"
242   if ( ! raw.IsNull()) fgRawFileName = raw;
243 }
244
245 void AliEveEventManager::SetCdbUri(const TString& cdb)
246 {
247   // Set path to CDB, there is no default.
248
249   if ( ! cdb.IsNull()) fgCdbUri = cdb;
250 }
251
252 void AliEveEventManager::SetAssertElements(Bool_t assertRunloader, Bool_t assertEsd,
253                                            Bool_t assertAod, Bool_t assertRaw)
254 {
255   // Set global flags that detrmine which parts of the event-data must
256   // be present when the event is opened.
257
258   fgAssertRunLoader = assertRunloader;
259   fgAssertESD = assertEsd;
260   fgAssertAOD = assertAod;
261   fgAssertRaw = assertRaw;
262 }
263
264 void AliEveEventManager::SearchRawForCentralReconstruction()
265 {
266   // Enable searching of raw data in standard location. The path passed to
267   // Open() is expected to point to a centrally reconstructed run, e.g.:
268   // "alien:///alice/data/2009/LHC09c/000101134/ESDs/pass1/09000101134018.10".
269
270   fgRawFromStandardLoc = kTRUE;
271 }
272
273 /******************************************************************************/
274
275 void AliEveEventManager::Open()
276 {
277   // Open event-data from URL specified in fPath.
278   // Attempts to create AliRunLoader() and to open ESD with ESDfriends.
279   // Warning is reported if run-loader or ESD is not found.
280   // Global data-members fgAssertRunLoader and fgAssertESD can be set
281   // to throw exceptions instead.
282
283   static const TEveException kEH("AliEveEventManager::Open ");
284
285   if (fExternalCtrl)
286   {
287     throw (kEH + "Event-loop is under external control.");
288   }
289   if (fIsOpen)
290   {
291     throw (kEH + "Event-files already opened.");
292   }
293
294   gSystem->ExpandPathName(fPath);
295   // The following magick is required for ESDfriends to be loaded properly
296   // from non-current directory.
297   if (fPath.IsNull() || fPath == ".")
298   {
299     fPath = gSystem->WorkingDirectory();
300   }
301   else if ( ! fPath.BeginsWith("file:/"))
302   {
303     TUrl    url(fPath, kTRUE);
304     TString protocol(url.GetProtocol());
305     if (protocol == "file" && fPath[0] != '/')
306       fPath = Form("%s/%s", gSystem->WorkingDirectory(), fPath.Data());
307   }
308
309   Int_t runNo = -1;
310
311   // Open ESD and ESDfriends
312
313   TString esdPath(Form("%s/%s", fPath.Data(), fgESDFileName.Data()));
314   if (fPath.EndsWith(".zip")) esdPath.Form("%s#%s",fPath.Data(),fgESDFileName.Data());
315   if ((fESDFile = TFile::Open(esdPath)))
316   {
317     fESD = new AliESDEvent();
318     fESDTree = (TTree*) fESDFile->Get("esdTree");
319     if (fESDTree != 0)
320     {
321       // Check if ESDfriends exists and attach the branch.
322       // We use TFile::Open() instead of gSystem->AccessPathName
323       // as it seems to work better when attachine alieve to a
324       // running reconstruction process with auto-save on.
325       TString p(Form("%s/AliESDfriends.root", fPath.Data()));
326       if (fPath.EndsWith(".zip")) p.Form("%s#AliESDfriends.root",fPath.Data());
327       TFile *esdFriendFile = TFile::Open(p);
328       if (esdFriendFile)
329       {
330         if (!esdFriendFile->IsZombie())
331         {
332           esdFriendFile->Close();
333           fESDfriendExists = kTRUE;
334           fESDTree->SetBranchStatus ("ESDfriend*", 1);
335         }
336         delete esdFriendFile;
337       }
338
339       fESD->ReadFromTree(fESDTree);
340       if (fESDfriendExists)
341       {
342         fESDfriend = (AliESDfriend*) fESD->FindListObject("AliESDfriend");
343         Info(kEH, "found and attached ESD friend.");
344       }
345       else
346       {
347         Warning(kEH, "ESDfriend not found.");
348       }
349
350       if (fESDTree->GetEntry(0) <= 0)
351       {
352         delete fESDFile; fESDFile = 0;
353         delete fESD; fESD = 0;
354         Warning(kEH, "failed getting the first entry from esdTree.");
355       }
356       else
357       {
358         if (runNo < 0)
359           runNo = fESD->GetESDRun()->GetRunNumber();
360       }
361     }
362     else // esdtree == 0
363     {
364       delete fESDFile; fESDFile = 0;
365       delete fESD; fESD = 0;
366       Warning(kEH, "failed getting the esdTree.");
367     }
368   }
369   else // esd not readable
370   {
371     Warning(kEH, "can not read ESD file '%s'.", esdPath.Data());
372   }
373   if (fESDTree == 0)
374   {
375     if (fgAssertESD)
376     {
377       throw (kEH + "ESD not initialized. Its precence was requested.");
378     } else {
379       Warning(kEH, "ESD not initialized.");
380     }
381   }
382
383   // Open AOD and registered friends
384
385   TString aodPath(Form("%s/%s", fPath.Data(), fgAODFileName.Data()));
386   if (fPath.EndsWith(".zip")) aodPath.Form("%s#%s",fPath.Data(),fgAODFileName.Data());
387   if ((fAODFile = TFile::Open(aodPath)))
388   {
389     fAOD = new AliAODEvent();
390     fAODTree = (TTree*) fAODFile->Get("aodTree");
391     if (fAODTree != 0)
392     {
393       // Check if AODfriends exist and attach them.
394       TIter       friends(fgAODfriends);
395       TObjString *name;
396       while ((name = (TObjString*) friends()) != 0)
397       {
398         TString p(Form("%s/%s", fPath.Data(), name->GetName()));
399         if (fPath.EndsWith(".zip")) p.Form("%s#%s",fPath.Data(),name->GetName());
400         if (gSystem->AccessPathName(p, kReadPermission) == kFALSE)
401         {
402           fAODTree->AddFriend("aodTree", name->GetName());
403         }
404       }
405
406       fAOD->ReadFromTree(fAODTree);
407
408       if (fAODTree->GetEntry(0) <= 0)
409       {
410         delete fAODFile; fAODFile = 0;
411         delete fAOD;     fAOD     = 0;
412         Warning(kEH, "failed getting the first entry from addTree.");
413       }
414       else
415       {
416         if (runNo < 0)
417           runNo = fAOD->GetRunNumber();
418       }
419     }
420     else // aodtree == 0
421     {
422       delete fAODFile; fAODFile = 0;
423       delete fAOD;     fAOD     = 0;
424       Warning(kEH, "failed getting the aodTree.");
425     }
426   }
427   else // aod not readable
428   {
429     Warning(kEH, "can not read AOD file '%s'.", aodPath.Data());
430   }
431   if (fAODTree == 0)
432   {
433     if (fgAssertAOD)
434     {
435       throw (kEH + "AOD not initialized. Its precence was requested.");
436     } else {
437       Warning(kEH, "AOD not initialized.");
438     }
439   }
440
441   // Open RunLoader from galice.root
442
443   TString gaPath(Form("%s/galice.root", fPath.Data()));
444   if (fPath.EndsWith(".zip")) gaPath.Form("%s#%s",fPath.Data(),"galice.root");
445   // If i use open directly, we get fatal.
446   // Is AccessPathName check ok for xrootd / alien? Yes, not for http.
447   // Seems not to work for alien anymore.
448   // Fixed in ROOT on 27.10.2009, rev 30888.
449   // To revert after we move to root-5.26.
450   TFile *gafile = TFile::Open(gaPath);
451   if (gafile)
452   {
453     gafile->Close();
454     delete gafile;
455   // if (gSystem->AccessPathName(gaPath, kReadPermission) == kFALSE)
456   // {
457     fRunLoader = AliRunLoader::Open(gaPath, GetName());
458     if (fRunLoader)
459     {
460       TString alicePath = fPath + "/";
461       fRunLoader->SetDirName(alicePath);
462
463       if (fRunLoader->LoadgAlice() != 0)
464         Warning(kEH, "failed loading gAlice via run-loader.");
465
466       if (fRunLoader->LoadHeader() == 0)
467       {
468         if (runNo < 0)
469           runNo = fRunLoader->GetHeader()->GetRun();
470       }
471       else
472       {
473         Warning(kEH, "failed loading run-loader's header.");
474         delete fRunLoader;
475         fRunLoader = 0;
476       }
477     }
478     else // run-loader open failed
479     {
480       Warning(kEH, "failed opening ALICE run-loader from '%s'.", gaPath.Data());
481     }
482   }
483   else // galice not readable
484   {
485     Warning(kEH, "can not read '%s'.", gaPath.Data());
486   }
487   if (fRunLoader == 0)
488   {
489     if (fgAssertRunLoader)
490       throw (kEH + "Bootstraping of run-loader failed. Its precence was requested.");
491     else
492       Warning(kEH, "Bootstraping of run-loader failed.");
493   }
494
495   // Open raw-data file
496
497   TString rawPath;
498   if (fgRawFromStandardLoc)
499   {
500     if (!fPath.BeginsWith("alien:"))
501       throw kEH + "Standard raw search requested, but the directory is not in AliEn.";
502     if (!fPath.Contains("/ESDs/"))
503       throw kEH + "Standard raw search requested, but does not contain 'ESDs' directory.";
504
505     TPMERegexp chunk("/([\\d\\.])+/?$");
506     Int_t nm = chunk.Match(fPath);
507     if (nm != 2)
508       throw kEH + "Standard raw search requested, but the path does not end with chunk-id directory.";
509
510     TPMERegexp esdstrip("/ESDs/.*");
511     rawPath = fPath;
512     esdstrip.Substitute(rawPath, "/raw/");
513     rawPath += chunk[0];
514     rawPath += ".root";
515
516     Info(kEH, "Standard raw search requested, using the following path:\n  %s\n", rawPath.Data());
517   }
518   else
519   {
520     rawPath.Form("%s/%s", fPath.Data(), fgRawFileName.Data());
521   }
522   // If i use open directly, raw-reader reports an error but i have
523   // no way to detect it.
524   // Is this (AccessPathName check) ok for xrootd / alien? Yes, not for http.
525   AliLog::EType_t oldLogLevel = (AliLog::EType_t) AliLog::GetGlobalLogLevel();
526   if (fgAssertRaw == kFALSE)
527   {
528     AliLog::SetGlobalLogLevel(AliLog::kFatal);
529   }
530   if (gSystem->AccessPathName(rawPath, kReadPermission) == kFALSE)
531   {
532     fRawReader = AliRawReader::Create(rawPath);
533   }
534   else
535   {
536     fRawReader = AliRawReader::Create(fgRawFileName);
537   }
538   if (fgAssertRaw == kFALSE)
539   {
540     AliLog::SetGlobalLogLevel(oldLogLevel);
541   }
542
543   if (fRawReader == 0)
544   {
545     if (fgAssertRaw)
546     {
547       throw (kEH + "raw-data not initialized. Its precence was requested.");
548     }
549     else
550     {
551       Warning(kEH, "raw-data not initialized.");
552     }
553   }
554
555   if (runNo < 0)
556   {
557     if (fRawReader)
558     {
559       if ( ! fRawReader->NextEvent())
560       {
561         throw (kEH + "can not go to first event in raw-reader to determine run-id.");
562       }
563       runNo = fRawReader->GetRunNumber();
564       Info(kEH, "Determining run-no from raw ... run=%d.", runNo);
565       fRawReader->RewindEvents();
566     }
567     else 
568     {
569       throw (kEH + "unknown run number.");
570     }
571   }
572
573   // Initialize OCDB ... only in master event-manager
574
575   if (this == fgMaster)
576   {
577     AliCDBManager* cdb = AliCDBManager::Instance();
578     if (cdb->IsDefaultStorageSet() == kTRUE)
579     {
580       Warning(kEH, "CDB already set - using the old storage:\n  '%s'",
581               cdb->GetDefaultStorage()->GetURI().Data());
582     }
583     else
584     {
585       if (fgCdbUri.IsNull())
586       {
587         gEnv->SetValue("Root.Stacktrace", "no");
588         Fatal("Open()", "OCDB path was not specified.");
589       }
590
591       // Handle some special cases for MC (should be in OCDBManager).
592       if (fgCdbUri == "mcideal://")
593         cdb->SetDefaultStorage("MC", "Ideal");
594       else if (fgCdbUri == "mcresidual://")
595         cdb->SetDefaultStorage("MC", "Residual");
596       else if (fgCdbUri == "mcfull://")
597         cdb->SetDefaultStorage("MC", "Full");
598       else if (fgCdbUri == "local://") {
599         fgCdbUri = "local://$ALICE_ROOT/OCDB";
600         cdb->SetDefaultStorage(fgCdbUri);
601       } else
602         cdb->SetDefaultStorage(fgCdbUri);
603
604       cdb->SetRun(runNo);
605
606       if (cdb->IsDefaultStorageSet() == kFALSE)
607         throw kEH + "CDB initialization failed for '" + fgCdbUri + "'.";
608     }
609     
610     if (fgCdbUri.BeginsWith("local://"))
611     {
612       TString grp     = "GRP/GRP/Data";
613       TString grppath = fPath + "/" + grp;
614       if (gSystem->AccessPathName(grppath, kReadPermission) == kFALSE)
615       {
616         if (cdb->GetSpecificStorage(grp))
617         {
618           Warning(kEH, "Local GRP exists, but the specific storage is already set.");
619         }
620         else
621         {
622           Info(kEH, "Setting CDB specific-storage for GRP from event directory.");
623           TString lpath("local://");
624           lpath += fPath;
625           cdb->SetSpecificStorage(grp, lpath);
626         }
627       }
628     }
629   }
630
631   fIsOpen = kTRUE;
632 }
633
634 void AliEveEventManager::SetEvent(AliRunLoader *runLoader, AliRawReader *rawReader, AliESDEvent *esd, AliESDfriend *esdf)
635 {
636   // Set an event from an external source.
637   // The method is used in the online visualisation.
638   // AOD is not supported.
639
640   static const TEveException kEH("AliEveEventManager::SetEvent ");
641
642   if (fIsOpen)
643   {
644     Warning(kEH, "Event-files were open. Closing and switching to external control.");
645     Close();
646   }
647
648   fRunLoader = runLoader;
649   fRawReader = rawReader;
650   fESD       = esd;
651   fESDfriend = esdf;
652   fAOD       = 0;
653
654   fEventId++;
655   fHasEvent     = kTRUE;
656   fExternalCtrl = kTRUE;
657
658   SetTitle("Online event in memory");
659   SetName ("Online Event");
660   ElementChanged();
661
662   AfterNewEventLoaded();
663
664   if (fAutoLoad) StartAutoLoadTimer();
665 }
666
667 Int_t AliEveEventManager::GetMaxEventId(Bool_t refreshESD) const
668 {
669   // Returns maximum available event id.
670   // If under external control or event is not opened -1 is returned.
671   // If raw-data is the only data-source this can not be known
672   // and 10,000,000 is returned.
673   // If neither data-source is initialised an exception is thrown.
674   // If refresh_esd is true and ESD is the primary event-data source
675   // its header is re-read from disk.
676
677   static const TEveException kEH("AliEveEventManager::GetMaxEventId ");
678
679   if (fExternalCtrl || fIsOpen == kFALSE)
680   {
681     return -1;
682   }
683
684   if (fESDTree)
685   {
686     if (refreshESD)
687     {
688        fESDTree->Refresh();
689        fPEventSelector->Update();
690     }
691     return fESDTree->GetEntries() - 1;
692   }
693   else if (fAODTree)
694   {
695     return fAODTree->GetEntries() - 1;
696   }
697   else if (fRunLoader)
698   {
699     return fRunLoader->GetNumberOfEvents() - 1;
700   }
701   else if (fRawReader)
702   {
703     Int_t n = fRawReader->GetNumberOfEvents() - 1;
704     return n > -1 ? n : 10000000;
705   }
706   else
707   {
708     throw (kEH + "neither ESD, AOD, RunLoader nor Raw loaded.");
709   }
710 }
711
712 void AliEveEventManager::GotoEvent(Int_t event)
713 {
714   // Load data for specified event.
715   // If event is out of range an exception is thrown and old state
716   // is preserved.
717   // After successful loading of event, the virtual function
718   // AfterNewEventLoaded() is called. This executes commands that
719   // were registered via TEveEventManager::AddNewEventCommand().
720   //
721   // If event is negative, it is subtracted from the number of
722   // available events, thus passing -1 will load the last event.
723   // This is not supported when raw-data is the only data-source
724   // as the number of events is not known.
725
726   static const TEveException kEH("AliEveEventManager::GotoEvent ");
727
728   if (fAutoLoadTimerRunning)
729   {
730     throw (kEH + "Event auto-load timer is running.");
731   }
732   if (fExternalCtrl)
733   {
734     throw (kEH + "Event-loop is under external control.");
735   }
736   else if (!fIsOpen)
737   {
738     throw (kEH + "Event-files not opened.");
739   }
740
741   fHasEvent = kFALSE;
742
743   Int_t maxEvent = 0;
744   if (fESDTree)
745   {
746     if (event >= fESDTree->GetEntries())
747       fESDTree->Refresh();
748     maxEvent = fESDTree->GetEntries() - 1;
749     if (event < 0)
750       event = fESDTree->GetEntries() + event;
751   }
752   else if (fAODTree)
753   {
754     maxEvent = fAODTree->GetEntries() - 1;
755     if (event < 0)
756       event = fAODTree->GetEntries() + event;
757   }
758   else if (fRunLoader)
759   {
760     maxEvent = fRunLoader->GetNumberOfEvents() - 1;
761     if (event < 0)
762       event = fRunLoader->GetNumberOfEvents() + event;
763   }
764   else if (fRawReader)
765   {
766     maxEvent = fRawReader->GetNumberOfEvents() - 1;
767     if (maxEvent < 0)
768     {
769       maxEvent = 10000000;
770       if (event < 0) {
771         Error(kEH, "current raw-data source does not support direct event access.");
772         return;
773       }
774       Info(kEH, "number of events unknown for current raw-data source, setting max-event id to 10M.");
775     }
776     else
777     {
778       if (event < 0)
779         event = fRawReader->GetNumberOfEvents() + event;
780     }
781   }
782   else
783   {
784     throw (kEH + "neither RunLoader, ESD nor Raw loaded.");
785   }
786   if (event < 0 || event > maxEvent)
787   {
788     throw (kEH + Form("event %d not present, available range [%d, %d].",
789                       event, 0, maxEvent));
790   }
791
792   TString sysInfoHeader;
793   sysInfoHeader.Form("AliEveEventManager::GotoEvent(%d) - ", event);
794   AliSysInfo::AddStamp(sysInfoHeader + "Start");
795
796   TEveManager::TRedrawDisabler rd(gEve);
797   gEve->Redraw3D(kFALSE, kTRUE); // Enforce drop of all logicals.
798
799   // !!! MT this is somewhat brutal; at least optionally, one could be
800   // a bit gentler, checking for objs owning their external refs and having
801   // additinal parents.
802   gEve->GetViewers()->DeleteAnnotations();
803   fTransients->DestroyElements();
804   for (TEveElement::List_i i = fTransientLists->BeginChildren();
805        i != fTransientLists->EndChildren(); ++i)
806   {
807     (*i)->DestroyElements();
808   }
809   DestroyElements();
810
811   AliSysInfo::AddStamp(sysInfoHeader + "PostDestroy");
812
813   if (fESDTree) {
814     if (fESDTree->GetEntry(event) <= 0)
815       throw (kEH + "failed getting required event from ESD.");
816
817     if (fESDfriendExists)
818       fESD->SetESDfriend(fESDfriend);
819   }
820
821   if (fAODTree) {
822     if (fAODTree->GetEntry(event) <= 0)
823       throw (kEH + "failed getting required event from AOD.");
824   }
825
826   if (fRunLoader) {
827     if (fRunLoader->GetEvent(event) != 0)
828       throw (kEH + "failed getting required event.");
829   }
830
831   if (fRawReader)
832   {
833     // AliRawReader::GotoEvent(Int_t) works for AliRawReaderRoot/Chain.
834     if (fRawReader->GotoEvent(event) == kFALSE)
835     {
836       // Use fallback method - iteration with NextEvent().
837       Int_t rawEv = fEventId;
838       if (event < rawEv)
839       {
840         fRawReader->RewindEvents();
841         rawEv = -1;
842       }
843
844       while (rawEv < event)
845       {
846         if ( ! fRawReader->NextEvent())
847         {
848           fRawReader->RewindEvents();
849           fEventId = -1;
850           throw (kEH + Form("Error going to next raw-event from event %d.", rawEv));
851         }
852         ++rawEv;
853       }
854       Warning(kEH, "Loaded raw-event %d with fallback method.\n", rawEv);
855     }
856   }
857
858   fHasEvent = kTRUE;
859   fEventId  = event;
860   if (this == fgMaster)
861   {
862     SetName(Form("Event %d", fEventId));
863     ElementChanged();
864   }
865
866   AliSysInfo::AddStamp(sysInfoHeader + "PostLoadEvent");
867
868   AfterNewEventLoaded();
869
870   AliSysInfo::AddStamp(sysInfoHeader + "PostUserActions");
871 }
872
873 void AliEveEventManager::NextEvent()
874 {
875   // Loads next event.
876   // Does magick needed for online display when under external event control.
877
878   static const TEveException kEH("AliEveEventManager::NextEvent ");
879
880   if (fAutoLoadTimerRunning)
881   {
882     throw (kEH + "Event auto-load timer is running.");
883   }
884
885   if (fExternalCtrl)
886   {
887     // !!! This should really go somewhere else. It is done in GotoEvent(),
888     // so here we should do it in SetEvent().
889     DestroyElements();
890
891     gSystem->ExitLoop();
892   }
893   else if (fESDTree)
894   {
895     Int_t nextevent=0;
896     if (fPEventSelector->FindNext(nextevent))
897     {
898       GotoEvent(nextevent);
899     }
900   }
901   else if (fEventId < GetMaxEventId(kTRUE))
902   {
903     GotoEvent(fEventId + 1);
904   }
905 }
906
907 void AliEveEventManager::PrevEvent()
908 {
909   // Loads previous event.
910
911   static const TEveException kEH("AliEveEventManager::PrevEvent ");
912
913   if (fAutoLoadTimerRunning)
914   {
915     throw (kEH + "Event auto-load timer is running.");
916   }
917   if (fExternalCtrl)
918   {
919     throw (kEH + "Event-loop is under external control.");
920   }
921
922   if (fESDTree)
923   {
924     Int_t nextevent=0;
925     if (fPEventSelector->FindPrev(nextevent))
926     {
927       GotoEvent(nextevent);
928     }
929   }
930   else if (fEventId > 0)
931   {
932     GotoEvent(fEventId - 1);
933   }
934 }
935
936 void AliEveEventManager::Close()
937 {
938   // Close the event data-files and delete ESD, ESDfriend, run-loader
939   // and raw-reader.
940
941   static const TEveException kEH("AliEveEventManager::Close ");
942
943   if (!fIsOpen)
944   {
945     throw (kEH + "Event-files not opened.");
946   }
947
948   if (fAutoLoadTimerRunning)
949     StopAutoLoadTimer();
950
951   if (fESDTree) {
952     delete fESD;       fESD       = 0;
953     // delete fESDfriend; // friend tree is deleted with the tree
954     fESDfriend = 0;
955     fESDfriendExists = kFALSE;
956
957     delete fESDTree;   fESDTree = 0;
958     delete fESDFile;   fESDFile = 0;
959   }
960
961   if (fAODTree) {
962     delete fAOD;       fAOD       = 0;
963
964     delete fAODTree;   fAODTree = 0;
965     delete fAODFile;   fAODFile = 0;
966   }
967
968   if (fRunLoader) {
969     delete fRunLoader; fRunLoader = 0;
970   }
971
972   if (fRawReader) {
973     delete fRawReader; fRawReader = 0;
974   }
975
976   fEventId  = -1;
977   fIsOpen   = kFALSE;
978   fHasEvent = kFALSE;
979 }
980
981
982 //------------------------------------------------------------------------------
983 // Static convenience functions, mainly used from macros.
984 //------------------------------------------------------------------------------
985
986 Int_t AliEveEventManager::CurrentEventId()
987 {
988   // Return current event-id.
989
990   static const TEveException kEH("AliEveEventManager::CurrentEventId ");
991
992   if (fgCurrent == 0 || fgCurrent->fHasEvent == kFALSE)
993     throw (kEH + "ALICE event not ready.");
994   return fgCurrent->GetEventId();
995 }
996
997 Bool_t AliEveEventManager::HasRunLoader()
998 {
999   // Check if AliRunLoader is initialized.
1000
1001   return fgCurrent && fgCurrent->fHasEvent && fgCurrent->fRunLoader;
1002 }
1003
1004 Bool_t AliEveEventManager::HasESD()
1005 {
1006   // Check if AliESDEvent is initialized.
1007
1008   return fgCurrent && fgCurrent->fHasEvent && fgCurrent->fESD;
1009 }
1010
1011 Bool_t AliEveEventManager::HasESDfriend()
1012 {
1013   // Check if AliESDfriend is initialized.
1014
1015   return fgCurrent && fgCurrent->fHasEvent && fgCurrent->fESDfriend;
1016 }
1017
1018 Bool_t AliEveEventManager::HasAOD()
1019 {
1020   // Check if AliESDEvent is initialized.
1021
1022   return fgCurrent && fgCurrent->fHasEvent && fgCurrent->fAOD;
1023 }
1024
1025 Bool_t AliEveEventManager::HasRawReader()
1026 {
1027   // Check if raw-reader is initialized.
1028
1029   return fgCurrent && fgCurrent->fHasEvent && fgCurrent->fRawReader;
1030 }
1031
1032 AliRunLoader* AliEveEventManager::AssertRunLoader()
1033 {
1034   // Make sure AliRunLoader is initialized and return it.
1035   // Throws exception in case run-loader is not available.
1036   // Static utility for macros.
1037
1038   static const TEveException kEH("AliEveEventManager::AssertRunLoader ");
1039
1040   if (fgCurrent == 0 || fgCurrent->fHasEvent == kFALSE)
1041     throw (kEH + "ALICE event not ready.");
1042   if (fgCurrent->fRunLoader == 0)
1043     throw (kEH + "AliRunLoader not initialised.");
1044   return fgCurrent->fRunLoader;
1045 }
1046
1047 AliESDEvent* AliEveEventManager::AssertESD()
1048 {
1049   // Make sure AliESDEvent is initialized and return it.
1050   // Throws exception in case ESD is not available.
1051   // Static utility for macros.
1052
1053   static const TEveException kEH("AliEveEventManager::AssertESD ");
1054
1055   if (fgCurrent == 0 || fgCurrent->fHasEvent == kFALSE)
1056     throw (kEH + "ALICE event not ready.");
1057   if (fgCurrent->fESD == 0)
1058     throw (kEH + "AliESD not initialised.");
1059   return fgCurrent->fESD;
1060 }
1061
1062 AliESDfriend* AliEveEventManager::AssertESDfriend()
1063 {
1064   // Make sure AliESDfriend is initialized and return it.
1065   // Throws exception in case ESDfriend-loader is not available.
1066   // Static utility for macros.
1067
1068   static const TEveException kEH("AliEveEventManager::AssertESDfriend ");
1069
1070   if (fgCurrent == 0 || fgCurrent->fHasEvent == kFALSE)
1071     throw (kEH + "ALICE event not ready.");
1072   if (fgCurrent->fESDfriend == 0)
1073     throw (kEH + "AliESDfriend not initialised.");
1074   return fgCurrent->fESDfriend;
1075 }
1076
1077 AliAODEvent* AliEveEventManager::AssertAOD()
1078 {
1079   // Make sure AliAODEvent is initialized and return it.
1080   // Throws exception in case AOD is not available.
1081   // Static utility for macros.
1082
1083   static const TEveException kEH("AliEveEventManager::AssertAOD ");
1084
1085   if (fgCurrent == 0 || fgCurrent->fHasEvent == kFALSE)
1086     throw (kEH + "ALICE event not ready.");
1087   if (fgCurrent->fAOD == 0)
1088     throw (kEH + "AliAOD not initialised.");
1089   return fgCurrent->fAOD;
1090 }
1091
1092 AliRawReader* AliEveEventManager::AssertRawReader()
1093 {
1094   // Make sure raw-reader is initialized and return it.
1095
1096   static const TEveException kEH("AliEveEventManager::AssertRawReader ");
1097
1098   if (fgCurrent == 0 || fgCurrent->fHasEvent == kFALSE)
1099     throw (kEH + "ALICE event not ready.");
1100   if (fgCurrent->fRawReader == 0)
1101     throw (kEH + "RawReader not ready.");
1102
1103   return fgCurrent->fRawReader;
1104 }
1105
1106 //==============================================================================
1107
1108 AliMagF* AliEveEventManager::AssertMagField()    
1109 {        
1110   // Make sure AliMagF is initialized and returns it.    
1111   // Throws exception in case magnetic field is not available.   
1112   // Static utility for macros.          
1113
1114   static const TEveException kEH("AliEveEventManager::AssertMagField ");         
1115                  
1116   if (fgMagField)
1117     return fgMagField;
1118
1119   if (TGeoGlobalMagField::Instance()->GetField())
1120   {
1121     fgMagField = dynamic_cast<AliMagF*>(TGeoGlobalMagField::Instance()->GetField());
1122     if (fgMagField == 0)
1123       throw kEH + "Global field set, but it is not AliMagF.";
1124     return fgMagField;
1125   }
1126
1127   if (!fgGRPLoaded)
1128   {
1129     InitGRP();
1130   }
1131
1132   if (TGeoGlobalMagField::Instance()->GetField())
1133   {
1134     fgMagField = dynamic_cast<AliMagF*>(TGeoGlobalMagField::Instance()->GetField());
1135     if (fgMagField == 0)
1136       throw kEH + "Global field set, but it is not AliMagF.";
1137   }
1138   else
1139   {
1140     throw kEH + "Could not initialize magnetic field.";
1141   }
1142
1143   return fgMagField;     
1144 }
1145
1146 TGeoManager* AliEveEventManager::AssertGeometry()
1147 {
1148   // Make sure AliGeomManager is initialized and returns the
1149   // corresponding TGeoManger.
1150   // gGeoManager is set to the return value.
1151   // Throws exception if geometry can not be loaded or if it is not
1152   // available and the TGeoManager is locked.
1153   // Static utility for macros.
1154
1155   static const TEveException kEH("AliEveEventManager::AssertGeometry ");
1156
1157   if (AliGeomManager::GetGeometry() == 0)
1158   {
1159     if (TGeoManager::IsLocked())
1160       throw (kEH + "geometry is not loaded but TGeoManager is locked.");
1161
1162     gGeoManager = 0;
1163     AliGeomManager::LoadGeometry();
1164     if ( ! AliGeomManager::GetGeometry())
1165     {
1166       throw (kEH + "can not load geometry.");
1167     }
1168     if ( ! AliGeomManager::ApplyAlignObjsFromCDB("ITS TPC TRD TOF PHOS HMPID EMCAL MUON FMD ZDC PMD T0 VZERO ACORDE"))
1169     {
1170       ::Warning(kEH, "mismatch of alignable volumes. Proceeding.");
1171       // throw (kEH + "could not apply align objs.");
1172     }
1173     AliGeomManager::GetGeometry()->DefaultColors();
1174   }
1175
1176   gGeoManager = AliGeomManager::GetGeometry();
1177   return gGeoManager;
1178 }
1179
1180 //------------------------------------------------------------------------------
1181
1182 AliEveEventManager* AliEveEventManager::AddDependentManager(const TString& name, const TString& path)
1183 {
1184   // Create and attach a dependent event-manager.
1185   // It is not added into eve list tree.
1186
1187   static const TEveException kEH("AliEveEventManager::AddDependentManager ");
1188
1189   if (fgMaster == 0)
1190     throw(kEH + "Master event-manager must be instantiated first.");
1191
1192   if (fgMaster->fSubManagers == 0)
1193   {
1194     fgMaster->fSubManagers = new TList;
1195     fgMaster->fSubManagers->SetOwner(kTRUE);
1196   }
1197
1198   AliEveEventManager* new_mgr = 0;
1199   fgCurrent = 0;
1200   try
1201   {
1202     new_mgr = new AliEveEventManager(name, path, fgMaster->fEventId);
1203     fgMaster->fSubManagers->Add(new_mgr);
1204   }
1205   catch (TEveException& exc)
1206   {
1207     ::Error(kEH, "Creation of new event-manager failed: '%s'.", exc.Data());
1208   }
1209   fgCurrent = fgMaster;
1210
1211   return new_mgr;
1212 }
1213
1214 AliEveEventManager* AliEveEventManager::GetDependentManager(const TString& name)
1215 {
1216   // Get a dependant manager by name.
1217   // This will not change the current manager, use helper class
1218   // AliEveEventManager::CurrentChanger for that.
1219
1220   static const TEveException kEH("AliEveEventManager::GetDependentManager ");
1221
1222   if (fgMaster == 0)
1223     throw(kEH + "Master event-manager must be instantiated first.");
1224
1225   if (fgMaster->fSubManagers == 0)
1226     return 0;
1227
1228   return dynamic_cast<AliEveEventManager*>(fgMaster->fSubManagers->FindObject(name));
1229 }
1230
1231 AliEveEventManager* AliEveEventManager::GetMaster()
1232 {
1233   // Get master event-manager.
1234
1235   return fgMaster;
1236 }
1237
1238 AliEveEventManager* AliEveEventManager::GetCurrent()
1239 {
1240   // Get current event-manager.
1241
1242   return fgCurrent;
1243 }
1244
1245 void AliEveEventManager::RegisterTransient(TEveElement* element)
1246 {
1247   GetCurrent()->fTransients->AddElement(element);
1248 }
1249
1250 void AliEveEventManager::RegisterTransientList(TEveElement* element)
1251 {
1252   GetCurrent()->fTransientLists->AddElement(element);
1253 }
1254
1255 //------------------------------------------------------------------------------
1256 // Autoloading of events
1257 //------------------------------------------------------------------------------
1258
1259 void AliEveEventManager::SetAutoLoadTime(Float_t time)
1260 {
1261   // Set the auto-load time in seconds
1262
1263   fAutoLoadTime = time;
1264 }
1265
1266 void AliEveEventManager::SetAutoLoad(Bool_t autoLoad)
1267 {
1268   // Set the automatic event loading mode
1269
1270   static const TEveException kEH("AliEveEventManager::SetAutoLoad ");
1271
1272   if (fAutoLoad == autoLoad)
1273   {
1274     Warning(kEH, "Setting autoload to the same value as before - %s. Ignoring.", fAutoLoad ? "true" : "false");
1275     return;
1276   }
1277
1278   fAutoLoad = autoLoad;
1279   if (fAutoLoad)
1280   {
1281     StartAutoLoadTimer();
1282   }
1283   else
1284   {
1285     StopAutoLoadTimer();
1286   }
1287 }
1288
1289 void AliEveEventManager::SetTrigSel(Int_t trig)
1290 {
1291   static const TEveException kEH("AliEveEventManager::SetTrigSel ");
1292
1293   if (!fRawReader)
1294     {
1295     Warning(kEH, "No Raw-reader exists. Ignoring the call.");
1296     return;
1297   }
1298   else
1299   {
1300     ULong64_t trigMask = 0;
1301     if (trig >= 0) trigMask = (1ull << trig);
1302     Info(kEH,"Trigger selection: 0x%llx",trigMask);
1303     fRawReader->SelectEvents(-1,trigMask,NULL);
1304   }
1305 }
1306
1307 void AliEveEventManager::StartAutoLoadTimer()
1308 {
1309   // Start the auto-load timer.
1310
1311   fAutoLoadTimer->SetTime((Long_t)(1000*fAutoLoadTime));
1312   fAutoLoadTimer->Reset();
1313   fAutoLoadTimer->TurnOn();
1314   fAutoLoadTimerRunning = kTRUE;
1315 }
1316
1317 void AliEveEventManager::StopAutoLoadTimer()
1318 {
1319   // Stop the auto-load timer.
1320
1321   fAutoLoadTimerRunning = kFALSE;
1322   fAutoLoadTimer->TurnOff();
1323 }
1324
1325 void AliEveEventManager::AutoLoadNextEvent()
1326 {
1327   // Called from auto-load timer, so it has to be public.
1328   // Do NOT call it directly.
1329
1330   static const TEveException kEH("AliEveEventManager::AutoLoadNextEvent ");
1331
1332   if ( ! fAutoLoadTimerRunning || ! fAutoLoadTimer->HasTimedOut())
1333   {
1334     Warning(kEH, "Called unexpectedly - ignoring the call. Should ONLY be called from an internal timer.");
1335     return;
1336   }
1337
1338   StopAutoLoadTimer();
1339   NextEvent();
1340   if (fAutoLoad && !fExternalCtrl)
1341     StartAutoLoadTimer();
1342 }
1343
1344 //------------------------------------------------------------------------------
1345 // Post event-loading functions
1346 //------------------------------------------------------------------------------
1347
1348 void AliEveEventManager::AfterNewEventLoaded()
1349 {
1350   // Execute registered macros and commands.
1351   // At the end emit NewEventLoaded signal.
1352   //
1353   // Virtual from TEveEventManager.
1354
1355   static const TEveException kEH("AliEveEventManager::AfterNewEventLoaded ");
1356
1357   NewEventDataLoaded();
1358
1359   if (fExecutor)
1360     fExecutor->ExecMacros();
1361
1362   TEveEventManager::AfterNewEventLoaded();
1363
1364   NewEventLoaded();
1365
1366   if (this == fgMaster && fSubManagers != 0)
1367   {
1368     TIter next(fSubManagers);
1369     while ((fgCurrent = dynamic_cast<AliEveEventManager*>(next())) != 0)
1370     {
1371       gEve->SetCurrentEvent(fgCurrent);
1372       try
1373       {
1374         fgCurrent->GotoEvent(fEventId);
1375       }
1376       catch (TEveException& exc)
1377       {
1378         // !!! Should somehow tag / disable / remove it?
1379         Error(kEH, "Getting event %d for sub-event-manager '%s' failed: '%s'.",
1380               fEventId, fgCurrent->GetName(), exc.Data());
1381       }
1382     }
1383     fgCurrent = fgMaster;
1384     gEve->SetCurrentEvent(fgMaster);
1385   }
1386 }
1387
1388 void AliEveEventManager::NewEventDataLoaded()
1389 {
1390   // Emit NewEventDataLoaded signal.
1391
1392   Emit("NewEventDataLoaded()");
1393 }
1394
1395 void AliEveEventManager::NewEventLoaded()
1396 {
1397   // Emit NewEventLoaded signal.
1398
1399   Emit("NewEventLoaded()");
1400 }
1401
1402
1403 //------------------------------------------------------------------------------
1404 // Event info dumpers
1405 //------------------------------------------------------------------------------
1406
1407 TString AliEveEventManager::GetEventInfoHorizontal() const
1408 {
1409   // Dumps the event-header contents in vertical formatting.
1410
1411   TString rawInfo, esdInfo;
1412
1413   if (!fRawReader)
1414   {
1415     rawInfo = "No raw-data event info is available!\n";
1416   }
1417   else
1418   {
1419     const UInt_t* attr = fRawReader->GetAttributes();
1420     TTimeStamp ts(fRawReader->GetTimestamp());
1421     rawInfo.Form("RAW event info: Run#: %d  Event type: %d (%s)  Period: %x  Orbit: %x  BC: %x\n"
1422                  "Trigger: %llx\nDetectors: %x (%s)\nAttributes:%x-%x-%x  Timestamp: %s\n",
1423                  fRawReader->GetRunNumber(),fRawReader->GetType(),AliRawEventHeaderBase::GetTypeName(fRawReader->GetType()),
1424                  fRawReader->GetPeriod(),fRawReader->GetOrbitID(),fRawReader->GetBCID(),
1425                  fRawReader->GetClassMask(),
1426                  *fRawReader->GetDetectorPattern(),AliDAQ::ListOfTriggeredDetectors(*fRawReader->GetDetectorPattern()),
1427                  attr[0],attr[1],attr[2], ts.AsString("s"));
1428   }
1429
1430   if (!fESD)
1431   {
1432     esdInfo = "No ESD event info is available!";
1433   }
1434   else
1435   {
1436     TString acttrclasses   = fESD->GetESDRun()->GetActiveTriggerClasses();
1437     TString firedtrclasses = fESD->GetFiredTriggerClasses();
1438     TTimeStamp ts(fESD->GetTimeStamp());
1439     esdInfo.Form("ESD event info: Run#: %d  Event type: %d (%s)  Period: %x  Orbit: %x  BC: %x\n"
1440                  "Active trigger classes: %s\nTrigger: %llx (%s)\nEvent# in file: %d  Timestamp: %s, MagField: %.2e",
1441                  fESD->GetRunNumber(),
1442                  fESD->GetEventType(),AliRawEventHeaderBase::GetTypeName(fESD->GetEventType()),
1443                  fESD->GetPeriodNumber(),fESD->GetOrbitNumber(),fESD->GetBunchCrossNumber(),
1444                  acttrclasses.Data(),
1445                  fESD->GetTriggerMask(),firedtrclasses.Data(),
1446                  fESD->GetEventNumberInFile(), ts.AsString("s"), fESD->GetMagneticField());
1447   }
1448
1449   return rawInfo + esdInfo;
1450 }
1451
1452 TString AliEveEventManager::GetEventInfoVertical() const
1453 {
1454   // Dumps the event-header contents in vertical formatting.
1455
1456   TString rawInfo, esdInfo;
1457
1458   if (!fRawReader)
1459   {
1460     rawInfo = "No raw-data event info is available!\n";
1461   }
1462   else
1463   {
1464     const UInt_t* attr = fRawReader->GetAttributes();
1465     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",
1466                  fRawReader->GetRunNumber(),fRawReader->GetType(),AliRawEventHeaderBase::GetTypeName(fRawReader->GetType()),
1467                  fRawReader->GetPeriod(),fRawReader->GetOrbitID(),fRawReader->GetBCID(),
1468                  fRawReader->GetClassMask(),
1469                  *fRawReader->GetDetectorPattern(),AliDAQ::ListOfTriggeredDetectors(*fRawReader->GetDetectorPattern()),
1470                  attr[0],attr[1],attr[2],
1471                  fRawReader->GetTimestamp());
1472   }
1473
1474   if (!fESD)
1475   {
1476     esdInfo = "No ESD event info is available!\n";
1477   }
1478   else
1479   {
1480     TString acttrclasses   = fESD->GetESDRun()->GetActiveTriggerClasses();
1481     TString firedtrclasses = fESD->GetFiredTriggerClasses();
1482     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",
1483                  fESD->GetRunNumber(),
1484                  acttrclasses.Data(),
1485                  fESD->GetEventType(),AliRawEventHeaderBase::GetTypeName(fESD->GetEventType()),
1486                  fESD->GetPeriodNumber(),fESD->GetOrbitNumber(),fESD->GetBunchCrossNumber(),
1487                  fESD->GetTriggerMask(),firedtrclasses.Data(),
1488                  fESD->GetEventNumberInFile(),
1489                  fESD->GetTimeStamp());
1490   }
1491
1492   return rawInfo + "\n" + esdInfo;
1493 }
1494
1495
1496 //==============================================================================
1497 // Reading of GRP and MagneticField.
1498 // This is a reap-off from reconstruction ... should really be a common
1499 // code to do this somewhere in STEER.
1500 //==============================================================================
1501
1502 Bool_t AliEveEventManager::InitGRP()
1503 {
1504   //------------------------------------
1505   // Initialization of the GRP entry 
1506   //------------------------------------
1507
1508   static const TEveException kEH("AliEveEventManager::InitGRP ");
1509
1510   AliGRPManager grpMgr;
1511   if (!grpMgr.ReadGRPEntry()) {
1512     return kFALSE;
1513   }
1514   fgGRPLoaded = kTRUE;
1515   if (!grpMgr.SetMagField()) {
1516     throw kEH + "Setting of field failed!";
1517   }
1518
1519   //*** Get the diamond profiles from OCDB
1520   // Eventually useful.
1521
1522   /*
1523     entry = AliCDBManager::Instance()->Get("GRP/Calib/MeanVertexSPD");
1524     if (entry) {
1525     fDiamondProfileSPD = dynamic_cast<AliESDVertex*> (entry->GetObject());  
1526     } else {
1527     ::Error(kEH, "No SPD diamond profile found in OCDB!");
1528     }
1529
1530     entry = AliCDBManager::Instance()->Get("GRP/Calib/MeanVertex");
1531     if (entry) {
1532     fDiamondProfile = dynamic_cast<AliESDVertex*> (entry->GetObject());  
1533     } else {
1534     ::Error(kEH, "No diamond profile found in OCDB!");
1535     }
1536
1537     entry = AliCDBManager::Instance()->Get("GRP/Calib/MeanVertexTPC");
1538     if (entry) {
1539     fDiamondProfileTPC = dynamic_cast<AliESDVertex*> (entry->GetObject());  
1540     } else {
1541     ::Error(kEH, "No TPC diamond profile found in OCDB!");
1542     }
1543   */
1544
1545   return kTRUE;
1546
1547
1548 //------------------------------------
1549 // Global variables management
1550 //------------------------------------
1551
1552 Bool_t AliEveEventManager::InsertGlobal(const TString& tag, TEveElement* model)
1553 {
1554    // Insert a new visualization-parameter database entry with the default
1555    return InsertGlobal(tag, model, fGlobalReplace, fGlobalUpdate);
1556 }
1557
1558 Bool_t AliEveEventManager::InsertGlobal(const TString& tag, TEveElement* model,
1559                     Bool_t replace, Bool_t update)
1560 {
1561    TPair* pair = (TPair*) fGlobal->FindObject(tag);
1562    if (pair)
1563    {
1564       if (replace)
1565       {
1566          model->IncDenyDestroy();
1567          model->SetRnrChildren(kFALSE);
1568
1569          TEveElement* old_model = dynamic_cast<TEveElement*>(pair->Value());
1570                  if(!old_model) AliFatal("old_model == 0, dynamic cast failed\n");
1571          while (old_model->HasChildren())
1572          {
1573             TEveElement *el = old_model->FirstChild();
1574             el->SetVizModel(model);
1575             if (update)
1576             {
1577                el->CopyVizParams(model);
1578                el->PropagateVizParamsToProjecteds();
1579             }
1580          }
1581          old_model->DecDenyDestroy();
1582
1583          pair->SetValue(dynamic_cast<TObject*>(model));
1584          return kTRUE;
1585       }
1586       else
1587       {
1588          return kFALSE;
1589       }
1590    }
1591    else
1592    {
1593       model->IncDenyDestroy();
1594       model->SetRnrChildren(kFALSE);
1595       fGlobal->Add(new TObjString(tag), dynamic_cast<TObject*>(model));
1596       return kTRUE;
1597    }
1598 }
1599
1600 TEveElement* AliEveEventManager::FindGlobal(const TString& tag)
1601 {
1602    return dynamic_cast<TEveElement*>(fGlobal->GetValue(tag));
1603 }