File indexing completed on 2024-04-06 12:14:54
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <cmath>
0011 #include <memory>
0012 #include <string>
0013 #include <unordered_set>
0014 #include <vector>
0015
0016 #include "DD4hep/DetFactoryHelper.h"
0017 #include "DataFormats/Math/interface/angle_units.h"
0018 #include "DetectorDescription/DDCMS/interface/DDPlugins.h"
0019 #include "DetectorDescription/DDCMS/interface/DDutils.h"
0020 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0021 #include "Geometry/HGCalCommonData/interface/HGCalGeomTools.h"
0022 #include "Geometry/HGCalCommonData/interface/HGCalParameters.h"
0023 #include "Geometry/HGCalCommonData/interface/HGCalTypes.h"
0024 #include "Geometry/HGCalCommonData/interface/HGCalWaferType.h"
0025
0026
0027 using namespace angle_units::operators;
0028
0029 struct HGCalEEAlgo {
0030 HGCalGeomTools geomTools_;
0031 std::unique_ptr<HGCalWaferType> waferType_;
0032 dd4hep::Volume mother_;
0033
0034 std::vector<std::string> wafers_;
0035 std::vector<std::string> materials_;
0036 std::vector<std::string> names_;
0037 std::vector<double> thick_;
0038 std::vector<int> copyNumber_;
0039 std::vector<int> layers_;
0040 std::vector<double> layerThick_;
0041 std::vector<int> layerType_;
0042 std::vector<int> layerSense_;
0043 std::vector<int> layerCenter_;
0044 int firstLayer_;
0045 int absorbMode_;
0046 int sensitiveMode_;
0047 double zMinBlock_;
0048 std::vector<double> rad100to200_;
0049 std::vector<double> rad200to300_;
0050 double zMinRadPar_;
0051 int choiceType_;
0052 int nCutRadPar_;
0053 double fracAreaMin_;
0054 double waferSize_;
0055 double waferSepar_;
0056 int sectors_;
0057 std::vector<double> slopeB_;
0058 std::vector<double> zFrontB_;
0059 std::vector<double> rMinFront_;
0060 std::vector<double> slopeT_;
0061 std::vector<double> zFrontT_;
0062 std::vector<double> rMaxFront_;
0063 std::unordered_set<int> copies_;
0064 double alpha_, cosAlpha_;
0065
0066 HGCalEEAlgo() = delete;
0067
0068 HGCalEEAlgo(cms::DDParsingContext& ctxt, xml_h e) {
0069 cms::DDNamespace ns(ctxt, e, true);
0070 cms::DDAlgoArguments args(ctxt, e);
0071
0072 mother_ = ns.volume(args.parentName());
0073 wafers_ = args.value<std::vector<std::string>>("WaferNames");
0074 #ifdef EDM_ML_DEBUG
0075 edm::LogVerbatim("HGCalGeom") << "DDHGCalEEAlgo: " << wafers_.size() << " wafers";
0076 for (unsigned int i = 0; i < wafers_.size(); ++i)
0077 edm::LogVerbatim("HGCalGeom") << "Wafer[" << i << "] " << wafers_[i];
0078 #endif
0079
0080 materials_ = args.value<std::vector<std::string>>("MaterialNames");
0081 names_ = args.value<std::vector<std::string>>("VolumeNames");
0082 thick_ = args.value<std::vector<double>>("Thickness");
0083 copyNumber_.resize(materials_.size(), 1);
0084 #ifdef EDM_ML_DEBUG
0085 edm::LogVerbatim("HGCalGeom") << "DDHGCalEEAlgo: " << materials_.size() << " types of volumes";
0086 for (unsigned int i = 0; i < names_.size(); ++i)
0087 edm::LogVerbatim("HGCalGeom") << "Volume [" << i << "] " << names_[i] << " of thickness "
0088 << cms::convert2mm(thick_[i]) << " filled with " << materials_[i]
0089 << " first copy number " << copyNumber_[i];
0090 #endif
0091
0092 layers_ = args.value<std::vector<int>>("Layers");
0093 layerThick_ = args.value<std::vector<double>>("LayerThick");
0094 #ifdef EDM_ML_DEBUG
0095 edm::LogVerbatim("HGCalGeom") << "There are " << layers_.size() << " blocks";
0096 for (unsigned int i = 0; i < layers_.size(); ++i)
0097 edm::LogVerbatim("HGCalGeom") << "Block [" << i << "] of thickness " << cms::convert2mm(layerThick_[i])
0098 << " with " << layers_[i] << " layers";
0099 #endif
0100
0101 layerType_ = args.value<std::vector<int>>("LayerType");
0102 layerSense_ = args.value<std::vector<int>>("LayerSense");
0103 firstLayer_ = args.value<int>("FirstLayer");
0104 absorbMode_ = args.value<int>("AbsorberMode");
0105 sensitiveMode_ = args.value<int>("SensitiveMode");
0106 #ifdef EDM_ML_DEBUG
0107 edm::LogVerbatim("HGCalGeom") << "First Layer " << firstLayer_ << " and "
0108 << "Absober:Sensitive mode " << absorbMode_ << ":" << sensitiveMode_;
0109 #endif
0110 layerCenter_ = args.value<std::vector<int>>("LayerCenter");
0111 #ifdef EDM_ML_DEBUG
0112 for (unsigned int i = 0; i < layerCenter_.size(); ++i)
0113 edm::LogVerbatim("HGCalGeom") << "LayerCenter [" << i << "] " << layerCenter_[i];
0114 #endif
0115 if (firstLayer_ > 0) {
0116 for (unsigned int i = 0; i < layerType_.size(); ++i) {
0117 if (layerSense_[i] > 0) {
0118 int ii = layerType_[i];
0119 copyNumber_[ii] = firstLayer_;
0120 #ifdef EDM_ML_DEBUG
0121 edm::LogVerbatim("HGCalGeom") << "First copy number for layer type " << i << ":" << ii << " with "
0122 << materials_[ii] << " changed to " << copyNumber_[ii];
0123 #endif
0124 break;
0125 }
0126 }
0127 } else {
0128 firstLayer_ = 1;
0129 }
0130 #ifdef EDM_ML_DEBUG
0131 edm::LogVerbatim("HGCalGeom") << "There are " << layerType_.size() << " layers";
0132 for (unsigned int i = 0; i < layerType_.size(); ++i)
0133 edm::LogVerbatim("HGCalGeom") << "Layer [" << i << "] with material type " << layerType_[i] << " sensitive class "
0134 << layerSense_[i];
0135 #endif
0136 zMinBlock_ = args.value<double>("zMinBlock");
0137
0138 rad100to200_ = args.value<std::vector<double>>("rad100to200");
0139 rad200to300_ = args.value<std::vector<double>>("rad200to300");
0140 zMinRadPar_ = args.value<double>("zMinForRadPar");
0141 choiceType_ = args.value<int>("choiceType");
0142 nCutRadPar_ = args.value<int>("nCornerCut");
0143 fracAreaMin_ = args.value<double>("fracAreaMin");
0144 waferSize_ = args.value<double>("waferSize");
0145 waferSepar_ = args.value<double>("SensorSeparation");
0146 sectors_ = args.value<int>("Sectors");
0147 alpha_ = (1._pi) / sectors_;
0148 cosAlpha_ = cos(alpha_);
0149 #ifdef EDM_ML_DEBUG
0150 edm::LogVerbatim("HGCalGeom") << "zStart " << cms::convert2mm(zMinBlock_)
0151 << " radius for wafer type separation uses " << rad100to200_.size()
0152 << " parameters; zmin " << cms::convert2mm(zMinRadPar_) << " cutoff " << choiceType_
0153 << ":" << nCutRadPar_ << ":" << fracAreaMin_ << " wafer width "
0154 << cms::convert2mm(waferSize_) << " separations " << cms::convert2mm(waferSepar_)
0155 << " sectors " << sectors_ << ":" << convertRadToDeg(alpha_) << ":" << cosAlpha_;
0156 for (unsigned int k = 0; k < rad100to200_.size(); ++k)
0157 edm::LogVerbatim("HGCalGeom") << "[" << k << "] 100-200 " << rad100to200_[k] << " 200-300 " << rad200to300_[k];
0158 #endif
0159
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 #endif
0174
0175 #ifdef EDM_ML_DEBUG
0176 edm::LogVerbatim("HGCalGeom") << "DDHGCalEEAlgo: NameSpace " << ns.name();
0177 #endif
0178
0179 waferType_ = std::make_unique<HGCalWaferType>(rad100to200_,
0180 rad200to300_,
0181 cms::convert2mm((waferSize_ + waferSepar_)),
0182 cms::convert2mm(zMinRadPar_),
0183 choiceType_,
0184 nCutRadPar_,
0185 fracAreaMin_);
0186
0187 ConstructAlgo(ctxt, e);
0188 }
0189
0190 void ConstructAlgo(cms::DDParsingContext& ctxt, xml_h e) {
0191 #ifdef EDM_ML_DEBUG
0192 edm::LogVerbatim("HGCalGeom") << "==>> Constructing DDHGCalEEAlgo...";
0193 copies_.clear();
0194 #endif
0195 dd4hep::Volume par;
0196 ConstructLayers(par, ctxt, e);
0197 #ifdef EDM_ML_DEBUG
0198 edm::LogVerbatim("HGCalGeom") << "DDHGCalEEAlgo: " << copies_.size() << " different wafer copy numbers";
0199 int k(0);
0200 for (std::unordered_set<int>::const_iterator itr = copies_.begin(); itr != copies_.end(); ++itr, ++k) {
0201 edm::LogVerbatim("HGCalGeom") << "Copy [" << k << "] : " << (*itr);
0202 }
0203 copies_.clear();
0204 edm::LogVerbatim("HGCalGeom") << "<<== End of DDHGCalEEAlgo construction...";
0205 #endif
0206 }
0207
0208 void ConstructLayers(const dd4hep::Volume module, cms::DDParsingContext& ctxt, xml_h e) {
0209 static constexpr double tol1 = 0.01 * dd4hep::mm;
0210 static constexpr double tol2 = 0.00001 * dd4hep::mm;
0211 cms::DDNamespace ns(ctxt, e, true);
0212
0213 #ifdef EDM_ML_DEBUG
0214 edm::LogVerbatim("HGCalGeom") << "DDHGCalEEAlgo: \t\tInside Layers";
0215 #endif
0216
0217 double zi(zMinBlock_);
0218 int laymin(0);
0219 for (unsigned int i = 0; i < layers_.size(); i++) {
0220 double zo = zi + layerThick_[i];
0221 double routF = HGCalGeomTools::radius(zi, zFrontT_, rMaxFront_, slopeT_);
0222 int laymax = laymin + layers_[i];
0223 double zz = zi;
0224 double thickTot(0);
0225 for (int ly = laymin; ly < laymax; ++ly) {
0226 int ii = layerType_[ly];
0227 int copy = copyNumber_[ii];
0228 double hthick = 0.5 * thick_[ii];
0229 double rinB = HGCalGeomTools::radius(zo - tol1, zFrontB_, rMinFront_, slopeB_);
0230 zz += hthick;
0231 thickTot += thick_[ii];
0232
0233 std::string name = ns.prepend(names_[ii]) + std::to_string(copy);
0234 #ifdef EDM_ML_DEBUG
0235 edm::LogVerbatim("HGCalGeom") << "DDHGCalEEAlgo: Layer " << ly << ":" << ii << " Front " << cms::convert2mm(zi)
0236 << ", " << cms::convert2mm(routF) << " Back " << cms::convert2mm(zo) << ", "
0237 << cms::convert2mm(rinB) << " superlayer thickness "
0238 << cms::convert2mm(layerThick_[i]);
0239 #endif
0240
0241 std::string matName = materials_[ii];
0242 dd4hep::Material matter = ns.material(matName);
0243 dd4hep::Volume glog;
0244 if (layerSense_[ly] < 1) {
0245 std::vector<double> pgonZ, pgonRin, pgonRout;
0246 if (layerSense_[ly] == 0 || absorbMode_ == 0) {
0247 double rmax = routF * cosAlpha_ - tol1;
0248 pgonZ.emplace_back(-hthick);
0249 pgonZ.emplace_back(hthick);
0250 pgonRin.emplace_back(rinB);
0251 pgonRin.emplace_back(rinB);
0252 pgonRout.emplace_back(rmax);
0253 pgonRout.emplace_back(rmax);
0254 } else {
0255 HGCalGeomTools::radius(zz - hthick,
0256 zz + hthick,
0257 zFrontB_,
0258 rMinFront_,
0259 slopeB_,
0260 zFrontT_,
0261 rMaxFront_,
0262 slopeT_,
0263 -layerSense_[ly],
0264 pgonZ,
0265 pgonRin,
0266 pgonRout);
0267 #ifdef EDM_ML_DEBUG
0268 edm::LogVerbatim("HGCalGeom") << "DDHGCalEEAlgo: z " << cms::convert2mm((zz - hthick)) << ":"
0269 << cms::convert2mm((zz + hthick)) << " with " << pgonZ.size() << " palnes";
0270 for (unsigned int isec = 0; isec < pgonZ.size(); ++isec)
0271 edm::LogVerbatim("HGCalGeom") << "[" << isec << "] z " << cms::convert2mm(pgonZ[isec]) << " R "
0272 << cms::convert2mm(pgonRin[isec]) << ":" << cms::convert2mm(pgonRout[isec]);
0273 #endif
0274 for (unsigned int isec = 0; isec < pgonZ.size(); ++isec) {
0275 pgonZ[isec] -= zz;
0276 pgonRout[isec] = pgonRout[isec] * cosAlpha_ - tol1;
0277 }
0278 }
0279
0280 dd4hep::Solid solid = dd4hep::Polyhedra(sectors_, -alpha_, 2._pi, pgonZ, pgonRin, pgonRout);
0281 ns.addSolidNS(ns.prepend(name), solid);
0282 glog = dd4hep::Volume(solid.name(), solid, matter);
0283 ns.addVolumeNS(glog);
0284
0285 #ifdef EDM_ML_DEBUG
0286 edm::LogVerbatim("HGCalGeom") << "DDHGCalEEAlgo: " << solid.name() << " polyhedra of " << sectors_
0287 << " sectors covering " << convertRadToDeg(-alpha_) << ":"
0288 << convertRadToDeg(-alpha_ + 2._pi) << " with " << pgonZ.size()
0289 << " sections and filled with " << matName;
0290
0291 for (unsigned int k = 0; k < pgonZ.size(); ++k)
0292 edm::LogVerbatim("HGCalGeom") << "[" << k << "] z " << cms::convert2mm(pgonZ[k]) << " R "
0293 << cms::convert2mm(pgonRin[k]) << ":" << cms::convert2mm(pgonRout[k]);
0294 #endif
0295 } else {
0296 double rins =
0297 (sensitiveMode_ < 1) ? rinB : HGCalGeomTools::radius(zz + hthick - tol1, zFrontB_, rMinFront_, slopeB_);
0298 double routs =
0299 (sensitiveMode_ < 1) ? routF : HGCalGeomTools::radius(zz - hthick, zFrontT_, rMaxFront_, slopeT_);
0300 dd4hep::Solid solid = dd4hep::Tube(rins, routs, hthick, 0.0, 2._pi);
0301 ns.addSolidNS(ns.prepend(name), solid);
0302 glog = dd4hep::Volume(solid.name(), solid, matter);
0303 ns.addVolumeNS(glog);
0304
0305 #ifdef EDM_ML_DEBUG
0306 edm::LogVerbatim("HGCalGeom") << "DDHGCalEEFileAlgo: " << solid.name() << " Tubs made of " << matter.name()
0307 << " of dimensions " << cms::convert2mm(rinB) << ":" << cms::convert2mm(rins)
0308 << ", " << cms::convert2mm(routF) << ":" << cms::convert2mm(routs) << ", "
0309 << cms::convert2mm(hthick) << ", 0.0, 360.0 and position " << glog.name()
0310 << " number " << copy << ":" << layerCenter_[copy - firstLayer_];
0311 #endif
0312 PositionSensitive(
0313 ctxt, e, glog, rins, routs, zz, layerSense_[ly], layerCenter_[copy - firstLayer_]);
0314 }
0315
0316 dd4hep::Position r1(0, 0, zz);
0317 mother_.placeVolume(glog, copy, r1);
0318 ++copyNumber_[ii];
0319
0320 #ifdef EDM_ML_DEBUG
0321 edm::LogVerbatim("HGCalGeom") << "DDHGCalEEAlgo: " << glog.name() << " number " << copy << " positioned in "
0322 << module.name() << " at (0,0," << cms::convert2mm(zz) << ") with no rotation";
0323 #endif
0324 zz += hthick;
0325 }
0326 zi = zo;
0327 laymin = laymax;
0328 if (std::abs(thickTot - layerThick_[i]) >= tol2) {
0329 if (thickTot > layerThick_[i]) {
0330 edm::LogError("HGCalGeom") << "Thickness of the partition " << cms::convert2mm(layerThick_[i])
0331 << " is smaller than " << cms::convert2mm(thickTot)
0332 << ": thickness of all its components **** ERROR ****";
0333 } else {
0334 edm::LogWarning("HGCalGeom") << "Thickness of the partition " << cms::convert2mm(layerThick_[i])
0335 << " does not match with " << cms::convert2mm(thickTot) << " of the components";
0336 }
0337 }
0338
0339 }
0340 }
0341
0342 void PositionSensitive(cms::DDParsingContext& ctxt,
0343 xml_h e,
0344 const dd4hep::Volume& glog,
0345 double rin,
0346 double rout,
0347 double zpos,
0348 int layertype,
0349 int layercenter) {
0350 cms::DDNamespace ns(ctxt, e, true);
0351 static const double sqrt3 = std::sqrt(3.0);
0352 double r = 0.5 * (waferSize_ + waferSepar_);
0353 double R = 2.0 * r / sqrt3;
0354 double dy = 0.75 * R;
0355 int N = (int)(0.5 * rout / r) + 2;
0356 const auto& xyoff = geomTools_.shiftXY(layercenter, (waferSize_ + waferSepar_));
0357 #ifdef EDM_ML_DEBUG
0358 int ium(0), ivm(0), iumAll(0), ivmAll(0), kount(0), ntot(0), nin(0);
0359 std::vector<int> ntype(6, 0);
0360 edm::LogVerbatim("HGCalGeom") << "DDHGCalEEAlgo: " << glog.name() << " rout " << cms::convert2mm(rout) << " N " << N
0361 << " for maximum u, v; r " << cms::convert2mm(r) << " R " << cms::convert2mm(R)
0362 << " dy " << cms::convert2mm(dy) << " Shift " << cms::convert2mm(xyoff.first) << ":"
0363 << cms::convert2mm(xyoff.second) << " WaferSize "
0364 << cms::convert2mm((waferSize_ + waferSepar_));
0365 #endif
0366
0367 for (int u = -N; u <= N; ++u) {
0368 for (int v = -N; v <= N; ++v) {
0369 int nr = 2 * v;
0370 int nc = -2 * u + v;
0371 double xpos = xyoff.first + nc * r;
0372 double ypos = xyoff.second + nr * dy;
0373 const auto& corner = HGCalGeomTools::waferCorner(xpos, ypos, r, R, rin, rout, false);
0374 #ifdef EDM_ML_DEBUG
0375 int iu = std::abs(u);
0376 int iv = std::abs(v);
0377 ++ntot;
0378 if (((corner.first <= 0) && std::abs(u) < 5 && std::abs(v) < 5) || (std::abs(u) < 2 && std::abs(v) < 2)) {
0379 edm::LogVerbatim("HGCalGeom") << "DDHGCalEEAlgo: " << glog.name() << " R " << cms::convert2mm(rin) << ":"
0380 << cms::convert2mm(rout) << "\n Z " << cms::convert2mm(zpos) << " LayerType "
0381 << layertype << " u " << u << " v " << v << " with " << corner.first
0382 << " corners";
0383 }
0384 #endif
0385 if (corner.first > 0) {
0386 int type = waferType_->getType(cms::convert2mm(xpos), cms::convert2mm(ypos), cms::convert2mm(zpos));
0387 int copy = HGCalTypes::packTypeUV(type, u, v);
0388 #ifdef EDM_ML_DEBUG
0389 if (iu > ium)
0390 ium = iu;
0391 if (iv > ivm)
0392 ivm = iv;
0393 kount++;
0394 if (copies_.count(copy) == 0)
0395 copies_.insert(copy);
0396 #endif
0397 if (corner.first == (int)(HGCalParameters::k_CornerSize)) {
0398 #ifdef EDM_ML_DEBUG
0399 if (iu > iumAll)
0400 iumAll = iu;
0401 if (iv > ivmAll)
0402 ivmAll = iv;
0403 ++nin;
0404 #endif
0405
0406 dd4hep::Position tran(xpos, ypos, 0.0);
0407 if (layertype > 1)
0408 type += 3;
0409 glog.placeVolume(ns.volume(wafers_[type]), copy, tran);
0410 #ifdef EDM_ML_DEBUG
0411 ++ntype[type];
0412 edm::LogVerbatim("HGCalGeom")
0413 << " DDHGCalEEAlgo: " << wafers_[type] << " number " << copy << " positioned in " << glog.name()
0414 << " at (" << cms::convert2mm(xpos) << ", " << cms::convert2mm(ypos) << ",0) with no rotation";
0415 #endif
0416 }
0417 }
0418 }
0419 }
0420
0421 #ifdef EDM_ML_DEBUG
0422 edm::LogVerbatim("HGCalGeom") << " DDHGCalEEAlgo: Maximum # of u " << ium << ":" << iumAll << " # of v " << ivm
0423 << ":" << ivmAll << " and " << nin << ":" << kount << ":" << ntot << " wafers ("
0424 << ntype[0] << ":" << ntype[1] << ":" << ntype[2] << ":" << ntype[3] << ":"
0425 << ntype[4] << ":" << ntype[5] << ") for " << glog.name() << " R "
0426 << cms::convert2mm(rin) << ":" << cms::convert2mm(rout);
0427 #endif
0428 }
0429 };
0430
0431 static long algorithm(dd4hep::Detector& , cms::DDParsingContext& ctxt, xml_h e) {
0432 HGCalEEAlgo eeAlgo(ctxt, e);
0433 return cms::s_executed;
0434 }
0435
0436 DECLARE_DDCMS_DETELEMENT(DDCMS_hgcal_DDHGCalEEAlgo, algorithm)