All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
TuplePut.h
Go to the documentation of this file.
1 // $Id: TuplePut.h,v 1.2 2007/05/24 14:22:58 hmd Exp $
2 // =============================================================================
3 #ifndef GAUDIALG_TUPLEPUT_H
4 #define GAUDIALG_TUPLEPUT_H 1
5 // =============================================================================
6 // Include files
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
39  {
40  friend class TupleObj ;
41  private:
43  public:
45  ItemStore() : m_map() {}
48  {
49  for ( typename Store::iterator ientry = m_map.begin() ;
50  m_map.end() != ientry ; ++ientry )
51  { if ( 0 != ientry->second ) { delete ientry->second ; } }
52  }
53  protected:
56  ( const std::string& key , Tuples::TupleObj* tuple )
57  {
58  // find the item by name
59  typename Store::iterator ifound = m_map.find( key ) ;
60  // existing item?
61  if ( m_map.end() != ifound ) { return ifound->second ; } // RETURN
62  // check the tuple for booking:
63  if ( 0 == tuple ) { return 0 ; }
64  // check the existence of the name
65  if ( !tuple->goodItem ( key ) )
66  {
67  tuple -> Error ( "ItemStore::getItem('" + key
68  + "') item name is not unique").ignore() ;
69  return 0 ; // RETURN
70  }
71  // get the underlying object
72  NTuple::Tuple* tup = tuple->tuple() ;
73  if ( 0 == tup )
74  {
75  tuple -> Error ( "ItemStore::getItem('" + key
76  + "') invalid NTuple::Tuple*" ).ignore() ;
77  return 0 ; // RETURN
78  }
79  // create new item:
81  // add it into N-tuple
82  StatusCode sc = tup->addItem( key , *item ) ; // ATTENTION!
83  if ( sc.isFailure() )
84  {
85  tuple -> Error ( "ItemStore::getItem('" + key
86  + "') cannot addItem" , sc ).ignore() ;
87  return 0 ; // RETURN
88  }
89  // check the name again
90  if ( !tuple->addItem( key , System::typeinfoName ( typeid ( VALUE ) ) ) )
91  {
92  tuple -> Warning ( "ItemStore::getItem('" + key
93  + "') the item not unique " ).ignore() ;
94  }
95  // add the newly created item into the store:
96  if ( !m_map.insert ( std::make_pair ( key , item ) ).second )
97  {
98  tuple -> Warning ( "ItemStore::getItem('" + key
99  + "') item is not inserted!" ).ignore() ;
100  }
101  //
102  return item ; // RETURN
103  }
104  private:
105  // copy constructor is disabled
106  ItemStore ( const ItemStore& ) ;
107  // assignment is disabled
108  ItemStore& operator=( const ItemStore& ) ;
109  private:
112  } ;
113 } // end of namespace Tuples
114 // =============================================================================
124 // =============================================================================
125 template <class TYPE>
127 ( const std::string& name , const TYPE* obj )
128 {
129  if ( invalid () ) { return InvalidTuple ; } // RETURN
130  if ( !evtColType () ) { return InvalidOperation ; } // RETURN
131 
132  // static block: The type description & the flag
133  static bool s_fail = false ; // STATIC
134  static TClass* s_type = 0 ; // STATIC
135  // check the status
136  if ( s_fail ) { return InvalidItem ; } // RETURN
137  else if ( !s_type )
138  {
139  s_type = TClass::GetClass(typeid(TYPE));
140  if ( !s_type )
141  {
142  s_fail = true ;
143  return Error ( " put('"+name+"'," + System::typeinfoName(typeid(TYPE)) +
144  ") :Invalid ROOT Type", InvalidItem ) ; // RETURN
145  }
146  }
147  // the local storage of items
148  static Tuples::ItemStore<TYPE*> s_map ;
149  // get the variable by name:
150  NTuple::Item<TYPE*>* item = s_map.getItem ( name , this ) ;
151  if ( 0 == item )
152  { return Error ( " put('" + name + "'): invalid item detected", InvalidItem ) ; }
153  // assign the item!
154  (*item) = const_cast<TYPE*> ( obj ) ; // THATS ALL!!
155  //
156  return StatusCode::SUCCESS ; // RETURN
157 }
158 // ============================================================================
159 
160 // ============================================================================
161 // The END
162 // ============================================================================
163 #endif // GAUDIALG_TUPLEPUT_H
164 // ============================================================================
~ItemStore()
destructor : delete all known entries
Definition: TuplePut.h:47
bool goodItem(const std::string &name) const
check the uniqueness of the name
Definition: TupleObj.h:2142
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:298
NTuple::Tuple * tuple() const
provide the access to underlying Gaudi N-tuple
Definition: TupleObj.h:2088
std::pair< iterator, bool > insert(const value_type &val)
Definition: Map.h:165
iterator end()
Definition: Map.h:131
ItemStore & operator=(const ItemStore &)
no assignment is allowed
A simple wrapper class over standard Gaudi NTuple::Tuple facility.
Definition: TupleObj.h:180
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:30
Store m_map
the underlying map
Definition: TuplePut.h:111
iterator find(const key_type &key)
Definition: Map.h:148
Abstract base class which allows the user to interact with the actual N tuple implementation.
Definition: NTuple.h:367
iterator begin()
Definition: Map.h:130
bool addItem(const std::string &name, const std::string &type)
add the item name into the list of known items
Definition: TupleObj.h:2134
GaudiUtils::HashMap< std::string, NTuple::Item< VALUE > * > Store
Definition: TuplePut.h:42
Common class providing an architecture-independent hash map.
Definition: HashMap.h:108
tuple item
print s1,s2
Definition: ana.py:146
StatusCode addItem(const std::string &name, Item< TYPE > &itm)
Add a scalar data item a N tuple.
Definition: NTuple.h:551
Simple class, which represents the local storage of N-tuple items of the given type.
Definition: TuplePut.h:38
Class acting as a smart pointer holding a N tuple _Item.
Definition: NTuple.h:43
StatusCode put(const std::string &name, const TYPE *obj)
The function allows to add almost arbitrary object into N-tuple.
Definition: TuplePut.h:127
NTuple::Item< VALUE > * getItem(const std::string &key, Tuples::TupleObj *tuple)
the only one method:
Definition: TuplePut.h:56
ItemStore()
constructor : create empty map
Definition: TuplePut.h:45