123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- /**
- * @file
- * @author Caleb Fangmeier <caleb@fangmeier.tech>
- * @version 0.1
- *
- * @section LICENSE
- *
- *
- * MIT License
- *
- * Copyright (c) 2017 Caleb Fangmeier
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- *
- */
- #ifndef config_hpp
- #define config_hpp
- #include <cstdlib>
- #include <cstdio>
- #include <string>
- #include <fstream>
- #include <sstream>
- #include <regex>
- #include "yaml-cpp/yaml.h"
- namespace fv_util {
- struct DataFileDescriptor {
- std::string filename;
- std::string label;
- std::string category;
- int file_number;
- int n_events;
- DataFileDescriptor() : filename(""), label(""), category(""), file_number(-1), n_events(-1) {}
- DataFileDescriptor(const std::string filename, const std::string label, const std::string category,
- const int n_events)
- : filename(filename), label(label), category(category), file_number(-1), n_events(n_events) {}
- DataFileDescriptor(const std::string filename, const std::string label, const std::string category,
- const int file_number, const int n_events)
- : filename(filename), label(label), category(category), file_number(file_number), n_events(n_events) {}
- };
- }
- namespace YAML {
- template<>
- struct convert<fv_util::DataFileDescriptor> {
- static Node encode(const fv_util::DataFileDescriptor &rhs) {
- Node node;
- node["filename"] = rhs.filename;
- node["label"] = rhs.label;
- node["category"] = rhs.category;
- node["n_events"] = rhs.n_events;
- return node;
- }
- static bool decode(const Node &node, fv_util::DataFileDescriptor &rhs) {
- if (!node.IsMap()) {
- return false;
- }
- if (!node["filename"]) return false;
- rhs.filename = node["filename"].as<std::string>();
- if (node["label"]) rhs.label = node["label"].as<std::string>();
- if (node["category"]) rhs.category = node["category"].as<std::string>();
- if (node["n_events"]) rhs.n_events = node["n_events"].as<int>();
- return true;
- }
- };
- }
- namespace fv_util {
- std::vector<std::string> glob(const std::string& base) {
- std::stringstream ss;
- ss << "ls -1 " << base << " > __tmp__";
- system(ss.str().c_str());
- std::vector<std::string> filenames;
- std::ifstream f("__tmp__");
- std::string line;
- while (std::getline(f, line)) {
- filenames.push_back(line);
- }
- std::remove("__tmp__");
- return filenames;
- }
- class Config {
- private:
- std::string input_filename;
- YAML::Node root;
- public:
- Config()
- : root(), input_filename("") {}
- Config(const Config &c)
- : root(c.root), input_filename(c.input_filename) {}
- Config(const std::string &input_filename)
- : root(YAML::LoadFile(input_filename)),
- input_filename(input_filename) {}
- template<typename KEY_TYPE>
- YAML::Node get(const KEY_TYPE &key) {
- auto val = root[key];
- return val;
- }
- template<typename T>
- void update_key(const std::string &key, const T &val) {
- root[key] = val;
- }
- std::string as_string() {
- std::stringstream config;
- std::ifstream s(input_filename);
- for (std::string line; std::getline(s, line);) {
- config << line << std::endl;
- }
- return config.str();
- }
- size_t get_max_events() {
- YAML::Node max_events = root["max-events"];
- if (!max_events) {
- return 0;
- }
- return max_events.as<size_t>();
- }
- bool get_debug() {
- YAML::Node debug_node = root["debug"];
- if (!debug_node) {
- return false;
- }
- return debug_node.as<bool>();
- }
- std::string get_output_filename() {
- YAML::Node output_file = root["output-file"];
- if (output_file) {
- return output_file.as<std::string>();
- } else {
- YAML::Node key = root["source-file-key"];
- if (key) {
- return "../hists/" + key.as<std::string>() + ".root";
- } else {
- return "../hists/output.root";
- }
- }
- }
- std::vector<DataFileDescriptor> get_source_files() {
- std::vector<DataFileDescriptor> source_files;
- std::string source_file_key = "source-files";
- if (root["source-file-key"]) {
- source_file_key = root["source-file-key"].as<std::string>();
- }
- YAML::Node source_files_nd = root[source_file_key];
- if (source_files_nd.IsMap()) {
- auto filenames = glob(source_files_nd["files"].as<std::string>());
- DataFileDescriptor dfd;
- for (const auto& filename : filenames) {
- dfd.filename = filename;
- source_files.push_back(dfd);
- }
- } else if (source_files_nd.IsSequence()) {
- for (const auto &f : source_files_nd) {
- source_files.push_back(f.as<DataFileDescriptor>());
- }
- } else {
- std::cout << "source-files missing or not a sequence" << std::endl;
- }
- return source_files;
- }
- };
- Config *the_config = nullptr;
- bool debug_on = false;
- void init_config(const std::string &input_filename) {
- if (the_config) delete the_config;
- the_config = new Config(input_filename);
- debug_on = the_config->get_debug();
- }
- }
- #endif // config_hpp
|