ATLAS Offline Software
Loading...
Searching...
No Matches
DeltaRTool.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
5// Author: James Catmore (james.catmore@cern.ch)
6//
7
9#include <utility> //for std::pair
10
11namespace DerivationFramework {
12
14 {
15 if (m_sgName.key().empty()) {
16 ATH_MSG_ERROR("No SG name provided for the output of invariant mass tool!");
17 return StatusCode::FAILURE;
18 }
19 ATH_CHECK(m_sgName.initialize());
20 ATH_CHECK(m_containerName.initialize());
21
22 if (!m_containerName2.key().empty()) {
23 ATH_CHECK(m_containerName2.initialize());
24 }
25
26 ATH_CHECK(initializeParser( {m_expression, m_2ndExpression} ));
27 return StatusCode::SUCCESS;
28 }
29
31 {
32 ATH_CHECK(finalizeParser());
33 return StatusCode::SUCCESS;
34 }
35
36 StatusCode DeltaRTool::addBranches(const EventContext& ctx) const
37 {
38 // Write deltaRs to SG for access by downstream algs
39 if (evtStore()->contains<std::vector<float> >(m_sgName.key())) { // FIXME Use Handles
40 ATH_MSG_ERROR("Tool is attempting to write a StoreGate key " << m_sgName << " which already exists. Please use a different key");
41 return StatusCode::FAILURE;
42 }
43 std::unique_ptr<std::vector<float> > deltaRs(new std::vector<float>());
44 ATH_CHECK(getDeltaRs(deltaRs.get(), ctx ));
45
47 ATH_CHECK(writeHandle.record(std::move(deltaRs)));
48
49 return StatusCode::SUCCESS;
50 }
51
52 StatusCode DeltaRTool::getDeltaRs(std::vector<float>* deltaRs, const EventContext& ctx) const
53 {
54
55 // check the relevant information is available
56 if (m_containerName.key().empty()) {
57 ATH_MSG_WARNING("Input container missing - returning zero");
58 deltaRs->push_back(0.0);
59 return StatusCode::FAILURE;
60 }
61 bool secondContainer(false);
62 if (!m_containerName2.key().empty()) secondContainer=true;
63
64 // get the relevant branches
66
67 const xAOD::IParticleContainer* secondParticles(nullptr);
68 if (secondContainer) {
70 secondParticles=particleHdl2.cptr();
71 }
72
73 // get the positions of the elements which pass the requirement
74 std::vector<int> entries, entries2;
75 if (!m_expression.empty()) {entries = m_parser[kDeltaRToolParser1]->evaluateAsVector();}
76 else {entries.assign(particles->size(),1);} // default: include all elements
77 unsigned int nEntries = entries.size();
78 // check the sizes are compatible
79 if (particles->size() != nEntries ) {
80 ATH_MSG_FATAL("Branch sizes incompatible");
81 return StatusCode::FAILURE;
82 }
83 unsigned int nEntries2(0);
84 if (secondContainer) {
85 if (!m_2ndExpression.empty()) {entries2 = m_parser[kDeltaRToolParser2]->evaluateAsVector();}
86 else {entries2.assign(secondParticles->size(),1);} // default: include all elements
87 nEntries2 = entries2.size();
88 // check the sizes are compatible
89 if (secondParticles->size() != nEntries2 ) {
90 ATH_MSG_FATAL("Branch sizes incompatible - returning zero");
91 return StatusCode::FAILURE;
92 }
93 }
94
95 // Double loop to get the pairs for which the mass should be calculated
96 std::vector<std::pair<unsigned, unsigned>> pairs;
97 if (!secondContainer) {
98 for (unsigned outerIt=0; outerIt<nEntries; ++outerIt) {
99 for (unsigned innerIt=outerIt+1; innerIt<nEntries; ++innerIt) {
100 if (entries[outerIt]==1 && entries[innerIt]==1) {
101 pairs.emplace_back(outerIt, innerIt);
102 }
103 }
104 }
105 }
106
107 if (secondContainer) {
108 for (unsigned coll1It=0; coll1It<nEntries; ++coll1It) {
109 for (unsigned coll2It=0; coll2It<nEntries2; ++coll2It) {
110 //coverity[copy_paste_error]
111 if (entries[coll1It]==1 && entries2[coll2It]==1) {
112 pairs.emplace_back(coll1It, coll2It);
113 }
114 }
115 }
116 }
117
118 // Loop over the pairs; calculate the mass; put into vector and return
119 for (const auto & [first, second] : pairs) {
120 if (!secondContainer) {
121 float phi1f = ((*particles)[first])->p4().Phi(); float phi2f = ((*particles)[second])->p4().Phi();
122 float eta1f = ((*particles)[first])->p4().Eta(); float eta2f = ((*particles)[second])->p4().Eta();
123 float deltaR = calculateDeltaR(phi1f,phi2f,eta1f,eta2f);
124 deltaRs->push_back(deltaR);
125 }
126 if (secondContainer) {
127 float phi1f = ((*particles)[first])->p4().Phi(); float phi2f = ((*secondParticles)[second])->p4().Phi();
128 float eta1f = ((*particles)[first])->p4().Eta(); float eta2f = ((*secondParticles)[second])->p4().Eta();
129 float deltaR = calculateDeltaR(phi1f,phi2f,eta1f,eta2f);
130 deltaRs->push_back(deltaR);
131 }
132 }
133
134 return StatusCode::SUCCESS;
135
136 }
137
138 float DeltaRTool::calculateDeltaR(float phi1, float phi2, float eta1, float eta2)
139 {
140 float deltaPhi = fabs(phi1-phi2);
141 if (deltaPhi>TMath::Pi()) deltaPhi = 2.0*TMath::Pi() - deltaPhi;
142 float deltaPhiSq = deltaPhi*deltaPhi;
143 float deltaEtaSq = (eta1-eta2)*(eta1-eta2);
144 float deltaR = sqrt(deltaPhiSq+deltaEtaSq);
145 return deltaR;
146 }
147}
Scalar deltaPhi(const MatrixBase< Derived > &vec) const
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_FATAL(x)
#define ATH_MSG_WARNING(x)
size_type size() const noexcept
Returns the number of elements in the collection.
Gaudi::Property< std::string > m_expression
Definition DeltaRTool.h:34
Gaudi::Property< std::string > m_2ndExpression
Definition DeltaRTool.h:35
SG::ReadHandleKey< xAOD::IParticleContainer > m_containerName2
Definition DeltaRTool.h:38
StatusCode getDeltaRs(std::vector< float > *, const EventContext &ctx) const
virtual StatusCode initialize() override final
virtual StatusCode finalize() override final
SG::ReadHandleKey< xAOD::IParticleContainer > m_containerName
Definition DeltaRTool.h:37
SG::WriteHandleKey< std::vector< float > > m_sgName
Definition DeltaRTool.h:36
virtual StatusCode addBranches(const EventContext &ctx) const override final
static float calculateDeltaR(float, float, float, float)
const_pointer_type cptr()
Dereference the pointer.
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
bool contains(const std::string &s, const std::string &regx)
does a string contain the substring
Definition hcg.cxx:114
double entries
Definition listroot.cxx:49
THE reconstruction tool.
double deltaR(double eta1, double eta2, double phi1, double phi2)
DataVector< IParticle > IParticleContainer
Simple convenience declaration of IParticleContainer.