ATLAS Offline Software
CorrelationMatrix.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
7 
8 #include "TH2D.h"
9 
10 namespace jet
11 {
12 
14 // //
15 // Constructor/destructor/initialization //
16 // //
18 
20  : asg::AsgMessaging(name)
21  , m_isInit(false)
22  , m_name(name.c_str())
23  , m_numBins(-1)
24  , m_minVal(1e10)
25  , m_maxVal(-1e10)
26  , m_fixedVal1(0)
27  , m_fixedVal2(0)
28  , m_corrMat(nullptr)
29  , m_jets(nullptr)
30  , m_eInfos(nullptr)
31  , m_jet1(nullptr)
32  , m_jet2(nullptr)
33  , m_eInfo(nullptr)
34 {
36 }
37 
38 CorrelationMatrix::CorrelationMatrix(const TString& name, const int numBins, const double minVal, const double maxVal, const double fixedVal1, const double fixedVal2)
39  : asg::AsgMessaging(name.Data())
40  , m_isInit(false)
41  , m_name(name)
42  , m_numBins(numBins)
43  , m_minVal(minVal)
44  , m_maxVal(maxVal)
45  , m_fixedVal1(fixedVal1)
46  , m_fixedVal2(fixedVal2)
47  , m_corrMat(nullptr)
48  , m_jets(nullptr)
49  , m_eInfos(nullptr)
50  , m_jet1(nullptr)
51  , m_jet2(nullptr)
52  , m_eInfo(nullptr)
53 {
54  ATH_MSG_DEBUG("Creating CorrelationMatrix named " << m_name.Data());
55 }
56 
57 
59 {
61 }
62 
63 
65 {
66  // Check for initialization and tool status
67  if (checkInitialization(uncTool).isFailure())
68  return StatusCode::FAILURE;
69  m_isInit = true;
70 
71  // Determine the center of mass energy for kinematic limits if possible
72  // If not possible, then ignores kinematic limits
73  // pt * cosh(eta) = sqrtS/2 for kinematic limit
74  const double sqrtS = uncTool.getSqrtS();
75  const double kinLimit1 = sqrtS > 0 ? (sqrtS/2.) / cosh(m_fixedVal1) : 1e99;
76  const double kinLimit2 = sqrtS > 0 ? (sqrtS/2.) / cosh(m_fixedVal2) : 1e99;
77 
78  std::cout << "Initializing for Pt with fixedVal1, fixedVal2 " << m_fixedVal1 << " " << m_fixedVal2 << std::endl;
79  std::cout << "Corresponding to kinematic limits " << kinLimit1 << " " << kinLimit2 << std::endl;
80 
81 
82  // Build the correlation matrix
84 
85  // Prepare the objects for use
86  if (createStore().isFailure())
87  return StatusCode::FAILURE;
88 
89  // Set default values
90  if (setDefaultProperties(uncTool).isFailure())
91  return StatusCode::FAILURE;
92 
93  // Fill the correlation matrix
94  for (int binX = 1; binX <= m_corrMat->GetNbinsX(); ++binX)
95  {
96  // Set the jet values, (pT, eta, phi, m)
97  // note that only pT and eta matter right now
98  const double valX = m_corrMat->GetXaxis()->GetBinCenter(binX);
100 
101  for (int binY = 1; binY <= m_corrMat->GetNbinsY(); ++binY)
102  {
103  const double valY = m_corrMat->GetYaxis()->GetBinCenter(binY);
104 
105  // Check for the kinematic limit (simple case)
106  if (valX > kinLimit1 || valY > kinLimit2 ) {
107 // std::cout << "Setting error code in space 2" << std::endl;
108 // std::cout << "Values, kin limits are " << valX << " " << kinLimit1 << " " << valY << " " << kinLimit2 << std::endl;
109  m_corrMat->SetBinContent(binX,binY,JESUNC_ERROR_CODE);
110  } else
111  {
112  // Set the jet values, (pT, eta, phi, m)
113  // note that only pT and eta matter right now
115 
116  // Now determine the correlation for the two jets
117  m_corrMat->SetBinContent(binX,binY,getCorrelation(uncTool));
118  }
119  }
120  }
121 
122  // Clear up the objects we used
123  if (clearStore().isFailure())
124  return StatusCode::FAILURE;
125 
126  // Done
127  return StatusCode::SUCCESS;
128 }
129 
131 {
132  // Check for initialization and tool status
133  if (checkInitialization(uncTool).isFailure())
134  return StatusCode::FAILURE;
135  m_isInit = true;
136 
137  // Determine the center of mass energy for kinematic limits if possible
138  // If not possible, then ignores kinematic limits
139  // pt * cosh(eta) = sqrtS/2 for kinematic limit
140  const double sqrtS = uncTool.getSqrtS();
141  const double kinLimit1 = sqrtS > 0 ? acosh((sqrtS/2.) / m_fixedVal1) : 1e99;
142  const double kinLimit2 = sqrtS > 0 ? acosh((sqrtS/2.) / m_fixedVal2) : 1e99;
143 
144  // Build the correlation matrix
146 
147  // Prepare the objects for use
148  if (createStore().isFailure())
149  return StatusCode::FAILURE;
150 
151  // Set default values
152  if (setDefaultProperties(uncTool).isFailure())
153  return StatusCode::FAILURE;
154 
155  // Fill the correlation matrix
156  for (int binX = 1; binX <= m_corrMat->GetNbinsX(); ++binX)
157  {
158  // Set the jet values, (pT, eta, phi, m)
159  // note that only pT and eta matter right now
160  const double valX = m_corrMat->GetXaxis()->GetBinCenter(binX);
162 
163  for (int binY = 1; binY <= m_corrMat->GetNbinsY(); ++binY)
164  {
165  const double valY = m_corrMat->GetYaxis()->GetBinCenter(binY);
166 
167  // Check for the kinematic limit (simple case)
168  if (valX > kinLimit1 || valY > kinLimit2) {
169  m_corrMat->SetBinContent(binX,binY,JESUNC_ERROR_CODE);
170  } else
171  {
172  // Set the jet values, (pT, eta, phi, m)
173  // note that only pT and eta matter right now
175 
176  // Now determine the correlation for the two jets
177  m_corrMat->SetBinContent(binX,binY,getCorrelation(uncTool));
178  }
179  }
180  }
181 
182  // Clear up the objects we used
183  if (clearStore().isFailure())
184  return StatusCode::FAILURE;
185 
186  // Done
187  return StatusCode::SUCCESS;
188 }
189 
190 
192 // //
193 // Helper methods for building the matrices //
194 // //
196 
198 {
199  // Prevent double-initialization
200  if (m_isInit)
201  {
202  ATH_MSG_ERROR("CorrelationMatrix is already initialized: " << m_name.Data());
203  return StatusCode::FAILURE;
204  }
205 
206  // Warn about any non-four-vector terms which will be ignored
207  bool scalesFourVec = false;
208  for (size_t iComp = 0; iComp < uncTool.getNumComponents(); ++iComp)
209  if (!uncTool.getComponentScalesFourVec(iComp))
210  ATH_MSG_WARNING("CorrelationMatrix will ignore component which does not scale the jet four-vector: " << uncTool.getComponentName(iComp));
211  else
212  scalesFourVec = true;
213 
214  // If no components scale the four-vector, end now
215  if (!scalesFourVec)
216  {
217  ATH_MSG_ERROR("No components scale the four-vector for the CorrelationMatrix: " << m_name.Data());
218  return StatusCode::FAILURE;
219  }
220 
221  return StatusCode::SUCCESS;
222 }
223 
225 {
226  // Build a jet container and a pair of jets for us to manipulate later
227  m_jets = new xAOD::JetContainer();
228  m_jets->setStore(new xAOD::JetAuxContainer());
229  m_jets->push_back(new xAOD::Jet());
230  m_jets->push_back(new xAOD::Jet());
231  m_jet1 = m_jets->at(0);
232  m_jet2 = m_jets->at(1);
233 
234  // Build an EventInfo object for us to manipulate later
236  m_eInfos->setStore(new xAOD::EventInfoAuxContainer());
238  m_eInfo = m_eInfos->at(0);
239 
240  return StatusCode::SUCCESS;
241 }
242 
244 {
245  m_jet1 = nullptr;
246  m_jet2 = nullptr;
247  m_eInfo = nullptr;
248 
249  //for (size_t iJet = 0; iJet < m_jets->size(); ++iJet)
250  // JESUNC_SAFE_DELETE((*m_jets)[iJet]);
251  m_jets->clear();
253 
254  //for (size_t iInfo = 0; iInfo < m_eInfos->size(); ++iInfo)
255  // JESUNC_SAFE_DELETE((*m_eInfos)[iInfo]);
256  m_eInfos->clear();
258 
259  return StatusCode::SUCCESS;
260 }
261 
263 {
264 
265  // TODO make this part of a common file
266  static const SG::AuxElement::Accessor<int> Nsegments("GhostMuonSegmentCount");
267  static const SG::AuxElement::Accessor<char> IsBjet("IsBjet");
268  static const SG::AuxElement::Accessor<float> mu("averageInteractionsPerCrossing");
269  static const SG::AuxElement::Accessor<float> NPV("NPV");
270 
271  // 25 segments is about average for jets receiving a correction
272  // This is modulated by the probability of punchthrough
273  // TODO add punch-through Nsegments/etc dependence on probability
274  Nsegments(*m_jet1) = 0; //25;
275  Nsegments(*m_jet2) = 0; //25;
276  IsBjet(*m_jet1) = false;
277  IsBjet(*m_jet2) = false;
278 
279  float sigmaMu = 0;
280  float sigmaNPV = 0;
281 
282  const TString config = uncTool.getConfigFile();
283  if (config.Contains("_2011/"))
284  {
285  // Dag, night of Febuary 4/5, 2013
286  sigmaMu = 3.0;
287  sigmaNPV = 3.0;
288  }
289  else if (config.Contains("_2012/") || config.Contains("_2015/Prerec"))
290  {
291  // Craig Sawyer, Jan 22 2013
292  sigmaMu = 5.593*1.11;
293  sigmaNPV = 3.672;
294  }
295  else if (config.Contains("_2015"))
296  {
297  // Kate, Jan 31 2016
298  sigmaMu = 1.9;
299  sigmaNPV = 2.9;
300  }
301  else if (config.Contains("_2016"))
302  {
303  // Kate, Nov 2016
304  // via Eric Corrigan's pileup studies
305  // Scaling term taken from
306  // https://indico.cern.ch/event/437993/contributions/1925644/attachments/1138739/1630981/spagan_MuRescaling.pdf
307  sigmaMu = 5.35;
308  sigmaNPV = 3.49;
309  }
310  else
311  {
312  ATH_MSG_ERROR("Unexpected year for setPileupShiftsForYear in config: " << config.Data());
313  return StatusCode::FAILURE;
314  }
315 
316  mu(*m_eInfo) = uncTool.getRefMu()+sigmaMu;
317  NPV(*m_eInfo) = uncTool.getRefNPV()+sigmaNPV;
318 
319  return StatusCode::SUCCESS;
320 }
321 
322 TH2D* CorrelationMatrix::buildMatrix(const std::vector<double>& bins) const
323 {
324  TH2D* matrix = new TH2D(m_name,m_name,bins.size()-1,&bins[0],bins.size()-1,&bins[0]);
325  matrix->GetZaxis()->SetRangeUser(-1,1);
326  return matrix;
327 }
328 
330 {
331  return getCovariance(uncTool,m_jet1,m_jet2)/sqrt(getCovariance(uncTool,m_jet1,m_jet1)*getCovariance(uncTool,m_jet2,m_jet2));
332 }
333 
334 double CorrelationMatrix::getCovariance(const JetUncertaintiesTool& uncTool, const xAOD::Jet* jet1, const xAOD::Jet* jet2) const
335 {
336  double covariance = 0;
337  for (size_t iComp = 0; iComp < uncTool.getNumComponents(); ++iComp)
338  covariance += uncTool.getUncertainty(iComp,*jet1,*m_eInfo)*uncTool.getUncertainty(iComp,*jet2,*m_eInfo);
339 
340  return covariance;
341 }
342 
343 } // end jet namespace
JetUncertaintiesTool::getUncertainty
virtual double getUncertainty(size_t index, const xAOD::Jet &jet) const
Definition: JetUncertaintiesTool.cxx:1711
jet::CorrelationMatrix::m_fixedVal1
const double m_fixedVal1
Definition: CorrelationMatrix.h:58
jet::CorrelationMatrix::m_isInit
bool m_isInit
Definition: CorrelationMatrix.h:52
jet::CorrelationMatrix::m_numBins
const int m_numBins
Definition: CorrelationMatrix.h:55
jet::utils::getUniformBins
std::vector< double > getUniformBins(const size_t numBins, const double minVal, const double maxVal)
Definition: Reconstruction/Jet/JetUncertainties/Root/Helpers.cxx:164
JetUncertaintiesTool::getComponentScalesFourVec
virtual bool getComponentScalesFourVec(const size_t index) const
Definition: JetUncertaintiesTool.cxx:1579
jet::CorrelationMatrix::checkInitialization
StatusCode checkInitialization(const JetUncertaintiesTool &uncTool) const
Definition: CorrelationMatrix.cxx:197
jet::CorrelationMatrix::CorrelationMatrix
CorrelationMatrix(const TString &name, const int numBins, const double minVal, const double maxVal, const double fixedVal1, const double fixedVal2)
Definition: CorrelationMatrix.cxx:38
SG::Accessor
Helper class to provide type-safe access to aux data.
Definition: Control/AthContainers/AthContainers/Accessor.h:68
Data
@ Data
Definition: BaseObject.h:11
jet::utils::getLogBins
std::vector< double > getLogBins(const size_t numBins, const double minVal, const double maxVal)
Definition: Reconstruction/Jet/JetUncertainties/Root/Helpers.cxx:152
asg
Definition: DataHandleTestTool.h:28
jet::CorrelationMatrix::m_jet2
xAOD::Jet * m_jet2
Definition: CorrelationMatrix.h:68
xAOD::EventInfoContainer
EventInfoContainer_v1 EventInfoContainer
Define the latest version of the container.
Definition: EventInfoContainer.h:17
jet::CorrelationMatrix::getCovariance
double getCovariance(const JetUncertaintiesTool &uncTool, const xAOD::Jet *jet1, const xAOD::Jet *jet2) const
Definition: CorrelationMatrix.cxx:334
jet::CorrelationMatrix::initializeForEta
virtual StatusCode initializeForEta(const JetUncertaintiesTool &uncTool)
Definition: CorrelationMatrix.cxx:130
jet::CorrelationMatrix::createStore
StatusCode createStore()
Definition: CorrelationMatrix.cxx:224
JetUncertaintiesTool::getRefNPV
virtual float getRefNPV() const
Definition: JetUncertaintiesTool.cxx:1428
jet::CorrelationMatrix::initializeForPt
virtual StatusCode initializeForPt(const JetUncertaintiesTool &uncTool)
Definition: CorrelationMatrix.cxx:64
config
Definition: PhysicsAnalysis/AnalysisCommon/AssociationUtils/python/config.py:1
jet::CorrelationMatrix::m_maxVal
const double m_maxVal
Definition: CorrelationMatrix.h:57
Helpers.h
xAOD::Jet_v1::setJetP4
void setJetP4(const JetFourMom_t &p4)
Definition: Jet_v1.cxx:171
jet::CorrelationMatrix::m_corrMat
TH2D * m_corrMat
Definition: CorrelationMatrix.h:61
CorrelationMatrix.h
JESUNC_ERROR_CODE
#define JESUNC_ERROR_CODE
Definition: Reconstruction/Jet/JetUncertainties/JetUncertainties/Helpers.h:23
Trk::binY
@ binY
Definition: BinningType.h:48
xAOD::JetAuxContainer_v1
Temporary container used until we have I/O for AuxStoreInternal.
Definition: JetAuxContainer_v1.h:37
jet
Definition: JetCalibTools_PlotJESFactors.cxx:23
JESUNC_SAFE_DELETE
#define JESUNC_SAFE_DELETE(T)
Definition: Reconstruction/Jet/JetUncertainties/JetUncertainties/Helpers.h:25
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
jet::CorrelationMatrix::getCorrelation
double getCorrelation(const JetUncertaintiesTool &uncTool) const
Definition: CorrelationMatrix.cxx:329
jet::CorrelationMatrix::m_eInfos
xAOD::EventInfoContainer * m_eInfos
Definition: CorrelationMatrix.h:65
JetUncertaintiesTool::getConfigFile
virtual std::string getConfigFile() const
Definition: JetUncertaintiesTool.h:70
JetUncertaintiesTool
Definition: JetUncertaintiesTool.h:44
JetUncertaintiesTool::getNumComponents
virtual size_t getNumComponents() const
Definition: JetUncertaintiesTool.cxx:1464
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
jet::CorrelationMatrix::~CorrelationMatrix
virtual ~CorrelationMatrix()
Definition: CorrelationMatrix.cxx:58
jet::CorrelationMatrix::m_fixedVal2
const double m_fixedVal2
Definition: CorrelationMatrix.h:59
plotting.yearwise_luminosity_vs_mu.bins
bins
Definition: yearwise_luminosity_vs_mu.py:30
Trk::binX
@ binX
Definition: BinningType.h:47
DataVector::clear
void clear()
Erase all the elements in the collection.
xAOD::EventInfoAuxContainer_v1
Auxiliary information about the pileup events.
Definition: EventInfoAuxContainer_v1.h:31
xAOD::JetFourMom_t
ROOT::Math::LorentzVector< ROOT::Math::PtEtaPhiM4D< double > > JetFourMom_t
Base 4 Momentum type for Jet.
Definition: JetTypes.h:17
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
DataVector::push_back
value_type push_back(value_type pElem)
Add an element to the end of the collection.
xAOD::EventInfo_v1
Class describing the basic event information.
Definition: EventInfo_v1.h:43
JESUNC_NO_DEFAULT_CONSTRUCTOR
#define JESUNC_NO_DEFAULT_CONSTRUCTOR
Definition: Reconstruction/Jet/JetUncertainties/JetUncertainties/Helpers.h:24
JetUncertaintiesTool::getComponentName
virtual std::string getComponentName(const size_t index) const
Definition: JetUncertaintiesTool.cxx:1496
xAOD::Jet_v1
Class describing a jet.
Definition: Jet_v1.h:57
python.testIfMatch.matrix
matrix
Definition: testIfMatch.py:66
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
jet::CorrelationMatrix::m_minVal
const double m_minVal
Definition: CorrelationMatrix.h:56
jet::CorrelationMatrix::m_name
const TString m_name
Definition: CorrelationMatrix.h:53
jet::CorrelationMatrix::m_jets
xAOD::JetContainer * m_jets
Definition: CorrelationMatrix.h:64
jet::CorrelationMatrix::m_jet1
xAOD::Jet * m_jet1
Definition: CorrelationMatrix.h:67
jet::CorrelationMatrix::setDefaultProperties
StatusCode setDefaultProperties(const JetUncertaintiesTool &uncTool)
Definition: CorrelationMatrix.cxx:262
JetUncertaintiesTool::getRefMu
virtual float getRefMu() const
Definition: JetUncertaintiesTool.cxx:1413
xAOD::JetContainer
JetContainer_v1 JetContainer
Definition of the current "jet container version".
Definition: JetContainer.h:17
DataVector::at
const T * at(size_type n) const
Access an element, as an rvalue.
jet::CorrelationMatrix::buildMatrix
TH2D * buildMatrix(const std::vector< double > &bins) const
Definition: CorrelationMatrix.cxx:322
jet::CorrelationMatrix::clearStore
StatusCode clearStore()
Definition: CorrelationMatrix.cxx:243
JetUncertaintiesTool::getSqrtS
virtual float getSqrtS() const
Definition: JetUncertaintiesTool.cxx:1399
CaloNoise_fillDB.mu
mu
Definition: CaloNoise_fillDB.py:53
jet::CorrelationMatrix::m_eInfo
xAOD::EventInfo * m_eInfo
Definition: CorrelationMatrix.h:69