ATLAS Offline Software
MultiRange.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 #include <iostream>
7 #include <algorithm> //remove_if
8 #include <ranges>
9 #include <numeric>
10 
11 MultiRange::MultiRange (const Range& r, const Range& s) {
12  m_ranges.push_back (r);
13  m_ranges.push_back (s);
14 }
15 
16 //-----------------------------------------------
18  m_ranges.clear ();
19 }
20 
21 //-----------------------------------------------
22 void MultiRange::add (const Range& range) {
23  // Add new range ONLY if an equivalent does NOT exist
24  if (std::ranges::find(m_ranges, range) == m_ranges.end()) {
25  m_ranges.push_back(range);
26  }
27 }
28 
29 //-----------------------------------------------
31  // Add new range ONLY if an equivalent does NOT exist
32  if (std::ranges::find(m_ranges, range) == m_ranges.end()) {
33  m_ranges.emplace_back (std::move(range));
34  }
35 }
36 
37 //-----------------------------------------------
39  m_ranges.emplace_back (id);
40 }
41 
42 //-----------------------------------------------
44  // Remove all ranges for which id matches
45  [[maybe_unused]] auto erased = std::erase_if (m_ranges,
46  [&id] (const Range& r) { return r.match(id); });
47 }
48 
49 
50 
51 //-----------------------------------------------
53  return (m_ranges.back());
54 }
55 
56 //-----------------------------------------------
57 int MultiRange::match(const ExpandedIdentifier& id) const {
58  bool result = std::ranges::any_of(m_ranges, [&id](const Range & r){return r.match(id);});
59  return result ? 1:0;
60 }
61 
62 //-----------------------------------------------
64  static const Range null_range;
65  if (index >= m_ranges.size ()) return (null_range);
66  return (m_ranges[index]);
67 }
68 
69 //-----------------------------------------------
71  return (m_ranges.size ());
72 }
73 
75  return (m_ranges.begin ());
76 }
77 
79  return (m_ranges.end ());
80 }
81 
83  size_type init = 0;
84  return std::accumulate(m_ranges.begin(), m_ranges.end(), init,
85  [](size_type a, const Range& b){return a + b.cardinality();});
86 }
87 
89  // Loop over ranges in MultiRange and calculate hash for each
90  // range
91  size_type init = 0;
92  return std::accumulate(m_ranges.begin(), m_ranges.end(), init,
93  [&id](size_type a, const Range& b){return a + b.cardinalityUpTo(id);});
94 }
95 
96 
97 //-----------------------------------------------
98 bool MultiRange::has_overlap() const {
99  range_vector::size_type i;
100  range_vector::size_type j;
101  for (i = 0; i < m_ranges.size (); ++i) {
102  const Range& r = m_ranges[i];
103  for (j = i + 1; j < m_ranges.size (); ++j) {
104  const Range& s = m_ranges[j];
105  if (r.overlaps_with (s)) return (true);
106  }
107  }
108  return (false);
109 }
110 
111 //-----------------------------------------------
113  const MultiRange& me = *this;
114  return (identifier_factory (me));
115 }
116 
117 //-----------------------------------------------
119  const MultiRange& me = *this;
120  return (const_identifier_factory (me));
121 }
122 
123 //-----------------------------------------------
125  static const identifier_factory factory(*this);
126  return (factory);
127 }
128 
129 //-----------------------------------------------
131  static const const_identifier_factory factory;
132  return (factory);
133 }
134 
135 //-----------------------------------------------
137  :
138  m_range_it(multirange.m_ranges.begin()),
139  m_range_end(multirange.m_ranges.end()){
140  if (m_range_it == m_range_end)return; // no ranges
144  if (m_range_it != m_range_end) {
145  m_id_fac_it = ConstRangeIterator(*m_range_it).begin();
146  m_id_fac_end = ConstRangeIterator(*m_range_it).end();
147  if(m_id_fac_it != m_id_fac_end) {
148  // Set id
149  m_id = *m_id_fac_it;
150  }
151  }
152 }
153 
154 //-----------------------------------------------
156  if (m_id.fields () == 0) return;
157  m_id.clear();
158  if (m_range_it != m_range_end) {
159  if (m_id_fac_it != m_id_fac_end) {
160  ++m_id_fac_it;
161  }
162  if (m_id_fac_it == m_id_fac_end) {
163  ++m_range_it;
164  if (m_range_it != m_range_end) {
165  m_id_fac_it = ConstRangeIterator(*m_range_it).begin();
166  m_id_fac_end = ConstRangeIterator(*m_range_it).end();
167  }
168  }
169  if (m_id_fac_it != m_id_fac_end) {
170  m_id = *m_id_fac_it;
171  }
172  }
173 }
174 
175 //-----------------------------------------------
177  return (m_id);
178 }
179 
180 //-----------------------------------------------
182  return (m_id == other.m_id);
183 }
184 
185 //-----------------------------------------------
187  :
188  m_range_it(multirange.m_ranges.begin()),
189  m_range_end(multirange.m_ranges.end()){
190 
191  if (m_range_it == m_range_end) return; // no ranges
195  if (m_range_it != m_range_end) {
196  m_id_fac_it = ConstRangeIterator(*m_range_it).begin();
197  m_id_fac_end = ConstRangeIterator(*m_range_it).end();
198  if(m_id_fac_it != m_id_fac_end) {
199  // Set id
200  m_id = *m_id_fac_it;
201  }
202  }
203 }
204 
205 //-----------------------------------------------
207  if (m_id.fields () == 0) return;
208  m_id.clear();
209  if (m_range_it != m_range_end) {
210  if (m_id_fac_it != m_id_fac_end) {
211  ++m_id_fac_it;
212  }
213  if (m_id_fac_it == m_id_fac_end) {
214  ++m_range_it;
215  if (m_range_it != m_range_end) {
216  m_id_fac_it = ConstRangeIterator(*m_range_it).begin();
217  m_id_fac_end = ConstRangeIterator(*m_range_it).end();
218  }
219  }
220  if (m_id_fac_it != m_id_fac_end) {
221  m_id = *m_id_fac_it;
222  }
223  }
224 }
225 
226 //-----------------------------------------------
228  return (m_id);
229 }
230 
231 //-----------------------------------------------
233  return (m_id == other.m_id);
234 }
235 
236 //-----------------------------------------------
237 void MultiRange::show () const {
238  show (std::cout);
239 }
240 
241 void MultiRange::show (std::ostream& s) const {
242  range_vector::size_type i;
243  for (i = 0; i < m_ranges.size (); ++i) {
244  if (i > 0) s << std::endl;
245  const Range& r = m_ranges[i];
246  r.show (s);
247  }
248 }
249 
250 //-----------------------------------------------
251 MultiRange::operator std::string () const {
252  std::string result;
253  range_vector::size_type i{};
254  for ( ; i < m_ranges.size (); ++i){
255  if (i > 0) result += " | ";
256  const Range& r = m_ranges[i];
257  result += (std::string) r;
258  }
259  return (result);
260 }
beamspotman.r
def r
Definition: beamspotman.py:676
ConstRangeIterator
Definition: RangeIterator.h:46
MultiRange::const_identifier_factory::operator==
bool operator==(const const_identifier_factory &other) const
Definition: MultiRange.cxx:232
MultiRange::const_identifier_factory::m_id_fac_it
ConstRangeIterator m_id_fac_it
Definition: MultiRange.h:66
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
MultiRange::operator[]
const Range & operator[](size_type index) const
Accessors.
Definition: MultiRange.cxx:63
get_generator_info.result
result
Definition: get_generator_info.py:21
MultiRange::identifier_factory::operator==
bool operator==(const identifier_factory &other) const
Definition: MultiRange.cxx:181
find
std::string find(const std::string &s)
return a remapped string
Definition: hcg.cxx:135
MultiRange::identifier_factory::operator++
void operator++()
Definition: MultiRange.cxx:155
MultiRange::factory_begin
identifier_factory factory_begin()
Definition: MultiRange.cxx:112
index
Definition: index.py:1
MultiRange::factory_end
identifier_factory factory_end()
Definition: MultiRange.cxx:124
MultiRange::add
void add(const Range &range)
Definition: MultiRange.cxx:22
MultiRange::identifier_factory::m_id_fac_end
ConstRangeIterator m_id_fac_end
Definition: MultiRange.h:45
MultiRange::clear
void clear()
Definition: MultiRange.cxx:17
MultiRange::cardinalityUpTo
size_type cardinalityUpTo(const ExpandedIdentifier &id) const
Definition: MultiRange.cxx:88
accumulate
bool accumulate(AccumulateMap &map, std::vector< module_t > const &modules, FPGATrackSimMatrixAccumulator const &acc)
Accumulates an accumulator (e.g.
Definition: FPGATrackSimMatrixAccumulator.cxx:22
PlotCalibFromCool.begin
begin
Definition: PlotCalibFromCool.py:94
MultiRange::size_type
ExpandedIdentifier::size_type size_type
Definition: MultiRange.h:21
ExpandedIdentifier
Definition: DetectorDescription/Identifier/Identifier/ExpandedIdentifier.h:102
MultiRange::const_identifier_factory::m_range_it
range_vector::const_iterator m_range_it
Definition: MultiRange.h:68
MultiRange::identifier_factory
This factory is able to generate all possible identifiers, from a fully bounded Range.
Definition: MultiRange.h:30
MultiRange::identifier_factory::m_range_it
range_vector::const_iterator m_range_it
Definition: MultiRange.h:46
mergePhysValFiles.end
end
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:93
MultiRange::m_ranges
range_vector m_ranges
Definition: MultiRange.h:131
MultiRange::const_identifier_factory::m_range_end
range_vector::const_iterator m_range_end
Definition: MultiRange.h:69
MultiRange::const_identifier_factory::m_id_fac_end
ConstRangeIterator m_id_fac_end
Definition: MultiRange.h:67
MultiRange::const_identifier_factory
Definition: MultiRange.h:52
lumiFormat.i
int i
Definition: lumiFormat.py:85
MultiRange::remove_range
void remove_range(const ExpandedIdentifier &id)
Remove a Range made from a single ExpandedIdentifier.
Definition: MultiRange.cxx:43
MultiRange::const_identifier_factory::const_identifier_factory
const_identifier_factory()=default
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:195
MultiRange::const_identifier_factory::m_id
ExpandedIdentifier m_id
Definition: MultiRange.h:65
MultiRange::begin
const_iterator begin() const
Definition: MultiRange.cxx:74
ConstRangeIterator::begin
ConstRangeIterator begin() const
Definition: RangeIterator.cxx:18
MultiRange::identifier_factory::identifier_factory
identifier_factory()=default
MultiRange::back
Range & back()
Get the last entered Range.
Definition: MultiRange.cxx:52
MultiRange::has_overlap
bool has_overlap() const
Check if there are overlaps between any couple of Ranges.
Definition: MultiRange.cxx:98
MultiRange
A MultiRange combines several Ranges.
Definition: MultiRange.h:17
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:77
Range
A Range describes the possible ranges for the field values of an ExpandedIdentifier.
Definition: DetectorDescription/Identifier/Identifier/Range.h:29
MultiRange::identifier_factory::m_id_fac_it
ConstRangeIterator m_id_fac_it
Definition: MultiRange.h:44
MultiRange::const_iterator
range_vector::const_iterator const_iterator
Definition: MultiRange.h:22
python.PyKernel.init
def init(v_theApp, v_rootStream=None)
Definition: PyKernel.py:45
MultiRange.h
MultiRange::MultiRange
MultiRange()=default
MultiRange::match
int match(const ExpandedIdentifier &id) const
Match an identifier.
Definition: MultiRange.cxx:57
MultiRange::const_identifier_factory::operator*
const ExpandedIdentifier & operator*() const
Definition: MultiRange.cxx:227
MultiRange::const_identifier_factory::operator++
void operator++()
Definition: MultiRange.cxx:206
a
TList * a
Definition: liststreamerinfos.cxx:10
InDetDD::other
@ other
Definition: InDetDD_Defs.h:16
MultiRange::end
const_iterator end() const
Definition: MultiRange.cxx:78
MultiRange::size
size_type size() const
Definition: MultiRange.cxx:70
MultiRange::identifier_factory::m_range_end
range_vector::const_iterator m_range_end
Definition: MultiRange.h:47
MultiRange::show
void show() const
Definition: MultiRange.cxx:237
MultiRange::identifier_factory::operator*
const ExpandedIdentifier & operator*() const
Definition: MultiRange.cxx:176
ConstRangeIterator::end
ConstRangeIterator end() const
Definition: RangeIterator.cxx:32
MultiRange::cardinality
size_type cardinality() const
Computes a possible cardinality from all ranges.
Definition: MultiRange.cxx:82
MultiRange::identifier_factory::m_id
ExpandedIdentifier m_id
Definition: MultiRange.h:43