5#include "gAnalysisView.h"
8#include "G4UImanager.hh"
16void resetSceneBeforeGeometryReload(G4UImanager* g4uim) {
17 if (!g4uim) {
return; }
19 g4uim->ApplyCommand(
"/vis/viewer/set/autoRefresh false");
20 g4uim->ApplyCommand(
"/vis/scene/endOfEventAction accumulate 0");
21 g4uim->ApplyCommand(
"/vis/scene/endOfRunAction refresh");
22 g4uim->ApplyCommand(
"/vis/scene/removeModel all");
23 g4uim->ApplyCommand(
"/vis/viewer/clearTransients");
24 g4uim->ApplyCommand(
"/vis/viewer/clear");
25 g4uim->ApplyCommand(
"/vis/viewer/set/autoRefresh true");
26 g4uim->ApplyCommand(
"/vis/viewer/flush");
27 g4uim->ApplyCommand(
"/vis/viewer/clearTransients");
30void resetEventDrawingBeforeRun(G4UImanager* g4uim) {
31 if (!g4uim) {
return; }
33 g4uim->ApplyCommand(
"/vis/viewer/set/autoRefresh false");
34 g4uim->ApplyCommand(
"/vis/scene/endOfEventAction accumulate 0");
35 g4uim->ApplyCommand(
"/vis/scene/endOfRunAction refresh");
36 g4uim->ApplyCommand(
"/vis/viewer/clearTransients");
37 g4uim->ApplyCommand(
"/vis/viewer/clear");
38 g4uim->ApplyCommand(
"/vis/viewer/flush");
39 g4uim->ApplyCommand(
"/vis/viewer/clearTransients");
42void restoreSceneModels(G4UImanager* g4uim,
const std::shared_ptr<GOptions>& gopts,
bool includeEventModels) {
43 if (!g4uim) {
return; }
48 g4uim->ApplyCommand(
"/vis/viewer/set/autoRefresh false");
49 g4uim->ApplyCommand(
"/vis/scene/removeModel all");
50 g4uim->ApplyCommand(
"/vis/viewer/clearTransients");
51 g4uim->ApplyCommand(
"/vis/viewer/clear");
52 g4uim->ApplyCommand(
"/vis/drawVolume");
53 g4uim->ApplyCommand(
"/vis/viewer/set/background " + g4view.background);
54 g4uim->ApplyCommand(
"/vis/viewer/set/numberOfCloudPoints " + std::to_string(g4view.cloudPoints));
55 for (
const auto& command : g4SceneProperties.addSceneDecorations(gopts)) { g4uim->ApplyCommand(command); }
56 for (
const auto& command : g4SceneProperties.addSceneTexts(gopts)) { g4uim->ApplyCommand(command); }
57 if (includeEventModels) {
59 g4uim->ApplyCommand(
"/vis/scene/add/trajectories smooth");
60 g4uim->ApplyCommand(
"/vis/modeling/trajectories/create/drawByCharge");
61 g4uim->ApplyCommand(
"/vis/modeling/trajectories/drawByCharge-0/default/setDrawStepPts true");
62 g4uim->ApplyCommand(
"/vis/modeling/trajectories/drawByCharge-0/default/setStepPtsSize 2");
63 g4uim->ApplyCommand(
"/vis/scene/add/hits");
64 g4uim->ApplyCommand(
"/vis/scene/endOfEventAction accumulate 10000");
65 if (decorations.eventID) {
66 g4uim->ApplyCommand(
"/vis/scene/add/eventID " + std::to_string(decorations.eventIDSize));
69 g4uim->ApplyCommand(
"/vis/viewer/set/autoRefresh true");
70 g4uim->ApplyCommand(
"/vis/viewer/flush");
76 std::shared_ptr<EventDispenser> ed,
78 std::shared_ptr<GAnalysisAccumulator> analyzer,
79 bool viewerAlreadyInitialized,
83 analysisAccumulator(std::move(analyzer)),
85 detectorConstruction(dc),
86 viewerInitialized(viewerAlreadyInitialized) {
95 guiSession = std::make_unique<GUI_Session>(gopts,
gboard);
98 createRightContent(gopts, dc,
gboard);
101 auto* topLayout =
new QHBoxLayout;
102 createTopButtons(topLayout);
105 auto* bottomLayout =
new QHBoxLayout;
106 bottomLayout->setContentsMargins(0, 0, 0, 0);
107 bottomLayout->setSpacing(6);
109 bottomLayout->addWidget(leftButtons, 1);
110 bottomLayout->addWidget(rightContent, 10);
113 auto* mainLayout =
new QVBoxLayout;
114 mainLayout->setContentsMargins(6, 6, 6, 6);
115 mainLayout->setSpacing(6);
116 mainLayout->addLayout(topLayout);
117 mainLayout->addLayout(bottomLayout, 1);
118 mainLayout->addWidget(
gboard, 0);
120 setLayout(mainLayout);
121 setWindowTitle(tr(
"GEMC: Geant4 Monte-Carlo"));
123 setMinimumHeight(760);
124 resize(1000, std::max(sizeHint().height(), 760));
125 QTimer::singleShot(0,
this, [
this]() {
127 const int preferredHeight = std::max(sizeHint().height(), 760);
128 setMinimumHeight(preferredHeight);
129 resize(1000, preferredHeight);
133 gtimer =
new QTimer(
this);
134 connect(gtimer, SIGNAL(timeout()),
this, SLOT(cycleBeamOn()));
137 connect(leftButtons->buttonsWidget,
138 SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem*)),
139 this, SLOT(change_page(QListWidgetItem *, QListWidgetItem*)));
143void GemcGUI::updateGui() {
145 std::vector<std::string> sBefore =
148 int nThatWasRun = nEvents->text().toInt();
149 int nBefore = stoi(sBefore[2]);
151 QString newNEvents(
"Event Number: ");
152 newNEvents.append(std::to_string(nBefore + nThatWasRun).c_str());
154 eventNumberLabel->setText(newNEvents);
158void GemcGUI::resetVisualizationBeforeGeometryReload() {
159 if (analysisView !=
nullptr) { analysisView->clearForGeometryReload(); }
160 resetSceneBeforeGeometryReload(G4UImanager::GetUIpointer());
163void GemcGUI::refreshVisualizationFromOptions() {
164 if (!detectorConstruction || !detectorConstruction->has_built_geometry()) {
return; }
166 auto* g4uim = G4UImanager::GetUIpointer();
167 if (!g4uim) {
return; }
169 restoreSceneModels(g4uim, guiOptions,
false);
170 visualizationNeedsRunRestore =
true;
173void GemcGUI::refreshGeometryTree() {
174 if (!rightContent || !detectorConstruction || !guiOptions || !geometryTree) {
return; }
176 geometryReloadedSinceRun =
true;
178 const int treeIndex = rightContent->indexOf(geometryTree);
179 if (treeIndex < 0) {
return; }
181 const int currentIndex = rightContent->currentIndex();
182 auto* g4uim = G4UImanager::GetUIpointer();
183 if (!g4uim) {
return; }
185 const auto g4volumes = detectorConstruction->has_built_geometry()
186 ? detectorConstruction->get_g4volumes_map()
187 : std::unordered_map<std::string, G4Volume*>{};
188 const auto gvolumes = detectorConstruction->has_built_geometry()
189 ? detectorConstruction->get_gvolumes_flat_map()
190 : std::unordered_map<std::string, const GVolume*>{};
191 auto* refreshedTree =
new GTree(guiOptions, g4volumes, gvolumes);
193 rightContent->removeWidget(geometryTree);
194 geometryTree->deleteLater();
195 geometryTree = refreshedTree;
196 rightContent->insertWidget(treeIndex, geometryTree);
197 rightContent->setCurrentIndex(currentIndex == treeIndex ? treeIndex : currentIndex);
199 if (g4volumes.size() > 1) {
200 if (!viewerInitialized) {
202 G4SceneProperties g4SceneProperties(guiOptions);
203 auto commands = g4SceneProperties.scene_commands(guiOptions);
204 for (
const auto& command : commands) { g4uim->ApplyCommand(command); }
205 viewerInitialized =
true;
208 restoreSceneModels(g4uim, guiOptions,
false);
212void GemcGUI::prepareGeometryForBeamOn() {
213 if ((!geometryReloadedSinceRun && !visualizationNeedsRunRestore) || !detectorConstruction) {
return; }
214 if (!detectorConstruction->has_built_geometry()) {
return; }
216 auto* g4uim = G4UImanager::GetUIpointer();
217 resetEventDrawingBeforeRun(g4uim);
219 if (geometryReloadedSinceRun) {
220 detectorConstruction->prepare_geometry_for_run();
221 if (eventDispenser) {
222 eventDispenser->resetRunContext();
229 restoreSceneModels(g4uim, guiOptions,
true);
231 geometryReloadedSinceRun =
false;
232 visualizationNeedsRunRestore =
false;
242void GemcGUI::change_page(QListWidgetItem* current, QListWidgetItem* previous) {
250 rightContent->setCurrentIndex(thisIndex);
GemcGUI(std::shared_ptr< GOptions > gopts, std::shared_ptr< EventDispenser > ed, GDetectorConstruction *dc, std::shared_ptr< GAnalysisAccumulator > analysisAccumulator, bool viewerAlreadyInitialized=false, QWidget *parent=nullptr)
Construct the main GUI widget.
~GemcGUI() override
Destroy the GUI widget and release explicitly owned resources.
G4View getG4View(const std::shared_ptr< GOptions > &gopts)
G4Decorations getG4Decorations(const std::shared_ptr< GOptions > &gopts)
vector< std::string > getStringVectorFromString(const std::string &input)