ATLAS Offline Software
SmearingCalibStep.cxx
Go to the documentation of this file.
1 
3 /*
4  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
5 */
6 
7 // SmearingCalibStep.cxx
8 // Implementation file for class SmearingCalibStep
9 // Author: Ben Hodkinson <ben.hodkinson@cern.ch>
11 
13 
14 
16  : asg::AsgTool( name ){ }
17 
18 
20 
21  ATH_MSG_DEBUG ("Initializing " << name() );
22 
23  ATH_MSG_INFO("Reading from " << m_jetStartScale << " and writing to " << m_jetOutScale);
24 
25  // Check the specified smearing type
26  if (m_smearType == "")
27  {
28  ATH_MSG_FATAL("No jet smearing type was specified. Aborting.");
29  return StatusCode::FAILURE;
30  }
31  else if (m_smearType=="pt")
32  {
34  }
35  else if (m_smearType=="mass")
36  {
38  }
39  else if (m_smearType=="FourVec")
40  {
42  }
43  else
44  {
45  ATH_MSG_FATAL("Unrecognized jet smearing type: " << m_smearType << "--> the available options are 'pt', 'mass' or 'FourVec'");
46  return StatusCode::FAILURE;
47  }
48 
49 
50  // Retrieve the histograms
51  ATH_CHECK( m_histToolMC.retrieve() );
52  ATH_CHECK( m_histToolData.retrieve() );
53 
54  return StatusCode::SUCCESS;
55 }
56 
57 TRandom3* SmearingCalibStep::getTLSRandomGen(unsigned long seed) const
58 {
59  TRandom3* random = m_rand_tls.get();
60  if (!random) {
61  random = new TRandom3();
62  m_rand_tls.reset(random);
63  }
64  random->SetSeed(seed);
65  return random;
66 }
67 
69 {
70  /*
71  Nominal jet smearing
72  If sigma_data > sigma_MC, then we want to smear MC to match data
73  If sigma_data < sigma_MC, then we do not want to smear data to match MC
74  The second case is instead the source of an uncertainty (see JetUncertainties)
75 
76  To make MC agree with data:
77  if (sigma_data > sigma_MC) then sigma_smear^2 = sigma_data^2 - sigma_MC^2
78  if (sigma_data < sigma_MC) then do nothing
79  Smearing using a Gaussian centered at 1 and with a width of sigma_smear
80 
81  Note that data is never smeared, as blocked in JetCalibrationTool.cxx
82  --> TODO: JetCalibrationTool.cxx throws an error if you try to run smearing calibration on data - include this in new tool
83  */
84 
85  double resolutionMC = 0;
86  if (getNominalResolutionMC(jet, jc, resolutionMC).isFailure())
87  return StatusCode::FAILURE;
88 
89  double resolutionData = 0;
90  if (getNominalResolutionData(jet, jc, resolutionData).isFailure())
91  return StatusCode::FAILURE;
92 
93  // Nominal smearing only if data resolution is larger than MC resolution
94  // This is because we want to smear the MC to match the data
95  // if MC is larger than data, don't make the nominal data worse, so smear is 0
96  if (resolutionMC < resolutionData)
97  sigmaSmear = sqrt(resolutionData*resolutionData - resolutionMC*resolutionMC);
98  else
99  sigmaSmear = 0;
100 
101  return StatusCode::SUCCESS;
102 }
103 
105 {
106  resolution = m_histToolData->getValue(jet, jc);
107  return StatusCode::SUCCESS;
108 }
109 
111 {
112  resolution = m_histToolMC->getValue(jet, jc);
113  return StatusCode::SUCCESS;
114 }
115 
117  ATH_MSG_DEBUG("Applying smearing calibration step to jet collection.");
118 
120 
121  for(const auto jet: jets){
122 
123  const xAOD::JetFourMom_t jetStartP4 = jet->jetP4();
124 
125  double sigmaSmear = 0;
126  if (getSigmaSmear(*jet, jc, sigmaSmear).isFailure())
127  return StatusCode::FAILURE;
128 
129  // Set the random seed deterministically using jet phi
130  unsigned long seed = static_cast<unsigned long>(1.e5*fabs(jet->phi()));
131  // SetSeed(0) uses the clock, so avoid this
132  if(seed == 0) seed = 45583453; // arbitrary number which the seed couldn't otherwise be
133  TRandom3* rng = getTLSRandomGen(seed);
134 
135  // Get the Gaussian-distributed random number
136  // Force this to be a positive value
137  // Negative values should be extraordinarily rare, but they do exist
138  double smearingFactor = -1;
139  while (smearingFactor < 0)
140  smearingFactor = rng->Gaus(1.,sigmaSmear);
141 
142  xAOD::JetFourMom_t calibP4 = jetStartP4;
143 
144  switch (m_smearTypeClass)
145  {
146  case SmearType::Pt:
147  calibP4 = xAOD::JetFourMom_t(jet->pt()*smearingFactor,jet->eta(),jet->phi(),jet->m());
148  break;
149 
150  case SmearType::Mass:
151  calibP4 = xAOD::JetFourMom_t(jet->pt(),jet->eta(),jet->phi(),smearingFactor*jet->m());
152  break;
153 
154  case SmearType::FourVec:
155  calibP4 = xAOD::JetFourMom_t(jet->pt()*smearingFactor,jet->eta(),jet->phi(),jet->m()*smearingFactor);
156  break;
157 
158  default:
159  // We should never reach this, it was checked during initialization
160  ATH_MSG_ERROR("Cannot smear the jet, the smearing type was not set");
161  return StatusCode::FAILURE;
162  }
163 
164  // Set the output scale
165  jet->setAttribute<xAOD::JetFourMom_t>(m_jetOutScale,calibP4);
166  jet->setJetP4(calibP4);
167  }
168 
169  return StatusCode::SUCCESS;
170 }
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
SmearingCalibStep::SmearType::Mass
@ Mass
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
JetHelper::JetContext
Class JetContext Designed to read AOD information related to the event, N vertices,...
Definition: JetContext.h:24
SmearingCalibStep::initialize
virtual StatusCode initialize() override
Dummy implementation of the initialisation function.
Definition: SmearingCalibStep.cxx:19
asg
Definition: DataHandleTestTool.h:28
SmearingCalibStep::getNominalResolutionMC
virtual StatusCode getNominalResolutionMC(const xAOD::Jet &jet, const JetHelper::JetContext &jc, double &resolution) const override
Definition: SmearingCalibStep.cxx:110
SmearingCalibStep.h
D3PDTest::rng
uint32_t rng()
Definition: FillerAlg.cxx:40
Dedxcorrection::resolution
double resolution[nGasTypes][nParametersResolution]
Definition: TRT_ToT_Corrections.h:46
SmearingCalibStep::m_histToolData
ToolHandle< JetHelper::IVarTool > m_histToolData
Definition: SmearingCalibStep.h:52
SmearingCalibStep::m_smearTypeClass
SmearType m_smearTypeClass
Definition: SmearingCalibStep.h:67
SmearingCalibStep::m_jetStartScale
Gaudi::Property< std::string > m_jetStartScale
Definition: SmearingCalibStep.h:45
jet
Definition: JetCalibTools_PlotJESFactors.cxx:23
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
SmearingCalibStep::getNominalResolutionData
virtual StatusCode getNominalResolutionData(const xAOD::Jet &jet, const JetHelper::JetContext &jc, double &resolution) const override
Definition: SmearingCalibStep.cxx:104
SmearingCalibStep::SmearType::Pt
@ Pt
SmearingCalibStep::m_rand_tls
boost::thread_specific_ptr< TRandom3 > m_rand_tls
Definition: SmearingCalibStep.h:69
SmearingCalibStep::getTLSRandomGen
TRandom3 * getTLSRandomGen(unsigned long seed) const
Definition: SmearingCalibStep.cxx:57
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
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
DataVector
Derived DataVector<T>.
Definition: DataVector.h:581
SmearingCalibStep::m_histToolMC
ToolHandle< JetHelper::IVarTool > m_histToolMC
Definition: SmearingCalibStep.h:51
xAOD::JetFourMom_t
ROOT::Math::LorentzVector< ROOT::Math::PtEtaPhiM4D< double > > JetFourMom_t
Base 4 Momentum type for Jet.
Definition: JetTypes.h:17
SmearingCalibStep::getSigmaSmear
StatusCode getSigmaSmear(xAOD::Jet &jet, JetHelper::JetContext jc, double &sigmaSmear) const
Definition: SmearingCalibStep.cxx:68
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
SmearingCalibStep::calibrate
virtual StatusCode calibrate(xAOD::JetContainer &) const override
Apply calibration to a jet container.
Definition: SmearingCalibStep.cxx:116
xAOD::Jet_v1
Class describing a jet.
Definition: Jet_v1.h:57
SmearingCalibStep::SmearType::FourVec
@ FourVec
defineDB.jets
list jets
Definition: JetTagCalibration/share/defineDB.py:24
SmearingCalibStep::m_smearType
Gaudi::Property< std::string > m_smearType
Definition: SmearingCalibStep.h:47
SmearingCalibStep::m_jetOutScale
Gaudi::Property< std::string > m_jetOutScale
Definition: SmearingCalibStep.h:46
SmearingCalibStep::SmearingCalibStep
SmearingCalibStep(const std::string &name="SmearingCalibStep")
Definition: SmearingCalibStep.cxx:15