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 49 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 533 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().

533  {
534  if (m_tls->runTerminated) {
535  return;
536  }
537  G4Track* t = m_tls->kernel->GetEventManager()->GetTrackingManager()->GetTrack();
538  t->SetTrackStatus(fStopAndKill);
539 
540  // CMS-specific act
541  //
542  TrackingAction* uta = static_cast<TrackingAction*>(m_tls->kernel->GetEventManager()->GetUserTrackingAction());
543  uta->PostUserTrackingAction(t);
544 
545  m_tls->currentEvent->SetEventAborted();
546  m_tls->kernel->GetEventManager()->GetStackManager()->clear();
547  m_tls->kernel->GetEventManager()->GetTrackingManager()->EventAborted();
548 }
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 550 of file RunManagerMTWorker.cc.

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

Referenced by SimRunInterface::abortRun().

550  {
551  if (!softAbort) {
552  abortEvent();
553  }
554  m_tls->currentRun = nullptr;
555  terminateRun();
556 }
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:128
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 400 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().

400  {
401  runAction->m_beginOfRunSignal.connect(m_tls->registry->beginOfRunSignal_);
402  runAction->m_endOfRunSignal.connect(m_tls->registry->endOfRunSignal_);
403 }
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 405 of file RunManagerMTWorker.cc.

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

405  {
406  eventAction->m_beginOfEventSignal.connect(m_tls->registry->beginOfEventSignal_);
407  eventAction->m_endOfEventSignal.connect(m_tls->registry->endOfEventSignal_);
408 }
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 410 of file RunManagerMTWorker.cc.

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

410  {
411  trackingAction->m_beginOfTrackSignal.connect(m_tls->registry->beginOfTrackSignal_);
412  trackingAction->m_endOfTrackSignal.connect(m_tls->registry->endOfTrackSignal_);
413 }
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 415 of file RunManagerMTWorker.cc.

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

415  {
416  steppingAction->m_g4StepSignal.connect(m_tls->registry->g4StepSignal_);
417 }
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 598 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().

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

558  {
559  m_tls->currentEvent.reset();
560  m_simEvent = nullptr;
561 
562  // 64 bits event ID in CMSSW converted into Geant4 event ID
563  G4int evtid = (G4int)inpevt.id().event();
564  G4Event* evt = new G4Event(evtid);
565 
567  inpevt.getByToken(m_InToken, HepMCEvt);
568 
569  m_generator.setGenEvent(HepMCEvt->GetEvent());
570 
571  // required to reset the GenParticle Id for particles transported
572  // along the beam pipe
573  // to their original value for SimTrack creation
574  resetGenParticleId(inpevt);
575 
576  if (!m_nonBeam) {
577  m_generator.HepMC2G4(HepMCEvt->GetEvent(), evt);
578  if (m_LHCTransport) {
580  inpevt.getByToken(m_LHCToken, LHCMCEvt);
581  m_generator.nonCentralEvent2G4(LHCMCEvt->GetEvent(), evt);
582  }
583  } else {
584  m_generator.nonCentralEvent2G4(HepMCEvt->GetEvent(), evt);
585  }
586 
587  return evt;
588 }
EventNumber_t event() const
Definition: EventID.h:40
void nonCentralEvent2G4(const HepMC::GenEvent *g, G4Event *e)
Definition: Generator.cc:549
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 419 of file RunManagerMTWorker.cc.

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

Referenced by SimRunInterface::SimRunInterface().

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

Definition at line 89 of file RunManagerMTWorker.h.

References m_thread_index.

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

89 { 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(), GeV, 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  G4StateManager::GetStateManager()->SetExceptionHandler(new ExceptionHandler(th, false));
285 
286  // Set the geometry for the worker, share from master
287  auto worldPV = runManagerMaster->world().GetWorldVolume();
288  m_tls->kernel->WorkerDefineWorldVolume(worldPV);
289  G4TransportationManager* tM = G4TransportationManager::GetTransportationManager();
290  tM->SetWorldForTracking(worldPV);
291 
292  // we need the track manager now
293  m_tls->trackManager = std::make_unique<SimTrackManager>();
294 
295  // setup the magnetic field
296  if (m_pUseMagneticField) {
297  const GlobalPoint g(0.f, 0.f, 0.f);
298 
299  sim::FieldBuilder fieldBuilder(m_pMagField, m_pField);
300 
301  CMSFieldManager* fieldManager = new CMSFieldManager();
302  tM->SetFieldManager(fieldManager);
303  fieldBuilder.build(fieldManager, tM->GetPropagatorInField());
304 
305  std::string fieldFile = m_p.getUntrackedParameter<std::string>("FileNameField", "");
306  if (!fieldFile.empty()) {
307  std::call_once(applyOnce, [this]() { m_dumpMF = true; });
308  if (m_dumpMF) {
309  edm::LogVerbatim("SimG4CoreApplication")
310  << "RunManagerMTWorker::InitializeG4: Dump magnetic field to file " << fieldFile;
311  DumpMagneticField(tM->GetFieldManager()->GetDetectorField(), fieldFile);
312  }
313  }
314  }
315 
316  // attach sensitive detector
317  auto sensDets = sim::attachSD(
318  m_sdMakers, es, runManagerMaster->catalog(), m_p, m_tls->trackManager.get(), *(m_tls->registry.get()));
319 
320  m_tls->sensTkDets.swap(sensDets.first);
321  m_tls->sensCaloDets.swap(sensDets.second);
322 
323  edm::LogVerbatim("SimG4CoreApplication")
324  << "RunManagerMTWorker::InitializeG4: Sensitive Detectors are built in thread " << thisID << " found "
325  << m_tls->sensTkDets.size() << " Tk type SD, and " << m_tls->sensCaloDets.size() << " Calo type SD";
326 
327  // Set the physics list for the worker, share from master
328  PhysicsList* physicsList = runManagerMaster->physicsListForWorker();
329 
330  edm::LogVerbatim("SimG4CoreApplication")
331  << "RunManagerMTWorker::InitializeG4: start initialisation of PhysicsList for the thread " << thisID;
332 
333  // Geant4 UI commands in PreInit state
334  if (!runManagerMaster->G4Commands().empty()) {
335  G4cout << "RunManagerMTWorker::InitializeG4: Requested UI commands: " << G4endl;
336  for (const std::string& command : runManagerMaster->G4Commands()) {
337  G4cout << " " << command << G4endl;
338  G4UImanager::GetUIpointer()->ApplyCommand(command);
339  }
340  }
341  G4StateManager::GetStateManager()->SetNewState(G4State_Init);
342 
343  physicsList->InitializeWorker();
344  m_tls->kernel->SetPhysics(physicsList);
345  m_tls->kernel->InitializePhysics();
346 
347  if (!m_tls->kernel->RunInitialization()) {
348  throw cms::Exception("Configuration")
349  << "RunManagerMTWorker::InitializeG4: Geant4 kernel initialization failed in thread " << thisID;
350  }
351  //tell all interesting parties that we are beginning the job
352  BeginOfJob aBeginOfJob(&es);
353  m_tls->registry->beginOfJobSignal_(&aBeginOfJob);
354 
355  G4int sv = m_p.getUntrackedParameter<int>("SteppingVerbosity", 0);
356  G4double elim = m_p.getUntrackedParameter<double>("StepVerboseThreshold", 0.1) * CLHEP::GeV;
357  std::vector<int> ve = m_p.getUntrackedParameter<std::vector<int>>("VerboseEvents");
358  std::vector<int> vn = m_p.getUntrackedParameter<std::vector<int>>("VertexNumber");
359  std::vector<int> vt = m_p.getUntrackedParameter<std::vector<int>>("VerboseTracks");
360 
361  if (sv > 0) {
362  m_sVerbose = std::make_unique<CMSSteppingVerbose>(sv, elim, ve, vn, vt);
363  }
365 
366  G4StateManager::GetStateManager()->SetNewState(G4State_Idle);
367 
368  timer.Stop();
369  edm::LogVerbatim("SimG4CoreApplication")
370  << "RunManagerMTWorker::initializeG4 done for the thread " << thisID << " " << timer;
371  m_tls->threadInitialized = true;
372 }
Log< level::Info, true > LogVerbatim
const SensitiveDetectorCatalog & catalog() const
Definition: RunManagerMT.h:72
T getUntrackedParameter(std::string const &, T const &) const
CustomUIsession * m_UIsession
const double GeV
Definition: MathUtil.h:16
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 436 of file RunManagerMTWorker.cc.

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

Referenced by produce().

436  {
437  int thisID = getThreadIndex();
438  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMTWorker::initializeRun " << thisID << " is started";
439  m_tls->currentRun = new G4Run();
440  G4StateManager::GetStateManager()->SetNewState(G4State_GeomClosed);
441  if (nullptr != m_tls->userRunAction) {
442  m_tls->userRunAction->BeginOfRunAction(m_tls->currentRun);
443  }
444 }
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 374 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().

