CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
RunManagerMT.cc
Go to the documentation of this file.
8 
11 
13 
19 
21 
25 
27 
29 
30 #include "HepPDT/defs.h"
31 #include "HepPDT/TableBuilder.hh"
32 #include "HepPDT/ParticleDataTable.hh"
33 
34 #include "G4GeometryManager.hh"
35 #include "G4StateManager.hh"
36 #include "G4ApplicationState.hh"
37 #include "G4MTRunManagerKernel.hh"
38 #include "G4UImanager.hh"
39 
40 #include "G4EventManager.hh"
41 #include "G4Run.hh"
42 #include "G4Event.hh"
43 #include "G4TransportationManager.hh"
44 #include "G4ParticleTable.hh"
45 #include "G4Field.hh"
46 #include "G4FieldManager.hh"
47 #include "G4CascadeInterface.hh"
48 
49 #include "G4GDMLParser.hh"
50 #include "G4SystemOfUnits.hh"
51 
52 #include <iostream>
53 #include <sstream>
54 #include <fstream>
55 #include <memory>
56 
58 
59 //#include "SimG4Core/Application/interface/ExceptionHandler.h"
60 
62  m_managerInitialized(false),
63  m_runTerminated(false),
64  m_pUseMagneticField(p.getParameter<bool>("UseMagneticField")),
65  m_PhysicsTablesDir(p.getParameter<std::string>("PhysicsTablesDirectory")),
66  m_StorePhysicsTables(p.getParameter<bool>("StorePhysicsTables")),
67  m_RestorePhysicsTables(p.getParameter<bool>("RestorePhysicsTables")),
68  m_pField(p.getParameter<edm::ParameterSet>("MagneticField")),
69  m_pPhysics(p.getParameter<edm::ParameterSet>("Physics")),
70  m_pRunAction(p.getParameter<edm::ParameterSet>("RunAction")),
71  m_G4Commands(p.getParameter<std::vector<std::string> >("G4Commands")),
72  m_fieldBuilder(nullptr)
73 {
74  G4RunManagerKernel *kernel = G4MTRunManagerKernel::GetRunManagerKernel();
75  if(!kernel) m_kernel = new G4MTRunManagerKernel();
76  else {
77  m_kernel = dynamic_cast<G4MTRunManagerKernel *>(kernel);
78  assert(m_kernel);
79  }
80 
81  m_check = p.getUntrackedParameter<bool>("CheckOverlap",false);
82  m_WriteFile = p.getUntrackedParameter<std::string>("FileNameGDML","");
83  m_FieldFile = p.getUntrackedParameter<std::string>("FileNameField","");
84  if("" != m_FieldFile) { m_FieldFile += ".txt"; }
85 }
86 
88 {
89  if(!m_runTerminated) { terminateRun(); }
90  G4StateManager::GetStateManager()->SetNewState(G4State_Quit);
91  G4GeometryManager::GetInstance()->OpenGeometry();
92 }
93 
94 void RunManagerMT::initG4(const DDCompactView *pDD, const MagneticField *pMF, const HepPDT::ParticleDataTable *fPDGTable)
95 {
96  if (m_managerInitialized) return;
97 
98  // DDDWorld: get the DDCV from the ES and use it to build the World
100  m_world.reset(new DDDWorld(pDD, map_, m_catalog, m_check));
102 
103  if("" != m_WriteFile) {
104  G4GDMLParser gdml;
105  gdml.Write(m_WriteFile, m_world->GetWorldVolume());
106  }
107 
108  // setup the magnetic field
110  {
111  const GlobalPoint g(0.,0.,0.);
112 
115  G4TransportationManager * tM =
116  G4TransportationManager::GetTransportationManager();
117  m_fieldBuilder->build( tM->GetFieldManager(),
118  tM->GetPropagatorInField(),
119  m_chordFinderSetter.get());
120  if("" != m_FieldFile) {
121  DumpMagneticField(tM->GetFieldManager()->GetDetectorField());
122  }
123  }
124 
125  // Create physics list
126  std::unique_ptr<PhysicsListMakerBase>
127  physicsMaker(PhysicsListFactory::get()->create(
128  m_pPhysics.getParameter<std::string> ("type")));
129  if (physicsMaker.get()==0) {
130  throw SimG4Exception("Unable to find the Physics list requested");
131  }
132  m_physicsList =
133  physicsMaker->make(map_,fPDGTable,m_chordFinderSetter.get(),m_pPhysics,m_registry);
134 
135  PhysicsList* phys = m_physicsList.get();
136  if (phys==0) {
137  throw SimG4Exception("Physics list construction failed!");
138  }
139 
140  // adding GFlash, Russian Roulette for eletrons and gamma,
141  // step limiters on top of any Physics Lists
142  phys->RegisterPhysics(new ParametrisedEMPhysics("EMoptions",m_pPhysics));
143 
144  m_kernel->SetPhysics(phys);
145  m_kernel->InitializePhysics();
146  // The following line was with the following comment in
147  // G4MTRunManager::InitializePhysics() in 10.00.p01; in practice
148  // needed to initialize certain singletons during the master thread
149  // initialization in order to avoid races later...
150  //
151  //BERTINI, this is needed to create pseudo-particles, to be removed
152  G4CascadeInterface::Initialize();
153  //
154 
155  m_physicsList->ResetStoredInAscii();
157  m_physicsList->SetPhysicsTableRetrieved(m_PhysicsTablesDir);
158  }
159 
160  if (m_kernel->RunInitialization()) { m_managerInitialized = true; }
161  else {
162  throw SimG4Exception("G4RunManagerKernel initialization failed!");
163  }
164 
166  {
167  std::ostringstream dir;
168  dir << m_PhysicsTablesDir << '\0';
169  std::string cmd = std::string("/control/shell mkdir -p ")+m_PhysicsTablesDir;
170  if (!std::ifstream(dir.str().c_str(), std::ios::in))
171  G4UImanager::GetUIpointer()->ApplyCommand(cmd);
172  m_physicsList->StorePhysicsTable(m_PhysicsTablesDir);
173  }
174 
176 
177  for (unsigned it=0; it<m_G4Commands.size(); it++) {
178  edm::LogInfo("SimG4CoreApplication") << "RunManagerMT:: Requests UI: "
179  << m_G4Commands[it];
180  G4UImanager::GetUIpointer()->ApplyCommand(m_G4Commands[it]);
181  }
182 
183  // If the Geant4 particle table is needed, decomment the lines below
184  //
185  // G4cout << "Output of G4ParticleTable DumpTable:" << G4endl;
186  // G4ParticleTable::GetParticleTable()->DumpTable("ALL");
187 }
188 
190  m_runInterface.reset(new SimRunInterface(this, true));
191 
193  Connect(m_userRunAction.get());
194 }
195 
197 {
200 }
201 
203 {
204  G4StateManager::GetStateManager()->SetNewState(G4State_Quit);
205  if(!m_runTerminated) { terminateRun(); }
206 }
207 
209  m_userRunAction.reset();
210  if(m_kernel && !m_runTerminated) {
211  m_kernel->RunTermination();
212  m_runTerminated = true;
213  }
214 }
215 
216 void RunManagerMT::DumpMagneticField(const G4Field* field) const
217 {
218  std::ofstream fout(m_FieldFile.c_str(), std::ios::out);
219  if(fout.fail()){
220  edm::LogWarning("SimG4CoreApplication")
221  << " RunManager WARNING : "
222  << "error opening file <" << m_FieldFile << "> for magnetic field";
223  } else {
224  double rmax = 9000*mm;
225  double zmax = 16000*mm;
226 
227  double dr = 5*cm;
228  double dz = 20*cm;
229 
230  int nr = (int)(rmax/dr);
231  int nz = 2*(int)(zmax/dz);
232 
233  double r = 0.0;
234  double z0 = -zmax;
235  double z;
236 
237  double phi = 0.0;
238  double cosf = cos(phi);
239  double sinf = sin(phi);
240 
241  double point[4] = {0.0,0.0,0.0,0.0};
242  double bfield[3] = {0.0,0.0,0.0};
243 
244  fout << std::setprecision(6);
245  for(int i=0; i<=nr; ++i) {
246  z = z0;
247  for(int j=0; j<=nz; ++j) {
248  point[0] = r*cosf;
249  point[1] = r*sinf;
250  point[2] = z;
251  field->GetFieldValue(point, bfield);
252  fout << "R(mm)= " << r/mm << " phi(deg)= " << phi/degree
253  << " Z(mm)= " << z/mm << " Bz(tesla)= " << bfield[2]/tesla
254  << " Br(tesla)= " << (bfield[0]*cosf + bfield[1]*sinf)/tesla
255  << " Bphi(tesla)= " << (bfield[0]*sinf - bfield[1]*cosf)/tesla
256  << G4endl;
257  z += dz;
258  }
259  r += dr;
260  }
261 
262  fout.close();
263  }
264 }
void Connect(RunAction *)
const std::string m_PhysicsTablesDir
Definition: RunManagerMT.h:112
bool m_managerInitialized
Definition: RunManagerMT.h:106
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
int i
Definition: DBlmapReader.cc:9
SensitiveDetectorCatalog m_catalog
Definition: RunManagerMT.h:124
edm::ParameterSet m_pRunAction
Definition: RunManagerMT.h:118
HepPDT::ParticleDataTable ParticleDataTable
void initG4(const DDCompactView *pDD, const MagneticField *pMF, const HepPDT::ParticleDataTable *fPDGTable)
Definition: RunManagerMT.cc:94
void terminateRun()
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
#define nullptr
SimActivityRegistry::EndOfRunSignal m_endOfRunSignal
Definition: RunAction.h:23
bool m_runTerminated
Definition: RunManagerMT.h:107
sim::FieldBuilder * m_fieldBuilder
Definition: RunManagerMT.h:126
BeginOfRunSignal beginOfRunSignal_
std::vector< std::string > m_G4Commands
Definition: RunManagerMT.h:119
void initializeUserActions()
type of data representation of DDCompactView
Definition: DDCompactView.h:77
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e g
Definition: Activities.doc:4
float float float z
std::string m_WriteFile
Definition: RunManagerMT.h:130
RunManagerMT(edm::ParameterSet const &p)
Definition: RunManagerMT.cc:61
void build(G4FieldManager *fM=nullptr, G4PropagatorInField *fP=nullptr, ChordFinderSetter *setter=nullptr)
Definition: FieldBuilder.cc:48
std::unique_ptr< RunAction > m_userRunAction
Definition: RunManagerMT.h:109
G4RunManagerKernel * m_kernel
Definition: RunManagerMT.h:103
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
const bool m_pUseMagneticField
Definition: RunManagerMT.h:108
string cmd
Definition: asciidump.py:19
int j
Definition: DBlmapReader.cc:9
EndOfRunSignal endOfRunSignal_
void DumpMagneticField(const G4Field *) const
void connect(Observer< const T * > *iObs)
does not take ownership of memory
Definition: Signaler.h:59
tuple out
Definition: dbtoconf.py:99
bool m_StorePhysicsTables
Definition: RunManagerMT.h:113
std::unique_ptr< PhysicsList > m_physicsList
Definition: RunManagerMT.h:105
std::string m_FieldFile
Definition: RunManagerMT.h:129
edm::ParameterSet m_pField
Definition: RunManagerMT.h:116
std::unique_ptr< DDDWorld > m_world
Definition: RunManagerMT.h:122
edm::ParameterSet m_pPhysics
Definition: RunManagerMT.h:117
DDDWorldSignal dddWorldSignal_
bool m_RestorePhysicsTables
Definition: RunManagerMT.h:114
std::unique_ptr< sim::ChordFinderSetter > m_chordFinderSetter
Definition: RunManagerMT.h:127
SimActivityRegistry::BeginOfRunSignal m_beginOfRunSignal
Definition: RunAction.h:22
std::unique_ptr< SimRunInterface > m_runInterface
Definition: RunManagerMT.h:110
dbl *** dir
Definition: mlp_gen.cc:35
volatile std::atomic< bool > shutdown_flag false
SurfaceDeformation * create(int type, const std::vector< double > &params)
T get(const Candidate &c)
Definition: component.h:55
*vegas h *****************************************************used in the default bin number in original ***version of VEGAS is ***a higher bin number might help to derive a more precise ***grade subtle point
Definition: invegas.h:5
SimActivityRegistry m_registry
Definition: RunManagerMT.h:123
Definition: DDAxes.h:10