CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 "G4StateManager.hh"
34 #include "G4ApplicationState.hh"
35 #include "G4MTRunManagerKernel.hh"
36 #include "G4UImanager.hh"
37 
38 #include "G4EventManager.hh"
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 
65 
67  : m_managerInitialized(false),
68  m_runTerminated(false),
69  m_PhysicsTablesDir(p.getUntrackedParameter<std::string>("PhysicsTablesDirectory", "")),
70  m_StorePhysicsTables(p.getUntrackedParameter<bool>("StorePhysicsTables", false)),
71  m_RestorePhysicsTables(p.getUntrackedParameter<bool>("RestorePhysicsTables", false)),
72  m_pPhysics(p.getParameter<edm::ParameterSet>("Physics")),
73  m_pRunAction(p.getParameter<edm::ParameterSet>("RunAction")),
74  m_g4overlap(p.getUntrackedParameter<edm::ParameterSet>("G4CheckOverlap")),
75  m_G4Commands(p.getParameter<std::vector<std::string> >("G4Commands")),
76  m_p(p) {
77  m_currentRun = nullptr;
79  m_physicsList.reset(nullptr);
80  m_world.reset(nullptr);
81 
82  m_runInterface.reset(nullptr);
83  m_userRunAction = nullptr;
84  m_currentRun = nullptr;
85 
86  m_kernel = new G4MTRunManagerKernel();
87  m_stateManager = G4StateManager::GetStateManager();
88  double th = p.getParameter<double>("ThresholdForGeometryExceptions") * CLHEP::GeV;
89  m_stateManager->SetExceptionHandler(new ExceptionHandler(th));
90  m_check = p.getUntrackedParameter<bool>("CheckGeometry", false);
91 }
92 
94 
96  const cms::DDCompactView* pDD4hep,
97  const HepPDT::ParticleDataTable* fPDGTable) {
99  edm::LogWarning("SimG4CoreApplication") << "RunManagerMT::initG4 was already done - exit";
100  return;
101  }
102  bool geoFromDD4hep = m_p.getParameter<bool>("g4GeometryDD4hepSource");
103  bool cuts = m_pPhysics.getParameter<bool>("CutsPerRegion");
104  bool protonCut = m_pPhysics.getParameter<bool>("CutsOnProton");
105  int verb = m_pPhysics.getUntrackedParameter<int>("Verbosity", 0);
106  int stepverb = m_p.getUntrackedParameter<int>("SteppingVerbosity", 0);
107  edm::LogVerbatim("SimG4CoreApplication")
108  << "RunManagerMT: start initialising of geometry DD4hep: " << geoFromDD4hep << "\n"
109  << " cutsPerRegion: " << cuts << " cutForProton: " << protonCut << "\n"
110  << " G4 verbosity: " << verb;
111 
112  G4Timer timer;
113  timer.Start();
114 
115  G4UImanager::GetUIpointer()->SetCoutDestination(m_UIsession);
116  G4UImanager::GetUIpointer()->SetMasterUIManager(true);
117 
118  m_world = std::make_unique<DDDWorld>(pDD, pDD4hep, m_catalog, verb, cuts, protonCut);
119  G4VPhysicalVolume* world = m_world.get()->GetWorldVolume();
120 
121  m_kernel->SetVerboseLevel(verb);
122  edm::LogVerbatim("SimG4CoreApplication")
123  << "RunManagerMT: Define cuts: " << cuts << " Geant4 run manager verbosity: " << verb;
124 
125  const G4RegionStore* regStore = G4RegionStore::GetInstance();
126  const G4PhysicalVolumeStore* pvs = G4PhysicalVolumeStore::GetInstance();
127  const G4LogicalVolumeStore* lvs = G4LogicalVolumeStore::GetInstance();
128  unsigned int numPV = pvs->size();
129  unsigned int numLV = lvs->size();
130  unsigned int nn = regStore->size();
131  edm::LogVerbatim("SimG4CoreApplication")
132  << "RunManagerMT: " << numPV << " physical volumes; " << numLV << " logical volumes; " << nn << " regions.";
133 
134  if (m_check) {
135  m_kernel->SetVerboseLevel(2);
136  }
137  m_kernel->DefineWorldVolume(world, true);
139  m_stateManager->SetNewState(G4State_PreInit);
140 
141  // Create physics list
142  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMT: create PhysicsList";
143 
144  std::unique_ptr<PhysicsListMakerBase> physicsMaker(
146  if (physicsMaker.get() == nullptr) {
147  throw edm::Exception(edm::errors::Configuration) << "Unable to find the Physics list requested";
148  }
149  m_physicsList = physicsMaker->make(m_pPhysics, m_registry);
150 
151  PhysicsList* phys = m_physicsList.get();
152  if (phys == nullptr) {
153  throw edm::Exception(edm::errors::Configuration, "Physics list construction failed!");
154  }
155  if (stepverb > 0) {
156  verb = std::max(verb, 1);
157  }
158  G4HadronicParameters::Instance()->SetVerboseLevel(verb);
159  G4EmParameters::Instance()->SetVerbose(verb);
160  G4EmParameters::Instance()->SetWorkerVerbose(std::max(verb - 1, 0));
161 
162  // exotic particle physics
163  double monopoleMass = m_pPhysics.getUntrackedParameter<double>("MonopoleMass", 0);
164  if (monopoleMass > 0.0) {
165  phys->RegisterPhysics(new CMSMonopolePhysics(fPDGTable, m_pPhysics));
166  }
167  bool exotica = m_pPhysics.getUntrackedParameter<bool>("ExoticaTransport", false);
168  if (exotica) {
169  CMSExoticaPhysics exo(phys, m_pPhysics);
170  }
171 
172  // adding GFlash, Russian Roulette for eletrons and gamma,
173  // step limiters on top of any Physics Lists
174  phys->RegisterPhysics(new ParametrisedEMPhysics("EMoptions", m_pPhysics));
175 
177  m_physicsList->SetPhysicsTableRetrieved(m_PhysicsTablesDir);
178  }
179  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMT: start initialisation of PhysicsList for master";
180 
181  m_physicsList->SetDefaultCutValue(m_pPhysics.getParameter<double>("DefaultCutValue") * CLHEP::cm);
182  m_physicsList->SetCutsWithDefault();
183  m_kernel->SetPhysics(phys);
184 
185  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMT: PhysicsList and cuts are defined";
186 
187  // Geant4 UI commands before initialisation of physics
188  if (!m_G4Commands.empty()) {
189  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMT: Requested UI commands: ";
190  for (const std::string& command : m_G4Commands) {
191  edm::LogVerbatim("SimG4CoreApplication") << " " << command;
192  G4UImanager::GetUIpointer()->ApplyCommand(command);
193  }
194  }
195 
196  m_stateManager->SetNewState(G4State_Init);
197  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMT: G4State is Init";
198  m_kernel->InitializePhysics();
199  m_kernel->SetUpDecayChannels();
200 
201  if (m_kernel->RunInitialization()) {
202  m_managerInitialized = true;
203  } else {
204  throw edm::Exception(edm::errors::LogicError, "G4RunManagerKernel initialization failed!");
205  }
206 
207  if (m_StorePhysicsTables) {
208  std::ostringstream dir;
209  dir << m_PhysicsTablesDir << '\0';
210  std::string cmd = std::string("/control/shell mkdir -p ") + m_PhysicsTablesDir;
211  if (!std::ifstream(dir.str().c_str(), std::ios::in))
212  G4UImanager::GetUIpointer()->ApplyCommand(cmd);
213  m_physicsList->StorePhysicsTable(m_PhysicsTablesDir);
214  }
215  // Appload nuclear level data up to Z=84
216  G4NuclearLevelData::GetInstance()->UploadNuclearLevelData(84);
217 
218  if (verb > 1) {
219  m_physicsList->DumpCutValuesTable();
220  }
221  edm::LogVerbatim("SimG4CoreApplication")
222  << "RunManagerMT: Physics is initilized, now initialise user actions, verb=" << verb;
223 
225 
226  // geometry dump
227  auto writeFile = m_p.getUntrackedParameter<std::string>("FileNameGDML", "");
228  if (!writeFile.empty()) {
229  G4GDMLParser gdml;
230  gdml.SetRegionExport(true);
231  gdml.SetEnergyCutsExport(true);
232  gdml.Write(writeFile, m_world->GetWorldVolume(), true);
233  }
234 
235  // G4Region dump file name
236  auto regionFile = m_p.getUntrackedParameter<std::string>("FileNameRegions", "");
237 
238  // Geometry checks
239  if (m_check || !regionFile.empty()) {
240  CMSG4CheckOverlap check(m_g4overlap, regionFile, m_UIsession, world);
241  }
242 
243  m_stateManager->SetNewState(G4State_PreInit);
244  G4HadronicParameters::Instance()->SetVerboseLevel(std::max(verb - 1, 0));
245 
246  // If the Geant4 particle table is needed, decomment the lines below
247  //
248  //G4ParticleTable::GetParticleTable()->DumpTable("ALL");
249  //
250  m_stateManager->SetNewState(G4State_GeomClosed);
251  m_currentRun = new G4Run();
253  timer.Stop();
254  G4cout.precision(4);
255  G4cout << "RunManagerMT: initG4 done " << timer << G4endl;
256 }
257 
259  m_runInterface = std::make_unique<SimRunInterface>(this, true);
261  Connect(m_userRunAction);
262 }
263 
267 }
268 
270  G4GeometryManager::GetInstance()->OpenGeometry();
271  m_stateManager->SetNewState(G4State_Quit);
272  if (!m_runTerminated) {
273  terminateRun();
274  }
275 }
276 
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 }
void Connect(RunAction *)
const std::string m_PhysicsTablesDir
Definition: RunManagerMT.h:95
Log< level::Info, true > LogVerbatim
bool m_managerInitialized
Definition: RunManagerMT.h:87
T getUntrackedParameter(std::string const &, T const &) const
void BeginOfRunAction(const G4Run *aRun) override
Definition: RunAction.cc:17
const double GeV
Definition: MathUtil.h:16
G4MTRunManagerKernel * m_kernel
Definition: RunManagerMT.h:83
SensitiveDetectorCatalog m_catalog
Definition: RunManagerMT.h:107
edm::ParameterSet m_pRunAction
Definition: RunManagerMT.h:100
HepPDT::ParticleDataTable ParticleDataTable
void terminateRun()
RunManagerMT(edm::ParameterSet const &)
Definition: RunManagerMT.cc:66
SimActivityRegistry::EndOfRunSignal m_endOfRunSignal
Definition: RunAction.h:24
bool m_runTerminated
Definition: RunManagerMT.h:88
G4StateManager * m_stateManager
Definition: RunManagerMT.h:91
BeginOfRunSignal beginOfRunSignal_
std::vector< std::string > m_G4Commands
Definition: RunManagerMT.h:102
G4Run * m_currentRun
Definition: RunManagerMT.h:90
void initializeUserActions()
Compact representation of the geometrical detector hierarchy.
Definition: DDCompactView.h:81
const DDDWorld & world() const
Definition: RunManagerMT.h:70
TkSoA const *__restrict__ CAHitNtupletGeneratorKernelsGPU::QualityCuts cuts
edm::ParameterSet m_p
Definition: RunManagerMT.h:103
EndOfRunSignal endOfRunSignal_
void connect(Observer< const T * > *iObs)
does not take ownership of memory
Definition: Signaler.h:55
bool m_StorePhysicsTables
Definition: RunManagerMT.h:96
std::unique_ptr< PhysicsList > m_physicsList
Definition: RunManagerMT.h:86
std::unique_ptr< DDDWorld > m_world
Definition: RunManagerMT.h:105
edm::ParameterSet m_pPhysics
Definition: RunManagerMT.h:99
DDDWorldSignal dddWorldSignal_
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
bool m_RestorePhysicsTables
Definition: RunManagerMT.h:97
void initG4(const DDCompactView *, const cms::DDCompactView *, const HepPDT::ParticleDataTable *)
Definition: RunManagerMT.cc:95
list command
Definition: mps_check.py:25
SimActivityRegistry::BeginOfRunSignal m_beginOfRunSignal
Definition: RunAction.h:23
list cmd
Definition: mps_setup.py:244
int32_t *__restrict__ nn
std::unique_ptr< SimRunInterface > m_runInterface
Definition: RunManagerMT.h:93
edm::ParameterSet m_g4overlap
Definition: RunManagerMT.h:101
#define get
Log< level::Warning, false > LogWarning
RunAction * m_userRunAction
Definition: RunManagerMT.h:89
CustomUIsession * m_UIsession
Definition: RunManagerMT.h:85
void EndOfRunAction(const G4Run *aRun) override
Definition: RunAction.cc:26
SimActivityRegistry m_registry
Definition: RunManagerMT.h:106