Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-27 09:14:57

0001 // lAger: General Purpose l/A-event Generator
0002 // Copyright (C) 2016-2021 Sylvester Joosten <sjoosten@anl.gov>
0003 // 
0004 // This file is part of lAger.
0005 // 
0006 // lAger is free software: you can redistribute it and/or modify
0007 // it under the terms of the GNU General Public License as published by
0008 // the Free Shoftware Foundation, either version 3 of the License, or
0009 // (at your option) any later version.
0010 // 
0011 // lAger is distributed in the hope that it will be useful,
0012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
0013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014 // GNU General Public License for more details.
0015 // 
0016 // You should have received a copy of the GNU General Public License
0017 // along with lAger.  If not, see <https://www.gnu.org/licenses/>.
0018 // 
0019 
0020 #ifndef LAGER_FRAMEWORK_LOADED
0021 #define LAGER_FRAMEWORK_LOADED
0022 
0023 #include <functional>
0024 #include <string>
0025 #include <vector>
0026 
0027 #include <lager/core/configuration.hh>
0028 #include <lager/core/logo.hh>
0029 
0030 #include <boost/program_options.hpp>
0031 
0032 namespace lager {
0033 // error prototypes
0034 class framework_error;
0035 class framework_help;
0036 class framework_file_error;
0037 class framework_parse_error;
0038 } // ns lager
0039 
0040 // =============================================================================
0041 // analyser::framework
0042 //
0043 // If you want to use the framework, call MAKE_LAGER_FRAMEWORK(function) at
0044 // the end of your main source file. "function" is the analysis function you
0045 // want to call. The analysis function should take the following inputs:
0046 //    * conf: configuration object made from the input configuration file
0047 //    * output: the base name for the output files.
0048 //  Note:
0049 //    * the framework takes care of all fancy exception handling
0050 // =============================================================================
0051 #define MAKE_LAGER_FRAMEWORK(function)                                         \
0052   int main(int argc, char* argv[]) {                                           \
0053     try {                                                                      \
0054       lager::print_logo();                                                     \
0055       lager::framework lager{argc, argv, (function)};                          \
0056       lager.run();                                                             \
0057       return 0;                                                                \
0058     } catch (...) {                                                            \
0059       return 1;                                                                \
0060     }                                                                          \
0061   }
0062 
0063 namespace lager {
0064 class framework {
0065 public:
0066   using lager_function_type =
0067       std::function<int(const configuration& conf, const std::string& output)>;
0068 
0069   // setup the analysis framework
0070   framework(int argc, char* argv[], lager_function_type lager_function);
0071   ~framework();
0072   // run the analyzis framework
0073   int run() const;
0074 
0075 private:
0076   boost::program_options::variables_map parse_arguments(int argc,
0077                                                         char* argv[]) const;
0078 
0079   // get our settings ptree used to initialize the configuration
0080   ptree get_settings() const;
0081 
0082   // suppress the ROOT signal handler
0083   int root_suppress_signals() const;
0084 
0085   // get an option from the command line, use the configuration file as fallback
0086   template <class T> T get_option(const std::string& key);
0087 
0088   int dummy_; // dummy used to ensure root_suppress_signals is run before
0089               // everything else
0090   boost::program_options::variables_map args_;
0091   configuration conf_;
0092   std::string output_;
0093   std::ofstream log_file_;
0094   lager_function_type lager_function_;
0095 };
0096 } // ns lager
0097 
0098 // =============================================================================
0099 // Definition: exceptions
0100 // =============================================================================
0101 namespace lager {
0102 class framework_error : public lager::exception {
0103 public:
0104   framework_error(const std::string& msg,
0105                   const std::string& type = "framework_error")
0106       : lager::exception{msg, type} {}
0107 };
0108 class framework_help : public framework_error {
0109 public:
0110   framework_help(const std::string& program,
0111                  const boost::program_options::options_description& opts);
0112 
0113 private:
0114   std::string
0115   message(const std::string& program,
0116           const boost::program_options::options_description& opts) const;
0117 };
0118 class framework_file_error : public framework_error {
0119 public:
0120   framework_file_error(const std::string& file);
0121 };
0122 class framework_parse_error : public framework_error {
0123 public:
0124   framework_parse_error(const std::string& file);
0125 };
0126 } // ns lager
0127 
0128 // =============================================================================
0129 // Implementation: framework
0130 // =============================================================================
0131 namespace lager {
0132 // get an option from the command line, use the configuration file as fallback
0133 template <class T> T framework::get_option(const std::string& key) {
0134   T val = 0;
0135   auto val_opt = conf_.get_optional<T>(key);
0136   if (args_.count(key)) {
0137     val = args_[key].as<T>();
0138     conf_.set(key, val);
0139   } else if (val_opt) {
0140     val = *val_opt;
0141   } else {
0142     throw framework_error(
0143         "Ensure that '" + key +
0144         "' is set on the command line or in the configuration file");
0145   }
0146   return val;
0147 }
0148 } // ns lager
0149 
0150 #endif