Example for analysis on kine tree.
[u/mrichter/AliRoot.git] / EVE / EveBase / AliEveEventManager.cxx
CommitLineData
d810d0de 1// $Id$
2// Main authors: Matevz Tadel & Alja Mrak-Tadel: 2006, 2007
5a5a1232 3
d810d0de 4/**************************************************************************
5 * Copyright(c) 1998-2008, ALICE Experiment at CERN, all rights reserved. *
6 * See http://aliceinfo.cern.ch/Offline/AliRoot/License.html for *
51346b82 7 * full copyright notice. *
d810d0de 8 **************************************************************************/
9
10#include "AliEveEventManager.h"
84aff7a4 11#include <TEveManager.h>
5a5a1232 12
13#include <AliRunLoader.h>
93845f6c 14#include <AliRun.h>
af885e0f 15#include <AliESDEvent.h>
3aecaefc 16#include <AliESDfriend.h>
93845f6c 17#include <AliMagFMaps.h>
632d2b03 18#include <AliCDBManager.h>
19#include <AliHeader.h>
20#include <AliGeomManager.h>
5a5a1232 21
22#include <TFile.h>
23#include <TTree.h>
5a5a1232 24#include <TSystem.h>
d810d0de 25
a15e6d7d 26//==============================================================================
27//==============================================================================
28// AliEveEventManager
29//==============================================================================
5a5a1232 30
57ffa5fb 31//______________________________________________________________________________
5a5a1232 32//
4852ff6f 33// Provide interface for loading and navigating standard AliRoot data
34// (AliRunLoader) and ESDs.
35//
36// Missing support for raw-data. For now this is handled individually
37// by each sub-detector.
51346b82 38//
a15e6d7d 39// Also provides interface to magnetic-field and geometry. Mostly
40// intended as wrappers over standard AliRoot functionality for
41// convenient use from visualizateion macros.
5a5a1232 42
d810d0de 43ClassImp(AliEveEventManager)
5a5a1232 44
a15e6d7d 45AliEveEventManager* gAliEveEvent = 0;
5a5a1232 46
d810d0de 47Bool_t AliEveEventManager::fgAssertRunLoader = kFALSE;
c76ea574 48Bool_t AliEveEventManager::fgAssertESD = kFALSE;
5a5a1232 49
d810d0de 50TString AliEveEventManager::fgCdbUri("local://$ALICE_ROOT");
632d2b03 51
d810d0de 52AliMagF* AliEveEventManager::fgMagField = 0;
93845f6c 53
54
d810d0de 55AliEveEventManager::AliEveEventManager() :
84aff7a4 56 TEveEventManager(),
265ecb21 57
c76ea574 58 fPath (), fEventId (0),
265ecb21 59 fRunLoader (0),
90fa773e 60 fESDFile (0), fESDTree (0), fESD (0),
32e219c2 61 fESDfriend (0), fESDfriendExists(kFALSE)
c76ea574 62{
63 // Default constructor.
64}
5a5a1232 65
d810d0de 66AliEveEventManager::AliEveEventManager(TString path, Int_t ev) :
67 TEveEventManager("AliEVE AliEveEventManager"),
265ecb21 68
c76ea574 69 fPath (path), fEventId(-1),
265ecb21 70 fRunLoader (0),
90fa773e 71 fESDFile (0), fESDTree (0), fESD (0),
32e219c2 72 fESDfriend (0), fESDfriendExists(kFALSE)
5a5a1232 73{
c76ea574 74 // Constructor with event-directory URL and event-id.
75
5a5a1232 76 Open();
90fa773e 77 if (ev >= 0) GotoEvent(ev);
5a5a1232 78}
79
a15e6d7d 80AliEveEventManager::~AliEveEventManager()
81{
82 // Destructor.
83
84 // Somewhat unclear what to do here.
85 // In principle should close all data sources and deregister from
86 // TEveManager.
87}
88
57ffa5fb 89/******************************************************************************/
5a5a1232 90
d810d0de 91void AliEveEventManager::Open()
5a5a1232 92{
c76ea574 93 // Open event-data from URL specified in fPath.
94 // Attempts to create AliRunLoader() and to open ESD with ESDfriends.
95 // Warning is reported if run-loader or ESD is not found.
96 // Global data-members fgAssertRunLoader and fgAssertESD can be set
97 // to throw exceptions instead.
98
a15e6d7d 99 static const TEveException kEH("AliEveEventManager::Open ");
5a5a1232 100
101 gSystem->ExpandPathName(fPath);
84aff7a4 102 if (fPath[0] != '/')
5a5a1232 103 fPath = Form("%s/%s", gSystem->WorkingDirectory(), fPath.Data());
104
632d2b03 105 Int_t runNo = -1;
106
a15e6d7d 107 TString gaPath(Form("%s/galice.root", fPath.Data()));
108 if (gSystem->AccessPathName(gaPath, kReadPermission) == kFALSE)
90fa773e 109 {
a15e6d7d 110 fRunLoader = AliRunLoader::Open(gaPath);
a1896a82 111 if (fRunLoader)
5a5a1232 112 {
a15e6d7d 113 TString alicePath = fPath + "/";
114 fRunLoader->SetDirName(alicePath);
a1896a82 115
116 if (fRunLoader->LoadgAlice() != 0)
a15e6d7d 117 Warning(kEH, "failed loading gAlice via run-loader.");
a1896a82 118
119 if (fRunLoader->LoadHeader() == 0)
120 {
121 runNo = fRunLoader->GetHeader()->GetRun();
122 }
123 else
124 {
a15e6d7d 125 Warning(kEH, "failed loading run-loader's header.");
a1896a82 126 delete fRunLoader;
127 fRunLoader = 0;
128 }
129 }
130 else // run-loader open failed
131 {
a15e6d7d 132 Warning(kEH, "failed opening ALICE run-loader from '%s'.", gaPath.Data());
5a5a1232 133 }
5a5a1232 134 }
a1896a82 135 else // galice not readable
136 {
a15e6d7d 137 Warning(kEH, "can not read '%s'.", gaPath.Data());
a1896a82 138 }
139 if (fRunLoader == 0)
140 {
84aff7a4 141 if (fgAssertRunLoader)
a15e6d7d 142 throw(kEH + "Bootstraping of run-loader failed. Its precence was requested.");
a1896a82 143 else
a15e6d7d 144 Warning(kEH, "Bootstraping of run-loader failed.");
a1896a82 145 }
51346b82 146
5a5a1232 147
a15e6d7d 148 TString esdPath(Form("%s/AliESDs.root", fPath.Data()));
149 if (gSystem->AccessPathName(esdPath, kReadPermission) == kFALSE)
90fa773e 150 {
a15e6d7d 151 fESDFile = new TFile(esdPath);
84aff7a4 152 if (fESDFile->IsZombie() == kFALSE)
90fa773e 153 {
af885e0f 154 fESD = new AliESDEvent();
a1896a82 155 fESDTree = (TTree*) fESDFile->Get("esdTree");
156 if (fESDTree != 0)
157 {
158 fESD->ReadFromTree(fESDTree);
159 runNo = fESD->GetESDRun()->GetRunNumber();
160
161 // Check if ESDfriends exists and attach the branch
162 TString p = Form("%s/AliESDfriends.root", fPath.Data());
84aff7a4 163 if (gSystem->AccessPathName(p, kReadPermission) == kFALSE)
a1896a82 164 {
165 fESDfriendExists = kTRUE;
166 fESDTree->SetBranchStatus ("ESDfriend*", 1);
167 fESDTree->SetBranchAddress("ESDfriend.", &fESDfriend);
168 }
169 }
170 else // esdtree == 0
171 {
172 delete fESDFile; fESDFile = 0;
a15e6d7d 173 Warning(kEH, "failed getting the esdTree.");
753fdd1e 174 }
175 }
a1896a82 176 else // esd tfile is zombie
177 {
5a5a1232 178 delete fESDFile; fESDFile = 0;
a15e6d7d 179 Warning(kEH, "failed opening ESD from '%s'.", esdPath.Data());
5a5a1232 180 }
a1896a82 181 }
182 else // esd not readable
183 {
a15e6d7d 184 Warning(kEH, "can not read ESD file '%s'.", esdPath.Data());
a1896a82 185 }
186 if (fESDTree == 0)
187 {
c76ea574 188 if (fgAssertESD)
90fa773e 189 {
a15e6d7d 190 throw(kEH + "ESD not initialized. Its precence was requested.");
51346b82 191 } else {
a15e6d7d 192 Warning(kEH, "ESD not initialized.");
3aecaefc 193 }
5a5a1232 194 }
195
632d2b03 196 if (runNo < 0)
a15e6d7d 197 throw(kEH + "invalid run number.");
632d2b03 198
199 {
200 AliCDBManager* cdb = AliCDBManager::Instance();
201 cdb->SetDefaultStorage(fgCdbUri);
202 if (cdb->IsDefaultStorageSet() == kFALSE)
a15e6d7d 203 throw(kEH + "CDB initialization failed.");
632d2b03 204 cdb->SetRun(runNo);
205 }
206
73c1c0ec 207 SetName(Form("Event %d", fEventId));
5a5a1232 208 SetTitle(fPath);
209}
210
d810d0de 211void AliEveEventManager::GotoEvent(Int_t event)
1eaa5849 212{
73c1c0ec 213 // Load data for specified event.
214 // If event is out of range an exception is thrown and old state
215 // is preserved.
216 // After successful loading of event, the virtual function
217 // AfterNewEventLoaded() is called. This executes commands that
218 // were registered via TEveEventManager::AddNewEventCommand().
219
a15e6d7d 220 static const TEveException kEH("AliEveEventManager::GotoEvent ");
1eaa5849 221
1eaa5849 222 Int_t maxEvent = 0;
84aff7a4 223 if (fRunLoader)
1eaa5849 224 maxEvent = fRunLoader->GetNumberOfEvents() - 1;
84aff7a4 225 else if (fESDTree)
1eaa5849 226 maxEvent = fESDTree->GetEntries() - 1;
753fdd1e 227 else
a15e6d7d 228 throw(kEH + "neither RunLoader nor ESD loaded.");
1eaa5849 229
84aff7a4 230 if (event < 0 || event > maxEvent)
a15e6d7d 231 throw(kEH + Form("event %d not present, available range [%d, %d].",
1eaa5849 232 event, 0, maxEvent));
233
84aff7a4 234 TEveManager::TRedrawDisabler rd(gEve);
235 gEve->Redraw3D(kFALSE, kTRUE); // Enforce drop of all logicals.
d9e0d6c5 236
32e219c2 237 // !!! MT this is somewhat brutal; at least optionally, one could be
238 // a bit gentler, checking for objs owning their external refs and having
239 // additinal parents.
1eaa5849 240 DestroyElements();
241 fEventId = event;
73c1c0ec 242 SetName(Form("Event %d", fEventId));
1eaa5849 243 UpdateItems();
244
84aff7a4 245 if (fRunLoader) {
246 if (fRunLoader->GetEvent(fEventId) != 0)
a15e6d7d 247 throw(kEH + "failed getting required event.");
1eaa5849 248 }
249
84aff7a4 250 if (fESDTree) {
251 if (fESDTree->GetEntry(fEventId) <= 0)
a15e6d7d 252 throw(kEH + "failed getting required event from ESD.");
1eaa5849 253
2cea771a 254 if (fESDfriendExists)
1eaa5849 255 fESD->SetESDfriend(fESDfriend);
1eaa5849 256 }
90fa773e 257
258 AfterNewEventLoaded();
1eaa5849 259}
260
d810d0de 261void AliEveEventManager::Close()
5a5a1232 262{
73c1c0ec 263 // Close the event files.
264 // For the moment only ESD is closed. Needs to be investigated for
265 // AliRunLoader and Raw.
266
2cea771a 267 if (fESDTree) {
268 delete fESD; fESD = 0;
269 delete fESDfriend; fESDfriend = 0;
270
271 delete fESDTree; fESDTree = 0;
272 delete fESDFile; fESDFile = 0;
273 }
5a5a1232 274}
275
90fa773e 276
57ffa5fb 277/******************************************************************************/
c76ea574 278// Static convenience functions, mainly used from macros.
57ffa5fb 279/******************************************************************************/
5a5a1232 280
d810d0de 281AliRunLoader* AliEveEventManager::AssertRunLoader()
5a5a1232 282{
73c1c0ec 283 // Make sure AliRunLoader is initialized and return it.
284 // Throws exception in case run-loader is not available.
285 // Static utility for macros.
286
a15e6d7d 287 static const TEveException kEH("AliEveEventManager::AssertRunLoader ");
5a5a1232 288
a15e6d7d 289 if (gAliEveEvent == 0)
290 throw(kEH + "ALICE event not ready.");
291 if (gAliEveEvent->fRunLoader == 0)
292 throw(kEH + "AliRunLoader not initialised.");
293 return gAliEveEvent->fRunLoader;
5a5a1232 294}
295
d810d0de 296AliESDEvent* AliEveEventManager::AssertESD()
5a5a1232 297{
73c1c0ec 298 // Make sure AliESDEvent is initialized and return it.
299 // Throws exception in case ESD is not available.
300 // Static utility for macros.
301
a15e6d7d 302 static const TEveException kEH("AliEveEventManager::AssertESD ");
5a5a1232 303
a15e6d7d 304 if (gAliEveEvent == 0)
305 throw(kEH + "ALICE event not ready.");
306 if (gAliEveEvent->fESD == 0)
307 throw(kEH + "AliESD not initialised.");
308 return gAliEveEvent->fESD;
5a5a1232 309}
3aecaefc 310
d810d0de 311AliESDfriend* AliEveEventManager::AssertESDfriend()
3aecaefc 312{
73c1c0ec 313 // Make sure AliESDfriend is initialized and return it.
314 // Throws exception in case ESDfriend-loader is not available.
315 // Static utility for macros.
316
a15e6d7d 317 static const TEveException kEH("AliEveEventManager::AssertESDfriend ");
3aecaefc 318
a15e6d7d 319 if (gAliEveEvent == 0)
320 throw(kEH + "ALICE event not ready.");
321 if (gAliEveEvent->fESDfriend == 0)
322 throw(kEH + "AliESDfriend not initialised.");
323 return gAliEveEvent->fESDfriend;
3aecaefc 324}
93845f6c 325
d810d0de 326AliMagF* AliEveEventManager::AssertMagField()
93845f6c 327{
73c1c0ec 328 // Make sure AliMagF is initialized and return it.
329 // Throws exception in case magnetic field is not available.
330 // Static utility for macros.
331
93845f6c 332 if (fgMagField == 0)
333 {
a15e6d7d 334 if (gAliEveEvent && gAliEveEvent->fRunLoader && gAliEveEvent->fRunLoader->GetAliRun())
335 fgMagField = gAliEveEvent->fRunLoader->GetAliRun()->Field();
93845f6c 336 else
337 fgMagField = new AliMagFMaps("Maps","Maps", 1, 1., 10., AliMagFMaps::k5kG);
338 }
339 return fgMagField;
340}
632d2b03 341
d810d0de 342TGeoManager* AliEveEventManager::AssertGeometry()
632d2b03 343{
73c1c0ec 344 // Make sure AliGeomManager is initialized and returns the
345 // corresponding TGeoManger.
346 // gGeoManager is not set, maybe it should be.
347 // Throws exception in case run-loader is not available.
348 // Static utility for macros.
349
350 // !!!! Should we set gGeoManager here?
351
a15e6d7d 352 static const TEveException kEH("AliEveEventManager::AssertGeometry ");
632d2b03 353
354 if (AliGeomManager::GetGeometry() == 0)
355 {
af2e4ef5 356 gGeoManager = 0;
632d2b03 357 AliGeomManager::LoadGeometry();
358 if ( ! AliGeomManager::GetGeometry())
359 {
a15e6d7d 360 throw(kEH + "can not load geometry.");
632d2b03 361 }
362 if ( ! AliGeomManager::ApplyAlignObjsFromCDB("ITS TPC TRD TOF PHOS HMPID EMCAL MUON FMD ZDC PMD T0 VZERO ACORDE"))
363 {
a15e6d7d 364 ::Warning(kEH, "mismatch of alignable volumes. Proceeding.");
365 // throw(kEH + "could not apply align objs.");
632d2b03 366 }
367 }
368
369 return AliGeomManager::GetGeometry();
370}