ATLAS Offline Software
PrimitiveHelpers.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3 */
4 #ifndef PRIMATIVE_HELPERS_H
5 #define PRIMATIVE_HELPERS_H
6 
8 
9 #include "HDF5Utils/Writer.h"
11 #include <functional> //std::function
12 #include <type_traits> //std::is_pointer
13 #include <cmath> //NAN
14 #include <stdexcept>
15 
16 // helper functions
17 namespace detail {
18 
19  bool isCustom(const Primitive&);
20  bool isCustom(const Primitive::Type&);
21 
22  // build accessor
23  template <typename T, typename I, typename A, typename S=T>
24  std::function<T(I)> get(const std::string & source, A ass, T def) {
26  using rettype = decltype(ass(std::declval<I>()));
27  if constexpr (std::is_pointer<rettype>::value) {
28  return [acc, ass, def](I in) -> T {
29  const auto* associated = ass(in);
30  if (!associated) return def;
31  return acc(*associated);
32  };
33  } else {
34  return [acc, ass](I in) -> T { return acc(ass(in)); };
35  }
36  }
37 
38  template <typename T>
39  auto defaultAccessor = [](typename T::input_type in){ return in; };
40  template <typename T>
41  using defaultAccessor_t = decltype(defaultAccessor<T>);
42 
43  using uchar = unsigned char;
44  using uint = unsigned int;
45  using ushort = unsigned short;
46  using ull = unsigned long long;
47  using ul = unsigned long;
48  using ll = long long;
49 
50  template <typename T, typename A=defaultAccessor_t<T>>
51  void addInput(T& c, const Primitive& input, A a=defaultAccessor<T>) {
52  using I = typename T::input_type;
53  using Tp = Primitive::Type;
54  std::string s = input.source;
55  std::string t = input.target.empty() ? input.source : input.target;
57  const char cz = char(0);
58  const char cm = char(-1);
59  const char sm = short(-1);
60  switch (input.type) {
61  case Tp::PRECISION_CUSTOM: // intentional fall-through
62  case Tp::CUSTOM: throw std::logic_error("custom type unsupported");
63  case Tp::UCHAR: c.add(t, get<uchar,I,A>(s,a,cz), cz); return;
64  case Tp::CHAR: c.add(t, get<char,I,A>(s,a,cm), cm); return;
65  case Tp::USHORT: c.add(t, get<ushort,I,A>(s,a,cz), cz); return;
66  case Tp::SHORT: c.add(t, get<short,I,A>(s,a,sm), sm); return;
67  case Tp::UINT: c.add(t, get<uint,I,A>(s,a,0), 0); return;
68  case Tp::INT: c.add(t, get<int,I,A>(s,a,-1), -1); return;
69  case Tp::ULONG: c.add(t, get<ul,I,A>(s,a,0), 0); return;
70  case Tp::LONG: c.add(t, get<long,I,A>(s,a,-1), -1); return;
71  case Tp::ULL: c.add(t, get<ull,I,A>(s,a,0), 0); return;
72  case Tp::LL: c.add(t, get<ll,I,A>(s,a,-1), -1); return;
73  case Tp::HALF: c.add(t, get<float,I,A>(s,a,NAN), NAN, h); return;
74  case Tp::FLOAT: c.add(t, get<float,I,A>(s,a,NAN), NAN); return;
75  case Tp::DOUBLE: c.add(t, get<double,I,A>(s,a,NAN), NAN); return;
76  case Tp::UINT2UCHAR: c.add(t, get<uchar,I,A,uint>(s,a,0), 0); return;
77  case Tp::INT2CHAR: c.add(t, get<char,I,A,int>(s,a,cm), cm); return;
78  case Tp::UINT2USHORT: c.add(t, get<ushort,I,A,uint>(s,a,0), 0); return;
79  case Tp::INT2SHORT: c.add(t, get<short,I,A,int>(s,a,sm), sm); return;
80  case Tp::UL2ULL: c.add(t, get<ull,I,A,ul>(s,a,0),0); return;
81  }
82  }
83 }
84 
85 #endif
detail::get
std::function< T(I)> get(const std::string &source, A ass, T def)
Definition: PrimitiveHelpers.h:24
detail::ll
long long ll
Definition: PrimitiveHelpers.h:48
detail::uint
unsigned int uint
Definition: PrimitiveHelpers.h:44
xAOD::short
short
Definition: Vertex_v1.cxx:165
detail::defaultAccessor
auto defaultAccessor
Definition: PrimitiveHelpers.h:39
xAOD::char
char
Definition: TrigDecision_v1.cxx:38
Primitive
Definition: Primitive.h:10
athena.value
value
Definition: athena.py:124
detail
Definition: extract_histogram_tag.cxx:14
detail::addInput
void addInput(T &c, const Primitive &input, A a=defaultAccessor< T >)
Definition: PrimitiveHelpers.h:51
SG::ConstAccessor
Helper class to provide constant type-safe access to aux data.
Definition: ConstAccessor.h:55
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
Writer.h
H5Utils::Compression::HALF_PRECISION
@ HALF_PRECISION
cm
const double cm
Definition: Simulation/ISF/ISF_FastCaloSim/ISF_FastCaloSimParametrization/tools/FCAL_ChannelMap.cxx:25
A
Primitive.h
detail::ushort
unsigned short ushort
Definition: PrimitiveHelpers.h:45
AthenaPoolTestRead.acc
acc
Definition: AthenaPoolTestRead.py:16
detail::ul
unsigned long ul
Definition: PrimitiveHelpers.h:47
detail::uchar
unsigned char uchar
Definition: PrimitiveHelpers.h:43
Primitive::Type
Type
Definition: Primitive.h:11
a
TList * a
Definition: liststreamerinfos.cxx:10
h
python.CaloAddPedShiftConfig.int
int
Definition: CaloAddPedShiftConfig.py:45
detail::isCustom
bool isCustom(const Primitive &p)
Definition: PrimitiveHelpers.cxx:10
python.SystemOfUnits.s
float s
Definition: SystemOfUnits.py:147
copySelective.source
string source
Definition: copySelective.py:31
I
#define I(x, y, z)
Definition: MD5.cxx:116
python.compressB64.c
def c
Definition: compressB64.py:93
detail::defaultAccessor_t
decltype(defaultAccessor< T >) defaultAccessor_t
Definition: PrimitiveHelpers.h:41
AuxElement.h
Base class for elements of a container that can have aux data.
detail::ull
unsigned long long ull
Definition: PrimitiveHelpers.h:46
ass
make the histogram assessment part of the config
Definition: hcg.cxx:627