ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4AdjointSimManager.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4AdjointSimManager.cc
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
26 //
28 // Class Name: G4AdjointCrossSurfChecker
29 // Author: L. Desorgher
30 // Organisation: SpaceIT GmbH
31 // Contract: ESA contract 21435/08/NL/AT
32 // Customer: ESA/ESTEC
34 
35 #include "G4AdjointSimManager.hh"
36 #include "G4Run.hh"
37 #include "G4RunManager.hh"
38 
39 #include "G4UserEventAction.hh"
41 #include "G4UserTrackingAction.hh"
42 #include "G4UserSteppingAction.hh"
43 #include "G4UserStackingAction.hh"
44 #include "G4UserRunAction.hh"
45 
50 
51 #include "G4AdjointSimMessenger.hh"
52 
54 
55 #include "G4ParticleTable.hh"
56 #include "G4PhysicsLogVector.hh"
57 /*
58 #ifdef G4MULTITHREADED
59 #include "G4MTAdjointSimManager.hh"
60 #endif
61 */
62 
64 //
66 
68 //
70  fUserRunAction(0), fUserEventAction(0),fUserPrimaryGeneratorAction(0),
71  fUserTrackingAction(0), fUserSteppingAction(0), fUserStackingAction(0),
72  theAdjointRunAction(0), theAdjointEventAction(0),
73  adjoint_tracking_mode(false),last_ekin(0),last_ekin_nuc(0),
74  last_cos_th(0),last_fwd_part_PDGEncoding(0),last_fwd_part_index(0),
75  last_weight(0), ID_of_last_particle_that_reach_the_ext_source(0),
76  nb_evt_of_last_run(0),area_of_the_adjoint_source(0),theAdjointPrimaryWeight(0)
77 {
78  //Create adjoint actions;
79  //----------------------
86  //Create messenger
87  //----------------
89 
91  use_user_StackingAction = false;
93 
94  adjoint_sim_mode = false;
95 
97 
98  nb_nuc=1.;
99 
100  welcome_message =true;
101 
102  //Define user action and set this class instance as RunAction
103  //----------------
104  //DefineUserActions();
105  //G4RunManager* theRunManager = G4RunManager::GetRunManager();
106 
107  //theRunManager->G4RunManager::SetUserAction(this);
108 /*
109 #ifdef G4MULTITHREADED
110 
111  if (theRunManager->GetRunManagerType() == G4RunManager::workerRM){
112  G4cout<<"Here"<<std::endl;
113  //G4MTAdjointSimManager::GetInstance()->RegisterLocalManager(this);
114  G4cout<<"Here1"<<std::endl;
115  }
116 #endif
117 */
118 }
120 //
122 {
129  if (theMessenger) delete theMessenger;
130 }
132 //
134 {
135  if (instance == 0) instance = new G4AdjointSimManager;
136  return instance;
137 }
139 //
141 { if (G4RunManager::GetRunManager()->GetRunManagerType() != G4RunManager::sequentialRM) return; //only for sequential mode
142  if (welcome_message) {
143  G4cout<<"****************************************************************"<<std::endl;
144  G4cout<<"*** Geant4 Reverse/Adjoint Monte Carlo mode ***"<<std::endl;
145  G4cout<<"*** Author: L.Desorgher ***"<<std::endl;
146  G4cout<<"*** Company: SpaceIT GmbH, Bern, Switzerland ***"<<std::endl;
147  G4cout<<"*** Sponsored by: ESA/ESTEC contract contract 21435/08/NL/AT ***"<<std::endl;
148  G4cout<<"****************************************************************"<<std::endl;
149  welcome_message=false;
150  }
151 
152  //Switch to adjoint simulation mode
153  //---------------------------------------------------------
155 
156  //Make the run
157  //------------
158 
159  nb_evt_of_last_run =nb_evt;
161  //G4RunManager::GetRunManager()->BeamOn(theAdjointPrimaryGeneratorAction->GetNbOfAdjointPrimaryTypes()*2*nb_evt);
162 
163  //Back to Fwd Simulation Mode
164  //--------------------------------
166 
167  /*
168  //Register the weight vector
169  //--------------------------
170  std::ofstream FileOutputElectronWeight("ElectronWeight.txt", std::ios::out);
171  FileOutputElectronWeight<<std::setiosflags(std::ios::scientific);
172  FileOutputElectronWeight<<std::setprecision(6);
173  G4bool aBool = electron_last_weight_vector->Store(FileOutputElectronWeight, true);
174  FileOutputElectronWeight.close();
175 
176  std::ofstream FileOutputProtonWeight("ProtonWeight.txt", std::ios::out);
177  FileOutputProtonWeight<<std::setiosflags(std::ios::scientific);
178  FileOutputProtonWeight<<std::setprecision(6);
179  aBool = proton_last_weight_vector->Store(FileOutputProtonWeight, true);
180  FileOutputProtonWeight.close();
181 
182  std::ofstream FileOutputGammaWeight("GammaWeight.txt", std::ios::out);
183  FileOutputGammaWeight<<std::setiosflags(std::ios::scientific);
184  FileOutputGammaWeight<<std::setprecision(6);
185  aBool = gamma_last_weight_vector->Store(FileOutputGammaWeight, true);
186  FileOutputGammaWeight.close();
187  */
188 }
190 //
192 {
193  G4RunManager* theRunManager = G4RunManager::GetRunManager();
194 
196 
197  //Replace the user action by the adjoint actions
198  //-------------------------------------------------
199 
200  theRunManager->G4RunManager::SetUserAction(theAdjointEventAction);
201  theRunManager->G4RunManager::SetUserAction(theAdjointSteppingAction);
202  theRunManager->G4RunManager::SetUserAction(theAdjointTrackingAction);
203 
204 }
206 //
208 { //Replace the user defined actions by the adjoint actions
209  //---------------------------------------------------------
211 
212  //Update the list of primaries
213  //-----------------------------
215  adjoint_sim_mode=true;
217 }
219 //
221 { //Restore the user defined actions
222  //--------------------------------
224  adjoint_sim_mode=false;
225 }
226 
228 //
230 {
231  G4RunManager* theRunManager = G4RunManager::GetRunManager();
232 
234 
235 
236  //Replace the user action by the adjoint actions
237  //-------------------------------------------------
238  theRunManager->G4RunManager::SetUserAction(this);
239  theRunManager->G4RunManager::SetUserAction(theAdjointPrimaryGeneratorAction);
240  theRunManager->G4RunManager::SetUserAction(theAdjointStackingAction);
243  theRunManager->G4RunManager::SetUserAction(theAdjointEventAction);
244  theRunManager->G4RunManager::SetUserAction(theAdjointSteppingAction);
245  theRunManager->G4RunManager::SetUserAction(theAdjointTrackingAction);
248 }
250 //
252 {
253  G4RunManager* theRunManager = G4RunManager::GetRunManager();
254 
256 
257  //Replace the user action by the adjoint actions
258  //-------------------------------------------------
259 
260  theRunManager->G4RunManager::SetUserAction(theAdjointRunAction);
261  theRunManager->G4RunManager::SetUserAction(theAdjointPrimaryGeneratorAction);
262  theRunManager->G4RunManager::SetUserAction(theAdjointStackingAction);
265 }
267 //
269 {
270  G4RunManager* theRunManager = G4RunManager::GetRunManager();
271 
272  //Restore the user defined actions
273  //-------------------------------
274  theRunManager->G4RunManager::SetUserAction(fUserRunAction);
275  theRunManager->G4RunManager::SetUserAction(fUserEventAction);
276  theRunManager->G4RunManager::SetUserAction(fUserSteppingAction);
277  theRunManager->G4RunManager::SetUserAction(fUserTrackingAction);
278  theRunManager->G4RunManager::SetUserAction(fUserPrimaryGeneratorAction);
279  theRunManager->G4RunManager::SetUserAction(fUserStackingAction);
280 }
282 //
284 {
285  G4RunManager* theRunManager = G4RunManager::GetRunManager();
286 
287  //Restore the user defined actions
288  //-------------------------------
289 
290  theRunManager->G4RunManager::SetUserAction(fUserEventAction);
291  theRunManager->G4RunManager::SetUserAction(fUserSteppingAction);
292  theRunManager->G4RunManager::SetUserAction(fUserTrackingAction);
293 }
294 
296 //
298 {
299  G4RunManager* theRunManager = G4RunManager::GetRunManager();
300  //Restore the user defined actions
301  //-------------------------------
302  theRunManager->G4RunManager::SetUserAction(fUserRunAction);
303  theRunManager->G4RunManager::SetUserAction(fUserPrimaryGeneratorAction);
304  theRunManager->G4RunManager::SetUserAction(fUserStackingAction);
305 }
307 //
309 {
310  G4RunManager* theRunManager = G4RunManager::GetRunManager();
311  fUserTrackingAction= const_cast<G4UserTrackingAction* >( theRunManager->GetUserTrackingAction() );
312  fUserEventAction= const_cast<G4UserEventAction* >( theRunManager->GetUserEventAction() );
313  fUserSteppingAction= const_cast<G4UserSteppingAction* >( theRunManager->GetUserSteppingAction() );
316  fUserRunAction= const_cast<G4UserRunAction*>( theRunManager->GetUserRunAction() );
317  fUserStackingAction= const_cast<G4UserStackingAction* >( theRunManager->GetUserStackingAction() );
319 }
321 //
324 }
326 //
328 {
329  adjoint_tracking_mode = aBool;
330 
331  if (adjoint_tracking_mode) {
335 
336  }
337  else {
338 
344  }
346  }
347 }
349 //
351 {
353 }
354 
356 //
357 std::vector<G4ParticleDefinition*>* G4AdjointSimManager::GetListOfPrimaryFwdParticles()
358 {
360 }
362 //
364 {
366 }
367 
369 //
372 }
373 
375 //
378 }
380 //
383 }
385 //
388 }
390 //
393 }
395 //
398 }
400 //
403 }
405 //
408 }
409 
411 //
414 }
416 //
418 {
420 }
422 //
425 }
426 
428 //
430 {
436 
437  last_fwd_part_name= aPartDef->GetParticleName();
438 
440 
442 
443  std::vector<G4ParticleDefinition*>* aList = theAdjointPrimaryGeneratorAction->GetListOfPrimaryFwdParticles();
445  size_t i=0;
446  while(i<aList->size() && last_fwd_part_index<0) {
447  if ((*aList)[i]->GetParticleName() == last_fwd_part_name) last_fwd_part_index=i;
448  i++;
449  }
450 
453  if (aPartDef->GetParticleType() == "adjoint_nucleus") {
454  nb_nuc=double(aPartDef->GetBaryonNumber());
456  }
457 
459 
460 
461 
462  last_pos_vec.push_back(last_pos);
464  last_ekin_vec.push_back(last_ekin);
465  last_ekin_nuc_vec.push_back(last_ekin_nuc);
466  last_cos_th_vec.push_back(last_cos_th);
467  last_weight_vec.push_back(last_weight);
472 
473 
474 
475 
476 
477 
478  /* G4PhysicsLogVector* theWeightVector=0;
479  if (last_fwd_part_name =="e-") theWeightVector=electron_last_weight_vector;
480  else if (last_fwd_part_name =="gamma") theWeightVector=gamma_last_weight_vector;
481  else if (last_fwd_part_name =="proton") theWeightVector=proton_last_weight_vector;
482 
483  if (theWeightVector){
484 
485  size_t ind = size_t(std::log10(last_weight/theAdjointPrimaryWeight)*10. + 200);
486  G4double low_val =theWeightVector->GetLowEdgeEnergy(ind);
487  G4bool aBool = true;
488  G4double bin_weight = theWeightVector->GetValue(low_val, aBool)+1.;
489  theWeightVector->PutValue(ind, bin_weight);
490  }
491  */
492  /*if ((last_weight/theAdjointPrimaryWeight)>1.) last_weight*=1000. ;
493  else if ( (last_weight/theAdjointPrimaryWeight)>0.1) last_weight*=100. ;
494  else if ( (last_weight/theAdjointPrimaryWeight)>0.01) last_weight*=10. ;*/
495 
496 
497  //G4cout <<"Last Weight "<<last_weight<<'\t'<<theAdjointPrimaryWeight<<'\t'<<last_weight/theAdjointPrimaryWeight<<std::endl;
498  /*if (last_weight/theAdjointPrimaryWeight >10.) {
499  G4cout<<"Warning a weight increase by a factor : "<<last_weight/theAdjointPrimaryWeight<<std::endl;
500  }
501  */
502 
503 
504 }
506 //
508 {
509  G4double area;
510  return G4AdjointCrossSurfChecker::GetInstance()->AddaSphericalSurface("ExternalSource", radius, pos, area);
511 }
513 //
515 {
516  G4double area;
517  G4ThreeVector center;
518  return G4AdjointCrossSurfChecker::GetInstance()->AddaSphericalSurfaceWithCenterAtTheCenterOfAVolume( "ExternalSource", radius, volume_name,center, area);
519 }
521 //
523 {
524  G4double area;
525  return G4AdjointCrossSurfChecker::GetInstance()->AddanExtSurfaceOfAvolume( "ExternalSource", volume_name,area);
526 }
528 //
530 {
532 }
534 //
536 {
537  G4double area;
538  G4bool aBool = G4AdjointCrossSurfChecker::GetInstance()->AddaSphericalSurface("AdjointSource", radius, pos, area);
541  return aBool;
542 }
544 //
546 {
547  G4double area;
548  G4ThreeVector center;
549  G4bool aBool = G4AdjointCrossSurfChecker::GetInstance()->AddaSphericalSurfaceWithCenterAtTheCenterOfAVolume( "AdjointSource", radius, volume_name,center, area);
552  return aBool;
553 }
555 //
557 {
558  G4double area;
559  G4bool aBool = G4AdjointCrossSurfChecker::GetInstance()->AddanExtSurfaceOfAvolume( "AdjointSource", volume_name,area);
561  if (aBool) {
563  }
564  return aBool;
565 }
567 //
569 {
571 }
573 //
575 {
577 }
579 //
581 {
583 }
585 //
587 {
589 }
591 //
592 /*void G4AdjointSimManager::SetPrimaryIon(G4int Z, G4int A)
593 {
594  theAdjointPrimaryGeneratorAction->SetPrimaryIon(Z, A);
595 }
596 */
598 //
600 {
601  theAdjointPrimaryGeneratorAction->SetPrimaryIon(adjointIon, fwdIon);
602 }
604 //
606 {
608 }
610 //
612 {
613  theAdjointPrimaryWeight = aWeight;
615 }
616 
618 //
620 {
621  theAdjointEventAction = anAction;
622 }
624 //
626 {
628 }
630 //
632 {
634 }
635 
637 //
639 {
640  theAdjointRunAction=anAction;
641 }
643 //
645 {
647 }
649 //
651 {
653 }
655 //
657 {
659 }
661 //
663 {
664 /*
665  if (!adjoint_sim_mode){
666  if(fUserRunAction) fUserRunAction->BeginOfRunAction(aRun);
667  }
668  else {
669  if (theAdjointRunAction) theAdjointRunAction->BeginOfRunAction(aRun);
670  }
671  */
673 }
675 //
677 {if (!adjoint_sim_mode){
679  }
681 /*
682 #ifdef G4MULTITHREADED
683  if (G4RunManager::GetRunManager()->GetRunManagerType() == G4RunManager::workerRM){
684  if (adjoint_sim_mode) BackToFwdSimulationMode();
685  }
686 #endif
687 */
688 
689 }
691 //
694 }
696 //
699 }
700 
701