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  G4StateManager::GetStateManager()->SetExceptionHandler(new ExceptionHandler());
304 
305  // Set the geometry for the worker, share from master
306  auto worldPV = runManagerMaster->world().GetWorldVolume();
307  m_tls->kernel->WorkerDefineWorldVolume(worldPV);
308  G4TransportationManager* tM = G4TransportationManager::GetTransportationManager();
309  tM->SetWorldForTracking(worldPV);
310 
311  // we need the track manager now
312  m_tls->trackManager = std::make_unique<SimTrackManager>();
313 
314  // setup the magnetic field
315  if (m_pUseMagneticField) {
316  const GlobalPoint g(0.f, 0.f, 0.f);
317 
318  sim::FieldBuilder fieldBuilder(m_pMagField, m_pField);
319 
320  CMSFieldManager* fieldManager = new CMSFieldManager();
321  tM->SetFieldManager(fieldManager);
322  fieldBuilder.build(fieldManager, tM->GetPropagatorInField());
323 
324  std::string fieldFile = m_p.getUntrackedParameter<std::string>("FileNameField", "");
325  if (!fieldFile.empty()) {
326  std::call_once(applyOnce, []() { dumpMF = true; });
327  if (dumpMF) {
328  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMTWorker: Dump magnetic field to file " << fieldFile;
329  DumpMagneticField(tM->GetFieldManager()->GetDetectorField(), fieldFile);
330  }
331  }
332  }
333 
334  // attach sensitive detector
335  auto sensDets = sim::attachSD(
336  m_sdMakers, es, runManagerMaster->catalog(), m_p, m_tls->trackManager.get(), *(m_tls->registry.get()));
337 
338  m_tls->sensTkDets.swap(sensDets.first);
339  m_tls->sensCaloDets.swap(sensDets.second);
340 
341  edm::LogVerbatim("SimG4CoreApplication")
342  << "RunManagerMTWorker: Sensitive Detectors are built in thread " << thisID << " found "
343  << m_tls->sensTkDets.size() << " Tk type SD, and " << m_tls->sensCaloDets.size() << " Calo type SD";
344 
345  // Set the physics list for the worker, share from master
346  PhysicsList* physicsList = runManagerMaster->physicsListForWorker();
347 
348  edm::LogVerbatim("SimG4CoreApplication")
349  << "RunManagerMTWorker: start initialisation of PhysicsList for the thread " << thisID;
350 
351  // Geant4 UI commands in PreInit state
352  if (!runManagerMaster->G4Commands().empty()) {
353  G4cout << "RunManagerMTWorker: Requested UI commands: " << G4endl;
354  for (const std::string& command : runManagerMaster->G4Commands()) {
355  G4cout << " " << command << G4endl;
356  G4UImanager::GetUIpointer()->ApplyCommand(command);
357  }
358  }
359  G4StateManager::GetStateManager()->SetNewState(G4State_Init);
360 
361  physicsList->InitializeWorker();
362  m_tls->kernel->SetPhysics(physicsList);
363  m_tls->kernel->InitializePhysics();
364 
365  if (!m_tls->kernel->RunInitialization()) {
367  << "RunManagerMTWorker: Geant4 kernel initialization failed in thread " << thisID;
368  }
369  //tell all interesting parties that we are beginning the job
370  BeginOfJob aBeginOfJob(&es);
371  m_tls->registry->beginOfJobSignal_(&aBeginOfJob);
372 
373  G4int sv = m_p.getUntrackedParameter<int>("SteppingVerbosity", 0);
374  G4double elim = m_p.getUntrackedParameter<double>("StepVerboseThreshold", 0.1) * CLHEP::GeV;
375  std::vector<int> ve = m_p.getUntrackedParameter<std::vector<int>>("VerboseEvents");
376  std::vector<int> vn = m_p.getUntrackedParameter<std::vector<int>>("VertexNumber");
377  std::vector<int> vt = m_p.getUntrackedParameter<std::vector<int>>("VerboseTracks");
378 
379  if (sv > 0) {
380  m_sVerbose = std::make_unique<CMSSteppingVerbose>(sv, elim, ve, vn, vt);
381  }
383 
384  G4StateManager::GetStateManager()->SetNewState(G4State_Idle);
385  m_tls->threadInitialized = true;
386 
387  timer.Stop();
388  edm::LogVerbatim("SimG4CoreApplication")
389  << "RunManagerMTWorker::initializeThread done for the thread " << thisID << " " << timer;
390 }
391 
393  m_tls->runInterface = std::make_unique<SimRunInterface>(this, false);
394  m_tls->userRunAction = std::make_unique<RunAction>(m_pRunAction, m_tls->runInterface.get(), false);
395  m_tls->userRunAction->SetMaster(false);
396  Connect(m_tls->userRunAction.get());
397 
398  G4EventManager* eventManager = m_tls->kernel->GetEventManager();
399  eventManager->SetVerboseLevel(m_EvtMgrVerbosity);
400 
401  EventAction* userEventAction =
403  Connect(userEventAction);
404  eventManager->SetUserAction(userEventAction);
405 
406  TrackingAction* userTrackingAction = new TrackingAction(userEventAction, m_pTrackingAction, m_sVerbose.get());
407  Connect(userTrackingAction);
408  eventManager->SetUserAction(userTrackingAction);
409 
410  SteppingAction* userSteppingAction =
411  new SteppingAction(userEventAction, m_pSteppingAction, m_sVerbose.get(), m_hasWatchers);
412  Connect(userSteppingAction);
413  eventManager->SetUserAction(userSteppingAction);
414 
415  eventManager->SetUserAction(new StackingAction(userTrackingAction, m_pStackingAction, m_sVerbose.get()));
416 }
417 
419  runAction->m_beginOfRunSignal.connect(m_tls->registry->beginOfRunSignal_);
420  runAction->m_endOfRunSignal.connect(m_tls->registry->endOfRunSignal_);
421 }
422 
424  eventAction->m_beginOfEventSignal.connect(m_tls->registry->beginOfEventSignal_);
425  eventAction->m_endOfEventSignal.connect(m_tls->registry->endOfEventSignal_);
426 }
427 
429  trackingAction->m_beginOfTrackSignal.connect(m_tls->registry->beginOfTrackSignal_);
430  trackingAction->m_endOfTrackSignal.connect(m_tls->registry->endOfTrackSignal_);
431 }
432 
434  steppingAction->m_g4StepSignal.connect(m_tls->registry->g4StepSignal_);
435 }
436 
438  initializeTLS();
439  return m_tls->trackManager.get();
440 }
441 std::vector<SensitiveTkDetector*>& RunManagerMTWorker::sensTkDetectors() {
442  initializeTLS();
443  return m_tls->sensTkDets;
444 }
445 std::vector<SensitiveCaloDetector*>& RunManagerMTWorker::sensCaloDetectors() {
446  initializeTLS();
447  return m_tls->sensCaloDets;
448 }
449 std::vector<std::shared_ptr<SimProducer>>& RunManagerMTWorker::producers() {
450  initializeTLS();
451  return m_tls->producers;
452 }
453 
455  int thisID = getThreadIndex();
456  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMTWorker::initializeRun " << thisID << " is started";
457  m_tls->currentRun = new G4Run();
458  G4StateManager::GetStateManager()->SetNewState(G4State_GeomClosed);
459  if (nullptr != m_tls->userRunAction) {
460  m_tls->userRunAction->BeginOfRunAction(m_tls->currentRun);
461  }
462 }
463 
465  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMTWorker::terminateRun ";
466  if (nullptr == m_tls || m_tls->runTerminated) {
467  return;
468  }
469  int thisID = getThreadIndex();
470  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMTWorker::terminateRun " << thisID << " is started";
471  if (m_tls->userRunAction) {
472  m_tls->userRunAction->EndOfRunAction(m_tls->currentRun);
473  m_tls->userRunAction.reset();
474  }
475  m_tls->currentEvent.reset();
476  m_simEvent = nullptr;
477 
478  if (m_tls->kernel) {
479  m_tls->kernel->RunTermination();
480  }
481 
482  m_tls->runTerminated = true;
483 }
484 
485 std::unique_ptr<G4SimEvent> RunManagerMTWorker::produce(const edm::Event& inpevt,
486  const edm::EventSetup& es,
487  RunManagerMT& runManagerMaster) {
488  // The initialization and begin/end run is a bit convoluted due to
489  // - Geant4 deals per-thread
490  // - OscarMTProducer deals per-stream
491  // and framework/TBB is free to schedule work in streams to the
492  // threads as it likes.
493  //
494  // We have to do the per-thread initialization, and per-thread
495  // per-run initialization here by ourselves.
496 
497  if (nullptr == m_tls || !m_tls->threadInitialized) {
498  edm::LogVerbatim("SimG4CoreApplication")
499  << "RunManagerMTWorker::produce(): stream " << inpevt.streamID() << " thread " << getThreadIndex()
500  << " Geant4 initialisation for this thread";
501  initializeG4(&runManagerMaster, es);
502  m_tls->threadInitialized = true;
503  }
504  // Initialize run
505  if (inpevt.id().run() != m_tls->currentRunNumber) {
506  edm::LogVerbatim("SimG4CoreApplication")
507  << "RunID= " << inpevt.id().run() << " TLS RunID= " << m_tls->currentRunNumber;
508  if (m_tls->currentRunNumber != 0 && !m_tls->runTerminated) {
509  // If previous run in this thread was not terminated via endRun() call,
510  // terminate it now
511  terminateRun();
512  }
513  initializeRun();
514  m_tls->currentRunNumber = inpevt.id().run();
515  }
516  m_tls->runInterface->setRunManagerMTWorker(this); // For UserActions
517 
518  m_tls->currentEvent.reset(generateEvent(inpevt));
519 
520  auto simEvent = std::make_unique<G4SimEvent>();
521  m_simEvent = simEvent.get();
524  if (m_generator.genVertex() != nullptr) {
525  auto genVertex = m_generator.genVertex();
526  m_simEvent->collisionPoint(math::XYZTLorentzVectorD(genVertex->x() / CLHEP::cm,
527  genVertex->y() / CLHEP::cm,
528  genVertex->z() / CLHEP::cm,
529  genVertex->t() / CLHEP::second));
530  }
531  if (m_tls->currentEvent->GetNumberOfPrimaryVertex() == 0) {
532  std::stringstream ss;
533  ss << "RunManagerMTWorker::produce: event " << inpevt.id().event() << " with no G4PrimaryVertices \n";
534  throw SimG4Exception(ss.str());
535 
536  } else {
537  edm::LogVerbatim("SimG4CoreApplication")
538  << "RunManagerMTWorker::produce: start EventID=" << inpevt.id().event() << " StreamID=" << inpevt.streamID()
539  << " threadIndex=" << getThreadIndex() << " weight=" << m_simEvent->weight() << "; "
540  << m_tls->currentEvent->GetNumberOfPrimaryVertex() << " vertices for Geant4; generator produced "
541  << m_simEvent->nGenParts() << " particles.";
542 
543  m_tls->kernel->GetEventManager()->ProcessOneEvent(m_tls->currentEvent.get());
544  }
545 
546  //remove memory only needed during event processing
547  m_tls->currentEvent.reset();
548 
549  for (auto& sd : m_tls->sensCaloDets) {
550  sd->reset();
551  }
552  edm::LogVerbatim("SimG4CoreApplication") << "RunManagerMTWorker::produce: ended Event " << inpevt.id().event();
553 
554  m_simEvent = nullptr;
555  return simEvent;
556 }
557 
559  if (m_tls->runTerminated) {
560  return;
561  }
562  G4Track* t = m_tls->kernel->GetEventManager()->GetTrackingManager()->GetTrack();
563  t->SetTrackStatus(fStopAndKill);
564 
565  // CMS-specific act
566  //
567  TrackingAction* uta = static_cast<TrackingAction*>(m_tls->kernel->GetEventManager()->GetUserTrackingAction());
569 
570  m_tls->currentEvent->SetEventAborted();
571  m_tls->kernel->GetEventManager()->GetStackManager()->clear();
572  m_tls->kernel->GetEventManager()->GetTrackingManager()->EventAborted();
573 }
574 
575 void RunManagerMTWorker::abortRun(bool softAbort) {
576  if (!softAbort) {
577  abortEvent();
578  }
579  m_tls->currentRun = nullptr;
580  terminateRun();
581 }
582 
584  m_tls->currentEvent.reset();
585  m_simEvent = nullptr;
586 
587  // 64 bits event ID in CMSSW converted into Geant4 event ID
588  G4int evtid = (G4int)inpevt.id().event();
589  G4Event* evt = new G4Event(evtid);
590 
592  inpevt.getByToken(m_InToken, HepMCEvt);
593 
594  m_generator.setGenEvent(HepMCEvt->GetEvent());
595 
596  // required to reset the GenParticle Id for particles transported
597  // along the beam pipe
598  // to their original value for SimTrack creation
599  resetGenParticleId(inpevt);
600 
601  if (!m_nonBeam) {
602  m_generator.HepMC2G4(HepMCEvt->GetEvent(), evt);
603  if (m_LHCTransport) {
605  inpevt.getByToken(m_LHCToken, LHCMCEvt);
606  m_generator.nonCentralEvent2G4(LHCMCEvt->GetEvent(), evt);
607  }
608  } else {
609  m_generator.nonCentralEvent2G4(HepMCEvt->GetEvent(), evt);
610  }
611 
612  return evt;
613 }
614 
617  inpevt.getByToken(m_theLHCTlinkToken, theLHCTlink);
618  if (theLHCTlink.isValid()) {
619  m_tls->trackManager->setLHCTransportLink(theLHCTlink.product());
620  }
621 }
622 
623 void RunManagerMTWorker::DumpMagneticField(const G4Field* field, const std::string& file) const {
624  std::ofstream fout(file.c_str(), std::ios::out);
625  if (fout.fail()) {
626  edm::LogWarning("SimG4CoreApplication")
627  << " RunManager WARNING : error opening file <" << file << "> for magnetic field";
628  } else {
629  // CMS magnetic field volume
630  double rmax = 9000 * mm;
631  double zmax = 24000 * mm;
632 
633  double dr = 1 * cm;
634  double dz = 5 * cm;
635 
636  int nr = (int)(rmax / dr);
637  int nz = 2 * (int)(zmax / dz);
638 
639  double r = 0.0;
640  double z0 = -zmax;
641  double z;
642 
643  double phi = 0.0;
644  double cosf = cos(phi);
645  double sinf = sin(phi);
646 
647  double point[4] = {0.0, 0.0, 0.0, 0.0};
648  double bfield[3] = {0.0, 0.0, 0.0};
649 
650  fout << std::setprecision(6);
651  for (int i = 0; i <= nr; ++i) {
652  z = z0;
653  for (int j = 0; j <= nz; ++j) {
654  point[0] = r * cosf;
655  point[1] = r * sinf;
656  point[2] = z;
657  field->GetFieldValue(point, bfield);
658  fout << "R(mm)= " << r / mm << " phi(deg)= " << phi / degree << " Z(mm)= " << z / mm
659  << " Bz(tesla)= " << bfield[2] / tesla << " Br(tesla)= " << (bfield[0] * cosf + bfield[1] * sinf) / tesla
660  << " Bphi(tesla)= " << (bfield[0] * sinf - bfield[1] * cosf) / tesla << G4endl;
661  z += dz;
662  }
663  r += dr;
664  }
665 
666  fout.close();
667  }
668 }
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:441
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:575
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:558
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:418
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:437
RunManagerMTWorker::~RunManagerMTWorker
~RunManagerMTWorker()
Definition: RunManagerMTWorker.cc:183
RunManagerMTWorker::sensCaloDetectors
std::vector< SensitiveCaloDetector * > & sensCaloDetectors()
Definition: RunManagerMTWorker.cc:445
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:392
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:120
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:623
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:265
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:306
RunManagerMTWorker::initializeRun
void initializeRun()
Definition: RunManagerMTWorker.cc:454
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:583
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:464
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:485
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:615
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:449
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