#include <Tuple.h>
Collaboration diagram for Tuples::TupleColumn< ITEM >:
Public Member Functions | |
TupleColumn (const std::string &name, const ITEM &value) | |
const std::string & | name () const |
Return the column name. | |
const ITEM & | value () const |
Return the column value. | |
Private Member Functions | |
TupleColumn () | |
Private Attributes | |
std::string | m_name |
The column name. | |
ITEM | m_value |
The column value. |
It allows to extend the functionality of Tuples::Tuple and Tuples::TupleObj classes for your own needs, according to your own taste and without touching the classes at all. Neither the extension or the functionality through inheritance nor the extension through aggregation is used. One use the trick with template specialization of streamer operators.
Assuming one need to add into private code the N-Tuple representation of e.g. MyClass class
// 0) Class which needs N-Tuple representation class MyClass { ... double field1() const ; double field2() const ; long field3() const ; bool field4() const ; }; // 1) define specialization of operator with needed // representation template <> inline Tuples::Tuple& operator<< ( Tuples::Tuple& tuple , const Tuples::TupleColumn<MyClass>& item ) { // no action for invalid tuple if( !tuple.valid() ) { return tuple ;} tuple->column( item.name() + "field1" , item.value().field1() ); tuple->column( item.name() + "field2" , item.value().field2() ); tuple->column( item.name() + "field3" , item.value().field3() ); tuple->column( item.name() + "field4" , item.value().field4() ); return tuple ; } // 3) use the operator to 'stream' objects of type MyClass ito // N-Tuple: Tuple tuple = ... ; MyClass a = ... ; tuple << Tuples::make_column( "A" , a ) ; // operators can be chained: MyClass a1 = ... ; MyClass a2 = ... ; MyClass a3 = ... ; tuple << Tuples::make_column( "A1" , a1 ) << Tuples::make_column( "A2" , a2 ) << Tuples::make_column( "A3" , a3 ) ;
Alternatively one can use function Tuples::Column
// MyClass a1 = ... ; MyClass a2 = ... ; MyClass a3 = ... ; tuple << Tuples::Column( "A1" , a1 ) << Tuples::Column( "A2" , a2 ) << Tuples::Column( "A3" , a3 ) ;
Using this technique one can put 'any' object into NTuple and create the own representation. E.g. if the 'standard' representation of HepLorentzVector is not suitable one can create the alternative representation.
Also one can create own representations of complex classes, e.g. class MCParticle :
template <> inline Tuples::Tuple& operator<< ( Tuples::Tuple& tuple , const Tuples::TupleColumn<const MCParticle*>& item ) { if( !tuple.valid() ) { return tuple ;} const MCParticle* mcp = item.value() ; tuple->column( item.name() + "Mom" , mcp->momentum() ) ; tuple->column( item.name() + "PID" , mcp->particleID().pid() ) ; tuple->column( item.name() + "hasVX" , 0 != mcp->originVertex() ) ; }; Tuple tuple = ... ; const MCParticle* mcp = ... ; tuple << Tuples::Column( "MCP" , mcp ) ;
Definition at line 278 of file Tuple.h.
Tuples::TupleColumn< ITEM >::TupleColumn | ( | const std::string & | name, | |
const ITEM & | value | |||
) | [inline] |
Tuples::TupleColumn< ITEM >::TupleColumn | ( | ) | [private] |
const std::string& Tuples::TupleColumn< ITEM >::name | ( | ) | const [inline] |
const ITEM& Tuples::TupleColumn< ITEM >::value | ( | ) | const [inline] |
std::string Tuples::TupleColumn< ITEM >::m_name [private] |
ITEM Tuples::TupleColumn< ITEM >::m_value [private] |