G4ErrorPropagatorManager.cc

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 // $Id: G4ErrorPropagatorManager.cc 69766 2013-05-14 14:33:55Z gcosmo $
00027 //
00028 // ------------------------------------------------------------
00029 //      GEANT 4 class implementation file 
00030 // ------------------------------------------------------------
00031 //
00032 
00033 #include "G4ErrorPropagatorManager.hh"
00034 
00035 #include "G4SystemOfUnits.hh"
00036 #include "G4MagIntegratorStepper.hh"
00037 #include "G4Mag_UsualEqRhs.hh"
00038 #include "G4Mag_EqRhs.hh"
00039 #include "G4MagIntegratorDriver.hh"
00040 
00041 #include "G4ClassicalRK4.hh"
00042 #include "G4ExactHelixStepper.hh"
00043 #include "G4HelixExplicitEuler.hh"
00044 
00045 #include "G4EventManager.hh"
00046 #include "G4ErrorRunManagerHelper.hh"
00047 #include "G4ErrorPropagator.hh"
00048 #include "G4ErrorMag_UsualEqRhs.hh"
00049 
00050 #include "G4VParticleChange.hh"
00051 #include "G4ParticleChangeForMSC.hh"
00052 #include "G4ParticleChange.hh"
00053 #include "G4Track.hh"
00054 #include "G4TransportationManager.hh"
00055 #include "G4ErrorPropagationNavigator.hh"
00056 #include "G4GeometryManager.hh"
00057 #include "G4StateManager.hh"
00058 #include "G4ChordFinder.hh"
00059 #include "G4EquationOfMotion.hh"
00060 #include "G4FieldManager.hh"
00061 #include "G4PropagatorInField.hh"
00062 #include "G4RunManager.hh"
00063 #include "G4VParticleChange.hh"
00064 
00065 G4ErrorPropagatorManager*
00066 G4ErrorPropagatorManager::theG4ErrorPropagatorManager = 0;
00067 
00068 //-----------------------------------------------------------------------
00069 G4ErrorPropagatorManager* G4ErrorPropagatorManager::GetErrorPropagatorManager()
00070 {
00071   if( !theG4ErrorPropagatorManager ) {
00072     theG4ErrorPropagatorManager = new G4ErrorPropagatorManager;
00073   }
00074 
00075   return theG4ErrorPropagatorManager;
00076 }
00077 
00078 
00079 //-----------------------------------------------------------------------
00080 G4ErrorPropagatorManager::G4ErrorPropagatorManager()
00081 {
00082   //----- Initialize a few things
00083   //o  theG4ErrorPropagatorManager = this;
00084 
00085   char* g4emverb = getenv("G4EVERBOSE");
00086   if( !g4emverb ) {
00087     G4ErrorPropagatorData::GetErrorPropagatorData()->SetVerbose( 0 );
00088   } else {
00089     G4ErrorPropagatorData::GetErrorPropagatorData()->SetVerbose( atoi( g4emverb ) );
00090   }
00091 
00092   thePropagator = 0;
00093 
00094   theEquationOfMotion = 0;
00095 
00096   StartG4ErrorRunManagerHelper(); 
00097   
00098   G4ErrorPropagatorData::GetErrorPropagatorData()->SetState( G4ErrorState_PreInit );
00099 
00100   theG4ErrorPropagationNavigator = 0;
00101 
00102   StartNavigator(); //navigator has to be initialized at the beggining !?!?!
00103 
00104 
00105 }
00106 
00107 
00108 //-----------------------------------------------------------------------
00109 G4ErrorPropagatorManager::~G4ErrorPropagatorManager()
00110 {
00111   delete theEquationOfMotion;
00112   delete theG4ErrorPropagationNavigator;
00113   delete thePropagator;
00114   delete theG4ErrorRunManagerHelper;
00115   delete theG4ErrorPropagatorManager;
00116 }
00117 
00118 
00119 //-----------------------------------------------------------------------
00120 void G4ErrorPropagatorManager::StartG4ErrorRunManagerHelper()
00121 {
00122   //----- Initialize G4ErrorRunManagerHelper
00123   theG4ErrorRunManagerHelper = G4ErrorRunManagerHelper::GetRunManagerKernel();
00124 
00125   if( theG4ErrorRunManagerHelper == 0 ) {
00126     theG4ErrorRunManagerHelper = new G4ErrorRunManagerHelper();
00127   }
00128 
00129   //----- User Initialization classes 
00130   //--- GEANT4e PhysicsList
00131   if( G4ErrorPropagatorData::verbose() >= 4 ) G4cout << " G4ErrorPropagatorManager::StartG4eRunManager() done " << theG4ErrorRunManagerHelper << G4endl;
00132   //-  theG4eRunManager->SetUserInitialization(new G4ErrorPhysicsList);
00133 
00134 }
00135 
00136 
00137 //-----------------------------------------------------------------------
00138 void G4ErrorPropagatorManager::StartNavigator()
00139 {
00140   if( theG4ErrorPropagationNavigator == 0 ) {
00141     G4TransportationManager*transportationManager = G4TransportationManager::GetTransportationManager();
00142     
00143     G4Navigator* g4navi = transportationManager->GetNavigatorForTracking();
00144    
00145     G4VPhysicalVolume* world = g4navi->GetWorldVolume();
00146     G4int verb = g4navi->GetVerboseLevel();
00147     delete g4navi;
00148 
00149     theG4ErrorPropagationNavigator = new G4ErrorPropagationNavigator();
00150 
00151     if( world != 0 ) {
00152       theG4ErrorPropagationNavigator->SetWorldVolume( world );
00153     }
00154     theG4ErrorPropagationNavigator->SetVerboseLevel( verb );   
00155     
00156     transportationManager->SetNavigatorForTracking(theG4ErrorPropagationNavigator);
00157     transportationManager->GetPropagatorInField()->GetIntersectionLocator()
00158                          ->SetNavigatorFor(theG4ErrorPropagationNavigator);
00159     G4EventManager::GetEventManager()->GetTrackingManager()->GetSteppingManager()
00160                          ->SetNavigator(theG4ErrorPropagationNavigator);
00161     //  G4ThreeVector center(0,0,0);
00162     //  theG4ErrorPropagationNavigator->LocateGlobalPointAndSetup(center,0,false);
00163     
00164   }
00165 
00166   if( G4ErrorPropagatorData::verbose() >= 2 ) G4cout << " theState at StartNavigator " << PrintG4ErrorState() << G4endl;
00167 
00168 }
00169   
00170 
00171 //-----------------------------------------------------------------------
00172 void G4ErrorPropagatorManager::InitGeant4e()
00173 {
00174   if( G4ErrorPropagatorData::verbose() >= 1 ) G4cout << "InitGeant4e GEANT4e State= " << PrintG4ErrorState() << " GEANT4 State= " << PrintG4State() << G4endl;
00175   G4ApplicationState currentState = G4StateManager::GetStateManager()->GetCurrentState();
00176   //----- Initialize run
00177   //  if( G4StateManager::GetStateManager()->GetCurrentState() == G4State_PreInit) {
00178   
00179   if( G4ErrorPropagatorData::GetErrorPropagatorData()->GetState() == G4ErrorState_PreInit ) {
00180     if ( currentState == G4State_PreInit || currentState == G4State_Idle) {
00181       //    G4eRunManager::GetRunManager()->Initialize();
00182       theG4ErrorRunManagerHelper->InitializeGeometry();
00183       theG4ErrorRunManagerHelper->InitializePhysics();
00184     }
00185     
00186     InitFieldForBackwards();
00187     
00188     //-    G4StateManager::GetStateManager()->SetNewState(G4State_Idle);
00189     
00190     if( G4ErrorPropagatorData::verbose() >= 4 )   G4cout << " bef  theG4ErrorPropagatorManager->RunInitialization() " <<  G4StateManager::GetStateManager()->GetCurrentState() << G4endl;
00191     theG4ErrorRunManagerHelper->RunInitialization();
00192     if( G4ErrorPropagatorData::verbose() >= 4 ) G4cout << " aft  theG4ErrorPropagatorManager->RunInitialization() " <<  G4StateManager::GetStateManager()->GetCurrentState() << G4endl;
00193     
00194     if( !thePropagator ) thePropagator = new G4ErrorPropagator();  // currently the only propagator possible
00195     
00196     InitTrackPropagation();
00197   } else {
00198     G4cerr << "G4ErrorPropagatorManager::InitGeant4e: Illegal application state - "
00199            << "G4ErrorPropagatorManager::InitGeant4e() ignored." << G4endl;
00200     G4cerr << " GEANT4e State= " << PrintG4ErrorState() 
00201       //<< " GEANT4 State= " <<  PrintG4State() 
00202            << G4endl;
00203   }
00204   
00205   //----- Set the tracking geometry for this propagation
00206   //t  SetTrackingGeometry();
00207   //----- Set the physics list for this propagation
00208   //t  SetPhysicsList();
00209   //----- Set the field propagation parameters for this propagation
00210   //t  SetFieldPropagationParameters();
00211   G4ErrorPropagatorData::GetErrorPropagatorData()->SetState( G4ErrorState_Init );
00212 
00213   if( G4ErrorPropagatorData::verbose() >= 2 ) G4cout << "End InitGeant4e GEANT4e State= " << PrintG4ErrorState() << " GEANT4 State= " << PrintG4State() << G4endl;
00214 
00215 
00216 }
00217 
00218 
00219 //-----------------------------------------------------------------------
00220 void G4ErrorPropagatorManager::InitTrackPropagation()
00221 {
00222   thePropagator->SetStepN( 0 );
00223 
00224   G4ErrorPropagatorData::GetErrorPropagatorData()->SetState( G4ErrorState_Propagating );
00225 
00226 }
00227 
00228 
00229 //-----------------------------------------------------------------------
00230 G4bool G4ErrorPropagatorManager::InitFieldForBackwards()
00231 {
00232 
00233   if( G4ErrorPropagatorData::verbose() >= 4 ) G4cout << " G4ErrorPropagatorManager::InitFieldForBackwards() " << G4endl;
00234   //----- Gets the current equation of motion
00235   G4FieldManager* fieldMgr= G4TransportationManager::GetTransportationManager()->GetFieldManager();
00236   //  G4cout << " fieldMgr " << fieldMgr << G4endl;
00237   if( !fieldMgr ) return 0;
00238 
00239   //  G4Field* myfield = fieldMgr->GetDetectorField();
00240   G4ChordFinder* cf = fieldMgr ->GetChordFinder();
00241   if( !cf ) return 0;
00242   G4MagInt_Driver* mid = cf->GetIntegrationDriver();
00243   if( !mid ) return 0;
00244   G4MagIntegratorStepper* stepper = const_cast<G4MagIntegratorStepper*>(mid->GetStepper());
00245   if( !stepper ) return 0;
00246   G4EquationOfMotion* equation = stepper->GetEquationOfMotion();
00247 
00248   //----- Replaces the equation by a G4ErrorMag_UsualEqRhs to handle backwards tracking
00249   if ( !dynamic_cast<G4ErrorMag_UsualEqRhs*>(equation) ) {
00250 
00251     G4MagneticField* myfield = (G4MagneticField*)fieldMgr->GetDetectorField();
00252     
00253     //    G4Mag_UsualEqRhs* fEquation_usual = dynamic_cast<G4Mag_UsualEqRhs*>(equation);
00254     if( theEquationOfMotion == 0 ) theEquationOfMotion = new G4ErrorMag_UsualEqRhs(myfield);
00255  
00256     //---- Pass the equation of motion to the G4MagIntegratorStepper
00257     stepper->SetEquationOfMotion( theEquationOfMotion );
00258 
00259     //--- change stepper for speed tests
00260    G4MagIntegratorStepper* g4eStepper = new G4ClassicalRK4(theEquationOfMotion);
00261    // G4MagIntegratorStepper* g4eStepper = new G4ExactHelixStepper(theEquationOfMotion);
00262     
00263     //---- 
00264     G4MagneticField* field = static_cast<G4MagneticField*>(const_cast<G4Field*>(fieldMgr->GetDetectorField()));
00265     G4ChordFinder* pChordFinder = new G4ChordFinder(field, 1.0e-2*mm, g4eStepper);
00266 
00267     fieldMgr->SetChordFinder(pChordFinder);
00268 
00269   }
00270 
00271   return 1;
00272 }
00273 
00274 
00275 //-----------------------------------------------------------------------
00276 G4int G4ErrorPropagatorManager::Propagate( G4ErrorTrajState* currentTS, const G4ErrorTarget* target, G4ErrorMode mode )
00277 {
00278   G4ErrorPropagatorData::GetErrorPropagatorData()->SetMode( mode );
00279   if( !thePropagator ) thePropagator = new G4ErrorPropagator();  // currently the only propagator possible
00280 
00281   SetSteppingManagerVerboseLevel();
00282   InitTrackPropagation();
00283 
00284   G4int ierr = thePropagator->Propagate( currentTS, target, mode );
00285 
00286   EventTermination();
00287 
00288   return ierr;
00289 }
00290 
00291 
00292 //-----------------------------------------------------------------------
00293 G4int G4ErrorPropagatorManager::PropagateOneStep( G4ErrorTrajState* currentTS, G4ErrorMode mode )
00294 {
00295   G4ErrorPropagatorData::GetErrorPropagatorData()->SetMode( mode );
00296 
00297   if( !thePropagator ) thePropagator = new G4ErrorPropagator();  // currently the only propagator possible
00298 
00299   SetSteppingManagerVerboseLevel();
00300 
00301   return thePropagator->PropagateOneStep( currentTS );
00302 }
00303 
00304 
00305 //-----------------------------------------------------------------------
00306 G4bool G4ErrorPropagatorManager::CloseGeometry()
00307 {
00308   G4GeometryManager* geomManager = G4GeometryManager::GetInstance();
00309   geomManager->OpenGeometry();
00310   if(  G4StateManager::GetStateManager()->GetCurrentState() != G4State_GeomClosed) {
00311     G4StateManager::GetStateManager()->SetNewState(G4State_Quit);
00312   }
00313 
00314   return TRUE;
00315 }
00316 
00317 
00318 //---------------------------------------------------------------------------
00319 void G4ErrorPropagatorManager::SetUserInitialization(G4VUserDetectorConstruction* userInit)
00320 {
00321   theG4ErrorRunManagerHelper->SetUserInitialization( userInit); 
00322 }
00323 
00324 
00325 //---------------------------------------------------------------------------
00326 void G4ErrorPropagatorManager::SetUserInitialization(G4VPhysicalVolume* userInit)
00327 { 
00328   theG4ErrorRunManagerHelper->SetUserInitialization( userInit); 
00329 }
00330  
00331 
00332 //---------------------------------------------------------------------------
00333 void G4ErrorPropagatorManager::SetUserInitialization(G4VUserPhysicsList* userInit)
00334 { 
00335   theG4ErrorRunManagerHelper->SetUserInitialization( userInit); 
00336 }
00337 
00338 
00339 //---------------------------------------------------------------------------
00340 void G4ErrorPropagatorManager::SetUserAction(G4UserTrackingAction* userAction)
00341 {
00342   G4EventManager::GetEventManager()->SetUserAction( userAction ); 
00343 }
00344 
00345 
00346 //---------------------------------------------------------------------------
00347 void G4ErrorPropagatorManager::SetUserAction(G4UserSteppingAction* userAction)
00348 {
00349   G4EventManager::GetEventManager()->SetUserAction( userAction ); 
00350 }
00351 
00352 
00353 //---------------------------------------------------------------------------
00354 void G4ErrorPropagatorManager::SetSteppingManagerVerboseLevel()
00355 {
00356   G4TrackingManager* trkmgr = G4EventManager::GetEventManager()->GetTrackingManager();
00357   trkmgr->GetSteppingManager()->SetVerboseLevel( trkmgr->GetVerboseLevel() );
00358 }
00359 
00360 
00361 //---------------------------------------------------------------------------
00362 void G4ErrorPropagatorManager::EventTermination()
00363 {
00364   G4ErrorPropagatorData::GetErrorPropagatorData()->SetState( G4ErrorState_Init );
00365 }
00366 
00367 
00368 //---------------------------------------------------------------------------
00369 void G4ErrorPropagatorManager::RunTermination()
00370 {
00371 G4ErrorPropagatorData::GetErrorPropagatorData()->SetState( G4ErrorState_PreInit );
00372   theG4ErrorRunManagerHelper->RunTermination(); 
00373 }
00374 
00375 
00376 //---------------------------------------------------------------------------
00377 G4String G4ErrorPropagatorManager::PrintG4ErrorState() 
00378 {
00379   return PrintG4ErrorState( G4ErrorPropagatorData::GetErrorPropagatorData()->GetState() );
00380 }
00381 
00382 
00383 //---------------------------------------------------------------------------
00384 G4String G4ErrorPropagatorManager::PrintG4ErrorState( G4ErrorState state ) 
00385 {
00386   G4String nam = "";
00387   switch (state){
00388   case G4ErrorState_PreInit: 
00389     nam = "G4ErrorState_PreInit"; 
00390     break;
00391   case G4ErrorState_Init: 
00392     nam = "G4ErrorState_Init"; 
00393     break;
00394   case G4ErrorState_Propagating:
00395     nam = "G4ErrorState_Propagating";
00396     break;
00397   case G4ErrorState_TargetCloserThanBoundary:
00398     nam = "G4ErrorState_TargetCloserThanBoundary";
00399     break;
00400   case G4ErrorState_StoppedAtTarget:
00401     nam = "G4ErrorState_StoppedAtTarget";
00402     break;
00403   }
00404 
00405   return nam;
00406 }
00407 
00408 
00409 //---------------------------------------------------------------------------
00410 G4String G4ErrorPropagatorManager::PrintG4State()
00411 {
00412   return PrintG4State(G4StateManager::GetStateManager()->GetCurrentState());
00413 }
00414 
00415 
00416 //---------------------------------------------------------------------------
00417 G4String G4ErrorPropagatorManager::PrintG4State( G4ApplicationState state )
00418 {
00419   G4String nam = "";
00420   switch ( state ){
00421   case G4State_PreInit:
00422     nam = "G4State_PreInit";
00423     break;
00424   case G4State_Init:
00425     nam = "G4State_Init";
00426     break;
00427   case G4State_Idle:
00428     nam = "G4State_Idle";
00429     break;
00430   case G4State_GeomClosed:
00431     nam = "G4State_GeomClosed";
00432     break;
00433   case G4State_EventProc:
00434     nam = "G4State_EventProc"; 
00435     break;
00436   case G4State_Quit:
00437     nam = "G4State_Quit";
00438     break;
00439   case G4State_Abort:
00440     nam = "G4State_Abort";
00441     break;
00442   }
00443   
00444   return nam;
00445 
00446 }

Generated on Mon May 27 17:48:11 2013 for Geant4 by  doxygen 1.4.7