Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:13:08

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