Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
GetData.h
Go to the documentation of this file.
1 #ifndef GAUDIUTILS_GETDATA_H
2 #define GAUDIUTILS_GETDATA_H 1
3 // ============================================================================
4 // Include files
5 // ============================================================================
6 // GaudiKernel
7 // ============================================================================
10 #include "GaudiKernel/IRegistry.h"
11 #include "GaudiKernel/MsgStream.h"
13 #include "GaudiKernel/StatusCode.h"
14 // ============================================================================
15 // GaudiUtils
16 // ============================================================================
18 #include "GaudiKernel/NamedRange.h"
19 #include "GaudiKernel/Range.h"
20 // ============================================================================
21 // Forward declaration
22 // ============================================================================
23 template <class BASE>
24 class GaudiCommon; // GaudiAlg
25 // ============================================================================
26 namespace Gaudi {
27  namespace Utils {
28  // ========================================================================
35  template <class TYPE>
36  struct _GetType {
37  typedef TYPE* return_type;
38  };
39  // ========================================================================
41  template <class TYPE>
42  struct _GetType<TYPE*> {
43  typedef TYPE* return_type;
44  };
45  // ========================================================================
47  template <class TYPE>
48  struct _GetType<TYPE&> {
49  typedef TYPE* return_type;
50  };
51  // ========================================================================
53  template <class CONTAINER>
54  struct _GetType<Gaudi::Range_<CONTAINER>> {
56  };
57  // ========================================================================
59  template <class CONTAINER>
60  struct _GetType<Gaudi::NamedRange_<CONTAINER>> {
62  };
63  // ========================================================================
68  template <class TYPE, std::enable_if_t<!std::is_constructible<TYPE>::value, void*> = nullptr>
70  DataObject* obj = nullptr;
71  // Return the casted pointer if the retrieve was successful or nullptr otherwise.
72  StatusCode sc = service->retrieveObject( location, obj );
73  if ( sc.isSuccess() ) {
74  typename _GetType<TYPE>::return_type tobj = dynamic_cast<typename _GetType<TYPE>::return_type>( obj );
75  return tobj;
76  } else {
77  return nullptr;
78  }
79  }
80  // ========================================================================
86  template <class TYPE, std::enable_if_t<std::is_constructible<TYPE>::value, void*> = nullptr>
87  inline typename _GetType<TYPE>::return_type getFromTS( IDataProviderSvc* service, const std::string& location ) {
88  DataObject* obj = nullptr;
89  // Return the casted pointer if the retrieve was successful or nullptr otherwise.
90  StatusCode sc = service->retrieveObject( location, obj );
91  if ( sc.isSuccess() ) {
92  typename _GetType<TYPE>::return_type tobj = dynamic_cast<typename _GetType<TYPE>::return_type>( obj );
93  if ( !tobj ) {
94  // try with AnyDataWrapper
95  AnyDataWrapper<TYPE>* tobj2 = dynamic_cast<AnyDataWrapper<TYPE>*>( obj );
96  if ( tobj2 ) { tobj = &( tobj2->getData() ); }
97  }
98  return tobj;
99  } else {
100  return nullptr;
101  }
102  }
103  // ========================================================================
112  template <class TYPE>
113  struct GetData {
114  public:
115  // ======================================================================
116  typedef TYPE Type;
119  // ======================================================================
120  public:
121  // ======================================================================
129  template <class COMMON>
130  inline return_type operator()( const COMMON& common, IDataProviderSvc* service, const std::string& location,
131  const bool checkData = true ) const {
132  // use Data Provider Service
133  return_type obj = getFromTS<Type>( service, location );
134  if ( checkData ) { // check the data
135  common.Assert( obj, "get():: No valid data at '" + location + "'" );
136  }
137  // debug printout
138  if ( common.msgLevel( MSG::DEBUG ) ) {
139  common.debug() << "The object of type '" << System::typeinfoName( typeid( obj ) ) << "' "
140  << ( obj ? "has been" : "could not be" ) << " retrieved from TS at address '" << location
141  << "'" << endmsg;
142  }
143  // return located data
144  return obj;
145  // ======================================================================
146  }
147  };
148  // ========================================================================
150  template <class TYPE>
151  struct GetData<Gaudi::Range_<std::vector<const TYPE*>>> {
152  public:
153  // ======================================================================
157  // ======================================================================
158  public:
159  // ======================================================================
167  template <class COMMON>
168  inline return_type operator()( const COMMON& common, IDataProviderSvc* service, const std::string& location,
169  const bool checkData = true ) const {
172  DataObject* object = this->getData( service, location );
173  if ( object ) {
175  typedef typename TYPE::Selection Selection_;
176  const Selection_* sel = dynamic_cast<Selection_*>( object );
177  if ( sel ) {
178  if ( common.msgLevel( MSG::DEBUG ) ) {
179  common.debug() << "The object of type '" << System::typeinfoName( typeid( *object ) )
180  << "' has been retrieved from TS at address '" << location << "'" << endmsg;
181  }
182  return make_range( sel );
183  }
185  typedef typename TYPE::Container Container_;
186  const Container_* cnt = dynamic_cast<Container_*>( object );
187  if ( cnt ) {
188  if ( common.msgLevel( MSG::DEBUG ) ) {
189  common.debug() << "The object of type '" << System::typeinfoName( typeid( *object ) )
190  << "' has been retrieved from TS at address '" << location << "'" << endmsg;
191  }
192  return make_range( cnt );
193  }
194  // no valid data
195  if ( checkData ) common.Assert( false, "get():: No valid data at '" + location + "'" );
196  }
197  // no valid data
198  if ( checkData ) common.Assert( false, "get():: No data at '" + location + "'" );
199  // the fictive return
200  return return_type();
201  }
202  // ======================================================================
203  public:
204  // ======================================================================
205  // create the range from the container
206  return_type make_range( const typename TYPE::Container* cnt ) const {
207  return 0 == cnt ? return_type() : make_range( cnt->begin(), cnt->end() );
208  }
209  // create the range from the selection
210  return_type make_range( const typename TYPE::Selection* cnt ) const {
211  return 0 == cnt ? return_type() : return_type( cnt->begin(), cnt->end() );
212  }
213  // ======================================================================
219  DataObject* getData( IDataProviderSvc* service, const std::string& location ) const {
221  SmartDataObjectPtr getter( SmartDataObjectPtr::ObjectLoader::access(), service, nullptr, location );
222  return getter.accessData();
223  }
224  // ======================================================================
225  private:
226  // ======================================================================
227  template <class ITERATOR>
228  return_type make_range( ITERATOR first, ITERATOR last ) const {
229  auto _begin = reinterpret_cast<typename return_type::const_iterator*>( &first );
230  auto _end = reinterpret_cast<typename return_type::const_iterator*>( &last );
231  return return_type( *_begin, *_end );
232  }
233  // ======================================================================
234  };
235  // ========================================================================
237  template <class TYPE>
238  struct GetData<Gaudi::NamedRange_<std::vector<const TYPE*>>> {
239  public:
240  // ======================================================================
244  // ======================================================================
245  public:
246  // ======================================================================
254  template <class COMMON>
255  inline return_type operator()( const COMMON& common, IDataProviderSvc* service, const std::string& location,
256  const bool checkData = true ) const {
257  return return_type( m_range( common, service, location, checkData ), location );
258  }
259  // ======================================================================
260  public:
261  // ======================================================================
262  // create the range from the container
263  return_type make_range( const typename TYPE::Container* cnt ) const {
264  if ( !cnt ) { return return_type(); }
265  static const std::string s_empty = "";
266  const IRegistry* reg = cnt->registry();
267  return return_type( m_range.make_range( cnt ), reg ? reg->identifier() : s_empty );
268  }
269  // create the range from the selection
270  return_type make_range( const typename TYPE::Selection* cnt ) const {
271  if ( !cnt ) { return return_type(); }
272  static const std::string s_empty = "";
273  const IRegistry* reg = cnt->registry();
274  return return_type( m_range.make_range( cnt ), reg ? reg->identifier() : s_empty );
275  }
276  // ======================================================================
282  DataObject* getData( IDataProviderSvc* service, const std::string& location ) const {
283  return m_range.getData( service, location );
284  }
285  // ======================================================================
286  private:
290  // ======================================================================
291  };
292  // ========================================================================
294  template <class TYPE>
295  struct GetData<const TYPE> : public GetData<TYPE> {};
296  // ========================================================================
298  template <class TYPE>
299  struct GetData<TYPE*> : public GetData<TYPE> {};
300  // ========================================================================
302  template <class TYPE>
303  struct GetData<TYPE&> : public GetData<TYPE> {};
304  // ========================================================================
313  template <class TYPE>
314  struct CheckData {
315  public:
316  // ======================================================================
323  inline bool operator()( IDataProviderSvc* service, const std::string& location ) const {
325  return getFromTS<TYPE>( service, location );
326  }
327  // ======================================================================
328  };
329  // ========================================================================
331  template <class TYPE>
332  struct CheckData<Gaudi::Range_<std::vector<const TYPE*>>> {
333  public:
334  // ======================================================================
341  inline bool operator()( IDataProviderSvc* service, const std::string& location ) const {
342  DataObject* object = this->getData( service, location );
343  if ( !object ) { return false; }
344  return dynamic_cast<typename TYPE::Selection*>( object ) || dynamic_cast<typename TYPE::Container*>( object );
345  }
346  // ======================================================================
347  protected:
348  // ======================================================================
354  DataObject* getData( IDataProviderSvc* service, const std::string& location ) const {
356  SmartDataObjectPtr getter( SmartDataObjectPtr::ObjectLoader::access(), service, nullptr, location );
357  return getter.accessData();
358  }
359  // ======================================================================
360  };
361  // ========================================================================
363  template <class TYPE>
364  struct CheckData<Gaudi::NamedRange_<std::vector<const TYPE*>>>
365  : public CheckData<Gaudi::Range_<std::vector<const TYPE*>>> {};
366  // ========================================================================
368  template <class TYPE>
369  struct CheckData<TYPE*> : public CheckData<TYPE> {};
370  // ========================================================================
372  template <class TYPE>
373  struct CheckData<TYPE&> : public CheckData<TYPE> {};
374  // ========================================================================
376  template <class TYPE>
377  struct CheckData<const TYPE> : public CheckData<TYPE> {};
378  // ========================================================================
387  template <class TYPE, class TYPE2>
389  private:
390  // ======================================================================
392  typedef GetData<TYPE> Getter; // the actual data getter
393  // ======================================================================
394  public:
395  // ======================================================================
396  typedef typename Getter::Type Type;
399  // ======================================================================
400  public:
401  // ======================================================================
408  template <class COMMON>
409  inline return_type operator()( const COMMON& common, IDataProviderSvc* service, const std::string& location,
410  const std::string& location2 ) const {
411  SmartDataPtr<TYPE> obj( service, location );
412  if ( !obj ) {
413  auto o = std::make_unique<TYPE2>();
414  auto r = o.get();
415  common.put( service, std::move( o ), location2 );
416  if ( common.msgLevel( MSG::DEBUG ) ) {
417  common.debug() << "The object of type '" << System::typeinfoName( typeid( *r ) )
418  << "' has been created from TS at address '" << location2 << "'" << endmsg;
419  }
420  return r;
421  }
422  auto ret = obj.ptr();
424  common.Assert( !( !ret ), "get():: No valid data at '" + location + "'" );
425  if ( common.msgLevel( MSG::DEBUG ) ) {
426  common.debug() << "The object of type '" << System::typeinfoName( typeid( *ret ) )
427  << "' has been retrieved from TS at address '" << location << "'" << endmsg;
428  }
429  // return *VALID* data
430  return ret;
431  // ====================================================================
432  }
433  };
434  // ========================================================================
435  template <class TYPE, class TYPE2>
436  struct GetOrCreateData<Gaudi::Range_<std::vector<const TYPE*>>, TYPE2> {
437  private:
438  // ======================================================================
441  typedef GetData<Range> Getter; // the actual data getter
443  typedef CheckData<Range> Checker; // the actual data checker
444  // ======================================================================
445  public:
446  // ======================================================================
447  typedef typename Getter::Type Type;
450  // ======================================================================
451  public:
452  // ======================================================================
459  template <class COMMON>
460  inline return_type operator()( const COMMON& common, IDataProviderSvc* service, const std::string& location,
461  const std::string& location2 ) const {
462  DataObject* obj = m_getter.getData( service, location );
463  if ( !obj ) {
464  common.put( service, std::make_unique<TYPE2>(), location2 );
465  if ( common.msgLevel( MSG::DEBUG ) ) {
466  common.debug() << "The object of type '" << System::typeinfoName( typeid( TYPE2 ) )
467  << "' has been created from TS at address '" << location2 << "'" << endmsg;
468  }
469  }
470  return m_getter( common, service, location );
471  // ====================================================================
472  }
473  // ======================================================================
474  private:
475  // ======================================================================
477  Getter m_getter; // the actual data getter
478  // ======================================================================
479  };
480  // ========================================================================
481  template <class TYPE, class TYPE2>
482  struct GetOrCreateData<Gaudi::NamedRange_<std::vector<const TYPE*>>, TYPE2> {
483  private:
484  // ======================================================================
489  typedef GetData<Range> Getter; // the actual data getter
490  // ======================================================================
491  public:
492  // ======================================================================
493  typedef typename Getter::Type Type;
496  // ======================================================================
497  public:
498  // ======================================================================
505  template <class COMMON>
506  inline return_type operator()( const COMMON& common, IDataProviderSvc* service, const std::string& location,
507  const std::string& location2 ) const {
508  return return_type( m_range( common, service, location, location2 ), location );
509  }
510  // ======================================================================
511  private:
512  // ======================================================================
514  Helper m_range; // the actual data getter
515  // ======================================================================
516  };
517  // ========================================================================
518  template <class TYPE, class TYPE2>
519  struct GetOrCreateData<TYPE, TYPE2*> : public GetOrCreateData<TYPE, TYPE2> {};
520  template <class TYPE, class TYPE2>
521  struct GetOrCreateData<TYPE*, TYPE2> : public GetOrCreateData<TYPE, TYPE2> {};
522  template <class TYPE, class TYPE2>
523  struct GetOrCreateData<TYPE*, TYPE2*> : public GetOrCreateData<TYPE, TYPE2> {};
524  // ========================================================================
525  template <class TYPE, class TYPE2>
526  struct GetOrCreateData<TYPE, const TYPE2> : public GetOrCreateData<TYPE, TYPE2> {};
527  template <class TYPE, class TYPE2>
528  struct GetOrCreateData<const TYPE, TYPE2> : public GetOrCreateData<TYPE, TYPE2> {};
529  template <class TYPE, class TYPE2>
530  struct GetOrCreateData<const TYPE, const TYPE2> : public GetOrCreateData<TYPE, TYPE2> {};
531  // ========================================================================
532  template <class TYPE, class TYPE2>
533  struct GetOrCreateData<TYPE, TYPE2&> : public GetOrCreateData<TYPE, TYPE2> {};
534  template <class TYPE, class TYPE2>
535  struct GetOrCreateData<TYPE&, TYPE2> : public GetOrCreateData<TYPE, TYPE2> {};
536  template <class TYPE, class TYPE2>
537  struct GetOrCreateData<TYPE&, TYPE2&> : public GetOrCreateData<TYPE, TYPE2> {};
538  // ========================================================================
539  } // namespace Utils
540  // ==========================================================================
541 } // end of namespace Gaudi
542 // ============================================================================
543 // The END
544 // ============================================================================
545 #endif // GAUDIUTILS_GETDATA_H
bool operator()(IDataProviderSvc *service, const std::string &location) const
the only one essential method
Definition: GetData.h:341
Gaudi::NamedRange_< CONTAINER > return_type
Definition: GetData.h:61
Helper structure to define the proper return type for "get"-functions.
Definition: GetData.h:36
return_type operator()(const COMMON &common, IDataProviderSvc *service, const std::string &location, const std::string &location2) const
the only one essential method
Definition: GetData.h:460
Gaudi::NamedRange_< std::vector< const TYPE * > > Type
the actual return type
Definition: GetData.h:242
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
Definition: System.cpp:309
bool isSuccess() const
Definition: StatusCode.h:267
DataObject * getData(IDataProviderSvc *service, const std::string &location) const
get the data form transient store
Definition: GetData.h:219
return_type operator()(const COMMON &common, IDataProviderSvc *service, const std::string &location, const std::string &location2) const
the only one essential method
Definition: GetData.h:506
return_type make_range(const typename TYPE::Selection *cnt) const
Definition: GetData.h:210
sel
Definition: IOTest.py:93
Helper structure for implementation of "get"-functions for GaudiCommon<BASE>
Definition: GaudiCommon.h:54
bool operator()(IDataProviderSvc *service, const std::string &location) const
the only one essential method
Definition: GetData.h:323
Data provider interface definition.
const T & getData() const
return_type make_range(ITERATOR first, ITERATOR last) const
Definition: GetData.h:228
STL class.
TYPE * ptr()
Automatic conversion to data type.
DataObject * getData(IDataProviderSvc *service, const std::string &location) const
get the data form transient store
Definition: GetData.h:354
return_type make_range(const typename TYPE::Selection *cnt) const
Definition: GetData.h:270
return_type operator()(const COMMON &common, IDataProviderSvc *service, const std::string &location, const bool checkData=true) const
the only one essential method
Definition: GetData.h:168
return_type operator()(const COMMON &common, IDataProviderSvc *service, const std::string &location, const std::string &location2) const
the only one essential method
Definition: GetData.h:409
return_type make_range(const typename TYPE::Container *cnt) const
Definition: GetData.h:206
Range make_range(const DataObject *obj)
virtual const id_type & identifier() const =0
Full identifier (or key)
Helper structure for implementation of "exists"-functions for GaudiCommon<BASE>
Definition: GetData.h:314
return_type operator()(const COMMON &common, IDataProviderSvc *service, const std::string &location, const bool checkData=true) const
the only one essential method
Definition: GetData.h:255
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
This file has been imported from LoKi project "C++ ToolKit for Smart and Friendly Physics Analysis" ...
DataObject * accessData()
Static Object retrieval method: must call specific function.
Helper structure for implementation of "getOrCreate"-functions for GaudiCommon<BASE> ...
Definition: GetData.h:388
The IRegistry represents the entry door to the environment any data object residing in a transient da...
Definition: IRegistry.h:22
This file has been imported from LoKi project "C++ ToolKit for Smart and Friendly Physics Analysis" ...
_GetType< Type >::return_type return_type
the actual return type
Definition: GetData.h:118
virtual StatusCode retrieveObject(IRegistry *pDirectory, boost::string_ref path, DataObject *&pObject)=0
Retrieve object identified by its directory entry.
return_type make_range(const typename TYPE::Container *cnt) const
Definition: GetData.h:263
T move(T...args)
Getter::return_type return_type
the actual return type
Definition: GetData.h:398
A small class used to access easily (and efficiently) data items residing in data stores...
_GetType< TYPE >::return_type getFromTS(IDataProviderSvc *service, const std::string &location)
Helper function to provide the minimal lookup and cast functionality of SmartDataPtr used in the help...
Definition: GetData.h:69
DataObject * getData(IDataProviderSvc *service, const std::string &location) const
get the data form transient store
Definition: GetData.h:282
GetData< Gaudi::Range_< std::vector< const TYPE * > > > m_range
===================================================================== the actual processor ...
Definition: GetData.h:289
Gaudi::Range_< std::vector< const TYPE * > > Type
the actual return type
Definition: GetData.h:155
Useful class for representation of "sequence" of the objects through the range of valid iterators...
Definition: Range.h:85
A small class used to access easily (and efficiently) data items residing in data stores...
Definition: SmartDataPtr.h:47
Implements the common functionality between GaudiTools and GaudiAlgorithms.
Definition: GaudiCommon.h:92
return_type operator()(const COMMON &common, IDataProviderSvc *service, const std::string &location, const bool checkData=true) const
the only one essential method
Definition: GetData.h:130
A DataObject is the base class of any identifiable object on any data store.
Definition: DataObject.h:30
Helper functions to set/get the application return code.
Definition: __init__.py:1
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192
GetData< TYPE > Getter
the actual data getter
Definition: GetData.h:392