53 #include "G4SystemOfUnits.hh" 54 #include "G4Threading.hh" 55 #include "G4UImanager.hh" 56 #include "G4WorkerThread.hh" 57 #include "G4WorkerRunManagerKernel.hh" 58 #include "G4StateManager.hh" 59 #include "G4TransportationManager.hh" 68 std::atomic<int> thread_counter{ 0 };
70 int get_new_thread_index() {
71 return thread_counter++;
74 thread_local
int s_thread_index = get_new_thread_index();
76 int getThreadIndex() {
return s_thread_index; }
80 std::vector<std::shared_ptr<SimWatcher> >& oWatchers,
81 std::vector<std::shared_ptr<SimProducer> >& oProds,
85 if(!iP.
exists(
"Watchers")) {
return; }
87 std::vector<edm::ParameterSet> watchers =
88 iP.
getParameter<std::vector<edm::ParameterSet> >(
"Watchers");
90 for(
auto & watcher : watchers) {
91 std::unique_ptr<SimWatcherMakerBase> maker(
96 <<
"Unable to find the requested Watcher <" 99 std::shared_ptr<SimWatcher> watcherTemp;
100 std::shared_ptr<SimProducer> producerTemp;
101 maker->make(watcher,*(iReg),watcherTemp,producerTemp);
102 oWatchers.push_back(watcherTemp);
104 oProds.push_back(producerTemp);
109 std::atomic<int> active_tlsdata{0};
110 std::atomic<bool> tls_shutdown_timeout{
false};
111 std::atomic<int> n_tls_shutdown_task{0};
115 std::unique_ptr<G4RunManagerKernel>
kernel;
123 std::vector<std::shared_ptr<SimWatcher> >
watchers;
168 std::vector<edm::ParameterSet> watchers =
169 iConfig.
getParameter<std::vector<edm::ParameterSet> >(
"Watchers");
177 ++n_tls_shutdown_task;
185 while(n_tls_shutdown_task != 0) { nanosleep(&s,
nullptr); }
193 if(active_tlsdata != 0 and not tls_shutdown_timeout) {
194 ++n_tls_shutdown_task;
199 tbb::task::enqueue(*
task);
207 while(active_tlsdata.load() != 0 and ++count < 1000) { nanosleep(&s,
nullptr);}
209 tls_shutdown_timeout=
true;
212 --n_tls_shutdown_task;
220 if(
m_tls) {
return; }
228 int thisID = getThreadIndex();
232 throw edm::Exception(
edm::errors::Configuration) <<
"SimActivityRegistry service (i.e. visualization) is not supported for more than 1 thread. If this use case is needed, RunManagerMTWorker has to be updated.";
244 int thisID = getThreadIndex();
247 <<
"RunManagerMTWorker::initializeThread " << thisID;
250 G4Threading::G4SetThreadId( thisID );
251 G4UImanager::GetUIpointer()->SetUpForAThread( thisID );
253 if(uitype ==
"MessageLogger") {
256 else if(uitype ==
"MessageLoggerThreadPrefix") {
259 else if(uitype ==
"FilePerThread") {
264 <<
"Invalid value of CustomUIsession.Type '" << uitype
265 <<
"', valid are MessageLogger, MessageLoggerThreadPrefix, FilePerThread";
269 G4WorkerThread::BuildGeometryAndPhysicsVector();
272 m_tls->
kernel.reset(G4WorkerRunManagerKernel::GetRunManagerKernel());
276 G4StateManager::GetStateManager()->SetExceptionHandler(
new ExceptionHandler());
299 G4TransportationManager * tM =
300 G4TransportationManager::GetTransportationManager();
301 tM->SetFieldManager(fieldManager);
302 fieldBuilder.build( fieldManager, tM->GetPropagatorInField());
316 <<
" RunManagerMTWorker: Sensitive Detector building finished; found " 324 <<
"RunManagerMTWorker: start initialisation of PhysicsList for the thread";
328 G4cout <<
"RunManagerMTWorker: Requested UI commands: " << G4endl;
331 G4UImanager::GetUIpointer()->ApplyCommand(
command);
334 G4StateManager::GetStateManager()->SetNewState(G4State_Init);
336 physicsList->InitializeWorker();
340 const bool kernelInit =
m_tls->
kernel->RunInitialization();
343 <<
"RunManagerMTWorker: Geant4 kernel initialization failed";
351 std::vector<int> ve =
m_p.
getParameter<std::vector<int> >(
"VerboseEvents");
352 std::vector<int> vn =
m_p.
getParameter<std::vector<int> >(
"VertexNumber");
353 std::vector<int> vt =
m_p.
getParameter<std::vector<int> >(
"VerboseTracks");
361 <<
"RunManagerMTWorker::initializeThread done for the thread " << thisID;
363 G4StateManager::GetStateManager()->SetNewState(G4State_Idle);
373 G4EventManager * eventManager =
m_tls->
kernel->GetEventManager();
380 eventManager->SetUserAction(userEventAction);
385 eventManager->SetUserAction(userTrackingAction);
390 eventManager->SetUserAction(userSteppingAction);
392 eventManager->SetUserAction(
new StackingAction(userTrackingAction,
439 G4StateManager::GetStateManager()->SetNewState(G4State_GeomClosed);
459 std::unique_ptr<G4SimEvent>
473 <<
"RunManagerMTWorker::produce(): stream " 474 << inpevt.
streamID() <<
" thread " << getThreadIndex() <<
" initializing";
491 auto simEvent =std::make_unique<G4SimEvent>();
497 m_simEvent->collisionPoint(
499 genVertex->y()/centimeter,
500 genVertex->z()/centimeter,
504 std::stringstream ss;
505 ss <<
"RunManagerMTWorker::produce(): event " << inpevt.
id().
event()
506 <<
" with no G4PrimaryVertices \n" ;
511 std::stringstream ss;
512 ss <<
" RunManagerMT::produce(): " 513 <<
" no G4WorkerRunManagerKernel yet for thread index" 514 << getThreadIndex() <<
", id " << std::hex
515 << std::this_thread::get_id() <<
" \n";
520 <<
" RunManagerMTWorker::produce: start Event " << inpevt.
id().
event()
521 <<
" stream id " << inpevt.
streamID()
522 <<
" thread index " << getThreadIndex()
523 <<
" of weight " << m_simEvent->weight()
524 <<
" with " << m_simEvent->nTracks() <<
" tracks and " 525 << m_simEvent->nVertices()
526 <<
" vertices, generated by " << m_simEvent->nGenParts() <<
" particles ";
531 <<
" RunManagerMTWorker::produce: ended Event " << inpevt.
id().
event();
547 G4Track*
t =
m_tls->
kernel->GetEventManager()->GetTrackingManager()->GetTrack();
548 t->SetTrackStatus(fStopAndKill) ;
557 m_tls->
kernel->GetEventManager()->GetStackManager()->clear();
558 m_tls->
kernel->GetEventManager()->GetTrackingManager()->EventAborted();
573 G4Event * evt =
new G4Event(evtid);
edm::ParameterSet m_pSteppingAction
T getParameter(std::string const &) const
EventNumber_t event() const
std::vector< std::shared_ptr< SimWatcher > > watchers
const SensitiveDetectorCatalog & catalog() const
T getUntrackedParameter(std::string const &, T const &) const
virtual const math::XYZTLorentzVector * genVertex() const
std::unique_ptr< G4Event > currentEvent
virtual const double eventWeight() const
ROOT::Math::LorentzVector< ROOT::Math::PxPyPzE4D< double > > XYZTLorentzVectorD
Lorentz vector with cylindrical internal representation using pseudorapidity.
void initializeUserActions()
def create(alignables, pedeDump, additionalData, outputFile, config)
virtual const HepMC::GenEvent * genEvent() const
SimActivityRegistry::G4StepSignal m_g4StepSignal
SimTrackManager * GetSimTrackManager()
bool getByToken(EDGetToken token, Handle< PROD > &result) const
FunctorTask< F > * make_functor_task(ALLOC &&iAlloc, F f)
void HepMC2G4(const HepMC::GenEvent *g, G4Event *e)
std::unique_ptr< SimRunInterface > runInterface
std::unique_ptr< CMSSteppingVerbose > m_sVerbose
SimActivityRegistry::EndOfRunSignal m_endOfRunSignal
bool exists(std::string const ¶meterName) const
checks if a parameter exists
edm::RunNumber_t currentRunNumber
std::vector< SensitiveTkDetector * > sensTkDets
SimActivityRegistry::EndOfEventSignal m_endOfEventSignal
static void createWatchers(const edm::ParameterSet &iP, SimActivityRegistry &iReg, std::vector< std::shared_ptr< SimWatcher > > &oWatchers, std::vector< std::shared_ptr< SimProducer > > &oProds)
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::unique_ptr< G4RunManagerKernel > kernel
U second(std::pair< T, U > const &p)
std::unique_ptr< SimActivityRegistry > registry
edm::ParameterSet m_pEventAction
const DDDWorld & world() const
RunAction
list of unwanted particles (gluons and quarks)
std::unique_ptr< SimTrackManager > trackManager
std::unique_ptr< CustomUIsession > UIsession
static thread_local TLSData * m_tls
edm::ParameterSet m_pRunAction
std::vector< SensitiveCaloDetector * > sensCaloDets
std::vector< SensitiveTkDetector * > & sensTkDetectors()
void resetGenParticleId(const edm::Event &inpevt)
std::unique_ptr< G4SimEvent > produce(const edm::Event &inpevt, const edm::EventSetup &es, RunManagerMT &runManagerMaster)
std::unique_ptr< RunAction > userRunAction
void initializeThread(RunManagerMT &runManagerMaster, const edm::EventSetup &es)
void hepEvent(const HepMC::GenEvent *r)
void setGenEvent(const HepMC::GenEvent *inpevt)
SimActivityRegistry::EndOfTrackSignal m_endOfTrackSignal
std::vector< std::shared_ptr< SimProducer > > producers
void abortRun(bool softAbort=false)
RunManagerMTWorker(const edm::ParameterSet &iConfig, edm::ConsumesCollector &&i)
void connect(Observer< const T * > *iObs)
does not take ownership of memory
PhysicsList * physicsListForWorker() const
const HepMC::GenEvent * GetEvent() const
std::pair< std::vector< SensitiveTkDetector * >, std::vector< SensitiveCaloDetector * > > create(const DDCompactView &, const SensitiveDetectorCatalog &, edm::ParameterSet const &, const SimTrackManager *, SimActivityRegistry ®) const
T const * product() const
std::vector< SensitiveCaloDetector * > & sensCaloDetectors()
edm::ParameterSet m_pTrackingAction
edm::ParameterSet m_pField
G4VPhysicalVolume * GetWorldVolumeForWorker() const
G4Event * generateEvent(const edm::Event &inpevt)
edm::EDGetTokenT< edm::LHCTransportLinkContainer > m_theLHCTlinkToken
void nonBeamEvent2G4(const HepMC::GenEvent *g, G4Event *e)
static void WorkerSetAsWorld(G4VPhysicalVolume *pv)
SimActivityRegistry::BeginOfRunSignal m_beginOfRunSignal
const std::vector< std::string > & G4Commands() const
edm::ParameterSet m_pCustomUIsession
std::vector< std::shared_ptr< SimProducer > > producers()
StreamID streamID() const
std::vector< LHCTransportLink > LHCTransportLinkContainer
void PostUserTrackingAction(const G4Track *aTrack) override
T const * product() const
SimActivityRegistry::BeginOfEventSignal m_beginOfEventSignal
T get(const Candidate &c)
edm::ParameterSet m_pStackingAction
void Connect(RunAction *)
SimActivityRegistry::BeginOfTrackSignal m_beginOfTrackSignal