ATLAS Offline Software
PrimitiveHelpers.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 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 
12 #include <stdexcept>
13 
14 // helper functions
15 namespace detail {
16 
17  bool isCustom(const Primitive&);
18  bool isCustom(const Primitive::Type&);
19 
20  // build accessor
21  template <typename T, typename I, typename A, typename S=T>
22  std::function<T(I)> get(std::string source, A ass, T def) {
24  using rettype = decltype(ass(std::declval<I>()));
25  if constexpr (std::is_pointer<rettype>::value) {
26  return [acc, ass, def](I in) -> T {
27  const auto* associated = ass(in);
28  if (!associated) return def;
29  return acc(*associated);
30  };
31  } else {
32  return [acc, ass](I in) -> T { return acc(ass(in)); };
33  }
34  }
35 
36  template <typename T>
37  auto defaultAccessor = [](typename T::input_type in){ return in; };
38  template <typename T>
39  using defaultAccessor_t = decltype(defaultAccessor<T>);
40 
41  using uchar = unsigned char;
42  using uint = unsigned int;
43  using ushort = unsigned short;
44  using ull = unsigned long long;
45  using ul = unsigned long;
46  using ll = long long;
47 
48  template <typename T, typename A=defaultAccessor_t<T>>
49  void addInput(T& c, const Primitive& input, A a=defaultAccessor<T>) {
50  using I = typename T::input_type;
51  using Tp = Primitive::Type;
52  std::string s = input.source;
53  std::string t = input.target.empty() ? input.source : input.target;
55  const char cz = char(0);
56  const char cm = char(-1);
57  const char sm = short(-1);
58  switch (input.type) {
59  case Tp::PRECISION_CUSTOM: // intentional fall-through
60  case Tp::CUSTOM: throw std::logic_error("custom type unsupported");
61  case Tp::UCHAR: c.add(t, get<uchar,I,A>(s,a,cz), cz); return;
62  case Tp::CHAR: c.add(t, get<char,I,A>(s,a,cm), cm); return;
63  case Tp::USHORT: c.add(t, get<ushort,I,A>(s,a,cz), cz); return;
64  case Tp::SHORT: c.add(t, get<short,I,A>(s,a,sm), sm); return;
65  case Tp::UINT: c.add(t, get<uint,I,A>(s,a,0), 0); return;
66  case Tp::INT: c.add(t, get<int,I,A>(s,a,-1), -1); return;
67  case Tp::ULONG: c.add(t, get<ul,I,A>(s,a,0), 0); return;
68  case Tp::LONG: c.add(t, get<long,I,A>(s,a,-1), -1); return;
69  case Tp::ULL: c.add(t, get<ull,I,A>(s,a,0), 0); return;
70  case Tp::LL: c.add(t, get<ll,I,A>(s,a,-1), -1); return;
71  case Tp::HALF: c.add(t, get<float,I,A>(s,a,NAN), NAN, h); return;
72  case Tp::FLOAT: c.add(t, get<float,I,A>(s,a,NAN), NAN); return;
73  case Tp::DOUBLE: c.add(t, get<double,I,A>(s,a,NAN), NAN); return;
74  case Tp::UINT2UCHAR: c.add(t, get<uchar,I,A,uint>(s,a,0), 0); return;
75  case Tp::INT2CHAR: c.add(t, get<char,I,A,int>(s,a,cm), cm); return;
76  case Tp::UINT2USHORT: c.add(t, get<ushort,I,A,uint>(s,a,0), 0); return;
77  case Tp::INT2SHORT: c.add(t, get<short,I,A,int>(s,a,sm), sm); return;
78  case Tp::UL2ULL: c.add(t, get<ull,I,A,ul>(s,a,0),0); return;
79  }
80  }
81 }
82 
83 #endif
python.SystemOfUnits.s
int s
Definition: SystemOfUnits.py:131
xAOD::short
short
Definition: Vertex_v1.cxx:165
detail::ll
long long ll
Definition: PrimitiveHelpers.h:46
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
detail::uint
unsigned int uint
Definition: PrimitiveHelpers.h:42
detail::defaultAccessor
auto defaultAccessor
Definition: PrimitiveHelpers.h:37
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:49
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:43
detail::get
std::function< T(I)> get(std::string source, A ass, T def)
Definition: PrimitiveHelpers.h:22
PlotPulseshapeFromCool.input
input
Definition: PlotPulseshapeFromCool.py:106
AthenaPoolTestRead.acc
acc
Definition: AthenaPoolTestRead.py:16
detail::ul
unsigned long ul
Definition: PrimitiveHelpers.h:45
detail::uchar
unsigned char uchar
Definition: PrimitiveHelpers.h:41
Primitive::Type
Type
Definition: Primitive.h:11
a
TList * a
Definition: liststreamerinfos.cxx:10
h
detail::isCustom
bool isCustom(const Primitive &p)
Definition: PrimitiveHelpers.cxx:10
copySelective.source
string source
Definition: copySelective.py:32
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:39
AuxElement.h
Base class for elements of a container that can have aux data.
detail::ull
unsigned long long ull
Definition: PrimitiveHelpers.h:44
ass
make the histogram assessment part of the config
Definition: hcg.cxx:626