ATLAS Offline Software
TrigTauPrecisionIDHypoTool.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3 */
4 
8 #include "GaudiKernel/SystemOfUnits.h"
9 
11 
12 
13 using namespace TrigCompositeUtils;
14 
15 TrigTauPrecisionIDHypoTool::TrigTauPrecisionIDHypoTool(const std::string& type, const std::string& name, const IInterface* parent)
16  : base_class(type, name, parent),
17  m_decisionId(HLT::Identifier::fromToolName(name))
18 {
19 
20 }
21 
22 
24 {
25  ATH_MSG_DEBUG(name() << ": in initialize()");
26 
27  ATH_MSG_DEBUG("TrigTauPrecisionIDHypoTool will cut on:");
28  ATH_MSG_DEBUG(" - PtMin: " << m_ptMin.value());
29  ATH_MSG_DEBUG(" - NTracksMin: " << m_numTrackMin.value());
30  ATH_MSG_DEBUG(" - NTracksMax: " << m_numTrackMax.value());
31  ATH_MSG_DEBUG(" - NIsoTracksMax: " << m_numIsoTrackMax.value());
32  if(m_trackPtCut >= 0) ATH_MSG_DEBUG(" - trackPtCut: " << m_trackPtCut.value());
33  ATH_MSG_DEBUG(" - IDMethod: " << m_idMethod.value());
34  ATH_MSG_DEBUG(" - IDWP: " << m_idWP.value());
35  if(m_idMethod == IDMethod::Decorator) ATH_MSG_DEBUG(" - IDWPNames: " << m_idWPNames.value());
36  ATH_MSG_DEBUG(" - HighPtSelectionTrkThr: " << m_highPtTrkThr.value());
37  ATH_MSG_DEBUG(" - HighPtSelectionLooseIDThr: " << m_highPtLooseIDThr.value());
38  ATH_MSG_DEBUG(" - HighPtSelectionJetThr: " << m_highPtJetThr.value());
39 
41  ATH_MSG_ERROR("Invalid tool configuration!");
42  return StatusCode::FAILURE;
43  }
44 
45  if(!(m_idMethod == IDMethod::Disabled || m_idMethod == IDMethod::RNN || m_idMethod == IDMethod::Decorator)) {
46  ATH_MSG_ERROR("Invalid IDMethod value, " << m_idMethod.value());
47  return StatusCode::FAILURE;
48  } else if(m_idWP < IDWP::None || m_idWP > IDWP::Tight) {
49  ATH_MSG_ERROR("Invalid IDWP value, " << m_idWP.value());
50  return StatusCode::FAILURE;
51  } else if(m_idMethod == IDMethod::Disabled && m_idWP != IDWP::None) {
52  ATH_MSG_ERROR("IDMethod=0 must be set together with IDWP=-1");
53  return StatusCode::FAILURE;
54  } else if(m_idMethod == IDMethod::Decorator && m_idWPNames.size() != 4) {
55  ATH_MSG_ERROR("There need to be 4 TauID WPs passed to IDWPNames if using IDMethod=2 (Decorator)");
56  return StatusCode::FAILURE;
57  }
58 
59  // Now create the "cache" of TauID score accessors for the Monitoring...
60  ATH_MSG_DEBUG("TauID score monitoring: ");
61  for(const auto& [key, p] : m_monitoredIdScores) {
62  ATH_MSG_DEBUG(" - IDName: " << key);
63  ATH_MSG_DEBUG(" - IDScoreName: " << p.first);
64  ATH_MSG_DEBUG(" - IDScoreSigTransName: " << p.second);
65  if(p.first.empty() || p.second.empty()) {
66  ATH_MSG_ERROR("Invalid score variable names; skipping this entry for the monitoring!");
67  continue;
68  }
69 
70  m_monitoredIdAccessors.emplace(key, std::make_pair(SG::ConstAccessor<float>(p.first), SG::ConstAccessor<float>(p.second)));
71  }
72 
73  return StatusCode::SUCCESS;
74 }
75 
76 
78 {
79  ATH_MSG_DEBUG(name() << ": in execute()");
80 
81  auto NInputTaus = Monitored::Scalar<int>("NInputTaus", -1);
82  auto passedCuts = Monitored::Scalar<int>("CutCounter", 0);
83  auto PtAccepted = Monitored::Scalar<float>("PtAccepted", -1);
84  auto NTracksAccepted = Monitored::Scalar<int>("NTracksAccepted", -1);
85  auto NIsoTracksAccepted = Monitored::Scalar<int>("NIsoTracksAccepted", -1);
86 
87  std::map<std::string, Monitored::Scalar<float>> monitoredIdVariables;
88  for(const auto& [key, p] : m_monitoredIdScores) {
89  monitoredIdVariables.emplace(key + "_TauJetScoreAccepted_0p", Monitored::Scalar<float>(key + "_TauJetScoreAccepted_0p", -1));
90  monitoredIdVariables.emplace(key + "_TauJetScoreTransAccepted_0p", Monitored::Scalar<float>(key + "_TauJetScoreTransAccepted_0p", -1));
91  monitoredIdVariables.emplace(key + "_TauJetScoreAccepted_1p", Monitored::Scalar<float>(key + "_TauJetScoreAccepted_1p", -1));
92  monitoredIdVariables.emplace(key + "_TauJetScoreTransAccepted_1p", Monitored::Scalar<float>(key + "_TauJetScoreTransAccepted_1p", -1));
93  monitoredIdVariables.emplace(key + "_TauJetScoreAccepted_mp", Monitored::Scalar<float>(key + "_TauJetScoreAccepted_mp", -1));
94  monitoredIdVariables.emplace(key + "_TauJetScoreTransAccepted_mp", Monitored::Scalar<float>(key + "_TauJetScoreTransAccepted_mp", -1));
95  }
96 
97  std::vector<std::reference_wrapper<Monitored::IMonitoredVariable>> monVars = {
98  std::ref(NInputTaus), std::ref(passedCuts), std::ref(PtAccepted), std::ref(NTracksAccepted), std::ref(NIsoTracksAccepted)
99  };
100  for(auto& [key, var] : monitoredIdVariables) monVars.push_back(std::ref(var));
101  auto monitorIt = Monitored::Group(m_monTool, monVars);
102 
103 
104  // Tau pass flag
105  bool pass = false;
106 
107  if(m_acceptAll) {
108  pass = true;
109  ATH_MSG_DEBUG("AcceptAll property is set: taking all events");
110  }
111 
112  // Debugging location of the TauJet RoI
113  ATH_MSG_DEBUG("Input RoI eta: " << input.roi->eta() << ", phi: " << input.roi->phi() << ", z: " << input.roi->zed());
114 
115  const xAOD::TauJetContainer* TauContainer = input.tauContainer;
116  NInputTaus = TauContainer->size();
117  // There should only be a single TauJet in the TauJetContainer; just in case we still run the loop
118  for(const xAOD::TauJet* Tau : *TauContainer) {
119  ATH_MSG_DEBUG(" New HLT TauJet candidate:");
120 
121  float pT = Tau->pt();
122 
123  //---------------------------------------------------------
124  // Calibrated tau pT cut ('idperf' step)
125  //---------------------------------------------------------
126  ATH_MSG_DEBUG(" pT: " << pT / Gaudi::Units::GeV);
127 
128  if(!(pT > m_ptMin)) continue;
129  passedCuts++;
130  PtAccepted = pT / Gaudi::Units::GeV;
131 
132 
133  //---------------------------------------------------------
134  // Track counting ('perf' step)
135  //---------------------------------------------------------
136  int numTrack = 0, numIsoTrack = 0;
137  if(m_trackPtCut > 0.) {
138  // Raise the track pT threshold when counting tracks in the 'perf' step, to reduce sensitivity to pileup tracks
139  // Overrides the default 1 GeV cut by the InDetTrackSelectorTool used during the TauJet construction
141  if(track->pt() > m_trackPtCut) numTrack++;
142  }
144  if(track->pt() > m_trackPtCut) numIsoTrack++;
145  }
146  } else {
147  // Use the default 1 GeV selection in the InDetTrackSelectorTool, executed during the TauJet construction
148  numTrack = Tau->nTracks();
149  numIsoTrack = Tau->nTracksIsolation();
150  }
151 
152  ATH_MSG_DEBUG(" N Tracks: " << numTrack);
153  ATH_MSG_DEBUG(" N Iso Tracks: " << numIsoTrack);
154 
155  // Apply track multiplicity cuts, except for idperf
156  if(!m_acceptAll) {
157  // NTrackMin and NIsoTracksMax
158  if(pT < m_highPtTrkThr) {
159  if(numTrack < m_numTrackMin) continue;
160  if(numIsoTrack > m_numIsoTrackMax) continue;
161  }
162  // NTrackMax
163  if(pT < m_highPtJetThr) {
164  if(numTrack > m_numTrackMax) continue;
165  }
166  }
167  // Note: we disabled the track selection for high pT taus
168 
169  passedCuts++;
170  NTracksAccepted = numTrack;
171  NIsoTracksAccepted = numIsoTrack;
172 
173 
174  //---------------------------------------------------------
175  // ID WP selection (ID step)
176  //---------------------------------------------------------
177  int local_idWP = m_idWP;
178 
179  // Loosen/disable the ID WP cut for high pT taus
180  if(pT > m_highPtLooseIDThr && m_idWP > IDWP::Loose) local_idWP = IDWP::Loose; // Set ID WP to Loose
181  if(pT > m_highPtJetThr) local_idWP = IDWP::None; // Disable the ID WP cut
182 
183  ATH_MSG_DEBUG(" Local Tau ID WP: " << local_idWP);
184 
185  if(m_idMethod == IDMethod::RNN) { // RNN/DeepSet scores
186  if(!Tau->hasDiscriminant(xAOD::TauJetParameters::RNNJetScoreSigTrans)) {
187  ATH_MSG_WARNING(" RNNJetScoreSigTrans not available. Make sure the TauWPDecorator is run for the RNN Tau ID!");
188  }
189 
190  if(!m_acceptAll && local_idWP != IDWP::None) {
191  if(local_idWP == IDWP::VeryLoose && !Tau->isTau(xAOD::TauJetParameters::JetRNNSigVeryLoose)) {
192  continue;
193  } else if(local_idWP == IDWP::Loose && !Tau->isTau(xAOD::TauJetParameters::JetRNNSigLoose)) {
194  continue;
195  } else if(local_idWP == IDWP::Medium && !Tau->isTau(xAOD::TauJetParameters::JetRNNSigMedium)) {
196  continue;
197  } else if(local_idWP == IDWP::Tight && !Tau->isTau(xAOD::TauJetParameters::JetRNNSigTight)) {
198  continue;
199  }
200  }
201 
202  } else if(m_idMethod == IDMethod::Decorator) { // Decorated scores (e.g. for GNTau)
203  const static SG::ConstAccessor<char> tauid_veryloose(m_idWPNames[0]);
204  const static SG::ConstAccessor<char> tauid_loose(m_idWPNames[1]);
205  const static SG::ConstAccessor<char> tauid_medium(m_idWPNames[2]);
206  const static SG::ConstAccessor<char> tauid_tight(m_idWPNames[3]);
207 
208  if(!tauid_veryloose.isAvailable(*Tau) || !tauid_loose.isAvailable(*Tau) || !tauid_medium.isAvailable(*Tau) || !tauid_tight.isAvailable(*Tau))
209  ATH_MSG_WARNING("The TauID WP variables for the current configuration are missing! Make sure the correct inferences are included in the chain reconstruction sequence!");
210 
211  if(!m_acceptAll && local_idWP != IDWP::None) {
212  if(local_idWP == IDWP::VeryLoose && !tauid_veryloose(*Tau)) {
213  continue;
214  } else if(local_idWP == IDWP::Loose && !tauid_loose(*Tau)) {
215  continue;
216  } else if(local_idWP == IDWP::Medium && !tauid_medium(*Tau)) {
217  continue;
218  } else if(local_idWP == IDWP::Tight && !tauid_tight(*Tau)) {
219  continue;
220  }
221  }
222 
223 
224  }
225 
226  // TauID Score monitoring
227  for(const auto& [key, p] : m_monitoredIdAccessors) {
228  if(!p.first.isAvailable(*Tau))
229  ATH_MSG_WARNING("TauID Score " << m_monitoredIdScores.value().at(key).first << " is not available. Make sure the correct inferences are included in the chain reconstruction sequence!");
230 
231  if(!p.second.isAvailable(*Tau))
232  ATH_MSG_WARNING("TauID ScoreSigTrans " << m_monitoredIdScores.value().at(key).second << " is not available. Make sure the correct inferences are included in the chain reconstruction sequence!");
233 
234  ATH_MSG_DEBUG(" TauID \"" << key << "\" ScoreSigTrans: " << p.second(*Tau));
235 
236  // Monitor ID scores
237  if(Tau->nTracks() == 0) {
238  monitoredIdVariables.at(key + "_TauJetScoreAccepted_0p") = p.first(*Tau);
239  monitoredIdVariables.at(key + "_TauJetScoreTransAccepted_0p") = p.second(*Tau);
240  } else if(Tau->nTracks() == 1) {
241  monitoredIdVariables.at(key + "_TauJetScoreAccepted_1p") = p.first(*Tau);
242  monitoredIdVariables.at(key + "_TauJetScoreTransAccepted_1p") = p.second(*Tau);
243  } else { // MP tau
244  monitoredIdVariables.at(key + "_TauJetScoreAccepted_mp") = p.first(*Tau);
245  monitoredIdVariables.at(key + "_TauJetScoreTransAccepted_mp") = p.second(*Tau);
246  }
247  }
248 
249  passedCuts++;
250 
251 
252  //---------------------------------------------------------
253  // At least one Tau passed all the cuts. Accept the event!
254  //---------------------------------------------------------
255  pass = true;
256 
257  ATH_MSG_DEBUG(" Pass hypo tool: " << pass);
258  }
259 
260  return pass;
261 }
262 
263 
264 StatusCode TrigTauPrecisionIDHypoTool::decide(std::vector<ITrigTauJetHypoTool::ToolInfo>& input) const {
266  if(passed(m_decisionId.numeric(), i.previousDecisionIDs)) {
267  if(decide(i)) {
268  addDecisionID(m_decisionId, i.decision);
269  }
270  }
271  }
272 
273  return StatusCode::SUCCESS;
274 }
TrigTauPrecisionIDHypoTool::m_acceptAll
Gaudi::Property< bool > m_acceptAll
Definition: TrigTauPrecisionIDHypoTool.h:63
LikeEnum::Loose
@ Loose
Definition: LikelihoodEnums.h:12
CalculateHighPtTerm.pT
pT
Definition: ICHEP2016/CalculateHighPtTerm.py:57
beamspotnt.var
var
Definition: bin/beamspotnt.py:1393
xAOD::TauJetParameters::RNNJetScoreSigTrans
@ RNNJetScoreSigTrans
RNN score which is signal transformed/flattened.
Definition: TauDefs.h:92
GeV
#define GeV
Definition: PhysicsAnalysis/TauID/TauAnalysisTools/Root/HelperFunctions.cxx:17
TrigDefs::Group
Group
Properties of a chain group.
Definition: GroupProperties.h:13
LikeEnum::VeryLoose
@ VeryLoose
Definition: LikelihoodEnums.h:11
TrigCompositeUtils::passed
bool passed(DecisionID id, const DecisionIDContainer &idSet)
checks if required decision ID is in the set of IDs in the container
Definition: TrigCompositeUtilsRoot.cxx:118
HLT::Identifier::numeric
TrigCompositeUtils::DecisionID numeric() const
numeric ID
Definition: TrigCompositeUtils/TrigCompositeUtils/HLTIdentifier.h:41
TrigTauPrecisionIDHypoTool::initialize
virtual StatusCode initialize() override
Definition: TrigTauPrecisionIDHypoTool.cxx:23
TrigTauPrecisionIDHypoTool::m_numTrackMax
Gaudi::Property< int > m_numTrackMax
Definition: TrigTauPrecisionIDHypoTool.h:50
TrigTauPrecisionIDHypoTool::m_monitoredIdScores
Gaudi::Property< std::map< std::string, std::pair< std::string, std::string > > > m_monitoredIdScores
Definition: TrigTauPrecisionIDHypoTool.h:66
TrigCompositeUtils::addDecisionID
void addDecisionID(DecisionID id, Decision *d)
Appends the decision (given as ID) to the decision object.
Definition: TrigCompositeUtilsRoot.cxx:59
xAOD::TauJetParameters::classifiedCharged
@ classifiedCharged
Definition: TauDefs.h:406
Tau
Definition: TauDQA/src/CorePlots.cxx:8
SG::ConstAccessor< float >
TrigTauPrecisionIDHypoTool::m_monitoredIdAccessors
std::map< std::string, std::pair< SG::ConstAccessor< float >, SG::ConstAccessor< float > > > m_monitoredIdAccessors
Definition: TrigTauPrecisionIDHypoTool.h:67
TrigTauPrecisionIDHypoTool.h
TrigTauPrecisionIDHypoTool::m_highPtJetThr
Gaudi::Property< float > m_highPtJetThr
Definition: TrigTauPrecisionIDHypoTool.h:61
python.CaloAddPedShiftConfig.type
type
Definition: CaloAddPedShiftConfig.py:42
TrigTauPrecisionIDHypoTool::m_decisionId
HLT::Identifier m_decisionId
Definition: TrigTauPrecisionIDHypoTool.h:45
TrigTauPrecisionIDHypoTool::TrigTauPrecisionIDHypoTool
TrigTauPrecisionIDHypoTool(const std::string &type, const std::string &name, const IInterface *parent)
Definition: TrigTauPrecisionIDHypoTool.cxx:15
TrigCompositeUtils.h
TrigTauPrecisionIDHypoTool::m_numIsoTrackMax
Gaudi::Property< int > m_numIsoTrackMax
Definition: TrigTauPrecisionIDHypoTool.h:51
CaloCellPos2Ntuple.None
None
Definition: CaloCellPos2Ntuple.py:23
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:209
TrigTauPrecisionIDHypoTool::m_highPtTrkThr
Gaudi::Property< float > m_highPtTrkThr
Definition: TrigTauPrecisionIDHypoTool.h:59
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
TrigTauPrecisionIDHypoTool::decide
virtual StatusCode decide(std::vector< ITrigTauJetHypoTool::ToolInfo > &input) const override
Definition: TrigTauPrecisionIDHypoTool.cxx:264
HLT
It used to be useful piece of code for replacing actual SG with other store of similar functionality ...
Definition: HLTResultReader.h:26
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
xAOD::TauJet_v3
Class describing a tau jet.
Definition: TauJet_v3.h:41
test_pyathena.parent
parent
Definition: test_pyathena.py:15
xAOD::TauJetParameters::JetRNNSigVeryLoose
@ JetRNNSigVeryLoose
Definition: TauDefs.h:145
TauJetContainer.h
DataVector
Derived DataVector<T>.
Definition: DataVector.h:795
Monitored.h
Header file to be included by clients of the Monitored infrastructure.
xAOD::TauJetParameters::classifiedIsolation
@ classifiedIsolation
Definition: TauDefs.h:407
ITrigTauJetHypoTool::ToolInfo
Definition: ITrigTauJetHypoTool.h:23
LikeEnum::Tight
@ Tight
Definition: LikelihoodEnums.h:15
xAOD::TauJetParameters::JetRNNSigTight
@ JetRNNSigTight
Definition: TauDefs.h:148
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:240
TrigTauPrecisionIDHypoTool::m_monTool
ToolHandle< GenericMonitoringTool > m_monTool
Definition: TrigTauPrecisionIDHypoTool.h:65
TrigTauPrecisionIDHypoTool::m_highPtLooseIDThr
Gaudi::Property< float > m_highPtLooseIDThr
Definition: TrigTauPrecisionIDHypoTool.h:60
xAOD::TauJetParameters::JetRNNSigMedium
@ JetRNNSigMedium
Definition: TauDefs.h:147
LikeEnum::Medium
@ Medium
Definition: LikelihoodEnums.h:14
trigbs_mixBSevents.input
input
Definition: trigbs_mixBSevents.py:56
TrigTauPrecisionIDHypoTool::m_idMethod
Gaudi::Property< int > m_idMethod
Definition: TrigTauPrecisionIDHypoTool.h:54
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
ref
const boost::regex ref(r_ef)
TrigCompositeUtils
Definition: Event/xAOD/xAODTrigger/xAODTrigger/TrigComposite.h:19
SG::ConstAccessor::isAvailable
bool isAvailable(const ELT &e) const
Test to see if this variable exists in the store.
xAOD::track
@ track
Definition: TrackingPrimitives.h:513
Monitored::Scalar
Declare a monitored scalar variable.
Definition: MonitoredScalar.h:34
TrigTauPrecisionIDHypoTool::m_ptMin
Gaudi::Property< float > m_ptMin
Definition: TrigTauPrecisionIDHypoTool.h:47
TrigTauPrecisionIDHypoTool::m_trackPtCut
Gaudi::Property< float > m_trackPtCut
Definition: TrigTauPrecisionIDHypoTool.h:52
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
TrigTauPrecisionIDHypoTool::m_idWP
Gaudi::Property< int > m_idWP
Definition: TrigTauPrecisionIDHypoTool.h:55
xAOD::TauJetParameters::JetRNNSigLoose
@ JetRNNSigLoose
Definition: TauDefs.h:146
TrigTauPrecisionIDHypoTool::m_idWPNames
Gaudi::Property< std::vector< std::string > > m_idWPNames
Definition: TrigTauPrecisionIDHypoTool.h:56
TrigTauPrecisionIDHypoTool::m_numTrackMin
Gaudi::Property< int > m_numTrackMin
Definition: TrigTauPrecisionIDHypoTool.h:49
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37
Identifier
Definition: IdentifierFieldParser.cxx:14