Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-06-24 22:52:01

0001 /*
0002  * DDHGCalWaferPartialRotated.cc
0003  *
0004  *  Created on: 09-Jan-2021
0005  */
0006 
0007 #include "DD4hep/DetFactoryHelper.h"
0008 #include "DetectorDescription/DDCMS/interface/DDPlugins.h"
0009 #include "DetectorDescription/DDCMS/interface/DDutils.h"
0010 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0011 #include "FWCore/Utilities/interface/Exception.h"
0012 #include "Geometry/HGCalCommonData/interface/HGCalTypes.h"
0013 #include "Geometry/HGCalCommonData/interface/HGCalWaferMask.h"
0014 #include "Geometry/HGCalCommonData/interface/HGCalCell.h"
0015 
0016 #include <string>
0017 #include <vector>
0018 #include <sstream>
0019 
0020 //#define EDM_ML_DEBUG
0021 
0022 static long algorithm(dd4hep::Detector& /* description */, cms::DDParsingContext& ctxt, xml_h e) {
0023   cms::DDNamespace ns(ctxt, e, true);
0024   cms::DDAlgoArguments args(ctxt, e);
0025   std::string parentName = args.parentName();
0026   const auto& material = args.value<std::string>("ModuleMaterial");
0027   const auto& thick = args.value<double>("ModuleThickness");
0028   const auto& waferSize = args.value<double>("WaferSize");
0029   const auto& waferThick = args.value<double>("WaferThickness");
0030   const auto& waferTag = args.value<std::string>("WaferTag");
0031 #ifdef EDM_ML_DEBUG
0032   const auto& waferSepar = args.value<double>("SensorSeparation");
0033   edm::LogVerbatim("HGCalGeom") << "DDHGCalWaferPartialRotated: Module " << parentName << " made of " << material
0034                                 << " T " << cms::convert2mm(thick) << " Wafer 2r " << cms::convert2mm(waferSize)
0035                                 << " Half Separation " << cms::convert2mm(waferSepar) << " T "
0036                                 << cms::convert2mm(waferThick);
0037 #endif
0038   const auto& tags = args.value<std::vector<std::string>>("Tags");
0039   const auto& partialTypes = args.value<std::vector<int>>("PartialTypes");
0040   const auto& placementIndex = args.value<std::vector<int>>("PlacementIndex");
0041   const auto& placementIndexTags = args.value<std::vector<std::string>>("PlacementIndexTags");
0042 #ifdef EDM_ML_DEBUG
0043   edm::LogVerbatim("HGCalGeom") << "DDHGCalWaferPartialRotated: " << tags.size() << " variations of wafer types";
0044   for (unsigned int k = 0; k < tags.size(); ++k) {
0045     for (unsigned int m = 0; m < placementIndex.size(); ++m) {
0046       edm::LogVerbatim("HGCalGeom") << "Type[" << k << "] " << tags[k] << " Partial " << partialTypes[k]
0047                                     << " Placement Index " << placementIndex[m] << " Tag " << placementIndexTags[m];
0048     }
0049   }
0050 #endif
0051   const auto& layerNames = args.value<std::vector<std::string>>("LayerNames");
0052   const auto& materials = args.value<std::vector<std::string>>("LayerMaterials");
0053   const auto& layerThick = args.value<std::vector<double>>("LayerThickness");
0054   const auto& layerType = args.value<std::vector<int>>("LayerTypes");
0055 #ifdef EDM_ML_DEBUG
0056   edm::LogVerbatim("HGCalGeom") << "DDHGCalWaferPartialRotated: " << layerNames.size() << " types of volumes";
0057   for (unsigned int i = 0; i < layerNames.size(); ++i)
0058     edm::LogVerbatim("HGCalGeom") << "Volume [" << i << "] " << layerNames[i] << " of thickness "
0059                                   << cms::convert2mm(layerThick[i]) << " filled with " << materials[i] << " type "
0060                                   << layerType[i];
0061 #endif
0062   const auto& layers = args.value<std::vector<int>>("Layers");
0063 #ifdef EDM_ML_DEBUG
0064   std::ostringstream st1;
0065   for (unsigned int i = 0; i < layers.size(); ++i)
0066     st1 << " [" << i << "] " << layers[i];
0067   edm::LogVerbatim("HGCalGeom") << "There are " << layers.size() << " blocks" << st1.str();
0068 #endif
0069   const auto& senseName = args.value<std::string>("SenseName");
0070   const auto& senseT = args.value<double>("SenseThick");
0071   const auto& senseType = args.value<int>("SenseType");
0072 #ifdef EDM_ML_DEBUG
0073   edm::LogVerbatim("HGCalGeom") << "DDHGCalWaferPartialRotated: NameSpace " << ns.name() << " Sensitive Layer Name "
0074                                 << senseName << " Thickness " << senseT << " Type " << senseType;
0075 #endif
0076 
0077 #ifdef EDM_ML_DEBUG
0078   edm::LogVerbatim("HGCalGeom") << "==>> Executing DDHGCalWaferPartialRotated...";
0079 #endif
0080   static constexpr double tol = 0.00001 * dd4hep::mm;
0081   static const double sqrt3 = std::sqrt(3.0);
0082   double r = 0.5 * waferSize;
0083   double R = 2.0 * r / sqrt3;
0084 
0085   // Loop over all types
0086   for (unsigned int k = 0; k < tags.size(); ++k) {
0087     for (unsigned int m = 0; m < placementIndex.size(); ++m) {
0088       // First the mother
0089       std::string mother = parentName + placementIndexTags[m] + waferTag + tags[k];
0090       std::vector<std::pair<double, double>> wxy =
0091           HGCalWaferMask::waferXY(partialTypes[k], placementIndex[m], r, R, 0.0, 0.0);
0092       std::vector<double> xM, yM;
0093       for (unsigned int i = 0; i < (wxy.size() - 1); ++i) {
0094         xM.emplace_back(wxy[i].first);
0095         yM.emplace_back(wxy[i].second);
0096       }
0097       std::vector<double> zw = {-0.5 * thick, 0.5 * thick};
0098       std::vector<double> zx(2, 0), zy(2, 0), scale(2, 1.0);
0099 
0100       dd4hep::Material matter = ns.material(material);
0101       dd4hep::Solid solid = dd4hep::ExtrudedPolygon(xM, yM, zw, zx, zy, scale);
0102       ns.addSolidNS(ns.prepend(mother), solid);
0103       dd4hep::Volume glogM = dd4hep::Volume(solid.name(), solid, matter);
0104       ns.addVolumeNS(glogM);
0105 #ifdef EDM_ML_DEBUG
0106       edm::LogVerbatim("HGCalGeom") << "DDHGCalWaferPartialRotated: " << solid.name() << " extruded polygon made of "
0107                                     << material << " z|x|y|s (0) " << cms::convert2mm(zw[0]) << ":"
0108                                     << cms::convert2mm(zx[0]) << ":" << cms::convert2mm(zy[0]) << ":" << scale[0]
0109                                     << " z|x|y|s (1) " << cms::convert2mm(zw[1]) << ":" << cms::convert2mm(zx[1]) << ":"
0110                                     << cms::convert2mm(zy[1]) << ":" << scale[1] << " partial " << partialTypes[k]
0111                                     << " placement index " << placementIndex[m] << " and " << xM.size() << " edges";
0112       for (unsigned int j = 0; j < xM.size(); ++j)
0113         edm::LogVerbatim("HGCalGeom") << "[" << j << "] " << cms::convert2mm(xM[j]) << ":" << cms::convert2mm(yM[j]);
0114 #endif
0115 
0116       // Then the layers
0117       dd4hep::Rotation3D rotation;
0118       wxy = HGCalWaferMask::waferXY(partialTypes[k], placementIndex[m], r, R, 0.0, 0.0);
0119       std::vector<double> xL, yL;
0120       for (unsigned int i = 0; i < (wxy.size() - 1); ++i) {
0121         xL.emplace_back(wxy[i].first);
0122         yL.emplace_back(wxy[i].second);
0123       }
0124       std::vector<dd4hep::Volume> glogs(materials.size());
0125       std::vector<int> copyNumber(materials.size(), 1);
0126       double zi(-0.5 * thick), thickTot(0.0);
0127       for (unsigned int l = 0; l < layers.size(); l++) {
0128         unsigned int i = layers[l];
0129 #ifdef EDM_ML_DEBUG
0130         edm::LogVerbatim("HGCalGeom") << "DDHGCalWaferPartialRotated:Layer " << l << ":" << i << " T " << layerThick[i]
0131                                       << " Copy " << copyNumber[i];
0132 #endif
0133         if (copyNumber[i] == 1) {
0134           if (layerType[i] > 0) {
0135             zw[0] = -0.5 * waferThick;
0136             zw[1] = 0.5 * waferThick;
0137           } else {
0138             zw[0] = -0.5 * layerThick[i];
0139             zw[1] = 0.5 * layerThick[i];
0140           }
0141           solid = dd4hep::ExtrudedPolygon(xL, yL, zw, zx, zy, scale);
0142           std::string lname = layerNames[i] + placementIndexTags[m] + waferTag + tags[k];
0143           ns.addSolidNS(ns.prepend(lname), solid);
0144           matter = ns.material(materials[i]);
0145           glogs[i] = dd4hep::Volume(solid.name(), solid, matter);
0146           ns.addVolumeNS(glogs[i]);
0147 #ifdef EDM_ML_DEBUG
0148           edm::LogVerbatim("HGCalGeom") << "DDHGCalWaferPartialRotated: " << solid.name()
0149                                         << " extruded polygon made of " << materials[i] << " z|x|y|s (0) "
0150                                         << cms::convert2mm(zw[0]) << ":" << cms::convert2mm(zx[0]) << ":"
0151                                         << cms::convert2mm(zy[0]) << ":" << scale[0] << " z|x|y|s (1) "
0152                                         << cms::convert2mm(zw[1]) << ": " << cms::convert2mm(zx[1]) << ":"
0153                                         << cms::convert2mm(zy[1]) << ":" << scale[1] << " partial " << partialTypes[k]
0154                                         << " placement index " << placementIndex[m] << " and " << xM.size() << " edges";
0155           for (unsigned int j = 0; j < xL.size(); ++j)
0156             edm::LogVerbatim("HGCalGeom")
0157                 << "[" << j << "] " << cms::convert2mm(xL[j]) << ":" << cms::convert2mm(yL[j]);
0158 #endif
0159         }
0160         if ((layerType[i] > 0) && (senseType >= 0)) {
0161           std::string sname = senseName + placementIndexTags[m] + waferTag + tags[k];
0162           zw[0] = -0.5 * senseT;
0163           zw[1] = 0.5 * senseT;
0164           solid = dd4hep::ExtrudedPolygon(xL, yL, zw, zx, zy, scale);
0165           ns.addSolidNS(ns.prepend(sname), solid);
0166           dd4hep::Volume glog = dd4hep::Volume(solid.name(), solid, matter);
0167           ns.addVolumeNS(glog);
0168 #ifdef EDM_ML_DEBUG
0169           edm::LogVerbatim("HGCalGeom") << "DDHGCalWaferPartialRotated: " << solid.name()
0170                                         << " extruded polygon made of " << materials[i] << " z|x|y|s (0) "
0171                                         << cms::convert2mm(zw[0]) << ":" << cms::convert2mm(zx[0]) << ":"
0172                                         << cms::convert2mm(zy[0]) << ":" << scale[0] << " z|x|y|s (1) "
0173                                         << cms::convert2mm(zw[1]) << ":" << cms::convert2mm(zx[1]) << ":"
0174                                         << cms::convert2mm(zy[1]) << ":" << scale[1] << " partial " << partialTypes[k]
0175                                         << " placement index " << placementIndex[m] << " and " << xL.size() << " edges";
0176           for (unsigned int j = 0; j < xL.size(); ++j)
0177             edm::LogVerbatim("HGCalGeom")
0178                 << "[" << j << "] " << cms::convert2mm(xL[j]) << ":" << cms::convert2mm(yL[j]);
0179 #endif
0180           auto posSense = HGCalCell::cellOrient(placementIndex[m]);
0181           double zpos = (posSense.second > 0) ? -0.5 * (waferThick - senseT) : 0.5 * (waferThick - senseT);
0182           dd4hep::Position tran(0, 0, zpos);
0183           int copy = 10 + senseType;
0184           glogs[i].placeVolume(glog, copy, tran);
0185 #ifdef EDM_ML_DEBUG
0186           edm::LogVerbatim("HGCalGeom") << "DDHGCalWaferPartialRotated: " << glog.name() << " number " << copy
0187                                         << " positioned in " << glogs[i].name() << " at (0, 0," << cms::convert2mm(zpos)
0188                                         << ") with no rotation";
0189 #endif
0190         }
0191         dd4hep::Position tran0(0, 0, (zi + 0.5 * layerThick[i]));
0192         glogM.placeVolume(glogs[i], copyNumber[i], tran0);
0193 #ifdef EDM_ML_DEBUG
0194         edm::LogVerbatim("HGCalGeom") << "DDHGCalWaferPartialRotated: " << glogs[i].name() << " number "
0195                                       << copyNumber[i] << " positioned in " << glogM.name() << " at (0,0,"
0196                                       << cms::convert2mm(zi + 0.5 * layerThick[i]) << ") with no rotation";
0197 #endif
0198         ++copyNumber[i];
0199         zi += layerThick[i];
0200         thickTot += layerThick[i];
0201       }
0202       if (std::abs(thickTot - thick) >= tol) {
0203         if (thickTot > thick) {
0204           edm::LogError("HGCalGeom") << "Thickness of the partition " << cms::convert2mm(thick) << " is smaller than "
0205                                      << cms::convert2mm(thickTot)
0206                                      << ": thickness of all its components **** ERROR ****";
0207         } else {
0208           edm::LogWarning("HGCalGeom") << "Thickness of the partition " << cms::convert2mm(thick)
0209                                        << " does not match with " << cms::convert2mm(thickTot) << " of the components";
0210         }
0211       }
0212     }
0213   }
0214 
0215   return cms::s_executed;
0216 }
0217 
0218 // first argument is the type from the xml file
0219 DECLARE_DDCMS_DETELEMENT(DDCMS_hgcal_DDHGCalWaferPartialRotated, algorithm)