ATLAS Offline Software
TrigTauMonitorTandPAlgorithm.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 
7 
8 TrigTauMonitorTandPAlgorithm::TrigTauMonitorTandPAlgorithm(const std::string& name, ISvcLocator* pSvcLocator)
9  : TrigTauMonitorBaseAlgorithm(name, pSvcLocator)
10 {}
11 
12 
15 
17  ATH_CHECK( m_hltMuonKey.initialize() );
18 
20  ATH_CHECK( m_offlineMuonKey.initialize() );
21 
22  return StatusCode::SUCCESS;
23 }
24 
25 
26 std::vector<const xAOD::Electron*> TrigTauMonitorTandPAlgorithm::getOfflineElectrons(const EventContext& ctx, const float threshold) const
27 {
28  std::vector<const xAOD::Electron*> el_vec;
29 
31  if(!electrons.isValid()) {
32  ATH_MSG_WARNING("Failed to retrieve offline Electrons");
33  return el_vec;
34  }
35 
36  for(const xAOD::Electron* el : *electrons) {
37  // Threshold selection
38  if(el->p4().Pt()/Gaudi::Units::GeV < threshold) continue;
39 
40  // Select offline electrons passing good quality cuts
41  if(!(el->passSelection("LHMedium") && el->isGoodOQ(xAOD::EgammaParameters::BADCLUSELECTRON))) continue;
42 
43  el_vec.push_back(el);
44  }
45 
46  return el_vec;
47 }
48 
49 
50 std::vector<const xAOD::Muon*> TrigTauMonitorTandPAlgorithm::getOfflineMuons(const EventContext& ctx, const float threshold) const
51 {
52  std::vector<const xAOD::Muon*> mu_vec;
53 
55  if(!muons.isValid()) {
56  ATH_MSG_WARNING("Failed to retrieve offline Muons");
57  return mu_vec;
58  }
59 
60  for(const xAOD::Muon* mu : *muons) {
61  // Threshold selection
62  if(mu->p4().Pt()/Gaudi::Units::GeV < threshold) continue;
63 
64  // Select offline muons passing good quality cuts (quality >= Medium, but the Enum indexes are reversed...)
65  if(!(mu->quality() <= xAOD::Muon::Medium && mu->passesIDCuts())) continue;
66 
67  mu_vec.push_back(mu);
68  }
69 
70  return mu_vec;
71 }
72 
73 
74 std::vector<const xAOD::Electron*> TrigTauMonitorTandPAlgorithm::getOnlineElectrons(const std::string& trigger) const
75 {
76  std::vector<const xAOD::Electron*> el_vec;
77 
78  auto vec = m_trigDecTool->features<xAOD::ElectronContainer>(trigger, TrigDefs::Physics, m_hltElectronKey.key());
79  for(auto &featLinkInfo : vec) {
80  const auto *feat = *(featLinkInfo.link);
81  if(!feat) continue;
82  el_vec.push_back(feat);
83  }
84 
85  return el_vec;
86 }
87 
88 
89 std::vector<const xAOD::Muon*> TrigTauMonitorTandPAlgorithm::getOnlineMuons(const std::string& trigger) const
90 {
91  std::vector<const xAOD::Muon*> mu_vec;
92 
93  auto vec = m_trigDecTool->features<xAOD::MuonContainer>(trigger, TrigDefs::Physics, m_hltMuonKey.key());
94  for(auto &featLinkInfo : vec) {
95  const auto *feat = *(featLinkInfo.link);
96  if(!feat) continue;
97  mu_vec.push_back(feat);
98  }
99 
100  return mu_vec;
101 }
102 
103 
105 {
106  constexpr float threshold_offset = 10.0;
107 
108  // Offline taus
109  auto offline_taus_all = getOfflineTausAll(ctx, 0.0);
110 
111  if(m_requireOfflineTaus && offline_taus_all.empty()) return StatusCode::SUCCESS;
112 
113  for(const std::string& trigger : m_triggers) {
114  const TrigTauInfo& info = getTrigInfo(trigger);
115 
116  if(!info.isHLTTandP()) {
117  ATH_MSG_WARNING("Chain \"" << trigger << "\" is not a Tag and Probe trigger. Skipping...");
118  continue;
119  }
120 
121  const auto passBits = m_trigDecTool->isPassedBits(trigger);
122  const bool l1_accept_flag = passBits & TrigDefs::L1_isPassedAfterVeto;
123  const bool hlt_not_prescaled_flag = (passBits & TrigDefs::EF_prescaled) == 0;
124 
125  // Filter offline taus
126  std::vector<const xAOD::TauJet*> offline_taus = classifyTausAll(offline_taus_all, info.getHLTTauThreshold() - threshold_offset);
127 
128  // Online taus
129  std::vector<const xAOD::TauJet*> hlt_taus = getOnlineTausAll(trigger, true);
130 
131  if(info.hasHLTElectronLeg()) { // Electron channel
132  // Offline Electrons
133  std::vector<const xAOD::IParticle*> offline_electrons;
134  for(const xAOD::Electron* p : getOfflineElectrons(ctx, info.getHLTElecThreshold()+1)) offline_electrons.push_back(dynamic_cast<const xAOD::IParticle*>(p));
135 
136  // Overlap removal: dR(Offline Tau, Offline Electron) > 0.2
137  for(int i = offline_taus.size()-1; i >= 0; i--) {
138  bool is_match = matchObjects(offline_taus.at(i), offline_electrons, 0.2);
139  if(is_match) offline_taus.erase(offline_taus.begin() + i);
140  }
141 
142  // Online Electrons
143  std::vector<const xAOD::IParticle*> hlt_electrons;
144  for(const xAOD::Electron* p : getOnlineElectrons(trigger)) hlt_electrons.push_back(dynamic_cast<const xAOD::IParticle*>(p));
145 
146  if(m_do_variable_plots) fillTagAndProbeVars(trigger, hlt_taus, hlt_electrons);
147  if(m_do_efficiency_plots && l1_accept_flag && hlt_not_prescaled_flag) fillTAndPHLTEfficiencies(ctx, trigger, offline_electrons, hlt_electrons, offline_taus, hlt_taus);
148 
149  } else if(info.hasHLTMuonLeg()) { // Muon channel
150  // Offline Muons
151  std::vector<const xAOD::IParticle*> offline_muons;
152  for(const xAOD::Muon* p : getOfflineMuons(ctx, info.getHLTMuonThreshold()+1)) offline_muons.push_back(dynamic_cast<const xAOD::IParticle*>(p));
153 
154  // Overlap removal: dR(Offline Tau, Offline Tau) > 0.2
155  for(int i = offline_taus.size()-1; i >= 0; i--) {
156  bool is_match = matchObjects(offline_taus.at(i), offline_muons, 0.2);
157  if(is_match) offline_taus.erase(offline_taus.begin() + i);
158  }
159 
160  // Online Muons
161  std::vector<const xAOD::IParticle*> hlt_muons;
162  for(const xAOD::Muon* p : getOnlineMuons(trigger)) hlt_muons.push_back(dynamic_cast<const xAOD::IParticle*>(p));
163 
164  if(m_do_variable_plots) fillTagAndProbeVars(trigger, hlt_taus, hlt_muons);
165  if(m_do_efficiency_plots && l1_accept_flag && hlt_not_prescaled_flag) fillTAndPHLTEfficiencies(ctx, trigger, offline_muons, hlt_muons, offline_taus, hlt_taus);
166  }
167  }
168 
169  return StatusCode::SUCCESS;
170 }
171 
172 
173 void TrigTauMonitorTandPAlgorithm::fillTAndPHLTEfficiencies(const EventContext& ctx, const std::string& trigger, const std::vector<const xAOD::IParticle*>& offline_lep_vec, const std::vector<const xAOD::IParticle*>& online_lep_vec, const std::vector<const xAOD::TauJet*>& offline_tau_vec, const std::vector<const xAOD::TauJet*>& online_tau_vec) const
174 {
175  ATH_MSG_DEBUG("Fill Tag and Probe HLT efficiencies: " << trigger);
176 
177  // Require 1 offline taus and 1 online taus
178  if(online_tau_vec.size() != 1 || offline_tau_vec.size() != 1) return;
179  // ...and require 1 offline lepton and 1 online lepton
180  if(online_lep_vec.size() != 1 || offline_lep_vec.size() != 1) return;
181 
182  auto monGroup = getGroup(trigger+"_TAndPHLT_Efficiency");
183 
184  auto tauPt = Monitored::Scalar<float>("tauPt", 0.0);
185  auto tauEta = Monitored::Scalar<float>("tauEta", 0.0);
186  auto tauPhi = Monitored::Scalar<float>("tauPhi", 0.0);
187  auto dR = Monitored::Scalar<float>("dR", 0.0);
188  auto dEta = Monitored::Scalar<float>("dEta", 0.0);
189  auto dPhi = Monitored::Scalar<float>("dPhi", 0.0);
190  auto averageMu = Monitored::Scalar<float>("averageMu", 0.0);
191  auto HLT_match = Monitored::Scalar<bool>("HLT_pass", false);
192  auto HLT_match_highPt = Monitored::Scalar<bool>("HLT_pass_highPt", false);
193 
194  // efficiency denominator : 1 offline tau, 1 online tau, 1 offline lepton, 1 online lepton
195  // efficiency numerator : hlt fires + offline and online tau matched + offline and online lepton matched
196  bool hlt_fires = m_trigDecTool->isPassed(trigger, TrigDefs::Physics);
197  bool tau1_match = matchObjects(offline_tau_vec[0], online_tau_vec, 0.2);
198  bool lep1_match = matchObjects(offline_lep_vec[0], online_lep_vec, 0.2);
199 
200  tauPt = offline_tau_vec[0]->pt()/Gaudi::Units::GeV;
201  tauEta = offline_tau_vec[0]->eta();
202  tauPhi = offline_tau_vec[0]->phi();
203  dR = offline_tau_vec[0]->p4().DeltaR(offline_lep_vec[0]->p4());
204  dEta = std::abs(offline_tau_vec[0]->eta() - offline_lep_vec[0]->eta());
205  dPhi = offline_tau_vec[0]->p4().DeltaPhi(offline_lep_vec[0]->p4());
206  averageMu = lbAverageInteractionsPerCrossing(ctx);
207  HLT_match = hlt_fires && tau1_match && lep1_match;
208 
209  fill(monGroup, tauPt, tauEta, tauPhi, dR, dEta, dPhi, averageMu, HLT_match);
210 
211  bool is_highPt = tauPt > getTrigInfo(trigger).getHLTTauThreshold() + 20.0;
212  if(is_highPt) {
213  HLT_match_highPt = static_cast<bool>(HLT_match);
214  fill(monGroup, tauEta, tauPhi, HLT_match_highPt);
215  }
216 
217 
218  ATH_MSG_DEBUG("After fill Tag and Probe HLT efficiencies: " << trigger);
219 }
220 
221 
222 void TrigTauMonitorTandPAlgorithm::fillTagAndProbeVars(const std::string& trigger, const std::vector<const xAOD::TauJet*>& tau_vec, const std::vector<const xAOD::IParticle*>& lep_vec) const
223 {
224  ATH_MSG_DEBUG("Fill Tag & Probe Variables: " << trigger);
225 
226  auto monGroup = getGroup(trigger+"_TAndPVars");
227 
228  // Require 1 tau and 1 lepton
229  if(tau_vec.empty() || lep_vec.empty()) return;
230 
231  auto dR = Monitored::Scalar<float>("dR", 0.0);
232  auto dEta = Monitored::Scalar<float>("dEta", 0.0);
233  auto dPhi = Monitored::Scalar<float>("dPhi", 0.0);
234  auto dPt = Monitored::Scalar<float>("dPt", 0.0);
235 
236  auto Pt = Monitored::Scalar<float>("Pt", 0.0);
237  auto Eta = Monitored::Scalar<float>("Eta", 0.0);
238  auto Phi = Monitored::Scalar<float>("Phi", 0.0);
239  auto M = Monitored::Scalar<float>("M", 0.0);
240 
241  // Choose a pair with dR > 0.3 to fill the plot (there must be always at least one pair with dR > 0.3 if the trigger fires)
242  uint index_tau = 0;
243  uint index_lep = 0;
244 
245  for(uint i=0; i < tau_vec.size(); i++) {
246  for(uint j=0; j< lep_vec.size(); j++) {
247  if(tau_vec[i]->p4().DeltaR(lep_vec[j]->p4()) >= 0.3) {
248  index_tau = i;
249  index_lep = j;
250  }
251  }
252  }
253 
254  dR = tau_vec[index_tau]->p4().DeltaR(lep_vec[index_lep]->p4());
255  dEta = std::abs(tau_vec[index_tau]->eta() - lep_vec[index_lep]->eta());
256  dPhi = tau_vec[index_tau]->p4().DeltaPhi(lep_vec[index_lep]->p4());
257  dPt = std::abs((tau_vec[index_tau]->pt() - lep_vec[index_lep]->pt())/Gaudi::Units::GeV);
258 
259  TLorentzVector diTau4V = tau_vec[index_tau]->p4() + lep_vec[index_lep]->p4();
260 
261  Pt = diTau4V.Pt()/Gaudi::Units::GeV;
262  Eta = diTau4V.Eta();
263  Phi = diTau4V.Phi();
264  M = diTau4V.M()/Gaudi::Units::GeV;
265 
266  fill(monGroup, dR, dEta, dPhi, dPt, Pt, Eta, Phi, M);
267 
268  ATH_MSG_DEBUG("After fill Tag & Probe variables: " << trigger);
269 }
grepfile.info
info
Definition: grepfile.py:38
TrigTauMonitorTandPAlgorithm::m_hltElectronKey
SG::ReadHandleKey< xAOD::ElectronContainer > m_hltElectronKey
Definition: TrigTauMonitorTandPAlgorithm.h:40
python.TIDAMonTool.monGroup
def monGroup(analysis_chain)
Definition: TIDAMonTool.py:297
GeV
#define GeV
Definition: PhysicsAnalysis/TauID/TauAnalysisTools/Root/HelperFunctions.cxx:17
TrigTauInfo::getHLTTauThreshold
float getHLTTauThreshold() const
Definition: TrigTauInfo.h:26
TauAnalysisTools::tauEta
double tauEta(const xAOD::TauJet &xTau)
return tau eta
Definition: PhysicsAnalysis/TauID/TauAnalysisTools/Root/HelperFunctions.cxx:99
AthMonitorAlgorithm::m_trigDecTool
PublicToolHandle< Trig::TrigDecisionTool > m_trigDecTool
Tool to tell whether a specific trigger is passed.
Definition: AthMonitorAlgorithm.h:340
TrigTauMonitorBaseAlgorithm::initialize
virtual StatusCode initialize() override
initialize
Definition: TrigTauMonitorBaseAlgorithm.cxx:16
TauAnalysisTools::tauPt
double tauPt(const xAOD::TauJet &xTau)
return tau pt in GeV
Definition: PhysicsAnalysis/TauID/TauAnalysisTools/Root/HelperFunctions.cxx:85
eta
Scalar eta() const
pseudorapidity method
Definition: AmgMatrixBasePlugin.h:83
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
met::DeltaR
@ DeltaR
Definition: METRecoCommon.h:11
TrigTauMonitorTandPAlgorithm::fillTagAndProbeVars
void fillTagAndProbeVars(const std::string &trigger, const std::vector< const xAOD::TauJet * > &tau_vec, const std::vector< const xAOD::IParticle * > &lep_vec) const
Definition: TrigTauMonitorTandPAlgorithm.cxx:222
TrigTauMonitorTandPAlgorithm::getOfflineElectrons
std::vector< const xAOD::Electron * > getOfflineElectrons(const EventContext &ctx, const float threshold=0.0) const
Definition: TrigTauMonitorTandPAlgorithm.cxx:26
test_pyathena.pt
pt
Definition: test_pyathena.py:11
xAOD::passBits
passBits
Definition: TrigPassBits_v1.cxx:115
TrigTauMonitorBaseAlgorithm::classifyTausAll
std::vector< const xAOD::TauJet * > classifyTausAll(const std::vector< const xAOD::TauJet * > &taus, const float threshold=0.0) const
Definition: TrigTauMonitorBaseAlgorithm.cxx:304
Phi
@ Phi
Definition: RPCdef.h:8
xAOD::EgammaParameters::BADCLUSELECTRON
const uint32_t BADCLUSELECTRON
Definition: EgammaDefs.h:116
vec
std::vector< size_t > vec
Definition: CombinationsGeneratorTest.cxx:12
SG::VarHandleKey::key
const std::string & key() const
Return the StoreGate ID for the referenced object.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:141
xAOD::IParticle
Class providing the definition of the 4-vector interface.
Definition: Event/xAOD/xAODBase/xAODBase/IParticle.h:41
xAOD::Muon_v1
Class describing a Muon.
Definition: Muon_v1.h:38
TrigTauMonitorBaseAlgorithm::m_do_variable_plots
Gaudi::Property< bool > m_do_variable_plots
Definition: TrigTauMonitorBaseAlgorithm.h:41
TrigTauMonitorTandPAlgorithm::processEvent
virtual StatusCode processEvent(const EventContext &ctx) const override
Definition: TrigTauMonitorTandPAlgorithm.cxx:104
uint
unsigned int uint
Definition: LArOFPhaseFill.cxx:20
TrigTauMonitorBaseAlgorithm::getOnlineTausAll
std::vector< const xAOD::TauJet * > getOnlineTausAll(const std::string &trigger, bool include_0P=true) const
Definition: TrigTauMonitorBaseAlgorithm.cxx:55
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
lumiFormat.i
int i
Definition: lumiFormat.py:85
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
TauGNNUtils::Variables::Track::dPhi
bool dPhi(const xAOD::TauJet &tau, const xAOD::TauTrack &track, double &out)
Definition: TauGNNUtils.cxx:538
TrigTauMonitorTandPAlgorithm::m_offlineMuonKey
SG::ReadHandleKey< xAOD::MuonContainer > m_offlineMuonKey
Definition: TrigTauMonitorTandPAlgorithm.h:38
TrigTauMonitorTandPAlgorithm::getOnlineElectrons
std::vector< const xAOD::Electron * > getOnlineElectrons(const std::string &trigger) const
Definition: TrigTauMonitorTandPAlgorithm.cxx:74
TrigTauMonitorTandPAlgorithm::m_requireOfflineTaus
Gaudi::Property< bool > m_requireOfflineTaus
Definition: TrigTauMonitorTandPAlgorithm.h:17
plotIsoValidation.el
el
Definition: plotIsoValidation.py:197
TrigTauMonitorTandPAlgorithm::m_offlineElectronKey
SG::ReadHandleKey< xAOD::ElectronContainer > m_offlineElectronKey
Definition: TrigTauMonitorTandPAlgorithm.h:37
TrigTauMonitorTandPAlgorithm::fillTAndPHLTEfficiencies
void fillTAndPHLTEfficiencies(const EventContext &ctx, const std::string &trigger, const std::vector< const xAOD::IParticle * > &offline_lep_vec, const std::vector< const xAOD::IParticle * > &online_lep_vec, const std::vector< const xAOD::TauJet * > &offline_tau_vec, const std::vector< const xAOD::TauJet * > &online_tau_vec) const
Definition: TrigTauMonitorTandPAlgorithm.cxx:173
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
AthMonitorAlgorithm::fill
void fill(const ToolHandle< GenericMonitoringTool > &groupHandle, std::vector< std::reference_wrapper< Monitored::IMonitoredVariable >> &&variables) const
Fills a vector of variables to a group by reference.
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
DataVector
Derived DataVector<T>.
Definition: DataVector.h:581
TrigTauMonitorTandPAlgorithm::TrigTauMonitorTandPAlgorithm
TrigTauMonitorTandPAlgorithm(const std::string &name, ISvcLocator *pSvcLocator)
Definition: TrigTauMonitorTandPAlgorithm.cxx:8
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
TrigTauMonitorBaseAlgorithm::dR
double dR(const double eta1, const double phi1, const double eta2, const double phi2) const
Definition: TrigTauMonitorBaseAlgorithm.h:65
TrigTauMonitorTandPAlgorithm::getOfflineMuons
std::vector< const xAOD::Muon * > getOfflineMuons(const EventContext &ctx, const float threshold=0.0) const
Definition: TrigTauMonitorTandPAlgorithm.cxx:50
TrigTauMonitorTandPAlgorithm::m_hltMuonKey
SG::ReadHandleKey< xAOD::MuonContainer > m_hltMuonKey
Definition: TrigTauMonitorTandPAlgorithm.h:41
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
threshold
Definition: chainparser.cxx:74
TrigTauMonitorBaseAlgorithm
Definition: TrigTauMonitorBaseAlgorithm.h:24
TrigTauMonitorBaseAlgorithm::m_do_efficiency_plots
Gaudi::Property< bool > m_do_efficiency_plots
Definition: TrigTauMonitorBaseAlgorithm.h:40
xAOD::Electron_v1
Definition: Electron_v1.h:34
AthMonitorAlgorithm::lbAverageInteractionsPerCrossing
virtual float lbAverageInteractionsPerCrossing(const EventContext &ctx=Gaudi::Hive::currentContext()) const
Calculate the average mu, i.e.
Definition: AthMonitorAlgorithm.cxx:222
TrigTauInfo
Definition: TrigTauInfo.h:15
Prompt::Def::Pt
@ Pt
Definition: VarHolder.h:76
LikeEnum::Medium
@ Medium
Definition: LikelihoodEnums.h:14
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
TrigTauMonitorBaseAlgorithm::matchObjects
bool matchObjects(const T1 *tau, const std::vector< const T2 * > &tau_vec, float threshold) const
Definition: TrigTauMonitorBaseAlgorithm.h:73
TrigTauMonitorBaseAlgorithm::m_triggers
Gaudi::Property< std::vector< std::string > > m_triggers
Definition: TrigTauMonitorBaseAlgorithm.h:32
TrigTauMonitorTandPAlgorithm::initialize
virtual StatusCode initialize() override
initialize
Definition: TrigTauMonitorTandPAlgorithm.cxx:13
TrigTauMonitorTandPAlgorithm::getOnlineMuons
std::vector< const xAOD::Muon * > getOnlineMuons(const std::string &trigger) const
Definition: TrigTauMonitorTandPAlgorithm.cxx:89
TrigTauMonitorBaseAlgorithm::getTrigInfo
const TrigTauInfo & getTrigInfo(const std::string &trigger) const
Definition: TrigTauMonitorBaseAlgorithm.h:45
Monitored::Scalar
Declare a monitored scalar variable.
Definition: MonitoredScalar.h:34
TauGNNUtils::Variables::Track::dEta
bool dEta(const xAOD::TauJet &tau, const xAOD::TauTrack &track, double &out)
Definition: TauGNNUtils.cxx:527
CaloNoise_fillDB.mu
mu
Definition: CaloNoise_fillDB.py:53
AthMonitorAlgorithm::getGroup
const ToolHandle< GenericMonitoringTool > & getGroup(const std::string &name) const
Get a specific monitoring tool from the tool handle array.
Definition: AthMonitorAlgorithm.cxx:164
InDetDD::electrons
@ electrons
Definition: InDetDD_Defs.h:17
Eta
@ Eta
Definition: RPCdef.h:8
TrigTauMonitorBaseAlgorithm::getOfflineTausAll
std::vector< const xAOD::TauJet * > getOfflineTausAll(const EventContext &ctx, const float threshold=20.0) const
Definition: TrigTauMonitorBaseAlgorithm.cxx:83
TrigTauMonitorTandPAlgorithm.h