ATLAS Offline Software
Loading...
Searching...
No Matches
DerivationFramework::TriggerMatchingTool Class Referencefinal

Tool to perform trigger matching in the derivation framework. More...

#include <TriggerMatchingTool.h>

Inheritance diagram for DerivationFramework::TriggerMatchingTool:
Collaboration diagram for DerivationFramework::TriggerMatchingTool:

Public Types

using particleVec_t = std::vector<const xAOD::IParticle *>
 Helper typedefs.

Public Member Functions

 TriggerMatchingTool (const std::string &type, const std::string &name, const IInterface *pSvcLocator)
 Constructor.
virtual StatusCode initialize () override
 Initialize the tool.
virtual StatusCode addBranches (const EventContext &ctx) const override
 Calculate the matchings.

Private Member Functions

StatusCode createOutputContainer (xAOD::TrigCompositeContainer *&container, const std::string &chain) const
 Create an output container for the named chain.
const particleVec_tgetCandidateMatchesFor (const xAOD::IParticle *part, std::map< xAOD::Type::ObjectType, particleVec_t > &offlineParticles, std::map< const xAOD::IParticle *, particleVec_t > &cache) const
 Get all offline particles that could match a given online one.
bool matchParticles (const xAOD::IParticle *lhs, const xAOD::IParticle *rhs) const
 Check if the dR between two particles is below threshold.

Private Attributes

std::vector< std::string > m_chainNames ATLAS_THREAD_SAFE
 The list of chain names to match.
ToolHandle< Trig::IIParticleRetrievalToolm_trigParticleTool {"Trig::IParticleRetrievalTool/OnlineParticleTool"}
 The tool to retrieve the online candidates.
std::map< xAOD::Type::ObjectType, SG::ReadHandleKey< xAOD::IParticleContainer > > m_offlineInputs
 The input containers to use. These are keyed by xAOD object type.
float m_drThreshold
 The DR threshold to use.
bool m_rerun
 Whether to match in rerun mode or not.
std::string m_outputPrefix
 The prefix to place at the beginning of the output containers.
bool m_checkEmptyChainGroups
 If set, discard any triggers with empty chain groups (break the job otherwise).
bool m_inputDependentConfig
 If using an input-file-dependent config then we warn when triggers are removed.
ToolHandle< Trig::TrigDecisionToolm_tdt {"Trig::TrigDecisionTool/TrigDecisionTool"}
 The trig decision tool.
ToolHandle< Trig::IMatchScoringToolm_scoreTool
 The pair scoring tool.

Detailed Description

Tool to perform trigger matching in the derivation framework.

Trigger matching in the derivation framework has to take the reverse approach to matching in analysis code. There, the matching tool is provided with a list of analysis particles to match to a given trigger. The navigation then proceeds to search out all features that match the types of those particles. In the derivation framework however, we need to get the full list of all particles that a user could ask for in their analysis code. To do this, we ask the navigation for all the relevant xAOD objects and then use those to query the available offline objects.

Author
Jon Burr

Definition at line 38 of file TriggerMatchingTool.h.

Member Typedef Documentation

◆ particleVec_t

Helper typedefs.

Definition at line 42 of file TriggerMatchingTool.h.

Constructor & Destructor Documentation

◆ TriggerMatchingTool()

DerivationFramework::TriggerMatchingTool::TriggerMatchingTool ( const std::string & type,
const std::string & name,
const IInterface * pSvcLocator )

Constructor.

Definition at line 42 of file TriggerMatchingTool.cxx.

