Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 /*
0002 //\class GEMGeometryESModule
0003 
0004  Description: GEM Geometry ES Module from DD & DD4hep
0005               DD4hep part added to the original old file (DD version) made by M. Maggi (INFN Bari)
0006 //
0007 // Author:  Sergio Lo Meo (sergio.lo.meo@cern.ch) following what Ianna Osburne made for DTs (DD4hep migration)
0008 //          Created:  27 Jan 2020 
0009 */
0010 #include "Geometry/GEMGeometryBuilder/src/GEMGeometryBuilder.h"
0011 #include "Geometry/GEMGeometryBuilder/src/GEMGeometryBuilderFromCondDB.h"
0012 #include "Geometry/GEMGeometry/interface/GEMGeometry.h"
0013 
0014 #include "Geometry/Records/interface/IdealGeometryRecord.h"
0015 #include "Geometry/Records/interface/MuonGeometryRecord.h"
0016 #include "Geometry/Records/interface/GEMRecoGeometryRcd.h"
0017 #include "Geometry/MuonNumbering/interface/MuonGeometryConstants.h"
0018 #include "DetectorDescription/Core/interface/DDCompactView.h"
0019 #include "DetectorDescription/DDCMS/interface/DDCompactView.h"
0020 
0021 #include "CondFormats/GeometryObjects/interface/RecoIdealGeometry.h"
0022 
0023 #include "FWCore/Framework/interface/EventSetup.h"
0024 #include "FWCore/Framework/interface/ESTransientHandle.h"
0025 #include "FWCore/Framework/interface/ModuleFactory.h"
0026 #include "FWCore/Framework/interface/ESProducer.h"
0027 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0028 #include "FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
0029 
0030 // Alignments
0031 #include "CondFormats/Alignment/interface/DetectorGlobalPosition.h"
0032 #include "CondFormats/Alignment/interface/AlignmentErrorsExtended.h"
0033 #include "CondFormats/AlignmentRecord/interface/GlobalPositionRcd.h"
0034 #include "CondFormats/AlignmentRecord/interface/GEMAlignmentRcd.h"
0035 #include "CondFormats/AlignmentRecord/interface/GEMAlignmentErrorExtendedRcd.h"
0036 #include "Geometry/CommonTopologies/interface/GeometryAligner.h"
0037 
0038 #include <memory>
0039 
0040 class GEMGeometryESModule : public edm::ESProducer {
0041 public:
0042   /// Constructor
0043   GEMGeometryESModule(const edm::ParameterSet& p);
0044 
0045   /// Define the cfi file
0046   static void fillDescriptions(edm::ConfigurationDescriptions&);
0047 
0048   /// Produce GEMGeometry.
0049   std::unique_ptr<GEMGeometry> produce(const MuonGeometryRecord& record);
0050 
0051 private:
0052   // use the DDD as Geometry source
0053   const bool fromDDD_;
0054   const bool fromDD4hep_;
0055   bool applyAlignment_;
0056   const std::string alignmentsLabel_;
0057   edm::ESGetToken<DDCompactView, IdealGeometryRecord> cpvToken_;
0058   edm::ESGetToken<MuonGeometryConstants, IdealGeometryRecord> mdcToken_;
0059   edm::ESGetToken<cms::DDCompactView, IdealGeometryRecord> dd4hepcpvToken_;
0060   edm::ESGetToken<RecoIdealGeometry, GEMRecoGeometryRcd> riggemToken_;
0061   edm::ESGetToken<Alignments, GlobalPositionRcd> globalPositionToken_;
0062   edm::ESGetToken<Alignments, GEMAlignmentRcd> alignmentsToken_;
0063   edm::ESGetToken<AlignmentErrorsExtended, GEMAlignmentErrorExtendedRcd> alignmentErrorsToken_;
0064 };
0065 
0066 GEMGeometryESModule::GEMGeometryESModule(const edm::ParameterSet& p)
0067     : fromDDD_{p.getParameter<bool>("fromDDD")},
0068       fromDD4hep_{p.getParameter<bool>("fromDD4hep")},
0069       applyAlignment_(p.getParameter<bool>("applyAlignment")),
0070       alignmentsLabel_(p.getParameter<std::string>("alignmentsLabel")) {
0071   auto cc = setWhatProduced(this);
0072   if (fromDDD_) {
0073     cpvToken_ = cc.consumes();
0074     mdcToken_ = cc.consumes();
0075   } else if (fromDD4hep_) {
0076     dd4hepcpvToken_ = cc.consumes();
0077     mdcToken_ = cc.consumes();
0078   } else {
0079     riggemToken_ = cc.consumes();
0080   }
0081   if (applyAlignment_) {
0082     globalPositionToken_ = cc.consumes(edm::ESInputTag{"", alignmentsLabel_});
0083     alignmentsToken_ = cc.consumes(edm::ESInputTag{"", alignmentsLabel_});
0084     alignmentErrorsToken_ = cc.consumes(edm::ESInputTag{"", alignmentsLabel_});
0085   }
0086   edm::LogVerbatim("GEMGeometry") << "GEMGeometryESModule::initailized with flags " << fromDDD_ << ":" << fromDD4hep_;
0087 }
0088 
0089 void GEMGeometryESModule::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0090   edm::ParameterSetDescription desc;
0091   desc.add<bool>("fromDDD", true);
0092   desc.add<bool>("fromDD4hep", false);
0093   desc.add<bool>("applyAlignment", false);
0094   desc.add<std::string>("alignmentsLabel", "");
0095   descriptions.add("gemGeometry", desc);
0096 }
0097 
0098 std::unique_ptr<GEMGeometry> GEMGeometryESModule::produce(const MuonGeometryRecord& record) {
0099   auto gemGeometry = std::make_unique<GEMGeometry>();
0100 
0101   if (fromDDD_) {
0102     edm::LogVerbatim("GEMGeometry") << "GEMGeometryESModule::produce :: GEMGeometryBuilder builder ddd";
0103     auto cpv = record.getTransientHandle(cpvToken_);
0104     const auto& mdc = record.get(mdcToken_);
0105     GEMGeometryBuilder builder;
0106     builder.build(*gemGeometry, cpv.product(), mdc);
0107   } else if (fromDD4hep_) {
0108     edm::LogVerbatim("GEMGeometry") << "GEMGeometryESModule::produce :: GEMGeometryBuilder builder dd4hep";
0109     edm::ESTransientHandle<cms::DDCompactView> cpv = record.getTransientHandle(dd4hepcpvToken_);
0110     const auto& mdc = record.get(mdcToken_);
0111     GEMGeometryBuilder builder;
0112     builder.build(*gemGeometry, cpv.product(), mdc);
0113   } else {
0114     edm::LogVerbatim("GEMGeometry") << "GEMGeometryESModule::produce :: GEMGeometryBuilder builder db";
0115     const auto& riggem = record.get(riggemToken_);
0116     GEMGeometryBuilderFromCondDB builder;
0117     builder.build(*gemGeometry, riggem);
0118   }
0119 
0120   if (applyAlignment_) {
0121     const auto& globalPosition = record.get(globalPositionToken_);
0122     const auto& alignments = record.get(alignmentsToken_);
0123     const auto& alignmentErrors = record.get(alignmentErrorsToken_);
0124 
0125     // No alignment records, assume ideal geometry is wanted
0126     if (alignments.empty() && alignmentErrors.empty() && globalPosition.empty()) {
0127       edm::LogInfo("Config") << "@SUB=GEMGeometryRecord::produce"
0128                              << "Alignment(Error)s and global position (label '" << alignmentsLabel_
0129                              << "') empty: it is assumed fake and will not apply.";
0130     } else {
0131       GeometryAligner aligner;
0132       aligner.applyAlignments<GEMGeometry>(gemGeometry.get(),
0133                                            &alignments,
0134                                            &alignmentErrors,
0135                                            align::DetectorGlobalPosition(globalPosition, DetId(DetId::Muon)));
0136     }
0137   }
0138 
0139   return gemGeometry;
0140 }
0141 
0142 DEFINE_FWK_EVENTSETUP_MODULE(GEMGeometryESModule);