File indexing completed on 2025-07-12 07:56:07
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include "DD4hep/DetFactoryHelper.h"
0012 #include "DD4hep/Printout.h"
0013 #include "DD4hep/Shapes.h"
0014 #include "DD4hepDetectorHelper.h"
0015 #include "DDRec/DetectorData.h"
0016 #include "DDRec/Surface.h"
0017 #include "XML/Layering.h"
0018 #include "XML/Utilities.h"
0019 #include <array>
0020 #include <map>
0021 #include <unordered_set>
0022
0023 using namespace std;
0024 using namespace dd4hep;
0025 using namespace dd4hep::rec;
0026 using namespace dd4hep::detail;
0027
0028 static Ref_t create_detector(Detector& description, xml_h e, SensitiveDetector sens) {
0029 typedef vector<PlacedVolume> Placements;
0030 xml_det_t x_det = e;
0031 int det_id = x_det.id();
0032 std::string det_name = x_det.nameStr();
0033 DetElement sdet(det_name, det_id);
0034 Material air = description.material("Air");
0035 PlacedVolume pv;
0036
0037 map<string, std::array<double, 2>> module_thicknesses;
0038
0039
0040 dd4hep::xml::setDetectorTypeFlag(x_det, sdet);
0041 auto& params = DD4hepDetectorHelper::ensureExtension<dd4hep::rec::VariantParameters>(sdet);
0042
0043 for (xml_coll_t bmat(x_det, _Unicode(boundary_material)); bmat; ++bmat) {
0044 xml_comp_t x_boundary_material = bmat;
0045 DD4hepDetectorHelper::xmlToProtoSurfaceMaterial(x_boundary_material, params,
0046 "boundary_material");
0047 }
0048
0049 Assembly assembly(det_name);
0050 assembly.setVisAttributes(description.invisible());
0051 sens.setType("tracker");
0052
0053
0054 xml_comp_t x_modsz = x_det.child(_Unicode(modsize));
0055
0056 double module_x = x_modsz.length();
0057 double module_y = x_modsz.width();
0058 double module_overlap = getAttrOrDefault(x_modsz, _Unicode(overlap), 0.);
0059 double module_spacing = getAttrOrDefault(x_modsz, _Unicode(spacing), 0.);
0060 double board_gap = getAttrOrDefault(x_modsz, _Unicode(board_gap), 0.);
0061
0062 map<string, Assembly> modules;
0063 map<string, Placements> sensitives;
0064 map<string, std::vector<VolPlane>> volplane_surfaces;
0065 map<string, double> mod_thickness;
0066
0067 for (xml_coll_t mi(x_det, _U(module)); mi; ++mi) {
0068 xml_comp_t x_mod = mi;
0069 double total_thickness = 0;
0070
0071 for (xml_coll_t ci(x_mod, _U(module_component)); ci; ++ci) {
0072 xml_comp_t x_comp = ci;
0073 bool keep_same_layer = getAttrOrDefault<bool>(x_comp, _Unicode(keep_layer), false);
0074 if (!keep_same_layer)
0075 total_thickness += x_comp.thickness();
0076 }
0077
0078 double thickness_so_far = 0.0;
0079 int sensitive_id = 0;
0080 const std::string m_nam = x_mod.nameStr();
0081 mod_thickness[m_nam] = total_thickness;
0082
0083
0084 Assembly m_vol(m_nam);
0085 m_vol.setVisAttributes(description.visAttributes(x_mod.visStr()));
0086
0087 int ncomponents = 0;
0088 for (xml_coll_t mci(x_mod, _U(module_component)); mci; ++mci, ++ncomponents) {
0089 xml_comp_t x_comp = mci;
0090 xml_comp_t x_pos = x_comp.position(false);
0091 xml_comp_t x_rot = x_comp.rotation(false);
0092 const string c_nam = x_comp.nameStr();
0093
0094 Box c_box(x_comp.width() / 2, x_comp.length() / 2, x_comp.thickness() / 2);
0095 Volume c_vol(c_nam, c_box, description.material(x_comp.materialStr()));
0096
0097 const double zoff = thickness_so_far + x_comp.thickness() / 2.0;
0098 if (x_pos && x_rot) {
0099 Position c_pos(x_pos.x(0), x_pos.y(0), x_pos.z(0) + zoff);
0100 RotationZYX c_rot(x_rot.z(0), x_rot.y(0), x_rot.x(0));
0101 pv = m_vol.placeVolume(c_vol, Transform3D(c_rot, c_pos));
0102 } else if (x_rot) {
0103 Position c_pos(0, 0, zoff);
0104 pv = m_vol.placeVolume(c_vol,
0105 Transform3D(RotationZYX(x_rot.z(0), x_rot.y(0), x_rot.x(0)), c_pos));
0106 } else if (x_pos) {
0107 pv = m_vol.placeVolume(c_vol, Position(x_pos.x(0), x_pos.y(0), x_pos.z(0) + zoff));
0108 } else {
0109 pv = m_vol.placeVolume(c_vol, Position(0, 0, zoff));
0110 }
0111 c_vol.setRegion(description, x_comp.regionStr());
0112 c_vol.setLimitSet(description, x_comp.limitsStr());
0113 c_vol.setVisAttributes(description, x_comp.visStr());
0114 if (x_comp.isSensitive()) {
0115 pv.addPhysVolID("ids", sensitive_id);
0116 ++sensitive_id;
0117
0118 c_vol.setSensitiveDetector(sens);
0119 module_thicknesses[m_nam] = {thickness_so_far + x_comp.thickness() / 2.0,
0120 total_thickness - thickness_so_far - x_comp.thickness() / 2.0};
0121 sensitives[m_nam].push_back(pv);
0122
0123
0124 Vector3D u(-1., 0., 0.);
0125 Vector3D v(0., -1., 0.);
0126 Vector3D n(0., 0., 1.);
0127
0128
0129
0130 double inner_thickness = module_thicknesses[m_nam][0];
0131 double outer_thickness = module_thicknesses[m_nam][1];
0132
0133 SurfaceType type(SurfaceType::Sensitive);
0134
0135 VolPlane surf(c_vol, type, inner_thickness, outer_thickness, u, v, n);
0136 volplane_surfaces[m_nam].push_back(surf);
0137
0138
0139 }
0140 bool keep_same_layer = getAttrOrDefault<bool>(x_comp, _Unicode(keep_layer), false);
0141 if (!keep_same_layer) {
0142 thickness_so_far += x_comp.thickness();
0143
0144 if (x_pos) {
0145 thickness_so_far += x_pos.z(0);
0146 }
0147 }
0148 }
0149 modules[m_nam] = m_vol;
0150 }
0151
0152 int module = 0;
0153 for (xml_coll_t li(x_det, _U(layer)); li; ++li) {
0154 xml_comp_t x_layer = li;
0155 bool front = x_layer.attr<bool>(_Unicode(front));
0156
0157 const std::string locStr = x_layer.nameStr();
0158
0159
0160 xml_comp_t envelope = x_layer.child(_Unicode(envelope), false);
0161 int lay_id = x_layer.id();
0162 string lay_nam = det_name + _toString(lay_id, "_layer%d");
0163 double phimin = dd4hep::getAttrOrDefault<double>(envelope, _Unicode(phimin), 0.);
0164 double phimax = dd4hep::getAttrOrDefault<double>(envelope, _Unicode(phimax), 2 * M_PI);
0165 double xoffset = getAttrOrDefault<double>(envelope, _Unicode(xoffset), 0);
0166
0167
0168 double envelope_length = 0;
0169 for (xml_coll_t llayout(x_layer, _Unicode(layout)); llayout; ++llayout) {
0170 xml_comp_t x_layout = llayout;
0171 string m_nam = x_layout.moduleStr();
0172 envelope_length = std::max(envelope_length, mod_thickness[m_nam]);
0173 }
0174
0175 Tube lay_tub(envelope.rmin(), envelope.rmax(), envelope_length / 2.0, phimin, phimax);
0176 Volume lay_vol(lay_nam, lay_tub, air);
0177 Position lay_pos(xoffset, 0,
0178 envelope.zstart() + (front ? -0.5 * envelope_length : 0.5 * envelope_length));
0179 lay_vol.setVisAttributes(description.visAttributes(x_layer.visStr()));
0180
0181 DetElement lay_elt(sdet, lay_nam, lay_id);
0182
0183 pv = assembly.placeVolume(lay_vol, lay_pos);
0184 pv.addPhysVolID("layer", lay_id);
0185 lay_elt.setAttributes(description, lay_vol, x_layer.regionStr(), x_layer.limitsStr(),
0186 x_layer.visStr());
0187 lay_elt.setPlacement(pv);
0188
0189
0190
0191 auto& layerParams =
0192 DD4hepDetectorHelper::ensureExtension<dd4hep::rec::VariantParameters>(lay_elt);
0193
0194 for (xml_coll_t lmat(x_layer, _Unicode(layer_material)); lmat; ++lmat) {
0195 xml_comp_t x_layer_material = lmat;
0196 DD4hepDetectorHelper::xmlToProtoSurfaceMaterial(x_layer_material, layerParams,
0197 "layer_material");
0198 }
0199
0200 for (xml_coll_t llayout(x_layer, _Unicode(layout)); llayout; ++llayout, ++module) {
0201 xml_comp_t x_layout = llayout;
0202 bool left = x_layout.attr<bool>(_Unicode(left));
0203 string m_nam = x_layout.moduleStr();
0204 Volume m_vol = modules[m_nam];
0205 double total_thickness = mod_thickness[m_nam];
0206 Placements& sensVols = sensitives[m_nam];
0207
0208 float ycoord = envelope.rmax() -
0209 module_y / 2.;
0210 int iy = 0;
0211
0212 for (xml_coll_t lrow(x_layout, _Unicode(row)); lrow; ++lrow) {
0213 xml_comp_t x_row = lrow;
0214 double deadspace = getAttrOrDefault<double>(x_row, _Unicode(deadspace), 0);
0215 if (deadspace > 0) {
0216 ycoord -= deadspace;
0217 continue;
0218 }
0219 double x_offset = getAttrOrDefault<double>(x_row, _Unicode(x_offset), 0);
0220 int nsensors = getAttrOrDefault<int>(x_row, _Unicode(nsensors), 0);
0221
0222
0223
0224 std::unordered_set<int> sensors_id_board_edge;
0225 int curr_ix = nsensors;
0226 for (xml_coll_t lboard(x_row, _Unicode(board)); lboard; ++lboard) {
0227 xml_comp_t x_board = lboard;
0228 int nboard_sensors = getAttrOrDefault<int>(x_board, _Unicode(nsensors), 1);
0229 curr_ix += nboard_sensors;
0230 sensors_id_board_edge.insert(curr_ix);
0231 sensors_id_board_edge.insert(2 * nsensors - curr_ix -
0232 1);
0233 }
0234
0235 double accum_xoffset = x_offset;
0236
0237 for (int ix = (left ? nsensors - 1 : nsensors); (ix >= 0) && (ix < 2 * nsensors);
0238 ix = ix + (left ? -1 : 1)) {
0239
0240 if (sensors_id_board_edge.find(ix) != sensors_id_board_edge.end())
0241 accum_xoffset = accum_xoffset + board_gap;
0242
0243
0244 float xcoord = (ix - nsensors + 0.5) * (module_x + module_spacing) +
0245 +(left ? -accum_xoffset : accum_xoffset);
0246
0247
0248 double module_z = -0.5 * envelope_length;
0249 if (front)
0250 module_z = 0.5 * envelope_length - total_thickness;
0251
0252
0253 Transform3D tr(RotationZYX(M_PI / 2, 0, 0), Position(xcoord, ycoord, module_z));
0254
0255 pv = lay_vol.placeVolume(m_vol, tr);
0256 pv.addPhysVolID("idx", ix);
0257 pv.addPhysVolID("idy", iy);
0258 pv.addPhysVolID("module", module);
0259
0260 string comp_nam = Form("%s_%s_ix%d_iy%d", lay_nam.c_str(), m_nam.c_str(), ix, iy);
0261 DetElement comp_elt(lay_elt, comp_nam, det_id);
0262 comp_elt.setPlacement(pv);
0263
0264 for (size_t ic = 0; ic < sensVols.size(); ++ic) {
0265 PlacedVolume sens_pv = sensVols[ic];
0266 DetElement sensor_elt(comp_elt, sens_pv.volume().name(), module);
0267 sensor_elt.setPlacement(sens_pv);
0268 auto& sensor_elt_params =
0269 DD4hepDetectorHelper::ensureExtension<dd4hep::rec::VariantParameters>(sensor_elt);
0270 sensor_elt_params.set<string>("axis_definitions", "XYZ");
0271 volSurfaceList(sensor_elt)->push_back(volplane_surfaces[m_nam][ic]);
0272 }
0273 }
0274 ycoord -= (module_y - module_overlap);
0275 ++iy;
0276 }
0277 }
0278 }
0279 pv = description.pickMotherVolume(sdet).placeVolume(assembly, Position(0, 0, 0));
0280 pv.addPhysVolID("system", det_id);
0281 sdet.setPlacement(pv);
0282
0283 return sdet;
0284 }
0285
0286 DECLARE_DETELEMENT(epic_TOFEndcap, create_detector)