CMS 3D CMS Logo

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(p.getParameter<bool>("GeoFromDD4hep")) {
65  // Look for an outside SimActivityRegistry
66  // this is used by the visualization code
67 
69  if (otherRegistry)
70  m_registry.connect(*otherRegistry);
72 
74  m_sdMakers = sim::sensitiveDetectorMakers(m_p, consumesCollector(), std::vector<std::string>());
75 
76  tokMF_ = esConsumes<MagneticField, IdealMagneticFieldRecord, edm::Transition::BeginRun>();
77  if (m_pGeoFromDD4hep) {
78  tokDD4hep_ = esConsumes<cms::DDCompactView, IdealGeometryRecord, edm::Transition::BeginRun>();
79  } else {
80  tokDDD_ = esConsumes<DDCompactView, IdealGeometryRecord, edm::Transition::BeginRun>();
81  }
82  produces<int>();
83 }
84 
86 
88  if (m_pUseMagneticField) {
89  // setup the magnetic field
90  auto const &pMF = &es.getData(tokMF_);
91  const GlobalPoint g(0., 0., 0.);
92  edm::LogInfo("GeometryProducer") << "B-field(T) at (0,0,0)(cm): " << pMF->inTesla(g);
93 
94  sim::FieldBuilder fieldBuilder(pMF, m_pField);
95  CMSFieldManager *fieldManager = new CMSFieldManager();
96  G4TransportationManager *tM = G4TransportationManager::GetTransportationManager();
97  tM->SetFieldManager(fieldManager);
98  fieldBuilder.build(fieldManager, tM->GetPropagatorInField());
99  edm::LogInfo("GeometryProducer") << "Magentic field is built";
100  }
101 }
102 
104  // mag field cannot be change in new lumi section - this is commented out
105  // updateMagneticField( es );
106 }
107 
109  makeGeom(es);
111  for (auto &maker : m_sdMakers) {
112  maker.second->beginRun(es);
113  }
114 }
115 
117 
119  if (!m_firstRun)
120  return;
121  m_firstRun = false;
122  for (Producers::iterator itProd = m_producers.begin(); itProd != m_producers.end(); ++itProd) {
123  (*itProd)->produce(e, es);
124  }
125 }
127  if (!m_firstRun)
128  return;
129 
130  edm::LogVerbatim("GeometryProducer") << "Producing G4 Geom";
131 
132  m_kernel = G4RunManagerKernel::GetRunManagerKernel();
133  if (m_kernel == nullptr)
134  m_kernel = new G4RunManagerKernel();
135  edm::LogVerbatim("GeometryProducer") << " GeometryProducer initializing ";
136  // DDDWorld: get the DDCV from the ES and use it to build the World
137  if (m_pGeoFromDD4hep) {
139  } else {
140  m_pDD = &es.getData(tokDDD_);
141  }
142 
144  const DDDWorld *dddworld = new DDDWorld(m_pDD, m_pDD4hep, catalog, 1, false, false);
145  G4VPhysicalVolume *world = dddworld->GetWorldVolume();
146  if (nullptr != world)
147  edm::LogVerbatim("GeometryProducer") << " World Volume: " << world->GetName();
148  m_kernel->DefineWorldVolume(world, true);
149 
150  m_registry.dddWorldSignal_(dddworld);
151 
152  edm::LogVerbatim("GeometryProducer") << " Magnetic field initialisation";
154 
156  edm::LogInfo("GeometryProducer") << " instantiating sensitive detectors ";
157  // instantiate and attach the sensitive detectors
158  m_trackManager = std::make_unique<SimTrackManager>();
159  {
160  std::pair<std::vector<SensitiveTkDetector *>, std::vector<SensitiveCaloDetector *>> sensDets =
162 
163  m_sensTkDets.swap(sensDets.first);
164  m_sensCaloDets.swap(sensDets.second);
165  }
166 
167  edm::LogInfo("GeometryProducer") << " Sensitive Detector building finished; found " << m_sensTkDets.size()
168  << " Tk type Producers, and " << m_sensCaloDets.size() << " Calo type producers ";
169  }
170 }
171 
Log< level::Info, true > LogVerbatim
std::vector< SensitiveTkDetector * > m_sensTkDets
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
GeometryProducer(edm::ParameterSet const &p)
void endRun(const edm::Run &r, const edm::EventSetup &c) override
def create(alignables, pedeDump, additionalData, outputFile, config)
void connect(Observer< const BeginOfJob *> *iObject)
const DDCompactView * m_pDD
edm::ESGetToken< MagneticField, IdealMagneticFieldRecord > tokMF_
edm::ParameterSet m_pField
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
~GeometryProducer() override
G4VPhysicalVolume * GetWorldVolume() const
Definition: DDDWorld.h:24
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
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
bool getData(T &iHolder) const
Definition: EventSetup.h:122
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
std::unique_ptr< SimTrackManager > m_trackManager
void beginLuminosityBlock(edm::LuminosityBlock &, edm::EventSetup const &)
const cms::DDCompactView * m_pDD4hep
HLT enums.
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
void makeGeom(const edm::EventSetup &c)