Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:15:55

0001 // SPDX-License-Identifier: LGPL-3.0-or-later
0002 // Copyright (C) 2022 - 2024, Nicolas Schmidt, Chun Yuen Tsang
0003 
0004 /** \addtogroup Trackers Trackers
0005  * \brief Type: **Endcap Tracker with TOF**.
0006  *
0007  * \ingroup trackers
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   xml_det_t x_det      = e;
0030   int det_id           = x_det.id();
0031   std::string det_name = x_det.nameStr();
0032   DetElement sdet(det_name, det_id);
0033   Material air    = description.material("Air");
0034   Material carbon = description.material("CarbonFiber");
0035   PlacedVolume pv;
0036 
0037   map<string, std::array<double, 2>> module_thicknesses;
0038 
0039   // Set detector type flag
0040   dd4hep::xml::setDetectorTypeFlag(x_det, sdet);
0041   auto& params = DD4hepDetectorHelper::ensureExtension<dd4hep::rec::VariantParameters>(sdet);
0042   // Add the volume boundary material if configured
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   float zPos = 0;
0054   // now build the envelope for the detector
0055   xml_comp_t x_layer  = x_det.child(_Unicode(layer));
0056   xml_comp_t envelope = x_layer.child(_Unicode(envelope), false);
0057   int lay_id          = x_layer.id();
0058   string l_nam        = x_layer.moduleStr();
0059   string lay_nam      = det_name + _toString(x_layer.id(), "_layer%d");
0060   Tube lay_tub(envelope.rmin(), envelope.rmax(), envelope.length() / 2.0);
0061   Volume lay_vol(lay_nam, lay_tub, air); // Create the layer envelope volume.
0062   zPos = envelope.zstart();
0063   Position lay_pos(0, 0, 0);
0064   lay_vol.setVisAttributes(description.visAttributes(x_layer.visStr()));
0065 
0066   DetElement lay_elt(sdet, lay_nam, lay_id);
0067 
0068   // the local coordinate systems of modules in dd4hep and acts differ
0069   // see http://acts.web.cern.ch/ACTS/latest/doc/group__DD4hepPlugins.html
0070   auto& layerParams =
0071       DD4hepDetectorHelper::ensureExtension<dd4hep::rec::VariantParameters>(lay_elt);
0072 
0073   for (xml_coll_t lmat(x_layer, _Unicode(layer_material)); lmat; ++lmat) {
0074     xml_comp_t x_layer_material = lmat;
0075     DD4hepDetectorHelper::xmlToProtoSurfaceMaterial(x_layer_material, layerParams,
0076                                                     "layer_material");
0077   }
0078 
0079   // dimensions of the modules (2x2 sensors)
0080   xml_comp_t x_modsz = x_det.child(_Unicode(modsize));
0081 
0082   double module_x       = x_modsz.length();
0083   double module_y       = x_modsz.width();
0084   double module_overlap = getAttrOrDefault(x_modsz, _Unicode(overlap), 0.); // x_modsz.overlap();
0085   double module_spacing = getAttrOrDefault(x_modsz, _Unicode(spacing), 0.); // x_modsz.overlap();
0086   double board_gap      = getAttrOrDefault(x_modsz, _Unicode(board_gap), 0.);
0087 
0088   //! Add support structure
0089   xml_comp_t x_supp          = x_det.child(_Unicode(support));
0090   xml_comp_t x_supp_envelope = x_supp.child(_Unicode(envelope), false);
0091 
0092   xml_comp_t x_modFrontLeft  = x_det.child(_Unicode(moduleFrontLeft));
0093   xml_comp_t x_modFrontRight = x_det.child(_Unicode(moduleFrontRight));
0094   xml_comp_t x_modBackLeft   = x_det.child(_Unicode(moduleBackLeft));
0095   xml_comp_t x_modBackRight  = x_det.child(_Unicode(moduleBackRight));
0096 
0097   xml_comp_t x_sensor_layout_front_left  = x_det.child(_Unicode(sensor_layout_front_left));
0098   xml_comp_t x_sensor_layout_back_left   = x_det.child(_Unicode(sensor_layout_back_left));
0099   xml_comp_t x_sensor_layout_front_right = x_det.child(_Unicode(sensor_layout_front_right));
0100   xml_comp_t x_sensor_layout_back_right  = x_det.child(_Unicode(sensor_layout_back_right));
0101 
0102   for (bool left : std::vector<bool>{true, false}) {
0103     for (bool front : std::vector<bool>{true, false}) {
0104       int module   = (front << 1) + left;
0105       float ycoord = envelope.rmax() -
0106                      module_y / 2.; // y-center-coord of the top sensor. Start from the top row
0107       int iy                     = 0;
0108       xml_comp_t x_sensor_layout = x_sensor_layout_front_left;
0109       xml_comp_t x_modCurr       = x_modFrontLeft;
0110 
0111       if (front) {
0112         if (left) {
0113           x_sensor_layout = x_sensor_layout_front_left;
0114           x_modCurr       = x_modFrontLeft;
0115         } else {
0116           x_sensor_layout = x_sensor_layout_front_right;
0117           x_modCurr       = x_modFrontRight;
0118         }
0119       } else {
0120         if (left) {
0121           x_sensor_layout = x_sensor_layout_back_left;
0122           x_modCurr       = x_modBackLeft;
0123         } else {
0124           x_sensor_layout = x_sensor_layout_back_right;
0125           x_modCurr       = x_modBackRight;
0126         }
0127       }
0128 
0129       double total_thickness = 0;
0130       // Compute module total thickness from components
0131       xml_coll_t ci(x_modCurr, _U(module_component));
0132 
0133       for (ci.reset(), total_thickness = 0.0; ci; ++ci) {
0134         xml_comp_t x_comp    = ci;
0135         bool keep_same_layer = getAttrOrDefault<bool>(x_comp, _Unicode(keep_layer), false);
0136         if (!keep_same_layer)
0137           total_thickness += x_comp.thickness();
0138       }
0139 
0140       for (xml_coll_t lrow(x_sensor_layout, _Unicode(row)); lrow; ++lrow) {
0141         xml_comp_t x_row = lrow;
0142         double deadspace = getAttrOrDefault<double>(x_row, _Unicode(deadspace), 0);
0143         if (deadspace > 0) {
0144           ycoord -= deadspace;
0145           continue;
0146         }
0147         double x_offset = getAttrOrDefault<double>(x_row, _Unicode(x_offset), 0);
0148         int nsensors    = getAttrOrDefault<int>(x_row, _Unicode(nsensors), 0);
0149 
0150         // find the sensor id that corrsponds to the rightmost sensor in a board
0151         // we need to know where to apply additional spaces between neighboring board
0152         std::unordered_set<int> sensors_id_board_edge;
0153         int curr_ix = nsensors; // the first sensor to the right of center has ix of nsensors
0154         for (xml_coll_t lboard(x_row, _Unicode(board)); lboard; ++lboard) {
0155           xml_comp_t x_board = lboard;
0156           int nboard_sensors = getAttrOrDefault<int>(x_board, _Unicode(nsensors), 1);
0157           curr_ix += nboard_sensors;
0158           sensors_id_board_edge.insert(curr_ix);
0159           sensors_id_board_edge.insert(2 * nsensors - curr_ix -
0160                                        1); // reflected to sensor id on the left
0161         }
0162 
0163         double accum_xoffset = x_offset;
0164         for (int ix = (left ? nsensors - 1 : nsensors); (ix >= 0) && (ix < 2 * nsensors);
0165              ix     = ix + (left ? -1 : 1)) {
0166           // add board spacing
0167           if (sensors_id_board_edge.find(ix) != sensors_id_board_edge.end())
0168             accum_xoffset = accum_xoffset + board_gap;
0169 
0170           // there is a hole in the middle, with radius = x_offset
0171           float xcoord = (ix - nsensors + 0.5) * (module_x + module_spacing) +
0172                          +(left ? -accum_xoffset : accum_xoffset);
0173           //! Note the module ordering is different for front and back side
0174 
0175           double module_z = x_supp_envelope.length() / 2.0 + total_thickness / 2;
0176           if (front)
0177             module_z *= -1;
0178 
0179           string module_name = Form("module%d_%d_%d", module, ix, iy);
0180           DetElement mod_elt(lay_elt, module_name, module);
0181 
0182           // create individual sensor layers here
0183           string m_nam = Form("EndcapTOF_Module%d_%d_%d", module, ix, iy);
0184 
0185           int ncomponents = 0;
0186           // the module assembly volume
0187           Assembly m_vol(m_nam);
0188           m_vol.setVisAttributes(description.visAttributes(x_modCurr.visStr()));
0189 
0190           double thickness_so_far     = 0.0;
0191           double thickness_sum        = -total_thickness / 2.0;
0192           double thickness_carbonsupp = 0.0;
0193           int sensitive_id            = 0;
0194           for (xml_coll_t mci(x_modCurr, _U(module_component)); mci; ++mci, ++ncomponents) {
0195             xml_comp_t x_comp  = mci;
0196             xml_comp_t x_pos   = x_comp.position(false);
0197             xml_comp_t x_rot   = x_comp.rotation(false);
0198             const string c_nam = Form("component_%d_%d_%d", module, ix, iy);
0199 
0200             Box c_box(x_comp.width() / 2, x_comp.length() / 2, x_comp.thickness() / 2);
0201             Volume c_vol(c_nam, c_box, description.material(x_comp.materialStr()));
0202             if (x_comp.materialStr() == "CarbonFiber") {
0203               thickness_carbonsupp = x_comp.thickness();
0204             }
0205             // Utility variable for the relative z-offset based off the previous components
0206             const double zoff = thickness_sum + x_comp.thickness() / 2.0;
0207             if (x_pos && x_rot) {
0208               Position c_pos(x_pos.x(0), x_pos.y(0), x_pos.z(0) + zoff);
0209               RotationZYX c_rot(x_rot.z(0), x_rot.y(0), x_rot.x(0));
0210               pv = m_vol.placeVolume(c_vol, Transform3D(c_rot, c_pos));
0211             } else if (x_rot) {
0212               Position c_pos(0, 0, zoff);
0213               pv = m_vol.placeVolume(
0214                   c_vol, Transform3D(RotationZYX(x_rot.z(0), x_rot.y(0), x_rot.x(0)), c_pos));
0215             } else if (x_pos) {
0216               pv = m_vol.placeVolume(c_vol, Position(x_pos.x(0), x_pos.y(0), x_pos.z(0) + zoff));
0217             } else {
0218               pv = m_vol.placeVolume(c_vol, Position(0, 0, zoff));
0219             }
0220             c_vol.setRegion(description, x_comp.regionStr());
0221             c_vol.setLimitSet(description, x_comp.limitsStr());
0222             c_vol.setVisAttributes(description, x_comp.visStr());
0223             if (x_comp.isSensitive()) {
0224               pv.addPhysVolID("idx", ix);
0225               pv.addPhysVolID("idy", iy);
0226               pv.addPhysVolID("ids", sensitive_id);
0227               ++sensitive_id;
0228 
0229               c_vol.setSensitiveDetector(sens);
0230               module_thicknesses[m_nam] = {thickness_so_far + x_comp.thickness() / 2.0,
0231                                            total_thickness - thickness_so_far -
0232                                                x_comp.thickness() / 2.0};
0233 
0234               // -------- create a measurement plane for the tracking surface attched to the sensitive volume -----
0235               Vector3D u(-1., 0., 0.);
0236               Vector3D v(0., -1., 0.);
0237               Vector3D n(0., 0., 1.);
0238 
0239               // compute the inner and outer thicknesses that need to be assigned to the tracking surface
0240               // depending on wether the support is above or below the sensor
0241               double inner_thickness = module_thicknesses[m_nam][0];
0242               double outer_thickness = module_thicknesses[m_nam][1];
0243 
0244               SurfaceType type(SurfaceType::Sensitive);
0245 
0246               VolPlane surf(c_vol, type, inner_thickness, outer_thickness, u, v, n);
0247 
0248               DetElement comp_de(mod_elt,
0249                                  std::string("de_") + pv.volume().name() + "_" +
0250                                      std::to_string(sensitive_id),
0251                                  module);
0252               comp_de.setPlacement(pv);
0253 
0254               auto& comp_de_params =
0255                   DD4hepDetectorHelper::ensureExtension<dd4hep::rec::VariantParameters>(comp_de);
0256               comp_de_params.set<string>("axis_definitions", "XYZ");
0257               volSurfaceList(comp_de)->push_back(surf);
0258 
0259               //--------------------------------------------
0260             }
0261             bool keep_same_layer = getAttrOrDefault<bool>(x_comp, _Unicode(keep_layer), false);
0262             if (!keep_same_layer) {
0263               thickness_sum += x_comp.thickness();
0264               thickness_so_far += x_comp.thickness();
0265               // apply relative offsets in z-position used to stack components side-by-side
0266               if (x_pos) {
0267                 thickness_sum += x_pos.z(0);
0268                 thickness_so_far += x_pos.z(0);
0269               }
0270             }
0271           }
0272 
0273           if (front) {
0274             // only draw support bar on one side
0275             // if you draw on both sides, they may overlap
0276             const string suppb_nam =
0277                 Form("suppbar_%d_%d", ix, iy); //_toString(ncomponents, "component%d");
0278             Box suppb_box((module_x + module_spacing) / 2, thickness_carbonsupp / 2,
0279                           x_supp_envelope.length() / 2);
0280             Volume suppb_vol(suppb_nam, suppb_box, carbon);
0281             Transform3D trsupp(RotationZYX(0, 0, 0),
0282                                Position(xcoord, ycoord + module_y / 2 - module_overlap / 2, 0));
0283             suppb_vol.setVisAttributes(description, "AnlGray");
0284 
0285             pv = lay_vol.placeVolume(suppb_vol, trsupp);
0286           }
0287           // module built!
0288 
0289           Transform3D tr(RotationZYX(M_PI / 2, 0, 0), Position(xcoord, ycoord, module_z));
0290 
0291           pv = lay_vol.placeVolume(m_vol, tr);
0292           pv.addPhysVolID("module", module);
0293           mod_elt.setPlacement(pv);
0294         }
0295         ycoord -= (module_y - module_overlap);
0296         ++iy;
0297       }
0298     }
0299   }
0300   // Create the PhysicalVolume for the layer.
0301   pv = assembly.placeVolume(lay_vol, lay_pos); // Place layer in mother
0302   pv.addPhysVolID("layer", lay_id);            // Set the layer ID.
0303   lay_elt.setAttributes(description, lay_vol, x_layer.regionStr(), x_layer.limitsStr(),
0304                         x_layer.visStr());
0305   lay_elt.setPlacement(pv);
0306 
0307   pv = description.pickMotherVolume(sdet).placeVolume(assembly, Position(0, 0, zPos));
0308   pv.addPhysVolID("system", det_id);
0309   sdet.setPlacement(pv);
0310 
0311   return sdet;
0312 }
0313 
0314 //@}
0315 // clang-format off
0316 DECLARE_DETELEMENT(epic_TOFEndcap, create_detector)