Geant4 Cross Reference |
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 /// \file errProp/errProp.cc 28 /// \brief Main program of the errorpropagation example 29 // 30 // ------------------------------------------------------------ 31 // GEANT 4 example main 32 // ------------------------------------------------------------ 33 // 34 // History: 35 // - Created: P. Arce May 2007 36 // 37 38 #include "ExErrorDetectorConstruction.hh" 39 40 #include "G4ErrorCylSurfaceTarget.hh" 41 #include "G4ErrorFreeTrajState.hh" 42 #include "G4ErrorGeomVolumeTarget.hh" 43 #include "G4ErrorPlaneSurfaceTarget.hh" 44 #include "G4ErrorPropagator.hh" 45 #include "G4ErrorPropagatorData.hh" 46 #include "G4ErrorPropagatorManager.hh" 47 #include "G4ErrorTrackLengthTarget.hh" 48 #include "G4SteppingVerbose.hh" 49 #include "G4SystemOfUnits.hh" 50 #include "G4UImanager.hh" 51 52 void Initialize(); 53 G4ErrorTarget* BuildTarget(G4int iTarget); 54 void ProcessEvent(G4int iProp, size_t nEv); 55 void Finalize(); 56 57 G4ErrorTarget* theTarget; 58 G4ErrorMode theG4ErrorMode; 59 60 //------------------------------------------------------------- 61 int main() 62 { 63 Initialize(); 64 65 //----- Choose propagation mode 66 // 0: propagate until target, all steps in one go 67 // 1: propagate until target, returning control to the user at each step 68 G4int iProp = 0; 69 char* prop = std::getenv("G4ERROR_PROP"); 70 if (prop) { 71 if (G4String(prop) == G4String("UNTIL_TARGET")) { 72 iProp = 0; 73 } 74 else if (G4String(prop) == G4String("STEP_BY_STEP")) { 75 iProp = 1; 76 } 77 else { 78 G4Exception("exG4eReco", "Fatal error in Argument", FatalErrorInArgument, 79 G4String("Variable G4ERROR_PROP = " + G4String(prop) 80 + " It must be: UNTIL_TARGET or STEP_BY_STEP") 81 .c_str()); 82 } 83 } 84 else { 85 G4Exception("exG4eReco", "Fatal error in Argument", JustWarning, 86 "Variable G4ERROR_PROP not defined, taking it = UNTIL_TARGET"); 87 } 88 89 size_t nEvents = 3; 90 for (size_t ii = 0; ii < nEvents; ii++) { 91 ProcessEvent(iProp, ii); 92 } 93 94 Finalize(); 95 } 96 97 //------------------------------------------------------------- 98 void Initialize() 99 { 100 G4VSteppingVerbose::SetInstance(new G4SteppingVerbose); 101 102 // Initialize the GEANT4e manager 103 G4ErrorPropagatorManager* g4emgr = G4ErrorPropagatorManager::GetErrorPropagatorManager(); 104 G4ErrorPropagatorData* g4edata = G4ErrorPropagatorData::GetErrorPropagatorData(); 105 106 g4emgr->SetUserInitialization(new ExErrorDetectorConstruction); 107 108 G4UImanager::GetUIpointer()->ApplyCommand("/exerror/setField -10. kilogauss"); 109 110 g4emgr->InitGeant4e(); 111 112 G4UImanager::GetUIpointer()->ApplyCommand("/control/verbose 1"); 113 G4UImanager::GetUIpointer()->ApplyCommand("/tracking/verbose 1"); 114 G4UImanager::GetUIpointer()->ApplyCommand("/geant4e/limits/stepLength 100 mm"); 115 116 //----- Choose target type: 117 // 1: PlaneSurfaceTarget 118 // 2: CylSurfaceTarget 119 // 3: GeomVolumeTarget 120 // 4: TrackLengthTarget 121 G4int iTarget = 1; 122 char* target = std::getenv("G4ERROR_TARGET"); 123 if (target) { 124 if (G4String(target) == G4String("PLANE_SURFACE")) { 125 iTarget = 1; 126 } 127 else if (G4String(target) == G4String("CYL_SURFACE")) { 128 iTarget = 2; 129 } 130 else if (G4String(target) == G4String("VOLUME")) { 131 iTarget = 3; 132 } 133 else if (G4String(target) == G4String("TRKLEN")) { 134 iTarget = 4; 135 } 136 else { 137 G4Exception("exG4eReco", "Fatal error in Argument", FatalErrorInArgument, 138 G4String("Variable G4ERROR_TARGET = " + G4String(target) 139 + " It must be: PLANE_SURFACE, CYL_SURFACE, VOLUME, TRKLEN") 140 .c_str()); 141 } 142 } 143 else { 144 G4Exception("exG4eReco", "Fatal error in Argument", JustWarning, 145 "Variable G4ERROR_TARGET not defined, taking it = PLANE_SURFACE"); 146 } 147 148 theTarget = BuildTarget(iTarget); 149 g4edata->SetTarget(theTarget); 150 151 theG4ErrorMode = G4ErrorMode_PropBackwards; 152 char* mode = std::getenv("G4ERROR_MODE"); 153 if (mode) { 154 if (G4String(mode) == G4String("FORWARDS")) { 155 theG4ErrorMode = G4ErrorMode_PropForwards; 156 } 157 else if (G4String(mode) == G4String("BACKWARDS")) { 158 theG4ErrorMode = G4ErrorMode_PropBackwards; 159 } 160 else { 161 G4Exception("exG4eReco", "Fatal error in Argument", FatalErrorInArgument, 162 G4String("Variable G4ERROR_MODE = " + G4String(mode) 163 + " It must be: FORWARDS or BACKWARDS") 164 .c_str()); 165 } 166 } 167 else { 168 G4Exception("exG4eReco", "Fatal error in Argument", JustWarning, 169 "Variable G4ERROR_MODE not defined, taking it = BACKWARDS"); 170 } 171 } 172 173 void ProcessEvent(G4int iProp, size_t) 174 { 175 // Set the starting trajectory. 176 G4ThreeVector xv3(0, 0, 0); 177 G4ThreeVector pv3(20.0 * GeV, 0.0, 0.0); 178 G4ErrorTrajErr error(5, 0); 179 G4ErrorFreeTrajState* g4ErrorTrajState = new G4ErrorFreeTrajState("mu-", xv3, pv3, error); 180 181 G4ErrorPropagatorManager* g4emgr = G4ErrorPropagatorManager::GetErrorPropagatorManager(); 182 183 // int ierr = 0; 184 185 G4Point3D surfPos(224. * cm, 0., 0.); 186 G4Normal3D surfNorm(1., 0., 0.); 187 //- G4ErrorTarget* theG4ErrorTarget 188 // = new G4ErrorPlaneSurfaceTarget(surfNorm, surfPos ); 189 190 if (iProp == 0) { 191 // Propagate until G4ErrorTarget is found all in one go 192 // ierr = 193 g4emgr->Propagate(g4ErrorTrajState, theTarget, theG4ErrorMode); 194 } 195 else if (iProp == 1) { 196 // Propagate until G4ErrorTarget is reached step by step 197 198 g4emgr->InitTrackPropagation(); 199 200 // G4Track* aTrack 201 // = G4EventManager::GetEventManager()->GetTrackingManager()->GetTrack(); 202 bool moreEvt = TRUE; 203 while (moreEvt) { 204 // ierr = 205 g4emgr->PropagateOneStep(g4ErrorTrajState, theG4ErrorMode); 206 207 //---- Check if target is reached 208 if (g4emgr->GetPropagator()->CheckIfLastStep(g4ErrorTrajState->GetG4Track())) { 209 g4emgr->GetPropagator()->InvokePostUserTrackingAction(g4ErrorTrajState->GetG4Track()); 210 moreEvt = 0; 211 G4cout << "STEP_BY_STEP propagation: Last Step " << G4endl; 212 } 213 } 214 } 215 216 G4cout << " $$$ PROPAGATION ENDED " << G4endl; 217 // extract current state info 218 G4Point3D posEnd = g4ErrorTrajState->GetPosition(); 219 G4Normal3D momEnd = g4ErrorTrajState->GetMomentum(); 220 G4ErrorTrajErr errorEnd = g4ErrorTrajState->GetError(); 221 222 G4cout << " Position: " << posEnd << G4endl << " Momentum: " << momEnd << G4endl 223 << " Error: " << errorEnd << G4endl; 224 } 225 226 //------------------------------------------------------------- 227 G4ErrorTarget* BuildTarget(G4int iTarget) 228 { 229 G4ErrorTarget* target = 0; 230 if (iTarget == 1) { 231 G4Point3D surfPos(221. * cm, 0., 0.); 232 G4Normal3D surfNorm(1., 0., 0.); 233 target = new G4ErrorPlaneSurfaceTarget(surfNorm, surfPos); 234 } 235 else if (iTarget == 2) { 236 G4double radius = 222 * cm; 237 target = new G4ErrorCylSurfaceTarget(radius); 238 } 239 else if (iTarget == 3) { 240 target = new G4ErrorGeomVolumeTarget("MUON"); 241 } 242 else if (iTarget == 4) { 243 target = new G4ErrorTrackLengthTarget(223. * cm); 244 } 245 else { 246 G4Exception("exG4eReco", "Fatal error in Argument", FatalErrorInArgument, 247 "Target type has to be between 1 and 4"); 248 } 249 return target; 250 } 251 252 //------------------------------------------------------------- 253 void Finalize() 254 { 255 G4ErrorPropagatorManager::GetErrorPropagatorManager()->CloseGeometry(); 256 } 257