CMS 3D CMS Logo

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