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 "G4EventManager.hh"
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 
49 #include "G4GDMLParser.hh"
50 #include "G4SystemOfUnits.hh"
51 
52 #include "G4LogicalVolume.hh"
53 #include "G4LogicalVolumeStore.hh"
54 #include "G4PhysicalVolumeStore.hh"
55 #include "G4Region.hh"
56 #include "G4RegionStore.hh"
57 
58 #include <iostream>
59 #include <memory>
60 
61 #include <sstream>
62 #include <fstream>
63 #include <memory>
64 
67 
69  : m_managerInitialized(false),
70  m_runTerminated(false),
71  m_PhysicsTablesDir(p.getUntrackedParameter<std::string>("PhysicsTablesDirectory", "")),
72  m_StorePhysicsTables(p.getUntrackedParameter<bool>("StorePhysicsTables", false)),
73  m_RestorePhysicsTables(p.getUntrackedParameter<bool>("RestorePhysicsTables", false)),
74  m_pPhysics(p.getParameter<edm::ParameterSet>("Physics")),
75  m_pRunAction(p.getParameter<edm::ParameterSet>("RunAction")),
76  m_g4overlap(p.getUntrackedParameter<edm::ParameterSet>("G4CheckOverlap")),
77  m_G4Commands(p.getParameter<std::vector<std::string> >("G4Commands")),
78  m_p(p) {
79  m_currentRun = nullptr;
81  m_physicsList.reset(nullptr);
82  m_world.reset(nullptr);
83 
84  m_runInterface.reset(nullptr);
85  m_userRunAction = nullptr;
86  m_currentRun = nullptr;
87 
88  m_kernel = new G4MTRunManagerKernel();
89  m_stateManager = G4StateManager::GetStateManager();
90  double th = p.getParameter<double>("ThresholdForGeometryExceptions") * CLHEP::GeV;
91  bool tr = p.getParameter<bool>("TraceExceptions");
92  m_stateManager->SetExceptionHandler(new ExceptionHandler(th, tr));
93  m_check = p.getUntrackedParameter<bool>("CheckGeometry", false);
94 }
95 
97 
99  const cms::DDCompactView* pDD4hep,
100  const HepPDT::ParticleDataTable* fPDGTable) {
101  if (m_managerInitialized) {
102  edm::LogWarning("SimG4CoreApplication") << "RunManagerMT::initG4 was already done - exit";
103  return;
104  }
105  bool geoFromDD4hep = m_p.getParameter<bool>("g4GeometryDD4hepSource");
106  bool cuts = m_pPhysics.getParameter<bool>("CutsPerRegion");
107  bool protonCut = m_pPhysics.getParameter<bool>("CutsOnProton");
108  int verb = m_pPhysics.getUntrackedParameter<int>("Verbosity", 0);
109  int stepverb = m_p.getUntrackedParameter<int>("SteppingVerbosity", 0);
110  edm::LogVerbatim("SimG4CoreApplication")
111  << "RunManagerMT: start initialising of geometry DD4hep: " << geoFromDD4hep << "\n"
112  << " cutsPerRegion: " << cuts << " cutForProton: " << protonCut << "\n"
113  << " G4 verbosity: " << verb;
114 
115  G4Timer timer;
116  timer.Start();
117 
118  G4UImanager::GetUIpointer()->SetCoutDestination(m_UIsession);
119  G4UImanager::GetUIpointer()->SetMasterUIManager(true);
120 
121  m_world = std::make_unique<DDDWorld>(pDD, pDD4hep, m_catalog, verb, cuts, protonCut);
122  G4VPhysicalVolume* world = m_world.get()->GetWorldVolume();
123 
124  m_kernel->SetVerboseLevel(verb);
125  edm::LogVerbatim("SimG4CoreApplication")
126  << "RunManagerMT: Define cuts: " << cuts << " Geant4 run manager verbosity: " << verb;
127 
128  const G4RegionStore* regStore = G4RegionStore::GetInstance();
129  const G4PhysicalVolumeStore* pvs = G4PhysicalVolumeStore::GetInstance();
130  const G4LogicalVolumeStore* lvs = G4LogicalVolumeStore::GetInstance();
131  unsigned int numPV = pvs->size();
132  unsigned int numLV = lvs->size();
133  unsigned int nn = regStore->size();
134  edm::LogVerbatim("SimG4CoreApplication")
135  << "RunManagerMT: " << numPV << " physical volumes; " << numLV << " logical volumes; " << nn << " regions.";
136 
137  if (m_check) {
138  m_kernel->SetVerboseLevel(2);
139  }
140  m_kernel->DefineWorldVolume(world, true);
142  m_stateManager->SetNewState(G4State_PreInit);
143 
144  // Create physics list
145  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMT: create PhysicsList";
146 
147  std::unique_ptr<PhysicsListMakerBase> physicsMaker(
149  if (physicsMaker.get() == nullptr) {
150  throw cms::Exception("Configuration") << "Unable to find the Physics list requested";
151  }
152  m_physicsList = physicsMaker->make(m_pPhysics, m_registry);
153 
154  PhysicsList* phys = m_physicsList.get();
155  if (phys == nullptr) {
156  throw cms::Exception("Configuration") << "Physics list construction failed!";
157  }
158  if (stepverb > 0) {
159  verb = std::max(verb, 1);
160  }
161  G4HadronicParameters::Instance()->SetVerboseLevel(verb);
162  G4EmParameters::Instance()->SetVerbose(verb);
163  G4EmParameters::Instance()->SetWorkerVerbose(std::max(verb - 1, 0));
164 
165  // exotic particle physics
166  double monopoleMass = m_pPhysics.getUntrackedParameter<double>("MonopoleMass", 0);
167  if (monopoleMass > 0.0) {
168  phys->RegisterPhysics(new CMSMonopolePhysics(fPDGTable, m_pPhysics));
169  }
170  bool exotica = m_pPhysics.getUntrackedParameter<bool>("ExoticaTransport", false);
171  if (exotica) {
172  CMSExoticaPhysics exo(phys, m_pPhysics);
173  }
174 
175  // adding GFlash, Russian Roulette for eletrons and gamma,
176  // step limiters on top of any Physics Lists
177  phys->RegisterPhysics(new ParametrisedEMPhysics("EMoptions", m_pPhysics));
178 
180  m_physicsList->SetPhysicsTableRetrieved(m_PhysicsTablesDir);
181  }
182  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMT: start initialisation of PhysicsList for master";
183 
184  m_physicsList->SetDefaultCutValue(m_pPhysics.getParameter<double>("DefaultCutValue") * CLHEP::cm);
185  m_physicsList->SetCutsWithDefault();
186  m_kernel->SetPhysics(phys);
187 
188  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMT: PhysicsList and cuts are defined";
189 
190  // Enable couple transportation
191  bool scorer = m_p.getParameter<bool>("UseCommandBaseScorer");
192  if (scorer) {
193  G4ScoringManager* scManager = G4ScoringManager::GetScoringManager();
194  scManager->SetVerboseLevel(1);
195  }
196  // Geant4 UI commands before initialisation of physics
197  if (!m_G4Commands.empty()) {
198  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMT: Requested UI commands: ";
199  for (const std::string& command : m_G4Commands) {
200  edm::LogVerbatim("SimG4CoreApplication") << " " << command;
201  G4UImanager::GetUIpointer()->ApplyCommand(command);
202  }
203  }
204 
205  m_stateManager->SetNewState(G4State_Init);
206  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMT: G4State is Init";
207  m_kernel->InitializePhysics();
208  m_kernel->SetUpDecayChannels();
209 
210  if (m_kernel->RunInitialization()) {
211  m_managerInitialized = true;
212  } else {
213  throw cms::Exception("LogicError") << "G4RunManagerKernel initialization failed!";
214  }
215 
216  if (m_StorePhysicsTables) {
217  std::ostringstream dir;
218  dir << m_PhysicsTablesDir << '\0';
219  std::string cmd = std::string("/control/shell mkdir -p ") + m_PhysicsTablesDir;
220  if (!std::ifstream(dir.str().c_str(), std::ios::in))
221  G4UImanager::GetUIpointer()->ApplyCommand(cmd);
222  m_physicsList->StorePhysicsTable(m_PhysicsTablesDir);
223  }
224  // Appload nuclear level data up to Z=84
225  G4NuclearLevelData::GetInstance()->UploadNuclearLevelData(84);
226 
227  if (verb > 1) {
228  m_physicsList->DumpCutValuesTable();
229  }
230  edm::LogVerbatim("SimG4CoreApplication")
231  << "RunManagerMT: Physics is initilized, now initialise user actions, verb=" << verb;
232 
234 
235  // geometry dump
236  auto writeFile = m_p.getUntrackedParameter<std::string>("FileNameGDML", "");
237  if (!writeFile.empty()) {
238  G4GDMLParser gdml;
239  gdml.SetRegionExport(true);
240  gdml.SetEnergyCutsExport(true);
241  gdml.Write(writeFile, m_world->GetWorldVolume(), true);
242  }
243 
244  // G4Region dump file name
245  auto regionFile = m_p.getUntrackedParameter<std::string>("FileNameRegions", "");
246 
247  // Geometry checks
248  if (m_check || !regionFile.empty()) {
250  }
251 
252  m_stateManager->SetNewState(G4State_PreInit);
253  G4HadronicParameters::Instance()->SetVerboseLevel(std::max(verb - 1, 0));
254 
255  // If the Geant4 particle table is needed, decomment the lines below
256  //
257  //G4ParticleTable::GetParticleTable()->DumpTable("ALL");
258  //
259  m_stateManager->SetNewState(G4State_GeomClosed);
260  m_currentRun = new G4Run();
262  timer.Stop();
263  G4cout.precision(4);
264  G4cout << "RunManagerMT: initG4 done " << timer << G4endl;
265 }
266 
268  m_runInterface = std::make_unique<SimRunInterface>(this, true);
271 }
272 
276 }
277 
279  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMT::stopG4";
280  G4GeometryManager::GetInstance()->OpenGeometry();
281  m_stateManager->SetNewState(G4State_Quit);
282  if (!m_runTerminated) {
283  terminateRun();
284  }
285  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMT::stopG4 done";
286 }
287 
289  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMT::terminateRun";
290  if (nullptr != m_userRunAction) {
292  delete m_userRunAction;
293  m_userRunAction = nullptr;
294  }
295  if (!m_runTerminated) {
296  m_kernel->RunTermination();
297  }
298  m_runTerminated = true;
299  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMT::terminateRun done";
300 }
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:95
Log< level::Info, true > LogVerbatim
bool m_managerInitialized
Definition: RunManagerMT.h:87
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:83
SensitiveDetectorCatalog m_catalog
Definition: RunManagerMT.h:107
edm::ParameterSet m_pRunAction
Definition: RunManagerMT.h:100
def create(alignables, pedeDump, additionalData, outputFile, config)
HepPDT::ParticleDataTable ParticleDataTable
void terminateRun()
RunManagerMT(edm::ParameterSet const &)
Definition: RunManagerMT.cc:68
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
T getUntrackedParameter(std::string const &, T const &) const
RunAction
list of unwanted particles (gluons and quarks)
TkSoA const *__restrict__ CAHitNtupletGeneratorKernelsGPU::QualityCuts cuts
edm::ParameterSet m_p
Definition: RunManagerMT.h:103
EndOfRunSignal endOfRunSignal_
bool m_StorePhysicsTables
Definition: RunManagerMT.h:96
const DDDWorld & world() const
Definition: RunManagerMT.h:70
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_
bool m_RestorePhysicsTables
Definition: RunManagerMT.h:97
void initG4(const DDCompactView *, const cms::DDCompactView *, const HepPDT::ParticleDataTable *)
Definition: RunManagerMT.cc:98
list command
Definition: mps_check.py:25
SimActivityRegistry::BeginOfRunSignal m_beginOfRunSignal
Definition: RunAction.h:23
HLT enums.
list cmd
Definition: mps_setup.py:244
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