ATLAS Offline Software
MatchingTool.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 #include "xAODBase/IParticle.h"
8 
10 
11 namespace Trig {
12 
13 MatchingTool::MatchingTool(const std::string& name) :
14  asg::AsgTool(name),
15  m_impl(new Trig::MatchingImplementation(*this)),
16  m_trigDecTool("Trig::TrigDecisionTool/TrigDecisionTool"),
17  m_matchingThreshold(0)
18 {
19  declareProperty( "TrigDecisionTool", m_trigDecTool);
20 }
21 
22 #ifndef XAOD_STANDALONE
23 void MatchingTool::updateThreshold(Gaudi::Details::PropertyBase& /*p*/) {
24  ATH_MSG_DEBUG("Matching Threshold is updated to:" << m_matchingThreshold);
26 }
27 #endif
28 
30  delete m_impl;
31 }
32 
35 
36  ATH_CHECK( m_trigDecTool.retrieve() );
37  ATH_CHECK( m_scoreTool.retrieve() );
38 
39  return StatusCode::SUCCESS;
40 }
41 
42  bool MatchingTool::matchCombination(const std::vector<const xAOD::IParticle*>& recoObjects, Trig::Combination& comb, const std::string& chain, double threshold) const {
43  std::map<xAOD::Type::ObjectType,std::vector<const xAOD::IParticle*> > typeSeparated;
44 
45  for (const auto& obj : recoObjects){
46  if(typeSeparated.find(obj->type()) == typeSeparated.end()){
47  std::vector<const xAOD::IParticle*> typevec;
48  typeSeparated[obj->type()] = typevec;
49  }
50  typeSeparated[obj->type()].push_back(obj);
51  }
52 
53  ATH_MSG_DEBUG("found: " << typeSeparated.size() << " unique objects types to match");
54  for(auto& [objType, recoObjs_thistype] : typeSeparated){
55  ATH_MSG_DEBUG("type: " << objType << "(" << recoObjs_thistype.size() << " elements)");
56  }
57 
58  bool overall_status = true;
59  std::map<xAOD::Type::ObjectType,bool> status;
60  for(auto& [objType, recoObjs_thistype] : typeSeparated){
61  auto single_status = matchSingleType(recoObjs_thistype, comb, chain, threshold);
62  ATH_MSG_DEBUG("type: " << objType << " status: " << single_status);
63  status[objType] = single_status;
64  overall_status = overall_status && single_status;
65  }
66 
67  return overall_status;
68 }
69 
70  bool MatchingTool::matchSingleType(const std::vector<const xAOD::IParticle*>& recoObjects, Trig::Combination& comb, const std::string& chain, double threshold) const {
71  ATH_MSG_DEBUG("matching combination with " << comb.tes().size() << " TEs");
72 
73  auto recoType = recoObjects.at(0)->type();
74  ATH_MSG_DEBUG("reco type is " << recoType);
75 
76  HLT::class_id_type clid = 0;
77  std::string container_typename("");
78 
79  // This hack is to make Offline electrons to be matched to online HLT CaloClusters if HLT chain is _etcut type
80  if (recoType == xAOD::Type::Electron && chain.find("etcut") != std::string::npos && chain.find("trkcut") == std::string::npos){
81  ATH_MSG_DEBUG("Electron offline and matching electron etcut chain. Try to match to cluster instead!: " );
82  recoType=xAOD::Type::CaloCluster;
83  }
84 
85  if(m_typeMap.isKnown(recoType)){
86 
87  auto clid_container = m_typeMap.get(recoType);
88 
89  clid = clid_container.first;
90  container_typename = clid_container.second;
91  ATH_MSG_DEBUG("getting trigger features (clid: " << clid << " and type: " << container_typename << ")");
92  }
93  else{
94  ATH_MSG_WARNING("could not find corresponding trigger type, can't match");
95  return false;
96  }
97 
98  auto iparticle_feats = comb.getIParticle(clid,container_typename);
99 
100  ATH_MSG_DEBUG("found: " << iparticle_feats.size() << " xAOD::IParticle");
101 
102  for(auto& feat : iparticle_feats){
103  ATH_MSG_DEBUG(" ==> pt: " << feat.cptr()->pt() << " and eta: " << feat.cptr()->eta() << " address: " << feat.cptr());
104  }
105 
106  if(recoObjects.size() > iparticle_feats.size()){
107  ATH_MSG_WARNING("more reco objects (" << recoObjects.size() << ") than trigger object (" << iparticle_feats.size() << "). no hope of matching!");
108  return false;
109  }
110 
111  ATH_MSG_DEBUG("now matching: " << recoObjects.size() << " reco objects to " << iparticle_feats.size() << " trigger objects");
112 
113  std::vector<const xAOD::IParticle*> trigObjects;
114  for(auto& feat : iparticle_feats){trigObjects.push_back(feat.cptr());}
115 
116  std::vector<std::vector<double>> scoreMatrix;
117  scoreMatrix.reserve(recoObjects.size());
118  for (const xAOD::IParticle *reco : recoObjects)
119  {
120  scoreMatrix.emplace_back();
121  std::vector<double> &scores = scoreMatrix.back();
122  scores.reserve(trigObjects.size());
123  for (const xAOD::IParticle *trig: trigObjects)
124  // For historic reasons a lot of the interfaces here expect doubles
125  // It doesn't seem worth fixing this now
126  scores.push_back(static_cast<double>(m_scoreTool->score(*trig, *reco)));
127  }
128 
129  ATH_MSG_DEBUG("made distance matrix");
130 
131  bool match_result = impl()->matchDistanceMatrix(scoreMatrix, Trig::MatchingStrategy::MinimalSum,threshold);
132 
133  ATH_MSG_DEBUG("got matching result: " << match_result);
134 
135  return match_result;
136 }
137 
138  bool MatchingTool::match(const xAOD::IParticle& recoObject, const std::string& chain, double matchThreshold, bool rerun) const {
139  std::vector<const xAOD::IParticle*> recoObjects(1,&recoObject);
140  bool out = match(recoObjects, chain, matchThreshold, rerun);
141  return out;
142 }
143 
144  bool MatchingTool::match(const std::vector<const xAOD::IParticle*>& recoObjects, const std::string& chain, double matchThreshold, bool rerun) const {
145  ATH_MSG_DEBUG("matching " << recoObjects.size() << " reco objects to chain: " << chain );
146 
147  auto chainGroup = impl()->tdt()->getChainGroup(chain);
148  bool decision;
149  if (!rerun)
150  decision = chainGroup->isPassed();
151  else
152  decision = chainGroup->isPassed(TrigDefs::Physics | TrigDefs::allowResurrectedDecision);
153  ATH_MSG_DEBUG(chain << " is passed: " << decision);
154  if(!decision) return false;
155 
156  auto featureContainer = chainGroup->features();
157  auto combinations = featureContainer.getCombinations();
158 
159  ATH_MSG_DEBUG("chain has: " << combinations.size() << " combos");
160 
161 
162  bool result = false;
163  for(auto& comb : combinations){
164  bool combResult = matchCombination(recoObjects,comb, chain, matchThreshold);
165  ATH_MSG_DEBUG("matching result for this combination: " << combResult);
166  result = result || combResult;
167  if(result) break; //no need to continue if result is true
168  }
169 
170  ATH_MSG_DEBUG("overall matching result: " << result);
171 
172  return result;
173 }
174 
176  return m_impl;
177 }
178 } //Trig namespace
179 
180 
181 
Trig::MatchingTool::m_impl
MatchingImplementation * m_impl
Definition: MatchingTool.h:49
xAOD::Electron
Electron_v1 Electron
Definition of the current "egamma version".
Definition: Event/xAOD/xAODEgamma/xAODEgamma/Electron.h:17
get_generator_info.result
result
Definition: get_generator_info.py:21
runLayerRecalibration.chain
chain
Definition: runLayerRecalibration.py:175
IParticle.h
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
CSV_InDetExporter.new
new
Definition: CSV_InDetExporter.py:145
Trig::MatchingTool::match
bool match(const std::vector< const xAOD::IParticle * > &recoObjects, const std::string &chain, double matchTreshold, bool rerun) const override
multi-object trigger matching
Definition: MatchingTool.cxx:144
asg
Definition: DataHandleTestTool.h:28
python.AthDsoLogger.out
out
Definition: AthDsoLogger.py:71
TypeMap::get
clid_string_t get(const xAOD::Type::ObjectType &recoType) const
Definition: TypeMap.cxx:21
Trig::MatchingImplementation::matchDistanceMatrix
bool matchDistanceMatrix(const std::vector< std::vector< double > > &matrix, const Trig::MatchingStrategy::Strategy strategy=Trig::MatchingStrategy::MinimalSum, double threshold=0.0) const
Definition: MatchingImplementation.cxx:35
xAOD::IParticle
Class providing the definition of the 4-vector interface.
Definition: Event/xAOD/xAODBase/xAODBase/IParticle.h:40
xAOD::CaloCluster
CaloCluster_v1 CaloCluster
Define the latest version of the calorimeter cluster class.
Definition: Event/xAOD/xAODCaloEvent/xAODCaloEvent/CaloCluster.h:19
Trig::MatchingTool::updateThreshold
void updateThreshold(Gaudi::Details::PropertyBase &p)
Definition: MatchingTool.cxx:23
Trig::MatchingImplementation
Definition: MatchingImplementation.h:33
Trig::MatchingStrategy::MinimalSum
@ MinimalSum
Definition: MatchingImplementation.h:27
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
Trig::Combination
Definition: Trigger/TrigAnalysis/TrigDecisionTool/TrigDecisionTool/Combination.h:55
Trig::MatchingTool::matchCombination
bool matchCombination(const std::vector< const xAOD::IParticle * > &recoObjects, Trig::Combination &comb, const std::string &chain, double threshold) const
Definition: MatchingTool.cxx:42
MatchingImplementation.h
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
HLT::class_id_type
uint32_t class_id_type
Definition: Trigger/TrigEvent/TrigNavStructure/Root/Types.h:11
Trig::MatchingTool::m_typeMap
TypeMap m_typeMap
Definition: MatchingTool.h:50
Trig::MatchingTool::m_scoreTool
ToolHandle< Trig::IMatchScoringTool > m_scoreTool
Definition: MatchingTool.h:52
Trig::ChainGroup::isPassed
bool isPassed(unsigned int condition=TrigDefs::Physics) const
tells if chain group passed
Definition: Trigger/TrigAnalysis/TrigDecisionTool/Root/ChainGroup.cxx:208
Trig::MatchingTool::impl
const MatchingImplementation * impl() const override
Definition: MatchingTool.cxx:175
Trig::Combination::tes
const std::vector< const HLT::TriggerElement * > & tes() const
trigger elements in the combination can be used directly with ancestor method
Definition: Trigger/TrigAnalysis/TrigDecisionTool/TrigDecisionTool/Combination.h:141
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
MatchingTool.h
threshold
Definition: chainparser.cxx:74
Trig::Combination::getIParticle
std::vector< Trig::Feature< xAOD::IParticle > > getIParticle(HLT::class_id_type clid, const std::string &container_name, const std::string &label="", unsigned int condition=TrigDefs::Physics, const std::string &teName="") const
Experimental flattened get for IParticle types.
Definition: Trigger/TrigAnalysis/TrigDecisionTool/TrigDecisionTool/Combination.h:91
Trig::MatchingImplementation::tdt
const Trig::TrigDecisionTool * tdt() const
Definition: MatchingImplementation.cxx:16
TypeMap::isKnown
bool isKnown(const xAOD::Type::ObjectType &recoType) const
Definition: TypeMap.cxx:16
python.combo.combinations
def combinations(items, n)
Definition: combo.py:85
Trig::MatchingTool::MatchingTool
MatchingTool(const std::string &name)
Definition: MatchingTool.cxx:13
Trig::MatchingTool::~MatchingTool
~MatchingTool()
Definition: MatchingTool.cxx:29
Trig::MatchingTool::m_matchingThreshold
double m_matchingThreshold
Definition: MatchingTool.h:54
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
Trig::MatchingTool::matchSingleType
bool matchSingleType(const std::vector< const xAOD::IParticle * > &subRecoObjects, Trig::Combination &comb, const std::string &chain, double threshold) const
Definition: MatchingTool.cxx:70
merge.status
status
Definition: merge.py:17
Trig::TrigDecisionToolCore::getChainGroup
const Trig::ChainGroup * getChainGroup(const std::vector< std::string > &patterns, TrigDefs::Group props=TrigDefs::Group::Default) const
Create/get chain group (.
Definition: ChainGroupFunctions.cxx:38
Trig::MatchingTool::m_trigDecTool
ToolHandle< Trig::TrigDecisionTool > m_trigDecTool
Definition: MatchingTool.h:51
python.PyAthena.obj
obj
Definition: PyAthena.py:135
Trig::MatchingTool::initialize
StatusCode initialize() override
Dummy implementation of the initialisation function.
Definition: MatchingTool.cxx:33
MuonSegmentReaderConfig.reco
reco
Definition: MuonSegmentReaderConfig.py:133
Trig::MatchingImplementation::setThreshold
void setThreshold(double in)
Definition: MatchingImplementation.h:38