Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:15:31

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> idealGDToken2_;
0089   edm::ESGetToken<DetGeomDesc, IdealGeometryRecord> idealGDToken_;
0090   edm::ESGetToken<DetGeomDesc, VeryForwardIdealGeometryRecord> idealDBGDToken2_;
0091   edm::ESGetToken<DetGeomDesc, VeryForwardIdealGeometryRecord> idealDBGDToken_;
0092   edm::ESGetToken<CTPPSRPAlignmentCorrectionsData, RPRealAlignmentRecord> realAlignmentToken_;
0093   edm::ESGetToken<CTPPSRPAlignmentCorrectionsData, RPMisalignedAlignmentRecord> misAlignmentToken_;
0094 
0095   edm::ESGetToken<DetGeomDesc, VeryForwardRealGeometryRecord> dgdRealToken_;
0096   edm::ESGetToken<DetGeomDesc, VeryForwardMisalignedGeometryRecord> dgdMisToken_;
0097 };
0098 
0099 CTPPSGeometryESModule::CTPPSGeometryESModule(const edm::ParameterSet& iConfig)
0100     : verbosity_(iConfig.getUntrackedParameter<unsigned int>("verbosity")),
0101       buildMisalignedGeometry_(iConfig.getParameter<bool>("buildMisalignedGeometry")),
0102       isRun2_(iConfig.getParameter<bool>("isRun2")),
0103       fromPreprocessedDB_(iConfig.getUntrackedParameter<bool>("fromPreprocessedDB", false)),
0104       fromDD4hep_(iConfig.getUntrackedParameter<bool>("fromDD4hep", false)) {
0105   if (fromPreprocessedDB_) {
0106     auto c = setWhatProduced(this, &CTPPSGeometryESModule::produceIdealGDFromPreprocessedDB);
0107     dbToken_ = c.consumes<PDetGeomDesc>(edm::ESInputTag("", iConfig.getParameter<std::string>("dbTag")));
0108 
0109     auto c1 = setWhatProduced(this, &CTPPSGeometryESModule::produceRealGDFromPreprocessedDB);
0110     idealDBGDToken_ = c1.consumesFrom<DetGeomDesc, VeryForwardIdealGeometryRecord>(edm::ESInputTag());
0111     realAlignmentToken_ = c1.consumesFrom<CTPPSRPAlignmentCorrectionsData, RPRealAlignmentRecord>(edm::ESInputTag());
0112 
0113     if (buildMisalignedGeometry_) {
0114       auto c2 = setWhatProduced(this, &CTPPSGeometryESModule::produceMisalignedGDFromPreprocessedDB);
0115       idealDBGDToken2_ = c2.consumesFrom<DetGeomDesc, VeryForwardIdealGeometryRecord>(edm::ESInputTag());
0116       misAlignmentToken_ =
0117           c2.consumesFrom<CTPPSRPAlignmentCorrectionsData, RPMisalignedAlignmentRecord>(edm::ESInputTag());
0118     }
0119   } else if (!fromDD4hep_) {
0120     auto c = setWhatProduced(this, &CTPPSGeometryESModule::produceIdealGD);
0121     ddToken_ = c.consumes<DDCompactView>(edm::ESInputTag("", iConfig.getParameter<std::string>("compactViewTag")));
0122 
0123     auto c1 = setWhatProduced(this, &CTPPSGeometryESModule::produceRealGD);
0124     idealGDToken_ = c1.consumesFrom<DetGeomDesc, IdealGeometryRecord>(edm::ESInputTag());
0125     realAlignmentToken_ = c1.consumesFrom<CTPPSRPAlignmentCorrectionsData, RPRealAlignmentRecord>(edm::ESInputTag());
0126 
0127     if (buildMisalignedGeometry_) {
0128       auto c2 = setWhatProduced(this, &CTPPSGeometryESModule::produceMisalignedGD);
0129       idealGDToken2_ = c2.consumesFrom<DetGeomDesc, IdealGeometryRecord>(edm::ESInputTag());
0130       misAlignmentToken_ =
0131           c2.consumesFrom<CTPPSRPAlignmentCorrectionsData, RPMisalignedAlignmentRecord>(edm::ESInputTag());
0132     }
0133   } else {
0134     auto c = setWhatProduced(this, &CTPPSGeometryESModule::produceIdealGD);
0135     dd4hepToken_ =
0136         c.consumes<cms::DDCompactView>(edm::ESInputTag("", iConfig.getParameter<std::string>("compactViewTag")));
0137 
0138     auto c1 = setWhatProduced(this, &CTPPSGeometryESModule::produceRealGD);
0139     idealGDToken_ = c1.consumesFrom<DetGeomDesc, IdealGeometryRecord>(edm::ESInputTag());
0140     realAlignmentToken_ = c1.consumesFrom<CTPPSRPAlignmentCorrectionsData, RPRealAlignmentRecord>(edm::ESInputTag());
0141 
0142     if (buildMisalignedGeometry_) {
0143       auto c2 = setWhatProduced(this, &CTPPSGeometryESModule::produceMisalignedGD);
0144       idealGDToken2_ = c2.consumesFrom<DetGeomDesc, IdealGeometryRecord>(edm::ESInputTag());
0145       misAlignmentToken_ =
0146           c2.consumesFrom<CTPPSRPAlignmentCorrectionsData, RPMisalignedAlignmentRecord>(edm::ESInputTag());
0147     }
0148   }
0149 
0150   auto c_RTG = setWhatProduced(this, &CTPPSGeometryESModule::produceRealTG);
0151   dgdRealToken_ = c_RTG.consumes<DetGeomDesc>(edm::ESInputTag());
0152 
0153   if (buildMisalignedGeometry_) {
0154     auto c_MTG = setWhatProduced(this, &CTPPSGeometryESModule::produceMisalignedTG);
0155     dgdMisToken_ = c_MTG.consumes<DetGeomDesc>(edm::ESInputTag());
0156   }
0157 }
0158 
0159 void CTPPSGeometryESModule::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
0160   edm::ParameterSetDescription desc;
0161   desc.addUntracked<unsigned int>("verbosity", 1);
0162   desc.add<bool>("buildMisalignedGeometry", false)->setComment("switch if misaligned geometry shall be built");
0163   desc.add<bool>("isRun2", false)->setComment("Switch to legacy (2017-18) definition of diamond geometry");
0164   desc.add<std::string>("dbTag", std::string());
0165   desc.add<std::string>("compactViewTag", std::string());
0166   desc.addUntracked<bool>("fromPreprocessedDB", false);
0167   desc.addUntracked<bool>("fromDD4hep", false);
0168   descriptions.add("CTPPSGeometryESModule", desc);
0169 }
0170 
0171 std::unique_ptr<DetGeomDesc> CTPPSGeometryESModule::produceIdealGD(const IdealGeometryRecord& iRecord) {
0172   if (!fromDD4hep_) {
0173     // Get the DDCompactView from EventSetup
0174     auto const& myCompactView = iRecord.get(ddToken_);
0175 
0176     // Build geo from compact view.
0177     return detgeomdescbuilder::buildDetGeomDescFromCompactView(myCompactView, isRun2_);
0178   }
0179 
0180   else {
0181     // Get the DDCompactView from EventSetup
0182     auto const& myCompactView = iRecord.get(dd4hepToken_);
0183 
0184     // Build geo from compact view.
0185     return detgeomdescbuilder::buildDetGeomDescFromCompactView(myCompactView, isRun2_);
0186   }
0187 }
0188 
0189 std::unique_ptr<DetGeomDesc> CTPPSGeometryESModule::produceIdealGDFromPreprocessedDB(
0190     const VeryForwardIdealGeometryRecord& iRecord) {
0191   // Get the PDetGeomDesc from EventSetup
0192   auto const& myDB = iRecord.get(dbToken_);
0193 
0194   edm::LogInfo("CTPPSGeometryESModule") << " myDB size = " << myDB.container_.size();
0195 
0196   // Build geo from PDetGeomDesc DB object.
0197   auto pdet = std::make_unique<DetGeomDesc>(myDB);
0198   return pdet;
0199 }
0200 
0201 template <typename REC, typename GEO>
0202 std::unique_ptr<DetGeomDesc> CTPPSGeometryESModule::produceGD(
0203     GEO const& iIdealRec,
0204     std::optional<REC> const& iAlignRec,
0205     edm::ESGetToken<DetGeomDesc, GEO> const& iGDToken,
0206     edm::ESGetToken<CTPPSRPAlignmentCorrectionsData, REC> const& iAlignToken,
0207     const char* name) {
0208   // get the input GeometricalDet
0209   auto const& idealGD = iIdealRec.get(iGDToken);
0210 
0211   // load alignments
0212   CTPPSRPAlignmentCorrectionsData const* alignments = nullptr;
0213   if (iAlignRec) {
0214     auto alignmentsHandle = iAlignRec->getHandle(iAlignToken);
0215     if (alignmentsHandle.isValid()) {
0216       alignments = alignmentsHandle.product();
0217     }
0218   }
0219 
0220   if (verbosity_) {
0221     if (alignments) {
0222       edm::LogVerbatim(name) << ">> " << name << " > Real geometry: " << alignments->getRPMap().size() << " RP and "
0223                              << alignments->getSensorMap().size() << " sensor alignments applied.";
0224     } else {
0225       edm::LogVerbatim(name) << ">> " << name << " > Real geometry: No alignment applied.";
0226     }
0227   }
0228 
0229   return CTPPSGeometryESCommon::applyAlignments(idealGD, alignments);
0230 }
0231 
0232 std::unique_ptr<DetGeomDesc> CTPPSGeometryESModule::produceRealGDFromPreprocessedDB(
0233     const VeryForwardRealGeometryRecord& iRecord) {
0234   return produceGD(iRecord.getRecord<VeryForwardIdealGeometryRecord>(),
0235                    iRecord.tryToGetRecord<RPRealAlignmentRecord>(),
0236                    idealDBGDToken_,
0237                    realAlignmentToken_,
0238                    "CTPPSGeometryESModule::produceRealGDFromPreprocessedDB");
0239 }
0240 
0241 //----------------------------------------------------------------------------------------------------
0242 
0243 std::unique_ptr<DetGeomDesc> CTPPSGeometryESModule::produceMisalignedGDFromPreprocessedDB(
0244     const VeryForwardMisalignedGeometryRecord& iRecord) {
0245   return produceGD(iRecord.getRecord<VeryForwardIdealGeometryRecord>(),
0246                    iRecord.tryToGetRecord<RPMisalignedAlignmentRecord>(),
0247                    idealDBGDToken2_,
0248                    misAlignmentToken_,
0249                    "CTPPSGeometryESModule::produceMisalignedGDFromPreprocessedDB");
0250 }
0251 
0252 //----------------------------------------------------------------------------------------------------
0253 
0254 std::unique_ptr<DetGeomDesc> CTPPSGeometryESModule::produceRealGD(const VeryForwardRealGeometryRecord& iRecord) {
0255   return produceGD(iRecord.getRecord<IdealGeometryRecord>(),
0256                    iRecord.tryToGetRecord<RPRealAlignmentRecord>(),
0257                    idealGDToken_,
0258                    realAlignmentToken_,
0259                    "CTPPSGeometryESModule::produceRealGD");
0260 }
0261 
0262 //----------------------------------------------------------------------------------------------------
0263 
0264 std::unique_ptr<DetGeomDesc> CTPPSGeometryESModule::produceMisalignedGD(
0265     const VeryForwardMisalignedGeometryRecord& iRecord) {
0266   return produceGD(iRecord.getRecord<IdealGeometryRecord>(),
0267                    iRecord.tryToGetRecord<RPMisalignedAlignmentRecord>(),
0268                    idealGDToken2_,
0269                    misAlignmentToken_,
0270                    "CTPPSGeometryESModule::produceMisalignedGD");
0271 }
0272 
0273 //----------------------------------------------------------------------------------------------------
0274 
0275 std::unique_ptr<CTPPSGeometry> CTPPSGeometryESModule::produceRealTG(const VeryForwardRealGeometryRecord& iRecord) {
0276   auto const& gD = iRecord.get(dgdRealToken_);
0277 
0278   return std::make_unique<CTPPSGeometry>(&gD, verbosity_);
0279 }
0280 
0281 //----------------------------------------------------------------------------------------------------
0282 
0283 std::unique_ptr<CTPPSGeometry> CTPPSGeometryESModule::produceMisalignedTG(
0284     const VeryForwardMisalignedGeometryRecord& iRecord) {
0285   auto const& gD = iRecord.get(dgdMisToken_);
0286 
0287   return std::make_unique<CTPPSGeometry>(&gD, verbosity_);
0288 }
0289 
0290 DEFINE_FWK_EVENTSETUP_MODULE(CTPPSGeometryESModule);