AnalysisTree
Loading...
Searching...
No Matches
HelperFunctions.hpp
1#ifndef ANALYSISTREE_INFRA_HELPER_FUNCTIONS_HPP
2#define ANALYSISTREE_INFRA_HELPER_FUNCTIONS_HPP
3
4#include "SimpleCut.hpp"
5
6#include <sstream>
7#include <string>
8#include <vector>
9
10namespace HelperFunctions {
11
12template<typename T>
13inline std::string ToStringWithPrecision(const T a_value, const int n) {
14 std::ostringstream out;
15 out.precision(n);
16 out << std::fixed << a_value;
17 return out.str();
18}
19
20template<typename T>
21inline std::string ToStringWithSignificantFigures(const T a_value, const int n) {
22 if (a_value == 0) return "0";
23
24 const double dMag = std::log10(std::abs(a_value));// scale of the a_value (e.g 1.* for 1.2345, 2.* for 12.345 etc)
25 const int iMag = static_cast<int>(dMag - n + 1 > 0 ? dMag - n + 1 : dMag - n);
26 const T shifted_value = a_value / std::pow(10, iMag); // shift decimal point to have all required digits to l.h.s. from it
27 const T rounded_value = static_cast<T>(std::round(shifted_value));// get rid of r.h.s. from decimal point
28 const T reshifted_value = rounded_value * std::pow(10, iMag); // return decimal point to its original place
29 const int precision = iMag < 0 ? -iMag : 0; // determine how many digits after decimal point one needs
30 return ToStringWithPrecision(reshifted_value, precision);
31}
32
33inline std::vector<AnalysisTree::SimpleCut> CreateRangeCuts(const std::vector<float>& ranges, const std::string& cutNamePrefix, const std::string& branchFieldName, bool isAppendWithOpenCut = false, int precision = -1) {
34 auto checkHasFractionalPart = [](float value) {
35 float fracPart = std::round(value) - value;
36 return std::abs(fracPart) > 1e-4;
37 };
38
39 auto countDigisAfterComma = [&](float value) {
40 int nDigis{0};
41 while (checkHasFractionalPart(value)) {
42 value *= 10;
43 ++nDigis;
44 }
45
46 return nDigis;
47 };
48
49 auto evaluateMaxDigisAfterComma = [&](const std::vector<float>& vec) {
50 int result = 0;
51 for (const auto& v : vec) {
52 result = std::max(result, countDigisAfterComma(v));
53 }
54
55 return result;
56 };
57
58 if (precision < 0) precision = evaluateMaxDigisAfterComma(ranges);
59
60 std::vector<AnalysisTree::SimpleCut> sliceCuts;
61 for (int iRange = 0; iRange < ranges.size() - 1; iRange++) {
62 const std::string cutName = cutNamePrefix + ToStringWithPrecision(ranges.at(iRange), precision) + "_" + ToStringWithPrecision(ranges.at(iRange + 1), precision);
63 sliceCuts.emplace_back(AnalysisTree::RangeCut(branchFieldName, ranges.at(iRange), ranges.at(iRange + 1), cutName));
64 }
65
66 if (isAppendWithOpenCut) sliceCuts.emplace_back(AnalysisTree::OpenCut(branchFieldName.substr(0, branchFieldName.find('.'))));
67
68 return sliceCuts;
69}
70
71inline std::vector<AnalysisTree::SimpleCut> CreateEqualCuts(const std::vector<int>& values, const std::string& cutNamePrefix, const std::string& branchFieldName) {
72 std::vector<AnalysisTree::SimpleCut> sliceCuts;
73 for (const auto& value : values) {
74 const std::string cutName = cutNamePrefix + std::to_string(value);
75 sliceCuts.emplace_back(AnalysisTree::EqualsCut(branchFieldName, value, cutName));
76 }
77
78 return sliceCuts;
79}
80
81inline bool StringToBool(const std::string& str) {
82 if (str == "true") return true;
83 else if (str == "false")
84 return false;
85 else
86 throw std::runtime_error("HelperFunctions::StringToBool(): argument must be either true or false");
87}
88
89template<typename T>
90inline std::vector<T> MergeVectors(const std::vector<T>& vec1, const std::vector<T>& vec2) {
91 std::vector<T> result;
92 result.reserve(vec1.size() + vec2.size());
93 result.insert(result.end(), vec1.begin(), vec1.end());
94 result.insert(result.end(), vec2.begin(), vec2.end());
95
96 return result;
97}
98
99template<typename T, typename... Args>
100inline std::vector<T> MergeVectors(const std::vector<T>& vec1, const std::vector<T>& vec2, const Args&... args) {
101 return MergeVectors(vec1, MergeVectors(vec2, args...));
102}
103
104}// namespace HelperFunctions
105#endif// ANALYSISTREE_INFRA_HELPER_FUNCTIONS_HPP
SimpleCut EqualsCut(const std::string &variable_name, int value, const std::string &title)
Definition SimpleCut.cpp:36
SimpleCut RangeCut(const std::string &variable_name, double lo, double hi, const std::string &title)
Definition SimpleCut.cpp:32
SimpleCut OpenCut(const std::string &branchName, const std::string &title)
Definition SimpleCut.cpp:48