Skip to content

Commit

Permalink
improve SimpleCut comparison (still not perfect; if possible to deal …
Browse files Browse the repository at this point in the history
…with rest?)
  • Loading branch information
lubynets committed Feb 26, 2025
1 parent f38bf10 commit 524263b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
12 changes: 11 additions & 1 deletion infra/SimpleCut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
Authors: Viktor Klochkov, Ilya Selyuzhenkov */
#include "SimpleCut.hpp"

#include "HelperFunctions.hpp"

#include <iostream>

namespace AnalysisTree {
Expand All @@ -22,7 +24,9 @@ bool operator==(const SimpleCut& that, const SimpleCut& other) {
if (&that == &other) {
return true;
}
return that.vars_ == other.vars_ && that.title_ == other.title_;
// 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;
return that.vars_ == other.vars_ && that.title_ == other.title_ && that.hash_ == other.hash_;
}

SimpleCut RangeCut(const std::string& variable_name, double lo, double hi, const std::string& title) {
Expand All @@ -45,12 +49,18 @@ SimpleCut::SimpleCut(const Variable& var, int value, std::string title) : title_
vars_.emplace_back(var);
lambda_ = [value](std::vector<double>& vars) { return vars[0] <= value + SmallNumber && vars[0] >= value - SmallNumber; };
FillBranchNames();
const std::string stringForHash = var.GetName() + HelperFunctions::ToStringWithPrecision(value, 6) + title_;
std::hash<std::string> hasher;
hash_ = hasher(stringForHash);
}

SimpleCut::SimpleCut(const Variable& var, double min, double max, std::string title) : title_(std::move(title)) {
vars_.emplace_back(var);
lambda_ = [max, min](std::vector<double>& vars) { return vars[0] <= max && vars[0] >= min; };
FillBranchNames();
const std::string stringForHash = var.GetName() + HelperFunctions::ToStringWithPrecision(min, 6) + HelperFunctions::ToStringWithPrecision(max, 6) + title_;
std::hash<std::string> hasher;
hash_ = hasher(stringForHash);
}

bool SimpleCut::Apply(std::vector<const BranchChannel*>& bch, std::vector<size_t>& id) const {
Expand Down
3 changes: 3 additions & 0 deletions infra/SimpleCut.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class SimpleCut {
[](const std::string& arg_name) { return Variable::FromString(arg_name); });

FillBranchNames();
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 @@ -49,6 +50,7 @@ class SimpleCut {
vars_.emplace_back(var);
}
FillBranchNames();
hash_ = 1; // Impossible to calculate for lambda_
}

/**
Expand Down Expand Up @@ -121,6 +123,7 @@ class SimpleCut {
std::vector<Variable> vars_{};
std::set<std::string> branch_names_{};
std::function<bool(std::vector<double>&)> lambda_;///< function used to evaluate the cut.
size_t hash_;

ClassDef(SimpleCut, 1);
};
Expand Down

0 comments on commit 524263b

Please sign in to comment.