CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
List of all members | Classes | Public Member Functions | Private Member Functions | Private Attributes
RunManagerMTWorker Class Reference

#include <RunManagerMTWorker.h>

Classes

struct  TLSData
 

Public Member Functions

void abortEvent ()
 
void abortRun (bool softAbort=false)
 
void beginRun (const edm::EventSetup &)
 
void Connect (RunAction *)
 
void Connect (EventAction *)
 
void Connect (TrackingAction *)
 
void Connect (SteppingAction *)
 
void endRun ()
 
SimTrackManagerGetSimTrackManager ()
 
void initializeG4 (RunManagerMT *runManagerMaster, const edm::EventSetup &es)
 
std::unique_ptr< G4SimEventproduce (const edm::Event &inpevt, const edm::EventSetup &es, RunManagerMT &runManagerMaster)
 
std::vector< std::shared_ptr
< SimProducer > > & 
producers ()
 
 RunManagerMTWorker (const edm::ParameterSet &iConfig, edm::ConsumesCollector &&iC)
 
std::vector
< SensitiveCaloDetector * > & 
sensCaloDetectors ()
 
std::vector
< SensitiveTkDetector * > & 
sensTkDetectors ()
 
G4SimEventsimEvent ()
 
 ~RunManagerMTWorker ()
 

Private Member Functions

void DumpMagneticField (const G4Field *, const std::string &) const
 
G4Event * generateEvent (const edm::Event &inpevt)
 
int getThreadIndex () const
 
void initializeRun ()
 
void initializeTLS ()
 
void initializeUserActions ()
 
void resetGenParticleId (const edm::Event &inpevt)
 
void terminateRun ()
 

Private Attributes

bool m_dumpMF {false}
 
int m_EvtMgrVerbosity {0}
 
Generator m_generator
 
bool m_hasWatchers {false}
 
edm::EDGetTokenT
< edm::HepMCProduct
m_InToken
 
edm::EDGetTokenT
< edm::HepMCProduct
m_LHCToken
 
bool m_LHCTransport {false}
 
edm::ESGetToken< MagneticField,
IdealMagneticFieldRecord
m_MagField
 
bool m_nonBeam {false}
 
edm::ParameterSet m_p
 
edm::ParameterSet m_pCustomUIsession
 
edm::ParameterSet m_pEventAction
 
edm::ParameterSet m_pField
 
const MagneticFieldm_pMagField {nullptr}
 
edm::ParameterSet m_pRunAction
 
edm::ParameterSet m_pStackingAction
 
edm::ParameterSet m_pSteppingAction
 
edm::ParameterSet m_pTrackingAction
 
bool m_pUseMagneticField {true}
 
std::unordered_map
< std::string, std::unique_ptr
< SensitiveDetectorMakerBase > > 
m_sdMakers
 
G4SimEventm_simEvent {nullptr}
 
std::unique_ptr
< CMSSteppingVerbose
m_sVerbose
 
edm::EDGetTokenT
< edm::LHCTransportLinkContainer
m_theLHCTlinkToken
 
const int m_thread_index {-1}
 
TLSDatam_tls {nullptr}
 
CustomUIsessionm_UIsession {nullptr}
 

Detailed Description

Definition at line 48 of file RunManagerMTWorker.h.

Constructor & Destructor Documentation

RunManagerMTWorker::RunManagerMTWorker ( const edm::ParameterSet iConfig,
edm::ConsumesCollector &&  iC 
)
explicit

Definition at line 143 of file RunManagerMTWorker.cc.

144  : m_generator(iConfig.getParameter<edm::ParameterSet>("Generator")),
146  iConfig.getParameter<edm::ParameterSet>("Generator").getParameter<edm::InputTag>("HepMCProductLabel"))),
148  iC.consumes<edm::LHCTransportLinkContainer>(iConfig.getParameter<edm::InputTag>("theLHCTlinkTag"))),
149  m_nonBeam(iConfig.getParameter<bool>("NonBeamEvent")),
150  m_pUseMagneticField(iConfig.getParameter<bool>("UseMagneticField")),
151  m_LHCTransport(iConfig.getParameter<bool>("LHCTransport")),
152  m_thread_index{get_new_thread_index()},
153  m_EvtMgrVerbosity(iConfig.getUntrackedParameter<int>("G4EventManagerVerbosity", 0)),
154  m_pField(iConfig.getParameter<edm::ParameterSet>("MagneticField")),
155  m_pRunAction(iConfig.getParameter<edm::ParameterSet>("RunAction")),
156  m_pEventAction(iConfig.getParameter<edm::ParameterSet>("EventAction")),
157  m_pStackingAction(iConfig.getParameter<edm::ParameterSet>("StackingAction")),
158  m_pTrackingAction(iConfig.getParameter<edm::ParameterSet>("TrackingAction")),
159  m_pSteppingAction(iConfig.getParameter<edm::ParameterSet>("SteppingAction")),
160  m_pCustomUIsession(iConfig.getUntrackedParameter<edm::ParameterSet>("CustomUIsession")),
161  m_p(iConfig) {
162  int thisID = getThreadIndex();
163  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMTWorker for the thread " << thisID;
164 
165  // sensitive detectors
166  std::vector<std::string> onlySDs = iConfig.getParameter<std::vector<std::string>>("OnlySDs");
168 
169  // TLS and watchers
170  initializeTLS();
171  if (m_hasWatchers) {
172  for (auto& watcher : m_tls->watchers) {
173  watcher->registerConsumes(iC);
174  }
175  }
176 
177  if (m_LHCTransport) {
178  m_LHCToken = iC.consumes<edm::HepMCProduct>(edm::InputTag("LHCTransport"));
179  }
180  if (m_pUseMagneticField) {
182  }
183  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMTWorker is constructed for the thread " << thisID;
184  unsigned int k = 0;
185  for (std::unordered_map<std::string, std::unique_ptr<SensitiveDetectorMakerBase>>::const_iterator itr =
186  m_sdMakers.begin();
187  itr != m_sdMakers.end();
188  ++itr, ++k)
189  edm::LogVerbatim("SimG4CoreApplication") << "SD[" << k << "] " << itr->first;
190 }
edm::ParameterSet m_pSteppingAction
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
Log< level::Info, true > LogVerbatim
std::vector< std::shared_ptr< SimWatcher > > watchers
T getUntrackedParameter(std::string const &, T const &) const
edm::EDGetTokenT< edm::HepMCProduct > m_InToken
edm::EDGetTokenT< edm::HepMCProduct > m_LHCToken
edm::ParameterSet m_pEventAction
edm::ParameterSet m_pRunAction
std::unordered_map< std::string, std::unique_ptr< SensitiveDetectorMakerBase > > m_sdMakers
int getThreadIndex() const
edm::ParameterSet m_pTrackingAction
edm::ParameterSet m_pField
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
edm::EDGetTokenT< edm::LHCTransportLinkContainer > m_theLHCTlinkToken
edm::ParameterSet m_pCustomUIsession
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
edm::ParameterSet m_p
edm::ESGetToken< MagneticField, IdealMagneticFieldRecord > m_MagField
edm::ParameterSet m_pStackingAction
RunManagerMTWorker::~RunManagerMTWorker ( )

