Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 09:25:31

0001 // This file is part of the ACTS project.
0002 //
0003 // Copyright (C) 2016 CERN for the benefit of the ACTS project
0004 //
0005 // This Source Code Form is subject to the terms of the Mozilla Public
0006 // License, v. 2.0. If a copy of the MPL was not distributed with this
0007 // file, You can obtain one at https://mozilla.org/MPL/2.0/.
0008 
0009 #include "DD4hepTestsHelper.hpp"
0010 
0011 #include "Acts/Definitions/Units.hpp"
0012 #include "Acts/Surfaces/Surface.hpp"
0013 #include "ActsPlugins/DD4hep/DD4hepConversionHelpers.hpp"
0014 
0015 #include <DD4hep/DD4hepUnits.h>
0016 
0017 using namespace Acts;
0018 using namespace ActsPlugins;
0019 
0020 namespace ActsTests {
0021 
0022 void DD4hepTestsHelper::decodeBinning(
0023     dd4hep::rec::VariantParameters& variantParams, const xml_comp_t& xmlBinning,
0024     const std::string& bname, const std::vector<std::string>& bvals) {
0025   // Set the surface binninng parameter to true
0026   variantParams.set<int>(bname + "_dim", bvals.size());
0027   for (const auto& bv : bvals) {
0028     // Gather the number of bins, 0 indicates variable binning
0029     int nBins = getAttrValueOr<int>(xmlBinning, "n" + bv, 0);
0030     // Gather the bin expansion parameter, expansion of 0 is default
0031     int nExpansion = getAttrValueOr<int>(xmlBinning, bv + "expansion", 0);
0032     // Auto-range detection
0033     bool autoRange = getAttrValueOr<bool>(xmlBinning, bv + "autorange", false);
0034     variantParams.set<bool>(bname + "_" + bv + "_autorange", autoRange);
0035     variantParams.set<int>(bname + "_" + bv + "_exp", nExpansion);
0036     // Equidistant binning detected
0037     if (nBins > 0) {
0038       // Set the type identification
0039       variantParams.set<std::string>(bname + "_" + bv + "_type", "equidistant");
0040       // Set the number of bins
0041       variantParams.set<int>(bname + "_" + bv + "_n", nBins);
0042       // Set min/max parameter
0043       if (!autoRange) {
0044         variantParams.set<double>(
0045             bname + "_" + bv + "_min",
0046             xmlBinning.attr<double>((bv + "min").c_str()));
0047         variantParams.set<double>(
0048             bname + "_" + bv + "_max",
0049             xmlBinning.attr<double>((bv + "max").c_str()));
0050       }
0051     } else {
0052       // Variable binning detected
0053       variantParams.set<std::string>(bname + "_" + bv + "_type", "variable");
0054       // Get the number of bins explicitly
0055       auto boundaries =
0056           xmlBinning.attr<std::string>((bv + "boundaries").c_str());
0057       std::string del = ",";
0058       auto end = boundaries.find(del);
0059       int ib = 0;
0060       // Unit conversion
0061       double unitScalar = 1.;
0062       if (bv != "phi") {
0063         unitScalar = UnitConstants::mm / dd4hep::millimeter;
0064       }
0065       // Split and convert
0066       while (end != std::string::npos) {
0067         double bR = unitScalar * dd4hep::_toFloat(boundaries.substr(0, end));
0068         variantParams.set<double>(
0069             bname + "_" + bv + "_b" + std::to_string(ib++), bR);
0070         boundaries.erase(boundaries.begin(), boundaries.begin() + end + 1);
0071         end = boundaries.find(del);
0072       }
0073       double bR = unitScalar * std::stod(boundaries.substr(0, end));
0074       variantParams.set<double>(bname + "_" + bv + "_b" + std::to_string(ib),
0075                                 bR);
0076       // The number of bins are needed to unpack the data
0077       variantParams.set<int>(bname + "_" + bv + "_n", ib);
0078     }
0079   }
0080 }
0081 
0082 dd4hep::Transform3D DD4hepTestsHelper::createTransform(
0083     const xml_comp_t& x_det_comp) {
0084   // Build the transform - center def
0085   double cx = getAttrValueOr<double>(x_det_comp, "cx", 0.);
0086   double cy = getAttrValueOr<double>(x_det_comp, "cy", 0.);
0087   double cz = getAttrValueOr<double>(x_det_comp, "cz", 0.);
0088 
0089   double xx = getAttrValueOr<double>(x_det_comp, "xx", 1.);
0090   double xy = getAttrValueOr<double>(x_det_comp, "xy", 0.);
0091   double xz = getAttrValueOr<double>(x_det_comp, "xz", 0.);
0092 
0093   double yx = getAttrValueOr<double>(x_det_comp, "yx", 0.);
0094   double yy = getAttrValueOr<double>(x_det_comp, "yy", 1.);
0095   double yz = getAttrValueOr<double>(x_det_comp, "yz", 0.);
0096 
0097   Position xAxis(xx, xy, xz);
0098   Position yAxis(yx, yy, yz);
0099   Position zAxis = xAxis.Cross(yAxis);
0100   double zx = zAxis.X();
0101   double zy = zAxis.Y();
0102   double zz = zAxis.Z();
0103 
0104   // Create the transform
0105   return Transform3D(xx, yx, zx, cx, xy, yy, zy, cy, xz, yz, zz, cz);
0106 }
0107 
0108 std::string DD4hepTestsHelper::transformToXML(const Transform3& tf,
0109                                               const std::array<int, 2u>& axes) {
0110   auto tr = tf.translation();
0111   auto rot = tf.rotation();
0112 
0113   std::stringstream sxml;
0114   sxml << "cx=\"" << tr[0u] << "*mm\" ";
0115   sxml << "cy=\"" << tr[1u] << "*mm\" ";
0116   sxml << "cz=\"" << tr[2u] << "*mm\" ";
0117 
0118   sxml << "xx=\"" << rot.col(axes[0u])[0u] << "\" ";
0119   sxml << "xy=\"" << rot.col(axes[0u])[1u] << "\" ";
0120   sxml << "xz=\"" << rot.col(axes[0u])[2u] << "\" ";
0121   sxml << "yx=\"" << rot.col(axes[1u])[0u] << "\" ";
0122   sxml << "yy=\"" << rot.col(axes[1u])[1u] << "\" ";
0123   sxml << "yz=\"" << rot.col(axes[1u])[2u] << "\" ";
0124 
0125   return sxml.str();
0126 }
0127 
0128 std::string DD4hepTestsHelper::surfaceToXML(const GeometryContext& gctx,
0129                                             const Surface& surface,
0130                                             const Transform3& ref) {
0131   // The xml to be translated
0132   std::stringstream sxml;
0133   auto boundValues = surface.bounds().values();
0134 
0135   std::array<int, 2u> axes = {0, 1};
0136   // Change/adapt the behavior
0137   switch (surface.bounds().type()) {
0138     case SurfaceBounds::eRectangle: {
0139       sxml << "<box ";
0140       double dx = (boundValues[2u] - boundValues[0u]);
0141       double dy = (boundValues[3u] - boundValues[1u]);
0142       double dz = 0.125;
0143       sxml << "dx=\"" << dx << "*mm\" ";
0144       sxml << "dy=\"" << dy << "*mm\" ";
0145       sxml << "dz=\"" << dz << "*mm\" ";
0146     }; break;
0147     case SurfaceBounds::eTrapezoid: {
0148       axes = {2, 0};
0149 
0150       sxml << "<trap ";
0151       double hxmin = boundValues[0u];
0152       double hxmax = boundValues[1u];
0153       double dy = 2 * boundValues[2u];
0154       double dz = 0.125;
0155       sxml << "x1=\"" << hxmin << "*mm\" ";
0156       sxml << "x2=\"" << hxmax << "*mm\" ";
0157       sxml << "dy=\"" << dy << "*mm\" ";
0158       sxml << "dz=\"" << dz << "*mm\" ";
0159     }; break;
0160     default:
0161       break;
0162   }
0163 
0164   // Unwind the placement you have already
0165   auto relTransform = ref * surface.transform(gctx);
0166   sxml << transformToXML(relTransform, axes);
0167   sxml << " material=\"Air\"";
0168   sxml << " sensitive=\"true\"/>";
0169   return sxml.str();
0170 }
0171 
0172 }  // namespace ActsTests