ATLAS Offline Software
Public Member Functions | Private Member Functions | Private Attributes | List of all members
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 More...
 
void setTypeAndName (const std::string &typeAndName)
 set the type and name for the tool More...
 
template<typename T >
void setProperty (const std::string &key, T &&value)
 set a property on the tool More...
 
const asg::AsgToolConfiggetConfig () const
 get the AsgToolConfig More...
 
void preinitialize ()
 preinitialize the tool More...
 
void renameContainers (const std::vector< std::pair< std::string, std::string >> &renames)
 rename the columns the tool uses More...
 
void initialize ()
 initialize the tool More...
 
void applySystematicVariation (const std::string &sysName)
 set the tool to apply the given systematic variation More...
 
template<typename CT >
void setColumn (const std::string &key, std::size_t size, CT *dataPtr)
 set a column pointer (raw pointer version) More...
 
void setColumnVoid (const std::string &name, std::size_t size, const void *dataPtr, const std::type_info &type, bool isConst)
 set a column pointer More...
 
void call ()
 call the tool and reset the columns More...
 
std::vector< ColumnInfogetColumnInfo () const
 get the expected column info More...
 
std::vector< std::string > getColumnNames () const
 get the expected column names More...
 
std::vector< std::string > getRecommendedSystematics () const
 get the recommended systematics More...
 
void setLevel (MSG::Level lvl)
 Change the current logging level. More...
 

Private Member Functions

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

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. More...
 
boost::thread_specific_ptr< MsgStream > m_msg_tls
 MsgStream instance (a std::cout like with print-out levels) More...
 
std::atomic< IMessageSvc * > m_imsg { nullptr }
 MessageSvc pointer. More...
 
std::atomic< MSG::Level > m_lvl { MSG::NIL }
 Current logging level. More...
 
std::atomic_flag m_initialized ATLAS_THREAD_SAFE = ATOMIC_FLAG_INIT
 Messaging initialized (initMessaging) More...
 

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 38 of file PythonToolHandle.h.

Constructor & Destructor Documentation

◆ PythonToolHandle()

columnar::PythonToolHandle::PythonToolHandle ( )
inline

standard constructor

Public Members

Definition at line 46 of file PythonToolHandle.h.

46 : 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 126 of file PythonToolHandle.h.

127  {
128  // by convention setting a systematic on a non-systematics tool
129  // will do nothing
130  if (m_systTool == nullptr)
131  return;
133  throw std::runtime_error ("failed to apply systematic variation");
134  }

◆ call()

void columnar::PythonToolHandle::call ( )
inline

call the tool and reset the columns

Definition at line 153 of file PythonToolHandle.h.

154  {
155  if (!m_columns)
156  throw std::runtime_error ("no columns set");
157  m_columns->checkData ();
158  m_columns->callNoCheck (*m_tool);
159  m_columns = std::make_unique<ColumnVectorData> (m_columnHeader.get());
160  }

◆ getColumnInfo()

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

get the expected column info

Definition at line 163 of file PythonToolHandle.h.

164  {
165  if (!m_toolWrapper)
166  throw std::runtime_error ("tool not initialized");
167  return m_tool->getColumnInfo ();
168  }

◆ getColumnNames()

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

get the expected column names

Definition at line 171 of file PythonToolHandle.h.

172  {
173  if (!m_toolWrapper)
174  throw std::runtime_error ("tool not initialized");
175  return m_toolWrapper->getColumnNames ();
176  }

◆ getConfig()

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

get the AsgToolConfig

Definition at line 63 of file PythonToolHandle.h.

64  {
65  return m_config;
66  }

◆ getRecommendedSystematics()

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

get the recommended systematics

Definition at line 179 of file PythonToolHandle.h.

180  {
181  if (!m_systTool)
182  return {""};
183  std::vector<std::string> result;
185  result.push_back (sys.name());
186  return result;
187  }

◆ initialize()

void columnar::PythonToolHandle::initialize ( )
inline

initialize the tool

Definition at line 114 of file PythonToolHandle.h.

115  {
116  if (m_tool == nullptr)
117  preinitialize ();
118 
119  auto columnHeader = std::make_shared<ColumnVectorHeader> ();
120  m_columnHeader = columnHeader;
121  m_toolWrapper = std::make_shared<ToolColumnVectorMap> (*columnHeader, *m_tool);
122  m_columns = std::make_unique<ColumnVectorData> (m_columnHeader.get());
123  }

