The Gaudi Framework  v30r3 (a5ef0a68)
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 ) return ifound->second.get(); // RETURN
57  // check the tuple for booking:
58  if ( !tuple ) return nullptr;
59  // check the existence of the name
60  if ( !tuple->goodItem( key ) ) {
61  tuple->Error( "ItemStore::getItem('" + key + "') 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  tuple->Error( "ItemStore::getItem('" + key + "') invalid NTuple::Tuple*" ).ignore();
68  return nullptr; // RETURN
69  }
70  // create new item:
71  // add the newly created item into the store:
72  auto stored = m_map.emplace( key, std::make_unique<NTuple::Item<VALUE>>() );
73  if ( !stored.second ) {
74  tuple->Warning( "ItemStore::getItem('" + key + "') item already exists, new one not inserted!" ).ignore();
75  return nullptr;
76  }
77  auto& item = stored.first->second;
78  // add it into N-tuple
79  StatusCode sc = tup->addItem( key, *item ); // ATTENTION!
80  if ( sc.isFailure() ) {
81  tuple->Error( "ItemStore::getItem('" + key + "') cannot addItem", sc ).ignore();
82  m_map.erase( stored.first );
83  return nullptr; // RETURN
84  }
85  // check the name again
86  if ( !tuple->addItem( key, System::typeinfoName( typeid( VALUE ) ) ) ) {
87  tuple->Warning( "ItemStore::getItem('" + key + "') the item not unique " ).ignore();
88  m_map.erase( stored.first );
89  return nullptr;
90  }
91  //
92  return item.get(); // RETURN
93  }
94 
95  // delete copy constructor and assignment
96  ItemStore( const ItemStore& ) = delete;
97  ItemStore& operator=( const ItemStore& ) = delete;
98 
99  private:
101  Store m_map;
102  };
103 } // end of namespace Tuples
104 // =============================================================================
114 // =============================================================================
115 template <class TYPE>
116 inline StatusCode Tuples::TupleObj::put( const std::string& name, const TYPE* obj )
117 {
118  if ( invalid() ) {
120  } // RETURN
121  if ( !evtColType() ) {
123  } // RETURN
124 
125  // static block: The type description & the flag
126  static bool s_fail = false; // STATIC
127  static TClass* s_type = nullptr; // STATIC
128  // check the status
129  if ( s_fail ) {
131  } // RETURN
132  else if ( !s_type ) {
133  s_type = TClass::GetClass( typeid( TYPE ) );
134  if ( !s_type ) {
135  s_fail = true;
136  return Error( " put('" + name + "'," + System::typeinfoName( typeid( TYPE ) ) + ") :Invalid ROOT Type",
137  ErrorCodes::InvalidItem ); // RETURN
138  }
139  }
140  // the local storage of items
141  static Tuples::ItemStore<TYPE*> s_map;
142  // get the variable by name:
143  auto item = s_map.getItem( name, this );
144  if ( !item ) {
145  return Error( " put('" + name + "'): invalid item detected", ErrorCodes::InvalidItem );
146  }
147  // assign the item!
148  ( *item ) = const_cast<TYPE*>( obj ); // THATS ALL!!
149  //
150  return StatusCode::SUCCESS; // RETURN
151 }
152 // ============================================================================
153 
154 // ============================================================================
155 // The END
156 // ============================================================================
157 #endif // GAUDIALG_TUPLEPUT_H
158 // ============================================================================
const NTuple::Tuple * tuple() const
provide the access to underlying Gaudi N-tuple
Definition: TupleObj.h:1891
bool goodItem(const std::string &name) const
check the uniqueness of the name
Definition: TupleObj.h:1935
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:332
ItemStore()=default
constructor : create empty map
GaudiUtils::HashMap< std::string, std::unique_ptr< NTuple::Item< VALUE > > > Store
Definition: TuplePut.h:43
bool isFailure() const
Definition: StatusCode.h:139
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:1926
iterator end()
Definition: Map.h:134
A simple wrapper class over standard Gaudi NTuple::Tuple facility.
Definition: TupleObj.h:204
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:51
Store m_map
the underlying map
Definition: TuplePut.h:101
iterator find(const key_type &key)
Definition: Map.h:151
std::pair< iterator, bool > emplace(Args &&...args)
Definition: Map.h:169
constexpr static const auto SUCCESS
Definition: StatusCode.h:87
Abstract base class which allows the user to interact with the actual N tuple implementation.
Definition: NTuple.h:407
iterator erase(const_iterator pos)
Definition: Map.h:192
virtual StatusCode Error(const std::string &msg, const StatusCode sc=StatusCode::FAILURE) const =0
const StatusCode & ignore() const
Ignore/check StatusCode.
Definition: StatusCode.h:165
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:564
Simple class, which represents the local storage of N-tuple items of the given type.
Definition: TuplePut.h:38
StatusCode put(const std::string &name, const TYPE *obj)
The function allows to add almost arbitrary object into N-tuple.
Definition: TuplePut.h:116
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