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  tokMF_ = esConsumes<MagneticField, IdealMagneticFieldRecord, edm::Transition::BeginRun>();
86  if (m_pGeoFromDD4hep) {
87  tokDD4hep_ = esConsumes<cms::DDCompactView, IdealGeometryRecord, edm::Transition::BeginRun>();
88  } else {
89  tokDDD_ = esConsumes<DDCompactView, IdealGeometryRecord, edm::Transition::BeginRun>();
90  }
91  produces<int>();
92 }
93 
95 
97  if (m_pUseMagneticField) {
98  // setup the magnetic field
99  auto const &pMF = &es.getData(tokMF_);
100  const GlobalPoint g(0., 0., 0.);
101  edm::LogVerbatim("GeometryProducer") << "B-field(T) at (0,0,0)(cm): " << pMF->inTesla(g);
102 
103  sim::FieldBuilder fieldBuilder(pMF, m_pField);
104  CMSFieldManager *fieldManager = new CMSFieldManager();
105  G4TransportationManager *tM = G4TransportationManager::GetTransportationManager();
106  tM->SetFieldManager(fieldManager);
107  fieldBuilder.build(fieldManager, tM->GetPropagatorInField());
108  edm::LogVerbatim("GeometryProducer") << "Magentic field is built";
109  }
110 }
111 
113  // mag field cannot be change in new lumi section - this is commented out
114  // updateMagneticField( es );
115 }
116 
118  makeGeom(es);
120  for (auto &maker : m_sdMakers) {
121  maker.second->beginRun(es);
122  }
123 }
124 
126 
128  if (!m_firstRun)
129  return;
130  m_firstRun = false;
131  for (Producers::iterator itProd = m_producers.begin(); itProd != m_producers.end(); ++itProd) {
132  (*itProd)->produce(e, es);
133  }
134 }
135 
137  if (!m_firstRun)
138  return;
139 
140  edm::LogVerbatim("GeometryProducer") << " GeometryProducer initializing ";
141  // DDDWorld: get the DDCV from the ES and use it to build the World
142  if (m_pGeoFromDD4hep) {
144  } else {
145  m_pDD = &es.getData(tokDDD_);
146  }
147 
149  const DDDWorld *dddworld = new DDDWorld(m_pDD, m_pDD4hep, catalog, m_verbose, false, false);
150  G4VPhysicalVolume *world = dddworld->GetWorldVolume();
151  if (nullptr != world)
152  edm::LogVerbatim("GeometryProducer") << " World Volume: " << world->GetName();
153  m_kernel->DefineWorldVolume(world, true);
154 
155  m_registry.dddWorldSignal_(dddworld);
156 
157  edm::LogVerbatim("GeometryProducer") << " Magnetic field initialisation";
159 
161  edm::LogVerbatim("GeometryProducer") << " instantiating sensitive detectors ";
162  // instantiate and attach the sensitive detectors
163  TmpSimEvent *ptr = nullptr;
164  m_trackManager = std::make_unique<SimTrackManager>(ptr, m_verbose);
165  {
166  std::pair<std::vector<SensitiveTkDetector *>, std::vector<SensitiveCaloDetector *>> sensDets =
168 
169  m_sensTkDets.swap(sensDets.first);
170  m_sensCaloDets.swap(sensDets.second);
171  }
172 
173  edm::LogVerbatim("GeometryProducer") << " Sensitive Detector building finished; found " << m_sensTkDets.size()
174  << " Tk type Producers, and " << m_sensCaloDets.size()
175  << " Calo type producers ";
176  }
177 }
178 
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
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)
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)