diff --git a/core/BranchConfig.cpp b/core/BranchConfig.cpp index 84677db3..b044f1b3 100644 --- a/core/BranchConfig.cpp +++ b/core/BranchConfig.cpp @@ -136,20 +136,49 @@ BranchConfig BranchConfig::CloneAndMerge(const BranchConfig& attached) const { } template -std::vector VectorConfig::SplitString(const std::string& input) { - std::vector result; - std::vector newlinepositions{-1}; - int it{0}; - while (it < std::string::npos) { - it = input.find("\n", it + 1); - newlinepositions.emplace_back(it); +void VectorConfig::Print() const { + if (map_.empty()) return; + + auto print_row = [](const std::vector>& 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 result; + std::vector 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 diff --git a/core/BranchConfig.hpp b/core/BranchConfig.hpp index dca9f6e2..e11ecdc3 100644 --- a/core/BranchConfig.hpp +++ b/core/BranchConfig.hpp @@ -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>& 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 SplitString(const std::string& input); void RemoveField(const std::string& name, int id); MapType map_{}; ShortInt_t size_{0}; @@ -127,7 +98,7 @@ class BranchConfig : public VectorConfig, public VectorConfig, publi BranchConfig(std::string name, DetType type, std::string title = ""); - void Print() const override; + void Print() const; void PrintBranchId() const; diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index e29f4233..65e011a3 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -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() diff --git a/examples/rootlogon.C b/examples/rootlogon.C deleted file mode 100644 index 3344aef9..00000000 --- a/examples/rootlogon.C +++ /dev/null @@ -1,7 +0,0 @@ -{ - - const TString dir = "../install_17/lib/"; - gSystem->Load(dir + "libAnalysisTreeBase"); - gSystem->Load(dir + "libAnalysisTreeInfra"); - -} diff --git a/infra/AnalysisTreeInfraLinkDef.h b/infra/AnalysisTreeInfraLinkDef.h index 01b8f60f..e1cf768a 100644 --- a/infra/AnalysisTreeInfraLinkDef.h +++ b/infra/AnalysisTreeInfraLinkDef.h @@ -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 diff --git a/infra/HelperFunctions.hpp b/infra/HelperFunctions.hpp index 56b95afc..50a300c7 100644 --- a/infra/HelperFunctions.hpp +++ b/infra/HelperFunctions.hpp @@ -19,18 +19,18 @@ inline std::string ToStringWithPrecision(const T a_value, const int n) { template 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(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(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(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(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 CreateRangeCuts(const std::vector& ranges, const std::string& cutNamePrefix, const std::string& branchFieldName, int precision=2) { +inline std::vector CreateRangeCuts(const std::vector& ranges, const std::string& cutNamePrefix, const std::string& branchFieldName, int precision = 2) { std::vector 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); @@ -40,7 +40,7 @@ inline std::vector CreateRangeCuts(const std::vector CreateEqualCuts(const std::vector& values, const std::string& cutNamePrefix, const std::string& branchFieldName, int precision=2) { +inline std::vector CreateEqualCuts(const std::vector& values, const std::string& cutNamePrefix, const std::string& branchFieldName, int precision = 2) { std::vector sliceCuts; for (int iValue = 0; iValue < values.size(); iValue++) { const std::string cutName = cutNamePrefix + ToStringWithPrecision(values.at(iValue), precision); diff --git a/infra/PlainTreeFiller.cpp b/infra/PlainTreeFiller.cpp index ba274128..1300241e 100644 --- a/infra/PlainTreeFiller.cpp +++ b/infra/PlainTreeFiller.cpp @@ -15,7 +15,7 @@ void PlainTreeFiller::AddBranch(const std::string& branch_name) { in_branches_.emplace(branch_name); } -void PlainTreeFiller::SetFieldsToIgnore(const std::vector&& fields_to_ignore) { +void PlainTreeFiller::SetFieldsToIgnore(const std::vector& fields_to_ignore) { if (branch_name_ == "") { throw std::runtime_error("PlainTreeFiller::SetFieldsToIgnore() must be called after PlainTreeFiller::AddBranch()\n"); } @@ -24,6 +24,15 @@ void PlainTreeFiller::SetFieldsToIgnore(const std::vector&& fields_ } } +void PlainTreeFiller::SetFieldsToPreserve(const std::vector& 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 defaultFieldsNames; @@ -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()) { @@ -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())); } diff --git a/infra/PlainTreeFiller.hpp b/infra/PlainTreeFiller.hpp index 8f7c0cb1..1bf9c5d4 100644 --- a/infra/PlainTreeFiller.hpp +++ b/infra/PlainTreeFiller.hpp @@ -27,9 +27,11 @@ class PlainTreeFiller : public AnalysisTask { tree_name_ = std::move(tree); } - void SetFieldsToIgnore(const std::vector&& fields_to_ignore); + void SetFieldsToIgnore(const std::vector& fields_to_ignore); + void SetFieldsToPreserve(const std::vector& 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}; @@ -41,8 +43,10 @@ class PlainTreeFiller : public AnalysisTask { std::vector vars_{}; std::vector fields_to_ignore_{}; + std::vector fields_to_preserve_{}; bool is_ignore_defual_fields_{false}; + bool is_prepend_leaves_with_branchname_{true}; }; }// namespace AnalysisTree diff --git a/infra/SimpleCut.cpp b/infra/SimpleCut.cpp index 29302b46..537d4e64 100644 --- a/infra/SimpleCut.cpp +++ b/infra/SimpleCut.cpp @@ -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_; } diff --git a/infra/SimpleCut.hpp b/infra/SimpleCut.hpp index 1e9171cd..cab78068 100644 --- a/infra/SimpleCut.hpp +++ b/infra/SimpleCut.hpp @@ -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& vars, std::function&)> lambda, std::string title = "") : title_(std::move(title)), @@ -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_ } /** diff --git a/infra/SimpleCut.test.cpp b/infra/SimpleCut.test.cpp index 9bfc05d6..a7331078 100644 --- a/infra/SimpleCut.test.cpp +++ b/infra/SimpleCut.test.cpp @@ -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); @@ -44,21 +44,21 @@ TEST(SimpleCut, Basics) { EXPECT_FALSE(rcut1 == rcut3); EXPECT_FALSE(rcut2 == rcut3); - SimpleCut lcut1 = SimpleCut((std::vector){"B1.f1", "B2.f2", "B3.f3"}, [](std::vector par){ return par[0]/par[1]*par[2] < 10; }); - SimpleCut lcut1same = SimpleCut((std::vector){"B1.f1", "B2.f2", "B3.f3"}, [](std::vector par){ return par[0]/par[1]*par[2] < 10; }); - SimpleCut lcut2 = SimpleCut((std::vector){"B11.f11", "B22.f22", "B33.f33"}, [](std::vector par){ return par[0]/par[1]*par[2] < 5; }); + SimpleCut lcut1 = SimpleCut((std::vector){"B1.f1", "B2.f2", "B3.f3"}, [](std::vector par) { return par[0] / par[1] * par[2] < 10; }); + SimpleCut lcut1same = SimpleCut((std::vector){"B1.f1", "B2.f2", "B3.f3"}, [](std::vector par) { return par[0] / par[1] * par[2] < 10; }); + SimpleCut lcut2 = SimpleCut((std::vector){"B11.f11", "B22.f22", "B33.f33"}, [](std::vector 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_