Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v38r1p1 (ae26267b)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
TupleAlg.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 <limits>
18 #include <numeric>
19 // ============================================================================
20 // CLHEP
21 // ============================================================================
22 #include "CLHEP/Matrix/Matrix.h"
23 #include "CLHEP/Matrix/Vector.h"
24 // ============================================================================
25 // GaudiKernel
26 // ============================================================================
29 // ============================================================================
30 // GaudiAlg
31 // ============================================================================
32 #include "GaudiAlg/GaudiTupleAlg.h"
33 #include "GaudiAlg/Tuples.h"
34 // ============================================================================
35 
36 // Handle CLHEP 2.0.x move to CLHEP namespace
37 namespace CLHEP {}
38 using namespace CLHEP;
39 
40 // CLHEP is just #()$)*#)*@#)$@ Not even the git master (as of Aug 2015) has HepVector::begin and HepVector::end
41 // defined!!!
42 // fortunately, ADL comes to the rescue...
43 namespace CLHEP {
44  class HepVector;
45  double* begin( CLHEP::HepVector& v ) { return &v[0]; }
46  const double* begin( const CLHEP::HepVector& v ) { return &v[0]; }
47 } // namespace CLHEP
48 
49 // ============================================================================
59 // ============================================================================
60 class TupleAlg : public GaudiTupleAlg {
61 public:
64  using GaudiTupleAlg::GaudiTupleAlg;
65 
67  StatusCode initialize() override {
69  if ( sc.isFailure() ) return sc;
70  // check for random numbers service
71  Assert( randSvc() != 0, "Random Service is not available!" );
72  //
73  return StatusCode::SUCCESS;
74  }
78  StatusCode execute() override;
79 
80  // copy constructor is disabled
81  TupleAlg( const TupleAlg& ) = delete;
82  // assignment operator is disabled
83  TupleAlg& operator=( const TupleAlg& ) = delete;
84 
85 private:
86  // Make a random generator for a type
87  template <class T>
89  const T min = std::numeric_limits<T>::min();
90  const T max = std::numeric_limits<T>::max();
91  return min + (T)( ( max - min ) * (double)( Rndm::Numbers( randSvc(), Rndm::Flat( 0, 1 ) )() ) );
92  }
93 };
94 
95 // ============================================================================
99 // ============================================================================
102  using namespace Tuples;
103 
104  Rndm::Numbers gauss( randSvc(), Rndm::Gauss( 0.0, 1.0 ) );
105  Rndm::Numbers flat( randSvc(), Rndm::Flat( -10.0, 10.0 ) );
106  Rndm::Numbers expo( randSvc(), Rndm::Exponential( 1.0 ) );
107  Rndm::Numbers breit( randSvc(), Rndm::BreitWigner( 0.0, 1.0 ) );
108  Rndm::Numbers poisson( randSvc(), Rndm::Poisson( 2.0 ) );
109  Rndm::Numbers binom( randSvc(), Rndm::Binomial( 8, 0.25 ) );
110 
111  // ==========================================================================
112  // book and fill simple Row-wise NTuple with scalar items only
113  // use a numeric ID
114  // ==========================================================================
115  Tuple tuple1 = nTuple( 1, "Trivial Row-Wise Tuple", CLID_RowWiseTuple );
116 
117  // fill N-Tuple with double/float numbers:
118  tuple1->column( "gauss", gauss() ).ignore();
119  tuple1->column( "flat", flat() ).ignore();
120  tuple1->column( "expo", expo() ).ignore();
121  tuple1->column( "breit", breit() ).ignore();
122 
123  // fill N-Tuple with integer numbers:
124  tuple1->column( "poiss", (int)poisson() ).ignore();
125  tuple1->column( "binom", (int)binom() ).ignore();
126 
127  // fill N-Tuple with "reduced" integer numbers:
128  tuple1->column( "pois2", (int)poisson(), 0, 14 ).ignore();
129  tuple1->column( "bino2", (int)binom(), 0, 14 ).ignore();
130 
131  // fill N-Tuple with "boolean" numbers:
132  tuple1->column( "poisb", poisson() > 0.0 ).ignore();
133 
134  tuple1->write().ignore();
135 
136  // ==========================================================================
137  // book and fill Column-wise NTuple with "identical" content
138  // use a literal ID instead of a numeric one
139  // ==========================================================================
140  Tuple tuple2 = nTuple( "two", "Column-Wise Tuple" );
141 
142  // fill N-Tuple with double/float numbers:
143  tuple2->column( "gauss", gauss() ).ignore();
144  tuple2->column( "flat", flat() ).ignore();
145  tuple2->column( "expo", expo() ).ignore();
146  tuple2->column( "breit", breit() ).ignore();
147 
148  // fill N-Tuple with integer numbers:
149  tuple2->column( "poiss", (int)poisson() ).ignore();
150  tuple2->column( "binom", (int)binom() ).ignore();
151 
152  // fill N-Tuple with "reduced" integer numbers:
153  tuple2->column( "pois2", (int)poisson(), 0, 10 ).ignore();
154  tuple2->column( "bino2", (int)binom(), 0, 10 ).ignore();
155 
156  // fill N-Tuple with "boolean" numbers:
157  tuple2->column( "poisb", poisson() > 0.0 ).ignore();
158 
159  tuple2->write().ignore();
160 
161  // ==========================================================================
162  // book and fill Column-wise NTuple with "fixed"-size arrays/vectors
163  // use a numeric ID
164  // ==========================================================================
165  Tuple tuple3 = nTuple( 3, "Fixed-size arrays/vectors" );
166 
167  { // fill using iterator/sequence protocol
168  const size_t nCol = 50;
169  float array[nCol];
170  for ( size_t i = 0; i < nCol; ++i ) { array[i] = (float)flat(); }
171 
172  // fill with simple array/vector (fixed size):
173  tuple3->array( "arflat", array, array + nCol ).ignore();
174  }
175 
176  {
177  typedef std::vector<double> Array;
178  const size_t nCol = 62;
179  Array array( nCol );
180  for ( size_t i = 0; i < array.size(); ++i ) { array[i] = expo(); }
181 
182  // fill with simple array/vector (fixed size):
183  tuple3->array( "arexpo", array ).ignore();
184  }
185 
186  { // fill with the explicit usage of sequence length
187  const size_t nCol = 42;
188  double array[nCol];
189  for ( size_t i = 0; i < nCol; ++i ) { array[i] = gauss(); }
190 
191  // fill with simple array/vector (fixed size):
192  tuple3->array( "argau", array, nCol ).ignore();
193  }
194 
195  { // fill with the explicit usage of sequence length
196  const size_t nCol = 42;
197  CLHEP::HepVector array( nCol );
198  for ( size_t i = 0; i < nCol; ++i ) { array[i] = gauss(); }
199 
200  // fill with simple array/vector (fixed size):
201  tuple3->array( "argau2", array, nCol ).ignore();
202  }
203 
204  tuple3->write().ignore();
205 
206  // ==========================================================================
207  // book and fill Column-wise NTuple with "fixed"-size matrices
208  // use a literal ID in a sub-dir instead of a numeric one
209  // ==========================================================================
210  Tuple tuple4 = nTuple( "subdir/four", "Fixed-size matrices" );
211 
212  {
213  // fill with simple 2D-array
214  const size_t nRow = 15;
215  const size_t nCol = 5;
216 
217  double mtrx[nRow][nCol];
218 
219  for ( size_t iRow = 0; iRow < nRow; ++iRow ) {
220  for ( size_t iCol = 0; iCol < nCol; ++iCol ) { mtrx[iRow][iCol] = gauss(); }
221  }
222 
223  tuple4->matrix( "mgau", mtrx, nRow, nCol ).ignore();
224  };
225 
226  {
227  // fill with simple "pseudo-matrix"
228  typedef std::vector<double> Row;
229  typedef std::vector<Row> Mtrx;
230 
231  const size_t nRow = 26;
232  const size_t nCol = 4;
233 
234  Mtrx mtrx( nRow, Row( nCol ) );
235 
236  for ( size_t iRow = 0; iRow < nRow; ++iRow ) {
237  for ( size_t iCol = 0; iCol < nCol; ++iCol ) { mtrx[iRow][iCol] = flat(); }
238  }
239 
240  tuple4->matrix( "mflat", mtrx, nRow, nCol ).ignore();
241  };
242 
243  {
244  // fill with simple CLHEP matrix
245  const size_t nRow = 13;
246  const size_t nCol = 3;
247  CLHEP::HepMatrix mtrx( nRow, nCol );
248  for ( int iCol = 0; iCol < mtrx.num_col(); ++iCol ) {
249  for ( int iRow = 0; iRow < mtrx.num_row(); ++iRow ) { mtrx[iRow][iCol] = expo(); }
250  }
251 
252  tuple4->matrix( "mexpo", mtrx, mtrx.num_row(), mtrx.num_col() ).ignore();
253  };
254 
255  tuple4->write().ignore();
256 
257  // ==========================================================================
258  // book and fill Column-wise NTuple with variable-size arrays/vectors
259  // ==========================================================================
260  Tuple tuple5 = nTuple( 5, "Variable-size arrays/vectors" );
261 
262  {
263  const size_t num = (size_t)poisson();
266  // fill with the content of vector
267  tuple5->farray( "arr", array.begin(), array.end(), "Len1", 100 ).ignore();
268  }
269  {
270  const size_t num = (size_t)poisson();
273  // fill with functions of vector
274  tuple5
275  ->farray( { { "sinar", sinf }, { "cosar", cosf }, { "tanar", tanf } }, array.begin(), array.end(), "Len2", 100 )
276  .ignore();
277  }
278 
279  tuple5->write().ignore();
280 
281  // ==========================================================================
282  // book and fill Column-wise NTuple with variable-size matrices
283  // ==========================================================================
284  Tuple tuple6 = nTuple( "six", "Variable-size matrices" );
285 
286  { // fill with the matrix
287  const size_t num = (size_t)poisson();
288  typedef std::vector<double> Row;
289  typedef std::vector<Row> Mtrx;
290  const size_t nCol = 15;
291 
292  Mtrx mtrx( num, Row( nCol ) );
293 
294  for ( size_t iRow = 0; iRow < num; ++iRow ) {
295  for ( size_t iCol = 0; iCol < nCol; ++iCol ) { mtrx[iRow][iCol] = gauss(); }
296  }
297 
298  tuple6->fmatrix( "mgau", mtrx.begin(), mtrx.end(), nCol, "Len1", 100 ).ignore();
299  };
300 
301  { // fill with the matrix
302  const size_t num = (size_t)poisson();
303  typedef std::vector<double> Row;
304  typedef std::vector<Row> Mtrx;
305  const size_t nCol = 15;
306 
307  Mtrx mtrx( num, Row( nCol ) );
308 
309  for ( size_t iRow = 0; iRow < num; ++iRow ) {
310  for ( size_t iCol = 0; iCol < nCol; ++iCol ) { mtrx[iRow][iCol] = expo(); }
311  }
312 
313  tuple6
314  ->fmatrix( "mexpo", // N-tuple entry name
315  mtrx, // matrix
316  mtrx.size(), // number of rows (variable)
317  nCol, // number of columns (fixed!)
318  "Len2", 100 )
319  .ignore();
320  };
321 
322  { // fill with the cross-product of functionXdata vectors
323  const size_t num = (size_t)poisson();
324 
325  typedef std::vector<double> Array;
326  Array array( num );
327  std::generate( array.begin(), array.end(), flat );
328 
329  typedef double ( *fun )( double );
330  typedef std::vector<fun> Funs;
331  Funs funs{ sin, cos, tan, sinh, cosh, tanh };
332 
333  tuple6
334  ->fmatrix( "m3flat", // N-tuple entry name
335  funs.begin(), funs.end(), array.begin(), array.end(), "Len3", 100 )
336  .ignore();
337  };
338 
339  { // fill with the matrix
340  const size_t num = (size_t)poisson();
341 
342  const size_t nCol = 15;
343 
344  CLHEP::HepMatrix mtrx( num, nCol );
345 
346  for ( size_t iRow = 0; iRow < num; ++iRow ) {
347  for ( size_t iCol = 0; iCol < nCol; ++iCol ) { mtrx[iRow][iCol] = expo(); }
348  }
349 
350  tuple6
351  ->fmatrix( "m2expo", // N-tuple entry name
352  mtrx, // matrix
353  mtrx.num_row(), // number of rows (variable)
354  mtrx.num_col(), // number of columns (fixed!)
355  "Len4", 100 )
356  .ignore();
357  };
358 
359  tuple6->write().ignore();
360 
361  // ============================================================================
362 
363  static unsigned long long evtID( 1e14 );
364  ++evtID;
365 
366  // Test for unsupported data types
367  Tuple tuple7 = nTuple( "typesCW", "Types Test Column Wise" );
368  {
369  tuple7->column( "bool", (bool)0 < flat() ).ignore();
370  tuple7->column( "float", (float)gauss() ).ignore();
371  tuple7->column( "double", (double)gauss() ).ignore();
372  tuple7->column( "short", (short)randomRange<char>() ).ignore();
373  tuple7->column( "ushort", (unsigned short)randomRange<unsigned char>() ).ignore();
374  tuple7->column( "int", (int)randomRange<char>() ).ignore();
375  tuple7->column( "uint", (unsigned int)randomRange<unsigned char>() ).ignore();
376  tuple7->column( "long", (long)randomRange<char>() ).ignore();
377  tuple7->column( "ulong", (unsigned long)randomRange<unsigned char>() ).ignore();
378  tuple7->column( "longlong", (long long)randomRange<char>() ).ignore();
379  tuple7->column( "ulonglong", (unsigned long long)randomRange<unsigned char>() ).ignore();
380  tuple7->column( "char", randomRange<char>() ).ignore();
381  tuple7->column( "uchar", randomRange<unsigned char>() ).ignore();
382  tuple7->column( "EventID", evtID ).ignore();
383  }
384  tuple7->write().ignore();
385 
386  // Test for unsupported data types
387  Tuple tuple8 = nTuple( "typesRW", "Types Test Row Wise", CLID_RowWiseTuple );
388  {
389  tuple8->column( "bool", (bool)0 < flat() ).ignore();
390  tuple8->column( "float", (float)gauss() ).ignore();
391  tuple8->column( "double", (double)gauss() ).ignore();
392  tuple8->column( "short", (short)randomRange<char>() ).ignore();
393  tuple8->column( "ushort", (unsigned short)randomRange<unsigned char>() ).ignore();
394  tuple8->column( "int", (int)randomRange<char>() ).ignore();
395  tuple8->column( "uint", (unsigned int)randomRange<unsigned char>() ).ignore();
396  tuple8->column( "long", (long)randomRange<char>() ).ignore();
397  tuple8->column( "ulong", (unsigned long)randomRange<unsigned char>() ).ignore();
398  tuple8->column( "longlong", (long long)randomRange<char>() ).ignore();
399  tuple8->column( "ulonglong", (unsigned long long)randomRange<unsigned char>() ).ignore();
400  tuple8->column( "char", randomRange<char>() ).ignore();
401  tuple8->column( "uchar", randomRange<unsigned char>() ).ignore();
402  tuple8->column( "EventID", evtID ).ignore();
403  }
404  tuple8->write().ignore();
405 
406  return StatusCode::SUCCESS;
407 }
408 
409 // ============================================================================
411 // ============================================================================
412 
413 // ============================================================================
414 // The END
415 // ============================================================================
TupleAlg::TupleAlg
TupleAlg(const TupleAlg &)=delete
GaudiAlg.TupleUtils.nTuple
def nTuple(dirpath, ID, ID2=None, topdir=None, LUN="FILE1")
Definition: TupleUtils.py:84
std::generate_n
T generate_n(T... args)
TupleAlg::execute
StatusCode execute() override
the only one essential method
Definition: TupleAlg.cpp:100
Containers::Array
KeyedObjectManager< array > Array
Forward declaration of specialized redirection array object manager.
Definition: KeyedObjectManager.h:111
Tuples.h
RndmGenerators.h
TupleAlg::initialize
StatusCode initialize() override
initialize the algorithm
Definition: TupleAlg.cpp:67
Tuples::TupleObj::column
StatusCode column(std::string_view name, float value)
Set the value for selected tuple column.
Definition: TupleObj.cpp:267
Tuples::TupleObj::matrix
StatusCode matrix(std::string_view name, const MATRIX &data, const MIndex &rows, const MIndex &cols)
fill N-Tuple with fixed-size matrix
Definition: TupleObj.h:1699
std::vector< double >
std::back_inserter
T back_inserter(T... args)
IRndmGenSvc.h
GaudiTupleAlg
Definition: GaudiTupleAlg.h:51
std::generate
T generate(T... args)
max
EventIDBase max(const EventIDBase &lhs, const EventIDBase &rhs)
Definition: EventIDBase.h:225
Tuples::TupleObj::array
StatusCode array(std::string_view name, DATA first, DATA last)
fill N-Tuple with fixed-size array
Definition: TupleObj.h:1544
Rndm::Flat
Parameters for the flat random number generation within boundaries [minimum, maximum].
Definition: RndmGenerators.h:253
Tuples
Definition: Maps.h:43
GaudiTupleAlg.h
StatusCode
Definition: StatusCode.h:65
Rndm::Gauss
Parameters for the Gauss random number generation.
Definition: RndmGenerators.h:32
Rndm::Numbers
Random number accessor This small class encapsulates the use of the random number generator.
Definition: RndmGenerators.h:359
GaudiPython.Pythonizations.execute
execute
Definition: Pythonizations.py:578
CLHEP::begin
double * begin(CLHEP::HepVector &v)
Definition: TupleAlg.cpp:45
Rndm::BreitWigner
Parameters for the BreitWigner distributed random number generation.
Definition: RndmGenerators.h:94
Tuples::TupleObj::fmatrix
StatusCode fmatrix(std::string_view name, const MATRIX &data, size_t rows, const MIndex &cols, std::string_view length, size_t maxv)
Fill N-Tuple with data from variable-size matrix.
Definition: TupleObj.h:1303
Tuples::Tuple
A simple wrapper class over standard Gaudi NTuple::Tuple facility.
Definition: Tuple.h:126
CLHEP
Definition: TupleDecorator.h:29
min
EventIDBase min(const EventIDBase &lhs, const EventIDBase &rhs)
Definition: EventIDBase.h:212
StatusCode::ignore
const StatusCode & ignore() const
Allow discarding a StatusCode without warning.
Definition: StatusCode.h:139
std::numeric_limits::min
T min(T... args)
StatusCode::isFailure
bool isFailure() const
Definition: StatusCode.h:129
Gaudi::Units::gauss
constexpr double gauss
Definition: SystemOfUnits.h:252
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
Containers::array
struct GAUDI_API array
Parametrisation class for redirection array - like implementation.
Definition: KeyedObjectManager.h:37
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginServiceV1.h:46
TupleAlg::operator=
TupleAlg & operator=(const TupleAlg &)=delete
Tuples::TupleObj::write
StatusCode write()
write a record to NTuple
Definition: TupleObj.cpp:245
Properties.v
v
Definition: Properties.py:122
Tuples::TupleObj::farray
StatusCode farray(std::string_view name, ITERATOR1 &&first, ITERATOR2 &&last, std::string_view length, size_t maxv)
Add an indexed array (of type float) to N-tuple.
Definition: TupleObj.h:886
Rndm::Exponential
Parameters for the Gauss random number generation.
Definition: RndmGenerators.h:56
Rndm::Poisson
Parameters for the Poisson distributed random number generation with a given mean.
Definition: RndmGenerators.h:209
GaudiTuples< GaudiHistoAlg >::initialize
StatusCode initialize() override
standard initialization method
Definition: GaudiTuples.icpp:47
std::numeric_limits::max
T max(T... args)
Rndm::Binomial
Parameters for the Binomial distributed random number generation.
Definition: RndmGenerators.h:230
TupleAlg
Definition: TupleAlg.cpp:60
TupleAlg::randomRange
T randomRange()
Definition: TupleAlg.cpp:88
GaudiPython::Row
Vector Row
Definition: Vector.h:29