Browse Source

Introducese yaml based configuration file

Caleb Fangmeier 6 years ago
parent
commit
0bd6e917d0
6 changed files with 112 additions and 3 deletions
  1. 3 0
      .gitmodules
  2. 68 0
      config.hpp
  3. 2 3
      dataset.hpp
  4. 1 0
      filval.hpp
  5. 37 0
      root/container.hpp
  6. 1 0
      yaml-cpp

+ 3 - 0
.gitmodules

@@ -0,0 +1,3 @@
+[submodule "yaml-cpp"]
+	path = yaml-cpp
+	url = https://github.com/jbeder/yaml-cpp.git

+ 68 - 0
config.hpp

@@ -0,0 +1,68 @@
+/**
+ * @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 <string>
+#include <iostream>
+
+#include "yaml-cpp/yaml.h"
+
+namespace fv::util{
+
+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;
+        }
+};
+
+Config* the_config = nullptr;
+void init_config(const std::string& input_filename){
+    if(the_config) delete the_config;
+    the_config = new Config(input_filename);
+}
+
+}
+#endif // config_hpp

+ 2 - 3
dataset.hpp

@@ -38,8 +38,6 @@
 
 namespace fv{
 
-typedef std::map<std::string, GenContainer*> ContainerSet;
-
 /*
  * A DataSet is a generic source of data that is used to populate
  * ObservedValues. For each ObservedValue, it is recommened that the DataSet
@@ -59,7 +57,7 @@ class DataSet{
         int max_events;
 
     protected:
-        ContainerSet containers;
+        std::map<std::string, GenContainer*> containers;
         virtual bool load_next() = 0;
         virtual int get_events() = 0;
         virtual int get_current_event() = 0;
@@ -101,6 +99,7 @@ class DataSet{
                     << " of file: " << get_current_file().filename << std::flush;
                 GenValue::reset();
                 for(auto con : containers){
+                    DEBUG("Filling container " << con.first << ".");
                     con.second->fill();
                 }
                 if(max_events && current_event+1 >= max_events) break;

+ 1 - 0
filval.hpp

@@ -8,6 +8,7 @@
 #include "dataset.hpp"
 #include "log.hpp"
 #include "argparse.hpp"
+#include "config.hpp"
 #include "datafile.hpp"
 #include "api.hpp"
 #endif // filval_hpp

+ 37 - 0
root/container.hpp

@@ -90,6 +90,23 @@ struct TH1Params{
     double low;
     double high;
     std::string label_y;
+
+    static TH1Params lookup(const std::string&& param_key){
+        auto hist_params = fv::util::the_config->get("hist-params");
+        if(!hist_params[param_key]){
+            CRITICAL("Key \"" << param_key << "\" does not exist under hist-params in supplied config file. Add it!", true);
+        }
+        else{
+            auto params = hist_params[param_key];
+            return TH1Params({params["label_x"].as<std::string>(),
+                              params["nbins"].as<int>(),
+                              params["low"].as<double>(),
+                              params["high"].as<double>(),
+                              params["label_y"].as<std::string>()
+                              });
+
+        }
+    }
 };
 
 template <typename V>
@@ -162,6 +179,26 @@ struct TH2Params{
     int nbins_y;
     double low_y;
     double high_y;
+
+    static TH2Params lookup(const std::string&& param_key){
+        auto hist_params = fv::util::the_config->get("hist-params");
+        if(!hist_params[param_key]){
+            CRITICAL("Key \"" << param_key << "\" does not exist under hist-params in supplied config file. Add it!", true);
+        }
+        else{
+            auto params = hist_params[param_key];
+            return TH2Params({params["label_x"].as<std::string>(),
+                              params["nbins_x"].as<int>(),
+                              params["low_x"].as<double>(),
+                              params["high_x"].as<double>(),
+                              params["label_y"].as<std::string>(),
+                              params["nbins_y"].as<int>(),
+                              params["low_y"].as<double>(),
+                              params["high_y"].as<double>()
+                              });
+
+        }
+    }
 };
 
 template <typename V>

+ 1 - 0
yaml-cpp

@@ -0,0 +1 @@
+Subproject commit beb44b872c07c74556314e730c6f20a00b32e8e5