The Gaudi Framework  v38r1p1 (ae26267b)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
TupleObj.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 // ============================================================================
12 // Include files
13 // ============================================================================
14 // STD & STL
15 // ============================================================================
16 #include <algorithm>
17 #include <cstdarg>
18 #include <map>
19 // ============================================================================
20 // GaudiKernel
21 // ============================================================================
23 // ============================================================================
24 // GaudiAlg
25 // ============================================================================
26 #include "GaudiAlg/TupleObj.h"
27 #include "GaudiAlg/Tuples.h"
28 // ============================================================================
29 // Boost
30 // ============================================================================
31 #include "boost/integer_traits.hpp"
32 // ============================================================================
40 // ============================================================================
41 
42 namespace {
43 
44  template <typename T>
45  struct tuple_type_;
46 
47  template <>
48  struct tuple_type_<typename Tuples::TupleObj::Float> {
49  static constexpr const char* fmt = "F";
50  static constexpr const char* typ = "floats";
51  };
52  template <>
53  struct tuple_type_<typename Tuples::TupleObj::Double> {
54  static constexpr const char* fmt = "D";
55  static constexpr const char* typ = "doubles";
56  };
57  template <>
58  struct tuple_type_<typename Tuples::TupleObj::Bool> {
59  static constexpr const char* fmt = "I";
60  static constexpr const char* typ = "bools";
61  };
62  template <>
63  struct tuple_type_<typename Tuples::TupleObj::Char> {
64  static constexpr const char* fmt = "I";
65  static constexpr const char* typ = "chars";
66  };
67  template <>
68  struct tuple_type_<typename Tuples::TupleObj::UChar> {
69  static constexpr const char* fmt = "I";
70  static constexpr const char* typ = "uchars";
71  };
72  template <>
73  struct tuple_type_<typename Tuples::TupleObj::Short> {
74  static constexpr const char* fmt = "I";
75  static constexpr const char* typ = "shorts";
76  };
77  template <>
78  struct tuple_type_<typename Tuples::TupleObj::UShort> {
79  static constexpr const char* fmt = "I";
80  static constexpr const char* typ = "ushorts";
81  };
82  template <>
83  struct tuple_type_<typename Tuples::TupleObj::Int> {
84  static constexpr const char* fmt = "I";
85  static constexpr const char* typ = "ints";
86  };
87  template <>
88  struct tuple_type_<typename Tuples::TupleObj::UInt> {
89  static constexpr const char* fmt = "I";
90  static constexpr const char* typ = "uints";
91  };
92  template <>
93  struct tuple_type_<typename Tuples::TupleObj::LongLong> {
94  static constexpr const char* fmt = "ULL";
95  static constexpr const char* typ = "longlongs";
96  };
97  template <>
98  struct tuple_type_<typename Tuples::TupleObj::ULongLong> {
99  static constexpr const char* fmt = "ULL";
100  static constexpr const char* typ = "ulonglongs";
101  };
102  template <>
103  struct tuple_type_<typename Tuples::TupleObj::Address> {
104  static constexpr const char* fmt = "IOpaqueAddress*";
105  static constexpr const char* typ = "addresses";
106  };
107  template <>
108  struct tuple_type_<typename Tuples::TupleObj::FArray> {
109  static constexpr const char* fmt = "FArray";
110  static constexpr const char* typ = "farray";
111  };
112  template <>
113  struct tuple_type_<typename Tuples::TupleObj::FMatrix> {
114  static constexpr const char* fmt = "FMatrix";
115  static constexpr const char* typ = "fmatrix";
116  };
117 
118  // helper functions to simplify things...
119  template <typename C, typename AddItem>
120  auto create_( Tuples::TupleObj* parent, C& container, std::string_view name, AddItem addItem ) {
121  using map_t = struct tuple_type_<typename C::element_type>;
122  auto [iter, ok] = container.emplace( name );
123  if ( !ok ) { parent->Error( fmt::format( "{} ('{}'): item is not inserted", map_t::typ, name ) ).ignore(); }
124  StatusCode sc = addItem( std::string{ name }, *iter );
125  if ( sc.isFailure() ) {
126  parent->Error( fmt::format( "{} ('{}'): item is not added", map_t::typ, name ), sc ).ignore();
127  }
128  if ( !parent->addItem( std::string{ name }, map_t::fmt ) ) {
129  parent->Error( fmt::format( "{} ('{}'): item is not unique", map_t::typ, name ) ).ignore();
130  }
131  return &*iter;
132  }
133 
134  template <typename C, typename... ExtraArgs>
135  auto find_or_create( Tuples::TupleObj* parent, std::string_view name, C& map, ExtraArgs&&... ea ) {
136  auto found = map.find( name );
137  return found != map.end() ? &*found : create_( parent, map, name, [&]( std::string_view n, auto& i ) {
138  return parent->tuple()->addItem( std::string{ n }, i, std::forward<ExtraArgs>( ea )... );
139  } );
140  }
141 
142  template <typename Container, typename UT, typename... ExtraArgs>
143  StatusCode column_( Tuples::TupleObj* parent, Container& container, std::string_view name, UT&& value,
144  ExtraArgs&&... ea ) {
145  if ( parent->invalid() ) return Tuples::ErrorCodes::InvalidTuple;
146  auto item = find_or_create( parent, name, container, std::forward<ExtraArgs>( ea )... );
147  if ( !item ) return Tuples::ErrorCodes::InvalidColumn;
148  *item = std::forward<UT>( value );
149  return StatusCode::SUCCESS;
150  }
151 
152  struct TuplesCategory : StatusCode::Category {
153  const char* name() const override { return "Tuples"; }
154 
155  bool isRecoverable( StatusCode::code_t ) const override { return false; }
156 
157  std::string message( StatusCode::code_t code ) const override {
158  switch ( static_cast<Tuples::ErrorCodes>( code ) ) {
160  return "InvalidTuple";
162  return "InvalidColumn";
164  return "InvalidOperation";
166  return "InvalidObject";
168  return "InvalidItem";
170  return "TruncateValue";
171  default:
172  return StatusCode::default_category().message( code );
173  }
174  }
175  };
176 } // namespace
177 
178 STATUSCODE_ENUM_IMPL( Tuples::ErrorCodes, TuplesCategory )
179 
180 namespace Tuples {
181  namespace Local {
182  class Counter final {
183  public:
184  // constructor
185  Counter( std::string msg = " Misbalance " ) : m_message( std::move( msg ) ) {}
186  // destructor
187  ~Counter() { report(); }
188  // make the increment
189  long increment( std::string_view object ) { return ++get( object ); }
190  // make the decrement
191  long decrement( std::string_view object ) { return --get( object ); }
192  // current count
193  long counts( std::string_view object ) const { return get( object ); }
194  // make a report
195  void report() const {
196  for ( auto& entry : m_map ) {
197  if ( entry.second != 0 )
198  std::cout << "Tuples::TupleObj WARNING " << m_message << "'" << entry.first << "' Counts = " << entry.second
199  << std::endl;
200  }
201  };
202 
203  private:
204  long& get( std::string_view sv ) {
205  auto i = m_map.find( sv );
206  if ( i == m_map.end() ) { i = m_map.emplace( std::pair{ std::string{ sv }, 0 } ).first; }
207  return i->second;
208  }
209  long get( std::string_view sv ) const {
210  auto i = m_map.find( sv );
211  if ( i == m_map.end() ) throw;
212  return i->second;
213  }
216  };
217 
223  static Counter s_InstanceCounter{ " Create/Destroy (mis)balance " };
224  } // namespace Local
225 } // namespace Tuples
226 // ============================================================================
227 // Standard constructor
228 // ============================================================================
230  //
231  : m_name( std::move( name ) ), m_tuple( tuple ), m_clid( clid ), m_type( type ) {
232  // make counts
233  Tuples::Local::s_InstanceCounter.increment( m_name );
234 }
235 // ============================================================================
236 // destructor
237 // ============================================================================
239  // make counts
240  Tuples::Local::s_InstanceCounter.decrement( m_name );
241 }
242 // ============================================================================
243 // write a record to NTuple
244 // ============================================================================
246  if ( invalid() ) return ErrorCodes::InvalidTuple;
247  return tuple()->write();
248 }
249 // ============================================================================
250 
251 // ============================================================================
252 // put IOpaqueAddress in NTuple (has sense only for Event tag collection Ntuples)
253 // ============================================================================
255  if ( !evtColType() ) return ErrorCodes::InvalidOperation;
256  if ( !address )
257  return Error( fmt::format( "column('{}') IOpaqueAddress* is NULL!", name ), ErrorCodes::InvalidObject );
258  return column_( this, m_addresses, name, address );
259 }
260 
261 // ============================================================================
262 // put IOpaqueAddress in NTuple (has sense only for Event tag collection Ntuples)
263 // ============================================================================
264 StatusCode Tuples::TupleObj::column( IOpaqueAddress* address ) { return column( "Address", address ); }
265 
266 // ============================================================================
267 StatusCode Tuples::TupleObj::column( std::string_view name, float value ) {
268  return column_( this, m_floats, name, value );
269 }
270 // ============================================================================
271 StatusCode Tuples::TupleObj::column( std::string_view name, double value ) {
272  return column_( this, m_doubles, name, value );
273 }
274 // ============================================================================
275 StatusCode Tuples::TupleObj::column( std::string_view name, char value ) {
276  return column_( this, m_chars, name, value );
277 }
278 // ============================================================================
279 StatusCode Tuples::TupleObj::column( std::string_view name, char value, char minv, char maxv ) {
280  return column_( this, m_chars, name, value, minv, maxv );
281 }
282 // ============================================================================
283 StatusCode Tuples::TupleObj::column( std::string_view name, unsigned char value ) {
284  return column_( this, m_uchars, name, value );
285 }
286 // ============================================================================
287 StatusCode Tuples::TupleObj::column( std::string_view name, unsigned char value, unsigned char minv,
288  unsigned char maxv ) {
289  return column_( this, m_uchars, name, value, minv, maxv );
290 }
291 // ============================================================================
292 StatusCode Tuples::TupleObj::column( std::string_view name, short value ) {
293  return column_( this, m_shorts, name, value );
294 }
295 // ============================================================================
296 StatusCode Tuples::TupleObj::column( std::string_view name, const short value, const short minv, const short maxv ) {
297  return column_( this, m_shorts, name, value, minv, maxv );
298 }
299 // ============================================================================
300 StatusCode Tuples::TupleObj::column( std::string_view name, const unsigned short value ) {
301  return column_( this, m_ushorts, name, value );
302 }
303 // ============================================================================
304 StatusCode Tuples::TupleObj::column( std::string_view name, unsigned short value, unsigned short minv,
305  unsigned short maxv ) {
306  return column_( this, m_ushorts, name, value, minv, maxv );
307 }
308 // ============================================================================
309 StatusCode Tuples::TupleObj::column( std::string_view name, int value ) { return column_( this, m_ints, name, value ); }
310 // ============================================================================
311 StatusCode Tuples::TupleObj::column( std::string_view name, int value, int minv, int maxv ) {
312  return column_( this, m_ints, name, value, minv, maxv );
313 }
314 // ============================================================================
315 Tuples::TupleObj::Int* Tuples::TupleObj::ints( std::string_view name, int minv, int maxv ) {
316  return find_or_create( this, name, m_ints, minv, maxv );
317 }
318 // ============================================================================
319 StatusCode Tuples::TupleObj::column( std::string_view name, unsigned int value ) {
320  return column_( this, m_uints, name, value );
321 }
322 // ============================================================================
323 StatusCode Tuples::TupleObj::column( std::string_view name, unsigned int value, unsigned int minv, unsigned int maxv ) {
324  return column_( this, m_uints, name, value, minv, maxv );
325 }
326 // ============================================================================
327 StatusCode Tuples::TupleObj::column( std::string_view name, const long value ) {
328  Warning( fmt::format( "'long' has different sizes on 32/64 bit systems. Casting '{}' to 'long long'", name ),
330  .ignore();
331  return column( name, static_cast<long long>( value ) );
332 }
333 // ============================================================================
334 StatusCode Tuples::TupleObj::column( std::string_view name, const long value, const long minv, const long maxv ) {
335  Warning( fmt::format( "'long' has different sizes on 32/64 bit systems. Casting '{}' to 'long long'", name ),
337  .ignore();
338  return column( name, static_cast<long long>( value ), static_cast<long long>( minv ),
339  static_cast<long long>( maxv ) );
340 }
341 // ============================================================================
342 StatusCode Tuples::TupleObj::column( std::string_view name, const unsigned long value ) {
343  Warning( fmt::format(
344  "'unsigned long' has different sizes on 32/64 bit systems. Casting '{}' to 'unsigned long long'", name ),
346  .ignore();
347  return column( name, static_cast<unsigned long long>( value ) );
348 }
349 // ============================================================================
350 StatusCode Tuples::TupleObj::column( std::string_view name, const unsigned long value, const unsigned long minv,
351  const unsigned long maxv ) {
352  Warning( fmt::format(
353  "'unsigned long' has different sizes on 32/64 bit systems. Casting '{}' to 'unsigned long long'", name ),
355  .ignore();
356  return column( name, static_cast<unsigned long long>( value ), static_cast<unsigned long long>( minv ),
357  static_cast<unsigned long long>( maxv ) );
358 }
359 // ============================================================================
360 StatusCode Tuples::TupleObj::column( std::string_view name, const long long value ) {
361  return column_( this, m_longlongs, name, value );
362 }
363 // ============================================================================
364 StatusCode Tuples::TupleObj::column( std::string_view name, long long value, long long minv, long long maxv ) {
365  return column_( this, m_longlongs, name, value, minv, maxv );
366 }
367 // ============================================================================
368 StatusCode Tuples::TupleObj::column( std::string_view name, unsigned long long value ) {
369  return column_( this, m_ulonglongs, name, value );
370 }
371 // ============================================================================
372 StatusCode Tuples::TupleObj::column( std::string_view name, unsigned long long value, unsigned long long minv,
373  unsigned long long maxv ) {
374  return column_( this, m_ulonglongs, name, value, minv, maxv );
375 }
376 // ============================================================================
377 StatusCode Tuples::TupleObj::column( std::string_view name, bool value ) {
378  return column_( this, m_bools, name, value );
379 }
380 // ============================================================================
381 // retrieve (book on demand) array-items for ntuple
382 // ============================================================================
384  // existing array ?
385  auto found = m_farrays.find( name );
386  if ( m_farrays.end() != found ) return &*found;
387  return create_( this, m_farrays, name, [&]( std::string_view n, FArray& i ) {
388  return this->tuple()->addIndexedItem( std::string{ n }, *length, i );
389  } );
390 }
391 // ============================================================================
392 // retrieve (book on demand) array-items for ntuple (fixed)
393 // ============================================================================
395  // existing array ?
396  auto found = m_arraysf.find( name );
397  if ( m_arraysf.end() != found ) return &*found;
398  return create_( this, m_arraysf, name, [&]( std::string_view n, FArray& i ) {
399  return this->tuple()->addItem( std::string{ n }, rows, i );
400  } );
401 }
402 // ============================================================================
403 // retrieve (book on demand) matrix-items for ntuple
404 // ============================================================================
406  const Tuples::TupleObj::MIndex& cols ) {
407  // existing array ?
408  auto found = m_fmatrices.find( name );
409  if ( m_fmatrices.end() != found ) return &*found;
410  return create_( this, m_fmatrices, name, [&]( std::string_view n, FMatrix& i ) {
411  return this->tuple()->addIndexedItem( std::string{ n }, *length, cols, i );
412  } );
413 }
414 // ============================================================================
415 // retrieve (book on demand) matrix-items for ntuple (fixed)
416 // ============================================================================
418  const Tuples::TupleObj::MIndex& cols ) {
419  // existing array ?
420  auto found = m_matricesf.find( name );
421  if ( m_matricesf.end() != found ) return &*found;
422  return create_( this, m_matricesf, name, [&]( std::string_view n, FMatrix& i ) {
423  return this->tuple()->addItem( std::string{ n }, rows, cols, i );
424  } );
425 }
426 // ============================================================================
427 // The END
428 // ============================================================================
Tuples::Local::Counter::get
long & get(std::string_view sv)
Definition: TupleObj.cpp:204
std::string
STL class.
Tuples.h
Tuples::ErrorCodes::InvalidColumn
@ InvalidColumn
StatusCode::default_category
static const Category & default_category() noexcept
Default Gaudi StatusCode category.
Definition: StatusCode.h:310
AlgTools.Double
Double
Definition: AlgTools.py:21
Tuples::TupleObj::column
StatusCode column(std::string_view name, float value)
Set the value for selected tuple column.
Definition: TupleObj.cpp:267
Tuples::TupleObj::tuple
const NTuple::Tuple * tuple() const
provide the access to underlying Gaudi N-tuple
Definition: TupleObj.h:1862
std::pair
GaudiException.h
Tuples::TupleObj::Error
virtual StatusCode Error(const std::string &msg, const StatusCode sc=StatusCode::FAILURE) const =0
IOpaqueAddress
Definition: IOpaqueAddress.h:33
Tuples::TupleObj::TupleObj
TupleObj(std::string name, NTuple::Tuple *tuple, const CLID &clid=CLID_ColumnWiseTuple, const Tuples::Type type=Tuples::NTUPLE)
Standard constructor.
Definition: TupleObj.cpp:229
std::map::find
T find(T... args)
Tuples::Local::Counter::report
void report() const
Definition: TupleObj.cpp:195
Tuples::Local::Counter
Definition: TupleObj.cpp:182
GaudiMP.FdsRegistry.msg
msg
Definition: FdsRegistry.py:19
std::map::emplace
T emplace(T... args)
Tuples::ErrorCodes::TruncateValue
@ TruncateValue
Tuples::Local::Counter::Counter
Counter(std::string msg=" Misbalance ")
Definition: TupleObj.cpp:185
StatusCode::code_t
unsigned long code_t
type of StatusCode value
Definition: StatusCode.h:67
GaudiAlg.Algs.column
column
Definition: Algs.py:1212
Tuples::Local::Counter::increment
long increment(std::string_view object)
Definition: TupleObj.cpp:189
Containers::map
struct GAUDI_API map
Parametrisation class for map-like implementation.
Definition: KeyedObjectManager.h:35
Tuples::Local::Counter::~Counter
~Counter()
Definition: TupleObj.cpp:187
Tuples::ErrorCodes
ErrorCodes
Definition: TupleObj.h:103
Tuples::TupleObj::~TupleObj
virtual ~TupleObj()
Definition: TupleObj.cpp:238
Tuples
Definition: Maps.h:43
Tuples::TupleObj
A simple wrapper class over standard Gaudi NTuple::Tuple facility.
Definition: TupleObj.h:211
StatusCode
Definition: StatusCode.h:65
StatusCode::Category
Definition: StatusCode.h:78
Tuples::Local::Counter::counts
long counts(std::string_view object) const
Definition: TupleObj.cpp:193
std::cout
TupleObj.h
Tuples::ErrorCodes::InvalidObject
@ InvalidObject
AlgTools.Int
Int
Definition: AlgTools.py:21
CLID
unsigned int CLID
Class ID definition.
Definition: ClassID.h:18
format
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:119
AlgTools.Bool
Bool
Definition: AlgTools.py:21
std::map
STL class.
GaudiPluginService.cpluginsvc.n
n
Definition: cpluginsvc.py:234
Tuples::TupleObj::m_name
std::string m_name
name
Definition: TupleObj.h:1943
StatusCode::ignore
const StatusCode & ignore() const
Allow discarding a StatusCode without warning.
Definition: StatusCode.h:139
NTuple::Matrix
Class acting as a smart pointer holding a N tuple _Item.
Definition: NTuple.h:62
StatusCode::isFailure
bool isFailure() const
Definition: StatusCode.h:129
Tuples::TupleObj::ints
Int * ints(std::string_view name, int minv, int maxv)
get the column
Definition: TupleObj.cpp:315
Tuples::TupleObj::addItem
bool addItem(std::string name, std::string type)
add the item name into the list of known items
Definition: TupleObj.h:1897
gaudirun.type
type
Definition: gaudirun.py:160
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
ConditionsStallTest.name
name
Definition: ConditionsStallTest.py:76
std::endl
T endl(T... args)
Tuples::TupleObj::invalid
bool invalid() const
invalid pointer to tuple ?
Definition: TupleObj.h:1888
Tuples::ErrorCodes::InvalidTuple
@ InvalidTuple
NTuple::Tuple
Abstract base class which allows the user to interact with the actual N tuple implementation.
Definition: NTuple.h:387
Tuples::Type
Type
Definition: TupleObj.h:91
STATUSCODE_ENUM_IMPL
#define STATUSCODE_ENUM_IMPL(...)
Assign a category to the StatusCode enum declared with STATUSCODE_ENUM_DECL( ENUM )
Definition: StatusCode.h:295
Tuples::TupleObj::MIndex
unsigned short MIndex
Definition: TupleObj.h:231
std
STL namespace.
Tuples::Local::Counter::decrement
long decrement(std::string_view object)
Definition: TupleObj.cpp:191
fmt
Definition: MessageSvcSink.cpp:26
Tuples::TupleObj::write
StatusCode write()
write a record to NTuple
Definition: TupleObj.cpp:245
StatusCode::Category::name
virtual const char * name() const =0
Name of the category.
std::map::end
T end(T... args)
NTuple::Array< float >
Tuples::ErrorCodes::InvalidItem
@ InvalidItem
Tuples::TupleObj::fMatrix
FMatrix * fMatrix(std::string_view name, Int *item, const MIndex &cols)
get the column
Definition: TupleObj.cpp:405
StatusCode::Category::isRecoverable
virtual bool isRecoverable(code_t code) const
Is code considered recoverable ?
Definition: StatusCode.h:93
Tuples::ErrorCodes::InvalidOperation
@ InvalidOperation
Tuples::TupleObj::fArray
FArray * fArray(std::string_view name, Int *item)
get the column
Definition: TupleObj.cpp:383
Tuples::Local::Counter::m_map
std::map< std::string, long, std::less<> > m_map
Definition: TupleObj.cpp:214
Tuples::Local::Counter::m_message
std::string m_message
Definition: TupleObj.cpp:215
StatusCode::Category::message
virtual std::string message(code_t code) const
Description for code within this category.
Definition: StatusCode.h:86
NTuple::Item< int >
Tuples::Local::Counter::get
long get(std::string_view sv) const
Definition: TupleObj.cpp:209