ATLAS Offline Software
Loading...
Searching...
No Matches
columnar::PythonToolHandle Class Referencefinal

a handle to a python tool for use via nanobind More...

#include <PythonToolHandle.h>

Inheritance diagram for columnar::PythonToolHandle:
Collaboration diagram for columnar::PythonToolHandle:

Public Member Functions

 PythonToolHandle ()
 standard constructor
void setTypeAndName (const std::string &typeAndName)
 set the type and name for the tool
template<typename T>
void setProperty (const std::string &key, T &&value)
 set a property on the tool
const asg::AsgToolConfiggetConfig () const
 get the AsgToolConfig
void preinitialize ()
 preinitialize the tool
void renameContainers (const std::vector< std::pair< std::string, std::string > > &renames)
 rename the columns the tool uses
void initialize ()
 initialize the tool
void applySystematicVariation (const std::string &sysName)
 set the tool to apply the given systematic variation
template<typename CT>
void setColumn (const std::string &key, std::size_t size, CT *dataPtr)
 set a column pointer (raw pointer version)
void setColumnVoid (const std::string &name, std::size_t size, const void *dataPtr, const std::type_info &type, bool isConst)
 set a column pointer
void call ()
 call the tool and reset the columns
std::vector< ColumnInfogetColumnInfo () const
 get the expected column info
std::vector< std::string > getColumnNames () const
 get the expected column names
std::vector< std::string > getRecommendedSystematics () const
 get the recommended systematics
void setLevel (MSG::Level lvl)
 Change the current logging level.
Functions providing the same interface as AthMessaging
bool msgLvl (const MSG::Level lvl) const
 Test the output level of the object.
MsgStream & msg () const
 The standard message stream.
MsgStream & msg (const MSG::Level lvl) const
 The standard message stream.

Private Member Functions

void initMessaging () const
 Initialize our message level and MessageSvc.

Private Attributes

asg::AsgToolConfig m_config
ToolHandle< asg::AsgToolm_toolHandle
std::shared_ptr< void > m_cleanup
IColumnarToolm_tool = nullptr
CP::ISystematicsToolm_systTool = nullptr
std::shared_ptr< const ColumnVectorHeaderm_columnHeader
std::shared_ptr< const ToolColumnVectorMapm_toolWrapper
std::unique_ptr< ColumnVectorDatam_columns
std::string m_nm
 Message source name.
boost::thread_specific_ptr< MsgStream > m_msg_tls
 MsgStream instance (a std::cout like with print-out levels)
std::atomic< IMessageSvc * > m_imsg { nullptr }
 MessageSvc pointer.
std::atomic< MSG::Level > m_lvl { MSG::NIL }
 Current logging level.
std::atomic_flag m_initialized ATLAS_THREAD_SAFE = ATOMIC_FLAG_INIT
 Messaging initialized (initMessaging)

Detailed Description

a handle to a python tool for use via nanobind

This is meant to be used together with a nanobind python binding, to provide a higher level and stable interface to the tool wrappers. For other uses people should use the underlying interfaces directly or write a new higher level interface (depending on what's appropriate).

For now this is not thread-safe. The underlying code is thread-safe, as long as there is a separate ColumnarToolWrapperData object for each thread. Also, the way the tools handle setting systematics is currently not thread-safe.

Definition at line 39 of file PythonToolHandle.h.

Constructor & Destructor Documentation

◆ PythonToolHandle()

columnar::PythonToolHandle::PythonToolHandle ( )
inline

standard constructor

Public Members

Definition at line 47 of file PythonToolHandle.h.

47: asg::AsgMessaging("PythonToolHandle") {}

Member Function Documentation

◆ applySystematicVariation()

void columnar::PythonToolHandle::applySystematicVariation ( const std::string & sysName)
inline

set the tool to apply the given systematic variation

Definition at line 116 of file PythonToolHandle.h.

117 {
118 // by convention setting a systematic on a non-systematics tool
119 // will do nothing
120 if (m_systTool == nullptr)
121 return;
122 if (!m_systTool->applySystematicVariation (CP::SystematicSet (sysName)).isSuccess())
123 throw std::runtime_error ("failed to apply systematic variation");
124 }
CP::ISystematicsTool * m_systTool

◆ call()

void columnar::PythonToolHandle::call ( )
inline

call the tool and reset the columns

Definition at line 143 of file PythonToolHandle.h.

144 {
145 if (!m_columns)
146 throw std::runtime_error ("no columns set");
147 m_columns->checkData ();
148 m_columns->callNoCheck (*m_tool);
149 m_columns = std::make_unique<ColumnVectorData> (m_columnHeader.get());
150 }
std::shared_ptr< const ColumnVectorHeader > m_columnHeader
std::unique_ptr< ColumnVectorData > m_columns

◆ getColumnInfo()

std::vector< ColumnInfo > columnar::PythonToolHandle::getColumnInfo ( ) const
inlinenodiscard

get the expected column info

Definition at line 153 of file PythonToolHandle.h.

154 {
155 if (!m_toolWrapper)
156 throw std::runtime_error ("tool not initialized");
157 return m_tool->getColumnInfo ();
158 }
std::shared_ptr< const ToolColumnVectorMap > m_toolWrapper

◆ getColumnNames()

std::vector< std::string > columnar::PythonToolHandle::getColumnNames ( ) const
inline

get the expected column names

Definition at line 161 of file PythonToolHandle.h.

162 {
163 if (!m_toolWrapper)
164 throw std::runtime_error ("tool not initialized");
165 return m_toolWrapper->getColumnNames ();
166 }

◆ getConfig()

const asg::AsgToolConfig & columnar::PythonToolHandle::getConfig ( ) const
inlinenodiscard

get the AsgToolConfig

Definition at line 64 of file PythonToolHandle.h.

65 {
66 return m_config;
67 }

◆ getRecommendedSystematics()

std::vector< std::string > columnar::PythonToolHandle::getRecommendedSystematics ( ) const
inline

get the recommended systematics

Definition at line 169 of file PythonToolHandle.h.

170 {
171 if (!m_systTool)
172 return {""};
173 std::vector<std::string> result;
174 for (auto& sys : CP::make_systematics_vector (m_systTool->recommendedSystematics()))
175 result.push_back (sys.name());
176 return result;
177 }
std::vector< CP::SystematicSet > make_systematics_vector(const SystematicSet &systematics)
utility functions for working with systematics

◆ initialize()

void columnar::PythonToolHandle::initialize ( )
inline

initialize the tool

Definition at line 104 of file PythonToolHandle.h.

105 {
106 if (m_tool == nullptr)
107 preinitialize ();
108
109 auto columnHeader = std::make_shared<ColumnVectorHeader> ();
110 m_columnHeader = columnHeader;
111 m_toolWrapper = std::make_shared<ToolColumnVectorMap> (*columnHeader, *m_tool);
112 m_columns = std::make_unique<ColumnVectorData> (m_columnHeader.get());
113 }
void preinitialize()
preinitialize the tool

◆ initMessaging()

void AthMessaging::initMessaging ( ) const
privateinherited

Initialize our message level and MessageSvc.

This method should only be called once.

Definition at line 39 of file AthMessaging.cxx.

40{
42 // If user did not set an explicit level, set a default
43 if (m_lvl == MSG::NIL) {
44 m_lvl = m_imsg ?
45 static_cast<MSG::Level>( m_imsg.load()->outputLevel(m_nm) ) :
46 MSG::INFO;
47 }
48}
std::string m_nm
Message source name.
std::atomic< IMessageSvc * > m_imsg
MessageSvc pointer.
std::atomic< MSG::Level > m_lvl
Current logging level.
IMessageSvc * getMessageSvc(bool quiet=false)

◆ msg() [1/2]

MsgStream & asg::AsgMessaging::msg ( ) const
inherited

The standard message stream.

Returns
A reference to the default message stream of this object.

Definition at line 49 of file AsgMessaging.cxx.

49 {
50#ifndef XAOD_STANDALONE
51 return ::AthMessaging::msg();
52#else // not XAOD_STANDALONE
53 return m_msg;
54#endif // not XAOD_STANDALONE
55 }

◆ msg() [2/2]

MsgStream & asg::AsgMessaging::msg ( const MSG::Level lvl) const
inherited

The standard message stream.

Parameters
lvlThe message level to set the stream to
Returns
A reference to the default message stream, set to level "lvl"

Definition at line 57 of file AsgMessaging.cxx.

57 {
58#ifndef XAOD_STANDALONE
59 return ::AthMessaging::msg( lvl );
60#else // not XAOD_STANDALONE
61 m_msg << lvl;
62 return m_msg;
63#endif // not XAOD_STANDALONE
64 }

◆ msgLvl()

bool asg::AsgMessaging::msgLvl ( const MSG::Level lvl) const
inherited

Test the output level of the object.

Parameters
lvlThe message level to test against
Returns
boolean Indicting if messages at given level will be printed
true If messages at level "lvl" will be printed

Definition at line 41 of file AsgMessaging.cxx.

41 {
42#ifndef XAOD_STANDALONE
43 return ::AthMessaging::msgLvl( lvl );
44#else // not XAOD_STANDALONE
45 return m_msg.msgLevel( lvl );
46#endif // not XAOD_STANDALONE
47 }

◆ preinitialize()

void columnar::PythonToolHandle::preinitialize ( )
inline

preinitialize the tool

Definition at line 70 of file PythonToolHandle.h.

71 {
72 ANA_MSG_DEBUG("preinitializing with " << m_toolHandle << " and cleanup " << m_cleanup);
73 if (!m_config.makeTool (m_toolHandle, m_cleanup).isSuccess())
74 throw std::runtime_error ("failed to create tool");
75
76 ANA_MSG_DEBUG("m_config created tool" << m_toolHandle);
77
78 m_tool = dynamic_cast<IColumnarTool*> (&*m_toolHandle);
79
80 ANA_MSG_DEBUG("attempting to dynamically cast to IColumnarTool* gives " << m_tool);
81 if (m_tool == nullptr)
82 throw std::runtime_error ("The tool does not implement IColumnarTool. First, check to make sure you're in the ColumnarAnalysis release. Then, check to see if the tool inherits from ColumnarTool.");
83 m_systTool = dynamic_cast<CP::ISystematicsTool*> (m_tool);
84 }
#define ANA_MSG_DEBUG(xmsg)
Macro printing debug messages.
std::shared_ptr< void > m_cleanup
ToolHandle< asg::AsgTool > m_toolHandle

◆ renameContainers()

void columnar::PythonToolHandle::renameContainers ( const std::vector< std::pair< std::string, std::string > > & renames)
inline

rename the columns the tool uses

Definition at line 87 of file PythonToolHandle.h.

88 {
89 if (m_tool == nullptr)
91 {
93 }
94 if (m_toolWrapper)
95 {
96 auto columnHeader = std::make_shared<ColumnVectorHeader> ();
97 m_columnHeader = columnHeader;
98 m_toolWrapper = std::make_shared<ToolColumnVectorMap> (*columnHeader, *m_tool);
99 m_columns = std::make_unique<ColumnVectorData> (m_columnHeader.get());
100 }
101 }
void renameContainers(IColumnarTool &tool, const std::vector< std::pair< std::string, std::string > > &renames)
rename containers in the columnar tool

◆ setColumn()

template<typename CT>
void columnar::PythonToolHandle::setColumn ( const std::string & key,
std::size_t size,
CT * dataPtr )
inline

set a column pointer (raw pointer version)

Definition at line 128 of file PythonToolHandle.h.

129 {
130 if (!m_columns)
131 throw std::runtime_error ("tool not initialized");
132 m_columns->setColumn (m_toolWrapper->getColumnIndex (key), size, dataPtr);
133 }

◆ setColumnVoid()

void columnar::PythonToolHandle::setColumnVoid ( const std::string & name,
std::size_t size,
const void * dataPtr,
const std::type_info & type,
bool isConst )
inline

set a column pointer

Definition at line 136 of file PythonToolHandle.h.

136 {
137 if (!m_columns)
138 throw std::runtime_error ("tool not initialized");
139 m_columns->setColumnVoid (m_toolWrapper->getColumnIndex (name), size, dataPtr, type, isConst);
140 }

◆ setLevel()

void AthMessaging::setLevel ( MSG::Level lvl)
inherited

Change the current logging level.

Use this rather than msg().setLevel() for proper operation with MT.

Definition at line 28 of file AthMessaging.cxx.

29{
30 m_lvl = lvl;
31}

◆ setProperty()

template<typename T>
void columnar::PythonToolHandle::setProperty ( const std::string & key,
T && value )
inline

set a property on the tool

Definition at line 57 of file PythonToolHandle.h.

58 {
59 if (m_config.setProperty (key, std::forward<T> (value)).isFailure())
60 throw std::runtime_error ("failed to set property: " + key);
61 }

◆ setTypeAndName()

void columnar::PythonToolHandle::setTypeAndName ( const std::string & typeAndName)
inline

set the type and name for the tool

Definition at line 50 of file PythonToolHandle.h.

51 {
52 m_config.setTypeAndName (typeAndName);
53 }

Member Data Documentation

◆ ATLAS_THREAD_SAFE

std::atomic_flag m_initialized AthMessaging::ATLAS_THREAD_SAFE = ATOMIC_FLAG_INIT
mutableprivateinherited

Messaging initialized (initMessaging)

Definition at line 141 of file AthMessaging.h.

◆ m_cleanup

std::shared_ptr<void> columnar::PythonToolHandle::m_cleanup
private

Definition at line 188 of file PythonToolHandle.h.

◆ m_columnHeader

std::shared_ptr<const ColumnVectorHeader> columnar::PythonToolHandle::m_columnHeader
private

Definition at line 193 of file PythonToolHandle.h.

◆ m_columns

std::unique_ptr<ColumnVectorData> columnar::PythonToolHandle::m_columns
private

Definition at line 195 of file PythonToolHandle.h.

◆ m_config

asg::AsgToolConfig columnar::PythonToolHandle::m_config
private

Private Members

Definition at line 186 of file PythonToolHandle.h.

◆ m_imsg

std::atomic<IMessageSvc*> AthMessaging::m_imsg { nullptr }
mutableprivateinherited

MessageSvc pointer.

Definition at line 135 of file AthMessaging.h.

135{ nullptr };

◆ m_lvl

std::atomic<MSG::Level> AthMessaging::m_lvl { MSG::NIL }
mutableprivateinherited

Current logging level.

Definition at line 138 of file AthMessaging.h.

138{ MSG::NIL };

◆ m_msg_tls

boost::thread_specific_ptr<MsgStream> AthMessaging::m_msg_tls
mutableprivateinherited

MsgStream instance (a std::cout like with print-out levels)

Definition at line 132 of file AthMessaging.h.

◆ m_nm

std::string AthMessaging::m_nm
privateinherited

Message source name.

Definition at line 129 of file AthMessaging.h.

◆ m_systTool

CP::ISystematicsTool* columnar::PythonToolHandle::m_systTool = nullptr
private

Definition at line 191 of file PythonToolHandle.h.

◆ m_tool

IColumnarTool* columnar::PythonToolHandle::m_tool = nullptr
private

Definition at line 190 of file PythonToolHandle.h.

◆ m_toolHandle

ToolHandle<asg::AsgTool> columnar::PythonToolHandle::m_toolHandle
private

Definition at line 187 of file PythonToolHandle.h.

◆ m_toolWrapper

std::shared_ptr<const ToolColumnVectorMap> columnar::PythonToolHandle::m_toolWrapper
private

Definition at line 194 of file PythonToolHandle.h.


The documentation for this class was generated from the following file: