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 #include "G4EmParameters.hh"
24 #include "G4HadronicParameters.hh"
25 
26 #include <iostream>
27 #include <memory>
28 
29 static void createWatchers(const edm::ParameterSet &iP,
30  SimActivityRegistry &iReg,
31  std::vector<std::shared_ptr<SimWatcher>> &oWatchers,
32  std::vector<std::shared_ptr<SimProducer>> &oProds) {
33  using namespace std;
34  using namespace edm;
35  std::vector<ParameterSet> watchers;
36  try {
37  watchers = iP.getParameter<vector<ParameterSet>>("Watchers");
38  } catch (edm::Exception const &) {
39  }
40 
41  for (std::vector<ParameterSet>::iterator itWatcher = watchers.begin(); itWatcher != watchers.end(); ++itWatcher) {
42  std::unique_ptr<SimWatcherMakerBase> maker(
43  SimWatcherFactory::get()->create(itWatcher->getParameter<std::string>("type")));
44  if (maker.get() == nullptr) {
45  throw cms::Exception("SimG4CoreGeometryProducer", " createWatchers: Unable to find the requested Watcher");
46  }
47 
48  std::shared_ptr<SimWatcher> watcherTemp;
49  std::shared_ptr<SimProducer> producerTemp;
50  maker->make(*itWatcher, iReg, watcherTemp, producerTemp);
51  oWatchers.push_back(watcherTemp);
52  if (producerTemp)
53  oProds.push_back(producerTemp);
54  }
55 }
56 
58  : m_kernel(nullptr),
59  m_pField(p.getParameter<edm::ParameterSet>("MagneticField")),
60  m_p(p),
61  m_pDD(nullptr),
62  m_pDD4hep(nullptr),
63  m_verbose(0),
64  m_firstRun(true),
65  m_pUseMagneticField(p.getParameter<bool>("UseMagneticField")),
66  m_pUseSensitiveDetectors(p.getParameter<bool>("UseSensitiveDetectors")),
67  m_pGeoFromDD4hep(p.getParameter<bool>("GeoFromDD4hep")) {
68  // Look for an outside SimActivityRegistry
69  // this is used by the visualization code
70 
72  if (otherRegistry)
73  m_registry.connect(*otherRegistry);
75 
76  G4EmParameters::Instance()->SetVerbose(m_verbose);
77  G4HadronicParameters::Instance()->SetVerboseLevel(m_verbose);
78 
79  m_kernel = G4RunManagerKernel::GetRunManagerKernel();
80  if (m_kernel == nullptr)
81  m_kernel = new G4RunManagerKernel();
82 
83  m_kernel->SetVerboseLevel(m_verbose);
84 
85  //if (m_pUseSensitiveDetectors)
86  // m_sdMakers = sim::sensitiveDetectorMakers(m_p, consumesCollector(), std::vector<std::string>());
87 
88  tokMF_ = esConsumes<MagneticField, IdealMagneticFieldRecord, edm::Transition::BeginRun>();
89  if (m_pGeoFromDD4hep) {
90  tokDD4hep_ = esConsumes<cms::DDCompactView, IdealGeometryRecord, edm::Transition::BeginRun>();
91  } else {
92  tokDDD_ = esConsumes<DDCompactView, IdealGeometryRecord, edm::Transition::BeginRun>();
93  }
94  produces<int>();
95 }
96 
98 
100  if (m_pUseMagneticField) {
101  // setup the magnetic field
102  auto const &pMF = &es.getData(tokMF_);
103  const GlobalPoint g(0., 0., 0.);
104  edm::LogInfo("GeometryProducer") << "B-field(T) at (0,0,0)(cm): " << pMF->inTesla(g);
105 
106  sim::FieldBuilder fieldBuilder(pMF, m_pField);
107  CMSFieldManager *fieldManager = new CMSFieldManager();
108  G4TransportationManager *tM = G4TransportationManager::GetTransportationManager();
109  tM->SetFieldManager(fieldManager);
110  fieldBuilder.build(fieldManager, tM->GetPropagatorInField());
111  edm::LogInfo("GeometryProducer") << "Magentic field is built";
112  }
113 }
114 
116  // mag field cannot be change in new lumi section - this is commented out
117  // updateMagneticField( es );
118 }
119 
121  makeGeom(es);
123  for (auto &maker : m_sdMakers) {
124  maker.second->beginRun(es);
125  }
126 }
127 
129 
131  if (!m_firstRun)
132  return;
133  m_firstRun = false;
134  for (Producers::iterator itProd = m_producers.begin(); itProd != m_producers.end(); ++itProd) {
135  (*itProd)->produce(e, es);
136  }
137 }
138 
140  if (!m_firstRun)
141  return;
142 
143  edm::LogVerbatim("GeometryProducer") << " GeometryProducer initializing ";
144  // DDDWorld: get the DDCV from the ES and use it to build the World
145  if (m_pGeoFromDD4hep) {
147  } else {
148  m_pDD = &es.getData(tokDDD_);
149  }
150 
152  const DDDWorld *dddworld = new DDDWorld(m_pDD, m_pDD4hep, catalog, m_verbose, false, false);
153  G4VPhysicalVolume *world = dddworld->GetWorldVolume();
154  if (nullptr != world)
155  edm::LogVerbatim("GeometryProducer") << " World Volume: " << world->GetName();
156  m_kernel->DefineWorldVolume(world, true);
157 
158  m_registry.dddWorldSignal_(dddworld);
159 
160  edm::LogVerbatim("GeometryProducer") << " Magnetic field initialisation";
162 
164  edm::LogInfo("GeometryProducer") << " instantiating sensitive detectors ";
165  // instantiate and attach the sensitive detectors
166  m_trackManager = std::make_unique<SimTrackManager>();
167  {
168  std::pair<std::vector<SensitiveTkDetector *>, std::vector<SensitiveCaloDetector *>> sensDets =
170 
171  m_sensTkDets.swap(sensDets.first);
172  m_sensCaloDets.swap(sensDets.second);
173  }
174 
175  edm::LogInfo("GeometryProducer") << " Sensitive Detector building finished; found " << m_sensTkDets.size()
176  << " Tk type Producers, and " << m_sensCaloDets.size() << " Calo type producers ";
177  }
178 }
179 
Log< level::Info, true > LogVerbatim
std::vector< SensitiveTkDetector * > m_sensTkDets
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
GeometryProducer(edm::ParameterSet const &p)
void endRun(const edm::Run &r, const edm::EventSetup &c) override
T const & getData(const ESGetToken< T, R > &iToken) const noexcept(false)
Definition: EventSetup.h:119
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
~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
G4RunManagerKernel * m_kernel
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
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:32
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.
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)