ATLAS Offline Software
initGaudi.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 /***************************************************************************
6  minimal gaudi initialization for AthenaServices unit testing
7  ------------------------------------------------------------
8  ATLAS Collaboration
9  ***************************************************************************/
10 
11 #include "TestTools/initGaudi.h"
12 
13 #include "GaudiKernel/Bootstrap.h"
14 #include "GaudiKernel/IProperty.h"
15 #include "GaudiKernel/ISvcManager.h"
16 #include "GaudiKernel/ISvcLocator.h"
17 #include "GaudiKernel/IAppMgrUI.h"
18 #include "GaudiKernel/SmartIF.h"
19 #include "GaudiKernel/ModuleIncident.h"
20 using std::cout;
21 using std::endl;
22 using std::string;
23 
24 namespace Athena_test {
25  bool initGaudi(ISvcLocator*& pSvcLoc) {
26  return initGaudi(string(), pSvcLoc); //wily hack
27  }
28  bool initGaudi(const std::string& jobOptsFile, ISvcLocator*& pSvcLoc) {
29  string jobOptsPath = jobOptsFile;
30  if (access (jobOptsPath.c_str(), R_OK) != 0)
31  jobOptsPath = "../share/"+jobOptsFile;
32  if (access (jobOptsPath.c_str(), R_OK) != 0) {
33  const char* jopath = getenv ("JOBOPTSEARCHPATH");
34  if (jopath) {
35  char* savepath = new char[strlen(jopath)+1];
36  strcpy (savepath, jopath);
37  char* saveptr = nullptr;
38  char* str = savepath;
39  while (char* tok = strtok_r (str, ":", &saveptr)) {
40  str = nullptr;
41  jobOptsPath = std::string(tok) + "/" + jobOptsFile;
42  if (access (jobOptsPath.c_str(), R_OK) == 0) {
43  break;
44  }
45  jobOptsPath.clear();
46  }
47  delete [] savepath;
48  }
49  }
50 
51  if (jobOptsPath.empty()) {
52  cout << "\n\nCannot find job opts " << jobOptsFile << endl;
53  }
54  else {
55  cout << "\n\nInitializing Gaudi ApplicationMgr using job opts " << jobOptsPath << endl;
56  }
57 
58  // Create an instance of an application manager
59  IInterface* iface = Gaudi::createApplicationMgr();
60  if( 0 == iface ) {
61  cout << "Fatal error while creating the ApplicationMgr " << endl;
62  return false;
63  }
64 
65  SmartIF<ISvcManager> svcMgr(iface);
66  SmartIF<IAppMgrUI> appMgr(iface);
67  SmartIF<IProperty> propMgr(iface);
68  SmartIF<ISvcLocator> svcLoc(iface);
69  if(!svcLoc.isValid() || !appMgr.isValid() || !svcMgr.isValid() || !propMgr.isValid()) {
70  cout << "Fatal error while creating the AppMgr smart if " << endl;
71  return false;
72  }
73 
74  pSvcLoc = svcLoc.pRef();
75 
76  propMgr->setProperty( "EvtSel", "NONE" ).
77  orThrow("Cannnot set EvtSel property", "initGaudi");
78  if (jobOptsFile.empty()) {
79  propMgr->setProperty( "JobOptionsType", "NONE" ).
80  orThrow("Cannnot set JobOptionsType property", "initGaudi");
81  } else {
82  propMgr->setProperty( "JobOptionsType", "FILE" ).
83  orThrow("Cannnot set JobOptionsType property", "initGaudi");
84  propMgr->setProperty( "JobOptionsPath", jobOptsPath ).
85  orThrow("Cannnot set JobOptionsPath property", "initGaudi");
86  }
87 
88  if ((appMgr->configure()).isSuccess() &&
89  (appMgr->initialize()).isSuccess()) {
90  cout<<"ApplicationMgr Ready"<<endl;
91  return true;
92  } else {
93  cout << "Fatal error while initializing the AppMgr" << endl;
94  return false;
95  }
96  }
97 
98 
99  // This is for the benefit of the tests in CLIDComps.
100  // By default, a dynamic symbol is not exported from a binary unless
101  // it's referenced by another library with which we link.
102  // A test in CLIDComps creates a ModuleLoadedIncident object in the binary
103  // and then passes it to code in CLIDComps. If ubsan is running,
104  // then it will try to verify that this object derives from ModuleIncident.
105  // But if the ModuleIncident type_info is not exported from the binary,
106  // then the type_info used by the library will be different from that
107  // in the binary, causing an ubsan warning.
108  // But this test gets linked against TestTools, so we can avoid the warning
109  // by referencing the type information here.
110  // (An alternative would be to link the binary with --export-dynamic,
111  // but that's not portable, so we don't want to put it in a package cmake.)
113  {
114  ModuleLoadedIncident inc2 ("", "");
115  }
116 }
117 
118 
StdJOSetup.svcMgr
svcMgr
Definition: StdJOSetup.py:33
Athena_test
functions & macros to test the difference between floats
Definition: InitGaudiGoogleTest.h:30
initGaudi.h
minimal gaudi initialization for AthenaServices unit testing
python.dummyaccess.access
def access(filename, mode)
Definition: dummyaccess.py:18
Athena_test::initGaudi
bool initGaudi(ISvcLocator *&pSvcLoc)
minimal gaudi initialization for AthenaServices unit testing
Definition: initGaudi.cxx:25
SCT_ConditionsAlgorithms::CoveritySafe::getenv
std::string getenv(const std::string &variableName)
get an environment variable
Definition: SCT_ConditionsUtilities.cxx:17
Athena_test::referenceGaudiSymbols
void referenceGaudiSymbols()
Definition: initGaudi.cxx:112
str
Definition: BTagTrackIpAccessor.cxx:11