From 798b5ce480ad7ad1e5224edc432d8d4323fbaaac Mon Sep 17 00:00:00 2001 From: Dmitry Kalinkin Date: Sat, 23 Nov 2024 17:13:23 -0500 Subject: [PATCH 1/2] drop GeometryHelper, a copy of GeometryHelpers --- src/B0ECal_geo.cpp | 35 ++++++++- src/GeometryHelper.cpp | 173 ----------------------------------------- src/GeometryHelper.h | 64 --------------- 3 files changed, 32 insertions(+), 240 deletions(-) delete mode 100644 src/GeometryHelper.cpp delete mode 100644 src/GeometryHelper.h diff --git a/src/B0ECal_geo.cpp b/src/B0ECal_geo.cpp index e4377a636..b15f2f486 100644 --- a/src/B0ECal_geo.cpp +++ b/src/B0ECal_geo.cpp @@ -1,15 +1,16 @@ // SPDX-License-Identifier: LGPL-3.0-or-later // Copyright (C) 2022 Sakib Rahman, Whitney Armstrong +#include +#include + #include "DD4hep/DetFactoryHelper.h" #include "DD4hep/OpticalSurfaces.h" #include "DD4hep/Printout.h" #include "DDRec/DetectorData.h" #include "DDRec/Surface.h" -#include "GeometryHelper.h" #include "Math/Point2D.h" #include -#include ////////////////////////////////////////////////// // Far Forward B0 Electromagnetic Calorimeter @@ -24,6 +25,9 @@ using namespace dd4hep; static tuple build_module(Detector& desc, xml_coll_t& plm, SensitiveDetector& sens); +static tuple add_individuals( + std::function(Detector&, xml_coll_t&, SensitiveDetector&)> build_module, + Detector& desc, Assembly& env, xml_coll_t& plm, SensitiveDetector& sens, int sid); static Ref_t createDetector(Detector& desc, xml_h e, SensitiveDetector sens) { xml_det_t x_det = e; @@ -54,7 +58,7 @@ static Ref_t createDetector(Detector& desc, xml_h e, SensitiveDetector sens) { for (xml_coll_t mod(plm, _Unicode(individuals)); mod; ++mod) { auto [sector, nmod] = - ip6::geo::add_individuals(build_module, desc, detVol, mod, sens, sector_id++); + add_individuals(build_module, desc, detVol, mod, sens, sector_id++); addModuleNumbers(sector, nmod); } @@ -99,4 +103,29 @@ static tuple build_module(Detector& desc, xml::Collection_t& p } } +// place modules, id must be provided +static tuple add_individuals( + std::function(Detector&, xml_coll_t&, SensitiveDetector&)> build_module, + Detector& desc, Assembly& env, xml_coll_t& plm, SensitiveDetector& sens, int sid) { + auto [modVol, modSize] = build_module(desc, plm, sens); + int sector_id = dd4hep::getAttrOrDefault(plm, _Unicode(sector), sid); + int nmodules = 0; + for (xml_coll_t pl(plm, _Unicode(placement)); pl; ++pl) { + Position pos(dd4hep::getAttrOrDefault(pl, _Unicode(x), 0.), + dd4hep::getAttrOrDefault(pl, _Unicode(y), 0.), + dd4hep::getAttrOrDefault(pl, _Unicode(z), 0.)); + Position rot(dd4hep::getAttrOrDefault(pl, _Unicode(rotx), 0.), + dd4hep::getAttrOrDefault(pl, _Unicode(roty), 0.), + dd4hep::getAttrOrDefault(pl, _Unicode(rotz), 0.)); + auto mid = pl.attr(_Unicode(id)); + Transform3D tr = + Translation3D(pos.x(), pos.y(), pos.z()) * RotationZYX(rot.z(), rot.y(), rot.x()); + auto modPV = env.placeVolume(modVol, tr); + modPV.addPhysVolID("sector", sector_id).addPhysVolID("module", mid); + nmodules++; + } + + return {sector_id, nmodules}; +} + DECLARE_DETELEMENT(B0_ECAL, createDetector) diff --git a/src/GeometryHelper.cpp b/src/GeometryHelper.cpp deleted file mode 100644 index c0208502a..000000000 --- a/src/GeometryHelper.cpp +++ /dev/null @@ -1,173 +0,0 @@ -// SPDX-License-Identifier: LGPL-3.0-or-later -// Copyright (C) 2022 Sakib Rahman, Chao Peng, Whitney Armstrong - -#include "GeometryHelper.h" - -namespace ip6::geo { - -Position get_xml_xyz(xml_coll_t& comp, dd4hep::xml::Strng_t name) { - Position pos(0., 0., 0.); - if (comp.hasChild(name)) { - auto child = comp.child(name); - pos.SetX(dd4hep::getAttrOrDefault(child, _Unicode(x), 0.)); - pos.SetY(dd4hep::getAttrOrDefault(child, _Unicode(y), 0.)); - pos.SetZ(dd4hep::getAttrOrDefault(child, _Unicode(z), 0.)); - } - return pos; -} - -// place modules, id must be provided -tuple add_individuals( - function(Detector&, xml_coll_t&, SensitiveDetector&)> build_module, - Detector& desc, Assembly& env, xml_coll_t& plm, SensitiveDetector& sens, int sid) { - auto [modVol, modSize] = build_module(desc, plm, sens); - int sector_id = dd4hep::getAttrOrDefault(plm, _Unicode(sector), sid); - int nmodules = 0; - for (xml_coll_t pl(plm, _Unicode(placement)); pl; ++pl) { - Position pos(dd4hep::getAttrOrDefault(pl, _Unicode(x), 0.), - dd4hep::getAttrOrDefault(pl, _Unicode(y), 0.), - dd4hep::getAttrOrDefault(pl, _Unicode(z), 0.)); - Position rot(dd4hep::getAttrOrDefault(pl, _Unicode(rotx), 0.), - dd4hep::getAttrOrDefault(pl, _Unicode(roty), 0.), - dd4hep::getAttrOrDefault(pl, _Unicode(rotz), 0.)); - auto mid = pl.attr(_Unicode(id)); - Transform3D tr = - Translation3D(pos.x(), pos.y(), pos.z()) * RotationZYX(rot.z(), rot.y(), rot.x()); - auto modPV = env.placeVolume(modVol, tr); - modPV.addPhysVolID("sector", sector_id).addPhysVolID("module", mid); - nmodules++; - } - - return {sector_id, nmodules}; -} - -// place disk of modules -tuple -add_disk(function(Detector&, xml_coll_t&, SensitiveDetector&)> build_module, - Detector& desc, Assembly& env, xml_coll_t& plm, SensitiveDetector& sens, int sid) { - auto [modVol, modSize] = build_module(desc, plm, sens); - int sector_id = dd4hep::getAttrOrDefault(plm, _Unicode(sector), sid); - int id_begin = dd4hep::getAttrOrDefault(plm, _Unicode(id_begin), 1); - double rmin = plm.attr(_Unicode(rmin)); - double rintermediate = plm.attr(_Unicode(rintermediate)); - double r_envelopeclearance = dd4hep::getAttrOrDefault(plm, _Unicode(r_envelopeclearance), 0); - double phi_envelopeclearance = - dd4hep::getAttrOrDefault(plm, _Unicode(phi_envelopeclearance), 0); - double rmax = plm.attr(_Unicode(rmax)); - double phimin = dd4hep::getAttrOrDefault(plm, _Unicode(phimin), 0.); - double phimax = dd4hep::getAttrOrDefault(plm, _Unicode(phimax), 2. * M_PI); - - // placement inside mother - auto pos = get_xml_xyz(plm, _Unicode(position)); - auto rot = get_xml_xyz(plm, _Unicode(rotation)); - - // optional envelope volume - bool has_envelope = dd4hep::getAttrOrDefault(plm, _Unicode(envelope), false); - Material material = desc.material(dd4hep::getAttrOrDefault(plm, _U(material), "Air")); - Tube inner_solid(rmin, rintermediate + r_envelopeclearance, modSize.z() / 2.0, 0, 2. * M_PI); - Tube outer_solid(rintermediate, rmax + r_envelopeclearance, modSize.z() / 2.0, - phimin - phi_envelopeclearance, phimax + phi_envelopeclearance); - UnionSolid solid(inner_solid, outer_solid); - Volume env_vol(string(env.name()) + "_envelope", solid, material); - Transform3D tr_global = - RotationZYX(rot.z(), rot.y(), rot.x()) * Translation3D(pos.x(), pos.y(), pos.z()); - if (has_envelope) { - env.placeVolume(env_vol, tr_global); - } - - // local placement of modules - int mid = 0; - auto points = - fillRectangles({0., 0.}, modSize.x(), modSize.y(), rmin, rintermediate, rmax, phimin, phimax); - for (auto& p : points) { - Transform3D tr_local = RotationZYX(0.0, 0.0, 0.0) * Translation3D(p.x(), p.y(), 0.0); - auto modPV = (has_envelope ? env_vol.placeVolume(modVol, tr_local) - : env.placeVolume(modVol, tr_global * tr_local)); - modPV.addPhysVolID("sector", sector_id).addPhysVolID("module", id_begin + mid++); - } - return {sector_id, mid}; -} - -// check if a 2d point is already in the container -bool already_placed(const Point& p, const vector& vec, double xs = 1.0, double ys = 1.0, - double tol = 1e-6) { - for (auto& pt : vec) { - if ((std::abs(pt.x() - p.x()) / xs < tol) && std::abs(pt.y() - p.y()) / ys < tol) { - return true; - } - } - return false; -} - -// check if a point is in a ring -inline bool rec_in_ring(const Point& pt, double sx, double sy, double rmin, double rintermediate, - double rmax, double phmin, double phmax) { - - // check four corners - vector pts{ - Point(pt.x() - sx / 2., pt.y() - sy / 2.), - Point(pt.x() - sx / 2., pt.y() + sy / 2.), - Point(pt.x() + sx / 2., pt.y() - sy / 2.), - Point(pt.x() + sx / 2., pt.y() + sy / 2.), - }; - - bool inside = false; - int minindex = 0; - int i = 0; - - for (auto& p : pts) { - minindex = (p.r() < pts[minindex].r()) ? i : minindex; - i++; - } - - double rmax_pacman = - (pts[minindex].phi() < phmin || pts[minindex].phi() > phmax) ? rintermediate : rmax; - inside = pts[minindex].r() <= rmax_pacman && pts[minindex].r() >= rmin; - - return inside; -} - -// a helper function to recursively fill square in a ring -void add_rectangle(Point p, vector& res, double sx, double sy, double rmin, - double rintermediate, double rmax, double phmin, double phmax, - int max_depth = 20, int depth = 0) { - // exceeds the maximum depth in searching or already placed - if ((depth > max_depth) || (already_placed(p, res, sx, sy))) { - return; - } - - bool in_ring = rec_in_ring(p, sx, sy, rmin, rintermediate, rmax, phmin, phmax); - if (in_ring) { - res.emplace_back(p); - } - // continue search for a good placement or if no placement found yet - if (in_ring || res.empty()) { - // check adjacent squares - add_rectangle(Point(p.x() + sx, p.y()), res, sx, sy, rmin, rintermediate, rmax, phmin, phmax, - max_depth, depth + 1); - add_rectangle(Point(p.x() - sx, p.y()), res, sx, sy, rmin, rintermediate, rmax, phmin, phmax, - max_depth, depth + 1); - add_rectangle(Point(p.x(), p.y() + sy), res, sx, sy, rmin, rintermediate, rmax, phmin, phmax, - max_depth, depth + 1); - add_rectangle(Point(p.x(), p.y() - sy), res, sx, sy, rmin, rintermediate, rmax, phmin, phmax, - max_depth, depth + 1); - } -} - -// fill squares -vector fillRectangles(Point ref, double sx, double sy, double rmin, double rintermediate, - double rmax, double phmin, double phmax) { - // convert (0, 2pi) to (-pi, pi) - if (phmax > M_PI) { - phmin -= M_PI; - phmax -= M_PI; - } - // start with a seed square and find one in the ring - // move to center - ref = ref - Point(int(ref.x() / sx) * sx, int(ref.y() / sy) * sy); - vector res; - add_rectangle(ref, res, sx, sy, rmin, rintermediate, rmax, phmin, phmax, - (int(rmax / sx) + 1) * (int(rmax / sy) + 1) * 2); - return res; -} -} // namespace ip6::geo diff --git a/src/GeometryHelper.h b/src/GeometryHelper.h deleted file mode 100644 index 33dcc95cf..000000000 --- a/src/GeometryHelper.h +++ /dev/null @@ -1,64 +0,0 @@ -// SPDX-License-Identifier: LGPL-3.0-or-later -// Copyright (C) 2022 Sakib Rahman, Wouter Deconinck, Whitney Armstrong - -#pragma once -#include "DD4hep/DetFactoryHelper.h" -#include "DD4hep/OpticalSurfaces.h" -#include "DD4hep/Printout.h" -#include "DDRec/DetectorData.h" -#include "DDRec/Surface.h" -#include "Math/Point2D.h" -#include -#include -#include - -// some utility functions that can be shared -namespace ip6::geo { - -using std::function; -using std::make_tuple; -using std::map; -using std::string; -using std::tuple; -using std::vector; -using Volume = dd4hep::Volume; -using Position = dd4hep::Position; -using Assembly = dd4hep::Assembly; -using Detector = dd4hep::Detector; -using SensitiveDetector = dd4hep::SensitiveDetector; -using Point = ROOT::Math::XYPoint; -using Transform3D = dd4hep::Transform3D; -using Translation3D = dd4hep::Translation3D; -using RotationZYX = dd4hep::RotationZYX; -using UnionSolid = dd4hep::UnionSolid; -using Material = dd4hep::Material; -using Tube = dd4hep::Tube; - -/* Fill rectangles in a pacman disk - * - * @param ref 2D reference point. - * @param sx x side length - * @param sy y side length - * @param rmin inner radius of disk - * @param rintermediate outer radius of inner full azimuth disk - * @param rmax outer radius of outer disk section ranging from phi min to phi max - * @param phmin phi min - * @param phmax phi max - * N.B. Current implementation only guarantees that inner radial bound will be strictly observed. - * Outer radial bunds can be penetrated by internal modules on or near the boundary. - */ - -vector fillRectangles(Point ref, double sx, double sy, double rmin, double rintermediate, - double rmax, double phmin = -M_PI, double phmax = M_PI); - -tuple add_individuals( - function(Detector&, xml_coll_t&, SensitiveDetector&)> build_module, - Detector& desc, Assembly& env, xml_coll_t& plm, SensitiveDetector& sens, int id); - -tuple -add_disk(function(Detector&, xml_coll_t&, SensitiveDetector&)> build_module, - Detector& desc, Assembly& env, xml_coll_t& plm, SensitiveDetector& sens, int id); - -Position get_xml_xyz(xml_comp_t& comp, dd4hep::xml::Strng_t name); - -} // namespace ip6::geo From 2738940bc23d329ff17f5d8e1ac25317af734d4d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 23 Nov 2024 22:19:22 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/B0ECal_geo.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/B0ECal_geo.cpp b/src/B0ECal_geo.cpp index b15f2f486..c6598d90f 100644 --- a/src/B0ECal_geo.cpp +++ b/src/B0ECal_geo.cpp @@ -57,8 +57,7 @@ static Ref_t createDetector(Detector& desc, xml_h e, SensitiveDetector sens) { int sector_id = 1; for (xml_coll_t mod(plm, _Unicode(individuals)); mod; ++mod) { - auto [sector, nmod] = - add_individuals(build_module, desc, detVol, mod, sens, sector_id++); + auto [sector, nmod] = add_individuals(build_module, desc, detVol, mod, sens, sector_id++); addModuleNumbers(sector, nmod); }