ATLAS Offline Software
Loading...
Searching...
No Matches
InferenceUtils.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4#ifndef MUONINFERENCE_INFERENCEUTILS_H
5#define MUONINFERENCE_INFERENCEUTILS_H
6
9
10#include <algorithm>
11#include <cmath>
12
14
16 bool isCuda{false};
18};
19
20template <class SessionToolHandle>
21SessionBackend sessionBackend(const SessionToolHandle& sessionTool) {
22 if (const auto* cudaTool = dynamic_cast<const AthOnnx::OnnxRuntimeSessionToolCUDA*>(sessionTool.get())) {
23 return SessionBackend{true, cudaTool->deviceId()};
24 }
25 return SessionBackend{};
26}
27
28inline float sigmoid(float x) {
29 if (x >= 0.f) {
30 const float z = std::exp(-x);
31 return 1.f / (1.f + z);
32 }
33 const float z = std::exp(x);
34 return z / (1.f + z);
35}
36
37inline float reducedChi2(const xAOD::MuonSegment& segment) {
38 return segment.chiSquared() / std::max(1.f, segment.numberDoF());
39}
40
42inline int compareFloat(float first, float second) {
43 if (std::isnan(first)) return std::isnan(second) ? 0 : 1;
44 if (std::isnan(second)) return -1;
45 if (first < second) return -1;
46 if (first > second) return 1;
47 return 0;
48}
49
51inline int compareFloatDescending(float first, float second) {
52 if (std::isnan(first)) return std::isnan(second) ? 0 : 1;
53 if (std::isnan(second)) return -1;
54 if (first > second) return -1;
55 if (first < second) return 1;
56 return 0;
57}
58
67 bool operator()(const xAOD::MuonSegment* first,
68 const xAOD::MuonSegment* second) const {
69 if (first->nPrecisionHits() != second->nPrecisionHits()) {
70 return first->nPrecisionHits() > second->nPrecisionHits();
71 }
72 if (first->nPhiLayers() != second->nPhiLayers()) {
73 return first->nPhiLayers() > second->nPhiLayers();
74 }
75 if (first->nTrigEtaLayers() != second->nTrigEtaLayers()) {
76 return first->nTrigEtaLayers() > second->nTrigEtaLayers();
77 }
78 const int chi2Order = compareFloat(reducedChi2(*first),
79 reducedChi2(*second));
80 if (chi2Order != 0) return chi2Order < 0;
81 return first->index() < second->index();
82 }
83};
84
85} // namespace MuonML::InferenceUtils
86
87#endif // MUONINFERENCE_INFERENCEUTILS_H
#define x
#define z
float numberDoF() const
Returns the numberDoF.
float chiSquared() const
int compareFloat(float first, float second)
Three-way float comparison which orders NaN after all numeric values.
SessionBackend sessionBackend(const SessionToolHandle &sessionTool)
int compareFloatDescending(float first, float second)
Three-way descending comparison which also orders NaN last.
float reducedChi2(const xAOD::MuonSegment &segment)
MuonSegment_v1 MuonSegment
Reference the current persistent version:
Common quality ordering for segment representatives.
bool operator()(const xAOD::MuonSegment *first, const xAOD::MuonSegment *second) const