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