CMS 3D CMS Logo

DDHGCalModuleAlgo.cc
Go to the documentation of this file.
1 /*
2  * DDHGCalModuleAlgo.cc
3  *
4  * Created on: 27-August-2019
5  * Author: Sunanda Banerjee
6  */
7 
9 #include "DD4hep/DetFactoryHelper.h"
15 
16 //#define EDM_ML_DEBUG
17 
18 #ifdef EDM_ML_DEBUG
19 #include <unordered_set>
20 #endif
21 using namespace cms_units::operators;
22 
23 static long algorithm(dd4hep::Detector& /* description */,
25  xml_h e,
26  dd4hep::SensitiveDetector& /* sens */) {
27  cms::DDNamespace ns(ctxt, e, true);
28  cms::DDAlgoArguments args(ctxt, e);
29 
30  const auto& wafer = args.value<std::vector<std::string> >("WaferName"); // Wafers
31  auto materials = args.value<std::vector<std::string> >("MaterialNames"); // Materials
32  const auto& names = args.value<std::vector<std::string> >("VolumeNames"); // Names
33  const auto& thick = args.value<std::vector<double> >("Thickness"); // Thickness of the material
34  std::vector<int> copyNumber; // Initial copy numbers
35  for (unsigned int i = 0; i < materials.size(); ++i) {
36  if (materials[i] == "materials:M_NEMAFR4plate")
37  materials[i] = "materials:M_NEMA FR4 plate";
38  copyNumber.emplace_back(1);
39  }
40 #ifdef EDM_ML_DEBUG
41  edm::LogVerbatim("HGCalGeom") << "DDHGCalModule: " << wafer.size() << " wafers";
42  for (unsigned int i = 0; i < wafer.size(); ++i)
43  edm::LogVerbatim("HGCalGeom") << "Wafer[" << i << "] " << wafer[i];
44  edm::LogVerbatim("HGCalGeom") << "DDHGCalModule: " << materials.size() << " types of volumes";
45  for (unsigned int i = 0; i < names.size(); ++i)
46  edm::LogVerbatim("HGCalGeom") << "Volume [" << i << "] " << names[i] << " of thickness " << thick[i]
47  << " filled with " << materials[i] << " first copy number " << copyNumber[i];
48 #endif
49  const auto& layers = args.value<std::vector<int> >("Layers"); // Number of layers in a section
50  const auto& layerThick = args.value<std::vector<double> >("LayerThick"); // Thickness of each section
51  const auto& layerType = args.value<std::vector<int> >("LayerType"); // Type of the layer
52  const auto& layerSense = args.value<std::vector<int> >("LayerSense"); // Content of a layer (sensitive?)
53 #ifdef EDM_ML_DEBUG
54  edm::LogVerbatim("HGCalGeom") << "DDHGCalModule: " << layers.size() << " blocks";
55  for (unsigned int i = 0; i < layers.size(); ++i)
56  edm::LogVerbatim("HGCalGeom") << "Block [" << i << "] of thickness " << layerThick[i] << " with " << layers[i]
57  << " layers";
58  edm::LogVerbatim("HGCalGeom") << "DDHGCalModule: " << layerType.size() << " layers";
59  for (unsigned int i = 0; i < layerType.size(); ++i)
60  edm::LogVerbatim("HGCalGeom") << "Layer [" << i << "] with material type " << layerType[i] << " sensitive class "
61  << layerSense[i];
62 #endif
63  double zMinBlock = args.value<double>("zMinBlock"); // Starting z-value of the block
64  double rMaxFine = args.value<double>("rMaxFine"); // Maximum r-value for fine wafer
65  double waferW = args.value<double>("waferW"); // Width of the wafer
66  double waferGap = args.value<double>("waferGap"); // Gap between 2 wafers
67  int sectors = args.value<int>("Sectors"); // Sectors
68 #ifdef EDM_ML_DEBUG
69  edm::LogVerbatim("HGCalGeom") << "DDHGCalModule: zStart " << zMinBlock << " rFineCoarse " << rMaxFine
70  << " wafer width " << waferW << waferW << " gap among wafers " << waferGap
71  << " sectors " << sectors;
72 #endif
73  const auto& slopeB = args.value<std::vector<double> >("SlopeBottom"); // Slope at the lower R
74  const auto& slopeT = args.value<std::vector<double> >("SlopeTop"); // Slopes at the larger R
75  const auto& zFront = args.value<std::vector<double> >("ZFront"); // Starting Z values for the slopes
76  const auto& rMaxFront = args.value<std::vector<double> >("RMaxFront"); // Corresponding rMax's
77 #ifdef EDM_ML_DEBUG
78  edm::LogVerbatim("HGCalGeom") << "DDHGCalModule: Bottom slopes " << slopeB[0] << ":" << slopeB[1] << " and "
79  << slopeT.size() << " slopes for top";
80  for (unsigned int i = 0; i < slopeT.size(); ++i)
81  edm::LogVerbatim("HGCalGeom") << "Block [" << i << "] Zmin " << zFront[i] << " Rmax " << rMaxFront[i] << " Slope "
82  << slopeT[i];
83 #endif
84  std::string idNameSpace = static_cast<std::string>(ns.name()); // Namespace of this and ALL sub-parts
85  const auto& idName = args.parentName(); // Name of the "parent" volume.
86 #ifdef EDM_ML_DEBUG
87  std::unordered_set<int> copies; // List of copy #'s
88  edm::LogVerbatim("HGCalGeom") << "DDHGCalModule: NameSpace " << idNameSpace << " Mother " << idName;
89 #endif
90 
91  // Mother module
92  dd4hep::Volume module = ns.volume(idName);
93 
94  double zi(zMinBlock);
95  int laymin(0);
96  const double tol(0.01);
97  for (unsigned int i = 0; i < layers.size(); i++) {
98  double zo = zi + layerThick[i];
99  double routF = HGCalGeomTools::radius(zi, zFront, rMaxFront, slopeT);
100  int laymax = laymin + layers[i];
101  double zz = zi;
102  double thickTot(0);
103  for (int ly = laymin; ly < laymax; ++ly) {
104  int ii = layerType[ly];
105  int copy = copyNumber[ii];
106  double rinB = (layerSense[ly] == 0) ? (zo * slopeB[0]) : (zo * slopeB[1]);
107  zz += (0.5 * thick[ii]);
108  thickTot += thick[ii];
109 
110  std::string name = "HGCal" + names[ii] + std::to_string(copy);
111 #ifdef EDM_ML_DEBUG
112  edm::LogVerbatim("HGCalGeom") << "DDHGCalModule: Layer " << ly << ":" << ii << " Front " << zi << ", " << routF
113  << " Back " << zo << ", " << rinB << " superlayer thickness " << layerThick[i];
114 #endif
115  dd4hep::Material matter = ns.material(materials[ii]);
116  dd4hep::Volume glog;
117  if (layerSense[ly] == 0) {
119  double rmax = routF * cos(alpha) - tol;
120  std::vector<double> pgonZ, pgonRin, pgonRout;
121  pgonZ.emplace_back(-0.5 * thick[ii]);
122  pgonZ.emplace_back(0.5 * thick[ii]);
123  pgonRin.emplace_back(rinB);
124  pgonRin.emplace_back(rinB);
125  pgonRout.emplace_back(rmax);
126  pgonRout.emplace_back(rmax);
127  dd4hep::Solid solid = dd4hep::Polyhedra(sectors, -alpha, 2 * cms_units::piRadians, pgonZ, pgonRin, pgonRout);
128  ns.addSolidNS(ns.prepend(name), solid);
129  glog = dd4hep::Volume(solid.name(), solid, matter);
130 #ifdef EDM_ML_DEBUG
131  edm::LogVerbatim("HGCalGeom") << "DDHGCalModule: " << solid.name() << " polyhedra of " << sectors
132  << " sectors covering " << convertRadToDeg(-alpha) << ":"
133  << (360.0 + convertRadToDeg(-alpha)) << " with " << pgonZ.size() << " sections";
134  for (unsigned int k = 0; k < pgonZ.size(); ++k)
135  edm::LogVerbatim("HGCalGeom") << "[" << k << "] z " << pgonZ[k] << " R " << pgonRin[k] << ":" << pgonRout[k];
136 #endif
137  } else {
138  dd4hep::Solid solid = dd4hep::Tube(0.5 * thick[ii], rinB, routF, 0.0, 2 * cms_units::piRadians);
139  ns.addSolidNS(ns.prepend(name), solid);
140  glog = dd4hep::Volume(solid.name(), solid, matter);
141 #ifdef EDM_ML_DEBUG
142  edm::LogVerbatim("HGCalGeom") << "DDHGCalModule: " << solid.name() << " Tubs made of " << materials[ii]
143  << " of dimensions " << rinB << ", " << routF << ", " << 0.5 * thick[ii]
144  << ", 0.0, 360.0";
145  edm::LogVerbatim("HGCalGeom") << "DDHGCalModule test position in: " << glog.name() << " number " << copy;
146 #endif
147  double ww = (waferW + waferGap);
148  double dx = 0.5 * ww;
149  double dy = 3.0 * dx * tan(30._deg);
150  double rr = 2.0 * dx * tan(30._deg);
151  int ncol = static_cast<int>(2.0 * routF / ww) + 1;
152  int nrow = static_cast<int>(routF / (ww * tan(30._deg))) + 1;
153 #ifdef EDM_ML_DEBUG
154  int incm(0), inrm(0), kount(0), ntot(0), nin(0), nfine(0), ncoarse(0);
155  edm::LogVerbatim("HGCalGeom") << glog.name() << " rout " << routF << " Row " << nrow << " Column " << ncol;
156 #endif
157  for (int nr = -nrow; nr <= nrow; ++nr) {
158  int inr = (nr >= 0) ? nr : -nr;
159  for (int nc = -ncol; nc <= ncol; ++nc) {
160  int inc = (nc >= 0) ? nc : -nc;
161  if (inr % 2 == inc % 2) {
162  double xpos = nc * dx;
163  double ypos = nr * dy;
164  std::pair<int, int> corner = HGCalGeomTools::waferCorner(xpos, ypos, dx, rr, rinB, routF, true);
165 #ifdef EDM_ML_DEBUG
166  ++ntot;
167 #endif
168  if (corner.first > 0) {
169  int copyL = inr * 100 + inc;
170  if (nc < 0)
171  copyL += 10000;
172  if (nr < 0)
173  copyL += 100000;
174 #ifdef EDM_ML_DEBUG
175  if (inc > incm)
176  incm = inc;
177  if (inr > inrm)
178  inrm = inr;
179  kount++;
180  copies.insert(copy);
181 #endif
182  if (corner.first == (int)(HGCalParameters::k_CornerSize)) {
183  double rpos = std::sqrt(xpos * xpos + ypos * ypos);
184  dd4hep::Position tran(xpos, ypos, 0.0);
185  dd4hep::Rotation3D rotation;
186  dd4hep::Volume glog1 = (rpos < rMaxFine) ? ns.volume(wafer[0]) : ns.volume(wafer[1]);
187  glog.placeVolume(glog1, copyL, dd4hep::Transform3D(rotation, tran));
188 #ifdef EDM_ML_DEBUG
189  ++nin;
190  if (rpos < rMaxFine)
191  ++nfine;
192  else
193  ++ncoarse;
194  edm::LogVerbatim("HGCalGeom")
195  << "DDHGCalModule: " << glog1.name() << " number " << copyL << " positioned in " << glog.name()
196  << " at " << tran << " with " << rotation;
197 #endif
198  }
199  }
200  }
201  }
202  }
203 #ifdef EDM_ML_DEBUG
204  edm::LogVerbatim("HGCalGeom") << "DDHGCalModule: # of columns " << incm << " # of rows " << inrm << " and "
205  << nin << ":" << kount << ":" << ntot << " wafers (" << nfine << ":" << ncoarse
206  << ") for " << glog.name() << " R " << rinB << ":" << routF;
207 #endif
208  }
209  dd4hep::Position r1(0, 0, zz);
210  dd4hep::Rotation3D rot;
211  module.placeVolume(glog, copy, dd4hep::Transform3D(rot, r1));
212  ++copyNumber[ii];
213 #ifdef EDM_ML_DEBUG
214  edm::LogVerbatim("HGCalGeom") << "DDHGCalModule: " << glog.name() << " number " << copy << " positioned in "
215  << module.name() << " at " << r1 << " with " << rot;
216 #endif
217  zz += (0.5 * thick[ii]);
218  } // End of loop over layers in a block
219  zi = zo;
220  laymin = laymax;
221  if (fabs(thickTot - layerThick[i]) < 0.00001) {
222  } else if (thickTot > layerThick[i]) {
223  edm::LogError("HGCalGeom") << "Thickness of the partition " << layerThick[i] << " is smaller than thickness "
224  << thickTot << " of all its components **** ERROR ****\n";
225  } else if (thickTot < layerThick[i]) {
226  edm::LogWarning("HGCalGeom") << "Thickness of the partition " << layerThick[i] << " does not match with "
227  << thickTot << " of the components\n";
228  }
229  } // End of loop over blocks
230 
231 #ifdef EDM_ML_DEBUG
232  edm::LogVerbatim("HGCalGeom") << copies.size() << " different wafer copy numbers";
233  int k(0);
234  for (std::unordered_set<int>::const_iterator itr = copies.begin(); itr != copies.end(); ++itr, ++k)
235  edm::LogVerbatim("HGCalGeom") << "Copy[" << k << "] : " << (*itr);
236  edm::LogVerbatim("HGCalGeom") << "<<== End of DDHGCalModule construction ...";
237 #endif
238 
239  return 1;
240 }
241 
242 // first argument is the type from the xml file
243 DECLARE_DDCMS_DETELEMENT(DDCMS_hgcal_DDHGCalModuleAlgo, algorithm)
dd4hep::Solid addSolidNS(const std::string &name, dd4hep::Solid solid) const
Definition: DDNamespace.cc:200
dd4hep::Volume volume(const std::string &name, bool exc=true) const
Definition: DDNamespace.cc:184
static void radius(double zf, double zb, std::vector< double > const &zFront1, std::vector< double > const &rFront1, std::vector< double > const &slope1, std::vector< double > const &zFront2, std::vector< double > const &rFront2, std::vector< double > const &slope2, int flag, std::vector< double > &zz, std::vector< double > &rin, std::vector< double > &rout)
constexpr NumType convertRadToDeg(NumType radians)
Definition: angle_units.h:21
T value(const std::string &name) const
const std::string names[nVars_]
int nin
std::string_view name() const
Definition: DDNamespace.h:68
constexpr long double piRadians(M_PIl)
static std::pair< int32_t, int32_t > waferCorner(double xpos, double ypos, double r, double R, double rMin, double rMax, bool oldBug=false)
T sqrt(T t)
Definition: SSEVec.h:19
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
Tan< T >::type tan(const T &t)
Definition: Tan.h:22
dd4hep::Material material(const std::string &name) const
Definition: DDNamespace.cc:121
static long algorithm(dd4hep::Detector &, cms::DDParsingContext &ctxt, xml_h e, dd4hep::SensitiveDetector &)
std::string prepend(const std::string &) const
Definition: DDNamespace.cc:66
dd4hep::Volume Volume
ii
Definition: cuy.py:590
static uint32_t k_CornerSize
alpha
zGenParticlesMatch = cms.InputTag(""),
#define DECLARE_DDCMS_DETELEMENT(name, func)
Definition: DDPlugins.h:30
Definition: vlib.h:198
std::string parentName() const
Access value of rParent child node.