Gaudi Framework, version v23r6

Home   Generated: Wed Jan 30 2013
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
NTuple.h
Go to the documentation of this file.
1 // $Id: NTuple.h,v 1.23 2008/10/27 19:22:20 marcocle Exp $
2 // ============================================================================
3 #ifndef GAUDIKERNEL_NTUPLE_H
4 #define GAUDIKERNEL_NTUPLE_H
5 // ============================================================================
6 // STL include files
7 // ============================================================================
8 #include <string>
9 #include <limits>
10 #include <cfloat>
11 #include <stdexcept>
12 // ============================================================================
13 // Framework include files
14 // ============================================================================
16 #include "GaudiKernel/DataObject.h"
17 #include "GaudiKernel/INTuple.h"
20 // ============================================================================
21 // Forward declarations
22 // ============================================================================
23 class NTupleFile;
24 class NTupleDirectory;
25 // ============================================================================
34 namespace NTuple
35 {
36  // local forward declarations
37  template <class TYP> class Range;
38  template <class TYP> class _Data;
39  template <class TYP> class _Item;
40  template <class TYP> class _Array;
41  template <class TYP> class _Matrix;
42  template <class TYP> class _Accessor;
43  template <class TYP> class Item;
44  template <class TYP> class Array;
45  template <class TYP> class Matrix;
46  // ==========================================================================
48  template <class TYP>
49  class Range {
51  /*const*/ TYP m_lower;
53  /*const*/ TYP m_upper;
54  public:
56  Range(const TYP low, const TYP upper) : m_lower(low), m_upper(upper) {
57  }
59  Range(const Range<TYP>& copy) : m_lower(copy.m_lower),
60  m_upper(copy.m_upper) {
61  }
63  Range& operator=(const Range<TYP>& copy) {
64  m_lower = copy.m_lower;
65  m_upper = copy.m_upper;
66  return *this;
67  }
69  virtual ~Range() { }
71  TYP lower() const { return m_lower; }
73  TYP upper() const { return m_upper; }
75  TYP distance() const { return m_upper-m_lower; }
77  static TYP min() { return std::numeric_limits<TYP>::min() ; }
79  static TYP max() { return std::numeric_limits<TYP>::max() ; }
80  };
81  // ==========================================================================
82  template <> class Range<bool>
83  {
84  public:
86  Range(const bool /* low */ , const bool /* upper */ ) {}
88  Range(const Range<bool>& /* copy */ ) {}
90  virtual ~Range() { }
92  bool lower() const { return false; }
94  bool upper() const { return true; }
96  bool distance() const { return true; }
98  static bool min() { return false; }
100  static bool max() { return true; }
101  };
102  // ==========================================================================
103  template <>
104  inline IOpaqueAddress*
106  template <>
107  inline IOpaqueAddress*
108  Range<IOpaqueAddress* >::max() { return (IOpaqueAddress*)0xffffffff ; }
109  // ==========================================================================
112  template <class TYP> class GAUDI_API _Data : virtual public INTupleItem {
113  protected:
115  TYP* m_buffer;
116  public:
120  virtual void setDefault(const TYP d) = 0;
122  virtual const ItemRange& range() const = 0;
123  };
124  // ==========================================================================
127  template <class TYP> class GAUDI_API _Item : virtual public _Data<TYP> {
128  public:
130  virtual ~_Item() {}
132  static _Item* create(INTuple* tup,
133  const std::string& name,
134  const std::type_info& info,
135  TYP min,
136  TYP max,
137  TYP def);
139  template <class T>
140  _Item<TYP>& operator=(const _Item<T>& copy) {
141  *this->m_buffer = copy.get();
142  return *this;
143  }
145  void set(const TYP& item) { *this->m_buffer = item; }
147  virtual TYP get() const { return *this->m_buffer; }
148  };
149  // ==========================================================================
152  template <class TYP> class GAUDI_API _Array : virtual public _Data<TYP> {
153  public:
155  static _Array* create(INTuple* tup,
156  const std::string& name,
157  const std::type_info& info,
158  const std::string& index,
159  long len,
160  TYP min,
161  TYP max,
162  TYP def);
164  template <class T>
166  long len = this->length();
167  if ( len == copy.length() ) {
168  const T* source = (const T*)copy.buffer();
169  for ( int i = 0; i < len; i++ ) {
170  *(this->m_buffer + i) = *(source + i);
171  }
172  return *this;
173  }
174  throw std::out_of_range
175  ("N-tuple matrix cannot be copied! The index range does not match!");
176  return *this;
177  }
179  const TYP& data(long i) const { return *(this->m_buffer + i); }
181  TYP& data(long i) { return *(this->m_buffer + i); }
182  };
183  // ==========================================================================
186  template <class TYP> class GAUDI_API _Matrix : virtual public _Data<TYP> {
187  protected:
189  long m_rows;
190  public:
192  static _Matrix* create(INTuple* tup,
193  const std::string& name,
194  const std::type_info& info,
195  const std::string& index,
196  long ncol,
197  long nrow,
198  TYP min,
199  TYP max,
200  TYP def);
202  template <class T>
204  long len = this->length();
205  if ( len == copy.length() ) {
206  const T* source = (const T*)copy.buffer();
207  for ( int i = 0; i < len; i++ ) {
208  *(this->m_buffer + i) = *(source + i);
209  }
210  return *this;
211  }
212  throw std::out_of_range
213  ("N-tuple matrix cannot be copied! The index range does not match!");
214  return *this;
215  }
217  TYP* column(long i) { return (this->m_buffer + i*m_rows); }
219  const TYP* column(long i) const { return (this->m_buffer + i*m_rows); }
220  };
221  // ==========================================================================
224  template <class TYP> class _Accessor {
225  friend class Tuple;
226  private:
228  return *this;
229  }
230  protected:
232  mutable TYP* m_ptr;
233  public:
235  _Accessor() : m_ptr(0) { }
237  virtual ~_Accessor() { }
239  bool operator !() const { return m_ptr != 0; }
241  operator const void*() const { return m_ptr; }
243  TYP* operator->() { return m_ptr; }
245  const TYP* operator->() const { return m_ptr; }
247  const Range<TYP>& range() const { return m_ptr->range(); }
248  };
249  // ==========================================================================
252  template <class TYP> class Item : virtual public _Accessor< _Item<TYP> > {
253  typedef Item<TYP> _My;
254  public:
256  Item() { }
258  operator const TYP () const { return this->m_ptr->get(); }
260  TYP operator*() { return this->m_ptr->get(); }
262  const TYP operator*() const { return this->m_ptr->get(); }
263  // Arithmetic operators defined on NTuple column entries
264  Item& operator ++ () { return *this += TYP(1); }
265  Item& operator ++ (int) { return *this += TYP(1); }
266  Item& operator -- () { return *this -= TYP(1); }
267  Item& operator -- (int) { return *this -= TYP(1); }
269  Item& operator=(const TYP data) {
270  this->m_ptr->set( data );
271  return *this;
272  }
274  template<class T>
275  Item& operator=(const Item<T>& data) {
276  this->m_ptr->set( data->get() );
277  return *this;
278  }
279  Item<TYP>& operator += (const TYP data) {
280  this->m_ptr->set ( this->m_ptr->get() + data );
281  return *this;
282  }
283  Item<TYP>& operator -= (const TYP data) {
284  this->m_ptr->set ( this->m_ptr->get() - data );
285  return *this;
286  }
287  Item<TYP>& operator *= (const TYP data) {
288  this->m_ptr->set ( this->m_ptr->get() * data );
289  return *this;
290  }
291  Item<TYP>& operator /= (const TYP data) {
292  this->m_ptr->set ( this->m_ptr->get() / data );
293  return *this;
294  }
295  };
296  // ==========================================================================
299  template <> class Item<bool> : virtual public _Accessor< _Item<bool> > {
300  typedef Item<bool> _My;
301  public:
303  Item() { }
305  operator bool () const { return this->m_ptr->get(); }
307  Item& operator=(const bool data) {
308  this->m_ptr->set( data );
309  return *this;
310  }
312  template<class T>
313  Item& operator=(const Item<T>& data) {
314  this->m_ptr->set( data->get() );
315  return *this;
316  }
317  };
318  // ==========================================================================
321  template <class TYP> class Array : virtual public _Accessor < _Array<TYP> > {
322  public:
324  Array() { }
326  template <class T>
327  Array& operator=(const Array<T>& copy) {
328  *(this->m_ptr) = *(copy.operator->());
329  return *this;
330  }
332  template <class T>
333  TYP& operator[] (const T i) { return this->m_ptr->data(i); }
335  template <class T>
336  const TYP& operator[] (const T i) const { return this->m_ptr->data(i); }
337  virtual ~Array() {}
338  };
339  // =========================================================================
342  template <class TYP> class Matrix : virtual public _Accessor< _Matrix<TYP> > {
343  public:
345  Matrix() { }
347  template <class T>
348  Matrix& operator=(const Matrix<T>& copy) {
349  *(this->m_ptr) = *(copy.operator->());
350  return *this;
351  }
353  template <class T>
354  TYP* operator[] (const T i) { return this->m_ptr->column(i); }
356  template <class T>
357  const TYP* operator[] (const T i) const { return this->m_ptr->column(i); }
358  virtual ~Matrix() {}
359  };
360  // =========================================================================
367  class Tuple : public DataObject, virtual public INTuple {
368 
369  protected:
371  template <class TYPE> StatusCode i_item(const std::string& name,
372  _Item<TYPE>*& result) const {
373  try {
374  result = dynamic_cast< _Item<TYPE>* > (i_find(name));
375  }
376  catch (...) {
377  result = 0;
378  }
379  return (0==result) ? StatusCode::FAILURE : StatusCode::SUCCESS;
380  }
382  template <class TYPE> StatusCode i_item(const std::string& name,
383  _Item<TYPE*>*& result) const {
384  try {
385  _Item<void*>* p = dynamic_cast< _Item<void*>* > (i_find(name));
386  result = (_Item<TYPE*>*)p;
387  }
388  catch (...) {
389  result = 0;
390  }
391  return (0==result) ? StatusCode::FAILURE : StatusCode::SUCCESS;
392  }
395  _Item<IOpaqueAddress*>*& result) const {
396  try {
397  result = dynamic_cast< _Item<IOpaqueAddress*>* > (i_find(name));
398  }
399  catch (...) {
400  result = 0;
401  }
402  return (0==result) ? StatusCode::FAILURE : StatusCode::SUCCESS;
403  }
405  template <class TYPE> StatusCode i_item(const std::string& name,
406  _Array<TYPE>*& result) const {
407  try {
408  if ( clID() == CLID_ColumnWiseTuple ) {
409  result = dynamic_cast< _Array<TYPE>* > (i_find(name));
410  }
411  }
412  catch (...) {
413  result = 0;
414  }
415  return (0==result) ? StatusCode::FAILURE : StatusCode::SUCCESS;
416  }
418  template <class TYPE> StatusCode i_item(const std::string& name,
419  _Matrix<TYPE>*& result) const {
420  try {
421  if ( clID() == CLID_ColumnWiseTuple ) {
422  result = dynamic_cast< _Matrix<TYPE>* > (i_find(name));
423  }
424  }
425  catch (...) {
426  result = 0;
427  }
428  return (0==result) ? StatusCode::FAILURE : StatusCode::SUCCESS;
429  }
431  template <class TYPE>
433  long,
434  const std::string&,
435  TYPE low,
436  TYPE high,
437  _Item<TYPE>*& result) {
438  if ( !i_find(name) ) {
439  TYPE nil;
440  nil = 0;
441  return add( result = _Item<TYPE>::create(this, name, typeid(TYPE), low, high, nil) );
442  }
443  return StatusCode::FAILURE;
444  }
446  template <class TYPE>
448  long dim,
449  const std::string& index,
450  TYPE low,
451  TYPE high,
452  _Array<TYPE>*& result) {
453  if ( !i_find(name) && clID() == CLID_ColumnWiseTuple ) {
454  return add( result = _Array<TYPE>::create(this,
455  name,
456  typeid(TYPE),
457  index,
458  dim,
459  low,
460  high,
461  TYPE(0)) );
462  }
463  return StatusCode::FAILURE;
464  }
466  template <class TYPE>
468  long dim1,
469  long dim2,
470  const std::string& index,
471  TYPE low,
472  TYPE high,
473  _Matrix<TYPE>*& result) {
474  if ( !i_find(name) && clID() == CLID_ColumnWiseTuple ) {
475  return add( result = _Matrix<TYPE>::create(this,
476  name,
477  typeid(TYPE),
478  index,
479  dim1,
480  dim2,
481  low,
482  high,
483  TYPE(0)) );
484  }
485  return StatusCode::FAILURE;
486  }
487  template <class TYPE> StatusCode
488  i_addObject(const std::string& name,_Item<TYPE*>*& result,const std::type_info& /* typ */) {
489  if ( !i_find(name) && clID() == CLID_ColumnWiseTuple ) {
490  return add( result = (_Item<TYPE*>*)_Item<void*>::create(this, name, typeid(TYPE),0,0,0) );
491  }
492  return StatusCode::FAILURE;
493  }
494 
495  public:
497  virtual ~Tuple() {
498  }
500  template <class TYPE> StatusCode item(const std::string& name,
501  Item<TYPE>& result)
502  {
503  return i_item(name, result.m_ptr);
504  }
506  template <class TYPE> StatusCode item(const std::string& name,
507  const Item<TYPE>& result) const
508  {
509  return i_item(name, result.m_ptr);
510  }
512  template <class TYPE> StatusCode
513  item(const std::string& name, Array<TYPE>& result)
514  {
515  return i_item(name, result.m_ptr);
516  }
518  template <class TYPE> StatusCode item(const std::string& name,
519  const Array<TYPE>& result) const
520  {
521  return i_item(name, result.m_ptr);
522  }
524  template <class TYPE> StatusCode item(const std::string& name,
525  Matrix<TYPE>& result)
526  {
527  return i_item(name, result.m_ptr);
528  }
529 
531  template <class TYPE> StatusCode item( const std::string& name,
532  const Matrix<TYPE>& result) const
533  {
534  return i_item(name, result.m_ptr);
535  }
536 
550  template <class TYPE> StatusCode
552  typedef Range<TYPE> _R;
553  return i_addItem(name, 1, "", _R::min(), _R::max(), itm.m_ptr);
554  }
555 
564  template <class TYPE> StatusCode
566  return i_addObject(name,itm.m_ptr,typeid(TYPE));
567  }
568 
577  StatusCode
579  typedef Range<IOpaqueAddress*> _R;
580  return i_addItem(name, 1, "", _R::min(), _R::max(), itm.m_ptr);
581  }
582 
600  template <class TYPE, class RANGE> StatusCode
602  Item<TYPE>& itm,
603  const RANGE low,
604  const RANGE high)
605  {
606  return i_addItem( name, 1, "", TYPE(low), TYPE(high), itm.m_ptr);
607  }
608 
622  template <class TYPE> StatusCode
624  long dim,
625  Array<TYPE>& array)
626  {
627  return i_addItem(name,
628  dim,
629  "",
632  array.m_ptr);
633  }
634 
654  template <class TYPE, class RANGE> StatusCode
656  long dim,
657  Array<TYPE>& array,
658  const RANGE low,
659  const RANGE high)
660  {
661  return i_addItem(name,
662  dim,
663  "",
664  TYPE(low),
665  TYPE(high),
666  array.m_ptr);
667  }
668 
699  template <class TYPE, class INDEX, class RANGE> StatusCode
701  Item<INDEX>& index,
702  Array<TYPE>& array,
703  const RANGE low,
704  const RANGE high)
705  {
706  return i_addItem( name,
707  index->range().distance(),
708  index->name(),
709  TYPE(low),
710  TYPE(high),
711  array.m_ptr);
712  }
713 
739  template <class TYPE, class INDEX, class RANGE> StatusCode
741  Item<INDEX>& index,
742  Array<TYPE>& array,
743  const RANGE low,
744  const RANGE high)
745  {
746  return i_addItem( name,
747  index->range().distance(),
748  index->name(),
749  TYPE(low),
750  TYPE(high),
751  array.m_ptr);
752  }
753 
778  template <class TYPE, class INDEX> StatusCode
780  Item<INDEX>& index,
781  Array<TYPE>& array)
782  {
783  return i_addItem( name,
784  index->range().distance(),
785  index->name(),
788  array.m_ptr);
789  }
790 
810  template <class TYPE, class INDEX> StatusCode
812  Item<INDEX>& index,
813  Array<TYPE>& array)
814  {
815  return i_addItem( name,
816  index->range().distance(),
817  index->name(),
820  array.m_ptr);
821  }
822 
840  template <class TYPE> StatusCode
842  long cols,
843  long rows,
844  Matrix<TYPE>& matrix)
845  {
846  return i_addItem(name,
847  cols,
848  rows,
849  "",
852  matrix.m_ptr);
853  }
854 
877  template <class TYPE, class RANGE> StatusCode
879  long cols,
880  long rows,
881  Matrix<TYPE>& result,
882  const RANGE low,
883  const RANGE high)
884  {
885  return i_addItem(name,
886  cols,
887  rows,
888  "",
889  TYPE(low),
890  TYPE(high),
891  result.m_ptr);
892  }
893 
919  template <class TYPE, class INDEX> StatusCode
921  Item<INDEX>& index,
922  Matrix<TYPE>& matrix,
923  long rows)
924  {
925  return i_addItem( name,
926  index->range().distance(),
927  rows,
928  index->name(),
931  matrix.m_ptr);
932  }
933 
954  template <class TYPE, class INDEX> StatusCode
956  Item<INDEX>& col_index,
957  long rows,
958  Matrix<TYPE>& matrix)
959  {
960  return i_addItem( name,
961  col_index->range().distance(),
962  rows,
963  col_index->name(),
966  matrix.m_ptr);
967  }
968 
1001  template <class TYPE, class INDEX, class RANGE> StatusCode
1003  Item<INDEX>& index,
1004  Matrix<TYPE>& matrix,
1005  long rows,
1006  const RANGE low,
1007  const RANGE high)
1008  {
1009  return i_addItem( name,
1010  index->range().distance(),
1011  rows,
1012  index->name(),
1013  TYPE(low),
1014  TYPE(high),
1015  matrix.m_ptr);
1016  }
1017 
1045  template <class TYPE, class INDEX, class RANGE> StatusCode
1047  Item<INDEX>& index,
1048  long rows,
1049  Matrix<TYPE>& matrix,
1050  const RANGE low,
1051  const RANGE high)
1052  {
1053  return i_addItem( name,
1054  index->range().distance(),
1055  rows,
1056  index->name(),
1057  TYPE(low),
1058  TYPE(high),
1059  matrix.m_ptr);
1060  }
1061  };
1062 
1065  class Directory : public DataObject {
1066  public:
1069  }
1071  virtual ~Directory() {
1072  }
1074  static const CLID& classID() {
1075  return CLID_NTupleDirectory;
1076  }
1078  virtual const CLID& clID() const {
1079  return classID();
1080  }
1081  };
1082 
1085  class File : public Directory {
1086  protected:
1092  long m_type;
1094  bool m_isOpen;
1095  public:
1096  File() : m_type(0), m_isOpen(false) {
1097  }
1099  File(long type, const std::string name, const std::string& logName)
1100  : m_name(name), m_logName(logName), m_type(type), m_isOpen(false) {
1101  }
1103  virtual ~File() {
1104  }
1106  static const CLID& classID() {
1107  return CLID_NTupleFile;
1108  }
1110  virtual const CLID& clID() const {
1111  return classID();
1112  }
1114  void setType(const long typ) {
1115  m_type = typ;
1116  }
1118  long type() const {
1119  return m_type;
1120  }
1122  const std::string& name() const {
1123  return m_name;
1124  }
1126  void setName(const std::string& nam) {
1127  m_name = nam;
1128  }
1130  const std::string& logicalName() const {
1131  return m_logName;
1132  }
1134  void setLogicalName( const std::string& l) {
1135  m_logName = l;
1136  }
1138  void setOpen(bool flag) {
1139  m_isOpen = flag;
1140  }
1142  bool isOpen() const {
1143  return m_isOpen;
1144  }
1145  };
1146  // =========================================================================
1147  // inhibit certain types by defining specialized templates which do not
1148  // allow for construction.
1149  template <>
1151  {
1152  private:
1153  Array(){}
1154  public:
1155  virtual ~Array() {}
1156  virtual void dummy() = 0;
1157  };
1158  template <>
1160  {
1161  private:
1163  public:
1164  virtual ~Matrix() {}
1165  virtual void dummy() = 0;
1166  };
1167  // =========================================================================
1168 #ifndef ALLOW_ALL_TYPES
1169 #else
1170  typedef Item<bool> BoolItem;
1171  typedef Item<char> CharItem;
1172  typedef Item<unsigned char> UCharItem;
1173  typedef Item<short> ShortItem;
1174  typedef Item<unsigned short> UShortItem;
1175  typedef Item<long> LongItem;
1176  typedef Item<long long> LongLongItem;
1177  typedef Item<unsigned long> ULongItem;
1178  typedef Item<unsigned long long> ULongLongItem;
1179  typedef Item<int> IntItem;
1180  typedef Item<unsigned int> UIntItem;
1181  typedef Item<float> FloatItem;
1182  typedef Item<double> DoubleItem;
1183  typedef Array<bool> BoolArray;
1184  typedef Array<char> CharArray;
1185  typedef Array<unsigned char> UCharArray;
1186  typedef Array<short> ShortArray;
1187  typedef Array<unsigned short> UShortArray;
1188  typedef Array<long> LongArray;
1189  typedef Array<unsigned long> ULongArray;
1190  typedef Array<int> IntArray;
1191  typedef Array<unsigned int> UIntArray;
1192  typedef Array<float> FloatArray;
1193  typedef Array<double> DoubleArray;
1194  typedef Matrix<bool> BoolMatrix;
1195  typedef Matrix<char> CharMatrix;
1196  typedef Matrix<unsigned char> UCharMatrix;
1197  typedef Matrix<short> ShortMatrix;
1198  typedef Matrix<unsigned short> UShortMatrix;
1199  typedef Matrix<long> LongMatrix;
1200  typedef Matrix<unsigned long> ULongMatrix;
1201  typedef Matrix<int> IntMatrix;
1202  typedef Matrix<unsigned int> UIntMatrix;
1203  typedef Matrix<float> FloatMatrix;
1204  typedef Matrix<double> DoubleMatrix;
1205 #endif
1206 
1207  template <class T>
1208  inline std::ostream& operator<<(std::ostream& s, const Item<T>& obj)
1209  {
1210  return s << T(obj);
1211  }
1212 } // end of namespace NTuple
1213 
1214 // Useful:
1218 
1219 #endif // GAUDIKERNEL_NTUPLE_H

Generated at Wed Jan 30 2013 17:13:39 for Gaudi Framework, version v23r6 by Doxygen version 1.8.2 written by Dimitri van Heesch, © 1997-2004