Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 //#define EDM_ML_DEBUG
0002 
0003 #include "Geometry/MTDGeometryBuilder/interface/MTDParametersFromDD.h"
0004 #include "Geometry/MTDCommonData/interface/MTDTopologyMode.h"
0005 #include "CondFormats/GeometryObjects/interface/PMTDParameters.h"
0006 #include "DetectorDescription/Core/interface/DDCompactView.h"
0007 #include "DetectorDescription/Core/interface/DDFilteredView.h"
0008 #include "DetectorDescription/Core/interface/DDutils.h"
0009 #include "DetectorDescription/DDCMS/interface/DDCompactView.h"
0010 #include "DetectorDescription/DDCMS/interface/DDFilteredView.h"
0011 
0012 using namespace MTDTopologyMode;
0013 
0014 namespace {
0015   int getMTDTopologyMode(const char* s, const DDsvalues_type& sv) {
0016     DDValue val(s);
0017     if (DDfetch(&sv, val)) {
0018       const std::vector<std::string>& fvec = val.strings();
0019       if (fvec.empty()) {
0020         throw cms::Exception("MTDParametersFromDD") << "Failed to get " << s << " tag.";
0021       }
0022 
0023       int result(-1);
0024       MTDTopologyMode::Mode eparser = MTDTopologyMode::MTDStringToEnumParser(fvec[0]);
0025       result = static_cast<int>(eparser);
0026       return result;
0027     } else {
0028       throw cms::Exception("MTDParametersFromDD") << "Failed to get " << s << " tag.";
0029     }
0030   }
0031 }  // namespace
0032 
0033 bool MTDParametersFromDD::build(const DDCompactView* cvp, PMTDParameters& ptp) {
0034   std::array<std::string, 2> mtdSubdet{{"BTL", "ETL"}};
0035   int subdet(0);
0036   for (const auto& name : mtdSubdet) {
0037     auto const& v = cvp->vector(name);
0038     if (!v.empty()) {
0039       subdet++;
0040       std::vector<int> subdetPars = dbl_to_int(v);
0041       putOne(subdet, subdetPars, ptp);
0042     } else {
0043       throw cms::Exception("MTDParametersFromDD") << "Not found " << name << " but needed.";
0044     }
0045   }
0046 
0047   ptp.vpars_ = dbl_to_int(cvp->vector("vPars"));
0048 
0049   std::string attribute = "OnlyForMTDRecNumbering";
0050   DDSpecificsHasNamedValueFilter filter1{attribute};
0051   DDFilteredView fv1(*cvp, filter1);
0052   bool ok = fv1.firstChild();
0053   int topoMode(-1);
0054   if (ok) {
0055     DDsvalues_type sv(fv1.mergedSpecifics());
0056     topoMode = getMTDTopologyMode("TopologyMode", sv);
0057     ptp.topologyMode_ = topoMode;
0058   } else {
0059     throw cms::Exception("MTDParametersFromDD") << "Not found " << attribute.c_str() << " but needed.";
0060   }
0061 
0062   if (MTDTopologyMode::etlLayoutFromTopoMode(topoMode) == ETLDetId::EtlLayout::v5 ||
0063       MTDTopologyMode::etlLayoutFromTopoMode(topoMode) == ETLDetId::EtlLayout::v8) {
0064     std::array<std::string, 8> etlLayout{{
0065         "StartCopyNo_Front_Left",
0066         "StartCopyNo_Front_Right",
0067         "StartCopyNo_Back_Left",
0068         "StartCopyNo_Back_Right",
0069         "Offset_Front_Left",
0070         "Offset_Front_Right",
0071         "Offset_Back_Left",
0072         "Offset_Back_Right",
0073     }};
0074     int sector(10);
0075     for (const auto& name : etlLayout) {
0076       auto const& v = cvp->vector(name);
0077       if (!v.empty()) {
0078         sector++;
0079         std::vector<int> ipos = dbl_to_int(v);
0080         putOne(sector, ipos, ptp);
0081       } else {
0082         throw cms::Exception("MTDParametersFromDD") << "Not found " << name << " but needed.";
0083       }
0084     }
0085   }
0086 
0087   return true;
0088 }
0089 
0090 bool MTDParametersFromDD::build(const cms::DDCompactView* cvp, PMTDParameters& ptp) {
0091   cms::DDVectorsMap vmap = cvp->detector()->vectors();
0092 
0093   std::array<std::string, 2> mtdSubdet{{"BTL", "ETL"}};
0094   int subdet(0);
0095   for (const auto& name : mtdSubdet) {
0096     bool found(false);
0097     for (auto const& it : vmap) {
0098       if (dd4hep::dd::compareEqual(dd4hep::dd::noNamespace(it.first), name)) {
0099         subdet++;
0100         std::vector<int> subdetPars;
0101         subdetPars.reserve(it.second.size());
0102         for (const auto& i : it.second)
0103           subdetPars.emplace_back(std::round(i));
0104         putOne(subdet, subdetPars, ptp);
0105         found = true;
0106         break;
0107       }
0108     }
0109     if (!found) {
0110       throw cms::Exception("MTDParametersFromDD") << "Not found " << name << " but needed.";
0111     }
0112   }
0113 
0114   auto it = vmap.find("vPars");
0115   if (it != end(vmap)) {
0116     std::vector<int> tmpVec;
0117     for (const auto& i : it->second)
0118       tmpVec.emplace_back(std::round(i));
0119     ptp.vpars_ = tmpVec;
0120   }
0121 
0122   cms::DDSpecParRefs ref;
0123   const cms::DDSpecParRegistry& mypar = cvp->specpars();
0124   std::string attribute = "OnlyForMTDRecNumbering";
0125   mypar.filter(ref, attribute, "MTD");
0126 
0127   std::string topoModeS(mypar.specPar("mtdNumbering")->strValue("TopologyMode"));
0128   int topoMode(-1);
0129   if (!topoModeS.empty()) {
0130     MTDTopologyMode::Mode eparser = MTDTopologyMode::MTDStringToEnumParser(topoModeS);
0131     topoMode = static_cast<int>(eparser);
0132     ptp.topologyMode_ = topoMode;
0133   } else {
0134     throw cms::Exception("MTDParametersFromDD") << "Not found " << attribute.c_str() << " but needed.";
0135   }
0136 
0137   if (MTDTopologyMode::etlLayoutFromTopoMode(topoMode) == ETLDetId::EtlLayout::v5 ||
0138       MTDTopologyMode::etlLayoutFromTopoMode(topoMode) == ETLDetId::EtlLayout::v8) {
0139     std::array<std::string, 8> etlLayout{{
0140         "StartCopyNo_Front_Left",
0141         "StartCopyNo_Front_Right",
0142         "StartCopyNo_Back_Left",
0143         "StartCopyNo_Back_Right",
0144         "Offset_Front_Left",
0145         "Offset_Front_Right",
0146         "Offset_Back_Left",
0147         "Offset_Back_Right",
0148     }};
0149     int sector(10);  // add vector index with offset, to distinguish from subdet
0150     for (const auto& name : etlLayout) {
0151       bool found(false);
0152       for (auto const& it : vmap) {
0153         if (dd4hep::dd::compareEqual(dd4hep::dd::noNamespace(it.first), name)) {
0154           sector++;
0155           std::vector<int> ipos;
0156           ipos.reserve(it.second.size());
0157           for (const auto& i : it.second)
0158             ipos.emplace_back(std::round(i));
0159           putOne(sector, ipos, ptp);
0160           found = true;
0161           break;
0162         }
0163       }
0164       if (!found) {
0165         throw cms::Exception("MTDParametersFromDD") << "Not found " << name << " but needed.";
0166       }
0167     }
0168   }
0169 
0170   return true;
0171 }
0172 
0173 void MTDParametersFromDD::putOne(int subdet, std::vector<int>& vpars, PMTDParameters& ptp) {
0174   PMTDParameters::Item item;
0175   item.id_ = subdet;
0176   item.vpars_ = vpars;
0177   ptp.vitems_.emplace_back(item);
0178 #ifdef EDM_ML_DEBUG
0179   auto print_item = [&]() {
0180     std::stringstream ss;
0181     ss << item.id_ << " with " << item.vpars_.size() << " elements:";
0182     for (const auto& thePar : item.vpars_) {
0183       ss << " " << thePar;
0184     }
0185     return ss.str();
0186   };
0187   edm::LogInfo("MTDParametersFromDD") << "Adding PMTDParameters item: " << print_item();
0188 #endif
0189 }