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
40 m_vPtRegion = m_vecPtRegion.value();
45
46 bool bConfigViaConfigFile = !m_sConfigPath.empty();
47 bool bConfigViaProperties = false;
48 if (!bConfigViaProperties and !m_vPtRegion.empty()) bConfigViaProperties = true;
49 if (!bConfigViaProperties and !std::isnan(m_dPtMin.value())) bConfigViaProperties = true;
50 if (!bConfigViaProperties and !std::isnan(m_dPtMax.value())) bConfigViaProperties = true;
51 if (!bConfigViaProperties and !m_vAbsEtaRegion.empty()) bConfigViaProperties = true;
52 if (!bConfigViaProperties and !std::isnan(m_dAbsEtaMin.value())) bConfigViaProperties = true;
53 if (!bConfigViaProperties and !std::isnan(m_dAbsEtaMax.value())) bConfigViaProperties = true;
54 if (!bConfigViaProperties and !m_vNSubjetsRegion.empty()) bConfigViaProperties = true;
55 if (!bConfigViaProperties and !std::isnan(m_dNSubjetsMin.value())) bConfigViaProperties = true;
56 if (!bConfigViaProperties and !std::isnan(m_dNSubjetsMax.value())) bConfigViaProperties = true;
57 if (!bConfigViaProperties and !m_vAbsCharges.empty()) bConfigViaProperties = true;
58 if (!bConfigViaProperties and !std::isnan(m_iAbsCharge.value())) bConfigViaProperties = true;
59 if (!bConfigViaProperties and !m_vOmniScoreRegion.empty()) bConfigViaProperties = true;
60 if (!bConfigViaProperties and !std::isnan(m_dOmniScoreMin.value())) bConfigViaProperties = true;
61 if (!bConfigViaProperties and !std::isnan(m_dOmniScoreMax.value())) bConfigViaProperties = true;
62 if (!bConfigViaProperties and m_iOmniIDWP != 0) bConfigViaProperties = true;
63
64 if (bConfigViaConfigFile and bConfigViaProperties)
65 {
66 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.");
67 return StatusCode::FAILURE;
68
69 }
70 if (!bConfigViaConfigFile and !bConfigViaProperties)
71 {
72 ATH_MSG_WARNING("No cut configuration provided, the tool will not do anything. For further details please refer to the documentation:");
73 ATH_MSG_WARNING("https://gitlab.cern.ch/atlas/athena/blob/main/PhysicsAnalysis/TauID/TauAnalysisTools/doc/README-DiTauSelectionTool.rst");
74 }
75
76 if (bConfigViaConfigFile)
77 {
78 TEnv rEnv;
79 std::string sInputFilePath = PathResolverFindCalibFile(m_sConfigPath);
80
81 if (!testFileForEOFContainsCharacters(sInputFilePath))
82 ATH_MSG_WARNING("Config file for DiTauSelectionTool with path "<<sInputFilePath<<" does not contain an empty last line. The tool might not be properly configured!");
83
84 rEnv.ReadFile(sInputFilePath.c_str(),
85 kEnvAll);
86
87 std::vector<std::string> vCuts;
88 // if Cuts are specified in the config file take these ones, if not take all
89 // specified in the config
90 if (rEnv.Defined("SelectionCuts"))
91 TauAnalysisTools::split(rEnv, "SelectionCuts", ' ', vCuts);
92 else
93 {
94 auto lList = rEnv.GetTable();
95 for( Int_t i = 0; i < lList->GetEntries(); ++i )
96 {
97 vCuts.push_back( lList->At( i )->GetName() );
98 }
99 }
100
101 int iSelectionCuts = 0;
102
103 for (const std::string& sCut : vCuts)
104 {
105 if (sCut == "PtRegion")
106 {
107 iSelectionCuts = iSelectionCuts | DiTauCutPt;
108 if (m_vPtRegion.empty())
109 TauAnalysisTools::split(rEnv,"PtRegion", ';', m_vPtRegion);
110 }
111 else if (sCut == "PtMin")
112 {
113 iSelectionCuts = iSelectionCuts | DiTauCutPt;
114 if (std::isnan(m_dPtMin.value()))
115 m_dPtMin = rEnv.GetValue("PtMin",NAN);
116 }
117 else if (sCut == "PtMax")
118 {
119 iSelectionCuts = iSelectionCuts | DiTauCutPt;
120 if (std::isnan(m_dPtMax.value()))
121 m_dPtMax = rEnv.GetValue("PtMax",NAN);
122 }
123 else if (sCut == "AbsEtaRegion")
124 {
125 iSelectionCuts = iSelectionCuts | DiTauCutAbsEta;
126 if (m_vAbsEtaRegion.empty())
127 TauAnalysisTools::split(rEnv,"AbsEtaRegion", ';', m_vAbsEtaRegion);
128 }
129 else if (sCut == "AbsEtaMin")
130 {
131 iSelectionCuts = iSelectionCuts | DiTauCutAbsEta;
132 if (std::isnan(m_dAbsEtaMin.value()))
133 m_dAbsEtaMin = rEnv.GetValue("AbsEtaMin",NAN);
134 }
135 else if (sCut == "AbsEtaMax")
136 {
137 iSelectionCuts = iSelectionCuts | DiTauCutAbsEta;
138 if (std::isnan(m_dAbsEtaMax.value()))
139 m_dAbsEtaMax = rEnv.GetValue("AbsEtaMax",NAN);
140 }
141 else if (sCut == "NSubjetsRegion")
142 {
143 iSelectionCuts = iSelectionCuts | DiTauCutNSubjets;
144 if (m_vNSubjetsRegion.empty())
145 TauAnalysisTools::split(rEnv,"NSubjetsRegion", ';', m_vNSubjetsRegion);
146 }
147 else if (sCut == "NSubjetsMin")
148 {
149 iSelectionCuts = iSelectionCuts | DiTauCutNSubjets;
150 if (std::isnan(m_dNSubjetsMin.value()))
151 m_dNSubjetsMin = rEnv.GetValue("NSubjetsMin",NAN);
152 }
153 else if (sCut == "NSubjetsMax")
154 {
155 iSelectionCuts = iSelectionCuts | DiTauCutNSubjets;
156 if (std::isnan(m_dNSubjetsMax.value()))
157 m_dNSubjetsMax = rEnv.GetValue("NSubjetsMax",NAN);
158 }
159 else if (sCut == "AbsCharges")
160 {
161 iSelectionCuts = iSelectionCuts | DiTauCutAbsCharge;
162 if (m_vAbsCharges.empty())
163 TauAnalysisTools::split(rEnv,"AbsCharges", ';', m_vAbsCharges);
164 }
165 else if (sCut == "AbsCharge")
166 {
167 iSelectionCuts = iSelectionCuts | DiTauCutAbsCharge;
168 if (std::isnan(m_iAbsCharge.value()))
169 m_iAbsCharge = rEnv.GetValue("AbsCharge",NAN);
170 }
171 else if (sCut == "OmniScoreRegion")
172 {
173 iSelectionCuts = iSelectionCuts | DiTauCutOmniScore;
174 if (m_vOmniScoreRegion.empty())
175 TauAnalysisTools::split(rEnv,"OmniScoreRegion", ';', m_vOmniScoreRegion);
176
177 // check if using OmniScore
178 m_useOmniScore = true;
179 }
180 else if (sCut == "OmniScoreMin")
181 {
182 iSelectionCuts = iSelectionCuts | DiTauCutOmniScore;
183 if (std::isnan(m_dOmniScoreMin.value()))
184 m_dOmniScoreMin = rEnv.GetValue("OmniScoreMin",NAN);
185
186 // check for possible mis-config in DiTau selection
187 for (const std::string& checkCut : vCuts){
188 if (checkCut.find("OmniScoreRegion") != std::string::npos) {
189 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");
190 return StatusCode::FAILURE;
191 }
192 }
193
194 // check if using OmniScore
195 m_useOmniScore = true;
196 }
197 else if (sCut == "OmniScoreMax")
198 {
199 iSelectionCuts = iSelectionCuts | DiTauCutOmniScore;
200 if (std::isnan(m_dOmniScoreMax.value()))
201 m_dOmniScoreMax = rEnv.GetValue("OmniScoreMax",NAN);
202
203 // check for possible mis-config in DiTau selection
204 for (const std::string& checkCut : vCuts){
205 if (checkCut.find("OmniScoreRegion") != std::string::npos) {
206 ATH_MSG_ERROR("Misconfig due to OmniScoreRegion and OmniScoreMax cuts both present in the config file. Please CHECK carefully config file again and choose of the two");
207 return StatusCode::FAILURE;
208 }
209 }
210
211 // check if using OmniScore
212 m_useOmniScore = true;
213 }
214 else if (sCut == "OmniIDWP")
215 {
216 iSelectionCuts = iSelectionCuts | DiTauCutOmniIDWP;
217 if (m_iOmniIDWP == OMNIIDNONE){
218 m_iOmniIDWP = convertStrToOmniIDWP(rEnv.GetValue("OmniIDWP","OMNIIDNONE"));
219 }
220 // check for possible mis-config in Tau selection
221 for (const std::string& checkCut : vCuts){
222 if (checkCut.find("OmniScore") != std::string::npos) {
223 ATH_MSG_ERROR("Misconfig due to OmniIDWP and OmniScore cuts both present in the config file. Please CHECK carefully config file again");
224 return StatusCode::FAILURE;
225 }
226 }
227 }
228 else ATH_MSG_WARNING("Cut " << sCut << " is not available");
229 }
230
232 m_iSelectionCuts = iSelectionCuts;
233 }
234
235 // initialise the ReadDecorHandleKey if OmniScore is applied
236 if (m_useOmniScore) {
237 if(m_OmniScoreDecorKey.empty()) {
238 ATH_CHECK( m_OmniScoreDecorKey.assign("DiTauJets.omni_score"));
239 }
240 }
242
244
245 // specify all available cut descriptions
246 using map_type = std::map<DiTauSelectionCuts, std::unique_ptr<TauAnalysisTools::DiTauSelectionCut>>;
247 using pair_type = map_type::value_type;
248
249 pair_type elements[] =
250 {
251 {DiTauCutPt, std::make_unique<TauAnalysisTools::DiTauSelectionCutPt>(this)},
252 {DiTauCutAbsEta, std::make_unique<TauAnalysisTools::DiTauSelectionCutAbsEta>(this)},
253 {DiTauCutNSubjets, std::make_unique<TauAnalysisTools::DiTauSelectionCutNSubjets>(this)},
254 {DiTauCutAbsCharge, std::make_unique<TauAnalysisTools::DiTauSelectionCutAbsCharge>(this)},
255 {DiTauCutOmniScore, std::make_unique<TauAnalysisTools::DiTauSelectionCutOmniScore>(this)},
256 {DiTauCutOmniIDWP, std::make_unique<TauAnalysisTools::DiTauSelectionCutOmniIDWP>(this)},
257 };
258
259 m_cMap = { std::make_move_iterator( begin(elements) ), std::make_move_iterator( end(elements) ) };
260
261 ATH_MSG_INFO( "Initializing DiTauSelectionTool" );
267
271 PrintConfigValue ("AbsCharge", m_vAbsCharges);
273 PrintConfigValue ("OmniIDWP", m_sOmniIDWP);
274
275 std::string sCuts = "";
276 if (m_iSelectionCuts & DiTauCutPt) sCuts += "Pt ";
277 if (m_iSelectionCuts & DiTauCutAbsEta) sCuts += "AbsEta ";
278 if (m_iSelectionCuts & DiTauCutNSubjets) sCuts += "NSubjets ";
279 if (m_iSelectionCuts & DiTauCutAbsCharge) sCuts += "AbsCharge ";
280 if (m_iSelectionCuts & DiTauCutOmniScore) sCuts += "OmniScore ";
281 if (m_iSelectionCuts & DiTauCutOmniIDWP) sCuts += "OmniIDWP ";
282
283 ATH_MSG_DEBUG( "cuts: " << sCuts);
284
287
288 for ( const auto& entry : m_cMap ) {
289 if ( m_iSelectionCuts &entry.first ) {
290 entry.second->setAcceptInfo(m_aAccept);
291 }
292 }
293
294 return StatusCode::SUCCESS;
295}
296
297
298//______________________________________________________________________________
300{
301 return StatusCode::SUCCESS;
302}
303
304//______________________________________________________________________________
309
310//______________________________________________________________________________
312{
313 // Check if this is a jet:
314 if( xP->type() != xAOD::Type::Jet )
315 {
316 ATH_MSG_ERROR( "accept(...) Function received a non-jet" );
317 return asg::AcceptData (&m_aAccept);
318 }
319
320 // Cast it to a ditau:
321 const xAOD::DiTauJet* xDiTau = dynamic_cast< const xAOD::DiTauJet* >( xP );
322 if( ! xDiTau )
323 {
324 ATH_MSG_FATAL( "accept(...) Failed to cast particle to tau" );
325 return asg::AcceptData (&m_aAccept);
326 }
327
328 // Let the specific function do the work:
329 return accept( *xDiTau );
330}
331
332//______________________________________________________________________________
334{
335 asg::AcceptData acceptData (&m_aAccept);
336
337 int iNBin = 0;
338
340 {
341 // fill cutflow 'All' bin
342 m_hCutFlow->Fill(iNBin);
343 // fill main distributions before all cuts
344 for (const auto& entry : m_cMap)
345 entry.second->fillHistogramCutPre(xDiTau);
346 }
347 try
348 {
349 for (const auto& entry : m_cMap)
350 {
351 if (m_iSelectionCuts & entry.first)
352 {
353 if (!entry.second->accept(xDiTau, acceptData))
354 return acceptData;
355 else
356 {
358 {
359 // fill cutflow after each passed cut
360 iNBin++;
361 m_hCutFlow->Fill(iNBin);
362 }
363 }
364 }
365 }
366 }
367 catch (const std::runtime_error& error)
368 {
369 // LEGACY: In practical terms this should probably just throw, not
370 // print a warning/error and then continue on. However, I leave
371 // that to the experts who probably had a reason not to let the
372 // exception escape. For now I just downgraded it from error to
373 // warning and limited the number of warnings (04 Jan 22).
374 static std::atomic<uint64_t> warning_count (0u);
375 auto mycount = ++ warning_count;
376 if (mycount < 10u)
377 {
378 ATH_MSG_WARNING(error.what());
379 if (mycount == 9u)
380 ATH_MSG_WARNING ("this is your last warning");
381 }
382 }
383
384 // fill main distributions after all cuts
386 {
387 for (const auto& entry : m_cMap)
388 entry.second->fillHistogramCut(xDiTau);
389 }
390
391 // // Return the result:
392 return acceptData;
393}
394
395//______________________________________________________________________________
396void DiTauSelectionTool::setOutFile( TFile* fOutFile )
397{
398 m_fOutFile = fOutFile;
399}
400
401//______________________________________________________________________________
403{
405 ATH_MSG_WARNING("CreateControlPlots was set to true, but no valid file pointer was provided");
407 {
409 m_fOutFile->mkdir((this->name()+"_control").c_str());
410 m_fOutFile->cd((this->name()+"_control").c_str());
412 m_hCutFlow->Write();
413
414 for (const auto& entry : m_cMap)
415 entry.second->writeControlHistograms();
416 }
417}
418
419
420//=================================PRIVATE-PART=================================
422{
423 // count number of cuts
424 int iNBins = 0;
425 for (const auto& entry : m_cMap)
426 if (m_iSelectionCuts & entry.first)
427 iNBins++;
428 // create cutflow histogram with iNBins+1 bins, where first bin is 'All' bin
429 m_hCutFlow = std::make_shared<TH1F>("hCutFlow","CutFlow;; events",iNBins+1,0,iNBins+1);
430 m_hCutFlow->GetXaxis()->SetBinLabel(1,"All");
431
432 // reusing this variable to reduce overhead
433 iNBins = 2;
434 // set bin labels
435 for (const auto& entry : m_cMap)
436 if (m_iSelectionCuts & entry.first)
437 {
438 m_hCutFlow->GetXaxis()->SetBinLabel(iNBins, entry.second->getName().c_str());
439 iNBins++;
440 }
441}
442
443//______________________________________________________________________________
444template<typename T, typename U>
445void DiTauSelectionTool::FillRegionVector(std::vector<T>& vRegion, U tMin, U tMax) const
446{
447 if (!vRegion.empty())
448 return;
449 if (!std::isnan(tMin)) // if tMin is NAN, then this assumption fails and -inf is added to the vector
450 vRegion.push_back(tMin);
451 else
452 vRegion.push_back(-std::numeric_limits<T>::infinity());
453
454 if (!std::isnan(tMax)) // if tMax is NAN, then this assumption fails and inf is added to the vector
455 vRegion.push_back(tMax);
456 else
457 vRegion.push_back(std::numeric_limits<T>::infinity());
458}
459
460//______________________________________________________________________________
461template<typename T, typename U>
462void DiTauSelectionTool::FillValueVector(std::vector<T>& vRegion, U tVal) const
463{
464 if (!vRegion.empty())
465 return;
466 if (tVal == tVal) // if tMax is NAN, then this assumption fails and nothing is added to the vector
467 vRegion.push_back(tVal);
468}
469
470//______________________________________________________________________________
471template<typename T>
472void DiTauSelectionTool::PrintConfigRegion(const std::string& sCutName, std::vector<T>& vRegion) const
473{
474 unsigned int iNumRegion = vRegion.size()/2;
475 for( unsigned int iRegion = 0; iRegion < iNumRegion; iRegion++ )
476 {
477 ATH_MSG_DEBUG( sCutName<<": " << vRegion.at(iRegion*2) << " to " << vRegion.at(iRegion*2+1) );
478 }
479}
480
481//______________________________________________________________________________
482template<typename T>
483void DiTauSelectionTool::PrintConfigValue(const std::string& sCutName, std::vector<T>& vRegion) const
484{
485 for (auto tVal : vRegion)
486 ATH_MSG_DEBUG( sCutName<<": " << tVal );
487}
488
489//______________________________________________________________________________
490template<typename T>
491void DiTauSelectionTool::PrintConfigValue(const std::string& sCutName, T& tVal) const
492{
493 ATH_MSG_DEBUG( sCutName<<": " << tVal );
494}
495
496//______________________________________________________________________________
497int DiTauSelectionTool::convertStrToOmniIDWP(const std::string& sOmniIDWP) const
498{
499 if (sOmniIDWP == "OMNIIDNONE") return int(OMNIIDNONE);
500 else if (sOmniIDWP == "OMNIIDVERYLOOSE") return int(OMNIIDVERYLOOSE);
501 else if (sOmniIDWP == "OMNIIDLOOSE") return int(OMNIIDLOOSE);
502 else if (sOmniIDWP == "OMNIIDMEDIUM") return int(OMNIIDMEDIUM);
503 else if (sOmniIDWP == "OMNIIDTIGHT") return int(OMNIIDTIGHT);
504
505 ATH_MSG_ERROR( "omni ID working point "<<sOmniIDWP<<" is unknown, the OmniIDWP cut will not accept any ditau!" );
506 return -1;
507}
508
509//______________________________________________________________________________
510std::string DiTauSelectionTool::convertOmniIDWPToStr(int iOmniIDWP) const
511{
512 switch (iOmniIDWP)
513 {
514 case OMNIIDNONE:
515 return "OMNIIDNONE";
516 case OMNIIDVERYLOOSE:
517 return "OMNIIDVERYLOOSE";
518 case OMNIIDLOOSE:
519 return "OMNIIDLOOSE";
520 case OMNIIDMEDIUM:
521 return "OMNIIDMEDIUM";
522 case OMNIIDTIGHT:
523 return "OMNIIDTIGHT";
524
525 default:
526 ATH_MSG_WARNING( "OmniID working point with enum " << iOmniIDWP << " is unknown, the OmniIDWP cut will not accept any ditau!" );
527 return "";
528 }
529}
530
531
532
533
#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.
Gaudi::Property< std::vector< float > > m_vecNSubjetsRegion
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.
Gaudi::Property< std::vector< float > > m_vecPtRegion
std::string convertOmniIDWPToStr(int iOmniIDWP) const
Gaudi::Property< std::vector< float > > m_vecOmniScoreRegion
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