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 #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_UseParametrisedEMPhysics(p.getUntrackedParameter<bool>("UseParametrisedEMPhysics")),
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  m_stateManager->SetExceptionHandler(new ExceptionHandler());
90  m_geometryManager->G4GeometryManager::GetInstance();
91 
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 = std::max(m_pPhysics.getUntrackedParameter<int>("Verbosity", 0),
108  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  m_world = std::make_unique<DDDWorld>(pDD, pDD4hep, m_catalog, verb, cuts, protonCut);
115  G4VPhysicalVolume* world = m_world.get()->GetWorldVolume();
116 
117  m_kernel->SetVerboseLevel(verb);
118  edm::LogVerbatim("SimG4CoreApplication")
119  << "RunManagerMT: Define cuts: " << cuts << " Geant4 run manager verbosity: " << verb;
120 
121  const G4RegionStore* regStore = G4RegionStore::GetInstance();
122  const G4PhysicalVolumeStore* pvs = G4PhysicalVolumeStore::GetInstance();
123  const G4LogicalVolumeStore* lvs = G4LogicalVolumeStore::GetInstance();
124  unsigned int numPV = pvs->size();
125  unsigned int numLV = lvs->size();
126  unsigned int nn = regStore->size();
127  edm::LogVerbatim("SimG4CoreApplication")
128  << "###RunManagerMT: " << numPV << " PhysVolumes; " << numLV << " LogVolumes; " << nn << " Regions.";
129 
130  if (m_check) {
131  m_kernel->SetVerboseLevel(2);
132  }
133  m_kernel->DefineWorldVolume(world, true);
135 
136  // Create physics list
137  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMT: create PhysicsList";
138 
139  std::unique_ptr<PhysicsListMakerBase> physicsMaker(
141  if (physicsMaker.get() == nullptr) {
142  throw edm::Exception(edm::errors::Configuration) << "Unable to find the Physics list requested";
143  }
144  m_physicsList = physicsMaker->make(m_pPhysics, m_registry);
145 
146  PhysicsList* phys = m_physicsList.get();
147  if (phys == nullptr) {
148  throw edm::Exception(edm::errors::Configuration, "Physics list construction failed!");
149  }
150  G4EmParameters::Instance()->SetVerbose(verb);
151  G4EmParameters::Instance()->SetWorkerVerbose(verb - 1);
152 
153  // exotic particle physics
154  double monopoleMass = m_pPhysics.getUntrackedParameter<double>("MonopoleMass", 0);
155  if (monopoleMass > 0.0) {
156  phys->RegisterPhysics(new CMSMonopolePhysics(fPDGTable, m_pPhysics));
157  }
158  bool exotica = m_pPhysics.getUntrackedParameter<bool>("ExoticaTransport", false);
159  if (exotica) {
160  CMSExoticaPhysics exo(phys, m_pPhysics);
161  }
162 
163  // adding GFlash, Russian Roulette for eletrons and gamma,
164  // 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  // Geant4 UI commands before initialisation of physics
180  if (!m_G4Commands.empty()) {
181  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMT: Requested UI commands: ";
182  for (const std::string& command : m_G4Commands) {
183  edm::LogVerbatim("SimG4CoreApplication") << " " << command;
184  G4UImanager::GetUIpointer()->ApplyCommand(command);
185  }
186  }
187 
188  m_stateManager->SetNewState(G4State_Init);
189  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMT: G4State is Init";
190  m_kernel->InitializePhysics();
191  m_kernel->SetUpDecayChannels();
192 
193  if (m_kernel->RunInitialization()) {
194  m_managerInitialized = true;
195  } else {
196  throw edm::Exception(edm::errors::LogicError, "G4RunManagerKernel initialization failed!");
197  }
198 
199  if (m_StorePhysicsTables) {
200  std::ostringstream dir;
201  dir << m_PhysicsTablesDir << '\0';
202  std::string cmd = std::string("/control/shell mkdir -p ") + m_PhysicsTablesDir;
203  if (!std::ifstream(dir.str().c_str(), std::ios::in))
204  G4UImanager::GetUIpointer()->ApplyCommand(cmd);
205  m_physicsList->StorePhysicsTable(m_PhysicsTablesDir);
206  }
207  G4NuclearLevelData::GetInstance()->UploadNuclearLevelData(84);
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()) {
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  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMT:: initG4 done";
241 }
242 
244  m_runInterface = std::make_unique<SimRunInterface>(this, true);
247 }
248 
252 }
253 
255  m_geometryManager->OpenGeometry();
256  m_stateManager->SetNewState(G4State_Quit);
257  if (!m_runTerminated) {
258  terminateRun();
259  }
260 }
261 
263  if (m_userRunAction) {
265  delete m_userRunAction;
266  m_userRunAction = nullptr;
267  }
268  if (m_kernel && !m_runTerminated) {
269  m_kernel->RunTermination();
270  }
271  m_runTerminated = true;
272  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMT:: terminateRun done";
273 }
RunManagerMT::m_runInterface
std::unique_ptr< SimRunInterface > m_runInterface
Definition: RunManagerMT.h:100
RunManagerMT::m_g4overlap
edm::ParameterSet m_g4overlap
Definition: RunManagerMT.h:109
mps_setup.cmd
list cmd
Definition: mps_setup.py:244
ParametrisedEMPhysics
Definition: ParametrisedEMPhysics.h:16
RunManagerMT::m_G4Commands
std::vector< std::string > m_G4Commands
Definition: RunManagerMT.h:110
electrons_cff.bool
bool
Definition: electrons_cff.py:393
RunAction.h
MessageLogger.h
RunManagerMT::m_runTerminated
bool m_runTerminated
Definition: RunManagerMT.h:94
funct::false
false
Definition: Factorize.h:29
RunManagerMT::initializeUserActions
void initializeUserActions()
Definition: RunManagerMT.cc:243
ExceptionHandler
Definition: ExceptionHandler.h:19
BeginOfJob.h
edm::errors::LogicError
Definition: EDMException.h:37
edm
HLT enums.
Definition: AlignableModifier.h:19
RunManagerMT::m_UIsession
CustomUIsession * m_UIsession
Definition: RunManagerMT.h:91
AlCaHLTBitMon_ParallelJobs.p
p
Definition: AlCaHLTBitMon_ParallelJobs.py:153
CMSExoticaPhysics
Definition: CMSExoticaPhysics.h:13
RunManagerMT::m_registry
SimActivityRegistry m_registry
Definition: RunManagerMT.h:114
SimActivityRegistry::dddWorldSignal_
DDDWorldSignal dddWorldSignal_
Definition: SimActivityRegistry.h:54
RunManagerMT::m_UseParametrisedEMPhysics
bool m_UseParametrisedEMPhysics
Definition: RunManagerMT.h:105
CMSG4CheckOverlap.h
CurrentG4Track.h
edm::ParameterSet::getUntrackedParameter
T getUntrackedParameter(std::string const &, T const &) const
beamerCreator.create
def create(alignables, pedeDump, additionalData, outputFile, config)
Definition: beamerCreator.py:44
mps_check.command
list command
Definition: mps_check.py:25
RunAction::m_beginOfRunSignal
SimActivityRegistry::BeginOfRunSignal m_beginOfRunSignal
Definition: RunAction.h:23
edm::LogWarning
Log< level::Warning, false > LogWarning
Definition: MessageLogger.h:122
DDCompactView.h
RunManagerMT::m_check
bool m_check
Definition: RunManagerMT.h:106
DDDWorld.h
g4SimHits_cfi.RunAction
RunAction
list of unwanted particles (gluons and quarks)
Definition: g4SimHits_cfi.py:222
RunManagerMT::terminateRun
void terminateRun()
Definition: RunManagerMT.cc:262
RunManagerMT::m_p
edm::ParameterSet m_p
Definition: RunManagerMT.h:111
RPCNoise_example.check
check
Definition: RPCNoise_example.py:71
PhysicsListFactory.h
g4SimHits_cfi.CustomUIsession
CustomUIsession
Definition: g4SimHits_cfi.py:87
RunManagerMT::RunManagerMT
RunManagerMT(edm::ParameterSet const &)
Definition: RunManagerMT.cc:66
DDCompactView
Compact representation of the geometrical detector hierarchy.
Definition: DDCompactView.h:81
RunManagerMT::m_currentRun
G4Run * m_currentRun
Definition: RunManagerMT.h:96
RunManagerMT::world
const DDDWorld & world() const
Definition: RunManagerMT.h:75
RunManagerMT::m_stateManager
G4StateManager * m_stateManager
Definition: RunManagerMT.h:97
CustomUIsession.h
PhysicsList
Definition: PhysicsList.h:7
DDCompactView.h
RunManagerMT::m_pPhysics
edm::ParameterSet m_pPhysics
Definition: RunManagerMT.h:107
sim_act::Signaler::connect
void connect(Observer< const T * > *iObs)
does not take ownership of memory
Definition: Signaler.h:55
RunManagerMT::m_physicsList
std::unique_ptr< PhysicsList > m_physicsList
Definition: RunManagerMT.h:92
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
CMSG4CheckOverlap
Definition: CMSG4CheckOverlap.h:11
RunAction
Definition: RunAction.h:15
edm::ParameterSet
Definition: ParameterSet.h:47
RunManagerMT::~RunManagerMT
~RunManagerMT()
Definition: RunManagerMT.cc:95
RunManagerMT::initG4
void initG4(const DDCompactView *, const cms::DDCompactView *, const HepPDT::ParticleDataTable *)
Definition: RunManagerMT.cc:97
ParameterSet
Definition: Functions.h:16
ParametrisedEMPhysics.h
SiStripPI::max
Definition: SiStripPayloadInspectorHelper.h:169
RunManagerMT.h
SimWatcherFactory.h
G4SimEvent.h
recoMuon::in
Definition: RecoMuonEnumerators.h:6
SimActivityRegistry::endOfRunSignal_
EndOfRunSignal endOfRunSignal_
Definition: SimActivityRegistry.h:79
trackerHitRTTI::vector
Definition: trackerHitRTTI.h:21
CMSGDMLWriteStructure.h
get
#define get
FSQDQM_cfi.pvs
pvs
Definition: FSQDQM_cfi.py:12
RunManagerMT::m_userRunAction
RunAction * m_userRunAction
Definition: RunManagerMT.h:95
groupFilesInBlocks.nn
nn
Definition: groupFilesInBlocks.py:150
RunAction::m_endOfRunSignal
SimActivityRegistry::EndOfRunSignal m_endOfRunSignal
Definition: RunAction.h:24
std
Definition: JetResolutionObject.h:76
ExceptionHandler.h
SimRunInterface.h
edm::LogVerbatim
Log< level::Info, true > LogVerbatim
Definition: MessageLogger.h:128
SimTrackManager.h
Exception
Definition: hltDiff.cc:246
cms::DDCompactView
Definition: DDCompactView.h:31
RunManagerMT::m_managerInitialized
bool m_managerInitialized
Definition: RunManagerMT.h:93
edm::ParameterSet::getParameter
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
RunManagerMT::m_StorePhysicsTables
bool m_StorePhysicsTables
Definition: RunManagerMT.h:103
PrimaryTransformer.h
RunAction::EndOfRunAction
void EndOfRunAction(const G4Run *aRun) override
Definition: RunAction.cc:26
RunManagerMT::Connect
void Connect(RunAction *)
Definition: RunManagerMT.cc:249
CMSExoticaPhysics.h
RunManagerMT::m_geometryManager
G4GeometryManager * m_geometryManager
Definition: RunManagerMT.h:98
RunManagerMT::m_PhysicsTablesDir
const std::string m_PhysicsTablesDir
Definition: RunManagerMT.h:102
CMSMonopolePhysics
Definition: CMSMonopolePhysics.h:18
RunManagerMT::m_world
std::unique_ptr< DDDWorld > m_world
Definition: RunManagerMT.h:113
RunManagerMT::m_RestorePhysicsTables
bool m_RestorePhysicsTables
Definition: RunManagerMT.h:104
RunManagerMT::stopG4
void stopG4()
Definition: RunManagerMT.cc:254
L1TMuonDQMOffline_cfi.cuts
cuts
Definition: L1TMuonDQMOffline_cfi.py:41
RunManagerMT::m_pRunAction
edm::ParameterSet m_pRunAction
Definition: RunManagerMT.h:108
SimActivityRegistry::beginOfRunSignal_
BeginOfRunSignal beginOfRunSignal_
Definition: SimActivityRegistry.h:59
RunManagerMT::m_kernel
G4MTRunManagerKernel * m_kernel
Definition: RunManagerMT.h:89
edm::errors::Configuration
Definition: EDMException.h:36
ParticleDataTable
HepPDT::ParticleDataTable ParticleDataTable
Definition: ParticleDataTable.h:8
RunManagerMT::m_catalog
SensitiveDetectorCatalog m_catalog
Definition: RunManagerMT.h:115
CMSMonopolePhysics.h
RunAction::BeginOfRunAction
void BeginOfRunAction(const G4Run *aRun) override
Definition: RunAction.cc:17
DeadROC_duringRun.dir
dir
Definition: DeadROC_duringRun.py:23