PODIO v00-16-03
An Event-Data-Model Toolkit for High Energy Physics Experiments
Loading...
Searching...
No Matches
podio::GenericParameters Class Reference

#include <GenericParameters.h>

Public Types

template<typename T >
using MapType = std::map< std::string, std::vector< T > >
 

Public Member Functions

 GenericParameters ()
 
 GenericParameters (const GenericParameters &)
 
GenericParametersoperator= (const GenericParameters &)=delete
 
 GenericParameters (GenericParameters &&)=default
 GenericParameters are default moveable.
 
GenericParametersoperator= (GenericParameters &&)=default
 
 ~GenericParameters ()=default
 
template<typename T , typename = EnableIfValidGenericDataType<T>>
GenericDataReturnType< T > getValue (const std::string &) const
 
template<typename T , typename = EnableIfValidGenericDataType<T>>
void setValue (const std::string &key, T value)
 Store (a copy of) the passed value under the given key.
 
void setValue (const std::string &key, std::string value)
 Overload for catching const char* setting for string values.
 
void setValue (const std::string &key, std::vector< std::string > values)
 Overlaod for catching initializer list setting of string vector values.
 
template<typename T , typename = std::enable_if_t<detail::isInTuple<T, SupportedGenericDataTypes>>>
void setValue (const std::string &key, std::initializer_list< T > &&values)
 Overload for catching initializer list setting for vector values.
 
template<typename T , typename = EnableIfValidGenericDataType<T>>
size_t getN (const std::string &key) const
 Get the number of elements stored under the given key for a type.
 
template<typename T , typename = EnableIfValidGenericDataType<T>>
std::vector< std::string > getKeys () const
 Get all available keys for a given type.
 
DEPRECATED_ACCESS int getIntVal (const std::string &key) const
 
DEPRECATED_ACCESS float getFloatVal (const std::string &key) const
 
DEPRECATED_ACCESS const std::string & getStringVal (const std::string &key) const
 
DEPRECATED_ACCESS IntVecgetIntVals (const std::string &key, IntVec &values) const
 
DEPRECATED_ACCESS FloatVecgetFloatVals (const std::string &key, FloatVec &values) const
 
DEPRECATED_ACCESS StringVecgetStringVals (const std::string &key, StringVec &values) const
 
DEPRECATED_ACCESS const StringVecgetIntKeys (StringVec &keys) const
 
DEPRECATED_ACCESS const StringVecgetFloatKeys (StringVec &keys) const
 
DEPRECATED_ACCESS const StringVecgetStringKeys (StringVec &keys) const
 
DEPRECATED_ACCESS int getNInt (const std::string &key) const
 
DEPRECATED_ACCESS int getNFloat (const std::string &key) const
 
DEPRECATED_ACCESS int getNString (const std::string &key) const
 
DEPRECATED_ACCESS void setValues (const std::string &key, const IntVec &values)
 
DEPRECATED_ACCESS void setValues (const std::string &key, const FloatVec &values)
 
DEPRECATED_ACCESS void setValues (const std::string &key, const StringVec &values)
 
void clear ()
 erase all elements
 
void print (std::ostream &os=std::cout, bool flush=true)
 
bool empty () const
 Check if no parameter is stored (i.e. if all internal maps are empty)
 
const IntMap & getIntMap () const
 
IntMap & getIntMap ()
 
const FloatMap & getFloatMap () const
 
FloatMap & getFloatMap ()
 
const DoubleMap & getDoubleMap () const
 
DoubleMap & getDoubleMap ()
 
const StringMap & getStringMap () const
 
StringMap & getStringMap ()
 

Detailed Description

GenericParameters objects allow to store generic named parameters of type int, float and string or vectors of these types. They can be used to store (user) meta data that is run, event or collection dependent. (based on lcio::LCParameters)

Author
F. Gaede, DESY
Date
Apr 2020

Definition at line 74 of file GenericParameters.h.

Member Typedef Documentation

◆ MapType

template<typename T >
using podio::GenericParameters::MapType = std::map<std::string, std::vector<T> >

Definition at line 77 of file GenericParameters.h.

Constructor & Destructor Documentation

◆ GenericParameters() [1/3]

podio::GenericParameters::GenericParameters ( )

Definition at line 8 of file GenericParameters.cc.

8 :
9 m_intMtx(std::make_unique<std::mutex>()),
10 m_floatMtx(std::make_unique<std::mutex>()),
11 m_stringMtx(std::make_unique<std::mutex>()),
12 m_doubleMtx(std::make_unique<std::mutex>()) {
13}