45 :
46 base_class(type, name, pSvcLocator)
47 {
48 declareProperty("ChainNames", m_chainNames,
49 "The list of trigger chains to match.");
50 declareProperty("OnlineParticleTool", m_trigParticleTool,
51 "The tool to retrieve online particles from the navigation.");
52 declareProperty("InputElectrons",
54 "Offline electron candidates for matching.");
55 declareProperty("InputPhotons",
57 "Offline photon candidates for matching.");
58 declareProperty("InputMuons",
60 "Offline muon candidates for matching.");
61 declareProperty("InputTaus",
63 "Offline tau candidates for matching.");
64 declareProperty("DRThreshold", m_drThreshold = 0.1,
65 "The maximum dR between an offline and an online particle to consider "
66 "a match between them.");
67 declareProperty("Rerun", m_rerun = true,
68 "Whether to match triggers in rerun mode.");
69 declareProperty("OutputContainerPrefix", m_outputPrefix="TrigMatch_",
70 "The prefix to add to the output containers.");
71 declareProperty("CheckEmptyChainGroups", m_checkEmptyChainGroups = true,
72 "If set, discard any empty chain groups. Otherwise these will cause "
73 "a job failure.");
74 declareProperty("InputDependentConfig", m_inputDependentConfig=false,
75 "Warn when a trigger is removed (if the configuration is dependent "
76 "on the inputs, removal is not expected).");
77 }
bool m_checkEmptyChainGroups
If set, discard any triggers with empty chain groups (break the job otherwise).
std::map< xAOD::Type::ObjectType, SG::ReadHandleKey< xAOD::IParticleContainer > > m_offlineInputs
The input containers to use. These are keyed by xAOD object type.
std::string m_outputPrefix
The prefix to place at the beginning of the output containers.
bool m_rerun
Whether to match in rerun mode or not.
ToolHandle< Trig::IIParticleRetrievalTool > m_trigParticleTool
The tool to retrieve the online candidates.
bool m_inputDependentConfig
If using an input-file-dependent config then we warn when triggers are removed.
float m_drThreshold
The DR threshold to use.
@ Photon
The object is a photon.
Definition ObjectType.h:47
@ Muon
The object is a muon.
Definition ObjectType.h:48
@ Electron
The object is an electron.
Definition ObjectType.h:46
@ Tau
The object is a tau (jet)
Definition ObjectType.h:49

Member Function Documentation

◆ addBranches()

StatusCode DerivationFramework::TriggerMatchingTool::addBranches ( const EventContext & ctx) const
overridevirtual

Calculate the matchings.

Definition at line 96 of file TriggerMatchingTool.cxx.

