|
| 1 | +// Copyright 2019-2026 CERN and copyright holders of ALICE O2. |
| 2 | +// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders. |
| 3 | +// All rights not expressly granted are reserved. |
| 4 | +// |
| 5 | +// This software is distributed under the terms of the GNU General Public |
| 6 | +// License v3 (GPL Version 3), copied verbatim in the file "COPYING". |
| 7 | +// |
| 8 | +// In applying this license CERN does not waive the privileges and immunities |
| 9 | +// granted to it by virtue of its status as an Intergovernmental Organization |
| 10 | +// or submit itself to any jurisdiction. |
| 11 | + |
| 12 | +#include <memory> |
| 13 | +#include <utility> |
| 14 | +#include <vector> |
| 15 | +#include <ostream> |
| 16 | + |
| 17 | +#include <TGeoMatrix.h> |
| 18 | +#include <TGeoPhysicalNode.h> |
| 19 | + |
| 20 | +namespace o2::its3::align |
| 21 | +{ |
| 22 | + |
| 23 | +using RigidBodyDOFMask = uint8_t; |
| 24 | +constexpr RigidBodyDOFMask DOF_BIT(RigidBodyDOFMask d) { return RigidBodyDOFMask(1) << d; } |
| 25 | +enum RigidBodyDOF : RigidBodyDOFMask { |
| 26 | + TX = 0, // Translation X |
| 27 | + TY, // Translation Y |
| 28 | + TZ, // Translation Z |
| 29 | + RZ, // Rotation around Z (phi) |
| 30 | + RY, // Rotation around y' (theta) |
| 31 | + RX, // Rotation around x'' (psi) |
| 32 | + NDOF, // number of DOFs |
| 33 | +}; |
| 34 | +constexpr RigidBodyDOFMask RigidBodyDOFTra = DOF_BIT(TX) | DOF_BIT(TY) | DOF_BIT(TZ); |
| 35 | +constexpr RigidBodyDOFMask RigidBodyDOFRot = DOF_BIT(RX) | DOF_BIT(RY) | DOF_BIT(RZ); |
| 36 | +constexpr RigidBodyDOFMask RigidBodyDOFAll = RigidBodyDOFTra | RigidBodyDOFRot; |
| 37 | +constexpr RigidBodyDOFMask RigidBodyDOFNone = 0; |
| 38 | +constexpr RigidBodyDOFMask RigidBodyDOFPseudo = std::numeric_limits<RigidBodyDOFMask>::max(); // special value representing that the volume itself does not have any dofs but should not curtail the parent's ones |
| 39 | +static constexpr const char* RigidBodyDOFNames[RigidBodyDOF::NDOF] = {"TX", "TY", "TZ", "RZ", "RY", "RX"}; |
| 40 | +inline bool hasRigidBodyDOF(RigidBodyDOFMask m, RigidBodyDOFMask d) |
| 41 | +{ |
| 42 | + return (m == RigidBodyDOFPseudo) || (m & DOF_BIT(d)); |
| 43 | +} |
| 44 | +inline void enableRigidBodyDOF(RigidBodyDOFMask& m, RigidBodyDOFMask d) |
| 45 | +{ |
| 46 | + m |= DOF_BIT(d); |
| 47 | +} |
| 48 | +inline void disableRigidBodyDOF(RigidBodyDOFMask& m, RigidBodyDOFMask d) |
| 49 | +{ |
| 50 | + m &= ~DOF_BIT(d); |
| 51 | +} |
| 52 | + |
| 53 | +class GlobalLabel |
| 54 | +{ |
| 55 | + public: |
| 56 | + using T = uint32_t; |
| 57 | + static constexpr int DOF_BITS = 5; // 0...4 |
| 58 | + static constexpr int ID_BITS = 23; // 5...27 |
| 59 | + static constexpr int TOTAL_BITS = sizeof(T) * 8; |
| 60 | + static constexpr int DET_BITS = TOTAL_BITS - (DOF_BITS + ID_BITS); |
| 61 | + static constexpr T bitMask(int b) noexcept |
| 62 | + { |
| 63 | + return (T(1) << b) - T(1); |
| 64 | + } |
| 65 | + static constexpr int DOF_SHIFT = 0; |
| 66 | + static constexpr T DOF_MAX = (T(1) << DOF_BITS) - T(1); |
| 67 | + static constexpr T DOF_MASK = DOF_MAX << DOF_SHIFT; |
| 68 | + static constexpr int ID_SHIFT = DOF_BITS; |
| 69 | + static constexpr T ID_MAX = (T(1) << ID_BITS) - T(1); |
| 70 | + static constexpr T ID_MASK = ID_MAX << ID_SHIFT; |
| 71 | + static constexpr int DET_SHIFT = DOF_BITS + ID_BITS; |
| 72 | + static constexpr T DET_MAX = (T(1) << DET_BITS) - T(1); |
| 73 | + static constexpr T DET_MASK = DET_MAX << DET_SHIFT; |
| 74 | + |
| 75 | + GlobalLabel(T det, T id) : mID(((id & ID_MAX) << ID_SHIFT) | ((det & DET_MAX) << DET_SHIFT)) |
| 76 | + { |
| 77 | + } |
| 78 | + |
| 79 | + constexpr T raw(T dof) const noexcept { return (mID & ~DOF_MASK) | ((dof & DOF_MAX) << DOF_SHIFT); } |
| 80 | + constexpr T id() const noexcept |
| 81 | + { |
| 82 | + return (mID & ID_MASK) >> ID_SHIFT; |
| 83 | + } |
| 84 | + constexpr T det() const noexcept |
| 85 | + { |
| 86 | + return (mID & DET_MASK) >> DET_SHIFT; |
| 87 | + } |
| 88 | + |
| 89 | + private: |
| 90 | + T mID{0}; |
| 91 | +}; |
| 92 | + |
| 93 | +// Rigid body constraints for the parents |
| 94 | +class HierarchyConstraint |
| 95 | +{ |
| 96 | + public: |
| 97 | + HierarchyConstraint(std::string name, double value) : mName(std::move(name)), mValue(value) {} |
| 98 | + void add(uint32_t lab, double coeff) |
| 99 | + { |
| 100 | + mLabels.push_back(lab); |
| 101 | + mCoeff.push_back(coeff); |
| 102 | + } |
| 103 | + void write(std::ostream& os) const; |
| 104 | + |
| 105 | + private: |
| 106 | + std::string mName; // name of the constraint |
| 107 | + double mValue{0.0}; // constraint value |
| 108 | + std::vector<uint32_t> mLabels; // parameter labels |
| 109 | + std::vector<double> mCoeff; // their coefficients |
| 110 | +}; |
| 111 | + |
| 112 | +class AlignableVolume |
| 113 | +{ |
| 114 | + using Ptr = std::unique_ptr<AlignableVolume>; |
| 115 | + |
| 116 | + public: |
| 117 | + AlignableVolume(const char* symName, uint32_t label, uint32_t det, RigidBodyDOFMask dof); |
| 118 | + |
| 119 | + void finalise(); |
| 120 | + |
| 121 | + // steering file output |
| 122 | + void writeRigidBodyConstraints(std::ostream& os) const; |
| 123 | + void writeTree(std::ostream& os, int indent = 0) const; |
| 124 | + |
| 125 | + // tree-like |
| 126 | + bool isRoot() const noexcept { return mParent == nullptr; } |
| 127 | + bool isLeaf() const noexcept { return mChildren.empty(); } |
| 128 | + AlignableVolume* addChild(const char* symName, uint32_t label, uint32_t det, RigidBodyDOFMask dof) |
| 129 | + { |
| 130 | + auto c = std::make_unique<AlignableVolume>(symName, label, det, dof); |
| 131 | + c->mParent = this; |
| 132 | + mChildren.push_back(std::move(c)); |
| 133 | + return mChildren.back().get(); |
| 134 | + } |
| 135 | + // bfs traversal |
| 136 | + void traverse(const std::function<void(AlignableVolume*)>& visitor) |
| 137 | + { |
| 138 | + visitor(this); |
| 139 | + for (auto& c : mChildren) { |
| 140 | + c->traverse(visitor); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + std::string getSymName() const noexcept { return mSymName; } |
| 145 | + GlobalLabel getLabel() const noexcept { return mLabel; } |
| 146 | + AlignableVolume* getParent() const { return mParent; } |
| 147 | + size_t getNChildren() const noexcept { return mChildren.size(); } |
| 148 | + RigidBodyDOFMask getRigidBodyDOF() const noexcept { return mDOF; } |
| 149 | + |
| 150 | + private: |
| 151 | + std::string mSymName; // unique symbolic name |
| 152 | + GlobalLabel mLabel; // internal global idetenifier |
| 153 | + RigidBodyDOFMask mDOF{RigidBodyDOFAll}; // allowed dofs |
| 154 | + AlignableVolume* mParent{nullptr}; // parent |
| 155 | + TGeoPNEntry* mPNE{nullptr}; // physical entry |
| 156 | + TGeoPhysicalNode* mPN{nullptr}; // physical node |
| 157 | + |
| 158 | + std::vector<Ptr> mChildren; // children |
| 159 | +}; |
| 160 | + |
| 161 | +} // namespace o2::its3::align |
0 commit comments