Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:03:04

0001 /****************************************************************************
0002  *
0003  *  DB builder for PPS geometry
0004  *
0005  *  Author: Wagner Carvalho (wcarvalh@cern.ch)
0006  *  Moved out common functionalities to DetGeomDesc(Builder) + support both old DD and DD4hep, by Gabrielle Hugo.
0007  *
0008  ****************************************************************************/
0009 
0010 #include "FWCore/Framework/interface/ESHandle.h"
0011 #include "FWCore/Framework/interface/MakerMacros.h"
0012 #include "FWCore/Framework/interface/one/EDAnalyzer.h"
0013 #include "FWCore/Framework/interface/Event.h"
0014 #include "FWCore/Framework/interface/EventSetup.h"
0015 #include "FWCore/Framework/interface/ESWatcher.h"
0016 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0017 #include "FWCore/Utilities/interface/Exception.h"
0018 #include "FWCore/ServiceRegistry/interface/Service.h"
0019 #include "CondCore/DBOutputService/interface/PoolDBOutputService.h"
0020 
0021 #include "Geometry/Records/interface/IdealGeometryRecord.h"
0022 #include "Geometry/Records/interface/VeryForwardIdealGeometryRecord.h"
0023 #include "DetectorDescription/Core/interface/DDCompactView.h"
0024 #include "DetectorDescription/DDCMS/interface/DDCompactView.h"
0025 
0026 #include "CondFormats/GeometryObjects/interface/PDetGeomDesc.h"
0027 #include "Geometry/VeryForwardGeometryBuilder/interface/DetGeomDescBuilder.h"
0028 
0029 class PPSGeometryBuilder : public edm::one::EDAnalyzer<> {
0030 public:
0031   explicit PPSGeometryBuilder(const edm::ParameterSet&);
0032   void analyze(const edm::Event&, const edm::EventSetup&) override;
0033 
0034 private:
0035   void buildSerializableDataFromGeoInfo(PDetGeomDesc* serializableData, const DetGeomDesc* geoInfo, int& counter);
0036   PDetGeomDesc::Item buildItemFromDetGeomDesc(const DetGeomDesc* geoInfo);
0037 
0038   bool fromDD4hep_;
0039   std::string compactViewTag_;
0040   bool isRun2_;
0041   edm::ESGetToken<DDCompactView, IdealGeometryRecord> ddToken_;
0042   edm::ESGetToken<cms::DDCompactView, IdealGeometryRecord> dd4hepToken_;
0043   edm::ESWatcher<IdealGeometryRecord> watcherIdealGeometry_;
0044   edm::Service<cond::service::PoolDBOutputService> dbService_;
0045 };
0046 
0047 PPSGeometryBuilder::PPSGeometryBuilder(const edm::ParameterSet& iConfig)
0048     : fromDD4hep_(iConfig.getUntrackedParameter<bool>("fromDD4hep", false)),
0049       compactViewTag_(iConfig.getUntrackedParameter<std::string>("compactViewTag", "XMLIdealGeometryESSource_CTPPS")),
0050       isRun2_(iConfig.getUntrackedParameter<bool>("isRun2", false)),
0051       ddToken_(esConsumes(edm::ESInputTag("", compactViewTag_))),
0052       dd4hepToken_(esConsumes(edm::ESInputTag("", compactViewTag_))) {}
0053 
0054 /*
0055  * Save PPS geo to DB.
0056  */
0057 void PPSGeometryBuilder::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup) {
0058   // Get DetGeomDesc tree
0059   std::unique_ptr<DetGeomDesc> geoInfoRoot = nullptr;
0060   if (watcherIdealGeometry_.check(iSetup)) {
0061     edm::LogInfo("PPSGeometryBuilder") << "Got IdealGeometryRecord ";
0062     // old DD
0063     if (!fromDD4hep_) {
0064       // Get CompactView from IdealGeometryRecord
0065       auto const& myCompactView = iSetup.getData(ddToken_);
0066 
0067       // Build geometry
0068       geoInfoRoot = detgeomdescbuilder::buildDetGeomDescFromCompactView(myCompactView, isRun2_);
0069     }
0070     // DD4hep
0071     else {
0072       // Get CompactView from IdealGeometryRecord
0073       auto const& myCompactView = iSetup.getData(dd4hepToken_);
0074 
0075       // Build geometry
0076       geoInfoRoot = detgeomdescbuilder::buildDetGeomDescFromCompactView(myCompactView, isRun2_);
0077     }
0078   }
0079 
0080   // Build persistent geometry data from geometry
0081   PDetGeomDesc serializableData;  // cond::service::PoolDBOutputService::writeOne interface requires raw pointer.
0082   int counter = 0;
0083   if (geoInfoRoot) {
0084     buildSerializableDataFromGeoInfo(&serializableData, geoInfoRoot.get(), counter);
0085   }
0086 
0087   // Save geometry in the database
0088   if (serializableData.container_.empty()) {
0089     throw cms::Exception("PPSGeometryBuilder") << "PDetGeomDesc is empty, no geometry to save in the database.";
0090   } else {
0091     if (dbService_.isAvailable()) {
0092       dbService_->writeOneIOV(serializableData, dbService_->beginOfTime(), "VeryForwardIdealGeometryRecord");
0093       edm::LogInfo("PPSGeometryBuilder") << "Successfully wrote DB, with " << serializableData.container_.size()
0094                                          << " PDetGeomDesc items.";
0095     } else {
0096       throw cms::Exception("PPSGeometryBuilder") << "PoolDBService required.";
0097     }
0098   }
0099 }
0100 
0101 /*
0102  * Build persistent data items to be stored in DB (PDetGeomDesc) from geo info (DetGeomDesc).
0103  * Recursive, depth-first search.
0104  */
0105 void PPSGeometryBuilder::buildSerializableDataFromGeoInfo(PDetGeomDesc* serializableData,
0106                                                           const DetGeomDesc* geoInfo,
0107                                                           int& counter) {
0108   PDetGeomDesc::Item serializableItem = buildItemFromDetGeomDesc(geoInfo);
0109   counter++;
0110 
0111   // Store item in serializableData
0112   if ((!fromDD4hep_ && counter >= 2)       // Old DD: Skip CMSE
0113       || (fromDD4hep_ && counter >= 4)) {  // DD4hep: Skip world + OCMS + CMSE
0114     serializableData->container_.emplace_back(serializableItem);
0115   }
0116 
0117   // Recursive calls on children
0118   for (auto& child : geoInfo->components()) {
0119     buildSerializableDataFromGeoInfo(serializableData, child, counter);
0120   }
0121 }
0122 
0123 /*
0124  * Build Item from DetGeomDesc info.
0125  */
0126 PDetGeomDesc::Item PPSGeometryBuilder::buildItemFromDetGeomDesc(const DetGeomDesc* geoInfo) {
0127   PDetGeomDesc::Item result;
0128   result.dx_ = geoInfo->translation().X();
0129   result.dy_ = geoInfo->translation().Y();
0130   result.dz_ = geoInfo->translation().Z();
0131 
0132   const DDRotationMatrix& rot = geoInfo->rotation();
0133   rot.GetComponents(result.axx_,
0134                     result.axy_,
0135                     result.axz_,
0136                     result.ayx_,
0137                     result.ayy_,
0138                     result.ayz_,
0139                     result.azx_,
0140                     result.azy_,
0141                     result.azz_);
0142   result.name_ = geoInfo->name();
0143   result.params_ = geoInfo->params();
0144   result.copy_ = geoInfo->copyno();
0145   result.z_ = geoInfo->parentZPosition();
0146   result.sensorType_ = geoInfo->sensorType();
0147   result.geographicalID_ = geoInfo->geographicalID();
0148 
0149   return result;
0150 }
0151 
0152 DEFINE_FWK_MODULE(PPSGeometryBuilder);