ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ConstrainedStep.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file ConstrainedStep.hpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2018 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 
9 #pragma once
10 
11 #include <algorithm>
12 #include <array>
13 #include <iomanip>
14 #include <limits>
15 #include <sstream>
16 
18 
19 namespace Acts {
20 
28  enum Type : int { accuracy = 0, actor = 1, aborter = 2, user = 3 };
29 
31  std::array<double, 4> values = {
34 
37 
45  void update(const double& value, Type type, bool releaseStep = false) {
46  if (releaseStep) {
47  release(type);
48  }
49  // The check the current value and set it if appropriate
50  double cValue = values[type];
51  values[type] = cValue * cValue < value * value ? cValue : value;
52  }
53 
59  void release(Type type) {
60  double mvalue = (direction == forward)
61  ? (*std::max_element(values.begin(), values.end()))
62  : (*std::min_element(values.begin(), values.end()));
63  values[type] = mvalue;
64  }
65 
68  ConstrainedStep(double value) : direction(value > 0. ? forward : backward) {
72  values[user] = value;
73  }
74 
80  ConstrainedStep& operator=(const double& value) {
83  // set/update the direction
84  direction = value > 0. ? forward : backward;
85  return (*this);
86  }
87 
90  operator double() const {
91  if (direction == forward) {
92  return (*std::min_element(values.begin(), values.end()));
93  }
94  return (*std::max_element(values.begin(), values.end()));
95  }
96 
100  double value(Type type) const { return values[type]; }
101 
104  double max() const {
105  return (*std::max_element(values.begin(), values.end()));
106  }
107 
110  double min() const {
111  return (*std::min_element(values.begin(), values.end()));
112  }
113 
116  Type currentType() const {
117  if (direction == forward) {
118  return Type(std::min_element(values.begin(), values.end()) -
119  values.begin());
120  }
121  return Type(std::max_element(values.begin(), values.end()) -
122  values.begin());
123  }
124 
126  std::string toString() const;
127 };
128 
129 inline std::string ConstrainedStep::toString() const {
130  std::stringstream dstream;
131 
132  // Helper method to avoid unreadable screen output
133  auto streamValue = [&](ConstrainedStep cstep) -> void {
134  double val = values[cstep];
135  dstream << std::setw(5);
137  dstream << (val > 0 ? "+∞" : "-∞");
138  } else {
139  dstream << val;
140  }
141  };
142 
143  dstream << "(";
144  streamValue(accuracy);
145  dstream << ", ";
146  streamValue(actor);
147  dstream << ", ";
148  streamValue(aborter);
149  dstream << ", ";
150  streamValue(user);
151  dstream << " )";
152  return dstream.str();
153 }
154 
155 } // namespace Acts