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