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