CMS 3D CMS Logo

RunManagerMT.cc
Go to the documentation of this file.
8 
12 
14 
18 
23 
25 
33 
35 
37 
38 #include "HepPDT/ParticleDataTable.hh"
39 
40 #include "G4GeometryManager.hh"
41 #include "G4StateManager.hh"
42 #include "G4ApplicationState.hh"
43 #include "G4MTRunManagerKernel.hh"
44 #include "G4UImanager.hh"
45 
46 #include "G4EventManager.hh"
47 #include "G4Run.hh"
48 #include "G4Event.hh"
49 #include "G4TransportationManager.hh"
50 #include "G4ParticleTable.hh"
51 #include "G4Field.hh"
52 #include "G4FieldManager.hh"
53 #include "G4CascadeInterface.hh"
54 
55 #include "G4GDMLParser.hh"
56 #include "G4SystemOfUnits.hh"
57 
58 #include <iostream>
59 #include <sstream>
60 #include <fstream>
61 #include <memory>
62 
64 
66  m_managerInitialized(false),
67  m_runTerminated(false),
68  m_pUseMagneticField(p.getParameter<bool>("UseMagneticField")),
69  m_PhysicsTablesDir(p.getParameter<std::string>("PhysicsTablesDirectory")),
70  m_StorePhysicsTables(p.getParameter<bool>("StorePhysicsTables")),
71  m_RestorePhysicsTables(p.getParameter<bool>("RestorePhysicsTables")),
72  m_pField(p.getParameter<edm::ParameterSet>("MagneticField")),
73  m_pPhysics(p.getParameter<edm::ParameterSet>("Physics")),
74  m_pRunAction(p.getParameter<edm::ParameterSet>("RunAction")),
75  m_g4overlap(p.getParameter<edm::ParameterSet>("G4CheckOverlap")),
76  m_G4Commands(p.getParameter<std::vector<std::string> >("G4Commands")),
77  m_p(p)
78 {
79  m_currentRun = nullptr;
80  m_UIsession.reset(new CustomUIsession());
81  m_physicsList.reset(nullptr);
82  m_world.reset(nullptr);
83 
84  m_runInterface.reset(nullptr);
85  m_prodCuts.reset(nullptr);
86  m_userRunAction = nullptr;
87  m_currentRun = nullptr;
88 
89  m_kernel = new G4MTRunManagerKernel();
90  m_stateManager = G4StateManager::GetStateManager();
91  m_stateManager->SetExceptionHandler(new ExceptionHandler());
92  m_geometryManager->G4GeometryManager::GetInstance();
93 
94  m_check = p.getUntrackedParameter<bool>("CheckOverlap",false);
95  m_WriteFile = p.getUntrackedParameter<std::string>("FileNameGDML","");
96  m_FieldFile = p.getUntrackedParameter<std::string>("FileNameField","");
97  m_RegionFile = p.getUntrackedParameter<std::string>("FileNameRegions","");
98 }
99 
101 {
102  stopG4();
103 }
104 
105 void RunManagerMT::initG4(const DDCompactView *pDD, const MagneticField *pMF,
106  const HepPDT::ParticleDataTable *fPDGTable)
107 {
108  if (m_managerInitialized) return;
109 
110  edm::LogVerbatim("SimG4CoreApplication")
111  << "RunManagerMT: start initialisation of geometry";
112 
113  // DDDWorld: get the DDCV from the ES and use it to build the World
115  m_world.reset(new DDDWorld(pDD, map_lv, m_catalog, false));
117 
118  // setup the magnetic field
119  edm::LogVerbatim("SimG4CoreApplication")
120  << "RunManagerMT: start initialisation of magnetic field";
121 
122  if (m_pUseMagneticField && !m_FieldFile.empty())
123  {
124  const GlobalPoint g(0.,0.,0.);
125  sim::FieldBuilder fieldBuilder(pMF, m_pField);
126  CMSFieldManager* fieldManager = new CMSFieldManager();
127  G4TransportationManager * tM =
128  G4TransportationManager::GetTransportationManager();
129  tM->SetFieldManager(fieldManager);
130  fieldBuilder.build( fieldManager, tM->GetPropagatorInField());
131  DumpMagneticField(tM->GetFieldManager()->GetDetectorField());
132  }
133 
134  // Create physics list
135  edm::LogVerbatim("SimG4CoreApplication")
136  << "RunManagerMT: create PhysicsList";
137 
138  std::unique_ptr<PhysicsListMakerBase>
139  physicsMaker(PhysicsListFactory::get()->create(
140  m_pPhysics.getParameter<std::string> ("type")));
141  if (physicsMaker.get()==nullptr) {
143  << "Unable to find the Physics list requested";
144  }
145  m_physicsList = physicsMaker->make(m_pPhysics,m_registry);
146 
147  PhysicsList* phys = m_physicsList.get();
148  if (phys==nullptr) {
150  "Physics list construction failed!");
151  }
152 
153  // exotic particle physics
154  double monopoleMass = m_pPhysics.getUntrackedParameter<double>("MonopoleMass",0);
155  if(monopoleMass > 0.0) {
156  phys->RegisterPhysics(new CMSMonopolePhysics(fPDGTable,m_pPhysics));
157  }
158  bool exotica = m_pPhysics.getUntrackedParameter<bool>("ExoticaTransport",false);
159  if(exotica) { CMSExoticaPhysics exo(phys, m_pPhysics); }
160 
161  // adding GFlash, Russian Roulette for eletrons and gamma,
162  // step limiters on top of any Physics Lists
163  phys->RegisterPhysics(new ParametrisedEMPhysics("EMoptions",m_pPhysics));
164 
166  m_physicsList->SetPhysicsTableRetrieved(m_PhysicsTablesDir);
167  }
168  edm::LogVerbatim("SimG4CoreApplication")
169  << "RunManagerMT: start initialisation of PhysicsList for master";
170 
171  int verb = std::max(m_pPhysics.getUntrackedParameter<int>("Verbosity",0),
172  m_p.getParameter<int>("SteppingVerbosity"));
173  m_kernel->SetVerboseLevel(verb);
174 
175  m_physicsList->SetDefaultCutValue(m_pPhysics.getParameter<double>("DefaultCutValue")*CLHEP::cm);
176  m_physicsList->SetCutsWithDefault();
177 
178  if(m_pPhysics.getParameter<bool>("CutsPerRegion")) {
179  m_prodCuts.reset(new DDG4ProductionCuts(map_lv, verb, m_pPhysics));
180  m_prodCuts->update();
181  }
182 
183  m_kernel->SetPhysics(phys);
184 
185  // Geant4 UI commands before initialisation of physics
186  if(!m_G4Commands.empty()) {
187  G4cout << "RunManagerMT: Requested UI commands: " << G4endl;
188  for (const std::string& command : m_G4Commands) {
189  G4cout << " " << command << G4endl;
190  G4UImanager::GetUIpointer()->ApplyCommand(command);
191  }
192  }
193 
194  m_stateManager->SetNewState(G4State_Init);
195  m_kernel->InitializePhysics();
196  m_kernel->SetUpDecayChannels();
197 
198  // The following line was with the following comment in
199  // G4MTRunManager::InitializePhysics() in 10.00.p01; in practice
200  // needed to initialize certain singletons during the master thread
201  // initialization in order to avoid races later...
202  //
203  //BERTINI, this is needed to create pseudo-particles, to be removed
204  G4CascadeInterface::Initialize();
205 
206  if (m_kernel->RunInitialization()) { m_managerInitialized = true; }
207  else {
209  "G4RunManagerKernel initialization failed!");
210  }
211 
212  if (m_StorePhysicsTables) {
213  std::ostringstream dir;
214  dir << m_PhysicsTablesDir << '\0';
215  std::string cmd = std::string("/control/shell mkdir -p ")+m_PhysicsTablesDir;
216  if (!std::ifstream(dir.str().c_str(), std::ios::in))
217  G4UImanager::GetUIpointer()->ApplyCommand(cmd);
218  m_physicsList->StorePhysicsTable(m_PhysicsTablesDir);
219  }
220 
222 
223  if(verb > 1) { m_physicsList->DumpCutValuesTable(); }
224 
225  // geometry dump
226  if(!m_WriteFile.empty()) {
227  G4GDMLParser gdml;
228  gdml.SetRegionExport(true);
229  gdml.SetEnergyCutsExport(true);
230  gdml.Write(m_WriteFile, m_world->GetWorldVolume(), true);
231  }
232 
233  // G4Region dump
234  if(!m_RegionFile.empty()) {
235  G4RegionReporter rrep;
237  }
238 
239  // Intersection check
241 
242  // If the Geant4 particle table is needed, decomment the lines below
243  //
244  //G4ParticleTable::GetParticleTable()->DumpTable("ALL");
245  //
246  m_stateManager->SetNewState(G4State_GeomClosed);
247  m_currentRun = new G4Run();
249 }
250 
252  m_runInterface.reset(new SimRunInterface(this, true));
255 }
256 
258 {
261 }
262 
264 {
265  m_geometryManager->OpenGeometry();
266  m_stateManager->SetNewState(G4State_Quit);
267  if(!m_runTerminated) { terminateRun(); }
268 }
269 
271  if(m_userRunAction) {
273  delete m_userRunAction;
274  m_userRunAction = nullptr;
275  }
276  if(m_kernel && !m_runTerminated) {
277  m_kernel->RunTermination();
278  }
279  m_runTerminated = true;
280 }
281 
282 void RunManagerMT::DumpMagneticField(const G4Field* field) const
283 {
284  std::ofstream fout(m_FieldFile.c_str(), std::ios::out);
285  if(fout.fail()){
286  edm::LogWarning("SimG4CoreApplication")
287  << " RunManager WARNING : "
288  << "error opening file <" << m_FieldFile << "> for magnetic field";
289  } else {
290  // CMS magnetic field volume
291  double rmax = 9000*mm;
292  double zmax = 24000*mm;
293 
294  double dr = 5*cm;
295  double dz = 20*cm;
296 
297  int nr = (int)(rmax/dr);
298  int nz = 2*(int)(zmax/dz);
299 
300  double r = 0.0;
301  double z0 = -zmax;
302  double z;
303 
304  double phi = 0.0;
305  double cosf = cos(phi);
306  double sinf = sin(phi);
307 
308  double point[4] = {0.0,0.0,0.0,0.0};
309  double bfield[3] = {0.0,0.0,0.0};
310 
311  fout << std::setprecision(6);
312  for(int i=0; i<=nr; ++i) {
313  z = z0;
314  for(int j=0; j<=nz; ++j) {
315  point[0] = r*cosf;
316  point[1] = r*sinf;
317  point[2] = z;
318  field->GetFieldValue(point, bfield);
319  fout << "R(mm)= " << r/mm << " phi(deg)= " << phi/degree
320  << " Z(mm)= " << z/mm << " Bz(tesla)= " << bfield[2]/tesla
321  << " Br(tesla)= " << (bfield[0]*cosf + bfield[1]*sinf)/tesla
322  << " Bphi(tesla)= " << (bfield[0]*sinf - bfield[1]*cosf)/tesla
323  << G4endl;
324  z += dz;
325  }
326  r += dr;
327  }
328 
329  fout.close();
330  }
331 }
void Connect(RunAction *)
const std::string m_PhysicsTablesDir
Definition: RunManagerMT.h:110
bool m_managerInitialized
Definition: RunManagerMT.h:100
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
std::unique_ptr< DDG4ProductionCuts > m_prodCuts
Definition: RunManagerMT.h:122
void BeginOfRunAction(const G4Run *aRun) override
Definition: RunAction.cc:19
G4MTRunManagerKernel * m_kernel
Definition: RunManagerMT.h:96
SensitiveDetectorCatalog m_catalog
Definition: RunManagerMT.h:124
edm::ParameterSet m_pRunAction
Definition: RunManagerMT.h:116
def create(alignables, pedeDump, additionalData, outputFile, config)
HepPDT::ParticleDataTable ParticleDataTable
void initG4(const DDCompactView *pDD, const MagneticField *pMF, const HepPDT::ParticleDataTable *fPDGTable)
void terminateRun()
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
SimActivityRegistry::EndOfRunSignal m_endOfRunSignal
Definition: RunAction.h:25
bool m_runTerminated
Definition: RunManagerMT.h:101
G4StateManager * m_stateManager
Definition: RunManagerMT.h:105
BeginOfRunSignal beginOfRunSignal_
std::vector< std::string > m_G4Commands
Definition: RunManagerMT.h:118
std::unique_ptr< CustomUIsession > m_UIsession
Definition: RunManagerMT.h:98
G4Run * m_currentRun
Definition: RunManagerMT.h:104
std::string m_RegionFile
Definition: RunManagerMT.h:128
void initializeUserActions()
Compact representation of the geometrical detector hierarchy.
Definition: DDCompactView.h:80
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:127
RunManagerMT(edm::ParameterSet const &p)
Definition: RunManagerMT.cc:65
RunAction
list of unwanted particles (gluons and quarks)
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
bool m_pUseMagneticField
Definition: RunManagerMT.h:102
edm::ParameterSet m_p
Definition: RunManagerMT.h:119
EndOfRunSignal endOfRunSignal_
void DumpMagneticField(const G4Field *) const
void connect(Observer< const T * > *iObs)
does not take ownership of memory
Definition: Signaler.h:55
void build(CMSFieldManager *fM, G4PropagatorInField *fP)
Definition: FieldBuilder.cc:31
bool m_StorePhysicsTables
Definition: RunManagerMT.h:111
G4GeometryManager * m_geometryManager
Definition: RunManagerMT.h:106
std::unique_ptr< PhysicsList > m_physicsList
Definition: RunManagerMT.h:99
std::string m_FieldFile
Definition: RunManagerMT.h:126
edm::ParameterSet m_pField
Definition: RunManagerMT.h:114
std::unique_ptr< DDDWorld > m_world
Definition: RunManagerMT.h:121
edm::ParameterSet m_pPhysics
Definition: RunManagerMT.h:115
DDDWorldSignal dddWorldSignal_
bool m_RestorePhysicsTables
Definition: RunManagerMT.h:112
void ReportRegions(const std::string &ss)
list command
Definition: mps_check.py:25
SimActivityRegistry::BeginOfRunSignal m_beginOfRunSignal
Definition: RunAction.h:24
HLT enums.
list cmd
Definition: mps_setup.py:240
std::unique_ptr< SimRunInterface > m_runInterface
Definition: RunManagerMT.h:108
edm::ParameterSet m_g4overlap
Definition: RunManagerMT.h:117
dbl *** dir
Definition: mlp_gen.cc:35
RunAction * m_userRunAction
Definition: RunManagerMT.h:103
def check(config)
Definition: trackerTree.py:14
T get(const Candidate &c)
Definition: component.h:55
void EndOfRunAction(const G4Run *aRun) override
Definition: RunAction.cc:31
*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