Definition at line 192 of file RunManagerMTWorker.cc.

References m_tls, and m_UIsession.

192  {
193  m_tls = nullptr;
194  delete m_UIsession;
195 }
CustomUIsession * m_UIsession

Member Function Documentation

void RunManagerMTWorker::abortEvent ( )

Definition at line 534 of file RunManagerMTWorker.cc.

References RunManagerMTWorker::TLSData::currentEvent, RunManagerMTWorker::TLSData::kernel, m_tls, TrackingAction::PostUserTrackingAction(), RunManagerMTWorker::TLSData::runTerminated, and submitPVValidationJobs::t.

Referenced by SimRunInterface::abortEvent(), and abortRun().

534  {
535  if (m_tls->runTerminated) {
536  return;
537  }
538  G4Track* t = m_tls->kernel->GetEventManager()->GetTrackingManager()->GetTrack();
539  t->SetTrackStatus(fStopAndKill);
540 
541  // CMS-specific act
542  //
543  TrackingAction* uta = static_cast<TrackingAction*>(m_tls->kernel->GetEventManager()->GetUserTrackingAction());
544  uta->PostUserTrackingAction(t);
545 
546  m_tls->currentEvent->SetEventAborted();
547  m_tls->kernel->GetEventManager()->GetStackManager()->clear();
548  m_tls->kernel->GetEventManager()->GetTrackingManager()->EventAborted();
549 }
std::unique_ptr< G4Event > currentEvent
std::unique_ptr< G4RunManagerKernel > kernel
void PostUserTrackingAction(const G4Track *aTrack) override
void RunManagerMTWorker::abortRun ( bool  softAbort = false)

Definition at line 551 of file RunManagerMTWorker.cc.

References abortEvent(), RunManagerMTWorker::TLSData::currentRun, m_tls, and terminateRun().

Referenced by SimRunInterface::abortRun().

551  {
552  if (!softAbort) {
553  abortEvent();
554  }
555  m_tls->currentRun = nullptr;
556  terminateRun();
557 }
void RunManagerMTWorker::beginRun ( const edm::EventSetup es)

Definition at line 197 of file RunManagerMTWorker.cc.

References edm::EventSetup::getData(), m_hasWatchers, m_MagField, m_pMagField, m_pUseMagneticField, m_tls, and RunManagerMTWorker::TLSData::watchers.

197  {
198  for (auto& maker : m_sdMakers) {
199  maker.second->beginRun(es);
200  }
201  if (m_pUseMagneticField) {
203  }
204  if (m_hasWatchers) {
205  for (auto& watcher : m_tls->watchers) {
206  watcher->beginRun(es);
207  }
208  }
209 }
std::vector< std::shared_ptr< SimWatcher > > watchers
bool getData(T &iHolder) const
Definition: EventSetup.h:122
std::unordered_map< std::string, std::unique_ptr< SensitiveDetectorMakerBase > > m_sdMakers
const MagneticField * m_pMagField
edm::ESGetToken< MagneticField, IdealMagneticFieldRecord > m_MagField
void RunManagerMTWorker::Connect ( RunAction runAction)

Definition at line 401 of file RunManagerMTWorker.cc.

References sim_act::Signaler< T >::connect(), RunAction::m_beginOfRunSignal, RunAction::m_endOfRunSignal, m_tls, and RunManagerMTWorker::TLSData::registry.

Referenced by SimRunInterface::Connect(), and initializeUserActions().

401  {
402  runAction->m_beginOfRunSignal.connect(m_tls->registry->beginOfRunSignal_);
403  runAction->m_endOfRunSignal.connect(m_tls->registry->endOfRunSignal_);
404 }
SimActivityRegistry::EndOfRunSignal m_endOfRunSignal
Definition: RunAction.h:24
std::unique_ptr< SimActivityRegistry > registry
void connect(Observer< const T * > *iObs)
does not take ownership of memory
Definition: Signaler.h:55
SimActivityRegistry::BeginOfRunSignal m_beginOfRunSignal
Definition: RunAction.h:23
void RunManagerMTWorker::Connect ( EventAction eventAction)

Definition at line 406 of file RunManagerMTWorker.cc.

References sim_act::Signaler< T >::connect(), EventAction::m_beginOfEventSignal, EventAction::m_endOfEventSignal, m_tls, and RunManagerMTWorker::TLSData::registry.

406  {
407  eventAction->m_beginOfEventSignal.connect(m_tls->registry->beginOfEventSignal_);
408  eventAction->m_endOfEventSignal.connect(m_tls->registry->endOfEventSignal_);
409 }
SimActivityRegistry::EndOfEventSignal m_endOfEventSignal
Definition: EventAction.h:47
std::unique_ptr< SimActivityRegistry > registry
void connect(Observer< const T * > *iObs)
does not take ownership of memory
Definition: Signaler.h:55
SimActivityRegistry::BeginOfEventSignal m_beginOfEventSignal
Definition: EventAction.h:46
void RunManagerMTWorker::Connect ( TrackingAction trackingAction)

