Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v36r16 (ea80daf8)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 #include "GaudiAlg/TupleObj.h"
14 #include "GaudiKernel/System.h"
15 #include "TClass.h"
16 #include <memory>
17 // =============================================================================
23 // =============================================================================
24 namespace Tuples {
34  template <class VALUE>
35  class ItemStore final {
36  friend class TupleObj;
37 
38  public:
40  ItemStore() = default;
41 
42  private:
44  NTuple::Item<VALUE>* getItem( std::string_view key, Tuples::TupleObj* tuple ) {
45  // find the item by name
46  auto ifound = m_map.find( key );
47  // existing item?
48  if ( m_map.end() != ifound ) return &ifound->second.first; // RETURN
49 
50  // check the tuple for booking:
51  if ( !tuple ) return nullptr;
52  // check the existence of the name
53  if ( !tuple->goodItem( key ) ) {
54  tuple->Error( fmt::format( "ItemStore::getItem('{}') item name is not unique", key ) ).ignore();
55  return nullptr; // RETURN
56  }
57  // get the underlying object
58  NTuple::Tuple* tup = tuple->tuple();
59  if ( !tup ) {
60  tuple->Error( fmt::format( "ItemStore::getItem('{}') invalid NTuple::Tuple*", key ) ).ignore();
61  return nullptr; // RETURN
62  }
63  // create new item:
64  // add the newly created item into the store -- and point the key view into the mapped value...
65  auto [iter, ok] = m_map.try_emplace( key, NTuple::Item<VALUE>{}, std::string{ key } );
66  if ( ok ) {
67  auto nh = m_map.extract( iter );
68  nh.key() = nh.mapped().second; // "re-point" key to the string contained value_type
69  // std::tie( iter,ok,std::ignore) = m_map.insert( std::move( nh ) );
70  auto r = m_map.insert( std::move( nh ) );
71  iter = r.position;
72  ok = r.inserted;
73  }
74  if ( !ok ) {
75  tuple->Warning( fmt::format( "ItemStore::getItem('{}') item already exists, new one not inserted!", key ) )
76  .ignore();
77  return nullptr;
78  }
79  auto& item = iter->second.first;
80  // add it into N-tuple
81  StatusCode sc = tup->addItem( iter->second.second, item ); // ATTENTION!
82  if ( sc.isFailure() ) {
83  tuple->Error( fmt::format( "ItemStore::getItem('{}') cannot addItem", key ), sc ).ignore();
84  m_map.erase( iter );
85  return nullptr; // RETURN
86  }
87  // check the name again
88  if ( !tuple->addItem( iter->second.second, System::typeinfoName( typeid( VALUE ) ) ) ) {
89  tuple->Warning( fmt::format( "ItemStore::getItem('{}') the item not unique ", key ) ).ignore();
90  m_map.erase( iter );
91  return nullptr;
92  }
93  //
94  return &item; // RETURN
95  }
96 
97  // delete copy constructor and assignment
98  ItemStore( const ItemStore& ) = delete;
99  ItemStore& operator=( const ItemStore& ) = delete;
100 
101  private:
103  };
104 } // end of namespace Tuples
105 // =============================================================================
115 // =============================================================================
116 template <class TYPE>
117 StatusCode Tuples::TupleObj::put( std::string_view name, const TYPE* obj ) {
118  if ( invalid() ) { return ErrorCodes::InvalidTuple; } // RETURN
119  if ( !evtColType() ) { return ErrorCodes::InvalidOperation; } // RETURN
120 
121  // static block: The type description & the flag
122  static bool s_fail = false; // STATIC
123  static TClass* s_type = nullptr; // STATIC
124  // check the status
125  if ( s_fail ) {
127  } // RETURN
128  else if ( !s_type ) {
129  s_type = TClass::GetClass( typeid( TYPE ) );
130  if ( !s_type ) {
131  s_fail = true;
132  return Error( fmt::format( " put('{}',{}) :Invalid ROOT Type", name, System::typeinfoName( typeid( TYPE ) ) ),
133  ErrorCodes::InvalidItem ); // RETURN
134  }
135  }
136  // the local storage of items
137  static Tuples::ItemStore<TYPE*> s_map;
138  // get the variable by name:
139  auto item = s_map.getItem( name, this );
140  if ( !item ) { return Error( fmt::format( " put('{}'): invalid item detected", name ), ErrorCodes::InvalidItem ); }
141  // assign the item!
142  *item = const_cast<TYPE*>( obj ); // THATS ALL!!
143  //
144  return StatusCode::SUCCESS; // RETURN
145 }
146 // ============================================================================
147 
148 // ============================================================================
149 // The END
150 // ============================================================================
151 #endif // GAUDIALG_TUPLEPUT_H
Tuples::TupleObj::put
StatusCode put(std::string_view name, const TYPE *obj)
The function allows to add almost arbitrary object into N-tuple.
Definition: TuplePut.h:117
std::string
STL class.
std::move
T move(T... args)
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
std::unordered_map::find
T find(T... args)
Tuples::ItemStore::operator=
ItemStore & operator=(const ItemStore &)=delete
Tuples::TupleObj::evtColType
bool evtColType() const
Event collection ?
Definition: TupleObj.h:1882
System::typeinfoName
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:313
Tuples::ItemStore::getItem
NTuple::Item< VALUE > * getItem(std::string_view key, Tuples::TupleObj *tuple)
the only one method:
Definition: TuplePut.h:44
Tuples
Definition: Maps.h:43
Tuples::TupleObj
A simple wrapper class over standard Gaudi NTuple::Tuple facility.
Definition: TupleObj.h:211
TimingHistograms.name
name
Definition: TimingHistograms.py:25
StatusCode
Definition: StatusCode.h:65
HistoDumpEx.r
r
Definition: HistoDumpEx.py:20
TupleObj.h
Tuples::ItemStore::m_map
std::unordered_map< std::string_view, std::pair< NTuple::Item< VALUE >, std::string > > m_map
the underlying map
Definition: TuplePut.h:102
std::unordered_map::erase
T erase(T... args)
format
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:119
NTuple::Tuple::addItem
StatusCode addItem(const std::string &name, Item< TYPE > &itm)
Add a scalar data item a N tuple.
Definition: NTuple.h:515
Tuples::TupleObj::goodItem
bool goodItem(std::string_view name) const
check the uniqueness of the name
Definition: TupleObj.h:1905
StatusCode::ignore
const StatusCode & ignore() const
Allow discarding a StatusCode without warning.
Definition: StatusCode.h:139
StatusCode::isFailure
bool isFailure() const
Definition: StatusCode.h:129
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
std::unordered_map::insert
T insert(T... args)
Tuples::ItemStore::ItemStore
ItemStore(const ItemStore &)=delete
std::unordered_map::end
T end(T... args)
Tuples::ItemStore::ItemStore
ItemStore()=default
constructor : create empty map
Tuples::ErrorCodes::InvalidItem
@ InvalidItem
Tuples::ErrorCodes::InvalidOperation
@ InvalidOperation
ProduceConsume.key
key
Definition: ProduceConsume.py:81
std::unordered_map
STL class.
Tuples::ItemStore
Definition: TuplePut.h:35
Tuples::TupleObj::name
const std::string & name() const
get the name
Definition: TupleObj.h:1857
NTuple::Item
Class acting as a smart pointer holding a N tuple _Item.
Definition: NTuple.h:58