The Gaudi Framework  v36r0 (4abb4d13)
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:
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
std::string
STL class.
GaudiUtils::Map::find
iterator find(const key_type &key)
Definition: Map.h:157
Tuples::TupleObj::tuple
const NTuple::Tuple * tuple() const
provide the access to underlying Gaudi N-tuple
Definition: TupleObj.h:1862
System.h
Tuples::TupleObj::Error
virtual StatusCode Error(const std::string &msg, const StatusCode sc=StatusCode::FAILURE) const =0
Tuples::ItemStore::m_map
Store m_map
the underlying map
Definition: TuplePut.h:108
Tuples::ItemStore::operator=
ItemStore & operator=(const ItemStore &)=delete
Tuples::TupleObj::evtColType
bool evtColType() const
Event collection ?
Definition: TupleObj.h:1882
Tuples::TupleObj::goodItem
bool goodItem(const std::string &name) const
check the uniqueness of the name
Definition: TupleObj.h:1905
System::typeinfoName
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:308
Tuples
Definition: Maps.h:43
Tuples::TupleObj
A simple wrapper class over standard Gaudi NTuple::Tuple facility.
Definition: TupleObj.h:209
TimingHistograms.name
name
Definition: TimingHistograms.py:23
StatusCode
Definition: StatusCode.h:65
Tuples::ItemStore::getItem
NTuple::Item< VALUE > * getItem(const std::string &key, Tuples::TupleObj *tuple)
the only one method:
Definition: TuplePut.h:59
TupleObj.h
NTuple::Tuple::addItem
StatusCode addItem(const std::string &name, Item< TYPE > &itm)
Add a scalar data item a N tuple.
Definition: NTuple.h:515
GaudiUtils::Map::end
iterator end()
Definition: Map.h:140
GaudiUtils::Map::erase
iterator erase(const_iterator pos)
Definition: Map.h:192
StatusCode::ignore
const StatusCode & ignore() const
Allow discarding a StatusCode without warning.
Definition: StatusCode.h:156
StatusCode::isFailure
bool isFailure() const
Definition: StatusCode.h:142
Tuples::TupleObj::addItem
bool addItem(std::string name, std::string type)
add the item name into the list of known items
Definition: TupleObj.h:1897
Tuples::TupleObj::Warning
virtual StatusCode Warning(const std::string &msg, const StatusCode sc=StatusCode::FAILURE) const =0
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
Tuples::TupleObj::invalid
bool invalid() const
invalid pointer to tuple ?
Definition: TupleObj.h:1888
Tuples::ErrorCodes::InvalidTuple
@ InvalidTuple
NTuple::Tuple
Abstract base class which allows the user to interact with the actual N tuple implementation.
Definition: NTuple.h:387
Tuples::TupleObj::put
StatusCode put(const std::string &name, const TYPE *obj)
The function allows to add almost arbitrary object into N-tuple.
Definition: TuplePut.h:123
Tuples::ItemStore::ItemStore
ItemStore(const ItemStore &)=delete
Tuples::ItemStore::Store
GaudiUtils::HashMap< std::string, std::unique_ptr< NTuple::Item< VALUE > > > Store
Definition: TuplePut.h:51
GaudiUtils::HashMap
Definition: HashMap.h:83
Tuples::ItemStore::ItemStore
ItemStore()=default
constructor : create empty map
Tuples::ErrorCodes::InvalidItem
@ InvalidItem
Tuples::ErrorCodes::InvalidOperation
@ InvalidOperation
ProduceConsume.key
key
Definition: ProduceConsume.py:52
Tuples::ItemStore
Definition: TuplePut.h:47
GaudiUtils::Map::emplace
std::pair< iterator, bool > emplace(Args &&... args)
Definition: Map.h:174
Tuples::TupleObj::name
const std::string & name() const
get the name
Definition: TupleObj.h:1857
NTuple::Item< VALUE >