diff --git a/infra/HelperFunctions.hpp b/infra/HelperFunctions.hpp index 6b6caaff..996a0d13 100644 --- a/infra/HelperFunctions.hpp +++ b/infra/HelperFunctions.hpp @@ -30,21 +30,49 @@ inline std::string ToStringWithSignificantFigures(const T a_value, const int n) return ToStringWithPrecision(reshifted_value, precision); } -inline std::vector CreateRangeCuts(const std::vector& ranges, const std::string& cutNamePrefix, const std::string& branchFieldName, int precision = 2) { +inline std::vector CreateRangeCuts(const std::vector& ranges, const std::string& cutNamePrefix, const std::string& branchFieldName, bool isAppendWithOpenCut = false, int precision = -1) { + auto checkHasFractionalPart = [](float value) { + float fracPart = std::round(value) - value; + return std::abs(fracPart) > 1e-4; + }; + + auto countDigisAfterComma = [&](float value) { + int nDigis{0}; + while (checkHasFractionalPart(value)) { + value *= 10; + ++nDigis; + } + + return nDigis; + }; + + auto evaluateMaxDigisAfterComma = [&](const std::vector& vec) { + int result = 0; + for (const auto& v : vec) { + result = std::max(result, countDigisAfterComma(v)); + } + + return result; + }; + + if (precision < 0) precision = evaluateMaxDigisAfterComma(ranges); + std::vector sliceCuts; for (int iRange = 0; iRange < ranges.size() - 1; iRange++) { const std::string cutName = cutNamePrefix + ToStringWithPrecision(ranges.at(iRange), precision) + "_" + ToStringWithPrecision(ranges.at(iRange + 1), precision); sliceCuts.emplace_back(AnalysisTree::RangeCut(branchFieldName, ranges.at(iRange), ranges.at(iRange + 1), cutName)); } + if (isAppendWithOpenCut) sliceCuts.emplace_back(AnalysisTree::OpenCut(branchFieldName.substr(0, branchFieldName.find('.')))); + return sliceCuts; } -inline std::vector CreateEqualCuts(const std::vector& values, const std::string& cutNamePrefix, const std::string& branchFieldName, int precision = 2) { +inline std::vector CreateEqualCuts(const std::vector& values, const std::string& cutNamePrefix, const std::string& branchFieldName) { std::vector sliceCuts; - for (int iValue = 0; iValue < values.size(); iValue++) { - const std::string cutName = cutNamePrefix + ToStringWithPrecision(values.at(iValue), precision); - sliceCuts.emplace_back(AnalysisTree::EqualsCut(branchFieldName, values.at(iValue), cutName)); + for (const auto& value : values) { + const std::string cutName = cutNamePrefix + std::to_string(value); + sliceCuts.emplace_back(AnalysisTree::EqualsCut(branchFieldName, value, cutName)); } return sliceCuts; @@ -58,5 +86,20 @@ inline bool StringToBool(const std::string& str) { throw std::runtime_error("HelperFunctions::StringToBool(): argument must be either true or false"); } +template +inline std::vector MergeVectors(const std::vector& vec1, const std::vector& vec2) { + std::vector result; + result.reserve(vec1.size() + vec2.size()); + result.insert(result.end(), vec1.begin(), vec1.end()); + result.insert(result.end(), vec2.begin(), vec2.end()); + + return result; +} + +template +inline std::vector MergeVectors(const std::vector& vec1, const std::vector& vec2, const Args&... args) { + return MergeVectors(vec1, MergeVectors(vec2, args...)); +} + }// namespace HelperFunctions #endif// ANALYSISTREE_INFRA_HELPER_FUNCTIONS_HPP diff --git a/infra/SimpleCut.cpp b/infra/SimpleCut.cpp index 537d4e64..ff3c979e 100644 --- a/infra/SimpleCut.cpp +++ b/infra/SimpleCut.cpp @@ -45,6 +45,10 @@ SimpleCut EqualsCut(const Variable& var, int value, const std::string& title) { return SimpleCut(var, value, title); } +SimpleCut OpenCut(const std::string& branchName, const std::string& title) { + return SimpleCut({branchName + ".ones"}, [](const std::vector& par) { return true; }); +} + SimpleCut::SimpleCut(const Variable& var, int value, std::string title) : title_(std::move(title)) { vars_.emplace_back(var); lambda_ = [value](std::vector& vars) { return vars[0] <= value + SmallNumber && vars[0] >= value - SmallNumber; }; diff --git a/infra/SimpleCut.hpp b/infra/SimpleCut.hpp index cab78068..5adbdd25 100644 --- a/infra/SimpleCut.hpp +++ b/infra/SimpleCut.hpp @@ -83,6 +83,14 @@ class SimpleCut { */ friend SimpleCut EqualsCut(const Variable& var, int value, const std::string& title); + /** + * Constructor for a cut which is always passed + * (needed for keeping generality of the user's code, when a set of cuts + * is iterated and an iteration with no-cut is needed) + * @param branchName name of the branch, which is present in other cuts + */ + friend SimpleCut OpenCut(const std::string& branchName, const std::string& title); + /** * @brief Evaluates cut * @tparam T type of data-object associated with TTree @@ -131,6 +139,7 @@ SimpleCut RangeCut(const std::string& variable_name, double lo, double hi, const SimpleCut EqualsCut(const std::string& variable_name, int value, const std::string& title = ""); SimpleCut RangeCut(const Variable& var, double lo, double hi, const std::string& title = ""); SimpleCut EqualsCut(const Variable& var, int value, const std::string& title = ""); +SimpleCut OpenCut(const std::string& branchName, const std::string& title = "alwaysTrue"); }// namespace AnalysisTree #endif//ANALYSISTREE_SIMPLECUT_H diff --git a/infra/TaskManager.cpp b/infra/TaskManager.cpp index 68f4f60c..511158ec 100644 --- a/infra/TaskManager.cpp +++ b/infra/TaskManager.cpp @@ -101,6 +101,12 @@ void TaskManager::Run(long long nEvents) { nEvents = nEvents < 0 || nEvents > chain_->GetEntries() ? chain_->GetEntries() : nEvents; } + if (verbosity_frequency_ > 0) { + const int verbosityPeriod = nEvents / verbosity_frequency_; + const int vPlog = static_cast(std::round(std::log10(verbosityPeriod))); + verbosity_period_ = static_cast(std::pow(10, vPlog)); + } + for (long long iEvent = 0; iEvent < nEvents; ++iEvent) { if (verbosity_period_ > 0 && iEvent % verbosity_period_ == 0) { std::cout << "Event no " << iEvent << "\n"; diff --git a/infra/TaskManager.hpp b/infra/TaskManager.hpp index 1e61a07d..59479353 100644 --- a/infra/TaskManager.hpp +++ b/infra/TaskManager.hpp @@ -159,6 +159,7 @@ class TaskManager { void SetWriteMode(eBranchWriteMode mode) { write_mode_ = mode; } void SetBranchesExclude(std::vector brex) { branches_exclude_ = std::move(brex); } void SetVerbosityPeriod(int value) { verbosity_period_ = value; } + void SetVerbosityFrequency(int value) { verbosity_frequency_ = value; } void SetIsWriteHashInfo(bool is = true) { is_write_hash_info_ = is; } void SetIsUpdateEntryInExec(bool is = true) { is_update_entry_in_exec_ = is; } @@ -187,6 +188,7 @@ class TaskManager { std::vector branches_exclude_{}; int verbosity_period_{-1}; + int verbosity_frequency_{-1}; // configuration parameters eBranchWriteMode write_mode_{eBranchWriteMode::kCreateNewTree};