ATLAS Offline Software
Loading...
Searching...
No Matches
DiTauSelectionTool.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
5// Local include(s):
8
9// Framework include(s):
11
12// ROOT include(s)
13#include "TEnv.h"
14#include "THashList.h"
15
16// System include(s)
17#include <cmath> // for std::isnan
18
19using namespace TauAnalysisTools;
20
21//=================================PUBLIC-PART==================================
22//______________________________________________________________________________
23DiTauSelectionTool::DiTauSelectionTool( const std::string& name )
24 : asg::AsgMetadataTool( name )
25 , m_sOmniIDWP("OMNIIDNONE")
26 , m_fOutFile(nullptr)
27 , m_aAccept( "DiTauSelection" )
28{}
29
30//______________________________________________________________________________
35
36//______________________________________________________________________________
38{
39
42
43 bool bConfigViaConfigFile = !m_sConfigPath.empty();
44 bool bConfigViaProperties = false;
45 if (!bConfigViaProperties and !std::isnan(m_dPtMin.value())) bConfigViaProperties = true;
46 if (!bConfigViaProperties and !m_vAbsEtaRegion.empty()) bConfigViaProperties = true;
47 if (!bConfigViaProperties and !std::isnan(m_dAbsEtaMin.value())) bConfigViaProperties = true;
48 if (!bConfigViaProperties and !std::isnan(m_dAbsEtaMax.value())) bConfigViaProperties = true;
49 if (!bConfigViaProperties and !std::isnan(m_dNSubjetsMin.value())) bConfigViaProperties = true;
50 if (!bConfigViaProperties and !m_vAbsCharges.empty()) bConfigViaProperties = true;
51 if (!bConfigViaProperties and !std::isnan(m_iAbsCharge.value())) bConfigViaProperties = true;
52 if (!bConfigViaProperties and !std::isnan(m_dOmniScoreMin.value())) bConfigViaProperties = true;
53 if (!bConfigViaProperties and m_iOmniIDWP != 0) bConfigViaProperties = true;
54
55 if (bConfigViaConfigFile and bConfigViaProperties)
56 {
57 ATH_MSG_ERROR("Configured tool via setProperty and configuration file, which may lead to unexpected configuration. Please setup the DiTauSelectionTool using only one of the two methods. For further details please refer to the documentation https://gitlab.cern.ch/atlas/athena/blob/main/PhysicsAnalysis/TauID/TauAnalysisTools/doc/README-DiTauSelectionTool.rst or contact the TauCP group.");
58 return StatusCode::FAILURE;
59
60 }
61 if (!bConfigViaConfigFile and !bConfigViaProperties)
62 {
63 ATH_MSG_WARNING("No cut configuration provided, the tool will not do anything. For further details please refer to the documentation:");
64 ATH_MSG_WARNING("https://gitlab.cern.ch/atlas/athena/blob/main/PhysicsAnalysis/TauID/TauAnalysisTools/doc/README-DiTauSelectionTool.rst");
65 }
66
67 if (bConfigViaConfigFile)
68 {
69 TEnv rEnv;
70 std::string sInputFilePath = PathResolverFindCalibFile(m_sConfigPath);
71
72 if (!testFileForEOFContainsCharacters(sInputFilePath))
73 ATH_MSG_WARNING("Config file for DiTauSelectionTool with path "<<sInputFilePath<<" does not contain an empty last line. The tool might not be properly configured!");
74
75 rEnv.ReadFile(sInputFilePath.c_str(),
76 kEnvAll);
77
78 std::vector<std::string> vCuts;
79 // if Cuts are specified in the config file take these ones, if not take all
80 // specified in the config
81 if (rEnv.Defined("SelectionCuts"))
82 TauAnalysisTools::split(rEnv, "SelectionCuts", ' ', vCuts);
83 else
84 {
85 auto lList = rEnv.GetTable();
86 for( Int_t i = 0; i < lList->GetEntries(); ++i )
87 {
88 vCuts.push_back( lList->At( i )->GetName() );
89 }
90 }
91
92 int iSelectionCuts = 0;
93
94 for (const std::string& sCut : vCuts)
95 {
96 if (sCut == "PtMin")
97 {
98 iSelectionCuts = iSelectionCuts | DiTauCutPt;
99 if (std::isnan(m_dPtMin.value()))
100 m_dPtMin = rEnv.GetValue("PtMin",NAN);
101 }
102 else if (sCut == "AbsEtaRegion")
103 {
104 iSelectionCuts = iSelectionCuts | DiTauCutAbsEta;
105 if (m_vAbsEtaRegion.empty())
106 TauAnalysisTools::split(rEnv,"AbsEtaRegion", ';', m_vAbsEtaRegion);
107 }
108 else if (sCut == "AbsEtaMin")
109 {
110 iSelectionCuts = iSelectionCuts | DiTauCutAbsEta;
111 if (std::isnan(m_dAbsEtaMin.value()))
112 m_dAbsEtaMin = rEnv.GetValue("AbsEtaMin",NAN);
113 }
114 else if (sCut == "AbsEtaMax")
115 {
116 iSelectionCuts = iSelectionCuts | DiTauCutAbsEta;
117 if (std::isnan(m_dAbsEtaMax.value()))
118 m_dAbsEtaMax = rEnv.GetValue("AbsEtaMax",NAN);
119 }
120 else if (sCut == "NSubjetsMin")
121 {
122 iSelectionCuts = iSelectionCuts | DiTauCutNSubjets;
123 if (std::isnan(m_dNSubjetsMin.value()))
124 m_dNSubjetsMin = rEnv.GetValue("NSubjetsMin",NAN);
125 }
126 else if (sCut == "AbsCharges")
127 {
128 iSelectionCuts = iSelectionCuts | DiTauCutAbsCharge;
129 if (m_vAbsCharges.empty())
130 TauAnalysisTools::split(rEnv,"AbsCharges", ';', m_vAbsCharges);
131 }
132 else if (sCut == "AbsCharge")
133 {
134 iSelectionCuts = iSelectionCuts | DiTauCutAbsCharge;
135 if (std::isnan(m_iAbsCharge.value()))
136 m_iAbsCharge = rEnv.GetValue("AbsCharge",NAN);
137 }
138 else if (sCut == "OmniScoreMin")
139 {
140 iSelectionCuts = iSelectionCuts | DiTauCutOmniScore;
141 if (std::isnan(m_dOmniScoreMin.value()))
142 m_dOmniScoreMin = rEnv.GetValue("OmniScoreMin",NAN);
143
144 // check for possible mis-config in DiTau selection
145 for (const std::string& checkCut : vCuts){
146 if (checkCut.find("OmniScoreRegion") != std::string::npos) {
147 ATH_MSG_ERROR("Misconfig due to OmniScoreRegion and OmniScoreMin cuts both present in the config file. Please CHECK carefully config file again and choose of the two");
148 return StatusCode::FAILURE;
149 }
150 }
151
152 // check if using OmniScore
153 m_useOmniScore = true;
154 }
155 else if (sCut == "OmniIDWP")
156 {
157 iSelectionCuts = iSelectionCuts | DiTauCutOmniIDWP;
158 if (m_iOmniIDWP == OMNIIDNONE){
159 m_sOmniIDWP = rEnv.GetValue("OmniIDWP","OMNIIDNONE");
161 }
162 // check for possible mis-config in Tau selection
163 for (const std::string& checkCut : vCuts){
164 if (checkCut.find("OmniScore") != std::string::npos) {
165 ATH_MSG_ERROR("Misconfig due to OmniIDWP and OmniScore cuts both present in the config file. Please CHECK carefully config file again");
166 return StatusCode::FAILURE;
167 }
168 }
169 }
170 else ATH_MSG_WARNING("Cut " << sCut << " is not available");
171 }
172
174 m_iSelectionCuts = iSelectionCuts;
175 }
176
177 // initialise the ReadDecorHandleKey if OmniScore is applied
178 if (m_useOmniScore) {
179 if(m_OmniScoreDecorKey.empty()) {
180 ATH_CHECK( m_OmniScoreDecorKey.assign("DiTauJets.omni_score"));
181 }
182 }
184
185 // specify all available cut descriptions
186 using map_type = std::map<DiTauSelectionCuts, std::unique_ptr<TauAnalysisTools::DiTauSelectionCut>>;
187 using pair_type = map_type::value_type;
188
189 pair_type elements[] =
190 {
191 {DiTauCutPt, std::make_unique<TauAnalysisTools::DiTauSelectionCutPt>(this)},
192 {DiTauCutAbsEta, std::make_unique<TauAnalysisTools::DiTauSelectionCutAbsEta>(this)},
193 {DiTauCutNSubjets, std::make_unique<TauAnalysisTools::DiTauSelectionCutNSubjets>(this)},
194 {DiTauCutAbsCharge, std::make_unique<TauAnalysisTools::DiTauSelectionCutAbsCharge>(this)},
195 {DiTauCutOmniScore, std::make_unique<TauAnalysisTools::DiTauSelectionCutOmniScore>(this)},
196 {DiTauCutOmniIDWP, std::make_unique<TauAnalysisTools::DiTauSelectionCutOmniIDWP>(this)},
197 };
198
199 m_cMap = { std::make_move_iterator( begin(elements) ), std::make_move_iterator( end(elements) ) };
200
201 ATH_MSG_INFO( "Initializing DiTauSelectionTool" );
207
211 PrintConfigValue ("AbsCharge", m_vAbsCharges);
213 PrintConfigValue ("OmniIDWP", m_sOmniIDWP);
214
215 std::string sCuts = "";
216 if (m_iSelectionCuts & DiTauCutPt) sCuts += "Pt ";
217 if (m_iSelectionCuts & DiTauCutAbsEta) sCuts += "AbsEta ";
218 if (m_iSelectionCuts & DiTauCutNSubjets) sCuts += "NSubjets ";
219 if (m_iSelectionCuts & DiTauCutAbsCharge) sCuts += "AbsCharge ";
220 if (m_iSelectionCuts & DiTauCutOmniScore) sCuts += "OmniScore ";
221 if (m_iSelectionCuts & DiTauCutOmniIDWP) sCuts += "OmniIDWP ";
222
223 ATH_MSG_DEBUG( "cuts: " << sCuts);
224
227
228 for ( const auto& entry : m_cMap ) {
229 if ( m_iSelectionCuts &entry.first ) {
230 entry.second->setAcceptInfo(m_aAccept);
231 }
232 }
233
234 return StatusCode::SUCCESS;
235}
236
237
238//______________________________________________________________________________
240{
241 return StatusCode::SUCCESS;
242}
243
244//______________________________________________________________________________
249
250//______________________________________________________________________________
252{
253 // Check if this is a jet:
254 if( xP->type() != xAOD::Type::Jet )
255 {
256 ATH_MSG_ERROR( "accept(...) Function received a non-jet" );
257 return asg::AcceptData (&m_aAccept);
258 }
259
260 // Cast it to a ditau:
261 const xAOD::DiTauJet* xDiTau = dynamic_cast< const xAOD::DiTauJet* >( xP );
262 if( ! xDiTau )
263 {
264 ATH_MSG_FATAL( "accept(...) Failed to cast particle to tau" );
265 return asg::AcceptData (&m_aAccept);
266 }
267
268 // Let the specific function do the work:
269 return accept( *xDiTau );
270}
271
272//______________________________________________________________________________
274{
275 asg::AcceptData acceptData (&m_aAccept);
276
277 int iNBin = 0;
278
280 {
281 // fill cutflow 'All' bin
282 m_hCutFlow->Fill(iNBin);
283 // fill main distributions before all cuts
284 for (const auto& entry : m_cMap)
285 entry.second->fillHistogramCutPre(xDiTau);
286 }
287 try
288 {
289 for (const auto& entry : m_cMap)
290 {
291 if (m_iSelectionCuts & entry.first)
292 {
293 if (!entry.second->accept(xDiTau, acceptData))
294 return acceptData;
295 else
296 {
298 {
299 // fill cutflow after each passed cut
300 iNBin++;
301 m_hCutFlow->Fill(iNBin);
302 }
303 }
304 }
305 }
306 }
307 catch (const std::runtime_error& error)
308 {
309 // LEGACY: In practical terms this should probably just throw, not
310 // print a warning/error and then continue on. However, I leave
311 // that to the experts who probably had a reason not to let the
312 // exception escape. For now I just downgraded it from error to
313 // warning and limited the number of warnings (04 Jan 22).
314 static std::atomic<uint64_t> warning_count (0u);
315 auto mycount = ++ warning_count;
316 if (mycount < 10u)
317 {
318 ATH_MSG_WARNING(error.what());
319 if (mycount == 9u)
320 ATH_MSG_WARNING ("this is your last warning");
321 }
322 }
323
324 // fill main distributions after all cuts
326 {
327 for (const auto& entry : m_cMap)
328 entry.second->fillHistogramCut(xDiTau);
329 }
330
331 // // Return the result:
332 return acceptData;
333}
334
335//______________________________________________________________________________
336void DiTauSelectionTool::setOutFile( TFile* fOutFile )
337{
338 m_fOutFile = fOutFile;
339}
340
341//______________________________________________________________________________
343{
345 ATH_MSG_WARNING("CreateControlPlots was set to true, but no valid file pointer was provided");
347 {
349 m_fOutFile->mkdir((this->name()+"_control").c_str());
350 m_fOutFile->cd((this->name()+"_control").c_str());
352 m_hCutFlow->Write();
353
354 for (const auto& entry : m_cMap)
355 entry.second->writeControlHistograms();
356 }
357}
358
359
360//=================================PRIVATE-PART=================================
362{
363 // count number of cuts
364 int iNBins = 0;
365 for (const auto& entry : m_cMap)
366 if (m_iSelectionCuts & entry.first)
367 iNBins++;
368 // create cutflow histogram with iNBins+1 bins, where first bin is 'All' bin
369 m_hCutFlow = std::make_shared<TH1F>("hCutFlow","CutFlow;; events",iNBins+1,0,iNBins+1);
370 m_hCutFlow->GetXaxis()->SetBinLabel(1,"All");
371
372 // reusing this variable to reduce overhead
373 iNBins = 2;
374 // set bin labels
375 for (const auto& entry : m_cMap)
376 if (m_iSelectionCuts & entry.first)
377 {
378 m_hCutFlow->GetXaxis()->SetBinLabel(iNBins, entry.second->getName().c_str());
379 iNBins++;
380 }
381}
382
383//______________________________________________________________________________
384template<typename T, typename U>
385void DiTauSelectionTool::FillRegionVector(std::vector<T>& vRegion, U tMin, U tMax) const
386{
387 if (!vRegion.empty())
388 return;
389 if (!std::isnan(tMin)) // if tMin is NAN, then this assumption fails and -inf is added to the vector
390 vRegion.push_back(tMin);
391 else
392 vRegion.push_back(-std::numeric_limits<T>::infinity());
393
394 if (!std::isnan(tMax)) // if tMax is NAN, then this assumption fails and inf is added to the vector
395 vRegion.push_back(tMax);
396 else
397 vRegion.push_back(std::numeric_limits<T>::infinity());
398}
399
400//______________________________________________________________________________
401template<typename T, typename U>
402void DiTauSelectionTool::FillValueVector(std::vector<T>& vRegion, U tVal) const
403{
404 if (!vRegion.empty())
405 return;
406 if (tVal == tVal) // if tMax is NAN, then this assumption fails and nothing is added to the vector
407 vRegion.push_back(tVal);
408}
409
410//______________________________________________________________________________
411template<typename T>
412void DiTauSelectionTool::PrintConfigRegion(const std::string& sCutName, std::vector<T>& vRegion) const
413{
414 unsigned int iNumRegion = vRegion.size()/2;
415 for( unsigned int iRegion = 0; iRegion < iNumRegion; iRegion++ )
416 {
417 ATH_MSG_DEBUG( sCutName<<": " << vRegion.at(iRegion*2) << " to " << vRegion.at(iRegion*2+1) );
418 }
419}
420
421//______________________________________________________________________________
422template<typename T>
423void DiTauSelectionTool::PrintConfigValue(const std::string& sCutName, std::vector<T>& vRegion) const
424{
425 for (auto tVal : vRegion)
426 ATH_MSG_DEBUG( sCutName<<": " << tVal );
427}
428
429//______________________________________________________________________________
430template<typename T>
431void DiTauSelectionTool::PrintConfigValue(const std::string& sCutName, T& tVal) const
432{
433 ATH_MSG_DEBUG( sCutName<<": " << tVal );
434}
435
436//______________________________________________________________________________
437int DiTauSelectionTool::convertStrToOmniIDWP(const std::string& sOmniIDWP) const
438{
439 if (sOmniIDWP == "OMNIIDNONE") return int(OMNIIDNONE);
440 else if (sOmniIDWP == "OMNIIDVERYLOOSE") return int(OMNIIDVERYLOOSE);
441 else if (sOmniIDWP == "OMNIIDLOOSE") return int(OMNIIDLOOSE);
442 else if (sOmniIDWP == "OMNIIDMEDIUM") return int(OMNIIDMEDIUM);
443 else if (sOmniIDWP == "OMNIIDTIGHT") return int(OMNIIDTIGHT);
444
445 ATH_MSG_ERROR( "omni ID working point "<<sOmniIDWP<<" is unknown, the OmniIDWP cut will not accept any ditau!" );
446 return -1;
447}
448
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_FATAL(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
std::string PathResolverFindCalibFile(const std::string &logical_file_name)
std::map< DiTauSelectionCuts, std::unique_ptr< TauAnalysisTools::DiTauSelectionCut > > m_cMap
Gaudi::Property< std::vector< float > > m_vecAbsEtaRegion
Gaudi::Property< bool > m_bCreateControlPlots
virtual asg::AcceptData accept(const xAOD::IParticle *p) const override
Get the decision using a generic IParticle pointer.
Gaudi::Property< std::string > m_sConfigPath
SG::ReadDecorHandleKey< xAOD::DiTauJetContainer > m_OmniScoreDecorKey
virtual StatusCode beginEvent() override
Function called when a new events is loaded.
int convertStrToOmniIDWP(const std::string &sOmniIDWP) const
Gaudi::Property< std::vector< int > > m_vecAbsCharges
void FillValueVector(std::vector< T > &vRegion, U tVal) const
void FillRegionVector(std::vector< T > &vRegion, U tMin, U tMax) const
void PrintConfigRegion(const std::string &sCutName, std::vector< T > &vRegion) const
virtual ASG_TOOL_CLASS2(DiTauSelectionTool, IAsgSelectionTool, TauAnalysisTools::IDiTauSelectionTool) public ~DiTauSelectionTool()
Create a proper constructor for Athena.
virtual const asg::AcceptInfo & getAcceptInfo() const override
Get an object describing the "selection steps" of the tool.
void PrintConfigValue(const std::string &sCutName, std::vector< T > &vRegion) const
virtual void writeControlHistograms() override
Write control histograms to output file.
asg::AcceptInfo m_aAccept
Object used to store selection information.
virtual void setOutFile(TFile *fOutFile) override
Set output file for control histograms.
virtual StatusCode initialize() override
Function initialising the tool.
Class providing the definition of the 4-vector interface.
virtual Type::ObjectType type() const =0
The type of the object as a simple enumeration.
void split(const std::string &sInput, const char cDelim, std::vector< std::string > &vOut)
bool testFileForEOFContainsCharacters(const std::string &sFileName)
returns true if last line in file is empty or the line starts with the number sign
@ Jet
The object is a jet.
Definition ObjectType.h:40
DiTauJet_v1 DiTauJet
Definition of the current version.
Definition DiTauJet.h:17