ATLAS Offline Software
PUResidual3DCorrection.h
Go to the documentation of this file.
1 // this file is -*- C++ -*-
2 /*
3  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
4 */
5 #ifndef JETCALIBTOOLS_PURESIDUAL3DCORRECTION_H
6 #define JETCALIBTOOLS_PURESIDUAL3DCORRECTION_H
7 
19 #include "TH2D.h"
20 #include "TFile.h"
21 #include "TAxis.h"
22 #include <stdio.h>
23 #include <vector>
24 #include <utility>
25 
26 namespace PUCorrection {
27 
29 
31 
32  }
33 
35  float correctedPt(float pt, float eta, float area, float rho, float mu, int NPV ) const {
36  float areaCorr = area*rho*m_rhoEnergyScale;
37 
38  float pt_ref = pt*m_pTEnergyScale ;
39  float calibration3D = correction3D(pt_ref - areaCorr ,
40  eta,
41  mu,
42  NPV);
43  pt_ref = pt_ref - areaCorr - calibration3D;
44  float deltaPt = 0.0;
46  deltaPt = deltaPtCorrection( pt_ref, eta );
47  }
48 
49  return (pt*m_pTEnergyScale -areaCorr - calibration3D + deltaPt)/m_pTEnergyScale;
50  }
51 
53  float correctionFactor(float pt, float eta, float area, float rho, float mu, int NPV ) const {
54  float ptCorr = correctedPt(pt,eta,area,rho,mu,NPV);
55  return ptCorr/pt;
56  }
57 
58 
59 
62  float correction3D(float pt, float eta , float mu, int NPV) const {
63  pt = pt < 1 ? 1 : pt;
64  int muNPVbin = std::as_const(m_ref3DHisto)->FindBin(mu, NPV);
65  int etaBin = m_etaBins->FindFixBin(std::abs(eta)) - 1;
66  float t0 = m_3Dp0_vs_muNPV[ etaBin ]->GetBinContent(muNPVbin);
67  if(t0 <= -999.9) {
68  muNPVbin = m_closestNonEmpty[etaBin][muNPVbin];
69  }
70 
71  float p0 = m_3Dp0_vs_muNPV[ etaBin ]->GetBinContent(muNPVbin);
72  float p1 = m_3Dp1_vs_muNPV[ etaBin ]->GetBinContent(muNPVbin);
73 
74  if(m_use3Dp2) {
75  float p2= m_3Dp2_vs_muNPV[ etaBin ]->GetBinContent(muNPVbin) ;
76  return p0+p1*pt+p2*log(pt);
77  }
78  return p0+p1*pt;
79  }
80 
81 
82 
84  float deltaPtCorrection(float pt, float eta) const {
85  int etabin = m_Dptp0_vs_eta->FindBin(std::abs(eta)) ;
86  float p0 = m_Dptp0_vs_eta->GetBinContent(etabin);
87  float p1 = m_Dptp1_vs_eta->GetBinContent(etabin);
88  return p0+pt*p1;
89  }
90 
91 
92 
94  void loadParameters(const std::string & fileName,
95  const std::string &param3D_name = "param3D",
96  const std::string &paramDelta_name = "paramDeltaPt",
97  const std::string &etaBins_name = "etaBins"
98  ){
99  std::unique_ptr<TFile> tmpF(TFile::Open( fileName.c_str() ));
100  std::vector<float> * etaBins_v = (std::vector<float>*)tmpF->Get(etaBins_name.c_str());
101  std::vector<double> tmp(etaBins_v->begin(), etaBins_v->end() );
102  m_etaBins.reset( new TAxis( tmp.size()-1, tmp.data() ) );
103  TList *param3D_l = (TList*) tmpF->Get(param3D_name.c_str());
104 
105  TList *param3D_p0 = (TList*) param3D_l->At(0);
106  m_3Dp0_vs_muNPV.resize( param3D_p0->GetSize() );
107  TList *param3D_p1 = (TList*) param3D_l->At(1);
108  m_3Dp1_vs_muNPV.resize( param3D_p1->GetSize() );
109 
110  TList *param3D_p2 = nullptr;
111  if(m_use3Dp2) {
112  param3D_p2 = (TList*) param3D_l->At(2);
113  m_3Dp2_vs_muNPV.resize( param3D_p2->GetSize() );
114  }
115 
116  for(size_t i=0 ; i<(etaBins_v->size()-1); i++){
117  m_3Dp0_vs_muNPV[i].reset((TH2D*)param3D_p0->At(i));
118  m_3Dp0_vs_muNPV[i]->SetDirectory(nullptr);
119  m_3Dp1_vs_muNPV[i].reset((TH2D*)param3D_p1->At(i));
120  m_3Dp1_vs_muNPV[i]->SetDirectory(nullptr);
121  if(m_use3Dp2) {
122  m_3Dp2_vs_muNPV[i].reset( (TH2D*)param3D_p2->At(i) );
123  m_3Dp2_vs_muNPV[i]->SetDirectory(nullptr);
124  }
125  }
126  m_ref3DHisto = m_3Dp0_vs_muNPV[0].get();
127 
128  TList* paramDelta_l = (TList*) tmpF->Get(paramDelta_name.c_str());
129  m_Dptp0_vs_eta.reset( (TH1F*) paramDelta_l->At(0) );
130  m_Dptp0_vs_eta->SetDirectory(nullptr);
131  m_Dptp1_vs_eta.reset( (TH1F*) paramDelta_l->At(1) ) ;
132  m_Dptp1_vs_eta->SetDirectory(nullptr);
134  }
135 
136 
137 
139  // Pre calculate the positions of the closest non empty bins
140 
141  // we have a map (bin -> non-empty bin) for each eta slice :
142  m_closestNonEmpty.resize( m_3Dp0_vs_muNPV.size() );
143  for(size_t etabin=0; etabin< m_closestNonEmpty.size() ;etabin++ ){
144 
145  TH2D *refHisto = m_3Dp0_vs_muNPV[etabin].get() ;
146  int nTot = refHisto->GetNcells();
147  TAxis * xax = refHisto->GetXaxis();
148  TAxis * yax = refHisto->GetYaxis();
149  float xscale = 1./(xax->GetXmax()-xax->GetXmin()); xscale *= xscale;
150  float yscale = 1./(yax->GetXmax()-yax->GetXmin()); yscale *= yscale;
151  int nbinX = xax->GetNbins();
152  int nbinY = yax->GetNbins();
153  // initialize the map with -1 :
154  m_closestNonEmpty[etabin].resize( nTot, -1 );
155 
156  // loop over each bin
157  for(int xi=1;xi<nbinX+1;xi++) for(int yi=1;yi<nbinY+1;yi++) {
158  int bi = refHisto->GetBin(xi,yi);
159  if(refHisto->GetBinContent( bi ) >-999.) continue; // non empty bin, we don't need to find anything for it.
160 
161  int clBin = -1;
162  float x0 = xax->GetBinCenter(xi);
163  float y0 = yax->GetBinCenter(yi);
164  float minDr2=1e10; // just pick a bigger value than any distance in the (mu,NPV) plan
165  // loop over other bins to find the closest non-empty :
166  for(int xj=1;xj<nbinX+1;xj++) for(int yj=1;yj<nbinY+1;yj++) {
167  int bj = refHisto->GetBin(xj,yj);
168  if(refHisto->GetBinContent( bj ) <=-999.) continue; // this is an empty bin
169  float x = xax->GetBinCenter(xj);
170  float y = yax->GetBinCenter(yj);
171  float dr2 = (x0-x)*(x0-x)*xscale+(y0-y)*(y0-y)*yscale;
172  if(dr2<minDr2){ minDr2 = dr2; clBin = bj;} // found a closest-bin candidate
173  }
174  m_closestNonEmpty[etabin][bi] = clBin;
175  }
176 
177  }
178  }
179 
180 
181 
182  //************************************
183  // data members
184 
185  // 3D corrections parameters :
186  std::unique_ptr<TAxis> m_etaBins ;
187  std::vector<std::unique_ptr<TH2D> > m_3Dp0_vs_muNPV;
188  std::vector<std::unique_ptr<TH2D> > m_3Dp1_vs_muNPV;
189  std::vector<std::unique_ptr<TH2D> > m_3Dp2_vs_muNPV;
190  TH2D* m_ref3DHisto = nullptr;
191  bool m_use3Dp2=true;
192 
193  // DeltaPt corrections parameters :
194  std::unique_ptr<TH1F> m_Dptp0_vs_eta=nullptr;
195  std::unique_ptr<TH1F> m_Dptp1_vs_eta=nullptr;
196 
197 
198  //
199  float m_maxPt=170.0 ; // GeV !!
200  float m_rhoEnergyScale = 0.001; // 0.001 when rho is given in MeV.
201  float m_pTEnergyScale = 0.001; // 0.001 when pT is given in MeV.
202 
203  bool m_applyDeltaPtTerm = true; //boolean to switch on/off the deltaPt correction
204 
205  // ***************
206  //
207  std::vector< std::vector<int> > m_closestNonEmpty;
208 
209 
210 
211 
212 
213 
214 
215 
216 
217 
218 
219 
220  // *******************************************************
221  // function belows are not used in the correction evaluation but have proven useful for tests during developments
222  // of the calibration methods. We keep them here just in case.
223  //
224 
227  float correction3D_noextrap(float pt, float eta , float mu, int NPV) const {
228  int muNPVbin = m_ref3DHisto->FindBin(mu, NPV);
229  int etaBin = m_etaBins->FindFixBin(std::abs(eta)) - 1;
230  float p0 = m_3Dp0_vs_muNPV[ etaBin ]->GetBinContent(muNPVbin);
231  float p1 = m_3Dp1_vs_muNPV[ etaBin ]->GetBinContent(muNPVbin);
232 
233 
234  if ( (p0<= -999.9) || (p1<=-999.9) ) return 0;
235 
236  if(m_use3Dp2) {
237  float p2= m_3Dp2_vs_muNPV[ etaBin ]->GetBinContent(muNPVbin) ;
238  if ( p2<=-999.9 ) return 0;
239  return p0+p1*log(pt-p2);
240  }
241  return p0+p1*pt;
242  }
243 
244 
245  // Just a test to see if we get smoother calib by doing an interpolation at point (mu,NPV), not used yet
246  float correction3D_interp(float pt, float eta , float mu, int NPV) const {
247  int etaBin = m_etaBins->FindFixBin(std::abs(eta)) - 1;
248  float p0 = m_3Dp0_vs_muNPV[ etaBin ]->Interpolate(mu, NPV);
249  float p1 = m_3Dp1_vs_muNPV[ etaBin ]->Interpolate(mu,NPV);
250 
251  if ( (p0<= -999.9) || (p1<=-999.9) ) return 0;
252 
253  if(m_use3Dp2) {
254  float p2= m_3Dp2_vs_muNPV[ etaBin ]->Interpolate(mu,NPV) ;
255  if ( p2<=-999.9 ) return 0;
256  return p0+p1*log(pt-p2);
257  }
258  return p0+p1*pt;
259  }
260 
261 
262  };
263 
264 }
265 
266 #endif
PUCorrection::PU3DCorrectionHelper::correctionFactor
float correctionFactor(float pt, float eta, float area, float rho, float mu, int NPV) const
same as above but returns the ration pT_corrected/pT_uncorrected
Definition: PUResidual3DCorrection.h:53
PUCorrection::PU3DCorrectionHelper::m_maxPt
float m_maxPt
Definition: PUResidual3DCorrection.h:199
xAOD::deltaPt
setSAddress setEtaMS setDirPhiMS setDirZMS setBarrelRadius setEndcapAlpha setEndcapRadius setInterceptInner setEtaMap setEtaBin setIsTgcFailure deltaPt
Definition: L2StandAloneMuon_v1.cxx:156
PUCorrection::PU3DCorrectionHelper::correction3D_interp
float correction3D_interp(float pt, float eta, float mu, int NPV) const
Definition: PUResidual3DCorrection.h:246
PUCorrection::PU3DCorrectionHelper::m_etaBins
std::unique_ptr< TAxis > m_etaBins
Definition: PUResidual3DCorrection.h:186
PUCorrection::PU3DCorrectionHelper::~PU3DCorrectionHelper
~PU3DCorrectionHelper()
Definition: PUResidual3DCorrection.h:30
PUCorrection
Definition: JetPileupCorrection.h:23
PUCorrection::PU3DCorrectionHelper::m_use3Dp2
bool m_use3Dp2
Definition: PUResidual3DCorrection.h:191
PUCorrection::PU3DCorrectionHelper::loadParameters
void loadParameters(const std::string &fileName, const std::string &param3D_name="param3D", const std::string &paramDelta_name="paramDeltaPt", const std::string &etaBins_name="etaBins")
Loads the calib constants from histograms in TFile named fileName.
Definition: PUResidual3DCorrection.h:94
PUCorrection::PU3DCorrectionHelper::deltaPtCorrection
float deltaPtCorrection(float pt, float eta) const
IMPORTANT : the pt must be given in GeV.
Definition: PUResidual3DCorrection.h:84
TRTCalib_cfilter.p1
p1
Definition: TRTCalib_cfilter.py:130
test_pyathena.pt
pt
Definition: test_pyathena.py:11
PUCorrection::PU3DCorrectionHelper::m_closestNonEmpty
std::vector< std::vector< int > > m_closestNonEmpty
Definition: PUResidual3DCorrection.h:207
PUCorrection::PU3DCorrectionHelper::m_3Dp1_vs_muNPV
std::vector< std::unique_ptr< TH2D > > m_3Dp1_vs_muNPV
Definition: PUResidual3DCorrection.h:188
PUCorrection::PU3DCorrectionHelper::m_Dptp0_vs_eta
std::unique_ptr< TH1F > m_Dptp0_vs_eta
Definition: PUResidual3DCorrection.h:194
x
#define x
TRTCalib_cfilter.p2
p2
Definition: TRTCalib_cfilter.py:131
PUCorrection::PU3DCorrectionHelper::m_applyDeltaPtTerm
bool m_applyDeltaPtTerm
Definition: PUResidual3DCorrection.h:203
xAOD::etaBin
setSAddress setEtaMS setDirPhiMS setDirZMS setBarrelRadius setEndcapAlpha setEndcapRadius setInterceptInner setEtaMap etaBin
Definition: L2StandAloneMuon_v1.cxx:148
PUCorrection::PU3DCorrectionHelper
Definition: PUResidual3DCorrection.h:28
PUCorrection::PU3DCorrectionHelper::m_ref3DHisto
TH2D * m_ref3DHisto
Definition: PUResidual3DCorrection.h:190
FortranAlgorithmOptions.fileName
fileName
Definition: FortranAlgorithmOptions.py:13
PUCorrection::PU3DCorrectionHelper::m_3Dp0_vs_muNPV
std::vector< std::unique_ptr< TH2D > > m_3Dp0_vs_muNPV
Definition: PUResidual3DCorrection.h:187
lumiFormat.i
int i
Definition: lumiFormat.py:85
PUCorrection::PU3DCorrectionHelper::m_rhoEnergyScale
float m_rhoEnergyScale
Definition: PUResidual3DCorrection.h:200
DeMoUpdate.tmp
string tmp
Definition: DeMoUpdate.py:1167
PUCorrection::PU3DCorrectionHelper::correction3D
float correction3D(float pt, float eta, float mu, int NPV) const
calculate the mu,NPV dependent part of the correction.
Definition: PUResidual3DCorrection.h:62
PUCorrection::PU3DCorrectionHelper::setupClosestNonEmptyBins
void setupClosestNonEmptyBins()
Definition: PUResidual3DCorrection.h:138
PUCorrection::PU3DCorrectionHelper::m_Dptp1_vs_eta
std::unique_ptr< TH1F > m_Dptp1_vs_eta
Definition: PUResidual3DCorrection.h:195
y
#define y
python.CaloCondTools.log
log
Definition: CaloCondTools.py:20
PUCorrection::PU3DCorrectionHelper::correction3D_noextrap
float correction3D_noextrap(float pt, float eta, float mu, int NPV) const
calculate the mu,NPV dependent part of the correction (this is only used for tests and validation) ...
Definition: PUResidual3DCorrection.h:227
python.TrigEgammaMonitorHelper.TH1F
def TH1F(name, title, nxbins, bins_par2, bins_par3=None, path='', **kwargs)
Definition: TrigEgammaMonitorHelper.py:24
area
double area(double R)
Definition: ConvertStaveServices.cxx:42
PUCorrection::PU3DCorrectionHelper::m_3Dp2_vs_muNPV
std::vector< std::unique_ptr< TH2D > > m_3Dp2_vs_muNPV
Definition: PUResidual3DCorrection.h:189
CaloNoise_fillDB.mu
mu
Definition: CaloNoise_fillDB.py:53
PUCorrection::PU3DCorrectionHelper::correctedPt
float correctedPt(float pt, float eta, float area, float rho, float mu, int NPV) const
Main function which returns the corrected pT.
Definition: PUResidual3DCorrection.h:35
fitman.rho
rho
Definition: fitman.py:532
TRTCalib_cfilter.p0
p0
Definition: TRTCalib_cfilter.py:129
PUCorrection::PU3DCorrectionHelper::m_pTEnergyScale
float m_pTEnergyScale
Definition: PUResidual3DCorrection.h:201