Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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:
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 ) {
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
const NTuple::Tuple * tuple() const
provide the access to underlying Gaudi N-tuple
Definition: TupleObj.h:1852
bool goodItem(const std::string &name) const
check the uniqueness of the name
Definition: TupleObj.h:1895
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:309
constexpr static const auto SUCCESS
Definition: StatusCode.h:85
ItemStore()=default
constructor : create empty map
GaudiUtils::HashMap< std::string, std::unique_ptr< NTuple::Item< VALUE > > > Store
Definition: TuplePut.h:41
bool isFailure() const
Definition: StatusCode.h:130
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:1887
iterator end()
Definition: Map.h:130
A simple wrapper class over standard Gaudi NTuple::Tuple facility.
Definition: TupleObj.h:200
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
Store m_map
the underlying map
Definition: TuplePut.h:98
iterator find(const key_type &key)
Definition: Map.h:147
std::pair< iterator, bool > emplace(Args &&...args)
Definition: Map.h:164
Abstract base class which allows the user to interact with the actual N tuple implementation.
Definition: NTuple.h:375
iterator erase(const_iterator pos)
Definition: Map.h:182
virtual StatusCode Error(const std::string &msg, const StatusCode sc=StatusCode::FAILURE) const =0
const StatusCode & ignore() const
Ignore/check StatusCode.
Definition: StatusCode.h:153
ItemStore & operator=(const ItemStore &)=delete
Common class providing an architecture-independent hash map.
Definition: HashMap.h:73
StatusCode addItem(const std::string &name, Item< TYPE > &itm)
Add a scalar data item a N tuple.
Definition: NTuple.h:503
Simple class, which represents the local storage of N-tuple items of the given type.
Definition: TuplePut.h:37
StatusCode put(const std::string &name, const TYPE *obj)
The function allows to add almost arbitrary object into N-tuple.
Definition: TuplePut.h:113
NTuple::Item< VALUE > * getItem(const std::string &key, Tuples::TupleObj *tuple)
the only one method:
Definition: TuplePut.h:49
General namespace for Tuple properties.
Definition: Maps.h:33