ATLAS Offline Software
Loading...
Searching...
No Matches
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
11namespace {
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
20namespace 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) {
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) {
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
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
uint32_t CLID
The Class ID type.
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T, V, H > &t)
ServiceHandle< StoreGateSvc > & evtStore()
SG::ConstAccessor< T, ALLOC > ConstAccessor
Definition AuxElement.h:569
std::map< SG::sgkey_t, SG::sgkey_t > m_keyRemap
~MatchFromCompositeTool() override
Default destructor.
MatchFromCompositeTool(const std::string &name)
Standard constructor.
bool match(const xAOD::IParticle &recoObject, const std::string &chain, double=0.1, bool=false) const override
Match a single object.
float m_drThreshold
If greater than 0 then will skip the above check and just check that DR between the two particles is ...
std::vector< ElementLink< T > > vecLink_t
StatusCode initialize() override
Initialise the tool.
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.
SG::AuxElement::ConstAccessor< T > constAcc_t
Helper typedefs for accessors/decorators, vectors of ele links.
bool areTheSame(const xAOD::IParticle &lhs, const xAOD::IParticle &rhs) const
Check if two particles are the same.
std::string m_inputPrefix
The prefix to expect at the front of the trig composite container name.
bool m_matchShallow
Allow matching shallow copy to shallow copy.
AsgTool(const std::string &name)
Constructor specifying the tool instance's name.
Definition AsgTool.cxx:58
Class providing the definition of the 4-vector interface.
XAOD_AUXDATA_DEPRECATED bool isAvailable(const std::string &name, const std::string &clsname="") const
Check if a user property is available for reading or not.
A crc-64 implementation, using pclmul where possible.
uint64_t crc64(const CRCTable &table, const char *data, size_t data_len)
Find the CRC-64 of a string,.
Definition crc64.cxx:696
uint64_t crc64addint(uint64_t crc, uint64_t x)
Extend a previously-calculated CRC to include an int.
Definition crc64.cxx:732
uint32_t sgkey_t
Type used for hashed StoreGate key+CLID pairs.
Definition sgkey_t.h:32
The common trigger namespace for trigger analysis tools.
double deltaR(double rapidity1, double phi1, double rapidity2, double phi2)
from bare bare rapidity,phi
TrigCompositeContainer_v1 TrigCompositeContainer
Declare the latest version of the container.
TrigComposite_v1 TrigComposite
Declare the latest version of the class.