374  {
375  m_tls->runInterface = std::make_unique<SimRunInterface>(this, false);
376  m_tls->userRunAction = std::make_unique<RunAction>(m_pRunAction, m_tls->runInterface.get(), false);
377  m_tls->userRunAction->SetMaster(false);
378  Connect(m_tls->userRunAction.get());
379 
380  G4EventManager* eventManager = m_tls->kernel->GetEventManager();
381  eventManager->SetVerboseLevel(m_EvtMgrVerbosity);
382 
383  EventAction* userEventAction =
385  Connect(userEventAction);
386  eventManager->SetUserAction(userEventAction);
387 
388  TrackingAction* userTrackingAction = new TrackingAction(userEventAction, m_pTrackingAction, m_sVerbose.get());
389  Connect(userTrackingAction);
390  eventManager->SetUserAction(userTrackingAction);
391 
392  SteppingAction* userSteppingAction =
393  new SteppingAction(userEventAction, m_pSteppingAction, m_sVerbose.get(), m_hasWatchers);
394  Connect(userSteppingAction);
395  eventManager->SetUserAction(userSteppingAction);
396 
397  eventManager->SetUserAction(new StackingAction(userTrackingAction, m_pStackingAction, m_sVerbose.get()));
398 }
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 467 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.

469  {
470  // The initialization and begin/end run is a bit convoluted due to
471  // - Geant4 deals per-thread
472  // - OscarMTProducer deals per-stream
473  // and framework/TBB is free to schedule work in streams to the
474  // threads as it likes.
475  //
476  // We have to do the per-thread initialization, and per-thread
477  // per-run initialization here by ourselves.
478 
479  assert(m_tls != nullptr and m_tls->threadInitialized);
480  // Initialize run
481  if (inpevt.id().run() != m_tls->currentRunNumber) {
482  edm::LogVerbatim("SimG4CoreApplication")
483  << "RunManagerMTWorker::produce: RunID= " << inpevt.id().run() << " TLS RunID= " << m_tls->currentRunNumber;
484  if (m_tls->currentRunNumber != 0 && !m_tls->runTerminated) {
485  // If previous run in this thread was not terminated via endRun() call,
486  // terminate it now
487  terminateRun();
488  }
489  initializeRun();
490  m_tls->currentRunNumber = inpevt.id().run();
491  }
492  m_tls->runInterface->setRunManagerMTWorker(this); // For UserActions
493 
494  m_tls->currentEvent.reset(generateEvent(inpevt));
495 
496  auto simEvent = std::make_unique<G4SimEvent>();
497  m_simEvent = simEvent.get();
498  m_simEvent->hepEvent(m_generator.genEvent());
499  m_simEvent->weight(m_generator.eventWeight());
500  if (m_generator.genVertex() != nullptr) {
501  auto genVertex = m_generator.genVertex();
502  m_simEvent->collisionPoint(math::XYZTLorentzVectorD(genVertex->x() / CLHEP::cm,
503  genVertex->y() / CLHEP::cm,
504  genVertex->z() / CLHEP::cm,
505  genVertex->t() / CLHEP::second));
506  }
507  if (m_tls->currentEvent->GetNumberOfPrimaryVertex() == 0) {
508  throw cms::Exception("EventCorruption")
509  << "RunManagerMTWorker::produce: event " << inpevt.id().event() << " with no G4PrimaryVertices"
510  << " StreamID=" << inpevt.streamID() << " threadIndex=" << getThreadIndex();
511 
512  } else {
513  edm::LogVerbatim("SimG4CoreApplication")
514  << "RunManagerMTWorker::produce: start EventID=" << inpevt.id().event() << " StreamID=" << inpevt.streamID()
515  << " threadIndex=" << getThreadIndex() << " weight=" << m_simEvent->weight() << "; "
516  << m_tls->currentEvent->GetNumberOfPrimaryVertex() << " vertices for Geant4; generator produced "
517  << m_simEvent->nGenParts() << " particles.";
518 
519  m_tls->kernel->GetEventManager()->ProcessOneEvent(m_tls->currentEvent.get());
520  }
521 
522  //remove memory only needed during event processing
523  m_tls->currentEvent.reset();
524 
525  for (auto& sd : m_tls->sensCaloDets) {
526  sd->reset();
527  }
528  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMTWorker::produce: ended Event " << inpevt.id().event();
529  m_simEvent = nullptr;
530  return simEvent;
531 }
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 431 of file RunManagerMTWorker.cc.

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

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

Definition at line 590 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().

