ATLAS Offline Software
Loading...
Searching...
No Matches
ToolWithConstants.h
Go to the documentation of this file.
1// This file's extension implies that it's C, but it's really -*- C++ -*-.
2/*
3 * Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration.
4 */
17
18#ifndef CALOUTILS_TOOLWITHCONSTANTS_H
19#define CALOUTILS_TOOLWITHCONSTANTS_H
20
21
29#include "Gaudi/Property.h"
30#include <string>
31#include <sstream>
32#include <type_traits>
33
34
35namespace Gaudi {
36namespace Parsers {
37
38
39// Unused, because we override fromString() below, but required
40// in order to compile the overridden fromString() from Gaudi.
41template <unsigned int N>
42StatusCode parse (CaloRec::Array<N>&, const std::string&)
43{
44 return StatusCode::FAILURE;
45}
46
47
48}} // namespace Gaudi::Parsers
49
50
51namespace CaloUtils {
52
53
60{
61public:
62 const EventContext& ctx() const { return m_ctx; }
63
65 const EventContext& ctx)
67 m_ctx (ctx)
68 {
69 }
71 const EventContext& m_ctx;
72};
73
74
75
80{
81public:
83 virtual ~IToolConstant() = default;
84
86 virtual std::string name() const = 0;
87
89 virtual bool setFromJO() const = 0;
90};
91
92
93//***********************************************************************
94
95
102{
103public:
105
106
113 ToolWithConstantsImpl (const std::string& toolName,
114 const std::string& prefix,
116
117
121 StatusCode initialize();
122
123
129 Context context (const EventContext& ctx) const;
130
131
136 void addConstant (IToolConstant* c);
137
138
145 void writeConstants (std::ostream& stream,
146 const std::string& name,
147 const EventContext& ctx) const;
148
149
158 StatusCode
159 mergeConstants (const std::string& typeName,
160 int toolVersion,
161 Context& myctx,
162 bool isDummy,
163 CaloRec::ToolConstants& out) const;
164
165
167 std::string m_toolName;
168
170 // Note: Held by a reference, not by value.
171 // This comes from a property of the owning tool, and is not set
172 // until we run initialize() on that tool.
173 const std::string& m_prefix;
174
177
180
182 std::vector<IToolConstant*> m_declaredConstants;
183};
184
185
186//***********************************************************************
187
188
194template <CxxUtils::FromArrayrep T>
195 // Must be able to initialize a T with fromArrayrep
196 // Doesn't actaully work like this, though, as there's a conflict
197 // with the friend declaration below.
199 : public IToolConstant
200{
201public:
204
205
212 template <class OWNER>
213 ToolConstant (OWNER* owner,
214 const std::string& name,
215 const std::string& doc = "");
216
217
228 template <class OWNER>
229 ToolConstant (OWNER* owner,
230 const std::string& name,
231 const char* doc);
232
233
243 template <class OWNER,
244 typename U = T,
245 typename = std::enable_if_t<std::is_arithmetic_v<U> > >
246 ToolConstant (OWNER* owner,
247 const std::string& name,
248 const T& deflt,
249 const std::string& doc = "");
250
251
256 T operator() (const Context& c) const;
257
258
264 T operator()() const;
265
266
270 virtual std::string name() const override;
271
272
276 virtual bool setFromJO() const override;
277
278
279private:
285 : public Gaudi::Property<T>
286 {
287 public:
294 CProperty (const std::string& name,
295 const std::string& doc,
297
298
306 CProperty (const std::string& name,
307 const std::string& doc,
309 const T& deflt);
310
311
312 // To be able to assign to this wrapper.
313 using Gaudi::Property<T>::operator=;
314
315
319 virtual std::string toString() const override;
320
321
326 virtual void toStream( std::ostream& out ) const override;
327
328
333 virtual StatusCode fromString( const std::string& value ) override;
334
335
338
340 bool m_setFromJO = false;
341
343 bool m_hasDefault = false;
344 };
345
346
349
352};
353
354
355//***************************************************************************
356
357
361template <class T>
363 requires (T& c, Gaudi::Property<int>& p)
364 {
365 { c.declareProperty(p) };
366 };
367
368
444template <HasProperties BASE>
446 : public extends<BASE, IToolWithConstants>
447{
448public:
451
452
454 using extends<BASE, IToolWithConstants>::extends;
455
456
459
460
462 template <CxxUtils::FromArrayrep T>
464
465
469 virtual StatusCode initialize() override;
470
471
477 Context context (const EventContext& ctx) const;
478
479
486 virtual void writeConstants (std::ostream& stream,
487 const std::string& name,
488 const EventContext& ctx) const;
489
490
496 virtual StatusCode mergeConstants (CaloRec::ToolConstants& out,
497 const EventContext& ctx) const override;
498
499
512 virtual int toolVersion() const;
513
514
525 virtual const std::string& toolType() const;
526
527
528private:
529 // Make this a friend so it can access m_impl.
530 template <CxxUtils::FromArrayrep T>
532
535 { this, "DBHandleKey", "", "" };
536
538 StringProperty m_prefix
539 { this, "prefix", "", "" };
540
543
548 { this, "order", 0 };
549
561 {this, "isDummy", false };
562};
563
564
565} // namespace CaloUtils
566
567
569
570
571#undef ATH_TWC_REQUIRES
572
573
574#endif // not CALOUTILS_TOOLWITHCONSTANTS_H
Exceptions that can be thrown from CaloUtils.
Helpers for checking error return status codes and reporting errors.
Abstact interface for ToolWithConstants.
Container for the tool constants managed by ToolWithConstants.
Container for the tool constants managed by ToolWithConstants.
Internal interface for dealing with a collection of ToolConstants.
virtual bool setFromJO() const =0
True if this constant was initialized from job options.
virtual ~IToolConstant()=default
Virtual destructor.
virtual std::string name() const =0
The name of the constant.
Internal wrapper for Gaudi::Property.
bool m_hasDefault
Does this constant have a default?
CProperty(const std::string &name, const std::string &doc, ToolWithConstantsImpl &impl, const T &deflt)
Constructor, with default value.
virtual void toStream(std::ostream &out) const override
Print the value of this property to a stream.
bool m_setFromJO
Has this constant been set from JO?
ToolWithConstantsImpl & m_impl
Reference to implementation object.
CProperty(const std::string &name, const std::string &doc, ToolWithConstantsImpl &impl)
Constructor, no default value.
virtual std::string toString() const override
Return the value of this property as a string.
virtual StatusCode fromString(const std::string &value) override
Initialize this propery's value from a string.
Constant of a ToolWithConstants.
ToolConstant(OWNER *owner, const std::string &name, const char *doc)
Declare a constant with no default.
T operator()() const
Retrieve the value of a constant.
CProperty m_prop
Internal Property instance, for getting JO settings from Gaudi.
ToolConstantsContext Context
Alias for the context object.
virtual bool setFromJO() const override
Was this constant set through job options?
virtual std::string name() const override
Return the name of this constant.
ToolConstant(OWNER *owner, const std::string &name, const T &deflt, const std::string &doc="")
Declare a constant with a default.
ToolConstant(OWNER *owner, const std::string &name, const std::string &doc="")
Declare a constant with no default.
int m_toolVersion
Version of the owning tool.
Internal implementation object.
void writeConstants(std::ostream &stream, const std::string &name, const EventContext &ctx) const
Dump method (for debugging)
Context context(const EventContext &ctx) const
Create a Context object.
CaloRec::ToolConstants m_constants
For settings made from JO.
std::vector< IToolConstant * > m_declaredConstants
All constants declared to this tool.
void addConstant(IToolConstant *c)
Add a new constant to the tool.
ToolWithConstantsImpl(const std::string &toolName, const std::string &prefix, SG::ReadCondHandleKey< CaloRec::ToolConstants > &DBHandle)
Constructor.
StatusCode mergeConstants(const std::string &typeName, int toolVersion, Context &myctx, bool isDummy, CaloRec::ToolConstants &out) const
Merge our constants into out with the proper prefix.
std::string m_toolName
Name of the owning tool.
const std::string & m_prefix
The prefix of this constant in COOL.
SG::ReadCondHandleKey< CaloRec::ToolConstants > & m_DBHandle
Handle to a ToolConstants conditions object.
Hold constants for a tool.
ToolConstant< T > Constant
Alias for the Constant type.
friend class CaloUtils::ToolConstant
Context context(const EventContext &ctx) const
Create a Context object.
virtual const std::string & toolType() const
Return the name of the type of this tool.
ToolWithConstantsImpl m_impl
Internal implementation object.
virtual int toolVersion() const
Return the version number for this tool.
SG::ReadCondHandleKey< CaloRec::ToolConstants > m_DBHandle
Handle to a ToolConstants conditions object.
ToolWithConstants base_class
Shorthand for derived classes.
CaloUtils::ToolConstantsContext Context
Convenient alias for the Context type.
virtual StatusCode initialize() override
Initialize method.
virtual void writeConstants(std::ostream &stream, const std::string &name, const EventContext &ctx) const
Dump method (for debugging)
Constant< bool > m_isdummy
If true, then this is a dummy tool that should not be executed.
virtual StatusCode mergeConstants(CaloRec::ToolConstants &out, const EventContext &ctx) const override
Merge our constants into out with the proper prefix.
StringProperty m_prefix
Prefix for finding our constants within the ToolConstants object.
Constant< int > m_order
Used to fix the ordering of tools when we're initializing from COOL based on a hierarchical tag.
Read-only multidimensional array.
Abstact interface for ToolWithConstants.
Concept to test if a class has Gaudi properties.
StatusCode parse(std::tuple< Tup... > &tup, const Gaudi::Parsers::InputData &input)
=============================================================================
Context object for retrieving ToolConstant values.
const CaloRec::ToolConstants * m_constants
const EventContext & ctx() const
ToolConstantsContext(const CaloRec::ToolConstants *constants, const EventContext &ctx)