97 {
98 [[maybe_unused]] static const bool firstEvent = [&](){
99 auto itr = m_chainNames.begin();
100 while (itr != m_chainNames.end() ) {
101 const Trig::ChainGroup* cg = m_tdt->getChainGroup(*itr);
102 if (cg->getListOfTriggers().size() == 0){
104 ATH_MSG_WARNING("Removing trigger " << (*itr) << " -- suggests a bad tool configuration (asking for triggers not in the menu)");
105 // We are now modifying the mutable m_chainNames but since it is done
106 // within this static initialization this is thread-safe:
107 itr = m_chainNames.erase(itr);
108 } else
109 ++itr;
110 }
111 return false;
112 }();
113
114 const Atlas::ExtendedEventContext &extendedCtx = Atlas::getExtendedEventContext(ctx);
115
116 // Now, get all the possible offline candidates
117 std::map<xAOD::Type::ObjectType, const xAOD::IParticleContainer *> offlineContainers;
118 std::map<xAOD::Type::ObjectType, particleVec_t> offlineCandidates;
119 for (const auto& p : m_offlineInputs) {
120 // Skip any that may have been disabled by providing an empty string to
121 // the corresponding property
122 if (p.second.empty() )
123 continue;
124 auto handle = SG::makeHandle(p.second, ctx);
125 if (!handle.isValid())
126 {
127 ATH_MSG_ERROR("Failed to retrieve " << p.second);
128 return StatusCode::FAILURE;
129 }
130 offlineContainers[p.first] = handle.ptr();
131 offlineCandidates.emplace(std::make_pair(
132 p.first,
133 particleVec_t(handle->begin(), handle->end() )
134 ) );
135 }
136
137 // Store possible matches from online to offline particles here
138 // We do this as multiple chains may use the same online particles so we can
139 // reuse this information.
140 std::map<const xAOD::IParticle*, particleVec_t> matches;
141
142 // Iterate through the chains to get the matchings
143 for (const std::string& chain : m_chainNames) {
144 // Create the output
145 xAOD::TrigCompositeContainer* container(nullptr);
146 ATH_CHECK( createOutputContainer(container, chain) );
147
148 // Get the list of online combinations
149 std::vector<particleVec_t> onlineCombinations;
150 ATH_CHECK( m_trigParticleTool->retrieveParticles(onlineCombinations, chain, m_rerun) );
151
153 onlineCombinations.size() << " combinations found for chain" << chain);
154
155 // If no combinations were found (because the trigger was not passed) then
156 // we can skip this trigger
157 if (onlineCombinations.size() == 0)
158 continue;
159
160 using particleRange_t = TriggerMatchingUtils::RangedItr<typename particleVec_t::const_iterator>;
161 // Now build up the list of offline combinations;
162 std::vector<particleVec_t> offlineCombinations;
163
164 // Projection operator to use for comparing vectors
165 // of IParticle*. Compare by using the sum of the pts
166 // of all particles in the vector.
167 struct outerproj {
168 double operator() (const particleVec_t& v) const
169 {
170 // Unfortunately std::ranges::accumulate hasn't made it into C++
171 // as of C++23 or we could use that directly.
172 auto r = std::views::transform (v, &xAOD::IParticle::pt);
173 return std::accumulate (std::begin(r), std::end(r), 0);
174 }
175 };
176
177 for (const particleVec_t& combination : onlineCombinations) {
178 // Here we store the possible candidates for the matching. We use the
179 // range type as a lightweight method to carry around a view of a vector
180 // without copying it (the RangedItr is essentially a combination of 3
181 // iterators).
182 std::vector<particleRange_t> matchCandidates;
183 for (const xAOD::IParticle* part : combination) {
184 const particleVec_t& possibleMatches = getCandidateMatchesFor(
185 part, offlineCandidates, matches);
186 matchCandidates.emplace_back(possibleMatches.begin(), possibleMatches.end() );
187 } //> end loop over particles
188 // Get all possible combinations of offline objects that could match to
189 // this particular online combination.
190 auto theseOfflineCombinations =
192 matchCandidates,
194 outerproj());
195 if (msgLvl(MSG::VERBOSE) ) {
196 // Spit out some verbose information
198 "Checking matching for chain " << chain
199 << " with " << matchCandidates.size() << " legs");
200 std::size_t idx = 0;
201 for (const particleRange_t& range : matchCandidates)
202 ATH_MSG_VERBOSE( "Leg #" << idx++ << " has " << range.size()
203 << " offline candidates." );
205 "Matching generated " << theseOfflineCombinations.size()
206 << " offline combinations");
207 }
208 // Now push them back into the output. Use a specialised function for
209 // inserting into a sorted vector that ensures that we only output
210 // unique combinations
211 for (const particleVec_t& vec : theseOfflineCombinations)
212 TriggerMatchingUtils::insertIntoSortedVector(offlineCombinations, vec, outerproj());
213 } //> end loop over combinations
214
215
216 // Decorate the found combinations onto trigger composites.
217 for (const particleVec_t& foundCombination : offlineCombinations) {
218 xAOD::TrigComposite* composite = new xAOD::TrigComposite();
219 container->push_back(composite);
220 static const acc_t<vecLink_t<xAOD::IParticleContainer>> dec_links(
221 "TrigMatchedObjects");
222 vecLink_t<xAOD::IParticleContainer>& links = dec_links(*composite);
223 for (const xAOD::IParticle* part : foundCombination) {
224 const xAOD::IParticleContainer *container = offlineContainers.at(part->type());
225 // If we have an owning container then things are relatively simple:
226 // we can just construct the element link from its SG key and the
227 // index. Otherwise we have to locate the correct proxy from the element
228 if (container->trackIndices())
229 links.emplace_back(
230 m_offlineInputs.at(part->type()).key(),
231 part->index(),
232 extendedCtx.proxy());
233 else
234 links.push_back(makeLink<xAOD::IParticleContainer>(part, extendedCtx.proxy()));
235 }
236 } //> end loop over the found combinations
237 } //> end loop over chains
238 return StatusCode::SUCCESS;
239 }
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
std::vector< size_t > vec
const particleVec_t & getCandidateMatchesFor(const xAOD::IParticle *part, std::map< xAOD::Type::ObjectType, particleVec_t > &offlineParticles, std::map< const xAOD::IParticle *, particleVec_t > &cache) const
Get all offline particles that could match a given online one.
std::vector< const xAOD::IParticle * > particleVec_t
Helper typedefs.
ToolHandle< Trig::TrigDecisionTool > m_tdt
The trig decision tool.
StatusCode createOutputContainer(xAOD::TrigCompositeContainer *&container, const std::string &chain) const
Create an output container for the named chain.
std::vector< std::string > getListOfTriggers() const
virtual double pt() const =0
The transverse momentum ( ) of the particle.
int r
Definition globals.cxx:22
const ExtendedEventContext & getExtendedEventContext(const EventContext &ctx)
Retrieve an extended context from a context object.
std::vector< std::vector< T > > getAllDistinctCombinations(std::vector< RangedItr< typename std::vector< T >::const_iterator > > &inputs, INNERPROJ innerproj={}, OUTERPROJ outerproj={})
Get all valid, unique combinations of distinct elements from the input ranges.
bool insertIntoSortedVector(std::vector< T > &vec, const T &ele, PROJ proj={})
Helper function for inserting an element into a sorted vector.
SG::ReadCondHandle< T > makeHandle(const SG::ReadCondHandleKey< T > &key, const EventContext &ctx=Gaudi::Hive::currentContext())
TrigCompositeContainer_v1 TrigCompositeContainer
Declare the latest version of the container.
TrigComposite_v1 TrigComposite
Declare the latest version of the class.
DataVector< IParticle > IParticleContainer
Simple convenience declaration of IParticleContainer.

