File indexing completed on 2025-02-23 09:21:15
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041 #include "G4BlineTracer.hh"
0042
0043 #include "G4BlineEquation.hh"
0044 #include "G4BlineEventAction.hh"
0045 #include "G4BlinePrimaryGeneratorAction.hh"
0046 #include "G4BlineSteppingAction.hh"
0047 #include "G4BlineTracerMessenger.hh"
0048 #include "G4CashKarpRKF45.hh"
0049 #include "G4ChordFinder.hh"
0050 #include "G4FieldManager.hh"
0051 #include "G4LogicalVolumeStore.hh"
0052 #include "G4MagIntegratorDriver.hh"
0053 #include "G4PropagatorInField.hh"
0054 #include "G4RunManager.hh"
0055 #include "G4SystemOfUnits.hh"
0056 #include "G4TransportationManager.hh"
0057
0058
0059
0060 G4BlineTracer::G4BlineTracer()
0061 {
0062 fMessenger = new G4BlineTracerMessenger(this);
0063 fSteppingAction = new G4BlineSteppingAction(this);
0064 fEventAction = new G4BlineEventAction(this);
0065 fPrimaryGeneratorAction = new G4BlinePrimaryGeneratorAction();
0066 }
0067
0068
0069
0070 G4BlineTracer::~G4BlineTracer()
0071 {
0072 delete fMessenger;
0073 delete fSteppingAction;
0074 delete fEventAction;
0075 delete fPrimaryGeneratorAction;
0076 for (size_t i = 0; i < fVecEquationOfMotion.size(); i++) {
0077 if (fVecEquationOfMotion[i]) delete fVecEquationOfMotion[i];
0078 if (fVecChordFinders[i]) delete fVecChordFinders[i];
0079 }
0080 }
0081
0082
0083
0084 void G4BlineTracer::BeginOfRunAction(const G4Run*) {}
0085
0086
0087
0088 void G4BlineTracer::EndOfRunAction(const G4Run*) {}
0089
0090
0091
0092 void G4BlineTracer::ComputeBlines(G4int n_of_lines)
0093 {
0094
0095
0096 if (!fWas_ResetChordFinders_already_called) {
0097 ResetChordFinders();
0098 fWas_ResetChordFinders_already_called = true;
0099 }
0100
0101
0102
0103 G4RunManager* theRunManager = G4RunManager::GetRunManager();
0104 auto user_run_action = (G4UserRunAction*)theRunManager->GetUserRunAction();
0105 theRunManager->SetUserAction(this);
0106
0107 auto user_stepping_action = (G4UserSteppingAction*)theRunManager->GetUserSteppingAction();
0108 theRunManager->SetUserAction(fSteppingAction);
0109
0110 auto userPrimaryAction =
0111 (G4VUserPrimaryGeneratorAction*)theRunManager->GetUserPrimaryGeneratorAction();
0112 if (userPrimaryAction) fPrimaryGeneratorAction->SetUserPrimaryAction(userPrimaryAction);
0113 theRunManager->SetUserAction(fPrimaryGeneratorAction);
0114
0115 auto user_event_action = (G4UserEventAction*)theRunManager->GetUserEventAction();
0116 theRunManager->SetUserAction(fEventAction);
0117
0118 auto user_tracking_action = (G4UserTrackingAction*)theRunManager->GetUserTrackingAction();
0119 G4UserTrackingAction* aNullTrackingAction = nullptr;
0120 theRunManager->SetUserAction(aNullTrackingAction);
0121
0122 auto user_stacking_action = (G4UserStackingAction*)theRunManager->GetUserStackingAction();
0123 G4UserStackingAction* aNullStackingAction = nullptr;
0124 theRunManager->SetUserAction(aNullStackingAction);
0125
0126
0127
0128 std::vector<G4ChordFinder*> user_chord_finders;
0129 std::vector<G4double> user_largest_acceptable_step;
0130 for (size_t i = 0; i < fVecChordFinders.size(); i++) {
0131 user_largest_acceptable_step.push_back(-1.);
0132 if (fVecChordFinders[i]) {
0133 user_chord_finders.push_back(fVecFieldManagers[i]->GetChordFinder());
0134 fVecChordFinders[i]->SetDeltaChord(user_chord_finders[i]->GetDeltaChord());
0135 fVecFieldManagers[i]->SetChordFinder(fVecChordFinders[i]);
0136 }
0137 else
0138 user_chord_finders.push_back(nullptr);
0139 }
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152
0153
0154 G4TransportationManager* tmanager = G4TransportationManager::GetTransportationManager();
0155 G4double previous_largest_acceptable_step =
0156 tmanager->GetPropagatorInField()->GetLargestAcceptableStep();
0157
0158 tmanager->GetPropagatorInField()->SetLargestAcceptableStep(fMaxTrackingStep);
0159
0160
0161
0162 for (G4int il = 0; il < n_of_lines; il++) {
0163
0164
0165
0166
0167
0168 for (size_t i = 0; i < fVecEquationOfMotion.size(); i++) {
0169 if (fVecEquationOfMotion[i]) fVecEquationOfMotion[i]->SetBackwardDirectionOfIntegration(true);
0170 }
0171 theRunManager->BeamOn(1);
0172
0173
0174
0175 for (size_t i = 0; i < fVecEquationOfMotion.size(); i++) {
0176 if (fVecEquationOfMotion[i])
0177 fVecEquationOfMotion[i]->SetBackwardDirectionOfIntegration(false);
0178 }
0179 theRunManager->BeamOn(1);
0180 }
0181
0182
0183
0184
0185
0186
0187
0188
0189
0190 tmanager->GetPropagatorInField()->SetLargestAcceptableStep(previous_largest_acceptable_step);
0191
0192
0193
0194 theRunManager->SetUserAction(user_run_action);
0195 theRunManager->SetUserAction(user_event_action);
0196 theRunManager->SetUserAction(userPrimaryAction);
0197 theRunManager->SetUserAction(user_stepping_action);
0198 theRunManager->SetUserAction(user_tracking_action);
0199 theRunManager->SetUserAction(user_stacking_action);
0200
0201
0202
0203 for (size_t i = 0; i < fVecFieldManagers.size(); i++) {
0204 if (user_chord_finders[i]) fVecFieldManagers[i]->SetChordFinder(user_chord_finders[i]);
0205 }
0206 }
0207
0208
0209
0210
0211
0212
0213
0214
0215
0216
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243
0244
0245
0246
0247
0248 void G4BlineTracer::ResetChordFinders()
0249 {
0250 for (size_t i = 0; i < fVecEquationOfMotion.size(); i++) {
0251 delete fVecEquationOfMotion[i];
0252 delete fVecChordFinders[i];
0253 }
0254
0255 fVecChordFinders.clear();
0256 fVecFieldManagers.clear();
0257 fVecMagneticFields.clear();
0258 fVecEquationOfMotion.clear();
0259
0260
0261
0262 fVecChordFinders.push_back(nullptr);
0263 fVecMagneticFields.push_back(nullptr);
0264 fVecEquationOfMotion.push_back(nullptr);
0265 fVecFieldManagers.push_back(
0266 G4TransportationManager::GetTransportationManager()->GetFieldManager());
0267 if (fVecFieldManagers[0]) {
0268 fVecMagneticFields[0] = (G4MagneticField*)fVecFieldManagers[0]->GetDetectorField();
0269 if (fVecMagneticFields[0]) {
0270 fVecEquationOfMotion[0] = new G4BlineEquation(fVecMagneticFields[0]);
0271 auto pStepper = new G4CashKarpRKF45(fVecEquationOfMotion[0]);
0272 auto pIntgrDriver =
0273 new G4MagInt_Driver(0.01 * mm, pStepper, pStepper->GetNumberOfVariables());
0274 fVecChordFinders[0] = new G4ChordFinder(pIntgrDriver);
0275 }
0276 }
0277
0278
0279
0280 G4LogicalVolumeStore* theVolumeStore = G4LogicalVolumeStore::GetInstance();
0281
0282 size_t j = 0;
0283 for (size_t i = 0; i < theVolumeStore->size(); i++) {
0284 if ((*theVolumeStore)[i]->GetFieldManager()) {
0285 j++;
0286 fVecFieldManagers.push_back(((*theVolumeStore)[i])->GetFieldManager());
0287 fVecMagneticFields.push_back((G4MagneticField*)fVecFieldManagers[j]->GetDetectorField());
0288 fVecEquationOfMotion.push_back(nullptr);
0289 fVecChordFinders.push_back(nullptr);
0290 if (fVecMagneticFields[j]) {
0291 fVecEquationOfMotion[j] = new G4BlineEquation(fVecMagneticFields[j]);
0292 auto pStepper = new G4CashKarpRKF45(fVecEquationOfMotion[j]);
0293 auto pIntgrDriver =
0294 new G4MagInt_Driver(.01 * mm, pStepper, pStepper->GetNumberOfVariables());
0295 fVecChordFinders[j] = new G4ChordFinder(pIntgrDriver);
0296 }
0297 }
0298 }
0299 }