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
53 changes: 41 additions & 12 deletions core/BranchConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,20 +136,49 @@ BranchConfig BranchConfig::CloneAndMerge(const BranchConfig& attached) const {
}

template<typename T>
std::vector<std::string> VectorConfig<T>::SplitString(const std::string& input) {
std::vector<std::string> result;
std::vector<int> newlinepositions{-1};
int it{0};
while (it < std::string::npos) {
it = input.find("\n", it + 1);
newlinepositions.emplace_back(it);
void VectorConfig<T>::Print() const {
if (map_.empty()) return;

auto print_row = [](const std::vector<std::pair<std::string, int>>& elements) {
for (const auto& el : elements) {
std::cout << std::left << std::setw(el.second) << std::setfill(' ') << el.first;
}
std::cout << std::endl;
};

int name_strlen{0};
for (const auto& entry : map_) {
name_strlen = std::max(name_strlen, (int) entry.first.length());
}
newlinepositions.back() = input.size();
for (int ip = 0; ip < newlinepositions.size() - 1; ++ip) {
result.emplace_back(input.substr(newlinepositions.at(ip) + 1, newlinepositions.at(ip + 1) - newlinepositions.at(ip) - 1));
name_strlen += 4;

auto SplitString = [](const std::string& input) {
std::vector<std::string> result;
std::vector<int> newlinepositions{-1};
int it{0};
while (it < std::string::npos) {
it = input.find("\n", it + 1);
newlinepositions.emplace_back(it);
}
newlinepositions.back() = input.size();
for (int ip = 0; ip < newlinepositions.size() - 1; ++ip) {
result.emplace_back(input.substr(newlinepositions.at(ip) + 1, newlinepositions.at(ip + 1) - newlinepositions.at(ip) - 1));
}
return result;
};

print_row({{"Id", 10}, {"Name", name_strlen}, {"Info", 50}});
for (const auto& entry : map_) {
if (entry.second.title_.find("\n") == std::string::npos) {
print_row({{std::to_string(entry.second.id_), 10}, {entry.first, name_strlen}, {entry.second.title_, 50}});
} else {
auto est = SplitString(entry.second.title_);
print_row({{std::to_string(entry.second.id_), 10}, {entry.first, name_strlen}, {est.at(0), 50}});
for (int iest = 1; iest < est.size(); ++iest) {
print_row({{"", 10}, {"", name_strlen}, {est.at(iest), 50}});
}
}
}

return result;
}

template<typename T>
Expand Down
33 changes: 2 additions & 31 deletions core/BranchConfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,38 +73,9 @@ class VectorConfig {
ANALYSISTREE_ATTR_NODISCARD virtual const MapType& GetMap() const { return map_; }
ANALYSISTREE_ATTR_NODISCARD virtual ShortInt_t GetSize() const { return size_; }

virtual void Print() const {
if (map_.empty()) return;

auto print_row = [](const std::vector<std::pair<std::string, int>>& elements) {
for (const auto& el : elements) {
std::cout << std::left << std::setw(el.second) << std::setfill(' ') << el.first;
}
std::cout << std::endl;
};

int name_strlen{0};
for (const auto& entry : map_) {
name_strlen = std::max(name_strlen, (int) entry.first.length());
}
name_strlen += 4;

print_row({{"Id", 10}, {"Name", name_strlen}, {"Info", 50}});
for (const auto& entry : map_) {
if (entry.second.title_.find("\n") == std::string::npos) {
print_row({{std::to_string(entry.second.id_), 10}, {entry.first, name_strlen}, {entry.second.title_, 50}});
} else {
auto est = SplitString(entry.second.title_);
print_row({{std::to_string(entry.second.id_), 10}, {entry.first, name_strlen}, {est.at(0), 50}});
for (int iest = 1; iest < est.size(); ++iest) {
print_row({{"", 10}, {"", name_strlen}, {est.at(iest), 50}});
}
}
}
}
void Print() const;

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

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

void Print() const override;
void Print() const;

void PrintBranchId() const;

Expand Down
1 change: 1 addition & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,5 @@ foreach(EXE ${EXECUTABLES})
add_executable(${EXE} ${EXE}.cpp)
target_link_libraries(${EXE} AnalysisTreeBase AnalysisTreeInfra AnalysisTreeUser ${ROOT_LIBRARIES} EG)
install (TARGETS ${EXE} RUNTIME DESTINATION bin)
install (FILES ${EXE}.cpp DESTINATION share)
endforeach()
7 changes: 0 additions & 7 deletions examples/rootlogon.C

This file was deleted.

1 change: 0 additions & 1 deletion infra/AnalysisTreeInfraLinkDef.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#pragma link C++ class AnalysisTree::AnalysisTask;
#pragma link C++ class AnalysisTree::Chain+;

#pragma link C++ class AnalysisTree::BranchReader+;
#pragma link C++ class AnalysisTree::AnalysisEntry+;

#endif
20 changes: 10 additions & 10 deletions infra/HelperFunctions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@ inline std::string ToStringWithPrecision(const T a_value, const int n) {

template<typename T>
inline std::string ToStringWithSignificantFigures(const T a_value, const int n) {
if(a_value == 0) return "0";

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)
const int iMag = static_cast<int>(dMag-n+1 > 0 ? dMag-n+1 : dMag-n);
const T shifted_value = a_value/std::pow(10, iMag); // shift decimal point to have all required digits to l.h.s. from it
const T rounded_value = static_cast<T>(std::round(shifted_value)); // get rid of r.h.s. from decimal point
const T reshifted_value = rounded_value*std::pow(10, iMag); // return decimal point to its original place
const int precision = iMag < 0 ? -iMag : 0; // determine how many digits after decimal point one needs
if (a_value == 0) return "0";

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)
const int iMag = static_cast<int>(dMag - n + 1 > 0 ? dMag - n + 1 : dMag - n);
const T shifted_value = a_value / std::pow(10, iMag); // shift decimal point to have all required digits to l.h.s. from it
const T rounded_value = static_cast<T>(std::round(shifted_value));// get rid of r.h.s. from decimal point
const T reshifted_value = rounded_value * std::pow(10, iMag); // return decimal point to its original place
const int precision = iMag < 0 ? -iMag : 0; // determine how many digits after decimal point one needs
return ToStringWithPrecision(reshifted_value, precision);
}

inline std::vector<AnalysisTree::SimpleCut> CreateRangeCuts(const std::vector<float>& ranges, const std::string& cutNamePrefix, const std::string& branchFieldName, int precision=2) {
inline std::vector<AnalysisTree::SimpleCut> CreateRangeCuts(const std::vector<float>& ranges, const std::string& cutNamePrefix, const std::string& branchFieldName, int precision = 2) {
std::vector<AnalysisTree::SimpleCut> sliceCuts;
for (int iRange = 0; iRange < ranges.size() - 1; iRange++) {
const std::string cutName = cutNamePrefix + ToStringWithPrecision(ranges.at(iRange), 2) + "_" + ToStringWithPrecision(ranges.at(iRange + 1), precision);
Expand All @@ -40,7 +40,7 @@ inline std::vector<AnalysisTree::SimpleCut> CreateRangeCuts(const std::vector<fl
return sliceCuts;
}

inline std::vector<AnalysisTree::SimpleCut> CreateEqualCuts(const std::vector<float>& values, const std::string& cutNamePrefix, const std::string& branchFieldName, int precision=2) {
inline std::vector<AnalysisTree::SimpleCut> CreateEqualCuts(const std::vector<float>& values, const std::string& cutNamePrefix, const std::string& branchFieldName, int precision = 2) {
std::vector<AnalysisTree::SimpleCut> sliceCuts;
for (int iValue = 0; iValue < values.size(); iValue++) {
const std::string cutName = cutNamePrefix + ToStringWithPrecision(values.at(iValue), precision);
Expand Down
19 changes: 17 additions & 2 deletions infra/PlainTreeFiller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ void PlainTreeFiller::AddBranch(const std::string& branch_name) {
in_branches_.emplace(branch_name);
}

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

void PlainTreeFiller::SetFieldsToPreserve(const std::vector<std::string>& fields_to_preserve) {
if (branch_name_ == "") {
throw std::runtime_error("PlainTreeFiller::SetFieldsToPreserve() must be called after PlainTreeFiller::AddBranch()\n");
}
for (auto& fti : fields_to_preserve) {
fields_to_preserve_.emplace_back((branch_name_ + "." + fti).c_str());
}
}

void PlainTreeFiller::Init() {
if (is_ignore_defual_fields_) {
std::vector<std::string> defaultFieldsNames;
Expand All @@ -38,6 +47,10 @@ void PlainTreeFiller::Init() {
SetFieldsToIgnore(std::move(defaultFieldsNames));
}

if (!fields_to_ignore_.empty() && !fields_to_preserve_.empty()) {
throw std::runtime_error("PlainTreeFiller::Init() - only one of fields_to_ignore_ and fields_to_preserve_ can be set");
}

if (!branch_name_.empty()) {
const auto& branch_config = config_->GetBranchConfig(branch_name_);
for (const auto& field : branch_config.GetMap<float>()) {
Expand All @@ -64,7 +77,9 @@ void PlainTreeFiller::Init() {
plain_tree_->SetAutoSave(0);
for (size_t i = 0; i < vars.size(); ++i) {
std::string leaf_name = vars[i].GetName();
if (std::find(fields_to_ignore_.begin(), fields_to_ignore_.end(), leaf_name) != fields_to_ignore_.end()) continue;
if (!fields_to_ignore_.empty() && std::find(fields_to_ignore_.begin(), fields_to_ignore_.end(), leaf_name) != fields_to_ignore_.end()) continue;
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()));
}
Expand Down
6 changes: 5 additions & 1 deletion infra/PlainTreeFiller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ class PlainTreeFiller : public AnalysisTask {
tree_name_ = std::move(tree);
}

void SetFieldsToIgnore(const std::vector<std::string>&& fields_to_ignore);
void SetFieldsToIgnore(const std::vector<std::string>& fields_to_ignore);
void SetFieldsToPreserve(const std::vector<std::string>& fields_to_preserve);

void SetIsIgnoreDefaultFields(bool is = true) { is_ignore_defual_fields_ = is; }
void SetIsPrependLeavesWithBranchName(bool is = true) { is_prepend_leaves_with_branchname_ = is; }

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

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

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

}// namespace AnalysisTree
Expand Down
2 changes: 1 addition & 1 deletion infra/SimpleCut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ bool operator==(const SimpleCut& that, const SimpleCut& other) {
return true;
}
// if both SimpleCuts were defined via lambda, they're assumed not equal (unless they're in the same memory place)
if(that.hash_ == 1 && other.hash_ == 1) return false;
if (that.hash_ == 1 && other.hash_ == 1) return false;
return that.vars_ == other.vars_ && that.title_ == other.title_ && that.hash_ == other.hash_;
}

Expand Down
4 changes: 2 additions & 2 deletions infra/SimpleCut.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class SimpleCut {
[](const std::string& arg_name) { return Variable::FromString(arg_name); });

FillBranchNames();
hash_ = 1; // Impossible to calculate for lambda_
hash_ = 1;// Impossible to calculate for lambda_
}

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

/**
Expand Down
16 changes: 8 additions & 8 deletions infra/SimpleCut.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ TEST(SimpleCut, Basics) {
EXPECT_FALSE(ecut1 == ecut2);
EXPECT_FALSE(ecut1 == ecut3);
EXPECT_FALSE(ecut2 == ecut3);

SimpleCut rcut1 = RangeCut(Variable::FromString("BranchName.FieldName"), 1, 2);
SimpleCut rcut1same = RangeCut(Variable::FromString("BranchName.FieldName"), 1, 2);
SimpleCut rcut2 = RangeCut(Variable::FromString("BranchName.FieldName"), 2, 5);
SimpleCut rcut3 = RangeCut(Variable::FromString("BranchName3.FieldName3"), 1, 2);
SimpleCut rcut1copy = rcut1;
SimpleCut& rcut1ref = rcut1;

EXPECT_TRUE(rcut1 == rcut1);
EXPECT_TRUE(rcut1 == rcut1same);
EXPECT_TRUE(rcut1 == rcut1copy);
Expand All @@ -44,21 +44,21 @@ TEST(SimpleCut, Basics) {
EXPECT_FALSE(rcut1 == rcut3);
EXPECT_FALSE(rcut2 == rcut3);

SimpleCut lcut1 = SimpleCut((std::vector<std::string>){"B1.f1", "B2.f2", "B3.f3"}, [](std::vector<double> par){ return par[0]/par[1]*par[2] < 10; });
SimpleCut lcut1same = SimpleCut((std::vector<std::string>){"B1.f1", "B2.f2", "B3.f3"}, [](std::vector<double> par){ return par[0]/par[1]*par[2] < 10; });
SimpleCut lcut2 = SimpleCut((std::vector<std::string>){"B11.f11", "B22.f22", "B33.f33"}, [](std::vector<double> par){ return par[0]/par[1]*par[2] < 5; });
SimpleCut lcut1 = SimpleCut((std::vector<std::string>){"B1.f1", "B2.f2", "B3.f3"}, [](std::vector<double> par) { return par[0] / par[1] * par[2] < 10; });
SimpleCut lcut1same = SimpleCut((std::vector<std::string>){"B1.f1", "B2.f2", "B3.f3"}, [](std::vector<double> par) { return par[0] / par[1] * par[2] < 10; });
SimpleCut lcut2 = SimpleCut((std::vector<std::string>){"B11.f11", "B22.f22", "B33.f33"}, [](std::vector<double> par) { return par[0] / par[1] * par[2] < 5; });
SimpleCut lcut1copy = lcut1;
SimpleCut& lcut1ref = lcut1;

EXPECT_TRUE(lcut1 == lcut1);
EXPECT_FALSE(lcut1 == lcut1same); // it should not be like that, but this is a concession
EXPECT_FALSE(lcut1 == lcut1copy); // it should not be like that, but this is a concession
EXPECT_FALSE(lcut1 == lcut1same);// it should not be like that, but this is a concession
EXPECT_FALSE(lcut1 == lcut1copy);// it should not be like that, but this is a concession
EXPECT_TRUE(lcut1 == lcut1ref);
EXPECT_FALSE(lcut1 == lcut2);
EXPECT_FALSE(lcut1 == ecut1);
EXPECT_FALSE(lcut1 == rcut1);
}

}
}// namespace

#endif//ANALYSISTREE_INFRA_SIMPLECUT_TEST_HPP_