Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-06-06 07:38:44

0001 /** \class DD4hep_VolumeBasedMagneticFieldESProducer
0002  *
0003  *  Producer for the VolumeBasedMagneticField.
0004  *
0005  */
0006 
0007 #include "FWCore/Framework/interface/ESTransientHandle.h"
0008 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0009 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0010 #include "FWCore/Framework/interface/EventSetupRecordIntervalFinder.h"
0011 #include "FWCore/Framework/interface/ESProducer.h"
0012 #include "FWCore/Framework/interface/ModuleFactory.h"
0013 
0014 #include "MagneticField/VolumeBasedEngine/interface/VolumeBasedMagneticField.h"
0015 #include "MagneticField/Records/interface/IdealMagneticFieldRecord.h"
0016 #include "MagneticField/Engine/interface/MagneticField.h"
0017 
0018 #include "MagneticField/GeomBuilder/src/DD4hep_MagGeoBuilder.h"
0019 #include "CondFormats/MFObjects/interface/MagFieldConfig.h"
0020 #include "DetectorDescription/DDCMS/interface/BenchmarkGrd.h"
0021 #include "DetectorDescription/DDCMS/interface/DDCompactView.h"
0022 #include "DetectorDescription/DDCMS/interface/DDDetector.h"
0023 
0024 #include <string>
0025 
0026 namespace magneticfield {
0027 
0028   class DD4hep_VolumeBasedMagneticFieldESProducer : public edm::ESProducer {
0029   public:
0030     DD4hep_VolumeBasedMagneticFieldESProducer(const edm::ParameterSet& iConfig);
0031 
0032     // forbid copy ctor and assignment op.
0033     DD4hep_VolumeBasedMagneticFieldESProducer(const DD4hep_VolumeBasedMagneticFieldESProducer&) = delete;
0034     const DD4hep_VolumeBasedMagneticFieldESProducer& operator=(const DD4hep_VolumeBasedMagneticFieldESProducer&) =
0035         delete;
0036 
0037     std::unique_ptr<MagneticField> produce(const IdealMagneticFieldRecord& iRecord);
0038 
0039     static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0040 
0041   private:
0042     edm::ParameterSet pset_;
0043     const bool debug_;
0044     const bool useParametrizedTrackerField_;
0045     const bool useMergeFileIfAvailable_;
0046     const MagFieldConfig conf_;
0047     const std::string version_;
0048     edm::ESGetToken<MagneticField, IdealMagneticFieldRecord> paramFieldToken_;
0049     edm::ESGetToken<cms::DDCompactView, IdealMagneticFieldRecord> cpvToken_;
0050   };
0051 }  // namespace magneticfield
0052 
0053 using namespace magneticfield;
0054 
0055 DD4hep_VolumeBasedMagneticFieldESProducer::DD4hep_VolumeBasedMagneticFieldESProducer(const edm::ParameterSet& iConfig)
0056     : pset_{iConfig},
0057       debug_{iConfig.getUntrackedParameter<bool>("debugBuilder", false)},
0058       useParametrizedTrackerField_{iConfig.getParameter<bool>("useParametrizedTrackerField")},
0059       useMergeFileIfAvailable_{iConfig.getParameter<bool>("useMergeFileIfAvailable")},
0060       conf_{iConfig, debug_},
0061       version_{iConfig.getParameter<std::string>("version")} {
0062   LogTrace("MagGeoBuilder") << "info:Constructing a DD4hep_VolumeBasedMagneticFieldESProducer";
0063 
0064   auto cc = setWhatProduced(this, iConfig.getUntrackedParameter<std::string>("label", ""));
0065   cpvToken_ = cc.consumes(edm::ESInputTag{"", "magfield"});
0066   if (useParametrizedTrackerField_) {
0067     paramFieldToken_ = cc.consumes(edm::ESInputTag{"", iConfig.getParameter<std::string>("paramLabel")});
0068   }
0069 }
0070 
0071 // ------------ method called to produce the data  ------------
0072 std::unique_ptr<MagneticField> DD4hep_VolumeBasedMagneticFieldESProducer::produce(
0073     const IdealMagneticFieldRecord& iRecord) {
0074   if (debug_) {
0075     LogTrace("MagGeoBuilder") << "DD4hep_VolumeBasedMagneticFieldESProducer::produce() " << version_;
0076   }
0077 
0078   MagGeoBuilder builder(conf_.version, conf_.geometryVersion, debug_, useMergeFileIfAvailable_);
0079 
0080   // Set scaling factors
0081   if (!conf_.keys.empty()) {
0082     builder.setScaling(conf_.keys, conf_.values);
0083   }
0084 
0085   // Set specification for the grid tables to be used.
0086   if (!conf_.gridFiles.empty()) {
0087     builder.setGridFiles(conf_.gridFiles);
0088   }
0089 
0090   auto cpv = iRecord.getTransientHandle(cpvToken_);
0091   const cms::DDCompactView* cpvPtr = cpv.product();
0092   const cms::DDDetector* det = cpvPtr->detector();
0093   builder.build(det);
0094   LogTrace("MagGeoBuilder") << "produce() finished build";
0095 
0096   // Get slave field (from ES)
0097   const MagneticField* paramField = nullptr;
0098   if (useParametrizedTrackerField_) {
0099     LogTrace("MagGeoBuilder") << "Getting MF for parametrized field";
0100     paramField = &iRecord.get(paramFieldToken_);
0101   }
0102   return std::make_unique<VolumeBasedMagneticField>(conf_.geometryVersion,
0103                                                     builder.barrelLayers(),
0104                                                     builder.endcapSectors(),
0105                                                     builder.barrelVolumes(),
0106                                                     builder.endcapVolumes(),
0107                                                     builder.maxR(),
0108                                                     builder.maxZ(),
0109                                                     paramField,
0110                                                     false);
0111 }
0112 
0113 void DD4hep_VolumeBasedMagneticFieldESProducer::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0114   edm::ParameterSetDescription desc;
0115   desc.addUntracked<bool>("debugBuilder", false);
0116   desc.add<bool>("useMergeFileIfAvailable", true);
0117   desc.add<bool>("useParametrizedTrackerField");
0118   desc.addUntracked<std::string>("label", "");
0119   desc.add<std::string>("version");
0120   desc.add<std::string>("paramLabel");
0121 
0122   //from MagFieldConfig
0123   desc.add<int>("geometryVersion");
0124   {
0125     edm::ParameterSetDescription sub;
0126     sub.add<std::string>("volumes");
0127     sub.add<std::string>("sectors");
0128     sub.add<int>("master");
0129     sub.add<std::string>("path");
0130     desc.addVPSet("gridFiles", sub);
0131   }
0132   desc.add<std::vector<int> >("scalingVolumes");
0133   desc.add<std::vector<double> >("scalingFactors");
0134   //default used to be compatible with older configurations
0135   desc.add<std::vector<double> >("paramData", std::vector<double>());
0136 
0137   descriptions.addDefault(desc);
0138 }
0139 
0140 DEFINE_FWK_EVENTSETUP_MODULE(DD4hep_VolumeBasedMagneticFieldESProducer);