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 
12 
14 
20 
22 
29 
31 
33 
34 #include "HepPDT/ParticleDataTable.hh"
35 
36 #include "G4GeometryManager.hh"
37 #include "G4StateManager.hh"
38 #include "G4ApplicationState.hh"
39 #include "G4MTRunManagerKernel.hh"
40 #include "G4UImanager.hh"
41 
42 #include "G4EventManager.hh"
43 #include "G4Run.hh"
44 #include "G4Event.hh"
45 #include "G4TransportationManager.hh"
46 #include "G4ParticleTable.hh"
47 #include "G4Field.hh"
48 #include "G4FieldManager.hh"
49 #include "G4CascadeInterface.hh"
50 
51 #include "G4GDMLParser.hh"
52 #include "G4SystemOfUnits.hh"
53 
54 #include <iostream>
55 #include <sstream>
56 #include <fstream>
57 #include <memory>
58 
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_g4overlap(p.getParameter<edm::ParameterSet>("G4CheckOverlap")),
72  m_G4Commands(p.getParameter<std::vector<std::string> >("G4Commands")),
73  m_fieldBuilder(nullptr)
74 {
75  m_currentRun = nullptr;
76  m_kernel = new G4MTRunManagerKernel();
77 
78  m_check = p.getUntrackedParameter<bool>("CheckOverlap",false);
79  m_WriteFile = p.getUntrackedParameter<std::string>("FileNameGDML","");
80  m_FieldFile = p.getUntrackedParameter<std::string>("FileNameField","");
81  m_RegionFile = p.getUntrackedParameter<std::string>("FileNameRegions","");
82 }
83 
85 {
86  if(!m_runTerminated) { terminateRun(); }
87  G4StateManager::GetStateManager()->SetNewState(G4State_Quit);
88  G4GeometryManager::GetInstance()->OpenGeometry();
89 }
90 
91 void RunManagerMT::initG4(const DDCompactView *pDD, const MagneticField *pMF,
92  const HepPDT::ParticleDataTable *fPDGTable)
93 {
94  if (m_managerInitialized) return;
95 
96  edm::LogInfo("SimG4CoreApplication")
97  << "RunManagerMT: start initialisation of geometry";
98 
99  // DDDWorld: get the DDCV from the ES and use it to build the World
101  m_world.reset(new DDDWorld(pDD, map_, m_catalog, false));
103 
104  // setup the magnetic field
105  edm::LogInfo("SimG4CoreApplication")
106  << "RunManagerMT: start initialisation of magnetic field";
107 
109  {
110  const GlobalPoint g(0.,0.,0.);
111 
114  G4TransportationManager * tM =
115  G4TransportationManager::GetTransportationManager();
116  m_fieldBuilder->build( tM->GetFieldManager(),
117  tM->GetPropagatorInField());
118  if("" != m_FieldFile) {
119  DumpMagneticField(tM->GetFieldManager()->GetDetectorField());
120  }
121  }
122 
123  // Create physics list
124  edm::LogInfo("SimG4CoreApplication")
125  << "RunManagerMT: create PhysicsList";
126 
127  std::unique_ptr<PhysicsListMakerBase>
128  physicsMaker(PhysicsListFactory::get()->create(
129  m_pPhysics.getParameter<std::string> ("type")));
130  if (physicsMaker.get()==0) {
131  throw SimG4Exception("Unable to find the Physics list requested");
132  }
133  m_physicsList =
134  physicsMaker->make(map_,fPDGTable,m_chordFinderSetter.get(),m_pPhysics,m_registry);
135 
136  PhysicsList* phys = m_physicsList.get();
137  if (phys==0) {
138  throw SimG4Exception("Physics list construction failed!");
139  }
140 
141  // adding GFlash, Russian Roulette for eletrons and gamma,
142  // step limiters on top of any Physics Lists
143  phys->RegisterPhysics(new ParametrisedEMPhysics("EMoptions",m_pPhysics));
144 
145  m_physicsList->ResetStoredInAscii();
147  m_physicsList->SetPhysicsTableRetrieved(m_PhysicsTablesDir);
148  }
149  edm::LogInfo("SimG4CoreApplication")
150  << "RunManagerMT: start initialisation of PhysicsList for master";
151 
152  int verb = m_pPhysics.getUntrackedParameter<int>("Verbosity",0);
153  m_physicsList->SetDefaultCutValue(m_pPhysics.getParameter<double>("DefaultCutValue")*CLHEP::cm);
154  m_physicsList->SetCutsWithDefault();
155  m_prodCuts.reset(new DDG4ProductionCuts(map_, verb, m_pPhysics));
156  m_prodCuts->update();
157 
158  m_kernel->SetPhysics(phys);
159  m_kernel->InitializePhysics();
160  m_kernel->SetUpDecayChannels();
161 
162  // The following line was with the following comment in
163  // G4MTRunManager::InitializePhysics() in 10.00.p01; in practice
164  // needed to initialize certain singletons during the master thread
165  // initialization in order to avoid races later...
166  //
167  //BERTINI, this is needed to create pseudo-particles, to be removed
168  G4CascadeInterface::Initialize();
169 
170  if (m_kernel->RunInitialization()) { m_managerInitialized = true; }
171  else {
172  throw SimG4Exception("G4RunManagerKernel initialization failed!");
173  }
174 
175  if (m_StorePhysicsTables) {
176  std::ostringstream dir;
177  dir << m_PhysicsTablesDir << '\0';
178  std::string cmd = std::string("/control/shell mkdir -p ")+m_PhysicsTablesDir;
179  if (!std::ifstream(dir.str().c_str(), std::ios::in))
180  G4UImanager::GetUIpointer()->ApplyCommand(cmd);
181  m_physicsList->StorePhysicsTable(m_PhysicsTablesDir);
182  }
183 
185 
186  if(0 < m_G4Commands.size()) {
187  G4cout << "RunManagerMT: Requested UI commands: " << G4endl;
188  for (unsigned it=0; it<m_G4Commands.size(); ++it) {
189  G4cout << " " << m_G4Commands[it] << G4endl;
190  G4UImanager::GetUIpointer()->ApplyCommand(m_G4Commands[it]);
191  }
192  }
193 
194  if(verb > 1) { m_physicsList->DumpCutValuesTable(); }
195 
196  // geometry dump
197  if("" != m_WriteFile) {
198  G4GDMLParser gdml(new G4GDMLReadStructure(), new CMSGDMLWriteStructure());
199  gdml.Write(m_WriteFile, m_world->GetWorldVolume(), true);
200  }
201 
202  // G4Region dump
203  if("" != m_RegionFile) {
204  G4RegionReporter rrep;
206  }
207 
208  // Intersection check
210 
211  // If the Geant4 particle table is needed, decomment the lines below
212  //
213  //G4ParticleTable::GetParticleTable()->DumpTable("ALL");
214  //
215  G4StateManager::GetStateManager()->SetNewState(G4State_GeomClosed);
216  m_currentRun = new G4Run();
218 }
219 
221  m_runInterface.reset(new SimRunInterface(this, true));
224 }
225 
227 {
230 }
231 
233 {
234  G4StateManager::GetStateManager()->SetNewState(G4State_Quit);
235  if(!m_runTerminated) { terminateRun(); }
236 }
237 
240  delete m_userRunAction;
241  m_userRunAction = 0;
242  if(m_kernel && !m_runTerminated) {
243  m_kernel->RunTermination();
244  m_runTerminated = true;
245  }
246 }
247 
248 void RunManagerMT::DumpMagneticField(const G4Field* field) const
249 {
250  std::ofstream fout(m_FieldFile.c_str(), std::ios::out);
251  if(fout.fail()){
252  edm::LogWarning("SimG4CoreApplication")
253  << " RunManager WARNING : "
254  << "error opening file <" << m_FieldFile << "> for magnetic field";
255  } else {
256  double rmax = 9000*mm;
257  double zmax = 16000*mm;
258 
259  double dr = 5*cm;
260  double dz = 20*cm;
261 
262  int nr = (int)(rmax/dr);
263  int nz = 2*(int)(zmax/dz);
264 
265  double r = 0.0;
266  double z0 = -zmax;
267  double z;
268 
269  double phi = 0.0;
270  double cosf = cos(phi);
271  double sinf = sin(phi);
272 
273  double point[4] = {0.0,0.0,0.0,0.0};
274  double bfield[3] = {0.0,0.0,0.0};
275 
276  fout << std::setprecision(6);
277  for(int i=0; i<=nr; ++i) {
278  z = z0;
279  for(int j=0; j<=nz; ++j) {
280  point[0] = r*cosf;
281  point[1] = r*sinf;
282  point[2] = z;
283  field->GetFieldValue(point, bfield);
284  fout << "R(mm)= " << r/mm << " phi(deg)= " << phi/degree
285  << " Z(mm)= " << z/mm << " Bz(tesla)= " << bfield[2]/tesla
286  << " Br(tesla)= " << (bfield[0]*cosf + bfield[1]*sinf)/tesla
287  << " Bphi(tesla)= " << (bfield[0]*sinf - bfield[1]*cosf)/tesla
288  << G4endl;
289  z += dz;
290  }
291  r += dr;
292  }
293 
294  fout.close();
295  }
296 }
void Connect(RunAction *)
const std::string m_PhysicsTablesDir
Definition: RunManagerMT.h:123
bool m_managerInitialized
Definition: RunManagerMT.h:116
T getParameter(std::string const &) const
Map map_
T getUntrackedParameter(std::string const &, T const &) const
int i
Definition: DBlmapReader.cc:9
std::unique_ptr< DDG4ProductionCuts > m_prodCuts
Definition: RunManagerMT.h:134
G4MTRunManagerKernel * m_kernel
Definition: RunManagerMT.h:113
SensitiveDetectorCatalog m_catalog
Definition: RunManagerMT.h:136
edm::ParameterSet m_pRunAction
Definition: RunManagerMT.h:129
HepPDT::ParticleDataTable ParticleDataTable
void initG4(const DDCompactView *pDD, const MagneticField *pMF, const HepPDT::ParticleDataTable *fPDGTable)
Definition: RunManagerMT.cc:91
void terminateRun()
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
SimActivityRegistry::EndOfRunSignal m_endOfRunSignal
Definition: RunAction.h:24
bool m_runTerminated
Definition: RunManagerMT.h:117
sim::FieldBuilder * m_fieldBuilder
Definition: RunManagerMT.h:138
BeginOfRunSignal beginOfRunSignal_
std::vector< std::string > m_G4Commands
Definition: RunManagerMT.h:131
G4Run * m_currentRun
Definition: RunManagerMT.h:120
std::string m_RegionFile
Definition: RunManagerMT.h:143
#define nullptr
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
std::string m_WriteFile
Definition: RunManagerMT.h:142
RunManagerMT(edm::ParameterSet const &p)
Definition: RunManagerMT.cc:61
void build(G4FieldManager *fM=nullptr, G4PropagatorInField *fP=nullptr, ChordFinderSetter *setter=nullptr)
Definition: FieldBuilder.cc:48
bool check(const std::string &)
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
int j
Definition: DBlmapReader.cc:9
bool m_pUseMagneticField
Definition: RunManagerMT.h:118
void EndOfRunAction(const G4Run *aRun)
Definition: RunAction.cc:39
EndOfRunSignal endOfRunSignal_
void DumpMagneticField(const G4Field *) const
void connect(Observer< const T * > *iObs)
does not take ownership of memory
Definition: Signaler.h:59
bool m_StorePhysicsTables
Definition: RunManagerMT.h:124
std::unique_ptr< PhysicsList > m_physicsList
Definition: RunManagerMT.h:115
std::string m_FieldFile
Definition: RunManagerMT.h:141
edm::ParameterSet m_pField
Definition: RunManagerMT.h:127
std::unique_ptr< DDDWorld > m_world
Definition: RunManagerMT.h:133
edm::ParameterSet m_pPhysics
Definition: RunManagerMT.h:128
void BeginOfRunAction(const G4Run *aRun)
Definition: RunAction.cc:21
DDDWorldSignal dddWorldSignal_
bool m_RestorePhysicsTables
Definition: RunManagerMT.h:125
std::unique_ptr< sim::ChordFinderSetter > m_chordFinderSetter
Definition: RunManagerMT.h:139
void ReportRegions(const std::string &ss)
string cmd
self.logger.debug(&quot;Path is now `%s&#39;&quot; % \ path)
SimActivityRegistry::BeginOfRunSignal m_beginOfRunSignal
Definition: RunAction.h:23
std::unique_ptr< SimRunInterface > m_runInterface
Definition: RunManagerMT.h:121
edm::ParameterSet m_g4overlap
Definition: RunManagerMT.h:130
dbl *** dir
Definition: mlp_gen.cc:35
volatile std::atomic< bool > shutdown_flag false
RunAction * m_userRunAction
Definition: RunManagerMT.h:119
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:135