Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-11-09 23:31:59

0001 ///////////////////////////////////////////////////////////////////////////////
0002 // File: DDHGCalHEFileAlgo.cc
0003 // Description: Geometry factory class for HGCal (Mix) adopted for DD4hep
0004 ///////////////////////////////////////////////////////////////////////////////
0005 
0006 #include <cmath>
0007 #include <memory>
0008 #include <string>
0009 #include <unordered_set>
0010 #include <vector>
0011 
0012 #include "Geometry/HGCalCommonData/interface/HGCalGeomTools.h"
0013 #include "Geometry/HGCalCommonData/interface/HGCalParameters.h"
0014 #include "Geometry/HGCalCommonData/interface/HGCalProperty.h"
0015 #include "Geometry/HGCalCommonData/interface/HGCalTypes.h"
0016 #include "Geometry/HGCalCommonData/interface/HGCalWaferIndex.h"
0017 #include "Geometry/HGCalCommonData/interface/HGCalWaferType.h"
0018 #include "DD4hep/DetFactoryHelper.h"
0019 #include "DataFormats/Math/interface/angle_units.h"
0020 #include "DetectorDescription/DDCMS/interface/DDPlugins.h"
0021 #include "DetectorDescription/DDCMS/interface/DDutils.h"
0022 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0023 
0024 //#define EDM_ML_DEBUG
0025 using namespace angle_units::operators;
0026 
0027 struct HGCalHEFileAlgo {
0028   HGCalHEFileAlgo() { throw cms::Exception("HGCalGeom") << "Wrong initialization to HGCalHEFileAlgo"; }
0029   HGCalHEFileAlgo(cms::DDParsingContext& ctxt, xml_h e) {
0030     cms::DDNamespace ns(ctxt, e, true);
0031     cms::DDAlgoArguments args(ctxt, e);
0032 
0033 #ifdef EDM_ML_DEBUG
0034     edm::LogVerbatim("HGCalGeom") << "DDHGCalHEFileAlgo: Creating an instance";
0035 #endif
0036 
0037     static constexpr double tol1 = 0.01 * dd4hep::mm;
0038     dd4hep::Volume mother = ns.volume(args.parentName());
0039 
0040     waferNames_ = args.value<std::vector<std::string>>("WaferNames");
0041 #ifdef EDM_ML_DEBUG
0042     edm::LogVerbatim("HGCalGeom") << "DDHGCalHEFileAlgo: " << waferNames_.size() << " wafers";
0043     for (unsigned int i = 0; i < waferNames_.size(); ++i)
0044       edm::LogVerbatim("HGCalGeom") << "Wafer[" << i << "] " << waferNames_[i];
0045 #endif
0046     materials_ = args.value<std::vector<std::string>>("MaterialNames");
0047     volumeNames_ = args.value<std::vector<std::string>>("VolumeNames");
0048     thickness_ = args.value<std::vector<double>>("Thickness");
0049     copyNumber_.resize(materials_.size(), 1);
0050 #ifdef EDM_ML_DEBUG
0051     edm::LogVerbatim("HGCalGeom") << "DDHGCalHEFileAlgo: " << materials_.size() << " types of volumes";
0052     for (unsigned int i = 0; i < volumeNames_.size(); ++i)
0053       edm::LogVerbatim("HGCalGeom") << "Volume [" << i << "] " << volumeNames_[i] << " of thickness "
0054                                     << cms::convert2mm(thickness_[i]) << " filled with " << materials_[i]
0055                                     << " first copy number " << copyNumber_[i];
0056 #endif
0057     layerNumbers_ = args.value<std::vector<int>>("Layers");
0058     layerThick_ = args.value<std::vector<double>>("LayerThick");
0059     rMixLayer_ = args.value<std::vector<double>>("LayerRmix");
0060 #ifdef EDM_ML_DEBUG
0061     edm::LogVerbatim("HGCalGeom") << "There are " << layerNumbers_.size() << " blocks";
0062     for (unsigned int i = 0; i < layerNumbers_.size(); ++i)
0063       edm::LogVerbatim("HGCalGeom") << "Block [" << i << "] of thickness " << cms::convert2mm(layerThick_[i])
0064                                     << " Rmid " << cms::convert2mm(rMixLayer_[i]) << " with " << layerNumbers_[i]
0065                                     << " layers";
0066 #endif
0067     layerType_ = args.value<std::vector<int>>("LayerType");
0068     layerSense_ = args.value<std::vector<int>>("LayerSense");
0069     firstLayer_ = args.value<int>("FirstLayer");
0070     absorbMode_ = args.value<int>("AbsorberMode");
0071     sensitiveMode_ = args.value<int>("SensitiveMode");
0072 #ifdef EDM_ML_DEBUG
0073     edm::LogVerbatim("HGCalGeom") << "First Layer " << firstLayer_ << " and "
0074                                   << "Absober:Sensitive mode " << absorbMode_ << ":" << sensitiveMode_;
0075 #endif
0076     layerCenter_ = args.value<std::vector<int>>("LayerCenter");
0077 #ifdef EDM_ML_DEBUG
0078     for (unsigned int i = 0; i < layerCenter_.size(); ++i)
0079       edm::LogVerbatim("HGCalGeom") << "LayerCenter [" << i << "] " << layerCenter_[i];
0080 #endif
0081     if (firstLayer_ > 0) {
0082       for (unsigned int i = 0; i < layerType_.size(); ++i) {
0083         if (layerSense_[i] > 0) {
0084           int ii = layerType_[i];
0085           copyNumber_[ii] = firstLayer_;
0086 #ifdef EDM_ML_DEBUG
0087           edm::LogVerbatim("HGCalGeom") << "First copy number for layer type " << i << ":" << ii << " with "
0088                                         << materials_[ii] << " changed to " << copyNumber_[ii];
0089 #endif
0090           break;
0091         }
0092       }
0093     } else {
0094       firstLayer_ = 1;
0095     }
0096 #ifdef EDM_ML_DEBUG
0097     edm::LogVerbatim("HGCalGeom") << "There are " << layerType_.size() << " layers";
0098     for (unsigned int i = 0; i < layerType_.size(); ++i)
0099       edm::LogVerbatim("HGCalGeom") << "Layer [" << i << "] with material type " << layerType_[i] << " sensitive class "
0100                                     << layerSense_[i];
0101 #endif
0102     materialsTop_ = args.value<std::vector<std::string>>("TopMaterialNames");
0103     namesTop_ = args.value<std::vector<std::string>>("TopVolumeNames");
0104     layerThickTop_ = args.value<std::vector<double>>("TopLayerThickness");
0105     layerTypeTop_ = args.value<std::vector<int>>("TopLayerType");
0106     copyNumberTop_.resize(materialsTop_.size(), 1);
0107 #ifdef EDM_ML_DEBUG
0108     edm::LogVerbatim("HGCalGeom") << "DDHGCalHEFileAlgo: " << materialsTop_.size()
0109                                   << " types of volumes in the top part";
0110     for (unsigned int i = 0; i < materialsTop_.size(); ++i)
0111       edm::LogVerbatim("HGCalGeom") << "Volume [" << i << "] " << namesTop_[i] << " of thickness "
0112                                     << cms::convert2mm(layerThickTop_[i]) << " filled with " << materialsTop_[i]
0113                                     << " first copy number " << copyNumberTop_[i];
0114     edm::LogVerbatim("HGCalGeom") << "There are " << layerTypeTop_.size() << " layers in the top part";
0115     for (unsigned int i = 0; i < layerTypeTop_.size(); ++i)
0116       edm::LogVerbatim("HGCalGeom") << "Layer [" << i << "] with material type " << layerTypeTop_[i];
0117 #endif
0118     materialsBot_ = args.value<std::vector<std::string>>("BottomMaterialNames");
0119     namesBot_ = args.value<std::vector<std::string>>("BottomVolumeNames");
0120     layerTypeBot_ = args.value<std::vector<int>>("BottomLayerType");
0121     layerSenseBot_ = args.value<std::vector<int>>("BottomLayerSense");
0122     layerThickBot_ = args.value<std::vector<double>>("BottomLayerThickness");
0123     copyNumberBot_.resize(materialsBot_.size(), 1);
0124 #ifdef EDM_ML_DEBUG
0125     edm::LogVerbatim("HGCalGeom") << "DDHGCalHEFileAlgo: " << materialsBot_.size()
0126                                   << " types of volumes in the bottom part";
0127     for (unsigned int i = 0; i < materialsBot_.size(); ++i)
0128       edm::LogVerbatim("HGCalGeom") << "Volume [" << i << "] " << namesBot_[i] << " of thickness "
0129                                     << cms::convert2mm(layerThickBot_[i]) << " filled with " << materialsBot_[i]
0130                                     << " first copy number " << copyNumberBot_[i];
0131     edm::LogVerbatim("HGCalGeom") << "There are " << layerTypeBot_.size() << " layers in the bottom part";
0132     for (unsigned int i = 0; i < layerTypeBot_.size(); ++i)
0133       edm::LogVerbatim("HGCalGeom") << "Layer [" << i << "] with material type " << layerTypeBot_[i]
0134                                     << " sensitive class " << layerSenseBot_[i];
0135 #endif
0136     zMinBlock_ = args.value<double>("zMinBlock");
0137     waferSize_ = args.value<double>("waferSize");
0138     waferSepar_ = args.value<double>("SensorSeparation");
0139     sectors_ = args.value<int>("Sectors");
0140     alpha_ = (1._pi) / sectors_;
0141     cosAlpha_ = cos(alpha_);
0142 #ifdef EDM_ML_DEBUG
0143     edm::LogVerbatim("HGCalGeom") << "DDHGCalHEFileAlgo: zStart " << cms::convert2mm(zMinBlock_) << " wafer width "
0144                                   << cms::convert2mm(waferSize_) << " separations " << cms::convert2mm(waferSepar_)
0145                                   << " sectors " << sectors_ << ":" << convertRadToDeg(alpha_) << ":" << cosAlpha_;
0146 #endif
0147     waferIndex_ = args.value<std::vector<int>>("WaferIndex");
0148     waferProperty_ = args.value<std::vector<int>>("WaferProperties");
0149 #ifdef EDM_ML_DEBUG
0150     edm::LogVerbatim("HGCalGeom") << "waferProperties with " << waferIndex_.size() << " entries";
0151     for (unsigned int k = 0; k < waferIndex_.size(); ++k)
0152       edm::LogVerbatim("HGCalGeom") << "[" << k << "] " << waferIndex_[k] << " ("
0153                                     << HGCalWaferIndex::waferLayer(waferIndex_[k]) << ", "
0154                                     << HGCalWaferIndex::waferU(waferIndex_[k]) << ", "
0155                                     << HGCalWaferIndex::waferV(waferIndex_[k]) << ") : ("
0156                                     << HGCalProperty::waferThick(waferProperty_[k]) << ":"
0157                                     << HGCalProperty::waferPartial(waferProperty_[k]) << ":"
0158                                     << HGCalProperty::waferOrient(waferProperty_[k]) << ")";
0159 #endif
0160     slopeB_ = args.value<std::vector<double>>("SlopeBottom");
0161     zFrontB_ = args.value<std::vector<double>>("ZFrontBottom");
0162     rMinFront_ = args.value<std::vector<double>>("RMinFront");
0163     slopeT_ = args.value<std::vector<double>>("SlopeTop");
0164     zFrontT_ = args.value<std::vector<double>>("ZFrontTop");
0165     rMaxFront_ = args.value<std::vector<double>>("RMaxFront");
0166 #ifdef EDM_ML_DEBUG
0167     for (unsigned int i = 0; i < slopeB_.size(); ++i)
0168       edm::LogVerbatim("HGCalGeom") << "Block [" << i << "] Zmin " << cms::convert2mm(zFrontB_[i]) << " Rmin "
0169                                     << cms::convert2mm(rMinFront_[i]) << " Slope " << slopeB_[i];
0170     for (unsigned int i = 0; i < slopeT_.size(); ++i)
0171       edm::LogVerbatim("HGCalGeom") << "Block [" << i << "] Zmin " << cms::convert2mm(zFrontT_[i]) << " Rmax "
0172                                     << cms::convert2mm(rMaxFront_[i]) << " Slope " << slopeT_[i];
0173     edm::LogVerbatim("HGCalGeom") << "DDHGCalHEFileAlgo: NameSpace " << ns.name();
0174 
0175     edm::LogVerbatim("HGCalGeom") << "==>> Constructing DDHGCalHEFileAlgo...";
0176     copies_.clear();
0177 #endif
0178 
0179     double zi(zMinBlock_);
0180     int laymin(0);
0181     for (unsigned int i = 0; i < layerNumbers_.size(); i++) {
0182       double zo = zi + layerThick_[i];
0183       double routF = HGCalGeomTools::radius(zi, zFrontT_, rMaxFront_, slopeT_);
0184       int laymax = laymin + layerNumbers_[i];
0185       double zz = zi;
0186       double thickTot(0);
0187       for (int ly = laymin; ly < laymax; ++ly) {
0188         int ii = layerType_[ly];
0189         int copy = copyNumber_[ii];
0190         double hthick = 0.5 * thickness_[ii];
0191         double rinB = HGCalGeomTools::radius(zo, zFrontB_, rMinFront_, slopeB_);
0192         zz += hthick;
0193         thickTot += thickness_[ii];
0194 
0195         std::string name = volumeNames_[ii] + std::to_string(copy);
0196 
0197 #ifdef EDM_ML_DEBUG
0198         edm::LogVerbatim("HGCalGeom") << "DDHGCalHEFileAlgo: Layer " << ly << ":" << ii << " Front "
0199                                       << cms::convert2mm(zi) << ", " << cms::convert2mm(routF) << " Back "
0200                                       << cms::convert2mm(zo) << ", " << cms::convert2mm(rinB)
0201                                       << " superlayer thickness " << cms::convert2mm(layerThick_[i]);
0202 #endif
0203 
0204         dd4hep::Material matter = ns.material(materials_[ii]);
0205         dd4hep::Volume glog;
0206 
0207         if (layerSense_[ly] < 1) {
0208           std::vector<double> pgonZ, pgonRin, pgonRout;
0209           double rmax =
0210               (std::min(routF, HGCalGeomTools::radius(zz + hthick, zFrontT_, rMaxFront_, slopeT_)) * cosAlpha_) - tol1;
0211           HGCalGeomTools::radius(zz - hthick,
0212                                  zz + hthick,
0213                                  zFrontB_,
0214                                  rMinFront_,
0215                                  slopeB_,
0216                                  zFrontT_,
0217                                  rMaxFront_,
0218                                  slopeT_,
0219                                  -layerSense_[ly],
0220                                  pgonZ,
0221                                  pgonRin,
0222                                  pgonRout);
0223           for (unsigned int isec = 0; isec < pgonZ.size(); ++isec) {
0224             pgonZ[isec] -= zz;
0225             if (layerSense_[ly] == 0 || absorbMode_ == 0)
0226               pgonRout[isec] = rmax;
0227             else
0228               pgonRout[isec] = pgonRout[isec] * cosAlpha_ - tol1;
0229           }
0230 
0231           dd4hep::Solid solid = dd4hep::Polyhedra(sectors_, -alpha_, 2._pi, pgonZ, pgonRin, pgonRout);
0232           ns.addSolidNS(ns.prepend(name), solid);
0233           glog = dd4hep::Volume(solid.name(), solid, matter);
0234           ns.addVolumeNS(glog);
0235 #ifdef EDM_ML_DEBUG
0236           edm::LogVerbatim("HGCalGeom") << "DDHGCalHEFileAlgo: " << solid.name() << " polyhedra of " << sectors_
0237                                         << " sectors covering " << convertRadToDeg(-alpha_) << ":"
0238                                         << convertRadToDeg(-alpha_ + 2._pi) << " with " << pgonZ.size() << " sections";
0239           for (unsigned int k = 0; k < pgonZ.size(); ++k)
0240             edm::LogVerbatim("HGCalGeom") << "[" << k << "] z " << cms::convert2mm(pgonZ[k]) << " R "
0241                                           << cms::convert2mm(pgonRin[k]) << ":" << cms::convert2mm(pgonRout[k]);
0242 #endif
0243         } else {
0244           double rins =
0245               (sensitiveMode_ < 1) ? rinB : HGCalGeomTools::radius(zz + hthick, zFrontB_, rMinFront_, slopeB_);
0246           double routs =
0247               (sensitiveMode_ < 1) ? routF : HGCalGeomTools::radius(zz - hthick, zFrontT_, rMaxFront_, slopeT_);
0248           dd4hep::Solid solid = dd4hep::Tube(rins, routs, hthick, 0.0, 2._pi);
0249           ns.addSolidNS(ns.prepend(name), solid);
0250           glog = dd4hep::Volume(solid.name(), solid, matter);
0251           ns.addVolumeNS(glog);
0252 
0253 #ifdef EDM_ML_DEBUG
0254           edm::LogVerbatim("HGCalGeom") << "DDHGCalHEFileAlgo: " << solid.name() << " Tubs made of " << matter.name()
0255                                         << " of dimensions " << cms::convert2mm(rinB) << ":" << cms::convert2mm(rins)
0256                                         << ", " << cms::convert2mm(routF) << ":" << cms::convert2mm(routs) << ", "
0257                                         << cms::convert2mm(hthick) << ", 0.0, 360.0 and positioned in: " << glog.name()
0258                                         << " number " << copy;
0259 #endif
0260           positionMix(ctxt, e, glog, name, copy, thickness_[ii], matter, rins, rMixLayer_[i], routs, zz);
0261         }
0262 
0263         dd4hep::Position r1(0, 0, zz);
0264         mother.placeVolume(glog, copy, r1);
0265         ++copyNumber_[ii];
0266 #ifdef EDM_ML_DEBUG
0267         edm::LogVerbatim("HGCalGeom") << "DDHGCalHEFileAlgo: " << glog.name() << " number " << copy << " positioned in "
0268                                       << mother.name() << " at (0,0," << cms::convert2mm(zz) << ") with no rotation";
0269 #endif
0270         zz += hthick;
0271       }  // End of loop over layers in a block
0272       zi = zo;
0273       laymin = laymax;
0274       if (std::abs(thickTot - layerThick_[i]) > tol2_) {
0275         if (thickTot > layerThick_[i]) {
0276           edm::LogError("HGCalGeom") << "Thickness of the partition " << cms::convert2mm(layerThick_[i])
0277                                      << " is smaller than " << cms::convert2mm(thickTot)
0278                                      << ": thickness of all its components **** ERROR ****";
0279         } else {
0280           edm::LogWarning("HGCalGeom") << "Thickness of the partition " << cms::convert2mm(layerThick_[i])
0281                                        << " does not match with " << cms::convert2mm(thickTot) << " of the components";
0282         }
0283       }
0284     }  // End of loop over blocks
0285 
0286 #ifdef EDM_ML_DEBUG
0287     edm::LogVerbatim("HGCalGeom") << "DDHGCalHEFileAlgo: " << copies_.size() << " different wafer copy numbers";
0288     int k(0);
0289     for (std::unordered_set<int>::const_iterator itr = copies_.begin(); itr != copies_.end(); ++itr, ++k) {
0290       edm::LogVerbatim("HGCalGeom") << "Copy [" << k << "] : " << (*itr);
0291     }
0292     copies_.clear();
0293     edm::LogVerbatim("HGCalGeom") << "<<== End of DDHGCalHEFileAlgo construction...";
0294 #endif
0295   }
0296 
0297   void positionMix(cms::DDParsingContext& ctxt,
0298                    xml_h e,
0299                    const dd4hep::Volume& glog,
0300                    const std::string& nameM,
0301                    int copyM,
0302                    double thick,
0303                    const dd4hep::Material& matter,
0304                    double rin,
0305                    double rmid,
0306                    double rout,
0307                    double zz) {
0308     cms::DDNamespace ns(ctxt, e, true);
0309 
0310     dd4hep::Volume glog1;
0311     for (unsigned int ly = 0; ly < layerTypeTop_.size(); ++ly) {
0312       int ii = layerTypeTop_[ly];
0313       copyNumberTop_[ii] = copyM;
0314     }
0315     for (unsigned int ly = 0; ly < layerTypeBot_.size(); ++ly) {
0316       int ii = layerTypeBot_[ly];
0317       copyNumberBot_[ii] = copyM;
0318     }
0319     double hthick = 0.5 * thick;
0320     // Make the top part first
0321     std::string name = nameM + "Top";
0322 
0323     dd4hep::Solid solid = dd4hep::Tube(rmid, rout, hthick, 0.0, 2._pi);
0324     ns.addSolidNS(ns.prepend(name), solid);
0325     glog1 = dd4hep::Volume(solid.name(), solid, matter);
0326     ns.addVolumeNS(glog1);
0327 #ifdef EDM_ML_DEBUG
0328     edm::LogVerbatim("HGCalGeom") << "DDHGCalHEFileAlgo: " << solid.name() << " Tubs made of " << matter.name()
0329                                   << " of dimensions " << cms::convert2mm(rmid) << ", " << cms::convert2mm(rout) << ", "
0330                                   << cms::convert2mm(hthick) << ", 0.0, 360.0";
0331 #endif
0332     glog.placeVolume(glog1, 1);
0333 
0334 #ifdef EDM_ML_DEBUG
0335     edm::LogVerbatim("HGCalGeom") << "DDHGCalHEFileAlgo: " << glog1.name() << " number 1 positioned in " << glog.name()
0336                                   << " at (0,0,0) with no rotation";
0337 #endif
0338     double thickTot(0), zpos(-hthick);
0339     for (unsigned int ly = 0; ly < layerTypeTop_.size(); ++ly) {
0340       int ii = layerTypeTop_[ly];
0341       int copy = copyNumberTop_[ii];
0342       double hthickl = 0.5 * layerThickTop_[ii];
0343       thickTot += layerThickTop_[ii];
0344       name = namesTop_[ii] + std::to_string(copy);
0345 #ifdef EDM_ML_DEBUG
0346       edm::LogVerbatim("HGCalGeom") << "DDHGCalHEFileAlgo: Layer " << ly << ":" << ii << " R " << cms::convert2mm(rmid)
0347                                     << ":" << cms::convert2mm(rout) << " Thick " << cms::convert2mm(layerThickTop_[ii]);
0348 #endif
0349 
0350       dd4hep::Material matter1 = ns.material(materialsTop_[ii]);
0351       solid = dd4hep::Tube(rmid, rout, hthickl, 0.0, 2._pi);
0352       ns.addSolidNS(ns.prepend(name), solid);
0353       dd4hep::Volume glog2 = dd4hep::Volume(solid.name(), solid, matter1);
0354       ns.addVolumeNS(glog2);
0355 
0356 #ifdef EDM_ML_DEBUG
0357       double eta1 = -log(tan(0.5 * atan(rmid / zz)));
0358       double eta2 = -log(tan(0.5 * atan(rout / zz)));
0359       edm::LogVerbatim("HGCalGeom") << name << " z|rin|rout " << cms::convert2mm(zz) << ":" << cms::convert2mm(rmid)
0360                                     << ":" << cms::convert2mm(rout) << " eta " << eta1 << ":" << eta2;
0361       edm::LogVerbatim("HGCalGeom") << "DDHGCalHEFileAlgo: " << solid.name() << " Tubs made of " << matter1.name()
0362                                     << " of dimensions " << cms::convert2mm(rmid) << ", " << cms::convert2mm(rout)
0363                                     << ", " << cms::convert2mm(hthickl) << ", 0.0, 360.0";
0364 #endif
0365       zpos += hthickl;
0366 
0367       dd4hep::Position r1(0, 0, zpos);
0368       glog1.placeVolume(glog2, copy, r1);
0369 
0370 #ifdef EDM_ML_DEBUG
0371       edm::LogVerbatim("HGCalGeom") << "DDHGCalHEFileAlgo: Position " << glog2.name() << " number " << copy << " in "
0372                                     << glog1.name() << " at (0,0," << cms::convert2mm(zpos) << ") with no rotation";
0373 #endif
0374       ++copyNumberTop_[ii];
0375       zpos += hthickl;
0376     }
0377     if (std::abs(thickTot - thick) > tol2_) {
0378       if (thickTot > thick) {
0379         edm::LogError("HGCalGeom") << "Thickness of the partition " << cms::convert2mm(thick) << " is smaller than "
0380                                    << cms::convert2mm(thickTot)
0381                                    << ": thickness of all its components in the top part **** ERROR ****";
0382       } else {
0383         edm::LogWarning("HGCalGeom") << "Thickness of the partition " << cms::convert2mm(thick)
0384                                      << " does not match with " << cms::convert2mm(thickTot)
0385                                      << " of the components in top part";
0386       }
0387     }
0388 
0389     // Make the bottom part next
0390     name = nameM + "Bottom";
0391 
0392     solid = dd4hep::Tube(rin, rmid, hthick, 0.0, 2._pi);
0393     ns.addSolidNS(ns.prepend(name), solid);
0394     glog1 = dd4hep::Volume(solid.name(), solid, matter);
0395     ns.addVolumeNS(glog1);
0396 
0397 #ifdef EDM_ML_DEBUG
0398     edm::LogVerbatim("HGCalGeom") << "DDHGCalHEFileAlgo: " << solid.name() << " Tubs made of " << matter.name()
0399                                   << " of dimensions " << cms::convert2mm(rin) << ", " << cms::convert2mm(rmid) << ", "
0400                                   << cms::convert2mm(hthick) << ", 0.0, 360.0";
0401 #endif
0402 
0403     glog.placeVolume(glog1, 1);
0404 #ifdef EDM_ML_DEBUG
0405     edm::LogVerbatim("HGCalGeom") << "DDHGCalHEFileAlgo: " << glog1.name() << " number 1 positioned in " << glog.name()
0406                                   << " at (0,0,0) with no rotation";
0407 #endif
0408     thickTot = 0;
0409     zpos = -hthick;
0410     for (unsigned int ly = 0; ly < layerTypeBot_.size(); ++ly) {
0411       int ii = layerTypeBot_[ly];
0412       int copy = copyNumberBot_[ii];
0413       double hthickl = 0.5 * layerThickBot_[ii];
0414       thickTot += layerThickBot_[ii];
0415       name = namesBot_[ii] + std::to_string(copy);
0416 #ifdef EDM_ML_DEBUG
0417       edm::LogVerbatim("HGCalGeom") << "DDHGCalHEFileAlgo: Layer " << ly << ":" << ii << " R " << cms::convert2mm(rin)
0418                                     << ":" << cms::convert2mm(rmid) << " Thick " << cms::convert2mm(layerThickBot_[ii]);
0419 #endif
0420 
0421       dd4hep::Material matter1 = ns.material(materialsBot_[ii]);
0422       solid = dd4hep::Tube(rin, rmid, hthickl, 0.0, 2._pi);
0423       ns.addSolidNS(ns.prepend(name), solid);
0424       dd4hep::Volume glog2 = dd4hep::Volume(solid.name(), solid, matter1);
0425       ns.addVolumeNS(glog2);
0426 
0427 #ifdef EDM_ML_DEBUG
0428       double eta1 = -log(tan(0.5 * atan(rin / zz)));
0429       double eta2 = -log(tan(0.5 * atan(rmid / zz)));
0430       edm::LogVerbatim("HGCalGeom") << name << " z|rin|rout " << cms::convert2mm(zz) << ":" << cms::convert2mm(rin)
0431                                     << ":" << cms::convert2mm(rmid) << " eta " << eta1 << ":" << eta2;
0432       edm::LogVerbatim("HGCalGeom") << "DDHGCalHEFileAlgo: " << solid.name() << " Tubs made of " << matter1.name()
0433                                     << " of dimensions " << cms::convert2mm(rin) << ", " << cms::convert2mm(rmid)
0434                                     << ", " << cms::convert2mm(hthickl) << ", 0.0, 360.0";
0435 #endif
0436       zpos += hthickl;
0437 
0438       dd4hep::Position r1(0, 0, zpos);
0439       glog1.placeVolume(glog2, copy, r1);
0440 #ifdef EDM_ML_DEBUG
0441       edm::LogVerbatim("HGCalGeom") << "DDHGCalHEFileAlgo: Position " << glog2.name() << " number " << copy << " in "
0442                                     << glog1.name() << " at (0,0," << cms::convert2mm(zpos) << ") with no rotation";
0443 #endif
0444       if (layerSenseBot_[ly] != 0) {
0445 #ifdef EDM_ML_DEBUG
0446         edm::LogVerbatim("HGCalGeom") << "DDHGCalHEFileAlgo: z " << cms::convert2mm((zz + zpos)) << " Center " << copy
0447                                       << ":" << (copy - firstLayer_) << ":" << layerCenter_[copy - firstLayer_];
0448 #endif
0449         positionSensitive(ctxt, e, glog2, rin, rmid, zz + zpos, layerSenseBot_[ly], (copy - firstLayer_));
0450       }
0451       zpos += hthickl;
0452       ++copyNumberBot_[ii];
0453     }
0454     if (std::abs(thickTot - thick) > tol2_) {
0455       if (thickTot > thick) {
0456         edm::LogError("HGCalGeom") << "Thickness of the partition " << cms::convert2mm(thick) << " is smaller than "
0457                                    << cms::convert2mm(thickTot)
0458                                    << ": thickness of all its components in the top part **** ERROR ****";
0459       } else {
0460         edm::LogWarning("HGCalGeom") << "Thickness of the partition " << cms::convert2mm(thick)
0461                                      << " does not match with " << cms::convert2mm(thickTot)
0462                                      << " of the components in top part";
0463       }
0464     }
0465   }
0466 
0467   void positionSensitive(cms::DDParsingContext& ctxt,
0468                          xml_h e,
0469                          const dd4hep::Volume& glog,
0470                          double rin,
0471                          double rout,
0472                          double zpos,
0473                          int layertype,
0474                          int layer) {
0475     cms::DDNamespace ns(ctxt, e, true);
0476     int layercenter = layerCenter_[layer];
0477     static const double sqrt3 = std::sqrt(3.0);
0478     double r = 0.5 * (waferSize_ + waferSepar_);
0479     double R = 2.0 * r / sqrt3;
0480     double dy = 0.75 * R;
0481     int N = (int)(0.5 * rout / r) + 2;
0482     const auto& xyoff = geomTools_.shiftXY(layercenter, (waferSize_ + waferSepar_));
0483 #ifdef EDM_ML_DEBUG
0484     int ium(0), ivm(0), iumAll(0), ivmAll(0), kount(0), ntot(0), nin(0);
0485     std::vector<int> ntype(6, 0);
0486     edm::LogVerbatim("HGCalGeom") << "DDHGCalHEFileAlgo: " << glog.name() << " rin:rout " << cms::convert2mm(rin) << ":"
0487                                   << cms::convert2mm(rout) << " zpos " << cms::convert2mm(zpos) << " N " << N
0488                                   << " for maximum u, v Offset; Shift " << cms::convert2mm(xyoff.first) << ":"
0489                                   << cms::convert2mm(xyoff.second) << " WaferSize "
0490                                   << cms::convert2mm((waferSize_ + waferSepar_));
0491 #endif
0492     for (int u = -N; u <= N; ++u) {
0493       for (int v = -N; v <= N; ++v) {
0494 #ifdef EDM_ML_DEBUG
0495         int iu = std::abs(u);
0496         int iv = std::abs(v);
0497 #endif
0498         int nr = 2 * v;
0499         int nc = -2 * u + v;
0500         double xpos = xyoff.first + nc * r;
0501         double ypos = xyoff.second + nr * dy;
0502         const auto& corner = HGCalGeomTools::waferCorner(xpos, ypos, r, R, rin, rout, false);
0503 #ifdef EDM_ML_DEBUG
0504         ++ntot;
0505 #endif
0506         int indx = HGCalWaferIndex::waferIndex((layer + firstLayer_), u, v, false);
0507         int type = HGCalWaferType::getType(indx, waferIndex_, waferProperty_);
0508         if (corner.first > 0 && type >= 0) {
0509           int copy = HGCalTypes::packTypeUV(type, u, v);
0510           if (layertype > 1)
0511             type += 3;
0512 #ifdef EDM_ML_DEBUG
0513           edm::LogVerbatim("HGCalGeom") << " DDHGCalHEFileAlgo: " << waferNames_[type] << " number " << copy << " type "
0514                                         << type << " layer:u:v:indx " << (layer + firstLayer_) << ":" << u << ":" << v
0515                                         << ":" << indx;
0516           if (iu > ium)
0517             ium = iu;
0518           if (iv > ivm)
0519             ivm = iv;
0520           kount++;
0521           if (copies_.count(copy) == 0)
0522             copies_.insert(copy);
0523 #endif
0524           if (corner.first == (int)(HGCalParameters::k_CornerSize)) {
0525 #ifdef EDM_ML_DEBUG
0526             if (iu > iumAll)
0527               iumAll = iu;
0528             if (iv > ivmAll)
0529               ivmAll = iv;
0530             ++nin;
0531 #endif
0532             dd4hep::Position tran(xpos, ypos, 0.0);
0533             glog.placeVolume(ns.volume(waferNames_[type]), copy, tran);
0534 #ifdef EDM_ML_DEBUG
0535             ++ntype[type];
0536             edm::LogVerbatim("HGCalGeom")
0537                 << "DDHGCalHEFileAlgo: " << waferNames_[type] << " number " << copy << " type " << layertype << ":"
0538                 << type << " positioned in " << glog.name() << " at (" << cms::convert2mm(xpos) << ", "
0539                 << cms::convert2mm(ypos) << ", 0) with no rotation";
0540 #endif
0541           }
0542         }
0543       }
0544     }
0545 #ifdef EDM_ML_DEBUG
0546     edm::LogVerbatim("HGCalGeom") << "DDHGCalHEFileAlgo: Maximum # of u " << ium << ":" << iumAll << " # of v " << ivm
0547                                   << ":" << ivmAll << " and " << nin << ":" << kount << ":" << ntot << " wafers ("
0548                                   << ntype[0] << ":" << ntype[1] << ":" << ntype[2] << ":" << ntype[3] << ":"
0549                                   << ntype[4] << ":" << ntype[5] << ") for " << glog.name() << " R "
0550                                   << cms::convert2mm(rin) << ":" << cms::convert2mm(rout);
0551 #endif
0552   }
0553 
0554   //Required data members to cache the values from XML file
0555   HGCalGeomTools geomTools_;
0556   static constexpr double tol2_ = 0.00001 * dd4hep::mm;
0557 
0558   std::vector<std::string> waferNames_;    // Wafer names
0559   std::vector<std::string> materials_;     // Materials
0560   std::vector<std::string> volumeNames_;   // Names
0561   std::vector<double> thickness_;          // Thickness of the material
0562   std::vector<int> copyNumber_;            // Initial copy numbers
0563   std::vector<int> layerNumbers_;          // Number of layers in a section
0564   std::vector<double> layerThick_;         // Thickness of each section
0565   std::vector<double> rMixLayer_;          // Partition between Si/Sci part
0566   std::vector<int> layerType_;             // Type of the layer
0567   std::vector<int> layerSense_;            // Content of a layer (sensitive?)
0568   int firstLayer_;                         // Copy # of the first sensitive layer
0569   int absorbMode_;                         // Absorber mode
0570   int sensitiveMode_;                      // Sensitive mode
0571   std::vector<std::string> materialsTop_;  // Materials of top layers
0572   std::vector<std::string> namesTop_;      // Names of top layers
0573   std::vector<double> layerThickTop_;      // Thickness of the top sections
0574   std::vector<int> layerTypeTop_;          // Type of the Top layer
0575   std::vector<int> copyNumberTop_;         // Initial copy numbers (top section)
0576   std::vector<std::string> materialsBot_;  // Materials of bottom layers
0577   std::vector<std::string> namesBot_;      // Names of bottom layers
0578   std::vector<double> layerThickBot_;      // Thickness of the bottom sections
0579   std::vector<int> layerTypeBot_;          // Type of the bottom layers
0580   std::vector<int> copyNumberBot_;         // Initial copy numbers (bot section)
0581   std::vector<int> layerSenseBot_;         // Content of bottom layer (sensitive?)
0582   std::vector<int> layerCenter_;           // Centering of the wafers
0583 
0584   double zMinBlock_;                // Starting z-value of the block
0585   double waferSize_;                // Width of the wafer
0586   double waferSepar_;               // Sensor separation
0587   int sectors_;                     // Sectors
0588   std::vector<int> waferIndex_;     // Wafer index for the types
0589   std::vector<int> waferProperty_;  // Wafer property
0590   std::vector<double> slopeB_;      // Slope at the lower R
0591   std::vector<double> zFrontB_;     // Starting Z values for the slopes
0592   std::vector<double> rMinFront_;   // Corresponding rMin's
0593   std::vector<double> slopeT_;      // Slopes at the larger R
0594   std::vector<double> zFrontT_;     // Starting Z values for the slopes
0595   std::vector<double> rMaxFront_;   // Corresponding rMax's
0596   std::unordered_set<int> copies_;  // List of copy #'s
0597   double alpha_, cosAlpha_;
0598 };
0599 
0600 static long algorithm(dd4hep::Detector& /* description */, cms::DDParsingContext& ctxt, xml_h e) {
0601   HGCalHEFileAlgo healgo(ctxt, e);
0602   return cms::s_executed;
0603 }
0604 
0605 DECLARE_DDCMS_DETELEMENT(DDCMS_hgcal_DDHGCalHEFileAlgo, algorithm)