◆ createOutputContainer()

StatusCode DerivationFramework::TriggerMatchingTool::createOutputContainer ( xAOD::TrigCompositeContainer *& container,
const std::string & chain ) const
private

Create an output container for the named chain.

Parameters
[out]containerA pointer to the created container
chainThe name of the chain to create the container for The container will be recorded in the StoreGate using the name of the chain as a key.

Definition at line 241 of file TriggerMatchingTool.cxx.

244 {
245 auto uniqueContainer = std::make_unique<xAOD::TrigCompositeContainer>();
246 auto aux = std::make_unique<xAOD::AuxContainerBase>();
247 uniqueContainer->setStore(aux.get() );
248 container = uniqueContainer.get();
249 std::string name = m_outputPrefix+chain;
250 // We have to replace '.' characters with '_' characters so that these are
251 // valid container names...
252 std::replace(name.begin(), name.end(), '.', '_');
253 ATH_CHECK( evtStore()->record(std::move(uniqueContainer), name) );
254 ATH_CHECK( evtStore()->record(std::move(aux), name+"Aux.") );
255 return StatusCode::SUCCESS;
256 }
const T * get(size_type n) const
Access an element, as an rvalue.

◆ getCandidateMatchesFor()

const TriggerMatchingTool::particleVec_t & DerivationFramework::TriggerMatchingTool::getCandidateMatchesFor ( const xAOD::IParticle * part,
std::map< xAOD::Type::ObjectType, particleVec_t > & offlineParticles,
std::map< const xAOD::IParticle *, particleVec_t > & cache ) const
private

Get all offline particles that could match a given online one.

Parameters
partThe online particle to match against.
offlineParticlesThe offline particles, key by xAOD type.
cacheStore past matches here to speed up future look ups.

Definition at line 258 of file TriggerMatchingTool.cxx.

