Skip to content

Commit 2b64796

Browse files
authored
Add some features to Infra (#144)
* add SetVerbosityFrequency() to the TaskManager * round verbosity_period_ from SetVerbosityFrequency() * add OpenCut() constructor of SimpleCut (always passed) * add template MergeVectors() to HelperFunctions * add const& to prev. commit * feature HelperFunctions::CreateRangeCuts - auto precision, switch for OpenCut
1 parent 79bc994 commit 2b64796

File tree

5 files changed

+69
-5
lines changed

5 files changed

+69
-5
lines changed

infra/HelperFunctions.hpp

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,49 @@ inline std::string ToStringWithSignificantFigures(const T a_value, const int n)
3030
return ToStringWithPrecision(reshifted_value, precision);
3131
}
3232

33-
inline std::vector<AnalysisTree::SimpleCut> CreateRangeCuts(const std::vector<float>& ranges, const std::string& cutNamePrefix, const std::string& branchFieldName, int precision = 2) {
33+
inline 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+
3460
std::vector<AnalysisTree::SimpleCut> sliceCuts;
3561
for (int iRange = 0; iRange < ranges.size() - 1; iRange++) {
3662
const std::string cutName = cutNamePrefix + ToStringWithPrecision(ranges.at(iRange), precision) + "_" + ToStringWithPrecision(ranges.at(iRange + 1), precision);
3763
sliceCuts.emplace_back(AnalysisTree::RangeCut(branchFieldName, ranges.at(iRange), ranges.at(iRange + 1), cutName));
3864
}
3965

66+
if (isAppendWithOpenCut) sliceCuts.emplace_back(AnalysisTree::OpenCut(branchFieldName.substr(0, branchFieldName.find('.'))));
67+
4068
return sliceCuts;
4169
}
4270

43-
inline std::vector<AnalysisTree::SimpleCut> CreateEqualCuts(const std::vector<float>& values, const std::string& cutNamePrefix, const std::string& branchFieldName, int precision = 2) {
71+
inline std::vector<AnalysisTree::SimpleCut> CreateEqualCuts(const std::vector<int>& values, const std::string& cutNamePrefix, const std::string& branchFieldName) {
4472
std::vector<AnalysisTree::SimpleCut> sliceCuts;
45-
for (int iValue = 0; iValue < values.size(); iValue++) {
46-
const std::string cutName = cutNamePrefix + ToStringWithPrecision(values.at(iValue), precision);
47-
sliceCuts.emplace_back(AnalysisTree::EqualsCut(branchFieldName, values.at(iValue), cutName));
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));
4876
}
4977

5078
return sliceCuts;
@@ -58,5 +86,20 @@ inline bool StringToBool(const std::string& str) {
5886
throw std::runtime_error("HelperFunctions::StringToBool(): argument must be either true or false");
5987
}
6088

89+
template<typename T>
90+
inline 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+
99+
template<typename T, typename... Args>
100+
inline 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+
61104
}// namespace HelperFunctions
62105
#endif// ANALYSISTREE_INFRA_HELPER_FUNCTIONS_HPP

infra/SimpleCut.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ SimpleCut EqualsCut(const Variable& var, int value, const std::string& title) {
4545
return SimpleCut(var, value, title);
4646
}
4747

48+
SimpleCut OpenCut(const std::string& branchName, const std::string& title) {
49+
return SimpleCut({branchName + ".ones"}, [](const std::vector<double>& par) { return true; });
50+
}
51+
4852
SimpleCut::SimpleCut(const Variable& var, int value, std::string title) : title_(std::move(title)) {
4953
vars_.emplace_back(var);
5054
lambda_ = [value](std::vector<double>& vars) { return vars[0] <= value + SmallNumber && vars[0] >= value - SmallNumber; };

infra/SimpleCut.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@ class SimpleCut {
8383
*/
8484
friend SimpleCut EqualsCut(const Variable& var, int value, const std::string& title);
8585

86+
/**
87+
* Constructor for a cut which is always passed
88+
* (needed for keeping generality of the user's code, when a set of cuts
89+
* is iterated and an iteration with no-cut is needed)
90+
* @param branchName name of the branch, which is present in other cuts
91+
*/
92+
friend SimpleCut OpenCut(const std::string& branchName, const std::string& title);
93+
8694
/**
8795
* @brief Evaluates cut
8896
* @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
131139
SimpleCut EqualsCut(const std::string& variable_name, int value, const std::string& title = "");
132140
SimpleCut RangeCut(const Variable& var, double lo, double hi, const std::string& title = "");
133141
SimpleCut EqualsCut(const Variable& var, int value, const std::string& title = "");
142+
SimpleCut OpenCut(const std::string& branchName, const std::string& title = "alwaysTrue");
134143

135144
}// namespace AnalysisTree
136145
#endif//ANALYSISTREE_SIMPLECUT_H

infra/TaskManager.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,12 @@ void TaskManager::Run(long long nEvents) {
101101
nEvents = nEvents < 0 || nEvents > chain_->GetEntries() ? chain_->GetEntries() : nEvents;
102102
}
103103

104+
if (verbosity_frequency_ > 0) {
105+
const int verbosityPeriod = nEvents / verbosity_frequency_;
106+
const int vPlog = static_cast<int>(std::round(std::log10(verbosityPeriod)));
107+
verbosity_period_ = static_cast<int>(std::pow(10, vPlog));
108+
}
109+
104110
for (long long iEvent = 0; iEvent < nEvents; ++iEvent) {
105111
if (verbosity_period_ > 0 && iEvent % verbosity_period_ == 0) {
106112
std::cout << "Event no " << iEvent << "\n";

infra/TaskManager.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ class TaskManager {
159159
void SetWriteMode(eBranchWriteMode mode) { write_mode_ = mode; }
160160
void SetBranchesExclude(std::vector<std::string> brex) { branches_exclude_ = std::move(brex); }
161161
void SetVerbosityPeriod(int value) { verbosity_period_ = value; }
162+
void SetVerbosityFrequency(int value) { verbosity_frequency_ = value; }
162163
void SetIsWriteHashInfo(bool is = true) { is_write_hash_info_ = is; }
163164
void SetIsUpdateEntryInExec(bool is = true) { is_update_entry_in_exec_ = is; }
164165

@@ -187,6 +188,7 @@ class TaskManager {
187188
std::vector<std::string> branches_exclude_{};
188189

189190
int verbosity_period_{-1};
191+
int verbosity_frequency_{-1};
190192

191193
// configuration parameters
192194
eBranchWriteMode write_mode_{eBranchWriteMode::kCreateNewTree};

0 commit comments

Comments
 (0)