[1316] | 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 | // SBTMessenger.cc |
---|
| 28 | // |
---|
| 29 | // Implementation of the messenger for controlling test 3 |
---|
| 30 | // |
---|
| 31 | |
---|
| 32 | #include "SBTMessenger.hh" |
---|
| 33 | #include "SBTrun.hh" |
---|
| 34 | #include "SBTVisManager.hh" |
---|
| 35 | #include "G4SolidQuery.hh" |
---|
| 36 | |
---|
| 37 | #include "G4ios.hh" |
---|
| 38 | #include "G4UIdirectory.hh" |
---|
| 39 | #include "G4UIcommand.hh" |
---|
| 40 | #include "G4UIcmdWith3VectorAndUnit.hh" |
---|
| 41 | #include "G4UIcmdWithAnInteger.hh" |
---|
| 42 | #include "G4UIcmdWithAString.hh" |
---|
| 43 | #include "G4UIcmdWithoutParameter.hh" |
---|
| 44 | |
---|
| 45 | #include <fstream> |
---|
| 46 | |
---|
| 47 | // |
---|
| 48 | // Constructor |
---|
| 49 | // |
---|
| 50 | SBTMessenger::SBTMessenger( const G4String prefix, const G4SolidQuery *theSolidQuery, SBTVisManager *theVisManager ) |
---|
| 51 | { |
---|
| 52 | // |
---|
| 53 | // Store solid query |
---|
| 54 | // |
---|
| 55 | solidQuery = theSolidQuery; |
---|
| 56 | |
---|
| 57 | // |
---|
| 58 | // Store visualization manager |
---|
| 59 | // |
---|
| 60 | visManager = theVisManager; |
---|
| 61 | |
---|
| 62 | // |
---|
| 63 | // Defaults (of locally stored values) |
---|
| 64 | // |
---|
| 65 | errorFile = "sbt.log"; |
---|
| 66 | |
---|
| 67 | // |
---|
| 68 | // Create tester |
---|
| 69 | // |
---|
| 70 | tester = new SBTrun(); |
---|
| 71 | |
---|
| 72 | // |
---|
| 73 | // Declare directory |
---|
| 74 | // |
---|
| 75 | test3Directory = new G4UIdirectory( prefix ); |
---|
| 76 | test3Directory->SetGuidance( "Controls for CSG batch test" ); |
---|
| 77 | |
---|
| 78 | // |
---|
| 79 | // Target command |
---|
| 80 | // |
---|
| 81 | G4String com = prefix+"target"; |
---|
| 82 | targetCmd = new G4UIcmdWith3VectorAndUnit( com, this ); |
---|
| 83 | targetCmd->SetGuidance( "Center of distribution of random points" ); |
---|
| 84 | targetCmd->SetParameterName( "X", "Y", "Z", true, true ); |
---|
| 85 | |
---|
| 86 | // |
---|
| 87 | // Widths command |
---|
| 88 | // |
---|
| 89 | com = prefix+"widths"; |
---|
| 90 | widthsCmd = new G4UIcmdWith3VectorAndUnit( com, this ); |
---|
| 91 | widthsCmd->SetGuidance( "Widths of distribution of random points" ); |
---|
| 92 | widthsCmd->SetParameterName( "Dx", "Dy", "Dz", true, true ); |
---|
| 93 | |
---|
| 94 | // |
---|
| 95 | // Grid Size command |
---|
| 96 | // |
---|
| 97 | com = prefix+"gridSizes"; |
---|
| 98 | gridSizesCmd = new G4UIcmdWith3VectorAndUnit( com, this ); |
---|
| 99 | gridSizesCmd->SetGuidance( "Grid size, or zero for no grid" ); |
---|
| 100 | gridSizesCmd->SetParameterName( "Dx", "Dy", "Dz", true, true ); |
---|
| 101 | |
---|
| 102 | // |
---|
| 103 | // Max Points command |
---|
| 104 | // |
---|
| 105 | com = prefix+"maxPoints"; |
---|
| 106 | maxPointsCmd = new G4UIcmdWithAnInteger( com, this ); |
---|
| 107 | maxPointsCmd->SetGuidance( "Maximum number of points before test ends" ); |
---|
| 108 | |
---|
| 109 | // |
---|
| 110 | // Max Errors command |
---|
| 111 | // |
---|
| 112 | com = prefix+"maxErrors"; |
---|
| 113 | maxErrorsCmd = new G4UIcmdWithAnInteger( com, this ); |
---|
| 114 | maxErrorsCmd->SetGuidance( "Maximum number of errors before test ends" ); |
---|
| 115 | |
---|
| 116 | // |
---|
| 117 | // Error filename command |
---|
| 118 | // |
---|
| 119 | com = prefix+"errorFileName"; |
---|
| 120 | errorFileCmd = new G4UIcmdWithAString( com, this ); |
---|
| 121 | errorFileCmd->SetGuidance( "Filename in which to send error listings" ); |
---|
| 122 | |
---|
| 123 | // |
---|
| 124 | // Run command |
---|
| 125 | // |
---|
| 126 | com = prefix+"run"; |
---|
| 127 | runCmd = new G4UIcmdWithoutParameter( com, this ); |
---|
| 128 | runCmd->SetGuidance( "Execute test 3" ); |
---|
| 129 | |
---|
| 130 | // |
---|
| 131 | // Debug commands |
---|
| 132 | // |
---|
| 133 | com = prefix+"draw"; |
---|
| 134 | drawCmd = new G4UIcmdWithAnInteger( com, this ); |
---|
| 135 | drawCmd->SetGuidance( "Draw error listed in log file" ); |
---|
| 136 | |
---|
| 137 | com = prefix+"debugInside"; |
---|
| 138 | debugInsideCmd = new G4UIcmdWithAnInteger( com, this ); |
---|
| 139 | debugInsideCmd->SetGuidance( "Call G4VSolid::Inside for error listed in log file" ); |
---|
| 140 | |
---|
| 141 | com = prefix+"debugToInP"; |
---|
| 142 | debugToInPCmd = new G4UIcmdWithAnInteger( com, this ); |
---|
| 143 | debugToInPCmd->SetGuidance( "Call G4VSolid::DistanceToIn(p) for error listed in log file" ); |
---|
| 144 | |
---|
| 145 | com = prefix+"debugToInPV"; |
---|
| 146 | debugToInPVCmd = new G4UIcmdWithAnInteger( com, this ); |
---|
| 147 | debugToInPVCmd->SetGuidance( "Call G4VSolid::DistanceToIn(p,v) for error listed in log file" ); |
---|
| 148 | |
---|
| 149 | com = prefix+"debugToOutP"; |
---|
| 150 | debugToOutPCmd = new G4UIcmdWithAnInteger( com, this ); |
---|
| 151 | debugToOutPCmd->SetGuidance( "Call G4VSolid::DistanceToOut(p) for error listed in log file" ); |
---|
| 152 | |
---|
| 153 | com = prefix+"debugToOutPV"; |
---|
| 154 | debugToOutPVCmd = new G4UIcmdWithAnInteger( com, this ); |
---|
| 155 | debugToOutPVCmd->SetGuidance( "Call G4VSolid::DistanceToOut(p,v) for error listed in log file" ); |
---|
| 156 | |
---|
| 157 | com = prefix+"debugSurfNorm"; |
---|
| 158 | debugSurfNormCmd = new G4UIcmdWithAnInteger( com, this ); |
---|
| 159 | debugSurfNormCmd->SetGuidance( "Call G4VSolid::SurfaceNormal(p) for error listed in log file" ); |
---|
| 160 | |
---|
| 161 | // |
---|
| 162 | // Pause command |
---|
| 163 | // |
---|
| 164 | com = prefix+"pause"; |
---|
| 165 | pauseCmd = new G4UIcmdWithoutParameter( com, this ); |
---|
| 166 | pauseCmd->SetGuidance( " Wait for a key " ); |
---|
| 167 | |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | |
---|
| 171 | // |
---|
| 172 | // Destructor |
---|
| 173 | // |
---|
| 174 | SBTMessenger::~SBTMessenger() |
---|
| 175 | { |
---|
| 176 | delete targetCmd; |
---|
| 177 | delete widthsCmd; |
---|
| 178 | delete gridSizesCmd; |
---|
| 179 | delete maxPointsCmd; |
---|
| 180 | delete maxErrorsCmd; |
---|
| 181 | delete errorFileCmd; |
---|
| 182 | delete test3Directory; |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | |
---|
| 186 | // |
---|
| 187 | // InvokeTest3 |
---|
| 188 | // |
---|
| 189 | // Run test 3 |
---|
| 190 | // |
---|
| 191 | void SBTMessenger::InvokeTest3() |
---|
| 192 | { |
---|
| 193 | // |
---|
| 194 | // Is there a Solid to test? |
---|
| 195 | // |
---|
| 196 | G4VSolid *testSolid = solidQuery->GetSolid(); |
---|
| 197 | if (testSolid == 0) { |
---|
| 198 | G4cerr << "Please initialize geometry before running test 3" << G4endl; |
---|
| 199 | G4cerr << "Test 3 ABORTED" << G4endl; |
---|
| 200 | return; |
---|
| 201 | } |
---|
| 202 | |
---|
| 203 | // |
---|
| 204 | // Open output file |
---|
| 205 | // |
---|
| 206 | std::ofstream logFile( errorFile ); |
---|
| 207 | |
---|
| 208 | // |
---|
| 209 | // Run the test |
---|
| 210 | // |
---|
| 211 | tester->RunTest( testSolid, logFile ); |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | |
---|
| 215 | // |
---|
| 216 | // Debug |
---|
| 217 | // |
---|
| 218 | // Run an event that should (approximately) duplicate the conditions |
---|
| 219 | // of one of the bugs discovered in a previous test3 run and stored |
---|
| 220 | // in a log file |
---|
| 221 | // |
---|
| 222 | void SBTMessenger::Debug( const G4int errorIndex, SBTMessenger::Debugger *debugger ) |
---|
| 223 | { |
---|
| 224 | // |
---|
| 225 | // Is there a Solid to test? |
---|
| 226 | // |
---|
| 227 | // If I was really clever (I'm not) I would also check |
---|
| 228 | // to make sure we have the same solid type and even the |
---|
| 229 | // same solid parameters. |
---|
| 230 | // |
---|
| 231 | G4VSolid *testSolid = solidQuery->GetSolid(); |
---|
| 232 | if (testSolid == 0) { |
---|
| 233 | G4cerr << "Please initialize geometry before debugging/drawing" << G4endl; |
---|
| 234 | return; |
---|
| 235 | } |
---|
| 236 | |
---|
| 237 | // |
---|
| 238 | // Open output file |
---|
| 239 | // |
---|
| 240 | std::ifstream logFile( errorFile ); |
---|
| 241 | if (!logFile) { |
---|
| 242 | G4cerr << "Cannot open input file " << errorFile << G4endl; |
---|
| 243 | return; |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | // |
---|
| 247 | // Debug |
---|
| 248 | // |
---|
| 249 | if (debugger->DebugMe( logFile, errorIndex )) { |
---|
| 250 | G4cerr << "Error reading index " << errorIndex << " from input file " << errorFile << G4endl; |
---|
| 251 | } |
---|
| 252 | } |
---|
| 253 | |
---|
| 254 | |
---|
| 255 | // |
---|
| 256 | // DebugMe (various classes) |
---|
| 257 | // |
---|
| 258 | G4int SBTMessenger::DrawError::DebugMe( std::ifstream &logFile, const G4int errorIndex ) |
---|
| 259 | { |
---|
| 260 | if (visManager) { |
---|
| 261 | // |
---|
| 262 | // Make sure we're kosher |
---|
| 263 | // |
---|
| 264 | if (visManager->BuildFakeWorld()) return 2; |
---|
| 265 | |
---|
| 266 | return tester->DrawError( testSolid, logFile, errorIndex, visManager ); |
---|
| 267 | } |
---|
| 268 | |
---|
| 269 | |
---|
| 270 | G4cerr << "Visualization is not available in this executable" << G4endl; |
---|
| 271 | return 1; |
---|
| 272 | } |
---|
| 273 | |
---|
| 274 | G4int SBTMessenger::DebugInside::DebugMe( std::ifstream &logFile, const G4int errorIndex ) |
---|
| 275 | { |
---|
| 276 | return tester->DebugInside( testSolid, logFile, errorIndex ); |
---|
| 277 | } |
---|
| 278 | |
---|
| 279 | G4int SBTMessenger::DebugToInP::DebugMe( std::ifstream &logFile, const G4int errorIndex ) |
---|
| 280 | { |
---|
| 281 | return tester->DebugToInP( testSolid, logFile, errorIndex ); |
---|
| 282 | } |
---|
| 283 | |
---|
| 284 | G4int SBTMessenger::DebugToInPV::DebugMe( std::ifstream &logFile, const G4int errorIndex ) |
---|
| 285 | { |
---|
| 286 | return tester->DebugToInPV( testSolid, logFile, errorIndex ); |
---|
| 287 | } |
---|
| 288 | |
---|
| 289 | G4int SBTMessenger::DebugToOutP::DebugMe( std::ifstream &logFile, const G4int errorIndex ) |
---|
| 290 | { |
---|
| 291 | return tester->DebugToOutP( testSolid, logFile, errorIndex ); |
---|
| 292 | } |
---|
| 293 | |
---|
| 294 | G4int SBTMessenger::DebugToOutPV::DebugMe( std::ifstream &logFile, const G4int errorIndex ) |
---|
| 295 | { |
---|
| 296 | return tester->DebugToOutPV( testSolid, logFile, errorIndex ); |
---|
| 297 | } |
---|
| 298 | |
---|
| 299 | G4int SBTMessenger::DebugSurfNorm::DebugMe( std::ifstream &logFile, const G4int errorIndex ) |
---|
| 300 | { |
---|
| 301 | return tester->DebugSurfNorm( testSolid, logFile, errorIndex ); |
---|
| 302 | } |
---|
| 303 | |
---|
| 304 | // |
---|
| 305 | // SetNewValue |
---|
| 306 | // |
---|
| 307 | // Call by the UI when user requests a change |
---|
| 308 | // |
---|
| 309 | void SBTMessenger::SetNewValue( G4UIcommand *command, G4String newValues ) |
---|
| 310 | { |
---|
| 311 | if (command == targetCmd) { |
---|
| 312 | tester->SetTarget( targetCmd->GetNew3VectorValue( newValues ) ); |
---|
| 313 | } |
---|
| 314 | else if (command == widthsCmd) { |
---|
| 315 | tester->SetWidths( widthsCmd->GetNew3VectorValue( newValues ) ); |
---|
| 316 | } |
---|
| 317 | else if (command == gridSizesCmd) { |
---|
| 318 | tester->SetGrids( gridSizesCmd->GetNew3VectorValue( newValues ) ); |
---|
| 319 | } |
---|
| 320 | else if (command == maxPointsCmd) { |
---|
| 321 | tester->SetMaxPoints( maxPointsCmd->GetNewIntValue( newValues ) ); |
---|
| 322 | } |
---|
| 323 | else if (command == maxErrorsCmd) { |
---|
| 324 | tester->SetMaxErrors( maxErrorsCmd->GetNewIntValue( newValues ) ); |
---|
| 325 | } |
---|
| 326 | else if (command == errorFileCmd) { |
---|
| 327 | errorFile = newValues; |
---|
| 328 | } |
---|
| 329 | else if (command == runCmd) { |
---|
| 330 | InvokeTest3(); |
---|
| 331 | } |
---|
| 332 | else if (command == drawCmd) { |
---|
| 333 | SBTMessenger::DrawError debugger( solidQuery->GetSolid(), tester, visManager ); |
---|
| 334 | Debug( drawCmd->GetNewIntValue( newValues ), &debugger ); |
---|
| 335 | } |
---|
| 336 | else if (command == debugInsideCmd) { |
---|
| 337 | SBTMessenger::DebugInside debugger( solidQuery->GetSolid(), tester ); |
---|
| 338 | Debug( debugInsideCmd->GetNewIntValue( newValues ), &debugger ); |
---|
| 339 | } |
---|
| 340 | else if (command == debugToInPCmd) { |
---|
| 341 | SBTMessenger::DebugToInP debugger( solidQuery->GetSolid(), tester ); |
---|
| 342 | Debug( debugToInPCmd->GetNewIntValue( newValues ), &debugger ); |
---|
| 343 | } |
---|
| 344 | else if (command == debugToInPVCmd) { |
---|
| 345 | SBTMessenger::DebugToInPV debugger( solidQuery->GetSolid(), tester ); |
---|
| 346 | Debug( debugToInPVCmd->GetNewIntValue( newValues ), &debugger ); |
---|
| 347 | } |
---|
| 348 | else if (command == debugToOutPCmd) { |
---|
| 349 | SBTMessenger::DebugToOutP debugger( solidQuery->GetSolid(), tester ); |
---|
| 350 | Debug( debugToOutPCmd->GetNewIntValue( newValues ), &debugger ); |
---|
| 351 | } |
---|
| 352 | else if (command == debugToOutPVCmd) { |
---|
| 353 | SBTMessenger::DebugToOutPV debugger( solidQuery->GetSolid(), tester ); |
---|
| 354 | Debug( debugToOutPVCmd->GetNewIntValue( newValues ), &debugger ); |
---|
| 355 | } |
---|
| 356 | else if (command == debugSurfNormCmd) { |
---|
| 357 | SBTMessenger::DebugSurfNorm debugger( solidQuery->GetSolid(), tester ); |
---|
| 358 | Debug( debugSurfNormCmd->GetNewIntValue( newValues ), &debugger ); |
---|
| 359 | } |
---|
| 360 | else if (command == pauseCmd) { |
---|
| 361 | char c; |
---|
| 362 | |
---|
| 363 | G4cout << "Press ENTER to continue..." << std::flush ; |
---|
| 364 | G4cin.get(c); |
---|
| 365 | } |
---|
| 366 | else { |
---|
| 367 | G4Exception( "Unrecognized command" ); |
---|
| 368 | } |
---|
| 369 | } |
---|
| 370 | |
---|
| 371 | |
---|
| 372 | // |
---|
| 373 | // GetCurrentValue |
---|
| 374 | // |
---|
| 375 | G4String SBTMessenger::GetCurrentValue( G4UIcommand *command ) |
---|
| 376 | { |
---|
| 377 | if (command == targetCmd) { |
---|
| 378 | return targetCmd->ConvertToString( tester->GetTarget(), "m" ); |
---|
| 379 | } |
---|
| 380 | else if (command == widthsCmd) { |
---|
| 381 | return widthsCmd->ConvertToString( tester->GetWidths(), "m" ); |
---|
| 382 | } |
---|
| 383 | else if (command == gridSizesCmd) { |
---|
| 384 | return gridSizesCmd->ConvertToString( tester->GetGrids(), "m" ); |
---|
| 385 | } |
---|
| 386 | else if (command == maxPointsCmd) { |
---|
| 387 | return maxPointsCmd->ConvertToString( tester->GetMaxPoints() ); |
---|
| 388 | } |
---|
| 389 | else if (command == maxErrorsCmd) { |
---|
| 390 | return maxErrorsCmd->ConvertToString( tester->GetMaxErrors() ); |
---|
| 391 | } |
---|
| 392 | else if (command == errorFileCmd) { |
---|
| 393 | return errorFile; |
---|
| 394 | } |
---|
| 395 | else if (command == runCmd) { |
---|
| 396 | return ""; |
---|
| 397 | } |
---|
| 398 | else if (command == drawCmd || |
---|
| 399 | command == debugInsideCmd || |
---|
| 400 | command == debugToInPCmd || |
---|
| 401 | command == debugToInPVCmd || |
---|
| 402 | command == debugToOutPCmd || |
---|
| 403 | command == debugToOutPVCmd || |
---|
| 404 | command == debugSurfNormCmd ) { |
---|
| 405 | return ""; |
---|
| 406 | } |
---|
| 407 | |
---|
| 408 | G4Exception( "Unrecognized command" ); |
---|
| 409 | return "foo!"; |
---|
| 410 | } |
---|
| 411 | |
---|