19#include "GaudiKernel/ThreadLocalContext.h"
30 void popRenounced(
const std::vector<std::string> &renounce,
31 std::vector<Gaudi::DataHandle *> &new_input_handles) {
32 for (
const std::string &a_key : renounce) {
33 if (new_input_handles.back()->objKey() == a_key ) {
34 new_input_handles.pop_back();
56 size_t dotPosition = varname.find(
'.');
57 containerName = varname.substr(0, dotPosition);
58 methodName = varname.substr(dotPosition + 1);
61 std::pair<RootUtils::TSMethodCall, TVirtualCollectionProxy *>
64 std::cout <<
"DEBUG SGxAODProxyLoader search for " << method_name <<
" in type " << info.name() << std::endl;
66 TClass *containerClass = TClass::GetClass(info);
67 if (!containerClass) {
69 std::cout <<
"DEBUG SGxAODProxyLoader search for " << method_name <<
" in normalisze type " <<
SG::normalizedTypeinfoName(info) << std::endl;
72 if (!containerClass) {
73 std::stringstream
msg;
74 msg <<
"No dictionary for " << info.name() << std::endl;
75 throw std::runtime_error(
msg.str());
78 TClass *elementClass=
nullptr;
79 TVirtualCollectionProxy *collection_proxy=
nullptr;
80 if( strcmp(containerClass->GetName(),
"SG::AuxElementStandaloneData")==0 ) {
81 elementClass=containerClass;
84 collection_proxy = containerClass->GetCollectionProxy();
85 elementClass = (collection_proxy ? collection_proxy->GetValueClass() :
nullptr);
87 std::stringstream
msg;
88 msg <<
"No collection proxy or element class for " << info.name() << std::endl;
89 throw std::runtime_error(
msg.str());
93 method_call.
setProto (elementClass, method_name,
"",
94 ROOT::kConversionMatch);
95 if (!method_call.
call() || !method_call.
call()->IsValid()) {
96 std::stringstream
msg;
97 msg <<
"No valid method " << method_name <<
" for " << info.name() << std::endl;
98 throw std::runtime_error(
msg.str());
102 std::cout <<
"DEBUG SGxAODProxyLoader got method " <<
" . " << method_name <<
" : "
103 << method_call.
call()->GetMethod()->GetReturnTypeNormalizedName ()
104 <<
" proto=" << method_call.
call()->GetProto()
105 <<
" signature=" << method_call.
call()->GetMethod()->GetSignature ()
106 <<
" proto=" << method_call.
call()->GetMethod()->GetPrototype()
109 return std::make_pair( method_call, collection_proxy);
113 template <
class T_Aux>
118 const std::string &method_name)
const
122 std::stringstream
msg;
123 msg <<
"Failed to get " << key.key();
124 throw std::runtime_error(
msg.str());
127 return std::unique_ptr<IAccessor>();
130 std::pair<RootUtils::TSMethodCall,TVirtualCollectionProxy *> method =
getMethodCallAccessor(method_name,
typeid(*handle));
140 const std::string &varname)
const
144 std::string container_name;
145 std::string method_name;
147 ReadHandleMap::const_iterator key_iter =
m_readKeys.find(container_name);
149 std::stringstream
msg;
150 msg <<
"No read handle key for " << container_name;
151 throw std::runtime_error(
msg.str());
154 ReadDecorHandleMap::const_iterator decor_key_iter =
m_decorKeys.end();
161 std::unique_ptr<IAccessor> accessor;
163 if (
m_verbose) { std::cout <<
"DEBUG SGxAODProxyLoader::computeClassForVarname is Vector: " << varname << std::endl; }
171 if (
m_verbose) { std::cout <<
"DEBUG SGxAODProxyLoader::computeClassForVarname is element: " << varname << std::endl; }
179 if (
m_verbose) { std::cout <<
"DEBUG SGxAODProxyLoader::computeClassForVarname try to create plain type accessor for " << varname << std::endl; }
185 IAccessor* accessorPlainPtr = accessor.get();
187 std::cout <<
"DEBUG SGxAODProxyLoader use accessor "
188 <<
typeid(*accessorPlainPtr).name() <<
" for " << varname
191 accessor_iter->second.set(std::move(accessor));
192 return *(accessor_iter->second);
196 const EventContext& ctx = Gaudi::Hive::currentContext();
201 template <
typename T>
202 std::ostream &
operator<<(std::ostream &out,
const std::vector<T> &
vec) {
203 for (
auto elm :
vec) {
204 std::cout <<
" " << elm;
209 T dump(
const std::string &varname, T val) {
211#ifdef DEBUG_VARIABLE_LOADING
212 std::cout <<
"DEBUG SGxAODProxyLoader::dump " <<
varname <<
val << std::endl;
220 const EventContext& ctx = Gaudi::Hive::currentContext();
226 const EventContext& ctx = Gaudi::Hive::currentContext();
232 const EventContext& ctx = Gaudi::Hive::currentContext();
238 const EventContext& ctx = Gaudi::Hive::currentContext();
243 const std::vector<std::string> &var_names,
244 const std::vector<std::string> &renounce,
245 const std::vector<const DataObjID *> &input_data_in,
246 const std::vector<const DataObjID *> &output_data_in,
247 std::vector<Gaudi::DataHandle *> &new_input_handles,
248 std::vector<Gaudi::DataHandle *> &new_output_handles)
250 (void) new_output_handles;
252 for (
const std::string &var_name : var_names) {
254 std::string container_name;
255 std::string method_name;
258 std::cout <<
"DEBUG SGxAODProxyLoader::updateDataDependencies var " << var_name <<
" -> " << container_name <<
" . " << method_name
259 <<
" i:" << input_data_in.size() <<
" o:" << output_data_in.size()
262 const DataObjID *container_data_id=
nullptr;
263 const DataObjID *method_data_id=
nullptr;
272 auto set_ids = [&method_data_id,&container_data_id,&var_name,&container_name,
verbose](
const DataObjID *obj_data_id) ->
bool {
273 std::string_view handle_key(obj_data_id->key());
274 std::string::size_type pos=obj_data_id->key().find(
'+');
275 pos = (pos==std::string::npos ? 0 : pos+1);
276 handle_key=handle_key.substr(pos,handle_key.size()-pos);
277 if (
verbose && (handle_key == container_name || handle_key == var_name)) {
279 std::cout <<
"DEBUG SGxAODProxyLoader::updateDataDependencies " << handle_key <<
" matches " << var_name <<
" ? "
280 << (handle_key == var_name ?
" method " :
"")
281 << (handle_key == container_name ?
" container " :
"")
282 <<
"-> " << (base_info ? base_info->
typeinfo().name() :
"")
285 if (handle_key == var_name) {
286 method_data_id = obj_data_id;
287 if (container_data_id)
return true;
289 else if (handle_key == container_name ) {
290 container_data_id = obj_data_id;
291 if (method_data_id)
return true;
296 for( std::vector<const DataObjID *>::const_iterator elm_iter = output_data_in.begin(); elm_iter != output_data_in.end() && !set_ids(*elm_iter); ++elm_iter) {}
297 if (!method_data_id) {
298 for( std::vector<const DataObjID *>::const_iterator elm_iter = input_data_in.begin(); elm_iter != input_data_in.end() && !set_ids(*elm_iter); ++elm_iter) {}
300 if (method_data_id) {
301 if (!container_data_id) {
302 container_data_id=method_data_id;
306 if (
m_verbose && method_data_id && method_data_id != container_data_id) {
307 std::cout <<
"DEBUG SGxAODProxyLoader method_clid " << method_data_id->clid() << std::endl;
310 std::cout <<
"DEBUG SGxAODProxyLoader container_clid " << container_data_id->clid() << std::endl;
314 if (container_data_id) {
317 if (have_container_read_key && !method_data_id)
continue;
319 auto clid = container_data_id->clid();
323 std::stringstream
msg;
324 msg <<
"Missing type information about container " << container_name <<
".";
325 if (container_data_id) {
326 msg <<
" It seems that inheritance information is missing for " << container_data_id->className() <<
"."
327 <<
" Only container which inherit from SG::AuxVectorBase or data inheriting from SG::AuxElement "
328 <<
" are supported by the SGxAODProxyLoader loader.";
331 msg <<
" This problem may occur if the container is read from the input"
332 <<
" file and not accessed anywhere in the job via read handles. ";
334 throw std::runtime_error(
msg.str());
338 std::cout <<
"DEBUG SGxAODProxyLoader " << container_name <<
" -> " << base_info->
typeinfo().name() << std::endl;
341 std::cout <<
"DEBUG SGxAODProxyLoader " << container_name <<
" base: " << a_base_info->
typeinfo().name() << std::endl;
350 if (!have_container_read_key) {
351 read_key = std::make_any<SG::ReadHandleKey<SG::AuxVectorBase> >(container_name);
353 if (method_data_id) {
354 decor_key = std::make_any<SG::ReadDecorHandleKey<SG::AuxVectorBase> >(var_name);
358 if (!have_container_read_key) {
359 read_key = std::make_any<SG::ReadHandleKey<SG::AuxElement> >(container_name);
361 if (method_data_id) {
362 decor_key = std::make_any<SG::ReadDecorHandleKey<SG::AuxElement> >(var_name);
366 if (method_data_id && method_data_id != container_data_id && method_data_id->clid() != container_data_id->clid()) {
367 std::stringstream
msg;
368 msg <<
"Did not expect a \"method\" handle for the plain type " << var_name;
369 throw std::logic_error(
msg.str());
372 if (registered_key) {
373 popRenounced(renounce, new_input_handles);
378 if (!have_container_read_key) {
379 if (!read_key.has_value()) {
380 std::stringstream
msg;
381 msg <<
"Container " << container_name <<
" is of unsupported type " << base_info->
typeinfo().name()
382 <<
" expected a type derived from SG::AuxVectorBase or SG::AuxElement";
383 throw std::runtime_error(
msg.str());
385 std::pair<ReadHandleMap::iterator, bool> ret =
m_readKeys.insert(std::make_pair(container_name, std::move(read_key)));
389 std::stringstream
msg;
390 msg <<
"Failed to add read handle key for " << container_name;
391 throw std::runtime_error(
msg.str());
399 popRenounced(renounce, new_input_handles);
403 if (method_data_id) {
404 if (!decor_key.has_value()) {
405 std::stringstream
msg;
406 msg <<
"Container " << container_name <<
" is of unsupported type " << base_info->
typeinfo().name()
407 <<
" expected a type derived from SG::AuxVectorBase or SG::AuxElement";
408 throw std::runtime_error(
msg.str());
410 std::pair<ReadDecorHandleMap::iterator, bool> ret =
m_decorKeys.insert(std::make_pair(var_name,std::move(decor_key)));
414 std::stringstream
msg;
415 msg <<
"Failed to add read decor handle key for " << var_name;
416 throw std::runtime_error(
msg.str());
424 popRenounced(renounce, new_input_handles);
Manage index tracking and synchronization of auxiliary data.
std::vector< size_t > vec
Provide an interface for finding inheritance information at run time.
uint32_t CLID
The Class ID type.
Property holding a SG store/key/clid/attr name from which a ReadDecorHandle is made.
Handle class for reading a decoration on an object.
Cached pointer with atomic update.
std::unique_ptr< IAccessor > create(const SG::ReadHandleKey< SG::AuxElement > &key, SG::auxid_t auxid, const SG::ReadDecorHandleKey< SG::AuxElement > *decor_key=nullptr) const
create an accessor for the specified content of an AuxElement.
Special accessor to handle empty containers until the correct accessor can be created.
Interface of auxiliary classes to access xAOD object content.
virtual IProxyLoader::VariableType variableType() const =0
std::unique_ptr< IAccessor > create(const SG::ReadHandleKey< SG::AuxElement > &key, RootUtils::TSMethodCall &&method_call, TVirtualCollectionProxy *proxy=nullptr) const
Create an accessor which calls the specified method of an AuxElement.
static bool registerReadKey(std::unordered_map< std::string, std::any > &read_keys, SGxAODProxyLoader::IParentHelper &parent, const std::string &var_name, std::vector< Gaudi::DataHandle * > &new_input_handles, bool verbose)
std::unique_ptr< IAccessor > createAccessor(const std::any &handle_key) const
Interface of an auxiliary class to pass the parent, e.g.
static SG::ReadHandleKey< SG::AuxVectorBase > & vectorKey(std::any &anything)
static bool isElement(const std::any &anything)
static bool isVector(const std::any &anything)
static SG::ReadHandleKey< SG::AuxElement > & elementKey(std::any &anything)
std::unordered_map< std::string, CxxUtils::CachedUniquePtrT< IAccessor > > m_accessor
Association of variable names to accessors for corresponding xAOD object content.
void splitVarnameIntoContainerAndMethod(const std::string &varname, std::string &containerName, std::string &methodName) const
virtual std::vector< double > loadVecDoubleVariableFromString(const std::string &varname) const
ServiceHandle< StoreGateSvc > StoreGateSvc_t
virtual double loadDoubleVariableFromString(const std::string &varname) const
StoreGateSvc_t m_evtStore
virtual std::vector< int > loadVecIntVariableFromString(const std::string &varname) const
bool updateDataDependencies(SGxAODProxyLoader::IParentHelper &parent, const std::vector< std::string > &var_names, const std::vector< std::string > &renounce, const std::vector< const DataObjID * > &input_data_in, const std::vector< const DataObjID * > &output_data_in, std::vector< Gaudi::DataHandle * > &new_input_handles, std::vector< Gaudi::DataHandle * > &new_output_handles)
Create extra data dependencies arising eventually from the given variables.
IAccessor & getAccessor(const EventContext &ctx, const std::string &varname) const
Get an existing or create a new accessor for the given variable name.
std::pair< RootUtils::TSMethodCall, TVirtualCollectionProxy * > getMethodCallAccessor(const std::string &method_name, const std::type_info &info) const
Auxiliary method to create a TMethodCall and eventually also a collection proxy to call the given met...
ReadDecorHandleMap m_decorKeys
IAccessor & computeClassForVarname(const EventContext &ctx, std::unordered_map< std::string, CxxUtils::CachedUniquePtrT< IAccessor > >::const_iterator accessor_iter, const std::string &varname) const
Auxiliary method to create an xAOD object content accessor for the given variable name.
virtual int loadIntVariableFromString(const std::string &varname) const
virtual IProxyLoader::VariableType variableTypeFromString(const std::string &varname) const
SGxAODProxyLoader(StoreGateSvc_t &evtStore, bool verbose=false)
std::unique_ptr< IAccessor > createAccessor(const EventContext &ctx, const SG::ReadHandleKey< T_Aux > &key, const SG::ReadDecorHandleKey< T_Aux > *decor_key, SG::auxid_t method_id, const std::string &method_name) const
Auxiliary method to create an accessor for the given method_name.
std::unique_ptr< IAccessor > m_emptyVectorAccessor
Special accessor to handle empty vectors Accessor are constructed at time of first evaluation.
virtual ~SGxAODProxyLoader()
static MethodAccessorFactory & instance()
Helper for making a thread-safe function call.
TMethodCall * call()
Return a pointer to the thread-specific TMethodCall.
void setProto(TClass *cls, const std::string &fname, const std::string &args, ROOT::EFunctionMatchMode mode=ROOT::kExactMatch)
Set the function that we're to call.
SG::auxid_t findAuxID(const std::string &name, const std::string &clsname="") const
Look up a name -> auxid_t mapping.
static AuxTypeRegistry & instance()
Return the singleton registry instance.
The non-template portion of the BaseInfo implementation.
static const BaseInfoBase * find(CLID clid)
Find the BaseInfoBase instance for clid.
const std::type_info & typeinfo() const
Return the std::type_info for this class.
const std::vector< CLID > & get_bases() const
Return the class IDs of all known bases of T (that have class IDs).
bool is_base(CLID clid) const
Return true if clid is the ID of a class that is known to be a base of T.
Property holding a SG store/key/clid/attr name from which a ReadDecorHandle is made.
Property holding a SG store/key/clid from which a ReadHandle is made.
virtual bool isValid() override final
Can the handle be successfully dereferenced?
const ExtendedEventContext & getExtendedEventContext(const EventContext &ctx)
Retrieve an extended context from a context object.
Namespace holding all the expression evaluation code.
std::ostream & operator<<(std::ostream &os, const StackElement &el)
Declare an output operator for StackElement objects.
bool isAvailable(const T_Aux &cont, SG::auxid_t auxid)
void initializeHandle(T_Key *key, std::vector< Gaudi::DataHandle * > &new_input_handles)
Auxiliary function to initialize newly create data handles.
void dumpDeclare(const T *key)
Function for debugging which dumps information about newly declared data handles.
std::size_t getContainerSize(const T &cont)
std::ostream & dump(std::ostream &out, const I4MomIter iBeg, const I4MomIter iEnd)
Helper to stream out a range of I4Momentum objects.
std::string normalizedTypeinfoName(const std::type_info &info)
Convert a type_info to a normalized string representation (matching the names used in the root dictio...
static const auxid_t null_auxid
To signal no aux data item.
size_t auxid_t
Identifier for a particular aux data item.