590  {
592  inpevt.getByToken(m_theLHCTlinkToken, theLHCTlink);
593  if (theLHCTlink.isValid()) {
594  m_tls->trackManager->setLHCTransportLink(theLHCTlink.product());
595  }
596 }
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 427 of file RunManagerMTWorker.cc.

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

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

Definition at line 423 of file RunManagerMTWorker.cc.

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

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

Definition at line 64 of file RunManagerMTWorker.h.

References m_simEvent.

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

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

Definition at line 446 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().

446  {
447  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMTWorker::terminateRun ";
448  if (nullptr == m_tls || m_tls->runTerminated) {
449  return;
450  }
451  int thisID = getThreadIndex();
452  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMTWorker::terminateRun " << thisID << " is started";
453  if (m_tls->userRunAction) {
454  m_tls->userRunAction->EndOfRunAction(m_tls->currentRun);
455  m_tls->userRunAction.reset();
456  }
457  m_tls->currentEvent.reset();
458  m_simEvent = nullptr;
459 
460  if (m_tls->kernel) {
461  m_tls->kernel->RunTermination();
462  }
463 
464  m_tls->runTerminated = true;
465 }
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 102 of file RunManagerMTWorker.h.

Referenced by initializeG4().

int RunManagerMTWorker::m_EvtMgrVerbosity {0}
private

Definition at line 105 of file RunManagerMTWorker.h.

Referenced by initializeUserActions().

Generator RunManagerMTWorker::m_generator
private

Definition at line 91 of file RunManagerMTWorker.h.

Referenced by generateEvent(), and produce().

bool RunManagerMTWorker::m_hasWatchers {false}
private

Definition at line 100 of file RunManagerMTWorker.h.

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

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

Definition at line 92 of file RunManagerMTWorker.h.

Referenced by generateEvent().

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

Definition at line 93 of file RunManagerMTWorker.h.

Referenced by generateEvent().

bool RunManagerMTWorker::m_LHCTransport {false}
private

Definition at line 101 of file RunManagerMTWorker.h.

Referenced by generateEvent().

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

Definition at line 95 of file RunManagerMTWorker.h.

Referenced by beginRun().

bool RunManagerMTWorker::m_nonBeam {false}
private

Definition at line 98 of file RunManagerMTWorker.h.

Referenced by generateEvent().

edm::ParameterSet RunManagerMTWorker::m_p
private

Definition at line 114 of file RunManagerMTWorker.h.

Referenced by initializeG4(), and initializeTLS().

edm::ParameterSet RunManagerMTWorker::m_pCustomUIsession
private

Definition at line 113 of file RunManagerMTWorker.h.

Referenced by initializeG4().

edm::ParameterSet RunManagerMTWorker::m_pEventAction
private

Definition at line 109 of file RunManagerMTWorker.h.

Referenced by initializeUserActions().

edm::ParameterSet RunManagerMTWorker::m_pField
private

Definition at line 107 of file RunManagerMTWorker.h.

Referenced by initializeG4().

const MagneticField* RunManagerMTWorker::m_pMagField {nullptr}
private

Definition at line 96 of file RunManagerMTWorker.h.

Referenced by beginRun(), and initializeG4().

edm::ParameterSet RunManagerMTWorker::m_pRunAction
private

Definition at line 108 of file RunManagerMTWorker.h.

Referenced by initializeUserActions().

edm::ParameterSet RunManagerMTWorker::m_pStackingAction
private

Definition at line 110 of file RunManagerMTWorker.h.

Referenced by initializeUserActions().

edm::ParameterSet RunManagerMTWorker::m_pSteppingAction
private

Definition at line 112 of file RunManagerMTWorker.h.

Referenced by initializeUserActions().

edm::ParameterSet RunManagerMTWorker::m_pTrackingAction
private

Definition at line 111 of file RunManagerMTWorker.h.

Referenced by initializeUserActions().

bool RunManagerMTWorker::m_pUseMagneticField {true}
private

Definition at line 99 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 122 of file RunManagerMTWorker.h.

G4SimEvent* RunManagerMTWorker::m_simEvent {nullptr}
private

Definition at line 120 of file RunManagerMTWorker.h.

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

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

Definition at line 121 of file RunManagerMTWorker.h.

Referenced by initializeG4(), and initializeUserActions().

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

Definition at line 94 of file RunManagerMTWorker.h.

Referenced by resetGenParticleId().

const int RunManagerMTWorker::m_thread_index {-1}
private

Definition at line 104 of file RunManagerMTWorker.h.

Referenced by getThreadIndex().

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

Definition at line 119 of file RunManagerMTWorker.h.

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