BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
Loading...
Searching...
No Matches
EventDisplay.cc
1/*
2Beam Delivery Simulation (BDSIM) Copyright (C) BDSIM Collaboration, 2001 - 2026.
3
4This file is part of BDSIM.
5
6BDSIM is free software: you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published
8by the Free Software Foundation version 3 of the License.
9
10BDSIM is distributed in the hope that it will be useful, but
11WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with BDSIM. If not, see <http://www.gnu.org/licenses/>.
17*/
18#include "EventDisplay.hh"
19
20#include <iostream>
21#include <string>
22
23#include "TFile.h"
24#include "TVector3.h"
25
26#include "TEveManager.h"
27#include "TGeoManager.h"
28#include "TEveGeoNode.h"
29#include "TEvePointSet.h"
30#include "TEveBoxSet.h"
31#include "TEveLine.h"
32#include "TEveScene.h"
33#include "TEveEventManager.h"
34
35#include "BDSOutputROOTEventLoss.hh"
36#include "BDSOutputROOTEventModel.hh"
37#include "BDSOutputROOTEventOptions.hh"
38#include "BDSOutputROOTEventTrajectory.hh"
39#include "Model.hh"
40#include "Options.hh"
41
42
43ClassImp(EventDisplay)
44
46
47
49{
50 if (!instance)
51 {std::cout << "EventDisplay> WARNING - no file supplied" << std::endl;}
52 return instance;
53}
54
56 TString dataFileName)
57{
58 instance = new EventDisplay(geoFileName, dataFileName);
59 return instance;
60}
61
62EventDisplay::EventDisplay(TString geoFileNameIn,
63 TString dataFileNameIn):
64 geoFileName(geoFileNameIn),
65 dataFileName(dataFileNameIn)
66{
67 std::cout << "EventDisplay::EventDisplay(" << geoFileName << ")" << std::endl;
68 TEveManager::Create();
69 dataLoader = new DataLoader(std::string(dataFileName.Data()));
70 event = dataLoader->GetEvent();
71 eventTree = dataLoader->GetEventTree();
72
73 options = dataLoader->GetOptions();
74 optionsTree= dataLoader->GetOptionsTree();
75
76 model = dataLoader->GetModel();
77 modelTree = dataLoader->GetModelTree();
78
80 LoadOptions(0);
81 LoadModel(0);
82 LoadData(0);
83}
84
85EventDisplay::~EventDisplay()
86{
87 delete dataLoader;
88 instance = nullptr;
89}
90
92{
93 std::cout << "EventDisplay::LoadGeometry>" << std::endl;
94 TFile::SetCacheFileDir(".");
95 std::cout << geoFileName.Data() << std::endl;
96 gGeoManager = gEve->GetGeometry(geoFileName.Data());
97 gGeoManager->DefaultColors();
98
99 TEveGeoTopNode* tn = new TEveGeoTopNode(gGeoManager, gGeoManager->GetTopNode());
100 gEve->AddGlobalElement(tn);
101 gEve->FullRedraw3D(kTRUE);
102}
103
105{
106 std::cout << "EventDisplay::LoadModel>" << std::endl;
107 modelTree->GetEntry(iMod);
108}
109
111{
112 std::cout << "EventDisplay::LoadOptions>" << std::endl;
113 optionsTree->GetEntry(iOpt);
114}
115
117{
118 std::cout << "EventDisplay::LoadData>" << std::endl;
119 eventTree->GetEntry(i);
120}
121
123{
124 std::cout << "EventDisplay::ClearEvent>" << std::endl;
125 gEve->GetCurrentEvent()->DestroyElements();
126}
127
129{
130 DrawModel();
133 DrawSamplers();
135}
136
138{
139 std::cout << "EventDisplay::DrawModel>" << std::endl;
140 BDSOutputROOTEventOptions* ob = options->options;
141 std::cout << "EventDisplay::DrawModel> horizontalWidth " << ob->horizontalWidth << std::endl;
142 double horizontalWidth = ob->horizontalWidth; // need cm
143
144
145 TEveBoxSet* bs = new TEveBoxSet("model");
146 bs->Reset(TEveBoxSet::kBT_FreeBox, kFALSE,64);
147
148 int i = 0;
149 float vert[8*3];
150 for(auto mp : model->model->midPos)
151 {
152 double modelLength = model->model->length[i];
153 TRotation modelRotation = model->model->midRot[i];
154
155 // 100 as need cm not m
156 TVector3 v1 = 100*(mp + modelRotation*TVector3(-horizontalWidth / 2, -horizontalWidth / 2, -modelLength / 2));
157 TVector3 v2 = 100*(mp + modelRotation*TVector3(-horizontalWidth / 2, horizontalWidth / 2, -modelLength / 2));
158 TVector3 v3 = 100*(mp + modelRotation*TVector3( horizontalWidth / 2, horizontalWidth / 2, -modelLength / 2));
159 TVector3 v4 = 100*(mp + modelRotation*TVector3( horizontalWidth / 2, -horizontalWidth / 2, -modelLength / 2));
160
161 TVector3 v5 = 100*(mp + modelRotation*TVector3(-horizontalWidth / 2, -horizontalWidth / 2, modelLength / 2));
162 TVector3 v6 = 100*(mp + modelRotation*TVector3(-horizontalWidth / 2, horizontalWidth / 2, modelLength / 2));
163 TVector3 v7 = 100*(mp + modelRotation*TVector3( horizontalWidth / 2, horizontalWidth / 2, modelLength / 2));
164 TVector3 v8 = 100*(mp + modelRotation*TVector3( horizontalWidth / 2, -horizontalWidth / 2, modelLength / 2));
165
166
167 vert[0] = (float) v1.x();
168 vert[1] = (float) v1.y();
169 vert[2] = (float) v1.z();
170
171 vert[3] = (float) v2.x();
172 vert[4] = (float) v2.y();
173 vert[5] = (float) v2.z();
174
175 vert[6] = (float) v3.x();
176 vert[7] = (float) v3.y();
177 vert[8] = (float) v3.z();
178
179 vert[9] = (float) v4.x();
180 vert[10] = (float) v4.y();
181 vert[11] = (float) v4.z();
182
183 vert[12] = (float) v5.x();
184 vert[13] = (float) v5.y();
185 vert[14] = (float) v5.z();
186
187 vert[15] = (float) v6.x();
188 vert[16] = (float) v6.y();
189 vert[17] = (float) v6.z();
190
191 vert[18] = (float) v7.x();
192 vert[19] = (float) v7.y();
193 vert[20] = (float) v7.z();
194
195 vert[21] = (float) v8.x();
196 vert[22] = (float) v8.y();
197 vert[23] = (float) v8.z();
198
199 bs->AddBox(vert);
200 ++i;
201 }
202 bs->SetMainColor(kWhite);
203 bs->SetRenderMode(TEveBoxSet::kRM_Line);
204 gEve->AddElement(bs);
205 gEve->Redraw3D();
206}
207
209{
210 std::cout << "EventDisplay::DrawElossHits>" << std::endl;
211 TEvePointSet* ps = new TEvePointSet("Eloss");
212 for(int i=0;i<(int)event->Eloss->X.size();++i)
213 {
214 ps->SetNextPoint(event->Eloss->X[i]*100.0,
215 event->Eloss->Y[i]*100.0,
216 event->Eloss->Z[i]*100.0);
217 }
218 ps->SetMainColor(kWhite);
219 gEve->AddElement(ps);
220 gEve->Redraw3D();
221}
222
224{
225 std::cout << "EventDisplay::DrawTunnelHits>" << std::endl;
226
227 TEveBoxSet* bs = new TEveBoxSet("TunnelHits");
228 bs->Reset(TEveBoxSet::kBT_AABox, kFALSE,64);
229
230 for(int i=0;i<(int)event->TunnelHit->X.size();++i)
231 {
232 bs->AddBox(event->TunnelHit->X[i]*100.0,
233 event->TunnelHit->Y[i]*100.0,
234 event->TunnelHit->Z[i]*100.0,
235 -event->TunnelHit->energy[i]*5000.0,
236 -event->TunnelHit->energy[i]*5000.0,
237 -event->TunnelHit->energy[i]*5000.0);
238 }
239 bs->RefitPlex();
240
241 bs->SetMainColor(kWhite);
242 gEve->AddElement(bs);
243 gEve->Redraw3D();
244}
245
247{
248 std::cout << "EventDisplay::DrawSamplers>" << std::endl;
249
250 // loop over samplers
251 for(auto sampler : event->Samplers)
252 {
253 TEvePointSet* ps = new TEvePointSet((std::string("Sampler_")+sampler->samplerName.c_str()).c_str());
254
255 // Get coordinate tranform for sampler
256 TVector3 mpos = model->model->endPos[sampler->modelID];
257 TRotation mrot = model->model->endRot[sampler->modelID];
258
259 // loop over sampler hits
260 for(int i=0;i<sampler->n;++i)
261 {
262 TVector3 v = mpos + mrot*TVector3(sampler->x[i],sampler->y[i],0);
263 ps->SetNextPoint(v.x()*100.0,
264 v.y()*100.0,
265 v.z()*100.0);
266 }
267
268 ps->SetMainColor(kBlue);
269 gEve->AddElement(ps);
270 }
271 gEve->Redraw3D();
272}
273
275{
276 std::cout << "EventDisplay::DrawTrajectories>" << std::endl;
277 std::cout << "EventDisplay::DrawTrajectories> ntraj=" << event->Trajectory->XYZ.size() << std::endl;
278
279 int iTraj = 0;
280 for (auto t : event->Trajectory->XYZ)
281 {
282 std::string trajNameAppend = std::string();
283 if (event->Trajectory->trackID[iTraj] == 1)
284 {trajNameAppend = "_primary";}
285 else
286 {trajNameAppend = "_"+std::to_string(event->Trajectory->trackID[iTraj]);}
287
288 TEveLine* et = new TEveLine((std::string("Trajectory")+trajNameAppend).c_str());
289
290 for (auto tp : t)
291 {
292 //std::cout << tp.x() << " " << tp.y() << " " << tp.z() << std::endl;
293 et->SetNextPoint(tp.x()*100,
294 tp.y()*100,
295 tp.z()*100);
296 }
297 et->SetMainColor(kWhite);
298 gEve->AddElement(et);
299 gEve->Redraw3D();
300
301 iTraj++;
302 }
303}
std::vector< float > energy
Energy deposited in step.
Class to store all options for a BDSIM run.
Loader for a ROOT file using classes used to generate the file.
Definition DataLoader.hh:46
Event * GetEvent()
Accessor.
Definition DataLoader.hh:93
TChain * GetOptionsTree()
Accessor.
Definition DataLoader.hh:98
TChain * GetModelTree()
Accessor.
Definition DataLoader.hh:99
Options * GetOptions()
Accessor.
Definition DataLoader.hh:91
TChain * GetEventTree()
Accessor.
Model * GetModel()
Accessor.
Definition DataLoader.hh:92
Event viewer using ROOT EVE framework.
EventDisplay()
Private default constructor as singleton pattern.
void LoadData(int iEvt)
Load an entry in the event tree.
void DrawModel()
Draw the geometry.
static EventDisplay * Instance()
Singleton accessor.
void LoadGeometry()
Load the geometry from gdml file.
void DrawTrajectories()
Draw loaded trajectories.
void LoadModel(int iMod)
Load an entry from the model tree.
void ClearEvent()
Clear a currently displayed event.
void DrawSamplers()
Draw hits on a sampler plane.
void LoadOptions(int iOpt)
Load an entry from the options tree.
void DrawElossHits()
Draw energy loss deposits.
static EventDisplay * instance
Singleton instance.
void Draw()
Draw all things loaded at this point.
void DrawTunnelHits()
Draw tunnel energy deposits.
BDSOutputROOTEventLoss * TunnelHit
Local variable ROOT data is mapped to.
Definition Event.hh:136
BDSOutputROOTEventTrajectory * Trajectory
Local variable ROOT data is mapped to.
Definition Event.hh:137
BDSOutputROOTEventLoss * Eloss
Local variable ROOT data is mapped to.
Definition Event.hh:128
BDSOutputROOTEventModel * model
Member that ROOT can map file data to locally.
Definition Model.hh:63
BDSOutputROOTEventOptions * options
Member that ROOT can map file data to locally.
Definition Options.hh:47