ATLAS Offline Software
Loading...
Searching...
No Matches
SmearingCalibStep.cxx
Go to the documentation of this file.
1
2
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
57TRandom3* 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
68StatusCode SmearingCalibStep::getSigmaSmear(xAOD::Jet& jet, const JetHelper::JetContext & jc, double& sigmaSmear) const
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
104StatusCode SmearingCalibStep::getNominalResolutionData(const xAOD::Jet& jet, const JetHelper::JetContext& jc, double& resolution) const
105{
106 resolution = m_histToolData->getValue(jet, jc);
107 return StatusCode::SUCCESS;
108}
109
110StatusCode SmearingCalibStep::getNominalResolutionMC(const xAOD::Jet& jet, const JetHelper::JetContext& jc, double& resolution) const
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
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}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_FATAL(x)
#define ATH_MSG_DEBUG(x)
Class JetContext Designed to read AOD information related to the event, N vertices,...
Definition JetContext.h:24
ToolHandle< JetHelper::IVarTool > m_histToolMC
virtual StatusCode getNominalResolutionData(const xAOD::Jet &jet, const JetHelper::JetContext &jc, double &resolution) const override
TRandom3 * getTLSRandomGen(unsigned long seed) const
Gaudi::Property< std::string > m_jetOutScale
ToolHandle< JetHelper::IVarTool > m_histToolData
SmearingCalibStep(const std::string &name="SmearingCalibStep")
Gaudi::Property< std::string > m_smearType
virtual StatusCode calibrate(xAOD::JetContainer &) const override
Apply calibration to a jet container.
virtual StatusCode getNominalResolutionMC(const xAOD::Jet &jet, const JetHelper::JetContext &jc, double &resolution) const override
Gaudi::Property< std::string > m_jetInScale
boost::thread_specific_ptr< TRandom3 > m_rand_tls
virtual StatusCode initialize() override
Dummy implementation of the initialisation function.
StatusCode getSigmaSmear(xAOD::Jet &jet, const JetHelper::JetContext &jc, double &sigmaSmear) const
AsgTool(const std::string &name)
Constructor specifying the tool instance's name.
Definition AsgTool.cxx:58
Jet_v1 Jet
Definition of the current "jet version".
JetContainer_v1 JetContainer
Definition of the current "jet container version".
ROOT::Math::LorentzVector< ROOT::Math::PtEtaPhiM4D< double > > JetFourMom_t
Base 4 Momentum type for Jet.
Definition JetTypes.h:17