Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
//#define EDM_ML_DEBUG

#include "Geometry/MTDGeometryBuilder/interface/MTDParametersFromDD.h"
#include "Geometry/MTDCommonData/interface/MTDTopologyMode.h"
#include "CondFormats/GeometryObjects/interface/PMTDParameters.h"
#include "DetectorDescription/Core/interface/DDCompactView.h"
#include "DetectorDescription/Core/interface/DDFilteredView.h"
#include "DetectorDescription/Core/interface/DDutils.h"
#include "DetectorDescription/DDCMS/interface/DDCompactView.h"
#include "DetectorDescription/DDCMS/interface/DDFilteredView.h"

using namespace MTDTopologyMode;

namespace {
  int getMTDTopologyMode(const char* s, const DDsvalues_type& sv) {
    DDValue val(s);
    if (DDfetch(&sv, val)) {
      const std::vector<std::string>& fvec = val.strings();
      if (fvec.empty()) {
        throw cms::Exception("MTDParametersFromDD") << "Failed to get " << s << " tag.";
      }

      int result(-1);
      MTDTopologyMode::Mode eparser = MTDTopologyMode::MTDStringToEnumParser(fvec[0]);
      result = static_cast<int>(eparser);
      return result;
    } else {
      throw cms::Exception("MTDParametersFromDD") << "Failed to get " << s << " tag.";
    }
  }
}  // namespace

bool MTDParametersFromDD::build(const DDCompactView* cvp, PMTDParameters& ptp) {
  std::array<std::string, 2> mtdSubdet{{"BTL", "ETL"}};
  int subdet(0);
  for (const auto& name : mtdSubdet) {
    auto const& v = cvp->vector(name);
    if (!v.empty()) {
      subdet++;
      std::vector<int> subdetPars = dbl_to_int(v);
      putOne(subdet, subdetPars, ptp);
    } else {
      throw cms::Exception("MTDParametersFromDD") << "Not found " << name << " but needed.";
    }
  }

  ptp.vpars_ = dbl_to_int(cvp->vector("vPars"));

  std::string attribute = "OnlyForMTDRecNumbering";
  DDSpecificsHasNamedValueFilter filter1{attribute};
  DDFilteredView fv1(*cvp, filter1);
  bool ok = fv1.firstChild();
  int topoMode(-1);
  if (ok) {
    DDsvalues_type sv(fv1.mergedSpecifics());
    topoMode = getMTDTopologyMode("TopologyMode", sv);
    ptp.topologyMode_ = topoMode;
  } else {
    throw cms::Exception("MTDParametersFromDD") << "Not found " << attribute.c_str() << " but needed.";
  }

  std::vector<std::string> etlLayout;
  if (static_cast<int>(MTDTopologyMode::etlLayoutFromTopoMode(topoMode)) <=
      static_cast<int>(MTDTopologyMode::EtlLayout::v8)) {
    etlLayout.emplace_back("StartCopyNo_Front_Left");
    etlLayout.emplace_back("StartCopyNo_Front_Right");
    etlLayout.emplace_back("StartCopyNo_Back_Left");
    etlLayout.emplace_back("StartCopyNo_Back_Right");
    etlLayout.emplace_back("Offset_Front_Left");
    etlLayout.emplace_back("Offset_Front_Right");
    etlLayout.emplace_back("Offset_Back_Left");
    etlLayout.emplace_back("Offset_Back_Right");
  } else if (static_cast<int>(MTDTopologyMode::etlLayoutFromTopoMode(topoMode)) >
             static_cast<int>(MTDTopologyMode::EtlLayout::v8)) {
    etlLayout.emplace_back("StartCopyNo_Front_Disc_1");
    etlLayout.emplace_back("StartCopyNo_Back_Disc_1");
    etlLayout.emplace_back("StartCopyNo_Front_Disc_2");
    etlLayout.emplace_back("StartCopyNo_Back_Disc_2");
    etlLayout.emplace_back("Offset_Front_Disc_1");
    etlLayout.emplace_back("Offset_Back_Disc_1");
    etlLayout.emplace_back("Offset_Front_Disc_2");
    etlLayout.emplace_back("Offset_Back_Disc_2");
  }
  int sector(10);
  for (const auto& name : etlLayout) {
    auto const& v = cvp->vector(name);
    if (!v.empty()) {
      sector++;
      std::vector<int> ipos = dbl_to_int(v);
      putOne(sector, ipos, ptp);
    } else {
      throw cms::Exception("MTDParametersFromDD") << "Not found " << name << " but needed.";
    }
  }

  return true;
}

