Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-07-12 02:58:21

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     std::array<std::string, 8> etlLayout{{
0064         "StartCopyNo_Front_Left",
0065         "StartCopyNo_Front_Right",
0066         "StartCopyNo_Back_Left",
0067         "StartCopyNo_Back_Right",
0068         "Offset_Front_Left",
0069         "Offset_Front_Right",
0070         "Offset_Back_Left",
0071         "Offset_Back_Right",
0072     }};
0073     int sector(10);
0074     for (const auto& name : etlLayout) {
0075       auto const& v = cvp->vector(name);
0076       if (!v.empty()) {
0077         sector++;
0078         std::vector<int> ipos = dbl_to_int(v);
0079         putOne(sector, ipos, ptp);
0080       } else {
0081         throw cms::Exception("MTDParametersFromDD") << "Not found " << name << " but needed.";
0082       }
0083     }
0084   }
0085 
0086   return true;
0087 }
0088 
0089 bool MTDParametersFromDD::build(const cms::DDCompactView* cvp, PMTDParameters& ptp) {
0090   cms::DDVectorsMap vmap = cvp->detector()->vectors();
0091 
0092   std::array<std::string, 2> mtdSubdet{{"BTL", "ETL"}};
0093   int subdet(0);
0094   for (const auto& name : mtdSubdet) {
0095     bool found(false);
0096     for (auto const& it : vmap) {
0097       if (dd4hep::dd::compareEqual(dd4hep::dd::noNamespace(it.first), name)) {
0098         subdet++;
0099         std::vector<int> subdetPars;
0100         for (const auto& i : it.second)
0101           subdetPars.emplace_back(std::round(i));
0102         putOne(subdet, subdetPars, ptp);
0103         found = true;
0104         break;
0105       }
0106     }
0107     if (!found) {
0108       throw cms::Exception("MTDParametersFromDD") << "Not found " << name << " but needed.";
0109     }
0110   }
0111 
0112   auto it = vmap.find("vPars");
0113   if (it != end(vmap)) {
0114     std::vector<int> tmpVec;
0115     for (const auto& i : it->second)
0116       tmpVec.emplace_back(std::round(i));
0117     ptp.vpars_ = tmpVec;
0118   }
0119 
0120   cms::DDSpecParRefs ref;
0121   const cms::DDSpecParRegistry& mypar = cvp->specpars();
0122   std::string attribute = "OnlyForMTDRecNumbering";
0123   mypar.filter(ref, attribute, "MTD");
0124 
0125   std::string topoModeS(mypar.specPar("mtdNumbering")->strValue("TopologyMode"));
0126   int topoMode(-1);
0127   if (!topoModeS.empty()) {
0128     MTDTopologyMode::Mode eparser = MTDTopologyMode::MTDStringToEnumParser(topoModeS);
0129     topoMode = static_cast<int>(eparser);
0130     ptp.topologyMode_ = topoMode;
0131   } else {
0132     throw cms::Exception("MTDParametersFromDD") << "Not found " << attribute.c_str() << " but needed.";
0133   }
0134 
0135   if (MTDTopologyMode::etlLayoutFromTopoMode(topoMode) == ETLDetId::EtlLayout::v5) {
0136     std::array<std::string, 8> etlLayout{{
0137         "StartCopyNo_Front_Left",
0138         "StartCopyNo_Front_Right",
0139         "StartCopyNo_Back_Left",
0140         "StartCopyNo_Back_Right",
0141         "Offset_Front_Left",
0142         "Offset_Front_Right",
0143         "Offset_Back_Left",
0144         "Offset_Back_Right",
0145     }};
0146     int sector(10);  // add vector index with offset, to distinguish from subdet
0147     for (const auto& name : etlLayout) {
0148       bool found(false);
0149       for (auto const& it : vmap) {
0150         if (dd4hep::dd::compareEqual(dd4hep::dd::noNamespace(it.first), name)) {
0151           sector++;
0152           std::vector<int> ipos;
0153           for (const auto& i : it.second)
0154             ipos.emplace_back(std::round(i));
0155           putOne(sector, ipos, ptp);
0156           found = true;
0157           break;
0158         }
0159       }
0160       if (!found) {
0161         throw cms::Exception("MTDParametersFromDD") << "Not found " << name << " but needed.";
0162       }
0163     }
0164   }
0165 
0166   return true;
0167 }
0168 
0169 void MTDParametersFromDD::putOne(int subdet, std::vector<int>& vpars, PMTDParameters& ptp) {
0170   PMTDParameters::Item item;
0171   item.id_ = subdet;
0172   item.vpars_ = vpars;
0173   ptp.vitems_.emplace_back(item);
0174 #ifdef EDM_ML_DEBUG
0175   auto print_item = [&]() {
0176     std::stringstream ss;
0177     ss << item.id_ << " with " << item.vpars_.size() << " elements:";
0178     for (const auto& thePar : item.vpars_) {
0179       ss << " " << thePar;
0180     }
0181     return ss.str();
0182   };
0183   edm::LogInfo("MTDParametersFromDD") << "Adding PMTDParameters item: " << print_item();
0184 #endif
0185 }