Definition at line 411 of file RunManagerMTWorker.cc.

References sim_act::Signaler< T >::connect(), TrackingAction::m_beginOfTrackSignal, TrackingAction::m_endOfTrackSignal, m_tls, and RunManagerMTWorker::TLSData::registry.

411  {
412  trackingAction->m_beginOfTrackSignal.connect(m_tls->registry->beginOfTrackSignal_);
413  trackingAction->m_endOfTrackSignal.connect(m_tls->registry->endOfTrackSignal_);
414 }
std::unique_ptr< SimActivityRegistry > registry
SimActivityRegistry::EndOfTrackSignal m_endOfTrackSignal
void connect(Observer< const T * > *iObs)
does not take ownership of memory
Definition: Signaler.h:55
SimActivityRegistry::BeginOfTrackSignal m_beginOfTrackSignal
void RunManagerMTWorker::Connect ( SteppingAction steppingAction)

Definition at line 416 of file RunManagerMTWorker.cc.

References sim_act::Signaler< T >::connect(), SteppingAction::m_g4StepSignal, m_tls, and RunManagerMTWorker::TLSData::registry.

416  {
417  steppingAction->m_g4StepSignal.connect(m_tls->registry->g4StepSignal_);
418 }
SimActivityRegistry::G4StepSignal m_g4StepSignal
std::unique_ptr< SimActivityRegistry > registry
void connect(Observer< const T * > *iObs)
does not take ownership of memory
Definition: Signaler.h:55
void RunManagerMTWorker::DumpMagneticField ( const G4Field *  field,
const std::string &  file 
) const
private

Definition at line 599 of file RunManagerMTWorker.cc.

References funct::cos(), runTauDisplay::dr, PVValHelper::dz, groupFilesInBlocks::fout, mps_fire::i, dqmiolumiharvest::j, submitPVResolutionJobs::out, phi, point, alignCSCRings::r, funct::sin(), and z.

Referenced by initializeG4().

599  {
600  std::ofstream fout(file.c_str(), std::ios::out);
601  if (fout.fail()) {
602  edm::LogWarning("SimG4CoreApplication")
603  << "MTWorker::DumpMagneticField: error opening file <" << file << "> for magnetic field";
604  } else {
605  // CMS magnetic field volume
606  double rmax = 9000 * mm;
607  double zmax = 24000 * mm;
608 
609  double dr = 1 * cm;
610  double dz = 5 * cm;
611 
612  int nr = (int)(rmax / dr);
613  int nz = 2 * (int)(zmax / dz);
614 
615  double r = 0.0;
616  double z0 = -zmax;
617  double z;
618 
619  double phi = 0.0;
620  double cosf = cos(phi);
621  double sinf = sin(phi);
622 
623  double point[4] = {0.0, 0.0, 0.0, 0.0};
624  double bfield[3] = {0.0, 0.0, 0.0};
625 
626  fout << std::setprecision(6);
627  for (int i = 0; i <= nr; ++i) {
628  z = z0;
629  for (int j = 0; j <= nz; ++j) {
630  point[0] = r * cosf;
631  point[1] = r * sinf;
632  point[2] = z;
633  field->GetFieldValue(point, bfield);
634  fout << "R(mm)= " << r / mm << " phi(deg)= " << phi / degree << " Z(mm)= " << z / mm
635  << " Bz(tesla)= " << bfield[2] / tesla << " Br(tesla)= " << (bfield[0] * cosf + bfield[1] * sinf) / tesla
636  << " Bphi(tesla)= " << (bfield[0] * sinf - bfield[1] * cosf) / tesla << G4endl;
637  z += dz;
638  }
639  r += dr;
640  }
641 
642  fout.close();
643  }
644 }
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
Log< level::Warning, false > LogWarning
*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
Definition: invegas.h:5
void RunManagerMTWorker::endRun ( )

Definition at line 211 of file RunManagerMTWorker.cc.

References getThreadIndex(), and terminateRun().

211  {
212  int thisID = getThreadIndex();
213  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMTWorker::endRun for the thread " << thisID;
214  terminateRun();
215 }
Log< level::Info, true > LogVerbatim
int getThreadIndex() const
G4Event * RunManagerMTWorker::generateEvent ( const edm::Event inpevt)
private

Definition at line 559 of file RunManagerMTWorker.cc.

References RunManagerMTWorker::TLSData::currentEvent, edm::EventID::event(), runTauDisplay::evtid, edm::Event::getByToken(), Generator::HepMC2G4(), edm::EventBase::id(), m_generator, m_InToken, m_LHCToken, m_LHCTransport, m_nonBeam, m_simEvent, m_tls, Generator::nonCentralEvent2G4(), resetGenParticleId(), and Generator::setGenEvent().

Referenced by produce().

