52 #include "G4SystemOfUnits.hh" 53 #include "G4Threading.hh" 54 #include "G4UImanager.hh" 55 #include "G4WorkerThread.hh" 56 #include "G4WorkerRunManagerKernel.hh" 57 #include "G4StateManager.hh" 58 #include "G4TransportationManager.hh" 67 std::atomic<int> thread_counter{ 0 };
69 int get_new_thread_index() {
70 return thread_counter++;
73 thread_local
int s_thread_index = get_new_thread_index();
75 int getThreadIndex() {
return s_thread_index; }
79 std::vector<std::shared_ptr<SimWatcher> >& oWatchers,
80 std::vector<std::shared_ptr<SimProducer> >& oProds,
84 if(!iP.
exists(
"Watchers")) {
return; }
86 std::vector<edm::ParameterSet> watchers =
87 iP.
getParameter<std::vector<edm::ParameterSet> >(
"Watchers");
89 for(
auto & watcher : watchers) {
90 std::unique_ptr<SimWatcherMakerBase> maker(
95 <<
"Unable to find the requested Watcher <" 98 std::shared_ptr<SimWatcher> watcherTemp;
99 std::shared_ptr<SimProducer> producerTemp;
100 maker->make(watcher,*(iReg),watcherTemp,producerTemp);
101 oWatchers.push_back(watcherTemp);
103 oProds.push_back(producerTemp);
117 std::vector<std::shared_ptr<SimWatcher> >
watchers;
121 std::unique_ptr<G4RunManagerKernel>
kernel;
148 std::vector<edm::ParameterSet> watchers =
149 iConfig.
getParameter<std::vector<edm::ParameterSet> >(
"Watchers");
162 if(
m_tls) {
return; }
169 int thisID = getThreadIndex();
173 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.";
185 int thisID = getThreadIndex();
188 <<
"RunManagerMTWorker::initializeThread " << thisID;
191 G4Threading::G4SetThreadId( thisID );
192 G4UImanager::GetUIpointer()->SetUpForAThread( thisID );
194 if(uitype ==
"MessageLogger") {
197 else if(uitype ==
"MessageLoggerThreadPrefix") {
200 else if(uitype ==
"FilePerThread") {
205 <<
"Invalid value of CustomUIsession.Type '" << uitype
206 <<
"', valid are MessageLogger, MessageLoggerThreadPrefix, FilePerThread";
210 G4WorkerThread::BuildGeometryAndPhysicsVector();
213 m_tls->
kernel.reset(G4WorkerRunManagerKernel::GetRunManagerKernel());
217 G4StateManager::GetStateManager()->SetExceptionHandler(
new ExceptionHandler());
240 G4TransportationManager * tM =
241 G4TransportationManager::GetTransportationManager();
242 tM->SetFieldManager(fieldManager);
243 fieldBuilder.build( fieldManager, tM->GetPropagatorInField());
248 std::pair< std::vector<SensitiveTkDetector*>,
249 std::vector<SensitiveCaloDetector*> > sensDets =
258 <<
" RunManagerMTWorker: Sensitive Detector building finished; found " 266 <<
"RunManagerMTWorker: start initialisation of PhysicsList for the thread";
270 G4cout <<
"RunManagerMTWorker: Requested UI commands: " << G4endl;
273 G4UImanager::GetUIpointer()->ApplyCommand(
command);
276 G4StateManager::GetStateManager()->SetNewState(G4State_Init);
278 physicsList->InitializeWorker();
282 const bool kernelInit =
m_tls->
kernel->RunInitialization();
285 <<
"RunManagerMTWorker: Geant4 kernel initialization failed";
293 std::vector<int> ve =
m_p.
getParameter<std::vector<int> >(
"VerboseEvents");
294 std::vector<int> vn =
m_p.
getParameter<std::vector<int> >(
"VertexNumber");
295 std::vector<int> vt =
m_p.
getParameter<std::vector<int> >(
"VerboseTracks");
303 <<
"RunManagerMTWorker::initializeThread done for the thread " << thisID;
305 G4StateManager::GetStateManager()->SetNewState(G4State_Idle);
315 G4EventManager * eventManager =
m_tls->
kernel->GetEventManager();
322 eventManager->SetUserAction(userEventAction);
327 eventManager->SetUserAction(userTrackingAction);
332 eventManager->SetUserAction(userSteppingAction);
334 eventManager->SetUserAction(
new StackingAction(userTrackingAction,
381 G4StateManager::GetStateManager()->SetNewState(G4State_GeomClosed);
414 <<
"RunManagerMTWorker::produce(): stream " 415 << inpevt.
streamID() <<
" thread " << getThreadIndex() <<
" initializing";
439 genVertex->y()/centimeter,
440 genVertex->z()/centimeter,
444 std::stringstream ss;
445 ss <<
"RunManagerMTWorker::produce(): event " << inpevt.
id().
event()
446 <<
" with no G4PrimaryVertices \n" ;
451 std::stringstream ss;
452 ss <<
" RunManagerMT::produce(): " 453 <<
" no G4WorkerRunManagerKernel yet for thread index" 454 << getThreadIndex() <<
", id " << std::hex
455 << std::this_thread::get_id() <<
" \n";
460 <<
" RunManagerMTWorker::produce: start Event " << inpevt.
id().
event()
461 <<
" stream id " << inpevt.
streamID()
462 <<
" thread index " << getThreadIndex()
464 <<
" with " <<
m_simEvent->nTracks() <<
" tracks and " 466 <<
" vertices, generated by " <<
m_simEvent->nGenParts() <<
" particles ";
471 <<
" RunManagerMTWorker::produce: ended Event " << inpevt.
id().
event();
477 G4Track*
t =
m_tls->
kernel->GetEventManager()->GetTrackingManager()->GetTrack();
478 t->SetTrackStatus(fStopAndKill) ;
487 m_tls->
kernel->GetEventManager()->GetStackManager()->clear();
488 m_tls->
kernel->GetEventManager()->GetTrackingManager()->EventAborted();
503 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
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
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)
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
void produce(const edm::Event &inpevt, const edm::EventSetup &es, RunManagerMT &runManagerMaster)
std::unique_ptr< G4Run > currentRun
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