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