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 "G4Timer.hh"
33 #include "G4GeometryManager.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 
66 
68  : m_managerInitialized(false),
69  m_runTerminated(false),
70  m_PhysicsTablesDir(p.getUntrackedParameter<std::string>("PhysicsTablesDirectory", "")),
71  m_StorePhysicsTables(p.getUntrackedParameter<bool>("StorePhysicsTables", false)),
72  m_RestorePhysicsTables(p.getUntrackedParameter<bool>("RestorePhysicsTables", false)),
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  double th = p.getParameter<double>("ThresholdForGeometryExceptions") * CLHEP::GeV;
89  G4StateManager::GetStateManager()->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  m_world = std::make_unique<DDDWorld>(pDD, pDD4hep, m_catalog, verb, cuts, protonCut);
116  G4VPhysicalVolume* world = m_world.get()->GetWorldVolume();
117 
118  m_kernel->SetVerboseLevel(verb);
119  edm::LogVerbatim("SimG4CoreApplication")
120  << "RunManagerMT: Define cuts: " << cuts << " Geant4 run manager verbosity: " << verb;
121 
122  const G4RegionStore* regStore = G4RegionStore::GetInstance();
123  const G4PhysicalVolumeStore* pvs = G4PhysicalVolumeStore::GetInstance();
124  const G4LogicalVolumeStore* lvs = G4LogicalVolumeStore::GetInstance();
125  unsigned int numPV = pvs->size();
126  unsigned int numLV = lvs->size();
127  unsigned int nn = regStore->size();
128  edm::LogVerbatim("SimG4CoreApplication")
129  << "RunManagerMT: " << numPV << " physical volumes; " << numLV << " logical volumes; " << nn << " regions.";
130 
131  if (m_check) {
132  m_kernel->SetVerboseLevel(2);
133  }
134  m_kernel->DefineWorldVolume(world, true);
136  G4StateManager::GetStateManager()->SetNewState(G4State_PreInit);
137 
138  // Create physics list
139  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMT: create PhysicsList";
140 
141  std::unique_ptr<PhysicsListMakerBase> physicsMaker(
143  if (physicsMaker.get() == nullptr) {
144  throw edm::Exception(edm::errors::Configuration) << "Unable to find the Physics list requested";
145  }
146  m_physicsList = physicsMaker->make(m_pPhysics, m_registry);
147 
148  PhysicsList* phys = m_physicsList.get();
149  if (phys == nullptr) {
150  throw edm::Exception(edm::errors::Configuration, "Physics list construction failed!");
151  }
152  if (stepverb > 0) {
153  verb = std::max(verb, 1);
154  }
155  G4HadronicParameters::Instance()->SetVerboseLevel(verb);
156  G4EmParameters::Instance()->SetVerbose(verb);
157  G4EmParameters::Instance()->SetWorkerVerbose(std::max(verb - 1, 0));
158 
159  // exotic particle physics
160  double monopoleMass = m_pPhysics.getUntrackedParameter<double>("MonopoleMass", 0);
161  if (monopoleMass > 0.0) {
162  phys->RegisterPhysics(new CMSMonopolePhysics(fPDGTable, m_pPhysics));
163  }
164  bool exotica = m_pPhysics.getUntrackedParameter<bool>("ExoticaTransport", false);
165  if (exotica) {
166  CMSExoticaPhysics exo(phys, m_pPhysics);
167  }
168 
169  // adding GFlash, Russian Roulette for eletrons and gamma,
170  // step limiters on top of any Physics Lists
171  phys->RegisterPhysics(new ParametrisedEMPhysics("EMoptions", m_pPhysics));
172 
174  m_physicsList->SetPhysicsTableRetrieved(m_PhysicsTablesDir);
175  }
176  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMT: start initialisation of PhysicsList for master";
177 
178  m_physicsList->SetDefaultCutValue(m_pPhysics.getParameter<double>("DefaultCutValue") * CLHEP::cm);
179  m_physicsList->SetCutsWithDefault();
180  m_kernel->SetPhysics(phys);
181 
182  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMT: PhysicsList and cuts are defined";
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  G4StateManager::GetStateManager()->SetNewState(G4State_Init);
194  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMT: G4State is Init";
195  m_kernel->InitializePhysics();
196  m_kernel->SetUpDecayChannels();
197 
198  if (m_kernel->RunInitialization()) {
199  m_managerInitialized = true;
200  } else {
201  throw edm::Exception(edm::errors::LogicError, "G4RunManagerKernel initialization failed!");
202  }
203 
204  if (m_StorePhysicsTables) {
205  std::ostringstream dir;
206  dir << m_PhysicsTablesDir << '\0';
207  std::string cmd = std::string("/control/shell mkdir -p ") + m_PhysicsTablesDir;
208  if (!std::ifstream(dir.str().c_str(), std::ios::in))
209  G4UImanager::GetUIpointer()->ApplyCommand(cmd);
210  m_physicsList->StorePhysicsTable(m_PhysicsTablesDir);
211  }
212  // Appload nuclear level data up to Z=84
213  G4NuclearLevelData::GetInstance()->UploadNuclearLevelData(84);
214 
215  if (verb > 1) {
216  m_physicsList->DumpCutValuesTable();
217  }
218  edm::LogVerbatim("SimG4CoreApplication")
219  << "RunManagerMT: Physics is initilized, now initialise user actions, verb=" << verb;
220 
222 
223  // geometry dump
224  auto writeFile = m_p.getUntrackedParameter<std::string>("FileNameGDML", "");
225  if (!writeFile.empty()) {
226  G4GDMLParser gdml;
227  gdml.SetRegionExport(true);
228  gdml.SetEnergyCutsExport(true);
229  gdml.Write(writeFile, m_world->GetWorldVolume(), true);
230  }
231 
232  // G4Region dump file name
233  auto regionFile = m_p.getUntrackedParameter<std::string>("FileNameRegions", "");
234 
235  // Geometry checks
236  if (m_check || !regionFile.empty()) {
238  }
239 
240  G4StateManager::GetStateManager()->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  G4StateManager::GetStateManager()->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  G4GeometryManager::GetInstance()->OpenGeometry();
268  G4StateManager::GetStateManager()->SetNewState(G4State_Quit);
269  if (!m_runTerminated) {
270  terminateRun();
271  }
272 }
273 
275  if (nullptr != m_userRunAction) {
277  delete m_userRunAction;
278  m_userRunAction = nullptr;
279  }
280  if (!m_runTerminated) {
281  m_kernel->RunTermination();
282  }
283  m_runTerminated = true;
284  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMT:: terminateRun done";
285 }
RunManagerMT::m_runInterface
std::unique_ptr< SimRunInterface > m_runInterface
Definition: RunManagerMT.h:92
RunManagerMT::m_g4overlap
edm::ParameterSet m_g4overlap
Definition: RunManagerMT.h:100
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:101
electrons_cff.bool
bool
Definition: electrons_cff.py:366
RunAction.h
MessageLogger.h
RunManagerMT::m_runTerminated
bool m_runTerminated
Definition: RunManagerMT.h:88
funct::false
false
Definition: Factorize.h:29
cuts
const TkSoA *__restrict__ CAHitNtupletGeneratorKernelsGPU::QualityCuts cuts
Definition: CAHitNtupletGeneratorKernelsImpl.h:416
RunManagerMT::initializeUserActions
void initializeUserActions()
Definition: RunManagerMT.cc:255
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:85
CMSExoticaPhysics
Definition: CMSExoticaPhysics.h:13
RunManagerMT::m_registry
SimActivityRegistry m_registry
Definition: RunManagerMT.h:105
SimActivityRegistry::dddWorldSignal_
DDDWorldSignal dddWorldSignal_
Definition: SimActivityRegistry.h:50
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:97
DDDWorld.h
g4SimHits_cfi.RunAction
RunAction
list of unwanted particles (gluons and quarks)
Definition: g4SimHits_cfi.py:263
RunManagerMT::terminateRun
void terminateRun()
Definition: RunManagerMT.cc:274
RunManagerMT::m_p
edm::ParameterSet m_p
Definition: RunManagerMT.h:102
RPCNoise_example.check
check
Definition: RPCNoise_example.py:71
PhysicsListFactory.h
g4SimHits_cfi.CustomUIsession
CustomUIsession
Definition: g4SimHits_cfi.py:121
RunManagerMT::RunManagerMT
RunManagerMT(edm::ParameterSet const &)
Definition: RunManagerMT.cc:67
DDCompactView
Compact representation of the geometrical detector hierarchy.
Definition: DDCompactView.h:81
RunManagerMT::m_currentRun
G4Run * m_currentRun
Definition: RunManagerMT.h:90
RunManagerMT::world
const DDDWorld & world() const
Definition: RunManagerMT.h:69
CustomUIsession.h
PhysicsList
Definition: PhysicsList.h:7
DDCompactView.h
RunManagerMT::m_pPhysics
edm::ParameterSet m_pPhysics
Definition: RunManagerMT.h:98
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:86
CMSG4CheckOverlap
Definition: CMSG4CheckOverlap.h:11
RunAction
Definition: RunAction.h:15
edm::ParameterSet
Definition: ParameterSet.h:47
AlCaHLTBitMon_ParallelJobs.p
def p
Definition: AlCaHLTBitMon_ParallelJobs.py:153
RunManagerMT::~RunManagerMT
~RunManagerMT()
Definition: RunManagerMT.cc:93
RunManagerMT::initG4
void initG4(const DDCompactView *, const cms::DDCompactView *, const HepPDT::ParticleDataTable *)
Definition: RunManagerMT.cc:95
ParameterSet
Definition: Functions.h:16
ParametrisedEMPhysics.h
SiStripPI::max
Definition: SiStripPayloadInspectorHelper.h:169
RunManagerMT.h
SimWatcherFactory.h
G4SimEvent.h
GeV
const double GeV
Definition: MathUtil.h:16
recoMuon::in
Definition: RecoMuonEnumerators.h:6
SimActivityRegistry::endOfRunSignal_
EndOfRunSignal endOfRunSignal_
Definition: SimActivityRegistry.h:75
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:89
AlCaHLTBitMon_QueryRunRegistry.string
string string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
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:245
cms::DDCompactView
Definition: DDCompactView.h:31
RunManagerMT::m_managerInitialized
bool m_managerInitialized
Definition: RunManagerMT.h:87
edm::ParameterSet::getParameter
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
RunManagerMT::m_StorePhysicsTables
bool m_StorePhysicsTables
Definition: RunManagerMT.h:95
PrimaryTransformer.h
RunAction::EndOfRunAction
void EndOfRunAction(const G4Run *aRun) override
Definition: RunAction.cc:26
RunManagerMT::Connect
void Connect(RunAction *)
Definition: RunManagerMT.cc:261
CMSExoticaPhysics.h
RunManagerMT::m_PhysicsTablesDir
const std::string m_PhysicsTablesDir
Definition: RunManagerMT.h:94
CMSMonopolePhysics
Definition: CMSMonopolePhysics.h:18
RunManagerMT::m_world
std::unique_ptr< DDDWorld > m_world
Definition: RunManagerMT.h:104
RunManagerMT::m_RestorePhysicsTables
bool m_RestorePhysicsTables
Definition: RunManagerMT.h:96
RunManagerMT::stopG4
void stopG4()
Definition: RunManagerMT.cc:266
RunManagerMT::m_pRunAction
edm::ParameterSet m_pRunAction
Definition: RunManagerMT.h:99
ecalTB2006H4_GenSimDigiReco_cfg.G4cout
G4cout
Definition: ecalTB2006H4_GenSimDigiReco_cfg.py:285
SimActivityRegistry::beginOfRunSignal_
BeginOfRunSignal beginOfRunSignal_
Definition: SimActivityRegistry.h:55
RunManagerMT::m_kernel
G4MTRunManagerKernel * m_kernel
Definition: RunManagerMT.h:83
edm::errors::Configuration
Definition: EDMException.h:36
ParticleDataTable
HepPDT::ParticleDataTable ParticleDataTable
Definition: ParticleDataTable.h:8
RunManagerMT::m_catalog
SensitiveDetectorCatalog m_catalog
Definition: RunManagerMT.h:106
CMSMonopolePhysics.h
RunAction::BeginOfRunAction
void BeginOfRunAction(const G4Run *aRun) override
Definition: RunAction.cc:17
DeadROC_duringRun.dir
dir
Definition: DeadROC_duringRun.py:23