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 
165  m_physicsList->ResetStoredInAscii();
167  m_physicsList->SetPhysicsTableRetrieved(m_PhysicsTablesDir);
168  }
169  edm::LogVerbatim("SimG4CoreApplication")
170  << "RunManagerMT: start initialisation of PhysicsList for master";
171 
172  int verb = std::max(m_pPhysics.getUntrackedParameter<int>("Verbosity",0),
173  m_p.getParameter<int>("SteppingVerbosity"));
174  m_kernel->SetVerboseLevel(verb);
175 
176  m_physicsList->SetDefaultCutValue(m_pPhysics.getParameter<double>("DefaultCutValue")*CLHEP::cm);
177  m_physicsList->SetCutsWithDefault();
178 
179  if(m_pPhysics.getParameter<bool>("CutsPerRegion")) {
180  m_prodCuts.reset(new DDG4ProductionCuts(map_lv, verb, m_pPhysics));
181  m_prodCuts->update();
182  }
183 
184  m_kernel->SetPhysics(phys);
185 
186  // Geant4 UI commands before initialisation of physics
187  if(!m_G4Commands.empty()) {
188  G4cout << "RunManagerMT: Requested UI commands: " << G4endl;
189  for (const std::string& command : m_G4Commands) {
190  G4cout << " " << command << G4endl;
191  G4UImanager::GetUIpointer()->ApplyCommand(command);
192  }
193  }
194 
195  m_stateManager->SetNewState(G4State_Init);
196  m_kernel->InitializePhysics();
197  m_kernel->SetUpDecayChannels();
198 
199  // The following line was with the following comment in
200  // G4MTRunManager::InitializePhysics() in 10.00.p01; in practice
201  // needed to initialize certain singletons during the master thread
202  // initialization in order to avoid races later...
203  //
204  //BERTINI, this is needed to create pseudo-particles, to be removed
205  G4CascadeInterface::Initialize();
206 
207  if (m_kernel->RunInitialization()) { m_managerInitialized = true; }
208  else {
210  "G4RunManagerKernel initialization failed!");
211  }
212 
213  if (m_StorePhysicsTables) {
214  std::ostringstream dir;
215  dir << m_PhysicsTablesDir << '\0';
216  std::string cmd = std::string("/control/shell mkdir -p ")+m_PhysicsTablesDir;
217  if (!std::ifstream(dir.str().c_str(), std::ios::in))
218  G4UImanager::GetUIpointer()->ApplyCommand(cmd);
219  m_physicsList->StorePhysicsTable(m_PhysicsTablesDir);
220  }
221 
223 
224  if(verb > 1) { m_physicsList->DumpCutValuesTable(); }
225 
226  // geometry dump
227  if(!m_WriteFile.empty()) {
228  G4GDMLParser gdml;
229  gdml.SetRegionExport(true);
230  gdml.SetEnergyCutsExport(true);
231  gdml.Write(m_WriteFile, m_world->GetWorldVolume(), true);
232  }
233 
234  // G4Region dump
235  if(!m_RegionFile.empty()) {
236  G4RegionReporter rrep;
238  }
239 
240  // Intersection check
242 
243  // If the Geant4 particle table is needed, decomment the lines below
244  //
245  //G4ParticleTable::GetParticleTable()->DumpTable("ALL");
246  //
247  m_stateManager->SetNewState(G4State_GeomClosed);
248  m_currentRun = new G4Run();
250 }
251 
253  m_runInterface.reset(new SimRunInterface(this, true));
256 }
257 
259 {
262 }
263 
265 {
266  m_geometryManager->OpenGeometry();
267  m_stateManager->SetNewState(G4State_Quit);
268  if(!m_runTerminated) { terminateRun(); }
269 }
270 
272  if(m_userRunAction) {
274  delete m_userRunAction;
275  m_userRunAction = nullptr;
276  }
277  if(m_kernel && !m_runTerminated) {
278  m_kernel->RunTermination();
279  }
280  m_runTerminated = true;
281 }
282 
283 void RunManagerMT::DumpMagneticField(const G4Field* field) const
284 {
285  std::ofstream fout(m_FieldFile.c_str(), std::ios::out);
286  if(fout.fail()){
287  edm::LogWarning("SimG4CoreApplication")
288  << " RunManager WARNING : "
289  << "error opening file <" << m_FieldFile << "> for magnetic field";
290  } else {
291  // CMS magnetic field volume
292  double rmax = 9000*mm;
293  double zmax = 24000*mm;
294 
295  double dr = 5*cm;
296  double dz = 20*cm;
297 
298  int nr = (int)(rmax/dr);
299  int nz = 2*(int)(zmax/dz);
300 
301  double r = 0.0;
302  double z0 = -zmax;
303  double z;
304 
305  double phi = 0.0;
306  double cosf = cos(phi);
307  double sinf = sin(phi);
308 
309  double point[4] = {0.0,0.0,0.0,0.0};
310  double bfield[3] = {0.0,0.0,0.0};
311 
312  fout << std::setprecision(6);
313  for(int i=0; i<=nr; ++i) {
314  z = z0;
315  for(int j=0; j<=nz; ++j) {
316  point[0] = r*cosf;
317  point[1] = r*sinf;
318  point[2] = z;
319  field->GetFieldValue(point, bfield);
320  fout << "R(mm)= " << r/mm << " phi(deg)= " << phi/degree
321  << " Z(mm)= " << z/mm << " Bz(tesla)= " << bfield[2]/tesla
322  << " Br(tesla)= " << (bfield[0]*cosf + bfield[1]*sinf)/tesla
323  << " Bphi(tesla)= " << (bfield[0]*sinf - bfield[1]*cosf)/tesla
324  << G4endl;
325  z += dz;
326  }
327  r += dr;
328  }
329 
330  fout.close();
331  }
332 }
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:59
void build(CMSFieldManager *fM, G4PropagatorInField *fP)
Definition: FieldBuilder.cc:34
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:24
SimActivityRegistry::BeginOfRunSignal m_beginOfRunSignal
Definition: RunAction.h:24
HLT enums.
list cmd
Definition: mps_setup.py:239
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