46 #include "G4SystemOfUnits.hh"
47 #include "G4Threading.hh"
48 #include "G4UImanager.hh"
49 #include "G4WorkerThread.hh"
50 #include "G4WorkerRunManagerKernel.hh"
51 #include "G4StateManager.hh"
52 #include "G4TransportationManager.hh"
61 static std::atomic<int> thread_counter{ 0 };
63 int get_new_thread_index() {
64 return thread_counter++;
67 static thread_local
int s_thread_index = get_new_thread_index();
69 int getThreadIndex() {
return s_thread_index; }
73 std::vector<std::shared_ptr<SimWatcher> >& oWatchers,
74 std::vector<std::shared_ptr<SimProducer> >& oProds,
83 vector<ParameterSet> watchers = iP.
getParameter<vector<ParameterSet> >(
"Watchers");
85 if(!watchers.empty() && thisThreadID > 0) {
86 throw cms::Exception(
"Unsupported") <<
"SimWatchers are not supported for more than 1 thread. If this use case is needed, RunManagerMTWorker has to be updated, and SimWatchers and SimProducers have to be made thread safe.";
89 for(vector<ParameterSet>::iterator itWatcher = watchers.begin();
90 itWatcher != watchers.end();
92 std::unique_ptr<SimWatcherMakerBase> maker(
99 std::shared_ptr<SimWatcher> watcherTemp;
100 std::shared_ptr<SimProducer> producerTemp;
101 maker->make(*itWatcher,iReg,watcherTemp,producerTemp);
102 oWatchers.push_back(watcherTemp);
104 oProds.push_back(producerTemp);
118 std::vector<std::shared_ptr<SimWatcher> >
watchers;
131 m_generator(iConfig.getParameter<edm::
ParameterSet>(
"Generator")),
134 m_nonBeam(iConfig.getParameter<bool>(
"NonBeamEvent")),
135 m_pUseMagneticField(iConfig.getParameter<bool>(
"UseMagneticField")),
136 m_EvtMgrVerbosity(iConfig.getUntrackedParameter<int>(
"G4EventManagerVerbosity",0)),
137 m_pField(iConfig.getParameter<edm::
ParameterSet>(
"MagneticField")),
138 m_pRunAction(iConfig.getParameter<edm::
ParameterSet>(
"RunAction")),
139 m_pEventAction(iConfig.getParameter<edm::
ParameterSet>(
"EventAction")),
140 m_pStackingAction(iConfig.getParameter<edm::
ParameterSet>(
"StackingAction")),
141 m_pTrackingAction(iConfig.getParameter<edm::
ParameterSet>(
"TrackingAction")),
142 m_pSteppingAction(iConfig.getParameter<edm::
ParameterSet>(
"SteppingAction")),
143 m_pCustomUIsession(iConfig.getUntrackedParameter<edm::
ParameterSet>(
"CustomUIsession")),
168 int thisID = getThreadIndex();
172 throw cms::Exception(
"Unsupported") <<
"SimActivityRegistry service (i.e. visualization) is not supported for more than 1 thread. If this use case is needed, RunManagerMTWorker has to be updated.";
183 int thisID = getThreadIndex();
186 G4Threading::G4SetThreadId( thisID );
187 G4UImanager::GetUIpointer()->SetUpForAThread( thisID );
189 if(uitype ==
"MessageLogger") {
192 else if(uitype ==
"MessageLoggerThreadPrefix") {
195 else if(uitype ==
"FilePerThread") {
199 throw cms::Exception(
"Configuration") <<
"Invalid value of CustomUIsession.Type '" << uitype <<
"', valid are MessageLogger, MessageLoggerThreadPrefix, FilePerThread";
203 G4WorkerThread::BuildGeometryAndPhysicsVector();
206 G4RunManagerKernel *kernel = G4WorkerRunManagerKernel::GetRunManagerKernel();
207 if(!kernel) kernel =
new G4WorkerRunManagerKernel();
229 G4TransportationManager * tM =
230 G4TransportationManager::GetTransportationManager();
232 tM->GetPropagatorInField(),
239 std::pair< std::vector<SensitiveTkDetector*>,
240 std::vector<SensitiveCaloDetector*> > sensDets =
252 <<
" RunManagerMTWorker: Sensitive Detector "
253 <<
"building finished; found "
255 <<
" Tk type Producers, and "
257 <<
" Calo type producers ";
263 <<
"RunManagerMTWorker: start initialisation of PhysicsList for a thread";
265 physicsList->InitializeWorker();
266 kernel->SetPhysics(physicsList);
267 kernel->InitializePhysics();
269 const bool kernelInit = kernel->RunInitialization();
271 throw SimG4Exception(
"G4WorkerRunManagerKernel initialization failed");
293 G4RunManagerKernel *kernel = G4WorkerRunManagerKernel::GetRunManagerKernel();
294 G4EventManager * eventManager = kernel->GetEventManager();
300 eventManager->SetUserAction(userEventAction);
305 eventManager->SetUserAction(userTrackingAction);
310 eventManager->SetUserAction(userSteppingAction);
312 eventManager->SetUserAction(
new StackingAction(userTrackingAction,
360 G4StateManager::GetStateManager()->SetNewState(G4State_GeomClosed);
370 G4RunManagerKernel *kernel = G4WorkerRunManagerKernel::GetRunManagerKernel();
374 kernel->RunTermination();
389 LogDebug(
"SimG4CoreApplication") <<
"RunManagerMTWorker::produce(): stream " << inpevt.
streamID() <<
" thread " << getThreadIndex() <<
" initializing";
414 genVertex->y()/centimeter,
415 genVertex->z()/centimeter,
420 <<
" RunManagerMT::produce event " << inpevt.
id().
event()
421 <<
" with no G4PrimaryVertices \n Aborting Run" ;
425 G4RunManagerKernel *kernel = G4WorkerRunManagerKernel::GetRunManagerKernel();
427 std::stringstream
ss;
428 ss <<
"No G4WorkerRunManagerKernel yet for thread index" << getThreadIndex() <<
", id " << std::hex << std::this_thread::get_id();
435 <<
" RunManagerMTWorker: saved : Event " << inpevt.
id().
event()
436 <<
" stream id " << inpevt.
streamID()
437 <<
" thread index " << getThreadIndex()
439 <<
" with " <<
m_simEvent->nTracks() <<
" tracks and "
441 <<
" vertices, generated by " <<
m_simEvent->nGenParts() <<
" particles ";
446 G4RunManagerKernel *kernel = G4WorkerRunManagerKernel::GetRunManagerKernel();
447 G4Track*
t = kernel->GetEventManager()->GetTrackingManager()->GetTrack();
448 t->SetTrackStatus(fStopAndKill) ;
453 static_cast<TrackingAction *
>(kernel->GetEventManager()->GetUserTrackingAction());
469 kernel->GetEventManager()->GetStackManager()->clear() ;
470 kernel->GetEventManager()->GetTrackingManager()->EventAborted() ;
472 G4StateManager* stateManager = G4StateManager::GetStateManager();
473 stateManager->SetNewState(G4State_GeomClosed);
487 G4int evtid = (G4int)inpevt.
id().
event();
488 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()
G4StepSignal g4StepSignal_
virtual const HepMC::GenEvent * genEvent() const
SimActivityRegistry::G4StepSignal m_g4StepSignal
SimTrackManager * GetSimTrackManager()
bool getByToken(EDGetToken token, Handle< PROD > &result) const
void connect(Observer< const BeginOfJob * > *iObject)
void HepMC2G4(const HepMC::GenEvent *g, G4Event *e)
EndOfEventSignal endOfEventSignal_
std::unique_ptr< SimRunInterface > runInterface
SimActivityRegistry::EndOfRunSignal m_endOfRunSignal
bool exists(std::string const ¶meterName) const
checks if a parameter exists
edm::RunNumber_t currentRunNumber
BeginOfRunSignal beginOfRunSignal_
std::vector< SensitiveTkDetector * > sensTkDets
std::unique_ptr< sim::FieldBuilder > fieldBuilder
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
const bool m_pUseMagneticField
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
BeginOfJobSignal beginOfJobSignal_
U second(std::pair< T, U > const &p)
edm::ParameterSet m_pEventAction
const DDDWorld & world() const
std::unique_ptr< SimTrackManager > trackManager
std::unique_ptr< CustomUIsession > UIsession
std::unique_ptr< G4SimEvent > m_simEvent
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< RunAction > userRunAction
void setGenEvent(const HepMC::GenEvent *inpevt)
BeginOfEventSignal beginOfEventSignal_
sim::ChordFinderSetter * chordFinderSetterForWorker() const
SimActivityRegistry::EndOfTrackSignal m_endOfTrackSignal
EndOfRunSignal endOfRunSignal_
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
T const * product() const
virtual void PostUserTrackingAction(const G4Track *aTrack)
std::vector< SensitiveCaloDetector * > & sensCaloDetectors()
SimActivityRegistry registry
edm::ParameterSet m_pTrackingAction
std::unique_ptr< G4Run > currentRun
edm::ParameterSet m_pField
T const * product() const
std::pair< std::vector< SensitiveTkDetector * >, std::vector< SensitiveCaloDetector * > > create(const DDDWorld &w, const DDCompactView &cpv, const SensitiveDetectorCatalog &clg, edm::ParameterSet const &p, const SimTrackManager *m, SimActivityRegistry ®) const
G4VPhysicalVolume * GetWorldVolumeForWorker() const
G4Event * generateEvent(const edm::Event &inpevt)
edm::EDGetTokenT< edm::LHCTransportLinkContainer > m_theLHCTlinkToken
void nonBeamEvent2G4(const HepMC::GenEvent *g, G4Event *e)
void initializeThread(const RunManagerMT &runManagerMaster, const edm::EventSetup &es)
static void WorkerSetAsWorld(G4VPhysicalVolume *pv)
SimActivityRegistry::BeginOfRunSignal m_beginOfRunSignal
edm::ParameterSet m_pCustomUIsession
std::vector< std::shared_ptr< SimProducer > > producers()
StreamID streamID() const
std::vector< LHCTransportLink > LHCTransportLinkContainer
EndOfTrackSignal endOfTrackSignal_
const int m_EvtMgrVerbosity
SimActivityRegistry::BeginOfEventSignal m_beginOfEventSignal
T get(const Candidate &c)
BeginOfTrackSignal beginOfTrackSignal_
edm::ParameterSet m_pStackingAction
void Connect(RunAction *)
void produce(const edm::Event &inpevt, const edm::EventSetup &es, const RunManagerMT &runManagerMaster)
SimActivityRegistry::BeginOfTrackSignal m_beginOfTrackSignal