CMS 3D CMS Logo

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