559  {
560  m_tls->currentEvent.reset();
561  m_simEvent = nullptr;
562 
563  // 64 bits event ID in CMSSW converted into Geant4 event ID
564  G4int evtid = (G4int)inpevt.id().event();
565  G4Event* evt = new G4Event(evtid);
566 
568  inpevt.getByToken(m_InToken, HepMCEvt);
569 
570  m_generator.setGenEvent(HepMCEvt->GetEvent());
571 
572  // required to reset the GenParticle Id for particles transported
573  // along the beam pipe
574  // to their original value for SimTrack creation
575  resetGenParticleId(inpevt);
576 
577  if (!m_nonBeam) {
578  m_generator.HepMC2G4(HepMCEvt->GetEvent(), evt);
579  if (m_LHCTransport) {
581  inpevt.getByToken(m_LHCToken, LHCMCEvt);
582  m_generator.nonCentralEvent2G4(LHCMCEvt->GetEvent(), evt);
583  }
584  } else {
585  m_generator.nonCentralEvent2G4(HepMCEvt->GetEvent(), evt);
586  }
587 
588  return evt;
589 }
EventNumber_t event() const
Definition: EventID.h:40
void nonCentralEvent2G4(const HepMC::GenEvent *g, G4Event *e)
Definition: Generator.cc:550
std::unique_ptr< G4Event > currentEvent
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:539
void HepMC2G4(const HepMC::GenEvent *g, G4Event *e)
Definition: Generator.cc:112
edm::EDGetTokenT< edm::HepMCProduct > m_InToken
edm::EDGetTokenT< edm::HepMCProduct > m_LHCToken
void resetGenParticleId(const edm::Event &inpevt)
void setGenEvent(const HepMC::GenEvent *inpevt)
Definition: Generator.h:24
edm::EventID id() const
Definition: EventBase.h:59
SimTrackManager * RunManagerMTWorker::GetSimTrackManager ( )

Definition at line 420 of file RunManagerMTWorker.cc.

References initializeTLS(), m_tls, and RunManagerMTWorker::TLSData::trackManager.

Referenced by SimRunInterface::SimRunInterface().

420  {
421  initializeTLS();
422  return m_tls->trackManager.get();
423 }
std::unique_ptr< SimTrackManager > trackManager
int RunManagerMTWorker::getThreadIndex ( ) const
inlineprivate

Definition at line 88 of file RunManagerMTWorker.h.

References m_thread_index.

Referenced by endRun(), initializeG4(), initializeRun(), initializeTLS(), produce(), and terminateRun().

88 { return m_thread_index; }
void RunManagerMTWorker::initializeG4 ( RunManagerMT runManagerMaster,
const edm::EventSetup es 
)

Definition at line 241 of file RunManagerMTWorker.cc.

References applyOnce, sim::attachSD(), sim::FieldBuilder::build(), RunManagerMT::catalog(), mps_check::command, DumpMagneticField(), Exception, validate-o2o-wbm::f, g, RunManagerMT::G4Commands(), edm::ParameterSet::getParameter(), getThreadIndex(), edm::ParameterSet::getUntrackedParameter(), DDDWorld::GetWorldVolume(), initializeTLS(), initializeUserActions(), RunManagerMTWorker::TLSData::kernel, m_dumpMF, m_p, m_pCustomUIsession, m_pField, m_pMagField, m_pUseMagneticField, m_sVerbose, m_tls, m_UIsession, RunManagerMT::physicsListForWorker(), RunManagerMTWorker::TLSData::registry, RunManagerMTWorker::TLSData::sensCaloDets, RunManagerMTWorker::TLSData::sensTkDets, AlCaHLTBitMon_QueryRunRegistry::string, RunManagerMTWorker::TLSData::threadInitialized, RunManagerMTWorker::TLSData::trackManager, and RunManagerMT::world().

