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
27 {
37  template <class VALUE>
38  class ItemStore final
39  {
40  friend class TupleObj ;
41  private:
43  public:
45  ItemStore() = default;
46  private:
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:
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>
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 // ============================================================================
const NTuple::Tuple * tuple() const
provide the access to underlying Gaudi N-tuple
Definition: TupleObj.h:1988
std::pair< iterator, bool > emplace(Args &&...args)
Definition: Map.h:166
bool goodItem(const std::string &name) const
check the uniqueness of the name
Definition: TupleObj.h:2047
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:299
ItemStore()=default
constructor : create empty map
bool isFailure() const
Test for a status code of FAILURE.
Definition: StatusCode.h:84
STL class.
bool addItem(std::string name, std::string type)
add the item name into the list of known items
Definition: TupleObj.h:2039
iterator end()
Definition: Map.h:132
A simple wrapper class over standard Gaudi NTuple::Tuple facility.
Definition: TupleObj.h:196
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
Store m_map
the underlying map
Definition: TuplePut.h:108
iterator find(const key_type &key)
Definition: Map.h:149
Abstract base class which allows the user to interact with the actual N tuple implementation.
Definition: NTuple.h:370
STL class.
iterator erase(const_iterator pos)
Definition: Map.h:175
GaudiUtils::HashMap< std::string, std::unique_ptr< NTuple::Item< VALUE > > > Store
Definition: TuplePut.h:42
ItemStore & operator=(const ItemStore &)=delete
Common class providing an architecture-independent hash map.
Definition: HashMap.h:77
StatusCode addItem(const std::string &name, Item< TYPE > &itm)
Add a scalar data item a N tuple.
Definition: NTuple.h:554
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:124
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:34