ATLAS Offline Software
DiTauSelectionTool.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // Local include(s):
9 
10 // Framework include(s):
12 
13 // ROOT include(s)
14 #include "TEnv.h"
15 #include "THashList.h"
16 
17 // System include(s)
18 #include <atomic>
19 
20 using namespace TauAnalysisTools;
21 
22 //=================================PUBLIC-PART==================================
23 //______________________________________________________________________________
24 DiTauSelectionTool::DiTauSelectionTool( const std::string& name )
25  : asg::AsgMetadataTool( name )
26  , m_fOutFile(nullptr)
27  , m_aAccept( "DiTauSelection" )
28 {
29  declareProperty( "CreateControlPlots", m_bCreateControlPlots = false);
30  /*
31  Baseline properties declaration:
32  properties containing 'Region' are a vector of lower and upper bounds
33  other properties named in plural are a list of exact values to cut on
34  other properties are single cuts
35  */
36  declareProperty( "ConfigPath", m_sConfigPath = "");
37  declareProperty( "SelectionCuts", m_iSelectionCuts = NoDiTauCut); // initialize with 'no' cuts
38  declareProperty( "PtRegion", m_vPtRegion = {}); // in GeV
39  declareProperty( "PtMin", m_dPtMin = NAN); // in GeV
40  declareProperty( "PtMax", m_dPtMax = NAN); // in GeV
41  declareProperty( "AbsEtaRegion", m_vAbsEtaRegion = {});
42  declareProperty( "AbsEtaMin", m_dAbsEtaMin = NAN);
43  declareProperty( "AbsEtaMax", m_dAbsEtaMax = NAN);
44  declareProperty( "NSubjetsRegion", m_vNSubjetsRegion = {});
45  declareProperty( "NSubjetsMin", m_dNSubjetsMin = NAN);
46  declareProperty( "NSubjetsMax", m_dNSubjetsMax = NAN);
47 
48 }
49 
50 //______________________________________________________________________________
51 DiTauSelectionTool::~DiTauSelectionTool()
52 {
53  m_cMap.clear();
54 }
55 
56 //______________________________________________________________________________
58 {
59  bool bConfigViaConfigFile = !m_sConfigPath.empty();
60  bool bConfigViaProperties = false;
61  if (!bConfigViaProperties and !m_vPtRegion.empty()) bConfigViaProperties = true;
62  if (!bConfigViaProperties and m_dPtMin == m_dPtMin) bConfigViaProperties = true;
63  if (!bConfigViaProperties and m_dPtMax == m_dPtMax) bConfigViaProperties = true;
64  if (!bConfigViaProperties and !m_vAbsEtaRegion.empty()) bConfigViaProperties = true;
65  if (!bConfigViaProperties and m_dAbsEtaMin == m_dAbsEtaMin) bConfigViaProperties = true;
66  if (!bConfigViaProperties and m_dAbsEtaMax == m_dAbsEtaMax) bConfigViaProperties = true;
67  if (!bConfigViaProperties and !m_vNSubjetsRegion.empty()) bConfigViaProperties = true;
68  if (!bConfigViaProperties and m_dNSubjetsMin == m_dNSubjetsMin) bConfigViaProperties = true;
69  if (!bConfigViaProperties and m_dNSubjetsMax == m_dNSubjetsMax) bConfigViaProperties = true;
70 
71  if (bConfigViaConfigFile and bConfigViaProperties)
72  {
73  ATH_MSG_WARNING("Configured tool via setProperty and configuration file, which may lead to unexpected configuration.");
74  ATH_MSG_WARNING("In doubt check the configuration that is printed when the tool is initialized and the message level is set to debug");
75  ATH_MSG_WARNING("For further details please refer to the documentation:");
76  ATH_MSG_WARNING("https://gitlab.cern.ch/atlas/athena/blob/master/PhysicsAnalysis/TauID/TauAnalysisTools/doc/README-DiTauSelectionTool.rst");
77  }
78  if (!bConfigViaConfigFile and !bConfigViaProperties)
79  {
80  ATH_MSG_WARNING("No cut configuration provided, the tool will not do anything. For further details please refer to the documentation:");
81  ATH_MSG_WARNING("https://gitlab.cern.ch/atlas/athena/blob/master/PhysicsAnalysis/TauID/TauAnalysisTools/doc/README-DiTauSelectionTool.rst");
82  }
83 
84  if (bConfigViaConfigFile)
85  {
86  TEnv rEnv;
87  std::string sInputFilePath = PathResolverFindCalibFile(m_sConfigPath);
88 
89  if (!testFileForEOFContainsCharacters(sInputFilePath))
90  ATH_MSG_WARNING("Config file for DiTauSelectionTool with path "<<sInputFilePath<<" does not contain an empty last line. The tool might not be properly configured!");
91 
92  rEnv.ReadFile(sInputFilePath.c_str(),
93  kEnvAll);
94 
95  std::vector<std::string> vCuts;
96  // if Cuts are specified in the config file take these ones, if not take all
97  // specified in the config
98  if (rEnv.Defined("SelectionCuts"))
99  TauAnalysisTools::split(rEnv, "SelectionCuts", ' ', vCuts);
100  else
101  {
102  auto lList = rEnv.GetTable();
103  for( Int_t i = 0; i < lList->GetEntries(); ++i )
104  {
105  vCuts.push_back( lList->At( i )->GetName() );
106  }
107  }
108 
109  int iSelectionCuts = 0;
110 
111  for (const std::string& sCut : vCuts)
112  {
113  if (sCut == "PtRegion")
114  {
115  iSelectionCuts = iSelectionCuts | DiTauCutPt;
116  if (m_vPtRegion.empty())
117  TauAnalysisTools::split(rEnv,"PtRegion", ';', m_vPtRegion);
118  }
119  else if (sCut == "PtMin")
120  {
121  iSelectionCuts = iSelectionCuts | DiTauCutPt;
122  if (m_dPtMin != m_dPtMin)
123  m_dPtMin = rEnv.GetValue("PtMin",NAN);
124  }
125  else if (sCut == "PtMax")
126  {
127  iSelectionCuts = iSelectionCuts | DiTauCutPt;
128  if (m_dPtMax != m_dPtMax)
129  m_dPtMax = rEnv.GetValue("PtMax",NAN);
130  }
131  else if (sCut == "AbsEtaRegion")
132  {
133  iSelectionCuts = iSelectionCuts | DiTauCutAbsEta;
134  if (m_vAbsEtaRegion.empty())
135  TauAnalysisTools::split(rEnv,"AbsEtaRegion", ';', m_vAbsEtaRegion);
136  }
137  else if (sCut == "AbsEtaMin")
138  {
139  iSelectionCuts = iSelectionCuts | DiTauCutAbsEta;
140  if (m_dAbsEtaMin != m_dAbsEtaMin)
141  m_dAbsEtaMin = rEnv.GetValue("AbsEtaMin",NAN);
142  }
143  else if (sCut == "AbsEtaMax")
144  {
145  iSelectionCuts = iSelectionCuts | DiTauCutAbsEta;
146  if (m_dAbsEtaMax != m_dAbsEtaMax)
147  m_dAbsEtaMax = rEnv.GetValue("AbsEtaMax",NAN);
148  }
149  else if (sCut == "NSubjetsRegion")
150  {
151  iSelectionCuts = iSelectionCuts | DiTauCutNSubjets;
152  if (m_vNSubjetsRegion.empty())
153  TauAnalysisTools::split(rEnv,"NSubjetsRegion", ';', m_vNSubjetsRegion);
154  }
155  else if (sCut == "NSubjetsMin")
156  {
157  iSelectionCuts = iSelectionCuts | DiTauCutNSubjets;
159  m_dNSubjetsMin = rEnv.GetValue("NSubjetsMin",NAN);
160  }
161  else if (sCut == "NSubjetsMax")
162  {
163  iSelectionCuts = iSelectionCuts | DiTauCutNSubjets;
165  m_dNSubjetsMax = rEnv.GetValue("NSubjetsMax",NAN);
166  }
167  else ATH_MSG_WARNING("Cut " << sCut << " is not available");
168  }
169 
171  m_iSelectionCuts = iSelectionCuts;
172  }
173 
174  // specify all available cut descriptions
175  using map_type = std::map<DiTauSelectionCuts, std::unique_ptr<TauAnalysisTools::DiTauSelectionCut>>;
176  using pair_type = map_type::value_type;
177 
178  pair_type elements[] =
179  {
180  {DiTauCutPt, std::make_unique<TauAnalysisTools::DiTauSelectionCutPt>(this)},
181  {DiTauCutAbsEta, std::make_unique<TauAnalysisTools::DiTauSelectionCutAbsEta>(this)},
182  {DiTauCutNSubjets, std::make_unique<TauAnalysisTools::DiTauSelectionCutNSubjets>(this)},
183  };
184 
185  m_cMap = { std::make_move_iterator( begin(elements) ), std::make_move_iterator( end(elements) ) };
186 
187  ATH_MSG_INFO( "Initializing TauSelectionTool" );
191 
194  PrintConfigRegion ("NSubjets", m_vNSubjetsRegion);
195 
196  std::string sCuts = "";
197  if (m_iSelectionCuts & DiTauCutPt) sCuts += "Pt ";
198  if (m_iSelectionCuts & DiTauCutAbsEta) sCuts += "AbsEta ";
199  if (m_iSelectionCuts & DiTauCutNSubjets) sCuts += "NSubjets ";
200 
201  ATH_MSG_DEBUG( "cuts: " << sCuts);
202 
205 
206  for ( const auto& entry : m_cMap ) {
207  if ( m_iSelectionCuts &entry.first ) {
208  entry.second->setAcceptInfo(m_aAccept);
209  }
210  }
211 
212  return StatusCode::SUCCESS;
213 }
214 
215 
216 //______________________________________________________________________________
218 {
219  return StatusCode::SUCCESS;
220 }
221 
222 //______________________________________________________________________________
224 {
225  return m_aAccept;
226 }
227 
228 //______________________________________________________________________________
230 {
231  // Check if this is a jet:
232  if( xP->type() != xAOD::Type::Jet )
233  {
234  ATH_MSG_ERROR( "accept(...) Function received a non-jet" );
235  return asg::AcceptData (&m_aAccept);
236  }
237 
238  // Cast it to a ditau:
239  const xAOD::DiTauJet* xDiTau = dynamic_cast< const xAOD::DiTauJet* >( xP );
240  if( ! xDiTau )
241  {
242  ATH_MSG_FATAL( "accept(...) Failed to cast particle to tau" );
243  return asg::AcceptData (&m_aAccept);
244  }
245 
246  // Let the specific function do the work:
247  return accept( *xDiTau );
248 }
249 
250 //______________________________________________________________________________
252 {
253  asg::AcceptData acceptData (&m_aAccept);
254 
255  int iNBin = 0;
256 
258  {
259  // fill cutflow 'All' bin
260  m_hCutFlow->Fill(iNBin);
261  // fill main distributions before all cuts
262  for (const auto& entry : m_cMap)
263  entry.second->fillHistogramCutPre(xDiTau);
264  }
265  try
266  {
267  for (const auto& entry : m_cMap)
268  {
269  if (m_iSelectionCuts & entry.first)
270  {
271  if (!entry.second->accept(xDiTau, acceptData))
272  return acceptData;
273  else
274  {
276  {
277  // fill cutflow after each passed cut
278  iNBin++;
279  m_hCutFlow->Fill(iNBin);
280  }
281  }
282  }
283  }
284  }
285  catch (const std::runtime_error& error)
286  {
287  // LEGACY: In practical terms this should probably just throw, not
288  // print a warning/error and then continue on. However, I leave
289  // that to the experts who probably had a reason not to let the
290  // exception escape. For now I just downgraded it from error to
291  // warning and limited the number of warnings (04 Jan 22).
292  static std::atomic<uint64_t> warning_count (0u);
293  auto mycount = ++ warning_count;
294  if (mycount < 10u)
295  {
296  ATH_MSG_WARNING(error.what());
297  if (mycount == 9u)
298  ATH_MSG_WARNING ("this is your last warning");
299  }
300  }
301 
302  // fill main distributions after all cuts
304  {
305  for (const auto& entry : m_cMap)
306  entry.second->fillHistogramCut(xDiTau);
307  }
308 
309  // // Return the result:
310  return acceptData;
311 }
312 
313 //______________________________________________________________________________
314 void DiTauSelectionTool::setOutFile( TFile* fOutFile )
315 {
316  m_fOutFile = fOutFile;
317 }
318 
319 //______________________________________________________________________________
321 {
323  ATH_MSG_WARNING("CreateControlPlots was set to true, but no valid file pointer was provided");
325  {
327  m_fOutFile->mkdir((this->name()+"_control").c_str());
328  m_fOutFile->cd((this->name()+"_control").c_str());
330  m_hCutFlow->Write();
331 
332  for (const auto& entry : m_cMap)
333  entry.second->writeControlHistograms();
334  }
335 }
336 
337 
338 //=================================PRIVATE-PART=================================
340 {
341  // count number of cuts
342  int iNBins = 0;
343  for (const auto& entry : m_cMap)
344  if (m_iSelectionCuts & entry.first)
345  iNBins++;
346  // create cutflow histogram with iNBins+1 bins, where first bin is 'All' bin
347  m_hCutFlow = std::make_shared<TH1F>("hCutFlow","CutFlow;; events",iNBins+1,0,iNBins+1);
348  m_hCutFlow->GetXaxis()->SetBinLabel(1,"All");
349 
350  // reusing this variable to reduce overhead
351  iNBins = 2;
352  // set bin labels
353  for (const auto& entry : m_cMap)
354  if (m_iSelectionCuts & entry.first)
355  {
356  m_hCutFlow->GetXaxis()->SetBinLabel(iNBins, entry.second->getName().c_str());
357  iNBins++;
358  }
359 }
360 
361 //______________________________________________________________________________
362 template<typename T, typename U>
363 void DiTauSelectionTool::FillRegionVector(std::vector<T>& vRegion, U tMin, U tMax) const
364 {
365  if (!vRegion.empty())
366  return;
367  if (tMin == tMin) // if tMin is NAN, then this assumption fails and -inf is added to the vector
368  vRegion.push_back(tMin);
369  else
370  vRegion.push_back(-std::numeric_limits<T>::infinity());
371 
372  if (tMax == tMax) // if tMax is NAN, then this assumption fails and inf is added to the vector
373  vRegion.push_back(tMax);
374  else
375  vRegion.push_back(std::numeric_limits<T>::infinity());
376 }
377 
378 //______________________________________________________________________________
379 template<typename T, typename U>
380 void DiTauSelectionTool::FillValueVector(std::vector<T>& vRegion, U tVal) const
381 {
382  if (!vRegion.empty())
383  return;
384  if (tVal == tVal) // if tMax is NAN, then this assumption fails and nothing is added to the vector
385  vRegion.push_back(tVal);
386 }
387 
388 //______________________________________________________________________________
389 template<typename T>
390 void DiTauSelectionTool::PrintConfigRegion(const std::string& sCutName, std::vector<T>& vRegion) const
391 {
392  unsigned int iNumRegion = vRegion.size()/2;
393  for( unsigned int iRegion = 0; iRegion < iNumRegion; iRegion++ )
394  {
395  ATH_MSG_DEBUG( sCutName<<": " << vRegion.at(iRegion*2) << " to " << vRegion.at(iRegion*2+1) );
396  }
397 }
398 
399 //______________________________________________________________________________
400 template<typename T>
401 void DiTauSelectionTool::PrintConfigValue(const std::string& sCutName, std::vector<T>& vRegion) const
402 {
403  for (auto tVal : vRegion)
404  ATH_MSG_DEBUG( sCutName<<": " << tVal );
405 }
406 
407 //______________________________________________________________________________
408 template<typename T>
409 void DiTauSelectionTool::PrintConfigValue(const std::string& sCutName, T& tVal) const
410 {
411  ATH_MSG_DEBUG( sCutName<<": " << tVal );
412 }
413 
TauAnalysisTools::DiTauSelectionTool::m_bCreateControlPlots
bool m_bCreateControlPlots
Definition: DiTauSelectionTool.h:122
TauAnalysisTools
Definition: TruthCollectionMakerTau.h:16
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
TauAnalysisTools::DiTauSelectionTool::m_aAccept
asg::AcceptInfo m_aAccept
Object used to store selection information.
Definition: DiTauSelectionTool.h:125
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
DiTauSelectionTool.h
TauAnalysisTools::DiTauSelectionTool::initialize
virtual StatusCode initialize() override
Function initialising the tool.
Definition: DiTauSelectionTool.cxx:57
PlotCalibFromCool.begin
begin
Definition: PlotCalibFromCool.py:94
asg
Definition: DataHandleTestTool.h:28
TauAnalysisTools::DiTauSelectionTool::getAcceptInfo
virtual const asg::AcceptInfo & getAcceptInfo() const override
Get an object describing the "selection steps" of the tool.
Definition: DiTauSelectionTool.cxx:223
TauAnalysisTools::DiTauSelectionTool::m_dPtMin
float m_dPtMin
Definition: DiTauSelectionTool.h:103
TauAnalysisTools::DiTauCutPt
@ DiTauCutPt
Definition: PhysicsAnalysis/TauID/TauAnalysisTools/TauAnalysisTools/Enums.h:58
xAOD::IParticle::type
virtual Type::ObjectType type() const =0
The type of the object as a simple enumeration.
TauAnalysisTools::DiTauSelectionTool::writeControlHistograms
virtual void writeControlHistograms() override
Write control histograms to output file.
Definition: DiTauSelectionTool.cxx:320
TauAnalysisTools::DiTauSelectionTool::beginEvent
virtual StatusCode beginEvent() override
Function called when a new events is loaded.
Definition: DiTauSelectionTool.cxx:217
TauAnalysisTools::DiTauSelectionTool::m_iSelectionCuts
int m_iSelectionCuts
Definition: DiTauSelectionTool.h:95
TauAnalysisTools::testFileForEOFContainsCharacters
bool testFileForEOFContainsCharacters(const std::string &sFileName)
returns true if last line in file is empty or the line starts with the number sign
Definition: PhysicsAnalysis/TauID/TauAnalysisTools/Root/HelperFunctions.cxx:279
xAOD::IParticle
Class providing the definition of the 4-vector interface.
Definition: Event/xAOD/xAODBase/xAODBase/IParticle.h:41
Trk::u
@ u
Enums for curvilinear frames.
Definition: ParamDefs.h:77
TauAnalysisTools::NoDiTauCut
@ NoDiTauCut
Definition: PhysicsAnalysis/TauID/TauAnalysisTools/TauAnalysisTools/Enums.h:57
mergePhysValFiles.end
end
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:93
SharedFilesVersion.h
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
TauAnalysisTools::DiTauSelectionTool::m_dNSubjetsMax
float m_dNSubjetsMax
Definition: DiTauSelectionTool.h:107
TauAnalysisTools::DiTauSelectionTool::m_dPtMax
float m_dPtMax
Definition: DiTauSelectionTool.h:104
asg::AcceptInfo
Definition: AcceptInfo.h:28
TauAnalysisTools::DiTauSelectionTool::m_vAbsEtaRegion
std::vector< float > m_vAbsEtaRegion
Definition: DiTauSelectionTool.h:99
lumiFormat.i
int i
Definition: lumiFormat.py:85
TauAnalysisTools::DiTauSelectionTool::m_hCutFlow
std::shared_ptr< TH1F > m_hCutFlow
Definition: DiTauSelectionTool.h:112
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
TauAnalysisTools::DiTauCutNSubjets
@ DiTauCutNSubjets
Definition: PhysicsAnalysis/TauID/TauAnalysisTools/TauAnalysisTools/Enums.h:60
TauAnalysisTools::DiTauSelectionTool::setupCutFlowHistogram
void setupCutFlowHistogram()
Definition: DiTauSelectionTool.cxx:339
DiTauSelectionCuts.h
TauAnalysisTools::DiTauSelectionTool::PrintConfigValue
void PrintConfigValue(const std::string &sCutName, std::vector< T > &vRegion) const
Definition: DiTauSelectionTool.cxx:401
TauAnalysisTools::DiTauSelectionTool::FillRegionVector
void FillRegionVector(std::vector< T > &vRegion, U tMin, U tMax) const
Definition: DiTauSelectionTool.cxx:363
GetAllXsec.entry
list entry
Definition: GetAllXsec.py:132
TauAnalysisTools::DiTauSelectionTool::m_vPtRegion
std::vector< float > m_vPtRegion
Definition: DiTauSelectionTool.h:97
PathResolver.h
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
PathResolverFindCalibFile
std::string PathResolverFindCalibFile(const std::string &logical_file_name)
Definition: PathResolver.cxx:431
TauAnalysisTools::DiTauSelectionTool::m_dAbsEtaMin
float m_dAbsEtaMin
Definition: DiTauSelectionTool.h:105
TauAnalysisTools::DiTauSelectionTool::setOutFile
virtual void setOutFile(TFile *fOutFile) override
Set output file for control histograms.
Definition: DiTauSelectionTool.cxx:314
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
TauAnalysisTools::DiTauSelectionTool::m_dNSubjetsMin
float m_dNSubjetsMin
Definition: DiTauSelectionTool.h:108
TauAnalysisTools::DiTauSelectionTool::PrintConfigRegion
void PrintConfigRegion(const std::string &sCutName, std::vector< T > &vRegion) const
Definition: DiTauSelectionTool.cxx:390
xAOD::DiTauJet_v1
Definition: DiTauJet_v1.h:31
TauAnalysisTools::DiTauCutAbsEta
@ DiTauCutAbsEta
Definition: PhysicsAnalysis/TauID/TauAnalysisTools/TauAnalysisTools/Enums.h:59
TauAnalysisTools::DiTauSelectionTool::m_cMap
std::map< DiTauSelectionCuts, std::unique_ptr< TauAnalysisTools::DiTauSelectionCut > > m_cMap
Definition: DiTauSelectionTool.h:117
TauAnalysisTools::DiTauSelectionCut::declareProperty
void declareProperty(const std::string &name, std::string &loc)
Definition: DiTauSelectionCuts.cxx:75
TauAnalysisTools::DiTauSelectionTool::m_dAbsEtaMax
float m_dAbsEtaMax
Definition: DiTauSelectionTool.h:106
TauAnalysisTools::DiTauSelectionTool::m_vNSubjetsRegion
std::vector< float > m_vNSubjetsRegion
Definition: DiTauSelectionTool.h:101
TauAnalysisTools::DiTauSelectionTool::FillValueVector
void FillValueVector(std::vector< T > &vRegion, U tVal) const
Definition: DiTauSelectionTool.cxx:380
asg::AcceptData
Definition: AcceptData.h:30
error
Definition: IImpactPoint3dEstimator.h:70
TauAnalysisTools::DiTauSelectionTool::accept
virtual asg::AcceptData accept(const xAOD::IParticle *p) const override
Get the decision using a generic IParticle pointer.
Definition: DiTauSelectionTool.cxx:229
xAOD::Jet
Jet_v1 Jet
Definition of the current "jet version".
Definition: Event/xAOD/xAODJet/xAODJet/Jet.h:17
TauAnalysisTools::DiTauSelectionTool::m_sConfigPath
std::string m_sConfigPath
Definition: DiTauSelectionTool.h:115
TauAnalysisTools::split
void split(const std::string &sInput, const char cDelim, std::vector< std::string > &vOut)
Definition: PhysicsAnalysis/TauID/TauAnalysisTools/Root/HelperFunctions.cxx:23
TauAnalysisTools::DiTauSelectionTool::m_fOutFile
TFile * m_fOutFile
Definition: DiTauSelectionTool.h:111