ATLAS Offline Software
InvariantMassTool.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 // InvariantMassTool.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 
19  const std::string& n,
20  const IInterface* p) :
22  m_expression("true"),
23  m_expression2(""),
24  m_massHypothesis(0.0),
25  m_massHypothesis2(0.0)
26  {
27  declareInterface<DerivationFramework::IAugmentationTool>(this);
28  declareProperty("ObjectRequirements", m_expression);
29  declareProperty("SecondObjectRequirements", m_expression2);
30  declareProperty("MassHypothesis", m_massHypothesis);
31  declareProperty("SecondMassHypothesis", m_massHypothesis2);
32  }
33 
35  {
36  if (m_sgName.key().empty()) {
37  ATH_MSG_ERROR("No SG name provided for the output of invariant mass tool!");
38  return StatusCode::FAILURE;
39  }
41 
44 
45  ATH_CHECK(m_containerName.initialize());
46  if (!m_containerName2.key().empty()) {
47  ATH_CHECK(m_containerName2.initialize());
48  }
49 
50 
51 
52  return StatusCode::SUCCESS;
53  }
54 
56  {
58  return StatusCode::SUCCESS;
59  }
60 
62  {
63  // Write masses to SG for access by downstream algs
64  if (evtStore()->contains<std::vector<float> >(m_sgName.key())) {
65  ATH_MSG_ERROR("Tool is attempting to write a StoreGate key " << m_sgName << " which already exists. Please use a different key");
66  return StatusCode::FAILURE;
67  }
68  std::unique_ptr<std::vector<float> > masses(new std::vector<float>());
70  //CHECK(evtStore()->record(std::move(masses), m_sgName));
72  ATH_CHECK(writeHandle.record(std::move(masses)));
73  return StatusCode::SUCCESS;
74  }
75 
77  {
78 
79  // check the relevant information is available
80  if (m_containerName.key().empty()) {
81  ATH_MSG_WARNING("Input container missing - returning zero");
82  masses->push_back(0.0);
83  return StatusCode::FAILURE;
84  }
85 
87 
88  bool from2Collections(false);
89  const xAOD::IParticleContainer* particles2{nullptr};
90  if (!m_containerName2.key().empty() && m_containerName2.key()!=m_containerName.key()) {
92  particles2=particleHdl2.cptr();
93  from2Collections = true;
94  }
95 
96  // get the positions of the elements which pass the requirement
97  std::vector<int> entries = m_parser[kInvariantMassToolParser1]->evaluateAsVector();
98  std::vector<int> entries2 = m_parser[kInvariantMassToolParser2]->evaluateAsVector();
99  unsigned int nEntries = entries.size();
100  unsigned int nEntries2 = entries2.size();
101 
102  // check the sizes are compatible
103  if (!from2Collections) {
104  if ( (particles->size() != nEntries) || (particles->size() != nEntries2) || (nEntries!=nEntries2) ) {
105  ATH_MSG_ERROR("Branch sizes incompatible - returning zero. Check your selection strings.");
106  masses->push_back(0.0);
107  return StatusCode::FAILURE;
108  }
109  }
110  if (from2Collections) {
111  if ( (particles->size() != nEntries) || (particles2->size() != nEntries2) ) {
112  ATH_MSG_ERROR("Branch sizes incompatible - returning zero. Check your selection strings.");
113  masses->push_back(0.0);
114  return StatusCode::FAILURE;
115  }
116  }
117 
118  // Double loop to get all possible index pairs
119  unsigned int outerIt, innerIt;
120  std::vector<std::vector<int> > pairs;
121  // Loop for case where both legs are from the same container
122  if (!from2Collections) {
123  for (outerIt=0; outerIt<nEntries; ++outerIt) {
124  for (innerIt=outerIt+1; innerIt<nEntries; ++innerIt) {
125  std::vector<int> tmpPair;
126  tmpPair.push_back(outerIt); tmpPair.push_back(innerIt);
127  pairs.push_back(tmpPair);
128  }
129  }
130  // Select the pairs for which the mass should be calculated, and then calculate it
131  std::vector<std::vector<int> >::iterator pairIt;
132  for (pairIt=pairs.begin(); pairIt!=pairs.end(); ++pairIt) {
133  unsigned int first = (*pairIt)[0];
134  unsigned int second = (*pairIt)[1];
135  if ( (entries[first]==1 && entries2[second]==1) || (entries2[first]==1 && entries[second]==1) ) {
136 
137  const float mass = calculateInvariantMass( ((*particles)[first])->p4().Vect(),
138  ((*particles)[second])->p4().Vect(),
141  masses->push_back(mass);
142  }
143  }
144  }
145 
146  // Loop for case where both legs are from different containers
147  if (from2Collections) {
148  for (outerIt=0; outerIt<nEntries; ++outerIt) {
149  if (entries[outerIt]==0) continue;
150  for (innerIt=0; innerIt<nEntries2; ++innerIt) {
151  if (entries2[innerIt]==0) continue;
152  std::vector<int> tmpPair;
153  tmpPair.push_back(outerIt); tmpPair.push_back(innerIt);
154  pairs.push_back(tmpPair);
155  }
156  }
157  // Select the pairs for which the mass should be calculated, and then calculate it
158  std::vector<std::vector<int> >::iterator pairIt;
159  for (pairIt=pairs.begin(); pairIt!=pairs.end(); ++pairIt) {
160  unsigned int first = (*pairIt)[0];
161  unsigned int second = (*pairIt)[1];
162  const float mass = calculateInvariantMass( ((*particles)[first])->p4().Vect(),
163  ((*particles2)[second])->p4().Vect(),
166  masses->push_back(mass);
167  }
168  }
169 
170  return StatusCode::SUCCESS;
171 
172  }
173  float InvariantMassTool::calculateInvariantMass(const TVector3& v1, const TVector3&v2,float M1,float M2) {
174  TLorentzVector p1(v1, M1 > 0 ? std::hypot(M1, v1.Mag()) : v1.Mag());
175  TLorentzVector p2(v2, M2 > 0 ? std::hypot(M2, v2.Mag()) : v2.Mag());
176  return (p1+p2).M();
177 
178  }
179 
180 }
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
ExpressionParserUserBase< AthAlgTool, NUM_PARSER >::finalizeParser
StatusCode finalizeParser()
python.SystemOfUnits.second
int second
Definition: SystemOfUnits.py:120
DerivationFramework::kInvariantMassToolParserNum
@ kInvariantMassToolParserNum
Definition: InvariantMassTool.h:23
DerivationFramework::InvariantMassTool::InvariantMassTool
InvariantMassTool(const std::string &t, const std::string &n, const IInterface *p)
Definition: InvariantMassTool.cxx:18
python.PerfMonSerializer.p
def p
Definition: PerfMonSerializer.py:743
DerivationFramework::InvariantMassTool::m_containerName
SG::ReadHandleKey< xAOD::IParticleContainer > m_containerName
Definition: InvariantMassTool.h:37
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
AthCommonDataStore< AthCommonMsg< AlgTool > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
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::InvariantMassTool::getInvariantMasses
StatusCode getInvariantMasses(std::vector< float > *) const
Definition: InvariantMassTool.cxx:76
DerivationFramework::InvariantMassTool::finalize
StatusCode finalize()
Definition: InvariantMassTool.cxx:55
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
dqt_zlumi_pandas.mass
mass
Definition: dqt_zlumi_pandas.py:170
python.CreateTierZeroArgdict.pairs
pairs
Definition: CreateTierZeroArgdict.py:201
DerivationFramework::InvariantMassTool::calculateInvariantMass
static float calculateInvariantMass(const TVector3 &v1, const TVector3 &v2, float M1, float M2)
Definition: InvariantMassTool.cxx:173
DerivationFramework::InvariantMassTool::initialize
StatusCode initialize()
Definition: InvariantMassTool.cxx:34
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::InvariantMassTool::m_expression2
std::string m_expression2
Definition: InvariantMassTool.h:34
DerivationFramework::InvariantMassTool::addBranches
virtual StatusCode addBranches() const
Pass the thinning service
Definition: InvariantMassTool.cxx:61
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
InvariantMassTool.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
DerivationFramework::kInvariantMassToolParser2
@ kInvariantMassToolParser2
Definition: InvariantMassTool.h:23
DerivationFramework::InvariantMassTool::m_expression
std::string m_expression
Definition: InvariantMassTool.h:33
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::InvariantMassTool::m_containerName2
SG::ReadHandleKey< xAOD::IParticleContainer > m_containerName2
Definition: InvariantMassTool.h:38
DataVector
Derived DataVector<T>.
Definition: DataVector.h:581
ExpressionParserUser< AthAlgTool, kInvariantMassToolParserNum >::initializeParser
StatusCode initializeParser(const ExpressionParsing::SelectionArg< NUM_PARSER > &selection_string)
ExpressionParserUser
Definition: ExpressionParserUser.h:107
ReadCellNoiseFromCoolCompare.v2
v2
Definition: ReadCellNoiseFromCoolCompare.py:364
SG::WriteHandle
Definition: StoreGate/StoreGate/WriteHandle.h:76
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
DerivationFramework::InvariantMassTool::m_massHypothesis2
float m_massHypothesis2
Definition: InvariantMassTool.h:36
LArG4FSStartPointFilter.particles
list particles
Definition: LArG4FSStartPointFilter.py:84
DerivationFramework::InvariantMassTool::m_massHypothesis
float m_massHypothesis
Definition: InvariantMassTool.h:36
entries
double entries
Definition: listroot.cxx:49
DerivationFramework::InvariantMassTool::m_sgName
SG::WriteHandleKey< std::vector< float > > m_sgName
Definition: InvariantMassTool.h:35
DerivationFramework::kInvariantMassToolParser1
@ kInvariantMassToolParser1
Definition: InvariantMassTool.h:23
AthAlgTool
Definition: AthAlgTool.h:26
dqBeamSpot.nEntries
int nEntries
Definition: dqBeamSpot.py:73
SUSY_SimplifiedModel_PreInclude.masses
dictionary masses
Definition: SUSY_SimplifiedModel_PreInclude.py:7