ATLAS Offline Software
TauCalibrateLC.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 
9 
10 #include "TFile.h"
11 #include "TF1.h"
12 #include "TH1D.h"
13 #include <utility>
14 
15 #define GeV 1000
16 
17 /********************************************************************/
18 TauCalibrateLC::TauCalibrateLC(const std::string& name) :
20  declareProperty("calibrationFile", m_calibrationFile = "");
21  declareProperty("VertexCorrection", m_doVertexCorrection = true);
22 }
23 
24 /********************************************************************/
26 }
27 
28 /********************************************************************/
30 
31  ATH_CHECK( m_aveIntPerXKey.initialize(inTrigger()) );
32  ATH_CHECK( m_vertexInputContainer.initialize(!inTrigger()) );
33 
34  std::string fullPath = find_file(m_calibrationFile);
35  ATH_MSG_INFO("Using calibration file: " << fullPath);
36 
37  std::unique_ptr<TFile> file(TFile::Open(fullPath.c_str(), "READ"));
38  if (!file) {
39  ATH_MSG_FATAL("Failed to open " << fullPath);
40  return StatusCode::FAILURE;
41  }
42 
43  // get the histogram defining eta binning
44  std::string key = "etaBinning";
45  TH1* histo = dynamic_cast<TH1*>(file->Get(key.c_str()));
46  if (histo) {
47  histo->SetDirectory(nullptr);
48  m_etaBinHist = std::unique_ptr<TH1>(histo);
49  }
50  else {
51  ATH_MSG_FATAL("Failed to get an object with key " << key);
52  return StatusCode::FAILURE;
53  }
54 
55  //retrieve number of eta bins from file
56  m_nEtaBins = m_etaBinHist->GetNbinsX();
57 
58  TString tmpSlopKey[s_nProngBins] = {"slopeNPV1P", "slopeNPV3P"};
59  TString tmpFuncBase[s_nProngBins] = {"OneP_Eta_", "MultiP_Eta_"};
60 
63 
65  m_calibFunc.resize(s_nProngBins);
66  for (int i = 0; i < s_nProngBins; ++i) {
67  m_calibFunc[i].resize(m_nEtaBins);
68  }
69 
70  for (int i = 0; i < s_nProngBins; i++) {
71  histo = dynamic_cast<TH1*>(file->Get(tmpSlopKey[i])); // get pile-up slope histograms
72  if (histo) {
73  histo->SetDirectory(nullptr);
74  m_slopeNPVHist[i] = std::unique_ptr<TH1>(histo);
75  }
76  else {
77  ATH_MSG_FATAL("Failed to get an object with key " << tmpSlopKey[i]);
78  return StatusCode::FAILURE;
79  }
80 
81  for (int j = 0; j < m_nEtaBins; j++) {
82  TString key = tmpFuncBase[i];
83  key += j;
84  TF1* fcn = dynamic_cast<TF1*>(file->Get(key));
85  if (fcn) {
86  m_calibFunc[i][j] = std::unique_ptr<TF1>(fcn);
87  }
88  else {
89  ATH_MSG_FATAL("Failed to get an object with key " << key);
90  return StatusCode::FAILURE;
91  }
92  }
93  }
94  m_averageNPV = m_slopeNPVHist[0]->GetBinContent(0); // underflow is the average number of reconstructed primary vertices
95  ATH_MSG_DEBUG("averageNPV = " << m_averageNPV);
96 
97  file->Close();
98 
99  return StatusCode::SUCCESS;
100 }
101 
102 /********************************************************************/
104 {
105  // energy calibration depends on number of tracks - 1p or Mp
106  int prongBin = 1; //Mp
107  if (tau.nTracks() <= 1) prongBin = 0; //1p
108 
109  // get IntermediateAxis or DetectorAxis momentum
111 
112  double absEta = std::abs( tau_p4.Eta() );
113  int etaBin = std::as_const(*m_etaBinHist).GetXaxis()->FindBin(absEta) - 1;
114 
115  if (etaBin>=m_nEtaBins) etaBin = m_nEtaBins-1; // correction from last bin should be applied on all taus outside stored eta range
116 
117  int nVertex = 0;
118 
119  // Obtain pileup
120  if (inTrigger()) { // online: retrieved from EventInfo
122  if (!eventInfoDecorHandle.isPresent()) {
123  ATH_MSG_WARNING ( "EventInfo decoration not available! Will set nVertex = " << m_averageNPV );
124  nVertex = m_averageNPV;
125  }
126  else {
127  nVertex = eventInfoDecorHandle(0);
128  ATH_MSG_DEBUG("AvgInteractions object in tau candidate = " << nVertex);
129  }
130  }
131  else { // offline: pileup vertex multiplicity
133  if (!vertexInHandle.isValid()) {
134  ATH_MSG_ERROR ("Could not retrieve HiveDataObj with key " << vertexInHandle.key());
135  return StatusCode::FAILURE;
136  }
137  const xAOD::VertexContainer * vxContainer = vertexInHandle.cptr();
138  for (const auto *const vertex : *vxContainer) {
139  if (vertex->vertexType() == xAOD::VxType::PileUp) {
140  ++nVertex;
141  }
142  }
143  ATH_MSG_DEBUG("calculated nVertex " << nVertex );
144  }
145 
146  double calibConst = 1.;
147 
148  double slopeNPV = m_slopeNPVHist[prongBin]->GetBinContent(etaBin + 1);
149  double offset = slopeNPV * (nVertex - m_averageNPV);
150 
151  // pt (energy) response parameterized as a function of pileup-corrected pt (energy)
152  double energyLC = tau_p4.Pt()/GeV;
153 
154  if (energyLC <= 0.) {
155  ATH_MSG_DEBUG("tau energy at LC scale is " << energyLC << "--> set energy=0.001");
156  tau.setP4(0.001, tau_p4.Eta(), tau_p4.Phi(), tau.m());
157  tau.setP4(xAOD::TauJetParameters::TauEnergyScale, tau.pt(), tau.eta(), tau.phi(), tau.m());
158  return StatusCode::SUCCESS;
159  }
160 
161  if (energyLC - offset <= 0.) {
162  ATH_MSG_DEBUG("after pile-up correction energy would be = " << energyLC - offset << " --> setting offset=0 now!");
163  offset = 0.;
164  }
165 
166  // apply offset correction
167  double energyPileupCorr = energyLC - offset;
168 
169  if (energyPileupCorr > 0. and energyPileupCorr < 10000.) // from 0 to 10 TeV
170  {
171  calibConst = m_calibFunc[prongBin][etaBin]->Eval(energyPileupCorr);
172 
173  if (calibConst <= 0.) {
174  ATH_MSG_DEBUG("calibration constant = " << calibConst);
175  ATH_MSG_DEBUG("prongBin = " << prongBin);
176  ATH_MSG_DEBUG("etaBin = " << etaBin);
177  ATH_MSG_DEBUG("energyPileupCorr = " << energyPileupCorr);
178  ATH_MSG_DEBUG("energyLC = " << energyLC);
179  calibConst = 1.;
180  }
181  }
182 
183  double energyFinal = energyPileupCorr / calibConst;
184 
185  // tau.m() is 0 by convention in tauRecTools
186  // while mDetectorAxis and mIntermediateAxis are not forced to 0, mTauEnergyScale is
187  tau.setP4( energyFinal * GeV, tau_p4.Eta(), tau_p4.Phi(), tau.m());
188  tau.setP4(xAOD::TauJetParameters::TauEnergyScale, tau.pt(), tau.eta(), tau.phi(), tau.m());
189  ATH_MSG_DEBUG("Energy at LC scale = " << energyLC << " pile-up offset " << offset << " calib. const. = " << calibConst << " final energy = " << energyFinal);
190 
191  return StatusCode::SUCCESS;
192 }
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
TauCalibrateLC.h
xAOD::TauJetParameters::IntermediateAxis
@ IntermediateAxis
Definition: TauDefs.h:338
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
SG::ReadHandle::cptr
const_pointer_type cptr()
Dereference the pointer.
xAOD::TauJet_v3::eta
virtual double eta() const
The pseudorapidity ( ) of the particle.
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
AthCommonDataStore< AthCommonMsg< AlgTool > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
xAOD::TauJet_v3::m
virtual double m() const
The invariant mass of the particle.
CscCalibQuery.fullPath
string fullPath
Definition: CscCalibQuery.py:360
xAOD::TauJet_v3::nTracks
size_t nTracks(TauJetParameters::TauTrackFlag flag=TauJetParameters::TauTrackFlag::classifiedCharged) const
Definition: TauJet_v3.cxx:526
TauCalibrateLC::s_nProngBins
static const int s_nProngBins
Definition: TauCalibrateLC.h:38
TauRecToolBase
The base class for all tau tools.
Definition: TauRecToolBase.h:21
SG::ReadDecorHandle::isPresent
bool isPresent() const
Is the referenced container present in SG?
TauCalibrateLC::m_aveIntPerXKey
SG::ReadDecorHandleKey< xAOD::EventInfo > m_aveIntPerXKey
Definition: TauCalibrateLC.h:50
TauRecToolBase::inTrigger
bool inTrigger() const
Definition: TauRecToolBase.h:87
xAOD::TauJet_v3::setP4
void setP4(double pt, double eta, double phi, double m)
Set methods for IParticle values.
Definition: TauJet_v3.cxx:172
TauCalibrateLC::TauCalibrateLC
TauCalibrateLC(const std::string &name="TauCalibrateLC")
Definition: TauCalibrateLC.cxx:18
xAOD::TauJet_v3::pt
virtual double pt() const
The transverse momentum ( ) of the particle.
xAOD::TauJet_v3::phi
virtual double phi() const
The azimuthal angle ( ) of the particle.
xAOD::etaBin
setSAddress setEtaMS setDirPhiMS setDirZMS setBarrelRadius setEndcapAlpha setEndcapRadius setInterceptInner setEtaMap etaBin
Definition: L2StandAloneMuon_v1.cxx:148
TauCalibrateLC::m_etaBinHist
std::unique_ptr< TH1 > m_etaBinHist
Definition: TauCalibrateLC.h:42
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
SG::ReadDecorHandle
Handle class for reading a decoration on an object.
Definition: StoreGate/StoreGate/ReadDecorHandle.h:94
TauCalibrateLC::initialize
virtual StatusCode initialize() override
Tool initializer.
Definition: TauCalibrateLC.cxx:29
lumiFormat.i
int i
Definition: lumiFormat.py:92
TauCalibrateLC::m_vertexInputContainer
SG::ReadHandleKey< xAOD::VertexContainer > m_vertexInputContainer
Definition: TauCalibrateLC.h:55
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
xAOD::TauJet_v3
Class describing a tau jet.
Definition: TauJet_v3.h:41
TauCalibrateLC::m_nEtaBins
int m_nEtaBins
Definition: TauCalibrateLC.h:44
file
TFile * file
Definition: tile_monitor.h:29
TauCalibrateLC::execute
virtual StatusCode execute(xAOD::TauJet &tau) const override
Execute - called for each tau candidate.
Definition: TauCalibrateLC.cxx:103
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
DataVector
Derived DataVector<T>.
Definition: DataVector.h:581
fcn
void fcn(int &, double *, double &result, double par[], int)
this is where we write out chi2
Definition: Chi2LJets.cxx:183
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
xAOD::TauJetParameters::TauEnergyScale
@ TauEnergyScale
Definition: TauDefs.h:339
TauCalibrateLC::m_averageNPV
double m_averageNPV
Definition: TauCalibrateLC.h:45
xAOD::VxType::PileUp
@ PileUp
Pile-up vertex.
Definition: TrackingPrimitives.h:573
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
ReadHandle.h
Handle class for reading from StoreGate.
TauRecToolBase::find_file
std::string find_file(const std::string &fname) const
Definition: TauRecToolBase.cxx:19
TauCalibrateLC::m_slopeNPVHist
std::vector< std::unique_ptr< TH1 > > m_slopeNPVHist
Definition: TauCalibrateLC.h:41
SG::VarHandleBase::key
virtual const std::string & key() const override final
Return the StoreGate ID for the referenced object.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleBase.cxx:64
TauCalibrateLC::~TauCalibrateLC
~TauCalibrateLC()
Definition: TauCalibrateLC.cxx:25
Trk::vertex
@ vertex
Definition: MeasurementType.h:21
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
xAOD::TauJet_v3::p4
virtual FourMom_t p4() const
The full 4-momentum of the particle.
Definition: TauJet_v3.cxx:97
TH1
Definition: rootspy.cxx:268
GeV
#define GeV
Definition: TauCalibrateLC.cxx:15
ReadDecorHandle.h
Handle class for reading a decoration on an object.
convertTimingResiduals.offset
offset
Definition: convertTimingResiduals.py:71
TauGNNUtils::Variables::absEta
bool absEta(const xAOD::TauJet &tau, double &out)
Definition: TauGNNUtils.cxx:232
xAOD::TauJetParameters::DetectorAxis
@ DetectorAxis
Definition: TauDefs.h:337
plotBeamSpotCompare.histo
histo
Definition: plotBeamSpotCompare.py:415
TauCalibrateLC::m_calibrationFile
std::string m_calibrationFile
Definition: TauCalibrateLC.h:47
TauCalibrateLC::m_doVertexCorrection
bool m_doVertexCorrection
Definition: TauCalibrateLC.h:48
TauCalibrateLC::m_calibFunc
std::vector< std::vector< std::unique_ptr< TF1 > > > m_calibFunc
Definition: TauCalibrateLC.h:40
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37