File indexing completed on 2026-06-01 07:54:30
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 #include "G4BlineTracer.hh"
0039
0040 #include "G4BlineEquation.hh"
0041 #include "G4BlineEventAction.hh"
0042 #include "G4BlinePrimaryGeneratorAction.hh"
0043 #include "G4BlineSteppingAction.hh"
0044 #include "G4BlineTracerMessenger.hh"
0045 #include "G4CashKarpRKF45.hh"
0046 #include "G4ChordFinder.hh"
0047 #include "G4FieldManager.hh"
0048 #include "G4LogicalVolumeStore.hh"
0049 #include "G4MagIntegratorDriver.hh"
0050 #include "G4PropagatorInField.hh"
0051 #include "G4RunManager.hh"
0052 #include "G4SystemOfUnits.hh"
0053 #include "G4TransportationManager.hh"
0054
0055
0056
0057 G4BlineTracer::G4BlineTracer()
0058 {
0059 fMessenger = new G4BlineTracerMessenger(this);
0060 fSteppingAction = new G4BlineSteppingAction(this);
0061 fEventAction = new G4BlineEventAction(this);
0062 fPrimaryGeneratorAction = new G4BlinePrimaryGeneratorAction();
0063 }
0064
0065
0066
0067 G4BlineTracer::~G4BlineTracer()
0068 {
0069 delete fMessenger;
0070 delete fSteppingAction;
0071 delete fEventAction;
0072 delete fPrimaryGeneratorAction;
0073 for (size_t i = 0; i < fVecEquationOfMotion.size(); i++) {
0074 if (fVecEquationOfMotion[i]) delete fVecEquationOfMotion[i];
0075 if (fVecChordFinders[i]) delete fVecChordFinders[i];
0076 }
0077 }
0078
0079
0080
0081 void G4BlineTracer::BeginOfRunAction(const G4Run*) {}
0082
0083
0084
0085 void G4BlineTracer::EndOfRunAction(const G4Run*) {}
0086
0087
0088
0089 void G4BlineTracer::ComputeBlines(G4int n_of_lines)
0090 {
0091
0092
0093 if (!fWas_ResetChordFinders_already_called) {
0094 ResetChordFinders();
0095 fWas_ResetChordFinders_already_called = true;
0096 }
0097
0098
0099
0100 G4RunManager* theRunManager = G4RunManager::GetRunManager();
0101 auto user_run_action = (G4UserRunAction*)theRunManager->GetUserRunAction();
0102 theRunManager->SetUserAction(this);
0103
0104 auto user_stepping_action = (G4UserSteppingAction*)theRunManager->GetUserSteppingAction();
0105 theRunManager->SetUserAction(fSteppingAction);
0106
0107 auto userPrimaryAction =
0108 (G4VUserPrimaryGeneratorAction*)theRunManager->GetUserPrimaryGeneratorAction();
0109 if (userPrimaryAction) fPrimaryGeneratorAction->SetUserPrimaryAction(userPrimaryAction);
0110 theRunManager->SetUserAction(fPrimaryGeneratorAction);
0111
0112 auto user_event_action = (G4UserEventAction*)theRunManager->GetUserEventAction();
0113 theRunManager->SetUserAction(fEventAction);
0114
0115 auto user_tracking_action = (G4UserTrackingAction*)theRunManager->GetUserTrackingAction();
0116 G4UserTrackingAction* aNullTrackingAction = nullptr;
0117 theRunManager->SetUserAction(aNullTrackingAction);
0118
0119 auto user_stacking_action = (G4UserStackingAction*)theRunManager->GetUserStackingAction();
0120 G4UserStackingAction* aNullStackingAction = nullptr;
0121 theRunManager->SetUserAction(aNullStackingAction);
0122
0123
0124
0125 std::vector<G4ChordFinder*> user_chord_finders;
0126 std::vector<G4double> user_largest_acceptable_step;
0127 for (size_t i = 0; i < fVecChordFinders.size(); i++) {
0128 user_largest_acceptable_step.push_back(-1.);
0129 if (fVecChordFinders[i]) {
0130 user_chord_finders.push_back(fVecFieldManagers[i]->GetChordFinder());
0131 fVecChordFinders[i]->SetDeltaChord(user_chord_finders[i]->GetDeltaChord());
0132 fVecFieldManagers[i]->SetChordFinder(fVecChordFinders[i]);
0133 }
0134 else
0135 user_chord_finders.push_back(nullptr);
0136 }
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151 G4TransportationManager* tmanager = G4TransportationManager::GetTransportationManager();
0152 G4double previous_largest_acceptable_step =
0153 tmanager->GetPropagatorInField()->GetLargestAcceptableStep();
0154
0155 tmanager->GetPropagatorInField()->SetLargestAcceptableStep(fMaxTrackingStep);
0156
0157
0158
0159 for (G4int il = 0; il < n_of_lines; il++) {
0160
0161
0162
0163
0164
0165 for (size_t i = 0; i < fVecEquationOfMotion.size(); i++) {
0166 if (fVecEquationOfMotion[i]) fVecEquationOfMotion[i]->SetBackwardDirectionOfIntegration(true);
0167 }
0168 theRunManager->BeamOn(1);
0169
0170
0171
0172 for (size_t i = 0; i < fVecEquationOfMotion.size(); i++) {
0173 if (fVecEquationOfMotion[i])
0174 fVecEquationOfMotion[i]->SetBackwardDirectionOfIntegration(false);
0175 }
0176 theRunManager->BeamOn(1);
0177 }
0178
0179
0180
0181
0182
0183
0184
0185
0186
0187 tmanager->GetPropagatorInField()->SetLargestAcceptableStep(previous_largest_acceptable_step);
0188
0189
0190
0191 theRunManager->SetUserAction(user_run_action);
0192 theRunManager->SetUserAction(user_event_action);
0193 theRunManager->SetUserAction(userPrimaryAction);
0194 theRunManager->SetUserAction(user_stepping_action);
0195 theRunManager->SetUserAction(user_tracking_action);
0196 theRunManager->SetUserAction(user_stacking_action);
0197
0198
0199
0200 for (size_t i = 0; i < fVecFieldManagers.size(); i++) {
0201 if (user_chord_finders[i]) fVecFieldManagers[i]->SetChordFinder(user_chord_finders[i]);
0202 }
0203 }
0204
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 void G4BlineTracer::ResetChordFinders()
0246 {
0247 for (size_t i = 0; i < fVecEquationOfMotion.size(); i++) {
0248 delete fVecEquationOfMotion[i];
0249 delete fVecChordFinders[i];
0250 }
0251
0252 fVecChordFinders.clear();
0253 fVecFieldManagers.clear();
0254 fVecMagneticFields.clear();
0255 fVecEquationOfMotion.clear();
0256
0257
0258
0259 fVecChordFinders.push_back(nullptr);
0260 fVecMagneticFields.push_back(nullptr);
0261 fVecEquationOfMotion.push_back(nullptr);
0262 fVecFieldManagers.push_back(
0263 G4TransportationManager::GetTransportationManager()->GetFieldManager());
0264 if (fVecFieldManagers[0]) {
0265 fVecMagneticFields[0] = (G4MagneticField*)fVecFieldManagers[0]->GetDetectorField();
0266 if (fVecMagneticFields[0]) {
0267 fVecEquationOfMotion[0] = new G4BlineEquation(fVecMagneticFields[0]);
0268 auto pStepper = new G4CashKarpRKF45(fVecEquationOfMotion[0]);
0269 auto pIntgrDriver =
0270 new G4MagInt_Driver(0.01 * mm, pStepper, pStepper->GetNumberOfVariables());
0271 fVecChordFinders[0] = new G4ChordFinder(pIntgrDriver);
0272 }
0273 }
0274
0275
0276
0277 G4LogicalVolumeStore* theVolumeStore = G4LogicalVolumeStore::GetInstance();
0278
0279 size_t j = 0;
0280 for (size_t i = 0; i < theVolumeStore->size(); i++) {
0281 if ((*theVolumeStore)[i]->GetFieldManager()) {
0282 j++;
0283 fVecFieldManagers.push_back(((*theVolumeStore)[i])->GetFieldManager());
0284 fVecMagneticFields.push_back((G4MagneticField*)fVecFieldManagers[j]->GetDetectorField());
0285 fVecEquationOfMotion.push_back(nullptr);
0286 fVecChordFinders.push_back(nullptr);
0287 if (fVecMagneticFields[j]) {
0288 fVecEquationOfMotion[j] = new G4BlineEquation(fVecMagneticFields[j]);
0289 auto pStepper = new G4CashKarpRKF45(fVecEquationOfMotion[j]);
0290 auto pIntgrDriver =
0291 new G4MagInt_Driver(.01 * mm, pStepper, pStepper->GetNumberOfVariables());
0292 fVecChordFinders[j] = new G4ChordFinder(pIntgrDriver);
0293 }
0294 }
0295 }
0296 }