bool MTDParametersFromDD::build(const cms::DDCompactView* cvp, PMTDParameters& ptp) {
  cms::DDVectorsMap vmap = cvp->detector()->vectors();

  std::array<std::string, 2> mtdSubdet{{"BTL", "ETL"}};
  int subdet(0);
  for (const auto& name : mtdSubdet) {
    bool found(false);
    for (auto const& it : vmap) {
      if (dd4hep::dd::compareEqual(dd4hep::dd::noNamespace(it.first), name)) {
        subdet++;
        std::vector<int> subdetPars;
        subdetPars.reserve(it.second.size());
        for (const auto& i : it.second)
          subdetPars.emplace_back(std::round(i));
        putOne(subdet, subdetPars, ptp);
        found = true;
        break;
      }
    }
    if (!found) {
      throw cms::Exception("MTDParametersFromDD") << "Not found " << name << " but needed.";
    }
  }

  auto it = vmap.find("vPars");
  if (it != end(vmap)) {
    std::vector<int> tmpVec;
    for (const auto& i : it->second)
      tmpVec.emplace_back(std::round(i));
    ptp.vpars_ = tmpVec;
  }

  cms::DDSpecParRefs ref;
  const cms::DDSpecParRegistry& mypar = cvp->specpars();
  std::string attribute = "OnlyForMTDRecNumbering";
  mypar.filter(ref, attribute, "MTD");

  std::string topoModeS(mypar.specPar("mtdNumbering")->strValue("TopologyMode"));
  int topoMode(-1);
  if (!topoModeS.empty()) {
    MTDTopologyMode::Mode eparser = MTDTopologyMode::MTDStringToEnumParser(topoModeS);
    topoMode = static_cast<int>(eparser);
    ptp.topologyMode_ = topoMode;
  } else {
    throw cms::Exception("MTDParametersFromDD") << "Not found " << attribute.c_str() << " but needed.";
  }

  std::vector<std::string> etlLayout;
  if (static_cast<int>(MTDTopologyMode::etlLayoutFromTopoMode(topoMode)) <=
      static_cast<int>(MTDTopologyMode::EtlLayout::v8)) {
    etlLayout.emplace_back("StartCopyNo_Front_Left");
    etlLayout.emplace_back("StartCopyNo_Front_Right");
    etlLayout.emplace_back("StartCopyNo_Back_Left");
    etlLayout.emplace_back("StartCopyNo_Back_Right");
    etlLayout.emplace_back("Offset_Front_Left");
    etlLayout.emplace_back("Offset_Front_Right");
    etlLayout.emplace_back("Offset_Back_Left");
    etlLayout.emplace_back("Offset_Back_Right");
  } else if (static_cast<int>(MTDTopologyMode::etlLayoutFromTopoMode(topoMode)) >
             static_cast<int>(MTDTopologyMode::EtlLayout::v8)) {
    etlLayout.emplace_back("StartCopyNo_Front_Disc_1");
    etlLayout.emplace_back("StartCopyNo_Back_Disc_1");
    etlLayout.emplace_back("StartCopyNo_Front_Disc_2");
    etlLayout.emplace_back("StartCopyNo_Back_Disc_2");
    etlLayout.emplace_back("Offset_Front_Disc_1");
    etlLayout.emplace_back("Offset_Back_Disc_1");
    etlLayout.emplace_back("Offset_Front_Disc_2");
    etlLayout.emplace_back("Offset_Back_Disc_2");
  }
  int sector(10);  // add vector index with offset, to distinguish from subdet
  for (const auto& name : etlLayout) {
    bool found(false);
    for (auto const& it : vmap) {
      if (dd4hep::dd::compareEqual(dd4hep::dd::noNamespace(it.first), name)) {
        sector++;
        std::vector<int> ipos;
        ipos.reserve(it.second.size());
        for (const auto& i : it.second)
          ipos.emplace_back(std::round(i));
        putOne(sector, ipos, ptp);
        found = true;
        break;
      }
    }
    if (!found) {
      throw cms::Exception("MTDParametersFromDD") << "Not found " << name << " but needed.";
    }
  }

  return true;
}

void MTDParametersFromDD::putOne(int subdet, std::vector<int>& vpars, PMTDParameters& ptp) {
  PMTDParameters::Item item;
  item.id_ = subdet;
  item.vpars_ = vpars;
  ptp.vitems_.emplace_back(item);
#ifdef EDM_ML_DEBUG
  auto print_item = [&]() {
    std::stringstream ss;
    ss << item.id_ << " with " << item.vpars_.size() << " elements:";
    for (const auto& thePar : item.vpars_) {
      ss << " " << thePar;
    }
    return ss.str();
  };
  edm::LogInfo("MTDParametersFromDD") << "Adding PMTDParameters item: " << print_item();
#endif
}