Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-01-21 00:19:53

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   // Look for an outside SimActivityRegistry
0069   // this is used by the visualization code
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   //if (m_pUseSensitiveDetectors)
0086   //  m_sdMakers = sim::sensitiveDetectorMakers(m_p, consumesCollector(), std::vector<std::string>());
0087 
0088   tokMF_ = esConsumes<MagneticField, IdealMagneticFieldRecord, edm::Transition::BeginRun>();
0089   if (m_pGeoFromDD4hep) {
0090     tokDD4hep_ = esConsumes<cms::DDCompactView, IdealGeometryRecord, edm::Transition::BeginRun>();
0091   } else {
0092     tokDDD_ = esConsumes<DDCompactView, IdealGeometryRecord, edm::Transition::BeginRun>();
0093   }
0094   produces<int>();
0095 }
0096 
0097 GeometryProducer::~GeometryProducer() { delete m_kernel; }
0098 
0099 void GeometryProducer::updateMagneticField(edm::EventSetup const &es) {
0100   if (m_pUseMagneticField) {
0101     // setup the magnetic field
0102     auto const &pMF = &es.getData(tokMF_);
0103     const GlobalPoint g(0., 0., 0.);
0104     edm::LogInfo("GeometryProducer") << "B-field(T) at (0,0,0)(cm): " << pMF->inTesla(g);
0105 
0106     sim::FieldBuilder fieldBuilder(pMF, m_pField);
0107     CMSFieldManager *fieldManager = new CMSFieldManager();
0108     G4TransportationManager *tM = G4TransportationManager::GetTransportationManager();
0109     tM->SetFieldManager(fieldManager);
0110     fieldBuilder.build(fieldManager, tM->GetPropagatorInField());
0111     edm::LogInfo("GeometryProducer") << "Magentic field is built";
0112   }
0113 }
0114 
0115 void GeometryProducer::beginLuminosityBlock(edm::LuminosityBlock &, edm::EventSetup const &) {
0116   // mag field cannot be change in new lumi section - this is commented out
0117   //     updateMagneticField( es );
0118 }
0119 
0120 void GeometryProducer::beginRun(const edm::Run &run, const edm::EventSetup &es) {
0121   makeGeom(es);
0122   updateMagneticField(es);
0123   for (auto &maker : m_sdMakers) {
0124     maker.second->beginRun(es);
0125   }
0126 }
0127 
0128 void GeometryProducer::endRun(const edm::Run &, const edm::EventSetup &) {}
0129 
0130 void GeometryProducer::produce(edm::Event &e, const edm::EventSetup &es) {
0131   if (!m_firstRun)
0132     return;
0133   m_firstRun = false;
0134   for (Producers::iterator itProd = m_producers.begin(); itProd != m_producers.end(); ++itProd) {
0135     (*itProd)->produce(e, es);
0136   }
0137 }
0138 
0139 void GeometryProducer::makeGeom(const edm::EventSetup &es) {
0140   if (!m_firstRun)
0141     return;
0142 
0143   edm::LogVerbatim("GeometryProducer") << " GeometryProducer initializing ";
0144   // DDDWorld: get the DDCV from the ES and use it to build the World
0145   if (m_pGeoFromDD4hep) {
0146     m_pDD4hep = &es.getData(tokDD4hep_);
0147   } else {
0148     m_pDD = &es.getData(tokDDD_);
0149   }
0150 
0151   SensitiveDetectorCatalog catalog;
0152   const DDDWorld *dddworld = new DDDWorld(m_pDD, m_pDD4hep, catalog, m_verbose, false, false);
0153   G4VPhysicalVolume *world = dddworld->GetWorldVolume();
0154   if (nullptr != world)
0155     edm::LogVerbatim("GeometryProducer") << " World Volume: " << world->GetName();
0156   m_kernel->DefineWorldVolume(world, true);
0157 
0158   m_registry.dddWorldSignal_(dddworld);
0159 
0160   edm::LogVerbatim("GeometryProducer") << " Magnetic field initialisation";
0161   updateMagneticField(es);
0162 
0163   if (m_pUseSensitiveDetectors) {
0164     edm::LogInfo("GeometryProducer") << " instantiating sensitive detectors ";
0165     // instantiate and attach the sensitive detectors
0166     m_trackManager = std::make_unique<SimTrackManager>();
0167     {
0168       std::pair<std::vector<SensitiveTkDetector *>, std::vector<SensitiveCaloDetector *>> sensDets =
0169           sim::attachSD(m_sdMakers, es, catalog, m_p, m_trackManager.get(), m_registry);
0170 
0171       m_sensTkDets.swap(sensDets.first);
0172       m_sensCaloDets.swap(sensDets.second);
0173     }
0174 
0175     edm::LogInfo("GeometryProducer") << " Sensitive Detector building finished; found " << m_sensTkDets.size()
0176                                      << " Tk type Producers, and " << m_sensCaloDets.size() << " Calo type producers ";
0177   }
0178 }
0179 
0180 DEFINE_FWK_MODULE(GeometryProducer);