CMS 3D CMS Logo

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