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
10
12
13
15 : asg::AsgTool( name ){ }
16
17
19
20 ATH_MSG_DEBUG ("Initializing " << name() );
21
22 ATH_MSG_DEBUG("Reading from " << m_jetInScale << " and writing to " << m_jetOutScale);
23
24 // Check the specified smearing type
25 if (m_smearType == "")
26 {
27 ATH_MSG_FATAL("No jet smearing type was specified. Aborting.");
28 return StatusCode::FAILURE;
29 }
30 else if (m_smearType=="pt")
31 {
33 }
34 else if (m_smearType=="mass")
35 {
37 }
38 else if (m_smearType=="FourVec")
39 {
41 }
42 else
43 {
44 ATH_MSG_FATAL("Unrecognized jet smearing type: " << m_smearType << "--> the available options are 'pt', 'mass' or 'FourVec'");
45 return StatusCode::FAILURE;
46 }
47
48
49 // Retrieve the histograms
50 ATH_CHECK( m_histToolMC.retrieve() );
51 ATH_CHECK( m_histToolData.retrieve() );
52
53 return StatusCode::SUCCESS;
54}
55
56TRandom3* SmearingCalibStep::getTLSRandomGen(unsigned long seed) const
57{
58 TRandom3* random = m_rand_tls.get();
59 if (!random) {
60 random = new TRandom3();
61 m_rand_tls.reset(random);
62 }
63 random->SetSeed(seed);
64 return random;
65}
66
67StatusCode SmearingCalibStep::getSigmaSmear(xAOD::Jet& jet, const JetHelper::JetContext & jc, double& sigmaSmear) const
68{
69 /*
70 Nominal jet smearing
71 If sigma_data > sigma_MC, then we want to smear MC to match data
72 If sigma_data < sigma_MC, then we do not want to smear data to match MC
73 The second case is instead the source of an uncertainty (see JetUncertainties)
74
75 To make MC agree with data:
76 if (sigma_data > sigma_MC) then sigma_smear^2 = sigma_data^2 - sigma_MC^2
77 if (sigma_data < sigma_MC) then do nothing
78 Smearing using a Gaussian centered at 1 and with a width of sigma_smear
79
80 Note that data is never smeared, as blocked in JetCalibrationTool.cxx
81 --> TODO: JetCalibrationTool.cxx throws an error if you try to run smearing calibration on data - include this in new tool
82 */
83
84 double resolutionMC = 0;
85 if (getNominalResolutionMC(jet, jc, resolutionMC).isFailure())
86 return StatusCode::FAILURE;
87
88 double resolutionData = 0;
89 if (getNominalResolutionData(jet, jc, resolutionData).isFailure())
90 return StatusCode::FAILURE;
91
92 // Nominal smearing only if data resolution is larger than MC resolution
93 // This is because we want to smear the MC to match the data
94 // if MC is larger than data, don't make the nominal data worse, so smear is 0
95 if (resolutionMC < resolutionData)
96 sigmaSmear = sqrt(resolutionData*resolutionData - resolutionMC*resolutionMC);
97 else
98 sigmaSmear = 0;
99
100 return StatusCode::SUCCESS;
101}
102
103StatusCode SmearingCalibStep::getNominalResolutionData(const xAOD::Jet& jet, const JetHelper::JetContext& jc, double& resolution) const
104{
105 resolution = m_histToolData->getValue(jet, jc);
106 return StatusCode::SUCCESS;
107}
108
109StatusCode SmearingCalibStep::getNominalResolutionMC(const xAOD::Jet& jet, const JetHelper::JetContext& jc, double& resolution) const
110{
111 resolution = m_histToolMC->getValue(jet, jc);
112 return StatusCode::SUCCESS;
113}
114
116 ATH_MSG_DEBUG("Applying smearing calibration step to jet collection.");
117
119
120 for(const auto jet: jets){
121
122 const xAOD::JetFourMom_t jetStartP4 = jet->getAttribute<xAOD::JetFourMom_t>(m_jetInScale);
123 jet->setJetP4(jetStartP4);
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
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}
#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:23
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