◆ GenericParameters() [2/3]

podio::GenericParameters::GenericParameters ( const GenericParameters other)

GenericParameters are copyable NOTE: This is currently mainly done to keep the ROOT I/O happy, because that needs a copy constructor

Definition at line 15 of file GenericParameters.cc.

15 :
16 m_intMtx(std::make_unique<std::mutex>()),
17 m_floatMtx(std::make_unique<std::mutex>()),
18 m_stringMtx(std::make_unique<std::mutex>()),
19 m_doubleMtx(std::make_unique<std::mutex>()) {
20 {
21 // acquire all three locks at once to make sure all three internal maps are
22 // copied at the same "state" of the GenericParameters
23 auto& intMtx = other.getMutex<int>();
24 auto& floatMtx = other.getMutex<float>();
25 auto& stringMtx = other.getMutex<std::string>();
26 auto& doubleMtx = other.getMutex<double>();
27 std::scoped_lock lock(intMtx, floatMtx, stringMtx, doubleMtx);
28 _intMap = other._intMap;
29 _floatMap = other._floatMap;
30 _stringMap = other._stringMap;
31 _doubleMap = other._doubleMap;
32 }
33}

◆ GenericParameters() [3/3]

podio::GenericParameters::GenericParameters ( GenericParameters &&  )
default

GenericParameters are default moveable.

◆ ~GenericParameters()

podio::GenericParameters::~GenericParameters ( )
default

Member Function Documentation

◆ clear()

void podio::GenericParameters::clear ( )
inline

erase all elements

Definition at line 199 of file GenericParameters.h.

199 {
200 _intMap.clear();
201 _floatMap.clear();
202 _stringMap.clear();
203 }

Referenced by podio::EventStore::clear(), and podio::EventStore::clearCollections().

◆ empty()

bool podio::GenericParameters::empty ( ) const
inline

Check if no parameter is stored (i.e. if all internal maps are empty)

Definition at line 208 of file GenericParameters.h.

208 {
209 return _intMap.empty() && _floatMap.empty() && _stringMap.empty();
210 }

Referenced by podio::EventStore::getEventMetaData(), and getValue().

◆ getDoubleMap() [1/2]

DoubleMap & podio::GenericParameters::getDoubleMap ( )
inline

Definition at line 238 of file GenericParameters.h.

238 {
239 return getMap<double>();
240 }

◆ getDoubleMap() [2/2]

const DoubleMap & podio::GenericParameters::getDoubleMap ( ) const
inline

Get the internal double map (necessary for serialization with SIO)

Definition at line 235 of file GenericParameters.h.

235 {
236 return getMap<double>();
237 }

Referenced by podio::readGenericParameters(), and podio::writeGenericParameters().

◆ getFloatKeys()

const StringVec & podio::GenericParameters::getFloatKeys ( StringVec keys) const

Returns a list of all keys of float parameters.

Definition at line 75 of file GenericParameters.cc.

75 {
76 for (const auto& k : getKeys<float>()) {
77 keys.push_back(k);
78 }
79 return keys;
80}

◆ getFloatMap() [1/2]

FloatMap & podio::GenericParameters::getFloatMap ( )
inline

Definition at line 228 of file GenericParameters.h.

228 {
229 return getMap<float>();
230 }

◆ getFloatMap() [2/2]

const FloatMap & podio::GenericParameters::getFloatMap ( ) const
inline

Get the internal float map (necessary for serialization with SIO)

Definition at line 225 of file GenericParameters.h.

225 {
226 return getMap<float>();
227 }

Referenced by podio::readGenericParameters(), and podio::writeGenericParameters().

◆ getFloatVal()

float podio::GenericParameters::getFloatVal ( const std::string &  key) const

Returns the first float value for the given key.

Definition at line 39 of file GenericParameters.cc.

39 {
40 return getValue<float>(key);
41}

◆ getFloatVals()

FloatVec & podio::GenericParameters::getFloatVals ( const std::string &  key,
FloatVec values 
) const

Adds all float values for the given key to values. Returns a reference to values for convenience.

Definition at line 54 of file GenericParameters.cc.

54 {
55 for (const auto v : getValue<std::vector<float>>(key)) {
56 values.push_back(v);
57 }
58 return values;
59}
GenericDataReturnType< T > getValue(const std::string &) const

◆ getIntKeys()

const StringVec & podio::GenericParameters::getIntKeys ( StringVec keys) const

Returns a list of all keys of integer parameters.

Definition at line 68 of file GenericParameters.cc.