241  {
243  return;
244 
245  G4Timer timer;
246  timer.Start();
247 
248  // I guess everything initialized here should be in thread_local storage
249  initializeTLS();
250 
251  int thisID = getThreadIndex();
252  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMTWorker::initializeG4 in thread " << thisID << " is started";
253 
254  // Initialize per-thread output
255  G4Threading::G4SetThreadId(thisID);
256  G4UImanager::GetUIpointer()->SetUpForAThread(thisID);
257  const std::string& uitype = m_pCustomUIsession.getUntrackedParameter<std::string>("Type", "MessageLogger");
258  if (uitype == "MessageLogger") {
260  } else if (uitype == "MessageLoggerThreadPrefix") {
262  m_pCustomUIsession.getUntrackedParameter<std::string>("ThreadPrefix", ""), thisID);
263  } else if (uitype == "FilePerThread") {
264  m_UIsession =
266  } else {
267  throw cms::Exception("Configuration")
268  << "RunManagerMTWorker::initializeG4: Invalid value of CustomUIsession.Type '" << uitype
269  << "', valid are MessageLogger, MessageLoggerThreadPrefix, FilePerThread";
270  }
271  G4UImanager::GetUIpointer()->SetCoutDestination(m_UIsession);
272 
273  // Initialize worker part of shared resources (geometry, physics)
274  G4WorkerThread::BuildGeometryAndPhysicsVector();
275 
276  // Create worker run manager
277  m_tls->kernel.reset(G4WorkerRunManagerKernel::GetRunManagerKernel());
278  if (nullptr == m_tls->kernel) {
279  m_tls->kernel = std::make_unique<G4WorkerRunManagerKernel>();
280  }
281 
282  // Define G4 exception handler
283  double th = m_p.getParameter<double>("ThresholdForGeometryExceptions") * CLHEP::GeV;
284  bool tr = m_p.getParameter<bool>("TraceExceptions");
285  G4StateManager::GetStateManager()->SetExceptionHandler(new ExceptionHandler(th, tr));
286 
287  // Set the geometry for the worker, share from master
288  auto worldPV = runManagerMaster->world().GetWorldVolume();
289  m_tls->kernel->WorkerDefineWorldVolume(worldPV);
290  G4TransportationManager* tM = G4TransportationManager::GetTransportationManager();
291  tM->SetWorldForTracking(worldPV);
292 
293  // we need the track manager now
294  m_tls->trackManager = std::make_unique<SimTrackManager>();
295 
296  // setup the magnetic field
297  if (m_pUseMagneticField) {
298  const GlobalPoint g(0.f, 0.f, 0.f);
299 
300  sim::FieldBuilder fieldBuilder(m_pMagField, m_pField);
301 
302  CMSFieldManager* fieldManager = new CMSFieldManager();
303  tM->SetFieldManager(fieldManager);
304  fieldBuilder.build(fieldManager, tM->GetPropagatorInField());
305 
306  std::string fieldFile = m_p.getUntrackedParameter<std::string>("FileNameField", "");
307  if (!fieldFile.empty()) {
308  std::call_once(applyOnce, [this]() { m_dumpMF = true; });
309  if (m_dumpMF) {
310  edm::LogVerbatim("SimG4CoreApplication")
311  << "RunManagerMTWorker::InitializeG4: Dump magnetic field to file " << fieldFile;
312  DumpMagneticField(tM->GetFieldManager()->GetDetectorField(), fieldFile);
313  }
314  }
315  }
316 
317  // attach sensitive detector
318  auto sensDets = sim::attachSD(
319  m_sdMakers, es, runManagerMaster->catalog(), m_p, m_tls->trackManager.get(), *(m_tls->registry.get()));
320 
321  m_tls->sensTkDets.swap(sensDets.first);
322  m_tls->sensCaloDets.swap(sensDets.second);
323 
324  edm::LogVerbatim("SimG4CoreApplication")
325  << "RunManagerMTWorker::InitializeG4: Sensitive Detectors are built in thread " << thisID << " found "
326  << m_tls->sensTkDets.size() << " Tk type SD, and " << m_tls->sensCaloDets.size() << " Calo type SD";
327 
328  // Set the physics list for the worker, share from master
329  PhysicsList* physicsList = runManagerMaster->physicsListForWorker();
330 
331  edm::LogVerbatim("SimG4CoreApplication")
332  << "RunManagerMTWorker::InitializeG4: start initialisation of PhysicsList for the thread " << thisID;
333 
334  // Geant4 UI commands in PreInit state
335  if (!runManagerMaster->G4Commands().empty()) {
336  G4cout << "RunManagerMTWorker::InitializeG4: Requested UI commands: " << G4endl;
337  for (const std::string& command : runManagerMaster->G4Commands()) {
338  G4cout << " " << command << G4endl;
339  G4UImanager::GetUIpointer()->ApplyCommand(command);
340  }
341  }
342  G4StateManager::GetStateManager()->SetNewState(G4State_Init);
343 
344  physicsList->InitializeWorker();
345  m_tls->kernel->SetPhysics(physicsList);
346  m_tls->kernel->InitializePhysics();
347 
348  if (!m_tls->kernel->RunInitialization()) {
349  throw cms::Exception("Configuration")
350  << "RunManagerMTWorker::InitializeG4: Geant4 kernel initialization failed in thread " << thisID;
351  }
352  //tell all interesting parties that we are beginning the job
353  BeginOfJob aBeginOfJob(&es);
354  m_tls->registry->beginOfJobSignal_(&aBeginOfJob);
355 
356  G4int sv = m_p.getUntrackedParameter<int>("SteppingVerbosity", 0);
357  G4double elim = m_p.getUntrackedParameter<double>("StepVerboseThreshold", 0.1) * CLHEP::GeV;
358  std::vector<int> ve = m_p.getUntrackedParameter<std::vector<int>>("VerboseEvents");
359  std::vector<int> vn = m_p.getUntrackedParameter<std::vector<int>>("VertexNumber");
360  std::vector<int> vt = m_p.getUntrackedParameter<std::vector<int>>("VerboseTracks");
361 
362  if (sv > 0) {
363  m_sVerbose = std::make_unique<CMSSteppingVerbose>(sv, elim, ve, vn, vt);
364  }
366 
367  G4StateManager::GetStateManager()->SetNewState(G4State_Idle);
368 
369  timer.Stop();
370  edm::LogVerbatim("SimG4CoreApplication")
371  << "RunManagerMTWorker::initializeG4 done for the thread " << thisID << " " << timer;
372  m_tls->threadInitialized = true;
373 }
Log< level::Info, true > LogVerbatim
const SensitiveDetectorCatalog & catalog() const
Definition: RunManagerMT.h:72
T getUntrackedParameter(std::string const &, T const &) const
CustomUIsession * m_UIsession
std::unique_ptr< CMSSteppingVerbose > m_sVerbose
std::vector< SensitiveTkDetector * > sensTkDets
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
Definition: Activities.doc:4
std::unique_ptr< G4RunManagerKernel > kernel
std::unique_ptr< SimActivityRegistry > registry
const DDDWorld & world() const
Definition: RunManagerMT.h:70
std::unique_ptr< SimTrackManager > trackManager
std::vector< SensitiveCaloDetector * > sensCaloDets
std::unordered_map< std::string, std::unique_ptr< SensitiveDetectorMakerBase > > m_sdMakers
int getThreadIndex() const
G4VPhysicalVolume * GetWorldVolume() const
Definition: DDDWorld.h:24
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 &reg)
static std::once_flag applyOnce
PhysicsList * physicsListForWorker() const
Definition: RunManagerMT.h:78
edm::ParameterSet m_pField
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
list command
Definition: mps_check.py:25
const std::vector< std::string > & G4Commands() const
Definition: RunManagerMT.h:74
edm::ParameterSet m_pCustomUIsession
const MagneticField * m_pMagField
edm::ParameterSet m_p
void DumpMagneticField(const G4Field *, const std::string &) const
void RunManagerMTWorker::initializeRun ( )
private

Definition at line 437 of file RunManagerMTWorker.cc.

References RunManagerMTWorker::TLSData::currentRun, getThreadIndex(), m_tls, and RunManagerMTWorker::TLSData::userRunAction.

Referenced by produce().

437  {
438  int thisID = getThreadIndex();
439  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMTWorker::initializeRun " << thisID << " is started";
440  m_tls->currentRun = new G4Run();
441  G4StateManager::GetStateManager()->SetNewState(G4State_GeomClosed);
442  if (nullptr != m_tls->userRunAction) {
443  m_tls->userRunAction->BeginOfRunAction(m_tls->currentRun);
444  }
445 }
Log< level::Info, true > LogVerbatim
std::unique_ptr< RunAction > userRunAction
int getThreadIndex() const
void RunManagerMTWorker::initializeTLS ( )
private

Definition at line 217 of file RunManagerMTWorker.cc.

References createWatchers(), Exception, getThreadIndex(), m_hasWatchers, m_p, m_tls, RunManagerMTWorker::TLSData::producers, RunManagerMTWorker::TLSData::registry, and RunManagerMTWorker::TLSData::watchers.

Referenced by GetSimTrackManager(), initializeG4(), producers(), sensCaloDetectors(), and sensTkDetectors().

217  {
218  if (nullptr != m_tls) {
219  return;
220  }
221 
222  m_tls = new TLSData();
223  m_tls->registry = std::make_unique<SimActivityRegistry>();
224 
225  edm::Service<SimActivityRegistry> otherRegistry;
226  //Look for an outside SimActivityRegistry
227  // this is used by the visualization code
228  int thisID = getThreadIndex();
229  if (otherRegistry) {
230  m_tls->registry->connect(*otherRegistry);
231  if (thisID > 0) {
232  throw cms::Exception("Configuration")
233  << "RunManagerMTWorker::initializeTLS : "
234  << "SimActivityRegistry service (i.e. visualization) is not supported for more than 1 thread. "
235  << " \n If this use case is needed, RunManagerMTWorker has to be updated.";
236  }
237  }
239 }
std::vector< std::shared_ptr< SimWatcher > > watchers
std::unique_ptr< SimActivityRegistry > registry
int getThreadIndex() const
std::vector< std::shared_ptr< SimProducer > > producers
static void createWatchers(const edm::ParameterSet &iP, SimActivityRegistry &iReg, std::vector< std::shared_ptr< SimWatcher >> &oWatchers, std::vector< std::shared_ptr< SimProducer >> &oProds)
edm::ParameterSet m_p
void RunManagerMTWorker::initializeUserActions ( )
private

Definition at line 375 of file RunManagerMTWorker.cc.

References Connect(), RunManagerMTWorker::TLSData::kernel, m_EvtMgrVerbosity, m_hasWatchers, m_pEventAction, m_pRunAction, m_pStackingAction, m_pSteppingAction, m_pTrackingAction, m_sVerbose, m_tls, RunManagerMTWorker::TLSData::runInterface, RunManagerMTWorker::TLSData::trackManager, and RunManagerMTWorker::TLSData::userRunAction.

Referenced by initializeG4().

375  {
376  m_tls->runInterface = std::make_unique<SimRunInterface>(this, false);
377  m_tls->userRunAction = std::make_unique<RunAction>(m_pRunAction, m_tls->runInterface.get(), false);
378  m_tls->userRunAction->SetMaster(false);
379  Connect(m_tls->userRunAction.get());
380 
381  G4EventManager* eventManager = m_tls->kernel->GetEventManager();
382  eventManager->SetVerboseLevel(m_EvtMgrVerbosity);
383 
384  EventAction* userEventAction =
386  Connect(userEventAction);
387  eventManager->SetUserAction(userEventAction);
388 
389  TrackingAction* userTrackingAction = new TrackingAction(userEventAction, m_pTrackingAction, m_sVerbose.get());
390  Connect(userTrackingAction);
391  eventManager->SetUserAction(userTrackingAction);
392 
393  SteppingAction* userSteppingAction =
394  new SteppingAction(userEventAction, m_pSteppingAction, m_sVerbose.get(), m_hasWatchers);
395  Connect(userSteppingAction);
396  eventManager->SetUserAction(userSteppingAction);
397 
398  eventManager->SetUserAction(new StackingAction(userTrackingAction, m_pStackingAction, m_sVerbose.get()));
399 }
edm::ParameterSet m_pSteppingAction
std::unique_ptr< SimRunInterface > runInterface
std::unique_ptr< CMSSteppingVerbose > m_sVerbose
std::unique_ptr< G4RunManagerKernel > kernel
edm::ParameterSet m_pEventAction
std::unique_ptr< SimTrackManager > trackManager
edm::ParameterSet m_pRunAction
std::unique_ptr< RunAction > userRunAction
edm::ParameterSet m_pTrackingAction
edm::ParameterSet m_pStackingAction
void Connect(RunAction *)
std::unique_ptr< G4SimEvent > RunManagerMTWorker::produce ( const edm::Event inpevt,
const edm::EventSetup es,
RunManagerMT runManagerMaster 
)

Definition at line 468 of file RunManagerMTWorker.cc.

References cms::cuda::assert(), RunManagerMTWorker::TLSData::currentEvent, RunManagerMTWorker::TLSData::currentRunNumber, edm::EventID::event(), Generator::eventWeight(), Exception, generateEvent(), Generator::genEvent(), Generator::genVertex(), getThreadIndex(), G4SimEvent::hepEvent(), edm::EventBase::id(), initializeRun(), RunManagerMTWorker::TLSData::kernel, m_generator, m_simEvent, m_tls, edm::EventID::run(), RunManagerMTWorker::TLSData::runInterface, RunManagerMTWorker::TLSData::runTerminated, sd, edm::second(), RunManagerMTWorker::TLSData::sensCaloDets, simEvent(), edm::Event::streamID(), terminateRun(), and RunManagerMTWorker::TLSData::threadInitialized.

