CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
GeometryProducer.cc
Go to the documentation of this file.
2 
4 
8 
20 
21 #include "G4RunManagerKernel.hh"
22 #include "G4TransportationManager.hh"
23 
24 #include <iostream>
25 #include <memory>
26 
27 static void createWatchers(const edm::ParameterSet &iP,
28  SimActivityRegistry &iReg,
29  std::vector<std::shared_ptr<SimWatcher>> &oWatchers,
30  std::vector<std::shared_ptr<SimProducer>> &oProds) {
31  using namespace std;
32  using namespace edm;
33  std::vector<ParameterSet> watchers;
34  try {
35  watchers = iP.getParameter<vector<ParameterSet>>("Watchers");
36  } catch (edm::Exception const &) {
37  }
38 
39  for (std::vector<ParameterSet>::iterator itWatcher = watchers.begin(); itWatcher != watchers.end(); ++itWatcher) {
40  std::unique_ptr<SimWatcherMakerBase> maker(
41  SimWatcherFactory::get()->create(itWatcher->getParameter<std::string>("type")));
42  if (maker.get() == nullptr) {
43  throw cms::Exception("SimG4CoreGeometryProducer", " createWatchers: Unable to find the requested Watcher");
44  }
45 
46  std::shared_ptr<SimWatcher> watcherTemp;
47  std::shared_ptr<SimProducer> producerTemp;
48  maker->make(*itWatcher, iReg, watcherTemp, producerTemp);
49  oWatchers.push_back(watcherTemp);
50  if (producerTemp)
51  oProds.push_back(producerTemp);
52  }
53 }
54 
56  : m_kernel(nullptr),
57  m_pField(p.getParameter<edm::ParameterSet>("MagneticField")),
58  m_p(p),
59  m_pDD(nullptr),
60  m_pDD4hep(nullptr),
61  m_firstRun(true),
62  m_pUseMagneticField(p.getParameter<bool>("UseMagneticField")),
63  m_pUseSensitiveDetectors(p.getParameter<bool>("UseSensitiveDetectors")),
64  m_pGeoFromDD4hep(false) {
65  // Look for an outside SimActivityRegistry
66  // this is used by the visualization code
68  if (otherRegistry)
69  m_registry.connect(*otherRegistry);
71 
72  m_sdMakers = sim::sensitiveDetectorMakers(m_p, consumesCollector(), std::vector<std::string>());
73  tokMF_ = esConsumes<MagneticField, IdealMagneticFieldRecord, edm::Transition::BeginRun>();
74  if (m_pGeoFromDD4hep) {
75  tokDD4hep_ = esConsumes<cms::DDCompactView, IdealGeometryRecord, edm::Transition::BeginRun>();
76  } else {
77  tokDDD_ = esConsumes<DDCompactView, IdealGeometryRecord, edm::Transition::BeginRun>();
78  }
79  produces<int>();
80 }
81 
83 
85  if (m_pUseMagneticField) {
86  // setup the magnetic field
87  auto const &pMF = &es.getData(tokMF_);
88  const GlobalPoint g(0., 0., 0.);
89  edm::LogInfo("GeometryProducer") << "B-field(T) at (0,0,0)(cm): " << pMF->inTesla(g);
90 
91  sim::FieldBuilder fieldBuilder(pMF, m_pField);
92  CMSFieldManager *fieldManager = new CMSFieldManager();
93  G4TransportationManager *tM = G4TransportationManager::GetTransportationManager();
94  tM->SetFieldManager(fieldManager);
95  fieldBuilder.build(fieldManager, tM->GetPropagatorInField());
96  edm::LogInfo("GeometryProducer") << "Magentic field is built";
97  }
98 }
99 
101  // mag field cannot be change in new lumi section - this is commented out
102  // updateMagneticField( es );
103 }
104 
107  for (auto &maker : m_sdMakers) {
108  maker.second->beginRun(es);
109  }
110 }
111 
113 
115  if (!m_firstRun)
116  return;
117  m_firstRun = false;
118 
119  edm::LogVerbatim("GeometryProducer") << "Producing G4 Geom";
120 
121  m_kernel = G4RunManagerKernel::GetRunManagerKernel();
122  if (m_kernel == nullptr)
123  m_kernel = new G4RunManagerKernel();
124  edm::LogVerbatim("GeometryProducer") << " GeometryProducer initializing ";
125  // DDDWorld: get the DDCV from the ES and use it to build the World
126  if (m_pGeoFromDD4hep) {
128  } else {
129  m_pDD = &es.getData(tokDDD_);
130  }
131 
133  const DDDWorld *dddworld = new DDDWorld(m_pDD, m_pDD4hep, catalog, 1, false, false);
134  G4VPhysicalVolume *world = dddworld->GetWorldVolume();
135  if (nullptr != world)
136  edm::LogVerbatim("GeometryProducer") << " World Volume: " << world->GetName();
137  m_kernel->DefineWorldVolume(world, true);
138 
139  m_registry.dddWorldSignal_(dddworld);
140 
141  edm::LogVerbatim("GeometryProducer") << " Magnetic field initialisation";
143 
145  edm::LogInfo("GeometryProducer") << " instantiating sensitive detectors ";
146  // instantiate and attach the sensitive detectors
147  m_trackManager = std::make_unique<SimTrackManager>();
148  {
149  std::pair<std::vector<SensitiveTkDetector *>, std::vector<SensitiveCaloDetector *>> sensDets =
150  sim::attachSD(m_sdMakers, es, catalog, m_p, m_trackManager.get(), m_registry);
151 
152  m_sensTkDets.swap(sensDets.first);
153  m_sensCaloDets.swap(sensDets.second);
154  }
155 
156  edm::LogInfo("GeometryProducer") << " Sensitive Detector building finished; found " << m_sensTkDets.size()
157  << " Tk type Producers, and " << m_sensCaloDets.size() << " Calo type producers ";
158  }
159 
160  for (Producers::iterator itProd = m_producers.begin(); itProd != m_producers.end(); ++itProd) {
161  (*itProd)->produce(e, es);
162  }
163 }
164 
Log< level::Info, true > LogVerbatim
std::vector< SensitiveTkDetector * > m_sensTkDets
GeometryProducer(edm::ParameterSet const &p)
void endRun(const edm::Run &r, const edm::EventSetup &c) override
const DDCompactView * m_pDD
edm::ESGetToken< MagneticField, IdealMagneticFieldRecord > tokMF_
void connect(Observer< const BeginOfJob * > *iObject)
edm::ParameterSet m_pField
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
~GeometryProducer() override
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
bool getData(T &iHolder) const
Definition: EventSetup.h:122
void beginRun(const edm::Run &r, const edm::EventSetup &c) override
std::vector< std::shared_ptr< SimWatcher > > m_watchers
std::vector< SensitiveCaloDetector * > m_sensCaloDets
ConsumesCollector consumesCollector()
Use a ConsumesCollector to gather consumes information from helper functions.
G4RunManagerKernel * m_kernel
G4VPhysicalVolume * GetWorldVolume() const
Definition: DDDWorld.h:24
edm::ESGetToken< cms::DDCompactView, IdealGeometryRecord > tokDD4hep_
std::vector< std::shared_ptr< SimProducer > > m_producers
SimActivityRegistry m_registry
void produce(edm::Event &e, const edm::EventSetup &c) override
void build(CMSFieldManager *fM, G4PropagatorInField *fP)
Definition: FieldBuilder.cc:31
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)
Log< level::Info, false > LogInfo
void updateMagneticField(edm::EventSetup const &es)
edm::ESGetToken< DDCompactView, IdealGeometryRecord > tokDDD_
DDDWorldSignal dddWorldSignal_
std::unordered_map< std::string, std::unique_ptr< SensitiveDetectorMakerBase > > m_sdMakers
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
std::unique_ptr< SimTrackManager > m_trackManager
void beginLuminosityBlock(edm::LuminosityBlock &, edm::EventSetup const &)
const cms::DDCompactView * m_pDD4hep
std::unordered_map< std::string, std::unique_ptr< SensitiveDetectorMakerBase > > sensitiveDetectorMakers(edm::ParameterSet const &, edm::ConsumesCollector, std::vector< std::string > const &chosenMakers)
static void createWatchers(const edm::ParameterSet &iP, SimActivityRegistry &iReg, std::vector< std::shared_ptr< SimWatcher >> &oWatchers, std::vector< std::shared_ptr< SimProducer >> &oProds)
#define get
edm::ParameterSet m_p
Definition: Run.h:45