| The Gaudi Framework v32r1 (f65d50dc)

DataObjectHandleBase.cpp
Go to the documentation of this file.
1 #include "GaudiKernel/DataObjectHandleBase.h"
2 #include "GaudiKernel/AlgTool.h"
3 #include "GaudiKernel/IDataProviderSvc.h"
4 #include "GaudiKernel/SerializeSTL.h"
5 #include "GaudiKernel/ServiceLocatorHelper.h"
6 #include <Gaudi/Algorithm.h>
7
8 #include <boost/tokenizer.hpp>
9 #include <ostream>
10 #include <sstream>
11 #include <string>
12
13 const DataObjID INVALID_DATAOBJID = DataObjID();
14
15 //---------------------------------------------------------------------------
16 DataObjectHandleBase::DataObjectHandleBase( DataObjectHandleBase&& other )
17 : Gaudi::DataHandle( other )
18 , m_EDS( std::move( other.m_EDS ) )
19 , m_MS( std::move( other.m_MS ) )
20 , m_init( other.m_init )
21 , m_optional( other.m_optional )
22 , m_wasRead( other.m_wasRead )
23 , m_wasWritten( other.m_wasWritten )
24 , m_searchDone( other.m_searchDone ) {
25 m_owner->declare( *this );
26 }
27
28 //---------------------------------------------------------------------------
29 DataObjectHandleBase& DataObjectHandleBase::operator=( const DataObjectHandleBase& other ) {
30 // avoid modification of searchDone in other while we are copying
31 std::lock_guard<std::mutex> guard( other.m_searchMutex );
32 // FIXME: operator= should not change our owner, only our 'value'
33 Gaudi::DataHandle::operator=( other );
34 m_EDS = other.m_EDS;
35 m_MS = other.m_MS;
36 m_init = other.m_init;
37 m_optional = other.m_optional;
38 m_wasRead = other.m_wasRead;
39 m_wasWritten = other.m_wasWritten;
40 m_searchDone = other.m_searchDone;
41 return *this;
42 }
43
44 //---------------------------------------------------------------------------
45 DataObjectHandleBase::DataObjectHandleBase( const DataObjID& k, Gaudi::DataHandle::Mode a, IDataHandleHolder* owner )
46 : Gaudi::DataHandle( k, a, owner ) {
47 m_owner->declare( *this );
48 }
49
50 //---------------------------------------------------------------------------
51
52 DataObjectHandleBase::DataObjectHandleBase( const std::string& k, Gaudi::DataHandle::Mode a, IDataHandleHolder* owner )
53 : DataObjectHandleBase( DataObjID( k ), a, owner ) {}
54
55 //---------------------------------------------------------------------------
56 DataObjectHandleBase::~DataObjectHandleBase() { owner()->renounce( *this ); }
57
58 //---------------------------------------------------------------------------
59 DataObject* DataObjectHandleBase::fetch() const {
60 DataObject* p = nullptr;
61 if ( LIKELY( m_searchDone ) ) { // fast path: searchDone, objKey is in its final state
62 m_EDS->retrieveObject( objKey(), p ).ignore();
63 return p;
64 }
65
66 // slow path -- move into seperate function to improve code generation?
67
68 // convenience towards users -- remind them to register
69 // but as m_MS is not yet set, we cannot use a MsgStream...
70 if ( !m_init ) {
71 std::cerr << ( owner() ? owner()->name() : "<UNKNOWN>:" ) << "DataObjectHandle: uninitialized data handle"
72 << std::endl;
73 }
74
75 // as m_searchDone is not true yet, objKey may change... so in this
76 // branch we _first_ grab the mutex, to avoid objKey changing while we use it
77
78 // take a lock to be sure we only execute this once at a time
79 std::lock_guard<std::mutex> guard( m_searchMutex );
80
81 StatusCode sc = m_EDS->retrieveObject( objKey(), p );
82 if ( m_searchDone ) { // another thread has done the search while we were blocked
83 // on the mutex. As a result, nothing left to do but return...
84 sc.ignore();
85 return p;
86 }
87
88 if ( !sc.isSuccess() ) {
89 auto tokens = boost::tokenizer<boost::char_separator<char>>{objKey(), boost::char_separator<char>{":"}};
90 // let's try our alternatives (if any)
91 auto alt = std::find_if( tokens.begin(), tokens.end(),
92 [&]( const std::string& n ) { return m_EDS->retrieveObject( n, p ).isSuccess(); } );
93 if ( alt != tokens.end() ) {
94 MsgStream log( m_MS, owner()->name() + ":DataObjectHandle" );
95 log << MSG::DEBUG << ": could not find \"" << objKey() << "\" -- using alternative source: \"" << *alt
96 << "\" instead" << endmsg;
97 // found something -- set it as default; this is not atomic, but
98 // at least in `fetch` there is no use of `objKey` that races with
99 // this assignment... (but there may be others!)
100 setKey( *alt );
101 }
102 }
103 m_searchDone = true;
104 return p;
105 }
106
107 //---------------------------------------------------------------------------
108
109 std::string DataObjectHandleBase::toString() const { return objKey(); }
110
111 //---------------------------------------------------------------------------
112 std::string DataObjectHandleBase::pythonRepr() const {
113 std::string m;
114 switch ( mode() ) {
115 case Gaudi::DataHandle::Mode::Reader:
116 m = "R";
117 break;
118 case Gaudi::DataHandle::Mode::Writer:
119 m = "W";
120 break;
121 default:
122 m = "UNKNOWN";
123 break;
124 }
125 return "DataObjectHandleBase('" + toString() + "', '" + m + "')";
126 }
127
128 //---------------------------------------------------------------------------
129 void DataObjectHandleBase::fromString( const std::string& s ) { Gaudi::Parsers::parse( *this, s ).ignore(); }
130
131 //---------------------------------------------------------------------------
132
133 bool DataObjectHandleBase::init() {
134
135 assert( !m_init );
136
137 if ( !owner() ) return false;
138
139 setRead( false );
140 setWritten( false );
141
142 Gaudi::Algorithm* algorithm = dynamic_cast<Gaudi::Algorithm*>( owner() );
143 if ( algorithm ) {
144 // Fetch the event Data Service from the algorithm
145 m_EDS = algorithm->evtSvc();
146 m_MS = algorithm->msgSvc();
147 } else {
148 AlgTool* tool = dynamic_cast<AlgTool*>( owner() );
149 if ( tool ) {
150 m_EDS = tool->evtSvc();
151 m_MS = tool->msgSvc();
152 } else {
153 throw GaudiException( "owner is neither AlgTool nor Gaudi::Algorithm", "Invalid Cast", StatusCode::FAILURE );
154 }
155 }
156 m_init = true;
157 return true;
158 }
159
160 //---------------------------------------------------------------------------
161
162 bool DataObjectHandleBase::isValid() const { return fullKey() != INVALID_DATAOBJID; }
163
164 //---------------------------------------------------------------------------
165
166 std::ostream& operator<<( std::ostream& str, const DataObjectHandleBase& d ) {
167
168 str << d.fullKey() << " m: " << d.mode();
169 if ( d.owner() ) str << " o: " << d.owner()->name();
170 str << " opt: " << d.isOptional();
171 return str;
172 }
Gaudi::DataHandle::mode
virtual Mode mode() const
Definition: DataHandle.h:42
DataObjectHandleBase::setWritten
void setWritten(bool wasWritten=true)
Definition: DataObjectHandleBase.h:67
MsgStream
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
DataObjectHandleBase::~DataObjectHandleBase
virtual ~DataObjectHandleBase()
Definition: DataObjectHandleBase.cpp:56
GaudiException
Define general base for Gaudi exception.
Definition: GaudiException.h:21
CommonMessagingBase::msgSvc
const SmartIF< IMessageSvc > & msgSvc() const
The standard message service.
Definition: CommonMessaging.h:68
IDataHandleHolder::declare
virtual void declare(Gaudi::DataHandle &)=0
gaudiComponentHelp.name
name
Definition: gaudiComponentHelp.py:41
DataObjectHandleBase::m_wasRead
bool m_wasRead
Definition: DataObjectHandleBase.h:79
DataObjID
Definition: DataObjID.h:37
Gaudi::DataHandle::owner
virtual IDataHandleHolder * owner() const
Definition: DataHandle.h:40
AlgTool.h
DataObjectHandleBase::pythonRepr
std::string pythonRepr() const override
Definition: DataObjectHandleBase.cpp:112
MSG::DEBUG
Definition: IMessageSvc.h:15
std::endl
T endl(T... args)
Gaudi::DataHandle::objKey
virtual const std::string & objKey() const
Definition: DataHandle.h:47
DataObjectHandleBase::m_EDS
SmartIF< IDataProviderSvc > m_EDS
Definition: DataObjectHandleBase.h:74
Gaudi::Algorithm::evtSvc
SmartIF< IDataProviderSvc > & evtSvc() const
shortcut for method eventSvc
Definition: Algorithm.h:265
std
STL namespace.
DataObjectHandleBase::m_init
bool m_init
Definition: DataObjectHandleBase.h:77
Gaudi::Parsers::parse
StatusCode parse(GaudiUtils::HashMap< K, V > &result, const std::string &input)
Basic parser for the types of HashMap used in DODBasicMapper.
Definition: DODBasicMapper.cpp:11
IDataHandleHolder::renounce
virtual void renounce(Gaudi::DataHandle &)=0
Gaudi::DataHandle::setKey
virtual void setKey(const DataObjID &key) const
Definition: DataHandle.h:44
DataObjectHandleBase::setRead
void setRead(bool wasRead=true)
Definition: DataObjectHandleBase.h:66
DataObjectHandleBase::m_searchDone
bool m_searchDone
Whether the search part of the fetch method (so dealing with alt names was already executed or not.
Definition: DataObjectHandleBase.h:87
SerializeSTL.h
Provide serialization function (output only) for some common STL classes (vectors,...
DataObjectHandleBase::m_optional
bool m_optional
Definition: DataObjectHandleBase.h:78
INamedInterface::name
virtual const std::string & name() const =0
Retrieve the name of the instance.
std::string
STL class.
GaudiPluginService.cpluginsvc.n
n
Definition: cpluginsvc.py:210
DataObjectHandleBase::operator=
DataObjectHandleBase & operator=(const DataObjectHandleBase &)
Definition: DataObjectHandleBase.cpp:29
IDataHandleHolder
Definition: IDataHandleHolder.h:14
IDataProviderSvc.h
Gaudi::DataHandle::fullKey
virtual const DataObjID & fullKey() const
Definition: DataHandle.h:48
Algorithm.h
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:50
Gaudi::Units::m
constexpr double m
Definition: SystemOfUnits.h:92
operator<<
std::ostream & operator<<(std::ostream &str, const DataObjectHandleBase &d)
Definition: DataObjectHandleBase.cpp:166
DataObjectHandleBase::isOptional
bool isOptional() const
Check if the data object declared is optional for the algorithm.
Definition: DataObjectHandleBase.h:56
Gaudi::DataHandle::m_owner
IDataHandleHolder * m_owner
Definition: DataHandle.h:66
INVALID_DATAOBJID
const DataObjID INVALID_DATAOBJID
Definition: DataObjectHandleBase.cpp:13
StatusCode::isSuccess
bool isSuccess() const
Definition: StatusCode.h:267
DataObjectHandleBase::fromString
void fromString(const std::string &s)
Definition: DataObjectHandleBase.cpp:129
LIKELY
#define LIKELY(x)
Definition: Kernel.h:95
DataObjectHandleBase.h
std::find_if
T find_if(T... args)
StatusCode::ignore
const StatusCode & ignore() const
Ignore/check StatusCode.
Definition: StatusCode.h:153
IDataProviderSvc::retrieveObject
virtual StatusCode retrieveObject(IRegistry *pDirectory, std::string_view path, DataObject *&pObject)=0
Retrieve object identified by its directory entry.
std::lock_guard
DataObjectHandleBase
DataObjectHandleBase GaudiKernel/DataObjectHandleBase.h.
Definition: DataObjectHandleBase.h:26
DataObjectHandleBase::isValid
bool isValid() const
Definition: DataObjectHandleBase.cpp:162
DataObjectHandleBase::fetch
DataObject * fetch() const
Definition: DataObjectHandleBase.cpp:59
DataObjectHandleBase::m_searchMutex
std::mutex m_searchMutex
A Mutex protecting the calls to the search part of the fetch method, so that we are sure that we only...
Definition: DataObjectHandleBase.h:93
AlgTool
Base class from which all the concrete tool classes should be derived.
Definition: AlgTool.h:47
Gaudi::Algorithm
Base class from which all concrete algorithm classes should be derived.
Definition: Algorithm.h:79
gaudirun.s
string s
Definition: gaudirun.py:316
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:86
ServiceLocatorHelper.h
DataObjectHandleBase::init
bool init() override
Definition: DataObjectHandleBase.cpp:133
DataObjectHandleBase::toString
std::string toString() const
Definition: DataObjectHandleBase.cpp:109
DataObjectHandleBase::m_MS
SmartIF< IMessageSvc > m_MS
Definition: DataObjectHandleBase.h:75
Gaudi::DataHandle::Mode
Mode
Definition: DataHandle.h:29
AlgTool::evtSvc
IDataProviderSvc * evtSvc() const
accessor to event service service
Definition: AlgTool.cpp:88
DataObject
A DataObject is the base class of any identifiable object on any data store.
Definition: DataObject.h:30
std::cerr
Gaudi
Header file for std:chrono::duration-based Counters.
Definition: __init__.py:1
compareOutputFiles.Reader
def Reader(readerType, filename, qacross, qToEngine)
Definition: compareOutputFiles.py:126
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192
DataObjectHandleBase::DataObjectHandleBase
DataObjectHandleBase(const DataObjID &k, Gaudi::DataHandle::Mode a, IDataHandleHolder *owner)
Definition: DataObjectHandleBase.cpp:45
DataObjectHandleBase::m_wasWritten
bool m_wasWritten
Definition: DataObjectHandleBase.h:80
Gaudi.Configuration.log
log
Definition: Configuration.py:13