cepgen is hosted by Hepforge, IPPP Durham
CepGen 1.2.5
Central exclusive processes event generator
Loading...
Searching...
No Matches
GlobalFunctions.cpp
Go to the documentation of this file.
1/*
2 * CepGen: a central exclusive processes event generator
3 * Copyright (C) 2013-2023 Laurent Forthomme
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include <atomic>
20
22#include "CepGen/Generator.h"
24#include "CepGen/Physics/PDG.h"
28#include "CepGen/Utils/String.h"
29#include "CepGen/Version.h"
30
31#ifdef _WIN32
32#include <libloaderapi.h>
33#else
34#include <dlfcn.h>
35#endif
36
37namespace cepgen {
38 namespace utils {
39 std::atomic<int> gSignal;
40 } // namespace utils
41
42 bool loadLibrary(const std::string& path, bool match) {
44 return true;
45#ifdef _WIN32
46 const auto fullpath = match ? path + ".dll" : path;
47#elif defined(__APPLE__)
48 const auto fullpath = match ? "lib" + path + ".dylib" : path;
49#else
50 const auto fullpath = match ? "lib" + path + ".so" : path;
51#endif
52 if (callPath(fullpath, [](const auto& path) {
53#ifdef _WIN32
54 if (LoadLibraryA(path.c_str()) != nullptr)
55 return true;
56 CG_WARNING("loadLibrary") << "Failed to load library \"" << path << "\".\n\t"
57 << "Error code #" << GetLastError() << ".";
58 return false;
59#else
60 if (dlopen(path.c_str(), RTLD_LAZY | RTLD_GLOBAL) != nullptr)
61 return true;
62 const char* err = dlerror();
63 CG_WARNING("loadLibrary") << "Failed to load library " << path << "."
64 << (err != nullptr ? utils::format("\n\t%s", err) : "");
65 return false;
66#endif
67 })) {
68 CG_DEBUG("loadLibrary") << "Loaded library \"" << path << "\".";
69 loaded_libraries.emplace_back(path);
70 return true;
71 }
72 invalid_libraries.emplace_back(path);
73 CG_DEBUG("loadLibrary") << "Library \"" << path << "\" (" << fullpath << ") does not exist.";
74 return false;
75 }
76
77 bool callPath(const std::string& local_path, bool (*callback)(const std::string&)) {
78 if (search_paths.empty()) {
79 CG_WARNING("callPath") << "List of search paths is empty.";
80 return false;
81 }
82 for (const auto& search_path : search_paths) {
83 fs::path the_path{search_path};
84 the_path /= local_path;
85 if (utils::fileExists(the_path))
86 return callback(the_path);
87 }
88 return false;
89 }
90
91 void initialise(bool safe_mode) {
92 //--- parse all particles properties
93 static const std::string pdg_file = "";
95 CG_DEBUG("initialise") << utils::s("Search path", search_paths.size(), false) << ": " << search_paths << ".";
96
97 //--- header message
98 try {
100 } catch (const Exception& e) {
101 e.dump();
102 }
103
104 //--- particles table parsing
105 std::string addons_file;
106 if (!callPath("mass_width_2023.txt", [](const auto& path) {
108 return true;
109 }))
110 CG_WARNING("init") << "No particles definition file found.";
111 if (PDG::get().size() < 10)
112 CG_WARNING("init") << "Only " << utils::s("particle", PDG::get().size(), true)
113 << " are defined in the runtime environment.\n\t"
114 << "Make sure the path to the MCD file is correct.";
115
116 for (const auto& path : search_paths) {
117 const fs::path the_path{path};
118 if (addons_file.empty() && utils::fileExists(the_path / "CepGenAddOns.txt"))
119 addons_file = the_path / "CepGenAddOns.txt";
120 utils::env::append("LD_LIBRARY_PATH", path);
121 }
122
123 //--- load all necessary modules
124 if (!safe_mode && !addons_file.empty())
125 for (const auto& lib : utils::split(utils::readFile(addons_file), '\n'))
126 loadLibrary(lib, true);
127 loadLibrary("CepGenProcesses", true);
128 if (!invalid_libraries.empty())
129 CG_WARNING("init") << "Failed to load the following libraries:\n\t" << invalid_libraries << ".";
130
131 //--- greeting message
132 CG_INFO("init").log([&](auto& log) {
133 log << "CepGen " << version::tag << " (" << version::extended << ") "
134 << "initialised";
135 if (!loaded_libraries.empty())
136 log << " with " << utils::s("add-on", loaded_libraries.size(), true) << ":\n\t" << loaded_libraries << ".\n\t";
137 else
138 log << ". ";
139 log << "Greetings!";
140 });
141 }
142
143 void printHeader() {
144 if (!callPath("README", [](const auto& path) {
145 if (!utils::fileExists(path))
146 return false;
147 CG_LOG << utils::readFile(path);
148 return true;
149 }))
150 CG_WARNING("printHeader") << "Failed to open README file.";
151 }
152} // namespace cepgen
#define CG_WARNING(mod)
Definition Message.h:228
#define CG_LOG
Definition Message.h:212
#define CG_DEBUG(mod)
Definition Message.h:220
#define CG_INFO(mod)
Definition Message.h:216
void dump(std::ostream *os=nullptr) const noexcept override
Human-readable dump of the exception.
Definition Exception.cpp:41
static PDG & get()
Retrieve a unique instance of this particles info collection.
Definition PDG.cpp:41
static void parse(const std::string &path)
Parse an external MCD file and retrieve all particles definition.
std::vector< std::string > searchPaths()
Retrieve a list of all search paths for external files.
void append(const std::string &var, const std::string &value)
Add a value to an environment variable.
std::string format(const std::string &fmt, Args... args)
Format a string using a printf style format descriptor.
Definition String.h:61
std::string s(const std::string &word, float num, bool show_number)
Add a trailing "s" when needed.
Definition String.cpp:228
std::string readFile(const std::string &filename)
Read the content of a file into a string buffer.
bool contains(const std::vector< T > &coll, const T &item)
Check if a vector contains an item.
Definition Collections.h:47
bool fileExists(const std::string &path)
Check if the file exists.
std::atomic< int > gSignal
Abort signal handler.
std::vector< std::string > split(const std::string &str, char delim, bool trim)
Split a string according to a separation character.
Definition String.cpp:233
Common namespace for this Monte Carlo generator.
static std::vector< std::string > invalid_libraries
Collection of libraries tested not to work with RTE.
Definition Generator.h:35
bool loadLibrary(const std::string &path, bool match)
Import a shared library in RTE Launch the initialisation procedure.
void printHeader()
Dump this program's header into the standard output stream.
void initialise(bool safe_mode)
static std::vector< std::string > search_paths
Collection of search paths to build RTE Execute an action on a path if found in search paths collecti...
Definition Generator.h:36
static std::vector< std::string > loaded_libraries
Collection of libraries loaded in RTE.
Definition Generator.h:34
bool callPath(const std::string &local_path, bool(*callback)(const std::string &))
static const std::string tag
CepGen version.
Definition Version.h:28
static const std::string extended
CepGen detailed version.
Definition Version.h:30