The Gaudi Framework  v33r1 (b1225454)
TuplePut.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2019 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #ifndef GAUDIALG_TUPLEPUT_H
12 #define GAUDIALG_TUPLEPUT_H 1
13 // =============================================================================
14 // Include files
15 // =============================================================================
16 #include <memory>
17 // =============================================================================
18 // GaudiKernel
19 // =============================================================================
20 #include "GaudiKernel/System.h"
21 // =============================================================================
22 // GaudiAlg
23 // =============================================================================
24 #include "GaudiAlg/TupleObj.h"
25 // ============================================================================
26 // ROOT TClass
27 // ============================================================================
28 #include "TClass.h"
29 // =============================================================================
35 // =============================================================================
36 namespace Tuples {
46  template <class VALUE>
47  class ItemStore final {
48  friend class TupleObj;
49 
50  private:
52 
53  public:
55  ItemStore() = default;
56 
57  private:
59  inline NTuple::Item<VALUE>* getItem( const std::string& key, Tuples::TupleObj* tuple ) {
60  // find the item by name
61  auto ifound = m_map.find( key );
62  // existing item?
63  if ( m_map.end() != ifound ) return ifound->second.get(); // RETURN
64  // check the tuple for booking:
65  if ( !tuple ) return nullptr;
66  // check the existence of the name
67  if ( !tuple->goodItem( key ) ) {
68  tuple->Error( "ItemStore::getItem('" + key + "') item name is not unique" ).ignore();
69  return nullptr; // RETURN
70  }
71  // get the underlying object
72  NTuple::Tuple* tup = tuple->tuple();
73  if ( !tup ) {
74  tuple->Error( "ItemStore::getItem('" + key + "') invalid NTuple::Tuple*" ).ignore();
75  return nullptr; // RETURN
76  }
77  // create new item:
78  // add the newly created item into the store:
79  auto stored = m_map.emplace( key, std::make_unique<NTuple::Item<VALUE>>() );
80  if ( !stored.second ) {
81  tuple->Warning( "ItemStore::getItem('" + key + "') item already exists, new one not inserted!" ).ignore();
82  return nullptr;
83  }
84  auto& item = stored.first->second;
85  // add it into N-tuple
86  StatusCode sc = tup->addItem( key, *item ); // ATTENTION!
87  if ( sc.isFailure() ) {
88  tuple->Error( "ItemStore::getItem('" + key + "') cannot addItem", sc ).ignore();
89  m_map.erase( stored.first );
90  return nullptr; // RETURN
91  }
92  // check the name again
93  if ( !tuple->addItem( key, System::typeinfoName( typeid( VALUE ) ) ) ) {
94  tuple->Warning( "ItemStore::getItem('" + key + "') the item not unique " ).ignore();
95  m_map.erase( stored.first );
96  return nullptr;
97  }
98  //
99  return item.get(); // RETURN
100  }
101 
102  // delete copy constructor and assignment
103  ItemStore( const ItemStore& ) = delete;
104  ItemStore& operator=( const ItemStore& ) = delete;
105 
106  private:
109  };
110 } // end of namespace Tuples
111 // =============================================================================
121 // =============================================================================
122 template <class TYPE>
123 inline StatusCode Tuples::TupleObj::put( const std::string& name, const TYPE* obj ) {
124  if ( invalid() ) { return ErrorCodes::InvalidTuple; } // RETURN
125  if ( !evtColType() ) { return ErrorCodes::InvalidOperation; } // RETURN
126 
127  // static block: The type description & the flag
128  static bool s_fail = false; // STATIC
129  static TClass* s_type = nullptr; // STATIC
130  // check the status
131  if ( s_fail ) {
133  } // RETURN
134  else if ( !s_type ) {
135  s_type = TClass::GetClass( typeid( TYPE ) );
136  if ( !s_type ) {
137  s_fail = true;
138  return Error( " put('" + name + "'," + System::typeinfoName( typeid( TYPE ) ) + ") :Invalid ROOT Type",
139  ErrorCodes::InvalidItem ); // RETURN
140  }
141  }
142  // the local storage of items
143  static Tuples::ItemStore<TYPE*> s_map;
144  // get the variable by name:
145  auto item = s_map.getItem( name, this );
146  if ( !item ) { return Error( " put('" + name + "'): invalid item detected", ErrorCodes::InvalidItem ); }
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
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:308
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
ItemStore()=default
constructor : create empty map
bool invalid() const
invalid pointer to tuple ?
Definition: TupleObj.h:1888
GaudiUtils::HashMap< std::string, std::unique_ptr< NTuple::Item< VALUE > > > Store
Definition: TuplePut.h:51
bool evtColType() const
Event collection ?
Definition: TupleObj.h:1882
virtual StatusCode Warning(const std::string &msg, const StatusCode sc=StatusCode::FAILURE) const =0
STL class.
bool addItem(std::string name, std::string type)
add the item name into the list of known items
Definition: TupleObj.h:1897
iterator end()
Definition: Map.h:140
A simple wrapper class over standard Gaudi NTuple::Tuple facility.
Definition: TupleObj.h:209
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:61
Store m_map
the underlying map
Definition: TuplePut.h:108
iterator find(const key_type &key)
Definition: Map.h:157
const NTuple::Tuple * tuple() const
provide the access to underlying Gaudi N-tuple
Definition: TupleObj.h:1862
virtual StatusCode Error(const std::string &msg, const StatusCode sc=StatusCode::FAILURE) const =0
Abstract base class which allows the user to interact with the actual N tuple implementation.
Definition: NTuple.h:385
const std::string & name() const
get the name
Definition: TupleObj.h:1857
const StatusCode & ignore() const
Ignore/check StatusCode.
Definition: StatusCode.h:168
iterator erase(const_iterator pos)
Definition: Map.h:192
ItemStore & operator=(const ItemStore &)=delete
Common class providing an architecture-independent hash map.
Definition: HashMap.h:83
StatusCode addItem(const std::string &name, Item< TYPE > &itm)
Add a scalar data item a N tuple.
Definition: NTuple.h:513
Simple class, which represents the local storage of N-tuple items of the given type.
Definition: TuplePut.h:47
bool isFailure() const
Definition: StatusCode.h:145
StatusCode put(const std::string &name, const TYPE *obj)
The function allows to add almost arbitrary object into N-tuple.
Definition: TuplePut.h:123
NTuple::Item< VALUE > * getItem(const std::string &key, Tuples::TupleObj *tuple)
the only one method:
Definition: TuplePut.h:59
bool goodItem(const std::string &name) const
check the uniqueness of the name
Definition: TupleObj.h:1905
std::pair< iterator, bool > emplace(Args &&... args)
Definition: Map.h:174
General namespace for Tuple properties.
Definition: Maps.h:43