diff --git a/apps/examples/histogrammer.cpp b/apps/examples/histogrammer.cpp index d676ff6..cb02eb8 100644 --- a/apps/examples/histogrammer.cpp +++ b/apps/examples/histogrammer.cpp @@ -12,39 +12,11 @@ using namespace std; -void CheckArgs(int argc, char **argv) { - if (argc != 2 && argc != 4 && argc != 6) { - fatal() << "Usage: " << argv[0] << " config_path"<(argc, argv); - if (!args->GetString("config").has_value()) { - fatal() << "No config file provided" << endl; - exit(1); - } - - ConfigManager::Initialize(args->GetString("config").value()); - auto &config = ConfigManager::GetInstance(); - - if (args->GetString("input_path").has_value()) { - config.SetInputPath(args->GetString("input_path").value()); - } - if (args->GetString("output_path").has_value()) { - config.SetHistogramsOutputPath(args->GetString("output_path").value()); - } - if (args->GetString("output_hists_path").has_value()) { - config.SetHistogramsOutputPath(args->GetString("output_hists_path").value()); - } - if (args->GetString("output_trees_path").has_value()) { - config.SetTreesOutputPath(args->GetString("output_trees_path").value()); - } + vector requiredArgs = {"config"}; + vector optionalArgs = {"input_path", "output_hists_path"}; + auto args = make_unique(argc, argv, requiredArgs, optionalArgs); + ConfigManager::Initialize(args); auto eventReader = make_shared(); auto histogramsHandler = make_shared(); diff --git a/apps/examples/skimmer.cpp b/apps/examples/skimmer.cpp index 0b5f04f..cc68611 100644 --- a/apps/examples/skimmer.cpp +++ b/apps/examples/skimmer.cpp @@ -15,22 +15,11 @@ using namespace std; int main(int argc, char **argv) { - auto args = make_unique(argc, argv); - if (!args->GetString("config").has_value()) { - fatal() << "No config file provided" << endl; - exit(1); - } - - ConfigManager::Initialize(args->GetString("config").value()); - auto &config = ConfigManager::GetInstance(); + vector requiredArgs = {"config"}; + vector optionalArgs = {"input_path", "output_trees_path"}; + auto args = make_unique(argc, argv, requiredArgs, optionalArgs); + ConfigManager::Initialize(args); - if (args->GetString("input_path").has_value()) { - config.SetInputPath(args->GetString("input_path").value()); - } - if (args->GetString("output_trees_path").has_value()) { - config.SetTreesOutputPath(args->GetString("output_trees_path").value()); - } - auto eventReader = make_shared(); auto eventWriter = make_shared(eventReader); auto cutFlowManager = make_shared(eventReader, eventWriter); diff --git a/jsonPOG b/jsonPOG index 377439e..ea5e2a9 160000 --- a/jsonPOG +++ b/jsonPOG @@ -1 +1 @@ -Subproject commit 377439e8b0eaa358c49f044698da0f8aa6d32f42 +Subproject commit ea5e2a97c3cba402658e58156b4f076075c1afab diff --git a/libs/core/include/ArgsManager.hpp b/libs/core/include/ArgsManager.hpp index 6a06664..f1a245a 100644 --- a/libs/core/include/ArgsManager.hpp +++ b/libs/core/include/ArgsManager.hpp @@ -1,31 +1,36 @@ +#pragma once + #include "Helpers.hpp" #include "Multitype.hpp" class ArgsManager { -public: - ArgsManager(int argc, char **argv); - ~ArgsManager() {} - - inline std::optional GetString(std::string key) { - if(args.count(key) == 0) return std::nullopt; - return args[key]; - } - - inline std::optional GetInt(std::string key) { - if(args.count(key) == 0) return std::nullopt; - return std::stoi(args[key]); - } - - inline std::optional GetFloat(std::string key) { - if(args.count(key) == 0) return std::nullopt; - return std::stof(args[key]); - } - - inline std::optional GetBool(std::string key) { - if(args.count(key) == 0) return std::nullopt; - return args[key] == "True"; - } - -private: - std::map args; + public: + ArgsManager(int argc, char** argv, std::vector _requiredArgs, std::vector _optionalArgs); + ~ArgsManager() {} + + inline std::optional GetString(std::string key) { + if (args.count(key) == 0) return std::nullopt; + return args[key]; + } + + inline std::optional GetInt(std::string key) { + if (args.count(key) == 0) return std::nullopt; + return std::stoi(args[key]); + } + + inline std::optional GetFloat(std::string key) { + if (args.count(key) == 0) return std::nullopt; + return std::stof(args[key]); + } + + inline std::optional GetBool(std::string key) { + if (args.count(key) == 0) return std::nullopt; + return args[key] == "True"; + } + + private: + void ValidateArgs(); + + std::vector requiredArgs, optionalArgs; + std::map args; }; diff --git a/libs/core/include/ConfigManager.hpp b/libs/core/include/ConfigManager.hpp index dcbf950..81da0ef 100644 --- a/libs/core/include/ConfigManager.hpp +++ b/libs/core/include/ConfigManager.hpp @@ -9,43 +9,63 @@ #include "Helpers.hpp" +#include "ArgsManager.hpp" + class ConfigManager { public: - static ConfigManager& GetInstance(){ return getInstanceImpl(); } + static ConfigManager& GetInstance() { return getInstanceImpl(); } static void Initialize(std::string _configPath) { getInstanceImpl(&_configPath); } + static void Initialize(const std::unique_ptr &args) { + std::string _configPath = args->GetString("config").value(); + getInstanceImpl(&_configPath); + + auto &config = GetInstance(); + + if (args->GetString("input_path").has_value()) { + config.SetInputPath(args->GetString("input_path").value()); + } + + if (args->GetString("output_hists_path").has_value()) { + config.SetHistogramsOutputPath(args->GetString("output_hists_path").value()); + } + + if (args->GetString("output_trees_path").has_value()) { + config.SetTreesOutputPath(args->GetString("output_trees_path").value()); + } + } ConfigManager(ConfigManager const&) = delete; void operator=(ConfigManager const&) = delete; template - void GetValue(std::string name, T &outputValue); + void GetValue(std::string name, T& outputValue); template - void GetVector(std::string name, std::vector &outputVector); + void GetVector(std::string name, std::vector& outputVector); template - void GetMap(std::string name, std::map &outputMap); + void GetMap(std::string name, std::map& outputMap); template - void GetPair(std::string name, std::pair &outputPair); + void GetPair(std::string name, std::pair& outputPair); - void GetExtraEventCollections(insertion_ordered_map &extraEventCollections); - void GetHistogramsParams(std::map &histogramsParams, std::string collectionName); - void GetHistogramsParams(std::map &histogramsParams, std::string collectionName); - void GetHistogramsParams(std::map &histogramsParams, std::string collectionName); - void GetHistogramsParams(std::map &histogramsParams, std::string collectionName); + void GetExtraEventCollections(insertion_ordered_map& extraEventCollections); + void GetHistogramsParams(std::map& histogramsParams, std::string collectionName); + void GetHistogramsParams(std::map& histogramsParams, std::string collectionName); + void GetHistogramsParams(std::map& histogramsParams, std::string collectionName); + void GetHistogramsParams(std::map& histogramsParams, std::string collectionName); - void GetScaleFactors(std::string name, std::map &scaleFactors); - void GetScaleFactors(std::string name, std::map &scaleFactors); + void GetScaleFactors(std::string name, std::map& scaleFactors); + void GetScaleFactors(std::string name, std::map& scaleFactors); - void GetCuts(std::vector>> &cuts); + void GetCuts(std::vector>>& cuts); void SetInputPath(std::string path) { inputPath = path; } void SetTreesOutputPath(std::string path) { treesOutputPath = path; } void SetHistogramsOutputPath(std::string path) { histogramsOutputPath = path; } std::string GetYear(); - + private: std::string configPath; ConfigManager(std::string* const _configPath); @@ -54,16 +74,16 @@ class ConfigManager { void PrintBanner(); - FILE *pythonFile; - PyObject *pythonModule; - PyObject *config; + FILE* pythonFile; + PyObject* pythonModule; + PyObject* config; - PyObject *GetPythonValue(std::string name); - PyObject *GetPythonList(std::string name); - PyObject *GetPythonDict(std::string name); + PyObject* GetPythonValue(std::string name); + PyObject* GetPythonList(std::string name); + PyObject* GetPythonDict(std::string name); - int GetCollectionSize(PyObject *collection); - PyObject *GetItem(PyObject *collection, int index); + int GetCollectionSize(PyObject* collection); + PyObject* GetItem(PyObject* collection, int index); std::string inputPath = ""; std::string treesOutputPath = ""; diff --git a/libs/core/include/EventWriter.hpp b/libs/core/include/EventWriter.hpp index 92f3af5..5f86bd4 100644 --- a/libs/core/include/EventWriter.hpp +++ b/libs/core/include/EventWriter.hpp @@ -24,6 +24,7 @@ class EventWriter { private: TFile *outFile; + std::string outputFilePath; std::map outputTrees; std::shared_ptr eventReader; @@ -31,7 +32,7 @@ class EventWriter { std::vector branchesToKeep; std::vector branchesToRemove; - void SetupOutputTree(std::string outFileName); + void SetupOutputTree(); friend class CutFlowManager; }; diff --git a/libs/core/src/ArgsManager.cpp b/libs/core/src/ArgsManager.cpp index 4e04e70..639926d 100644 --- a/libs/core/src/ArgsManager.cpp +++ b/libs/core/src/ArgsManager.cpp @@ -1,15 +1,37 @@ #include "ArgsManager.hpp" + #include "Logger.hpp" -ArgsManager::ArgsManager(int argc, char **argv) { - for (int i = 1; i < argc; i += 2) { - std::string key = argv[i]; - if (key.size() < 2 || key[0] != '-' || key[1] != '-') { - fatal() << "Invalid key: " << key << std::endl; - exit(1); - } - key = key.substr(2); - std::string value = argv[i + 1]; - args[key] = value; +using namespace std; + +ArgsManager::ArgsManager(int argc, char** argv, std::vector _requiredArgs, std::vector _optionalArgs) + : requiredArgs(_requiredArgs), optionalArgs(_optionalArgs) { + for (int i = 1; i < argc; i += 2) { + string key = argv[i]; + if (key.size() < 2 || key[0] != '-' || key[1] != '-') { + fatal() << "Invalid key: " << key << endl; + exit(1); + } + key = key.substr(2); + string value = argv[i + 1]; + args[key] = value; + } + + ValidateArgs(); +} + +void ArgsManager::ValidateArgs() { + for (const auto& [key, value] : args) { + if (std::find(requiredArgs.begin(), requiredArgs.end(), key) == requiredArgs.end() && + std::find(optionalArgs.begin(), optionalArgs.end(), key) == optionalArgs.end()) { + fatal() << "ArgsManager -- Unknown argument provided: " << key << endl; + exit(7); + } + } + for (string requiredArg : requiredArgs) { + if (args.count(requiredArg) == 0) { + fatal() << "ArgsManager -- Required argument not provided: " << requiredArg << endl; + exit(8); } + } } diff --git a/libs/core/src/EventProcessor.cpp b/libs/core/src/EventProcessor.cpp index 654853d..c80c4ff 100644 --- a/libs/core/src/EventProcessor.cpp +++ b/libs/core/src/EventProcessor.cpp @@ -62,12 +62,9 @@ bool EventProcessor::PassesTriggerCuts(const shared_ptr event) { for (auto& triggerName : triggerNames) { passes = false; try { - passes = event->Get(triggerName); + passes = event->GetAs(triggerName); } catch (Exception&) { - if (find(triggerWarningsPrinted.begin(), triggerWarningsPrinted.end(), triggerName) == triggerWarningsPrinted.end()) { - warn() << "Trigger not present: " << triggerName << endl; - triggerWarningsPrinted.push_back(triggerName); - } + warn() << "Trigger not present: " << triggerName << endl; } if (passes) return true; } diff --git a/libs/core/src/EventWriter.cpp b/libs/core/src/EventWriter.cpp index 8c0c038..b080af4 100644 --- a/libs/core/src/EventWriter.cpp +++ b/libs/core/src/EventWriter.cpp @@ -23,7 +23,6 @@ int FilterBranch(T* vec, const std::vector& keepIndices) { EventWriter::EventWriter(const shared_ptr &eventReader_) : eventReader(eventReader_) { auto &config = ConfigManager::GetInstance(); - string outputFilePath; config.GetValue("treeOutputFilePath", outputFilePath); try { @@ -37,15 +36,15 @@ EventWriter::EventWriter(const shared_ptr &eventReader_) : eventRea branchesToRemove = {}; // Remove no branches by default } - SetupOutputTree(outputFilePath); + SetupOutputTree(); } EventWriter::~EventWriter() {} -void EventWriter::SetupOutputTree(string outFileName) { - makeParentDirectories(outFileName); +void EventWriter::SetupOutputTree() { + makeParentDirectories(outputFilePath); - outFile = new TFile(outFileName.c_str(), "recreate"); + outFile = new TFile(outputFilePath.c_str(), "recreate"); outFile->cd(); for (auto &[name, tree] : eventReader->inputTrees) { @@ -104,5 +103,6 @@ void EventWriter::Save() { for (auto &[name, tree] : outputTrees) { tree->Write(); } + info() << "Saved output trees to " << outputFilePath << endl; outFile->Close(); } diff --git a/pylibs/plotting/Legend.py b/pylibs/plotting/Legend.py index cc3ac1a..1dbb897 100644 --- a/pylibs/plotting/Legend.py +++ b/pylibs/plotting/Legend.py @@ -10,6 +10,7 @@ class Legend: y2: float = 0.5 options: str = "" title: str = "" + text_size: float = 20 def getRootLegend(self): legend = TLegend(self.x1, self.y1, self.x2, self.y2) @@ -21,7 +22,7 @@ def __setupLegend(self, legend): # legend.SetFillColor(0) # legend.SetFillStyle(0) legend.SetTextFont(43) - legend.SetTextSize(20) + legend.SetTextSize(self.text_size) # set legend title if self.title != "": diff --git a/pylibs/plotting/Sample.py b/pylibs/plotting/Sample.py index 147d00e..8192c88 100644 --- a/pylibs/plotting/Sample.py +++ b/pylibs/plotting/Sample.py @@ -3,7 +3,7 @@ from enum import Enum from Legend import Legend -from Logger import error +from Logger import fatal # enum class with signal, background, data