2 * Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration.
5 * @file CaloUtils/ToolWithConstants.icc
6 * @author scott snyder <snyder@bnl.gov>
8 * @brief Hold constants for a tool.
16 * @brief Declare a constant with no default.
17 * @param owner The owning @c ToolWithConstants.
18 * @param name Name of the constant.
19 * @param doc Documentation string.
22 template <class OWNER>
23 ToolConstant<T>::ToolConstant (OWNER* owner,
24 const std::string& name,
25 const std::string& doc /*= ""*/)
26 : m_prop (name, doc, owner->m_impl),
27 m_toolVersion (owner->toolVersion())
29 owner->declareProperty (m_prop);
30 owner->m_impl.addConstant (this);
31 m_prop.template setOwnerType<OWNER>();
36 * @brief Declare a constant with no default.
37 * @param owner The owning @c ToolWithConstants.
38 * @param name Name of the constant.
39 * @param doc Documentation string.
41 * (Needed in addition to the previous signature in order to avoid ambiguities;
42 * otherwise, if a char* is given for the third argument, it would
43 * match the following signature instead of the previous one.)
46 template <class OWNER>
47 ToolConstant<T>::ToolConstant (OWNER* owner,
48 const std::string& name,
50 : ToolConstant (owner, name, std::string(doc))
56 * @brief Declare a constant with a default.
57 * @param owner The owning @c ToolWithConstants.
58 * @param name Name of the constant.
59 * @param deflt Default value.
60 * @param doc Documentation string.
62 * Only possible for arithmetic types, not @c Array<N>.
65 template <class OWNER,
67 typename /*= std::enable_if_t<std::is_arithmetic_v<U> >*/ >
68 ToolConstant<T>::ToolConstant (OWNER* owner,
69 const std::string& name,
71 const std::string& doc /*= ""*/)
72 : m_prop (name, doc, owner->m_impl, deflt),
73 m_toolVersion (owner->toolVersion())
75 owner->declareProperty (m_prop);
76 owner->m_impl.addConstant (this);
77 m_prop.template setOwnerType<OWNER>();
82 * @brief Retrieve the value of a constant.
83 * @param c Context for accessing condtions (as returned from context()).
86 T ToolConstant<T>::operator() (const Context& c) const
88 // If it's been set from JO, just return the value.
89 if (m_prop.m_setFromJO) {
90 return static_cast<T> (m_prop);
93 // Check for a setting from COOL.
96 if (c.m_constants->version() > m_toolVersion) {
97 throw ExcBadToolConstantVersion (m_prop.m_impl.m_toolName,
100 c.m_constants->version());
103 // Convert form the Arrayrep stored in COOL.
104 const CxxUtils::Arrayrep& rep =
105 c.m_constants->getrep (m_prop.m_impl.m_toolName,
106 m_prop.m_impl.m_prefix + m_prop.name());
109 CxxUtils::fromArrayrep (rep, ret);
113 // If we have a default value, return it.
114 if (m_prop.m_hasDefault) {
115 return static_cast<T> (m_prop);
118 // Otherwise it's an error.
119 throw CaloUtils::ExcConstantNotSet (m_prop.m_impl.m_toolName, m_prop.name());
124 * @brief Retrieve the value of a constant.
126 * This variant may only be used if the constant was initialized via JO.
130 T ToolConstant<T>::operator()() const
132 if (!m_prop.m_setFromJO) {
133 CaloUtils::throwExcBadContextlessRetrieve (m_prop.m_impl.m_toolName, m_prop.name());
135 return static_cast<T> (m_prop);
140 * @brief Return the name of this constant.
143 std::string ToolConstant<T>::name() const
145 return m_prop.name();
150 * @brief Was this constant set through job options?
153 bool ToolConstant<T>::setFromJO() const
155 return m_prop.m_setFromJO;
160 * @brief Constructor, no default value.
161 * @param name Constant name.
162 * @param doc Documentation string.
163 * @param impl Internal implementation object.
166 ToolConstant<T>::CProperty::CProperty (const std::string& name,
167 const std::string& doc,
168 ToolWithConstantsImpl& impl)
169 : Gaudi::Property<T> (name, T(), doc),
177 * @brief Constructor, with default value.
178 * @param name Constant name.
179 * @param doc Documentation string.
180 * @param impl Internal implementation object.
181 * @param deflt Default value.
184 ToolConstant<T>::CProperty::CProperty (const std::string& name,
185 const std::string& doc,
186 ToolWithConstantsImpl& impl,
188 : Gaudi::Property<T> (name, deflt, doc),
192 // Install the default value in our internal constats representation.
193 CaloRec::Arrayrep rep;
194 rep.m_data.push_back (deflt);
195 m_impl.m_constants.setrep (this->name(), std::move (rep));
200 * @brief Return the value of this property as a string.
203 std::string ToolConstant<T>::CProperty::toString() const
205 // Do this in terms of toStream().
206 std::ostringstream ss;
213 * @brief Print the value of this property to a stream.
214 * @param out Stream to which to print.
217 void ToolConstant<T>::CProperty::toStream( std::ostream& out ) const
219 out << static_cast<T> (*this);
224 * @brief Initialize this property's value from a string.
225 * @param value String from which to initialize.
228 StatusCode ToolConstant<T>::CProperty::fromString( const std::string& value )
230 // A given constant should not be set from JO more than once.
232 throw ExcConstantReset (m_impl.m_toolName, this->name());
235 // Convert from string and save as the value of the property.
236 CaloRec::Arrayrep rep (value, m_impl.m_toolName);
237 m_impl.m_constants.setrep (this->name(), rep);
239 CxxUtils::fromArrayrep (m_impl.m_constants.getrep (m_impl.m_toolName,
243 // Remember that this property was set from JO.
245 return StatusCode::SUCCESS;
249 //***************************************************************************
254 * @brief Initialize method. Derived classes must call this.
256 template <class BASE>
258 StatusCode ToolWithConstants<BASE>::initialize()
260 ATH_CHECK( m_impl.initialize() );
261 ATH_CHECK( BASE::initialize() );
262 return StatusCode::SUCCESS;
267 * @brief Create a @c Context object.
269 * This can then be passed to @c Constant::operator().
271 template <class BASE>
274 typename ToolWithConstants<BASE>::Context
275 ToolWithConstants<BASE>::context (const EventContext& ctx) const
277 return m_impl.context (ctx);
282 * @brief Dump method (for debugging)
283 * @param stream Ostream to which to write.
284 * @param name Name to go in output
285 * @param ctx Event context.
287 template <class BASE>
289 void ToolWithConstants<BASE>::writeConstants (std::ostream& stream,
290 const std::string& name,
291 const EventContext& ctx) const
293 m_impl.writeConstants (stream, name, ctx);
298 * @brief Merge our constants into @c out with the proper prefix.
299 * @param[out] out Object to receive our constants.
300 * @param ctx Event context.
302 template <class BASE>
305 ToolWithConstants<BASE>::mergeConstants (CaloRec::ToolConstants& out,
306 const EventContext& ctx) const
308 Context myctx = context (ctx);
309 return m_impl.mergeConstants (toolType(),
318 * @brief Return the version number for this tool.
320 * A saved set of constants includes both the C++ class name and
321 * a version number. The idea is that the version number can
322 * be bumped whenever there's a backwards-incompatible change;
323 * this gives some protection against trying to use an old version
324 * of a tool with an incompatible newer set of constants.
326 * If you want a tool to have a version number, override this method.
327 * Otherwise, it will default to a version number of 0.
329 template <class BASE>
331 int ToolWithConstants<BASE>::toolVersion() const
338 * @brief Return the name of the type of this tool.
340 * A saved set of constants includes both the C++ class name and
341 * a version number. Normally, the class name is taken from the
342 * Gaudi type() method, but that may be changed by overriding
343 * this method. This can be used, for example, when there are
344 * tools with distinct C++ classes but which are yet similar enough
345 * to combine together.
347 template <class BASE>
349 const std::string& ToolWithConstants<BASE>::toolType() const
355 } // namespace CaloUtils