ATLAS Offline Software
AthAnalysisHelper.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 
8 
9 #include "GaudiKernel/AttribStringParser.h"
10 #include <algorithm>
11 #include <string>
12 #include "TROOT.h"
13 
14 const std::string AthAnalysisHelper::UNDEFINED = "__UNDEFINED__";
15 
16 //need a constructor, implemented here, so that the dictionary library is linked to
17 //the implementation library (see ldd libAthAnalysisBaseCompsDict.so ... needs a link)
19 
20 
21 IAppMgrUI* AthAnalysisHelper::initGaudi(const char* options) {
22  IAppMgrUI* theApp = Gaudi::createApplicationMgr();
23  //check if already configured
24  if(theApp->FSMState() != Gaudi::StateMachine::OFFLINE) return theApp;
25  //set the joboptions
26  SmartIF<IProperty> propMgr(theApp);
27  if(strlen(options)) {
28  if (propMgr->setProperty("JobOptionsPath",options).isFailure()) return nullptr;
29  } else {
30  //no joboptions given
31  if (propMgr->setProperty( "JobOptionsType", "NONE" ).isFailure()) return nullptr;
32  }
33  //using this instead of the default EventLoopMgr means some services (e.g. EventSelector) are not created, which is good! :-)
34  if (propMgr->setProperty("EventLoop","MinimalEventLoopMgr").isFailure()) return nullptr;
35 
36  //configure and return
37  if (theApp->configure().isFailure()) return nullptr;
38  propMgr->setProperty("OutputLevel","3").ignore(); //INFO
39  if (theApp->initialize().isFailure()) return nullptr;
40  return theApp;
41 }
42 
43 bool AthAnalysisHelper::toolExists( const std::string& fullName ) {
44  ServiceHandle<IToolSvc> toolSvc("ToolSvc","AthAnalysisHelper");
45  if(toolSvc.retrieve().isFailure()) return false;
46  auto existingTools = toolSvc->getInstances();
47  for(auto& toolName : existingTools) {
48  if(fullName==toolName) { toolSvc.release().ignore(); return true; }
49  }
50  toolSvc.release().ignore();
51  return false;
52 }
53 
55  ServiceHandle<Gaudi::Interfaces::IOptionsSvc> joSvc("JobOptionsSvc","AthAnalysisHelper");
56  if(joSvc.retrieve().isFailure()) return;
57  for(const auto& [name,value] : joSvc->items()) {
58  if(name.find(client)!=0) continue; //must start with client
59  std::cout << name << " = " << value << std::endl;
60  }
61 }
62 
63 std::string AthAnalysisHelper::getProperty(const std::string& client, const std::string& property) {
64  ServiceHandle<Gaudi::Interfaces::IOptionsSvc> joSvc("JobOptionsSvc","AthAnalysisHelper");
65  if(joSvc.retrieve().isFailure()) return UNDEFINED;
66  return joSvc->get(client+"."+property, UNDEFINED);
67 }
68 
70  const SG::auxid_set_t& auxids = ae.getAuxIDs();
72  for( SG::auxid_t aux : auxids) {
73  std::string name = reg.getName( aux );
74  std::cout << " " << name << " = ";
75  const std::type_info& typeinfo = *reg.getType(aux);
76 #define PRINT_AE( TYPE ) \
77  if(typeinfo==typeid(TYPE)) { \
78  SG::ConstAccessor<TYPE> acc (aux); \
79  if (acc.isAvailable(ae)) std::cout << acc(ae) << " (" << #TYPE << ")" << std::endl; \
80  }
81 
82  PRINT_AE( bool )
83  else PRINT_AE(uint)
84  else PRINT_AE(int)
85  else PRINT_AE(float)
86  else PRINT_AE(double)
87  else PRINT_AE(unsigned char)
88  else PRINT_AE(char)
89  else PRINT_AE(short)
90  else PRINT_AE(unsigned short)
91  else PRINT_AE(unsigned long long)
92  else std::cout << " (" << System::typeinfoName( typeinfo ) << ")" << std::endl;
93 
94 #undef PRINT_AE
95  }
96 }
97 
98 void AthAnalysisHelper::dumpProperties(const IProperty& component) {
99  for(auto p : component.getProperties()) {
100  std::cout << p->name() << " = " << p->toString() << std::endl;
101  }
102 }
103 
104 
105 TFile* AthAnalysisHelper::getOutputFile(const std::string& streamName) {
106  ServiceHandle<IProperty> histSvc("THistSvc","");
107  auto& prop = histSvc->getProperty("Output");
108 
109  std::vector<std::string> outputs;
110  if( Gaudi::Parsers::parse(outputs,prop.toString()).isFailure() ) {
111  return nullptr;
112  }
113 
114  //extract the DATAFILE part of the string
115  std::string fileName="";
116  for(std::string& output : outputs) {
117  if( output.substr(0,output.find(' '))!=streamName ) continue;
118 
119  //got here .. means we found the stream ...
120  for(auto attrib : Gaudi::Utils::AttribStringParser(output.substr(output.find(' ')+1))) {
121  auto TAG = attrib.tag;
122  std::transform(TAG.begin(), TAG.end(), TAG.begin(), [](unsigned char c){ return std::toupper(c); });
123 
124  if(TAG=="FILE" || TAG=="DATAFILE") {
125  fileName = attrib.value;
126  break;
127  }
128 
129  }
130  if(fileName.length()) {
131  return static_cast<TFile*>(gROOT->GetListOfFiles()->FindObject(fileName.c_str()));
132  }
133 
134  }
135  return 0;
136 
137 }
AthAnalysisHelper::toolExists
static bool toolExists(const std::string &fullName)
check if tool already exists. FullName = Parent.Name
Definition: AthAnalysisHelper.cxx:43
Gaudi::Parsers::parse
StatusCode parse(std::tuple< Tup... > &tup, const Gaudi::Parsers::InputData &input)
Definition: CaloGPUClusterAndCellDataMonitorOptions.h:284
SG::AuxTypeRegistry::instance
static AuxTypeRegistry & instance()
Return the singleton registry instance.
Definition: AuxTypeRegistry.cxx:640
SG::AuxElement
Base class for elements of a container that can have aux data.
Definition: AuxElement.h:472
SG::AuxTypeRegistry::getName
std::string getName(SG::auxid_t auxid) const
Return the name of an aux data item.
Definition: AuxTypeRegistry.cxx:882
WriteCellNoiseToCool.fullName
fullName
Definition: WriteCellNoiseToCool.py:461
athena.value
value
Definition: athena.py:124
AthAnalysisHelper::getOutputFile
static TFile * getOutputFile(const std::string &streamName)
Definition: AthAnalysisHelper.cxx:105
PRINT_AE
#define PRINT_AE(TYPE)
rerun_display.client
client
Definition: rerun_display.py:31
SG::AuxTypeRegistry
Handle mappings between names and auxid_t.
Definition: AuxTypeRegistry.h:61
uint
unsigned int uint
Definition: LArOFPhaseFill.cxx:20
SG::auxid_t
size_t auxid_t
Identifier for a particular aux data item.
Definition: AuxTypes.h:27
SG::AuxTypeRegistry::getType
const std::type_info * getType(SG::auxid_t auxid) const
Return the type of an aux data item.
Definition: AuxTypeRegistry.cxx:909
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
FortranAlgorithmOptions.fileName
fileName
Definition: FortranAlgorithmOptions.py:13
Amg::transform
Amg::Vector3D transform(Amg::Vector3D &v, Amg::Transform3D &tr)
Transform a point from a Trasformation3D.
Definition: GeoPrimitivesHelpers.h:156
MuonSegmentReaderConfig.histSvc
histSvc
Definition: MuonSegmentReaderConfig.py:96
python.AtlRunQueryLib.options
options
Definition: AtlRunQueryLib.py:379
SG::AuxElement::getAuxIDs
const SG::auxid_set_t & getAuxIDs() const
Return a set of identifiers for existing data items for this object.
Definition: AuxElement.cxx:335
python.CreateTierZeroArgdict.outputs
outputs
Definition: CreateTierZeroArgdict.py:189
merge.output
output
Definition: merge.py:17
AthAnalysisHelper::dumpProperties
static void dumpProperties(const std::string &client="")
Definition: AthAnalysisHelper.h:367
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
AthAnalysisHelper::dumpJobOptionProperties
static void dumpJobOptionProperties(const std::string &client="")
Dump the properties from joboptionsvc of clients with names beginning with given string.
Definition: AthAnalysisHelper.cxx:54
AthAnalysisHelper::UNDEFINED
static const std::string UNDEFINED
Definition: AthAnalysisHelper.h:42
AthAnalysisHelper.h
AthAnalysisHelper::initGaudi
static IAppMgrUI * initGaudi(const char *options="")
initGaudi method starts the gaudi ApplicationMgr ready for working with all the components
Definition: AthAnalysisHelper.cxx:21
AthenaPoolExample_Copy.streamName
string streamName
Definition: AthenaPoolExample_Copy.py:39
AthAnalysisHelper::printAuxElement
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...
Definition: AthAnalysisHelper.cxx:69
AuxTypeRegistry.h
Handle mappings between names and auxid_t.
AthAnalysisHelper::getProperty
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 ...
Definition: AthAnalysisHelper.cxx:63
SG::auxid_set_t
A set of aux data identifiers.
Definition: AuxTypes.h:47
python.AppMgr.theApp
theApp
Definition: AppMgr.py:771
python.compressB64.c
def c
Definition: compressB64.py:93
AthAnalysisHelper::AthAnalysisHelper
AthAnalysisHelper()
Definition: AthAnalysisHelper.cxx:18
ServiceHandle< IToolSvc >