config.hpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /**
  2. * @file
  3. * @author Caleb Fangmeier <caleb@fangmeier.tech>
  4. * @version 0.1
  5. *
  6. * @section LICENSE
  7. *
  8. *
  9. * MIT License
  10. *
  11. * Copyright (c) 2017 Caleb Fangmeier
  12. *
  13. * Permission is hereby granted, free of charge, to any person obtaining a copy
  14. * of this software and associated documentation files (the "Software"), to deal
  15. * in the Software without restriction, including without limitation the rights
  16. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  17. * copies of the Software, and to permit persons to whom the Software is
  18. * furnished to do so, subject to the following conditions:
  19. *
  20. * The above copyright notice and this permission notice shall be included in all
  21. * copies or substantial portions of the Software.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  26. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  28. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  29. * SOFTWARE.
  30. *
  31. */
  32. #ifndef config_hpp
  33. #define config_hpp
  34. #include <cstdlib>
  35. #include <cstdio>
  36. #include <string>
  37. #include <fstream>
  38. #include <sstream>
  39. #include <regex>
  40. #include <functional>
  41. #include "yaml-cpp/yaml.h"
  42. namespace fv_util {
  43. struct DataFileDescriptor {
  44. std::string filename;
  45. std::string label;
  46. std::string category;
  47. int file_number;
  48. int n_events;
  49. DataFileDescriptor() : filename(""), label(""), category(""), file_number(-1), n_events(-1) {}
  50. DataFileDescriptor(const std::string filename, const std::string label, const std::string category,
  51. const int n_events)
  52. : filename(filename), label(label), category(category), file_number(-1), n_events(n_events) {}
  53. DataFileDescriptor(const std::string filename, const std::string label, const std::string category,
  54. const int file_number, const int n_events)
  55. : filename(filename), label(label), category(category), file_number(file_number), n_events(n_events) {}
  56. };
  57. }
  58. namespace YAML {
  59. template<>
  60. struct convert<fv_util::DataFileDescriptor> {
  61. static Node encode(const fv_util::DataFileDescriptor &rhs) {
  62. Node node;
  63. node["filename"] = rhs.filename;
  64. node["label"] = rhs.label;
  65. node["category"] = rhs.category;
  66. node["n_events"] = rhs.n_events;
  67. return node;
  68. }
  69. static bool decode(const Node &node, fv_util::DataFileDescriptor &rhs) {
  70. if (!node.IsMap()) {
  71. return false;
  72. }
  73. if (!node["filename"]) return false;
  74. rhs.filename = node["filename"].as<std::string>();
  75. if (node["label"]) rhs.label = node["label"].as<std::string>();
  76. if (node["category"]) rhs.category = node["category"].as<std::string>();
  77. if (node["n_events"]) rhs.n_events = node["n_events"].as<int>();
  78. return true;
  79. }
  80. };
  81. }
  82. namespace fv_util {
  83. std::vector<std::string> glob(const std::string& base) {
  84. std::hash<std::string> s_hash;
  85. std::stringstream ss;
  86. std::stringstream tmp_filename;
  87. tmp_filename << "__tmp" << s_hash(base) << "__";
  88. ss << "ls -1 " << base << " > " << tmp_filename.str();
  89. system(ss.str().c_str());
  90. std::vector<std::string> filenames;
  91. std::ifstream f(tmp_filename.str());
  92. std::string line;
  93. while (std::getline(f, line)) {
  94. filenames.push_back(line);
  95. }
  96. std::remove(tmp_filename.str().c_str());
  97. return filenames;
  98. }
  99. class Config {
  100. private:
  101. std::string input_filename;
  102. YAML::Node root;
  103. public:
  104. Config()
  105. : root(), input_filename("") {}
  106. Config(const Config &c)
  107. : root(c.root), input_filename(c.input_filename) {}
  108. Config(const std::string &input_filename)
  109. : root(YAML::LoadFile(input_filename)),
  110. input_filename(input_filename) {}
  111. template<typename KEY_TYPE>
  112. YAML::Node get(const KEY_TYPE &key) {
  113. auto val = root[key];
  114. return val;
  115. }
  116. template<typename T>
  117. void update_key(const std::string &key, const T &val) {
  118. root[key] = val;
  119. }
  120. std::string as_string() {
  121. std::stringstream config;
  122. std::ifstream s(input_filename);
  123. for (std::string line; std::getline(s, line);) {
  124. config << line << std::endl;
  125. }
  126. return config.str();
  127. }
  128. size_t get_max_events() {
  129. YAML::Node max_events = root["max-events"];
  130. if (!max_events) {
  131. return 0;
  132. }
  133. return max_events.as<size_t>();
  134. }
  135. bool get_debug() {
  136. YAML::Node debug_node = root["debug"];
  137. if (!debug_node) {
  138. return false;
  139. }
  140. return debug_node.as<bool>();
  141. }
  142. std::string get_output_filename() {
  143. YAML::Node output_file = root["output-file"];
  144. if (output_file) {
  145. return output_file.as<std::string>();
  146. } else {
  147. YAML::Node key = root["source-file-key"];
  148. if (key) {
  149. return "../hists/" + key.as<std::string>() + ".root";
  150. } else {
  151. return "../hists/output.root";
  152. }
  153. }
  154. }
  155. std::vector<DataFileDescriptor> get_source_files() {
  156. std::vector<DataFileDescriptor> source_files;
  157. std::string source_file_key = "source-files";
  158. if (root["source-file-key"]) {
  159. source_file_key = root["source-file-key"].as<std::string>();
  160. }
  161. YAML::Node source_files_nd = root[source_file_key];
  162. if (source_files_nd.IsMap()) {
  163. auto filenames = glob(source_files_nd["files"].as<std::string>());
  164. DataFileDescriptor dfd;
  165. for (const auto& filename : filenames) {
  166. dfd.filename = filename;
  167. source_files.push_back(dfd);
  168. }
  169. } else if (source_files_nd.IsSequence()) {
  170. for (const auto &f : source_files_nd) {
  171. source_files.push_back(f.as<DataFileDescriptor>());
  172. }
  173. } else {
  174. std::cout << "source-files missing or not a sequence" << std::endl;
  175. }
  176. return source_files;
  177. }
  178. };
  179. Config *the_config = nullptr;
  180. bool debug_on = false;
  181. void init_config(const std::string &input_filename) {
  182. if (the_config) delete the_config;
  183. the_config = new Config(input_filename);
  184. debug_on = the_config->get_debug();
  185. }
  186. }
  187. #endif // config_hpp