Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 27 additions & 12 deletions infra/GenericContainerFiller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,33 @@

#include "GenericContainerFiller.hpp"

#include <fstream>

using namespace AnalysisTree;

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

void GenericContainerFiller::Init() {
file_in_ = TFile::Open(file_in_name_.c_str(), "read");
if (file_in_ == nullptr) throw std::runtime_error("GenericContainerFiller::Run(): file_in_ == nullptr");
tree_in_ = new TChain(tree_in_name_.c_str());

auto ends_with = [](const std::string& str, const std::string& suffix) {
if (suffix.size() > str.size()) return false;
return std::equal(suffix.rbegin(), suffix.rend(), str.rbegin());
};

if (ends_with(file_in_name_, ".root")) {
tree_in_->Add(file_in_name_.c_str());
} else {
std::ifstream filelist(file_in_name_);
std::string line;

tree_in_ = file_in_->Get<TTree>(tree_in_name_.c_str());
if (tree_in_ == nullptr) throw std::runtime_error("GenericContainerFiller::Run(): tree_in_ == nullptr");
if (!filelist) throw std::runtime_error("GenericContainerFiller::Init(): filelist " + file_in_name_ + " is missing");

while (std::getline(filelist, line)) {
tree_in_->Add(line.c_str());
}
}

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

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

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

generic_detector_ = new GenericDetector(branchConfig.GetId());
Expand Down Expand Up @@ -74,7 +89,7 @@ void GenericContainerFiller::Finish() {
config_.Write("Configuration");
tree_out_->Write();
file_out_->Close();
file_in_->Close();
delete tree_in_;
}

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

void GenericContainerFiller::SetAddressFICS(TBranch* branch, const IndexMap& imap, FICS& ficc) {
if (imap.field_type_ == "TLeafF") branch->SetAddress(&ficc.float_);
void GenericContainerFiller::SetAddressFICS(const std::string& branchName, const IndexMap& imap, FICS& ficc) {
if (imap.field_type_ == "TLeafF") tree_in_->SetBranchAddress(branchName.c_str(), &ficc.float_);
else if (imap.field_type_ == "TLeafI")
branch->SetAddress(&ficc.int_);
tree_in_->SetBranchAddress(branchName.c_str(), &ficc.int_);
else if (imap.field_type_ == "TLeafB")
branch->SetAddress(&ficc.char_);
tree_in_->SetBranchAddress(branchName.c_str(), &ficc.char_);
else if (imap.field_type_ == "TLeafS")
branch->SetAddress(&ficc.short_);
tree_in_->SetBranchAddress(branchName.c_str(), &ficc.short_);
else
throw std::runtime_error("GenericContainerFiller::SetAddressFICS(): unsupported filed type " + imap.field_type_);
}
Expand Down
11 changes: 5 additions & 6 deletions infra/GenericContainerFiller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
#include "Container.hpp"
#include "Detector.hpp"

#include <TChain.h>
#include <TFile.h>
#include <TTree.h>

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

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

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

std::string file_in_name_;
Expand All @@ -73,8 +73,7 @@ class GenericContainerFiller {
std::string tree_out_name_{"aTree"};
std::string branch_out_name_{"PlainBranch"};

TFile* file_in_{nullptr};
TTree* tree_in_{nullptr};
TChain* tree_in_{nullptr};
TFile* file_out_{nullptr};
TTree* tree_out_{nullptr};

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

// variable, change of value of which triggers switch to a new AT event
std::string entry_switch_trigger_var_name_{""};
std::string entry_switch_trigger_var_name_;

int entry_switch_trigger_id_{-1};

Expand Down
28 changes: 21 additions & 7 deletions infra/PlainTreeFiller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ void PlainTreeFiller::AddBranch(const std::string& branch_name) {
}

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

void PlainTreeFiller::SetFieldsToPreserve(const std::vector<std::string>& fields_to_preserve) {
if (branch_name_ == "") {
if (branch_name_.empty()) {
throw std::runtime_error("PlainTreeFiller::SetFieldsToPreserve() must be called after PlainTreeFiller::AddBranch()\n");
}
for (auto& fti : fields_to_preserve) {
Expand All @@ -44,7 +44,7 @@ void PlainTreeFiller::Init() {
if (me.second.id_ < 0) defaultFieldsNames.emplace_back(me.first);
}
}
SetFieldsToIgnore(std::move(defaultFieldsNames));
SetFieldsToIgnore(defaultFieldsNames);
}

if (!fields_to_ignore_.empty() && !fields_to_preserve_.empty()) {
Expand All @@ -55,12 +55,18 @@ void PlainTreeFiller::Init() {
const auto& branch_config = config_->GetBranchConfig(branch_name_);
for (const auto& field : branch_config.GetMap<float>()) {
AnalysisTask::AddEntry(AnalysisEntry({Variable(branch_name_, field.first)}));
vars_.emplace_back();
vars_.back().type_ = Types::kFloat;
}
for (const auto& field : branch_config.GetMap<int>()) {
AnalysisTask::AddEntry(AnalysisEntry({Variable(branch_name_, field.first)}));
vars_.emplace_back();
vars_.back().type_ = Types::kInteger;
}
for (const auto& field : branch_config.GetMap<bool>()) {
AnalysisTask::AddEntry(AnalysisEntry({Variable(branch_name_, field.first)}));
vars_.emplace_back();
vars_.back().type_ = Types::kBool;
}
}

Expand All @@ -70,7 +76,8 @@ void PlainTreeFiller::Init() {
throw std::runtime_error("Only 1 output branch");
}
const auto& vars = entries_[0].GetVariables();
vars_.resize(vars.size());

if (vars_.size() != vars.size()) throw std::runtime_error("PlainTreeFiller::Init(): vars_.size() != vars.size()");

file_ = TFile::Open(file_name_.c_str(), "recreate");
plain_tree_ = new TTree(tree_name_.c_str(), "Plain Tree");
Expand All @@ -81,7 +88,11 @@ void PlainTreeFiller::Init() {
if (!fields_to_preserve_.empty() && std::find(fields_to_preserve_.begin(), fields_to_preserve_.end(), leaf_name) == fields_to_preserve_.end()) continue;
if (!is_prepend_leaves_with_branchname_) leaf_name.erase(0, branch_name_.size() + 1);
std::replace(leaf_name.begin(), leaf_name.end(), '.', '_');
plain_tree_->Branch(leaf_name.c_str(), &(vars_.at(i)), Form("%s/F", leaf_name.c_str()));
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()));
else if (vars_.at(i).type_ == Types::kInteger)
plain_tree_->Branch(leaf_name.c_str(), &vars_.at(i).int_, Form("%s/I", leaf_name.c_str()));
else if (vars_.at(i).type_ == Types::kBool)
plain_tree_->Branch(leaf_name.c_str(), &vars_.at(i).bool_, Form("%s/O", leaf_name.c_str()));
}

for (auto& cm : cuts_map_) {
Expand All @@ -97,7 +108,11 @@ void PlainTreeFiller::Exec() {
for (const auto& channel : values) {
assert(channel.size() == vars_.size());
for (size_t i = 0; i < channel.size(); ++i) {
vars_.at(i) = channel.at(i);
if (vars_.at(i).type_ == Types::kFloat) vars_.at(i).float_ = static_cast<float>(channel.at(i));
else if (vars_.at(i).type_ == Types::kInteger)
vars_.at(i).int_ = static_cast<int>(std::round(channel.at(i)));
else if (vars_.at(i).type_ == Types::kBool)
vars_.at(i).bool_ = static_cast<bool>(std::round(channel.at(i)));
}
plain_tree_->Fill();
}
Expand All @@ -109,6 +124,5 @@ void PlainTreeFiller::Finish() {
plain_tree_->Write();
file_->Close();
delete file_;
// delete plain_tree_;
}
}// namespace AnalysisTree
9 changes: 8 additions & 1 deletion infra/PlainTreeFiller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@

namespace AnalysisTree {

struct FIB {
float float_{-299.f};
int int_{-299};
bool bool_{false};
Types type_{Types::kNumberOfTypes};
};

class PlainTreeFiller : public AnalysisTask {
public:
PlainTreeFiller() = default;
Expand Down Expand Up @@ -41,7 +48,7 @@ class PlainTreeFiller : public AnalysisTask {
std::string tree_name_{"PlainTree"};
std::string branch_name_;

std::vector<float> vars_{};
std::vector<FIB> vars_;
std::vector<std::string> fields_to_ignore_{};
std::vector<std::string> fields_to_preserve_{};

Expand Down