Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v36r7 (7f57a304)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
PropertyAlg.cpp
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2020 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
12 #include "GaudiKernel/DataObject.h"
15 #include "GaudiKernel/MsgStream.h"
16 #include "GaudiKernel/SmartIF.h"
17 #include <sstream>
18 
20 
21 #include "PropertyAlg.h"
22 
23 #ifdef __ICC
24 // disable icc remark #1572: floating-point equality and inequality comparisons are unreliable
25 # pragma warning( disable : 1572 )
26 #endif
27 
28 // Read Handler
29 //------------------------------------------------------------------------------
31  // do not print messages if we are created in genconf
32  const std::string cmd = System::cmdLineArgs()[0];
33  if ( cmd.find( "genconf" ) != std::string::npos ) return;
34 
35  info() << "Read handler called for property: " << p << endmsg;
36 }
37 
38 // Update Handler
39 //------------------------------------------------------------------------------
41  // avoid the readHandler (which writes to the _same_ MsgStream!) from writing
42  // something in the middle of the printout of this message...
44  os << p;
45  info() << "Update handler called for property: " << os.str() << endmsg;
46 }
47 
48 // Constructor
49 //------------------------------------------------------------------------------
51  //------------------------------------------------------------------------------
52  // Associate read and update handlers
53 
54  p_double.declareUpdateHandler( &PropertyAlg::updateHandler, this );
55  p_double.declareReadHandler( &PropertyAlg::readHandler, this );
56 
57  {
58  // do not print messages if we are created in genconf
59  const std::string cmd = System::cmdLineArgs()[0];
60  if ( cmd.find( "genconf" ) != std::string::npos ) return;
61  }
62 
63  info() << "Before Initialization......" << endmsg;
64 
65  info() << "Int = " << m_int.value() << endmsg;
66  info() << "Int64 = " << m_int64.value() << endmsg;
67  info() << "UInt64 = " << m_uint64.value() << endmsg;
68  info() << "Double = " << m_double.value() << endmsg;
69  info() << "String = " << m_string.value() << endmsg;
70  info() << "Bool = " << m_bool.value() << endmsg;
71  info() << "IntArray = " << m_intarray.value() << endmsg;
72  info() << "Int64Array = " << m_int64array.value() << endmsg;
73  info() << "UInt64Array = " << m_uint64array.value() << endmsg;
74  info() << "DoubleArray = " << m_doublearray.value() << endmsg;
75  info() << "StringArray = " << m_stringarray.value() << endmsg;
76  info() << "BoolArray = " << m_boolarray.value() << endmsg;
77  info() << "EmptyArray = " << m_emptyarray.value() << endmsg;
78  info() << "IntPairArray = " << u_intpairarray.value() << endmsg;
79  info() << "DoublePairArray = " << u_doublepairarray.value() << endmsg;
80 
81  info() << "PInt = " << p_int << endmsg;
83  os << p_double; // avoid read handler from printing _during_ info()!
84  info() << "PDouble = " << os.str() << endmsg;
85  info() << "PString = " << p_string << endmsg;
86  info() << "PBool = " << p_bool << endmsg;
87  info() << "PIntArray = " << p_intarray << endmsg;
88  info() << "PDoubleArray = " << p_doublearray << endmsg;
89  info() << "PStringArray = " << p_stringarray << endmsg;
90  info() << "PBoolArray = " << p_boolarray << endmsg;
91 }
92 
93 //------------------------------------------------------------------------------
95  //------------------------------------------------------------------------------
96 
97  //
98  // Checking the JobOptions interface. Be able to set the properties
99  //
100  info() << "After Initialization having read the JobOptions file..." << endmsg;
101 
102  info() << "Int = " << m_int.value() << endmsg;
103  info() << "Int64 = " << m_int64.value() << endmsg;
104  info() << "UInt64 = " << m_uint64.value() << endmsg;
105  info() << "Double = " << m_double.value() << endmsg;
106  info() << "String = " << m_string.value() << endmsg;
107  info() << "Bool = " << m_bool.value() << endmsg;
108  info() << "IntArray = " << m_intarray.value() << endmsg;
109  info() << "Int64Array = " << m_int64array.value() << endmsg;
110  info() << "UInt64Array = " << m_uint64array.value() << endmsg;
111  info() << "DoubleArray = " << m_doublearray.value() << endmsg;
112  info() << "StringArray = " << m_stringarray.value() << endmsg;
113  info() << "BoolArray = " << m_boolarray.value() << endmsg;
114  info() << "EmptyArray = " << m_emptyarray.value() << endmsg;
115  info() << "IntPairArray = " << u_intpairarray.value() << endmsg;
116  info() << "DoublePairArray = " << u_doublepairarray.value() << endmsg;
117  info() << "StringMap = " << m_strmap << endmsg;
118 
119  info() << "PInt = " << p_int << endmsg;
121  os << p_double; // avoid read handler from printing _during_ info()!
122  info() << "PDouble = " << os.str() << endmsg;
123  info() << "PString = " << p_string << endmsg;
124  info() << "PBool = " << p_bool << endmsg;
125  info() << "PIntArray = " << p_intarray << endmsg;
126  info() << "PDoubleArray = " << p_doublearray << endmsg;
127  info() << "PStringArray = " << p_stringarray << endmsg;
128  info() << "PBoolArray = " << p_boolarray << endmsg;
129  //
130  // Checking units
131  //
132  auto& doublearrayunits = u_doublearrayunits.value();
133  auto& doublearray = u_doublearray.value();
134  for ( unsigned int i = 0; i < doublearrayunits.size(); i++ ) {
135 
136  if ( doublearrayunits[i] != doublearray[i] ) {
137  error() << format( "DoubleArrayWithUnits[%d] = %g and should be %g", i, doublearrayunits[i], doublearray[i] )
138  << endmsg;
139  } else {
140  info() << format( "DoubleArrayWithUnits[%d] = %g", i, doublearrayunits[i] ) << endmsg;
141  }
142  }
143 
144  //
145  // Checking the Property Verifier
146  //
147  info() << "===============Checking Property Verifier ===============" << endmsg;
148 
149  info() << "Playing with PropertyVerifiers..." << endmsg;
150 
151  p_int.verifier().setBounds( 0, 200 );
152  p_int = 155;
153  // info() << "PInt= " << p_int << " [should be 155, bounds are " <<
154  // p_int.verifier().lower() << ", " <<
155  // p_int.verifier().upper() << " ]" << endmsg;
156  info() << format( "PInt= %d [should be 155, bounds are %d, %d]", (int)p_int, (int)p_int.verifier().lower(),
157  (int)p_int.verifier().upper() )
158  << endmsg;
159  try {
160  p_int = 255;
161  } catch ( ... ) { info() << "Got an exception when setting a value outside bounds" << endmsg; }
162  info() << "PInt= " << p_int << " [should be 155]" << endmsg;
163 
164  //
165  // Checking the Property CallBacks
166  //
167  info() << "===============Checking Property CallBaks ===============" << endmsg;
168 
169  double d;
170  info() << "Accessing PDouble ... " << endmsg;
171  d = p_double;
172 
173  info() << "Value obtained is: " << d << endmsg;
174 
175  info() << "Updating PDouble ... " << endmsg;
176  p_double = 999.;
177 
178  //
179  // Checking Accessing Properties by string
180  //
181 
182  info() << "==========Checking Accesing Properties by string=========" << endmsg;
183 
184  auto appmgr = serviceLocator()->as<IProperty>();
185  // StatusCode sc = serviceLocator()->service("ApplicationMgr", appmgr);
186  if ( !appmgr ) {
187  error() << "Unable to locate the ApplicationMgr" << endmsg;
188  } else {
189  std::string value( "empty" );
190  appmgr->getProperty( "ExtSvc", value ).ignore();
191  info() << " Got property ApplicationMgr.ExtSvc = " << value << ";" << endmsg;
192 
193  appmgr->setPropertyRepr( "ExtSvc", "[\"EvtDataSvc/EventDataSvc\", \"DetDataSvc/DetectorDataSvc\"]" ).ignore();
194  info() << " Set property ApplicationMgr.ExtSvc = "
195  << " [\"EvtDataSvc/EventDataSvc\", \"DetDataSvc/DetectorDataSvc\"]"
196  << ";" << endmsg;
197  appmgr->getProperty( "ExtSvc", value ).ignore();
198  info() << " Got property ApplicationMgr.ExtSvc = " << value << ";" << endmsg;
199 
200  appmgr->setPropertyRepr( "ExtSvc", "[ 'EventDataSvc', 'DetectorDataSvc']" ).ignore();
201  info() << " Set property ApplicationMgr.ExtSvc = "
202  << " [ 'EventDataSvc', 'DetectorDataSvc']"
203  << ";" << endmsg;
204  appmgr->getProperty( "ExtSvc", value ).ignore();
205  info() << " Got property ApplicationMgr.ExtSvc = " << value << ";" << endmsg;
206  }
207  // Testing setting bool
209  std::make_tuple( "true", true ), std::make_tuple( "false", false ), std::make_tuple( "True", true ),
210  std::make_tuple( "False", false ), std::make_tuple( "TRUE", true ), std::make_tuple( "FALSE", false ),
211  std::make_tuple( "T", true ), // not expected to work
212  std::make_tuple( "F", false ), // not expected to work
213  std::make_tuple( "10", true ), // not expected to work
214  };
216  for ( auto& c : cases ) {
217  p_bool = m_bool = !std::get<1>( c );
218  try {
219  sc = setPropertyRepr( "PBool", std::get<0>( c ) );
220  } catch ( ... ) { sc = StatusCode::FAILURE; }
221  if ( !sc || ( p_bool != std::get<1>( c ) ) ) {
222  error() << "PBool can not be set to " << std::get<0>( c ) << endmsg;
223  }
224  try {
225  sc = setPropertyRepr( "Bool", std::get<0>( c ) );
226  } catch ( ... ) { sc = StatusCode::FAILURE; }
227  if ( !sc || ( m_bool != std::get<1>( c ) ) ) { error() << "Bool can not be set to " << std::get<0>( c ) << endmsg; }
228  }
229 
230  // Testing the control of the output level directly from MessageSvc
231  MsgStream newlog( msgSvc(), "MsgTest" );
232  newlog << MSG::VERBOSE << "This should be printed if threshold is VERBOSE" << endmsg;
233  newlog << MSG::DEBUG << "This should be printed if threshold is DEBUG" << endmsg;
234  newlog << MSG::INFO << "This should be printed if threshold is INFO" << endmsg;
235  newlog << MSG::WARNING << "This should be printed if threshold is WARNING" << endmsg;
236  newlog << MSG::ERROR << "This should be printed if threshold is ERROR" << endmsg;
237  newlog << MSG::FATAL << "This should be printed if threshold is FATAL" << endmsg;
238  newlog << MSG::ALWAYS << "This should be printed ALWAYS" << endmsg;
239 
240  // Testing access to the JobOptions catalogue
241  auto& opts = serviceLocator()->getOptsSvc();
242 
243  // Dump of the catalogue
244  info() << "=================================================" << endmsg;
246  auto opt_items = opts.items(); // this copy is just to hide differences between gcc and clang outputs
247  ostream_joiner( info() << "Dump of the property catalogue:\n", opt_items, '\n',
248  []( MsgStream& os, const auto& item ) -> MsgStream& {
249  return os << std::get<0>( item ) << ": " << std::get<1>( item );
250  } )
251  << endmsg;
252  info() << "=================================================" << endmsg;
253 
254  // Change an option of my own....
255  opts.set( name() + '.' + "PInt", "154" );
256  info() << "PInt= " << p_int << " [should be 154]" << endmsg;
257 
258  auto orig_policy =
260  try {
261  info() << "Try to assign invalid value to DoubleArray" << endmsg;
262  opts.set( name() + '.' + "DoubleArray", "{\"12.12\", \"13.13\"}" );
263  } catch ( const GaudiException& exc ) { error() << "got exception: " << exc.what() << endmsg; }
264 
265  try {
266  info() << "Try to assign invalid value to StringMap" << endmsg;
267  opts.set( name() + '.' + "StringMap", "{\"one\", {\"une\", \"eins\"}}" );
268  } catch ( const GaudiException& exc ) { error() << "got exception: " << exc.what() << endmsg; }
269 
271 
272  info() << "DoubleArray = " << m_doublearray.value() << endmsg;
273  info() << "=================================================" << endmsg;
274  return StatusCode::SUCCESS;
275 }
276 
277 //------------------------------------------------------------------------------
279  //------------------------------------------------------------------------------
280  info() << "executing...." << endmsg;
281  return StatusCode::SUCCESS;
282 }
283 
284 //------------------------------------------------------------------------------
286  //------------------------------------------------------------------------------
287  info() << "finalizing...." << endmsg;
288  return StatusCode::SUCCESS;
289 }
290 
291 // Static Factory declaration
MSG::DEBUG
@ DEBUG
Definition: IMessageSvc.h:25
Gaudi::Details::PropertyBase
PropertyBase base class allowing PropertyBase* collections to be "homogeneous".
Definition: PropertyBase.h:35
PropertyAlg::m_doublearray
Gaudi::Property< std::vector< double > > m_doublearray
Definition: PropertyAlg.h:55
std::make_tuple
T make_tuple(T... args)
PropertyAlg::m_emptyarray
Gaudi::Property< std::vector< double > > m_emptyarray
Definition: PropertyAlg.h:58
PropertyAlg::initialize
StatusCode initialize() override
Three mandatory member functions of any algorithm.
Definition: PropertyAlg.cpp:94
PropertyAlg.h
PropertyAlg::m_stringarray
Gaudi::Property< std::vector< std::string > > m_stringarray
Definition: PropertyAlg.h:56
std::string
STL class.
PropertyAlg::p_stringarray
Gaudi::Property< std::vector< std::string > > p_stringarray
Definition: PropertyAlg.h:75
Gaudi::Algorithm::name
const std::string & name() const override
The identifying name of the algorithm object.
Definition: Algorithm.cpp:542
PropertyAlg::m_strmap
Gaudi::Property< std::map< std::string, std::string > > m_strmap
Definition: PropertyAlg.h:65
MSG::INFO
@ INFO
Definition: IMessageSvc.h:25
PropertyAlg::p_double
Gaudi::PropertyWithReadHandler< double > p_double
Definition: PropertyAlg.h:68
std::vector
STL class.
std::string::find
T find(T... args)
ISvcLocator
Definition: ISvcLocator.h:46
GaudiException
Definition: GaudiException.h:31
Algorithm
Alias for backward compatibility.
Definition: Algorithm.h:58
PropertyAlg::p_bool
Gaudi::Property< bool > p_bool
Definition: PropertyAlg.h:71
PropertyAlg::u_intpairarray
Gaudi::Property< std::vector< std::pair< int, int > > > u_intpairarray
Definition: PropertyAlg.h:62
Gaudi::Algorithm::serviceLocator
SmartIF< ISvcLocator > & serviceLocator() const override
The standard service locator.
Definition: Algorithm.cpp:586
MSG::WARNING
@ WARNING
Definition: IMessageSvc.h:25
PropertyAlg::PropertyAlg
PropertyAlg(const std::string &name, ISvcLocator *pSvcLocator)
Constructor of this form must be provided.
Definition: PropertyAlg.cpp:50
gaudirun.c
c
Definition: gaudirun.py:525
PropertyAlg::readHandler
void readHandler(Gaudi::Details::PropertyBase &)
Callbacks for properties.
Definition: PropertyAlg.cpp:30
PropertyAlg::p_doublearray
Gaudi::Property< std::vector< double > > p_doublearray
Definition: PropertyAlg.h:74
PropertyAlg::m_int64array
Gaudi::Property< std::vector< long long > > m_int64array
Definition: PropertyAlg.h:53
IDataProviderSvc.h
IProperty
Definition: IProperty.h:33
PropertyAlg::p_string
Gaudi::Property< std::string > p_string
Definition: PropertyAlg.h:70
SmartIF.h
PropertyAlg::m_uint64
Gaudi::Property< unsigned long long > m_uint64
Definition: PropertyAlg.h:47
PropertyAlg::p_boolarray
Gaudi::Property< std::vector< bool > > p_boolarray
Definition: PropertyAlg.h:76
TimingHistograms.name
name
Definition: TimingHistograms.py:25
StatusCode
Definition: StatusCode.h:65
gaudirun.opts
opts
Definition: gaudirun.py:336
PropertyHolder< CommonMessaging< implements< IAlgorithm, IDataHandleHolder, IProperty, IStateful > > >::setPropertyRepr
StatusCode setPropertyRepr(const std::string &n, const std::string &r) override
set the property from name and value string representation
Definition: PropertyHolder.h:177
Gaudi::Property::value
const ValueType & value() const
Backward compatibility (.
Definition: Property.h:240
PropertyAlg::m_intarray
Gaudi::Property< std::vector< int > > m_intarray
Definition: PropertyAlg.h:52
format
GAUDI_API std::string format(const char *,...)
MsgStream format utility "a la sprintf(...)".
Definition: MsgStream.cpp:119
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:203
MsgStream
Definition: MsgStream.h:34
PropertyAlg::u_doublearrayunits
Gaudi::Property< std::vector< double > > u_doublearrayunits
Definition: PropertyAlg.h:59
MSG::FATAL
@ FATAL
Definition: IMessageSvc.h:25
Gaudi::Details::Property::setParsingErrorPolicy
ParsingErrorPolicy setParsingErrorPolicy(ParsingErrorPolicy p)
Definition: Property.cpp:523
PropertyAlg::u_doublepairarray
Gaudi::Property< std::vector< std::pair< double, double > > > u_doublepairarray
Definition: PropertyAlg.h:63
PropertyAlg::m_string
Gaudi::Property< std::string > m_string
Definition: PropertyAlg.h:49
SmartIF::as
SmartIF< IFace > as() const
return a new SmartIF instance to another interface
Definition: SmartIF.h:117
std::ostringstream
STL class.
PropertyAlg::p_int
Gaudi::CheckedProperty< int > p_int
Definition: PropertyAlg.h:67
PropertyAlg::m_uint64array
Gaudi::Property< std::vector< unsigned long long > > m_uint64array
Definition: PropertyAlg.h:54
MSG::VERBOSE
@ VERBOSE
Definition: IMessageSvc.h:25
StatusCode::SUCCESS
constexpr static const auto SUCCESS
Definition: StatusCode.h:100
DataObject.h
IChronoStatSvc.h
PropertyAlg::execute
StatusCode execute() override
Definition: PropertyAlg.cpp:278
MSG::ALWAYS
@ ALWAYS
Definition: IMessageSvc.h:25
PropertyAlg::m_bool
Gaudi::Property< bool > m_bool
Definition: PropertyAlg.h:50
DECLARE_COMPONENT
#define DECLARE_COMPONENT(type)
Definition: PluginServiceV1.h:46
System::cmdLineArgs
GAUDI_API const std::vector< std::string > cmdLineArgs()
Command line arguments including executable name as arg[0] as vector of strings.
Definition: System.cpp:354
PropertyAlg::updateHandler
void updateHandler(Gaudi::Details::PropertyBase &)
Definition: PropertyAlg.cpp:40
MSG::ERROR
@ ERROR
Definition: IMessageSvc.h:25
PropertyAlg::m_boolarray
Gaudi::Property< std::vector< bool > > m_boolarray
Definition: PropertyAlg.h:57
PropertyAlg::m_double
Gaudi::Property< double > m_double
Definition: PropertyAlg.h:48
PropertyAlg::p_intarray
Gaudi::Property< std::vector< int > > p_intarray
Definition: PropertyAlg.h:73
std::ostringstream::str
T str(T... args)
SerializeSTL.h
AsyncIncidents.msgSvc
msgSvc
Definition: AsyncIncidents.py:34
StatusCode::FAILURE
constexpr static const auto FAILURE
Definition: StatusCode.h:101
PropertyAlg::finalize
StatusCode finalize() override
Definition: PropertyAlg.cpp:285
PropertyAlg
Definition: PropertyAlg.h:28
Gaudi::Details::Property::ParsingErrorPolicy::Exception
@ Exception
PropertyAlg::m_int64
Gaudi::Property< long long > m_int64
Definition: PropertyAlg.h:46
PropertyAlg::m_int
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
PropertyAlg::u_doublearray
Gaudi::Property< std::vector< double > > u_doublearray
Definition: PropertyAlg.h:60
GaudiException::what
const char * what() const override
method from std::exception
Definition: GaudiException.h:110
MsgStream.h
GaudiUtils::details::ostream_joiner
Stream & ostream_joiner(Stream &os, Iterator first, Iterator last, Separator sep, OutputElement output=OutputElement{})
Definition: SerializeSTL.h:73