| The Gaudi Framework v32r1 (f65d50dc)

TuplePut.h
Go to the documentation of this file.
1 #ifndef GAUDIALG_TUPLEPUT_H
2 #define GAUDIALG_TUPLEPUT_H 1
3 // =============================================================================
4 // Include files
5 // =============================================================================
6 #include <memory>
7 // =============================================================================
8 // GaudiKernel
9 // =============================================================================
10 #include "GaudiKernel/System.h"
11 // =============================================================================
12 // GaudiAlg
13 // =============================================================================
14 #include "GaudiAlg/TupleObj.h"
15 // ============================================================================
16 // ROOT TClass
17 // ============================================================================
18 #include "TClass.h"
19 // =============================================================================
25 // =============================================================================
26 namespace Tuples {
36 template <class VALUE>
37 class ItemStore final {
38 friend class TupleObj;
39
40 private:
41 typedef GaudiUtils::HashMap<std::string, std::unique_ptr<NTuple::Item<VALUE>>> Store;
42
43 public:
45 ItemStore() = default;
46
47 private:
49 inline NTuple::Item<VALUE>* getItem( const std::string& key, Tuples::TupleObj* tuple ) {
50 // find the item by name
51 auto ifound = m_map.find( key );
52 // existing item?
53 if ( m_map.end() != ifound ) return ifound->second.get(); // RETURN
54 // check the tuple for booking:
55 if ( !tuple ) return nullptr;
56 // check the existence of the name
57 if ( !tuple->goodItem( key ) ) {
58 tuple->Error( "ItemStore::getItem('" + key + "') item name is not unique" ).ignore();
59 return nullptr; // RETURN
60 }
61 // get the underlying object
62 NTuple::Tuple* tup = tuple->tuple();
63 if ( !tup ) {
64 tuple->Error( "ItemStore::getItem('" + key + "') invalid NTuple::Tuple*" ).ignore();
65 return nullptr; // RETURN
66 }
67 // create new item:
68 // add the newly created item into the store:
69 auto stored = m_map.emplace( key, std::make_unique<NTuple::Item<VALUE>>() );
70 if ( !stored.second ) {
71 tuple->Warning( "ItemStore::getItem('" + key + "') item already exists, new one not inserted!" ).ignore();
72 return nullptr;
73 }
74 auto& item = stored.first->second;
75 // add it into N-tuple
76 StatusCode sc = tup->addItem( key, *item ); // ATTENTION!
77 if ( sc.isFailure() ) {
78 tuple->Error( "ItemStore::getItem('" + key + "') cannot addItem", sc ).ignore();
79 m_map.erase( stored.first );
80 return nullptr; // RETURN
81 }
82 // check the name again
83 if ( !tuple->addItem( key, System::typeinfoName( typeid( VALUE ) ) ) ) {
84 tuple->Warning( "ItemStore::getItem('" + key + "') the item not unique " ).ignore();
85 m_map.erase( stored.first );
86 return nullptr;
87 }
88 //
89 return item.get(); // RETURN
90 }
91
92 // delete copy constructor and assignment
93 ItemStore( const ItemStore& ) = delete;
94 ItemStore& operator=( const ItemStore& ) = delete;
95
96 private:
98 Store m_map;
99 };
100 } // end of namespace Tuples
101 // =============================================================================
111 // =============================================================================
112 template <class TYPE>
113 inline StatusCode Tuples::TupleObj::put( const std::string& name, const TYPE* obj ) {
114 if ( invalid() ) { return ErrorCodes::InvalidTuple; } // RETURN
115 if ( !evtColType() ) { return ErrorCodes::InvalidOperation; } // RETURN
116
117 // static block: The type description & the flag
118 static bool s_fail = false; // STATIC
119 static TClass* s_type = nullptr; // STATIC
120 // check the status
121 if ( s_fail ) {
122 return ErrorCodes::InvalidItem;
123 } // RETURN
124 else if ( !s_type ) {
125 s_type = TClass::GetClass( typeid( TYPE ) );
126 if ( !s_type ) {
127 s_fail = true;
128 return Error( " put('" + name + "'," + System::typeinfoName( typeid( TYPE ) ) + ") :Invalid ROOT Type",
129 ErrorCodes::InvalidItem ); // RETURN
130 }
131 }
132 // the local storage of items
133 static Tuples::ItemStore<TYPE*> s_map;
134 // get the variable by name:
135 auto item = s_map.getItem( name, this );
136 if ( !item ) { return Error( " put('" + name + "'): invalid item detected", ErrorCodes::InvalidItem ); }
137 // assign the item!
138 ( *item ) = const_cast<TYPE*>( obj ); // THATS ALL!!
139 //
140 return StatusCode::SUCCESS; // RETURN
141 }
142 // ============================================================================
143
144 // ============================================================================
145 // The END
146 // ============================================================================
147 #endif // GAUDIALG_TUPLEPUT_H
System.h
gaudiComponentHelp.name
name
Definition: gaudiComponentHelp.py:41
TupleObj.h
Header file for class TupleObj.
System::typeinfoName
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:298
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
Tuples::ItemStore::ItemStore
ItemStore()=default
constructor : create empty map
Tuples::TupleObj::invalid
bool invalid() const
invalid pointer to tuple ?
Definition: TupleObj.h:1877
Tuples::ItemStore::Store
GaudiUtils::HashMap< std::string, std::unique_ptr< NTuple::Item< VALUE > > > Store
Definition: TuplePut.h:41
Tuples::ErrorCodes::InvalidItem
Tuples::TupleObj::evtColType
bool evtColType() const
Event collection ?
Definition: TupleObj.h:1871
Tuples::ErrorCodes::InvalidOperation
Tuples::TupleObj::Warning
virtual StatusCode Warning(const std::string &msg, const StatusCode sc=StatusCode::FAILURE) const =0
std::string
STL class.
Tuples::TupleObj::addItem
bool addItem(std::string name, std::string type)
add the item name into the list of known items
Definition: TupleObj.h:1886
GaudiUtils::Map::end
iterator end()
Definition: Map.h:130
Tuples::TupleObj
A simple wrapper class over standard Gaudi NTuple::Tuple facility.
Definition: TupleObj.h:199
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
Tuples::ErrorCodes::InvalidTuple
Tuples::ItemStore::m_map
Store m_map
the underlying map
Definition: TuplePut.h:98
GaudiUtils::Map::find
iterator find(const key_type &key)
Definition: Map.h:147
Tuples::TupleObj::tuple
const NTuple::Tuple * tuple() const
provide the access to underlying Gaudi N-tuple
Definition: TupleObj.h:1851
Tuples::TupleObj::Error
virtual StatusCode Error(const std::string &msg, const StatusCode sc=StatusCode::FAILURE) const =0
NTuple::Tuple
Abstract base class which allows the user to interact with the actual N tuple implementation.
Definition: NTuple.h:375
Tuples::TupleObj::name
const std::string & name() const
get the name
Definition: TupleObj.h:1846
StatusCode::ignore
const StatusCode & ignore() const
Ignore/check StatusCode.
Definition: StatusCode.h:153
GaudiUtils::Map::erase
iterator erase(const_iterator pos)
Definition: Map.h:182
Tuples::ItemStore::operator=
ItemStore & operator=(const ItemStore &)=delete
GaudiUtils::HashMap
Common class providing an architecture-independent hash map.
Definition: HashMap.h:73
NTuple::Tuple::addItem
StatusCode addItem(const std::string &name, Item< TYPE > &itm)
Add a scalar data item a N tuple.
Definition: NTuple.h:503
Tuples::ItemStore
Simple class, which represents the local storage of N-tuple items of the given type.
Definition: TuplePut.h:37
StatusCode::isFailure
bool isFailure() const
Definition: StatusCode.h:130
NTuple::Item< VALUE >
Tuples::TupleObj::put
StatusCode put(const std::string &name, const TYPE *obj)
The function allows to add almost arbitrary object into N-tuple.
Definition: TuplePut.h:113
Tuples::ItemStore::getItem
NTuple::Item< VALUE > * getItem(const std::string &key, Tuples::TupleObj *tuple)
the only one method:
Definition: TuplePut.h:49
Tuples::TupleObj::goodItem
bool goodItem(const std::string &name) const
check the uniqueness of the name
Definition: TupleObj.h:1894
GaudiUtils::Map::emplace
std::pair< iterator, bool > emplace(Args &&... args)
Definition: Map.h:164
Tuples
General namespace for Tuple properties.
Definition: Maps.h:33