1 #ifndef GAUDIKERNEL_NTUPLE_H
2 #define GAUDIKERNEL_NTUPLE_H
14 #include "GaudiKernel/DataTypeInfo.h"
15 #include "GaudiKernel/DataObject.h"
16 #include "GaudiKernel/INTuple.h"
17 #include "GaudiKernel/SmartDataPtr.h"
18 #include "GaudiKernel/IOpaqueAddress.h"
23 class NTupleDirectory;
36 template <
class TYP>
class Range;
37 template <
class TYP>
class _Data;
38 template <
class TYP>
class _Item;
39 template <
class TYP>
class _Array;
40 template <
class TYP>
class _Matrix;
41 template <
class TYP>
class _Accessor;
42 template <
class TYP>
class Item;
43 template <
class TYP>
class Array;
44 template <
class TYP>
class Matrix;
55 Range(TYP low, TYP
upper) : m_lower(
std::move(low)), m_upper(
std::move(upper)) {
58 Range(
const Range<TYP>& copy) : m_lower(copy.m_lower),
59 m_upper(copy.m_upper) {
63 m_lower = copy.m_lower;
64 m_upper = copy.m_upper;
68 virtual ~Range() =
default;
76 static TYP
min() {
return std::numeric_limits<TYP>::min() ; }
78 static TYP
max() {
return std::numeric_limits<TYP>::max() ; }
81 template <>
class Range<bool>
85 Range(
const bool ,
const bool ) {}
87 Range(
const Range<bool>& ) {}
89 virtual ~Range() =
default;
91 bool lower()
const {
return false; }
93 bool upper()
const {
return true; }
95 bool distance()
const {
return true; }
97 static bool min() {
return false; }
99 static bool max() {
return true; }
114 TYP* m_buffer =
nullptr;
117 typedef Range<TYP> ItemRange;
119 virtual void setDefault(
const TYP d) = 0;
121 virtual const ItemRange&
range()
const = 0;
126 template <
class TYP>
class GAUDI_API _Item :
virtual public _Data<TYP> {
129 ~_Item()
override =
default;
131 static _Item* create(
INTuple* tup,
132 const std::string&
name,
133 const std::type_info& info,
139 _Item<TYP>& operator=(
const _Item<T>& copy) {
140 *this->m_buffer = copy.get();
144 void set(
const TYP&
item) { *this->m_buffer =
item; }
146 virtual TYP
get()
const {
return *this->m_buffer; }
151 template <
class TYP>
class GAUDI_API _Array :
virtual public _Data<TYP> {
154 static _Array* create(
INTuple* tup,
155 const std::string&
name,
156 const std::type_info& info,
157 const std::string& index,
164 _Array<TYP>& operator=(
const _Array<T>& copy) {
165 long len = this->length();
166 if ( len == copy.length() ) {
167 const T* source = (
const T*)copy.buffer();
168 std::copy_n(source,len,this->m_buffer);
171 throw std::out_of_range
172 (
"N-tuple matrix cannot be copied! The index range does not match!");
176 const TYP& data(
long i)
const {
return this->m_buffer[
i]; }
178 TYP& data(
long i) {
return this->m_buffer[
i]; }
180 TYP*
begin() {
return this->m_buffer; }
181 TYP*
end() {
return this->m_buffer+this->length(); }
186 template <
class TYP>
class GAUDI_API _Matrix :
virtual public _Data<TYP> {
192 static _Matrix* create(
INTuple* tup,
193 const std::string&
name,
194 const std::type_info& info,
195 const std::string& index,
203 _Matrix<TYP>& operator=(
const _Matrix<T>& copy) {
204 long len = this->length();
205 if ( len == copy.length() ) {
206 const T* source = (
const T*)copy.buffer();
207 std::copy_n(source,len,this->m_buffer);
210 throw std::out_of_range
211 (
"N-tuple matrix cannot be copied! The index range does not match!");
215 TYP* column(
long i) {
return this->m_buffer + i*m_rows; }
217 const TYP* column(
long i)
const {
return this->m_buffer + i*m_rows; }
222 template <
class TYP>
class _Accessor {
225 _Accessor<TYP>&
operator=(
const _Accessor<TYP>&) {
230 mutable TYP*
m_ptr =
nullptr;
237 bool operator !()
const {
return m_ptr != 0; }
239 operator const void*()
const {
return m_ptr; }
245 const Range<TYP>&
range()
const {
return m_ptr->range(); }
250 template <
class TYP>
class Item :
virtual public _Accessor< _Item<TYP> > {
251 typedef Item<TYP>
_My;
256 operator const TYP ()
const {
return this->m_ptr->get(); }
258 TYP
operator*() {
return this->m_ptr->get(); }
260 const TYP
operator*()
const {
return this->m_ptr->get(); }
268 this->m_ptr->set( data );
274 this->m_ptr->set( data->get() );
278 this->m_ptr->set ( this->m_ptr->get() + data );
282 this->m_ptr->set ( this->m_ptr->get() - data );
286 this->m_ptr->set ( this->m_ptr->get() * data );
290 this->m_ptr->set ( this->m_ptr->get() / data );
297 template <>
class Item<bool> :
virtual public _Accessor< _Item<bool> > {
298 typedef Item<bool>
_My;
303 operator bool ()
const {
return this->m_ptr->get(); }
306 this->m_ptr->set( data );
312 this->m_ptr->set( data->get() );
319 template <
class TYP>
class Array :
virtual public _Accessor < _Array<TYP> > {
326 *(this->
m_ptr) = *(copy.operator->());
331 TYP&
operator[] (
const T
i) {
return this->m_ptr->data(i); }
334 const TYP&
operator[] (
const T i)
const {
return this->m_ptr->data(i); }
336 TYP*
begin() {
return this->m_ptr->begin(); }
337 TYP*
end() {
return this->m_ptr->end(); }
340 ~Array()
override =
default;
345 template <
class TYP>
class Matrix :
virtual public _Accessor< _Matrix<TYP> > {
352 *(this->
m_ptr) = *(copy.operator->());
357 TYP*
operator[] (
const T i) {
return this->m_ptr->column(i); }
360 const TYP*
operator[] (
const T i)
const {
return this->m_ptr->column(i); }
375 _Item<TYPE>*& result)
const {
377 result =
dynamic_cast< _Item<TYPE>*
> (
i_find(name));
386 _Item<TYPE*>*& result)
const {
388 _Item<void*>* p =
dynamic_cast< _Item<void*>*
> (
i_find(name));
389 result = (_Item<TYPE*>*)p;
398 _Item<IOpaqueAddress*>*& result)
const {
400 result =
dynamic_cast< _Item<IOpaqueAddress*>*
> (
i_find(name));
409 _Array<TYPE>*& result)
const {
411 if (
clID() == CLID_ColumnWiseTuple ) {
412 result =
dynamic_cast< _Array<TYPE>*
> (
i_find(name));
422 _Matrix<TYPE>*& result)
const {
424 if (
clID() == CLID_ColumnWiseTuple ) {
425 result =
dynamic_cast< _Matrix<TYPE>*
> (
i_find(name));
434 template <
class TYPE>
440 _Item<TYPE>*& result) {
444 return add( result = _Item<TYPE>::create(
this, name,
typeid(TYPE), low, high, nil) );
449 template <
class TYPE>
452 const std::string& index,
455 _Array<TYPE>*& result) {
456 if ( !
i_find(name) &&
clID() == CLID_ColumnWiseTuple ) {
457 return add( result = _Array<TYPE>::create(
this,
469 template <
class TYPE>
473 const std::string& index,
476 _Matrix<TYPE>*& result) {
477 if ( !
i_find(name) &&
clID() == CLID_ColumnWiseTuple ) {
478 return add( result = _Matrix<TYPE>::create(
this,
491 i_addObject(
const std::string& name,_Item<TYPE*>*& result,
const std::type_info& ) {
492 if ( !
i_find(name) &&
clID() == CLID_ColumnWiseTuple ) {
493 return add( result = (_Item<TYPE*>*)_Item<void*>::create(
this, name,
typeid(TYPE),0,0,0) );
500 ~Tuple()
override =
default;
506 return i_item(name, result.m_ptr);
510 const Item<TYPE>& result)
const
512 return i_item(name, result.m_ptr);
516 item(
const std::string& name, Array<TYPE>& result)
518 return i_item(name, result.m_ptr);
522 const Array<TYPE>& result)
const
524 return i_item(name, result.m_ptr);
528 Matrix<TYPE>& result)
530 return i_item(name, result.m_ptr);
535 const Matrix<TYPE>& result)
const
537 return i_item(name, result.m_ptr);
554 addItem(
const std::string& name, Item<TYPE>&
itm) {
555 typedef Range<TYPE> _R;
556 return i_addItem(name, 1,
"", _R::min(), _R::max(), itm.m_ptr);
568 addItem(
const std::string& name, Item<TYPE*>& itm) {
581 addItem(
const std::string& name, Item<IOpaqueAddress*>& itm) {
582 typedef Range<IOpaqueAddress*> _R;
583 return i_addItem(name, 1,
"", _R::min(), _R::max(), itm.m_ptr);
604 addItem(
const std::string& name,
609 return i_addItem( name, 1,
"", TYPE(low), TYPE(high), itm.m_ptr);
626 addItem(
const std::string& name,
658 addItem(
const std::string& name,
702 template <
class TYPE,
class INDEX,
class RANGE>
StatusCode
703 addItem(
const std::string& name,
710 index->range().distance(),
742 template <
class TYPE,
class INDEX,
class RANGE>
StatusCode
750 index->range().distance(),
782 addItem(
const std::string& name,
787 index->range().distance(),
819 index->range().distance(),
844 addItem(
const std::string& name,
847 Matrix<TYPE>& matrix)
881 addItem(
const std::string& name,
884 Matrix<TYPE>& result,
923 addItem(
const std::string& name,
925 Matrix<TYPE>& matrix,
929 index->range().distance(),
959 Item<INDEX>& col_index,
961 Matrix<TYPE>& matrix)
964 col_index->range().distance(),
1004 template <
class TYPE,
class INDEX,
class RANGE>
StatusCode
1005 addItem(
const std::string& name,
1007 Matrix<TYPE>& matrix,
1013 index->range().distance(),
1048 template <
class TYPE,
class INDEX,
class RANGE>
StatusCode
1052 Matrix<TYPE>& matrix,
1057 index->range().distance(),
1078 return CLID_NTupleDirectory;
1081 const CLID&
clID()
const override {
1088 class File :
public Directory {
1101 File(
long type, std::string name, std::string logName)
1102 : m_name(
std::move(name)), m_logName(
std::move(logName)), m_type(type) {
1109 return CLID_NTupleFile;
1112 const CLID&
clID()
const override {
1116 void setType(
const long typ) {
1124 const std::string&
name()
const {
1128 void setName(std::string nam) {
1129 m_name = std::move(nam);
1137 m_logName = std::move(l);
1156 virtual ~
Array() =
default;
1157 virtual void dummy() = 0;
1164 virtual ~
Matrix() =
default;
1165 virtual void dummy() = 0;
1168 #ifndef ALLOW_ALL_TYPES
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;
1208 inline std::ostream& operator<<(std::ostream& s, const Item<T>& obj)
1219 #endif // GAUDIKERNEL_NTUPLE_H
const std::string & name() const
Retrun physical file name.
Item< TYP > & operator/=(const TYP data)
Item< TYP > & operator+=(const TYP data)
void setOpen(bool flag)
Set "open" flag.
KeyedObjectManager< array > Array
Forward declaration of specialized redirection array object manager.
SmartDataPtr< NTuple::File > NTupleFilePtr
TYP m_lower
Lower boundary of range.
_Accessor()=default
Standard Constructor.
Matrix()=default
Standard Constructor.
std::string m_logName
Logical file name.
StatusCode i_item(const std::string &name, _Item< TYPE > *&result) const
Locate a _Column of data to the N tuple type safe.
const CLID & clID() const override
class ID of the object
auto begin(reverse_wrapper< T > &w)
TYP & operator[](const T i)
Array operator.
virtual StatusCode add(INTupleItem *item)=0
Add an item row to the N tuple.
static TYP min()
Minimal number of data.
TYP * operator->()
Dereference operator.
SmartDataPtr< NTuple::Directory > NTupleDirPtr
std::vector< Row > Matrix
~Directory() override=default
Standard destructor.
~Array() override=default
StatusCode addIndexedItem(const std::string &name, Item< INDEX > &index, Array< TYPE > &array, const RANGE low, const RANGE high)
Add an indexed Array of data to a column wise N tuple with a range.
const Range< TYP > & range() const
Access the range.
long type() const
Return access type.
static const CLID & classID()
class ID of the object
NTuple interface class definition.
Matrix & operator=(const Matrix< T > ©)
Assignment operator.
Item< TYP > & operator-=(const TYP data)
NTuple interface class definition.
virtual const CLID & clID() const
Retrieve reference to class definition structure.
const CLID & clID() const override
class ID of the object
NamedRange_< CONTAINER > range(const CONTAINER &cnt, std::string name)
simple function to create the named range form arbitrary container
auto end(reverse_wrapper< T > &w)
This class is used for returning status codes from appropriate routines.
virtual ~Range()=default
Destructor.
~File() override=default
Standard destructor.
void setName(std::string nam)
Set access type.
void setLogicalName(std::string l)
static TYP max()
Maximal number of data.
std::string m_name
Physical file name.
StatusCode i_addObject(const std::string &name, _Item< TYPE * > *&result, const std::type_info &)
~Matrix() override=default
TYP upper() const
Upper boundary of range.
bool isOpen() const
Access "open" flag.
unsigned int CLID
Class ID definition.
bool operator!() const
Check if column is present.
void setType(const long typ)
Set access type.
bool m_isOpen
Flag to indicate wether the file was opened already.
TYP distance() const
Distance between lower and upper range.
struct GAUDI_API array
Parametrisation class for redirection array - like implementation.
TYP * operator[](const T i)
Array operator.
virtual INTupleItem * i_find(const std::string &name) const =0
Internally used by abstract classes.
Item & operator=(const TYP data)
Assignment operator.
A small class used to access easily (and efficiently) data items residing in data stores...
StatusCode addItem(const std::string &name, Item< TYPE > &itm)
Add a scalar data item a N tuple.
const std::string & name() const
Retreive DataObject name. It is the name when registered in the store.
TYP m_upper
Upper boundary of range.
Item< TYP > & operator*=(const TYP data)
Array & operator=(const Array< T > ©)
Assignment operator.
StatusCode item(const std::string &name, Item< TYPE > &result)
Locate a scalar Item of data to the N tuple type safe.
Directory()
Standard constructor.
Opaque address interface definition.
TYP * m_ptr
Pointer to instance.
Item()=default
Standard Constructor.
Array()=default
Standard Constructor.
~Tuple() override=default
Standard destructor.
A DataObject is the base class of any identifiable object on any data store.
_Accessor< TYP > & operator=(const _Accessor< TYP > &)
const std::string & logicalName() const
TYP operator*()
Dereference operator for pointers.
TYP lower() const
Lower boundary of range.
StatusCode i_addItem(const std::string &name, long, const std::string &, TYPE low, TYPE high, _Item< TYPE > *&result)
Add a _Item of data to the N tuple.
Range & operator=(const Range< TYP > ©)
Adjust ranges.
static const CLID & classID()
class ID of the object
SmartDataPtr< NTuple::Tuple > NTuplePtr
virtual ~_Accessor()=default
Standard Destructor.
Range(TYP low, TYP upper)
Standard constructor.