Skip to content

Commit 79bc994

Browse files
authored
Maintain GenericContainer- and PlainTreeFiller (#143)
* enable run GenericContainerFiller with filelist * bugfix non-progressing trees in a chain when SetAddress() * apply clang-format * distinguish float, int, bool in PlainTreeFiller * follow clang-tidy suggestions * apply clang format * more safe cast of double into int and bool
1 parent 24420e1 commit 79bc994

4 files changed

Lines changed: 61 additions & 26 deletions

File tree

infra/GenericContainerFiller.cpp

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,33 @@
44

55
#include "GenericContainerFiller.hpp"
66

7+
#include <fstream>
8+
79
using namespace AnalysisTree;
810

911
GenericContainerFiller::GenericContainerFiller(std::string fileInName, std::string treeInName) : file_in_name_(std::move(fileInName)),
1012
tree_in_name_(std::move(treeInName)) {}
1113

1214
void GenericContainerFiller::Init() {
13-
file_in_ = TFile::Open(file_in_name_.c_str(), "read");
14-
if (file_in_ == nullptr) throw std::runtime_error("GenericContainerFiller::Run(): file_in_ == nullptr");
15+
tree_in_ = new TChain(tree_in_name_.c_str());
16+
17+
auto ends_with = [](const std::string& str, const std::string& suffix) {
18+
if (suffix.size() > str.size()) return false;
19+
return std::equal(suffix.rbegin(), suffix.rend(), str.rbegin());
20+
};
21+
22+
if (ends_with(file_in_name_, ".root")) {
23+
tree_in_->Add(file_in_name_.c_str());
24+
} else {
25+
std::ifstream filelist(file_in_name_);
26+
std::string line;
1527

16-
tree_in_ = file_in_->Get<TTree>(tree_in_name_.c_str());
17-
if (tree_in_ == nullptr) throw std::runtime_error("GenericContainerFiller::Run(): tree_in_ == nullptr");
28+
if (!filelist) throw std::runtime_error("GenericContainerFiller::Init(): filelist " + file_in_name_ + " is missing");
29+
30+
while (std::getline(filelist, line)) {
31+
tree_in_->Add(line.c_str());
32+
}
33+
}
1834

1935
if (!fields_to_ignore_.empty() && !fields_to_preserve_.empty()) throw std::runtime_error("GenericContainerFiller::Run(): !fields_to_ignore_.empty() && !fields_to_preserve_.empty()");
2036

@@ -40,8 +56,7 @@ void GenericContainerFiller::Init() {
4056
config_.AddBranchConfig(branchConfig);
4157

4258
for (int iV = 0; iV < branch_values_.size(); iV++) {
43-
TBranch* branch = tree_in_->GetBranch(branch_map_.at(iV).name_.c_str());
44-
SetAddressFICS(branch, branch_map_.at(iV), branch_values_.at(iV));
59+
SetAddressFICS(branch_map_.at(iV).name_, branch_map_.at(iV), branch_values_.at(iV));
4560
}
4661

4762
generic_detector_ = new GenericDetector(branchConfig.GetId());
@@ -74,7 +89,7 @@ void GenericContainerFiller::Finish() {
7489
config_.Write("Configuration");
7590
tree_out_->Write();
7691
file_out_->Close();
77-
file_in_->Close();
92+
delete tree_in_;
7893
}
7994

8095
void GenericContainerFiller::Run(int nEntries) {
@@ -98,14 +113,14 @@ int GenericContainerFiller::DetermineFieldIdByName(const std::vector<IndexMap>&
98113
return distance;
99114
}
100115

101-
void GenericContainerFiller::SetAddressFICS(TBranch* branch, const IndexMap& imap, FICS& ficc) {
102-
if (imap.field_type_ == "TLeafF") branch->SetAddress(&ficc.float_);
116+
void GenericContainerFiller::SetAddressFICS(const std::string& branchName, const IndexMap& imap, FICS& ficc) {
117+
if (imap.field_type_ == "TLeafF") tree_in_->SetBranchAddress(branchName.c_str(), &ficc.float_);
103118
else if (imap.field_type_ == "TLeafI")
104-
branch->SetAddress(&ficc.int_);
119+
tree_in_->SetBranchAddress(branchName.c_str(), &ficc.int_);
105120
else if (imap.field_type_ == "TLeafB")
106-
branch->SetAddress(&ficc.char_);
121+
tree_in_->SetBranchAddress(branchName.c_str(), &ficc.char_);
107122
else if (imap.field_type_ == "TLeafS")
108-
branch->SetAddress(&ficc.short_);
123+
tree_in_->SetBranchAddress(branchName.c_str(), &ficc.short_);
109124
else
110125
throw std::runtime_error("GenericContainerFiller::SetAddressFICS(): unsupported filed type " + imap.field_type_);
111126
}

infra/GenericContainerFiller.hpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
#include "Container.hpp"
1010
#include "Detector.hpp"
1111

12+
#include <TChain.h>
1213
#include <TFile.h>
13-
#include <TTree.h>
1414

1515
#include <string>
1616
#include <vector>
@@ -27,7 +27,7 @@ struct FICS {// FICS stands for float, int, char, short
2727
char char_{static_cast<char>(-199)};
2828
short short_{static_cast<short>(-199)};
2929

30-
float get() {
30+
float get() const {
3131
if (std::fabs(float_ + 199.f) > 1e-4) return float_;
3232
if (int_ != -199) return static_cast<float>(int_);
3333
if (char_ != static_cast<char>(-199)) return static_cast<float>(char_);
@@ -63,7 +63,7 @@ class GenericContainerFiller {
6363
void Finish();
6464

6565
static int DetermineFieldIdByName(const std::vector<IndexMap>& iMap, const std::string& name);
66-
static void SetAddressFICS(TBranch* branch, const IndexMap& imap, FICS& ficc);
66+
void SetAddressFICS(const std::string& branchName, const IndexMap& imap, FICS& ficc);
6767
static void SetFieldsFICS(const std::vector<IndexMap>& imap, AnalysisTree::Container& container, const std::vector<FICS>& ficc);
6868

6969
std::string file_in_name_;
@@ -73,8 +73,7 @@ class GenericContainerFiller {
7373
std::string tree_out_name_{"aTree"};
7474
std::string branch_out_name_{"PlainBranch"};
7575

76-
TFile* file_in_{nullptr};
77-
TTree* tree_in_{nullptr};
76+
TChain* tree_in_{nullptr};
7877
TFile* file_out_{nullptr};
7978
TTree* tree_out_{nullptr};
8079

@@ -84,7 +83,7 @@ class GenericContainerFiller {
8483
std::vector<FICS> branch_values_;
8584

8685
// variable, change of value of which triggers switch to a new AT event
87-
std::string entry_switch_trigger_var_name_{""};
86+
std::string entry_switch_trigger_var_name_;
8887

8988
int entry_switch_trigger_id_{-1};
9089

infra/PlainTreeFiller.cpp

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ void PlainTreeFiller::AddBranch(const std::string& branch_name) {
1616
}
1717

1818
void PlainTreeFiller::SetFieldsToIgnore(const std::vector<std::string>& fields_to_ignore) {
19-
if (branch_name_ == "") {
19+
if (branch_name_.empty()) {
2020
throw std::runtime_error("PlainTreeFiller::SetFieldsToIgnore() must be called after PlainTreeFiller::AddBranch()\n");
2121
}
2222
for (auto& fti : fields_to_ignore) {
@@ -25,7 +25,7 @@ void PlainTreeFiller::SetFieldsToIgnore(const std::vector<std::string>& fields_t
2525
}
2626

2727
void PlainTreeFiller::SetFieldsToPreserve(const std::vector<std::string>& fields_to_preserve) {
28-
if (branch_name_ == "") {
28+
if (branch_name_.empty()) {
2929
throw std::runtime_error("PlainTreeFiller::SetFieldsToPreserve() must be called after PlainTreeFiller::AddBranch()\n");
3030
}
3131
for (auto& fti : fields_to_preserve) {
@@ -44,7 +44,7 @@ void PlainTreeFiller::Init() {
4444
if (me.second.id_ < 0) defaultFieldsNames.emplace_back(me.first);
4545
}
4646
}
47-
SetFieldsToIgnore(std::move(defaultFieldsNames));
47+
SetFieldsToIgnore(defaultFieldsNames);
4848
}
4949

5050
if (!fields_to_ignore_.empty() && !fields_to_preserve_.empty()) {
@@ -55,12 +55,18 @@ void PlainTreeFiller::Init() {
5555
const auto& branch_config = config_->GetBranchConfig(branch_name_);
5656
for (const auto& field : branch_config.GetMap<float>()) {
5757
AnalysisTask::AddEntry(AnalysisEntry({Variable(branch_name_, field.first)}));
58+
vars_.emplace_back();
59+
vars_.back().type_ = Types::kFloat;
5860
}
5961
for (const auto& field : branch_config.GetMap<int>()) {
6062
AnalysisTask::AddEntry(AnalysisEntry({Variable(branch_name_, field.first)}));
63+
vars_.emplace_back();
64+
vars_.back().type_ = Types::kInteger;
6165
}
6266
for (const auto& field : branch_config.GetMap<bool>()) {
6367
AnalysisTask::AddEntry(AnalysisEntry({Variable(branch_name_, field.first)}));
68+
vars_.emplace_back();
69+
vars_.back().type_ = Types::kBool;
6470
}
6571
}
6672

@@ -70,7 +76,8 @@ void PlainTreeFiller::Init() {
7076
throw std::runtime_error("Only 1 output branch");
7177
}
7278
const auto& vars = entries_[0].GetVariables();
73-
vars_.resize(vars.size());
79+
80+
if (vars_.size() != vars.size()) throw std::runtime_error("PlainTreeFiller::Init(): vars_.size() != vars.size()");
7481

7582
file_ = TFile::Open(file_name_.c_str(), "recreate");
7683
plain_tree_ = new TTree(tree_name_.c_str(), "Plain Tree");
@@ -81,7 +88,11 @@ void PlainTreeFiller::Init() {
8188
if (!fields_to_preserve_.empty() && std::find(fields_to_preserve_.begin(), fields_to_preserve_.end(), leaf_name) == fields_to_preserve_.end()) continue;
8289
if (!is_prepend_leaves_with_branchname_) leaf_name.erase(0, branch_name_.size() + 1);
8390
std::replace(leaf_name.begin(), leaf_name.end(), '.', '_');
84-
plain_tree_->Branch(leaf_name.c_str(), &(vars_.at(i)), Form("%s/F", leaf_name.c_str()));
91+
if (vars_.at(i).type_ == Types::kFloat) plain_tree_->Branch(leaf_name.c_str(), &vars_.at(i).float_, Form("%s/F", leaf_name.c_str()));
92+
else if (vars_.at(i).type_ == Types::kInteger)
93+
plain_tree_->Branch(leaf_name.c_str(), &vars_.at(i).int_, Form("%s/I", leaf_name.c_str()));
94+
else if (vars_.at(i).type_ == Types::kBool)
95+
plain_tree_->Branch(leaf_name.c_str(), &vars_.at(i).bool_, Form("%s/O", leaf_name.c_str()));
8596
}
8697

8798
for (auto& cm : cuts_map_) {
@@ -97,7 +108,11 @@ void PlainTreeFiller::Exec() {
97108
for (const auto& channel : values) {
98109
assert(channel.size() == vars_.size());
99110
for (size_t i = 0; i < channel.size(); ++i) {
100-
vars_.at(i) = channel.at(i);
111+
if (vars_.at(i).type_ == Types::kFloat) vars_.at(i).float_ = static_cast<float>(channel.at(i));
112+
else if (vars_.at(i).type_ == Types::kInteger)
113+
vars_.at(i).int_ = static_cast<int>(std::round(channel.at(i)));
114+
else if (vars_.at(i).type_ == Types::kBool)
115+
vars_.at(i).bool_ = static_cast<bool>(std::round(channel.at(i)));
101116
}
102117
plain_tree_->Fill();
103118
}
@@ -109,6 +124,5 @@ void PlainTreeFiller::Finish() {
109124
plain_tree_->Write();
110125
file_->Close();
111126
delete file_;
112-
// delete plain_tree_;
113127
}
114128
}// namespace AnalysisTree

infra/PlainTreeFiller.hpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212

1313
namespace AnalysisTree {
1414

15+
struct FIB {
16+
float float_{-299.f};
17+
int int_{-299};
18+
bool bool_{false};
19+
Types type_{Types::kNumberOfTypes};
20+
};
21+
1522
class PlainTreeFiller : public AnalysisTask {
1623
public:
1724
PlainTreeFiller() = default;
@@ -41,7 +48,7 @@ class PlainTreeFiller : public AnalysisTask {
4148
std::string tree_name_{"PlainTree"};
4249
std::string branch_name_;
4350

44-
std::vector<float> vars_{};
51+
std::vector<FIB> vars_;
4552
std::vector<std::string> fields_to_ignore_{};
4653
std::vector<std::string> fields_to_preserve_{};
4754

0 commit comments

Comments
 (0)