Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:48:54

0001 #include "Geometry/MTDNumberingBuilder/plugins/DDCmsMTDConstruction.h"
0002 
0003 #include <utility>
0004 #include <sstream>
0005 
0006 #include "DetectorDescription/Core/interface/DDFilteredView.h"
0007 #include "DetectorDescription/Core/interface/DDCompactView.h"
0008 #include "DetectorDescription/DDCMS/interface/DDFilteredView.h"
0009 #include "DetectorDescription/DDCMS/interface/DDCompactView.h"
0010 #include "DetectorDescription/DDCMS/interface/DDSpecParRegistry.h"
0011 #include "Geometry/MTDNumberingBuilder/interface/GeometricTimingDet.h"
0012 #include "Geometry/MTDNumberingBuilder/plugins/ExtractStringFromDD.h"
0013 #include "Geometry/MTDNumberingBuilder/plugins/CmsMTDConstruction.h"
0014 
0015 #include "DataFormats/ForwardDetId/interface/BTLDetId.h"
0016 #include "DataFormats/ForwardDetId/interface/ETLDetId.h"
0017 
0018 //#define EDM_ML_DEBUG
0019 
0020 class DDNameFilter : public DDFilter {
0021 public:
0022   void add(const std::string& add) { allowed_.emplace_back(add); }
0023   void veto(const std::string& veto) { veto_.emplace_back(veto); }
0024 
0025   bool accept(const DDExpandedView& ev) const final {
0026     for (const auto& test : veto_) {
0027       if (ev.logicalPart().name().fullname().find(test) != std::string::npos)
0028         return false;
0029     }
0030     for (const auto& test : allowed_) {
0031       if (ev.logicalPart().name().fullname().find(test) != std::string::npos)
0032         return true;
0033     }
0034     return false;
0035   }
0036 
0037 private:
0038   std::vector<std::string> allowed_;
0039   std::vector<std::string> veto_;
0040 };
0041 
0042 std::unique_ptr<GeometricTimingDet> DDCmsMTDConstruction::construct(const DDCompactView& cpv) {
0043   std::string attribute{"CMSCutsRegion"};
0044   DDNameFilter filter;
0045   filter.add("mtd:");
0046   filter.add("btl:");
0047   filter.add("etl:");
0048   filter.veto("service");
0049   filter.veto("support");
0050   filter.veto("FSide");
0051   filter.veto("BSide");
0052   filter.veto("LSide");
0053   filter.veto("RSide");
0054   filter.veto("Between");
0055   filter.veto("SupportPlate");
0056   filter.veto("Shield");
0057   filter.veto("ThermalScreen");
0058   filter.veto("Aluminium_Disc");
0059   filter.veto("MIC6_Aluminium_Disc");
0060   filter.veto("ThermalPad");
0061   filter.veto("AlN");
0062   filter.veto("LairdFilm");
0063   filter.veto("ETROC");
0064   filter.veto("SensorModule");
0065   filter.veto("DiscSector");
0066   filter.veto("LGAD_Substrate");
0067 
0068   DDFilteredView fv(cpv, filter);
0069 
0070   CmsMTDStringToEnum theCmsMTDStringToEnum;
0071 
0072   auto check_root = theCmsMTDStringToEnum.type(ExtractStringFromDD<DDFilteredView>::getString(attribute, &fv));
0073   if (check_root != GeometricTimingDet::MTD) {
0074     fv.firstChild();
0075     auto check_child = theCmsMTDStringToEnum.type(ExtractStringFromDD<DDFilteredView>::getString(attribute, &fv));
0076     if (check_child != GeometricTimingDet::MTD) {
0077       throw cms::Exception("Configuration") << " The first child of the DDFilteredView is not what is expected \n"
0078                                             << ExtractStringFromDD<DDFilteredView>::getString(attribute, &fv) << "\n";
0079     }
0080     fv.parent();
0081   }
0082 
0083 #ifdef EDM_ML_DEBUG
0084   edm::LogInfo("MTDNumbering") << "Top level node = " << fv.name();
0085 #endif
0086 
0087   auto mtd = std::make_unique<GeometricTimingDet>(&fv, GeometricTimingDet::MTD);
0088   size_t limit = 0;
0089   CmsMTDConstruction<DDFilteredView> theCmsMTDConstruction;
0090 
0091   std::vector<GeometricTimingDet*> subdet;
0092   std::vector<GeometricTimingDet*> layer;
0093 
0094   do {
0095     GeometricTimingDet::GeometricTimingEnumType fullNode = theCmsMTDStringToEnum.type(fv.name());
0096     GeometricTimingDet::GeometricTimingEnumType thisNode =
0097         theCmsMTDStringToEnum.type(fv.name().substr(0, CmsMTDStringToEnum::kModStrLen));
0098     size_t num = fv.geoHistory().size();
0099 
0100 #ifdef EDM_ML_DEBUG
0101     edm::LogVerbatim("MTDNumbering") << "Module = " << fv.name() << " fullNode = " << fullNode
0102                                      << " thisNode = " << thisNode;
0103 #endif
0104 
0105     if (fullNode == GeometricTimingDet::BTL || fullNode == GeometricTimingDet::ETL) {
0106       limit = 0;
0107 
0108       // define subdetectors as GeometricTimingDet components
0109 
0110       subdet.emplace_back(theCmsMTDConstruction.buildSubdet(fv));
0111     }
0112     if (fullNode == GeometricTimingDet::BTLLayer || fullNode == GeometricTimingDet::ETLDisc) {
0113       layer.emplace_back(theCmsMTDConstruction.buildLayer(fv));
0114 #ifdef EDM_ML_DEBUG
0115       edm::LogVerbatim("MTDNumbering") << "Number of layers: " << layer.size();
0116 #endif
0117     }
0118     //
0119     // the level chosen corresponds to wafers for D50 and previous scenarios
0120     //
0121     if ((thisNode == GeometricTimingDet::BTLModule) && limit == 0) {
0122       limit = num + 1;
0123     }
0124     //
0125     // workaround to make old and TDR structure to cohexist until needed
0126     //
0127     else if ((thisNode == GeometricTimingDet::ETLModule) && limit == 0) {
0128       if (theCmsMTDConstruction.isETLtdr(fv)) {
0129         limit = num;
0130       } else {
0131         limit = num + 1;
0132       }
0133     }
0134     if (num != limit && limit > 0) {
0135       continue;
0136     }
0137     if (thisNode == GeometricTimingDet::BTLModule) {
0138 #ifdef EDM_ML_DEBUG
0139       edm::LogVerbatim("MTDNumbering") << "Registered in GeometricTimingDet as type " << thisNode;
0140 #endif
0141       theCmsMTDConstruction.buildBTLModule(fv, layer.back());
0142       limit = num;
0143     } else if (thisNode == GeometricTimingDet::ETLModule) {
0144 #ifdef EDM_ML_DEBUG
0145       edm::LogVerbatim("MTDNumbering") << "Registered in GeometricTimingDet as type " << thisNode;
0146 #endif
0147       theCmsMTDConstruction.buildETLModule(fv, layer.back());
0148       limit = num;
0149     }
0150   } while (fv.next());
0151 
0152   // sort GeometricTimingDet
0153 
0154 #ifdef EDM_ML_DEBUG
0155   auto comp = mtd->deepComponents();
0156   std::stringstream before(std::stringstream::in | std::stringstream::out);
0157   for (const auto& it : comp) {
0158     before << "ORDER1 " << it->geographicalId().rawId() << " " << it->type() << " " << it->translation().z() << "\n";
0159   }
0160   edm::LogVerbatim("MTDNumbering") << "GeometricTimingDet order before sorting \n" << before.str();
0161 #endif
0162 
0163   for (size_t index = 0; index < layer.size(); index++) {
0164     GeometricTimingDet::ConstGeometricTimingDetContainer& icomp = layer[index]->components();
0165     std::stable_sort(icomp.begin(), icomp.end(), CmsMTDConstruction<DDFilteredView>::mtdOrderZ);
0166     std::stable_sort(icomp.begin(), icomp.end(), CmsMTDConstruction<DDFilteredView>::mtdOrderRR);
0167     if (index > 0) {
0168       std::stable_sort(icomp.begin(), icomp.end(), CmsMTDConstruction<DDFilteredView>::mtdOrderPhi);
0169     }
0170   }
0171 
0172   // Add layers to subdetectors:
0173   // first BTL (one layer only)
0174 
0175   subdet[0]->addComponent(layer[0]);
0176 
0177   // then ETL (number of layers depend on preTDR design or not)
0178 
0179   if (layer.size() == kNLayerPreTDR) {
0180     subdet[1]->addComponent(layer[1]);
0181     subdet[2]->addComponent(layer[2]);
0182   } else if (layer.size() == kNLayerTDR) {
0183     subdet[1]->addComponent(layer[1]);
0184     subdet[1]->addComponent(layer[2]);
0185     subdet[2]->addComponent(layer[3]);
0186     subdet[2]->addComponent(layer[4]);
0187   } else {
0188     throw cms::Exception("MTDNumbering") << "Wrong number of layers: " << layer.size();
0189   }
0190 
0191   // Add subdetectors to MTD
0192 
0193   mtd.get()->addComponents(subdet);
0194 
0195 #ifdef EDM_ML_DEBUG
0196   comp.clear();
0197   comp = mtd->deepComponents();
0198   std::stringstream after(std::stringstream::in | std::stringstream::out);
0199   for (const auto& it : comp) {
0200     after << "ORDER2 " << it->geographicalId().rawId() << " " << it->type() << " " << it->translation().z() << "\n";
0201   }
0202   edm::LogVerbatim("MTDNumbering") << "GeometricTimingDet order after sorting \n" << after.str();
0203 #endif
0204 
0205   return mtd;
0206 }
0207 
0208 std::unique_ptr<GeometricTimingDet> DDCmsMTDConstruction::construct(const cms::DDCompactView& cpv) {
0209   cms::DDFilteredView fv(cpv.detector(), cpv.detector()->worldVolume());
0210 
0211   fv.next(0);
0212   edm::LogVerbatim("DD4hep_MTDNumbering") << fv.path();
0213   auto mtd = std::make_unique<GeometricTimingDet>(&fv, GeometricTimingDet::MTD);
0214 
0215   cms::DDSpecParRefs ref;
0216   const cms::DDSpecParRegistry& mypar = cpv.specpars();
0217   std::string attribute("MtdDDStructure");
0218   mypar.filter(ref, attribute, "BarrelTimingLayer");
0219   mypar.filter(ref, attribute, "EndcapTimingLayer");
0220   fv.mergedSpecifics(ref);
0221 
0222 #ifdef EDM_ML_DEBUG
0223   edm::LogVerbatim("DD4hep_MTDNumbering") << "Active filters using " << attribute << ":";
0224   fv.printFilter();
0225   edm::LogVerbatim("Geometry").log([&ref](auto& log) {
0226     log << "Filtered DD SpecPar Registry size: " << ref.size() << "\n";
0227     for (const auto& t : ref) {
0228       log << "\nSpecPar " << t.first << ":\nRegExps { ";
0229       for (const auto& ki : t.second->paths)
0230         log << ki << " ";
0231       log << "};\n ";
0232       for (const auto& kl : t.second->spars) {
0233         log << kl.first << " = ";
0234         for (const auto& kil : kl.second) {
0235           log << kil << " ";
0236         }
0237         log << "\n ";
0238       }
0239     }
0240   });
0241 #endif
0242 
0243   bool doSubdet = fv.firstChild();
0244   edm::LogVerbatim("DD4hep_MTDNumbering") << fv.path();
0245 
0246   CmsMTDStringToEnum theCmsMTDStringToEnum;
0247 
0248   CmsMTDConstruction<cms::DDFilteredView> theCmsMTDConstruction;
0249 
0250   std::vector<GeometricTimingDet*> subdet;
0251   std::vector<GeometricTimingDet*> layer;
0252 
0253   while (doSubdet) {
0254     std::string nodeName(fv.name());
0255     GeometricTimingDet::GeometricTimingEnumType fullNode = theCmsMTDStringToEnum.type(nodeName);
0256     GeometricTimingDet::GeometricTimingEnumType thisNode =
0257         theCmsMTDStringToEnum.type(nodeName.substr(0, CmsMTDStringToEnum::kModStrLen));
0258 
0259 #ifdef EDM_ML_DEBUG
0260     edm::LogVerbatim("DD4hep_MTDNumbering") << fv.path();
0261     edm::LogVerbatim("DD4hep_MTDNumbering")
0262         << "Module = " << fv.name() << " fullNode = " << fullNode << " thisNode = " << thisNode;
0263 #endif
0264 
0265     if (fullNode == GeometricTimingDet::BTL || fullNode == GeometricTimingDet::ETL) {
0266       // define subdetectors as GeometricTimingDet components
0267 
0268       subdet.emplace_back(theCmsMTDConstruction.buildSubdet(fv));
0269     }
0270     if (fullNode == GeometricTimingDet::BTLLayer || fullNode == GeometricTimingDet::ETLDisc) {
0271       layer.emplace_back(theCmsMTDConstruction.buildLayer(fv));
0272 #ifdef EDM_ML_DEBUG
0273       edm::LogVerbatim("DD4hep_MTDNumbering") << "Number of layers: " << layer.size();
0274 #endif
0275     }
0276     if (thisNode == GeometricTimingDet::BTLModule) {
0277 #ifdef EDM_ML_DEBUG
0278       edm::LogVerbatim("DD4hep_MTDNumbering") << "Registered in GeometricTimingDet as type " << thisNode;
0279 #endif
0280       theCmsMTDConstruction.buildBTLModule(fv, layer.back());
0281     } else if (thisNode == GeometricTimingDet::ETLModule) {
0282 #ifdef EDM_ML_DEBUG
0283       edm::LogVerbatim("DD4hep_MTDNumbering") << "Registered in GeometricTimingDet as type " << thisNode;
0284 #endif
0285       theCmsMTDConstruction.buildETLModule(fv, layer.back());
0286     }
0287 
0288     doSubdet = fv.firstChild();
0289   }
0290 
0291   // sort GeometricTimingDet
0292 
0293 #ifdef EDM_ML_DEBUG
0294   auto comp = mtd->deepComponents();
0295   std::stringstream before(std::stringstream::in | std::stringstream::out);
0296   for (const auto& it : comp) {
0297     before << "ORDER1 " << it->geographicalId().rawId() << " " << it->type() << " " << it->translation().z() << "\n";
0298   }
0299   edm::LogVerbatim("DD4hep_MTDNumbering") << "GeometricTimingDet order before sorting \n" << before.str();
0300 #endif
0301 
0302   for (size_t index = 0; index < layer.size(); index++) {
0303     GeometricTimingDet::ConstGeometricTimingDetContainer& icomp = layer[index]->components();
0304     std::stable_sort(icomp.begin(), icomp.end(), CmsMTDConstruction<cms::DDFilteredView>::mtdOrderZ);
0305     std::stable_sort(icomp.begin(), icomp.end(), CmsMTDConstruction<cms::DDFilteredView>::mtdOrderRR);
0306     if (index > 0) {
0307       std::stable_sort(icomp.begin(), icomp.end(), CmsMTDConstruction<cms::DDFilteredView>::mtdOrderPhi);
0308     }
0309   }
0310 
0311   // Add layers to subdetectors:
0312   // first BTL (one layer only)
0313 
0314   subdet[0]->addComponent(layer[0]);
0315 
0316   // then ETL (number of layers depend on preTDR design or not)
0317 
0318   if (layer.size() == kNLayerPreTDR) {
0319     subdet[1]->addComponent(layer[1]);
0320     subdet[2]->addComponent(layer[2]);
0321   } else if (layer.size() == kNLayerTDR) {
0322     subdet[1]->addComponent(layer[1]);
0323     subdet[1]->addComponent(layer[2]);
0324     subdet[2]->addComponent(layer[3]);
0325     subdet[2]->addComponent(layer[4]);
0326   } else {
0327     throw cms::Exception("DD4hep_MTDNumbering") << "Wrong number of layers: " << layer.size();
0328   }
0329 
0330   // Add subdetectors to MTD
0331 
0332   mtd.get()->addComponents(subdet);
0333 
0334 #ifdef EDM_ML_DEBUG
0335   comp.clear();
0336   comp = mtd->deepComponents();
0337   std::stringstream after(std::stringstream::in | std::stringstream::out);
0338   for (const auto& it : comp) {
0339     after << "ORDER2 " << it->geographicalId().rawId() << " " << it->type() << " " << it->translation().z() << "\n";
0340   }
0341   edm::LogVerbatim("DD4hep_MTDNumbering") << "GeometricTimingDet order after sorting \n" << after.str();
0342 #endif
0343 
0344   return mtd;
0345 }