Loading [MathJax]/extensions/tex2jax.js
ATLAS Offline Software
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
DeltaRTool.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 // DeltaRTool.cxx, (c) ATLAS Detector software
8 // Author: James Catmore (james.catmore@cern.ch)
9 //
10 
13 #include <vector>
14 #include <string>
15 
16 namespace DerivationFramework {
17 
18  DeltaRTool::DeltaRTool(const std::string& t,
19  const std::string& n,
20  const IInterface* p) :
21  base_class(t,n,p),
22  m_expression(""),
23  m_2ndExpression("")
24  {
25  declareProperty("ObjectRequirements", m_expression);
26  declareProperty("SecondObjectRequirements", m_2ndExpression);
27  }
28 
30  {
31  if (m_sgName.key().empty()) {
32  ATH_MSG_ERROR("No SG name provided for the output of invariant mass tool!");
33  return StatusCode::FAILURE;
34  }
36  ATH_CHECK(m_containerName.initialize());
37 
38  if (!m_containerName2.key().empty()) {
39  ATH_CHECK(m_containerName2.initialize());
40  }
41 
42  ATH_CHECK(initializeParser( {m_expression, m_2ndExpression} ));
43  return StatusCode::SUCCESS;
44  }
45 
47  {
48  ATH_CHECK(finalizeParser());
49  return StatusCode::SUCCESS;
50  }
51 
53  {
54  // Write deltaRs to SG for access by downstream algs
55  if (evtStore()->contains<std::vector<float> >(m_sgName.key())) {
56  ATH_MSG_ERROR("Tool is attempting to write a StoreGate key " << m_sgName << " which already exists. Please use a different key");
57  return StatusCode::FAILURE;
58  }
59  std::unique_ptr<std::vector<float> > deltaRs(new std::vector<float>());
60  ATH_CHECK(getDeltaRs(deltaRs.get()));
61 
63  ATH_CHECK(writeHandle.record(std::move(deltaRs)));
64 
65  return StatusCode::SUCCESS;
66  }
67 
68  StatusCode DeltaRTool::getDeltaRs(std::vector<float>* deltaRs) const
69  {
70 
71  // check the relevant information is available
72  if (m_containerName.key().empty()) {
73  ATH_MSG_WARNING("Input container missing - returning zero");
74  deltaRs->push_back(0.0);
75  return StatusCode::FAILURE;
76  }
77  bool secondContainer(false);
78  if (!m_containerName2.key().empty()) secondContainer=true;
79 
80  // get the relevant branches
82 
83  const xAOD::IParticleContainer* secondParticles(nullptr);
84  if (secondContainer) {
86  secondParticles=particleHdl2.cptr();
87  }
88 
89  // get the positions of the elements which pass the requirement
90  std::vector<int> entries, entries2;
91  if (!m_expression.empty()) {entries = m_parser[kDeltaRToolParser1]->evaluateAsVector();}
92  else {entries.assign(particles->size(),1);} // default: include all elements
93  unsigned int nEntries = entries.size();
94  // check the sizes are compatible
95  if (particles->size() != nEntries ) {
96  ATH_MSG_FATAL("Branch sizes incompatible");
97  return StatusCode::FAILURE;
98  }
99  unsigned int nEntries2(0);
100  if (secondContainer) {
101  if (!m_2ndExpression.empty()) {entries2 = m_parser[kDeltaRToolParser2]->evaluateAsVector();}
102  else {entries2.assign(secondParticles->size(),1);} // default: include all elements
103  nEntries2 = entries2.size();
104  // check the sizes are compatible
105  if (secondParticles->size() != nEntries2 ) {
106  ATH_MSG_FATAL("Branch sizes incompatible - returning zero");
107  return StatusCode::FAILURE;
108  }
109  }
110 
111  // Double loop to get the pairs for which the mass should be calculated
112  std::vector<std::vector<int> > pairs;
113  if (!secondContainer) {
114  unsigned int outerIt, innerIt;
115  for (outerIt=0; outerIt<nEntries; ++outerIt) {
116  for (innerIt=outerIt+1; innerIt<nEntries; ++innerIt) {
117  std::vector<int> tmpPair;
118  if (entries[outerIt]==1 && entries[innerIt]==1) {
119  tmpPair.push_back(outerIt); tmpPair.push_back(innerIt);
120  pairs.push_back(tmpPair);
121  }
122  }
123  }
124  }
125 
126  if (secondContainer) {
127  unsigned int coll1It, coll2It;
128  for (coll1It=0; coll1It<nEntries; ++coll1It) {
129  for (coll2It=0; coll2It<nEntries2; ++coll2It) {
130  std::vector<int> tmpPair;
131  if (entries[coll1It]==1 && entries2[coll2It]==1) {
132  tmpPair.push_back(coll1It); tmpPair.push_back(coll2It);
133  pairs.push_back(tmpPair);
134  }
135  }
136  }
137  }
138 
139  // Loop over the pairs; calculate the mass; put into vector and return
140  std::vector<std::vector<int> >::iterator pairIt;
141  for (pairIt=pairs.begin(); pairIt!=pairs.end(); ++pairIt) {
142  unsigned int first = (*pairIt)[0];
143  unsigned int second = (*pairIt)[1];
144  if (!secondContainer) {
145  float phi1f = ((*particles)[first])->p4().Phi(); float phi2f = ((*particles)[second])->p4().Phi();
146  float eta1f = ((*particles)[first])->p4().Eta(); float eta2f = ((*particles)[second])->p4().Eta();
147  float deltaR = calculateDeltaR(phi1f,phi2f,eta1f,eta2f);
148  deltaRs->push_back(deltaR);
149  }
150  if (secondContainer) {
151  float phi1f = ((*particles)[first])->p4().Phi(); float phi2f = ((*secondParticles)[second])->p4().Phi();
152  float eta1f = ((*particles)[first])->p4().Eta(); float eta2f = ((*secondParticles)[second])->p4().Eta();
153  float deltaR = calculateDeltaR(phi1f,phi2f,eta1f,eta2f);
154  deltaRs->push_back(deltaR);
155  }
156  }
157 
158  return StatusCode::SUCCESS;
159 
160  }
161 
162  float DeltaRTool::calculateDeltaR(float phi1, float phi2, float eta1, float eta2)
163  {
164  float deltaPhi = fabs(phi1-phi2);
165  if (deltaPhi>TMath::Pi()) deltaPhi = 2.0*TMath::Pi() - deltaPhi;
166  float deltaPhiSq = deltaPhi*deltaPhi;
167  float deltaEtaSq = (eta1-eta2)*(eta1-eta2);
168  float deltaR = sqrt(deltaPhiSq+deltaEtaSq);
169  return deltaR;
170  }
171 }
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
DerivationFramework::kDeltaRToolParser1
@ kDeltaRToolParser1
Definition: DeltaRTool.h:25
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:38
DerivationFramework::DeltaRTool::m_2ndExpression
std::string m_2ndExpression
Definition: DeltaRTool.h:36
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:160
DerivationFramework::DeltaRTool::m_containerName2
SG::ReadHandleKey< xAOD::IParticleContainer > m_containerName2
Definition: DeltaRTool.h:39
DerivationFramework::DeltaRTool::initialize
StatusCode initialize()
Definition: DeltaRTool.cxx:29
python.SystemOfUnits.second
float second
Definition: SystemOfUnits.py:134
xAOD::eta1
setEt setPhi setE277 setWeta2 eta1
Definition: TrigEMCluster_v1.cxx:41
DerivationFramework::DeltaRTool::m_expression
std::string m_expression
Definition: DeltaRTool.h:35
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:210
DerivationFramework::DeltaRTool::calculateDeltaR
static float calculateDeltaR(float, float, float, float)
Definition: DeltaRTool.cxx:162
DerivationFramework::DeltaRTool::DeltaRTool
DeltaRTool(const std::string &t, const std::string &n, const IInterface *p)
Definition: DeltaRTool.cxx:18
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
DeltaRTool.h
beamspotman.n
n
Definition: beamspotman.py:731
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
IParticleContainer.h
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
DataVector
Derived DataVector<T>.
Definition: DataVector.h:794
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:25
SG::WriteHandle
Definition: StoreGate/StoreGate/WriteHandle.h:73
DerivationFramework::DeltaRTool::m_sgName
SG::WriteHandleKey< std::vector< float > > m_sgName
Definition: DeltaRTool.h:37
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:536
LArG4FSStartPointFilter.particles
list particles
Definition: LArG4FSStartPointFilter.py:84
entries
double entries
Definition: listroot.cxx:49
DerivationFramework::DeltaRTool::finalize
StatusCode finalize()
Definition: DeltaRTool.cxx:46
dqBeamSpot.nEntries
int nEntries
Definition: dqBeamSpot.py:73
makeComparison.deltaR
float deltaR
Definition: makeComparison.py:36
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
DerivationFramework::DeltaRTool::getDeltaRs
StatusCode getDeltaRs(std::vector< float > *) const
Definition: DeltaRTool.cxx:68
DerivationFramework::DeltaRTool::addBranches
virtual StatusCode addBranches() const
Definition: DeltaRTool.cxx:52