ATLAS Offline Software
HIPileupTool.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // HIPileupTool.cxx
6 
13 
16 
17 #include "TSystem.h"
18 #include "TH1D.h"
19 #include "TH2D.h"
20 #include "TFile.h"
21 #include <vector>
22 #include <exception>
23 
24 namespace HI
25 
26 {
27 
28 HIPileupTool::HIPileupTool( const std::string& myname) : AsgTool(myname),
29  m_accept("HIPileup")
30  {
31  declareProperty("EtCutAndPurity", m_etCutAndPurity, "Et and purity for efficiency calculation");
32  declareProperty("inpFilePath", m_fname, "Calibration file path");
33 
34  m_setup = 0;
35 }
36 
38 
39  if (m_setup) {
40  delete m_hEvents; delete m_hPileUp;
41  delete m_hCut; delete m_hEff; delete m_hPurity;
42  }
43 }
44 
46 
47  ATH_MSG_VERBOSE( "Initialising tool " << name() );
48 
49  m_accept.addCut("pileupVeto","for pileup rejection");
50 
51  const char* fullInpFilePath;
52  if (!m_fname.empty()) fullInpFilePath = gSystem->ExpandPathName (m_fname.c_str());
53  else fullInpFilePath = gSystem->ExpandPathName ("$ROOTCOREBIN/data/HIEventUtils/HIRun2PileUp_PbPb5p02_v2.root");
54  if (gSystem->AccessPathName(fullInpFilePath)) throw std::invalid_argument(("File not found at \"" + (std::string)fullInpFilePath + "\"").c_str());
55 
56  ATH_MSG_INFO(("Read pileup cuts from " + (std::string)fullInpFilePath).c_str());
57  TFile* fIn = new TFile(fullInpFilePath);
58 
59  std::string hname;
60  hname = "hEvents"; if (!(fIn->GetListOfKeys()->Contains(hname.c_str()))) {
61  fIn->Close(); throw std::invalid_argument(("Expected histogram \"" + hname + "\" not found in \"" + m_fname + "\"").c_str());}
62  hname = "hPileUp"; if (!(fIn->GetListOfKeys()->Contains(hname.c_str()))) {
63  fIn->Close(); throw std::invalid_argument(("Expected histogram \"" + hname + "\" not found in \"" + m_fname + "\"").c_str());}
64  hname = "hCut"; if (!(fIn->GetListOfKeys()->Contains(hname.c_str()))) {
65  fIn->Close(); throw std::invalid_argument(("Expected histogram \"" + hname + "\" not found in \"" + m_fname + "\"").c_str());}
66  hname = "hEff"; if (!(fIn->GetListOfKeys()->Contains(hname.c_str()))) {
67  fIn->Close(); throw std::invalid_argument(("Expected histogram \"" + hname + "\" not found in \"" + m_fname + "\"").c_str());}
68  hname = "hPurity"; if (!(fIn->GetListOfKeys()->Contains(hname.c_str()))) {
69  fIn->Close(); throw std::invalid_argument(("Expected histogram \"" + hname + "\" not found in \"" + m_fname + "\"").c_str());}
70 
71  m_hEvents = (TH2D*)((TH2D*)fIn->Get("hEvents"))->Clone("hEvents_HIPileupTool"); m_hEvents->SetDirectory(0);
72  m_hPileUp = (TH2D*)((TH2D*)fIn->Get("hPileUp"))->Clone("hPileUp_HIPileupTool"); m_hPileUp->SetDirectory(0);
73  m_hCut = (TH1D*)((TH1D*)fIn->Get("hCut"))->Clone("hCut_HIPileupTool"); m_hCut->SetDirectory(0);
74  m_hEff = (TH1D*)((TH1D*)fIn->Get("hEff"))->Clone("hEff_HIPileupTool"); m_hEff->SetDirectory(0);
75  m_hPurity = (TH1D*)((TH1D*)fIn->Get("hPurity"))->Clone("hPurity_HIPileupTool"); m_hPurity->SetDirectory(0);
76  m_setup = 1;
77 
78  fIn->Close();
79 
80  m_etThreshold = 6.0;
81  m_purityCut = -1.0;
82  if (!m_etCutAndPurity.empty()) {
83  if (m_etCutAndPurity.size()<2) ATH_MSG_WARNING("Cuts vector should have two elements. Will use default settings");
84  else {
85  if (m_etCutAndPurity.at(1)<0 || m_etCutAndPurity.at(1)>1) ATH_MSG_WARNING("Purity cut should be between 0 and 1. Will use default settings");
86  else {
89  if (fabs(m_purityCut-1)<1e-6) m_purityCut -= 1e-3;
90  }
91  }
92  }
93 
94  if (m_purityCut<0) {
95  ATH_MSG_INFO("ET threshold for purity cut = NONE");
96  ATH_MSG_INFO("Purity cut = DEFAULT");
97  }
98  else {
99  ATH_MSG_INFO("ET threshold for purity cut = " << m_etThreshold << " TeV");
100  ATH_MSG_INFO("Purity cut = " << m_purityCut );
101  }
102 
103  for (int ibx=0;ibx<m_hPileUp->GetNbinsX();ibx++) {
104  if (m_hPileUp->GetXaxis()->GetBinCenter(ibx+1) < m_etThreshold) continue;
105  int NY = m_hPileUp->GetNbinsY();
106  if (m_hPileUp->Integral(ibx+1,ibx+1,1,NY) == 0) {m_hCut->SetBinContent(ibx+1,0); continue;}
107  //Cut on 5.0 because efficiency is calculated only for FCal < 5 TeV
108  if (m_hPileUp->GetXaxis()->GetBinCenter(ibx+1)>5.0 && fabs(m_purityCut-1)<1e-6) {m_hCut->SetBinContent(ibx+1,0); continue;}
109  for (int iby=0;iby<NY;iby++) {
110  if (m_hPileUp->Integral(ibx+1,ibx+1,NY-iby,NY)/m_hPileUp->Integral(ibx+1,ibx+1,1,NY) < m_purityCut) continue;
111  m_hCut->SetBinContent(ibx+1,m_hPileUp->GetYaxis()->GetBinCenter(NY-iby)); break;
112  }
113  }
114 
115  for (int ibx=0;ibx<m_hEvents->GetNbinsX();ibx++) {
116  if (m_hEvents->GetXaxis()->GetBinCenter(ibx+1) < m_etThreshold) {m_hEff->SetBinContent(ibx+1,1-m_hEff->GetBinContent(ibx+1)); continue;}
117  double cut = m_hEvents->Integral(ibx+1,ibx+1,m_hEvents->GetYaxis()->FindBin(m_hCut->GetBinContent(ibx+1)), m_hEvents->GetNbinsY());
118  double all = m_hEvents->Integral(ibx+1,ibx+1,1,m_hEvents->GetYaxis()->GetNbins()) - m_hPileUp->Integral(ibx+1,ibx+1,1,m_hPileUp->GetYaxis()->GetNbins());
119  double pu_cut = m_hPileUp->Integral(ibx+1,ibx+1,m_hPileUp->GetYaxis()->FindBin(m_hCut->GetBinContent(ibx+1)), m_hPileUp->GetNbinsY());
120  double pu_all = m_hPileUp->Integral(ibx+1,ibx+1,1,m_hPileUp->GetYaxis()->GetNbins());
121  double eff = 1 - (cut - pu_cut)/all; if ((cut - pu_cut) < 0) eff=1;
122  double pty = pu_cut/pu_all;
123  if (all!=0) m_hEff->SetBinContent(ibx+1,eff);
124  if (pu_all!=0) m_hPurity->SetBinContent(ibx+1,pty);
125  }
126 
127  return StatusCode::SUCCESS;
128 }
129 
131 
132  bool kPileup = false;
133  double FCal_Et = get_et(evShCont);
134  double nNeutrons;
135  nNeutrons = get_nNeutrons(ZdcCont);
136  if (nNeutrons > m_hCut->GetBinContent(m_hCut->FindFixBin(FCal_Et))) kPileup = true;
137 
138  return kPileup;
139 }
140 
141 double HIPileupTool::get_efficiency(const xAOD::HIEventShapeContainer& evShCont, double FCal_Et) const{
142 
143  FCal_Et = get_et(evShCont);
144  return m_hEff->GetBinContent(m_hEff->FindFixBin(FCal_Et));
145 }
146 
147 double HIPileupTool::get_purity(const xAOD::HIEventShapeContainer& evShCont, double& FCal_Et) {
148 
149  FCal_Et = get_et(evShCont);
150  return m_hPurity->GetBinContent(m_hPurity->FindFixBin(FCal_Et));
151 }
152 
153 
154 double HIPileupTool::get_et(const xAOD::HIEventShapeContainer& evShCont) const{
155  double Fcal_Et=0;
156  Fcal_Et = evShCont.at(5)->et()*1e-6;
157  return Fcal_Et;
158 }
159 
161 
162  bool isCalib=1;
163  double ZdcE=0;
164  for (const auto *zdcModule : ZdcCont) {
165  if (zdcModule->zdcType()!=0) continue;
166 
167  static const SG::ConstAccessor<float> CalibEnergyAcc("CalibEnergy");
168  if (!(CalibEnergyAcc.isAvailable(*zdcModule))) {isCalib = 0; continue;}
169  float modE = CalibEnergyAcc(*zdcModule);
170  ZdcE += modE;
171  }
172  if (!isCalib) throw std::invalid_argument("ZDC Module not Calibrated");
173 
174  return ZdcE/m_npeak;
175 }
176 
177 void HIPileupTool::print() const {
178  AsgTool::print();
179  if (m_purityCut<0) {
180  ATH_MSG_INFO("ET threshold for purity cut = NONE");
181  ATH_MSG_INFO("Purity cut = DEFAULT");
182  }
183  else {
184  ATH_MSG_INFO("ET threshold for purity cut = " << m_etThreshold << " TeV");
185  ATH_MSG_INFO("Purity cut = " << m_purityCut );
186  }
187 }
188 
189 void HIPileupTool::write(TFile* fOut) {
190 
191  fOut->cd(0);
192  m_hEvents->Write();
193  m_hPileUp->Write();
194  m_hCut->Write();
195  m_hEff->Write();
196  m_hPurity->Write();
197 }
198 //R.Longo 13-10-2019 - Replacing PATCore/TAccept (inherited from 21.0 HI-equalization)
200 {
202  bool pileup_dec = !is_pileup(evShCont, ZdcCont);
203  asg::AcceptData(&m_accept).setCutResult("pileupVeto", pileup_dec);
204  return m_accept;
205 }
206 
207 } // HI
AllowedVariables::e
e
Definition: AsgElectronSelectorTool.cxx:37
HI::HIPileupTool::~HIPileupTool
~HIPileupTool()
Definition: HIPileupTool.cxx:37
HI::HIPileupTool::m_hPurity
TH1D * m_hPurity
Definition: HIPileupTool.h:67
HI::HIPileupTool::m_hPileUp
TH2D * m_hPileUp
Definition: HIPileupTool.h:64
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
HI::HIPileupTool::m_hEff
TH1D * m_hEff
Definition: HIPileupTool.h:66
dqt_zlumi_pandas.hname
string hname
Definition: dqt_zlumi_pandas.py:279
AthCommonDataStore< AthCommonMsg< AlgTool > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
HI::HIPileupTool::print
virtual void print() const override
Print the state of the tool.
Definition: HIPileupTool.cxx:177
HI::HIPileupTool::m_hEvents
TH2D * m_hEvents
Definition: HIPileupTool.h:63
TriggerTowerContainer.h
HI::HIPileupTool::write
void write(TFile *fOut)
Definition: HIPileupTool.cxx:189
SG::ConstAccessor< float >
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
HI::HIPileupTool::m_etCutAndPurity
std::vector< double > m_etCutAndPurity
Definition: HIPileupTool.h:54
HI::HIPileupTool::m_accept
asg::AcceptInfo m_accept
Definition: HIPileupTool.h:52
ZdcModuleAuxContainer.h
HI::HIPileupTool::m_etThreshold
double m_etThreshold
Definition: HIPileupTool.h:57
asg::AcceptInfo
Definition: AcceptInfo.h:28
HI::HIPileupTool::m_fname
std::string m_fname
Definition: HIPileupTool.h:55
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
HI::HIPileupTool::get_efficiency
double get_efficiency(const xAOD::HIEventShapeContainer &, double) const
Definition: HIPileupTool.cxx:141
HIEventShape.h
HIEventShapeContainer.h
HI::HIPileupTool::HIPileupTool
HIPileupTool(const std::string &myname="HIPileupTool")
Definition: HIPileupTool.cxx:28
BindingsTest.cut
cut
This script demonstrates how to call a C++ class from Python Also how to use PyROOT is shown.
Definition: BindingsTest.py:13
makeTOC.fOut
fOut
Definition: makeTOC.py:37
DataVector
Derived DataVector<T>.
Definition: DataVector.h:794
HI::HIPileupTool::m_purityCut
double m_purityCut
Definition: HIPileupTool.h:58
print
void print(char *figname, TCanvas *c1)
Definition: TRTCalib_StrawStatusPlots.cxx:25
HI
Definition: HIEventDefs.h:14
HI::HIPileupTool::getAcceptInfo
virtual const asg::AcceptInfo & getAcceptInfo(const xAOD::HIEventShapeContainer &evShCont, const xAOD::ZdcModuleContainer &ZdcCont) const override
Definition: HIPileupTool.cxx:199
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
HI::HIPileupTool::get_nNeutrons
double get_nNeutrons(const xAOD::ZdcModuleContainer &ZdcCont) const
Definition: HIPileupTool.cxx:160
HI::HIPileupTool::m_hCut
TH1D * m_hCut
Definition: HIPileupTool.h:65
Cut::all
@ all
Definition: SUSYToolsAlg.cxx:67
ZdcModule.h
asg::AcceptData::setCutResult
void setCutResult(const std::string &cutName, bool cutResult)
Set the result of a cut, based on the cut name (safer)
Definition: AcceptData.h:134
asg::AcceptData::clear
void clear()
Clear all bits.
Definition: AcceptData.h:54
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
HI::HIPileupTool::is_pileup
bool is_pileup(const xAOD::HIEventShapeContainer &, const xAOD::ZdcModuleContainer &) const override
Definition: HIPileupTool.cxx:130
HI::HIPileupTool::get_purity
double get_purity(const xAOD::HIEventShapeContainer &, double &)
Definition: HIPileupTool.cxx:147
HI::HIPileupTool::initialize
virtual StatusCode initialize() override
Dummy implementation of the initialisation function.
Definition: HIPileupTool.cxx:45
SG::ConstAccessor::isAvailable
bool isAvailable(const ELT &e) const
Test to see if this variable exists in the store.
dqt_zlumi_alleff_HIST.eff
int eff
Definition: dqt_zlumi_alleff_HIST.py:113
DataVector::at
const T * at(size_type n) const
Access an element, as an rvalue.
ConstAccessor.h
Helper class to provide constant type-safe access to aux data.
isCalib
bool isCalib()
Definition: TBPatternUnitHelper.h:43
asg::AcceptData
Definition: AcceptData.h:30
HI::HIPileupTool::m_npeak
const double m_npeak
Definition: HIPileupTool.h:61
HI::HIPileupTool::m_setup
bool m_setup
Definition: HIPileupTool.h:59
MooRTT_summarizeCPU.fIn
fIn
Definition: MooRTT_summarizeCPU.py:11
HI::HIPileupTool::get_et
double get_et(const xAOD::HIEventShapeContainer &evShCont) const
Definition: HIPileupTool.cxx:154
ZdcModuleContainer.h
HIPileupTool.h
asg::AcceptInfo::addCut
int addCut(const std::string &cutName, const std::string &cutDescription)
Add a cut; returning the cut position.
Definition: AcceptInfo.h:53