13#ifndef ATHANALYSISBASECOMPS_ATHANALYSISHELPER_H
14#define ATHANALYSISBASECOMPS_ATHANALYSISHELPER_H 1
17#include "GaudiKernel/ServiceHandle.h"
19#include "Gaudi/Interfaces/IOptionsSvc.h"
20#include "GaudiKernel/IToolSvc.h"
24#include "GaudiKernel/AlgTool.h"
25#include "GaudiKernel/Algorithm.h"
29#include "GaudiKernel/IAppMgrUI.h"
32#include "GaudiKernel/ToolHandle.h"
45 static IAppMgrUI*
initGaudi(
const char* options =
"");
50 template<
typename W>
static StatusCode
addPropertyToCatalogue(
const std::string& name ,
const std::string& property,
const W& value,
bool override=
true) {
52 if(joSvc.retrieve().isFailure())
return StatusCode::FAILURE;
57 std::cout <<
"WARNING: " << name <<
"." <<
property <<
" already defined as " << pValue <<
" ... ignoring " << Gaudi::Utils::toString ( value ) << std::endl;
58 return StatusCode::FAILURE;
62 if constexpr (std::is_convertible<W, std::string>::value) {
69 joSvc->set( name+
"."+property , std::string (value) );
72 joSvc->set( name+
"."+property , Gaudi::Utils::toString ( value ) );
74 return StatusCode::SUCCESS;
79 template<
typename W>
static StatusCode
setProperty(
const std::string& name ,
const std::string& property,
const W& value,
bool override=
true) {
89 static StatusCode
setProperty(
const std::string& name ,
const std::string& property,
const std::string& value,
bool override=
true) {
106 template<class W, typename = typename std::enable_if<!std::is_base_of<GaudiHandleBase,W>::value &&
107 !std::is_base_of<GaudiHandleArrayBase,W>::value>
::type>
108 static StatusCode
setProperty(
const GaudiHandleBase& toolHandle,
const std::string& property,
const W& value,
bool override=
true) {
109 if(toolHandle.empty()) {
110 std::cout <<
"ERROR: Empty toolHandle passed to AthAnalysisHelper::setProperty" << std::endl;
111 return StatusCode::FAILURE;
113 std::string fullName = toolHandle.parentName() +
"." + toolHandle.name();
114 std::string thePropertyName(property);
116 std::string::size_type dotLocation = thePropertyName.find_last_of(
'.');
117 if(dotLocation != std::string::npos) {
118 fullName +=
"." + thePropertyName.substr(0,dotLocation);
119 thePropertyName = thePropertyName.substr(dotLocation+1,thePropertyName.length()-dotLocation);
123 std::cout <<
"ERROR: Cannot setProperty on a tool that is already initialized" << std::endl;
124 return StatusCode::FAILURE;
131 static StatusCode
setProperty(
const GaudiHandleBase& toolHandle,
const std::string& property,
const GaudiHandleBase& value,
bool override=
true) {
132 std::string subToolName(value.name());
133 size_t start_pos = subToolName.find(toolHandle.name()+
".");
134 if(start_pos!=std::string::npos) { subToolName.replace( start_pos, toolHandle.name().length()+1,
"" ); }
135 std::string typeAndName = value.type();
if(!subToolName.empty()) typeAndName +=
"/"+subToolName;
136 return setProperty( toolHandle, property, typeAndName,
override );
138 static StatusCode
setProperty(
const GaudiHandleBase& toolHandle,
const std::string& property,
const GaudiHandleArrayBase& value,
bool override=
true) {
139 return setProperty( toolHandle, property, value.typesAndNames(),
override );
143 template<
typename W>
static StatusCode
setProperty(IAlgTool* tool,
const std::string& property,
const W& value) {
144 AlgTool* algtool =
dynamic_cast<AlgTool*
>(tool);
146 std::cout <<
"ERROR: Tool is not an algtool. Cannot AthAnalysisHelper::setProperty on it" << std::endl;
147 return StatusCode::FAILURE;
149 return algtool->setProperty(property, value);
153 template<
typename W>
static StatusCode
setProperty(IAlgorithm* alg,
const std::string& property,
const W& value) {
154 Gaudi::Algorithm* theAlg =
dynamic_cast<Gaudi::Algorithm*
>(alg);
156 std::cout <<
"ERROR: alg is not an Algorithm. Cannot AthAnalysisHelper::setProperty on it" << std::endl;
157 return StatusCode::FAILURE;
159 return theAlg->setProperty(property, value);
162 template<
typename T>
static std::string
toString(
const T& value) {
return Gaudi::Utils::toString( value ); }
163 static std::string
toString(
const std::string& value) {
return value; }
164 static std::string
toString(
const char* value) {
return value; }
168 template<
typename T,
typename W>
static StatusCode
setProperty(
const ServiceHandle<T>& serviceHandle,
const std::string& property,
const W& value) {
169 if(serviceHandle.isSet()) {
170 return dynamic_cast<Service&
>(*serviceHandle).setProperty(property,
toString ( value ));
172 std::string fullName = serviceHandle.name();
173 std::string thePropertyName(property);
175 std::string::size_type dotLocation = thePropertyName.find_last_of(
'.');
176 if(dotLocation != std::string::npos) {
177 fullName +=
"." + thePropertyName.substr(0,dotLocation);
178 thePropertyName = thePropertyName.substr(dotLocation+1,thePropertyName.length()-dotLocation);
182 if(Gaudi::svcLocator()->existsService(serviceHandle.name())) {
184 return dynamic_cast<Service&
>(*serviceHandle).setProperty(property,
toString ( value ));
204 template<
typename W>
static W*
createTool(
const std::string& typeAndName, INamedInterface* parent = 0) {
205 std::string
type = typeAndName; std::string name = typeAndName;
206 std::string::size_type pos =
type.find(
'/');
207 if(pos!=std::string::npos) {
209 name = name.substr(pos+1,name.length());
213 parent = Gaudi::svcLocator()->service(
"ToolSvc" );
215 IAlgTool *algtool = AlgTool::Factory::create(
type,
type,name,parent).release();
217 W* out =
dynamic_cast<W*
>(algtool);
219 std::cout <<
"ERROR: Tool of type " <<
type <<
" does not implement the interface " << System::typeinfoName(
typeid(W)) << std::endl;
225 static IAlgTool*
createTool(
const std::string& typeAndName, INamedInterface* parent = 0) {
232 std::string
type = typeAndName; std::string name = typeAndName;
233 std::string::size_type pos =
type.find(
'/');
234 if(pos!=std::string::npos) {
236 name = name.substr(pos+1,name.length());
238 IAlgorithm* out = Algorithm::Factory::create(
type,name,Gaudi::svcLocator()).release();
245 static bool toolExists(
const std::string& fullName );
246 static bool toolExists(
const GaudiHandleBase& toolHandle) {
return toolExists( toolHandle.parentName() +
"." + toolHandle.name() ); }
257 if(
result.isFailure()) { std::cout <<
"ERROR: Could not retrieve IOVMetadata with folder " << folder <<
" and key " << key << std::endl; }
263 if(
result.isFailure()) { std::cout <<
"ERROR: Could not retrieve IOVMetadata with folder " << folder <<
" and key " << key << std::endl; }
270 template<
typename T>
static StatusCode
retrieveMetadata(
const std::string& folder,
const std::string& key, T& out) {
272 if(inputMetaStore.retrieve().isFailure())
return StatusCode::FAILURE;
274 if(inputMetaStore.release().isFailure())
return StatusCode::FAILURE;
283 if( inputMetaStore->retrieve(cont,folder).isFailure())
return StatusCode::FAILURE;
294 if(attr.specification().typeName()==
"string") {
297 out = attr.data<T>();
301 return StatusCode::FAILURE;
304 return StatusCode::SUCCESS;
306 return StatusCode::FAILURE;
313 if( inputMetaStore->retrieve(cont,folder).isFailure())
return StatusCode::FAILURE;
321 if(channel<0) channel = (*cond)->chanNum(0);
324 auto attrlist = (*cond)->chanAttrListPair(channel);
325 if(attrlist == (*cond)->end()) {
return StatusCode::FAILURE; }
332 const coral::Attribute& attr = attrlist->second[key];
333 if(attr.specification().typeName()==
"string") {
336 out = attr.data<T>();
340 return StatusCode::FAILURE;
343 return StatusCode::SUCCESS;
346 template<
typename T>
static StatusCode
retrieveMetadata(
const std::string& folder,
const std::string& key, T& out,
const IOVTime& time,
int channel=-1) {
348 if(inputMetaStore.retrieve().isFailure())
return StatusCode::FAILURE;
350 if(inputMetaStore.release().isFailure())
return StatusCode::FAILURE;
368 static std::string
getProperty(
const std::string& client,
const std::string& property);
383 if(!handle.isSet()) {std::cout <<
"Please retrieve handle before dumping properties" << std::endl;
return;}
401 static StatusCode
setProperty (
const GaudiHandleBase& toolHandle,
const std::string& property,
const int& value,
bool override =
true)
403 static StatusCode
setProperty (
const GaudiHandleBase& toolHandle,
const std::string& property,
const double& value,
bool override =
true)
405 static StatusCode
setProperty (
const GaudiHandleBase& toolHandle,
const std::string& property,
const std::string& value,
bool override =
true)
Base class for elements of a container that can have aux data.
static StatusCode setProperty(const GaudiHandleBase &toolHandle, const std::string &property, const int &value, bool override=true)
static StatusCode setProperty(const GaudiHandleBase &toolHandle, const std::string &property, const std::string &value, bool override=true)
static StatusCode setProperty(const GaudiHandleBase &toolHandle, const std::string &property, const double &value, bool override=true)
static void printAuxElement(const SG::AuxElement &ae)
Print the aux variables of an xAOD object (aux element) An alternative to this method is the 'xAOD::d...
static void dumpProperties(const std::string &client="")
static TFile * getOutputFile(const std::string &streamName)
static StatusCode setProperty(const GaudiHandleBase &toolHandle, const std::string &property, const GaudiHandleArrayBase &value, bool override=true)
static std::string getProperty(const std::string &client, const std::string &property)
Check catalogue for property of specified client returns AAH::UNDEFINED in case of no property value ...
static std::string toString(const std::string &value)
static void dumpJobOptionProperties(const std::string &client="")
Dump the properties from joboptionsvc of clients with names beginning with given string.
static std::string toString(const T &value)
static StatusCode retrieveMetadata(const std::string &folder, const std::string &key, T &out, const ServiceHandle< StoreGateSvc > &inputMetaStore, const IOVTime &time, int channel=-1)
retrieve metadata, for a specified IOVTime and a specific channel, unless the channel is -1,...
static StatusCode setProperty(IAlgTool *tool, const std::string &property, const W &value)
setProperty on any tool, even when initialized
static IAlgorithm * createAlgorithm(const std::string &typeAndName)
static StatusCode setProperty(const ServiceHandle< T > &serviceHandle, const std::string &property, const W &value)
setProperty for services ... will allow setProperty on already-existing services
static bool toolExists(const std::string &fullName)
check if tool already exists. FullName = Parent.Name
static bool toolExists(const GaudiHandleBase &toolHandle)
static IAppMgrUI * initGaudi(const char *options="")
initGaudi method starts the gaudi ApplicationMgr ready for working with all the components
static const std::string UNDEFINED
static void dumpProperties(const GaudiHandle< T > &handle)
static StatusCode retrieveMetadata(const std::string &folder, const std::string &key, T &out, const IOVTime &time, int channel=-1)
static std::string retrieveMetadata(const std::string &folder, const std::string &key, const ServiceHandle< StoreGateSvc > &inputMetaStore)
method that always returns as a string you can use from, e.g, pyROOT with evt = ROOT....
static W * createTool(const std::string &typeAndName, INamedInterface *parent=0)
Create a tool using the gaudi factory methods.
static StatusCode setProperty(const std::string &name, const std::string &property, const W &value, bool override=true)
static StatusCode setProperty(IAlgorithm *alg, const std::string &property, const W &value)
setProperty on an alg, even when initialized
static IAlgTool * createTool(const std::string &typeAndName, INamedInterface *parent=0)
static std::string retrieveMetadata(const std::string &folder, const std::string &key, ServiceHandle< StoreGateSvc > &inputMetaStore)
static StatusCode setProperty(const std::string &name, const std::string &property, const std::string &value, bool override=true)
non-template method, for use in joboptions current usage would be: ... usual joboptions ....
static void dumpProperties(const SG::AuxElement &ae)
static StatusCode retrieveMetadata(const std::string &folder, const std::string &key, T &out, const ServiceHandle< StoreGateSvc > &inputMetaStore)
implemenation where you pass it a particular store instead
static StatusCode setProperty(const GaudiHandleBase &toolHandle, const std::string &property, const GaudiHandleBase &value, bool override=true)
Partial template specialization for ToolHandles and ToolHandleArrays ... strips parent name from tool...
static StatusCode setProperty(const GaudiHandleBase &toolHandle, const std::string &property, const W &value, bool override=true)
helper method for setting a property on a tool before retrieving it uses the toolhandle to work out w...
static StatusCode addPropertyToCatalogue(const std::string &name, const std::string &property, const W &value, bool override=true)
helper method for adding a property to the JobOptionsSvc to list all the properties in the catalogue,...
static std::string toString(const char *value)
static StatusCode retrieveMetadata(const std::string &folder, const std::string &key, T &out)
retrieve metadata from the input metadata storegate.
const AttributeList & attributeList(ChanNum chanNum) const
attribute list for a given channel number
ChanNum chanNum(unsigned int index) const
channel number for index: (index = 0 to size-1)
size_type size() const
number of Chan/AttributeList pairs
size_type size() const
size of payload vector
const_iterator end() const
End of payload vector.
const_iterator find(const IOVTime &time) const
find the first payload that has a IOVRange which includes the
CondAttrListCollection * at(unsigned int i) const
Element access.
Basic time unit for IOVSvc.
Base class for elements of a container that can have aux data.
StatusCode parse(std::tuple< Tup... > &tup, const Gaudi::Parsers::InputData &input)