68 {
69 for (const auto& k : getKeys<int>()) {
70 keys.push_back(k);
71 }
72 return keys;
73}

◆ getIntMap() [1/2]

IntMap & podio::GenericParameters::getIntMap ( )
inline

Definition at line 218 of file GenericParameters.h.

218 {
219 return getMap<int>();
220 }

◆ getIntMap() [2/2]

const IntMap & podio::GenericParameters::getIntMap ( ) const
inline

Get the internal int map (necessary for serialization with SIO)

Definition at line 215 of file GenericParameters.h.

215 {
216 return getMap<int>();
217 }

Referenced by podio::readGenericParameters(), and podio::writeGenericParameters().

◆ getIntVal()

int podio::GenericParameters::getIntVal ( const std::string &  key) const

Returns the first integer value for the given key.

Definition at line 35 of file GenericParameters.cc.

35 {
36 return getValue<int>(key);
37}

◆ getIntVals()

IntVec & podio::GenericParameters::getIntVals ( const std::string &  key,
IntVec values 
) const

Adds all integer values for the given key to values. Returns a reference to values for convenience.

Definition at line 47 of file GenericParameters.cc.

47 {
48 for (const auto v : getValue<std::vector<int>>(key)) {
49 values.push_back(v);
50 }
51 return values;
52}

◆ getKeys()

template<typename T , typename >
std::vector< std::string > podio::GenericParameters::getKeys

Get all available keys for a given type.

Definition at line 356 of file GenericParameters.h.

356 {
357 std::vector<std::string> keys;
358 const auto& map = getMap<T>();
359 keys.reserve(map.size());
360 {
361 auto& mtx = getMutex<T>();
362 std::lock_guard lock{mtx};
363 std::transform(map.begin(), map.end(), std::back_inserter(keys), [](const auto& pair) { return pair.first; });
364 }
365
366 return keys;
367}

◆ getN()

template<typename T , typename >
size_t podio::GenericParameters::getN ( const std::string &  key) const

Get the number of elements stored under the given key for a type.

Definition at line 345 of file GenericParameters.h.

345 {
346 const auto& map = getMap<T>();
347 auto& mtx = getMutex<T>();
348 std::lock_guard lock{mtx};
349 if (const auto it = map.find(key); it != map.end()) {
350 return it->second.size();
351 }
352 return 0;
353}

◆ getNFloat()

int podio::GenericParameters::getNFloat ( const std::string &  key) const

The number of float values stored for this key.

Definition at line 93 of file GenericParameters.cc.

93 {
94 return getN<float>(key);
95}

◆ getNInt()

int podio::GenericParameters::getNInt ( const std::string &  key) const

The number of integer values stored for this key.

Definition at line 89 of file GenericParameters.cc.

89 {
90 return getN<int>(key);
91}

◆ getNString()

int podio::GenericParameters::getNString ( const std::string &  key) const

The number of string values stored for this key.

Definition at line 97 of file GenericParameters.cc.

97 {
98 return getN<std::string>(key);
99}

◆ getStringKeys()

const StringVec & podio::GenericParameters::getStringKeys ( StringVec keys) const

Returns a list of all keys of string parameters.

Definition at line 82 of file GenericParameters.cc.

82 {
83 for (const auto& k : getKeys<std::string>()) {
84 keys.push_back(k);
85 }
86 return keys;
87}

◆ getStringMap() [1/2]

StringMap & podio::GenericParameters::getStringMap ( )
inline

Definition at line 248 of file GenericParameters.h.

248 {
249 return getMap<std::string>();
250 }

◆ getStringMap() [2/2]

const StringMap & podio::GenericParameters::getStringMap ( ) const
inline

Get the internal string map (necessary for serialization with SIO)

Definition at line 245 of file GenericParameters.h.

245 {
246 return getMap<std::string>();
247 }

Referenced by podio::readGenericParameters(), and podio::writeGenericParameters().

◆ getStringVal()

const std::string & podio::GenericParameters::getStringVal ( const std::string &  key) const

Returns the first string value for the given key.

Definition at line 43 of file GenericParameters.cc.

43 {
44 return getValue<std::string>(key);
45}

◆ getStringVals()

StringVec & podio::GenericParameters::getStringVals ( const std::string &  key,
StringVec values 
) const

Adds all float values for the given key to values. Returns a reference to values for convenience.

Definition at line 61 of file GenericParameters.cc.

61 {
62 for (const auto& v : getValue<std::vector<std::string>>(key)) {
63 values.push_back(v);
64 }
65 return values;
66}

◆ getValue()

