ATLAS Offline Software
AnaToolHandle.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 
7 
8 
9 #ifndef ASG_TOOLS__ANA_TOOL_HANDLE_H
10 #define ASG_TOOLS__ANA_TOOL_HANDLE_H
11 
12 // Author: Will Buttinger
13 
14 
15 #include <AsgTools/AsgTool.h>
16 #include <AsgTools/AsgToolConfig.h>
17 #include <AsgTools/ToolHandle.h>
18 #include <type_traits>
19 #include <atomic>
20 #include <list>
21 #include <map>
22 #include <mutex>
23 
24 namespace asg
25 {
26 #ifdef XAOD_STANDALONE
27  typedef INamedInterface parentType_t;
28  typedef IAsgTool interfaceType_t;
29 #else
30  typedef INamedInterface parentType_t;
31  typedef IAlgTool interfaceType_t;
32 #endif
33 
34  template<class T> class AnaToolHandle;
35 
36  namespace detail
37  {
38  class AnaToolShare;
39 
40 
41 
44 
45  enum class AnaToolHandleMode
46  {
52  EMPTY,
53 
56 
59 
62 
67  USER
68  };
69  }
70 
71 
72 
78  template <typename T>
79  std::ostream& operator << (std::ostream& str, const AnaToolHandle<T>& obj);
80 
81 
164 
165  template<class T>
166  class AnaToolHandle final
167  {
168  //
169  // public interface
170  //
171 
175  public:
176  void testInvariant () const;
177 
178 
189  public:
190  explicit AnaToolHandle (const std::string& val_name = "",
191  parentType_t *val_parent = nullptr);
192 
193 
197  public:
199 
200 
211  public:
213 
214 
218  public:
219  ~AnaToolHandle () noexcept;
220 
221 
232  public:
233  AnaToolHandle& operator = (const AnaToolHandle<T>& that);
234 
235 
239  public:
240  AnaToolHandle<T>& operator = (AnaToolHandle<T>&& that);
241 
242 
246  public:
247  void swap (AnaToolHandle<T>& that) noexcept;
248 
249 
258  public:
259  bool empty () const;
260 
264  public:
265  bool isPublic () const noexcept;
266 
271  public:
272  bool isInitialized () const noexcept;
273 
274 
282  public:
283  template<typename T2> void
284  declarePropertyFor (T2 *tool, const std::string& name,
285  const std::string& description = "");
286 
290  public:
291  const ToolHandle<T>& getHandle () const noexcept;
292 
293 
311  public:
312  // ToolHandle and ToolHandleArray have ctors that can accept a single int,
313  // so use enable_if to avoid ambiguities.
314  template <class T2> StatusCode
315  setProperty (const std::string& property, const T2& value);
316  template <class T2>
317  typename std::enable_if<std::is_base_of_v<parentType_t, T2>, StatusCode>::type
318  setProperty (const std::string& property, const ToolHandle<T2>& value);
319  template <class T2> StatusCode
320  setProperty (const std::string& property, const AnaToolHandle<T2>& value);
321  template <class T2>
322  typename std::enable_if<std::is_base_of_v<parentType_t, T2>, StatusCode>::type
323  setProperty (const std::string& property, const ToolHandleArray<T2>& value);
325 
326 
330  public:
331  const std::string& type () const noexcept;
332 
337  public:
338  void setType (std::string val_type) noexcept;
339 
340 
344  public:
345  const std::string& name () const noexcept;
346 
351  public:
352  void setName (std::string val_name) noexcept;
353 
360  public:
361  std::string fullName () const;
362 
363 
369  public:
370  std::string typeAndName () const;
371 
378  public:
379  void setTypeAndName (const std::string& val_typeAndName);
380 
385  public:
386  void setTypeAndName (std::string val_type, std::string val_name) noexcept;
387 
388 
395  public:
397 
399  public:
401 
402 
409  public:
410  T *operator -> ();
411 
418  public:
419  const T *operator -> () const;
420 
426  public:
427  T& operator * ();
428 
434  public:
435  const T& operator * () const;
436 
443  public:
444  T *get ();
445 
452  public:
453  const T *get () const;
454 
455 
464  public:
465  bool isUserConfigured () const noexcept;
466 
467 
477  public:
479 
480 
490  public:
492 
493 
503  public:
504  bool allowEmpty () const noexcept;
505 
506 
507 
512  public:
513  void setAllowEmpty (bool val_allowEmpty = true) noexcept;
514 
515 
516 
517  //
518  // deprecated interface
519  //
520 
534  public:
536 
537  public:
538  [[deprecated("please use isInitialized() instead")]]
539  bool inPremakeState () const noexcept {
540  return !isInitialized();}
541 
542  public:
543  [[deprecated("please use isInitialized() instead")]]
544  bool inBrokenState () const noexcept {
545  return false;};
546 
547  public:
548  [[deprecated("no longer need to call make()")]]
550  return StatusCode::SUCCESS;};
551 
552  public:
553  [[deprecated("please use setType() or setTypeAndName() instead")]]
554  StatusCode make (const std::string& val_type) noexcept {
555  if (!val_type.empty()) {
556  if (val_type.find ('/') != std::string::npos)
557  setTypeAndName (val_type);
558  else
559  setType (val_type); }
560  return StatusCode::SUCCESS; };
561 
562 
563 
564  //
565  // private interface
566  //
567 
576  private:
577  std::vector<std::function<StatusCode ()>> m_extraInit;
578 
582  private:
583  std::shared_ptr<void> m_cleanup;
584 
586  private:
588 
590  private:
591  std::string m_name;
592 
594  private:
596 
597 
608  private:
609  std::shared_ptr<ToolHandle<T>> m_handleUser;
610 
615  private:
617 
619  private:
620  std::atomic<bool> m_isInitialized {false};
621 
631  private:
632  T *m_toolPtr = nullptr;
633 
639  private:
641 
654  private:
656  getMode (std::shared_ptr<detail::AnaToolShare>& sharedTool) const;
657 
659  private:
661  getMode () const;
662 
663 
665  private:
666  bool m_allowEmpty = false;
667 
668 
674  private:
676  (T*& toolPtr, ToolHandle<T>& toolHandle) const;
677 
678 
684  private:
685  std::recursive_mutex m_initializeMutex;
686  };
687 }
688 
690 #define ASG_MAKE_ANA_TOOL(handle,type) \
691  (ASG_SET_ANA_TOOL_TYPE(handle,type), StatusCode (StatusCode::SUCCESS))
692 
694 #define ASG_SET_ANA_TOOL_TYPE(handle,type) \
695  (handle).setType (#type)
696 
698 
699 
700 #endif
asg::AnaToolHandle::initialize
StatusCode initialize()
initialize the tool
asg::AnaToolHandle::m_cleanup
std::shared_ptr< void > m_cleanup
any stuff we need to release as part of cleanup
Definition: AnaToolHandle.h:583
AnaToolHandle.icc
asg::AnaToolHandle
a modified tool handle that allows its owner to configure new tools from the C++ side
Definition: AnaToolHandle.h:34
asg::AnaToolHandle::setAllowEmpty
void setAllowEmpty(bool val_allowEmpty=true) noexcept
set the value of allowEmpty
asg::AnaToolHandle::isUserConfigured
bool isUserConfigured() const noexcept
whether this tool handle has been configured by the user
asg::detail::AnaToolHandleMode::CREATE_PRIVATE
@ CREATE_PRIVATE
create a private tool normally
asg::operator<<
std::ostream & operator<<(std::ostream &str, const AnaToolHandle< T > &obj)
standard output operator
asg::AnaToolHandle::retrieve
StatusCode retrieve()
initialize the tool
asg::AnaToolHandle::setName
void setName(std::string val_name) noexcept
set the value of name
asg::interfaceType_t
IAlgTool interfaceType_t
Definition: AnaToolHandle.h:31
asg::AnaToolHandle::testInvariant
void testInvariant() const
test the invariant of this object
asg::AnaToolHandle::m_isInitialized
std::atomic< bool > m_isInitialized
the value of isInitialized
Definition: AnaToolHandle.h:620
asg::AnaToolHandle::setTypeAndName
void setTypeAndName(const std::string &val_typeAndName)
set the value of type and name
taskman.template
dictionary template
Definition: taskman.py:317
asg
Definition: DataHandleTestTool.h:28
asg::AnaToolHandle::make
StatusCode make(const std::string &val_type) noexcept
Definition: AnaToolHandle.h:554
asg::AnaToolHandle::m_name
std::string m_name
the value of name
Definition: AnaToolHandle.h:591
asg::AnaToolHandle::getMode
detail::AnaToolHandleMode getMode(std::shared_ptr< detail::AnaToolShare > &sharedTool) const
get the mode with which this ToolHandle will be initialized
athena.value
value
Definition: athena.py:124
detail
Definition: extract_histogram_tag.cxx:14
asg::AnaToolHandle::m_parentPtr
parentType_t * m_parentPtr
the pointer to the parent
Definition: AnaToolHandle.h:595
asg::AnaToolHandle::inBrokenState
bool inBrokenState() const noexcept
Definition: AnaToolHandle.h:544
asg::detail::AnaToolHandleMode::EMPTY
@ EMPTY
do not create a tool
asg::AnaToolHandle::AnaToolHandle
AnaToolHandle(AnaToolHandle< T > &&that)
move constructor
const
bool const RAWDATA *ch2 const
Definition: LArRodBlockPhysicsV0.cxx:560
asg::AnaToolHandle::setProperty
StatusCode setProperty(const std::string &property, const T2 &value)
set the given property of the tool.
asg::AnaToolHandle::m_extraInit
std::vector< std::function< StatusCode()> > m_extraInit
any extra initialization we need to do before creating the tool
Definition: AnaToolHandle.h:560
asg::AnaToolHandle::declarePropertyFor
void declarePropertyFor(T2 *tool, const std::string &name, const std::string &description="")
declare as property on the given tool
config
Definition: PhysicsAnalysis/AnalysisCommon/AssociationUtils/python/config.py:1
asg::parentType_t
INamedInterface parentType_t
Definition: AnaToolHandle.h:30
asg::AnaToolHandle::m_toolPtr
T * m_toolPtr
the pointer to the tool we use
Definition: AnaToolHandle.h:632
asg::AnaToolHandle::m_mode
detail::AnaToolHandleMode m_mode
the value of getMode cached when we initialize the tool
Definition: AnaToolHandle.h:640
asg::AnaToolHandle::fullName
std::string fullName() const
the full name of the tool to be used during tool initialization
asg::AnaToolHandle::type
const std::string & type() const noexcept
the type configured for this AnaToolHandle
asg::AsgToolConfig
an object that can create a AsgTool
Definition: AsgToolConfig.h:22
asg::AnaToolHandle::m_allowEmpty
bool m_allowEmpty
the value of allowEmpty
Definition: AnaToolHandle.h:666
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
AsgToolConfig.h
asg::AnaToolHandle::m_initializeMutex
std::recursive_mutex m_initializeMutex
a mutex to ensure that we don't call initialize twice concurrently
Definition: AnaToolHandle.h:685
asg::AnaToolHandle::~AnaToolHandle
~AnaToolHandle() noexcept
standard destructor
asg::detail::AnaToolHandleMode::CREATE_SHARED
@ CREATE_SHARED
create a shared tool normally
asg::detail::AnaToolHandleMode::USER
@ USER
retrieve a tool from the user tool handle
asg::AnaToolHandle::isConfigurable
bool isConfigurable() const
whether the tool can be configured by us, i.e.
asg::detail::AnaToolHandleMode::RETRIEVE_SHARED
@ RETRIEVE_SHARED
retrieve a shared tool
asg::AnaToolHandle::name
const std::string & name() const noexcept
the name configured for this AnaToolHandle
asg::AnaToolHandle::getMode
detail::AnaToolHandleMode getMode() const
get the mode with which this ToolHandle will be initialized
AtlCoolConsole.tool
tool
Definition: AtlCoolConsole.py:453
asg::AnaToolHandle::make
StatusCode make()
Definition: AnaToolHandle.h:549
asg::AnaToolHandle::isPublic
bool isPublic() const noexcept
whether this is a public tool (or tool handle)
asg::AnaToolHandle::isInitialized
bool isInitialized() const noexcept
whether initialize has been called successfully, i.e.
python.Dumpers.typename
def typename(t)
Definition: Dumpers.py:194
asg::AnaToolHandle::m_originalTypeAndName
std::string m_originalTypeAndName
the typeAndName at time of creation
Definition: AnaToolHandle.h:616
runJobs.deprecated
deprecated
Definition: runJobs.py:191
asg::AnaToolHandle::getHandle
const ToolHandle< T > & getHandle() const noexcept
the tool handle we wrap
asg::AnaToolHandle::inPremakeState
bool inPremakeState() const noexcept
Definition: AnaToolHandle.h:539
asg::AnaToolHandle::typeAndName
std::string typeAndName() const
the value of type and name
asg::AnaToolHandle::swap
void swap(AnaToolHandle< T > &that) noexcept
standard swap
asg::AnaToolHandle::m_config
AsgToolConfig m_config
the configuration for this tool
Definition: AnaToolHandle.h:587
asg::AnaToolHandle::setType
void setType(std::string val_type) noexcept
set the value of type
asg::AnaToolHandle::AnaToolHandle
AnaToolHandle(const std::string &val_name="", parentType_t *val_parent=nullptr)
create a tool handle with the given name and parent
str
Definition: BTagTrackIpAccessor.cxx:11
asg::AnaToolHandle::AnaToolHandle
AnaToolHandle(const AnaToolHandle< T > &that)
copy constructor
ToolHandle.h
asg::AnaToolHandle::m_handleUser
std::shared_ptr< ToolHandle< T > > m_handleUser
the ToolHandle exposed to the user
Definition: AnaToolHandle.h:609
asg::AnaToolHandle::makeToolRetrieve
StatusCode makeToolRetrieve(T *&toolPtr, ToolHandle< T > &toolHandle) const
make a tool by retrieving the ToolHandle
AsgTool.h
asg::detail::AnaToolHandleMode
AnaToolHandleMode
the mode with which an AnaToolHandle object is initialized
Definition: AnaToolHandle.h:46
python.PyAthena.obj
obj
Definition: PyAthena.py:132
asg::AnaToolHandle::allowEmpty
bool allowEmpty() const noexcept
whether the user is allowed to configure this with an empty tool handle
asg::AnaToolHandle::empty
bool empty() const
whether this ToolHandle is completely empty, i.e.
asg::AnaToolHandle::mode
detail::AnaToolHandleMode mode() const
the detail::AnaToolHandleMode for this handle
asg::AnaToolHandle::get
T * get()
access the tool
description
std::string description
glabal timer - how long have I taken so far?
Definition: hcg.cxx:88