Skip to content

Commit 165fe19

Browse files
committed
PlainTreeFiller: enable fields renaming
1 parent a584a66 commit 165fe19

2 files changed

Lines changed: 14 additions & 0 deletions

File tree

infra/PlainTreeFiller.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ void PlainTreeFiller::SetFieldsToPreserve(const std::vector<std::string>& fields
3333
}
3434
}
3535

36+
void PlainTreeFiller::SetFieldsToRename(const std::vector<std::pair<std::string, std::string>>& fields_to_rename) {
37+
if (branch_name_.empty()) {
38+
throw std::runtime_error("PlainTreeFiller::SetFieldsToRename() must be called after PlainTreeFiller::AddBranch()\n");
39+
}
40+
for (const auto& ftr : fields_to_rename) {
41+
fields_to_rename_.emplace((branch_name_ + "." + ftr.first).c_str(), (branch_name_ + "." + ftr.second).c_str());
42+
}
43+
}
44+
3645
void PlainTreeFiller::Init() {
3746
if (is_ignore_defual_fields_) {
3847
std::vector<std::string> defaultFieldsNames;
@@ -103,6 +112,9 @@ void PlainTreeFiller::Init() {
103112
std::string leaf_name = leafNames.at(iLeaf);
104113
if (!fields_to_ignore_.empty() && std::find(fields_to_ignore_.begin(), fields_to_ignore_.end(), leaf_name) != fields_to_ignore_.end()) continue;
105114
if (!fields_to_preserve_.empty() && std::find(fields_to_preserve_.begin(), fields_to_preserve_.end(), leaf_name) == fields_to_preserve_.end()) continue;
115+
if (!fields_to_rename_.empty() && fields_to_rename_.find(leaf_name) != fields_to_rename_.end()) {
116+
leaf_name = fields_to_rename_.at(leaf_name);
117+
}
106118
if (!is_prepend_leaves_with_branchname_) leaf_name.erase(0, branch_name_.size() + 1);
107119
std::replace(leaf_name.begin(), leaf_name.end(), '.', '_');
108120
if (vars_.at(iLeaf).type_ == Types::kFloat) plain_tree_->Branch(leaf_name.c_str(), &vars_.at(iLeaf).float_, Form("%s/F", leaf_name.c_str()));

infra/PlainTreeFiller.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class PlainTreeFiller : public AnalysisTask {
3636

3737
void SetFieldsToIgnore(const std::vector<std::string>& fields_to_ignore);
3838
void SetFieldsToPreserve(const std::vector<std::string>& fields_to_preserve);
39+
void SetFieldsToRename(const std::vector<std::pair<std::string, std::string>>& fields_to_rename);
3940

4041
void SetIsIgnoreDefaultFields(bool is = true) { is_ignore_defual_fields_ = is; }
4142
void SetIsPrependLeavesWithBranchName(bool is = true) { is_prepend_leaves_with_branchname_ = is; }
@@ -51,6 +52,7 @@ class PlainTreeFiller : public AnalysisTask {
5152
std::vector<FIB> vars_;
5253
std::vector<std::string> fields_to_ignore_{};
5354
std::vector<std::string> fields_to_preserve_{};
55+
std::map<std::string, std::string> fields_to_rename_{};
5456

5557
bool is_ignore_defual_fields_{false};
5658
bool is_prepend_leaves_with_branchname_{true};

0 commit comments

Comments
 (0)