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_DEBUG("Reading from " << m_jetInScale << " 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->getAttribute<xAOD::JetFourMom_t>(m_jetInScale);
124  jet->setJetP4(jetStartP4);
125 
126  double sigmaSmear = 0;
127  if (getSigmaSmear(*jet, jc, sigmaSmear).isFailure())
128  return StatusCode::FAILURE;
129 
130  // Set the random seed deterministically using jet phi
131  unsigned long seed = static_cast<unsigned long>(1.e5*fabs(jet->phi()));
132  // SetSeed(0) uses the clock, so avoid this
133  if(seed == 0) seed = 45583453; // arbitrary number which the seed couldn't otherwise be
134  TRandom3* rng = getTLSRandomGen(seed);
135 
136  // Get the Gaussian-distributed random number
137  // Force this to be a positive value
138  // Negative values should be extraordinarily rare, but they do exist
139  double smearingFactor = -1;
140  while (smearingFactor < 0)
141  smearingFactor = rng->Gaus(1.,sigmaSmear);
142 
143  xAOD::JetFourMom_t calibP4 = jetStartP4;
144 
145  switch (m_smearTypeClass)
146  {
147  case SmearType::Pt:
148  calibP4 = xAOD::JetFourMom_t(jet->pt()*smearingFactor,jet->eta(),jet->phi(),jet->m());
149  break;
150 
151  case SmearType::Mass:
152  calibP4 = xAOD::JetFourMom_t(jet->pt(),jet->eta(),jet->phi(),smearingFactor*jet->m());
153  break;
154 
155  case SmearType::FourVec:
156  calibP4 = xAOD::JetFourMom_t(jet->pt()*smearingFactor,jet->eta(),jet->phi(),jet->m()*smearingFactor);
157  break;
158 
159  default:
160  // We should never reach this, it was checked during initialization
161  ATH_MSG_ERROR("Cannot smear the jet, the smearing type was not set");
162  return StatusCode::FAILURE;
163  }
164 
165  // Set the output scale
166  jet->setAttribute<xAOD::JetFourMom_t>(m_jetOutScale,calibP4);
167  jet->setJetP4(calibP4);
168  }
169 
170  return StatusCode::SUCCESS;
171 }
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
SmearingCalibStep::SmearType::Mass
@ Mass
SmearingCalibStep::getSigmaSmear
StatusCode getSigmaSmear(xAOD::Jet &jet, const JetHelper::JetContext &jc, double &sigmaSmear) const
Definition: SmearingCalibStep.cxx:68
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
defineDB.jets
jets
Definition: JetTagCalibration/share/defineDB.py:24
SmearingCalibStep::getNominalResolutionMC
virtual StatusCode getNominalResolutionMC(const xAOD::Jet &jet, const JetHelper::JetContext &jc, double &resolution) const override
Definition: SmearingCalibStep.cxx:110
SmearingCalibStep.h
SmearingCalibStep::m_jetInScale
Gaudi::Property< std::string > m_jetInScale
Definition: SmearingCalibStep.h:45
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:50
SmearingCalibStep::m_smearTypeClass
SmearType m_smearTypeClass
Definition: SmearingCalibStep.h:65
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:67
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:794
SmearingCalibStep::m_histToolMC
ToolHandle< JetHelper::IVarTool > m_histToolMC
Definition: SmearingCalibStep.h:49
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:240
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
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