Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-11-09 23:31:47

0001 // -*- C++ -*-
0002 //
0003 // Package:    DetectorDescription/DDDetectorESProducer
0004 // Class:      DDDetectorESProducer
0005 //
0006 /**\class DDDetectorESProducer
0007 
0008  Description: Produce Detector description
0009 
0010  Implementation:
0011      Detector is described in XML
0012 */
0013 //
0014 // Original Author:  Ianna Osborne
0015 //         Created:  Fri, 07 Dec 2018 11:20:52 GMT
0016 //
0017 //
0018 
0019 #include <memory>
0020 
0021 #include "FWCore/Framework/interface/EventSetupRecordIntervalFinder.h"
0022 #include "FWCore/Framework/interface/SourceFactory.h"
0023 #include "FWCore/Framework/interface/ESProducer.h"
0024 #include "FWCore/Framework/interface/ESHandle.h"
0025 #include "FWCore/Concurrency/interface/SharedResourceNames.h"
0026 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0027 #include "CondFormats/Common/interface/FileBlob.h"
0028 #include "CondFormats/DataRecord/interface/MFGeometryFileRcd.h"
0029 
0030 #include "Geometry/Records/interface/IdealGeometryRecord.h"
0031 #include "Geometry/Records/interface/GeometryFileRcd.h"
0032 #include "MagneticField/Records/interface/IdealMagneticFieldRecord.h"
0033 #include "DetectorDescription/DDCMS/interface/DDDetector.h"
0034 #include "DD4hep/Detector.h"
0035 
0036 using namespace std;
0037 using namespace cms;
0038 using namespace edm;
0039 
0040 class DDDetectorESProducer : public ESProducer, public EventSetupRecordIntervalFinder {
0041 public:
0042   DDDetectorESProducer(const ParameterSet&);
0043   ~DDDetectorESProducer() override;
0044 
0045   using ReturnType = unique_ptr<DDDetector>;
0046   using Detector = dd4hep::Detector;
0047 
0048   ReturnType produceGeom(const IdealGeometryRecord&);
0049   ReturnType produceMagField(const IdealMagneticFieldRecord&);
0050   ReturnType produce();
0051   static void fillDescriptions(ConfigurationDescriptions&);
0052 
0053 protected:
0054   void setIntervalFor(const eventsetup::EventSetupRecordKey&, const IOVSyncValue&, ValidityInterval&) override;
0055 
0056 private:
0057   const bool fromDB_;
0058   const string appendToDataLabel_;
0059   const string confGeomXMLFiles_;
0060   const string rootDDName_;
0061   const string label_;
0062   edm::ESGetToken<FileBlob, MFGeometryFileRcd> mfToken_;
0063   edm::ESGetToken<FileBlob, GeometryFileRcd> geomToken_;
0064 };
0065 
0066 DDDetectorESProducer::DDDetectorESProducer(const ParameterSet& iConfig)
0067     : fromDB_(iConfig.getParameter<bool>("fromDB")),
0068       appendToDataLabel_(iConfig.getParameter<string>("appendToDataLabel")),
0069       confGeomXMLFiles_(fromDB_ ? "none" : iConfig.getParameter<FileInPath>("confGeomXMLFiles").fullPath()),
0070       rootDDName_(iConfig.getParameter<string>("rootDDName")),
0071       label_(iConfig.getParameter<string>("label")) {
0072   usesResources({edm::ESSharedResourceNames::kDD4hep});
0073   edm::LogVerbatim("Geometry") << "DDDetectorESProducer::fromDB " << fromDB_ << " appendToDataLabel "
0074                                << appendToDataLabel_ << " rootDDName " << rootDDName_ << " label " << label_
0075                                << " confGeomXMLFiles " << confGeomXMLFiles_;
0076   if (rootDDName_ == "MagneticFieldVolumes:MAGF" || rootDDName_ == "cmsMagneticField:MAGF") {
0077     auto c = setWhatProduced(this,
0078                              &DDDetectorESProducer::produceMagField,
0079                              edm::es::Label(iConfig.getParameter<std::string>("@module_label")));
0080     if (fromDB_) {
0081       mfToken_ = c.consumes(edm::ESInputTag("", label_));
0082     }
0083     findingRecord<IdealMagneticFieldRecord>();
0084   } else {
0085     auto c = setWhatProduced(this, &DDDetectorESProducer::produceGeom);
0086     if (fromDB_) {
0087       geomToken_ = c.consumes(edm::ESInputTag("", label_));
0088     }
0089     findingRecord<IdealGeometryRecord>();
0090   }
0091 }
0092 
0093 DDDetectorESProducer::~DDDetectorESProducer() {}
0094 
0095 void DDDetectorESProducer::fillDescriptions(ConfigurationDescriptions& descriptions) {
0096   ParameterSetDescription desc;
0097 
0098   desc.addOptional<FileInPath>("confGeomXMLFiles");
0099   desc.add<string>("rootDDName", "cms:OCMS");
0100   desc.add<string>("label", "");
0101   desc.add<bool>("fromDB", false);
0102   descriptions.add("DDDetectorESProducer", desc);
0103 
0104   edm::ParameterSetDescription descDB;
0105   descDB.add<string>("rootDDName", "cms:OCMS");
0106   descDB.add<string>("label", "Extended");
0107   descDB.add<bool>("fromDB", true);
0108   descriptions.add("DDDetectorESProducerFromDB", descDB);
0109 }
0110 
0111 void DDDetectorESProducer::setIntervalFor(const eventsetup::EventSetupRecordKey& iKey,
0112                                           const IOVSyncValue& iTime,
0113                                           ValidityInterval& oInterval) {
0114   oInterval = ValidityInterval(IOVSyncValue::beginOfTime(), IOVSyncValue::endOfTime());  //infinite
0115 }
0116 
0117 DDDetectorESProducer::ReturnType DDDetectorESProducer::produceMagField(const IdealMagneticFieldRecord& iRecord) {
0118   LogVerbatim("Geometry") << "DDDetectorESProducer::Produce MF " << appendToDataLabel_;
0119   if (fromDB_) {
0120     edm::ESTransientHandle<FileBlob> gdd = iRecord.getTransientHandle(mfToken_);
0121     unique_ptr<vector<unsigned char> > tb = (*gdd).getUncompressedBlob();
0122 
0123     return make_unique<cms::DDDetector>(label_, string(tb->begin(), tb->end()), true);
0124   } else {
0125     return make_unique<DDDetector>(appendToDataLabel_, confGeomXMLFiles_);
0126   }
0127 }
0128 
0129 DDDetectorESProducer::ReturnType DDDetectorESProducer::produceGeom(const IdealGeometryRecord& iRecord) {
0130   LogVerbatim("Geometry") << "DDDetectorESProducer::Produce " << appendToDataLabel_;
0131   if (fromDB_) {
0132     edm::ESTransientHandle<FileBlob> gdd = iRecord.getTransientHandle(geomToken_);
0133     unique_ptr<vector<unsigned char> > tb = (*gdd).getUncompressedBlob();
0134 
0135     return make_unique<cms::DDDetector>(label_, string(tb->begin(), tb->end()), true);
0136   } else {
0137     return make_unique<DDDetector>(appendToDataLabel_, confGeomXMLFiles_, false);
0138   }
0139 }
0140 
0141 DDDetectorESProducer::ReturnType DDDetectorESProducer::produce() {
0142   LogVerbatim("Geometry") << "DDDetectorESProducer::Produce " << appendToDataLabel_;
0143   return make_unique<DDDetector>(appendToDataLabel_, confGeomXMLFiles_, false);
0144 }
0145 
0146 DEFINE_FWK_EVENTSETUP_SOURCE(DDDetectorESProducer);