Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-26 09:25:58

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 <boost/test/unit_test.hpp>
0010 
0011 #include "Acts/Definitions/Algebra.hpp"
0012 #include "Acts/Geometry/GeometryContext.hpp"
0013 #include "Acts/Surfaces/Surface.hpp"
0014 #include "Acts/Utilities/Logger.hpp"
0015 #include "ActsPlugins/DD4hep/BlueprintBuilder.hpp"
0016 #include "ActsPlugins/DD4hep/DD4hepDetectorElement.hpp"
0017 #include "ActsPlugins/Root/TGeoAxes.hpp"
0018 #include "ActsTests/CommonHelpers/FloatComparisons.hpp"
0019 
0020 #include <fstream>
0021 #include <iostream>
0022 #include <sstream>
0023 #include <string>
0024 #include <vector>
0025 
0026 #include <DD4hep/DetElement.h>
0027 #include <DD4hep/Detector.h>
0028 
0029 #include "DD4hep/DetFactoryHelper.h"
0030 #include "XML/Utilities.h"
0031 #include "XMLFragments.hpp"
0032 
0033 using namespace Acts;
0034 using namespace ActsPlugins;
0035 
0036 auto tContext = GeometryContext::dangerouslyDefaultConstruct();
0037 
0038 const char* cylinder_xml =
0039     R""""(
0040     <detectors>
0041         <detector id="1" name="Cylinder" type="Cylinder">
0042             <type_flags type="DetType_TRACKER"/>
0043             <tubs name="Cylinder" rmin="10*cm" rmax="11*cm" dz="100*cm" material="Vacuum" sensitive="true"/>
0044         </detector>
0045     </detectors>
0046 )"""";
0047 
0048 const char* sectoral_cylinder_xml =
0049     R""""(
0050     <detectors>
0051         <detector id="1" name="SectoralCylinder" type="Cylinder">
0052             <type_flags type="DetType_TRACKER"/>
0053             <tubs name="Cylinder" rmin="10*cm" rmax="11*cm" dz="100*cm" phimin="0.1*rad" phimax="0.8*rad" material="Vacuum" sensitive="true"/>
0054         </detector>
0055     </detectors>
0056 )"""";
0057 
0058 const char* disc_xml =
0059     R""""(
0060     <detectors>
0061         <detector id="1" name="Disc" type="Disc">
0062             <type_flags type="DetType_TRACKER"/>
0063             <tubs name="Disc" rmin="10*cm" rmax="90*cm" dz="1*cm" material="Vacuum" sensitive="true"/>
0064         </detector>
0065     </detectors>
0066 )"""";
0067 
0068 const char* sectoral_disc_xml =
0069     R""""(
0070     <detectors>
0071         <detector id="1" name="SectoralDisc" type="Disc">
0072             <type_flags type="DetType_TRACKER"/>
0073             <tubs name="Disc" rmin="10*cm" rmax="90*cm" dz="1*cm" phimin="0.*rad" phimax="1.5*rad" material="Vacuum" sensitive="true"/>
0074         </detector>
0075     </detectors>
0076 )"""";
0077 
0078 const char* rectangle_xml =
0079     R""""(
0080     <detectors>
0081         <detector id="1" name="Rectangle" type="Rectangle">
0082             <type_flags type="DetType_TRACKER"/>
0083             <box name="Rectangle" dx="10*cm" dy="90*cm" dz="0.1*cm" cx="1.*cm" cy="2.*cm" cz="3.*cm" material="Vacuum" sensitive="true"/>
0084         </detector>
0085     </detectors>
0086 )"""";
0087 
0088 const char* trapezoid_xml =
0089     R""""(
0090     <detectors>
0091         <detector id="1" name="Trapezoid" type="Trapezoid">
0092             <type_flags type="DetType_TRACKER"/>
0093             <trap name="Trapezoid" x1="10*cm" x2="20*cm" dy="30*cm" dz="0.1*cm" cx="2.*cm" cy="3.*cm" cz="4.*cm" material="Vacuum" sensitive="true"/>
0094         </detector>
0095     </detectors>
0096 )"""";
0097 
0098 namespace ActsTests {
0099 
0100 BOOST_AUTO_TEST_SUITE(DD4hepSuite)
0101 
0102 BOOST_AUTO_TEST_CASE(DD4hepPluginDetectorElementCylinder) {
0103   std::ofstream cxml;
0104   cxml.open("Cylinder.xml");
0105   cxml << head_xml;
0106   cxml << cylinder_xml;
0107   cxml << end_xml;
0108   cxml.close();
0109 
0110   auto lcdd = &(dd4hep::Detector::getInstance());
0111   lcdd->fromCompact("Cylinder.xml");
0112   lcdd->volumeManager();
0113   lcdd->apply("DD4hepVolumeManager", 0, nullptr);
0114 
0115   auto world = lcdd->world();
0116 
0117   std::shared_ptr<ActsPlugins::DD4hepDetectorElement> cylindricalElement =
0118       nullptr;
0119   for (const auto& [chn, child] : world.children()) {
0120     cylindricalElement = std::make_shared<ActsPlugins::DD4hepDetectorElement>(
0121         child, TGeoAxes{"XYZ"}, 10.);
0122   }
0123 
0124   BOOST_REQUIRE_NE(cylindricalElement, nullptr);
0125 
0126   const auto& surface = cylindricalElement->surface();
0127   BOOST_CHECK_EQUAL(surface.type(), Surface::SurfaceType::Cylinder);
0128   BOOST_CHECK(surface.localToGlobalTransform(tContext).isApprox(
0129       Transform3::Identity()));
0130   auto boundValues = surface.bounds().values();
0131   CHECK_CLOSE_ABS(boundValues[0u], 105., 1e-10);
0132   CHECK_CLOSE_ABS(boundValues[1u], 1000., 1e-10);
0133   lcdd->destroyInstance();
0134 }
0135 
0136 BOOST_AUTO_TEST_CASE(DD4hepPluginDetectorElementSectoralCylinder) {
0137   std::ofstream cxml;
0138   cxml.open("SectoralCylinder.xml");
0139   cxml << head_xml;
0140   cxml << sectoral_cylinder_xml;
0141   cxml << end_xml;
0142   cxml.close();
0143 
0144   auto lcdd = &(dd4hep::Detector::getInstance());
0145   lcdd->fromCompact("SectoralCylinder.xml");
0146   lcdd->volumeManager();
0147   lcdd->apply("DD4hepVolumeManager", 0, nullptr);
0148 
0149   auto world = lcdd->world();
0150 
0151   std::shared_ptr<ActsPlugins::DD4hepDetectorElement> cylindricalElement =
0152       nullptr;
0153   for (const auto& [chn, child] : world.children()) {
0154     cylindricalElement = std::make_shared<ActsPlugins::DD4hepDetectorElement>(
0155         child, TGeoAxes{"XYZ"}, 10.);
0156   }
0157 
0158   BOOST_REQUIRE_NE(cylindricalElement, nullptr);
0159 
0160   const auto& surface = cylindricalElement->surface();
0161   BOOST_CHECK_EQUAL(surface.type(), Surface::SurfaceType::Cylinder);
0162   BOOST_CHECK(surface.localToGlobalTransform(tContext).isApprox(
0163       Transform3::Identity()));
0164   auto boundValues = surface.bounds().values();
0165   CHECK_CLOSE_ABS(boundValues[0u], 105., 1e-10);
0166   CHECK_CLOSE_ABS(boundValues[1u], 1000., 1e-10);
0167   CHECK_CLOSE_ABS(boundValues[2u], 0.35, 1e-10);
0168   CHECK_CLOSE_ABS(boundValues[3u], 0.45, 1e-10);
0169   lcdd->destroyInstance();
0170 }
0171 
0172 BOOST_AUTO_TEST_CASE(DD4hepPluginDetectorElementDisc) {
0173   std::ofstream cxml;
0174   cxml.open("Disc.xml");
0175   cxml << head_xml;
0176   cxml << disc_xml;
0177   cxml << end_xml;
0178   cxml.close();
0179 
0180   auto lcdd = &(dd4hep::Detector::getInstance());
0181   lcdd->fromCompact("Disc.xml");
0182   lcdd->volumeManager();
0183   lcdd->apply("DD4hepVolumeManager", 0, nullptr);
0184 
0185   auto world = lcdd->world();
0186 
0187   std::shared_ptr<ActsPlugins::DD4hepDetectorElement> discElement = nullptr;
0188   for (const auto& [chn, child] : world.children()) {
0189     discElement = std::make_shared<ActsPlugins::DD4hepDetectorElement>(
0190         child, TGeoAxes{"XYZ"}, 10.);
0191   }
0192 
0193   BOOST_REQUIRE_NE(discElement, nullptr);
0194 
0195   const auto& surface = discElement->surface();
0196   BOOST_CHECK_EQUAL(surface.type(), Surface::SurfaceType::Disc);
0197   BOOST_CHECK(surface.localToGlobalTransform(tContext).isApprox(
0198       Transform3::Identity()));
0199   auto boundValues = surface.bounds().values();
0200   CHECK_CLOSE_ABS(boundValues[0u], 100., 1e-10);
0201   CHECK_CLOSE_ABS(boundValues[1u], 900., 1e-10);
0202   lcdd->destroyInstance();
0203 }
0204 
0205 BOOST_AUTO_TEST_CASE(DD4hepPluginDetectorElementSectoralDisc) {
0206   std::ofstream cxml;
0207   cxml.open("SectoralDisc.xml");
0208   cxml << head_xml;
0209   cxml << sectoral_disc_xml;
0210   cxml << end_xml;
0211   cxml.close();
0212 
0213   auto lcdd = &(dd4hep::Detector::getInstance());
0214   lcdd->fromCompact("SectoralDisc.xml");
0215   lcdd->volumeManager();
0216   lcdd->apply("DD4hepVolumeManager", 0, nullptr);
0217 
0218   auto world = lcdd->world();
0219 
0220   std::shared_ptr<ActsPlugins::DD4hepDetectorElement> discElement = nullptr;
0221   for (const auto& [chn, child] : world.children()) {
0222     discElement = std::make_shared<ActsPlugins::DD4hepDetectorElement>(
0223         child, TGeoAxes{"XYZ"}, 10.);
0224   }
0225 
0226   BOOST_REQUIRE_NE(discElement, nullptr);
0227 
0228   const auto& surface = discElement->surface();
0229   BOOST_CHECK_EQUAL(surface.type(), Surface::SurfaceType::Disc);
0230   BOOST_CHECK(surface.localToGlobalTransform(tContext).isApprox(
0231       Transform3::Identity()));
0232   auto boundValues = surface.bounds().values();
0233 
0234   CHECK_CLOSE_ABS(boundValues[0u], 100., 1e-10);
0235   CHECK_CLOSE_ABS(boundValues[1u], 900., 1e-10);
0236   CHECK_CLOSE_ABS(boundValues[2u], 0.75, 1e-10);
0237   CHECK_CLOSE_ABS(boundValues[3u], 0.75, 1e-10);
0238   lcdd->destroyInstance();
0239 }
0240 
0241 BOOST_AUTO_TEST_CASE(DD4hepPluginDetectorElementRectangle) {
0242   std::ofstream cxml;
0243   cxml.open("Rectangle.xml");
0244   cxml << head_xml;
0245   cxml << rectangle_xml;
0246   cxml << end_xml;
0247   cxml.close();
0248 
0249   auto lcdd = &(dd4hep::Detector::getInstance());
0250   lcdd->fromCompact("Rectangle.xml");
0251   lcdd->volumeManager();
0252   lcdd->apply("DD4hepVolumeManager", 0, nullptr);
0253 
0254   auto world = lcdd->world();
0255 
0256   std::shared_ptr<ActsPlugins::DD4hepDetectorElement> rectangleElement =
0257       nullptr;
0258   for (const auto& [chn, child] : world.children()) {
0259     rectangleElement = std::make_shared<ActsPlugins::DD4hepDetectorElement>(
0260         child, TGeoAxes{"XYZ"}, 10.);
0261   }
0262 
0263   BOOST_REQUIRE_NE(rectangleElement, nullptr);
0264 
0265   Surface& surface = rectangleElement->surface();
0266   surface.assignGeometryId(GeometryIdentifier().withVolume(1).withLayer(2));
0267   BOOST_CHECK_EQUAL(surface.type(), Surface::SurfaceType::Plane);
0268 
0269   auto sTransform = surface.localToGlobalTransform(tContext);
0270   BOOST_CHECK(sTransform.translation().isApprox(Vector3(10., 20., 30.)));
0271 
0272   const auto& sBounds = surface.bounds();
0273   BOOST_CHECK_EQUAL(sBounds.type(), SurfaceBounds::BoundsType::eRectangle);
0274 
0275   auto boundValues = sBounds.values();
0276 
0277   CHECK_CLOSE_ABS(boundValues[0u], -50., 1e-10);
0278   CHECK_CLOSE_ABS(boundValues[1u], -450., 1e-10);
0279   CHECK_CLOSE_ABS(boundValues[2u], 50, 1e-10);
0280   CHECK_CLOSE_ABS(boundValues[3u], 450, 1e-10);
0281 
0282   // memory cleanup
0283   lcdd->destroyInstance();
0284 }
0285 
0286 BOOST_AUTO_TEST_CASE(DD4hepPluginDetectorElementTrapezoid) {
0287   std::ofstream cxml;
0288   cxml.open("Trapezoid.xml");
0289   cxml << head_xml;
0290   cxml << trapezoid_xml;
0291   cxml << end_xml;
0292   cxml.close();
0293 
0294   auto lcdd = &(dd4hep::Detector::getInstance());
0295   lcdd->fromCompact("Trapezoid.xml");
0296   lcdd->volumeManager();
0297   lcdd->apply("DD4hepVolumeManager", 0, nullptr);
0298 
0299   auto world = lcdd->world();
0300 
0301   std::shared_ptr<ActsPlugins::DD4hepDetectorElement> trapezoidElement =
0302       nullptr;
0303   for (const auto& [chn, child] : world.children()) {
0304     trapezoidElement = std::make_shared<ActsPlugins::DD4hepDetectorElement>(
0305         child, TGeoAxes("xZy"), 10.);
0306   }
0307 
0308   BOOST_REQUIRE_NE(trapezoidElement, nullptr);
0309 
0310   const auto& surface = trapezoidElement->surface();
0311   BOOST_CHECK_EQUAL(surface.type(), Surface::SurfaceType::Plane);
0312 
0313   auto sTransform = surface.localToGlobalTransform(tContext);
0314   BOOST_CHECK(sTransform.translation().isApprox(Vector3(20., 30., 40.)));
0315 
0316   const auto& sBounds = surface.bounds();
0317   BOOST_CHECK_EQUAL(sBounds.type(), SurfaceBounds::BoundsType::eTrapezoid);
0318 
0319   auto boundValues = sBounds.values();
0320   CHECK_CLOSE_ABS(boundValues[0u], 100., 1e-10);
0321   CHECK_CLOSE_ABS(boundValues[1u], 200., 1e-10);
0322   CHECK_CLOSE_ABS(boundValues[2u], 150, 1e-10);
0323 
0324   lcdd->destroyInstance();
0325 }
0326 
0327 BOOST_AUTO_TEST_CASE(DD4hepPluginWarnNonSensitiveSensors) {
0328   // Warnings of makeSurfaces for the rectangle, made sensitive or not
0329   auto warnings = [](bool sensitive) {
0330     std::ofstream cxml("PassiveRectangle.xml");
0331     cxml << head_xml << rectangle_xml << end_xml;
0332     cxml.close();
0333 
0334     auto lcdd = &(dd4hep::Detector::getInstance());
0335     lcdd->fromCompact("PassiveRectangle.xml");
0336     lcdd->volumeManager();
0337     lcdd->apply("DD4hepVolumeManager", 0, nullptr);
0338 
0339     const auto rectangle = lcdd->world().child("Rectangle");
0340     if (sensitive) {
0341       dd4hep::SensitiveDetector sd("RectangleSD", "tracker");
0342       lcdd->addSensitiveDetector(sd);
0343       rectangle.volume().setSensitiveDetector(sd);
0344     }
0345 
0346     std::ostringstream log;
0347     auto logger = getDefaultLogger("DD4hepBackend", Logging::WARNING, &log);
0348     using ActsPlugins::DD4hep::DD4hepBackend;
0349     DD4hepBackend backend{
0350         {.dd4hepDetector = lcdd, .lengthScale = 10., .gctx = tContext},
0351         *logger};
0352     {
0353       Logging::ScopedFailureThreshold threshold{Logging::Level::FATAL};
0354       backend.makeSurfaces(std::vector{rectangle},
0355                            DD4hepBackend::LayerSpec{.axes = TGeoAxes{"XYZ"},
0356                                                     .layerAxes = std::nullopt,
0357                                                     .layerName = std::nullopt});
0358     }
0359 
0360     lcdd->destroyInstance();
0361     return log.str();
0362   };
0363 
0364   BOOST_CHECK(warnings(false).find(
0365                   "1 of 1 sensor elements are not sensitive volumes") !=
0366               std::string::npos);
0367   BOOST_CHECK(warnings(true).empty());
0368 }
0369 
0370 BOOST_AUTO_TEST_SUITE_END()
0371 
0372 }  // namespace ActsTests