Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-15 08:12:27

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 "Acts/Utilities/ProtoAxis.hpp"
0010 
0011 #include <sstream>
0012 
0013 Acts::ProtoAxis::ProtoAxis(Acts::AxisBoundaryType abType,
0014                            const std::vector<double>& edges)
0015     : m_axis(IAxis::createVariable(abType, edges)) {}
0016 
0017 Acts::ProtoAxis::ProtoAxis(AxisBoundaryType abType, double minE, double maxE,
0018                            std::size_t nbins)
0019     : m_axis(IAxis::createEquidistant(abType, minE, maxE, nbins)) {}
0020 
0021 Acts::ProtoAxis::ProtoAxis(AxisBoundaryType abType, std::size_t nbins)
0022     : m_axis(IAxis::createEquidistant(abType, 0., 1., nbins)),
0023       m_autorange(true) {}
0024 
0025 Acts::ProtoAxis::ProtoAxis(const ProtoAxis& other)
0026     : m_autorange(other.m_autorange) {
0027   const auto& axis = other.getAxis();
0028   if (!m_autorange) {
0029     const auto& edges = axis.getBinEdges();
0030     if (axis.getType() == AxisType::Variable) {
0031       m_axis = IAxis::createVariable(axis.getBoundaryType(), edges);
0032     } else {
0033       m_axis = IAxis::createEquidistant(axis.getBoundaryType(), edges.front(),
0034                                         edges.back(), axis.getNBins());
0035     }
0036   } else {
0037     m_axis = IAxis::createEquidistant(axis.getBoundaryType(), 0., 1.,
0038                                       axis.getNBins());
0039   }
0040 }
0041 
0042 Acts::ProtoAxis& Acts::ProtoAxis::operator=(const ProtoAxis& other) {
0043   if (this != &other) {
0044     m_autorange = other.m_autorange;
0045     const auto& axis = other.getAxis();
0046     if (!m_autorange) {
0047       const auto& edges = axis.getBinEdges();
0048       if (axis.getType() == AxisType::Variable) {
0049         m_axis = IAxis::createVariable(axis.getBoundaryType(), edges);
0050       } else {
0051         m_axis = IAxis::createEquidistant(axis.getBoundaryType(), edges.front(),
0052                                           edges.back(), axis.getNBins());
0053       }
0054     } else {
0055       m_axis = IAxis::createEquidistant(axis.getBoundaryType(), 0., 1.,
0056                                         axis.getNBins());
0057     }
0058   }
0059   return *this;
0060 }
0061 
0062 const Acts::IAxis& Acts::ProtoAxis::getAxis() const {
0063   return *m_axis;
0064 }
0065 
0066 void Acts::ProtoAxis::setRange(double minE, double maxE) {
0067   if (minE > maxE) {
0068     throw std::invalid_argument(
0069         "ProtoAxis::setRange: minE > maxE is not allowed.");
0070   }
0071 
0072   if (m_axis->getType() == AxisType::Equidistant) {
0073     m_axis = IAxis::createEquidistant(m_axis->getBoundaryType(), minE, maxE,
0074                                       m_axis->getNBins());
0075   } else {
0076     std::vector<double> edges = m_axis->getBinEdges();
0077     // Clip it to min/max
0078     std::erase_if(edges,
0079                   [minE, maxE](double e) { return (e < minE || e > maxE); });
0080     // Add the min and max
0081     edges.emplace_back(minE);
0082     edges.emplace_back(maxE);
0083     std::ranges::sort(edges);
0084     m_axis = IAxis::createVariable(m_axis->getBoundaryType(), edges);
0085   }
0086 
0087   // Force autorange to be false
0088   m_autorange = false;
0089 }
0090 
0091 bool Acts::ProtoAxis::isAutorange() const {
0092   return m_autorange;
0093 }
0094 
0095 void Acts::ProtoAxis::toStream(std::ostream& os) const {
0096   os << toString();
0097 }
0098 
0099 std::string Acts::ProtoAxis::toString() const {
0100   std::stringstream ss;
0101   const auto& axis = getAxis();
0102   ss << "ProtoAxis: " << axis.getNBins() << " bins";
0103   ss << (axis.getType() == AxisType::Variable ? ", variable "
0104                                               : ", equidistant ");
0105   if (!m_autorange) {
0106     const auto& edges = axis.getBinEdges();
0107     ss << "within [" << edges.front() << ", " << edges.back() << "]";
0108   } else {
0109     ss << "within automatic range";
0110   }
0111   return ss.str();
0112 }
0113 
0114 // Ostream operator implementation
0115 std::ostream& Acts::operator<<(std::ostream& os,
0116                                const std::vector<ProtoAxis>& as) {
0117   for (const auto& a : as) {
0118     os << a.toString() << '\n';
0119   }
0120   return os;
0121 }
0122 
0123 Acts::DirectedProtoAxis::DirectedProtoAxis(AxisDirection axisDir,
0124                                            AxisBoundaryType abType,
0125                                            const std::vector<double>& edges)
0126     : ProtoAxis(abType, edges), m_direction(axisDir) {}
0127 
0128 Acts::DirectedProtoAxis::DirectedProtoAxis(AxisDirection axisDir,
0129                                            AxisBoundaryType abType, double minE,
0130                                            double maxE, std::size_t nbins)
0131     : ProtoAxis(abType, minE, maxE, nbins), m_direction(axisDir) {}
0132 
0133 Acts::DirectedProtoAxis::DirectedProtoAxis(AxisDirection axisDir,
0134                                            AxisBoundaryType abType,
0135                                            std::size_t nbins)
0136     : ProtoAxis(abType, nbins), m_direction(axisDir) {}
0137 
0138 Acts::AxisDirection Acts::DirectedProtoAxis::getAxisDirection() const {
0139   return m_direction;
0140 }
0141 
0142 void Acts::DirectedProtoAxis::toStream(std::ostream& os) const {
0143   os << toString();
0144 }
0145 
0146 std::string Acts::DirectedProtoAxis::toString() const {
0147   std::stringstream ss;
0148   const auto& axis = getAxis();
0149   ss << "DirectedProtoAxis: " << axis.getNBins() << " bins in "
0150      << axisDirectionName(m_direction);
0151   ss << (axis.getType() == AxisType::Variable ? ", variable "
0152                                               : ", equidistant ");
0153   if (!m_autorange) {
0154     const auto& edges = axis.getBinEdges();
0155     ss << "within [" << edges.front() << ", " << edges.back() << "]";
0156   } else {
0157     ss << "within automatic range";
0158   }
0159   return ss.str();
0160 }
0161 
0162 // Ostream operator implementation vector of directed proto axes
0163 std::ostream& Acts::operator<<(std::ostream& os,
0164                                const std::vector<DirectedProtoAxis>& as) {
0165   for (const auto& a : as) {
0166     os << a << '\n';
0167   }
0168   return os;
0169 }