Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:14:54

0001 ///////////////////////////////////////////////////////////////////////////////
0002 // File: DDHGCalEEFileAlgo.cc
0003 // Description: Geometry factory class for HGCal (EE and HESil) using
0004 //              information from the file for dd4hep
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 HGCalEEFileAlgo {
0028   HGCalEEFileAlgo() { throw cms::Exception("HGCalGeom") << "Wrong initialization to HGCalEEFileAlgo"; }
0029   HGCalEEFileAlgo(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") << "DDHGCalEEFileAlgo: Creating an instance";
0035 #endif
0036     static constexpr double tol1 = 0.01 * dd4hep::mm;
0037     static constexpr double tol2 = 0.00001 * dd4hep::mm;
0038 
0039     dd4hep::Volume mother = ns.volume(args.parentName());
0040     wafers_ = args.value<std::vector<std::string>>("WaferNames");
0041 #ifdef EDM_ML_DEBUG
0042     edm::LogVerbatim("HGCalGeom") << "DDHGCalEEFileAlgo: " << wafers_.size() << " wafers";
0043     for (unsigned int i = 0; i < wafers_.size(); ++i)
0044       edm::LogVerbatim("HGCalGeom") << "Wafer[" << i << "] " << wafers_[i];
0045 #endif
0046     materials_ = args.value<std::vector<std::string>>("MaterialNames");
0047     names_ = args.value<std::vector<std::string>>("VolumeNames");
0048     thick_ = args.value<std::vector<double>>("Thickness");
0049     copyNumber_.resize(materials_.size(), 1);
0050 #ifdef EDM_ML_DEBUG
0051     edm::LogVerbatim("HGCalGeom") << "DDHGCalEEFileAlgo: " << materials_.size() << " types of volumes";
0052     for (unsigned int i = 0; i < names_.size(); ++i)
0053       edm::LogVerbatim("HGCalGeom") << "Volume [" << i << "] " << names_[i] << " of thickness "
0054                                     << cms::convert2mm(thick_[i]) << " filled with " << materials_[i]
0055                                     << " first copy number " << copyNumber_[i];
0056 #endif
0057     layers_ = args.value<std::vector<int>>("Layers");
0058     layerThick_ = args.value<std::vector<double>>("LayerThick");
0059 #ifdef EDM_ML_DEBUG
0060     edm::LogVerbatim("HGCalGeom") << "There are " << layers_.size() << " blocks";
0061     for (unsigned int i = 0; i < layers_.size(); ++i)
0062       edm::LogVerbatim("HGCalGeom") << "Block [" << i << "] of thickness " << cms::convert2mm(layerThick_[i])
0063                                     << " with " << layers_[i] << " layers";
0064 #endif
0065     layerType_ = args.value<std::vector<int>>("LayerType");
0066     layerSense_ = args.value<std::vector<int>>("LayerSense");
0067     firstLayer_ = args.value<int>("FirstLayer");
0068     absorbMode_ = args.value<int>("AbsorberMode");
0069     sensitiveMode_ = args.value<int>("SensitiveMode");
0070 #ifdef EDM_ML_DEBUG
0071     edm::LogVerbatim("HGCalGeom") << "First Layer " << firstLayer_ << " and "
0072                                   << "Absober:Sensitive mode " << absorbMode_ << ":" << sensitiveMode_;
0073 #endif
0074     layerCenter_ = args.value<std::vector<int>>("LayerCenter");
0075 #ifdef EDM_ML_DEBUG
0076     for (unsigned int i = 0; i < layerCenter_.size(); ++i)
0077       edm::LogVerbatim("HGCalGeom") << "LayerCenter [" << i << "] " << layerCenter_[i];
0078 #endif
0079     if (firstLayer_ > 0) {
0080       for (unsigned int i = 0; i < layerType_.size(); ++i) {
0081         if (layerSense_[i] > 0) {
0082           int ii = layerType_[i];
0083           copyNumber_[ii] = firstLayer_;
0084 #ifdef EDM_ML_DEBUG
0085           edm::LogVerbatim("HGCalGeom") << "First copy number for layer type " << i << ":" << ii << " with "
0086                                         << materials_[ii] << " changed to " << copyNumber_[ii];
0087 #endif
0088           break;
0089         }
0090       }
0091     } else {
0092       firstLayer_ = 1;
0093     }
0094 #ifdef EDM_ML_DEBUG
0095     edm::LogVerbatim("HGCalGeom") << "There are " << layerType_.size() << " layers";
0096     for (unsigned int i = 0; i < layerType_.size(); ++i)
0097       edm::LogVerbatim("HGCalGeom") << "Layer [" << i << "] with material type " << layerType_[i] << " sensitive class "
0098                                     << layerSense_[i];
0099 #endif
0100     zMinBlock_ = args.value<double>("zMinBlock");
0101     waferSize_ = args.value<double>("waferSize");
0102     waferSepar_ = args.value<double>("SensorSeparation");
0103     sectors_ = args.value<int>("Sectors");
0104     alpha_ = (1._pi) / sectors_;
0105     cosAlpha_ = cos(alpha_);
0106 #ifdef EDM_ML_DEBUG
0107     edm::LogVerbatim("HGCalGeom") << "zStart " << cms::convert2mm(zMinBlock_) << " wafer width "
0108                                   << cms::convert2mm(waferSize_) << " separations " << cms::convert2mm(waferSepar_)
0109                                   << " sectors " << sectors_ << ":" << convertRadToDeg(alpha_) << ":" << cosAlpha_;
0110 #endif
0111     waferIndex_ = args.value<std::vector<int>>("WaferIndex");
0112     waferProperty_ = args.value<std::vector<int>>("WaferProperties");
0113 #ifdef EDM_ML_DEBUG
0114     edm::LogVerbatim("HGCalGeom") << "waferProperties with " << waferIndex_.size() << " entries";
0115     for (unsigned int k = 0; k < waferIndex_.size(); ++k)
0116       edm::LogVerbatim("HGCalGeom") << "[" << k << "] " << waferIndex_[k] << " ("
0117                                     << HGCalWaferIndex::waferLayer(waferIndex_[k]) << ", "
0118                                     << HGCalWaferIndex::waferU(waferIndex_[k]) << ", "
0119                                     << HGCalWaferIndex::waferV(waferIndex_[k]) << ") : ("
0120                                     << HGCalProperty::waferThick(waferProperty_[k]) << ":"
0121                                     << HGCalProperty::waferPartial(waferProperty_[k]) << ":"
0122                                     << HGCalProperty::waferOrient(waferProperty_[k]) << ")";
0123 #endif
0124     slopeB_ = args.value<std::vector<double>>("SlopeBottom");
0125     zFrontB_ = args.value<std::vector<double>>("ZFrontBottom");
0126     rMinFront_ = args.value<std::vector<double>>("RMinFront");
0127     slopeT_ = args.value<std::vector<double>>("SlopeTop");
0128     zFrontT_ = args.value<std::vector<double>>("ZFrontTop");
0129     rMaxFront_ = args.value<std::vector<double>>("RMaxFront");
0130 #ifdef EDM_ML_DEBUG
0131     for (unsigned int i = 0; i < slopeB_.size(); ++i)
0132       edm::LogVerbatim("HGCalGeom") << "Bottom Block [" << i << "] Zmin " << cms::convert2mm(zFrontB_[i]) << " Rmin "
0133                                     << cms::convert2mm(rMinFront_[i]) << " Slope " << slopeB_[i];
0134     for (unsigned int i = 0; i < slopeT_.size(); ++i)
0135       edm::LogVerbatim("HGCalGeom") << "Top Block [" << i << "] Zmin " << cms::convert2mm(zFrontT_[i]) << " Rmax "
0136                                     << cms::convert2mm(rMaxFront_[i]) << " Slope " << slopeT_[i];
0137     edm::LogVerbatim("HGCalGeom") << "DDHGCalEEFileAlgo: NameSpace " << ns.name();
0138 #endif
0139 
0140 #ifdef EDM_ML_DEBUG
0141     edm::LogVerbatim("HGCalGeom") << "==>> Constructing DDHGCalEEFileAlgo...";
0142     copies_.clear();
0143 #endif
0144 
0145     double zi(zMinBlock_);
0146     int laymin(0);
0147     for (unsigned int i = 0; i < layers_.size(); ++i) {
0148       double zo = zi + layerThick_[i];
0149       double routF = HGCalGeomTools::radius(zi, zFrontT_, rMaxFront_, slopeT_);
0150       int laymax = laymin + layers_[i];
0151       double zz = zi;
0152       double thickTot(0);
0153       for (int ly = laymin; ly < laymax; ++ly) {
0154         int ii = layerType_[ly];
0155         int copy = copyNumber_[ii];
0156         double hthick = 0.5 * thick_[ii];
0157         double rinB = HGCalGeomTools::radius(zo - tol1, zFrontB_, rMinFront_, slopeB_);
0158         zz += hthick;
0159         thickTot += thick_[ii];
0160 
0161         std::string name = names_[ii] + std::to_string(copy);
0162 #ifdef EDM_ML_DEBUG
0163         edm::LogVerbatim("HGCalGeom") << "DDHGCalEEFileAlgo: Layer " << ly << ":" << ii << " Front "
0164                                       << cms::convert2mm(zi) << ", " << cms::convert2mm(routF) << " Back "
0165                                       << cms::convert2mm(zo) << ", " << cms::convert2mm(rinB)
0166                                       << " superlayer thickness " << cms::convert2mm(layerThick_[i]);
0167 #endif
0168 
0169         dd4hep::Material matter = ns.material(materials_[ii]);
0170         dd4hep::Volume glog;
0171 
0172         if (layerSense_[ly] < 1) {
0173           std::vector<double> pgonZ, pgonRin, pgonRout;
0174           double rmax = routF * cosAlpha_ - tol1;
0175           HGCalGeomTools::radius(zz - hthick,
0176                                  zz + hthick,
0177                                  zFrontB_,
0178                                  rMinFront_,
0179                                  slopeB_,
0180                                  zFrontT_,
0181                                  rMaxFront_,
0182                                  slopeT_,
0183                                  -layerSense_[ly],
0184                                  pgonZ,
0185                                  pgonRin,
0186                                  pgonRout);
0187           for (unsigned int isec = 0; isec < pgonZ.size(); ++isec) {
0188             pgonZ[isec] -= zz;
0189             if (layerSense_[ly] == 0 || absorbMode_ == 0)
0190               pgonRout[isec] = rmax;
0191             else
0192               pgonRout[isec] = pgonRout[isec] * cosAlpha_ - tol1;
0193           }
0194           dd4hep::Solid solid = dd4hep::Polyhedra(sectors_, -alpha_, 2._pi, pgonZ, pgonRin, pgonRout);
0195           ns.addSolidNS(ns.prepend(name), solid);
0196           glog = dd4hep::Volume(solid.name(), solid, matter);
0197           ns.addVolumeNS(glog);
0198 #ifdef EDM_ML_DEBUG
0199           edm::LogVerbatim("HGCalGeom") << "DDHGCalEEFileAlgo: " << solid.name() << " polyhedra of " << sectors_
0200                                         << " sectors covering " << convertRadToDeg(-alpha_) << ":"
0201                                         << convertRadToDeg(-alpha_ + 2._pi) << " with " << pgonZ.size()
0202                                         << " sections and filled with " << matter.name();
0203           for (unsigned int k = 0; k < pgonZ.size(); ++k)
0204             edm::LogVerbatim("HGCalGeom") << "[" << k << "] z " << cms::convert2mm(pgonZ[k]) << " R "
0205                                           << cms::convert2mm(pgonRin[k]) << ":" << cms::convert2mm(pgonRout[k]);
0206 #endif
0207         } else {
0208           double rins =
0209               (sensitiveMode_ < 1) ? rinB : HGCalGeomTools::radius(zz + hthick - tol1, zFrontB_, rMinFront_, slopeB_);
0210           double routs =
0211               (sensitiveMode_ < 1) ? routF : HGCalGeomTools::radius(zz - hthick, zFrontT_, rMaxFront_, slopeT_);
0212           dd4hep::Solid solid = dd4hep::Tube(rins, routs, hthick, 0.0, 2._pi);
0213           ns.addSolidNS(ns.prepend(name), solid);
0214           glog = dd4hep::Volume(solid.name(), solid, matter);
0215           ns.addVolumeNS(glog);
0216 
0217 #ifdef EDM_ML_DEBUG
0218           edm::LogVerbatim("HGCalGeom") << "DDHGCalEEFileAlgo: " << solid.name() << " Tubs made of " << matter.name()
0219                                         << " of dimensions " << cms::convert2mm(rinB) << ":" << cms::convert2mm(rins)
0220                                         << ", " << cms::convert2mm(routF) << ":" << cms::convert2mm(routs) << ", "
0221                                         << cms::convert2mm(hthick) << ", 0.0, 360.0 and position " << glog.name()
0222                                         << " number " << copy << ":" << layerCenter_[copy - firstLayer_];
0223 #endif
0224           positionSensitive(ctxt, e, glog, rins, routs, zz, layerSense_[ly], (copy - firstLayer_));
0225         }
0226 
0227         dd4hep::Position r1(0, 0, zz);
0228         mother.placeVolume(glog, copy, r1);
0229         ++copyNumber_[ii];
0230 #ifdef EDM_ML_DEBUG
0231         edm::LogVerbatim("HGCalGeom") << "DDHGCalEEFileAlgo: " << glog.name() << " number " << copy << " positioned in "
0232                                       << mother.name() << " at (0,0," << cms::convert2mm(zz) << ") with no rotation";
0233 #endif
0234         zz += hthick;
0235       }  // End of loop over layers in a block
0236       zi = zo;
0237       laymin = laymax;
0238       // Make consistency check of all the partitions of the block
0239       if (std::abs(thickTot - layerThick_[i]) >= tol2) {
0240         if (thickTot > layerThick_[i]) {
0241           edm::LogError("HGCalGeom") << "Thickness of the partition " << cms::convert2mm(layerThick_[i])
0242                                      << " is smaller than " << cms::convert2mm(thickTot)
0243                                      << ": thickness of all its components **** ERROR ****";
0244         } else {
0245           edm::LogWarning("HGCalGeom") << "Thickness of the partition " << cms::convert2mm(layerThick_[i])
0246                                        << " does not match with " << cms::convert2mm(thickTot) << " of the components";
0247         }
0248       }
0249     }  // End of loop over blocks
0250 
0251 #ifdef EDM_ML_DEBUG
0252     edm::LogVerbatim("HGCalGeom") << "DDHGCalEEFileAlgo: " << copies_.size() << " different wafer copy numbers";
0253     int k(0);
0254     for (std::unordered_set<int>::const_iterator itr = copies_.begin(); itr != copies_.end(); ++itr, ++k) {
0255       edm::LogVerbatim("HGCalGeom") << "Copy [" << k << "] : " << (*itr);
0256     }
0257     copies_.clear();
0258     edm::LogVerbatim("HGCalGeom") << "<<== End of DDHGCalEEFileAlgo construction...";
0259 #endif
0260   }
0261 
0262   void positionSensitive(cms::DDParsingContext& ctxt,
0263                          xml_h e,
0264                          const dd4hep::Volume& glog,
0265                          double rin,
0266                          double rout,
0267                          double zpos,
0268                          int layertype,
0269                          int layer) {
0270     cms::DDNamespace ns(ctxt, e, true);
0271     static const double sqrt3 = std::sqrt(3.0);
0272     int layercenter = layerCenter_[layer];
0273     double r = 0.5 * (waferSize_ + waferSepar_);
0274     double R = 2.0 * r / sqrt3;
0275     double dy = 0.75 * R;
0276     int N = (int)(0.5 * rout / r) + 2;
0277     const auto& xyoff = geomTools_.shiftXY(layercenter, (waferSize_ + waferSepar_));
0278 #ifdef EDM_ML_DEBUG
0279     int ium(0), ivm(0), iumAll(0), ivmAll(0), kount(0), ntot(0), nin(0);
0280     std::vector<int> ntype(6, 0);
0281     edm::LogVerbatim("HGCalGeom") << "DDHGCalEEFileAlgo: " << glog.name() << " rin:rout " << cms::convert2mm(rin) << ":"
0282                                   << cms::convert2mm(rout) << " zpos " << cms::convert2mm(zpos) << " N " << N
0283                                   << " for maximum u, v;  r " << cms::convert2mm(r) << " R " << cms::convert2mm(R)
0284                                   << " dy " << cms::convert2mm(dy) << " Shift " << cms::convert2mm(xyoff.first) << ":"
0285                                   << cms::convert2mm(xyoff.second) << " WaferSize "
0286                                   << cms::convert2mm((waferSize_ + waferSepar_));
0287 #endif
0288     for (int u = -N; u <= N; ++u) {
0289       for (int v = -N; v <= N; ++v) {
0290 #ifdef EDM_ML_DEBUG
0291         int iu = std::abs(u);
0292         int iv = std::abs(v);
0293 #endif
0294         int nr = 2 * v;
0295         int nc = -2 * u + v;
0296         double xpos = xyoff.first + nc * r;
0297         double ypos = xyoff.second + nr * dy;
0298         const auto& corner = HGCalGeomTools::waferCorner(xpos, ypos, r, R, rin, rout, false);
0299 #ifdef EDM_ML_DEBUG
0300         ++ntot;
0301         if (((corner.first <= 0) && std::abs(u) < 5 && std::abs(v) < 5) || (std::abs(u) < 2 && std::abs(v) < 2)) {
0302           edm::LogVerbatim("HGCalGeom") << "DDHGCalEEFileAlgo: " << glog.name() << " R " << cms::convert2mm(rin) << ":"
0303                                         << cms::convert2mm(rout) << "\n Z " << cms::convert2mm(zpos) << " LayerType "
0304                                         << layertype << " u " << u << " v " << v << " with " << corner.first
0305                                         << " corners";
0306         }
0307 #endif
0308         int indx = HGCalWaferIndex::waferIndex((layer + firstLayer_), u, v, false);
0309         int type = HGCalWaferType::getType(indx, waferIndex_, waferProperty_);
0310         if (corner.first > 0 && type >= 0) {
0311           int copy = HGCalTypes::packTypeUV(type, u, v);
0312 #ifdef EDM_ML_DEBUG
0313           edm::LogVerbatim("HGCalGeom") << " DDHGCalEEFileAlgo: " << wafers_[type] << " number " << copy << " type "
0314                                         << type << " layer:u:v:indx " << (layer + firstLayer_) << ":" << u << ":" << v
0315                                         << ":" << indx;
0316           if (iu > ium)
0317             ium = iu;
0318           if (iv > ivm)
0319             ivm = iv;
0320           kount++;
0321           if (copies_.count(copy) == 0)
0322             copies_.insert(copy);
0323 #endif
0324           if (corner.first == (int)(HGCalParameters::k_CornerSize)) {
0325 #ifdef EDM_ML_DEBUG
0326             if (iu > iumAll)
0327               iumAll = iu;
0328             if (iv > ivmAll)
0329               ivmAll = iv;
0330             ++nin;
0331 #endif
0332             dd4hep::Position tran(xpos, ypos, 0.0);
0333             if (layertype > 1)
0334               type += 3;
0335             glog.placeVolume(ns.volume(wafers_[type]), copy, tran);
0336 #ifdef EDM_ML_DEBUG
0337             ++ntype[type];
0338             edm::LogVerbatim("HGCalGeom")
0339                 << " DDHGCalEEFileAlgo: " << wafers_[type] << " number " << copy << " type " << layertype << ":" << type
0340                 << " positioned in " << glog.name() << " at (" << cms::convert2mm(xpos) << ", " << cms::convert2mm(ypos)
0341                 << ", 0) with no rotation";
0342 #endif
0343           }
0344         }
0345       }
0346     }
0347 
0348 #ifdef EDM_ML_DEBUG
0349     edm::LogVerbatim("HGCalGeom") << "DDHGCalEEFileAlgo: Maximum # of u " << ium << ":" << iumAll << " # of v " << ivm
0350                                   << ":" << ivmAll << " and " << nin << ":" << kount << ":" << ntot << " wafers ("
0351                                   << ntype[0] << ":" << ntype[1] << ":" << ntype[2] << ":" << ntype[3] << ":"
0352                                   << ntype[4] << ":" << ntype[5] << ") for " << glog.name() << " R "
0353                                   << cms::convert2mm(rin) << ":" << cms::convert2mm(rout);
0354 #endif
0355   }
0356 
0357   //Required data members to cache the values from XML file
0358   HGCalGeomTools geomTools_;
0359 
0360   std::vector<std::string> wafers_;     // Wafers
0361   std::vector<std::string> materials_;  // Materials
0362   std::vector<std::string> names_;      // Names
0363   std::vector<double> thick_;           // Thickness of the material
0364   std::vector<int> copyNumber_;         // Initial copy numbers
0365   std::vector<int> layers_;             // Number of layers in a section
0366   std::vector<double> layerThick_;      // Thickness of each section
0367   std::vector<int> layerType_;          // Type of the layer
0368   std::vector<int> layerSense_;         // Content of a layer (sensitive?)
0369   std::vector<int> layerCenter_;        // Centering of the wafers
0370   int firstLayer_;                      // Copy # of the first sensitive layer
0371   int absorbMode_;                      // Absorber mode
0372   int sensitiveMode_;                   // Sensitive mode
0373   double zMinBlock_;                    // Starting z-value of the block
0374   std::vector<int> waferIndex_;         // Wafer index for the types
0375   std::vector<int> waferProperty_;      // Wafer property
0376   double waferSize_;                    // Width of the wafer
0377   double waferSepar_;                   // Sensor separation
0378   int sectors_;                         // Sectors
0379   std::vector<double> slopeB_;          // Slope at the lower R
0380   std::vector<double> zFrontB_;         // Starting Z values for the slopes
0381   std::vector<double> rMinFront_;       // Corresponding rMin's
0382   std::vector<double> slopeT_;          // Slopes at the larger R
0383   std::vector<double> zFrontT_;         // Starting Z values for the slopes
0384   std::vector<double> rMaxFront_;       // Corresponding rMax's
0385   std::unordered_set<int> copies_;      // List of copy #'s
0386   double alpha_, cosAlpha_;
0387 };
0388 
0389 static long algorithm(dd4hep::Detector& /* description */, cms::DDParsingContext& ctxt, xml_h e) {
0390   HGCalEEFileAlgo eeFileAlgo(ctxt, e);
0391   return cms::s_executed;
0392 }
0393 
0394 DECLARE_DDCMS_DETELEMENT(DDCMS_hgcal_DDHGCalEEFileAlgo, algorithm)