Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
alice_vsd.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_eve
3 /// Complex example showing ALICE VSD visualization.
4 ///
5 /// alice_vsd.C - a simple event-display for ALICE
6 ///
7 /// Only standard ROOT is used to process the ALICE VSD files.
8 ///
9 /// No ALICE code is needed -- the VSD file is exported from AliRoot into
10 /// VSD format -- see TEveVSDStructs.h and TEveVSD.h.
11 ///
12 /// A simple geometry of 10KB, extracted from the full TGeo-geometry, is
13 /// used to outline the central detectors of ALICE.
14 ///
15 /// All files are access from the web by using the "CACHEREAD" option.
16 ///
17 /// \image html eve_alice_vsd.png
18 /// \macro_code
19 ///
20 /// \author Matevz Tadel
21 
22 
23 #include <TEveManager.h>
24 #include <TEveEventManager.h>
25 #include <TEveVSD.h>
26 #include <TEveVSDStructs.h>
27 
28 #include <TEveTrack.h>
29 #include <TEveTrackPropagator.h>
30 #include <TEveGeoShape.h>
31 
32 #include <TGTab.h>
33 #include <TGButton.h>
34 
35 #include <TFile.h>
36 #include <TKey.h>
37 #include <TSystem.h>
38 #include <TPRegexp.h>
39 
40 
41 // Include components -- compile time link :)
42 
43 #include "MultiView.C"
44 MultiView* gMultiView = 0;
45 
46 
47 class TVSDReader
48 {
49 public:
50  // ----------------------------------------------------------
51  // File / Event Data
52  // ----------------------------------------------------------
53 
54  TFile *fFile;
55  TDirectory *fDirectory;
56 
57  TObjArray *fEvDirKeys;
58 
59  TEveVSD *fVSD;
60 
61  Int_t fMaxEv, fCurEv;
62 
63  // ----------------------------------------------------------
64  // Event visualization structures
65  // ----------------------------------------------------------
66 
67  TEveTrackList *fTrackList;
68  TEvePointSet *fITSClusters;
69  TEvePointSet *fTPCClusters;
70  TEvePointSet *fTRDClusters;
71  TEvePointSet *fTOFClusters;
72 
73 public:
74  TVSDReader(const char* file_name) :
75  fFile(0), fDirectory(0), fEvDirKeys(0),
76  fVSD(0),
77 
78  fMaxEv(-1), fCurEv(-1),
79 
80  fTrackList(0),
81  fITSClusters(0), fTPCClusters(0), fTRDClusters(0), fTOFClusters(0)
82  {
83  fFile = TFile::Open(file_name);
84  if (!fFile)
85  {
86  Error("VSD_Reader", "Can not open file '%s' ... terminating.",
87  file_name);
88  gSystem->Exit(1);
89  }
90 
91  fEvDirKeys = new TObjArray;
92  TPMERegexp name_re("Event\\d+");
93  TObjLink* lnk = fFile->GetListOfKeys()->FirstLink();
94  while (lnk) {
95  if (name_re.Match(lnk->GetObject()->GetName()))
96  {
97  fEvDirKeys->Add(lnk->GetObject());
98  }
99  lnk = lnk->Next();
100  }
101 
102  fMaxEv = fEvDirKeys->GetEntriesFast();
103  if (fMaxEv == 0) {
104  Error("VSD_Reader", "No events to show ... terminating.");
105  gSystem->Exit(1);
106  }
107 
108  fVSD = new TEveVSD;
109  }
110 
111  virtual ~TVSDReader()
112  {
113  // Destructor.
114 
115  DropEvent();
116 
117  delete fVSD;
118  delete fEvDirKeys;
119 
120  fFile->Close();
121  delete fFile;
122  }
123 
124  void AttachEvent()
125  {
126  // Attach event data from current directory.
127 
128  fVSD->LoadTrees();
129  fVSD->SetBranchAddresses();
130  }
131 
132  void DropEvent()
133  {
134  // Drup currently held event data, release current directory.
135 
136  // Drop old visualization structures.
137 
138  gEve->GetViewers()->DeleteAnnotations();
139  gEve->GetCurrentEvent()->DestroyElements();
140 
141  // Drop old event-data.
142 
143  fVSD->DeleteTrees();
144  delete fDirectory;
145  fDirectory = 0;
146  }
147 
148  //---------------------------------------------------------------------------
149  // Event navigation
150  //---------------------------------------------------------------------------
151 
152  void NextEvent()
153  {
154  GotoEvent(fCurEv + 1);
155  }
156 
157  void PrevEvent()
158  {
159  GotoEvent(fCurEv - 1);
160  }
161 
162  Bool_t GotoEvent(Int_t ev)
163  {
164  if (ev < 0 || ev >= fMaxEv)
165  {
166  Warning("GotoEvent", "Invalid event id %d.", ev);
167  return kFALSE;
168  }
169 
170  DropEvent();
171 
172  // Connect to new event-data.
173 
174  fCurEv = ev;
175  fDirectory = (TDirectory*) ((TKey*) fEvDirKeys->At(fCurEv))->ReadObj();
176  fVSD->SetDirectory(fDirectory);
177 
178  AttachEvent();
179 
180  // Load event data into visualization structures.
181 
182  LoadClusters(fITSClusters, "ITS", 0);
183  LoadClusters(fTPCClusters, "TPC", 1);
184  LoadClusters(fTRDClusters, "TRD", 2);
185  LoadClusters(fTOFClusters, "TOF", 3);
186 
187  LoadEsdTracks();
188 
189  // Fill projected views.
190 
191  auto top = gEve->GetCurrentEvent();
192 
193  gMultiView->DestroyEventRPhi();
194  gMultiView->ImportEventRPhi(top);
195 
196  gMultiView->DestroyEventRhoZ();
197  gMultiView->ImportEventRhoZ(top);
198 
199  gEve->Redraw3D(kFALSE, kTRUE);
200 
201  return kTRUE;
202  }
203 
204 
205  //---------------------------------------------------------------------------
206  // Cluster loading
207  //---------------------------------------------------------------------------
208 
209  void LoadClusters(TEvePointSet*& ps, const TString& det_name, Int_t det_id)
210  {
211  if (ps == 0) {
212  ps = new TEvePointSet(det_name);
213  ps->SetMainColor((Color_t)(det_id + 2));
214  ps->SetMarkerSize(0.5);
215  ps->SetMarkerStyle(2);
216  ps->IncDenyDestroy();
217  } else {
218  ps->Reset();
219  }
220 
221  TEvePointSelector ss(fVSD->fTreeC, ps, "fV.fX:fV.fY:fV.fZ",
222  TString::Format("fDetId==%d", det_id));
223  ss.Select();
224  ps->SetTitle(TString::Format("N=%d", ps->Size()));
225 
226  gEve->AddElement(ps);
227  }
228 
229 
230  //---------------------------------------------------------------------------
231  // Track loading
232  //---------------------------------------------------------------------------
233 
234  enum ESDTrackFlags
235  {
236  kITSin=0x0001,kITSout=0x0002,kITSrefit=0x0004,kITSpid=0x0008,
237  kTPCin=0x0010,kTPCout=0x0020,kTPCrefit=0x0040,kTPCpid=0x0080,
238  kTRDin=0x0100,kTRDout=0x0200,kTRDrefit=0x0400,kTRDpid=0x0800,
239  kTOFin=0x1000,kTOFout=0x2000,kTOFrefit=0x4000,kTOFpid=0x8000,
240  kHMPIDpid=0x20000,
241  kEMCALmatch=0x40000,
242  kTRDbackup=0x80000,
243  kTRDStop=0x20000000,
244  kESDpid=0x40000000,
245  kTIME=0x80000000
246  };
247 
248  Bool_t trackIsOn(TEveTrack* t, Int_t mask)
249  {
250  // Check is track-flag specified by mask are set.
251 
252  return (t->GetStatus() & mask) > 0;
253  }
254 
255  void LoadEsdTracks()
256  {
257  // Read reconstructed tracks from current event.
258 
259  if (fTrackList == 0) {
260  fTrackList = new TEveTrackList("ESD Tracks");
261  fTrackList->SetMainColor(6);
262  fTrackList->SetMarkerColor(kYellow);
263  fTrackList->SetMarkerStyle(4);
264  fTrackList->SetMarkerSize(0.5);
265 
266  fTrackList->IncDenyDestroy();
267  } else {
268  fTrackList->DestroyElements();
269  }
270 
271  auto trkProp = fTrackList->GetPropagator();
272  // !!!! Need to store field on file !!!!
273  // Can store TEveMagField ?
274  trkProp->SetMagField(0.5);
275  trkProp->SetStepper(TEveTrackPropagator::kRungeKutta);
276 
277  Int_t nTracks = fVSD->fTreeR->GetEntries();
278  for (Int_t n = 0; n < nTracks; ++n) {
279  fVSD->fTreeR->GetEntry(n);
280 
281  TEveTrack* track = new TEveTrack(&fVSD->fR, trkProp);
282  track->SetName(Form("ESD Track %d", fVSD->fR.fIndex));
283  track->SetStdTitle();
284  track->SetAttLineAttMarker(fTrackList);
285  fTrackList->AddElement(track);
286  }
287 
288  fTrackList->MakeTracks();
289 
290  gEve->AddElement(fTrackList);
291  }
292 
293  ClassDef(TVSDReader, 0);
294 };
295 
296 TVSDReader* gVSDReader = 0;
297 
298 
299 // Forward declaration.
300 void make_gui();
301 
302 //______________________________________________________________________________
303 void alice_vsd(const char* vsd_file_name=
304  "http://mtadel.home.cern.ch/mtadel/root/AliVSD.root")
305 {
306  // Main function, initializes the application.
307  //
308  // 1. Load the auto-generated library holding ESD classes and
309  // ESD dictionaries.
310  // 2. Open ESD data-files.
311  // 3. Load cartoon geometry.
312  // 4. Spawn simple GUI.
313  // 5. Load first event.
314 
315  TFile::SetCacheFileDir(".");
316 
317  TEveVSD::DisableTObjectStreamersForVSDStruct();
318 
319  gVSDReader = new TVSDReader(vsd_file_name);
320 
321  TEveManager::Create();
322 
323  TEveGeoShape *gentle_geom = 0;
324 
325  { // Simple geometry
326  auto geom =
327  TFile::Open("http://mtadel.home.cern.ch/mtadel/root/alice_mini_geom.root",
328  "CACHEREAD");
329  if (!geom)
330  return;
331  auto gse = (TEveGeoShapeExtract*) geom->Get("Gentle");
332  gentle_geom = TEveGeoShape::ImportShapeExtract(gse, 0);
333  geom->Close();
334  delete geom;
335  gEve->AddGlobalElement(gentle_geom);
336  }
337 
338 
339  // Standard multi-view
340  //=====================
341 
342  gMultiView = new MultiView;
343  gMultiView->f3DView->GetGLViewer()->SetStyle(TGLRnrCtx::kOutline);
344 
345  gMultiView->SetDepth(-10);
346  gMultiView->ImportGeomRPhi(gentle_geom);
347  gMultiView->ImportGeomRhoZ(gentle_geom);
348  gMultiView->SetDepth(0);
349 
350 
351  // Final stuff
352  //=============
353 
354  gEve->GetViewers()->SwitchColorSet();
355  gEve->GetDefaultGLViewer()->SetStyle(TGLRnrCtx::kOutline);
356 
357  gEve->GetBrowser()->GetTabRight()->SetTab(1);
358 
359  make_gui();
360 
361  gEve->AddEvent(new TEveEventManager("Event", "ALICE VSD Event"));
362 
363  gVSDReader->GotoEvent(0);
364 
365  gEve->Redraw3D(kTRUE); // Reset camera after the first event has been shown.
366 }
367 
368 
369 //______________________________________________________________________________
370 void make_gui()
371 {
372  // Create minimal GUI for event navigation.
373 
374  auto browser = gEve->GetBrowser();
375  browser->StartEmbedding(TRootBrowser::kLeft);
376 
377  auto frmMain = new TGMainFrame(gClient->GetRoot(), 1000, 600);
378  frmMain->SetWindowName("XX GUI");
379  frmMain->SetCleanup(kDeepCleanup);
380 
381  auto hf = new TGHorizontalFrame(frmMain);
382  {
383  TString icondir(TString::Format("%s/icons/", gSystem->Getenv("ROOTSYS")));
384  TGPictureButton* b = 0;
385 
386  b = new TGPictureButton(hf, gClient->GetPicture(icondir+"GoBack.gif"));
387  hf->AddFrame(b);
388  b->Connect("Clicked()", "TVSDReader", gVSDReader, "PrevEvent()");
389 
390  b = new TGPictureButton(hf, gClient->GetPicture(icondir+"GoForward.gif"));
391  hf->AddFrame(b);
392  b->Connect("Clicked()", "TVSDReader", gVSDReader, "NextEvent()");
393  }
394  frmMain->AddFrame(hf);
395 
396  frmMain->MapSubwindows();
397  frmMain->Resize();
398  frmMain->MapWindow();
399 
400  browser->StopEmbedding();
401  browser->SetTabTitle("Event Control", 0);
402 }
403