Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-09-30 03:09:15

0001 /****************************************************************************
0002 *  Based on CTPPSGeometryESModule.cc by:
0003 *  Jan Kaspar (jan.kaspar@gmail.com)
0004 *  Dominik Mierzejewski <dmierzej@cern.ch>
0005 *
0006 *  Rewritten + Moved out common functionailities to DetGeomDesc(Builder) by Gabrielle Hugo.
0007 *  Migrated to DD4hep by Wagner Carvalho and Gabrielle Hugo.
0008 *
0009 *  Add the capability of reading PPS reco geometry from the database
0010 *
0011 ****************************************************************************/
0012 
0013 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0014 #include "FWCore/Framework/interface/EventSetup.h"
0015 #include "FWCore/Framework/interface/ModuleFactory.h"
0016 #include "FWCore/Framework/interface/ESProducer.h"
0017 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0018 
0019 #include "DetectorDescription/Core/interface/DDCompactView.h"
0020 #include "DetectorDescription/DDCMS/interface/DDCompactView.h"
0021 #include "CondFormats/PPSObjects/interface/CTPPSRPAlignmentCorrectionsData.h"
0022 
0023 #include "CondFormats/GeometryObjects/interface/PDetGeomDesc.h"
0024 #include "CondFormats/AlignmentRecord/interface/RPRealAlignmentRecord.h"
0025 #include "CondFormats/AlignmentRecord/interface/RPMisalignedAlignmentRecord.h"
0026 #include "Geometry/Records/interface/IdealGeometryRecord.h"
0027 #include "Geometry/Records/interface/VeryForwardIdealGeometryRecord.h"
0028 #include "Geometry/Records/interface/VeryForwardMisalignedGeometryRecord.h"
0029 #include "Geometry/Records/interface/VeryForwardRealGeometryRecord.h"
0030 
0031 #include "Geometry/VeryForwardGeometryBuilder/interface/DetGeomDescBuilder.h"
0032 #include "Geometry/VeryForwardGeometryBuilder/interface/CTPPSDDDNames.h"
0033 #include "Geometry/VeryForwardGeometryBuilder/interface/DetGeomDesc.h"
0034 #include "Geometry/VeryForwardGeometryBuilder/interface/CTPPSGeometry.h"
0035 
0036 #include "Geometry/VeryForwardGeometryBuilder/interface/CTPPSGeometryESCommon.h"
0037 
0038 #include <regex>
0039 
0040 /**
0041  * \brief Builds ideal, real and misaligned geometries.
0042  *
0043  * First, it creates a tree of DetGeomDesc from DDCompView. For real and misaligned geometries,
0044  * it applies alignment corrections (RPAlignmentCorrections) found in corresponding ...GeometryRecord.
0045  *
0046  * Second, it creates CTPPSGeometry from DetGeoDesc tree.
0047  **/
0048 
0049 using RotationMatrix = ROOT::Math::Rotation3D;
0050 using Translation = ROOT::Math::DisplacementVector3D<ROOT::Math::Cartesian3D<double>>;
0051 
0052 class CTPPSGeometryESModule : public edm::ESProducer {
0053 public:
0054   CTPPSGeometryESModule(const edm::ParameterSet&);
0055   ~CTPPSGeometryESModule() override {}
0056 
0057   static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
0058 
0059 private:
0060   std::unique_ptr<DetGeomDesc> produceIdealGD(const IdealGeometryRecord&);
0061   std::unique_ptr<DetGeomDesc> produceIdealGDFromPreprocessedDB(const VeryForwardIdealGeometryRecord&);
0062   std::vector<int> fillCopyNos(TGeoIterator& it);
0063 
0064   std::unique_ptr<DetGeomDesc> produceRealGD(const VeryForwardRealGeometryRecord&);
0065   std::unique_ptr<DetGeomDesc> produceRealGDFromPreprocessedDB(const VeryForwardRealGeometryRecord&);
0066   std::unique_ptr<CTPPSGeometry> produceRealTG(const VeryForwardRealGeometryRecord&);
0067 
0068   std::unique_ptr<DetGeomDesc> produceMisalignedGD(const VeryForwardMisalignedGeometryRecord&);
0069   std::unique_ptr<DetGeomDesc> produceMisalignedGDFromPreprocessedDB(const VeryForwardMisalignedGeometryRecord&);
0070   std::unique_ptr<CTPPSGeometry> produceMisalignedTG(const VeryForwardMisalignedGeometryRecord&);
0071 
0072   template <typename REC, typename GEO>
0073   std::unique_ptr<DetGeomDesc> produceGD(const GEO&,
0074                                          const std::optional<REC>&,
0075                                          edm::ESGetToken<DetGeomDesc, GEO> const&,
0076                                          edm::ESGetToken<CTPPSRPAlignmentCorrectionsData, REC> const&,
0077                                          const char* name);
0078 
0079   const unsigned int verbosity_;
0080   const bool buildMisalignedGeometry_;
0081   const bool isRun2_;
0082 
0083   edm::ESGetToken<DDCompactView, IdealGeometryRecord> ddToken_;
0084   edm::ESGetToken<cms::DDCompactView, IdealGeometryRecord> dd4hepToken_;
0085   edm::ESGetToken<PDetGeomDesc, VeryForwardIdealGeometryRecord> dbToken_;
0086   const bool fromPreprocessedDB_, fromDD4hep_;
0087 
0088   edm::ESGetToken<DetGeomDesc, IdealGeometryRecord> idealGDToken_;
0089   edm::ESGetToken<DetGeomDesc, VeryForwardIdealGeometryRecord> idealDBGDToken_;
0090   edm::ESGetToken<CTPPSRPAlignmentCorrectionsData, RPRealAlignmentRecord> realAlignmentToken_;
0091   edm::ESGetToken<CTPPSRPAlignmentCorrectionsData, RPMisalignedAlignmentRecord> misAlignmentToken_;
0092 
0093   edm::ESGetToken<DetGeomDesc, VeryForwardRealGeometryRecord> dgdRealToken_;
0094   edm::ESGetToken<DetGeomDesc, VeryForwardMisalignedGeometryRecord> dgdMisToken_;
0095 };
0096 
0097 CTPPSGeometryESModule::CTPPSGeometryESModule(const edm::ParameterSet& iConfig)
0098     : verbosity_(iConfig.getUntrackedParameter<unsigned int>("verbosity")),
0099       buildMisalignedGeometry_(iConfig.getParameter<bool>("buildMisalignedGeometry")),
0100       isRun2_(iConfig.getParameter<bool>("isRun2")),
0101       fromPreprocessedDB_(iConfig.getUntrackedParameter<bool>("fromPreprocessedDB", false)),
0102       fromDD4hep_(iConfig.getUntrackedParameter<bool>("fromDD4hep", false)) {
0103   if (fromPreprocessedDB_) {
0104     auto c = setWhatProduced(this, &CTPPSGeometryESModule::produceIdealGDFromPreprocessedDB);
0105     dbToken_ = c.consumes<PDetGeomDesc>(edm::ESInputTag("", iConfig.getParameter<std::string>("dbTag")));
0106 
0107     auto c1 = setWhatProduced(this, &CTPPSGeometryESModule::produceRealGDFromPreprocessedDB);
0108     idealDBGDToken_ = c1.consumesFrom<DetGeomDesc, VeryForwardIdealGeometryRecord>(edm::ESInputTag());
0109     realAlignmentToken_ = c1.consumesFrom<CTPPSRPAlignmentCorrectionsData, RPRealAlignmentRecord>(edm::ESInputTag());
0110 
0111     if (buildMisalignedGeometry_) {
0112       auto c2 = setWhatProduced(this, &CTPPSGeometryESModule::produceMisalignedGDFromPreprocessedDB);
0113       misAlignmentToken_ =
0114           c2.consumesFrom<CTPPSRPAlignmentCorrectionsData, RPMisalignedAlignmentRecord>(edm::ESInputTag());
0115     }
0116   } else if (!fromDD4hep_) {
0117     auto c = setWhatProduced(this, &CTPPSGeometryESModule::produceIdealGD);
0118     ddToken_ = c.consumes<DDCompactView>(edm::ESInputTag("", iConfig.getParameter<std::string>("compactViewTag")));
0119 
0120     auto c1 = setWhatProduced(this, &CTPPSGeometryESModule::produceRealGD);
0121     idealGDToken_ = c1.consumesFrom<DetGeomDesc, IdealGeometryRecord>(edm::ESInputTag());
0122     realAlignmentToken_ = c1.consumesFrom<CTPPSRPAlignmentCorrectionsData, RPRealAlignmentRecord>(edm::ESInputTag());
0123 
0124     if (buildMisalignedGeometry_) {
0125       auto c2 = setWhatProduced(this, &CTPPSGeometryESModule::produceMisalignedGD);
0126       misAlignmentToken_ =
0127           c2.consumesFrom<CTPPSRPAlignmentCorrectionsData, RPMisalignedAlignmentRecord>(edm::ESInputTag());
0128     }
0129   } else {
0130     auto c = setWhatProduced(this, &CTPPSGeometryESModule::produceIdealGD);
0131     dd4hepToken_ =
0132         c.consumes<cms::DDCompactView>(edm::ESInputTag("", iConfig.getParameter<std::string>("compactViewTag")));
0133 
0134     auto c1 = setWhatProduced(this, &CTPPSGeometryESModule::produceRealGD);
0135     idealGDToken_ = c1.consumesFrom<DetGeomDesc, IdealGeometryRecord>(edm::ESInputTag());
0136     realAlignmentToken_ = c1.consumesFrom<CTPPSRPAlignmentCorrectionsData, RPRealAlignmentRecord>(edm::ESInputTag());
0137 
0138     if (buildMisalignedGeometry_) {
0139       auto c2 = setWhatProduced(this, &CTPPSGeometryESModule::produceMisalignedGD);
0140       misAlignmentToken_ =
0141           c2.consumesFrom<CTPPSRPAlignmentCorrectionsData, RPMisalignedAlignmentRecord>(edm::ESInputTag());
0142     }
0143   }
0144 
0145   auto c_RTG = setWhatProduced(this, &CTPPSGeometryESModule::produceRealTG);
0146   dgdRealToken_ = c_RTG.consumes<DetGeomDesc>(edm::ESInputTag());
0147 
0148   if (buildMisalignedGeometry_) {
0149     auto c_MTG = setWhatProduced(this, &CTPPSGeometryESModule::produceMisalignedTG);
0150     dgdMisToken_ = c_MTG.consumes<DetGeomDesc>(edm::ESInputTag());
0151   }
0152 }
0153 
0154 void CTPPSGeometryESModule::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0155   edm::ParameterSetDescription desc;
0156   desc.addUntracked<unsigned int>("verbosity", 1);
0157   desc.add<bool>("buildMisalignedGeometry", false)->setComment("switch if misaligned geometry shall be built");
0158   desc.add<bool>("isRun2", false)->setComment("Switch to legacy (2017-18) definition of diamond geometry");
0159   desc.add<std::string>("dbTag", std::string());
0160   desc.add<std::string>("compactViewTag", std::string());
0161   desc.addUntracked<bool>("fromPreprocessedDB", false);
0162   desc.addUntracked<bool>("fromDD4hep", false);
0163   descriptions.add("CTPPSGeometryESModule", desc);
0164 }
0165 
0166 std::unique_ptr<DetGeomDesc> CTPPSGeometryESModule::produceIdealGD(const IdealGeometryRecord& iRecord) {
0167   if (!fromDD4hep_) {
0168     // Get the DDCompactView from EventSetup
0169     auto const& myCompactView = iRecord.get(ddToken_);
0170 
0171     // Build geo from compact view.
0172     return detgeomdescbuilder::buildDetGeomDescFromCompactView(myCompactView, isRun2_);
0173   }
0174 
0175   else {
0176     // Get the DDCompactView from EventSetup
0177     auto const& myCompactView = iRecord.get(dd4hepToken_);
0178 
0179     // Build geo from compact view.
0180     return detgeomdescbuilder::buildDetGeomDescFromCompactView(myCompactView, isRun2_);
0181   }
0182 }
0183 
0184 std::unique_ptr<DetGeomDesc> CTPPSGeometryESModule::produceIdealGDFromPreprocessedDB(
0185     const VeryForwardIdealGeometryRecord& iRecord) {
0186   // Get the PDetGeomDesc from EventSetup
0187   auto const& myDB = iRecord.get(dbToken_);
0188 
0189   edm::LogInfo("CTPPSGeometryESModule") << " myDB size = " << myDB.container_.size();
0190 
0191   // Build geo from PDetGeomDesc DB object.
0192   auto pdet = std::make_unique<DetGeomDesc>(myDB);
0193   return pdet;
0194 }
0195 
0196 template <typename REC, typename GEO>
0197 std::unique_ptr<DetGeomDesc> CTPPSGeometryESModule::produceGD(
0198     GEO const& iIdealRec,
0199     std::optional<REC> const& iAlignRec,
0200     edm::ESGetToken<DetGeomDesc, GEO> const& iGDToken,
0201     edm::ESGetToken<CTPPSRPAlignmentCorrectionsData, REC> const& iAlignToken,
0202     const char* name) {
0203   // get the input GeometricalDet
0204   auto const& idealGD = iIdealRec.get(iGDToken);
0205 
0206   // load alignments
0207   CTPPSRPAlignmentCorrectionsData const* alignments = nullptr;
0208   if (iAlignRec) {
0209     auto alignmentsHandle = iAlignRec->getHandle(iAlignToken);
0210     if (alignmentsHandle.isValid()) {
0211       alignments = alignmentsHandle.product();
0212     }
0213   }
0214 
0215   if (verbosity_) {
0216     if (alignments) {
0217       edm::LogVerbatim(name) << ">> " << name << " > Real geometry: " << alignments->getRPMap().size() << " RP and "
0218                              << alignments->getSensorMap().size() << " sensor alignments applied.";
0219     } else {
0220       edm::LogVerbatim(name) << ">> " << name << " > Real geometry: No alignment applied.";
0221     }
0222   }
0223 
0224   return CTPPSGeometryESCommon::applyAlignments(idealGD, alignments);
0225 }
0226 
0227 std::unique_ptr<DetGeomDesc> CTPPSGeometryESModule::produceRealGDFromPreprocessedDB(
0228     const VeryForwardRealGeometryRecord& iRecord) {
0229   return produceGD(iRecord.getRecord<VeryForwardIdealGeometryRecord>(),
0230                    iRecord.tryToGetRecord<RPRealAlignmentRecord>(),
0231                    idealDBGDToken_,
0232                    realAlignmentToken_,
0233                    "CTPPSGeometryESModule::produceRealGDFromPreprocessedDB");
0234 }
0235 
0236 //----------------------------------------------------------------------------------------------------
0237 
0238 std::unique_ptr<DetGeomDesc> CTPPSGeometryESModule::produceMisalignedGDFromPreprocessedDB(
0239     const VeryForwardMisalignedGeometryRecord& iRecord) {
0240   return produceGD(iRecord.getRecord<VeryForwardIdealGeometryRecord>(),
0241                    iRecord.tryToGetRecord<RPMisalignedAlignmentRecord>(),
0242                    idealDBGDToken_,
0243                    misAlignmentToken_,
0244                    "CTPPSGeometryESModule::produceMisalignedGDFromPreprocessedDB");
0245 }
0246 
0247 //----------------------------------------------------------------------------------------------------
0248 
0249 std::unique_ptr<DetGeomDesc> CTPPSGeometryESModule::produceRealGD(const VeryForwardRealGeometryRecord& iRecord) {
0250   return produceGD(iRecord.getRecord<IdealGeometryRecord>(),
0251                    iRecord.tryToGetRecord<RPRealAlignmentRecord>(),
0252                    idealGDToken_,
0253                    realAlignmentToken_,
0254                    "CTPPSGeometryESModule::produceRealGD");
0255 }
0256 
0257 //----------------------------------------------------------------------------------------------------
0258 
0259 std::unique_ptr<DetGeomDesc> CTPPSGeometryESModule::produceMisalignedGD(
0260     const VeryForwardMisalignedGeometryRecord& iRecord) {
0261   return produceGD(iRecord.getRecord<IdealGeometryRecord>(),
0262                    iRecord.tryToGetRecord<RPMisalignedAlignmentRecord>(),
0263                    idealGDToken_,
0264                    misAlignmentToken_,
0265                    "CTPPSGeometryESModule::produceMisalignedGD");
0266 }
0267 
0268 //----------------------------------------------------------------------------------------------------
0269 
0270 std::unique_ptr<CTPPSGeometry> CTPPSGeometryESModule::produceRealTG(const VeryForwardRealGeometryRecord& iRecord) {
0271   auto const& gD = iRecord.get(dgdRealToken_);
0272 
0273   return std::make_unique<CTPPSGeometry>(&gD, verbosity_);
0274 }
0275 
0276 //----------------------------------------------------------------------------------------------------
0277 
0278 std::unique_ptr<CTPPSGeometry> CTPPSGeometryESModule::produceMisalignedTG(
0279     const VeryForwardMisalignedGeometryRecord& iRecord) {
0280   auto const& gD = iRecord.get(dgdMisToken_);
0281 
0282   return std::make_unique<CTPPSGeometry>(&gD, verbosity_);
0283 }
0284 
0285 DEFINE_FWK_EVENTSETUP_MODULE(CTPPSGeometryESModule);