Skip to content

Commit bb048ef

Browse files
authored
DPL Analysis: add clamp expression node to constrain a result of an expresison between two values (#14305)
1 parent 981cd40 commit bb048ef

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

Framework/Core/include/Framework/Expressions.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,14 @@ inline Node updateParameters(Node const& pexp, int bins, std::vector<T> const& p
546546
return result;
547547
}
548548

549+
/// clamping functional
550+
template <typename T>
551+
inline Node clamp(Node&& expr, T low, T hi)
552+
{
553+
auto copy = expr;
554+
return ifnode(Node{copy} < LiteralNode{low}, LiteralNode{low}, ifnode(Node{copy} > LiteralNode{hi}, LiteralNode{hi}, Node{copy}));
555+
}
556+
549557
/// A struct, containing the root of the expression tree
550558
struct Filter {
551559
Filter() = default;

Framework/Core/test/test_Expressions.cxx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,13 @@ TEST_CASE("TestConditionalExpressions")
290290
auto gandiva_condition2 = makeCondition(gandiva_tree2);
291291
auto gandiva_filter2 = createFilter(schema2, gandiva_condition2);
292292
REQUIRE(gandiva_tree2->ToString() == "bool greater_than((float) fSigned1Pt, (const float) 0 raw(0)) && if (bool less_than(float absf((float) fEta), (const float) 1 raw(3f800000)) && if (bool less_than((float) fPt, (const float) 1 raw(3f800000))) { bool greater_than((float) fPhi, (const float) 1.5708 raw(3fc90fdb)) } else { bool less_than((float) fPhi, (const float) 1.5708 raw(3fc90fdb)) }) { bool greater_than(float absf((float) fX), (const float) 1 raw(3f800000)) } else { bool greater_than(float absf((float) fY), (const float) 1 raw(3f800000)) }");
293+
294+
// clamp
295+
Projector clp = clamp(o2::aod::track::pt, 1.0f, 10.f);
296+
auto clpspecs = createOperations(clp);
297+
auto schemaclp = std::make_shared<arrow::Schema>(std::vector{o2::aod::track::Pt::asArrowField()});
298+
auto gandiva_tree_clp = createExpressionTree(clpspecs, schemaclp);
299+
REQUIRE(gandiva_tree_clp->ToString() == "if (bool less_than((float) fPt, (const float) 1 raw(3f800000))) { (const float) 1 raw(3f800000) } else { if (bool greater_than((float) fPt, (const float) 10 raw(41200000))) { (const float) 10 raw(41200000) } else { (float) fPt } }");
293300
}
294301

295302
TEST_CASE("TestBinnedExpressions")

0 commit comments

Comments
 (0)