ATLAS Offline Software
InvariantMassTool.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2025 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 
12 #include <utility> //for std::pair
13 #include <cmath> //for std::hypot
14 
15 namespace DerivationFramework {
16 
18  const std::string& n,
19  const IInterface* p) :
20  base_class(t,n,p),
21  m_expression("true"),
22  m_expression2(""),
23  m_massHypothesis(0.0),
24  m_massHypothesis2(0.0)
25  {
26  declareProperty("ObjectRequirements", m_expression);
27  declareProperty("SecondObjectRequirements", m_expression2);
28  declareProperty("MassHypothesis", m_massHypothesis);
29  declareProperty("SecondMassHypothesis", m_massHypothesis2);
30  }
31 
33  {
34  if (m_sgName.key().empty()) {
35  ATH_MSG_ERROR("No SG name provided for the output of invariant mass tool!");
36  return StatusCode::FAILURE;
37  }
39 
41  ATH_CHECK(initializeParser({m_expression,m_expression2} ) );
42 
43  ATH_CHECK(m_containerName.initialize());
44  if (!m_containerName2.key().empty()) {
45  ATH_CHECK(m_containerName2.initialize());
46  }
47 
49 
50  return StatusCode::SUCCESS;
51  }
52 
54  {
55  ATH_CHECK( finalizeParser());
56  return StatusCode::SUCCESS;
57  }
58 
59  StatusCode InvariantMassTool::addBranches(const EventContext& ctx) const
60  {
61  // Write masses to SG for access by downstream algs
62  if (evtStore()->contains<std::vector<float> >(m_sgName.key())) {
63  ATH_MSG_ERROR("Tool is attempting to write a StoreGate key " << m_sgName << " which already exists. Please use a different key");
64  return StatusCode::FAILURE;
65  }
66  std::unique_ptr<std::vector<float> > masses(new std::vector<float>());
67  ATH_CHECK(getInvariantMasses(masses.get(), ctx));
68  //CHECK(evtStore()->record(std::move(masses), m_sgName));
69  SG::WriteHandle<std::vector<float> > writeHandle(m_sgName, ctx);
70  ATH_CHECK(writeHandle.record(std::move(masses)));
71  return StatusCode::SUCCESS;
72  }
73 
74  StatusCode InvariantMassTool::getInvariantMasses(std::vector<float>* masses, const EventContext& ctx) const
75  {
76 
77  // check the relevant information is available
78  if (m_containerName.key().empty()) {
79  ATH_MSG_WARNING("Input container missing - returning zero");
80  masses->push_back(0.0);
81  return StatusCode::FAILURE;
82  }
83 
85 
86  bool from2Collections(false);
87  const xAOD::IParticleContainer* particles2{nullptr};
88  if (!m_containerName2.key().empty() && m_containerName2.key()!=m_containerName.key()) {
90  particles2=particleHdl2.cptr();
91  from2Collections = true;
92  }
93 
94  // get the positions of the elements which pass the requirement
95  std::vector<int> entries = m_parser[kInvariantMassToolParser1]->evaluateAsVector();
96  std::vector<int> entries2 = m_parser[kInvariantMassToolParser2]->evaluateAsVector();
97  unsigned int nEntries = entries.size();
98  unsigned int nEntries2 = entries2.size();
99 
100  // check the sizes are compatible
101  if (!from2Collections) {
102  if ( (particles->size() != nEntries) || (particles->size() != nEntries2) || (nEntries!=nEntries2) ) {
103  ATH_MSG_ERROR("Branch sizes incompatible - returning zero. Check your selection strings.");
104  masses->push_back(0.0);
105  return StatusCode::FAILURE;
106  }
107  }
108  if (from2Collections) {
109  if ( (particles->size() != nEntries) || (particles2->size() != nEntries2) ) {
110  ATH_MSG_ERROR("Branch sizes incompatible - returning zero. Check your selection strings.");
111  masses->push_back(0.0);
112  return StatusCode::FAILURE;
113  }
114  }
115 
116  // Double loop to get all possible index pairs
117  unsigned int outerIt, innerIt;
118  std::vector<std::pair<int, int> > pairs;
119  // Loop for case where both legs are from the same container
120  if (!from2Collections) {
121  for (outerIt=0; outerIt<nEntries; ++outerIt) {
122  for (innerIt=outerIt+1; innerIt<nEntries; ++innerIt) {
123  pairs.push_back({static_cast<int>(outerIt),static_cast<int>(innerIt)});
124  }
125  }
126  // Select the pairs for which the mass should be calculated, and then calculate it
127  for (const auto & [first, second]: pairs) {
128  if ( (entries[first]==1 && entries2[second]==1) || (entries2[first]==1 && entries[second]==1) ) {
129  const float mass = calculateInvariantMass( ((*particles)[first])->p4().Vect(),
130  ((*particles)[second])->p4().Vect(),
133  masses->push_back(mass);
134  }
135  }
136  }
137 
138  // Loop for case where both legs are from different containers
139  if (from2Collections) {
140  for (outerIt=0; outerIt<nEntries; ++outerIt) {
141  if (entries[outerIt]==0) continue;
142  for (innerIt=0; innerIt<nEntries2; ++innerIt) {
143  if (entries2[innerIt]==0) continue;
144  pairs.push_back({static_cast<int>(outerIt),static_cast<int>(innerIt)});
145  }
146  }
147  // Select the pairs for which the mass should be calculated, and then calculate it
148  for (const auto & [first, second]: pairs) {
149  const float mass = calculateInvariantMass( ((*particles)[first])->p4().Vect(),
150  ((*particles2)[second])->p4().Vect(),
153  masses->push_back(mass);
154  }
155  }
156 
157  return StatusCode::SUCCESS;
158 
159  }
160  float InvariantMassTool::calculateInvariantMass(const TVector3& v1, const TVector3&v2,float M1,float M2) {
161  TLorentzVector p1(v1, M1 > 0 ? std::hypot(M1, v1.Mag()) : v1.Mag());
162  TLorentzVector p2(v2, M2 > 0 ? std::hypot(M2, v2.Mag()) : v2.Mag());
163  return (p1+p2).M();
164 
165  }
166 
167 }
DerivationFramework::InvariantMassTool::InvariantMassTool
InvariantMassTool(const std::string &t, const std::string &n, const IInterface *p)
Definition: InvariantMassTool.cxx:17
Base_Fragment.mass
mass
Definition: Sherpa_i/share/common/Base_Fragment.py:59
DerivationFramework::InvariantMassTool::m_containerName
SG::ReadHandleKey< xAOD::IParticleContainer > m_containerName
Definition: InvariantMassTool.h:43
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:67
TRTCalib_cfilter.p1
p1
Definition: TRTCalib_cfilter.py:130
python.SystemOfUnits.second
float second
Definition: SystemOfUnits.py:135
DerivationFramework::InvariantMassTool::finalize
StatusCode finalize()
Definition: InvariantMassTool.cxx:53
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
DerivationFramework::InvariantMassTool::calculateInvariantMass
static float calculateInvariantMass(const TVector3 &v1, const TVector3 &v2, float M1, float M2)
Definition: InvariantMassTool.cxx:160
DerivationFramework::InvariantMassTool::initialize
StatusCode initialize()
Definition: InvariantMassTool.cxx:32
TRTCalib_cfilter.p2
p2
Definition: TRTCalib_cfilter.py:131
DerivationFramework::InvariantMassTool::m_expression2
std::string m_expression2
Definition: InvariantMassTool.h:40
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:209
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
InvariantMassTool.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
DerivationFramework::kInvariantMassToolParser2
@ kInvariantMassToolParser2
Definition: InvariantMassTool.h:29
DerivationFramework::InvariantMassTool::m_expression
std::string m_expression
Definition: InvariantMassTool.h:39
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:44
DataVector
Derived DataVector<T>.
Definition: DataVector.h:795
DerivationFramework::InvariantMassTool::getInvariantMasses
StatusCode getInvariantMasses(std::vector< float > *, const EventContext &ctx) const
Definition: InvariantMassTool.cxx:74
DerivationFramework::InvariantMassTool::addBranches
virtual StatusCode addBranches(const EventContext &ctx) const
Definition: InvariantMassTool.cxx:59
ReadCellNoiseFromCoolCompare.v2
v2
Definition: ReadCellNoiseFromCoolCompare.py:364
SG::WriteHandle
Definition: StoreGate/StoreGate/WriteHandle.h:73
SG::WriteHandle::record
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
DerivationFramework::InvariantMassTool::m_inputDecorNames
SG::ReadDecorHandleKeyArray< xAOD::IParticleContainer > m_inputDecorNames
Definition: InvariantMassTool.h:45
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:42
LArG4FSStartPointFilter.particles
list particles
Definition: LArG4FSStartPointFilter.py:84
DerivationFramework::InvariantMassTool::m_massHypothesis
float m_massHypothesis
Definition: InvariantMassTool.h:42
entries
double entries
Definition: listroot.cxx:49
DerivationFramework::InvariantMassTool::m_sgName
SG::WriteHandleKey< std::vector< float > > m_sgName
Definition: InvariantMassTool.h:41
DerivationFramework::kInvariantMassToolParser1
@ kInvariantMassToolParser1
Definition: InvariantMassTool.h:29
dqBeamSpot.nEntries
int nEntries
Definition: dqBeamSpot.py:72
SG::AllowEmpty
@ AllowEmpty
Definition: StoreGate/StoreGate/VarHandleKey.h:27
SUSY_SimplifiedModel_PreInclude.masses
dictionary masses
Definition: SUSY_SimplifiedModel_PreInclude.py:7