ATLAS Offline Software
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) :
22  m_expression(""),
23  m_2ndExpression("")
24  {
25  declareInterface<DerivationFramework::IAugmentationTool>(this);
26  declareProperty("ObjectRequirements", m_expression);
27  declareProperty("SecondObjectRequirements", m_2ndExpression);
28  }
29 
31  {
32  if (m_sgName.key().empty()) {
33  ATH_MSG_ERROR("No SG name provided for the output of invariant mass tool!");
34  return StatusCode::FAILURE;
35  }
37  ATH_CHECK(m_containerName.initialize());
38 
39  if (!m_containerName2.key().empty()) {
40  ATH_CHECK(m_containerName2.initialize());
41  }
42 
44  return StatusCode::SUCCESS;
45  }
46 
48  {
50  return StatusCode::SUCCESS;
51  }
52 
54  {
55  // Write deltaRs to SG for access by downstream algs
56  if (evtStore()->contains<std::vector<float> >(m_sgName.key())) {
57  ATH_MSG_ERROR("Tool is attempting to write a StoreGate key " << m_sgName << " which already exists. Please use a different key");
58  return StatusCode::FAILURE;
59  }
60  std::unique_ptr<std::vector<float> > deltaRs(new std::vector<float>());
61  ATH_CHECK(getDeltaRs(deltaRs.get()));
62 
64  ATH_CHECK(writeHandle.record(std::move(deltaRs)));
65 
66  return StatusCode::SUCCESS;
67  }
68 
69  StatusCode DeltaRTool::getDeltaRs(std::vector<float>* deltaRs) const
70  {
71 
72  // check the relevant information is available
73  if (m_containerName.key().empty()) {
74  ATH_MSG_WARNING("Input container missing - returning zero");
75  deltaRs->push_back(0.0);
76  return StatusCode::FAILURE;
77  }
78  bool secondContainer(false);
79  if (!m_containerName2.key().empty()) secondContainer=true;
80 
81  // get the relevant branches
83 
84  const xAOD::IParticleContainer* secondParticles(nullptr);
85  if (secondContainer) {
87  secondParticles=particleHdl2.cptr();
88  }
89 
90  // get the positions of the elements which pass the requirement
91  std::vector<int> entries, entries2;
92  if (!m_expression.empty()) {entries = m_parser[kDeltaRToolParser1]->evaluateAsVector();}
93  else {entries.assign(particles->size(),1);} // default: include all elements
94  unsigned int nEntries = entries.size();
95  // check the sizes are compatible
96  if (particles->size() != nEntries ) {
97  ATH_MSG_FATAL("Branch sizes incompatible");
98  return StatusCode::FAILURE;
99  }
100  unsigned int nEntries2(0);
101  if (secondContainer) {
102  if (!m_2ndExpression.empty()) {entries2 = m_parser[kDeltaRToolParser2]->evaluateAsVector();}
103  else {entries2.assign(secondParticles->size(),1);} // default: include all elements
104  nEntries2 = entries2.size();
105  // check the sizes are compatible
106  if (secondParticles->size() != nEntries2 ) {
107  ATH_MSG_FATAL("Branch sizes incompatible - returning zero");
108  return StatusCode::FAILURE;
109  }
110  }
111 
112  // Double loop to get the pairs for which the mass should be calculated
113  std::vector<std::vector<int> > pairs;
114  if (!secondContainer) {
115  unsigned int outerIt, innerIt;
116  for (outerIt=0; outerIt<nEntries; ++outerIt) {
117  for (innerIt=outerIt+1; innerIt<nEntries; ++innerIt) {
118  std::vector<int> tmpPair;
119  if (entries[outerIt]==1 && entries[innerIt]==1) {
120  tmpPair.push_back(outerIt); tmpPair.push_back(innerIt);
121  pairs.push_back(tmpPair);
122  }
123  }
124  }
125  }
126 
127  if (secondContainer) {
128  unsigned int coll1It, coll2It;
129  for (coll1It=0; coll1It<nEntries; ++coll1It) {
130  for (coll2It=0; coll2It<nEntries2; ++coll2It) {
131  std::vector<int> tmpPair;
132  if (entries[coll1It]==1 && entries2[coll2It]==1) {
133  tmpPair.push_back(coll1It); tmpPair.push_back(coll2It);
134  pairs.push_back(tmpPair);
135  }
136  }
137  }
138  }
139 
140  // Loop over the pairs; calculate the mass; put into vector and return
141  std::vector<std::vector<int> >::iterator pairIt;
142  for (pairIt=pairs.begin(); pairIt!=pairs.end(); ++pairIt) {
143  unsigned int first = (*pairIt)[0];
144  unsigned int second = (*pairIt)[1];
145  if (!secondContainer) {
146  float phi1f = ((*particles)[first])->p4().Phi(); float phi2f = ((*particles)[second])->p4().Phi();
147  float eta1f = ((*particles)[first])->p4().Eta(); float eta2f = ((*particles)[second])->p4().Eta();
148  float deltaR = calculateDeltaR(phi1f,phi2f,eta1f,eta2f);
149  deltaRs->push_back(deltaR);
150  }
151  if (secondContainer) {
152  float phi1f = ((*particles)[first])->p4().Phi(); float phi2f = ((*secondParticles)[second])->p4().Phi();
153  float eta1f = ((*particles)[first])->p4().Eta(); float eta2f = ((*secondParticles)[second])->p4().Eta();
154  float deltaR = calculateDeltaR(phi1f,phi2f,eta1f,eta2f);
155  deltaRs->push_back(deltaR);
156  }
157  }
158 
159  return StatusCode::SUCCESS;
160 
161  }
162 
163  float DeltaRTool::calculateDeltaR(float phi1, float phi2, float eta1, float eta2)
164  {
165  float deltaPhi = fabs(phi1-phi2);
166  if (deltaPhi>TMath::Pi()) deltaPhi = 2.0*TMath::Pi() - deltaPhi;
167  float deltaPhiSq = deltaPhi*deltaPhi;
168  float deltaEtaSq = (eta1-eta2)*(eta1-eta2);
169  float deltaR = sqrt(deltaPhiSq+deltaEtaSq);
170  return deltaR;
171  }
172 }
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
DerivationFramework::kDeltaRToolParser1
@ kDeltaRToolParser1
Definition: DeltaRTool.h:25
DerivationFramework::kDeltaRToolParserNum
@ kDeltaRToolParserNum
Definition: DeltaRTool.h:25
ExpressionParserUserBase< AthAlgTool, NUM_PARSER >::finalizeParser
StatusCode finalizeParser()
python.SystemOfUnits.second
int second
Definition: SystemOfUnits.py:120
P4Helpers::deltaEtaSq
double deltaEtaSq(const I4Momentum &p1, const I4Momentum &p2)
Computes efficiently .
Definition: P4Helpers.h:85
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
python.PerfMonSerializer.p
def p
Definition: PerfMonSerializer.py:743
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:70
AthCommonDataStore< AthCommonMsg< AlgTool > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
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
ExpressionParserUserBase< AthAlgTool, NUM_PARSER >::m_parser
std::conditional< NUM_PARSER==1, std::unique_ptr< ExpressionParsing::ExpressionParser >, std::array< std::unique_ptr< ExpressionParsing::ExpressionParser >, NUM_PARSER > >::type m_parser
Definition: ExpressionParserUser.h:100
DerivationFramework::DeltaRTool::initialize
StatusCode initialize()
Definition: DeltaRTool.cxx:30
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
AthCommonDataStore< AthCommonMsg< AlgTool > >::evtStore
ServiceHandle< StoreGateSvc > & evtStore()
The standard StoreGateSvc (event store) Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:85
DerivationFramework::DeltaRTool::calculateDeltaR
static float calculateDeltaR(float, float, float, float)
Definition: DeltaRTool.cxx:163
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:581
ExpressionParserUser< AthAlgTool, kDeltaRToolParserNum >::initializeParser
StatusCode initializeParser(const ExpressionParsing::SelectionArg< NUM_PARSER > &selection_string)
ExpressionParserUser
Definition: ExpressionParserUser.h:107
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:172
DerivationFramework::kDeltaRToolParser2
@ kDeltaRToolParser2
Definition: DeltaRTool.h:25
SG::WriteHandle
Definition: StoreGate/StoreGate/WriteHandle.h:76
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:534
LArG4FSStartPointFilter.particles
list particles
Definition: LArG4FSStartPointFilter.py:84
entries
double entries
Definition: listroot.cxx:49
DerivationFramework::DeltaRTool::finalize
StatusCode finalize()
Definition: DeltaRTool.cxx:47
AthAlgTool
Definition: AthAlgTool.h:26
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:69
DerivationFramework::DeltaRTool::addBranches
virtual StatusCode addBranches() const
Pass the thinning service
Definition: DeltaRTool.cxx:53