CMS 3D CMS Logo

DDHGCalWaferPartialRotated.cc
Go to the documentation of this file.
1 /*
2  * DDHGCalWaferPartialRotated.cc
3  *
4  * Created on: 09-Jan-2021
5  */
6 
7 #include "DD4hep/DetFactoryHelper.h"
15 
16 #include <string>
17 #include <vector>
18 #include <sstream>
19 
20 //#define EDM_ML_DEBUG
21 
22 static long algorithm(dd4hep::Detector& /* description */, cms::DDParsingContext& ctxt, xml_h e) {
23  cms::DDNamespace ns(ctxt, e, true);
25  std::string parentName = args.parentName();
26  const auto& material = args.value<std::string>("ModuleMaterial");
27  const auto& thick = args.value<double>("ModuleThickness");
28  const auto& waferMode = args.value<int>("WaferMode");
29  const auto& waferSize = args.value<double>("WaferSize");
30  const auto& waferThick = args.value<double>("WaferThickness");
31  const auto& waferTag = args.value<std::string>("WaferTag");
32 #ifdef EDM_ML_DEBUG
33  const auto& waferSepar = args.value<double>("SensorSeparation");
34  edm::LogVerbatim("HGCalGeom") << "DDHGCalWaferPartialRotated: Module " << parentName << " made of " << material
35  << " T " << cms::convert2mm(thick) << " Wafer 2r " << cms::convert2mm(waferSize)
36  << " Half Separation " << cms::convert2mm(waferSepar) << " T "
37  << cms::convert2mm(waferThick) << " Mode " << waferMode;
38 #endif
39  const auto& tags = args.value<std::vector<std::string>>("Tags");
40  const auto& partialTypes = args.value<std::vector<int>>("PartialTypes");
41  const auto& placementIndex = args.value<std::vector<int>>("PlacementIndex");
42  const auto& placementIndexTags = args.value<std::vector<std::string>>("PlacementIndexTags");
43 #ifdef EDM_ML_DEBUG
44  edm::LogVerbatim("HGCalGeom") << "DDHGCalWaferPartialRotated: " << tags.size() << " variations of wafer types";
45  for (unsigned int k = 0; k < tags.size(); ++k) {
46  for (unsigned int m = 0; m < placementIndex.size(); ++m) {
47  edm::LogVerbatim("HGCalGeom") << "Type[" << k << "] " << tags[k] << " Partial " << partialTypes[k]
48  << " Placement Index " << placementIndex[m] << " Tag " << placementIndexTags[m];
49  }
50  }
51 #endif
52  const auto& layerNames = args.value<std::vector<std::string>>("LayerNames");
53  const auto& materials = args.value<std::vector<std::string>>("LayerMaterials");
54  const auto& layerThick = args.value<std::vector<double>>("LayerThickness");
55  const auto& layerSizeOff = args.value<std::vector<double>>("LayerSizeOffset");
56  const auto& layerType = args.value<std::vector<int>>("LayerTypes");
57 #ifdef EDM_ML_DEBUG
58  edm::LogVerbatim("HGCalGeom") << "DDHGCalWaferPartialRotated: " << layerNames.size() << " types of volumes";
59  for (unsigned int i = 0; i < layerNames.size(); ++i)
60  edm::LogVerbatim("HGCalGeom") << "Volume [" << i << "] " << layerNames[i] << " of thickness "
61  << cms::convert2mm(layerThick[i]) << " size offset "
62  << cms::convert2mm(layerSizeOff[i]) << " filled with " << materials[i] << " type "
63  << layerType[i];
64 #endif
65  const auto& layers = args.value<std::vector<int>>("Layers");
66 #ifdef EDM_ML_DEBUG
67  std::ostringstream st1;
68  for (unsigned int i = 0; i < layers.size(); ++i)
69  st1 << " [" << i << "] " << layers[i];
70  edm::LogVerbatim("HGCalGeom") << "There are " << layers.size() << " blocks" << st1.str();
71 #endif
72  const auto& senseName = args.value<std::string>("SenseName");
73  const auto& senseT = args.value<double>("SenseThick");
74  const auto& senseType = args.value<int>("SenseType");
75 #ifdef EDM_ML_DEBUG
76  edm::LogVerbatim("HGCalGeom") << "DDHGCalWaferPartialRotated: NameSpace " << ns.name() << " Sensitive Layer Name "
77  << senseName << " Thickness " << senseT << " Type " << senseType;
78 #endif
79 
80 #ifdef EDM_ML_DEBUG
81  edm::LogVerbatim("HGCalGeom") << "==>> Executing DDHGCalWaferPartialRotated...";
82 #endif
83  static constexpr double tol = 0.00001 * dd4hep::mm;
84 
85  // Loop over all types
86  for (unsigned int k = 0; k < tags.size(); ++k) {
87  for (unsigned int m = 0; m < placementIndex.size(); ++m) {
88  // First the mother
89  std::string mother = parentName + placementIndexTags[m] + waferTag + tags[k];
90  std::vector<std::pair<double, double>> wxy =
91  HGCalWaferMask::waferXY(partialTypes[k], placementIndex[m], waferSize, 0.0, 0.0, 0.0, (waferMode > 0));
92  std::vector<double> xM, yM;
93  for (unsigned int i = 0; i < (wxy.size() - 1); ++i) {
94  xM.emplace_back(wxy[i].first);
95  yM.emplace_back(wxy[i].second);
96  }
97  std::vector<double> zw = {-0.5 * thick, 0.5 * thick};
98  std::vector<double> zx(2, 0), zy(2, 0), scale(2, 1.0);
99 
100  dd4hep::Material matter = ns.material(material);
101  dd4hep::Solid solid = dd4hep::ExtrudedPolygon(xM, yM, zw, zx, zy, scale);
102  ns.addSolidNS(ns.prepend(mother), solid);
103  dd4hep::Volume glogM = dd4hep::Volume(solid.name(), solid, matter);
104  ns.addVolumeNS(glogM);
105 #ifdef EDM_ML_DEBUG
106  edm::LogVerbatim("HGCalGeom") << "DDHGCalWaferPartialRotated: " << solid.name() << " extruded polygon made of "
107  << material << " z|x|y|s (0) " << cms::convert2mm(zw[0]) << ":"
108  << cms::convert2mm(zx[0]) << ":" << cms::convert2mm(zy[0]) << ":" << scale[0]
109  << " z|x|y|s (1) " << cms::convert2mm(zw[1]) << ":" << cms::convert2mm(zx[1]) << ":"
110  << cms::convert2mm(zy[1]) << ":" << scale[1] << " partial " << partialTypes[k]
111  << " placement index " << placementIndex[m] << " and " << xM.size() << " edges";
112  for (unsigned int j = 0; j < xM.size(); ++j)
113  edm::LogVerbatim("HGCalGeom") << "[" << j << "] " << cms::convert2mm(xM[j]) << ":" << cms::convert2mm(yM[j]);
114 #endif
115 
116  // Then the layers
117  dd4hep::Rotation3D rotation;
118  std::vector<dd4hep::Volume> glogs(materials.size());
119  std::vector<int> copyNumber(materials.size(), 1);
120  double zi(-0.5 * thick), thickTot(0.0);
121  for (unsigned int l = 0; l < layers.size(); l++) {
122  unsigned int i = layers[l];
124  partialTypes[k], placementIndex[m], waferSize, layerSizeOff[i], 0.0, 0.0, (waferMode > 0));
125  std::vector<double> xL, yL;
126  for (unsigned int i0 = 0; i0 < (wxy.size() - 1); ++i0) {
127  xL.emplace_back(wxy[i0].first);
128  yL.emplace_back(wxy[i0].second);
129  }
130 #ifdef EDM_ML_DEBUG
131  edm::LogVerbatim("HGCalGeom") << "DDHGCalWaferPartialRotated:Layer " << l << ":" << i << " T " << layerThick[i]
132  << " Size offset " << layerSizeOff[i] << " Copy " << copyNumber[i]
133  << " Partial type " << partialTypes[k];
134 #endif
135  if (copyNumber[i] == 1) {
136  if (layerType[i] > 0) {
137  zw[0] = -0.5 * waferThick;
138  zw[1] = 0.5 * waferThick;
139  } else {
140  zw[0] = -0.5 * layerThick[i];
141  zw[1] = 0.5 * layerThick[i];
142  }
143  solid = dd4hep::ExtrudedPolygon(xL, yL, zw, zx, zy, scale);
144  std::string lname = layerNames[i] + placementIndexTags[m] + waferTag + tags[k];
145  ns.addSolidNS(ns.prepend(lname), solid);
146  matter = ns.material(materials[i]);
147  glogs[i] = dd4hep::Volume(solid.name(), solid, matter);
148  ns.addVolumeNS(glogs[i]);
149 #ifdef EDM_ML_DEBUG
150  edm::LogVerbatim("HGCalGeom") << "DDHGCalWaferPartialRotated: " << solid.name()
151  << " extruded polygon made of " << materials[i] << " z|x|y|s (0) "
152  << cms::convert2mm(zw[0]) << ":" << cms::convert2mm(zx[0]) << ":"
153  << cms::convert2mm(zy[0]) << ":" << scale[0] << " z|x|y|s (1) "
154  << cms::convert2mm(zw[1]) << ":" << cms::convert2mm(zx[1]) << ":"
155  << cms::convert2mm(zy[1]) << ":" << scale[1] << " partial " << partialTypes[k]
156  << " placement index " << placementIndex[m] << " and " << xM.size() << " edges";
157  for (unsigned int j = 0; j < xL.size(); ++j)
158  edm::LogVerbatim("HGCalGeom")
159  << "[" << j << "] " << cms::convert2mm(xL[j]) << ":" << cms::convert2mm(yL[j]);
160 #endif
161  }
162  if ((layerType[i] > 0) && (senseType >= 0)) {
163  std::string sname = senseName + placementIndexTags[m] + waferTag + tags[k];
164  zw[0] = -0.5 * senseT;
165  zw[1] = 0.5 * senseT;
166  solid = dd4hep::ExtrudedPolygon(xL, yL, zw, zx, zy, scale);
167  ns.addSolidNS(ns.prepend(sname), solid);
168  dd4hep::Volume glog = dd4hep::Volume(solid.name(), solid, matter);
169  ns.addVolumeNS(glog);
170 #ifdef EDM_ML_DEBUG
171  edm::LogVerbatim("HGCalGeom") << "DDHGCalWaferPartialRotated: " << solid.name()
172  << " extruded polygon made of " << materials[i] << " z|x|y|s (0) "
173  << cms::convert2mm(zw[0]) << ":" << cms::convert2mm(zx[0]) << ":"
174  << cms::convert2mm(zy[0]) << ":" << scale[0] << " z|x|y|s (1) "
175  << cms::convert2mm(zw[1]) << ":" << cms::convert2mm(zx[1]) << ":"
176  << cms::convert2mm(zy[1]) << ":" << scale[1] << " partial " << partialTypes[k]
177  << " placement index " << placementIndex[m] << " and " << xL.size() << " edges";
178  for (unsigned int j = 0; j < xL.size(); ++j)
179  edm::LogVerbatim("HGCalGeom")
180  << "[" << j << "] " << cms::convert2mm(xL[j]) << ":" << cms::convert2mm(yL[j]);
181 #endif
182  auto posSense = HGCalCell::cellOrient(placementIndex[m]);
183  double zpos = (posSense.second > 0) ? -0.5 * (waferThick - senseT) : 0.5 * (waferThick - senseT);
184  dd4hep::Position tran(0, 0, zpos);
185  int copy = 10 + senseType;
186  glogs[i].placeVolume(glog, copy, tran);
187 #ifdef EDM_ML_DEBUG
188  edm::LogVerbatim("HGCalGeom") << "DDHGCalWaferPartialRotated: " << glog.name() << " number " << copy
189  << " positioned in " << glogs[i].name() << " at (0,0," << cms::convert2mm(zpos)
190  << ") with no rotation";
191 #endif
192  }
193  dd4hep::Position tran0(0, 0, (zi + 0.5 * layerThick[i]));
194  glogM.placeVolume(glogs[i], copyNumber[i], tran0);
195 #ifdef EDM_ML_DEBUG
196  edm::LogVerbatim("HGCalGeom") << "DDHGCalWaferPartialRotated: " << glogs[i].name() << " number "
197  << copyNumber[i] << " positioned in " << glogM.name() << " at (0,0,"
198  << cms::convert2mm(zi + 0.5 * layerThick[i]) << ") with no rotation";
199 #endif
200  ++copyNumber[i];
201  zi += layerThick[i];
202  thickTot += layerThick[i];
203  }
204  if (std::abs(thickTot - thick) >= tol) {
205  if (thickTot > thick) {
206  edm::LogError("HGCalGeom") << "Thickness of the partition " << cms::convert2mm(thick) << " is smaller than "
207  << cms::convert2mm(thickTot)
208  << ": thickness of all its components **** ERROR ****";
209  } else {
210  edm::LogWarning("HGCalGeom") << "Thickness of the partition " << cms::convert2mm(thick)
211  << " does not match with " << cms::convert2mm(thickTot) << " of the components";
212  }
213  }
214  }
215  }
216 
217  return cms::s_executed;
218 }
219 
220 // first argument is the type from the xml file
221 DECLARE_DDCMS_DETELEMENT(DDCMS_hgcal_DDHGCalWaferPartialRotated, algorithm)
Log< level::Info, true > LogVerbatim
static long algorithm(dd4hep::Detector &, cms::DDParsingContext &ctxt, xml_h e)
constexpr NumType convert2mm(NumType length)
Definition: DDutils.h:7
Log< level::Error, false > LogError
dd4hep::Material material(const std::string &name) const
Definition: DDNamespace.cc:166
static std::vector< std::pair< double, double > > waferXY(const int &part, const int &orient, const int &zside, const double &waferSize, const double &offset, const double &xpos, const double &ypos, const bool &v17)
U second(std::pair< T, U > const &p)
#define DECLARE_DDCMS_DETELEMENT(name, func)
Definition: DDPlugins.h:25
static std::pair< int32_t, int32_t > cellOrient(int32_t placementIndex)
Definition: HGCalCell.cc:242
static constexpr long s_executed
std::string_view name() const
Definition: DDNamespace.h:79
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
int32_t waferThick(const int32_t property)
dd4hep::Volume Volume
auto zw(V v) -> Vec2< typename std::remove_reference< decltype(v[0])>::type >
Definition: ExtVec.h:71
Log< level::Warning, false > LogWarning
dd4hep::Volume addVolumeNS(dd4hep::Volume vol) const
Definition: DDNamespace.cc:202
dd4hep::Solid addSolidNS(const std::string &name, dd4hep::Solid solid) const
Definition: DDNamespace.cc:292
std::string prepend(const std::string &) const
Definition: DDNamespace.cc:99