50 #include "G4SystemOfUnits.hh" 51 #include "G4Threading.hh" 52 #include "G4UImanager.hh" 53 #include "G4WorkerThread.hh" 54 #include "G4WorkerRunManagerKernel.hh" 55 #include "G4StateManager.hh" 56 #include "G4TransportationManager.hh" 65 std::atomic<int> thread_counter{ 0 };
67 int get_new_thread_index() {
68 return thread_counter++;
71 thread_local
int s_thread_index = get_new_thread_index();
73 int getThreadIndex() {
return s_thread_index; }
77 std::vector<std::shared_ptr<SimWatcher> >& oWatchers,
78 std::vector<std::shared_ptr<SimProducer> >& oProds,
84 if(!iP.
exists(
"Watchers")) {
return; }
86 vector<ParameterSet> watchers = iP.
getParameter<vector<ParameterSet> >(
"Watchers");
88 if(thisThreadID > 0) {
89 throw edm::Exception(
edm::errors::Configuration) <<
"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.";
92 for(vector<ParameterSet>::iterator itWatcher = watchers.begin();
93 itWatcher != watchers.end();
95 std::unique_ptr<SimWatcherMakerBase> maker(
98 if(maker.get()==
nullptr) {
100 <<
"Unable to find the requested Watcher";
103 std::shared_ptr<SimWatcher> watcherTemp;
104 std::shared_ptr<SimProducer> producerTemp;
105 maker->make(*itWatcher,iReg,watcherTemp,producerTemp);
106 oWatchers.push_back(watcherTemp);
108 oProds.push_back(producerTemp);
122 std::vector<std::shared_ptr<SimWatcher> >
watchers;
127 G4RunManagerKernel* kernel =
nullptr;
128 bool threadInitialized =
false;
129 bool runTerminated =
false;
138 m_nonBeam(iConfig.getParameter<bool>(
"NonBeamEvent")),
139 m_pUseMagneticField(iConfig.getParameter<bool>(
"UseMagneticField")),
140 m_EvtMgrVerbosity(iConfig.getUntrackedParameter<
int>(
"G4EventManagerVerbosity",0)),
143 m_pEventAction(iConfig.getParameter<
edm::
ParameterSet>(
"EventAction")),
144 m_pStackingAction(iConfig.getParameter<
edm::
ParameterSet>(
"StackingAction")),
145 m_pTrackingAction(iConfig.getParameter<
edm::
ParameterSet>(
"TrackingAction")),
146 m_pSteppingAction(iConfig.getParameter<
edm::
ParameterSet>(
"SteppingAction")),
147 m_pCustomUIsession(iConfig.getUntrackedParameter<
edm::
ParameterSet>(
"CustomUIsession")),
152 std::vector<edm::ParameterSet> watchers =
153 iConfig.
getParameter<std::vector<edm::ParameterSet> >(
"Watchers");
166 if(
m_tls) {
return; }
172 int thisID = getThreadIndex();
176 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.";
188 int thisID = getThreadIndex();
191 <<
"RunManagerMTWorker::initializeThread " << thisID;
194 G4Threading::G4SetThreadId( thisID );
195 G4UImanager::GetUIpointer()->SetUpForAThread( thisID );
197 if(uitype ==
"MessageLogger") {
200 else if(uitype ==
"MessageLoggerThreadPrefix") {
203 else if(uitype ==
"FilePerThread") {
208 <<
"Invalid value of CustomUIsession.Type '" << uitype
209 <<
"', valid are MessageLogger, MessageLoggerThreadPrefix, FilePerThread";
213 G4WorkerThread::BuildGeometryAndPhysicsVector();
216 m_tls->
kernel = G4WorkerRunManagerKernel::GetRunManagerKernel();
220 G4StateManager::GetStateManager()->SetExceptionHandler(
new ExceptionHandler());
243 G4TransportationManager * tM =
244 G4TransportationManager::GetTransportationManager();
245 tM->SetFieldManager(fieldManager);
246 fieldBuilder.build( fieldManager, tM->GetPropagatorInField());
251 std::pair< std::vector<SensitiveTkDetector*>,
252 std::vector<SensitiveCaloDetector*> > sensDets =
264 <<
" RunManagerMTWorker: Sensitive Detector " 265 <<
"building finished; found " 267 <<
" Tk type Producers, and " 269 <<
" Calo type producers ";
275 <<
"RunManagerMTWorker: start initialisation of PhysicsList for a thread";
277 physicsList->InitializeWorker();
281 const bool kernelInit =
m_tls->
kernel->RunInitialization();
283 throw SimG4Exception(
"G4WorkerRunManagerKernel initialization failed");
291 std::vector<int> ve =
m_p.
getParameter<std::vector<int> >(
"VerboseEvents");
292 std::vector<int> vn =
m_p.
getParameter<std::vector<int> >(
"VertexNumber");
293 std::vector<int> vt =
m_p.
getParameter<std::vector<int> >(
"VerboseTracks");
301 <<
"RunManagerMTWorker::initializeThread done for the thread " << thisID;
304 edm::LogInfo(
"SimG4CoreApplication") <<
"RunManagerMTWorker:: Requests UI: " 306 G4UImanager::GetUIpointer()->ApplyCommand(
command);
317 G4EventManager * eventManager =
m_tls->
kernel->GetEventManager();
324 eventManager->SetUserAction(userEventAction);
329 eventManager->SetUserAction(userTrackingAction);
334 eventManager->SetUserAction(userSteppingAction);
336 eventManager->SetUserAction(
new StackingAction(userTrackingAction,
383 G4StateManager::GetStateManager()->SetNewState(G4State_GeomClosed);
416 <<
"RunManagerMTWorker::produce(): stream " 417 << inpevt.
streamID() <<
" thread " << getThreadIndex() <<
" initializing";
441 genVertex->y()/centimeter,
442 genVertex->z()/centimeter,
446 std::stringstream ss;
447 ss <<
"RunManagerMTWorker::produce(): event " << inpevt.
id().
event()
448 <<
" with no G4PrimaryVertices \n" ;
453 std::stringstream ss;
454 ss <<
" RunManagerMT::produce(): " 455 <<
" no G4WorkerRunManagerKernel yet for thread index" 456 << getThreadIndex() <<
", id " << std::hex
457 << std::this_thread::get_id() <<
" \n";
462 <<
" RunManagerMTWorker::produce: start Event " << inpevt.
id().
event()
463 <<
" stream id " << inpevt.
streamID()
464 <<
" thread index " << getThreadIndex()
466 <<
" with " <<
m_simEvent->nTracks() <<
" tracks and " 468 <<
" vertices, generated by " <<
m_simEvent->nGenParts() <<
" particles ";
473 <<
" RunManagerMTWorker::produce: ended Event " << inpevt.
id().
event();
479 G4Track*
t =
m_tls->
kernel->GetEventManager()->GetTrackingManager()->GetTrack();
480 t->SetTrackStatus(fStopAndKill) ;
489 m_tls->
kernel->GetEventManager()->GetStackManager()->clear();
490 m_tls->
kernel->GetEventManager()->GetTrackingManager()->EventAborted();
505 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)
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
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
BeginOfRunSignal beginOfRunSignal_
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
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 initializeThread(RunManagerMT &runManagerMaster, const edm::EventSetup &es)
void setGenEvent(const HepMC::GenEvent *inpevt)
BeginOfEventSignal beginOfEventSignal_
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
const HepMC::GenEvent * GetEvent() const
T const * product() const
virtual void PostUserTrackingAction(const G4Track *aTrack)
std::vector< SensitiveCaloDetector * > & sensCaloDetectors()
SimActivityRegistry registry
edm::ParameterSet m_pTrackingAction
void produce(const edm::Event &inpevt, const edm::EventSetup &es, RunManagerMT &runManagerMaster)
std::unique_ptr< G4Run > currentRun
edm::ParameterSet m_pField
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)
G4RunManagerKernel * kernel
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
EndOfTrackSignal endOfTrackSignal_
T const * product() const
SimActivityRegistry::BeginOfEventSignal m_beginOfEventSignal
T get(const Candidate &c)
BeginOfTrackSignal beginOfTrackSignal_
edm::ParameterSet m_pStackingAction
void Connect(RunAction *)
SimActivityRegistry::BeginOfTrackSignal m_beginOfTrackSignal