470  {
471  // The initialization and begin/end run is a bit convoluted due to
472  // - Geant4 deals per-thread
473  // - OscarMTProducer deals per-stream
474  // and framework/TBB is free to schedule work in streams to the
475  // threads as it likes.
476  //
477  // We have to do the per-thread initialization, and per-thread
478  // per-run initialization here by ourselves.
479 
480  assert(m_tls != nullptr and m_tls->threadInitialized);
481  // Initialize run
482  if (inpevt.id().run() != m_tls->currentRunNumber) {
483  edm::LogVerbatim("SimG4CoreApplication")
484  << "RunManagerMTWorker::produce: RunID= " << inpevt.id().run() << " TLS RunID= " << m_tls->currentRunNumber;
485  if (m_tls->currentRunNumber != 0 && !m_tls->runTerminated) {
486  // If previous run in this thread was not terminated via endRun() call,
487  // terminate it now
488  terminateRun();
489  }
490  initializeRun();
491  m_tls->currentRunNumber = inpevt.id().run();
492  }
493  m_tls->runInterface->setRunManagerMTWorker(this); // For UserActions
494 
495  m_tls->currentEvent.reset(generateEvent(inpevt));
496 
497  auto simEvent = std::make_unique<G4SimEvent>();
498  m_simEvent = simEvent.get();
499  m_simEvent->hepEvent(m_generator.genEvent());
500  m_simEvent->weight(m_generator.eventWeight());
501  if (m_generator.genVertex() != nullptr) {
502  auto genVertex = m_generator.genVertex();
503  m_simEvent->collisionPoint(math::XYZTLorentzVectorD(genVertex->x() / CLHEP::cm,
504  genVertex->y() / CLHEP::cm,
505  genVertex->z() / CLHEP::cm,
506  genVertex->t() / CLHEP::second));
507  }
508  if (m_tls->currentEvent->GetNumberOfPrimaryVertex() == 0) {
509  throw cms::Exception("EventCorruption")
510  << "RunManagerMTWorker::produce: event " << inpevt.id().event() << " with no G4PrimaryVertices"
511  << " StreamID=" << inpevt.streamID() << " threadIndex=" << getThreadIndex();
512 
513  } else {
514  edm::LogVerbatim("SimG4CoreApplication")
515  << "RunManagerMTWorker::produce: start EventID=" << inpevt.id().event() << " StreamID=" << inpevt.streamID()
516  << " threadIndex=" << getThreadIndex() << " weight=" << m_simEvent->weight() << "; "
517  << m_tls->currentEvent->GetNumberOfPrimaryVertex() << " vertices for Geant4; generator produced "
518  << m_simEvent->nGenParts() << " particles.";
519 
520  m_tls->kernel->GetEventManager()->ProcessOneEvent(m_tls->currentEvent.get());
521  }
522 
523  //remove memory only needed during event processing
524  m_tls->currentEvent.reset();
525 
526  for (auto& sd : m_tls->sensCaloDets) {
527  sd->reset();
528  }
529  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMTWorker::produce: ended Event " << inpevt.id().event();
530  m_simEvent = nullptr;
531  return simEvent;
532 }
RunNumber_t run() const
Definition: EventID.h:38
Log< level::Info, true > LogVerbatim
EventNumber_t event() const
Definition: EventID.h:40
virtual const math::XYZTLorentzVector * genVertex() const
Definition: Generator.h:31
std::unique_ptr< G4Event > currentEvent
virtual const double eventWeight() const
Definition: Generator.h:32
ROOT::Math::LorentzVector< ROOT::Math::PxPyPzE4D< double > > XYZTLorentzVectorD
Lorentz vector with cylindrical internal representation using pseudorapidity.
Definition: LorentzVector.h:14
virtual const HepMC::GenEvent * genEvent() const
Definition: Generator.h:30
std::unique_ptr< SimRunInterface > runInterface
assert(be >=bs)
std::unique_ptr< G4RunManagerKernel > kernel
U second(std::pair< T, U > const &p)
G4SimEvent * simEvent()
std::vector< SensitiveCaloDetector * > sensCaloDets
int getThreadIndex() const
void hepEvent(const HepMC::GenEvent *r)
Definition: G4SimEvent.h:23
double sd
G4Event * generateEvent(const edm::Event &inpevt)
edm::EventID id() const
Definition: EventBase.h:59
StreamID streamID() const
Definition: Event.h:98
std::vector< std::shared_ptr< SimProducer > > & RunManagerMTWorker::producers ( )

Definition at line 432 of file RunManagerMTWorker.cc.

References initializeTLS(), m_tls, and RunManagerMTWorker::TLSData::producers.

432  {
433  initializeTLS();
434  return m_tls->producers;
435 }
std::vector< std::shared_ptr< SimProducer > > producers
void RunManagerMTWorker::resetGenParticleId ( const edm::Event inpevt)
private

Definition at line 591 of file RunManagerMTWorker.cc.

References edm::Event::getByToken(), edm::HandleBase::isValid(), m_theLHCTlinkToken, m_tls, edm::Handle< T >::product(), and RunManagerMTWorker::TLSData::trackManager.

Referenced by generateEvent().

591  {
593  inpevt.getByToken(m_theLHCTlinkToken, theLHCTlink);
594  if (theLHCTlink.isValid()) {
595  m_tls->trackManager->setLHCTransportLink(theLHCTlink.product());
596  }
597 }
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:539
std::unique_ptr< SimTrackManager > trackManager
bool isValid() const
Definition: HandleBase.h:70
T const * product() const
Definition: Handle.h:70
edm::EDGetTokenT< edm::LHCTransportLinkContainer > m_theLHCTlinkToken
std::vector< SensitiveCaloDetector * > & RunManagerMTWorker::sensCaloDetectors ( )

Definition at line 428 of file RunManagerMTWorker.cc.

References initializeTLS(), m_tls, and RunManagerMTWorker::TLSData::sensCaloDets.

428  {
429  initializeTLS();
430  return m_tls->sensCaloDets;
431 }
std::vector< SensitiveCaloDetector * > sensCaloDets
std::vector< SensitiveTkDetector * > & RunManagerMTWorker::sensTkDetectors ( )

Definition at line 424 of file RunManagerMTWorker.cc.

References initializeTLS(), m_tls, and RunManagerMTWorker::TLSData::sensTkDets.

424  {
425  initializeTLS();
426  return m_tls->sensTkDets;
427 }
std::vector< SensitiveTkDetector * > sensTkDets
G4SimEvent* RunManagerMTWorker::simEvent ( )
inline

Definition at line 63 of file RunManagerMTWorker.h.

References m_simEvent.

Referenced by produce(), and SimRunInterface::simEvent().

63 { return m_simEvent; }
void RunManagerMTWorker::terminateRun ( )
private

Definition at line 447 of file RunManagerMTWorker.cc.

