CMS 3D CMS Logo

RunManagerMT.cc
Go to the documentation of this file.
7 
10 
14 
16 
23 
26 
28 
29 #include "HepPDT/ParticleDataTable.hh"
30 
31 #include "G4Timer.hh"
32 #include "G4GeometryManager.hh"
33 #include "G4ScoringManager.hh"
34 #include "G4StateManager.hh"
35 #include "G4ApplicationState.hh"
36 #include "G4MTRunManagerKernel.hh"
37 #include "G4UImanager.hh"
38 
39 #include "G4Run.hh"
40 #include "G4Event.hh"
41 #include "G4TransportationManager.hh"
42 #include "G4ParticleTable.hh"
43 #include "G4CascadeInterface.hh"
44 #include "G4EmParameters.hh"
45 #include "G4HadronicParameters.hh"
46 #include "G4NuclearLevelData.hh"
47 
48 #include "G4GDMLParser.hh"
49 #include "G4SystemOfUnits.hh"
50 
51 #include "G4LogicalVolume.hh"
52 #include "G4LogicalVolumeStore.hh"
53 #include "G4PhysicalVolumeStore.hh"
54 #include "G4Region.hh"
55 #include "G4RegionStore.hh"
56 
57 #include <iostream>
58 #include <memory>
59 
60 #include <sstream>
61 #include <fstream>
62 #include <memory>
63 
66 
68  : m_managerInitialized(false),
69  m_runTerminated(false),
70  m_PhysicsTablesDir(p.getUntrackedParameter<std::string>("PhysicsTablesDirectory", "")),
71  m_StorePhysicsTables(p.getUntrackedParameter<bool>("StorePhysicsTables", false)),
72  m_RestorePhysicsTables(p.getUntrackedParameter<bool>("RestorePhysicsTables", false)),
73  m_pPhysics(p.getParameter<edm::ParameterSet>("Physics")),
74  m_pRunAction(p.getParameter<edm::ParameterSet>("RunAction")),
75  m_g4overlap(p.getUntrackedParameter<edm::ParameterSet>("G4CheckOverlap")),
76  m_G4Commands(p.getParameter<std::vector<std::string> >("G4Commands")),
77  m_p(p) {
78  m_currentRun = nullptr;
80  m_physicsList.reset(nullptr);
81  m_world.reset(nullptr);
82 
83  m_runInterface.reset(nullptr);
84  m_userRunAction = nullptr;
85  m_currentRun = nullptr;
86 
87  m_kernel = new G4MTRunManagerKernel();
88  m_stateManager = G4StateManager::GetStateManager();
89  double th = p.getParameter<double>("ThresholdForGeometryExceptions") * CLHEP::GeV;
90  bool tr = p.getParameter<bool>("TraceExceptions");
91  m_stateManager->SetExceptionHandler(new ExceptionHandler(th, tr));
92  m_check = p.getUntrackedParameter<bool>("CheckGeometry", false);
93 }
94 
96 
98  const cms::DDCompactView* pDD4hep,
99  const HepPDT::ParticleDataTable* fPDGTable) {
100  if (m_managerInitialized) {
101  edm::LogWarning("SimG4CoreApplication") << "RunManagerMT::initG4 was already done - exit";
102  return;
103  }
104  bool geoFromDD4hep = m_p.getParameter<bool>("g4GeometryDD4hepSource");
105  bool cuts = m_pPhysics.getParameter<bool>("CutsPerRegion");
106  bool protonCut = m_pPhysics.getParameter<bool>("CutsOnProton");
107  int verb = m_pPhysics.getUntrackedParameter<int>("Verbosity", 0);
108  int stepverb = m_p.getUntrackedParameter<int>("SteppingVerbosity", 0);
109  edm::LogVerbatim("SimG4CoreApplication")
110  << "RunManagerMT: start initialising of geometry DD4hep: " << geoFromDD4hep << "\n"
111  << " cutsPerRegion: " << cuts << " cutForProton: " << protonCut << "\n"
112  << " G4 verbosity: " << verb;
113 
114  G4Timer timer;
115  timer.Start();
116 
117  G4UImanager::GetUIpointer()->SetCoutDestination(m_UIsession);
118  G4UImanager::GetUIpointer()->SetMasterUIManager(true);
119 
120  m_world = std::make_unique<DDDWorld>(pDD, pDD4hep, m_catalog, verb, cuts, protonCut);
121  G4VPhysicalVolume* world = m_world.get()->GetWorldVolume();
122 
123  m_kernel->SetVerboseLevel(verb);
124  edm::LogVerbatim("SimG4CoreApplication")
125  << "RunManagerMT: Define cuts: " << cuts << " Geant4 run manager verbosity: " << verb;
126 
127  const G4RegionStore* regStore = G4RegionStore::GetInstance();
128  const G4PhysicalVolumeStore* pvs = G4PhysicalVolumeStore::GetInstance();
129  const G4LogicalVolumeStore* lvs = G4LogicalVolumeStore::GetInstance();
130  unsigned int numPV = pvs->size();
131  unsigned int numLV = lvs->size();
132  unsigned int nn = regStore->size();
133  edm::LogVerbatim("SimG4CoreApplication")
134  << "RunManagerMT: " << numPV << " physical volumes; " << numLV << " logical volumes; " << nn << " regions.";
135 
136  if (m_check) {
137  m_kernel->SetVerboseLevel(2);
138  }
139  m_kernel->DefineWorldVolume(world, true);
141  m_stateManager->SetNewState(G4State_PreInit);
142 
143  // Create physics list
144  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMT: create PhysicsList";
145 
146  std::unique_ptr<PhysicsListMakerBase> physicsMaker(
148  if (physicsMaker.get() == nullptr) {
149  throw cms::Exception("Configuration") << "Unable to find the Physics list requested";
150  }
151  m_physicsList = physicsMaker->make(m_pPhysics, m_registry);
152 
153  PhysicsList* phys = m_physicsList.get();
154  if (phys == nullptr) {
155  throw cms::Exception("Configuration") << "Physics list construction failed!";
156  }
157  if (stepverb > 0) {
158  verb = std::max(verb, 1);
159  }
160  G4HadronicParameters::Instance()->SetVerboseLevel(verb);
161  G4EmParameters::Instance()->SetVerbose(verb);
162  G4EmParameters::Instance()->SetWorkerVerbose(std::max(verb - 1, 0));
163 
164  // exotic particle physics
165  double monopoleMass = m_pPhysics.getUntrackedParameter<double>("MonopoleMass", 0);
166  if (monopoleMass > 0.0) {
167  phys->RegisterPhysics(new CMSMonopolePhysics(fPDGTable, m_pPhysics));
168  }
169  bool exotica = m_pPhysics.getUntrackedParameter<bool>("ExoticaTransport", false);
170  if (exotica) {
171  CMSExoticaPhysics exo(phys, m_pPhysics);
172  }
173 
174  // adding GFlash, Russian Roulette for eletrons and gamma,
175  // step limiters on top of any Physics Lists
176  phys->RegisterPhysics(new ParametrisedEMPhysics("EMoptions", m_pPhysics));
177 
179  m_physicsList->SetPhysicsTableRetrieved(m_PhysicsTablesDir);
180  }
181  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMT: start initialisation of PhysicsList for master";
182 
183  m_physicsList->SetDefaultCutValue(m_pPhysics.getParameter<double>("DefaultCutValue") * CLHEP::cm);
184  m_physicsList->SetCutsWithDefault();
185  m_kernel->SetPhysics(phys);
186 
187  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMT: PhysicsList and cuts are defined";
188 
189  // Enable couple transportation
190  bool scorer = m_p.getParameter<bool>("UseCommandBaseScorer");
191  if (scorer) {
192  G4ScoringManager* scManager = G4ScoringManager::GetScoringManager();
193  scManager->SetVerboseLevel(1);
194  }
195  // Geant4 UI commands before initialisation of physics
196  if (!m_G4Commands.empty()) {
197  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMT: Requested UI commands: ";
198  for (const std::string& command : m_G4Commands) {
199  edm::LogVerbatim("SimG4CoreApplication") << " " << command;
200  G4UImanager::GetUIpointer()->ApplyCommand(command);
201  }
202  }
203 
204  setupVoxels();
205 
206  m_stateManager->SetNewState(G4State_Init);
207  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMT: G4State is Init";
208  m_kernel->InitializePhysics();
209  if (verb > 0) {
210  G4EmParameters::Instance()->Dump();
211  }
212  m_kernel->SetUpDecayChannels();
213 
214  if (m_kernel->RunInitialization()) {
215  m_managerInitialized = true;
216  } else {
217  throw cms::Exception("LogicError") << "G4RunManagerKernel initialization failed!";
218  }
219 
220  if (m_check) {
221  checkVoxels();
222  }
223 
224  if (m_StorePhysicsTables) {
225  std::ostringstream dir;
226  dir << m_PhysicsTablesDir << '\0';
227  std::string cmd = std::string("/control/shell mkdir -p ") + m_PhysicsTablesDir;
228  if (!std::ifstream(dir.str().c_str(), std::ios::in))
229  G4UImanager::GetUIpointer()->ApplyCommand(cmd);
230  m_physicsList->StorePhysicsTable(m_PhysicsTablesDir);
231  }
232  // Appload nuclear level data up to Z=84
233  G4NuclearLevelData::GetInstance()->UploadNuclearLevelData(84);
234 
235  if (verb > 1) {
236  m_physicsList->DumpCutValuesTable();
237  }
238  edm::LogVerbatim("SimG4CoreApplication")
239  << "RunManagerMT: Physics is initilized, now initialise user actions, verb=" << verb;
240 
242 
243  // geometry dump
244  auto writeFile = m_p.getUntrackedParameter<std::string>("FileNameGDML", "");
245  if (!writeFile.empty()) {
246  G4GDMLParser gdml;
247  gdml.SetRegionExport(true);
248  gdml.SetEnergyCutsExport(true);
249  gdml.Write(writeFile, m_world->GetWorldVolume(), true);
250  }
251 
252  // G4Region dump file name
253  auto regionFile = m_p.getUntrackedParameter<std::string>("FileNameRegions", "");
254 
255  // Geometry checks
256  if (m_check || !regionFile.empty()) {
258  }
259 
260  m_stateManager->SetNewState(G4State_PreInit);
261  G4HadronicParameters::Instance()->SetVerboseLevel(std::max(verb - 1, 0));
262 
263  // If the Geant4 particle table is needed, decomment the lines below
264  //
265  //G4ParticleTable::GetParticleTable()->DumpTable("ALL");
266  //
267  m_stateManager->SetNewState(G4State_GeomClosed);
268  m_currentRun = new G4Run();
270  timer.Stop();
271  G4cout.precision(4);
272  G4cout << "RunManagerMT: initG4 done " << timer << G4endl;
273 }
274 
276  m_runInterface = std::make_unique<SimRunInterface>(this, true);
279 }
280 
284 }
285 
287  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMT::stopG4";
288  G4GeometryManager::GetInstance()->OpenGeometry();
289  m_stateManager->SetNewState(G4State_Quit);
290  if (!m_runTerminated) {
291  terminateRun();
292  }
293  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMT::stopG4 done";
294 }
295 
297  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMT::terminateRun";
298  if (nullptr != m_userRunAction) {
300  delete m_userRunAction;
301  m_userRunAction = nullptr;
302  }
303  if (!m_runTerminated) {
304  m_kernel->RunTermination();
305  }
306  m_runTerminated = true;
307  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMT::terminateRun done";
308 }
309 
311  const G4LogicalVolumeStore* lvs = G4LogicalVolumeStore::GetInstance();
312  int numLV = lvs->size();
313  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMT: nLV=" << numLV;
314  int nvox = 0;
315  int nslice = 0;
316  for (int i = 0; i < numLV; ++i) {
317  auto lv = (*lvs)[i];
318  auto nd = lv->GetNoDaughters();
319  auto vox = lv->GetVoxelHeader();
320  auto sma = lv->GetSmartless();
321  auto reg = lv->GetRegion();
322  size_t nsl = (nullptr == vox) ? 0 : vox->GetNoSlices();
323  if (0 < nsl) {
324  nslice += nsl;
325  std::string rname = (nullptr != reg) ? reg->GetName() : "";
326  edm::LogVerbatim("Voxels") << " " << i << ". Nd=" << nd << " Nsl=" << nsl << " Smartless=" << sma << " "
327  << lv->GetName() << " Region: " << rname;
328  ++nvox;
329  }
330  }
331  edm::LogVerbatim("SimG4CoreApplication")
332  << "RunManagerMT: nLV=" << numLV << " NlvVox=" << nvox << " Nslices=" << nslice;
333 }
334 
336  double density = m_p.getParameter<double>("DefaultVoxelDensity");
337  std::vector<std::string> rnames = m_p.getParameter<std::vector<std::string> >("VoxelRegions");
338  std::vector<double> rdensities = m_p.getParameter<std::vector<double> >("VoxelDensityPerRegion");
339  int nr = 0;
340  std::size_t n = rnames.size();
341  if (n == rdensities.size()) {
342  nr = (int)n;
343  }
344  const G4LogicalVolumeStore* lvs = G4LogicalVolumeStore::GetInstance();
345  for (auto& lv : *lvs) {
346  double den = density;
347  if (0 < nr) {
348  std::string nam = lv->GetRegion()->GetName();
349  for (int i = 0; i < nr; ++i) {
350  if (nam == rnames[i]) {
351  den = rdensities[i];
352  break;
353  }
354  }
355  }
356  lv->SetSmartless(den);
357  }
358  edm::LogVerbatim("SimG4CoreApplication")
359  << "RunManagerMT: default voxel density=" << density << "; number of regions with special density " << nr;
360 }
void Connect(RunAction *)
void connect(Observer< const T *> *iObs)
does not take ownership of memory
Definition: Signaler.h:55
const std::string m_PhysicsTablesDir
Definition: RunManagerMT.h:99
Log< level::Info, true > LogVerbatim
bool m_managerInitialized
Definition: RunManagerMT.h:91
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
void BeginOfRunAction(const G4Run *aRun) override
Definition: RunAction.cc:17
G4MTRunManagerKernel * m_kernel
Definition: RunManagerMT.h:87
SensitiveDetectorCatalog m_catalog
Definition: RunManagerMT.h:111
edm::ParameterSet m_pRunAction
Definition: RunManagerMT.h:104
def create(alignables, pedeDump, additionalData, outputFile, config)
HepPDT::ParticleDataTable ParticleDataTable
void terminateRun()
RunManagerMT(edm::ParameterSet const &)
Definition: RunManagerMT.cc:67
void setupVoxels()
SimActivityRegistry::EndOfRunSignal m_endOfRunSignal
Definition: RunAction.h:24
bool m_runTerminated
Definition: RunManagerMT.h:92
G4StateManager * m_stateManager
Definition: RunManagerMT.h:95
BeginOfRunSignal beginOfRunSignal_
std::vector< std::string > m_G4Commands
Definition: RunManagerMT.h:106
G4Run * m_currentRun
Definition: RunManagerMT.h:94
void initializeUserActions()
Compact representation of the geometrical detector hierarchy.
Definition: DDCompactView.h:81
void checkVoxels()
T getUntrackedParameter(std::string const &, T const &) const
RunAction
list of unwanted particles (gluons and quarks)
edm::ParameterSet m_p
Definition: RunManagerMT.h:107
EndOfRunSignal endOfRunSignal_
bool m_StorePhysicsTables
Definition: RunManagerMT.h:100
const DDDWorld & world() const
Definition: RunManagerMT.h:70
std::unique_ptr< PhysicsList > m_physicsList
Definition: RunManagerMT.h:90
std::unique_ptr< DDDWorld > m_world
Definition: RunManagerMT.h:109
edm::ParameterSet m_pPhysics
Definition: RunManagerMT.h:103
DDDWorldSignal dddWorldSignal_
bool m_RestorePhysicsTables
Definition: RunManagerMT.h:101
void initG4(const DDCompactView *, const cms::DDCompactView *, const HepPDT::ParticleDataTable *)
Definition: RunManagerMT.cc:97
list command
Definition: mps_check.py:25
SimActivityRegistry::BeginOfRunSignal m_beginOfRunSignal
Definition: RunAction.h:23
const G4String rname[NREG]
HLT enums.
list cmd
Definition: mps_setup.py:244
std::unique_ptr< SimRunInterface > m_runInterface
Definition: RunManagerMT.h:97
edm::ParameterSet m_g4overlap
Definition: RunManagerMT.h:105
#define get
Log< level::Warning, false > LogWarning
RunAction * m_userRunAction
Definition: RunManagerMT.h:93
CustomUIsession * m_UIsession
Definition: RunManagerMT.h:89
void EndOfRunAction(const G4Run *aRun) override
Definition: RunAction.cc:26
SimActivityRegistry m_registry
Definition: RunManagerMT.h:110