| The Gaudi Framework v28r2-patches (bc4ad17a)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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
27 {
37 template <class VALUE>
38 class ItemStore final
39 {
40 friend class TupleObj ;
41 private:
42 typedef GaudiUtils::HashMap<std::string,std::unique_ptr<NTuple::Item<VALUE>>> Store;
43 public:
45 ItemStore() = default;
46 private:
48 inline NTuple::Item<VALUE>* getItem
49 ( const std::string& key , Tuples::TupleObj* tuple )
50 {
51 // find the item by name
52 auto ifound = m_map.find( key ) ;
53 // existing item?
54 if ( m_map.end() != ifound ) { return ifound->second.get() ; } // RETURN
55 // check the tuple for booking:
56 if ( !tuple ) { return nullptr ; }
57 // check the existence of the name
58 if ( !tuple->goodItem ( key ) )
59 {
60 tuple -> Error ( "ItemStore::getItem('" + key
61 + "') item name is not unique").ignore() ;
62 return nullptr ; // RETURN
63 }
64 // get the underlying object
65 NTuple::Tuple* tup = tuple->tuple() ;
66 if ( !tup )
67 {
68 tuple -> Error ( "ItemStore::getItem('" + key
69 + "') invalid NTuple::Tuple*" ).ignore() ;
70 return nullptr ; // RETURN
71 }
72 // create new item:
73 // add the newly created item into the store:
74 auto stored = m_map.emplace( key , std::unique_ptr<NTuple::Item<VALUE>>{ new NTuple::Item<VALUE>() });
75 if ( !stored.second )
76 {
77 tuple -> Warning ( "ItemStore::getItem('" + key
78 + "') item already exists, new one not inserted!" ).ignore() ;
79 return nullptr;
80 }
81 auto& item = stored.first->second;
82 // add it into N-tuple
83 StatusCode sc = tup->addItem( key , *item ) ; // ATTENTION!
84 if ( sc.isFailure() )
85 {
86 tuple -> Error ( "ItemStore::getItem('" + key
87 + "') cannot addItem" , sc ).ignore() ;
88 m_map.erase(stored.first);
89 return nullptr ; // RETURN
90 }
91 // check the name again
92 if ( !tuple->addItem( key , System::typeinfoName ( typeid ( VALUE ) ) ) )
93 {
94 tuple -> Warning ( "ItemStore::getItem('" + key
95 + "') the item not unique " ).ignore() ;
96 m_map.erase(stored.first);
97 return nullptr;
98 }
99 //
100 return item.get() ; // RETURN
101 }
102 private:
103 // delete copy constructor and assignment
104 ItemStore ( const ItemStore& ) = delete;
105 ItemStore& operator=( const ItemStore& ) = delete;
106 private:
108 Store m_map ;
109 } ;
110 } // end of namespace Tuples
111 // =============================================================================
121 // =============================================================================
122 template <class TYPE>
123 inline StatusCode Tuples::TupleObj::put
124 ( const std::string& name , const TYPE* obj )
125 {
126 if ( invalid () ) { return InvalidTuple ; } // RETURN
127 if ( !evtColType () ) { return InvalidOperation ; } // RETURN
128
129 // static block: The type description & the flag
130 static bool s_fail = false ; // STATIC
131 static TClass* s_type = nullptr; // STATIC
132 // check the status
133 if ( s_fail ) { return InvalidItem ; } // RETURN
134 else if ( !s_type )
135 {
136 s_type = TClass::GetClass(typeid(TYPE));
137 if ( !s_type )
138 {
139 s_fail = true ;
140 return Error ( " put('"+name+"'," + System::typeinfoName(typeid(TYPE)) +
141 ") :Invalid ROOT Type", InvalidItem ) ; // RETURN
142 }
143 }
144 // the local storage of items
145 static Tuples::ItemStore<TYPE*> s_map ;
146 // get the variable by name:
147 auto item = s_map.getItem ( name , this ) ;
148 if ( !item )
149 { return Error ( " put('" + name + "'): invalid item detected", InvalidItem ) ; }
150 // assign the item!
151 (*item) = const_cast<TYPE*> ( obj ) ; // THATS ALL!!
152 //
153 return StatusCode::SUCCESS ; // RETURN
154 }
155 // ============================================================================
156
157 // ============================================================================
158 // The END
159 // ============================================================================
160 #endif // GAUDIALG_TUPLEPUT_H
161 // ============================================================================
System.h
StatusCode::SUCCESS
Definition: StatusCode.h:30
Tuples::TupleObj::tuple
const NTuple::Tuple * tuple() const
provide the access to underlying Gaudi N-tuple
Definition: TupleObj.h:1988
GaudiUtils::Map::emplace
std::pair< iterator, bool > emplace(Args &&...args)
Definition: Map.h:166
gaudiComponentHelp.name
name
Definition: gaudiComponentHelp.py:28
Tuples::TupleObj::goodItem
bool goodItem(const std::string &name) const
check the uniqueness of the name
Definition: TupleObj.h:2047
Tuples::InvalidTuple
Definition: TupleObj.h:94
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:299
Tuples::ItemStore::ItemStore
ItemStore()=default
constructor : create empty map
Tuples::InvalidItem
Definition: TupleObj.h:98
StatusCode::isFailure
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:84
Tuples::InvalidOperation
Definition: TupleObj.h:96
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:2039
GaudiUtils::Map::end
iterator end()
Definition: Map.h:132
Tuples::TupleObj
A simple wrapper class over standard Gaudi NTuple::Tuple facility.
Definition: TupleObj.h:196
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
Tuples::ItemStore::m_map
Store m_map
the underlying map
Definition: TuplePut.h:108
GaudiUtils::Map::find
iterator find(const key_type &key)
Definition: Map.h:149
NTuple::Tuple
Abstract base class which allows the user to interact with the actual N tuple implementation.
Definition: NTuple.h:370
std::unique_ptr
STL class.
GaudiUtils::Map::erase
iterator erase(const_iterator pos)
Definition: Map.h:175
Tuples::ItemStore::Store
GaudiUtils::HashMap< std::string, std::unique_ptr< NTuple::Item< VALUE > > > Store
Definition: TuplePut.h:42
Tuples::ItemStore::operator=
ItemStore & operator=(const ItemStore &)=delete
GaudiUtils::HashMap
Common class providing an architecture-independent hash map.
Definition: HashMap.h:77
NTuple::Tuple::addItem
StatusCode addItem(const std::string &name, Item< TYPE > &itm)
Add a scalar data item a N tuple.
Definition: NTuple.h:554
Tuples::ItemStore
Simple class, which represents the local storage of N-tuple items of the given type.
Definition: TuplePut.h:38
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:124
Tuples::ItemStore::getItem
NTuple::Item< VALUE > * getItem(const std::string &key, Tuples::TupleObj *tuple)
the only one method:
Definition: TuplePut.h:49
Tuples
General namespace for Tuple properties.
Definition: Maps.h:34