Belle II Software  release-08-01-10
Environment.cc
1 /**************************************************************************
2  * basf2 (Belle II Analysis Software Framework) *
3  * Author: The Belle II Collaboration *
4  * *
5  * See git log for contributors and copyright holders. *
6  * This file is licensed under LGPL-3.0, see LICENSE.md. *
7  **************************************************************************/
8 
9 #include <framework/core/Module.h>
10 #include <framework/core/Path.h>
11 #include <framework/core/Environment.h>
12 #include <framework/core/ModuleManager.h>
13 #include <framework/logging/LogConfig.h>
14 #include <framework/core/InputController.h>
15 #include <framework/datastore/StoreObjPtr.h>
16 #include <framework/dataobjects/FileMetaData.h>
17 
18 #include <memory>
19 #include <iostream>
20 #include <cstdlib>
21 #include <filesystem>
22 
23 using namespace Belle2;
24 using namespace std;
25 namespace fs = std::filesystem;
26 
27 
29 {
30  static Environment instance;
31  return instance;
32 }
33 
34 const list<string>& Environment::getModuleSearchPaths() const
35 {
37 }
38 
39 unsigned int Environment::getNumberOfEvents() const
40 {
41  // for EventInfoSetter, -n is already taken into account
42  if (m_mcEvents != 0)
43  return m_mcEvents;
44 
45  unsigned int numEventsFromInput = InputController::getNumEntriesToProcess();
46  unsigned int numEventsFromArgument = getNumberEventsOverride();
47  if (numEventsFromArgument != 0
48  && (numEventsFromInput == 0 || numEventsFromArgument < numEventsFromInput))
49  return numEventsFromArgument;
50  else
51  return numEventsFromInput;
52 }
53 
54 bool Environment::isMC() const
55 {
57  if (fileMetaData) return fileMetaData->isMC();
58  return true;
59 }
60 
62 {
63  m_realm = realm;
64  B2INFO("Execution realm: " << LogConfig::logRealmToString(realm));
65 }
66 
67 std::string Environment::consumeOutputFileOverride(const std::string& module)
68 {
69  std::string s{""};
70  if (!m_outputFileOverrideModule.empty()) {
71  B2WARNING("Module '" << module << "' requested to handle -o which has already been handled by '" << module << "', ignoring");
72  return s;
73  }
74  if (!m_outputFileOverride.empty()) {
75  m_outputFileOverrideModule = module;
76  std::swap(s, m_outputFileOverride);
77  }
78  return s;
79 }
80 
81 //============================================================================
82 // Private methods
83 //============================================================================
84 
86  m_numberProcesses(0),
87  m_steering(""),
88  m_numberEventsOverride(0),
89  m_inputFilesOverride(),
90  m_secondaryInputFilesOverride(),
91  m_entrySequencesOverride(),
92  m_outputFileOverride(""),
93  m_numberProcessesOverride(-1),
94  m_logLevelOverride(LogConfig::c_Default),
95  m_visualizeDataFlow(false),
96  m_noStats(false),
97  m_dryRun(false),
98  m_mcEvents(0),
99  m_run(-1),
100  m_experiment(-1),
101  m_skipNEvents(0)
102 {
103  // Check for environment variables set by setuprel
104  const char* envarReleaseDir = getenv("BELLE2_RELEASE_DIR");
105  const char* envarLocalDir = getenv("BELLE2_LOCAL_DIR");
106  const char* envarAnalysisDir = getenv("BELLE2_ANALYSIS_DIR");
107  if (!envarReleaseDir and !envarLocalDir) {
108  B2FATAL("The basf2 environment is not set up. Please execute the 'setuprel' script first.");
109  }
110 
111  //also set when just sourcing setup_belle2.sh (which is why we also check for local/release dir)
112  const char* envarSubDir = getenv("BELLE2_SUBDIR");
113  if (!envarSubDir) {
114  B2FATAL("The environment variable BELLE2_SUBDIR is not set. Please execute the 'setuprel' script first.");
115  }
116 
117  const char* envarExtDir = getenv("BELLE2_EXTERNALS_DIR");
118  if (!envarExtDir) {
119  B2FATAL("The environment variable BELLE2_EXTERNALS_DIR is not set. Please execute the 'setuprel' script first.");
120  }
121 
122  // add module directories for current build options, starting with the working directory on program startup
123  std::string added_dirs = fs::current_path().string();
125 
126  if (envarAnalysisDir) {
127  const string analysisModules = (fs::path(envarAnalysisDir) / "modules" / envarSubDir).string();
128  ModuleManager::Instance().addModuleSearchPath(analysisModules);
129  added_dirs += " " + analysisModules;
130  }
131 
132  if (envarLocalDir) {
133  const string localModules = (fs::path(envarLocalDir) / "modules" / envarSubDir).string();
135  added_dirs += " " + localModules;
136  }
137 
138  if (envarReleaseDir) {
139  const string centralModules = (fs::path(envarReleaseDir) / "modules" / envarSubDir).string();
141  added_dirs += " " + centralModules;
142  }
143 
144  if (ModuleManager::Instance().getAvailableModules().empty()) {
145  B2ERROR("No modules found! Did you forget to run 'scons'? Module paths added: " << added_dirs);
146  }
147 
148  //set path to external software
149  setExternalsPath(envarExtDir);
150 }
151 
152 Environment::~Environment() = default;
153 
154 
155 // we know getFileNames is deprecated but we need it as long as --dry-run is available
156 // so let's remove the warning for now ...
157 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
158 
159 void Environment::setJobInformation(const std::shared_ptr<Path>& path)
160 {
161  const std::list<ModulePtr>& modules = path->buildModulePathList(true);
162 
163  for (const ModulePtr& m : modules) {
164  if (m->hasProperties(Module::c_Input)) {
165  std::vector<std::string> inputFiles = m->getFileNames(false);
166  for (const string& file : inputFiles) {
167  m_jobInfoOutput += "INPUT FILE: " + file + "\n";
168  }
169  }
170  if (m->hasProperties(Module::c_Output)) {
171  std::vector<std::string> outputFiles = m->getFileNames(true);
172  for (const string& file : outputFiles) {
173  m_jobInfoOutput += "OUTPUT FILE: " + file + "\n";
174  }
175  }
176  }
177 }
178 
180 {
181  cout << m_jobInfoOutput;
182 }
@ c_Persistent
Object is available during entire execution time.
Definition: DataStore.h:60
This class stores all environment information required to run the framework, such as module or data f...
Definition: Environment.h:32
Environment()
Set up environment from standard BELLE2_ environment variables.
Definition: Environment.cc:85
std::string consumeOutputFileOverride(const std::string &moduleName)
Return overriden output file name, or "" if none was set.
Definition: Environment.cc:67
const std::list< std::string > & getModuleSearchPaths() const
Returns a list of file paths searched for module libraries.
Definition: Environment.cc:34
unsigned int getNumberOfEvents() const
Return the number of events, from either input or EventInfoSetter, or -n command line override (if le...
Definition: Environment.cc:39
bool isMC() const
Do we have generated, not real data?
Definition: Environment.cc:54
~Environment()
The Environment destructor.
std::string m_jobInfoOutput
Output for printJobInformation(), generated by setJobInformation().
Definition: Environment.h:386
void setRealm(LogConfig::ELogRealm realm)
Set the basf2 execution realm.
Definition: Environment.cc:61
void setJobInformation(const std::shared_ptr< Path > &path)
Set info from path executed by the framework.
Definition: Environment.cc:159
void setExternalsPath(const std::string &externalsPath)
Sets the path which points to the externals directory of the framework.
Definition: Environment.h:53
void printJobInformation() const
Print information on input/output files in current steering file, used by –dry-run.
Definition: Environment.cc:179
static Environment & Instance()
Static method to get a reference to the Environment instance.
Definition: Environment.cc:28
static long getNumEntriesToProcess()
Necessary to make sure the ProgressModule shows reasonable output.
The LogConfig class.
Definition: LogConfig.h:22
static const char * logRealmToString(ELogRealm realm)
Converts a log realm type to a string.
Definition: LogConfig.cc:49
ELogRealm
Definition of the supported execution realms.
Definition: LogConfig.h:48
const std::list< std::string > & getModuleSearchPaths() const
Returns a reference to the list of the modules search filepaths.
static ModuleManager & Instance()
Exception is thrown if the requested module could not be created by the ModuleManager.
void addModuleSearchPath(const std::string &path)
Adds a new filepath to the list of filepaths which are searched for a requested module.
@ c_Input
This module is an input module (reads data).
Definition: Module.h:78
@ c_Output
This module is an output module (writes data).
Definition: Module.h:79
Type-safe access to single objects in the data store.
Definition: StoreObjPtr.h:96
std::shared_ptr< Module > ModulePtr
Defines a pointer to a module object as a boost shared pointer.
Definition: Module.h:40
Abstract base class for different kinds of events.