Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-10-25 09:49:13

0001 /** \class DTGeometryParsFromDD
0002  *
0003  *  Build the RPCGeometry from the DDD and DD4hep description
0004  *  
0005  *  DD4hep part added to the original old file (DD version) made by Stefano Lacaprara (INFN LNL)
0006  *  \author:  Sergio Lo Meo (sergio.lo.meo@cern.ch) 
0007  *  Created:  Tue, 26 Jan 2021 
0008  *
0009  */
0010 #include <Geometry/DTGeometryBuilder/interface/DTGeometryParsFromDD.h>
0011 #include <Geometry/DTGeometry/interface/DTGeometry.h>
0012 #include <Geometry/DTGeometry/interface/DTChamber.h>
0013 #include <Geometry/DTGeometry/interface/DTLayer.h>
0014 #include <CondFormats/GeometryObjects/interface/RecoIdealGeometry.h>
0015 #include <DetectorDescription/Core/interface/DDFilter.h>
0016 #include <DetectorDescription/Core/interface/DDFilteredView.h>
0017 #include <DetectorDescription/Core/interface/DDSolid.h>
0018 #include "Geometry/MuonNumbering/interface/MuonGeometryNumbering.h"
0019 #include "Geometry/MuonNumbering/interface/MuonBaseNumber.h"
0020 #include "Geometry/MuonNumbering/interface/DTNumberingScheme.h"
0021 #include "DataFormats/MuonDetId/interface/DTChamberId.h"
0022 #include "DataFormats/GeometrySurface/interface/RectangularPlaneBounds.h"
0023 #include "DataFormats/Math/interface/GeantUnits.h"
0024 #include "DataFormats/GeometryVector/interface/Basic3DVector.h"
0025 #include <DetectorDescription/DDCMS/interface/DDFilteredView.h>
0026 #include <DetectorDescription/DDCMS/interface/DDCompactView.h>
0027 #include "DetectorDescription/DDCMS/interface/DDSpecParRegistry.h"
0028 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0029 #include <iostream>
0030 #include <algorithm>
0031 #include <string>
0032 
0033 using namespace geant_units;
0034 using namespace geant_units::operators;
0035 
0036 //#define EDM_ML_DEBUG
0037 
0038 DTGeometryParsFromDD::DTGeometryParsFromDD() {}
0039 
0040 DTGeometryParsFromDD::~DTGeometryParsFromDD() {}
0041 
0042 // DD
0043 void DTGeometryParsFromDD::build(const DDCompactView* cview,
0044                                  const MuonGeometryConstants& muonConstants,
0045                                  RecoIdealGeometry& rig) {
0046 #ifdef EDM_ML_DEBUG
0047   edm::LogVerbatim("DTGeometry") << "DTGeometryParsFromDD::build";
0048 #endif
0049 
0050   std::string attribute = "MuStructure";
0051   std::string value = "MuonBarrelDT";
0052 
0053   // Asking only for the Muon DTs
0054   DDSpecificsMatchesValueFilter filter{DDValue(attribute, value, 0.0)};
0055   DDFilteredView fview(*cview, filter);
0056   buildGeometry(fview, muonConstants, rig);
0057 #ifdef EDM_ML_DEBUG
0058   edm::LogVerbatim("DTGeometry") << "RecoIdealGeometry " << rig.size();
0059 #endif
0060 }
0061 
0062 // DD4hep
0063 
0064 void DTGeometryParsFromDD::build(const cms::DDCompactView* cview,
0065                                  const MuonGeometryConstants& muonConstants,
0066                                  RecoIdealGeometry& rgeo) {
0067   const std::string attribute = "MuStructure";
0068   const std::string value = "MuonBarrelDT";
0069   const cms::DDFilter filter(attribute, value);
0070   cms::DDFilteredView fview(*cview, filter);
0071   buildGeometry(fview, muonConstants, rgeo);
0072 }
0073 
0074 // DD
0075 void DTGeometryParsFromDD::buildGeometry(DDFilteredView& fv,
0076                                          const MuonGeometryConstants& muonConstants,
0077                                          RecoIdealGeometry& rig) const {
0078 #ifdef EDM_ML_DEBUG
0079   edm::LogVerbatim("DTGeometryParsFromDD") << "(0) DTGeometryParsFromDD - DDD ";
0080 #endif
0081   bool doChamber = fv.firstChild();
0082 
0083   // Loop on chambers
0084   while (doChamber) {
0085     DDValue val("Type");
0086     const DDsvalues_type params(fv.mergedSpecifics());
0087     std::string type;
0088     if (DDfetch(&params, val))
0089       type = val.strings()[0];
0090     // FIXME
0091     val = DDValue("FEPos");
0092     std::string FEPos;
0093     if (DDfetch(&params, val))
0094       FEPos = val.strings()[0];
0095     insertChamber(fv, type, muonConstants, rig);
0096 
0097     // Loop on SLs
0098     bool doSL = fv.firstChild();
0099     while (doSL) {
0100       insertSuperLayer(fv, type, muonConstants, rig);
0101 
0102       bool doL = fv.firstChild();
0103       // Loop on SLs
0104       while (doL) {
0105         insertLayer(fv, type, muonConstants, rig);
0106 
0107         // fv.parent();
0108         doL = fv.nextSibling();  // go to next layer
0109       }                          // layers
0110 
0111       fv.parent();
0112       doSL = fv.nextSibling();  // go to next SL
0113     }                           // sls
0114 
0115     fv.parent();
0116     doChamber = fv.nextSibling();  // go to next chamber
0117   }                                // chambers
0118 }
0119 
0120 void DTGeometryParsFromDD::insertChamber(DDFilteredView& fv,
0121                                          const std::string& type,
0122                                          const MuonGeometryConstants& muonConstants,
0123                                          RecoIdealGeometry& rig) const {
0124   MuonGeometryNumbering mdddnum(muonConstants);
0125   DTNumberingScheme dtnum(muonConstants);
0126   int rawid = dtnum.getDetId(mdddnum.geoHistoryToBaseNumber(fv.geoHistory()));
0127   DTChamberId detId(rawid);
0128 #ifdef EDM_ML_DEBUG
0129   edm::LogVerbatim("DTGeometry") << "inserting Chamber " << detId;
0130 #endif
0131 
0132   // Chamber specific parameter (size)
0133   std::vector<double> par;
0134   par.emplace_back(DTChamberTag);
0135   std::vector<double> size = extractParameters(fv);
0136   par.insert(par.end(), size.begin(), size.end());
0137 #ifdef EDM_ML_DEBUG
0138   edm::LogVerbatim("DTGeometryParsFromDD")
0139       << "(1) DDD, Chamber DetID " << rawid << " " << par[0] << " " << par[1] << " " << par[2] << " " << par[3];
0140 #endif
0141   ///SL the definition of length, width, thickness depends on the local reference frame of the Det
0142   // width is along local X
0143   // length is along local Y
0144   // thickness is long local Z
0145 
0146   PosRotPair posRot(plane(fv));
0147   rig.insert(rawid, posRot.first, posRot.second, par);
0148 }
0149 
0150 void DTGeometryParsFromDD::insertSuperLayer(DDFilteredView& fv,
0151                                             const std::string& type,
0152                                             const MuonGeometryConstants& muonConstants,
0153                                             RecoIdealGeometry& rig) const {
0154   MuonGeometryNumbering mdddnum(muonConstants);
0155   DTNumberingScheme dtnum(muonConstants);
0156   int rawid = dtnum.getDetId(mdddnum.geoHistoryToBaseNumber(fv.geoHistory()));
0157   DTSuperLayerId slId(rawid);
0158 #ifdef EDM_ML_DEBUG
0159   edm::LogVerbatim("DTGeometry") << "inserting SuperLayer " << slId;
0160 #endif
0161 
0162   // Slayer specific parameter (size)
0163   std::vector<double> par;
0164   par.emplace_back(DTSuperLayerTag);
0165   std::vector<double> size = extractParameters(fv);
0166   par.insert(par.end(), size.begin(), size.end());
0167 #ifdef EDM_ML_DEBUG
0168   edm::LogVerbatim("DTGeometryParsFromDD")
0169       << "(2) DDD, Super Layer DetID " << rawid << " " << par[0] << " " << par[1] << " " << par[2] << " " << par[3];
0170 #endif
0171   // Ok this is the slayer position...
0172   PosRotPair posRot(plane(fv));
0173   rig.insert(slId, posRot.first, posRot.second, par);
0174 }
0175 
0176 void DTGeometryParsFromDD::insertLayer(DDFilteredView& fv,
0177                                        const std::string& type,
0178                                        const MuonGeometryConstants& muonConstants,
0179                                        RecoIdealGeometry& rig) const {
0180   MuonGeometryNumbering mdddnum(muonConstants);
0181   DTNumberingScheme dtnum(muonConstants);
0182   int rawid = dtnum.getDetId(mdddnum.geoHistoryToBaseNumber(fv.geoHistory()));
0183   DTLayerId layId(rawid);
0184 #ifdef EDM_ML_DEBUG
0185   edm::LogVerbatim("DTGeometry") << "inserting Layer " << layId;
0186 #endif
0187   // Layer specific parameter (size)
0188   std::vector<double> par;
0189   par.emplace_back(DTLayerTag);
0190   std::vector<double> size = extractParameters(fv);
0191   par.insert(par.end(), size.begin(), size.end());
0192 
0193   // Loop on wires
0194   bool doWire = fv.firstChild();
0195   int WCounter = 0;
0196   int firstWire = fv.copyno();
0197   //float wireLength = par[1]/cm;
0198   while (doWire) {
0199     WCounter++;
0200     doWire = fv.nextSibling();  // next wire
0201   }
0202   std::vector<double> sensSize = extractParameters(fv);
0203   //int lastWire=fv.copyno();
0204   par.emplace_back(firstWire);
0205   par.emplace_back(WCounter);
0206   par.emplace_back(sensSize[1]);
0207   fv.parent();
0208 
0209   PosRotPair posRot(plane(fv));
0210 
0211 #ifdef EDM_ML_DEBUG
0212   edm::LogVerbatim("DTGeometryParsFromDD")
0213       << "(3) DDD, Layer DetID " << rawid << " " << par[0] << " " << par[1] << " " << par[2] << " " << par[3] << " "
0214       << par[4] << " " << par[5] << " " << par[6];
0215 #endif
0216   rig.insert(layId, posRot.first, posRot.second, par);
0217 }
0218 
0219 std::vector<double> DTGeometryParsFromDD::extractParameters(DDFilteredView& fv) const {
0220   std::vector<double> par;
0221   if (fv.logicalPart().solid().shape() != DDSolidShape::ddbox) {
0222     DDBooleanSolid bs(fv.logicalPart().solid());
0223     DDSolid A = bs.solidA();
0224     while (A.shape() != DDSolidShape::ddbox) {
0225       DDBooleanSolid bs(A);
0226       A = bs.solidA();
0227     }
0228     par = A.parameters();
0229   } else {
0230     par = fv.logicalPart().solid().parameters();
0231   }
0232   return par;
0233 }
0234 
0235 DTGeometryParsFromDD::PosRotPair DTGeometryParsFromDD::plane(const DDFilteredView& fv) const {
0236   // extract the position
0237   const DDTranslation& trans(fv.translation());
0238 
0239   std::vector<double> gtran(3);
0240   gtran[0] = convertMmToCm(trans.x());
0241   gtran[1] = convertMmToCm(trans.y());
0242   gtran[2] = convertMmToCm(trans.z());
0243 
0244 #ifdef EDM_ML_DEBUG
0245   edm::LogVerbatim("DTGeometryParsFromDD") << "(4) DDD, Position "
0246                                            << " " << gtran[0] << " " << gtran[1] << " " << gtran[2];
0247 #endif
0248   // now the rotation
0249   //     'active' and 'passive' rotations are inverse to each other
0250   const DDRotationMatrix& rotation = fv.rotation();  //REMOVED .Inverse();
0251   DD3Vector x, y, z;
0252   rotation.GetComponents(x, y, z);
0253 
0254   std::vector<double> grmat(9);
0255   grmat[0] = x.X();
0256   grmat[1] = x.Y();
0257   grmat[2] = x.Z();
0258 
0259   grmat[3] = y.X();
0260   grmat[4] = y.Y();
0261   grmat[5] = y.Z();
0262 
0263   grmat[6] = z.X();
0264   grmat[7] = z.Y();
0265   grmat[8] = z.Z();
0266 
0267   return std::pair<std::vector<double>, std::vector<double> >(gtran, grmat);
0268 }
0269 
0270 // DD4hep
0271 
0272 void DTGeometryParsFromDD::buildGeometry(cms::DDFilteredView& fv,
0273                                          const MuonGeometryConstants& muonConstants,
0274                                          RecoIdealGeometry& rig) const {
0275 #ifdef EDM_ML_DEBUG
0276   edm::LogVerbatim("DTGeometryParsFromDD") << "(0) DTGeometryParsFromDD - DD4hep ";
0277 #endif
0278   bool doChamber = fv.firstChild();
0279 
0280   while (doChamber) {
0281     insertChamber(fv, muonConstants, rig);
0282 
0283     bool doSL = fv.nextSibling();
0284     while (doSL) {
0285       insertSuperLayer(fv, muonConstants, rig);
0286 
0287       fv.down();
0288       bool doLayers = fv.sibling();
0289       while (doLayers) {
0290         insertLayer(fv, muonConstants, rig);
0291 
0292         doLayers = fv.sibling();
0293       }
0294 
0295       doSL = fv.nextSibling();
0296     }
0297 
0298     fv.parent();
0299     doChamber = fv.firstChild();
0300   }
0301 }
0302 
0303 DTGeometryParsFromDD::PosRotPair DTGeometryParsFromDD::plane(const cms::DDFilteredView& fv) const {
0304   const Double_t* tr = fv.trans();
0305 
0306   std::vector<double> gtran(3);
0307 
0308   gtran[0] = tr[0] / dd4hep::cm;
0309   gtran[1] = tr[1] / dd4hep::cm;
0310   gtran[2] = tr[2] / dd4hep::cm;
0311 
0312 #ifdef EDM_ML_DEBUG
0313   edm::LogVerbatim("DTGeometryParsFromDD") << "(4) DD4hep, Position "
0314                                            << " " << gtran[0] << " " << gtran[1] << " " << gtran[2];
0315 #endif
0316   DDRotationMatrix rotation = fv.rotation();
0317   DD3Vector x, y, z;
0318   rotation.GetComponents(x, y, z);
0319 
0320   std::vector<double> grmat(9);
0321 
0322   grmat[0] = x.X();
0323   grmat[1] = x.Y();
0324   grmat[2] = x.Z();
0325 
0326   grmat[3] = y.X();
0327   grmat[4] = y.Y();
0328   grmat[5] = y.Z();
0329 
0330   grmat[6] = z.X();
0331   grmat[7] = z.Y();
0332   grmat[8] = z.Z();
0333 
0334   return std::pair<std::vector<double>, std::vector<double> >(gtran, grmat);
0335 }
0336 
0337 void DTGeometryParsFromDD::insertChamber(cms::DDFilteredView& fv,
0338                                          const MuonGeometryConstants& muonConstants,
0339                                          RecoIdealGeometry& rig) const {
0340   MuonGeometryNumbering mdddnum(muonConstants);
0341   DTNumberingScheme dtnum(muonConstants);
0342   int rawid = dtnum.baseNumberToUnitNumber(mdddnum.geoHistoryToBaseNumber(fv.history()));
0343   DTChamberId detId(rawid);
0344 
0345   std::vector<double> par_temp = fv.parameters();
0346   std::vector<double> par(4);
0347   par[0] = DTChamberTag;  //DTChamberTag is the ID of a Chamber
0348   par[1] = par_temp[0] / dd4hep::mm;
0349   par[2] = par_temp[1] / dd4hep::mm;
0350   par[3] = par_temp[2] / dd4hep::mm;
0351 
0352 #ifdef EDM_ML_DEBUG
0353   edm::LogVerbatim("DTGeometryParsFromDD")
0354       << "(1) DD4hep, Chamber DetID " << rawid << " " << par[0] << " " << par[1] << " " << par[2] << " " << par[3];
0355 #endif
0356   PosRotPair posRot(plane(fv));
0357   rig.insert(rawid, posRot.first, posRot.second, par);
0358 }
0359 
0360 void DTGeometryParsFromDD::insertSuperLayer(cms::DDFilteredView& fv,
0361                                             const MuonGeometryConstants& muonConstants,
0362                                             RecoIdealGeometry& rig) const {
0363   MuonGeometryNumbering mdddnum(muonConstants);
0364   DTNumberingScheme dtnum(muonConstants);
0365   int rawid = dtnum.baseNumberToUnitNumber(mdddnum.geoHistoryToBaseNumber(fv.history()));
0366   DTSuperLayerId slId(rawid);
0367 
0368   std::vector<double> par_temp = fv.parameters();
0369   std::vector<double> par(4);
0370   par[0] = DTSuperLayerTag;  //DTSuperLayerTag is the ID of a SuperLayer
0371   par[1] = par_temp[0] / dd4hep::mm;
0372   par[2] = par_temp[1] / dd4hep::mm;
0373   par[3] = par_temp[2] / dd4hep::mm;
0374 
0375 #ifdef EDM_ML_DEBUG
0376   edm::LogVerbatim("DTGeometryParsFromDD")
0377       << "(2) DD4hep, Super Layer DetID " << rawid << " " << par[0] << " " << par[1] << " " << par[2] << " " << par[3];
0378 #endif
0379   PosRotPair posRot(plane(fv));
0380   rig.insert(slId, posRot.first, posRot.second, par);
0381 }
0382 
0383 void DTGeometryParsFromDD::insertLayer(cms::DDFilteredView& fv,
0384                                        const MuonGeometryConstants& muonConstants,
0385                                        RecoIdealGeometry& rig) const {
0386   MuonGeometryNumbering mdddnum(muonConstants);
0387   DTNumberingScheme dtnum(muonConstants);
0388   int rawid = dtnum.baseNumberToUnitNumber(mdddnum.geoHistoryToBaseNumber(fv.history()));
0389   DTLayerId layId(rawid);
0390 
0391   std::vector<double> par_temp = fv.parameters();
0392   std::vector<double> par(4);
0393   par[0] = DTLayerTag;  //DTLayerTag is the ID of a Layer
0394   par[1] = par_temp[0] / dd4hep::mm;
0395   par[2] = par_temp[1] / dd4hep::mm;
0396   par[3] = par_temp[2] / dd4hep::mm;
0397 
0398   fv.down();
0399   bool doWire = fv.sibling();
0400   int firstWire = fv.volume()->GetNumber();
0401   auto const& wpar = fv.parameters();
0402   float wireLength = wpar[1] / dd4hep::mm;
0403 
0404   int WCounter = 0;
0405   while (doWire) {
0406     doWire = fv.checkChild();
0407     WCounter++;
0408   }
0409 
0410   par.emplace_back(firstWire);
0411   par.emplace_back(WCounter);
0412   par.emplace_back(wireLength);
0413 
0414   fv.up();
0415 
0416   PosRotPair posRot(plane(fv));
0417 
0418 #ifdef EDM_ML_DEBUG
0419   edm::LogVerbatim("DTGeometryParsFromDD")
0420       << "(3) DD4hep, Layer DetID " << rawid << " " << par[0] << " " << par[1] << " " << par[2] << " " << par[3] << " "
0421       << par[4] << " " << par[5] << " " << par[6];
0422 #endif
0423   rig.insert(layId, posRot.first, posRot.second, par);
0424 }