Geant4 11.1.1
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
G4VisCommands.cc
Go to the documentation of this file.
1//
2// ********************************************************************
3// * License and Disclaimer *
4// * *
5// * The Geant4 software is copyright of the Copyright Holders of *
6// * the Geant4 Collaboration. It is provided under the terms and *
7// * conditions of the Geant4 Software License, included in the file *
8// * LICENSE and available at http://cern.ch/geant4/license . These *
9// * include a list of copyright holders. *
10// * *
11// * Neither the authors of this software system, nor their employing *
12// * institutes,nor the agencies providing financial support for this *
13// * work make any representation or warranty, express or implied, *
14// * regarding this software system or assume any liability for its *
15// * use. Please see the license in the file LICENSE and URL above *
16// * for the full disclaimer and the limitation of liability. *
17// * *
18// * This code implementation is the result of the scientific and *
19// * technical work of the GEANT4 collaboration. *
20// * By using, copying, modifying or distributing the software (or *
21// * any work based on the software) you agree to acknowledge its *
22// * use in resulting scientific publications, and indicate your *
23// * acceptance of all terms of the Geant4 Software license. *
24// ********************************************************************
25//
26//
27
28// /vis/ top level commands - John Allison 5th February 2001
29
30#include "G4VisCommands.hh"
31
32#include "G4VisManager.hh"
33#include "G4UImanager.hh"
34#include "G4UIcmdWithABool.hh"
35#include "G4UIcmdWithAString.hh"
37#include "G4RunManager.hh"
39#include "G4Run.hh"
40#include "G4UIsession.hh"
41#include "G4Trajectory.hh"
42#include "G4TrajectoryPoint.hh"
43#include "G4RichTrajectory.hh"
45#include "G4SmoothTrajectory.hh"
48#include "G4AttDef.hh"
49
50#define G4warn G4cout
51
52////////////// /vis/abortReviewKeptEvents /////////////////////////////
53
55 G4bool omitable;
56
57 fpCommand = new G4UIcmdWithABool("/vis/abortReviewKeptEvents", this);
58 fpCommand -> SetGuidance("Abort review of kept events.");
59 fpCommand -> SetParameterName("abort", omitable=true);
60 fpCommand -> SetDefaultValue(true);
61}
62
64 delete fpCommand;
65}
66
68 return G4String();
69}
70
72 G4String newValue) {
74 G4warn << "Type \"continue\" to complete the abort." << G4endl;
75}
76
77////////////// /vis/abortReviewPlots /////////////////////////////
78
80 G4bool omitable;
81
82 fpCommand = new G4UIcmdWithABool("/vis/abortReviewPlots", this);
83 fpCommand -> SetGuidance("Abort review of plots.");
84 fpCommand -> SetParameterName("abort", omitable=true);
85 fpCommand -> SetDefaultValue(true);
86}
87
89 delete fpCommand;
90}
91
93 return G4String();
94}
95
97 G4String newValue) {
99 G4warn << "Type \"continue\" to complete the abort." << G4endl;
100}
101
102////////////// /vis/drawOnlyToBeKeptEvents /////////////////////////////
103
105{
106 G4bool omitable;
107
108 fpCommand = new G4UIcmdWithABool("/vis/drawOnlyToBeKeptEvents", this);
109 fpCommand -> SetGuidance
110 ("DURING A RUN draw only those events that have been \"to be kept\" by the user"
111 "\nwith G4EventManager::GetEventManager()->KeepTheCurrentEvent() or"
112 "\nwith \"/event/keepCurrentEvent\".");
113 fpCommand -> SetGuidance(
114 "To draw selected events the user should set this flag, then in a user action:"
115 "\n if ( some criterion ) {"
116 "\n G4EventManager::GetEventManager()->KeepTheCurrentEvent();"
117 "\n }");
118 fpCommand -> SetParameterName("draw", omitable=true);
119 fpCommand -> SetDefaultValue(true);
120}
121
123 delete fpCommand;
124}
125
127 return G4String();
128}
129
131 G4String newValue) {
134 if (verbosity < G4VisManager::warnings) {
136 G4warn << "Only events that have been kept will be drawn." << G4endl;
137 } else {
138 G4warn << "All events will be drawn." << G4endl;
139 }
140 }
141}
142
143////////////// /vis/enable ///////////////////////////////////////
144
146 G4bool omitable;
147
148 fpCommand = new G4UIcmdWithABool("/vis/enable", this);
149 fpCommand -> SetGuidance("Enables/disables visualization system.");
150 fpCommand -> SetParameterName("enabled", omitable=true);
151 fpCommand -> SetDefaultValue(true);
152
153 fpCommand1 = new G4UIcmdWithoutParameter("/vis/disable", this);
154 fpCommand1 -> SetGuidance("Disables visualization system.");
155}
156
158 delete fpCommand;
159 delete fpCommand1;
160}
161
163 return G4String();
164}
165
167 G4String newValue) {
168 if (command == fpCommand) {
169 G4bool enable = G4UIcommand::ConvertToBool(newValue);
170 if (enable) fpVisManager->Enable(); // Printing is in vis manager.
171 else fpVisManager->Disable(); // Printing is in vis manager.
172 } else fpVisManager->Disable(); // Printing is in vis manager.
173 // Note: Printing is in vis manager.
174}
175
176////////////// /vis/initialize ///////////////////////////////////////
177
179{
180 fpCommand = new G4UIcmdWithoutParameter("/vis/initialize", this);
181 fpCommand -> SetGuidance("Initialise visualisation manager.");
182}
183
185 delete fpCommand;
186}
187
189 G4String) {
191}
192
193////////////// /vis/list ///////////////////////////////////////
194
196{
197 G4bool omitable;
198
199 fpCommand = new G4UIcmdWithAString("/vis/list", this);
200 fpCommand -> SetGuidance("Lists visualization parameters.");
201 fpCommand -> SetParameterName("verbosity", omitable=true);
202 fpCommand -> SetDefaultValue("warnings");
203}
204
206{
207 delete fpCommand;
208}
209
211{
212 return "";
213}
214
216{
217 G4String& verbosityString = newValue;
218 G4VisManager::Verbosity verbosity =
219 fpVisManager->GetVerbosityValue(verbosityString);
220
222 G4cout << G4endl;
223 fpVisManager->PrintAvailableModels(verbosity);
224 G4cout << G4endl;
225 fpVisManager->PrintAvailableUserVisActions(verbosity);
226 G4cout << G4endl;
227 fpVisManager->PrintAvailableColours(verbosity);
228 G4cout << G4endl;
230 UImanager->ApplyCommand("/vis/scene/list ! " + newValue);
231 UImanager->ApplyCommand("/vis/viewer/list ! " + newValue);
232
233 G4cout <<
234 "\nAttributes available for modeling and filtering with"
235 "\n \"/vis/modeling/trajectories/create/drawByAttribute\" and"
236 "\n \"/vis/filtering/trajectories/create/attributeFilter\" commands"
237 "\nand by picking:"
238 << G4endl;
239 G4cout
241 G4cout
244 G4cout
247 G4cout
250
251 G4cout <<
252 "\nGeometry attributes available for touchables by picking:\n";
253 G4cout
255
256 if (verbosity < G4VisManager::parameters)
257 G4cout <<
258 "\nTo get more information, \"/vis/list all\" or use individual commands"
259 "\n such as (use \"ls\" or \"help\"):"
260 "\n /vis/scene/list all all"
261 "\n /vis/viewer/list all all"
262 "\n /vis/modeling/trajectories/list"
263 "\n /vis/filtering/trajectories/list"
264 << G4endl;
265}
266
267////////////// /vis/reviewKeptEvents ///////////////////////////////////////
268
270{
271 G4bool omitable;
272
273 fpCommand = new G4UIcmdWithAString("/vis/reviewKeptEvents", this);
274 fpCommand -> SetGuidance("Review kept events.");
275 fpCommand -> SetGuidance
276 ("If a macro file is specified, it is executed for each event.");
277 fpCommand -> SetGuidance(
278 "If a macro file is not specified, each event is drawn to the current"
279 "\nviewer. After each event, the session is paused. The user may issue"
280 "\nany allowed command. Then enter \"cont[inue]\" to continue to the next"
281 "\nevent."
282 "\nUseful commands might be:"
283 "\n \"/vis/viewer/...\" to change the view (zoom, set/viewpoint,...)."
284 "\n \"/vis/ogl/export\" to get hard copy."
285 "\n \"/vis/open\" to get alternative viewer."
286 "\n \"/vis/abortReviewKeptEvents\", then \"cont[inue]\", to abort.");
287 fpCommand -> SetParameterName("macro-file-name", omitable=true);
288 fpCommand -> SetDefaultValue("");
289}
290
292{
293 delete fpCommand;
294}
295
297{
298 return "";
299}
300
302{
304 G4warn <<
305 "\"/vis/reviewKeptEvents\" not allowed within an already started review."
306 "\n No action taken."
307 << G4endl;
308 return;
309 }
310
311 G4String& macroFileName = newValue;
313
315 const G4Run* run = runManager ? runManager->GetCurrentRun() : nullptr;
316 const std::vector<const G4Event*>* events =
317 run ? run->GetEventVector() : nullptr;
318 size_t nKeptEvents = events ? events->size() : 0;
319
320 if (!nKeptEvents) {
321 if (verbosity >= G4VisManager::errors) {
322 G4warn <<
323 "ERROR: G4VisCommandReviewKeptEvents::SetNewValue: No kept events,"
324 "\n or kept events not accessible."
325 << G4endl;
326 }
327 return;
328 }
329
331 if (!viewer) {
332 if (verbosity >= G4VisManager::errors) {
333 G4warn <<
334 "ERROR: No current viewer - \"/vis/viewer/list\" to see possibilities."
335 << G4endl;
336 }
337 return;
338 }
339
341 if (!pScene) {
342 if (verbosity >= G4VisManager::errors) {
343 G4warn << "ERROR: No current scene. Please create one." << G4endl;
344 }
345 return;
346 }
347
349 G4int keepControlVerbose = UImanager->GetVerboseLevel();
350 G4int newVerbose(0);
351 if (keepControlVerbose >= 2 || verbosity >= G4VisManager::confirmations)
352 newVerbose = 2;
353 UImanager->SetVerboseLevel(newVerbose);
354
355 G4VVisManager* keepConcreteInstance = fpVisManager->GetConcreteInstance();
357
358 // Start on clean view
359 UImanager->ApplyCommand("/vis/viewer/rebuild");
360
361 // Event by event refreshing...
363 G4bool currentRefreshAtEndOfEvent = pScene->GetRefreshAtEndOfEvent();
364 pScene->SetRefreshAtEndOfEvent(true);
365 if (macroFileName.empty()) {
366
367 // Draw to viewer and pause session...
368 G4UIsession* session = UImanager->GetSession();
369 for (size_t i = 0; i < nKeptEvents; ++i) {
370 const G4Event* event = (*events)[i];
371 if (verbosity >= G4VisManager::warnings) {
372 G4warn << "Drawing event : " << event->GetEventID() <<
373 ". At EndOfEvent, enter any command, then \"cont[inue]\"..."
374 << G4endl;
375 static G4bool first = true;
376 if (first) {
377 first = false;
378 G4warn <<
379 " Useful commands might be:"
380 "\n \"/vis/scene/add/trajectories\" if not already added."
381 "\n \"/vis/viewer/...\" to change the view (zoom, set/viewpoint,...)."
382 "\n \"/vis/ogl/export\" to get hard copy."
383 "\n \"/vis/open\" to get alternative viewer."
384 "\n \"/vis/abortReviewKeptEvents\", then \"cont[inue]\", to abort."
385 << G4endl;
386 }
387 }
389 UImanager->ApplyCommand("/vis/viewer/rebuild");
390 /* The above command forces a rebuild of the scene, including
391 the detector. This is fine for "immediate" viewers - a
392 refresh requires a rebuild anyway. But for "stored mode"
393 viewers, you could, in principle, avoid a rebuild of the
394 detector with something like the following:
395 sceneHandler->ClearTransientStore();
396 viewer->DrawView();
397 sceneHandler->DrawEvent(event);
398 but this causes mayhem for "immediate" viewers because
399 ClearTransientStore issues a DrawView and some curious sort
400 of recursion takes place. For "stored" viewers, the event
401 gets drawn but not the eventID, so something odd is happening
402 there too. This needs further investigation - enhanced
403 features or a complete re-think.
404 */
405 UImanager->ApplyCommand("/vis/viewer/flush");
406 session->PauseSessionStart("EndOfEvent");
409 }
411
412 } else {
413
414 // Execute macro file...
415 for (size_t i = 0; i < nKeptEvents; ++i) {
416 const G4Event* event = (*events)[i];
417 if (verbosity >= G4VisManager::warnings) {
418 G4warn << "Drawing event : " << event->GetEventID()
419 << " with macro file \"" << macroFileName << G4endl;
420 }
422 UImanager->ApplyCommand("/control/execute " + macroFileName);
424 }
425 }
426 pScene->SetRefreshAtEndOfEvent(currentRefreshAtEndOfEvent);
428
429 if (keepConcreteInstance) fpVisManager->Enable();
430 else fpVisManager->Disable();
431 UImanager->SetVerboseLevel(keepControlVerbose);
432}
433
434////////////// /vis/reviewPlots ///////////////////////////////////////
435
437{
438 fpCommand = new G4UIcmdWithoutParameter("/vis/reviewPlots", this);
439 fpCommand -> SetGuidance("Review plots.");
440 fpCommand -> SetGuidance
441 ("Each plot is drawn, one by one, to the current viewer. After each"
442 "\nplot the session is paused. The user may issue any allowed command."
443 "\nThen enter \"cont[inue]\" to continue to the next plot."
444 "\nUseful commands might be:"
445 "\n \"/vis/tsg/export\" to get hard copy."
446 "\n \"/vis/abortReviewPlots\", then \"cont[inue]\", to abort.");
447}
448
450{
451 delete fpCommand;
452}
453
455{
456 return "";
457}
458
459#include <tools/histo/h1d>
460#include <tools/histo/h2d>
461
462namespace {
463 template <typename HT> // tools::histo::h1d, etc
464 G4bool ReviewPlots(const G4String& plotType) { // h1, etc.
465 auto visManager = G4VisManager::GetInstance();
466 auto ui = G4UImanager::GetUIpointer();
467 auto session = ui->GetSession();
468 G4bool aborting = false;
469 auto keepControlVerbose = ui->GetVerboseLevel();
470 ui->SetVerboseLevel(0);
471 auto status = ui->ApplyCommand("/analysis/" + plotType + "/getVector");
472 ui->SetVerboseLevel(keepControlVerbose);
474 G4String hexString = ui->GetCurrentValues(G4String("/analysis/" + plotType + "/getVector"));
475 if(hexString.size()) {
476 void* ptr;
477 std::istringstream is(hexString);
478 is >> ptr;
479 auto _v = (const std::vector<HT*>*)ptr;
480 auto _n = _v->size();
481 for (size_t i = 0; i < _n; ++i) {
482 // Draw then pause session...
483 std::ostringstream oss;
484 oss << "/vis/plot " << plotType << ' ' << i;
485 ui->ApplyCommand(oss.str());
486 session->PauseSessionStart("EndOfEvent");
487 if (visManager->GetAbortReviewPlots()) {
488 aborting = true;
489 break;
490 }
491 }
492 }
493 }
494 return aborting;
495 }
496}
497
499{
501 G4warn <<
502 "\"/vis/reviewPlots\" not allowed within an already started review."
503 "\n No action taken."
504 << G4endl;
505 return;
506 }
507
508 auto verbosity = fpVisManager->GetVerbosity();
509
510 auto currentViewer = fpVisManager->GetCurrentViewer();
511 if (!currentViewer) {
512 if (verbosity >= G4VisManager::errors) {
513 G4warn <<
514 "ERROR: No current viewer - \"/vis/viewer/list\" to see possibilities."
515 << G4endl;
516 }
517 return;
518 }
519
520 if (currentViewer->GetName().find("TOOLSSG") == std::string::npos) {
521 G4warn <<
522 "WARNING: Current viewer not able to draw plots."
523 "\n Try \"/vis/open TSG\", then \"/vis/reviewPlots\" again."
524 << G4endl;
525 return;
526 }
527
529 if (!pScene) {
530 if (verbosity >= G4VisManager::errors) {
531 G4warn << "ERROR: No current scene. Please create one." << G4endl;
532 }
533 return;
534 }
535
536 auto ui = G4UImanager::GetUIpointer();
537
538 auto keepControlVerbose = ui->GetVerboseLevel();
539 ui->SetVerboseLevel(0);
540 auto keepVisVerbose = fpVisManager->GetVerbosity();
542 auto keepEnable = fpVisManager->IsEnabled();
545
546 if (ReviewPlots<tools::histo::h1d>("h1")) goto finish; // Aborting?
547 if (ReviewPlots<tools::histo::h2d>("h2")) goto finish; // Aborting?
548
549finish:
551 if (!keepEnable) fpVisManager->Disable();
552 fpVisManager->SetVerboseLevel(keepVisVerbose);
553 ui->SetVerboseLevel(keepControlVerbose);
554}
555
556////////////// /vis/verbose ///////////////////////////////////////
557
559 G4bool omitable;
560
561 fpCommand = new G4UIcmdWithAString("/vis/verbose", this);
562 for (size_t i = 0; i < G4VisManager::VerbosityGuidanceStrings.size(); ++i) {
563 fpCommand -> SetGuidance(G4VisManager::VerbosityGuidanceStrings[i]);
564 }
565 fpCommand -> SetParameterName("verbosity", omitable=true);
566 fpCommand -> SetDefaultValue("warnings");
567}
568
570 delete fpCommand;
571}
572
574 return G4String();
575}
576
578 G4String newValue) {
579 G4VisManager::Verbosity verbosity =
581 fpVisManager->SetVerboseLevel(verbosity);
582 // Always prints whatever the verbosity...
583 G4cout << "Visualization verbosity changed to "
584 << G4VisManager::VerbosityString(verbosity) << G4endl;
585}
#define G4warn
Definition: G4Scene.cc:41
bool G4bool
Definition: G4Types.hh:86
int G4int
Definition: G4Types.hh:85
@ fCommandSucceeded
#define G4endl
Definition: G4ios.hh:57
G4GLOB_DLL std::ostream G4cout
const std::map< G4String, G4AttDef > * GetAttDefs() const
virtual const std::map< G4String, G4AttDef > * GetAttDefs() const
virtual const std::map< G4String, G4AttDef > * GetAttDefs() const
static G4RunManager * GetMasterRunManager()
const G4Run * GetCurrentRun() const
Definition: G4Run.hh:49
const std::vector< const G4Event * > * GetEventVector() const
Definition: G4Run.hh:96
G4bool GetRefreshAtEndOfEvent() const
void SetRefreshAtEndOfEvent(G4bool)
virtual const std::map< G4String, G4AttDef > * GetAttDefs() const
virtual const std::map< G4String, G4AttDef > * GetAttDefs() const
const std::map< G4String, G4AttDef > * GetAttDefs() const
virtual const std::map< G4String, G4AttDef > * GetAttDefs() const
virtual const std::map< G4String, G4AttDef > * GetAttDefs() const
static G4bool ConvertToBool(const char *st)
Definition: G4UIcommand.cc:549
G4int ApplyCommand(const char *aCommand)
Definition: G4UImanager.cc:495
G4int GetVerboseLevel() const
Definition: G4UImanager.hh:200
G4UIsession * GetSession() const
Definition: G4UImanager.hh:187
static G4UImanager * GetUIpointer()
Definition: G4UImanager.cc:77
void SetVerboseLevel(G4int val)
Definition: G4UImanager.hh:199
static G4VisManager * fpVisManager
static G4VVisManager * GetConcreteInstance()
void SetNewValue(G4UIcommand *command, G4String newValue)
G4String GetCurrentValue(G4UIcommand *command)
void SetNewValue(G4UIcommand *command, G4String newValue)
G4String GetCurrentValue(G4UIcommand *command)
void SetNewValue(G4UIcommand *command, G4String newValue)
G4String GetCurrentValue(G4UIcommand *command)
virtual ~G4VisCommandEnable()
G4String GetCurrentValue(G4UIcommand *command)
void SetNewValue(G4UIcommand *command, G4String newValue)
virtual ~G4VisCommandInitialize()
void SetNewValue(G4UIcommand *command, G4String newValue)
void SetNewValue(G4UIcommand *command, G4String newValue)
G4String GetCurrentValue(G4UIcommand *command)
virtual ~G4VisCommandList()
void SetNewValue(G4UIcommand *command, G4String newValue)
G4String GetCurrentValue(G4UIcommand *command)
virtual ~G4VisCommandReviewPlots()
void SetNewValue(G4UIcommand *command, G4String newValue)
G4String GetCurrentValue(G4UIcommand *command)
void SetNewValue(G4UIcommand *command, G4String newValue)
G4String GetCurrentValue(G4UIcommand *command)
virtual ~G4VisCommandVerbose()
void Initialize()
G4Scene * GetCurrentScene() const
void PrintAvailableGraphicsSystems(Verbosity, std::ostream &=G4cout) const
G4bool IsEnabled() const
void SetDrawEventOnlyIfToBeKept(G4bool)
static std::vector< G4String > VerbosityGuidanceStrings
static G4String VerbosityString(Verbosity)
G4bool GetAbortReviewKeptEvents() const
void SetAbortReviewPlots(G4bool)
void SetAbortReviewKeptEvents(G4bool)
void SetReviewingPlots(G4bool)
G4VViewer * GetCurrentViewer() const
void SetRequestedEvent(const G4Event *)
G4bool GetDrawEventOnlyIfToBeKept() const
void SetReviewingKeptEvents(G4bool)
G4bool GetReviewingPlots() const
static Verbosity GetVerbosity()
void SetVerboseLevel(G4int)
G4bool GetReviewingKeptEvents() const
static Verbosity GetVerbosityValue(const G4String &)
static G4VisManager * GetInstance()