Skip to content

Commit b021434

Browse files
authored
Make PlainTreeFiller more user friendly (#141)
* mv VectorConfig::Print() implementation to .cpp * enable non-prepending output leaves names with branch name * propagate examples .cpp into install/share * enable cherry-picking fields in PlainTreeFiller (instead of ignoring) * remove virtual from VectorConfig::Print() * remove BranchReader infra-1.0 remnants * remove rootlogon.C * apply clang-format
1 parent 4a5a25e commit b021434

11 files changed

Lines changed: 87 additions & 75 deletions

core/BranchConfig.cpp

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -136,20 +136,49 @@ BranchConfig BranchConfig::CloneAndMerge(const BranchConfig& attached) const {
136136
}
137137

138138
template<typename T>
139-
std::vector<std::string> VectorConfig<T>::SplitString(const std::string& input) {
140-
std::vector<std::string> result;
141-
std::vector<int> newlinepositions{-1};
142-
int it{0};
143-
while (it < std::string::npos) {
144-
it = input.find("\n", it + 1);
145-
newlinepositions.emplace_back(it);
139+
void VectorConfig<T>::Print() const {
140+
if (map_.empty()) return;
141+
142+
auto print_row = [](const std::vector<std::pair<std::string, int>>& elements) {
143+
for (const auto& el : elements) {
144+
std::cout << std::left << std::setw(el.second) << std::setfill(' ') << el.first;
145+
}
146+
std::cout << std::endl;
147+
};
148+
149+
int name_strlen{0};
150+
for (const auto& entry : map_) {
151+
name_strlen = std::max(name_strlen, (int) entry.first.length());
146152
}
147-
newlinepositions.back() = input.size();
148-
for (int ip = 0; ip < newlinepositions.size() - 1; ++ip) {
149-
result.emplace_back(input.substr(newlinepositions.at(ip) + 1, newlinepositions.at(ip + 1) - newlinepositions.at(ip) - 1));
153+
name_strlen += 4;
154+
155+
auto SplitString = [](const std::string& input) {
156+
std::vector<std::string> result;
157+
std::vector<int> newlinepositions{-1};
158+
int it{0};
159+
while (it < std::string::npos) {
160+
it = input.find("\n", it + 1);
161+
newlinepositions.emplace_back(it);
162+
}
163+
newlinepositions.back() = input.size();
164+
for (int ip = 0; ip < newlinepositions.size() - 1; ++ip) {
165+
result.emplace_back(input.substr(newlinepositions.at(ip) + 1, newlinepositions.at(ip + 1) - newlinepositions.at(ip) - 1));
166+
}
167+
return result;
168+
};
169+
170+
print_row({{"Id", 10}, {"Name", name_strlen}, {"Info", 50}});
171+
for (const auto& entry : map_) {
172+
if (entry.second.title_.find("\n") == std::string::npos) {
173+
print_row({{std::to_string(entry.second.id_), 10}, {entry.first, name_strlen}, {entry.second.title_, 50}});
174+
} else {
175+
auto est = SplitString(entry.second.title_);
176+
print_row({{std::to_string(entry.second.id_), 10}, {entry.first, name_strlen}, {est.at(0), 50}});
177+
for (int iest = 1; iest < est.size(); ++iest) {
178+
print_row({{"", 10}, {"", name_strlen}, {est.at(iest), 50}});
179+
}
180+
}
150181
}
151-
152-
return result;
153182
}
154183

155184
template<typename T>

core/BranchConfig.hpp

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -73,38 +73,9 @@ class VectorConfig {
7373
ANALYSISTREE_ATTR_NODISCARD virtual const MapType& GetMap() const { return map_; }
7474
ANALYSISTREE_ATTR_NODISCARD virtual ShortInt_t GetSize() const { return size_; }
7575

76-
virtual void Print() const {
77-
if (map_.empty()) return;
78-
79-
auto print_row = [](const std::vector<std::pair<std::string, int>>& elements) {
80-
for (const auto& el : elements) {
81-
std::cout << std::left << std::setw(el.second) << std::setfill(' ') << el.first;
82-
}
83-
std::cout << std::endl;
84-
};
85-
86-
int name_strlen{0};
87-
for (const auto& entry : map_) {
88-
name_strlen = std::max(name_strlen, (int) entry.first.length());
89-
}
90-
name_strlen += 4;
91-
92-
print_row({{"Id", 10}, {"Name", name_strlen}, {"Info", 50}});
93-
for (const auto& entry : map_) {
94-
if (entry.second.title_.find("\n") == std::string::npos) {
95-
print_row({{std::to_string(entry.second.id_), 10}, {entry.first, name_strlen}, {entry.second.title_, 50}});
96-
} else {
97-
auto est = SplitString(entry.second.title_);
98-
print_row({{std::to_string(entry.second.id_), 10}, {entry.first, name_strlen}, {est.at(0), 50}});
99-
for (int iest = 1; iest < est.size(); ++iest) {
100-
print_row({{"", 10}, {"", name_strlen}, {est.at(iest), 50}});
101-
}
102-
}
103-
}
104-
}
76+
void Print() const;
10577

10678
protected:
107-
static std::vector<std::string> SplitString(const std::string& input);
10879
void RemoveField(const std::string& name, int id);
10980
MapType map_{};
11081
ShortInt_t size_{0};
@@ -127,7 +98,7 @@ class BranchConfig : public VectorConfig<int>, public VectorConfig<float>, publi
12798

12899
BranchConfig(std::string name, DetType type, std::string title = "");
129100

130-
void Print() const override;
101+
void Print() const;
131102

132103
void PrintBranchId() const;
133104

examples/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,5 @@ foreach(EXE ${EXECUTABLES})
6969
add_executable(${EXE} ${EXE}.cpp)
7070
target_link_libraries(${EXE} AnalysisTreeBase AnalysisTreeInfra AnalysisTreeUser ${ROOT_LIBRARIES} EG)
7171
install (TARGETS ${EXE} RUNTIME DESTINATION bin)
72+
install (FILES ${EXE}.cpp DESTINATION share)
7273
endforeach()

examples/rootlogon.C

Lines changed: 0 additions & 7 deletions
This file was deleted.

infra/AnalysisTreeInfraLinkDef.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#pragma link C++ class AnalysisTree::AnalysisTask;
1818
#pragma link C++ class AnalysisTree::Chain+;
1919

20-
#pragma link C++ class AnalysisTree::BranchReader+;
2120
#pragma link C++ class AnalysisTree::AnalysisEntry+;
2221

2322
#endif

infra/HelperFunctions.hpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,18 @@ inline std::string ToStringWithPrecision(const T a_value, const int n) {
1919

2020
template<typename T>
2121
inline 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
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
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, int precision = 2) {
3434
std::vector<AnalysisTree::SimpleCut> sliceCuts;
3535
for (int iRange = 0; iRange < ranges.size() - 1; iRange++) {
3636
const std::string cutName = cutNamePrefix + ToStringWithPrecision(ranges.at(iRange), 2) + "_" + ToStringWithPrecision(ranges.at(iRange + 1), precision);
@@ -40,7 +40,7 @@ inline std::vector<AnalysisTree::SimpleCut> CreateRangeCuts(const std::vector<fl
4040
return sliceCuts;
4141
}
4242

43-
inline std::vector<AnalysisTree::SimpleCut> CreateEqualCuts(const std::vector<float>& values, const std::string& cutNamePrefix, const std::string& branchFieldName, int precision=2) {
43+
inline std::vector<AnalysisTree::SimpleCut> CreateEqualCuts(const std::vector<float>& values, const std::string& cutNamePrefix, const std::string& branchFieldName, int precision = 2) {
4444
std::vector<AnalysisTree::SimpleCut> sliceCuts;
4545
for (int iValue = 0; iValue < values.size(); iValue++) {
4646
const std::string cutName = cutNamePrefix + ToStringWithPrecision(values.at(iValue), precision);

infra/PlainTreeFiller.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ void PlainTreeFiller::AddBranch(const std::string& branch_name) {
1515
in_branches_.emplace(branch_name);
1616
}
1717

18-
void PlainTreeFiller::SetFieldsToIgnore(const std::vector<std::string>&& fields_to_ignore) {
18+
void PlainTreeFiller::SetFieldsToIgnore(const std::vector<std::string>& fields_to_ignore) {
1919
if (branch_name_ == "") {
2020
throw std::runtime_error("PlainTreeFiller::SetFieldsToIgnore() must be called after PlainTreeFiller::AddBranch()\n");
2121
}
@@ -24,6 +24,15 @@ void PlainTreeFiller::SetFieldsToIgnore(const std::vector<std::string>&& fields_
2424
}
2525
}
2626

27+
void PlainTreeFiller::SetFieldsToPreserve(const std::vector<std::string>& fields_to_preserve) {
28+
if (branch_name_ == "") {
29+
throw std::runtime_error("PlainTreeFiller::SetFieldsToPreserve() must be called after PlainTreeFiller::AddBranch()\n");
30+
}
31+
for (auto& fti : fields_to_preserve) {
32+
fields_to_preserve_.emplace_back((branch_name_ + "." + fti).c_str());
33+
}
34+
}
35+
2736
void PlainTreeFiller::Init() {
2837
if (is_ignore_defual_fields_) {
2938
std::vector<std::string> defaultFieldsNames;
@@ -38,6 +47,10 @@ void PlainTreeFiller::Init() {
3847
SetFieldsToIgnore(std::move(defaultFieldsNames));
3948
}
4049

50+
if (!fields_to_ignore_.empty() && !fields_to_preserve_.empty()) {
51+
throw std::runtime_error("PlainTreeFiller::Init() - only one of fields_to_ignore_ and fields_to_preserve_ can be set");
52+
}
53+
4154
if (!branch_name_.empty()) {
4255
const auto& branch_config = config_->GetBranchConfig(branch_name_);
4356
for (const auto& field : branch_config.GetMap<float>()) {
@@ -64,7 +77,9 @@ void PlainTreeFiller::Init() {
6477
plain_tree_->SetAutoSave(0);
6578
for (size_t i = 0; i < vars.size(); ++i) {
6679
std::string leaf_name = vars[i].GetName();
67-
if (std::find(fields_to_ignore_.begin(), fields_to_ignore_.end(), leaf_name) != fields_to_ignore_.end()) continue;
80+
if (!fields_to_ignore_.empty() && std::find(fields_to_ignore_.begin(), fields_to_ignore_.end(), leaf_name) != fields_to_ignore_.end()) continue;
81+
if (!fields_to_preserve_.empty() && std::find(fields_to_preserve_.begin(), fields_to_preserve_.end(), leaf_name) == fields_to_preserve_.end()) continue;
82+
if (!is_prepend_leaves_with_branchname_) leaf_name.erase(0, branch_name_.size() + 1);
6883
std::replace(leaf_name.begin(), leaf_name.end(), '.', '_');
6984
plain_tree_->Branch(leaf_name.c_str(), &(vars_.at(i)), Form("%s/F", leaf_name.c_str()));
7085
}

infra/PlainTreeFiller.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ class PlainTreeFiller : public AnalysisTask {
2727
tree_name_ = std::move(tree);
2828
}
2929

30-
void SetFieldsToIgnore(const std::vector<std::string>&& fields_to_ignore);
30+
void SetFieldsToIgnore(const std::vector<std::string>& fields_to_ignore);
31+
void SetFieldsToPreserve(const std::vector<std::string>& fields_to_preserve);
3132

3233
void SetIsIgnoreDefaultFields(bool is = true) { is_ignore_defual_fields_ = is; }
34+
void SetIsPrependLeavesWithBranchName(bool is = true) { is_prepend_leaves_with_branchname_ = is; }
3335

3436
protected:
3537
TFile* file_{nullptr};
@@ -41,8 +43,10 @@ class PlainTreeFiller : public AnalysisTask {
4143

4244
std::vector<float> vars_{};
4345
std::vector<std::string> fields_to_ignore_{};
46+
std::vector<std::string> fields_to_preserve_{};
4447

4548
bool is_ignore_defual_fields_{false};
49+
bool is_prepend_leaves_with_branchname_{true};
4650
};
4751

4852
}// namespace AnalysisTree

infra/SimpleCut.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ bool operator==(const SimpleCut& that, const SimpleCut& other) {
2525
return true;
2626
}
2727
// if both SimpleCuts were defined via lambda, they're assumed not equal (unless they're in the same memory place)
28-
if(that.hash_ == 1 && other.hash_ == 1) return false;
28+
if (that.hash_ == 1 && other.hash_ == 1) return false;
2929
return that.vars_ == other.vars_ && that.title_ == other.title_ && that.hash_ == other.hash_;
3030
}
3131

infra/SimpleCut.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class SimpleCut {
4141
[](const std::string& arg_name) { return Variable::FromString(arg_name); });
4242

4343
FillBranchNames();
44-
hash_ = 1; // Impossible to calculate for lambda_
44+
hash_ = 1;// Impossible to calculate for lambda_
4545
}
4646

4747
SimpleCut(const std::vector<Variable>& vars, std::function<bool(std::vector<double>&)> lambda, std::string title = "") : title_(std::move(title)),
@@ -50,7 +50,7 @@ class SimpleCut {
5050
vars_.emplace_back(var);
5151
}
5252
FillBranchNames();
53-
hash_ = 1; // Impossible to calculate for lambda_
53+
hash_ = 1;// Impossible to calculate for lambda_
5454
}
5555

5656
/**

0 commit comments

Comments
 (0)