Geant4  9.6.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4GeometryMessenger.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 // $Id$
28 //
29 // --------------------------------------------------------------------
30 // GEANT 4 class source file
31 //
32 // G4GeometryMessenger
33 //
34 // Author: G.Cosmo, CERN.
35 //
36 // --------------------------------------------------------------------
37 
38 #include <iomanip>
39 #include "G4GeometryMessenger.hh"
40 
42 #include "G4GeometryManager.hh"
43 #include "G4VPhysicalVolume.hh"
44 #include "G4Navigator.hh"
45 
46 #include "G4UIdirectory.hh"
47 #include "G4UIcommand.hh"
49 #include "G4UIcmdWith3Vector.hh"
51 #include "G4UIcmdWithABool.hh"
52 #include "G4UIcmdWithAnInteger.hh"
53 #include "G4UIcmdWithADouble.hh"
55 
57 #include "G4GeomTestVolume.hh"
58 
59 //
60 // Constructor
61 //
63  : x(0,0,0), p(0,0,1), grdRes(100,100,100), cylRes(90,50,50),
64  cylfZ(0.8), cylfR(0.8), newtol(false), tol(1E-4), // mm
65  recLevel(0), recDepth(-1), tmanager(tman), tlogger(0), tvolume(0)
66 {
67  geodir = new G4UIdirectory( "/geometry/" );
68  geodir->SetGuidance( "Geometry control commands." );
69 
70  //
71  // Geometry navigator commands
72  //
73  navdir = new G4UIdirectory( "/geometry/navigator/" );
74  navdir->SetGuidance( "Geometry navigator control setup." );
75 
76  resCmd = new G4UIcmdWithoutParameter( "/geometry/navigator/reset", this );
77  resCmd->SetGuidance( "Reset navigator and navigation history." );
78  resCmd->SetGuidance( "NOTE: must be called only after kernel has been" );
79  resCmd->SetGuidance( " initialized once through the run manager!" );
81 
82  verbCmd = new G4UIcmdWithAnInteger( "/geometry/navigator/verbose", this );
83  verbCmd->SetGuidance( "Set run-time verbosity for the navigator." );
84  verbCmd->SetGuidance(" 0 : Silent (default)");
85  verbCmd->SetGuidance(" 1 : Display volume positioning and step lengths");
86  verbCmd->SetGuidance(" 2 : Display step/safety info on point location");
87  verbCmd->SetGuidance(" 3 : Display minimal state at -every- step");
88  verbCmd->SetGuidance(" 4 : Maximum verbosity (very detailed!)");
89  verbCmd->SetGuidance( "NOTE: this command has effect -only- if Geant4 has" );
90  verbCmd->SetGuidance( " been installed with the G4VERBOSE flag set!" );
91  verbCmd->SetParameterName("level",true);
92  verbCmd->SetDefaultValue(0);
93  verbCmd->SetRange("level >=0 && level <=4");
94 
95  chkCmd = new G4UIcmdWithABool( "/geometry/navigator/check_mode", this );
96  chkCmd->SetGuidance( "Set navigator in -check_mode- state." );
97  chkCmd->SetGuidance( "This will cause extra checks to be applied during" );
98  chkCmd->SetGuidance( "navigation. More strict and less tolerant conditions" );
99  chkCmd->SetGuidance( "are applied. A run-time performance penalty may be" );
100  chkCmd->SetGuidance( "observed when the -check_mode- state is activated." );
101  chkCmd->SetGuidance( "NOTE: this command has effect -only- if Geant4 has" );
102  chkCmd->SetGuidance( " been installed with the G4VERBOSE flag set!" );
103  chkCmd->SetParameterName("checkFlag",true);
104  chkCmd->SetDefaultValue(false);
106 
107  pchkCmd = new G4UIcmdWithABool( "/geometry/navigator/push_notify", this );
108  pchkCmd->SetGuidance( "Set navigator verbosity push notifications." );
109  pchkCmd->SetGuidance( "This allows to disable/re-enable verbosity in" );
110  pchkCmd->SetGuidance( "navigation, when tracks may get stuck and require" );
111  pchkCmd->SetGuidance( "one artificial push along the direction by the" );
112  pchkCmd->SetGuidance( "navigator. Notification is active by default." );
113  pchkCmd->SetGuidance( "NOTE: this command has effect -only- if Geant4 has" );
114  pchkCmd->SetGuidance( " been installed with the G4VERBOSE flag set!" );
115  pchkCmd->SetParameterName("pushFlag",true);
116  pchkCmd->SetDefaultValue(true);
118 
119  //
120  // Geometry verification test commands
121  //
122  testdir = new G4UIdirectory( "/geometry/test/" );
123  testdir->SetGuidance( "Geometry verification control setup." );
124  testdir->SetGuidance( "Helps in detecting possible overlapping regions." );
125 
126  tolCmd = new G4UIcmdWithADoubleAndUnit( "/geometry/test/tolerance",this );
127  tolCmd->SetGuidance( "Set error tolerance value." );
128  tolCmd->SetGuidance( "Initial default value: 1E-4*mm." );
129  tolCmd->SetParameterName( "Tolerance", true, true );
130  tolCmd->SetDefaultValue( 1E-4 );
131  tolCmd->SetDefaultUnit( "mm" );
132  tolCmd->SetUnitCategory( "Length" );
133 
134  posCmd = new G4UIcmdWith3VectorAndUnit( "/geometry/test/position", this );
135  posCmd->SetGuidance( "Set starting position for the line_test." );
136  posCmd->SetParameterName( "X", "Y", "Z", true, true );
137  posCmd->SetDefaultUnit( "cm" );
138 
139  dirCmd = new G4UIcmdWith3VectorAndUnit( "/geometry/test/direction", this );
140  dirCmd->SetGuidance( "Set momentum direction for the line_test." );
141  dirCmd->SetGuidance( "Direction needs not to be a unit vector." );
142  dirCmd->SetParameterName( "Px", "Py", "Pz", true, true );
143  dirCmd->SetRange( "Px != 0 || Py != 0 || Pz != 0" );
144 
145  linCmd = new G4UIcmdWithABool( "/geometry/test/line_test", this );
146  linCmd->SetGuidance( "Performs test along a single specified direction/position." );
147  linCmd->SetGuidance( "Use position and direction commands to change default." );
148  linCmd->SetGuidance( "Initial default: position(0,0,0), direction(0,0,1)." );
149  linCmd->SetGuidance( "If recursion flag is set to TRUE, the intersection checks" );
150  linCmd->SetGuidance( "will be performed recursively in the geometry tree." );
151  linCmd->SetParameterName("recursionFlag",true);
152  linCmd->SetDefaultValue(false);
154 
155  grzCmd = new G4UIcmdWith3Vector( "/geometry/test/grid_cells", this );
156  grzCmd->SetGuidance( "Define resolution of grid geometry as number of cells," );
157  grzCmd->SetGuidance( "specifying them for each dimension, X, Y and Z." );
158  grzCmd->SetGuidance( "Will be applied to grid_test and recursive_test commands." );
159  grzCmd->SetGuidance( "Initial default values: X=100, Y=100, Z=100." );
160  grzCmd->SetParameterName( "X", "Y", "Z", true, true );
161  grzCmd->SetDefaultValue( G4ThreeVector(100, 100, 100) );
162 
163  grdCmd = new G4UIcmdWithABool( "/geometry/test/grid_test", this );
164  grdCmd->SetGuidance( "Start running the default grid test." );
165  grdCmd->SetGuidance( "A grid of lines parallel to a cartesian axis is used;" );
166  grdCmd->SetGuidance( "By default, only direct daughters of the mother volumes are checked." );
167  grdCmd->SetGuidance( "If recursion flag is set to TRUE, the intersection checks" );
168  grdCmd->SetGuidance( "will be performed recursively in the geometry tree." );
169  grdCmd->SetGuidance( "NOTE: the recursion may take a very long time," );
170  grdCmd->SetGuidance( " depending on the geometry complexity !");
171  grdCmd->SetParameterName("recursionFlag",true);
172  grdCmd->SetDefaultValue(false);
174 
175  cyzCmd = new G4UIcmdWith3Vector( "/geometry/test/cylinder_geometry", this );
176  cyzCmd->SetGuidance( "Define details of the cylinder geometry, specifying:" );
177  cyzCmd->SetGuidance( " nPhi - number of lines per Phi" );
178  cyzCmd->SetGuidance( " nZ - number of Z points" );
179  cyzCmd->SetGuidance( " nRho - number of Rho points" );
180  cyzCmd->SetGuidance( "Will be applied to the cylinder_test command." );
181  cyzCmd->SetGuidance( "Initial default values: nPhi=90, nZ=50, nRho=50." );
182  cyzCmd->SetParameterName( "nPhi", "nZ", "nRho", true, true );
183  cyzCmd->SetDefaultValue( G4ThreeVector(90, 50, 50) );
184 
185  cfzCmd = new G4UIcmdWithADouble( "/geometry/test/cylinder_scaleZ", this );
186  cfzCmd->SetGuidance( "Define the resolution of the cylinder geometry, specifying" );
187  cfzCmd->SetGuidance( "the fraction scale for points along Z." );
188  cfzCmd->SetGuidance( "Initial default values: fracZ=0.8" );
189  cfzCmd->SetParameterName("fracZ",true);
190  cfzCmd->SetDefaultValue(0.8);
191 
192  cfrCmd = new G4UIcmdWithADouble( "/geometry/test/cylinder_scaleRho", this );
193  cfrCmd->SetGuidance( "Define the resolution of the cylinder geometry, specifying" );
194  cfrCmd->SetGuidance( "the fraction scale for points along Rho." );
195  cfrCmd->SetGuidance( "Initial default values: fracRho=0.8" );
196  cfrCmd->SetParameterName("fracRho",true);
197  cfrCmd->SetDefaultValue(0.8);
198 
199  cylCmd = new G4UIcmdWithABool( "/geometry/test/cylinder_test", this );
200  cylCmd->SetGuidance( "Start running the cylinder test." );
201  cylCmd->SetGuidance( "A set of lines in a cylindrical pattern of gradually" );
202  cylCmd->SetGuidance( "increasing mesh size." );
203  cylCmd->SetGuidance( "By default, only direct daughters of the mother volumes are checked." );
204  cylCmd->SetGuidance( "If recursion flag is set to TRUE, the intersection checks" );
205  cylCmd->SetGuidance( "will be performed recursively in the geometry tree." );
206  cylCmd->SetGuidance( "NOTE: the recursion may take a very long time," );
207  cylCmd->SetGuidance( " depending on the geometry complexity !");
208  cylCmd->SetParameterName("recursionFlag",true);
209  cylCmd->SetDefaultValue(false);
211 
212  rcsCmd = new G4UIcmdWithAnInteger( "/geometry/test/recursion_start", this );
213  rcsCmd->SetGuidance( "Set the initial level in the geometry tree for recursion." );
214  rcsCmd->SetGuidance( "recursive_test will then start from the specified level." );
215  rcsCmd->SetParameterName("initial_level",true);
216  rcsCmd->SetDefaultValue(0);
217 
218  rcdCmd = new G4UIcmdWithAnInteger( "/geometry/test/recursion_depth", this );
219  rcdCmd->SetGuidance( "Set the depth in the geometry tree for recursion." );
220  rcdCmd->SetGuidance( "recursive_test will then stop after reached the specified depth." );
221  rcdCmd->SetGuidance( "By default, recursion will proceed for the whole depth." );
222  rcdCmd->SetParameterName("recursion_depth",true);
223  rcdCmd->SetDefaultValue(-1);
224 
225  // Obsolete verification commands ...
226 
227  runCmd = new G4UIcmdWithABool( "/geometry/test/run", this );
228  runCmd->SetGuidance( "Start running the default grid test." );
229  runCmd->SetGuidance( "Same as the grid_test command." );
230  runCmd->SetGuidance( "If recursion flag is set to TRUE, the intersection checks" );
231  runCmd->SetGuidance( "will be performed recursively in the geometry tree." );
232  runCmd->SetGuidance( "NOTE: the recursion may take a very long time," );
233  runCmd->SetGuidance( " depending on the geometry complexity !");
234  runCmd->SetParameterName("recursionFlag",true);
235  runCmd->SetDefaultValue(false);
237 
238  recCmd = new G4UIcmdWithoutParameter( "/geometry/test/recursive_test", this );
239  recCmd->SetGuidance( "Start running the recursive grid test." );
240  recCmd->SetGuidance( "A grid of lines along a cartesian axis is recursively" );
241  recCmd->SetGuidance( "to all daughters and daughters of daughters, etc." );
242  recCmd->SetGuidance( "NOTE: it may take a very long time," );
243  recCmd->SetGuidance( " depending on the geometry complexity !");
245 }
246 
247 //
248 // Destructor
249 //
251 {
252  delete linCmd; delete posCmd; delete dirCmd;
253  delete grzCmd; delete grdCmd; delete recCmd; delete runCmd;
254  delete rcsCmd; delete rcdCmd;
255  delete cyzCmd; delete cfzCmd; delete cfrCmd; delete cylCmd;
256  delete tolCmd;
257  delete resCmd; delete verbCmd; delete pchkCmd; delete chkCmd;
258  delete geodir; delete navdir; delete testdir;
259  delete tvolume; delete tlogger;
260 }
261 
262 //
263 // Init
264 //
265 void
266 G4GeometryMessenger::Init()
267 {
268  // Clean old allocated loggers...
269  //
270  if (tlogger) delete tlogger;
271  if (tvolume) delete tvolume;
272 
273  // Create a logger to send errors/output to cout
274  //
275  tlogger = new G4GeomTestStreamLogger(std::cout);
276 
277  // Get the world volume
278  //
279  G4VPhysicalVolume* world =
281 
282  // Test the actual detector...
283  //
284  tvolume = new G4GeomTestVolume(world, tlogger);
285 }
286 
287 //
288 // SetNewValue
289 //
290 void
292 {
293  if (command == resCmd) {
294  ResetNavigator();
295  }
296  else if (command == verbCmd) {
297  SetVerbosity( newValues );
298  }
299  else if (command == chkCmd) {
300  SetCheckMode( newValues );
301  }
302  else if (command == posCmd) {
303  x = posCmd->GetNew3VectorValue( newValues );
304  }
305  else if (command == dirCmd) {
306  p = dirCmd->GetNew3VectorValue( newValues );
307  if (p.mag() < DBL_MIN) {
308  G4cerr << "Please specify non-zero momentum!" << G4endl;
309  p = G4ThreeVector(0,0,1);
310  }
311  }
312  else if (command == tolCmd) {
313  tol = tolCmd->GetNewDoubleValue( newValues );
314  newtol = true;
315  }
316  else if (command == linCmd) {
317  Init();
318  if (linCmd->GetNewBoolValue( newValues ))
319  RecursiveLineTest();
320  else
321  LineTest();
322  }
323  else if ((command == grdCmd) || (command == runCmd)){
324  Init();
325  if (grdCmd->GetNewBoolValue( newValues ) || runCmd->GetNewBoolValue( newValues ))
326  RecursiveGridTest();
327  else
328  GridTest();
329  }
330  else if (command == grzCmd) {
331  grdRes = grzCmd->GetNew3VectorValue( newValues );
332  if (grdRes.mag() < DBL_MIN) {
333  G4cerr << "Please specify non-zero resolution!" << G4endl;
334  grdRes = G4ThreeVector(100,100,100);
335  }
336  }
337  else if (command == cyzCmd) {
338  cylRes = cyzCmd->GetNew3VectorValue( newValues );
339  }
340  else if (command == cfzCmd) {
341  cylfZ = cfzCmd->GetNewDoubleValue( newValues );
342  }
343  else if (command == cfrCmd) {
344  cylfR = cfrCmd->GetNewDoubleValue( newValues );
345  }
346  else if (command == rcsCmd) {
347  recLevel = rcsCmd->GetNewIntValue( newValues );
348  }
349  else if (command == rcdCmd) {
350  recDepth = rcdCmd->GetNewIntValue( newValues );
351  }
352  else if (command == recCmd) {
353  Init();
354  RecursiveGridTest();
355  }
356  else if (command == cylCmd) {
357  Init();
358  if (cylCmd->GetNewBoolValue( newValues ))
359  RecursiveCylinderTest();
360  else
361  CylinderTest();
362  }
363 }
364 
365 //
366 // GetCurrentValue
367 //
368 G4String
370 {
371  G4String cv = "";
372  if (command == posCmd) {
373  cv = posCmd->ConvertToString( x, "cm" );
374  }
375  else if (command == tolCmd) {
376  cv = tolCmd->ConvertToString( tol, "mm" );
377  }
378  else if (command == dirCmd) {
379  cv = dirCmd->ConvertToString( p, "GeV" );
380  }
381  return cv;
382 }
383 
384 //
385 // CheckGeometry
386 //
387 void
388 G4GeometryMessenger::CheckGeometry()
389 {
390  // Verify that the geometry is closed
391  //
393  if (!geomManager->IsGeometryClosed()) {
394  geomManager->OpenGeometry();
395  geomManager->CloseGeometry(true);
396  }
397 }
398 
399 //
400 // ResetNavigator
401 //
402 void
403 G4GeometryMessenger::ResetNavigator()
404 {
405  // Close geometry and reset optimisation if necessary
406  //
407  CheckGeometry();
408 
409  // Reset navigator's state
410  //
411  G4ThreeVector pt(0,0,0);
413  navigator->LocateGlobalPointAndSetup(pt,0,false);
414 }
415 
416 //
417 // Set navigator verbosity
418 //
419 void
420 G4GeometryMessenger::SetVerbosity(G4String input)
421 {
422  G4int level = verbCmd->GetNewIntValue(input);
423  G4Navigator* navigator = tmanager->GetNavigatorForTracking();
424  navigator->SetVerboseLevel(level);
425 }
426 
427 //
428 // Set navigator mode
429 //
430 void
431 G4GeometryMessenger::SetCheckMode(G4String input)
432 {
433  G4bool mode = chkCmd->GetNewBoolValue(input);
434  G4Navigator* navigator = tmanager->GetNavigatorForTracking();
435  navigator->CheckMode(mode);
436 }
437 
438 //
439 // Set navigator verbosity for push notifications
440 //
441 void
442 G4GeometryMessenger::SetPushFlag(G4String input)
443 {
444  G4bool mode = pchkCmd->GetNewBoolValue(input);
445  G4Navigator* navigator = tmanager->GetNavigatorForTracking();
446  navigator->SetPushVerbosity(mode);
447 }
448 
449 //
450 // LineTest
451 //
452 void
453 G4GeometryMessenger::LineTest()
454 {
455  // Close geometry if necessary
456  //
457  CheckGeometry();
458 
459  // Verify if error tolerance has changed
460  //
461  if (newtol) tvolume->SetTolerance(tol);
462 
463  // Make test on single line supplied by user
464  //
465  tvolume->TestOneLine( x, p );
466 
467  // Print out any errors, if found
468  //
469  tvolume->ReportErrors();
470 }
471 
472 //
473 // RecursiveLineTest
474 //
475 void
476 G4GeometryMessenger::RecursiveLineTest()
477 {
478  // Close geometry if necessary
479  //
480  CheckGeometry();
481 
482  // Verify if error tolerance has changed
483  //
484  if (newtol) tvolume->SetTolerance(tol);
485 
486  // Make test on single line supplied by user recursively
487  //
488  tvolume->TestRecursiveLine( x, p, recLevel, recDepth );
489 
490  // Print out any errors, if found
491  //
492  tvolume->ReportErrors();
493 }
494 
495 //
496 // GridTest
497 //
498 void
499 G4GeometryMessenger::GridTest()
500 {
501  // Close geometry if necessary
502  //
503  CheckGeometry();
504 
505  // Verify if error tolerance has changed
506  //
507  if (newtol) tvolume->SetTolerance(tol);
508 
509  // Apply set of trajectories in a 3D grid pattern
510  //
511  tvolume->TestCartGridXYZ( G4int(grdRes.x()),
512  G4int(grdRes.y()),
513  G4int(grdRes.z()) );
514 
515  // Print out any errors, if found
516  //
517  tvolume->ReportErrors();
518 }
519 
520 //
521 // RecursiveGridTest
522 //
523 void
524 G4GeometryMessenger::RecursiveGridTest()
525 {
526  // Close geometry if necessary
527  //
528  CheckGeometry();
529 
530  // Verify if error tolerance has changed
531  //
532  if (newtol) tvolume->SetTolerance(tol);
533 
534  // Apply set of trajectories in a 3D grid pattern recursively
535  //
536  tvolume->TestRecursiveCartGrid( G4int(grdRes.x()),
537  G4int(grdRes.y()),
538  G4int(grdRes.z()),
539  recLevel, recDepth );
540 
541  // Print out any errors, if found
542  //
543  tvolume->ReportErrors();
544 }
545 
546 //
547 // CylinderTest
548 //
549 void
550 G4GeometryMessenger::CylinderTest()
551 {
552  // Close geometry if necessary
553  //
554  CheckGeometry();
555 
556  // Verify if error tolerance has changed
557  //
558  if (newtol) tvolume->SetTolerance(tol);
559 
560  // Apply default set of lines in a cylindrical pattern of gradually
561  // increasing mesh size of trajectories in a 3D grid pattern
562  //
563  tvolume->TestCylinder(G4int(cylRes.x()),
564  G4int(cylRes.y()),
565  G4int(cylRes.z()),
566  cylfZ, cylfR, true);
567 
568  // Print out any errors, if found
569  //
570  tvolume->ReportErrors();
571 }
572 
573 //
574 // RecursiveCylinderTest
575 //
576 void
577 G4GeometryMessenger::RecursiveCylinderTest()
578 {
579  // Close geometry if necessary
580  //
581  CheckGeometry();
582 
583  // Verify if error tolerance has changed
584  //
585  if (newtol) tvolume->SetTolerance(tol);
586 
587  // Apply default set of lines in a cylindrical pattern of gradually
588  // increasing mesh size of trajectories in a 3D grid pattern recursively
589  //
590  tvolume->TestRecursiveCylinder(G4int(cylRes.x()),
591  G4int(cylRes.y()),
592  G4int(cylRes.z()),
593  cylfZ, cylfR, true,
594  recLevel, recDepth );
595 
596  // Print out any errors, if found
597  //
598  tvolume->ReportErrors();
599 }