File indexing completed on 2024-04-06 12:30:24
0001 #include "FWCore/PluginManager/interface/PluginManager.h"
0002
0003 #include "SimG4Core/GeometryProducer/interface/GeometryProducer.h"
0004
0005 #include "FWCore/Framework/interface/ConsumesCollector.h"
0006 #include "FWCore/ServiceRegistry/interface/Service.h"
0007 #include "FWCore/Utilities/interface/Exception.h"
0008
0009 #include "SimG4Core/Geometry/interface/DDDWorld.h"
0010 #include "SimG4Core/Geometry/interface/G4LogicalVolumeToDDLogicalPartMap.h"
0011 #include "SimG4Core/Geometry/interface/SensitiveDetectorCatalog.h"
0012 #include "SimG4Core/MagneticField/interface/CMSFieldManager.h"
0013 #include "SimG4Core/MagneticField/interface/Field.h"
0014 #include "SimG4Core/MagneticField/interface/FieldBuilder.h"
0015 #include "SimG4Core/Notification/interface/SimTrackManager.h"
0016 #include "SimG4Core/Watcher/interface/SimProducer.h"
0017 #include "SimG4Core/Watcher/interface/SimWatcherFactory.h"
0018 #include "SimG4Core/SensitiveDetector/interface/sensitiveDetectorMakers.h"
0019 #include "SimG4Core/SensitiveDetector/interface/AttachSD.h"
0020
0021 #include "G4RunManagerKernel.hh"
0022 #include "G4TransportationManager.hh"
0023 #include "G4EmParameters.hh"
0024 #include "G4HadronicParameters.hh"
0025
0026 #include <iostream>
0027 #include <memory>
0028
0029 static void createWatchers(const edm::ParameterSet &iP,
0030 SimActivityRegistry &iReg,
0031 std::vector<std::shared_ptr<SimWatcher>> &oWatchers,
0032 std::vector<std::shared_ptr<SimProducer>> &oProds) {
0033 using namespace std;
0034 using namespace edm;
0035 std::vector<ParameterSet> watchers;
0036 try {
0037 watchers = iP.getParameter<vector<ParameterSet>>("Watchers");
0038 } catch (edm::Exception const &) {
0039 }
0040
0041 for (std::vector<ParameterSet>::iterator itWatcher = watchers.begin(); itWatcher != watchers.end(); ++itWatcher) {
0042 std::unique_ptr<SimWatcherMakerBase> maker(
0043 SimWatcherFactory::get()->create(itWatcher->getParameter<std::string>("type")));
0044 if (maker.get() == nullptr) {
0045 throw cms::Exception("SimG4CoreGeometryProducer", " createWatchers: Unable to find the requested Watcher");
0046 }
0047
0048 std::shared_ptr<SimWatcher> watcherTemp;
0049 std::shared_ptr<SimProducer> producerTemp;
0050 maker->make(*itWatcher, iReg, watcherTemp, producerTemp);
0051 oWatchers.push_back(watcherTemp);
0052 if (producerTemp)
0053 oProds.push_back(producerTemp);
0054 }
0055 }
0056
0057 GeometryProducer::GeometryProducer(edm::ParameterSet const &p)
0058 : m_kernel(nullptr),
0059 m_pField(p.getParameter<edm::ParameterSet>("MagneticField")),
0060 m_p(p),
0061 m_pDD(nullptr),
0062 m_pDD4hep(nullptr),
0063 m_verbose(0),
0064 m_firstRun(true),
0065 m_pUseMagneticField(p.getParameter<bool>("UseMagneticField")),
0066 m_pUseSensitiveDetectors(p.getParameter<bool>("UseSensitiveDetectors")),
0067 m_pGeoFromDD4hep(p.getParameter<bool>("GeoFromDD4hep")) {
0068
0069
0070
0071 edm::Service<SimActivityRegistry> otherRegistry;
0072 if (otherRegistry)
0073 m_registry.connect(*otherRegistry);
0074 createWatchers(m_p, m_registry, m_watchers, m_producers);
0075
0076 G4EmParameters::Instance()->SetVerbose(m_verbose);
0077 G4HadronicParameters::Instance()->SetVerboseLevel(m_verbose);
0078
0079 m_kernel = G4RunManagerKernel::GetRunManagerKernel();
0080 if (m_kernel == nullptr)
0081 m_kernel = new G4RunManagerKernel();
0082
0083 m_kernel->SetVerboseLevel(m_verbose);
0084
0085 tokMF_ = esConsumes<MagneticField, IdealMagneticFieldRecord, edm::Transition::BeginRun>();
0086 if (m_pGeoFromDD4hep) {
0087 tokDD4hep_ = esConsumes<cms::DDCompactView, IdealGeometryRecord, edm::Transition::BeginRun>();
0088 } else {
0089 tokDDD_ = esConsumes<DDCompactView, IdealGeometryRecord, edm::Transition::BeginRun>();
0090 }
0091 produces<int>();
0092 }
0093
0094 GeometryProducer::~GeometryProducer() { delete m_kernel; }
0095
0096 void GeometryProducer::updateMagneticField(edm::EventSetup const &es) {
0097 if (m_pUseMagneticField) {
0098
0099 auto const &pMF = &es.getData(tokMF_);
0100 const GlobalPoint g(0., 0., 0.);
0101 edm::LogVerbatim("GeometryProducer") << "B-field(T) at (0,0,0)(cm): " << pMF->inTesla(g);
0102
0103 sim::FieldBuilder fieldBuilder(pMF, m_pField);
0104 CMSFieldManager *fieldManager = new CMSFieldManager();
0105 G4TransportationManager *tM = G4TransportationManager::GetTransportationManager();
0106 tM->SetFieldManager(fieldManager);
0107 fieldBuilder.build(fieldManager, tM->GetPropagatorInField());
0108 edm::LogVerbatim("GeometryProducer") << "Magentic field is built";
0109 }
0110 }
0111
0112 void GeometryProducer::beginLuminosityBlock(edm::LuminosityBlock &, edm::EventSetup const &) {
0113
0114
0115 }
0116
0117 void GeometryProducer::beginRun(const edm::Run &run, const edm::EventSetup &es) {
0118 makeGeom(es);
0119 updateMagneticField(es);
0120 for (auto &maker : m_sdMakers) {
0121 maker.second->beginRun(es);
0122 }
0123 }
0124
0125 void GeometryProducer::endRun(const edm::Run &, const edm::EventSetup &) {}
0126
0127 void GeometryProducer::produce(edm::Event &e, const edm::EventSetup &es) {
0128 if (!m_firstRun)
0129 return;
0130 m_firstRun = false;
0131 for (Producers::iterator itProd = m_producers.begin(); itProd != m_producers.end(); ++itProd) {
0132 (*itProd)->produce(e, es);
0133 }
0134 }
0135
0136 void GeometryProducer::makeGeom(const edm::EventSetup &es) {
0137 if (!m_firstRun)
0138 return;
0139
0140 edm::LogVerbatim("GeometryProducer") << " GeometryProducer initializing ";
0141
0142 if (m_pGeoFromDD4hep) {
0143 m_pDD4hep = &es.getData(tokDD4hep_);
0144 } else {
0145 m_pDD = &es.getData(tokDDD_);
0146 }
0147
0148 SensitiveDetectorCatalog catalog;
0149 const DDDWorld *dddworld = new DDDWorld(m_pDD, m_pDD4hep, catalog, m_verbose, false, false);
0150 G4VPhysicalVolume *world = dddworld->GetWorldVolume();
0151 if (nullptr != world)
0152 edm::LogVerbatim("GeometryProducer") << " World Volume: " << world->GetName();
0153 m_kernel->DefineWorldVolume(world, true);
0154
0155 m_registry.dddWorldSignal_(dddworld);
0156
0157 edm::LogVerbatim("GeometryProducer") << " Magnetic field initialisation";
0158 updateMagneticField(es);
0159
0160 if (m_pUseSensitiveDetectors) {
0161 edm::LogVerbatim("GeometryProducer") << " instantiating sensitive detectors ";
0162
0163 TmpSimEvent *ptr = nullptr;
0164 m_trackManager = std::make_unique<SimTrackManager>(ptr, m_verbose);
0165 {
0166 std::pair<std::vector<SensitiveTkDetector *>, std::vector<SensitiveCaloDetector *>> sensDets =
0167 sim::attachSD(m_sdMakers, es, catalog, m_p, m_trackManager.get(), m_registry);
0168
0169 m_sensTkDets.swap(sensDets.first);
0170 m_sensCaloDets.swap(sensDets.second);
0171 }
0172
0173 edm::LogVerbatim("GeometryProducer") << " Sensitive Detector building finished; found " << m_sensTkDets.size()
0174 << " Tk type Producers, and " << m_sensCaloDets.size()
0175 << " Calo type producers ";
0176 }
0177 }
0178
0179 DEFINE_FWK_MODULE(GeometryProducer);