Geant4 Cross Reference

Cross-Referencing   Geant4
Geant4/run/src/G4AdjointSimManager.cc

Version: [ ReleaseNotes ] [ 1.0 ] [ 1.1 ] [ 2.0 ] [ 3.0 ] [ 3.1 ] [ 3.2 ] [ 4.0 ] [ 4.0.p1 ] [ 4.0.p2 ] [ 4.1 ] [ 4.1.p1 ] [ 5.0 ] [ 5.0.p1 ] [ 5.1 ] [ 5.1.p1 ] [ 5.2 ] [ 5.2.p1 ] [ 5.2.p2 ] [ 6.0 ] [ 6.0.p1 ] [ 6.1 ] [ 6.2 ] [ 6.2.p1 ] [ 6.2.p2 ] [ 7.0 ] [ 7.0.p1 ] [ 7.1 ] [ 7.1.p1 ] [ 8.0 ] [ 8.0.p1 ] [ 8.1 ] [ 8.1.p1 ] [ 8.1.p2 ] [ 8.2 ] [ 8.2.p1 ] [ 8.3 ] [ 8.3.p1 ] [ 8.3.p2 ] [ 9.0 ] [ 9.0.p1 ] [ 9.0.p2 ] [ 9.1 ] [ 9.1.p1 ] [ 9.1.p2 ] [ 9.1.p3 ] [ 9.2 ] [ 9.2.p1 ] [ 9.2.p2 ] [ 9.2.p3 ] [ 9.2.p4 ] [ 9.3 ] [ 9.3.p1 ] [ 9.3.p2 ] [ 9.4 ] [ 9.4.p1 ] [ 9.4.p2 ] [ 9.4.p3 ] [ 9.4.p4 ] [ 9.5 ] [ 9.5.p1 ] [ 9.5.p2 ] [ 9.6 ] [ 9.6.p1 ] [ 9.6.p2 ] [ 9.6.p3 ] [ 9.6.p4 ] [ 10.0 ] [ 10.0.p1 ] [ 10.0.p2 ] [ 10.0.p3 ] [ 10.0.p4 ] [ 10.1 ] [ 10.1.p1 ] [ 10.1.p2 ] [ 10.1.p3 ] [ 10.2 ] [ 10.2.p1 ] [ 10.2.p2 ] [ 10.2.p3 ] [ 10.3 ] [ 10.3.p1 ] [ 10.3.p2 ] [ 10.3.p3 ] [ 10.4 ] [ 10.4.p1 ] [ 10.4.p2 ] [ 10.4.p3 ] [ 10.5 ] [ 10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

Diff markup

Differences between /run/src/G4AdjointSimManager.cc (Version 11.3.0) and /run/src/G4AdjointSimManager.cc (Version 10.3.p1)


  1 //                                                  1 //
  2 // *******************************************      2 // ********************************************************************
  3 // * License and Disclaimer                         3 // * License and Disclaimer                                           *
  4 // *                                                4 // *                                                                  *
  5 // * The  Geant4 software  is  copyright of th      5 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
  6 // * the Geant4 Collaboration.  It is provided      6 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
  7 // * conditions of the Geant4 Software License      7 // * conditions of the Geant4 Software License,  included in the file *
  8 // * LICENSE and available at  http://cern.ch/      8 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
  9 // * include a list of copyright holders.           9 // * include a list of copyright holders.                             *
 10 // *                                               10 // *                                                                  *
 11 // * Neither the authors of this software syst     11 // * Neither the authors of this software system, nor their employing *
 12 // * institutes,nor the agencies providing fin     12 // * institutes,nor the agencies providing financial support for this *
 13 // * work  make  any representation or  warran     13 // * work  make  any representation or  warranty, express or implied, *
 14 // * regarding  this  software system or assum     14 // * regarding  this  software system or assume any liability for its *
 15 // * use.  Please see the license in the file      15 // * use.  Please see the license in the file  LICENSE  and URL above *
 16 // * for the full disclaimer and the limitatio     16 // * for the full disclaimer and the limitation of liability.         *
 17 // *                                               17 // *                                                                  *
 18 // * This  code  implementation is the result      18 // * This  code  implementation is the result of  the  scientific and *
 19 // * technical work of the GEANT4 collaboratio     19 // * technical work of the GEANT4 collaboration.                      *
 20 // * By using,  copying,  modifying or  distri     20 // * By using,  copying,  modifying or  distributing the software (or *
 21 // * any work based  on the software)  you  ag     21 // * any work based  on the software)  you  agree  to acknowledge its *
 22 // * use  in  resulting  scientific  publicati     22 // * use  in  resulting  scientific  publications,  and indicate your *
 23 // * acceptance of all terms of the Geant4 Sof     23 // * acceptance of all terms of the Geant4 Software license.          *
 24 // *******************************************     24 // ********************************************************************
 25 //                                                 25 //
 26 // G4AdjointCrossSurfChecker implementation    <<  26 // $Id: G4AdjointSimManager.cc 102566 2017-02-09 08:35:24Z gcosmo $
 27 //                                                 27 //
 28 // ------------------------------------------- <<  28 /////////////////////////////////////////////////////////////////////////////
 29 //   Class Name:   G4AdjointCrossSurfChecker   <<  29 //      Class Name: G4AdjointCrossSurfChecker
 30 //   Author:       L. Desorgher, 2007-2009     <<  30 //  Author:         L. Desorgher
 31 //   Organisation: SpaceIT GmbH                <<  31 //  Organisation:   SpaceIT GmbH
 32 //   Contract:     ESA contract 21435/08/NL/AT <<  32 //  Contract: ESA contract 21435/08/NL/AT
 33 //   Customer:     ESA/ESTEC                   <<  33 //  Customer:       ESA/ESTEC
 34 // ------------------------------------------- <<  34 /////////////////////////////////////////////////////////////////////////////
 35                                                    35 
 36 #include "G4AdjointSimManager.hh"                  36 #include "G4AdjointSimManager.hh"
 37                                                << 
 38 #include "G4AdjointCrossSurfChecker.hh"        << 
 39 #include "G4AdjointPrimaryGeneratorAction.hh"  << 
 40 #include "G4AdjointSimMessenger.hh"            << 
 41 #include "G4AdjointStackingAction.hh"          << 
 42 #include "G4AdjointSteppingAction.hh"          << 
 43 #include "G4AdjointTrackingAction.hh"          << 
 44 #include "G4ParticleTable.hh"                  << 
 45 #include "G4PhysicsLogVector.hh"               << 
 46 #include "G4Run.hh"                                37 #include "G4Run.hh"
 47 #include "G4RunManager.hh"                         38 #include "G4RunManager.hh"
 48 #include "G4WorkerRunManager.hh"               <<  39 
 49 #include "G4UserEventAction.hh"                    40 #include "G4UserEventAction.hh"
 50 #include "G4UserRunAction.hh"                  << 
 51 #include "G4UserStackingAction.hh"             << 
 52 #include "G4UserSteppingAction.hh"             << 
 53 #include "G4UserTrackingAction.hh"             << 
 54 #include "G4VUserPrimaryGeneratorAction.hh"        41 #include "G4VUserPrimaryGeneratorAction.hh"
                                                   >>  42 #include "G4UserTrackingAction.hh"
                                                   >>  43 #include "G4UserSteppingAction.hh"
                                                   >>  44 #include "G4UserStackingAction.hh"
                                                   >>  45 #include "G4UserRunAction.hh"
 55                                                    46 
 56 // ------------------------------------------- <<  47 #include "G4AdjointPrimaryGeneratorAction.hh"
 57 //                                             <<  48 #include "G4AdjointSteppingAction.hh"
 58 G4ThreadLocal G4AdjointSimManager* G4AdjointSi <<  49 #include "G4AdjointStackingAction.hh"
                                                   >>  50 #include "G4AdjointTrackingAction.hh"
 59                                                    51 
 60 // ------------------------------------------- <<  52 #include "G4AdjointSimMessenger.hh"
 61 //                                             <<  53 
 62 G4AdjointSimManager::G4AdjointSimManager()     <<  54 #include "G4AdjointCrossSurfChecker.hh"
 63 {                                              << 
 64   theAdjointPrimaryGeneratorAction = new G4Adj << 
 65   theAdjointSteppingAction = new G4AdjointStep << 
 66   theAdjointTrackingAction = new G4AdjointTrac << 
 67   theAdjointStackingAction = new G4AdjointStac << 
 68   theAdjointTrackingAction->SetListOfPrimaryFw << 
 69   theAdjointPrimaryGeneratorAction->GetListOfP << 
 70                                                    55 
                                                   >>  56 #include "G4ParticleTable.hh"
                                                   >>  57 #include "G4PhysicsLogVector.hh"
                                                   >>  58 /*
                                                   >>  59 #ifdef G4MULTITHREADED
                                                   >>  60 #include "G4MTAdjointSimManager.hh"
                                                   >>  61 #endif
                                                   >>  62 */
                                                   >>  63 
                                                   >>  64 ////////////////////////////////////////////////////////////////////////////////
                                                   >>  65 //
                                                   >>  66 G4ThreadLocal G4AdjointSimManager* G4AdjointSimManager::instance = 0;
                                                   >>  67 
                                                   >>  68 ////////////////////////////////////////////////////////////////////////////////
                                                   >>  69 //
                                                   >>  70 G4AdjointSimManager::G4AdjointSimManager():
                                                   >>  71    fUserRunAction(0), fUserEventAction(0),fUserPrimaryGeneratorAction(0),
                                                   >>  72    fUserTrackingAction(0), fUserSteppingAction(0), fUserStackingAction(0),
                                                   >>  73    theAdjointRunAction(0), theAdjointEventAction(0),
                                                   >>  74    adjoint_tracking_mode(false),last_ekin(0),last_ekin_nuc(0),
                                                   >>  75    last_cos_th(0),last_fwd_part_PDGEncoding(0),last_fwd_part_index(0),
                                                   >>  76    last_weight(0), ID_of_last_particle_that_reach_the_ext_source(0),
                                                   >>  77    nb_evt_of_last_run(0),area_of_the_adjoint_source(0),theAdjointPrimaryWeight(0)
                                                   >>  78 { 
                                                   >>  79  //Create adjoint actions;
                                                   >>  80  //----------------------
                                                   >>  81  theAdjointPrimaryGeneratorAction = new G4AdjointPrimaryGeneratorAction(); 
                                                   >>  82  theAdjointSteppingAction = new G4AdjointSteppingAction();
                                                   >>  83  theAdjointTrackingAction = new G4AdjointTrackingAction(theAdjointSteppingAction);
                                                   >>  84  theAdjointStackingAction = new G4AdjointStackingAction(theAdjointTrackingAction);
                                                   >>  85  theAdjointTrackingAction->SetListOfPrimaryFwdParticles(
                                                   >>  86  theAdjointPrimaryGeneratorAction->GetListOfPrimaryFwdParticles());
                                                   >>  87  //Create messenger
                                                   >>  88  //----------------
 71   theMessenger = new G4AdjointSimMessenger(thi     89   theMessenger = new G4AdjointSimMessenger(this);
 72 }                                              <<  90   
                                                   >>  91   user_action_already_defined=false;
                                                   >>  92   use_user_StackingAction = false; 
                                                   >>  93   use_user_TrackingAction =true;
 73                                                    94 
 74 // ------------------------------------------- <<  95   adjoint_sim_mode = false;
                                                   >>  96   
                                                   >>  97   normalisation_mode=3;
                                                   >>  98   
                                                   >>  99   nb_nuc=1.;
                                                   >> 100   
                                                   >> 101   welcome_message =true;
                                                   >> 102   
                                                   >> 103   //Define user action and set this class instance  as RunAction
                                                   >> 104   //----------------
                                                   >> 105   //DefineUserActions();
                                                   >> 106   //G4RunManager* theRunManager = G4RunManager::GetRunManager();
                                                   >> 107 
                                                   >> 108   //theRunManager->G4RunManager::SetUserAction(this);
                                                   >> 109 /*
                                                   >> 110 #ifdef G4MULTITHREADED
                                                   >> 111 
                                                   >> 112  if (theRunManager->GetRunManagerType() == G4RunManager::workerRM){
                                                   >> 113      G4cout<<"Here"<<std::endl;
                                                   >> 114      //G4MTAdjointSimManager::GetInstance()->RegisterLocalManager(this);
                                                   >> 115      G4cout<<"Here1"<<std::endl;
                                                   >> 116  }
                                                   >> 117 #endif
                                                   >> 118 */
                                                   >> 119 }
                                                   >> 120 ////////////////////////////////////////////////////////////////////////////////
 75 //                                                121 //
 76 G4AdjointSimManager::~G4AdjointSimManager()       122 G4AdjointSimManager::~G4AdjointSimManager()
 77 {                                              << 123 { 
 78   delete theAdjointRunAction;                  << 124   if (theAdjointRunAction) delete theAdjointRunAction;
 79   delete theAdjointPrimaryGeneratorAction;     << 125   if (theAdjointPrimaryGeneratorAction) delete theAdjointPrimaryGeneratorAction;
 80   delete theAdjointSteppingAction;             << 126   if (theAdjointSteppingAction) delete theAdjointSteppingAction;
 81   delete theAdjointEventAction;                << 127   if (theAdjointEventAction) delete theAdjointEventAction;
 82   delete theAdjointTrackingAction;             << 128   if (theAdjointTrackingAction) delete theAdjointTrackingAction;
 83   delete theAdjointStackingAction;             << 129   if (theAdjointStackingAction) delete theAdjointStackingAction;
 84   delete theMessenger;                         << 130   if (theMessenger) delete theMessenger;
 85 }                                                 131 }
 86                                                << 132 ////////////////////////////////////////////////////////////////////////////////
 87 // ------------------------------------------- << 
 88 //                                                133 //
 89 G4AdjointSimManager* G4AdjointSimManager::GetI    134 G4AdjointSimManager* G4AdjointSimManager::GetInstance()
 90 {                                                 135 {
 91   if (instance == nullptr) instance = new G4Ad << 136   if (instance == 0) instance = new G4AdjointSimManager;
 92   return instance;                                137   return instance;
 93 }                                                 138 }
 94                                                << 139 ////////////////////////////////////////////////////////////////////////////////
 95 // ------------------------------------------- << 
 96 //                                                140 //
 97 void G4AdjointSimManager::RunAdjointSimulation    141 void G4AdjointSimManager::RunAdjointSimulation(G4int nb_evt)
 98 {                                              << 142 { if (G4RunManager::GetRunManager()->GetRunManagerType() != G4RunManager::sequentialRM) return; //only for sequential mode
 99                                                << 
100   if (welcome_message) {                          143   if (welcome_message) {
101     G4cout << "******************************* << 144     G4cout<<"****************************************************************"<<std::endl;
102     G4cout << "*** Geant4 Reverse/Adjoint Mont << 145   G4cout<<"*** Geant4 Reverse/Adjoint Monte Carlo mode          ***"<<std::endl;
103     G4cout << "*** Author:       L.Desorgher   << 146   G4cout<<"*** Author:  L.Desorgher             ***"<<std::endl;
104     G4cout << "*** Company:      SpaceIT GmbH, << 147   G4cout<<"*** Company: SpaceIT GmbH, Bern, Switzerland         ***"<<std::endl;
105     G4cout << "*** Sponsored by: ESA/ESTEC con << 148   G4cout<<"*** Sponsored by: ESA/ESTEC contract contract 21435/08/NL/AT ***"<<std::endl;  
106     G4cout << "******************************* << 149   G4cout<<"****************************************************************"<<std::endl;
107     welcome_message = false;                   << 150   welcome_message=false;
108   }                                            << 151   } 
109                                                << 152   
110   // Switch to adjoint simulation mode         << 153   //Switch to adjoint simulation mode
111   //------------------------------------------    154   //---------------------------------------------------------
112   SwitchToAdjointSimulationMode();                155   SwitchToAdjointSimulationMode();
113                                                << 156   
114   // Make the run                              << 157   //Make the run
115   //------------                                  158   //------------
116   nb_evt_of_last_run = nb_evt;                 << 159   
117   if (G4Threading::G4GetThreadId() < 0){       << 160   nb_evt_of_last_run =nb_evt;
118     G4RunManager::GetRunManager()->BeamOn(     << 161   G4RunManager::GetRunManager()->BeamOn(nb_evt*theAdjointPrimaryGeneratorAction->GetNbOfAdjointPrimaryTypes());
119     G4int(nb_evt * theAdjointPrimaryGeneratorA << 162   //G4RunManager::GetRunManager()->BeamOn(theAdjointPrimaryGeneratorAction->GetNbOfAdjointPrimaryTypes()*2*nb_evt);
120   }                                            << 163 
121 }                                              << 164   //Back to Fwd Simulation Mode
                                                   >> 165   //--------------------------------
                                                   >> 166   BackToFwdSimulationMode();
122                                                   167 
123 // ------------------------------------------- << 168   /*
                                                   >> 169   //Register the weight vector
                                                   >> 170   //--------------------------
                                                   >> 171   std::ofstream FileOutputElectronWeight("ElectronWeight.txt", std::ios::out);
                                                   >> 172   FileOutputElectronWeight<<std::setiosflags(std::ios::scientific);
                                                   >> 173   FileOutputElectronWeight<<std::setprecision(6);
                                                   >> 174   G4bool aBool = electron_last_weight_vector->Store(FileOutputElectronWeight, true);
                                                   >> 175   FileOutputElectronWeight.close();
                                                   >> 176   
                                                   >> 177   std::ofstream FileOutputProtonWeight("ProtonWeight.txt", std::ios::out);
                                                   >> 178   FileOutputProtonWeight<<std::setiosflags(std::ios::scientific);
                                                   >> 179   FileOutputProtonWeight<<std::setprecision(6);
                                                   >> 180   aBool = proton_last_weight_vector->Store(FileOutputProtonWeight, true);
                                                   >> 181   FileOutputProtonWeight.close();
                                                   >> 182   
                                                   >> 183   std::ofstream FileOutputGammaWeight("GammaWeight.txt", std::ios::out);
                                                   >> 184   FileOutputGammaWeight<<std::setiosflags(std::ios::scientific);
                                                   >> 185   FileOutputGammaWeight<<std::setprecision(6);
                                                   >> 186   aBool = gamma_last_weight_vector->Store(FileOutputGammaWeight, true);
                                                   >> 187   FileOutputGammaWeight.close();
                                                   >> 188   */  
                                                   >> 189 }
                                                   >> 190 ////////////////////////////////////////////////////////////////////////////////
124 //                                                191 //
125 void G4AdjointSimManager::SetRestOfAdjointActi    192 void G4AdjointSimManager::SetRestOfAdjointActions()
126 { if (G4Threading::G4GetThreadId() == -1) retu << 193 {
127   G4RunManager* theRunManager = G4RunManager:: << 194   G4RunManager* theRunManager =  G4RunManager::GetRunManager();
128                                                   195 
129   if (!user_action_already_defined) DefineUser    196   if (!user_action_already_defined) DefineUserActions();
130                                                   197 
131   // Replace the user action by the adjoint ac << 198  //Replace the user action by the adjoint actions
132   //------------------------------------------ << 199  //-------------------------------------------------
133                                                   200 
134   theRunManager->G4RunManager::SetUserAction(t    201   theRunManager->G4RunManager::SetUserAction(theAdjointEventAction);
135   theRunManager->G4RunManager::SetUserAction(t    202   theRunManager->G4RunManager::SetUserAction(theAdjointSteppingAction);
136   theRunManager->G4RunManager::SetUserAction(t    203   theRunManager->G4RunManager::SetUserAction(theAdjointTrackingAction);
137 }                                              << 
138                                                   204 
139 // ------------------------------------------- << 205 }
                                                   >> 206 ////////////////////////////////////////////////////////////////////////////////
140 //                                                207 //
141 void G4AdjointSimManager::SwitchToAdjointSimul    208 void G4AdjointSimManager::SwitchToAdjointSimulationMode()
142 {                                              << 209 { //Replace the user defined actions by the adjoint actions
143   // Replace the user defined actions by the a << 
144   //------------------------------------------    210   //---------------------------------------------------------
145   SetAdjointActions();                            211   SetAdjointActions();
146                                                   212 
147   // Update the list of primaries              << 213   //Update the list of primaries
148   //-----------------------------                 214   //-----------------------------
149   if (G4Threading::G4GetThreadId() != -1)  the << 215   theAdjointPrimaryGeneratorAction->UpdateListOfPrimaryParticles();
150   adjoint_sim_mode = true;                     << 216   adjoint_sim_mode=true;
151   ID_of_last_particle_that_reach_the_ext_sourc << 217   ID_of_last_particle_that_reach_the_ext_source=0;
152                                                << 
153 }                                                 218 }
154                                                << 219 ////////////////////////////////////////////////////////////////////////////////
155 // ------------------------------------------- << 
156 //                                                220 //
157 void G4AdjointSimManager::BackToFwdSimulationM    221 void G4AdjointSimManager::BackToFwdSimulationMode()
158 {                                              << 222 {  //Restore the user defined actions
159   // Restore the user defined actions          << 223    //--------------------------------
160   //--------------------------------           << 224    ResetUserActions();
161   ResetUserActions();                          << 225    adjoint_sim_mode=false;
162   adjoint_sim_mode = false;                    << 
163 }                                                 226 }
164                                                   227 
165 // ------------------------------------------- << 228 ////////////////////////////////////////////////////////////////////////////////
166 //                                                229 //
167 void G4AdjointSimManager::SetAdjointActions()     230 void G4AdjointSimManager::SetAdjointActions()
168 {                                              << 231 {  
169   auto theRunManager = G4RunManager::GetRunMan << 232   G4RunManager* theRunManager =  G4RunManager::GetRunManager();
170                                                << 233   
171                                                << 
172   if (!user_action_already_defined) DefineUser    234   if (!user_action_already_defined) DefineUserActions();
173                                                   235 
174                                                   236 
175   // Replace the user action by the adjoint ac << 237  //Replace the user action by the adjoint actions
176   //------------------------------------------ << 238  //------------------------------------------------- 
177   theRunManager->G4RunManager::SetUserAction(t    239   theRunManager->G4RunManager::SetUserAction(this);
178   if (G4RunManager::GetRunManager()->GetRunMan << 
179       G4RunManager::GetRunManager()->GetRunMan << 
180   theRunManager->G4RunManager::SetUserAction(t    240   theRunManager->G4RunManager::SetUserAction(theAdjointPrimaryGeneratorAction);
181   theRunManager->G4RunManager::SetUserAction(t    241   theRunManager->G4RunManager::SetUserAction(theAdjointStackingAction);
182   if (use_user_StackingAction)                 << 242   if (use_user_StackingAction)  theAdjointStackingAction->SetUserFwdStackingAction(fUserStackingAction);
183     theAdjointStackingAction->SetUserFwdStacki << 243   else theAdjointStackingAction->SetUserFwdStackingAction(0);
184   else                                         << 
185     theAdjointStackingAction->SetUserFwdStacki << 
186   theRunManager->G4RunManager::SetUserAction(t    244   theRunManager->G4RunManager::SetUserAction(theAdjointEventAction);
187   theRunManager->G4RunManager::SetUserAction(t    245   theRunManager->G4RunManager::SetUserAction(theAdjointSteppingAction);
188   theRunManager->G4RunManager::SetUserAction(t    246   theRunManager->G4RunManager::SetUserAction(theAdjointTrackingAction);
189   if (use_user_TrackingAction)                 << 247   if (use_user_TrackingAction)  theAdjointTrackingAction->SetUserForwardTrackingAction(fUserTrackingAction);
190     theAdjointTrackingAction->SetUserForwardTr << 248   else theAdjointTrackingAction->SetUserForwardTrackingAction(0);
191   else                                         << 
192     theAdjointTrackingAction->SetUserForwardTr << 
193 }                                                 249 }
194 }                                              << 250 ////////////////////////////////////////////////////////////////////////////////
195                                                << 
196 // ------------------------------------------- << 
197 //                                                251 //
198 void G4AdjointSimManager::SetAdjointPrimaryRun    252 void G4AdjointSimManager::SetAdjointPrimaryRunAndStackingActions()
199 {                                              << 253 {  
200   G4RunManager* theRunManager = G4RunManager:: << 254   G4RunManager* theRunManager =  G4RunManager::GetRunManager();
201                                                << 255   
202   if (!user_action_already_defined) DefineUser    256   if (!user_action_already_defined) DefineUserActions();
203                                                << 257   
204   // Replace the user action by the adjoint ac << 258  //Replace the user action by the adjoint actions
205   //------------------------------------------ << 259  //------------------------------------------------- 
206                                                << 260   
207   theRunManager->G4RunManager::SetUserAction(t    261   theRunManager->G4RunManager::SetUserAction(theAdjointRunAction);
208   if (G4Threading::G4GetThreadId() != -1) {    << 
209   theRunManager->G4RunManager::SetUserAction(t    262   theRunManager->G4RunManager::SetUserAction(theAdjointPrimaryGeneratorAction);
210   theRunManager->G4RunManager::SetUserAction(t    263   theRunManager->G4RunManager::SetUserAction(theAdjointStackingAction);
211   if (use_user_StackingAction)                 << 264   if (use_user_StackingAction)  theAdjointStackingAction->SetUserFwdStackingAction(fUserStackingAction);
212     theAdjointStackingAction->SetUserFwdStacki << 265   else theAdjointStackingAction->SetUserFwdStackingAction(0);
213   else                                         << 
214     theAdjointStackingAction->SetUserFwdStacki << 
215 }                                                 266 }
216 }                                              << 267 ////////////////////////////////////////////////////////////////////////////////
217                                                << 
218 // ------------------------------------------- << 
219 //                                                268 //
220 void G4AdjointSimManager::ResetUserActions()      269 void G4AdjointSimManager::ResetUserActions()
221 {                                                 270 {
222   G4RunManager* theRunManager = G4RunManager:: << 271   G4RunManager* theRunManager =  G4RunManager::GetRunManager();
223                                                   272 
224   // Restore the user defined actions          << 273   //Restore the user defined actions
225   //-------------------------------               274   //-------------------------------
226   theRunManager->G4RunManager::SetUserAction(f    275   theRunManager->G4RunManager::SetUserAction(fUserRunAction);
227   if (G4Threading::G4GetThreadId() != -1) {    << 
228   theRunManager->G4RunManager::SetUserAction(f    276   theRunManager->G4RunManager::SetUserAction(fUserEventAction);
229   theRunManager->G4RunManager::SetUserAction(f    277   theRunManager->G4RunManager::SetUserAction(fUserSteppingAction);
230   theRunManager->G4RunManager::SetUserAction(f    278   theRunManager->G4RunManager::SetUserAction(fUserTrackingAction);
231   theRunManager->G4RunManager::SetUserAction(f    279   theRunManager->G4RunManager::SetUserAction(fUserPrimaryGeneratorAction);
232   theRunManager->G4RunManager::SetUserAction(f    280   theRunManager->G4RunManager::SetUserAction(fUserStackingAction);
233   }                                            << 
234 }                                                 281 }
235                                                << 282 ////////////////////////////////////////////////////////////////////////////////
236 // ------------------------------------------- << 
237 //                                                283 //
238 void G4AdjointSimManager::ResetRestOfUserActio    284 void G4AdjointSimManager::ResetRestOfUserActions()
239 {                                              << 285 {
240   if (G4Threading::G4GetThreadId() == -1) retu << 286   G4RunManager* theRunManager =  G4RunManager::GetRunManager();
241   G4RunManager* theRunManager = G4RunManager:: << 287   
242                                                << 288   //Restore the user defined actions
243   // Restore the user defined actions          << 
244   //-------------------------------               289   //-------------------------------
245                                                << 290  
246   theRunManager->G4RunManager::SetUserAction(f    291   theRunManager->G4RunManager::SetUserAction(fUserEventAction);
247   theRunManager->G4RunManager::SetUserAction(f    292   theRunManager->G4RunManager::SetUserAction(fUserSteppingAction);
248   theRunManager->G4RunManager::SetUserAction(f    293   theRunManager->G4RunManager::SetUserAction(fUserTrackingAction);
249 }                                                 294 }
250                                                   295 
251 // ------------------------------------------- << 296 ////////////////////////////////////////////////////////////////////////////////
252 //                                                297 //
253 void G4AdjointSimManager::ResetUserPrimaryRunA    298 void G4AdjointSimManager::ResetUserPrimaryRunAndStackingActions()
254 {                                              << 299 { 
255   G4RunManager* theRunManager = G4RunManager:: << 300   G4RunManager* theRunManager =  G4RunManager::GetRunManager();
256   // Restore the user defined actions          << 301   //Restore the user defined actions
257   //-------------------------------               302   //-------------------------------
258   theRunManager->G4RunManager::SetUserAction(f    303   theRunManager->G4RunManager::SetUserAction(fUserRunAction);
259   if (G4Threading::G4GetThreadId() != -1) {    << 
260   theRunManager->G4RunManager::SetUserAction(f    304   theRunManager->G4RunManager::SetUserAction(fUserPrimaryGeneratorAction);
261   theRunManager->G4RunManager::SetUserAction(f    305   theRunManager->G4RunManager::SetUserAction(fUserStackingAction);
262   }                                            << 
263 }                                                 306 }
264                                                << 307 ////////////////////////////////////////////////////////////////////////////////
265 // ------------------------------------------- << 
266 //                                                308 //
267 void G4AdjointSimManager::DefineUserActions()     309 void G4AdjointSimManager::DefineUserActions()
268 {                                              << 310 { 
269   G4RunManager* theRunManager = G4RunManager:: << 311    G4RunManager* theRunManager =  G4RunManager::GetRunManager();
270   fUserRunAction =                             << 312    fUserTrackingAction= const_cast<G4UserTrackingAction* >( theRunManager->GetUserTrackingAction() );
271     const_cast<G4UserRunAction*>(theRunManager << 313    fUserEventAction= const_cast<G4UserEventAction* >( theRunManager->GetUserEventAction() ); 
272  if (G4Threading::G4GetThreadId() != -1) {     << 314    fUserSteppingAction= const_cast<G4UserSteppingAction* >( theRunManager->GetUserSteppingAction() );
273   fUserTrackingAction = const_cast<G4UserTrack << 315    theAdjointSteppingAction->SetUserForwardSteppingAction(fUserSteppingAction);
274   fUserEventAction = const_cast<G4UserEventAct << 316    fUserPrimaryGeneratorAction= const_cast<G4VUserPrimaryGeneratorAction* >( theRunManager->GetUserPrimaryGeneratorAction() );
275   fUserSteppingAction = const_cast<G4UserStepp << 317    fUserRunAction= const_cast<G4UserRunAction*>( theRunManager->GetUserRunAction() );
276   theAdjointSteppingAction->SetUserForwardStep << 318    fUserStackingAction= const_cast<G4UserStackingAction* >( theRunManager->GetUserStackingAction() );
277   fUserPrimaryGeneratorAction =                << 319    user_action_already_defined=true;  
278     const_cast<G4VUserPrimaryGeneratorAction*> << 
279   fUserStackingAction = const_cast<G4UserStack << 
280   user_action_already_defined = true;          << 
281  }                                             << 
282 }                                                 320 }
283                                                << 321 ///////////////////////////////////////////////////////////////////////////////
284 // ------------------------------------------- << 
285 //                                                322 //
286 G4bool G4AdjointSimManager::GetAdjointTracking << 323 G4bool G4AdjointSimManager::GetAdjointTrackingMode(){
287 {                                              << 324   return  theAdjointTrackingAction->GetIsAdjointTrackingMode();
288   return theAdjointTrackingAction->GetIsAdjoin << 
289 }                                                 325 }
290                                                << 326 ///////////////////////////////////////////////////////////////////////////////
291 // ------------------------------------------- << 
292 //                                                327 //
293 void G4AdjointSimManager::SetAdjointTrackingMo << 328 void G4AdjointSimManager::SetAdjointTrackingMode(G4bool aBool)//could be removed
294 {                                                 329 {
295   adjoint_tracking_mode = aBool;                  330   adjoint_tracking_mode = aBool;
296                                                << 331  
297   if (adjoint_tracking_mode) {                    332   if (adjoint_tracking_mode) {
298     SetRestOfAdjointActions();                 << 333     SetRestOfAdjointActions();
299     theAdjointStackingAction->SetAdjointMode(t << 334   theAdjointStackingAction->SetAdjointMode(true);
300     theAdjointStackingAction->SetKillTracks(fa << 335   theAdjointStackingAction->SetKillTracks(false);
301   }                                            << 336   
                                                   >> 337   } 
302   else {                                          338   else {
303     ResetRestOfUserActions();                  << 339     
304     theAdjointStackingAction->SetAdjointMode(f << 340     ResetRestOfUserActions();
305     if (GetDidAdjParticleReachTheExtSource())  << 341   theAdjointStackingAction->SetAdjointMode(false);
306       theAdjointStackingAction->SetKillTracks( << 342   if (GetDidAdjParticleReachTheExtSource()){
307       RegisterAtEndOfAdjointTrack();           << 343     theAdjointStackingAction->SetKillTracks(false);
308     }                                          << 344     RegisterAtEndOfAdjointTrack();
309     else                                       << 345   }
310       theAdjointStackingAction->SetKillTracks( << 346   else theAdjointStackingAction->SetKillTracks(true);
311   }                                            << 347   } 
312 }                                                 348 }
313                                                << 349 ///////////////////////////////////////////////////////////////////////////////
314 // ------------------------------------------- << 
315 //                                                350 //
316 G4bool G4AdjointSimManager::GetDidAdjParticleR    351 G4bool G4AdjointSimManager::GetDidAdjParticleReachTheExtSource()
317 {                                                 352 {
318   return (GetNbOfAdointTracksReachingTheExtern << 353   return (GetNbOfAdointTracksReachingTheExternalSurface()>0);
319 }                                                 354 }
320                                                   355 
321 // ------------------------------------------- << 356 ///////////////////////////////////////////////////////////////////////////////
322 //                                                357 //
323 std::vector<G4ParticleDefinition*>* G4AdjointS << 358 std::vector<G4ParticleDefinition*>*  G4AdjointSimManager::GetListOfPrimaryFwdParticles()
324 {                                                 359 {
325   return theAdjointPrimaryGeneratorAction->Get    360   return theAdjointPrimaryGeneratorAction->GetListOfPrimaryFwdParticles();
326 }                                                 361 }
327                                                << 362 ///////////////////////////////////////////////////////////////////////////////
328 // ------------------------------------------- << 
329 //                                                363 //
330 std::size_t G4AdjointSimManager::GetNbOfPrimar << 364 size_t G4AdjointSimManager::GetNbOfPrimaryFwdParticles()
331 {                                                 365 {
332   return theAdjointPrimaryGeneratorAction->Get    366   return theAdjointPrimaryGeneratorAction->GetListOfPrimaryFwdParticles()->size();
333 }                                                 367 }
334                                                   368 
335 // ------------------------------------------- << 369 ///////////////////////////////////////////////////////////////////////////////
336 //                                                370 //
337 G4ThreeVector G4AdjointSimManager::GetPosition << 371 G4ThreeVector  G4AdjointSimManager::GetPositionAtEndOfLastAdjointTrack(size_t i){
338 {                                              << 372    return theAdjointTrackingAction->GetPositionAtEndOfLastAdjointTrack(i);
339   return theAdjointTrackingAction->GetPosition << 
340 }                                                 373 }
341                                                   374 
342 // ------------------------------------------- << 375 ///////////////////////////////////////////////////////////////////////////////
343 //                                                376 //
344 G4ThreeVector G4AdjointSimManager::GetDirectio << 377 G4ThreeVector G4AdjointSimManager::GetDirectionAtEndOfLastAdjointTrack(size_t i){
345 {                                              << 378    return theAdjointTrackingAction->GetDirectionAtEndOfLastAdjointTrack(i);
346   return theAdjointTrackingAction->GetDirectio << 
347 }                                                 379 }
348                                                << 380 //////////////////////////////////////////////////////////////////////////////
349 // ------------------------------------------- << 
350 //                                                381 //
351 G4double G4AdjointSimManager::GetEkinAtEndOfLa << 382 G4double G4AdjointSimManager::GetEkinAtEndOfLastAdjointTrack(size_t i){
352 {                                              << 383  return theAdjointTrackingAction->GetEkinAtEndOfLastAdjointTrack(i);
353   return theAdjointTrackingAction->GetEkinAtEn << 
354 }                                                 384 }
355                                                << 385 ///////////////////////////////////////////////////////////////////////////////
356 // ------------------------------------------- << 
357 //                                                386 //
358 G4double G4AdjointSimManager::GetEkinNucAtEndO << 387 G4double G4AdjointSimManager::GetEkinNucAtEndOfLastAdjointTrack(size_t i){
359 {                                              << 388  return theAdjointTrackingAction->GetEkinNucAtEndOfLastAdjointTrack(i);
360   return theAdjointTrackingAction->GetEkinNucA << 
361 }                                                 389 }
362                                                << 390 ///////////////////////////////////////////////////////////////////////////////
363 // ------------------------------------------- << 
364 //                                                391 //
365 G4double G4AdjointSimManager::GetWeightAtEndOf << 392 G4double G4AdjointSimManager::GetWeightAtEndOfLastAdjointTrack(size_t i){
366 {                                              << 393  return theAdjointTrackingAction->GetWeightAtEndOfLastAdjointTrack(i);
367   return theAdjointTrackingAction->GetWeightAt << 
368 }                                                 394 }
369                                                << 395 ///////////////////////////////////////////////////////////////////////////////
370 // ------------------------------------------- << 
371 //                                                396 //
372 G4double G4AdjointSimManager::GetCosthAtEndOfL << 397 G4double G4AdjointSimManager::GetCosthAtEndOfLastAdjointTrack(size_t i){
373 {                                              << 398  return theAdjointTrackingAction->GetCosthAtEndOfLastAdjointTrack(i);
374   return theAdjointTrackingAction->GetCosthAtE << 
375 }                                                 399 }
376                                                << 400 ///////////////////////////////////////////////////////////////////////////////
377 // ------------------------------------------- << 
378 //                                                401 //
379 const G4String& G4AdjointSimManager::GetFwdPar    402 const G4String& G4AdjointSimManager::GetFwdParticleNameAtEndOfLastAdjointTrack()
380 {                                              << 403 {return theAdjointTrackingAction->GetFwdParticleNameAtEndOfLastAdjointTrack();
381   return theAdjointTrackingAction->GetFwdParti << 
382 }                                                 404 }
383                                                << 405 ///////////////////////////////////////////////////////////////////////////////
384 // ------------------------------------------- << 
385 //                                                406 //
386 G4int G4AdjointSimManager::GetFwdParticlePDGEn << 407 G4int G4AdjointSimManager::GetFwdParticlePDGEncodingAtEndOfLastAdjointTrack(size_t i){
387 {                                              << 408  return theAdjointTrackingAction->GetFwdParticlePDGEncodingAtEndOfLastAdjointTrack(i);
388   return theAdjointTrackingAction->GetFwdParti << 
389 }                                                 409 }
390                                                   410 
391 // ------------------------------------------- << 411 ///////////////////////////////////////////////////////////////////////////////
392 //                                                412 //
393 G4int G4AdjointSimManager::GetFwdParticleIndex << 413 G4int G4AdjointSimManager::GetFwdParticleIndexAtEndOfLastAdjointTrack(size_t i)
394 {                                              << 414 {return theAdjointTrackingAction->GetLastFwdParticleIndex(i);
395   return theAdjointTrackingAction->GetLastFwdP << 
396 }                                                 415 }
397                                                << 416 ///////////////////////////////////////////////////////////////////////////////
398 // ------------------------------------------- << 
399 //                                                417 //
400 std::size_t G4AdjointSimManager::GetNbOfAdoint << 418 size_t G4AdjointSimManager::GetNbOfAdointTracksReachingTheExternalSurface()
401 {                                                 419 {
402   return theAdjointTrackingAction->GetNbOfAdoi    420   return theAdjointTrackingAction->GetNbOfAdointTracksReachingTheExternalSurface();
403 }                                                 421 }
404                                                << 422 ///////////////////////////////////////////////////////////////////////////////
405 // ------------------------------------------- << 
406 //                                                423 //
407 void G4AdjointSimManager::ClearEndOfAdjointTra    424 void G4AdjointSimManager::ClearEndOfAdjointTrackInfoVectors()
408 {                                              << 425 {theAdjointTrackingAction->ClearEndOfAdjointTrackInfoVectors();
409   theAdjointTrackingAction->ClearEndOfAdjointT << 
410 }                                                 426 }
411                                                   427 
412 // ------------------------------------------- << 428 ///////////////////////////////////////////////////////////////////////////////
413 //                                                429 //
414 void G4AdjointSimManager::RegisterAtEndOfAdjoi    430 void G4AdjointSimManager::RegisterAtEndOfAdjointTrack()
415 {                                                 431 {
416   last_pos = theAdjointSteppingAction->GetLast << 432   last_pos = theAdjointSteppingAction->GetLastPosition();  
417   last_direction = theAdjointSteppingAction->G    433   last_direction = theAdjointSteppingAction->GetLastMomentum();
418   last_direction /= last_direction.mag();      << 434   last_direction /=last_direction.mag();
419   last_cos_th = last_direction.z();            << 435   last_cos_th =  last_direction.z();
420   G4ParticleDefinition* aPartDef = theAdjointS << 436   G4ParticleDefinition* aPartDef= theAdjointSteppingAction->GetLastPartDef(); 
421                                                << 437     
422   last_fwd_part_name = aPartDef->GetParticleNa << 438   last_fwd_part_name= aPartDef->GetParticleName();
423                                                << 439  
424   last_fwd_part_name.erase(0, 4);              << 440   last_fwd_part_name.remove(0,4);
425                                                << 441   
426   last_fwd_part_PDGEncoding =                  << 442   last_fwd_part_PDGEncoding=G4ParticleTable::GetParticleTable()->FindParticle(last_fwd_part_name)->GetPDGEncoding();
427     G4ParticleTable::GetParticleTable()->FindP << 443   
428                                                << 444   std::vector<G4ParticleDefinition*>* aList = theAdjointPrimaryGeneratorAction->GetListOfPrimaryFwdParticles();
429   std::vector<G4ParticleDefinition*>* aList =  << 445   last_fwd_part_index=-1;
430     theAdjointPrimaryGeneratorAction->GetListO << 446   size_t i=0;
431   last_fwd_part_index = -1;                    << 447   while(i<aList->size() && last_fwd_part_index<0) {
432   G4int i = 0;                                 << 448     if ((*aList)[i]->GetParticleName() == last_fwd_part_name) last_fwd_part_index=i;
433   while (i < (G4int)aList->size() && last_fwd_ << 449   i++;
434     if ((*aList)[i]->GetParticleName() == last << 
435     ++i;                                       << 
436   }                                               450   }
437                                                << 451   
438   last_ekin = theAdjointSteppingAction->GetLas    452   last_ekin = theAdjointSteppingAction->GetLastEkin();
439   last_ekin_nuc = last_ekin;                      453   last_ekin_nuc = last_ekin;
440   if (aPartDef->GetParticleType() == "adjoint_    454   if (aPartDef->GetParticleType() == "adjoint_nucleus") {
441     nb_nuc = static_cast<G4double>(aPartDef->G << 455     nb_nuc=double(aPartDef->GetBaryonNumber());
442     last_ekin_nuc /= nb_nuc;                   << 456     last_ekin_nuc /=nb_nuc;
443   }                                               457   }
444                                                   458 
445   last_weight = theAdjointSteppingAction->GetL << 459   last_weight = theAdjointSteppingAction->GetLastWeight(); 
                                                   >> 460   
                                                   >> 461 
446                                                   462 
447   last_pos_vec.push_back(last_pos);               463   last_pos_vec.push_back(last_pos);
448   last_direction_vec.push_back(last_direction)    464   last_direction_vec.push_back(last_direction);
449   last_ekin_vec.push_back(last_ekin);             465   last_ekin_vec.push_back(last_ekin);
450   last_ekin_nuc_vec.push_back(last_ekin_nuc);     466   last_ekin_nuc_vec.push_back(last_ekin_nuc);
451   last_cos_th_vec.push_back(last_cos_th);         467   last_cos_th_vec.push_back(last_cos_th);
452   last_weight_vec.push_back(last_weight);         468   last_weight_vec.push_back(last_weight);
453   last_fwd_part_PDGEncoding_vec.push_back(last    469   last_fwd_part_PDGEncoding_vec.push_back(last_fwd_part_PDGEncoding);
454   last_fwd_part_index_vec.push_back(last_fwd_p    470   last_fwd_part_index_vec.push_back(last_fwd_part_index);
455   ID_of_last_particle_that_reach_the_ext_sourc    471   ID_of_last_particle_that_reach_the_ext_source++;
456   ID_of_last_particle_that_reach_the_ext_sourc << 472   ID_of_last_particle_that_reach_the_ext_source_vec.push_back(ID_of_last_particle_that_reach_the_ext_source);
457     ID_of_last_particle_that_reach_the_ext_sou << 
458 }                                              << 
459                                                   473 
460 // ------------------------------------------- << 
461 //                                             << 
462 G4bool G4AdjointSimManager::DefineSphericalExt << 
463 {                                              << 
464   G4double area;                               << 
465   return G4AdjointCrossSurfChecker::GetInstanc << 
466                                                << 
467 }                                              << 
468                                                   474 
469 // ------------------------------------------- << 
470 //                                             << 
471 G4bool G4AdjointSimManager::DefineSphericalExt << 
472   G4double radius, const G4String& volume_name << 
473 {                                              << 
474   G4double area;                               << 
475   G4ThreeVector center;                        << 
476   return G4AdjointCrossSurfChecker::GetInstanc << 
477     ->AddaSphericalSurfaceWithCenterAtTheCente << 
478                                                << 
479 }                                              << 
480                                                   475 
481 // ------------------------------------------- << 476 
                                                   >> 477 
                                                   >> 478 
                                                   >> 479   /* G4PhysicsLogVector* theWeightVector=0;
                                                   >> 480   if (last_fwd_part_name =="e-")  theWeightVector=electron_last_weight_vector;
                                                   >> 481   else if (last_fwd_part_name =="gamma") theWeightVector=gamma_last_weight_vector;
                                                   >> 482   else if (last_fwd_part_name =="proton") theWeightVector=proton_last_weight_vector;
                                                   >> 483   
                                                   >> 484   if (theWeightVector){
                                                   >> 485 
                                                   >> 486   size_t ind =  size_t(std::log10(last_weight/theAdjointPrimaryWeight)*10. + 200);
                                                   >> 487     G4double low_val =theWeightVector->GetLowEdgeEnergy(ind);
                                                   >> 488     G4bool aBool = true;
                                                   >> 489     G4double bin_weight = theWeightVector->GetValue(low_val, aBool)+1.;
                                                   >> 490     theWeightVector->PutValue(ind, bin_weight);
                                                   >> 491   }
                                                   >> 492   */
                                                   >> 493   /*if ((last_weight/theAdjointPrimaryWeight)>1.) last_weight*=1000. ;
                                                   >> 494   else if ( (last_weight/theAdjointPrimaryWeight)>0.1) last_weight*=100. ;
                                                   >> 495   else if ( (last_weight/theAdjointPrimaryWeight)>0.01) last_weight*=10. ;*/
                                                   >> 496   
                                                   >> 497   
                                                   >> 498   //G4cout <<"Last Weight "<<last_weight<<'\t'<<theAdjointPrimaryWeight<<'\t'<<last_weight/theAdjointPrimaryWeight<<std::endl;
                                                   >> 499   /*if (last_weight/theAdjointPrimaryWeight >10.) {
                                                   >> 500     G4cout<<"Warning a weight increase by a factor : "<<last_weight/theAdjointPrimaryWeight<<std::endl;
                                                   >> 501   }
                                                   >> 502   */
                                                   >> 503  
                                                   >> 504 
                                                   >> 505 }
                                                   >> 506 ///////////////////////////////////////////////////////////////////////////////
482 //                                                507 //
483 G4bool G4AdjointSimManager::DefineExtSourceOnT << 508 G4bool  G4AdjointSimManager::DefineSphericalExtSource(G4double radius, G4ThreeVector pos)
484 {                                              << 509 { 
485   G4double area;                               << 510    G4double area;
486   return G4AdjointCrossSurfChecker::GetInstanc << 511    return G4AdjointCrossSurfChecker::GetInstance()->AddaSphericalSurface("ExternalSource", radius, pos, area);
487                                                << 
488 }                                                 512 }
489                                                << 513 ///////////////////////////////////////////////////////////////////////////////
490 // ------------------------------------------- << 
491 //                                                514 //
492 void G4AdjointSimManager::SetExtSourceEmax(G4d << 515 G4bool  G4AdjointSimManager::DefineSphericalExtSourceWithCentreAtTheCentreOfAVolume(G4double radius, const G4String& volume_name)
493 {                                                 516 {
494   theAdjointSteppingAction->SetExtSourceEMax(E << 517    G4double area;
                                                   >> 518    G4ThreeVector center;
                                                   >> 519    return G4AdjointCrossSurfChecker::GetInstance()->AddaSphericalSurfaceWithCenterAtTheCenterOfAVolume( "ExternalSource", radius, volume_name,center, area);
495 }                                                 520 }
496                                                << 521 ///////////////////////////////////////////////////////////////////////////////
497 // ------------------------------------------- << 
498 //                                                522 //
499 G4bool G4AdjointSimManager::DefineSphericalAdj << 523 G4bool  G4AdjointSimManager::DefineExtSourceOnTheExtSurfaceOfAVolume(const G4String& volume_name)
500 {                                                 524 {
501   G4double area;                               << 525    G4double area;
502   G4bool aBool = G4AdjointCrossSurfChecker::Ge << 526    return G4AdjointCrossSurfChecker::GetInstance()->AddanExtSurfaceOfAvolume( "ExternalSource", volume_name,area);
503                                                << 
504   theAdjointPrimaryGeneratorAction->SetSpheric << 
505   area_of_the_adjoint_source = area;           << 
506   return aBool;                                << 
507 }                                                 527 }
508                                                << 528 ///////////////////////////////////////////////////////////////////////////////
509 // ------------------------------------------- << 
510 //                                                529 //
511 G4bool G4AdjointSimManager::DefineSphericalAdj << 530 void  G4AdjointSimManager::SetExtSourceEmax(G4double Emax)
512   G4double radius, const G4String& volume_name << 
513 {                                                 531 {
514   G4double area;                               << 532   theAdjointSteppingAction->SetExtSourceEMax(Emax);
515   G4ThreeVector center;                        << 
516   G4bool aBool =                               << 
517     G4AdjointCrossSurfChecker::GetInstance()-> << 
518       "AdjointSource", radius, volume_name, ce << 
519   theAdjointPrimaryGeneratorAction->SetSpheric << 
520   area_of_the_adjoint_source = area;           << 
521   return aBool;                                << 
522 }                                                 533 }
523                                                << 534 ///////////////////////////////////////////////////////////////////////////////
524 // ------------------------------------------- << 
525 //                                                535 //
526 G4bool G4AdjointSimManager::DefineAdjointSourc << 536 G4bool  G4AdjointSimManager::DefineSphericalAdjointSource(G4double radius, G4ThreeVector pos)
527 {                                              << 537 { 
528   G4double area;                               << 538    G4double area;
529   G4bool aBool = G4AdjointCrossSurfChecker::Ge << 539    G4bool aBool = G4AdjointCrossSurfChecker::GetInstance()->AddaSphericalSurface("AdjointSource", radius, pos, area); 
530     "AdjointSource", volume_name, area);       << 540    theAdjointPrimaryGeneratorAction->SetSphericalAdjointPrimarySource(radius, pos); 
531   area_of_the_adjoint_source = area;           << 541    area_of_the_adjoint_source=area;
532   if (aBool) {                                 << 542    return aBool;  
533     theAdjointPrimaryGeneratorAction->SetAdjoi << 543 }
534   }                                            << 544 ///////////////////////////////////////////////////////////////////////////////
535   return aBool;                                << 545 //
                                                   >> 546 G4bool  G4AdjointSimManager::DefineSphericalAdjointSourceWithCentreAtTheCentreOfAVolume(G4double radius, const G4String& volume_name)
                                                   >> 547 {
                                                   >> 548    G4double area;
                                                   >> 549    G4ThreeVector center;
                                                   >> 550    G4bool aBool = G4AdjointCrossSurfChecker::GetInstance()->AddaSphericalSurfaceWithCenterAtTheCenterOfAVolume( "AdjointSource", radius, volume_name,center, area);
                                                   >> 551    theAdjointPrimaryGeneratorAction->SetSphericalAdjointPrimarySource(radius, center);
                                                   >> 552    area_of_the_adjoint_source=area;
                                                   >> 553    return aBool;
                                                   >> 554 }
                                                   >> 555 ///////////////////////////////////////////////////////////////////////////////
                                                   >> 556 //
                                                   >> 557 G4bool  G4AdjointSimManager::DefineAdjointSourceOnTheExtSurfaceOfAVolume(const G4String& volume_name)
                                                   >> 558 {
                                                   >> 559    G4double area;
                                                   >> 560    G4bool aBool = G4AdjointCrossSurfChecker::GetInstance()->AddanExtSurfaceOfAvolume( "AdjointSource", volume_name,area); 
                                                   >> 561    area_of_the_adjoint_source=area; 
                                                   >> 562    if (aBool) { 
                                                   >> 563     theAdjointPrimaryGeneratorAction->SetAdjointPrimarySourceOnAnExtSurfaceOfAVolume(volume_name);
                                                   >> 564    }
                                                   >> 565    return aBool;
536 }                                                 566 }
537                                                << 567 ///////////////////////////////////////////////////////////////////////////////
538 // ------------------------------------------- << 
539 //                                                568 //
540 void G4AdjointSimManager::SetAdjointSourceEmin    569 void G4AdjointSimManager::SetAdjointSourceEmin(G4double Emin)
541 {                                                 570 {
542   theAdjointPrimaryGeneratorAction->SetEmin(Em    571   theAdjointPrimaryGeneratorAction->SetEmin(Emin);
543 }                                                 572 }
544                                                << 573 ///////////////////////////////////////////////////////////////////////////////
545 // ------------------------------------------- << 
546 //                                                574 //
547 void G4AdjointSimManager::SetAdjointSourceEmax    575 void G4AdjointSimManager::SetAdjointSourceEmax(G4double Emax)
548 {                                                 576 {
549   theAdjointPrimaryGeneratorAction->SetEmax(Em    577   theAdjointPrimaryGeneratorAction->SetEmax(Emax);
550 }                                                 578 }
551                                                << 579 ///////////////////////////////////////////////////////////////////////////////
552 // ------------------------------------------- << 
553 //                                                580 //
554 void G4AdjointSimManager::ConsiderParticleAsPr    581 void G4AdjointSimManager::ConsiderParticleAsPrimary(const G4String& particle_name)
555 {                                                 582 {
556   theAdjointPrimaryGeneratorAction->ConsiderPa    583   theAdjointPrimaryGeneratorAction->ConsiderParticleAsPrimary(particle_name);
557 }                                                 584 }
558                                                << 585 ///////////////////////////////////////////////////////////////////////////////
559 // ------------------------------------------- << 
560 //                                                586 //
561 void G4AdjointSimManager::NeglectParticleAsPri    587 void G4AdjointSimManager::NeglectParticleAsPrimary(const G4String& particle_name)
562 {                                                 588 {
563   theAdjointPrimaryGeneratorAction->NeglectPar    589   theAdjointPrimaryGeneratorAction->NeglectParticleAsPrimary(particle_name);
564 }                                                 590 }
565                                                << 591 ///////////////////////////////////////////////////////////////////////////////
566 // ------------------------------------------- << 592 //
                                                   >> 593 /*void G4AdjointSimManager::SetPrimaryIon(G4int Z, G4int A)
                                                   >> 594 {
                                                   >> 595   theAdjointPrimaryGeneratorAction->SetPrimaryIon(Z, A);
                                                   >> 596 }
                                                   >> 597 */
                                                   >> 598 ///////////////////////////////////////////////////////////////////////////////
567 //                                                599 //
568 void G4AdjointSimManager::SetPrimaryIon(G4Part << 600 void G4AdjointSimManager::SetPrimaryIon(G4ParticleDefinition* adjointIon, G4ParticleDefinition* fwdIon)
569                                         G4Part << 
570 {                                                 601 {
571   theAdjointPrimaryGeneratorAction->SetPrimary    602   theAdjointPrimaryGeneratorAction->SetPrimaryIon(adjointIon, fwdIon);
572 }                                                 603 }
573                                                << 604 ///////////////////////////////////////////////////////////////////////////////
574 // ------------------------------------------- << 
575 //                                                605 //
576 const G4String& G4AdjointSimManager::GetPrimar    606 const G4String& G4AdjointSimManager::GetPrimaryIonName()
577 {                                                 607 {
578   return theAdjointPrimaryGeneratorAction->Get    608   return theAdjointPrimaryGeneratorAction->GetPrimaryIonName();
579 }                                                 609 }
580                                                << 610 ///////////////////////////////////////////////////////////////////////////////
581 // ------------------------------------------- << 
582 //                                                611 //
583 void G4AdjointSimManager::RegisterAdjointPrima    612 void G4AdjointSimManager::RegisterAdjointPrimaryWeight(G4double aWeight)
584 {                                                 613 {
585   theAdjointPrimaryWeight = aWeight;              614   theAdjointPrimaryWeight = aWeight;
586   theAdjointSteppingAction->SetPrimWeight(aWei    615   theAdjointSteppingAction->SetPrimWeight(aWeight);
587 }                                              << 616 } 
588                                                   617 
589 // ------------------------------------------- << 618 ///////////////////////////////////////////////////////////////////////////////
590 //                                                619 //
591 void G4AdjointSimManager::SetAdjointEventActio    620 void G4AdjointSimManager::SetAdjointEventAction(G4UserEventAction* anAction)
592 {                                                 621 {
593   theAdjointEventAction = anAction;               622   theAdjointEventAction = anAction;
594 }                                                 623 }
595                                                << 624 ///////////////////////////////////////////////////////////////////////////////
596 // ------------------------------------------- << 
597 //                                                625 //
598 void G4AdjointSimManager::SetAdjointSteppingAc    626 void G4AdjointSimManager::SetAdjointSteppingAction(G4UserSteppingAction* anAction)
599 {                                                 627 {
600   theAdjointSteppingAction->SetUserAdjointStep    628   theAdjointSteppingAction->SetUserAdjointSteppingAction(anAction);
601 }                                                 629 }
602                                                << 630 ///////////////////////////////////////////////////////////////////////////////
603 // ------------------------------------------- << 
604 //                                                631 //
605 void G4AdjointSimManager::SetAdjointStackingAc    632 void G4AdjointSimManager::SetAdjointStackingAction(G4UserStackingAction* anAction)
606 {                                                 633 {
607   theAdjointStackingAction->SetUserAdjointStac    634   theAdjointStackingAction->SetUserAdjointStackingAction(anAction);
608 }                                                 635 }
609                                                   636 
610 // ------------------------------------------- << 637 ///////////////////////////////////////////////////////////////////////////////
611 //                                                638 //
612 void G4AdjointSimManager::SetAdjointRunAction(    639 void G4AdjointSimManager::SetAdjointRunAction(G4UserRunAction* anAction)
613 {                                                 640 {
614   theAdjointRunAction = anAction;              << 641   theAdjointRunAction=anAction;
615 }                                              << 642 } 
616                                                << 643 ///////////////////////////////////////////////////////////////////////////////
617 // ------------------------------------------- << 
618 //                                                644 //
619 void G4AdjointSimManager::SetNbOfPrimaryFwdGam    645 void G4AdjointSimManager::SetNbOfPrimaryFwdGammasPerEvent(G4int nb)
620 {                                                 646 {
621   theAdjointPrimaryGeneratorAction->SetNbPrima    647   theAdjointPrimaryGeneratorAction->SetNbPrimaryFwdGammasPerEvent(nb);
622 }                                                 648 }
623                                                << 649 ///////////////////////////////////////////////////////////////////////////////
624 // ------------------------------------------- << 
625 //                                                650 //
626 void G4AdjointSimManager::SetNbAdjointPrimaryG    651 void G4AdjointSimManager::SetNbAdjointPrimaryGammasPerEvent(G4int nb)
627 {                                                 652 {
628   theAdjointPrimaryGeneratorAction->SetNbAdjoi    653   theAdjointPrimaryGeneratorAction->SetNbAdjointPrimaryGammasPerEvent(nb);
629 }                                                 654 }
630                                                << 655 ///////////////////////////////////////////////////////////////////////////////
631 // ------------------------------------------- << 
632 //                                                656 //
633 void G4AdjointSimManager::SetNbAdjointPrimaryE    657 void G4AdjointSimManager::SetNbAdjointPrimaryElectronsPerEvent(G4int nb)
634 {                                                 658 {
635   theAdjointPrimaryGeneratorAction->SetNbAdjoi    659   theAdjointPrimaryGeneratorAction->SetNbAdjointPrimaryElectronsPerEvent(nb);
636 }                                                 660 }
637                                                << 661 ///////////////////////////////////////////////////////////////////////////////
638 // ------------------------------------------- << 
639 //                                                662 //
640 void G4AdjointSimManager::BeginOfRunAction(con    663 void G4AdjointSimManager::BeginOfRunAction(const G4Run* aRun)
641 {  if (!adjoint_sim_mode){                     << 664 {
642     if(fUserRunAction  != nullptr) fUserRunAct << 665 /*
643    }                                           << 666  if (!adjoint_sim_mode){
644    else {                                      << 667   if(fUserRunAction) fUserRunAction->BeginOfRunAction(aRun);
645     if (theAdjointRunAction  != nullptr) theAd << 668  }
646    }                                           << 669  else {
647                                                << 670   if (theAdjointRunAction) theAdjointRunAction->BeginOfRunAction(aRun);
648   //fUserRunAction->BeginOfRunAction(aRun);    << 671  }
                                                   >> 672  */
                                                   >> 673 fUserRunAction->BeginOfRunAction(aRun);
649 }                                                 674 }
650                                                << 675 ///////////////////////////////////////////////////////////////////////////////
651 // ------------------------------------------- << 
652 //                                                676 //
653 void G4AdjointSimManager::EndOfRunAction(const    677 void G4AdjointSimManager::EndOfRunAction(const G4Run* aRun)
654 {                                              << 678 {if (!adjoint_sim_mode){
655   if (!adjoint_sim_mode) {                     << 679   if(fUserRunAction) fUserRunAction->EndOfRunAction(aRun);
656     if (fUserRunAction != nullptr) fUserRunAct << 680  }
657   }                                            << 681  else if (theAdjointRunAction) theAdjointRunAction->EndOfRunAction(aRun);
658   else if (theAdjointRunAction != nullptr)     << 682 /*
659     theAdjointRunAction->EndOfRunAction(aRun); << 683 #ifdef G4MULTITHREADED
660                                                << 684  if (G4RunManager::GetRunManager()->GetRunManagerType() == G4RunManager::workerRM){
661   BackToFwdSimulationMode();                   << 685   if (adjoint_sim_mode) BackToFwdSimulationMode();
                                                   >> 686  }
                                                   >> 687 #endif
                                                   >> 688 */
662                                                   689 
663 }                                                 690 }
664                                                << 691 ///////////////////////////////////////////////////////////////////////////////
665 // ------------------------------------------- << 
666 //                                                692 //
667 G4ParticleDefinition* G4AdjointSimManager::Get << 693 G4ParticleDefinition* G4AdjointSimManager::GetLastGeneratedFwdPrimaryParticle(){
668 {                                              << 694     return theAdjointPrimaryGeneratorAction->GetLastGeneratedFwdPrimaryParticle();
669   return theAdjointPrimaryGeneratorAction->Get << 
670 }                                                 695 }
671                                                << 696 ///////////////////////////////////////////////////////////////////////////////
672 // ------------------------------------------- << 
673 //                                                697 //
674 void G4AdjointSimManager::ResetDidOneAdjPartRe    698 void G4AdjointSimManager::ResetDidOneAdjPartReachExtSourceDuringEvent()
675 {                                              << 699 {theAdjointSteppingAction->ResetDidOneAdjPartReachExtSourceDuringEvent();
676   theAdjointSteppingAction->ResetDidOneAdjPart << 
677 }                                                 700 }
                                                   >> 701 
                                                   >> 702 
678                                                   703