Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Extent.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file Extent.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2022 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
10 
12 
13 #include <algorithm>
14 #include <cmath>
15 #include <cstddef>
16 #include <iomanip>
17 #include <limits>
18 
20  const std::array<std::array<ActsScalar, 2>, binValues>& envelope)
21  : m_constrains(0), m_envelope(envelope) {
22  m_range[binR] =
23  Range1D<ActsScalar>(0., std::numeric_limits<ActsScalar>::max());
24  m_range[binPhi] = Range1D<ActsScalar>(-M_PI, M_PI);
25  m_range[binRPhi] =
26  Range1D<ActsScalar>(0., std::numeric_limits<ActsScalar>::max());
27  m_range[binMag] =
28  Range1D<ActsScalar>(0., std::numeric_limits<ActsScalar>::max());
29 }
30 
31 void Acts::Extent::extend(const Vector3& vtx,
32  const std::vector<BinningValue>& bValues,
33  bool applyEnv, bool fillHistograms) {
34  for (auto bValue : bValues) {
35  // Get the casted value given the binning value description
36  ActsScalar cValue = VectorHelpers::cast(vtx, bValue);
37  if (fillHistograms) {
38  m_valueHistograms[bValue].push_back(cValue);
39  }
40  // Apply envelope as suggested
41  ActsScalar lEnv = applyEnv ? m_envelope[bValue][0] : 0.;
42  ActsScalar hEnv = applyEnv ? m_envelope[bValue][1] : 0.;
43  ActsScalar mValue = cValue - lEnv;
44  // Special protection for radial value
45  if (bValue == binR and mValue < 0.) {
46  mValue = std::max(mValue, 0.);
47  }
48  if (constrains(bValue)) {
49  m_range[bValue].expand(mValue, cValue + hEnv);
50  } else {
51  m_range[bValue].shrink(mValue, cValue + hEnv);
52  }
53  m_constrains.set(bValue);
54  }
55 }
56 
58  const std::vector<BinningValue>& bValues,
59  bool applyEnv) {
60  for (auto bValue : bValues) {
61  // The value is constraint, envelope can be optional
62  if (rhs.constrains(bValue)) {
63  ActsScalar lEnv = applyEnv ? m_envelope[bValue][0] : 0.;
64  ActsScalar hEnv = applyEnv ? m_envelope[bValue][1] : 0.;
65  if (constrains(bValue)) {
66  m_range[bValue].expand(rhs.range()[bValue].min() - lEnv,
67  rhs.range()[bValue].max() + hEnv);
68  } else {
69  m_range[bValue].shrink(rhs.range()[bValue].min() - lEnv,
70  rhs.range()[bValue].max() + hEnv);
71  }
72  m_constrains.set(bValue);
73  } else if (rhs.envelope()[bValue] != zeroEnvelope) {
74  // Only an envelope given, but value is not constraint -> apply envelope
75  m_range[bValue].expand(m_range[bValue].min() - rhs.envelope()[bValue][0],
76  m_range[bValue].max() + rhs.envelope()[bValue][1]);
77  m_constrains.set(bValue);
78  }
79  }
80 }
81 
83  const ExtentEnvelope& envelope) {
84  for (const auto& bValue : s_binningValues) {
85  if (rhs.constrains(bValue) and not constrains(bValue)) {
86  const auto& cRange = rhs.range(bValue);
87  m_range[bValue].setMin(cRange.min() + envelope[bValue][0u]);
88  m_range[bValue].setMax(cRange.max() + envelope[bValue][1u]);
89  m_constrains.set(bValue);
90  }
91  }
92 }
93 
95  ActsScalar minval = min;
96  if (bValue == binR and minval < 0.) {
97  minval = 0.;
98  }
99  m_range[bValue] = Range1D{minval, max};
100  m_constrains.set(bValue);
101 }
102 
104  m_envelope = envelope;
105 }
106 
107 bool Acts::Extent::contains(const Extent& rhs, BinningValue bValue) const {
108  // Helper to check including a constraint bit set check
109  auto checkContainment = [&](BinningValue bvc) -> bool {
110  if (not constrains(bvc)) {
111  return true;
112  }
113  return (rhs.range()[bvc] <= m_range[bvc]);
114  };
115 
116  // Check all
117  if (bValue == binValues) {
118  for (int ibv = 0; ibv < (int)binValues; ++ibv) {
119  if (not checkContainment((BinningValue)ibv)) {
120  return false;
121  }
122  }
123  return true;
124  }
125  // Check specific
126  return checkContainment(bValue);
127 }
128 
129 bool Acts::Extent::intersects(const Extent& rhs, BinningValue bValue) const {
130  // Helper to check including a constraint bit set check
131  auto checkIntersect = [&](BinningValue bvc) -> bool {
132  if (not constrains(bvc) or not rhs.constrains(bvc)) {
133  return false;
134  }
135  return (m_range[bvc] && rhs.range()[bvc]);
136  };
137 
138  // Check all
139  if (bValue == binValues) {
140  for (int ibv = 0; ibv < (int)binValues; ++ibv) {
141  if (checkIntersect((BinningValue)ibv)) {
142  return true;
143  }
144  }
145  return false;
146  }
147  // Check specific
148  return checkIntersect(bValue);
149 }
150 
152  if (bValue == binValues) {
153  return (m_constrains.count() > 0);
154  }
155  return m_constrains.test(size_t(bValue));
156 }
157 
158 bool Acts::Extent::operator==(const Extent& e) const {
159  if (m_constrains != e.m_constrains) {
160  return false;
161  }
162  if (m_envelope != e.m_envelope) {
163  return false;
164  }
165  if (not(m_range == e.m_range)) {
166  return false;
167  }
168  if (m_valueHistograms != e.m_valueHistograms) {
169  return false;
170  }
171  return true;
172 }
173 
175  std::stringstream sl;
176  sl << indent << "Extent in space : " << std::endl;
177  for (size_t ib = 0; ib < static_cast<size_t>(binValues); ++ib) {
178  if (constrains((BinningValue)ib)) {
179  sl << indent << " - value :" << std::setw(10) << binningValueNames()[ib]
180  << " | range = [" << m_range[ib].min() << ", " << m_range[ib].max()
181  << "]" << std::endl;
182  }
183  }
184  return sl.str();
185 }
186 
187 // Overload of << operator for std::ostream for debug output
188 std::ostream& Acts::operator<<(std::ostream& sl, const Extent& rhs) {
189  sl << rhs.toString();
190  return sl;
191 }