13 #include "GaudiKernel/SystemOfUnits.h"
14 #include "Gaudi/Property.h"
21 #include <onnxruntime_cxx_api.h>
34 ATH_CHECK(m_filterParams.initialize(
false));
35 ATH_CHECK( m_eventInfoContainerName.initialize() );
36 ATH_CHECK( m_truthParticleName.initialize( (m_pileupSwitch ==
"HardScatter" or m_pileupSwitch ==
"All") and not m_truthParticleName.key().empty() ) );
37 ATH_CHECK(m_truthSelectionTool.retrieve(EnableTool {not m_truthParticleName.key().empty()} ));
38 ATH_CHECK( m_truthEventName.initialize( (m_pileupSwitch ==
"HardScatter" or m_pileupSwitch ==
"All") and not m_truthEventName.key().empty() ) );
39 ATH_CHECK( m_truthPileUpEventName.initialize( (m_pileupSwitch ==
"PileUp" or m_pileupSwitch ==
"All") and not m_truthPileUpEventName.key().empty() ) );
41 std::string this_file = __FILE__;
43 Ort::SessionOptions session_options;
45 m_session = std::make_unique<Ort::Session>(m_svc->env(), model_path.c_str(), session_options);
46 m_inputInfo = TrackOverlayDecisionAlg::GetInputNodeInfo(m_session);
47 m_outputInfo = TrackOverlayDecisionAlg::GetOutputNodeInfo(m_session);
48 return StatusCode::SUCCESS;
54 ATH_MSG_VERBOSE(
"-----------------------------------------------------------------");
55 ATH_MSG_VERBOSE(
"m_filterParams.summary()" << m_filterParams.summary());
56 ATH_MSG_VERBOSE(
"-----------------------------------------------------------------");
58 ATH_MSG_VERBOSE(
" =====================================================================");
60 return StatusCode::SUCCESS;
64 std::vector<const xAOD::TruthParticle*> tempVec {};
65 if (m_pileupSwitch ==
"All") {
66 if (m_truthParticleName.key().empty()) {
70 if (not truthParticleContainer.
isValid()) {
73 tempVec.insert(tempVec.begin(), truthParticleContainer->
begin(), truthParticleContainer->
end());
75 if (m_pileupSwitch ==
"HardScatter") {
76 if (not m_truthEventName.key().empty()) {
83 const auto&
links =
event->truthParticleLinks();
84 tempVec.reserve(
event->nTruthParticles());
85 for (
const auto& link :
links) {
87 tempVec.push_back(*link);
91 }
else if (m_pileupSwitch ==
"PileUp") {
92 if (not m_truthPileUpEventName.key().empty()) {
96 if (truthPileupEventContainer.
isValid()) {
97 const unsigned int nPileup = truthPileupEventContainer->
size();
98 tempVec.reserve(nPileup * 200);
99 for (
unsigned int i(0);
i != nPileup; ++
i) {
100 const auto *eventPileup = truthPileupEventContainer->
at(
i);
102 int ntruth = eventPileup->nTruthParticles();
103 ATH_MSG_VERBOSE(
"Adding " << ntruth <<
" truth particles from TruthPileupEvents container");
104 const auto&
links = eventPileup->truthParticleLinks();
105 for (
const auto& link :
links) {
107 tempVec.push_back(*link);
129 float eventPxSum = 0.0;
130 float eventPySum = 0.0;
132 float puEvents = 0.0;
134 std::vector<float> pxValues, pyValues, pzValues, eValues, etaValues, phiValues, ptValues;
135 float truthMultiplicity = 0.0;
136 const int truthParticles = truthParticlesVec.size();
137 for (
int itruth = 0; itruth < truthParticles; itruth++) {
141 pxValues.push_back((thisTruth->
px()*0.001-1.46988000e+03)*
px_diff);
142 pyValues.push_back((thisTruth->
py()*0.001-1.35142000e+03)*
py_diff);
143 pzValues.push_back((thisTruth->
pz()*0.001-1.50464000e+03)*
pz_diff);
144 ptValues.push_back((thisTruth->
pt()*0.001-5.00006000e-01)*
pt_diff);
146 etaValues.push_back(thisTruth->
eta());
147 phiValues.push_back(thisTruth->
phi());
148 eValues.push_back((thisTruth->
e()*0.001-5.08307000e-01)*
e_diff);
150 eventPxSum += thisTruth->
px();
151 eventPySum += thisTruth->
py();
157 if (!m_truthPileUpEventName.key().empty()) {
161 eventPt = std::sqrt(eventPxSum*eventPxSum + eventPySum*eventPySum)*0.001;
163 std::vector<float> puEventsVec(pxValues.size(), (puEvents-1.55000000e+01)*
pu_diff);
164 std::vector<float> truthMultiplicityVec(pxValues.size(), (truthMultiplicity-1.80000000e+01)*
multi_diff);
165 std::vector<float> eventPtVec(pxValues.size(), (eventPt-3.42359395e-01)*
eventPt_diff);
166 std::vector<float> predictions;
169 Eigen::VectorXf ptEigen = Eigen::VectorXf::Map(ptValues.data(), ptValues.size());
170 Eigen::VectorXf phiEigen = Eigen::VectorXf::Map(phiValues.data(), phiValues.size());
171 Eigen::VectorXf etaEigen = Eigen::VectorXf::Map(etaValues.data(), etaValues.size());
172 for (std::size_t
i = 0;
i < truthMultiplicity; ++
i) {
173 float multiplicity_0p05 = 0.0, multiplicity_0p2 = 0.0;
174 float sum_0p05 = 0.0, sum_0p2 = 0.0;
175 float pt_0p05 = 0.0, pt_0p2 = 0.0;
176 float deltaEtaI = etaEigen[
i];
177 float phiI = phiEigen[
i];
178 for (std::size_t j = 0; j < truthMultiplicity; ++j) {
179 if (
i == j)
continue;
180 float deltaEta = deltaEtaI - etaEigen[j];
181 float deltaPhi = phiI - phiEigen[j];
186 if (distances < 0.05){
188 sum_0p05 += distances;
189 pt_0p05 += ptEigen[j];
191 if (distances < 0.2){
193 sum_0p2 += distances;
194 pt_0p2 += ptEigen[j];
198 std::vector<float> featData;
199 featData.push_back(pxValues[
i]);
200 featData.push_back(pyValues[
i]);
201 featData.push_back(pzValues[
i]);
202 featData.push_back(eValues[
i]);
203 featData.push_back(ptValues[
i]);
211 featData.push_back(puEventsVec[
i]);
212 featData.push_back(truthMultiplicityVec[
i]);
213 featData.push_back(eventPtVec[
i]);
215 std::vector<int64_t> input_node_dims;
216 std::vector<char*> input_node_names;
217 input_node_dims = std::get<0>(m_inputInfo);
218 input_node_names = std::get<1>(m_inputInfo);
220 std::vector<int64_t> output_node_dims;
221 std::vector<char*> output_node_names;
222 output_node_dims = std::get<0>(m_outputInfo);
223 output_node_names = std::get<1>(m_outputInfo);
225 Ort::MemoryInfo memoryInfo = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeCPU);
226 input_node_dims[0]=1;
227 Ort::Value input_data = Ort::Value::CreateTensor(memoryInfo, featData.data(), featData.size(), input_node_dims.data(), input_node_dims.size());
228 Ort::RunOptions run_options(
nullptr);
231 auto output_values = mysession.Run(run_options, input_node_names.data(), &input_data, input_node_names.size(), output_node_names.data(), output_node_names.size());
232 float* predictionData = output_values[0].GetTensorMutableData<
float>();
233 float prediction = predictionData[0];
235 predictions.push_back(prediction);
240 for (
float prediction : predictions) {
245 float rouletteScore =
static_cast<float>(badTracks) /
static_cast<float>(truthMultiplicity);
249 int decision = rouletteScore == 0;
257 if (m_invertfilter) {
261 ATH_MSG_ALWAYS(
"End TrackOverlayDecisionAlg, difference in filters: "<<(pass ?
"found" :
"not found")<<
"="<<pass<<
", invert="<<m_invertfilter);
262 return StatusCode::SUCCESS;