References RunManagerMTWorker::TLSData::currentEvent, RunManagerMTWorker::TLSData::currentRun, getThreadIndex(), RunManagerMTWorker::TLSData::kernel, m_simEvent, m_tls, RunManagerMTWorker::TLSData::runTerminated, and RunManagerMTWorker::TLSData::userRunAction.

Referenced by abortRun(), endRun(), and produce().

447  {
448  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMTWorker::terminateRun ";
449  if (nullptr == m_tls || m_tls->runTerminated) {
450  return;
451  }
452  int thisID = getThreadIndex();
453  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMTWorker::terminateRun " << thisID << " is started";
454  if (m_tls->userRunAction) {
455  m_tls->userRunAction->EndOfRunAction(m_tls->currentRun);
456  m_tls->userRunAction.reset();
457  }
458  m_tls->currentEvent.reset();
459  m_simEvent = nullptr;
460 
461  if (m_tls->kernel) {
462  m_tls->kernel->RunTermination();
463  }
464 
465  m_tls->runTerminated = true;
466 }
Log< level::Info, true > LogVerbatim
std::unique_ptr< G4Event > currentEvent
std::unique_ptr< G4RunManagerKernel > kernel
std::unique_ptr< RunAction > userRunAction
int getThreadIndex() const

Member Data Documentation

bool RunManagerMTWorker::m_dumpMF {false}
private

Definition at line 101 of file RunManagerMTWorker.h.

Referenced by initializeG4().

int RunManagerMTWorker::m_EvtMgrVerbosity {0}
private

Definition at line 104 of file RunManagerMTWorker.h.

Referenced by initializeUserActions().

Generator RunManagerMTWorker::m_generator
private

Definition at line 90 of file RunManagerMTWorker.h.

Referenced by generateEvent(), and produce().

bool RunManagerMTWorker::m_hasWatchers {false}
private

Definition at line 99 of file RunManagerMTWorker.h.

Referenced by beginRun(), initializeTLS(), and initializeUserActions().

edm::EDGetTokenT<edm::HepMCProduct> RunManagerMTWorker::m_InToken
private

Definition at line 91 of file RunManagerMTWorker.h.

Referenced by generateEvent().

edm::EDGetTokenT<edm::HepMCProduct> RunManagerMTWorker::m_LHCToken
private

Definition at line 92 of file RunManagerMTWorker.h.

Referenced by generateEvent().

bool RunManagerMTWorker::m_LHCTransport {false}
private

Definition at line 100 of file RunManagerMTWorker.h.

Referenced by generateEvent().

edm::ESGetToken<MagneticField, IdealMagneticFieldRecord> RunManagerMTWorker::m_MagField
private

Definition at line 94 of file RunManagerMTWorker.h.

Referenced by beginRun().

bool RunManagerMTWorker::m_nonBeam {false}
private

Definition at line 97 of file RunManagerMTWorker.h.

Referenced by generateEvent().

edm::ParameterSet RunManagerMTWorker::m_p
private

Definition at line 113 of file RunManagerMTWorker.h.

Referenced by initializeG4(), and initializeTLS().

edm::ParameterSet RunManagerMTWorker::m_pCustomUIsession
private

Definition at line 112 of file RunManagerMTWorker.h.

Referenced by initializeG4().

edm::ParameterSet RunManagerMTWorker::m_pEventAction
private

Definition at line 108 of file RunManagerMTWorker.h.

Referenced by initializeUserActions().

edm::ParameterSet RunManagerMTWorker::m_pField
private

Definition at line 106 of file RunManagerMTWorker.h.

Referenced by initializeG4().

const MagneticField* RunManagerMTWorker::m_pMagField {nullptr}
private

Definition at line 95 of file RunManagerMTWorker.h.

Referenced by beginRun(), and initializeG4().

edm::ParameterSet RunManagerMTWorker::m_pRunAction
private

Definition at line 107 of file RunManagerMTWorker.h.

Referenced by initializeUserActions().

edm::ParameterSet RunManagerMTWorker::m_pStackingAction
private

Definition at line 109 of file RunManagerMTWorker.h.

Referenced by initializeUserActions().

edm::ParameterSet RunManagerMTWorker::m_pSteppingAction
private

Definition at line 111 of file RunManagerMTWorker.h.

Referenced by initializeUserActions().

edm::ParameterSet RunManagerMTWorker::m_pTrackingAction
private

Definition at line 110 of file RunManagerMTWorker.h.

Referenced by initializeUserActions().

bool RunManagerMTWorker::m_pUseMagneticField {true}
private

Definition at line 98 of file RunManagerMTWorker.h.

Referenced by beginRun(), and initializeG4().

std::unordered_map<std::string, std::unique_ptr<SensitiveDetectorMakerBase> > RunManagerMTWorker::m_sdMakers
private

Definition at line 121 of file RunManagerMTWorker.h.

G4SimEvent* RunManagerMTWorker::m_simEvent {nullptr}
private

Definition at line 119 of file RunManagerMTWorker.h.

Referenced by generateEvent(), produce(), simEvent(), and terminateRun().

std::unique_ptr<CMSSteppingVerbose> RunManagerMTWorker::m_sVerbose
private

Definition at line 120 of file RunManagerMTWorker.h.

Referenced by initializeG4(), and initializeUserActions().

edm::EDGetTokenT<edm::LHCTransportLinkContainer> RunManagerMTWorker::m_theLHCTlinkToken
private

Definition at line 93 of file RunManagerMTWorker.h.

Referenced by resetGenParticleId().

const int RunManagerMTWorker::m_thread_index {-1}
private

Definition at line 103 of file RunManagerMTWorker.h.

Referenced by getThreadIndex().

TLSData* RunManagerMTWorker::m_tls {nullptr}
private
CustomUIsession* RunManagerMTWorker::m_UIsession {nullptr}
private

Definition at line 118 of file RunManagerMTWorker.h.

Referenced by initializeG4(), and ~RunManagerMTWorker().