Belle II Software prerelease-11-00-00a
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
23using namespace Belle2;
24using namespace std;
25namespace fs = std::filesystem;
26
27
29{
30 static Environment instance;
31 return instance;
32}
33
34const list<string>& Environment::getModuleSearchPaths() const
35{
37}
38
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
55{
57 if (fileMetaData) return fileMetaData->isMC();
58 else if (m_mcEvents == 0) return false;
59 return true;
60}
61
63{
64 m_realm = realm;
65 B2INFO("Execution realm: " << LogConfig::logRealmToString(realm));
66}
67
68std::string Environment::consumeOutputFileOverride(const std::string& module)
69{
70 std::string s{""};
71 if (!m_outputFileOverrideModule.empty()) {
72 B2WARNING("Module '" << module << "' requested to handle -o which has already been handled by '" << module << "', ignoring");
73 return s;
74 }
75 if (!m_outputFileOverride.empty()) {
77 std::swap(s, m_outputFileOverride);
78 }
79 return s;
80}
81
82//============================================================================
83// Private methods
84//============================================================================
85
88 m_steering(""),
95 m_logLevelOverride(LogConfig::c_Default),
97 m_stats(false),
98 m_dryRun(false),
99 m_mcEvents(0),
100 m_run(-1),
101 m_experiment(-1),
102 m_runType(Const::c_Beam),
103 m_skipNEvents(0),
104 m_writeSimSteps(false)
105{
106 // Check for environment variables set by b2setup
107 const char* envarReleaseDir = getenv("BELLE2_RELEASE_DIR");
108 const char* envarLocalDir = getenv("BELLE2_LOCAL_DIR");
109 const char* envarAnalysisDir = getenv("BELLE2_ANALYSIS_DIR");
110 if (!envarReleaseDir and !envarLocalDir) {
111 B2FATAL("The basf2 environment is not set up. Please execute the 'b2setup' script first.");
112 }
113
114 //also set when just sourcing setup_belle2.sh (which is why we also check for local/release dir)
115 const char* envarSubDir = getenv("BELLE2_SUBDIR");
116 if (!envarSubDir) {
117 B2FATAL("The environment variable BELLE2_SUBDIR is not set. Please execute the 'b2setup' script first.");
118 }
119
120 const char* envarExtDir = getenv("BELLE2_EXTERNALS_DIR");
121 if (!envarExtDir) {
122 B2FATAL("The environment variable BELLE2_EXTERNALS_DIR is not set. Please execute the 'b2setup' script first.");
123 }
124
125 // add module directories for current build options, starting with the working directory on program startup
126 std::string added_dirs = fs::current_path().string();
128
129 if (envarAnalysisDir) {
130 const string analysisModules = (fs::path(envarAnalysisDir) / "modules" / envarSubDir).string();
132 added_dirs += " " + analysisModules;
133 }
134
135 if (envarLocalDir) {
136 const string localModules = (fs::path(envarLocalDir) / "modules" / envarSubDir).string();
138 added_dirs += " " + localModules;
139 }
140
141 if (envarReleaseDir) {
142 const string centralModules = (fs::path(envarReleaseDir) / "modules" / envarSubDir).string();
144 added_dirs += " " + centralModules;
145 }
146
147 if (ModuleManager::Instance().getAvailableModules().empty()) {
148 B2ERROR("No modules found! Did you forget to run 'scons'? Module paths added: " << added_dirs);
149 }
150
151 //set path to external software
152 setExternalsPath(envarExtDir);
153}
154
155Environment::~Environment() = default;
156
157
158void Environment::setJobInformation(const std::shared_ptr<Path>& path)
159{
160 const std::list<ModulePtr>& modules = path->buildModulePathList(true);
161
162 for (const ModulePtr& m : modules) {
163 if (m->hasProperties(Module::c_Input)) {
164 std::vector<std::string> inputFiles = m->getFileNames(false);
165 for (const string& file : inputFiles) {
166 m_jobInfoOutput += "INPUT FILE: " + file + "\n";
167 }
168 }
169 if (m->hasProperties(Module::c_Output)) {
170 std::vector<std::string> outputFiles = m->getFileNames(true);
171 for (const string& file : outputFiles) {
172 m_jobInfoOutput += "OUTPUT FILE: " + file + "\n";
173 }
174 }
175 }
176}
177
179{
180 cout << m_jobInfoOutput;
181}
This class provides a set of constants for the framework.
Definition Const.h:34
@ c_Persistent
Object is available during entire execution time.
Definition DataStore.h:60
Environment()
Set up environment from standard BELLE2_ environment variables.
std::string m_steering
The content of the steering file.
std::vector< std::string > m_entrySequencesOverride
A number sequence (e.g.
int m_numberProcesses
The number of worker processes that should be used for the parallel processing.
std::string consumeOutputFileOverride(const std::string &moduleName)
Return overridden output file name, or "" if none was set.
const std::list< std::string > & getModuleSearchPaths() const
Returns a list of file paths searched for module libraries.
unsigned int getNumberOfEvents() const
Return the number of events, from either input or EventInfoSetter, or -n command line override (if le...
bool isMC() const
Do we have generated, not real data?
~Environment()
The Environment destructor.
std::string m_jobInfoOutput
Output for printJobInformation(), generated by setJobInformation().
void setRealm(LogConfig::ELogRealm realm)
Set the basf2 execution realm.
void setJobInformation(const std::shared_ptr< Path > &path)
Set info from path executed by the framework.
Const::ERunType m_runType
Flag which tells if the run is beam (default: c_Beam) or cosmic (c_Cosmic).
std::string m_outputFileOverride
Override name of output file for output module.
unsigned int m_mcEvents
counter for number of generated events.
std::vector< std::string > m_inputFilesOverride
Override input file names for input modules.
bool m_writeSimSteps
bool for writing out the simulation steps into a csv file.
void setExternalsPath(const std::string &externalsPath)
Sets the path which points to the externals directory of the framework.
Definition Environment.h:55
bool m_stats
Boolean to enable collection of statistics during event processing.
unsigned int m_numberEventsOverride
Override number of events in the first run.
std::string m_outputFileOverrideModule
Name of the module which consumed the output file Override if any was given.
bool m_visualizeDataFlow
Whether to generate DOT files with data store inputs/outputs of each module.
int m_experiment
override experiment for EventInfoSetter.
int m_logLevelOverride
Override global log level if != LogConfig::c_Default.
int m_numberProcessesOverride
Override m_numberProcesses if >= 0.
void printJobInformation() const
Print information on input/output files in current steering file, used by –dry-run.
LogConfig::ELogRealm m_realm
The realm in which basf2 is executed.
static Environment & Instance()
Static method to get a reference to the Environment instance.
std::vector< std::string > m_secondaryInputFilesOverride
Override secondary input file names for input modules.
bool m_dryRun
Read steering file, but do not start any actually start any event processing.
unsigned int getNumberEventsOverride() const
Returns number of events in run 1 for EventInfoSetter module, or 0 for no override.
Definition Environment.h:68
unsigned int m_skipNEvents
override skipNEvents for EventInfoSetter/RootInput.
int m_run
override run for EventInfoSetter.
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:43
Abstract base class for different kinds of events.
STL namespace.