Skip to content

Commit 524263b

Browse files
committed
improve SimpleCut comparison (still not perfect; if possible to deal with rest?)
1 parent f38bf10 commit 524263b

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

infra/SimpleCut.cpp

+11-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
Authors: Viktor Klochkov, Ilya Selyuzhenkov */
44
#include "SimpleCut.hpp"
55

6+
#include "HelperFunctions.hpp"
7+
68
#include <iostream>
79

810
namespace AnalysisTree {
@@ -22,7 +24,9 @@ bool operator==(const SimpleCut& that, const SimpleCut& other) {
2224
if (&that == &other) {
2325
return true;
2426
}
25-
return that.vars_ == other.vars_ && that.title_ == other.title_;
27+
// if both SimpleCuts were defined via lambda, they're assumed not equal (unless they're in the same memory place)
28+
if(that.hash_ == 1 && other.hash_ == 1) return false;
29+
return that.vars_ == other.vars_ && that.title_ == other.title_ && that.hash_ == other.hash_;
2630
}
2731

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

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

5666
bool SimpleCut::Apply(std::vector<const BranchChannel*>& bch, std::vector<size_t>& id) const {

infra/SimpleCut.hpp

+3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class SimpleCut {
4141
[](const std::string& arg_name) { return Variable::FromString(arg_name); });
4242

4343
FillBranchNames();
44+
hash_ = 1; // Impossible to calculate for lambda_
4445
}
4546

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

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

125128
ClassDef(SimpleCut, 1);
126129
};

0 commit comments

Comments
 (0)