◆ 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  m_lvl = m_imsg ?
43  static_cast<MSG::Level>( m_imsg.load()->outputLevel(m_nm) ) :
44  MSG::INFO;
45 }

◆ 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
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
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 69 of file PythonToolHandle.h.

70  {
71  ANA_MSG_DEBUG("preinitializing with " << m_toolHandle << " and cleanup " << m_cleanup);
72  if (!m_config.makeTool (m_toolHandle, m_cleanup).isSuccess())
73  throw std::runtime_error ("failed to create tool");
74 
75  ANA_MSG_DEBUG("m_config created tool" << m_toolHandle);
76 
77  m_tool = dynamic_cast<IColumnarTool*> (&*m_toolHandle);
78 
79  ANA_MSG_DEBUG("attempting to dynamically cast to IColumnarTool* gives " << m_tool);
80  if (m_tool == nullptr)
81  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.");
82  m_systTool = dynamic_cast<CP::ISystematicsTool*> (m_tool);
83  }

◆ 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 86 of file PythonToolHandle.h.

87  {
88  if (m_tool == nullptr)
89  preinitialize ();
90  {
91  auto columnInfo = m_tool->getColumnInfo ();
92  for (auto& [from, to] : renames)
93  {
94  for (auto& column : columnInfo)
95  {
96  if (column.name.starts_with (from) && (column.name.size() == from.size() || column.name[from.size()] == '.'))
97  {
98  std::string newName = to + column.name.substr (from.size());
100  }
101  }
102  }
103  }
104  if (m_toolWrapper)
105  {
106  auto columnHeader = std::make_shared<ColumnVectorHeader> ();
107  m_columnHeader = columnHeader;
108  m_toolWrapper = std::make_shared<ToolColumnVectorMap> (*columnHeader, *m_tool);
109  m_columns = std::make_unique<ColumnVectorData> (m_columnHeader.get());
110  }
111  }

◆ 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 138 of file PythonToolHandle.h.

139  {
140  if (!m_columns)
141  throw std::runtime_error ("tool not initialized");
142  m_columns->setColumn (m_toolWrapper->getColumnIndex (key), size, dataPtr);
143  }

◆ 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 146 of file PythonToolHandle.h.

146  {
147  if (!m_columns)
148  throw std::runtime_error ("tool not initialized");
149  m_columns->setColumnVoid (m_toolWrapper->getColumnIndex (name), size, dataPtr, type, isConst);
150  }

◆ 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 56 of file PythonToolHandle.h.

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

◆ setTypeAndName()

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

set the type and name for the tool

Definition at line 49 of file PythonToolHandle.h.

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

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 198 of file PythonToolHandle.h.

◆ m_columnHeader

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

Definition at line 203 of file PythonToolHandle.h.

◆ m_columns

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

Definition at line 205 of file PythonToolHandle.h.

◆ m_config

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

Private Members

Definition at line 196 of file PythonToolHandle.h.

◆ m_imsg

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

MessageSvc pointer.

Definition at line 135 of file AthMessaging.h.

◆ m_lvl

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

Current logging level.

Definition at line 138 of file AthMessaging.h.

◆ 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 201 of file PythonToolHandle.h.

◆ m_tool

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

Definition at line 200 of file PythonToolHandle.h.

◆ m_toolHandle

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

Definition at line 197 of file PythonToolHandle.h.

◆ m_toolWrapper

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

Definition at line 204 of file PythonToolHandle.h.


The documentation for this class was generated from the following file:
columnar::PythonToolHandle::m_toolHandle
ToolHandle< asg::AsgTool > m_toolHandle
Definition: PythonToolHandle.h:197
AthMessaging::m_lvl
std::atomic< MSG::Level > m_lvl
Current logging level.
Definition: AthMessaging.h:138
CaloCondBlobAlgs_fillNoiseFromASCII.sysName
sysName
Definition: CaloCondBlobAlgs_fillNoiseFromASCII.py:92
columnar::PythonToolHandle::m_cleanup
std::shared_ptr< void > m_cleanup
Definition: PythonToolHandle.h:198
get_generator_info.result
result
Definition: get_generator_info.py:21
CP::make_systematics_vector
std::vector< CP::SystematicSet > make_systematics_vector(const SystematicSet &systematics)
utility functions for working with systematics
Definition: SystematicsUtil.cxx:25
columnar::PythonToolHandle::m_toolWrapper
std::shared_ptr< const ToolColumnVectorMap > m_toolWrapper
Definition: PythonToolHandle.h:204
CP::SystematicSet
Class to wrap a set of SystematicVariations.
Definition: SystematicSet.h:31
DeMoUpdate.column
dictionary column
Definition: DeMoUpdate.py:1110
columnar::PythonToolHandle::m_tool
IColumnarTool * m_tool
Definition: PythonToolHandle.h:200
athena.value
value
Definition: athena.py:124
columnar::PythonToolHandle::m_systTool
CP::ISystematicsTool * m_systTool
Definition: PythonToolHandle.h:201
AthMessaging::m_imsg
std::atomic< IMessageSvc * > m_imsg
MessageSvc pointer.
Definition: AthMessaging.h:135
columnar::PythonToolHandle::m_columnHeader
std::shared_ptr< const ColumnVectorHeader > m_columnHeader
Definition: PythonToolHandle.h:203
python.CaloAddPedShiftConfig.type
type
Definition: CaloAddPedShiftConfig.py:42
Athena::getMessageSvc
IMessageSvc * getMessageSvc(bool quiet=false)
Definition: getMessageSvc.cxx:20
mapkey::sys
@ sys
Definition: TElectronEfficiencyCorrectionTool.cxx:42
TrigConf::MSGTC::Level
Level
Definition: Trigger/TrigConfiguration/TrigConfBase/TrigConfBase/MsgStream.h:21
columnar::IColumnarTool::getColumnInfo
virtual std::vector< ColumnInfo > getColumnInfo() const =0
the meta-information for the columns
columnar::PythonToolHandle::m_config
asg::AsgToolConfig m_config
Definition: PythonToolHandle.h:196
columnar::size
std::size_t size() const noexcept
Definition: ObjectRange.h:166
columnar::IColumnarTool::renameColumn
virtual void renameColumn(const std::string &from, const std::string &to)=0
rename one of the columns the tool uses
CP::IReentrantSystematicsTool::recommendedSystematics
virtual SystematicSet recommendedSystematics() const =0
the list of all systematics this tool recommends to use
asg::AsgComponentConfig::setTypeAndName
void setTypeAndName(const std::string &val_typeAndName)
set type and name at the same time
Definition: AsgComponentConfig.cxx:116
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:240
asg::AsgMessaging
Class mimicking the AthMessaging class from the offline software.
Definition: AsgMessaging.h:40
MakeNewFileFromOldAndSubstitution.newName
dictionary newName
Definition: ICHEP2016/MakeNewFileFromOldAndSubstitution.py:95
columnar::PythonToolHandle::preinitialize
void preinitialize()
preinitialize the tool
Definition: PythonToolHandle.h:69
CxxUtils::to
CONT to(RANGE &&r)
Definition: ranges.h:39
CP::ISystematicsTool
Interface for all CP tools supporting systematic variations.
Definition: ISystematicsTool.h:32
python.Constants.INFO
int INFO
Definition: Control/AthenaCommon/python/Constants.py:15
AthMessaging::m_nm
std::string m_nm
Message source name.
Definition: AthMessaging.h:129
asg::AsgComponentConfig::setProperty
StatusCode setProperty(const std::string &name, const T &value)
set the given property
columnar::PythonToolHandle::m_columns
std::unique_ptr< ColumnVectorData > m_columns
Definition: PythonToolHandle.h:205
asg::AsgToolConfig::makeTool
::StatusCode makeTool(ToolHandle< T > &toolHandle, std::shared_ptr< void > &cleanup, bool allowNestedName=false) const
make a tool with the given configuration
CP::ISystematicsTool::applySystematicVariation
virtual StatusCode applySystematicVariation(const SystematicSet &systConfig)=0
effects: configure this tool for the given list of systematic variations.
python.difftuple.renames
dictionary renames
Definition: difftuple.py:22
python.AutoConfigFlags.msg
msg
Definition: AutoConfigFlags.py:7
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37
ANA_MSG_DEBUG
#define ANA_MSG_DEBUG(xmsg)
Macro printing debug messages.
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:288