49 #include "G4SystemOfUnits.hh" 50 #include "G4Threading.hh" 51 #include "G4UImanager.hh" 52 #include "G4WorkerThread.hh" 53 #include "G4WorkerRunManagerKernel.hh" 54 #include "G4StateManager.hh" 55 #include "G4TransportationManager.hh" 57 #include "G4FieldManager.hh" 58 #include "G4ScoringManager.hh" 72 std::atomic<int> thread_counter{0};
74 int get_new_thread_index() {
return thread_counter++; }
78 std::vector<std::shared_ptr<SimWatcher>>& oWatchers,
81 std::vector<edm::ParameterSet>
ws = iP.
getParameter<std::vector<edm::ParameterSet>>(
"Watchers");
86 for (
auto& watcher :
ws) {
87 std::unique_ptr<SimWatcherMakerBase> maker(
89 if (maker ==
nullptr) {
91 <<
"RunManagerMTWorker::createWatchers: " 92 <<
"Unable to find the requested Watcher " << watcher.getParameter<
std::string>(
"type");
94 std::shared_ptr<SimWatcher> watcherTemp;
95 std::shared_ptr<SimProducer> producerTemp;
96 maker->make(watcher, *(iReg), watcherTemp, producerTemp);
97 if (
nullptr != watcherTemp) {
98 if (!watcherTemp->isMT() && 0 < threadID) {
100 <<
"RunManagerMTWorker::createWatchers: " 101 <<
"Unable to use Watcher " << watcher.getParameter<
std::string>(
"type") <<
" if number of threads > 1";
103 oWatchers.push_back(watcherTemp);
104 if (
nullptr != producerTemp) {
105 oProds.push_back(producerTemp);
111 return (!oWatchers.empty());
116 std::unique_ptr<G4RunManagerKernel>
kernel;
152 m_nonBeam(iConfig.getParameter<
bool>(
"NonBeamEvent")),
153 m_UseG4EventManager(iConfig.getParameter<
bool>(
"UseG4EventManager")),
154 m_pUseMagneticField(iConfig.getParameter<
bool>(
"UseMagneticField")),
155 m_LHCTransport(iConfig.getParameter<
bool>(
"LHCTransport")),
156 m_thread_index{get_new_thread_index()},
163 m_pCustomUIsession(iConfig.getUntrackedParameter<
edm::ParameterSet>(
"CustomUIsession")),
164 m_G4CommandsEndRun(iConfig.getParameter<std::vector<std::string>>(
"G4CommandsEndRun")),
166 int thisID = getThreadIndex();
167 edm::LogVerbatim(
"SimG4CoreApplication") <<
"RunManagerMTWorker for the thread " << thisID;
170 std::vector<std::string> onlySDs = iConfig.getParameter<std::vector<std::string>>(
"OnlySDs");
176 for (
auto& watcher : m_tls->watchers) {
177 watcher->registerConsumes(iC);
180 if (m_LHCTransport) {
183 if (m_pUseMagneticField) {
186 edm::LogVerbatim(
"SimG4CoreApplication") <<
"RunManagerMTWorker is constructed for the thread " << thisID;
188 for (std::unordered_map<
std::string, std::unique_ptr<SensitiveDetectorMakerBase>>::const_iterator itr =
190 itr != m_sdMakers.end();
202 edm::LogVerbatim(
"SimG4CoreApplication") <<
"RunManagerMTWorker::beginRun for the thread " <<
id;
204 maker.second->beginRun(es);
211 watcher->beginRun(es);
214 edm::LogVerbatim(
"SimG4CoreApplication") <<
"RunManagerMTWorker::beginRun done for the thread " <<
id;
219 edm::LogVerbatim(
"SimG4CoreApplication") <<
"RunManagerMTWorker::endRun for the thread " <<
id;
224 if (
nullptr !=
m_tls) {
239 <<
"RunManagerMTWorker::initializeTLS : " 240 <<
"SimActivityRegistry service (i.e. visualization) is not supported for more than 1 thread. " 241 <<
" \n If this use case is needed, RunManagerMTWorker has to be updated.";
258 edm::LogVerbatim(
"SimG4CoreApplication") <<
"RunManagerMTWorker::initializeG4 in thread " << thisID <<
" is started";
261 G4Threading::G4SetThreadId(thisID);
262 G4UImanager::GetUIpointer()->SetUpForAThread(thisID);
264 if (uitype ==
"MessageLogger") {
266 }
else if (uitype ==
"MessageLoggerThreadPrefix") {
269 }
else if (uitype ==
"FilePerThread") {
274 <<
"RunManagerMTWorker::initializeG4: Invalid value of CustomUIsession.Type '" << uitype
275 <<
"', valid are MessageLogger, MessageLoggerThreadPrefix, FilePerThread";
277 G4UImanager::GetUIpointer()->SetCoutDestination(
m_UIsession);
280 G4WorkerThread::BuildGeometryAndPhysicsVector();
283 m_tls->
kernel.reset(G4WorkerRunManagerKernel::GetRunManagerKernel());
285 m_tls->
kernel = std::make_unique<G4WorkerRunManagerKernel>();
289 double th =
m_p.
getParameter<
double>(
"ThresholdForGeometryExceptions") * CLHEP::GeV;
291 G4StateManager::GetStateManager()->SetExceptionHandler(
new ExceptionHandler(th, tr));
296 G4TransportationManager* tM = G4TransportationManager::GetTransportationManager();
297 tM->SetWorldForTracking(worldPV);
309 tM->SetFieldManager(fieldManager);
310 fieldBuilder.
build(fieldManager, tM->GetPropagatorInField());
313 if (!fieldFile.empty()) {
317 <<
"RunManagerMTWorker::InitializeG4: Dump magnetic field to file " << fieldFile;
331 <<
"RunManagerMTWorker::InitializeG4: Sensitive Detectors are built in thread " << thisID <<
" found " 337 G4ScoringManager* scManager = G4ScoringManager::GetScoringManager();
338 scManager->SetVerboseLevel(1);
345 <<
"RunManagerMTWorker::InitializeG4: start initialisation of PhysicsList for the thread " << thisID;
348 if (!runManagerMaster->
G4Commands().empty()) {
349 G4cout <<
"RunManagerMTWorker::InitializeG4: Requested UI commands: " << G4endl;
352 G4UImanager::GetUIpointer()->ApplyCommand(
command);
355 G4StateManager::GetStateManager()->SetNewState(G4State_Init);
357 physicsList->InitializeWorker();
363 <<
"RunManagerMTWorker::InitializeG4: Geant4 kernel initialization failed in thread " << thisID;
377 m_sVerbose = std::make_unique<CMSSteppingVerbose>(
sv, elim, ve, vn, vt);
383 G4StateManager::GetStateManager()->SetNewState(G4State_Idle);
387 <<
"RunManagerMTWorker::initializeG4 done for the thread " << thisID <<
" " << timer;
398 G4EventManager* eventManager =
m_tls->
kernel->GetEventManager();
399 eventManager->SetVerboseLevel(ver);
401 auto userEventAction =
405 eventManager->SetUserAction(userEventAction);
413 eventManager->SetUserAction(userTrackingAction);
418 auto userSteppingAction =
422 eventManager->SetUserAction(userSteppingAction);
429 eventManager->SetUserAction(userStackingAction);
473 G4StateManager::GetStateManager()->SetNewState(G4State_GeomClosed);
478 edm::LogVerbatim(
"SimG4CoreApplication") <<
"RunManagerMTWorker::initializeRun done for thread " <<
id;
483 edm::LogVerbatim(
"SimG4CoreApplication") <<
"RunManagerMTWorker::terminateRun for thread " <<
id;
492 edm::LogVerbatim(
"SimG4CoreApplication") <<
"RunManagerMTWorker: Requested end of run UI commands: ";
495 G4UImanager::GetUIpointer()->ApplyCommand(
command);
508 edm::LogVerbatim(
"SimG4CoreApplication") <<
"RunManagerMTWorker::terminateRun done for thread " <<
id;
546 genVertex->y() / CLHEP::cm,
547 genVertex->z() / CLHEP::cm,
552 <<
"RunManagerMTWorker::produce: event " << inpevt.
id().
event() <<
" with no G4PrimaryVertices" 557 <<
"RunManagerMTWorker::produce: start EventID=" << inpevt.
id().
event() <<
" StreamID=" << inpevt.
streamID()
559 <<
m_tls->
currentEvent->GetNumberOfPrimaryVertex() <<
" vertices for Geant4; generator produced " 575 edm::LogVerbatim(
"SimG4CoreApplication") <<
"RunManagerMTWorker::produce: ended Event " << inpevt.
id().
event();
583 G4Track*
t =
m_tls->
kernel->GetEventManager()->GetTrackingManager()->GetTrack();
584 t->SetTrackStatus(fStopAndKill);
592 m_tls->
kernel->GetEventManager()->GetStackManager()->clear();
593 m_tls->
kernel->GetEventManager()->GetTrackingManager()->EventAborted();
609 G4Event* evt =
new G4Event(
evtid);
647 <<
"MTWorker::DumpMagneticField: error opening file <" <<
file <<
"> for magnetic field";
650 double rmax = 9000 * mm;
651 double zmax = 24000 * mm;
667 double point[4] = {0.0, 0.0, 0.0, 0.0};
668 double bfield[3] = {0.0, 0.0, 0.0};
670 fout << std::setprecision(6);
671 for (
int i = 0;
i <=
nr; ++
i) {
673 for (
int j = 0;
j <= nz; ++
j) {
677 field->GetFieldValue(
point, bfield);
678 fout <<
"R(mm)= " <<
r / mm <<
" phi(deg)= " <<
phi / degree <<
" Z(mm)= " <<
z / mm
679 <<
" Bz(tesla)= " << bfield[2] / tesla <<
" Br(tesla)= " << (bfield[0] * cosf + bfield[1] * sinf) / tesla
680 <<
" Bphi(tesla)= " << (bfield[0] * sinf - bfield[1] * cosf) / tesla << G4endl;
void connect(Observer< const T *> *iObs)
does not take ownership of memory
edm::ParameterSet m_pSteppingAction
Log< level::Info, true > LogVerbatim
virtual const HepMC::GenEvent * genEvent() const
std::vector< std::shared_ptr< SimWatcher > > watchers
T getParameter(std::string const &) const
CustomUIsession * m_UIsession
void nonCentralEvent2G4(const HepMC::GenEvent *g, G4Event *e)
std::unique_ptr< G4Event > currentEvent
T const & getData(const ESGetToken< T, R > &iToken) const noexcept(false)
ROOT::Math::LorentzVector< ROOT::Math::PxPyPzE4D< double > > XYZTLorentzVectorD
Lorentz vector with cylindrical internal representation using pseudorapidity.
unsigned int nGenParts() const
void initializeUserActions()
def create(alignables, pedeDump, additionalData, outputFile, config)
SimActivityRegistry::G4StepSignal m_g4StepSignal
SimTrackManager * GetSimTrackManager()
void HepMC2G4(const HepMC::GenEvent *g, G4Event *e)
std::unique_ptr< SimRunInterface > runInterface
Sin< T >::type sin(const T &t)
std::unique_ptr< CMSSteppingVerbose > m_sVerbose
T const * product() const
SimActivityRegistry::EndOfRunSignal m_endOfRunSignal
edm::RunNumber_t currentRunNumber
std::vector< SensitiveTkDetector * > sensTkDets
bool getByToken(EDGetToken token, Handle< PROD > &result) const
SimActivityRegistry::EndOfEventSignal m_endOfEventSignal
G4VPhysicalVolume * GetWorldVolume() const
virtual const math::XYZTLorentzVector * genVertex() const
void beginRun(const edm::EventSetup &)
edm::EDGetTokenT< edm::HepMCProduct > m_InToken
The Signals That Services Can Subscribe To This is based on ActivityRegistry and is current per Services can connect to the signals distributed by the ActivityRegistry in order to monitor the activity of the application Each possible callback has some defined which we here list in angle e g
std::vector< std::string > m_G4CommandsEndRun
G4SimEvent * produce(const edm::Event &inpevt, const edm::EventSetup &es, RunManagerMT &runManagerMaster)
std::unique_ptr< G4RunManagerKernel > kernel
std::unique_ptr< CMSSimEventManager > m_evtManager
edm::EDGetTokenT< edm::HepMCProduct > m_LHCToken
T getUntrackedParameter(std::string const &, T const &) const
U second(std::pair< T, U > const &p)
static std::once_flag applyOnceEnd
std::unique_ptr< SimActivityRegistry > registry
edm::ParameterSet m_pEventAction
std::unique_ptr< SimTrackManager > trackManager
edm::ParameterSet m_pRunAction
std::vector< SensitiveCaloDetector * > sensCaloDets
std::unordered_map< std::string, std::unique_ptr< SensitiveDetectorMakerBase > > m_sdMakers
Cos< T >::type cos(const T &t)
std::vector< SensitiveTkDetector * > & sensTkDetectors()
void resetGenParticleId(const edm::Event &inpevt)
RunManagerMTWorker(const edm::ParameterSet &iConfig, edm::ConsumesCollector &&iC)
const SensitiveDetectorCatalog & catalog() const
std::unique_ptr< RunAction > userRunAction
void hepEvent(const HepMC::GenEvent *r)
void setGenEvent(const HepMC::GenEvent *inpevt)
SimActivityRegistry::EndOfTrackSignal m_endOfTrackSignal
StreamID streamID() const
std::vector< std::shared_ptr< SimProducer > > producers
void abortRun(bool softAbort=false)
const HepMC::GenEvent * GetEvent() const
void build(CMSFieldManager *fM, G4PropagatorInField *fP)
std::pair< std::vector< SensitiveTkDetector * >, std::vector< SensitiveCaloDetector * > > attachSD(const std::unordered_map< std::string, std::unique_ptr< SensitiveDetectorMakerBase >> &, const edm::EventSetup &, const SensitiveDetectorCatalog &, edm::ParameterSet const &, const SimTrackManager *, SimActivityRegistry ®)
const DDDWorld & world() const
static std::once_flag applyOnce
std::vector< SensitiveCaloDetector * > & sensCaloDetectors()
edm::ParameterSet m_pTrackingAction
edm::ParameterSet m_pField
G4Event * generateEvent(const edm::Event &inpevt)
std::vector< std::shared_ptr< SimProducer > > & producers()
edm::EDGetTokenT< edm::LHCTransportLinkContainer > m_theLHCTlinkToken
const std::vector< std::string > & G4Commands() const
SimActivityRegistry::BeginOfRunSignal m_beginOfRunSignal
edm::ParameterSet m_pCustomUIsession
const MagneticField * m_pMagField
std::unordered_map< std::string, std::unique_ptr< SensitiveDetectorMakerBase > > sensitiveDetectorMakers(edm::ParameterSet const &, edm::ConsumesCollector, std::vector< std::string > const &chosenMakers)
std::vector< LHCTransportLink > LHCTransportLinkContainer
static void createWatchers(const edm::ParameterSet &iP, SimActivityRegistry &iReg, std::vector< std::shared_ptr< SimWatcher >> &oWatchers, std::vector< std::shared_ptr< SimProducer >> &oProds)
void PostUserTrackingAction(const G4Track *aTrack) override
void initializeG4(RunManagerMT *runManagerMaster, const edm::EventSetup &es)
virtual const double eventWeight() const
Log< level::Warning, false > LogWarning
void DumpMagneticField(const G4Field *, const std::string &) const
int getThreadIndex() const
edm::ESGetToken< MagneticField, IdealMagneticFieldRecord > m_MagField
void collisionPoint(const math::XYZTLorentzVectorD &v)
SimActivityRegistry::BeginOfEventSignal m_beginOfEventSignal
*vegas h *****************************************************used in the default bin number in original ***version of VEGAS is ***a higher bin number might help to derive a more precise ***grade subtle point
EventNumber_t event() const
PhysicsList * physicsListForWorker() const
edm::ParameterSet m_pStackingAction
void Connect(RunAction *)
SimActivityRegistry::BeginOfTrackSignal m_beginOfTrackSignal