262 {
263 // Build up all the possible matches between online and offline particles
264 auto cacheItr = cache.find(part);
265 if (cacheItr == cache.end() ) {
268 if (type == xAOD::Type::CaloCluster) {
269 // If it's a calo cluster then we need to get the cluster from the
270 // egamma types.
271 static const constAcc_t<vecLink_t<xAOD::CaloClusterContainer>> acc_calo("caloClusterLinks");
272 for (xAOD::Type::ObjectType egType : {
274 {
275 for (const xAOD::IParticle* cand : offlineParticles[egType]) {
276 const vecLink_t<xAOD::CaloClusterContainer>& links = acc_calo(*cand);
277 if (links.size() == 0 || !links.at(0).isValid() )
278 continue;
279 const xAOD::CaloCluster* clus = *links.at(0);
280 if (matchParticles(part, clus) )
281 candidates.push_back(cand);
282 }
283 }
284 }
285 else {
286 for (const xAOD::IParticle* cand : offlineParticles[type])
287 if (matchParticles(part, cand) )
288 candidates.push_back(cand);
289 }
290 cacheItr = cache.emplace(
291 std::make_pair(part, std::move(candidates) ) ).first;
292 }
293 return cacheItr->second;
294 }
bool matchParticles(const xAOD::IParticle *lhs, const xAOD::IParticle *rhs) const
Check if the dR between two particles is below threshold.
ObjectType
Type of objects that have a representation in the xAOD EDM.
Definition ObjectType.h:32
@ CaloCluster
The object is a calorimeter cluster.
Definition ObjectType.h:39
CaloCluster_v1 CaloCluster
Define the latest version of the calorimeter cluster class.

◆ initialize()

StatusCode DerivationFramework::TriggerMatchingTool::initialize ( )
overridevirtual

Initialize the tool.

Definition at line 79 of file TriggerMatchingTool.cxx.

80 {
81 ATH_MSG_INFO( "Initializing " << name() );
82
83 // Remove any duplicates from the list of chain names
84 std::sort(m_chainNames.begin(), m_chainNames.end() );
85 m_chainNames.erase(std::unique(m_chainNames.begin(), m_chainNames.end() ), m_chainNames.end() );
86
87 ATH_CHECK( m_trigParticleTool.retrieve() );
88 ATH_CHECK( m_scoreTool.retrieve() );
89 for (auto &p : m_offlineInputs)
90 {
91 ATH_CHECK(p.second.initialize(SG::AllowEmpty));
92 }
93 return StatusCode::SUCCESS;
94 }
#define ATH_MSG_INFO(x)
ToolHandle< Trig::IMatchScoringTool > m_scoreTool
The pair scoring tool.
DataModel_detail::iterator< DVL > unique(typename DataModel_detail::iterator< DVL > beg, typename DataModel_detail::iterator< DVL > end)
Specialization of unique for DataVector/List.
void sort(typename DataModel_detail::iterator< DVL > beg, typename DataModel_detail::iterator< DVL > end)
Specialization of sort for DataVector/List.

◆ matchParticles()

bool DerivationFramework::TriggerMatchingTool::matchParticles ( const xAOD::IParticle * lhs,
const xAOD::IParticle * rhs ) const
private

Check if the dR between two particles is below threshold.

Definition at line 296 of file TriggerMatchingTool.cxx.

299 {
300 return m_scoreTool->score(*lhs, *rhs) < m_drThreshold;
301 }

Member Data Documentation

◆ ATLAS_THREAD_SAFE

std::vector<std::string> m_chainNames DerivationFramework::TriggerMatchingTool::ATLAS_THREAD_SAFE
mutableprivate

The list of chain names to match.

Definition at line 58 of file TriggerMatchingTool.h.

◆ m_checkEmptyChainGroups

bool DerivationFramework::TriggerMatchingTool::m_checkEmptyChainGroups
private

If set, discard any triggers with empty chain groups (break the job otherwise).

Definition at line 83 of file TriggerMatchingTool.h.

◆ m_drThreshold

float DerivationFramework::TriggerMatchingTool::m_drThreshold
private

The DR threshold to use.

Definition at line 73 of file TriggerMatchingTool.h.

◆ m_inputDependentConfig

bool DerivationFramework::TriggerMatchingTool::m_inputDependentConfig
private

If using an input-file-dependent config then we warn when triggers are removed.

Definition at line 87 of file TriggerMatchingTool.h.

◆ m_offlineInputs

std::map<xAOD::Type::ObjectType, SG::ReadHandleKey<xAOD::IParticleContainer> > DerivationFramework::TriggerMatchingTool::m_offlineInputs
private

The input containers to use. These are keyed by xAOD object type.

Definition at line 70 of file TriggerMatchingTool.h.

◆ m_outputPrefix

std::string DerivationFramework::TriggerMatchingTool::m_outputPrefix
private

The prefix to place at the beginning of the output containers.

Definition at line 79 of file TriggerMatchingTool.h.

◆ m_rerun

bool DerivationFramework::TriggerMatchingTool::m_rerun
private

Whether to match in rerun mode or not.

Definition at line 76 of file TriggerMatchingTool.h.

◆ m_scoreTool

ToolHandle<Trig::IMatchScoringTool> DerivationFramework::TriggerMatchingTool::m_scoreTool
private
Initial value:
{
this, "ScoringTool", "Trig::DRScoringTool", "The pair scoring tool"}

The pair scoring tool.

Definition at line 93 of file TriggerMatchingTool.h.

93 {
94 this, "ScoringTool", "Trig::DRScoringTool", "The pair scoring tool"};

◆ m_tdt

ToolHandle<Trig::TrigDecisionTool> DerivationFramework::TriggerMatchingTool::m_tdt {"Trig::TrigDecisionTool/TrigDecisionTool"}
private

The trig decision tool.

Definition at line 90 of file TriggerMatchingTool.h.

90{"Trig::TrigDecisionTool/TrigDecisionTool"};

◆ m_trigParticleTool

ToolHandle<Trig::IIParticleRetrievalTool> DerivationFramework::TriggerMatchingTool::m_trigParticleTool {"Trig::IParticleRetrievalTool/OnlineParticleTool"}
private

The tool to retrieve the online candidates.

Definition at line 67 of file TriggerMatchingTool.h.

67{"Trig::IParticleRetrievalTool/OnlineParticleTool"};

The documentation for this class was generated from the following files: