ATLAS Offline Software
Loading...
Searching...
No Matches
MuonSelectionTool.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
5#ifndef MUONSELECTORTOOLS_MUONSELECTIONTOOL_H
6#define MUONSELECTORTOOLS_MUONSELECTIONTOOL_H
7
9#include "AsgTools/AsgTool.h"
11#include "AsgTools/ToolHandle.h"
14#include "TF1.h"
15#include "TFile.h"
16#include "TH2D.h"
17#include "TMVA/Reader.h"
18#include "TSystem.h" // Replace with PathResolver
21#include "MVAUtils/BDT.h"
22#include <string>
23#include <vector>
24#include <memory>
25#include <mutex>
26#include <cstdint>
27#include <stdexcept>
28
29namespace CP {
30
41 class MuonSelectionTool : public virtual IAsgSelectionTool, public virtual IMuonSelectionTool, public asg::AsgTool {
44
45 public:
47 MuonSelectionTool(const std::string& name = "MuonSelection");
48
50
53
55 virtual StatusCode initialize() override;
56
58
61
63 virtual const asg::AcceptInfo& getAcceptInfo() const override;
64
66 virtual asg::AcceptData accept(const xAOD::IParticle* p) const override;
67
69
72
74 virtual asg::AcceptData accept(const xAOD::Muon& mu) const override;
75
77 virtual void setPassesIDCuts(xAOD::Muon&) const override;
78
80 // void setPassesLowPtEfficiencyCuts( xAOD::Muon& mu ) const;
81
83 virtual void setQuality(xAOD::Muon& mu) const override;
84
86 virtual bool passedIDCuts(const xAOD::Muon&) const override;
87
89 virtual bool passedMuonCuts(const xAOD::Muon&) const override;
90
92 virtual bool passedIDCuts(const xAOD::TrackParticle&) const override;
93
95 virtual bool passedHighPtCuts(const xAOD::Muon&) const override;
96
99 virtual bool passedLowPtEfficiencyCuts(const xAOD::Muon&) const override;
100 virtual bool passedLowPtEfficiencyCuts(const xAOD::Muon&, xAOD::Muon::Quality thisMu_quality) const override;
101
103 virtual bool passedErrorCutCB(const xAOD::Muon&) const override;
104
106 virtual bool isBadMuon(const xAOD::Muon&) const override;
107
109 virtual xAOD::Muon::Quality getQuality(const xAOD::Muon& mu) const override;
110
112 virtual bool passedCaloTagQuality(const xAOD::Muon& mu) const override;
113
115 virtual bool passedCaloScore(const xAOD::Muon& mu) const override;
116
118 virtual int getResolutionCategory(const xAOD::Muon&) const override;
120
122 std::vector<const xAOD::MuonSegment*> getSegmentsSorted(const xAOD::Muon& mu) const;
123
125 bool isBIS78(const float eta, const float phi) const;
127 bool isBEE(const float eta, const float phi) const;
129 bool isBMG(const float eta, const float phi) const;
130
132 float qOverPsignificance(const xAOD::Muon& muon) const;
134 float rhoPrime(const xAOD::Muon& muon) const;
135
156
158 template <class P, class T, class S> inline void retrieveSummaryValue(const P& muon, T& value, const S type, bool ignoreMissing = false) const {
159 if (!muon.summaryValue(value, type) && !ignoreMissing) {
160 ATH_MSG_FATAL(__FILE__ << ":" << __LINE__ << " Failed to retrieve summary value " << type);
161 throw std::runtime_error("MuonSelectorTool summary retrieval failed");
162 }
163 }
164
166 void fillSummary(const xAOD::Muon& muon, hitSummary& summary) const;
167
169 virtual float getTightNNScore(const xAOD::Muon& muon) const override;
170
171 private:
172 bool passedLowPtEfficiencyMVACut(const xAOD::Muon&) const;
174
176 bool passTight(const xAOD::Muon& mu, float rho, float oneOverPSig) const;
177
180 bool passedBMVmimicCut(const xAOD::Muon&) const;
181
184
185 Gaudi::Property<double> m_maxEta{this, "MaxEta", 2.7, "Maximum eta range to select the muons"};
186 Gaudi::Property<int> m_quality{this, "MuQuality", 1,"Quality to select. Values correspond to 0=Tight, 1=Medium, 2=Loose, 3=VeryLoose (only for debug, not supported), 4=HighPt, 5=LowPtEfficiency"};
187 Gaudi::Property<bool> m_toroidOff{this, "ToroidOff", false, "Run the tool in Toroid off setup"};
188 Gaudi::Property<bool> m_isRun3{this, "IsRun3Geo", false, "Switch to toggle the run 2 & run 3 geometry cuts. The tool will throw an exception if the run number does not match the expectations later"};
189 Gaudi::Property<bool> m_excludeNSWFromPrecisionLayers{this, "ExcludeNSWFromPrecisionLayers", false, "Cut on the nprecisionlayers ignoring the NSW"};
190 Gaudi::Property<bool> m_recalcPrecisionLayerswNSW{this, "RecalcPrecisionLayerswNSW", true, "Recalculate nprecisionlayers with internal criteria for NSW"};
191 Gaudi::Property<bool> m_useCaloScore{this, "UseCaloScore", true, "Use CaloScore for calo-tags in the Loose working point. False will revert back to CaloMuonIDTag (not recommended in Rel.22)"};
192 Gaudi::Property<int> m_caloScoreWP{this, "CaloScoreWP", 4,"Currently there are 4 CaloScore WPs for testing; WP4 is the recommended one"};
193 Gaudi::Property<bool> m_useLRT{this, "UseLRT", false, "Enable support for LRT muons"};
194
195 // Expert development options
196 Gaudi::Property<bool> m_turnOffMomCorr{this, "TurnOffMomCorr", false};
197 Gaudi::Property<bool> m_disablePtCuts{this, "DisablePtCuts", false};
198 Gaudi::Property<bool> m_developMode{this, "ExpertDevelopMode", false};
199 Gaudi::Property<bool> m_TrtCutOff{this, "TrtCutOff", true};
200 Gaudi::Property<bool> m_SctCutOff{this, "SctCutOff", false};
201 Gaudi::Property<bool> m_PixCutOff{this, "PixCutOff", false};
202 Gaudi::Property<bool> m_SiHolesCutOff{this, "SiHolesCutOff", false};
203 Gaudi::Property<bool> m_useAllAuthors{this, "UseAllAuthors", true};
204 Gaudi::Property<bool> m_use2stationMuonsHighPt{this, "Use2stationMuonsHighPt", true, "for users of high-pT working point to choose whether to include 'safe' 2-station muons"};
205 Gaudi::Property<bool> m_useMVALowPt{this, "UseMVALowPt", false, "for users of low-pT working point to choose whether to use MVA and whether to include MuTagIMO muons. Not yet supported in release 22, crrently only for development studies."};
206 Gaudi::Property<bool> m_useSegmentTaggedLowPt{this, "UseSegmentTaggedLowPt", false, "Use MVA low-pt WP. In development phase"};
207 Gaudi::Property<bool> m_geoOnTheFly{this, "AllowSettingGeometryOnTheFly", false,"avoids crash if run2/run3 geo is wrongly set"};
208 Gaudi::Property<bool> m_doBadMuonVetoMimic{this, "DoBadMuonVetoMimic", false}; // switch to cut away the tail of very large smearing in MC to mimic the effect of the bad muon veto for 2-station muons in the high-pT selection
209 Gaudi::Property<bool> m_allowComm{this, "AcceptCommChain", true, "Accepts muons from the EMEO chain if set to true"};
210 Gaudi::Property<bool> m_useBEEBISInHighPtRun3{this, "UseBEEBISInHighPtRun3", true, "Include BEE and BIS7/8 in high-pT WP in Run3"};
211
212 Gaudi::Property<int> m_expertMode_RunNumber{this, "expertMode_RunNumber", 0, "Expert only option. Overwrites RunNumber"};
213 Gaudi::Property<unsigned long long> m_expertMode_EvtNumber{this, "expertMode_EvtNumber", 0, "Expert only option. Overwrites EventNumber"};
214
215 SG::ReadHandleKey<xAOD::EventInfo> m_eventInfo{this, "EventInfoContName", "EventInfo", "event info key"};
216
217 // MVA configs for low-pT working point. Expert use only!
218 Gaudi::Property<std::string> m_MVAreaderFile_EVEN_MuidCB{
219 this, "MVAreaderFile_EVEN_MuidCB",
220 "MuonSelectorTools/190118_PrelimLowPtMVA/LowPtMVA_Weights/BDTG_9JAN2019_MuidCB_EVEN.weights.xml"};
221 Gaudi::Property<std::string> m_MVAreaderFile_ODD_MuidCB{
222 this, "MVAreaderFile_ODD_MuidCB",
223 "MuonSelectorTools/190118_PrelimLowPtMVA/LowPtMVA_Weights/BDTG_9JAN2019_MuidCB_ODD.weights.xml"};
224
225 Gaudi::Property<std::string> m_MVAreaderFile_EVEN_MuGirl{
226 this, "MVAreaderFile_EVEN_MuGirl",
227 "MuonSelectorTools/190118_PrelimLowPtMVA/LowPtMVA_Weights/BDTG_9JAN2019_MuGirl_EVEN.weights.xml"};
228 Gaudi::Property<std::string> m_MVAreaderFile_ODD_MuGirl{
229 this, "MVAreaderFile_ODD_MuGirl",
230 "MuonSelectorTools/190118_PrelimLowPtMVA/LowPtMVA_Weights/BDTG_9JAN2019_MuGirl_ODD.weights.xml"};
231
232 Gaudi::Property<std::string> m_MVAreaderFile_MuTagIMO_etaBin1{
233 this, "MVAreaderFile_MuTagIMO_etaBin1", "dev/MuonSelectorTools/181121_MuTagIMO_BDT/BDT_NOV2021_MuTagIMO_etaBin1.weights.xml"};
234 Gaudi::Property<std::string> m_MVAreaderFile_MuTagIMO_etaBin2{
235 this, "MVAreaderFile_MuTagIMO_etaBin2", "dev/MuonSelectorTools/181121_MuTagIMO_BDT/BDT_NOV2021_MuTagIMO_etaBin2.weights.xml"};
236 Gaudi::Property<std::string> m_MVAreaderFile_MuTagIMO_etaBin3{
237 this, "MVAreaderFile_MuTagIMO_etaBin3", "dev/MuonSelectorTools/181121_MuTagIMO_BDT/BDT_NOV2021_MuTagIMO_etaBin3.weights.xml"};
238
239 // subfolder to load from the calibration db
240 Gaudi::Property<std::string> m_calibration_version{this, "CalibrationRelease", "PreRec2016_2016-04-13"};
241
242 // possible override for the calibration version
243 Gaudi::Property<std::string> m_custom_dir{this, "CustomInputFolder", ""};
244
246 Gaudi::Property<std::string> m_BMVcutFile{this, "BMVcutFile",
247 "MuonSelectorTools/180620_BMVmimicCutFunctions/BMVmimicCutFunctions.root"};
248
250 StatusCode getHist(TFile* file, const std::string& histName, std::unique_ptr<TH1>& hist) const;
251 //
252 std::unique_ptr<TH1> m_tightWP_lowPt_rhoCuts;
253 std::unique_ptr<TH1> m_tightWP_lowPt_qOverPCuts;
254 std::unique_ptr<TH1> m_tightWP_mediumPt_rhoCuts;
255 std::unique_ptr<TH1> m_tightWP_highPt_rhoCuts;
256 //
257 std::unique_ptr<TF1> m_BMVcutFunction_barrel;
258 std::unique_ptr<TF1> m_BMVcutFunction_endcap;
259
260 // Need run number (or random run number) to apply period-dependent selections.
261 // If selection depends only on data taking year, this can be specified by passing
262 // argument needOnlyCorrectYear=true, in which case the random run number decoration
263 // from the pile-up reweighting tool is not needed.
264 unsigned int getRunNumber(bool needOnlyCorrectYear = false) const;
265
266 // TMVA readers for low-pT working point
267 std::unique_ptr<TMVA::Reader> m_readerE_MUID{nullptr};
268 std::unique_ptr<TMVA::Reader> m_readerO_MUID{nullptr};
269 std::unique_ptr<TMVA::Reader> m_readerE_MUGIRL{nullptr};
270 std::unique_ptr<TMVA::Reader> m_readerO_MUGIRL{nullptr};
271
272 std::unique_ptr<TMVA::Reader> m_reader_MUTAGIMO_etaBin1{nullptr};
273 std::unique_ptr<TMVA::Reader> m_reader_MUTAGIMO_etaBin2{nullptr};
274 std::unique_ptr<TMVA::Reader> m_reader_MUTAGIMO_etaBin3{nullptr};
275
276 // MVAUtils for Run-3 low-pT working point
277 std::unique_ptr<MVAUtils::BDT> m_MuidCO{nullptr};
278 std::vector<double> m_lowPtMuidCO_means;
279 std::vector<double> m_lowPtMuidCO_scaler;
280 std::unique_ptr<MVAUtils::BDT> m_MuGirl{nullptr};
281 std::vector<double> m_lowPtMuGirl_means;
282 std::vector<double> m_lowPtMuGirl_scaler;
283
284 // variables for the TMVA readers
285 mutable std::mutex m_low_pt_mva_mutex;
286
287 void IdMsPt(const xAOD::Muon& muon, float& idPt, float& msPt) const;
288
289 void checkSanity() const;
290
291 bool isRun3(bool forceOnTheFly = false) const {
292 if (!m_geoOnTheFly && !forceOnTheFly) return m_isRun3;
293 int rn = getRunNumber(true);
294 return rn>=399999;
295 }
296
297 void retrieveParam(const xAOD::Muon& muon, float& value, const xAOD::Muon::ParamDef param) const;
298
299 //ONNX
300 Gaudi::Property<bool> m_calculateTightNNScore{this, "CalculateTightNNScore", false};
301 ToolHandle< AthInfer::IAthInferenceTool > m_onnxTool{this, "ORTInferenceTool", "AthOnnx::OnnxRuntimeInferenceTool"};
302
303 }; // class MuonSelectionTool
304
305} // namespace CP
306
307#endif // CPTOOLTESTS_MUONSELECTIONTOOL_H
Scalar eta() const
pseudorapidity method
#define ASG_TOOL_CLASS2(CLASSNAME, INT1, INT2)
#define ATH_MSG_FATAL(x)
Property holding a SG store/key/clid from which a ReadHandle is made.
static Double_t P(Double_t *tt, Double_t *par)
Interface for (a) muon selector tool(s)
Gaudi::Property< bool > m_developMode
float rhoPrime(const xAOD::Muon &muon) const
Returns rhoPrime of the muon (see definition in https://cds.cern.ch/record/2665711 )
virtual bool passedHighPtCuts(const xAOD::Muon &) const override
Returns true if the muon passes the standard MCP High Pt cuts.
float qOverPsignificance(const xAOD::Muon &muon) const
Returns q/p significance of the muon (see definition in https://cds.cern.ch/record/2665711 )
std::unique_ptr< TH1 > m_tightWP_mediumPt_rhoCuts
Gaudi::Property< bool > m_SctCutOff
Gaudi::Property< bool > m_PixCutOff
virtual xAOD::Muon::Quality getQuality(const xAOD::Muon &mu) const override
Returns the quality of the muon. To set the value on the muon, instead call setQuality(xAOD::Muon&) c...
std::unique_ptr< MVAUtils::BDT > m_MuidCO
virtual void setQuality(xAOD::Muon &mu) const override
set the passes low pT cuts variable of the muon
std::vector< double > m_lowPtMuidCO_scaler
std::unique_ptr< TMVA::Reader > m_readerE_MUID
std::unique_ptr< TMVA::Reader > m_reader_MUTAGIMO_etaBin2
Gaudi::Property< bool > m_isRun3
std::unique_ptr< TMVA::Reader > m_readerO_MUGIRL
std::unique_ptr< TH1 > m_tightWP_highPt_rhoCuts
std::unique_ptr< TH1 > m_tightWP_lowPt_qOverPCuts
Gaudi::Property< double > m_maxEta
bool passedBMVmimicCut(const xAOD::Muon &) const
Returns true if the muon passes a cut which mimics the effect of the combined error cut This is neces...
virtual bool isBadMuon(const xAOD::Muon &) const override
Returns true if a CB muon fails some loose quaility requirements designed to remove pathological trac...
virtual ~MuonSelectionTool()
virtual bool passedCaloTagQuality(const xAOD::Muon &mu) const override
Returns true if the muon passed additional calo-tag quality cuts.
std::vector< double > m_lowPtMuidCO_means
Gaudi::Property< bool > m_SiHolesCutOff
bool isRun3(bool forceOnTheFly=false) const
Gaudi::Property< bool > m_toroidOff
Gaudi::Property< bool > m_geoOnTheFly
void fillSummary(const xAOD::Muon &muon, hitSummary &summary) const
functions that fills a hitSummary for a muon
Gaudi::Property< bool > m_doBadMuonVetoMimic
Gaudi::Property< bool > m_useLRT
Gaudi::Property< std::string > m_MVAreaderFile_ODD_MuidCB
Gaudi::Property< bool > m_excludeNSWFromPrecisionLayers
Gaudi::Property< std::string > m_MVAreaderFile_MuTagIMO_etaBin2
virtual int getResolutionCategory(const xAOD::Muon &) const override
Returns an integer corresponding to categorization of muons with different resolutions.
std::vector< double > m_lowPtMuGirl_scaler
bool passedLowPtEfficiencyMVACutRun3(const xAOD::Muon &) const
Gaudi::Property< bool > m_useBEEBISInHighPtRun3
virtual bool passedLowPtEfficiencyCuts(const xAOD::Muon &) const override
Returns true if the muon passes the standard MCP low pt cuts.
Gaudi::Property< std::string > m_calibration_version
Gaudi::Property< bool > m_useCaloScore
Gaudi::Property< bool > m_recalcPrecisionLayerswNSW
Gaudi::Property< bool > m_turnOffMomCorr
void retrieveParam(const xAOD::Muon &muon, float &value, const xAOD::Muon::ParamDef param) const
std::unique_ptr< TF1 > m_BMVcutFunction_endcap
std::unique_ptr< TH1 > m_tightWP_lowPt_rhoCuts
Gaudi::Property< bool > m_calculateTightNNScore
Gaudi::Property< int > m_caloScoreWP
Gaudi::Property< std::string > m_MVAreaderFile_ODD_MuGirl
Gaudi::Property< std::string > m_MVAreaderFile_MuTagIMO_etaBin3
virtual bool passedCaloScore(const xAOD::Muon &mu) const override
Returns true if the muon passed the CaloScore calo-tag working point.
asg::AcceptInfo m_acceptInfo
Store selection information.
virtual StatusCode initialize() override
Function initialising the tool.
virtual void setPassesIDCuts(xAOD::Muon &) const override
set the passes ID cuts variable of the muon
Gaudi::Property< unsigned long long > m_expertMode_EvtNumber
std::vector< const xAOD::MuonSegment * > getSegmentsSorted(const xAOD::Muon &mu) const
Returns a vector of the muon's segments, sorted according to chamber index.
Gaudi::Property< int > m_quality
Gaudi::Property< std::string > m_BMVcutFile
file for bad muon veto mimic cut functions
virtual bool passedIDCuts(const xAOD::TrackParticle &) const override
Returns true if the track particle passes the standard MCP ID cuts.
virtual float getTightNNScore(const xAOD::Muon &muon) const override
Returns the TightNNscore of the muon, an experimental ML-based score for the identification of muons ...
Gaudi::Property< bool > m_use2stationMuonsHighPt
virtual bool passedIDCuts(const xAOD::Muon &) const override
Returns true if the muon passes the standard MCP ID cuts.
Gaudi::Property< bool > m_disablePtCuts
unsigned int getRunNumber(bool needOnlyCorrectYear=false) const
bool isBMG(const float eta, const float phi) const
Check if muon eta/phi falls in BMG chambers.
virtual asg::AcceptData accept(const xAOD::IParticle *p) const override
Get the decision using a generic IParticle pointer.
virtual const asg::AcceptInfo & getAcceptInfo() const override
Get an object describing the "selection steps" of the tool.
Gaudi::Property< std::string > m_MVAreaderFile_EVEN_MuidCB
std::unique_ptr< TF1 > m_BMVcutFunction_barrel
std::unique_ptr< TMVA::Reader > m_readerE_MUGIRL
void IdMsPt(const xAOD::Muon &muon, float &idPt, float &msPt) const
bool passTight(const xAOD::Muon &mu, float rho, float oneOverPSig) const
Returns true if the muon passed the tight working point cuts.
std::unique_ptr< TMVA::Reader > m_readerO_MUID
Gaudi::Property< std::string > m_MVAreaderFile_EVEN_MuGirl
Gaudi::Property< bool > m_TrtCutOff
ToolHandle< AthInfer::IAthInferenceTool > m_onnxTool
Gaudi::Property< bool > m_useSegmentTaggedLowPt
virtual bool passedErrorCutCB(const xAOD::Muon &) const override
Returns true if a CB muon passes a pt- and eta-dependent cut on the relative CB q/p error.
virtual bool passedMuonCuts(const xAOD::Muon &) const override
Returns true if the muon passes a standardized loose preselection.
StatusCode getHist(TFile *file, const std::string &histName, std::unique_ptr< TH1 > &hist) const
Checks for each histogram.
std::unique_ptr< TMVA::Reader > m_reader_MUTAGIMO_etaBin1
Gaudi::Property< bool > m_useAllAuthors
std::vector< double > m_lowPtMuGirl_means
Gaudi::Property< int > m_expertMode_RunNumber
std::unique_ptr< TMVA::Reader > m_reader_MUTAGIMO_etaBin3
bool isBIS78(const float eta, const float phi) const
Check if muon eta/phi falls in BIS7/8 chambers.
bool passedLowPtEfficiencyMVACut(const xAOD::Muon &) const
void retrieveSummaryValue(const P &muon, T &value, const S type, bool ignoreMissing=false) const
helper function to retrieve a hitSummary value
SG::ReadHandleKey< xAOD::EventInfo > m_eventInfo
MuonSelectionTool(const std::string &name="MuonSelection")
Create a proper constructor for Athena.
std::unique_ptr< MVAUtils::BDT > m_MuGirl
Gaudi::Property< bool > m_useMVALowPt
Gaudi::Property< std::string > m_custom_dir
Gaudi::Property< std::string > m_MVAreaderFile_MuTagIMO_etaBin1
Gaudi::Property< bool > m_allowComm
bool isBEE(const float eta, const float phi) const
Check if muon eta/phi falls in BEE chambers.
Property holding a SG store/key/clid from which a ReadHandle is made.
Base class for the dual-use tool implementation classes.
Definition AsgTool.h:47
Class providing the definition of the 4-vector interface.
Select isolated Photons, Electrons and Muons.
TrackParticle_v1 TrackParticle
Reference the current persistent version:
Muon_v1 Muon
Reference the current persistent version:
struct to handle easily number of hits in different parts of the MS
TFile * file