template<typename T , typename >
GenericDataReturnType< T > podio::GenericParameters::getValue ( const std::string &  key) const

Get the value that is stored under the given key, by const reference or by value depending on the desired type

Definition at line 307 of file GenericParameters.h.

307 {
308 const auto& map = getMap<T>();
309 auto& mtx = getMutex<T>();
310 std::lock_guard lock{mtx};
311 const auto it = map.find(key);
312 // If there is no entry to the key, we just return an empty default
313 // TODO: make this case detectable from the outside
314 if (it == map.end()) {
315 static const auto empty = T{};
316 return empty;
317 }
318
319 // We have to check whether the return type is a vector or a single value
320 if constexpr (detail::isVector<T>) {
321 return it->second;
322 } else {
323 const auto& iv = it->second;
324 return iv[0];
325 }
326}
bool empty() const
Check if no parameter is stored (i.e. if all internal maps are empty)

Referenced by getFloatVals(), getIntVals(), and getStringVals().

◆ operator=() [1/2]

GenericParameters & podio::GenericParameters::operator= ( const GenericParameters )
delete

◆ operator=() [2/2]

GenericParameters & podio::GenericParameters::operator= ( GenericParameters &&  )
default

◆ print()

void podio::GenericParameters::print ( std::ostream &  os = std::cout,
bool  flush = true 
)

Definition at line 136 of file GenericParameters.cc.

136 {
137 os << "int parameters\n\n";
138 printMap(getMap<int>(), os);
139 os << "\nfloat parameters\n";
140 printMap(getMap<float>(), os);
141 os << "\ndouble parameters\n";
142 printMap(getMap<double>(), os);
143 os << "\nstd::string parameters\n";
144 printMap(getMap<std::string>(), os);
145
146 if (flush) {
147 os.flush();
148 }
149}
void printMap(const MapType &map, std::ostream &os)

◆ setValue() [1/4]

template<typename T , typename = std::enable_if_t<detail::isInTuple<T, SupportedGenericDataTypes>>>
void podio::GenericParameters::setValue ( const std::string &  key,
std::initializer_list< T > &&  values 
)
inline

Overload for catching initializer list setting for vector values.

Definition at line 123 of file GenericParameters.h.

123 {
124 setValue<std::vector<T>>(key, std::move(values));
125 }

◆ setValue() [2/4]

void podio::GenericParameters::setValue ( const std::string &  key,
std::string  value 
)
inline

Overload for catching const char* setting for string values.

Definition at line 112 of file GenericParameters.h.

112 {
113 setValue<std::string>(key, std::move(value));
114 }

◆ setValue() [3/4]

void podio::GenericParameters::setValue ( const std::string &  key,
std::vector< std::string >  values 
)
inline

Overlaod for catching initializer list setting of string vector values.

Definition at line 117 of file GenericParameters.h.

117 {
118 setValue<std::vector<std::string>>(key, std::move(values));
119 }

◆ setValue() [4/4]

template<typename T , typename >
void podio::GenericParameters::setValue ( const std::string &  key,
value 
)

Store (a copy of) the passed value under the given key.

Definition at line 329 of file GenericParameters.h.

329 {
330 auto& map = getMap<T>();
331 auto& mtx = getMutex<T>();
332
333 if constexpr (detail::isVector<T>) {
334 std::lock_guard lock{mtx};
335 map.insert_or_assign(key, std::move(value));
336 } else {
337 // Wrap the value into a vector with exactly one entry and store that
338 std::vector<T> v = {value};
339 std::lock_guard lock{mtx};
340 map.insert_or_assign(key, std::move(v));
341 }
342}

Referenced by setValues(), TEST_CASE(), and write().

◆ setValues() [1/3]

void podio::GenericParameters::setValues ( const std::string &  key,
const FloatVec values 
)

Set float values for the given key.

Definition at line 105 of file GenericParameters.cc.

105 {
106 setValue(key, values);
107}
void setValue(const std::string &key, T value)
Store (a copy of) the passed value under the given key.

◆ setValues() [2/3]

void podio::GenericParameters::setValues ( const std::string &  key,
const IntVec values 
)

Set integer values for the given key.

Definition at line 101 of file GenericParameters.cc.

101 {
102 setValue(key, values);
103}

◆ setValues() [3/3]

void podio::GenericParameters::setValues ( const std::string &  key,
const StringVec values 
)

Set string values for the given key.

Definition at line 109 of file GenericParameters.cc.

109 {
110 setValue(key, values);
111}

The documentation for this class was generated from the following files: