ATLAS Offline Software
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 
10 namespace DerivationFramework {
11 
12  DeltaRTool::DeltaRTool(const std::string& t,
13  const std::string& n,
14  const IInterface* p) :
15  base_class(t,n,p),
16  m_expression(""),
17  m_2ndExpression("")
18  {
19  declareProperty("ObjectRequirements", m_expression);
20  declareProperty("SecondObjectRequirements", m_2ndExpression);
21  }
22 
24  {
25  if (m_sgName.key().empty()) {
26  ATH_MSG_ERROR("No SG name provided for the output of invariant mass tool!");
27  return StatusCode::FAILURE;
28  }
30  ATH_CHECK(m_containerName.initialize());
31 
32  if (!m_containerName2.key().empty()) {
33  ATH_CHECK(m_containerName2.initialize());
34  }
35 
36  ATH_CHECK(initializeParser( {m_expression, m_2ndExpression} ));
37  return StatusCode::SUCCESS;
38  }
39 
41  {
42  ATH_CHECK(finalizeParser());
43  return StatusCode::SUCCESS;
44  }
45 
46  StatusCode DeltaRTool::addBranches(const EventContext& ctx) const
47  {
48  // Write deltaRs to SG for access by downstream algs
49  if (evtStore()->contains<std::vector<float> >(m_sgName.key())) { // FIXME Use Handles
50  ATH_MSG_ERROR("Tool is attempting to write a StoreGate key " << m_sgName << " which already exists. Please use a different key");
51  return StatusCode::FAILURE;
52  }
53  std::unique_ptr<std::vector<float> > deltaRs(new std::vector<float>());
54  ATH_CHECK(getDeltaRs(deltaRs.get(), ctx ));
55 
56  SG::WriteHandle<std::vector<float> > writeHandle(m_sgName, ctx);
57  ATH_CHECK(writeHandle.record(std::move(deltaRs)));
58 
59  return StatusCode::SUCCESS;
60  }
61 
62  StatusCode DeltaRTool::getDeltaRs(std::vector<float>* deltaRs, const EventContext& ctx) const
63  {
64 
65  // check the relevant information is available
66  if (m_containerName.key().empty()) {
67  ATH_MSG_WARNING("Input container missing - returning zero");
68  deltaRs->push_back(0.0);
69  return StatusCode::FAILURE;
70  }
71  bool secondContainer(false);
72  if (!m_containerName2.key().empty()) secondContainer=true;
73 
74  // get the relevant branches
76 
77  const xAOD::IParticleContainer* secondParticles(nullptr);
78  if (secondContainer) {
80  secondParticles=particleHdl2.cptr();
81  }
82 
83  // get the positions of the elements which pass the requirement
84  std::vector<int> entries, entries2;
85  if (!m_expression.empty()) {entries = m_parser[kDeltaRToolParser1]->evaluateAsVector();}
86  else {entries.assign(particles->size(),1);} // default: include all elements
87  unsigned int nEntries = entries.size();
88  // check the sizes are compatible
89  if (particles->size() != nEntries ) {
90  ATH_MSG_FATAL("Branch sizes incompatible");
91  return StatusCode::FAILURE;
92  }
93  unsigned int nEntries2(0);
94  if (secondContainer) {
95  if (!m_2ndExpression.empty()) {entries2 = m_parser[kDeltaRToolParser2]->evaluateAsVector();}
96  else {entries2.assign(secondParticles->size(),1);} // default: include all elements
97  nEntries2 = entries2.size();
98  // check the sizes are compatible
99  if (secondParticles->size() != nEntries2 ) {
100  ATH_MSG_FATAL("Branch sizes incompatible - returning zero");
101  return StatusCode::FAILURE;
102  }
103  }
104 
105  // Double loop to get the pairs for which the mass should be calculated
106  std::vector<std::vector<int> > pairs;
107  if (!secondContainer) {
108  unsigned int outerIt, innerIt;
109  for (outerIt=0; outerIt<nEntries; ++outerIt) {
110  for (innerIt=outerIt+1; innerIt<nEntries; ++innerIt) {
111  std::vector<int> tmpPair;
112  if (entries[outerIt]==1 && entries[innerIt]==1) {
113  tmpPair.push_back(outerIt); tmpPair.push_back(innerIt);
114  pairs.push_back(tmpPair);
115  }
116  }
117  }
118  }
119 
120  if (secondContainer) {
121  unsigned int coll1It, coll2It;
122  for (coll1It=0; coll1It<nEntries; ++coll1It) {
123  for (coll2It=0; coll2It<nEntries2; ++coll2It) {
124  std::vector<int> tmpPair;
125  if (entries[coll1It]==1 && entries2[coll2It]==1) {
126  tmpPair.push_back(coll1It); tmpPair.push_back(coll2It);
127  pairs.push_back(tmpPair);
128  }
129  }
130  }
131  }
132 
133  // Loop over the pairs; calculate the mass; put into vector and return
134  std::vector<std::vector<int> >::iterator pairIt;
135  for (pairIt=pairs.begin(); pairIt!=pairs.end(); ++pairIt) {
136  unsigned int first = (*pairIt)[0];
137  unsigned int second = (*pairIt)[1];
138  if (!secondContainer) {
139  float phi1f = ((*particles)[first])->p4().Phi(); float phi2f = ((*particles)[second])->p4().Phi();
140  float eta1f = ((*particles)[first])->p4().Eta(); float eta2f = ((*particles)[second])->p4().Eta();
141  float deltaR = calculateDeltaR(phi1f,phi2f,eta1f,eta2f);
142  deltaRs->push_back(deltaR);
143  }
144  if (secondContainer) {
145  float phi1f = ((*particles)[first])->p4().Phi(); float phi2f = ((*secondParticles)[second])->p4().Phi();
146  float eta1f = ((*particles)[first])->p4().Eta(); float eta2f = ((*secondParticles)[second])->p4().Eta();
147  float deltaR = calculateDeltaR(phi1f,phi2f,eta1f,eta2f);
148  deltaRs->push_back(deltaR);
149  }
150  }
151 
152  return StatusCode::SUCCESS;
153 
154  }
155 
156  float DeltaRTool::calculateDeltaR(float phi1, float phi2, float eta1, float eta2)
157  {
158  float deltaPhi = fabs(phi1-phi2);
159  if (deltaPhi>TMath::Pi()) deltaPhi = 2.0*TMath::Pi() - deltaPhi;
160  float deltaPhiSq = deltaPhi*deltaPhi;
161  float deltaEtaSq = (eta1-eta2)*(eta1-eta2);
162  float deltaR = sqrt(deltaPhiSq+deltaEtaSq);
163  return deltaR;
164  }
165 }
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
DerivationFramework::kDeltaRToolParser1
@ kDeltaRToolParser1
Definition: DeltaRTool.h:23
P4Helpers::deltaEtaSq
double deltaEtaSq(const I4Momentum &p1, const I4Momentum &p2)
Computes efficiently .
Definition: P4Helpers.h:98
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
DerivationFramework::DeltaRTool::m_containerName
SG::ReadHandleKey< xAOD::IParticleContainer > m_containerName
Definition: DeltaRTool.h:36
DerivationFramework::DeltaRTool::m_2ndExpression
std::string m_2ndExpression
Definition: DeltaRTool.h:34
ParticleGun_SamplingFraction.eta2
eta2
Definition: ParticleGun_SamplingFraction.py:96
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:67
xAOD::deltaPhi
setSAddress setEtaMS setDirPhiMS setDirZMS setBarrelRadius setEndcapAlpha setEndcapRadius setInterceptInner setEtaMap setEtaBin setIsTgcFailure setDeltaPt deltaPhi
Definition: L2StandAloneMuon_v1.cxx:161
DerivationFramework::DeltaRTool::m_containerName2
SG::ReadHandleKey< xAOD::IParticleContainer > m_containerName2
Definition: DeltaRTool.h:37
DerivationFramework::DeltaRTool::initialize
StatusCode initialize()
Definition: DeltaRTool.cxx:23
python.SystemOfUnits.second
float second
Definition: SystemOfUnits.py:135
xAOD::eta1
setEt setPhi setE277 setWeta2 eta1
Definition: TrigEMCluster_v1.cxx:41
DerivationFramework::DeltaRTool::m_expression
std::string m_expression
Definition: DeltaRTool.h:33
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
SG::VarHandleKey::key
const std::string & key() const
Return the StoreGate ID for the referenced object.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:141
python.CreateTierZeroArgdict.pairs
pairs
Definition: CreateTierZeroArgdict.py:201
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:209
DerivationFramework::DeltaRTool::calculateDeltaR
static float calculateDeltaR(float, float, float, float)
Definition: DeltaRTool.cxx:156
DerivationFramework::DeltaRTool::DeltaRTool
DeltaRTool(const std::string &t, const std::string &n, const IInterface *p)
Definition: DeltaRTool.cxx:12
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
DeltaRTool.h
beamspotman.n
n
Definition: beamspotman.py:727
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
contains
bool contains(const std::string &s, const std::string &regx)
does a string contain the substring
Definition: hcg.cxx:111
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
DerivationFramework
THE reconstruction tool.
Definition: ParticleSortingAlg.h:24
SG::VarHandleKey::initialize
StatusCode initialize(bool used=true)
If this object is used as a property, then this should be called during the initialize phase.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:103
DerivationFramework::DeltaRTool::addBranches
virtual StatusCode addBranches(const EventContext &ctx) const
Definition: DeltaRTool.cxx:46
DerivationFramework::DeltaRTool::getDeltaRs
StatusCode getDeltaRs(std::vector< float > *, const EventContext &ctx) const
Definition: DeltaRTool.cxx:62
DataVector
Derived DataVector<T>.
Definition: DataVector.h:795
P4Helpers::deltaPhiSq
double deltaPhiSq(const I4Momentum &pA, const I4Momentum &pB)
delta Phi squared in range ([-pi,pi[)^2 from two I4momentum references
Definition: P4Helpers.h:185
DerivationFramework::kDeltaRToolParser2
@ kDeltaRToolParser2
Definition: DeltaRTool.h:23
SG::WriteHandle
Definition: StoreGate/StoreGate/WriteHandle.h:73
DerivationFramework::DeltaRTool::m_sgName
SG::WriteHandleKey< std::vector< float > > m_sgName
Definition: DeltaRTool.h:35
SG::WriteHandle::record
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
DeMoScan.first
bool first
Definition: DeMoScan.py:534
LArG4FSStartPointFilter.particles
list particles
Definition: LArG4FSStartPointFilter.py:84
entries
double entries
Definition: listroot.cxx:49
DerivationFramework::DeltaRTool::finalize
StatusCode finalize()
Definition: DeltaRTool.cxx:40
dqBeamSpot.nEntries
int nEntries
Definition: dqBeamSpot.py:72
makeComparison.deltaR
float deltaR
Definition: makeComparison.py:36
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.