ATLAS Offline Software
MatchFromCompositeTool.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
3 */
4 
7 
8 #include "CxxUtils/crc64.h"
9 
10 #ifdef XAOD_STANDALONE
11 namespace {
12  SG::sgkey_t hashContainer(const std::string &container, CLID clid)
13  {
14  static const SG::sgkey_t sgkey_t_max = (static_cast<SG::sgkey_t>(1) << 30) - 1;
15  return CxxUtils::crc64addint(CxxUtils::crc64(container), clid) & sgkey_t_max;
16  }
17 }
18 #endif
19 
20 namespace Trig {
22  asg::AsgTool(name)
23  {
24  declareProperty("MatchShallow", m_matchShallow,
25  "Whether to check if two objects are shallow copies of each other. "
26  "If this is not true then pointer equality will be used.");
27  declareProperty("DRThreshold", m_drThreshold,
28  "If greater than 0 then use the DR between two objects to check if "
29  "they are the same object.");
30  declareProperty("InputPrefix", m_inputPrefix="TrigMatch_",
31  "The input prefix to expect at the beginning of the TrigComposite "
32  "container names.");
33 #ifdef XAOD_STANDALONE
34  declareProperty("RemapBrokenLinks", m_remapBrokenLinks,
35  "Whether to remap element links which are broken in some derivations for AnalysisBase");
36  declareProperty("RemapContainers", m_remapContainers,
37  "Containers whose links need remapping");
38  declareProperty("RemapCLIDs", m_remapCLIDs, "CLIDs for those containers");
39 #endif
40  }
41 
43 
45  ATH_MSG_INFO( "initializing " << name() );
46 #ifdef XAOD_STANDALONE
47  ATH_MSG_INFO("Remap broken links? " << m_remapBrokenLinks);
48  if (m_remapBrokenLinks)
49  {
50  if (m_remapContainers.size() != m_remapCLIDs.size())
51  {
52  ATH_MSG_ERROR("Number of containers and CLIDs to remap do not match!");
53  return StatusCode::FAILURE;
54  }
56  for (std::size_t idx = 0; idx < m_remapContainers.size(); ++idx)
57  {
58  const std::string &name = m_remapContainers[idx];
59  m_keyRemap[hashContainer(name, iparticleCLID)] = hashContainer(name, m_remapCLIDs[idx]);
60  }
61  ATH_MSG_INFO("Remap: ");
62  for (const auto &p : m_keyRemap)
63  ATH_MSG_INFO("\t" << p.first << " -> " << p.second);
64 
65  }
66 #endif
67  return StatusCode::SUCCESS;
68  }
69 
71  const xAOD::IParticle& recoObject,
72  const std::string& chain,
73  double, bool) const
74  {
75  return match({&recoObject}, chain);
76  }
77 
79  const std::vector<const xAOD::IParticle*>& recoObjects,
80  const std::string& chain,
81  double, bool) const
82  {
83  std::string containerName = m_inputPrefix+chain;
84  // We have to replace '.' characters with '_' characters so that these are
85  // valid container names...
86  std::replace(containerName.begin(), containerName.end(), '.', '_');
87  const xAOD::TrigCompositeContainer* composites(nullptr);
88  if (evtStore()->retrieve(composites, containerName).isFailure() ){
89  ATH_MSG_ERROR("Failed to retrieve composite container for chain "+chain);
90  ATH_MSG_ERROR("Please check your derivation to see if the container is there");
91  ATH_MSG_ERROR("This likely means the trigger is not in your file's menu");
92  throw std::runtime_error(
93  "Failed to retrieve composite corresponding to chain " + chain);
94  }
95  for (const xAOD::TrigComposite* composite : *composites) {
96  static const constAcc_t<vecLink_t<xAOD::IParticleContainer>> accMatched(
97  "TrigMatchedObjects");
98  if (testCombination(accMatched(*composite), recoObjects) )
99  return true;
100  }
101  // If we get here then none of the online combinations worked
102  return false;
103  }
104 
106  const vecLink_t<xAOD::IParticleContainer>& onlineLinks,
107  const std::vector<const xAOD::IParticle*>& offline) const
108  {
109  // We need to make sure we don't allow two offline particles to match to the
110  // same online particle, so we copy the vector so we can erase objects if
111  // necessary. Dereference the links at the same time.
112  std::vector<const xAOD::IParticle*> online;
113  online.reserve(onlineLinks.size() );
114  for (const ElementLink<xAOD::IParticleContainer>& link : onlineLinks)
115  // Skip invalid links - these are usually objects that have been removed
116  // by derivation framework level thinning. Going by the logic that we just
117  // need a match for all the offline particles provided rather than for the
118  // trigger particles implies that we should allow a combination that has
119  // had some of its members removed.
120  if (link.isValid() )
121  online.push_back(*link);
122 #ifdef XAOD_STANDALONE
123  else if (m_remapBrokenLinks)
124  {
125  auto itr = m_keyRemap.find(link.persKey());
126  if (itr != m_keyRemap.end())
127  online.push_back(*ElementLink<xAOD::IParticleContainer>(itr->second, link.index()));
128  }
129 #endif
130  // I will follow the way the current tool works and match even if there are
131  // fewer reco objects than trigger objects
132  for (const xAOD::IParticle* offlinePart : offline) {
133  bool isMatched = false;
134  for (auto itr = online.begin(); itr != online.end(); ++itr) {
135  if (areTheSame(*offlinePart, **itr) ) {
136  // Remove this online particle from consideration
137  online.erase(itr);
138  isMatched = true;
139  break;
140  }
141  }
142  if (!isMatched)
143  // We've found an offline particle we couldn't match so the whole
144  // combination doesn't match.
145  return false;
146  }
147  // To reach this point every offline particle must have found a match.
148  return true;
149  }
150 
152  const xAOD::IParticle& lhs,
153  const xAOD::IParticle& rhs) const
154  {
155  // If we've been given a dR threshold > 0 then we just use that.
156  if (m_drThreshold > 0) {
157  return xAOD::P4Helpers::deltaR(lhs, rhs, false) < m_drThreshold;
158  }
159  else if (m_matchShallow) {
160  static const SG::AuxElement::ConstAccessor<
161  ElementLink<xAOD::IParticleContainer>> accOOL("originalObjectLink");
162  // For now assume that we've got a shallow copy iff this is available
163  const xAOD::IParticle* lhsOrig =
164  accOOL.isAvailable(lhs) ? *accOOL(lhs) : &lhs;
165  const xAOD::IParticle* rhsOrig =
166  accOOL.isAvailable(rhs) ? *accOOL(rhs) : &rhs;
167  return lhsOrig == rhsOrig;
168  }
169  else {
170  // Otherwise just use pointer equality
171  return &lhs == &rhs;
172  }
173  }
174 
175 } //> end namespace Trig
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
Trig::MatchFromCompositeTool::m_inputPrefix
std::string m_inputPrefix
The prefix to expect at the front of the trig composite container name.
Definition: MatchFromCompositeTool.h:88
replace
std::string replace(std::string s, const std::string &s2, const std::string &s3)
Definition: hcg.cxx:307
Trig::MatchFromCompositeTool::m_keyRemap
std::map< SG::sgkey_t, SG::sgkey_t > m_keyRemap
Definition: MatchFromCompositeTool.h:121
python.PerfMonSerializer.p
def p
Definition: PerfMonSerializer.py:743
runLayerRecalibration.chain
chain
Definition: runLayerRecalibration.py:175
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
Trig::MatchFromCompositeTool::MatchFromCompositeTool
MatchFromCompositeTool(const std::string &name)
Standard constructor.
Definition: MatchFromCompositeTool.cxx:21
Trig
The common trigger namespace for trigger analysis tools.
Definition: CaloTowerVecMon.h:44
xAODP4Helpers.h
AthCommonDataStore< AthCommonMsg< AlgTool > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
Trig::MatchFromCompositeTool::~MatchFromCompositeTool
~MatchFromCompositeTool() override
Default destructor.
Definition: MatchFromCompositeTool.cxx:42
Trig::MatchFromCompositeTool::areTheSame
bool areTheSame(const xAOD::IParticle &lhs, const xAOD::IParticle &rhs) const
Check if two particles are the same.
Definition: MatchFromCompositeTool.cxx:151
asg
Definition: DataHandleTestTool.h:28
offline
InDetSecVtxTruthMatchUtils::isMatched
bool isMatched(int matchInfo)
Definition: InDetSecVtxTruthMatchTool.h:48
SG::ConstAccessor
Helper class to provide constant type-safe access to aux data.
Definition: ConstAccessor.h:54
Trig::MatchFromCompositeTool::vecLink_t
std::vector< ElementLink< T > > vecLink_t
Definition: MatchFromCompositeTool.h:77
MatchFromCompositeTool.h
xAOD::IParticle
Class providing the definition of the 4-vector interface.
Definition: Event/xAOD/xAODBase/xAODBase/IParticle.h:40
Trig::MatchFromCompositeTool::m_drThreshold
float m_drThreshold
If greater than 0 then will skip the above check and just check that DR between the two particles is ...
Definition: MatchFromCompositeTool.h:85
Trig::MatchFromCompositeTool::testCombination
bool testCombination(const vecLink_t< xAOD::IParticleContainer > &onlineLinks, const std::vector< const xAOD::IParticle * > &offline) const
Test a combination of offline objects against a combination of online objects.
Definition: MatchFromCompositeTool.cxx:105
ClassID_traits::ID
static const CLID & ID()
the CLID of T
Definition: Control/AthenaKernel/AthenaKernel/ClassID_traits.h:50
AthCommonDataStore< AthCommonMsg< AlgTool > >::evtStore
ServiceHandle< StoreGateSvc > & evtStore()
The standard StoreGateSvc (event store) Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:85
TileSynchronizeBch.online
online
Definition: TileSynchronizeBch.py:88
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
Trig::MatchFromCompositeTool::m_matchShallow
bool m_matchShallow
Allow matching shallow copy to shallow copy.
Definition: MatchFromCompositeTool.h:81
xAOD::P4Helpers::deltaR
double deltaR(double rapidity1, double phi1, double rapidity2, double phi2)
from bare bare rapidity,phi
Definition: xAODP4Helpers.h:150
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
CxxUtils::crc64addint
uint64_t crc64addint(uint64_t crc, uint64_t x)
Extend a previously-calculated CRC to include an int.
Definition: crc64.cxx:732
xAOD::TrigComposite_v1
Class used to describe composite objects in the HLT.
Definition: TrigComposite_v1.h:52
DataVector
Derived DataVector<T>.
Definition: DataVector.h:581
CLID
uint32_t CLID
The Class ID type.
Definition: Event/xAOD/xAODCore/xAODCore/ClassID_traits.h:47
CxxUtils::crc64
uint64_t crc64(const CRCTable &table, const char *data, size_t data_len)
Find the CRC-64 of a string,.
Definition: crc64.cxx:696
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:192
SG::sgkey_t
uint32_t sgkey_t
Type used for hashed StoreGate key+CLID pairs.
Definition: CxxUtils/CxxUtils/sgkey_t.h:32
xAOD::IParticle::isAvailable
bool isAvailable(const std::string &name, const std::string &clsname="") const
Check if a user property is available for reading or not.
Definition: Event/xAOD/xAODBase/xAODBase/IParticle.h:131
Trig::MatchFromCompositeTool::match
bool match(const xAOD::IParticle &recoObject, const std::string &chain, double=0.1, bool=false) const override
Match a single object.
Definition: MatchFromCompositeTool.cxx:70
LArNewCalib_DelayDump_OFC_Cali.idx
idx
Definition: LArNewCalib_DelayDump_OFC_Cali.py:69
crc64.h
A crc-64 implementation, using pclmul where possible.
Trig::MatchFromCompositeTool::initialize
StatusCode initialize() override
Initialise the tool.
Definition: MatchFromCompositeTool.cxx:44