Belle II Software  release-05-01-25
FixedSizeNamedFloatTuple.h
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2015 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Oliver Frost *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 #pragma once
11 
12 #include <tracking/trackFindingCDC/varsets/NamedFloatTuple.h>
13 
14 #include <string>
15 #include <cassert>
16 
17 namespace Belle2 {
22  namespace TrackFindingCDC {
23 
31  constexpr
32  bool strequal(char const* s1, char const* s2)
33  {
34  return (not * s1 and not * s2) or (*s1 and * s2 and * s1 == *s2 and strequal(s1 + 1, s2 + 1));
35  }
36 
47  template <size_t nVars>
48  constexpr int index(const char* const(&names)[nVars], const char* name, size_t iName = 0)
49  {
50  return ((nVars == iName) ?
51  iName :
52  strequal(names[iName], name) ? iName : index(names, name, iName + 1));
53  }
54 
65  template<size_t nVars>
66  constexpr
67  int index(const char* (getName(int)),
68  const char* const name,
69  const size_t iName = 0)
70  {
71  return ((nVars == iName) ?
72  iName :
73  strequal(getName(iName), name) ? iName : index<nVars>(getName, name, iName + 1));
74  }
75 
76  // *INDENT-OFF*
84  template <class T, size_t N>
85  constexpr size_t size(T (&array)[N] __attribute__((unused)))
86  {
87  return N;
88  }
89  // *INDENT-ON*
90 
99  template<class ANames>
100  class FixedSizeNamedFloatTuple : public NamedFloatTuple {
101 
102  private:
104  static const size_t nVars = ANames::nVars;
105 
106  protected:
117  constexpr static int named(const char* name)
118  {
119  return index<nVars>(ANames::getName, name);
120  }
121 
122  public:
124  size_t size() const final
125  {
126  return ANames::nVars;
127  }
128 
137  int getNameIndex(const char* name) const final
138  {
139  return named(name);
140  }
141 
143  std::string getName(int iValue) const final
144  {
145  assert(iValue < (int)nVars);
146  assert(iValue >= 0);
147  return ANames::getName(iValue);
148  }
149 
150  public:
152  template<int I>
153  void set(Float_t value)
154  {
155  static_assert(nVars != I, "Requested name not found in names.");
156  assert(I < (int)nVars);
157  assert(I >= 0);
158  m_values[I] = value;
159  }
160 
162  void set(int iValue, Float_t value) final {
163  assert(iValue < (int)nVars);
164  assert(iValue >= 0);
165  m_values[iValue] = value;
166  }
167 
169  void set(const char* name, Float_t value)
170  {
171  set(named(name), value);
172  }
173 
175  template<int I>
176  Float_t get() const
177  {
178  static_assert(nVars != I, "Requested name not found in names.");
179  assert(I < (int)nVars);
180  assert(I >= 0);
181  return m_values[I];
182  }
183 
185  Float_t get(int iValue) const final
186  {
187  assert(iValue < (int)nVars);
188  assert(iValue >= 0);
189  return m_values[iValue];
190  }
191 
193  Float_t get(const char* name) const
194  {
195  return get(named(name));
196  }
197 
199  template<int I>
200  Float_t& var()
201  {
202  static_assert(nVars != I, "Requested name not found in names.");
203  assert(I < (int)nVars);
204  assert(I >= 0);
205  return m_values[I];
206  }
207 
209  Float_t& operator[](int iValue) final {
210  assert(iValue < (int)nVars);
211  assert(iValue >= 0);
212  return m_values[iValue];
213  }
214 
216  Float_t& operator[](const char* name)
217  {
218  return this->var(named(name));
219  }
220 
221  public:
223  Float_t m_values[nVars] = {};
224 
225  };
226  }
228 }
Belle2::ObjectInfo::getName
TString getName(const TObject *obj)
human-readable name (e.g.
Definition: ObjectInfo.cc:38
Belle2::TrackFindingCDC::FixedSizeNamedFloatTuple::get
Float_t get() const
Getter for the ith value. Static index version.
Definition: FixedSizeNamedFloatTuple.h:184
Belle2::TrackFindingCDC::FixedSizeNamedFloatTuple::getName
std::string getName(int iValue) const final
Getter for the ith name.
Definition: FixedSizeNamedFloatTuple.h:151
Belle2::TrackFindingCDC::FixedSizeNamedFloatTuple::set
void set(Float_t value)
Setter for the ith values. Static index version.
Definition: FixedSizeNamedFloatTuple.h:161
Belle2::TrackFindingCDC::FixedSizeNamedFloatTuple::get
Float_t get(int iValue) const final
Getter for the ith value.
Definition: FixedSizeNamedFloatTuple.h:193
Belle2::TrackFindingCDC::FixedSizeNamedFloatTuple::operator[]
Float_t & operator[](int iValue) final
Reference getter for the ith value.
Definition: FixedSizeNamedFloatTuple.h:217
Belle2::TrackFindingCDC::FixedSizeNamedFloatTuple::nVars
static const size_t nVars
Number of floating point values represented by this class.
Definition: FixedSizeNamedFloatTuple.h:112
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::TrackFindingCDC::FixedSizeNamedFloatTuple::var
Float_t & var()
Reference getter for the ith value. Static index version.
Definition: FixedSizeNamedFloatTuple.h:208
Belle2::TrackFindingCDC::FixedSizeNamedFloatTuple::size
size_t size() const final
Getter for number of floating point values represented by this class.
Definition: FixedSizeNamedFloatTuple.h:132
Belle2::TrackFindingCDC::FixedSizeNamedFloatTuple::getNameIndex
int getNameIndex(const char *name) const final
Getter for the index from a name.
Definition: FixedSizeNamedFloatTuple.h:145
Belle2::TrackFindingCDC::FixedSizeNamedFloatTuple::m_values
Float_t m_values[nVars]
Memory for nVars floating point values.
Definition: FixedSizeNamedFloatTuple.h:231
Belle2::TrackFindingCDC::FixedSizeNamedFloatTuple::named
constexpr static int named(const char *name)
Static getter for the index from the name.
Definition: FixedSizeNamedFloatTuple.h:125