source: trunk/source/geometry/navigation/src/G4GeometryMessenger.cc @ 831

Last change on this file since 831 was 831, checked in by garnier, 16 years ago

import all except CVS

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