CMS 3D CMS Logo

RunManagerMTWorker.cc
Go to the documentation of this file.
12 
14 
23 
30 
35 
39 
42 
44 
46 
50 
51 #include "G4Timer.hh"
52 #include "G4Event.hh"
53 #include "G4Run.hh"
54 #include "G4SystemOfUnits.hh"
55 #include "G4Threading.hh"
56 #include "G4UImanager.hh"
57 #include "G4WorkerThread.hh"
58 #include "G4WorkerRunManagerKernel.hh"
59 #include "G4StateManager.hh"
60 #include "G4TransportationManager.hh"
61 #include "G4Field.hh"
62 #include "G4FieldManager.hh"
63 
64 #include <atomic>
65 #include <memory>
66 
67 #include <thread>
68 #include <sstream>
69 #include <vector>
70 #include "tbb/task_arena.h"
71 
72 static std::once_flag applyOnce;
73 thread_local bool RunManagerMTWorker::dumpMF = false;
74 
75 // from https://hypernews.cern.ch/HyperNews/CMS/get/edmFramework/3302/2.html
76 namespace {
77  std::atomic<int> thread_counter{0};
78 
79  int get_new_thread_index() { return thread_counter++; }
80 
81  thread_local int s_thread_index = get_new_thread_index();
82 
83  int getThreadIndex() { return s_thread_index; }
84 
85  void createWatchers(const edm::ParameterSet& iP,
86  SimActivityRegistry* iReg,
87  std::vector<std::shared_ptr<SimWatcher> >& oWatchers,
88  std::vector<std::shared_ptr<SimProducer> >& oProds) {
89  if (!iP.exists("Watchers")) {
90  return;
91  }
92 
93  std::vector<edm::ParameterSet> watchers = iP.getParameter<std::vector<edm::ParameterSet> >("Watchers");
94 
95  for (auto& watcher : watchers) {
96  std::unique_ptr<SimWatcherMakerBase> maker(
97  SimWatcherFactory::get()->create(watcher.getParameter<std::string>("type")));
98  if (maker == nullptr) {
100  << "Unable to find the requested Watcher <" << watcher.getParameter<std::string>("type");
101  }
102  std::shared_ptr<SimWatcher> watcherTemp;
103  std::shared_ptr<SimProducer> producerTemp;
104  maker->make(watcher, *(iReg), watcherTemp, producerTemp);
105  oWatchers.push_back(watcherTemp);
106  if (producerTemp) {
107  oProds.push_back(producerTemp);
108  }
109  }
110  }
111 
112  std::atomic<int> active_tlsdata{0};
113  std::atomic<bool> tls_shutdown_timeout{false};
114  std::atomic<int> n_tls_shutdown_task{0};
115 } // namespace
116 
118  std::unique_ptr<G4RunManagerKernel> kernel; //must be deleted last
119  std::unique_ptr<RunAction> userRunAction;
120  std::unique_ptr<SimRunInterface> runInterface;
121  std::unique_ptr<SimActivityRegistry> registry;
122  std::unique_ptr<SimTrackManager> trackManager;
123  std::vector<SensitiveTkDetector*> sensTkDets;
124  std::vector<SensitiveCaloDetector*> sensCaloDets;
125  std::vector<std::shared_ptr<SimWatcher> > watchers;
126  std::vector<std::shared_ptr<SimProducer> > producers;
127  //G4Run can only be deleted if there is a G4RunManager
128  // on the thread where the G4Run is being deleted,
129  // else it causes a segmentation fault
130  G4Run* currentRun = nullptr;
131  std::unique_ptr<G4Event> currentEvent;
133  bool threadInitialized = false;
134  bool runTerminated = false;
135 
136  TLSData() { ++active_tlsdata; }
137 
138  ~TLSData() { --active_tlsdata; }
139 };
140 
141 //This can not be a smart pointer since we must delete some of the members
142 // before leaving main() else we get a segmentation fault caused by accessing
143 // other 'singletons' after those singletons have been deleted. Instead we
144 // atempt to delete all TLS at RunManagerMTWorker destructor. If that fails for
145 // some reason, it is better to leak than cause a crash.
147 
149  : m_generator(iConfig.getParameter<edm::ParameterSet>("Generator")),
150  m_InToken(iC.consumes<edm::HepMCProduct>(
151  iConfig.getParameter<edm::ParameterSet>("Generator").getParameter<edm::InputTag>("HepMCProductLabel"))),
152  m_theLHCTlinkToken(
153  iC.consumes<edm::LHCTransportLinkContainer>(iConfig.getParameter<edm::InputTag>("theLHCTlinkTag"))),
154  m_nonBeam(iConfig.getParameter<bool>("NonBeamEvent")),
155  m_pUseMagneticField(iConfig.getParameter<bool>("UseMagneticField")),
156  m_LHCTransport(iConfig.getParameter<bool>("LHCTransport")),
157  m_EvtMgrVerbosity(iConfig.getUntrackedParameter<int>("G4EventManagerVerbosity", 0)),
158  m_pField(iConfig.getParameter<edm::ParameterSet>("MagneticField")),
159  m_pRunAction(iConfig.getParameter<edm::ParameterSet>("RunAction")),
160  m_pEventAction(iConfig.getParameter<edm::ParameterSet>("EventAction")),
161  m_pStackingAction(iConfig.getParameter<edm::ParameterSet>("StackingAction")),
162  m_pTrackingAction(iConfig.getParameter<edm::ParameterSet>("TrackingAction")),
163  m_pSteppingAction(iConfig.getParameter<edm::ParameterSet>("SteppingAction")),
164  m_pCustomUIsession(iConfig.getUntrackedParameter<edm::ParameterSet>("CustomUIsession")),
165  m_p(iConfig),
166  m_simEvent(nullptr),
167  m_sVerbose(nullptr) {
168  std::vector<edm::ParameterSet> watchers = iConfig.getParameter<std::vector<edm::ParameterSet> >("Watchers");
169  m_hasWatchers = !watchers.empty();
170  initializeTLS();
171  int thisID = getThreadIndex();
172  if (m_LHCTransport) {
173  m_LHCToken = iC.consumes<edm::HepMCProduct>(edm::InputTag("LHCTransport"));
174  }
175  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMTWorker is constructed for the thread " << thisID;
176 }
177 
179  ++n_tls_shutdown_task;
180  resetTLS();
181 
182  {
183  //make sure all tasks are done before continuing
184  timespec s;
185  s.tv_sec = 0;
186  s.tv_nsec = 10000;
187  while (n_tls_shutdown_task != 0) {
188  nanosleep(&s, nullptr);
189  }
190  }
191 }
192 
194  m_tls = nullptr;
195 
196  if (active_tlsdata != 0 and not tls_shutdown_timeout) {
197  ++n_tls_shutdown_task;
198  //need to run tasks on each thread which has set the tls
199  {
200  tbb::task_arena arena(tbb::task_arena::attach{});
201  arena.enqueue([]() { RunManagerMTWorker::resetTLS(); });
202  }
203  timespec s;
204  s.tv_sec = 0;
205  s.tv_nsec = 10000;
206  //we do not want this thread to be used for a new task since it
207  // has already cleared its structures. In order to fill all TBB
208  // threads we wait for all TLSes to clear
209  int count = 0;
210  while (active_tlsdata.load() != 0 and ++count < 1000) {
211  nanosleep(&s, nullptr);
212  }
213  if (count >= 1000) {
214  tls_shutdown_timeout = true;
215  }
216  }
217  --n_tls_shutdown_task;
218 }
219 
221  int thisID = getThreadIndex();
222  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMTWorker::endRun for the thread " << thisID;
223  terminateRun();
224 }
225 
227  if (m_tls) {
228  return;
229  }
230 
231  m_tls = new TLSData();
232  m_tls->registry = std::make_unique<SimActivityRegistry>();
233 
234  edm::Service<SimActivityRegistry> otherRegistry;
235  //Look for an outside SimActivityRegistry
236  // this is used by the visualization code
237  int thisID = getThreadIndex();
238  if (otherRegistry) {
239  m_tls->registry->connect(*otherRegistry);
240  if (thisID > 0) {
242  << "SimActivityRegistry service (i.e. visualization) is not supported for more than 1 thread. "
243  << " \n If this use case is needed, RunManagerMTWorker has to be updated.";
244  }
245  }
246  if (m_hasWatchers) {
248  }
249 }
250 
252  G4Timer timer;
253  timer.Start();
254 
255  // I guess everything initialized here should be in thread_local storage
256  initializeTLS();
258  return;
259 
260  int thisID = getThreadIndex();
261  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMTWorker::initializeThread " << thisID << " is started";
262 
263  // Initialize per-thread output
264  G4Threading::G4SetThreadId(thisID);
265  G4UImanager::GetUIpointer()->SetUpForAThread(thisID);
266  const std::string& uitype = m_pCustomUIsession.getUntrackedParameter<std::string>("Type", "MessageLogger");
267  if (uitype == "MessageLogger") {
268  new CustomUIsession();
269  } else if (uitype == "MessageLoggerThreadPrefix") {
271  } else if (uitype == "FilePerThread") {
273  } else {
275  << "Invalid value of CustomUIsession.Type '" << uitype
276  << "', valid are MessageLogger, MessageLoggerThreadPrefix, FilePerThread";
277  }
278 
279  // Initialize worker part of shared resources (geometry, physics)
280  G4WorkerThread::BuildGeometryAndPhysicsVector();
281 
282  // Create worker run manager
283  m_tls->kernel.reset(G4WorkerRunManagerKernel::GetRunManagerKernel());
284  if (!m_tls->kernel) {
285  m_tls->kernel = std::make_unique<G4WorkerRunManagerKernel>();
286  }
287 
288  // Define G4 exception handler
289  G4StateManager::GetStateManager()->SetExceptionHandler(new ExceptionHandler());
290 
291  // Set the geometry for the worker, share from master
292  auto worldPV = runManagerMaster->world().GetWorldVolume();
293  m_tls->kernel->WorkerDefineWorldVolume(worldPV);
294  G4TransportationManager* tM = G4TransportationManager::GetTransportationManager();
295  tM->SetWorldForTracking(worldPV);
296 
297  // we need the track manager now
298  m_tls->trackManager = std::make_unique<SimTrackManager>();
299 
300  // setup the magnetic field
301  if (m_pUseMagneticField) {
302  const GlobalPoint g(0., 0., 0.);
303 
305  es.get<IdealMagneticFieldRecord>().get(pMF);
306 
307  sim::FieldBuilder fieldBuilder(pMF.product(), m_pField);
308  CMSFieldManager* fieldManager = new CMSFieldManager();
309  tM->SetFieldManager(fieldManager);
310  fieldBuilder.build(fieldManager, tM->GetPropagatorInField());
311 
312  std::string fieldFile = m_p.getUntrackedParameter<std::string>("FileNameField", "");
313  if (!fieldFile.empty()) {
314  std::call_once(applyOnce, []() { dumpMF = true; });
315  if (dumpMF) {
316  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMTWorker: Dump magnetic field to file " << fieldFile;
317  DumpMagneticField(tM->GetFieldManager()->GetDetectorField(), fieldFile);
318  }
319  }
320  }
321 
322  // attach sensitive detector
323  AttachSD attach;
324  auto sensDets =
325  attach.create(es, runManagerMaster->catalog(), m_p, m_tls->trackManager.get(), *(m_tls->registry.get()));
326 
327  m_tls->sensTkDets.swap(sensDets.first);
328  m_tls->sensCaloDets.swap(sensDets.second);
329 
330  edm::LogVerbatim("SimG4CoreApplication")
331  << "RunManagerMTWorker: Sensitive Detectors are built in thread " << thisID << " found "
332  << m_tls->sensTkDets.size() << " Tk type SD, and " << m_tls->sensCaloDets.size() << " Calo type SD";
333 
334  // Set the physics list for the worker, share from master
335  PhysicsList* physicsList = runManagerMaster->physicsListForWorker();
336 
337  edm::LogVerbatim("SimG4CoreApplication")
338  << "RunManagerMTWorker: start initialisation of PhysicsList for the thread " << thisID;
339 
340  // Geant4 UI commands in PreInit state
341  if (!runManagerMaster->G4Commands().empty()) {
342  G4cout << "RunManagerMTWorker: Requested UI commands: " << G4endl;
343  for (const std::string& command : runManagerMaster->G4Commands()) {
344  G4cout << " " << command << G4endl;
345  G4UImanager::GetUIpointer()->ApplyCommand(command);
346  }
347  }
348  G4StateManager::GetStateManager()->SetNewState(G4State_Init);
349 
350  physicsList->InitializeWorker();
351  m_tls->kernel->SetPhysics(physicsList);
352  m_tls->kernel->InitializePhysics();
353 
354  const bool kernelInit = m_tls->kernel->RunInitialization();
355  if (!kernelInit) {
357  << "RunManagerMTWorker: Geant4 kernel initialization failed in thread " << thisID;
358  }
359  //tell all interesting parties that we are beginning the job
360  BeginOfJob aBeginOfJob(&es);
361  m_tls->registry->beginOfJobSignal_(&aBeginOfJob);
362 
363  G4int sv = m_p.getUntrackedParameter<int>("SteppingVerbosity", 0);
364  G4double elim = m_p.getUntrackedParameter<double>("StepVerboseThreshold", 0.1) * CLHEP::GeV;
365  std::vector<int> ve = m_p.getUntrackedParameter<std::vector<int> >("VerboseEvents");
366  std::vector<int> vn = m_p.getUntrackedParameter<std::vector<int> >("VertexNumber");
367  std::vector<int> vt = m_p.getUntrackedParameter<std::vector<int> >("VerboseTracks");
368 
369  if (sv > 0) {
370  m_sVerbose = std::make_unique<CMSSteppingVerbose>(sv, elim, ve, vn, vt);
371  }
373 
374  G4StateManager::GetStateManager()->SetNewState(G4State_Idle);
375  m_tls->threadInitialized = true;
376 
377  timer.Stop();
378  edm::LogVerbatim("SimG4CoreApplication")
379  << "RunManagerMTWorker::initializeThread done for the thread " << thisID << " " << timer;
380 }
381 
383  m_tls->runInterface = std::make_unique<SimRunInterface>(this, false);
384  m_tls->userRunAction = std::make_unique<RunAction>(m_pRunAction, m_tls->runInterface.get(), false);
385  m_tls->userRunAction->SetMaster(false);
386  Connect(m_tls->userRunAction.get());
387 
388  G4EventManager* eventManager = m_tls->kernel->GetEventManager();
389  eventManager->SetVerboseLevel(m_EvtMgrVerbosity);
390 
391  EventAction* userEventAction =
393  Connect(userEventAction);
394  eventManager->SetUserAction(userEventAction);
395 
396  TrackingAction* userTrackingAction = new TrackingAction(userEventAction, m_pTrackingAction, m_sVerbose.get());
397  Connect(userTrackingAction);
398  eventManager->SetUserAction(userTrackingAction);
399 
400  SteppingAction* userSteppingAction =
401  new SteppingAction(userEventAction, m_pSteppingAction, m_sVerbose.get(), m_hasWatchers);
402  Connect(userSteppingAction);
403  eventManager->SetUserAction(userSteppingAction);
404 
405  eventManager->SetUserAction(new StackingAction(userTrackingAction, m_pStackingAction, m_sVerbose.get()));
406 }
407 
409  runAction->m_beginOfRunSignal.connect(m_tls->registry->beginOfRunSignal_);
410  runAction->m_endOfRunSignal.connect(m_tls->registry->endOfRunSignal_);
411 }
412 
414  eventAction->m_beginOfEventSignal.connect(m_tls->registry->beginOfEventSignal_);
415  eventAction->m_endOfEventSignal.connect(m_tls->registry->endOfEventSignal_);
416 }
417 
419  trackingAction->m_beginOfTrackSignal.connect(m_tls->registry->beginOfTrackSignal_);
420  trackingAction->m_endOfTrackSignal.connect(m_tls->registry->endOfTrackSignal_);
421 }
422 
424  steppingAction->m_g4StepSignal.connect(m_tls->registry->g4StepSignal_);
425 }
426 
428  initializeTLS();
429  return m_tls->trackManager.get();
430 }
431 std::vector<SensitiveTkDetector*>& RunManagerMTWorker::sensTkDetectors() {
432  initializeTLS();
433  return m_tls->sensTkDets;
434 }
435 std::vector<SensitiveCaloDetector*>& RunManagerMTWorker::sensCaloDetectors() {
436  initializeTLS();
437  return m_tls->sensCaloDets;
438 }
439 std::vector<std::shared_ptr<SimProducer> >& RunManagerMTWorker::producers() {
440  initializeTLS();
441  return m_tls->producers;
442 }
443 
445  int thisID = getThreadIndex();
446  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMTWorker::initializeRun " << thisID << " is started";
447  m_tls->currentRun = new G4Run();
448  G4StateManager::GetStateManager()->SetNewState(G4State_GeomClosed);
449  if (m_tls->userRunAction) {
450  m_tls->userRunAction->BeginOfRunAction(m_tls->currentRun);
451  }
452 }
453 
455  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMTWorker::terminateRun ";
456  if (!m_tls || m_tls->runTerminated) {
457  return;
458  }
459  int thisID = getThreadIndex();
460  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMTWorker::terminateRun " << thisID << " is started";
461  if (m_tls->userRunAction) {
462  m_tls->userRunAction->EndOfRunAction(m_tls->currentRun);
463  m_tls->userRunAction.reset();
464  }
465  m_tls->currentEvent.reset();
466  m_simEvent = nullptr;
467 
468  if (m_tls->kernel) {
469  m_tls->kernel->RunTermination();
470  }
471 
472  m_tls->runTerminated = true;
473 }
474 
475 std::unique_ptr<G4SimEvent> RunManagerMTWorker::produce(const edm::Event& inpevt,
476  const edm::EventSetup& es,
477  RunManagerMT& runManagerMaster) {
478  // The initialization and begin/end run is a bit convoluted due to
479  // - Geant4 deals per-thread
480  // - OscarMTProducer deals per-stream
481  // and framework/TBB is free to schedule work in streams to the
482  // threads as it likes.
483  //
484  // We have to do the per-thread initialization, and per-thread
485  // per-run initialization here by ourselves.
486 
487  if (!(m_tls && m_tls->threadInitialized)) {
488  edm::LogVerbatim("SimG4CoreApplication")
489  << "RunManagerMTWorker::produce(): stream " << inpevt.streamID() << " thread " << getThreadIndex()
490  << " Geant4 initialisation for this thread";
491  initializeG4(&runManagerMaster, es);
492  m_tls->threadInitialized = true;
493  }
494  // Initialize run
495  if (inpevt.id().run() != m_tls->currentRunNumber) {
496  if (m_tls->currentRunNumber != 0 && !m_tls->runTerminated) {
497  // If previous run in this thread was not terminated via endRun() call, terminate it now
498  terminateRun();
499  }
500  initializeRun();
501  m_tls->currentRunNumber = inpevt.id().run();
502  }
503  m_tls->runInterface->setRunManagerMTWorker(this); // For UserActions
504 
505  m_tls->currentEvent.reset(generateEvent(inpevt));
506 
507  auto simEvent = std::make_unique<G4SimEvent>();
508  m_simEvent = simEvent.get();
511  if (m_generator.genVertex() != nullptr) {
512  auto genVertex = m_generator.genVertex();
513  m_simEvent->collisionPoint(math::XYZTLorentzVectorD(genVertex->x() / CLHEP::cm,
514  genVertex->y() / CLHEP::cm,
515  genVertex->z() / CLHEP::cm,
516  genVertex->t() / CLHEP::second));
517  }
518  if (m_tls->currentEvent->GetNumberOfPrimaryVertex() == 0) {
519  std::stringstream ss;
520  ss << "RunManagerMTWorker::produce: event " << inpevt.id().event() << " with no G4PrimaryVertices \n";
521  throw SimG4Exception(ss.str());
522 
523  } else {
524  if (!m_tls->kernel) {
525  std::stringstream ss;
526  ss << "RunManagerMTWorker::produce: "
527  << " no G4WorkerRunManagerKernel yet for thread index" << getThreadIndex() << ", id " << std::hex
528  << std::this_thread::get_id() << " \n";
529  throw SimG4Exception(ss.str());
530  }
531 
532  edm::LogVerbatim("SimG4CoreApplication")
533  << "RunManagerMTWorker::produce: start EventID=" << inpevt.id().event() << " StreamID=" << inpevt.streamID()
534  << " threadIndex=" << getThreadIndex() << " weight=" << m_simEvent->weight() << "; "
535  << m_tls->currentEvent->GetNumberOfPrimaryVertex() << " vertices for Geant4; generator produced "
536  << m_simEvent->nGenParts() << " particles.";
537 
538  m_tls->kernel->GetEventManager()->ProcessOneEvent(m_tls->currentEvent.get());
539  }
540 
541  //remove memory only needed during event processing
542  m_tls->currentEvent.reset();
543 
544  for (auto& sd : m_tls->sensCaloDets) {
545  sd->reset();
546  }
547  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMTWorker::produce: ended Event " << inpevt.id().event();
548 
549  m_simEvent = nullptr;
550  return simEvent;
551 }
552 
554  if (m_tls->runTerminated) {
555  return;
556  }
557  G4Track* t = m_tls->kernel->GetEventManager()->GetTrackingManager()->GetTrack();
558  t->SetTrackStatus(fStopAndKill);
559 
560  // CMS-specific act
561  //
562  TrackingAction* uta = static_cast<TrackingAction*>(m_tls->kernel->GetEventManager()->GetUserTrackingAction());
564 
565  m_tls->currentEvent->SetEventAborted();
566  m_tls->kernel->GetEventManager()->GetStackManager()->clear();
567  m_tls->kernel->GetEventManager()->GetTrackingManager()->EventAborted();
568 }
569 
570 void RunManagerMTWorker::abortRun(bool softAbort) {
571  if (!softAbort) {
572  abortEvent();
573  }
574  m_tls->currentRun = nullptr;
575  terminateRun();
576 }
577 
579  m_tls->currentEvent.reset();
580  m_simEvent = nullptr;
581 
582  // 64 bits event ID in CMSSW converted into Geant4 event ID
583  G4int evtid = (G4int)inpevt.id().event();
584  G4Event* evt = new G4Event(evtid);
585 
587  inpevt.getByToken(m_InToken, HepMCEvt);
588 
589  m_generator.setGenEvent(HepMCEvt->GetEvent());
590 
591  // required to reset the GenParticle Id for particles transported
592  // along the beam pipe
593  // to their original value for SimTrack creation
594  resetGenParticleId(inpevt);
595 
596  if (!m_nonBeam) {
597  m_generator.HepMC2G4(HepMCEvt->GetEvent(), evt);
598  if (m_LHCTransport) {
600  inpevt.getByToken(m_LHCToken, LHCMCEvt);
601  m_generator.nonCentralEvent2G4(LHCMCEvt->GetEvent(), evt);
602  }
603  } else {
604  m_generator.nonCentralEvent2G4(HepMCEvt->GetEvent(), evt);
605  }
606 
607  return evt;
608 }
609 
612  inpevt.getByToken(m_theLHCTlinkToken, theLHCTlink);
613  if (theLHCTlink.isValid()) {
614  m_tls->trackManager->setLHCTransportLink(theLHCTlink.product());
615  }
616 }
617 
618 void RunManagerMTWorker::DumpMagneticField(const G4Field* field, const std::string& file) const {
619  std::ofstream fout(file.c_str(), std::ios::out);
620  if (fout.fail()) {
621  edm::LogWarning("SimG4CoreApplication")
622  << " RunManager WARNING : error opening file <" << file << "> for magnetic field";
623  } else {
624  // CMS magnetic field volume
625  double rmax = 9000 * mm;
626  double zmax = 24000 * mm;
627 
628  double dr = 1 * cm;
629  double dz = 5 * cm;
630 
631  int nr = (int)(rmax / dr);
632  int nz = 2 * (int)(zmax / dz);
633 
634  double r = 0.0;
635  double z0 = -zmax;
636  double z;
637 
638  double phi = 0.0;
639  double cosf = cos(phi);
640  double sinf = sin(phi);
641 
642  double point[4] = {0.0, 0.0, 0.0, 0.0};
643  double bfield[3] = {0.0, 0.0, 0.0};
644 
645  fout << std::setprecision(6);
646  for (int i = 0; i <= nr; ++i) {
647  z = z0;
648  for (int j = 0; j <= nz; ++j) {
649  point[0] = r * cosf;
650  point[1] = r * sinf;
651  point[2] = z;
652  field->GetFieldValue(point, bfield);
653  fout << "R(mm)= " << r / mm << " phi(deg)= " << phi / degree << " Z(mm)= " << z / mm
654  << " Bz(tesla)= " << bfield[2] / tesla << " Br(tesla)= " << (bfield[0] * cosf + bfield[1] * sinf) / tesla
655  << " Bphi(tesla)= " << (bfield[0] * sinf - bfield[1] * cosf) / tesla << G4endl;
656  z += dz;
657  }
658  r += dr;
659  }
660 
661  fout.close();
662  }
663 }
Generator::eventWeight
virtual const double eventWeight() const
Definition: Generator.h:32
FieldBuilder.h
G4SimEvent::hepEvent
void hepEvent(const HepMC::GenEvent *r)
Definition: G4SimEvent.h:23
RunManagerMTWorker::m_theLHCTlinkToken
edm::EDGetTokenT< edm::LHCTransportLinkContainer > m_theLHCTlinkToken
Definition: RunManagerMTWorker.h:86
RunManagerMTWorker::m_tls
static thread_local TLSData * m_tls
Definition: RunManagerMTWorker.h:103
edm::ESHandle::product
T const * product() const
Definition: ESHandle.h:86
edm::RunNumber_t
unsigned int RunNumber_t
Definition: RunLumiEventNumber.h:14
RunManagerMTWorker::initializeG4
void initializeG4(RunManagerMT *runManagerMaster, const edm::EventSetup &es)
Definition: RunManagerMTWorker.cc:251
RunManagerMTWorker::sensTkDetectors
std::vector< SensitiveTkDetector * > & sensTkDetectors()
Definition: RunManagerMTWorker.cc:431
SimTrackManager
Definition: SimTrackManager.h:35
Handle.h
RunManagerMTWorker::TLSData::trackManager
std::unique_ptr< SimTrackManager > trackManager
Definition: RunManagerMTWorker.cc:122
electrons_cff.bool
bool
Definition: electrons_cff.py:366
mps_fire.i
i
Definition: mps_fire.py:428
RunAction.h
TrackingAction.h
RunManagerMTWorker::abortRun
void abortRun(bool softAbort=false)
Definition: RunManagerMTWorker.cc:570
edm::LHCTransportLinkContainer
std::vector< LHCTransportLink > LHCTransportLinkContainer
Definition: LHCTransportLinkContainer.h:8
ESTransientHandle.h
MessageLogger.h
SteppingAction.h
EventAction
Definition: EventAction.h:23
RunManagerMTWorker::m_simEvent
G4SimEvent * m_simEvent
Definition: RunManagerMTWorker.h:107
SensitiveCaloDetector.h
CustomUIsessionThreadPrefix
Definition: CustomUIsessionThreadPrefix.h:13
edm::Handle::product
T const * product() const
Definition: Handle.h:70
RunManagerMTWorker::TLSData::producers
std::vector< std::shared_ptr< SimProducer > > producers
Definition: RunManagerMTWorker.cc:126
ExceptionHandler
Definition: ExceptionHandler.h:19
ESHandle.h
BeginOfJob.h
EventAction::m_beginOfEventSignal
SimActivityRegistry::BeginOfEventSignal m_beginOfEventSignal
Definition: EventAction.h:46
EventAction::m_endOfEventSignal
SimActivityRegistry::EndOfEventSignal m_endOfEventSignal
Definition: EventAction.h:47
runTauDisplay.evtid
evtid
Definition: runTauDisplay.py:293
RunManagerMTWorker::TLSData::watchers
std::vector< std::shared_ptr< SimWatcher > > watchers
Definition: RunManagerMTWorker.cc:125
RunManagerMTWorker::resetTLS
static void resetTLS()
Definition: RunManagerMTWorker.cc:193
CMSFieldManager.h
edm
HLT enums.
Definition: AlignableModifier.h:19
TrackingAction::PostUserTrackingAction
void PostUserTrackingAction(const G4Track *aTrack) override
Definition: TrackingAction.cc:68
math::XYZTLorentzVectorD
ROOT::Math::LorentzVector< ROOT::Math::PxPyPzE4D< double > > XYZTLorentzVectorD
Lorentz vector with cylindrical internal representation using pseudorapidity.
Definition: LorentzVector.h:14
SimG4Exception
Definition: SimG4Exception.h:13
Generator::genVertex
virtual const math::XYZTLorentzVector * genVertex() const
Definition: Generator.h:31
HLT_FULL_cff.InputTag
InputTag
Definition: HLT_FULL_cff.py:89285
CMSFieldManager
Definition: CMSFieldManager.h:24
RunManagerMTWorker.h
Generator::nonCentralEvent2G4
void nonCentralEvent2G4(const HepMC::GenEvent *g, G4Event *e)
Definition: Generator.cc:549
RunManagerMTWorker::m_generator
Generator m_generator
Definition: RunManagerMTWorker.h:83
RunManagerMTWorker::abortEvent
void abortEvent()
Definition: RunManagerMTWorker.cc:553
edm::second
U second(std::pair< T, U > const &p)
Definition: ParameterSet.cc:222
EventAction.h
RunManagerMTWorker::m_LHCTransport
bool m_LHCTransport
Definition: RunManagerMTWorker.h:91
SteppingAction
Definition: SteppingAction.h:31
RunManagerMTWorker::simEvent
G4SimEvent * simEvent()
Definition: RunManagerMTWorker.h:55
RunManagerMTWorker::m_pEventAction
edm::ParameterSet m_pEventAction
Definition: RunManagerMTWorker.h:96
edm::ParameterSet::getUntrackedParameter
T getUntrackedParameter(std::string const &, T const &) const
RunManagerMTWorker::Connect
void Connect(RunAction *)
Definition: RunManagerMTWorker.cc:408
AttachSD::create
std::pair< std::vector< SensitiveTkDetector * >, std::vector< SensitiveCaloDetector * > > create(const edm::EventSetup &, const SensitiveDetectorCatalog &, edm::ParameterSet const &, const SimTrackManager *, SimActivityRegistry &reg) const
Definition: AttachSD.cc:15
RunManagerMTWorker::m_EvtMgrVerbosity
int m_EvtMgrVerbosity
Definition: RunManagerMTWorker.h:92
beamerCreator.create
def create(alignables, pedeDump, additionalData, outputFile, config)
Definition: beamerCreator.py:44
CastorDigiValidation.StackingAction
StackingAction
Definition: CastorDigiValidation.py:136
mps_check.command
list command
Definition: mps_check.py:25
RunManagerMTWorker::m_pCustomUIsession
edm::ParameterSet m_pCustomUIsession
Definition: RunManagerMTWorker.h:100
edm::Handle< edm::HepMCProduct >
RunAction::m_beginOfRunSignal
SimActivityRegistry::BeginOfRunSignal m_beginOfRunSignal
Definition: RunAction.h:23
edm::LogWarning
Log< level::Warning, false > LogWarning
Definition: MessageLogger.h:122
RunManagerMTWorker::GetSimTrackManager
SimTrackManager * GetSimTrackManager()
Definition: RunManagerMTWorker.cc:427
RunManagerMTWorker::~RunManagerMTWorker
~RunManagerMTWorker()
Definition: RunManagerMTWorker.cc:178
RunManagerMTWorker::sensCaloDetectors
std::vector< SensitiveCaloDetector * > & sensCaloDetectors()
Definition: RunManagerMTWorker.cc:435
SensitiveTkDetector.h
DDDWorld.h
funct::sin
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
SimActivityRegistry.h
RunManagerMTWorker::TLSData::sensCaloDets
std::vector< SensitiveCaloDetector * > sensCaloDets
Definition: RunManagerMTWorker.cc:124
contentValuesCheck.ss
ss
Definition: contentValuesCheck.py:33
IdealMagneticFieldRecord
Definition: IdealMagneticFieldRecord.h:11
RunManagerMT::catalog
const SensitiveDetectorCatalog & catalog() const
Definition: RunManagerMT.h:77
TrackingAction
Definition: TrackingAction.h:16
RunManagerMTWorker::m_p
edm::ParameterSet m_p
Definition: RunManagerMTWorker.h:101
alignCSCRings.s
s
Definition: alignCSCRings.py:92
RunManagerMTWorker::initializeUserActions
void initializeUserActions()
Definition: RunManagerMTWorker.cc:382
funct::cos
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
RunManagerMTWorker::m_hasWatchers
bool m_hasWatchers
Definition: RunManagerMTWorker.h:90
edm::EventSetup::get
T get() const
Definition: EventSetup.h:87
RunManagerMTWorker::TLSData::currentRun
G4Run * currentRun
Definition: RunManagerMTWorker.cc:130
SiStripMonitorCluster_cfi.zmax
zmax
Definition: SiStripMonitorCluster_cfi.py:201
g4SimHits_cfi.CustomUIsession
CustomUIsession
Definition: g4SimHits_cfi.py:112
RunManagerMTWorker::TLSData::currentEvent
std::unique_ptr< G4Event > currentEvent
Definition: RunManagerMTWorker.cc:131
Service.h
pfDeepBoostedJetPreprocessParams_cfi.sv
sv
Definition: pfDeepBoostedJetPreprocessParams_cfi.py:352
DDAxes::z
RunManagerMT::world
const DDDWorld & world() const
Definition: RunManagerMT.h:75
IdealMagneticFieldRecord.h
edm::ESHandle< MagneticField >
TrackingAction::m_endOfTrackSignal
SimActivityRegistry::EndOfTrackSignal m_endOfTrackSignal
Definition: TrackingAction.h:29
Generator::setGenEvent
void setGenEvent(const HepMC::GenEvent *inpevt)
Definition: Generator.h:24
CustomUIsessionToFile
Definition: CustomUIsessionToFile.h:19
CustomUIsession.h
submitPVResolutionJobs.count
count
Definition: submitPVResolutionJobs.py:352
RunManagerMTWorker::DumpMagneticField
void DumpMagneticField(const G4Field *, const std::string &) const
Definition: RunManagerMTWorker.cc:618
G4SimEvent::nGenParts
unsigned int nGenParts() const
Definition: G4SimEvent.h:22
BeginOfJob
Definition: BeginOfJob.h:8
RunManagerMTWorker::m_pStackingAction
edm::ParameterSet m_pStackingAction
Definition: RunManagerMTWorker.h:97
edm::Event::getByToken
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:535
PhysicsList
Definition: PhysicsList.h:7
HLTMuonOfflineAnalyzer_cfi.z0
z0
Definition: HLTMuonOfflineAnalyzer_cfi.py:98
Point3DBase< float, GlobalTag >
edm::EventID::run
RunNumber_t run() const
Definition: EventID.h:38
g4SimHits_cfi.EventAction
EventAction
Definition: g4SimHits_cfi.py:258
sim_act::Signaler::connect
void connect(Observer< const T * > *iObs)
does not take ownership of memory
Definition: Signaler.h:55
g4SimHits_cfi.TrackingAction
TrackingAction
Definition: g4SimHits_cfi.py:299
RunManagerMTWorker::initializeRun
void initializeRun()
Definition: RunManagerMTWorker.cc:444
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
RunManagerMTWorker::m_LHCToken
edm::EDGetTokenT< edm::HepMCProduct > m_LHCToken
Definition: RunManagerMTWorker.h:85
edm::ParameterSet::exists
bool exists(std::string const &parameterName) const
checks if a parameter exists
Definition: ParameterSet.cc:681
RunManagerMTWorker::generateEvent
G4Event * generateEvent(const edm::Event &inpevt)
Definition: RunManagerMTWorker.cc:578
SimG4Exception.h
RunManagerMT
Definition: RunManagerMT.h:55
RunManagerMTWorker::m_pSteppingAction
edm::ParameterSet m_pSteppingAction
Definition: RunManagerMTWorker.h:99
RunManagerMTWorker::terminateRun
void terminateRun()
Definition: RunManagerMTWorker.cc:454
RunAction
Definition: RunAction.h:15
RunManagerMTWorker::TLSData::kernel
std::unique_ptr< G4RunManagerKernel > kernel
Definition: RunManagerMTWorker.cc:118
edm::ParameterSet
Definition: ParameterSet.h:47
EgHLTOffHistBins_cfi.nr
nr
Definition: EgHLTOffHistBins_cfi.py:4
RunManagerMTWorker::m_pField
edm::ParameterSet m_pField
Definition: RunManagerMTWorker.h:94
DDDWorld::GetWorldVolume
G4VPhysicalVolume * GetWorldVolume() const
Definition: DDDWorld.h:24
Event.h
ParameterSet
Definition: Functions.h:16
RunManagerMT.h
RunManagerMTWorker::TLSData::runTerminated
bool runTerminated
Definition: RunManagerMTWorker.cc:134
SimWatcherFactory.h
G4SimEvent.h
GeV
const double GeV
Definition: MathUtil.h:16
thread_safety_macros.h
StackingAction.h
groupFilesInBlocks.fout
fout
Definition: groupFilesInBlocks.py:162
RunManagerMTWorker::TLSData::runInterface
std::unique_ptr< SimRunInterface > runInterface
Definition: RunManagerMTWorker.cc:120
edm::EventID::event
EventNumber_t event() const
Definition: EventID.h:40
edm::Service
Definition: Service.h:30
createfilelist.int
int
Definition: createfilelist.py:10
FrontierConditions_GlobalTag_cff.file
file
Definition: FrontierConditions_GlobalTag_cff.py:13
RunManagerMT::G4Commands
const std::vector< std::string > & G4Commands() const
Definition: RunManagerMT.h:79
RunManagerMTWorker::m_nonBeam
bool m_nonBeam
Definition: RunManagerMTWorker.h:88
trackerHitRTTI::vector
Definition: trackerHitRTTI.h:21
RunManagerMTWorker::TLSData::threadInitialized
bool threadInitialized
Definition: RunManagerMTWorker.cc:133
RunManagerMTWorker::produce
std::unique_ptr< G4SimEvent > produce(const edm::Event &inpevt, const edm::EventSetup &es, RunManagerMT &runManagerMaster)
Definition: RunManagerMTWorker.cc:475
IdealGeometryRecord.h
MagneticField.h
RunManagerMTWorker::m_sVerbose
std::unique_ptr< CMSSteppingVerbose > m_sVerbose
Definition: RunManagerMTWorker.h:108
edm::EventSetup
Definition: EventSetup.h:58
CastorDigiValidation.SteppingAction
SteppingAction
Definition: CastorDigiValidation.py:147
RunManagerMTWorker::TLSData::~TLSData
~TLSData()
Definition: RunManagerMTWorker.cc:138
edm::HepMCProduct::GetEvent
const HepMC::GenEvent * GetEvent() const
Definition: HepMCProduct.h:37
createWatchers
static void createWatchers(const edm::ParameterSet &iP, SimActivityRegistry &iReg, std::vector< std::shared_ptr< SimWatcher >> &oWatchers, std::vector< std::shared_ptr< SimProducer >> &oProds)
Definition: GeometryProducer.cc:34
RunManagerMTWorker::endRun
void endRun()
Definition: RunManagerMTWorker.cc:220
get
#define get
RunManagerMTWorker::resetGenParticleId
void resetGenParticleId(const edm::Event &inpevt)
Definition: RunManagerMTWorker.cc:610
CustomUIsessionToFile.h
G4SimEvent::collisionPoint
void collisionPoint(const math::XYZTLorentzVectorD &v)
Definition: G4SimEvent.h:27
AttachSD.h
TrackingAction::m_beginOfTrackSignal
SimActivityRegistry::BeginOfTrackSignal m_beginOfTrackSignal
Definition: TrackingAction.h:28
alignCSCRings.r
r
Definition: alignCSCRings.py:93
RunAction::m_endOfRunSignal
SimActivityRegistry::EndOfRunSignal m_endOfRunSignal
Definition: RunAction.h:24
DDAxes::phi
edm::Event::streamID
StreamID streamID() const
Definition: Event.h:98
SteppingAction::m_g4StepSignal
SimActivityRegistry::G4StepSignal m_g4StepSignal
Definition: SteppingAction.h:38
ExceptionHandler.h
SimActivityRegistry
Definition: SimActivityRegistry.h:39
CMSSteppingVerbose.h
RunManagerMT::physicsListForWorker
PhysicsList * physicsListForWorker() const
Definition: RunManagerMT.h:84
SimRunInterface.h
PVValHelper::dz
Definition: PVValidationHelpers.h:51
RunManagerMTWorker::m_InToken
edm::EDGetTokenT< edm::HepMCProduct > m_InToken
Definition: RunManagerMTWorker.h:84
edm::LogVerbatim
Log< level::Info, true > LogVerbatim
Definition: MessageLogger.h:128
RunManagerMTWorker::TLSData::TLSData
TLSData()
Definition: RunManagerMTWorker.cc:136
RunManagerMTWorker::TLSData::registry
std::unique_ptr< SimActivityRegistry > registry
Definition: RunManagerMTWorker.cc:121
flavorHistoryFilter_cfi.dr
dr
Definition: flavorHistoryFilter_cfi.py:37
Exception
Definition: hltDiff.cc:245
applyOnce
static std::once_flag applyOnce
Definition: RunManagerMTWorker.cc:72
edm::EventBase::id
edm::EventID id() const
Definition: EventBase.h:59
CustomUIsessionThreadPrefix.h
G4SimEvent::weight
void weight(float w)
Definition: G4SimEvent.h:25
RunManagerMTWorker::TLSData
Definition: RunManagerMTWorker.cc:117
EventSetup.h
edm::ParameterSet::getParameter
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
RunManagerMTWorker::producers
std::vector< std::shared_ptr< SimProducer > > & producers()
Definition: RunManagerMTWorker.cc:439
RunManagerMTWorker::RunManagerMTWorker
RunManagerMTWorker(const edm::ParameterSet &iConfig, edm::ConsumesCollector &&i)
Definition: RunManagerMTWorker.cc:148
sd
double sd
Definition: CascadeWrapper.h:113
Generator::HepMC2G4
void HepMC2G4(const HepMC::GenEvent *g, G4Event *e)
Definition: Generator.cc:112
RunManagerMTWorker::TLSData::userRunAction
std::unique_ptr< RunAction > userRunAction
Definition: RunManagerMTWorker.cc:119
AttachSD
Definition: AttachSD.h:17
MillePedeFileConverter_cfg.out
out
Definition: MillePedeFileConverter_cfg.py:31
HepMCProduct
RunManagerMTWorker::TLSData::sensTkDets
std::vector< SensitiveTkDetector * > sensTkDets
Definition: RunManagerMTWorker.cc:123
RunManagerMTWorker::initializeTLS
void initializeTLS()
Definition: RunManagerMTWorker.cc:226
ConsumesCollector.h
edm::HepMCProduct
Definition: HepMCProduct.h:21
ParameterSet.h
RunManagerMTWorker::m_pTrackingAction
edm::ParameterSet m_pTrackingAction
Definition: RunManagerMTWorker.h:98
Generator::genEvent
virtual const HepMC::GenEvent * genEvent() const
Definition: Generator.h:30
RunManagerMTWorker::dumpMF
static thread_local bool dumpMF
Definition: RunManagerMTWorker.h:105
HepMCProduct.h
dqmiolumiharvest.j
j
Definition: dqmiolumiharvest.py:66
ecalTB2006H4_GenSimDigiReco_cfg.G4cout
G4cout
Definition: ecalTB2006H4_GenSimDigiReco_cfg.py:285
point
*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
edm::HandleBase::isValid
bool isValid() const
Definition: HandleBase.h:70
edm::Event
Definition: Event.h:73
submitPVValidationJobs.t
string t
Definition: submitPVValidationJobs.py:644
edm::errors::Configuration
Definition: EDMException.h:36
RunManagerMTWorker::m_pUseMagneticField
bool m_pUseMagneticField
Definition: RunManagerMTWorker.h:89
edm::ConsumesCollector
Definition: ConsumesCollector.h:45
RunManagerMTWorker::TLSData::currentRunNumber
edm::RunNumber_t currentRunNumber
Definition: RunManagerMTWorker.cc:132
PhysicsList.h
g
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
RunManagerMTWorker::m_pRunAction
edm::ParameterSet m_pRunAction
Definition: RunManagerMTWorker.h:95
sim::FieldBuilder
Definition: FieldBuilder.h:15