Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-06-27 22:37:17

0001 /*
0002  * DDHGCalWaferF.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 
0014 #include <string>
0015 #include <vector>
0016 #include <sstream>
0017 
0018 //#define EDM_ML_DEBUG
0019 
0020 static long algorithm(dd4hep::Detector& /* description */, cms::DDParsingContext& ctxt, xml_h e) {
0021   cms::DDNamespace ns(ctxt, e, true);
0022   cms::DDAlgoArguments args(ctxt, e);
0023   std::string motherName = args.parentName();
0024   const auto& material = args.value<std::string>("ModuleMaterial");
0025   const auto& thick = args.value<double>("ModuleThickness");
0026   const auto& waferSize = args.value<double>("WaferSize");
0027   const auto& waferThick = args.value<double>("WaferThickness");
0028 #ifdef EDM_ML_DEBUG
0029   const auto& waferSepar = args.value<double>("SensorSeparation");
0030   edm::LogVerbatim("HGCalGeom") << "DDHGCalWaferF: Module " << motherName << " made of " << material << " T "
0031                                 << cms::convert2mm(thick) << " Wafer 2r " << cms::convert2mm(waferSize)
0032                                 << " Half Separation " << cms::convert2mm(waferSepar) << " T "
0033                                 << cms::convert2mm(waferThick);
0034 #endif
0035   const auto& layerNames = args.value<std::vector<std::string>>("LayerNames");
0036   const auto& materials = args.value<std::vector<std::string>>("LayerMaterials");
0037   const auto& layerThick = args.value<std::vector<double>>("LayerThickness");
0038   const auto& layerType = args.value<std::vector<int>>("LayerTypes");
0039   std::vector<int> copyNumber(materials.size(), 1);
0040 #ifdef EDM_ML_DEBUG
0041   edm::LogVerbatim("HGCalGeom") << "DDHGCalWaferF: " << layerNames.size() << " types of volumes";
0042   for (unsigned int i = 0; i < layerNames.size(); ++i)
0043     edm::LogVerbatim("HGCalGeom") << "Volume [" << i << "] " << layerNames[i] << " of thickness "
0044                                   << cms::convert2mm(layerThick[i]) << " filled with " << materials[i] << " type "
0045                                   << layerType[i];
0046 #endif
0047   const auto& layers = args.value<std::vector<int>>("Layers");
0048 #ifdef EDM_ML_DEBUG
0049   std::ostringstream st1;
0050   for (unsigned int i = 0; i < layers.size(); ++i)
0051     st1 << " [" << i << "] " << layers[i];
0052   edm::LogVerbatim("HGCalGeom") << "There are " << layers.size() << " blocks" << st1.str();
0053 #endif
0054   const auto& nCells = args.value<int>("NCells");
0055   const auto& cellType = args.value<int>("CellType");
0056   const auto& cellNames = args.value<std::vector<std::string>>("CellNames");
0057 #ifdef EDM_ML_DEBUG
0058   edm::LogVerbatim("HGCalGeom") << "DDHGCalWaferF: Cells/Wafer " << nCells << " Cell Type " << cellType << " NameSpace "
0059                                 << ns.name() << " # of cells " << cellNames.size();
0060   for (unsigned int k = 0; k < cellNames.size(); ++k)
0061     edm::LogVerbatim("HGCalGeom") << "DDHGCalWaferF: Cell[" << k << "] " << cellNames[k];
0062   int counter(0);
0063 #endif
0064 
0065   static constexpr double tol = 0.00001 * dd4hep::mm;
0066   static const double sqrt3 = std::sqrt(3.0);
0067   double rM = 0.5 * waferSize;
0068   double RM2 = rM / sqrt3;
0069   double R = waferSize / (3.0 * nCells);
0070   double r = 0.5 * R * sqrt3;
0071   double r2 = 0.5 * waferSize;
0072   double R2 = r2 / sqrt3;
0073 
0074   // Mother Module
0075   std::vector<double> xM = {rM, 0, -rM, -rM, 0, rM};
0076   std::vector<double> yM = {RM2, 2 * RM2, RM2, -RM2, -2 * RM2, -RM2};
0077   std::vector<double> zw = {-0.5 * thick, 0.5 * thick};
0078   std::vector<double> zx(2, 0), zy(2, 0), scale(2, 1.0);
0079 
0080   dd4hep::Material matter = ns.material(material);
0081   dd4hep::Solid solid = dd4hep::ExtrudedPolygon(xM, yM, zw, zx, zy, scale);
0082   ns.addSolidNS(ns.prepend(motherName), solid);
0083   dd4hep::Volume glogM = dd4hep::Volume(solid.name(), solid, matter);
0084   ns.addVolumeNS(glogM);
0085 #ifdef EDM_ML_DEBUG
0086   edm::LogVerbatim("HGCalGeom") << "DDHGCalWaferF: " << solid.name() << " extruded polygon made of " << material
0087                                 << " z|x|y|s (0) " << cms::convert2mm(zw[0]) << ":" << cms::convert2mm(zx[0]) << ":"
0088                                 << cms::convert2mm(zy[0]) << ":" << scale[0] << " z|x|y|s (1) "
0089                                 << cms::convert2mm(zw[1]) << ":" << cms::convert2mm(zx[1]) << ":"
0090                                 << cms::convert2mm(zy[1]) << ":" << scale[1] << " and " << xM.size() << " edges";
0091   for (unsigned int k = 0; k < xM.size(); ++k)
0092     edm::LogVerbatim("HGCalGeom") << "[" << k << "] " << cms::convert2mm(xM[k]) << ":" << cms::convert2mm(yM[k]);
0093 #endif
0094 
0095   // Then the layers
0096   dd4hep::Rotation3D rotation;
0097   std::vector<double> xL = {r2, 0, -r2, -r2, 0, r2};
0098   std::vector<double> yL = {R2, 2 * R2, R2, -R2, -2 * R2, -R2};
0099   std::vector<dd4hep::Volume> glogs(materials.size());
0100   double zi(-0.5 * thick), thickTot(0.0);
0101   for (unsigned int l = 0; l < layers.size(); l++) {
0102     unsigned int i = layers[l];
0103     if (copyNumber[i] == 1) {
0104       if (layerType[i] > 0) {
0105         zw[0] = -0.5 * waferThick;
0106         zw[1] = 0.5 * waferThick;
0107       } else {
0108         zw[0] = -0.5 * layerThick[i];
0109         zw[1] = 0.5 * layerThick[i];
0110       }
0111       solid = dd4hep::ExtrudedPolygon(xL, yL, zw, zx, zy, scale);
0112       ns.addSolidNS(ns.prepend(layerNames[i]), solid);
0113       matter = ns.material(materials[i]);
0114       glogs[i] = dd4hep::Volume(solid.name(), solid, matter);
0115       ns.addVolumeNS(glogs[i]);
0116 #ifdef EDM_ML_DEBUG
0117       edm::LogVerbatim("HGCalGeom") << "DDHGCalWaferF: " << solid.name() << " extruded polygon made of " << materials[i]
0118                                     << " z|x|y|s (0) " << cms::convert2mm(zw[0]) << ":" << cms::convert2mm(zx[0]) << ":"
0119                                     << cms::convert2mm(zy[0]) << ":" << scale[0] << " z|x|y|s (1) "
0120                                     << cms::convert2mm(zw[1]) << ":" << cms::convert2mm(zx[1]) << ":"
0121                                     << cms::convert2mm(zy[1]) << ":" << scale[1] << " and " << xM.size() << " edges";
0122       for (unsigned int k = 0; k < xL.size(); ++k)
0123         edm::LogVerbatim("HGCalGeom") << "[" << k << "] " << cms::convert2mm(xL[k]) << ":" << cms::convert2mm(yL[k]);
0124 #endif
0125     }
0126     dd4hep::Position tran0(0, 0, (zi + 0.5 * layerThick[i]));
0127     glogM.placeVolume(glogs[i], copyNumber[i], tran0);
0128 #ifdef EDM_ML_DEBUG
0129     edm::LogVerbatim("HGCalGeom") << "DDHGCalWaferF: " << glogs[i].name() << " number " << copyNumber[i]
0130                                   << " positioned in " << glogM.name() << " at (0,0,"
0131                                   << cms::convert2mm(zi + 0.5 * layerThick[i]) << ") with no rotation";
0132 #endif
0133     ++copyNumber[i];
0134     zi += layerThick[i];
0135     thickTot += layerThick[i];
0136 
0137     if (layerType[i] > 0) {
0138       for (int u = 0; u < 2 * nCells; ++u) {
0139         for (int v = 0; v < 2 * nCells; ++v) {
0140           if (((v - u) < nCells) && (u - v) <= nCells) {
0141 #ifdef EDM_ML_DEBUG
0142             counter++;
0143 #endif
0144             int n2 = nCells / 2;
0145             double yp = (u - 0.5 * v - n2) * 2 * r;
0146             double xp = (1.5 * (v - nCells) + 1.0) * R;
0147             int cell(0);
0148             if ((u == 0) && (v == 0))
0149               cell = 7;
0150             else if ((u == 0) && (v == nCells - 1))
0151               cell = 8;
0152             else if ((u == nCells) && (v == 2 * nCells - 1))
0153               cell = 9;
0154             else if ((u == 2 * nCells - 1) && (v == 2 * nCells - 1))
0155               cell = 10;
0156             else if ((u == 2 * nCells - 1) && (v == nCells - 1))
0157               cell = 11;
0158             else if ((u == nCells) && (v == 0))
0159               cell = 12;
0160             else if (u == 0)
0161               cell = 1;
0162             else if ((v - u) == (nCells - 1))
0163               cell = 4;
0164             else if (v == (2 * nCells - 1))
0165               cell = 2;
0166             else if (u == (2 * nCells - 1))
0167               cell = 5;
0168             else if ((u - v) == nCells)
0169               cell = 3;
0170             else if (v == 0)
0171               cell = 6;
0172             dd4hep::Position tran(xp, yp, 0);
0173             int copy = HGCalTypes::packCellTypeUV(cellType, u, v);
0174             glogs[i].placeVolume(ns.volume(cellNames[cell]), copy, dd4hep::Transform3D(rotation, tran));
0175 #ifdef EDM_ML_DEBUG
0176             edm::LogVerbatim("HGCalGeom")
0177                 << "DDHGCalWaferF: " << cellNames[cell] << " number " << copy << " positioned in " << glogs[i].name()
0178                 << " at (" << cms::convert2mm(xp) << "," << cms::convert2mm(yp) << ",0)  with no rotation";
0179 #endif
0180           }
0181         }
0182       }
0183     }
0184   }
0185 #ifdef EDM_ML_DEBUG
0186   edm::LogVerbatim("HGCalGeom") << "\nDDHGCalWaferF::Counter : " << counter << "\n===============================\n";
0187 #endif
0188   if (std::abs(thickTot - thick) >= tol) {
0189     if (thickTot > thick) {
0190       edm::LogError("HGCalGeom") << "Thickness of the partition " << thick << " is smaller than " << thickTot
0191                                  << ": thickness of all its components **** ERROR ****";
0192     } else {
0193       edm::LogWarning("HGCalGeom") << "Thickness of the partition " << thick << " does not match with " << thickTot
0194                                    << " of the components";
0195     }
0196   }
0197 
0198   return cms::s_executed;
0199 }
0200 
0201 // first argument is the type from the xml file
0202 DECLARE_DDCMS_DETELEMENT(DDCMS_hgcal_DDHGCalWaferF, algorithm)