CMS 3D CMS Logo

RunManagerMT.cc
Go to the documentation of this file.
1 
8 
11 
15 
17 
24 
27 
29 
30 #include "HepPDT/ParticleDataTable.hh"
31 
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 
45 #include "G4GDMLParser.hh"
46 #include "G4SystemOfUnits.hh"
47 
48 #include "G4LogicalVolume.hh"
49 #include "G4LogicalVolumeStore.hh"
50 #include "G4PhysicalVolumeStore.hh"
51 #include "G4Region.hh"
52 #include "G4RegionStore.hh"
53 
54 #include <iostream>
55 #include <sstream>
56 #include <fstream>
57 #include <memory>
58 
60 
62  : m_managerInitialized(false),
63  m_runTerminated(false),
64  m_PhysicsTablesDir(p.getUntrackedParameter<std::string>("PhysicsTablesDirectory", "")),
65  m_StorePhysicsTables(p.getUntrackedParameter<bool>("StorePhysicsTables", false)),
66  m_RestorePhysicsTables(p.getUntrackedParameter<bool>("RestorePhysicsTables", false)),
67  m_UseParametrisedEMPhysics(p.getUntrackedParameter<bool>("UseParametrisedEMPhysics")),
68  m_pPhysics(p.getParameter<edm::ParameterSet>("Physics")),
69  m_pRunAction(p.getParameter<edm::ParameterSet>("RunAction")),
70  m_g4overlap(p.getUntrackedParameter<edm::ParameterSet>("G4CheckOverlap")),
71  m_G4Commands(p.getParameter<std::vector<std::string> >("G4Commands")),
72  m_p(p) {
73  m_currentRun = nullptr;
74  m_UIsession.reset(new CustomUIsession());
75  m_physicsList.reset(nullptr);
76  m_world.reset(nullptr);
77 
78  m_runInterface.reset(nullptr);
79  m_userRunAction = nullptr;
80  m_currentRun = nullptr;
81 
82  m_kernel = new G4MTRunManagerKernel();
83  m_stateManager = G4StateManager::GetStateManager();
84  m_stateManager->SetExceptionHandler(new ExceptionHandler());
85  m_geometryManager->G4GeometryManager::GetInstance();
86 
87  m_check = p.getUntrackedParameter<bool>("CheckGeometry", false);
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 geoFromDD4hep = m_p.getParameter<bool>("g4GeometryDD4hepSource");
100  bool cuts = m_pPhysics.getParameter<bool>("CutsPerRegion");
101  bool protonCut = m_pPhysics.getParameter<bool>("CutsOnProton");
102  int verb = std::max(m_pPhysics.getUntrackedParameter<int>("Verbosity", 0),
103  m_p.getUntrackedParameter<int>("SteppingVerbosity", 0));
104  edm::LogVerbatim("SimG4CoreApplication")
105  << "RunManagerMT: start initialising of geometry DD4Hep: " << geoFromDD4hep << "\n"
106  << " cutsPerRegion: " << cuts << " cutForProton: " << protonCut << "\n"
107  << " G4 verbosity: " << verb;
108 
109  m_world.reset(new DDDWorld(pDD, pDD4hep, m_catalog, verb, cuts, protonCut));
110  G4VPhysicalVolume* world = m_world.get()->GetWorldVolume();
111 
112  m_kernel->SetVerboseLevel(verb);
113  edm::LogVerbatim("SimG4CoreApplication")
114  << "RunManagerMT: Define cuts: " << cuts << " Geant4 run manager verbosity: " << verb;
115 
116  const G4RegionStore* regStore = G4RegionStore::GetInstance();
117  const G4PhysicalVolumeStore* pvs = G4PhysicalVolumeStore::GetInstance();
118  const G4LogicalVolumeStore* lvs = G4LogicalVolumeStore::GetInstance();
119  unsigned int numPV = pvs->size();
120  unsigned int numLV = lvs->size();
121  unsigned int nn = regStore->size();
122  edm::LogVerbatim("SimG4CoreApplication")
123  << "###RunManagerMT: " << numPV << " PhysVolumes; " << numLV << " LogVolumes; " << nn << " Regions.";
124 
125  if (m_check) {
126  m_kernel->SetVerboseLevel(2);
127  }
128  m_kernel->DefineWorldVolume(world, true);
130 
131  // Create physics list
132  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMT: create PhysicsList";
133 
134  std::unique_ptr<PhysicsListMakerBase> physicsMaker(
136  if (physicsMaker.get() == nullptr) {
137  throw edm::Exception(edm::errors::Configuration) << "Unable to find the Physics list requested";
138  }
139  m_physicsList = physicsMaker->make(m_pPhysics, m_registry);
140 
141  PhysicsList* phys = m_physicsList.get();
142  if (phys == nullptr) {
143  throw edm::Exception(edm::errors::Configuration, "Physics list construction failed!");
144  }
145 
146  // exotic particle physics
147  double monopoleMass = m_pPhysics.getUntrackedParameter<double>("MonopoleMass", 0);
148  if (monopoleMass > 0.0) {
149  phys->RegisterPhysics(new CMSMonopolePhysics(fPDGTable, m_pPhysics));
150  }
151  bool exotica = m_pPhysics.getUntrackedParameter<bool>("ExoticaTransport", false);
152  if (exotica) {
153  CMSExoticaPhysics exo(phys, m_pPhysics);
154  }
155 
156  // adding GFlash, Russian Roulette for eletrons and gamma,
157  // step limiters on top of any Physics Lists
159  phys->RegisterPhysics(new ParametrisedEMPhysics("EMoptions", m_pPhysics));
160 
162  m_physicsList->SetPhysicsTableRetrieved(m_PhysicsTablesDir);
163  }
164  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMT: start initialisation of PhysicsList for master";
165 
166  m_physicsList->SetDefaultCutValue(m_pPhysics.getParameter<double>("DefaultCutValue") * CLHEP::cm);
167  m_physicsList->SetCutsWithDefault();
168  m_kernel->SetPhysics(phys);
169 
170  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMT: PhysicsList and cuts are defined";
171 
172  // Geant4 UI commands before initialisation of physics
173  if (!m_G4Commands.empty()) {
174  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMT: Requested UI commands: ";
175  for (const std::string& command : m_G4Commands) {
176  edm::LogVerbatim("SimG4CoreApplication") << " " << command;
177  G4UImanager::GetUIpointer()->ApplyCommand(command);
178  }
179  }
180 
181  m_stateManager->SetNewState(G4State_Init);
182  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMT: G4State is Init";
183  m_kernel->InitializePhysics();
184  m_kernel->SetUpDecayChannels();
185 
186  // The following line was with the following comment in
187  // G4MTRunManager::InitializePhysics() in 10.00.p01; in practice
188  // needed to initialize certain singletons during the master thread
189  // initialization in order to avoid races later...
190  //
191  //BERTINI, this is needed to create pseudo-particles, to be removed
192  G4CascadeInterface::Initialize();
193 
194  if (m_kernel->RunInitialization()) {
195  m_managerInitialized = true;
196  } else {
197  throw edm::Exception(edm::errors::LogicError, "G4RunManagerKernel initialization failed!");
198  }
199 
200  if (m_StorePhysicsTables) {
201  std::ostringstream dir;
202  dir << m_PhysicsTablesDir << '\0';
203  std::string cmd = std::string("/control/shell mkdir -p ") + m_PhysicsTablesDir;
204  if (!std::ifstream(dir.str().c_str(), std::ios::in))
205  G4UImanager::GetUIpointer()->ApplyCommand(cmd);
206  m_physicsList->StorePhysicsTable(m_PhysicsTablesDir);
207  }
208 
209  if (verb > 1) {
210  m_physicsList->DumpCutValuesTable();
211  }
212  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMT: Physics is initilized, now initialise user actions";
213 
215 
216  // geometry dump
217  auto writeFile = m_p.getUntrackedParameter<std::string>("FileNameGDML", "");
218  if (!writeFile.empty()) {
219  G4GDMLParser gdml;
220  gdml.SetRegionExport(true);
221  gdml.SetEnergyCutsExport(true);
222  gdml.Write(writeFile, m_world->GetWorldVolume(), true);
223  }
224 
225  // G4Region dump file name
226  auto regionFile = m_p.getUntrackedParameter<std::string>("FileNameRegions", "");
227 
228  // Geometry checks
229  if (m_check || !regionFile.empty()) {
230  CMSG4CheckOverlap check(m_g4overlap, regionFile, m_UIsession.get(), world);
231  }
232 
233  // If the Geant4 particle table is needed, decomment the lines below
234  //
235  //G4ParticleTable::GetParticleTable()->DumpTable("ALL");
236  //
237  m_stateManager->SetNewState(G4State_GeomClosed);
238  m_currentRun = new G4Run();
240 }
241 
243  m_runInterface.reset(new SimRunInterface(this, true));
246 }
247 
251 }
252 
254  m_geometryManager->OpenGeometry();
255  m_stateManager->SetNewState(G4State_Quit);
256  if (!m_runTerminated) {
257  terminateRun();
258  }
259 }
260 
262  if (m_userRunAction) {
264  delete m_userRunAction;
265  m_userRunAction = nullptr;
266  }
267  if (m_kernel && !m_runTerminated) {
268  m_kernel->RunTermination();
269  }
270  m_runTerminated = true;
271 }
void Connect(RunAction *)
bool m_UseParametrisedEMPhysics
Definition: RunManagerMT.h:105
const std::string m_PhysicsTablesDir
Definition: RunManagerMT.h:102
bool m_managerInitialized
Definition: RunManagerMT.h:93
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
void BeginOfRunAction(const G4Run *aRun) override
Definition: RunAction.cc:17
G4MTRunManagerKernel * m_kernel
Definition: RunManagerMT.h:89
SensitiveDetectorCatalog m_catalog
Definition: RunManagerMT.h:115
edm::ParameterSet m_pRunAction
Definition: RunManagerMT.h:108
def create(alignables, pedeDump, additionalData, outputFile, config)
HepPDT::ParticleDataTable ParticleDataTable
void terminateRun()
RunManagerMT(edm::ParameterSet const &)
Definition: RunManagerMT.cc:61
SimActivityRegistry::EndOfRunSignal m_endOfRunSignal
Definition: RunAction.h:24
bool m_runTerminated
Definition: RunManagerMT.h:94
G4StateManager * m_stateManager
Definition: RunManagerMT.h:97
BeginOfRunSignal beginOfRunSignal_
std::vector< std::string > m_G4Commands
Definition: RunManagerMT.h:110
std::unique_ptr< CustomUIsession > m_UIsession
Definition: RunManagerMT.h:91
G4Run * m_currentRun
Definition: RunManagerMT.h:96
void initializeUserActions()
Compact representation of the geometrical detector hierarchy.
Definition: DDCompactView.h:80
const DDDWorld & world() const
Definition: RunManagerMT.h:75
RunAction
list of unwanted particles (gluons and quarks)
edm::ParameterSet m_p
Definition: RunManagerMT.h:111
EndOfRunSignal endOfRunSignal_
void connect(Observer< const T * > *iObs)
does not take ownership of memory
Definition: Signaler.h:55
bool m_StorePhysicsTables
Definition: RunManagerMT.h:103
G4GeometryManager * m_geometryManager
Definition: RunManagerMT.h:98
std::unique_ptr< PhysicsList > m_physicsList
Definition: RunManagerMT.h:92
std::unique_ptr< DDDWorld > m_world
Definition: RunManagerMT.h:113
edm::ParameterSet m_pPhysics
Definition: RunManagerMT.h:107
DDDWorldSignal dddWorldSignal_
bool m_RestorePhysicsTables
Definition: RunManagerMT.h:104
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
HLT enums.
list cmd
Definition: mps_setup.py:244
std::unique_ptr< SimRunInterface > m_runInterface
Definition: RunManagerMT.h:100
edm::ParameterSet m_g4overlap
Definition: RunManagerMT.h:109
RunAction * m_userRunAction
Definition: RunManagerMT.h:95
void EndOfRunAction(const G4Run *aRun) override
Definition: RunAction.cc:26
SimActivityRegistry m_registry
Definition: RunManagerMT.h:114