The Gaudi Framework  master (adcf1ca6)
Loading...
Searching...
No Matches
PropertyAlg.cpp
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 1998-2025 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// Include files
16#include <GaudiKernel/SmartIF.h>
17#include <sstream>
18
20
21#include "PropertyAlg.h"
22
23namespace {
24 // idea coming from The art of computer programming by Knuth
25 constexpr bool essentiallyEqual( double const a, double const b ) {
26 return std::abs( a - b ) <= std::min( std::abs( a ), std::abs( b ) ) * std::numeric_limits<double>::epsilon();
27 }
28} // namespace
29
30// Read Handler
31//------------------------------------------------------------------------------
33 // do not print messages if we are created in genconf
34 const std::string cmd = System::cmdLineArgs()[0];
35 if ( cmd.find( "genconf" ) != std::string::npos ) return;
36
37 info() << "Read handler called for property: " << p << endmsg;
38}
39
40// Update Handler
41//------------------------------------------------------------------------------
43 // avoid the readHandler (which writes to the _same_ MsgStream!) from writing
44 // something in the middle of the printout of this message...
45 std::ostringstream os;
46 os << p;
47 info() << "Update handler called for property: " << os.str() << endmsg;
48}
49
50// Constructor
51//------------------------------------------------------------------------------
52PropertyAlg::PropertyAlg( const std::string& name, ISvcLocator* ploc ) : Algorithm( name, ploc ) {
53 //------------------------------------------------------------------------------
54 // Associate read and update handlers
55
56 p_double.declareUpdateHandler( &PropertyAlg::updateHandler, this );
57 p_double.declareReadHandler( &PropertyAlg::readHandler, this );
58
59 {
60 // do not print messages if we are created in genconf
61 const std::string cmd = System::cmdLineArgs()[0];
62 if ( cmd.find( "genconf" ) != std::string::npos ) return;
63 }
64
65 info() << "Before Initialization......" << endmsg;
66
67 info() << "Int = " << m_int.value() << endmsg;
68 info() << "Int64 = " << m_int64.value() << endmsg;
69 info() << "UInt64 = " << m_uint64.value() << endmsg;
70 info() << "Double = " << m_double.value() << endmsg;
71 info() << "String = " << m_string.value() << endmsg;
72 info() << "Bool = " << m_bool.value() << endmsg;
73 info() << "IntArray = " << m_intarray.value() << endmsg;
74 info() << "Int64Array = " << m_int64array.value() << endmsg;
75 info() << "UInt64Array = " << m_uint64array.value() << endmsg;
76 info() << "DoubleArray = " << m_doublearray.value() << endmsg;
77 info() << "StringArray = " << m_stringarray.value() << endmsg;
78 info() << "BoolArray = " << m_boolarray.value() << endmsg;
79 info() << "EmptyArray = " << m_emptyarray.value() << endmsg;
80 info() << "IntPairArray = " << u_intpairarray.value() << endmsg;
81 info() << "DoublePairArray = " << u_doublepairarray.value() << endmsg;
82 info() << "IntSet = " << m_intset.value() << endmsg;
83 info() << "StringSet = " << m_stringset.value() << endmsg;
84 info() << "FloatUnorderedSet = " << m_floatuset.value() << endmsg;
85
86 info() << "PInt = " << p_int << endmsg;
87 std::ostringstream os;
88 os << p_double; // avoid read handler from printing _during_ info()!
89 info() << "PDouble = " << os.str() << endmsg;
90 info() << "PString = " << p_string << endmsg;
91 info() << "PString2 = " << ( p_string + p_string2 ) << endmsg;
92 info() << "PBool = " << p_bool << endmsg;
93 info() << "PIntArray = " << p_intarray << endmsg;
94 info() << "PDoubleArray = " << p_doublearray << endmsg;
95 info() << "PStringArray = " << p_stringarray << endmsg;
96 info() << "PBoolArray = " << p_boolarray << endmsg;
97}
98
99//------------------------------------------------------------------------------
101 //------------------------------------------------------------------------------
102
103 //
104 // Checking the JobOptions interface. Be able to set the properties
105 //
106 info() << "After Initialization having read the JobOptions file..." << endmsg;
107
108 info() << "Int = " << m_int.value() << endmsg;
109 info() << "Int64 = " << m_int64.value() << endmsg;
110 info() << "UInt64 = " << m_uint64.value() << endmsg;
111 info() << "Double = " << m_double.value() << endmsg;
112 info() << "String = " << m_string.value() << endmsg;
113 info() << "Bool = " << m_bool.value() << endmsg;
114 info() << "IntArray = " << m_intarray.value() << endmsg;
115 info() << "Int64Array = " << m_int64array.value() << endmsg;
116 info() << "UInt64Array = " << m_uint64array.value() << endmsg;
117 info() << "DoubleArray = " << m_doublearray.value() << endmsg;
118 info() << "StringArray = " << m_stringarray.value() << endmsg;
119 info() << "BoolArray = " << m_boolarray.value() << endmsg;
120 info() << "EmptyArray = " << m_emptyarray.value() << endmsg;
121 info() << "IntPairArray = " << u_intpairarray.value() << endmsg;
122 info() << "DoublePairArray = " << u_doublepairarray.value() << endmsg;
123 info() << "IntSet = " << m_intset.value() << endmsg;
124 info() << "StringSet = " << m_stringset.value() << endmsg;
125 info() << "FloatUnorderedSet = " << m_floatuset.value() << endmsg;
126 info() << "StringMap = " << m_strmap << endmsg;
127
128 info() << "PInt = " << p_int << endmsg;
129 std::ostringstream os;
130 os << p_double; // avoid read handler from printing _during_ info()!
131 info() << "PDouble = " << os.str() << endmsg;
132 info() << "PString = " << p_string << endmsg;
133 info() << "PString2 = " << ( p_string + p_string2 ) << endmsg;
134 info() << "PBool = " << p_bool << endmsg;
135 info() << "PIntArray = " << p_intarray << endmsg;
136 info() << "PDoubleArray = " << p_doublearray << endmsg;
137 info() << "PStringArray = " << p_stringarray << endmsg;
138 info() << "PBoolArray = " << p_boolarray << endmsg;
139 //
140 // Checking units
141 //
142 auto& doublearrayunits = u_doublearrayunits.value();
143 auto& doublearray = u_doublearray.value();
144 for ( unsigned int i = 0; i < doublearrayunits.size(); i++ ) {
145
146 if ( !essentiallyEqual( doublearrayunits[i], doublearray[i] ) ) {
147 error() << format( "DoubleArrayWithUnits[%d] = %g and should be %g", i, doublearrayunits[i], doublearray[i] )
148 << endmsg;
149 } else {
150 info() << format( "DoubleArrayWithUnits[%d] = %g", i, doublearrayunits[i] ) << endmsg;
151 }
152 }
153
154 //
155 // Checking the Property Verifier
156 //
157 info() << "===============Checking Property Verifier ===============" << endmsg;
158
159 info() << "Playing with PropertyVerifiers..." << endmsg;
160
161 p_int.verifier().setBounds( 0, 200 );
162 p_int = 155;
163 // info() << "PInt= " << p_int << " [should be 155, bounds are " <<
164 // p_int.verifier().lower() << ", " <<
165 // p_int.verifier().upper() << " ]" << endmsg;
166 info() << format( "PInt= %d [should be 155, bounds are %d, %d]", (int)p_int, (int)p_int.verifier().lower(),
167 (int)p_int.verifier().upper() )
168 << endmsg;
169 try {
170 p_int = 255;
171 } catch ( ... ) { info() << "Got an exception when setting a value outside bounds" << endmsg; }
172 info() << "PInt= " << p_int << " [should be 155]" << endmsg;
173
174 //
175 // Checking the Property CallBacks
176 //
177 info() << "===============Checking Property CallBaks ===============" << endmsg;
178
179 double d;
180 info() << "Accessing PDouble ... " << endmsg;
181 d = p_double;
182
183 info() << "Value obtained is: " << d << endmsg;
184
185 info() << "Updating PDouble ... " << endmsg;
186 p_double = 999.;
187
188 //
189 // Checking Accessing Properties by string
190 //
191
192 info() << "==========Checking Accesing Properties by string=========" << endmsg;
193
194 auto appmgr = serviceLocator()->as<IProperty>();
195 // StatusCode sc = serviceLocator()->service("ApplicationMgr", appmgr);
196 if ( !appmgr ) {
197 error() << "Unable to locate the ApplicationMgr" << endmsg;
198 } else {
199 std::string value( "empty" );
200 appmgr->getProperty( "ExtSvc", value ).ignore();
201 info() << " Got property ApplicationMgr.ExtSvc = " << value << ";" << endmsg;
202
203 appmgr->setPropertyRepr( "ExtSvc", "[\"EvtDataSvc/EventDataSvc\", \"DetDataSvc/DetectorDataSvc\"]" ).ignore();
204 info() << " Set property ApplicationMgr.ExtSvc = "
205 << " [\"EvtDataSvc/EventDataSvc\", \"DetDataSvc/DetectorDataSvc\"]"
206 << ";" << endmsg;
207 appmgr->getProperty( "ExtSvc", value ).ignore();
208 info() << " Got property ApplicationMgr.ExtSvc = " << value << ";" << endmsg;
209
210 appmgr->setPropertyRepr( "ExtSvc", "[ 'EventDataSvc', 'DetectorDataSvc']" ).ignore();
211 info() << " Set property ApplicationMgr.ExtSvc = "
212 << " [ 'EventDataSvc', 'DetectorDataSvc']"
213 << ";" << endmsg;
214 appmgr->getProperty( "ExtSvc", value ).ignore();
215 info() << " Got property ApplicationMgr.ExtSvc = " << value << ";" << endmsg;
216 }
217 // Testing setting bool
218 std::vector<std::tuple<std::string, bool>> cases = {
219 std::make_tuple( "true", true ), std::make_tuple( "false", false ), std::make_tuple( "True", true ),
220 std::make_tuple( "False", false ), std::make_tuple( "TRUE", true ), std::make_tuple( "FALSE", false ),
221 std::make_tuple( "T", true ), // not expected to work
222 std::make_tuple( "F", false ), // not expected to work
223 std::make_tuple( "10", true ), // not expected to work
224 };
226 for ( auto& c : cases ) {
227 p_bool = m_bool = !std::get<1>( c );
228 try {
229 sc = setPropertyRepr( "PBool", std::get<0>( c ) );
230 } catch ( ... ) { sc = StatusCode::FAILURE; }
231 if ( !sc || ( p_bool != std::get<1>( c ) ) ) {
232 error() << "PBool can not be set to " << std::get<0>( c ) << endmsg;
233 }
234 try {
235 sc = setPropertyRepr( "Bool", std::get<0>( c ) );
236 } catch ( ... ) { sc = StatusCode::FAILURE; }
237 if ( !sc || ( m_bool != std::get<1>( c ) ) ) { error() << "Bool can not be set to " << std::get<0>( c ) << endmsg; }
238 }
239
240 // Testing the control of the output level directly from MessageSvc
241 MsgStream newlog( msgSvc(), "MsgTest" );
242 newlog << MSG::VERBOSE << "This should be printed if threshold is VERBOSE" << endmsg;
243 newlog << MSG::DEBUG << "This should be printed if threshold is DEBUG" << endmsg;
244 newlog << MSG::INFO << "This should be printed if threshold is INFO" << endmsg;
245 newlog << MSG::WARNING << "This should be printed if threshold is WARNING" << endmsg;
246 newlog << MSG::ERROR << "This should be printed if threshold is ERROR" << endmsg;
247 newlog << MSG::FATAL << "This should be printed if threshold is FATAL" << endmsg;
248 newlog << MSG::ALWAYS << "This should be printed ALWAYS" << endmsg;
249
250 // Testing access to the JobOptions catalogue
251 auto& opts = serviceLocator()->getOptsSvc();
252
253 // Dump of the catalogue
254 info() << "=================================================" << endmsg;
256 auto opt_items = opts.items(); // this copy is just to hide differences between gcc and clang outputs
257 ostream_joiner( info() << "Dump of the property catalogue:\n", opt_items, '\n',
258 []( MsgStream& os, const auto& item ) -> MsgStream& {
259 return os << std::get<0>( item ) << ": " << std::get<1>( item );
260 } )
261 << endmsg;
262 info() << "=================================================" << endmsg;
263
264 // Change an option of my own....
265 opts.set( name() + '.' + "PInt", "154" );
266 info() << "PInt= " << p_int << " [should be 154]" << endmsg;
267
268 auto orig_policy =
270 try {
271 info() << "Try to assign invalid value to DoubleArray" << endmsg;
272 opts.set( name() + '.' + "DoubleArray", "{\"12.12\", \"13.13\"}" );
273 } catch ( const GaudiException& exc ) { error() << "got exception: " << exc.what() << endmsg; }
274
275 try {
276 info() << "Try to assign invalid value to StringMap" << endmsg;
277 opts.set( name() + '.' + "StringMap", "{\"one\", {\"une\", \"eins\"}}" );
278 } catch ( const GaudiException& exc ) { error() << "got exception: " << exc.what() << endmsg; }
279
281
282 info() << "DoubleArray = " << m_doublearray.value() << endmsg;
283 info() << "=================================================" << endmsg;
284 return StatusCode::SUCCESS;
285}
286
287//------------------------------------------------------------------------------
289 //------------------------------------------------------------------------------
290 info() << "executing...." << endmsg;
291 return StatusCode::SUCCESS;
292}
293
294//------------------------------------------------------------------------------
296 //------------------------------------------------------------------------------
297 info() << "finalizing...." << endmsg;
298 return StatusCode::SUCCESS;
299}
300
301// Static Factory declaration
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition MsgStream.cpp:93
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition MsgStream.h:198
#define DECLARE_COMPONENT(type)
Provide serialization function (output only) for some common STL classes (vectors,...
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
const SmartIF< IMessageSvc > & msgSvc() const
The standard message service.
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
Algorithm(std::string name, ISvcLocator *svcloc, std::string version=PACKAGE_VERSION)
Constructor.
Definition Algorithm.h:98
SmartIF< ISvcLocator > & serviceLocator() const override
The standard service locator.
const std::string & name() const override
The identifying name of the algorithm object.
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
Define general base for Gaudi exception.
const char * what() const override
method from std::exception
The IProperty is the basic interface for all components which have properties that can be set or get.
Definition IProperty.h:32
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition ISvcLocator.h:42
SmartIF< IFace > as()
Definition ISvcLocator.h:64
Gaudi::Interfaces::IOptionsSvc & getOptsSvc()
Direct access to Gaudi::Interfaces::IOptionsSvc implementation.
Definition of the MsgStream class used to transmit messages.
Definition MsgStream.h:29
Trivial Algorithm for tutorial purposes.
Definition PropertyAlg.h:28
void readHandler(Gaudi::Details::PropertyBase &)
Callbacks for properties.
Gaudi::Property< double > m_double
Definition PropertyAlg.h:48
Gaudi::Property< std::map< std::string, std::string > > m_strmap
Definition PropertyAlg.h:69
Gaudi::Property< unsigned long long > m_uint64
Definition PropertyAlg.h:47
Gaudi::Property< std::vector< std::pair< double, double > > > u_doublepairarray
Definition PropertyAlg.h:63
Gaudi::Property< std::vector< std::pair< int, int > > > u_intpairarray
Definition PropertyAlg.h:62
Gaudi::Property< std::vector< int > > p_intarray
Definition PropertyAlg.h:78
Gaudi::Property< std::string > p_string2
Definition PropertyAlg.h:75
Gaudi::Property< int > m_int
These data members are used in the execution of this algorithm They are set in the initialization pha...
Definition PropertyAlg.h:45
Gaudi::Property< std::vector< std::string > > p_stringarray
Definition PropertyAlg.h:80
Gaudi::Property< std::string > p_string
Definition PropertyAlg.h:74
StatusCode finalize() override
Gaudi::Property< bool > m_bool
Definition PropertyAlg.h:50
Gaudi::Property< std::vector< double > > u_doublearrayunits
Definition PropertyAlg.h:59
Gaudi::Property< bool > p_bool
Definition PropertyAlg.h:76
StatusCode initialize() override
Three mandatory member functions of any algorithm.
Gaudi::Property< std::vector< long long > > m_int64array
Definition PropertyAlg.h:53
Gaudi::Property< std::vector< bool > > p_boolarray
Definition PropertyAlg.h:81
Gaudi::Property< long long > m_int64
Definition PropertyAlg.h:46
Gaudi::Property< std::vector< bool > > m_boolarray
Definition PropertyAlg.h:57
Gaudi::PropertyWithReadHandler< double > p_double
Definition PropertyAlg.h:72
Gaudi::Property< std::set< std::string > > m_stringset
Definition PropertyAlg.h:66
Gaudi::Property< std::vector< int > > m_intarray
Definition PropertyAlg.h:52
Gaudi::Property< std::vector< unsigned long long > > m_uint64array
Definition PropertyAlg.h:54
Gaudi::CheckedProperty< int > p_int
Definition PropertyAlg.h:71
Gaudi::Property< std::string > m_string
Definition PropertyAlg.h:49
Gaudi::Property< std::vector< std::string > > m_stringarray
Definition PropertyAlg.h:56
Gaudi::Property< std::unordered_set< float > > m_floatuset
Definition PropertyAlg.h:67
Gaudi::Property< std::vector< double > > p_doublearray
Definition PropertyAlg.h:79
PropertyAlg(const std::string &name, ISvcLocator *pSvcLocator)
Constructor of this form must be provided.
StatusCode execute() override
void updateHandler(Gaudi::Details::PropertyBase &)
Gaudi::Property< std::vector< double > > m_emptyarray
Definition PropertyAlg.h:58
Gaudi::Property< std::vector< double > > m_doublearray
Definition PropertyAlg.h:55
Gaudi::Property< std::vector< double > > u_doublearray
Definition PropertyAlg.h:60
Gaudi::Property< std::set< int > > m_intset
Definition PropertyAlg.h:65
This class is used for returning status codes from appropriate routines.
Definition StatusCode.h:64
constexpr static const auto SUCCESS
Definition StatusCode.h:99
constexpr static const auto FAILURE
Definition StatusCode.h:100
ParsingErrorPolicy setParsingErrorPolicy(ParsingErrorPolicy p)
Definition Property.cpp:477
Stream & ostream_joiner(Stream &os, Iterator first, Iterator last, Separator sep, OutputElement output=OutputElement{})
@ WARNING
Definition IMessageSvc.h:22
@ FATAL
Definition IMessageSvc.h:22
@ DEBUG
Definition IMessageSvc.h:22
@ ERROR
Definition IMessageSvc.h:22
@ ALWAYS
Definition IMessageSvc.h:22
@ INFO
Definition IMessageSvc.h:22
@ VERBOSE
Definition IMessageSvc.h:22
GAUDI_API const std::vector< std::string > cmdLineArgs()
Command line arguments including executable name as arg[0] as vector of strings.
Definition System.cpp:310
Gaudi::ParticleID abs(const Gaudi::ParticleID &p)
Return the absolute value for a PID.
Definition ParticleID.h:191