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