ATLAS Offline Software
ResidualOffsetCorrection.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 #include <TEnv.h>
6 #include <TAxis.h>
7 
10 
13 #include <utility>
14 
16  : asg::AsgMessaging("ResidualOffsetCorrection"),
17  m_config(nullptr), m_jetAlgo(""), m_calibAreaTag(""), m_dev(false), m_isData(false),
18  m_npvBeamspotCorr(nullptr), m_resOffsetBins(nullptr)
19 { }
20 
21 ResidualOffsetCorrection::ResidualOffsetCorrection(const std::string& name, TEnv* config, TString jetAlgo, TString calibAreaTag, bool isData, bool dev)
22  : asg::AsgMessaging(name),
23  m_config(config), m_jetAlgo(std::move(jetAlgo)), m_calibAreaTag(std::move(calibAreaTag)), m_dev(dev), m_isData(isData),
24  m_npvBeamspotCorr(nullptr), m_resOffsetBins(nullptr)
25 { }
26 
28 
31 
32 }
33 
35 
36  //Read mu scale factor from global config
37  m_muSF = m_config->GetValue("MuScaleFactor",1.0);
38 
39  // Read useNjet from global config
40  m_useNjet = m_config->GetValue("OffsetCorrection.UseNjet",false);
41 
42  bool doMuOnly = m_config->GetValue("ApplyOnlyMuResidual",false);
43  bool doNPVOnly = m_config->GetValue("ApplyOnlyNPVResidual",false);
44  bool doNJetOnly = m_config->GetValue("ApplyOnlyNJetResidual",false);
45 
46  //Read reference mu, NPV and nJet from global config
47  m_mu_ref = m_config->GetValue("OffsetCorrection.DefaultMuRef",-99.0);
48  if (m_mu_ref==-99 && !doNPVOnly && !doNJetOnly) {
49  ATH_MSG_FATAL("OffsetCorrection.DefaultMuRef not specified.");
50  return StatusCode::FAILURE;
51  }
52  m_NPV_ref = m_config->GetValue("OffsetCorrection.DefaultNPVRef",-99.0);
53  if (m_NPV_ref==-99 && !doMuOnly && !m_useNjet) {
54  ATH_MSG_FATAL("OffsetCorrection.DefaultNPVRef not specified.");
55  return StatusCode::FAILURE;
56  }
57 
58  if (m_NPV_ref==-99 && !doMuOnly && !m_useNjet) {
59  ATH_MSG_FATAL("OffsetCorrection.DefaultNPVRef not specified.");
60  return StatusCode::FAILURE;
61  }
62  m_nJet_ref = m_config->GetValue("OffsetCorrection.DefaultNjetRef",-99.0);
63  if (m_nJet_ref==-99 && m_useNjet) {
64  ATH_MSG_FATAL("OffsetCorrection.DefaultNjetRef not specified.");
65  return StatusCode::FAILURE;
66  }
67 
68  //Add the residual offset correction factors to the config TEnv
69  TString ResidualOffsetCalibFile = m_config->GetValue("ResidualOffset.CalibFile","");
70  if(m_dev){
71  ResidualOffsetCalibFile.Remove(0,33);
72  ResidualOffsetCalibFile.Insert(0,"JetCalibTools/");
73  }
74  else{ResidualOffsetCalibFile.Insert(14,m_calibAreaTag);}
75  TString calibFile = PathResolverFindCalibFile(ResidualOffsetCalibFile.Data());
76  m_config->ReadFile(calibFile, kEnvLocal);
77  //Retrieve information specific to the residual offset correction from the TEnv
78  TString offsetName = m_config->GetValue("ResidualOffsetCorrection.Name","");
79  m_resOffsetDesc = m_config->GetValue(offsetName+".Description","");
80  ATH_MSG_INFO("Reading residual jet-area pile-up correction factors from: " << calibFile);
81  ATH_MSG_INFO("Description: " << m_resOffsetDesc);
82 
83  //Check if the config is set to apply the beamspot correction to NPV
84  m_applyNPVBeamspotCorrection = m_config->GetValue("ApplyNPVBeamspotCorrection",false);
85 
86  std::vector<double> offsetEtaBins = JetCalibUtils::VectorizeD( m_config->GetValue(offsetName+".AbsEtaBins","") );
87  if (offsetEtaBins.size()<3) { ATH_MSG_FATAL(offsetName << ".AbsEtaBins not specified"); return StatusCode::FAILURE; }
88  m_resOffsetBins = new TAxis(offsetEtaBins.size()-1,&offsetEtaBins[0]);
89 
90  if(!doNPVOnly && !doNJetOnly){
91  m_resOffsetMu = JetCalibUtils::VectorizeD(m_config->GetValue(offsetName+".MuTerm."+m_jetAlgo,""));
92  if ( (int)m_resOffsetMu.size()!=m_resOffsetBins->GetNbins()+1 ) {
93  ATH_MSG_FATAL( "Incorrect specification of " << offsetName << ".MuTerm." << m_jetAlgo );
94  return StatusCode::FAILURE;
95  }
96  }
97  if(m_useNjet) {
98  m_resOffsetNjet = JetCalibUtils::VectorizeD(m_config->GetValue(offsetName+".nJetTerm."+m_jetAlgo,""));
99  if ((int)m_resOffsetNjet.size()!=m_resOffsetBins->GetNbins()+1) {
100  ATH_MSG_FATAL( "Incorrect specification of " << offsetName << ".nJetTerm." << m_jetAlgo );
101  return StatusCode::FAILURE;
102  }
103  } else if(!doMuOnly){
104  m_resOffsetNPV = JetCalibUtils::VectorizeD(m_config->GetValue(offsetName+".NPVTerm."+m_jetAlgo,""));
105  if ((int)m_resOffsetNPV.size()!=m_resOffsetBins->GetNbins()+1) {
106  ATH_MSG_FATAL( "Incorrect specification of " << offsetName << ".NPVTerm." << m_jetAlgo );
107  return StatusCode::FAILURE;
108  }
111  ATH_MSG_INFO("\n NPV beamspot correction will be applied.");
112  }
113  }
114 
115  return StatusCode::SUCCESS;
116 }
117 
118 double ResidualOffsetCorrection::GetResidualOffset ( double abseta, double mu, double NPV,
119  int nJet, bool MuOnly, bool NOnly ) const {
120  return GetResidualOffsetET(abseta, mu, NPV, nJet, MuOnly, NOnly,
122 }
123 
124 double ResidualOffsetCorrection::GetResidualOffsetET( double abseta, double mu, double NPV,
125  int nJet, bool MuOnly, bool NOnly,
126  const std::vector<double>& OffsetMu,
127  const std::vector<double>& OffsetNPV,
128  const std::vector<double>& OffsetNjet,
129  const TAxis *OffsetBins ) const {
130 
131  //mu rescaling
132  const double muCorr = m_isData ? mu : mu*m_muSF;
133  if(m_useNjet) {
134  // further correction to nJet if desired
135  int nJetCorr = nJet;
136 
137  double alpha, beta, etaEdge;
138  if(!MuOnly){ beta = OffsetNjet[0];
139  } else { beta = 0; }
140  if(!NOnly){ alpha = OffsetMu[0];
141  } else { alpha = 0; }
142  etaEdge=0;
143  int bin=1;
144  for (;bin<=OffsetBins->GetNbins();++bin) {
145  etaEdge = OffsetBins->GetBinLowEdge(bin);
146  const double width=OffsetBins->GetBinWidth(bin);
147  if (abseta<etaEdge+width) break;
148  if(!NOnly) alpha += width*OffsetMu[bin];
149  if(!MuOnly) beta += width*OffsetNjet[bin];
150  }
151  if(!NOnly) alpha += OffsetMu[bin]*(abseta-etaEdge);
152  if(!MuOnly) beta += OffsetNjet[bin]*(abseta-etaEdge);
153  return (alpha*(muCorr-m_mu_ref) + beta*(nJetCorr-m_nJet_ref))*m_GeV;
154  } else {
155  //NPV beamspot correction
156  const double NPVCorr = GetNPVBeamspotCorrection(NPV);
157 
158  double alpha, beta, etaEdge;
159  if(!MuOnly){ beta = OffsetNPV[0];
160  } else { beta = 0; }
161  if(!NOnly){ alpha = OffsetMu[0];
162  } else { alpha = 0; }
163  etaEdge = 0;
164  int bin=1;
165  for (;bin<=OffsetBins->GetNbins();++bin) {
166  etaEdge = OffsetBins->GetBinLowEdge(bin);
167  const double width=OffsetBins->GetBinWidth(bin);
168  if (abseta<etaEdge+width) break;
169  if(!NOnly) alpha += width*OffsetMu[bin];
170  if(!MuOnly) beta += width*OffsetNPV[bin];
171  }
172  if(!NOnly) alpha += OffsetMu[bin]*(abseta-etaEdge);
173  if(!MuOnly) beta += OffsetNPV[bin]*(abseta-etaEdge);
174  return (alpha*(muCorr-m_mu_ref) + beta*(NPVCorr-m_NPV_ref))*m_GeV;
175  }
176 
177 }
178 
180  if(!m_isData && m_applyNPVBeamspotCorrection) return std::as_const(m_npvBeamspotCorr)->GetNVertexBsCorrection(NPV);
181  return NPV;
182 }
183 
ResidualOffsetCorrection::m_npvBeamspotCorr
NPVBeamspotCorrection * m_npvBeamspotCorr
Definition: ResidualOffsetCorrection.h:54
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
ResidualOffsetCorrection::m_resOffsetNjet
std::vector< double > m_resOffsetNjet
Definition: ResidualOffsetCorrection.h:63
ResidualOffsetCorrection::m_config
TEnv * m_config
Definition: ResidualOffsetCorrection.h:48
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
ResidualOffsetCorrection::m_applyNPVBeamspotCorrection
bool m_applyNPVBeamspotCorrection
Definition: ResidualOffsetCorrection.h:58
ResidualOffsetCorrection::m_resOffsetDesc
TString m_resOffsetDesc
Definition: ResidualOffsetCorrection.h:56
asg
Definition: DataHandleTestTool.h:28
bin
Definition: BinsDiffFromStripMedian.h:43
ResidualOffsetCorrection::m_muSF
double m_muSF
Definition: ResidualOffsetCorrection.h:59
NPVBeamspotCorrection.h
ResidualOffsetCorrection::ResidualOffsetCorrection
ResidualOffsetCorrection()
Definition: ResidualOffsetCorrection.cxx:15
config
Definition: PhysicsAnalysis/AnalysisCommon/AssociationUtils/python/config.py:1
ResidualOffsetCorrection::initialize
virtual StatusCode initialize()
Definition: ResidualOffsetCorrection.cxx:34
ResidualOffsetCorrection::m_mu_ref
double m_mu_ref
Definition: ResidualOffsetCorrection.h:60
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
jet::PileupComp::OffsetNPV
@ OffsetNPV
Definition: UncertaintyEnum.h:165
ResidualOffsetCorrection::m_jetAlgo
TString m_jetAlgo
Definition: ResidualOffsetCorrection.h:49
ResidualOffsetCorrection::m_resOffsetMu
std::vector< double > m_resOffsetMu
Definition: ResidualOffsetCorrection.h:63
ResidualOffsetCorrection::m_isData
bool m_isData
Definition: ResidualOffsetCorrection.h:51
PathResolver.h
JetCalibUtils.h
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
ResidualOffsetCorrection::GetResidualOffset
double GetResidualOffset(double abseta, double mu, double NPV, int nJet, bool MuOnly, bool NOnly) const
Definition: ResidualOffsetCorrection.cxx:118
plotBeamSpotVxVal.bin
int bin
Definition: plotBeamSpotVxVal.py:83
PathResolverFindCalibFile
std::string PathResolverFindCalibFile(const std::string &logical_file_name)
Definition: PathResolver.cxx:431
jet::PileupComp::OffsetMu
@ OffsetMu
Definition: UncertaintyEnum.h:166
ResidualOffsetCorrection::m_calibAreaTag
TString m_calibAreaTag
Definition: ResidualOffsetCorrection.h:49
ResidualOffsetCorrection::m_resOffsetNPV
std::vector< double > m_resOffsetNPV
Definition: ResidualOffsetCorrection.h:63
Base_Fragment.width
width
Definition: Sherpa_i/share/common/Base_Fragment.py:59
ResidualOffsetCorrection::m_NPV_ref
double m_NPV_ref
Definition: ResidualOffsetCorrection.h:60
ResidualOffsetCorrection::m_GeV
static constexpr float m_GeV
Definition: ResidualOffsetCorrection.h:52
ResidualOffsetCorrection.h
ResidualOffsetCorrection::~ResidualOffsetCorrection
virtual ~ResidualOffsetCorrection()
Definition: ResidualOffsetCorrection.cxx:27
ResidualOffsetCorrection::GetNPVBeamspotCorrection
double GetNPVBeamspotCorrection(double NPV) const
Definition: ResidualOffsetCorrection.cxx:179
ResidualOffsetCorrection::m_useNjet
bool m_useNjet
Definition: ResidualOffsetCorrection.h:61
ResidualOffsetCorrection::GetResidualOffsetET
double GetResidualOffsetET(double abseta, double mu, double NPV, int nJet, bool MuOnly, bool NOnly, const std::vector< double > &OffsetMu, const std::vector< double > &OffsetNPV, const std::vector< double > &OffsetNjet, const TAxis *OffsetBins) const
Definition: ResidualOffsetCorrection.cxx:124
JetCalibUtils::VectorizeD
VecD VectorizeD(const TString &str, const TString &sep=" ")
Definition: JetCalibUtils.cxx:25
ResidualOffsetCorrection::m_resOffsetBins
TAxis * m_resOffsetBins
Definition: ResidualOffsetCorrection.h:57
MuonParameters::beta
@ beta
Definition: MuonParamDefs.h:144
CaloNoise_fillDB.mu
mu
Definition: CaloNoise_fillDB.py:53
ResidualOffsetCorrection::m_dev
bool m_dev
Definition: ResidualOffsetCorrection.h:50
NPVBeamspotCorrection
Definition: NPVBeamspotCorrection.h:21
ResidualOffsetCorrection::m_nJet_ref
double m_nJet_ref
Definition: ResidualOffsetCorrection.h:60