ATLAS Offline Software
Loading...
Searching...
No Matches
InSituCalibStep.cxx
Go to the documentation of this file.
1
2
3/*
4 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
5*/
6
7// InSituCalibStep.cxx
8// Implementation file for class InSituCalibStep
10
14
15InSituCalibStep::InSituCalibStep(const std::string& name)
16 : asg::AsgTool( name ){ }
17
18
19
21// Public methods:
23
25 ATH_MSG_DEBUG ("Initializing " << name() );
26 ATH_MSG_DEBUG("Reading from " << m_jetInScale << " and writing to " << m_jetOutScale);
27 // Initialise ReadHandle(s)
28 ATH_CHECK( m_evtInfoKey.initialize() );
29
30 if ( m_isMC )
31 ATH_MSG_WARNING("InSituCalibStep::calibrate : Running over MC, will not calibrate unless expert option CalibrateMC is set to true");
32
33 ATH_CHECK( m_histTool_EtaInter.retrieve() );
34 ATH_CHECK( m_histTool_Abs.retrieve() );
35
36 if( m_histTool_Abs.size() != m_histTool_EtaInter.size() )
37 return StatusCode::FAILURE;
38 unsigned int nHist = m_histTool_Abs.size();
39
40 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
41 // This multiplies the histogram and matches the behaviour in the old InsituDataCorrection step.
42 // This block of code should be removed entirely (along with the combineCalibration() function) once we have validated the simpler multiplication in getInsituCorr()
43 for(unsigned int i = 0 ; i < nHist ; i++){
44 JetHelper::HistoInputBase* histoTool_EtaInter = dynamic_cast<JetHelper::HistoInputBase*>( &(*m_histTool_EtaInter[i]) );
45 JetHelper::HistoInputBase* histoTool_Abs = dynamic_cast<JetHelper::HistoInputBase*>( &(*m_histTool_Abs[i]) );
46 TH1D *h_Abs = dynamic_cast<TH1D*>( &histoTool_Abs->getHistogram());
47 TH2D *h_EtaInter = dynamic_cast<TH2D*>( &histoTool_EtaInter->getHistogram());
48 // combine relative and absolute calibration (central + eta-intercalibration)
49 std::unique_ptr<const TH2> h_insituCorr = combineCalibration(h_EtaInter, h_Abs);
50 m_insituCorr_vec.push_back(std::move(h_insituCorr) );
51 // combined histogram boundaries
52 double etaMax = m_insituCorr_vec[i] -> GetYaxis() -> GetBinUpEdge(m_insituCorr_vec[i] -> GetYaxis() -> GetLast());
53 double etaMin = m_insituCorr_vec[i] -> GetYaxis() -> GetBinLowEdge(m_insituCorr_vec[i] -> GetYaxis() -> GetFirst());
54 double ptMax = m_insituCorr_vec[i] -> GetXaxis() -> GetBinUpEdge(m_insituCorr_vec[i] -> GetXaxis() -> GetLast());
55 double ptMin = m_insituCorr_vec[i] -> GetXaxis() -> GetBinLowEdge(m_insituCorr_vec[i] -> GetXaxis() -> GetFirst());
56 m_etaMax_vec.push_back(etaMax);
57 m_etaMin_vec.push_back(etaMin);
58 m_ptMax_vec.push_back(ptMax);
59 m_ptMin_vec.push_back(ptMin);
60 }
61 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
62
63 return StatusCode::SUCCESS;
64}
65
66
68
69 ATH_MSG_DEBUG("calibrating jet collection.");
70 // Retrieve EventInfo object, for time-dependent calibration and isMC flag
71 unsigned int rNumber = 0;
72 if (retrieveEventInfo(rNumber).isFailure())
73 return StatusCode::FAILURE;
74 if( (bool)(m_isMC) && !( (bool)(m_CalibrateMC)) ) //no calibration
75 return StatusCode::SUCCESS;
76 unsigned int runNumber = static_cast<unsigned int>(rNumber+0.5);
77 // Pick up the correct time-dependent histogram
78 unsigned int periodInd = 9999;
79 for(unsigned int i=0; i<m_RunNumBoundaries.size()-1; i++){
80 unsigned int firstRun = m_RunNumBoundaries[i] + 1.5;
81 unsigned int lastRun = m_RunNumBoundaries[i+1]+0.5;
82 if (firstRun<=runNumber && runNumber <= lastRun ){
83 periodInd = i;
84 break;
85 }
86 }
87
88 if (periodInd==9999){ // periodInd could not be set
89 ATH_MSG_WARNING("No calibration found for run number "<<runNumber);
90 }
91
92 if( periodInd >= m_histTool_Abs.size() ) //outside run numbers, return no calibration
93 return StatusCode::SUCCESS;
94
96 for (xAOD::Jet* jet : jets){
97 const xAOD::JetFourMom_t jetStartP4 = jet->getAttribute<xAOD::JetFourMom_t>(m_jetInScale);
98 jet->setJetP4(jetStartP4);
99 double s = 1.0; // scale
100 if (getInsituCorr(*jet, jc, periodInd, s).isFailure())
101 return StatusCode::FAILURE;
102 xAOD::JetFourMom_t calibP4=jet->jetP4();
103 calibP4 = calibP4 * s;
104 // Set the output scale
105 jet->setAttribute<xAOD::JetFourMom_t>(m_jetOutScale,calibP4);
106 jet->setJetP4(calibP4);
107 }
108 return StatusCode::SUCCESS;
109}
110StatusCode InSituCalibStep::retrieveEventInfo(unsigned int &r) const {
111
112 const xAOD::EventInfo * eventObj = nullptr;
113 static std::atomic<unsigned int> eventInfoWarnings = 0;
115 if ( rhEvtInfo.isValid() ) {
116 eventObj = rhEvtInfo.cptr();
117 r = eventObj->runNumber();
118 } else {
119 ++eventInfoWarnings;
120 if ( eventInfoWarnings < 20 )
121 ATH_MSG_ERROR(" InSituCalibStep::calibrate : Failed to retrieve event information.");
122 return StatusCode::SUCCESS; //error is recoverable, so return SUCCESS
123 }
124 return StatusCode::SUCCESS;
125}
126
127StatusCode InSituCalibStep::getInsituCorr(const xAOD::Jet& jet, JetHelper::JetContext& jc, unsigned int periodIndex, double &scale) const{
128
129 scale = 1.0 ;
130
131 // Multiply histograms on-the-fly - this will not perfectly match the old InsituDataCorrection step, but is simpler and arguably more correct.
132 // Note that JetToolHelpers handles the protection against values outside the histogram range and the interpolation.
133 const double R_abs = m_histTool_Abs[periodIndex]->getValue(jet,jc);
134 double c_rel = m_histTool_EtaInter[periodIndex]->getValue(jet,jc);
135 scale = c_rel/R_abs;
136
137 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
138 // This uses the histogram created in initialize, which matches the behaviour in the old InsituDataCorrection step.
139 // However this does not make best use of the available JetToolHelpers functionality.
140 // This block of code can be removed once we have validated the simpler on-the-fly multiplication above.
141 double v1 = m_vartool1->getValue(jet,jc);
142 double v2 = m_vartool2->getValue(jet,jc);
143 //protection against values outside the histogram range - snap back to the lowest/highest bin edge
144 if ( v1 <= m_ptMin_vec[periodIndex] ) v1 = m_ptMin_vec[periodIndex] + 1e-6;
145 else if ( v1 >= m_ptMax_vec[periodIndex] ) v1 = m_ptMax_vec[periodIndex] - 1e-6;
146
147 if (v2 <= m_etaMin_vec[periodIndex]) v2 = 1e-6 + m_etaMin_vec[periodIndex];
148 else if (v2 >= m_etaMax_vec[periodIndex]) v2 = m_etaMax_vec[periodIndex] - 1e-6;
149
150 double scale_OG = m_insituCorr_vec[periodIndex] -> Interpolate(v1,v2);
151
152 double scale_diff_perc = 100*abs(scale_OG - scale)/scale_OG;
153 if (scale_diff_perc > 0.01){ ATH_MSG_WARNING("Simplified InsituScale calculation differs by "<<scale_diff_perc<<"% --> original scale = "<<scale_OG<<", simplified scale = "<<scale);}
154
155 // m_useOriginalHistCombination=false by default, so by default this value will not be used.
156 if (m_useOriginalHistCombination){ scale = scale_OG;}
157 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
158
159 return StatusCode::SUCCESS;
160}
161
162
163std::unique_ptr<const TH2> InSituCalibStep::combineCalibration(const TH2* h2d, const TH1* h) {
164 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
165 // This function can be removed once we have validated the simpler on-the-fly multiplication in getInsituCorr()
166 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
167 std::unique_ptr<TH2> prod(static_cast<TH2*>( h2d->Clone() ) );
168 for (int xi=1;xi<=prod->GetNbinsX();xi++) {
169 double pt=prod->GetXaxis()->GetBinCenter(xi);
170 const double R_abs=h->Interpolate(pt); // Rdata/RMC for the absolute scale
171 const double inv_R_abs = 1. / R_abs;
172 //printf("pT = %7.1f GeV, abs calib: %.4f\n",pt,abs);
173 for (int yi=1;yi<=prod->GetNbinsY();yi++) {
174 double c_rel = h2d->GetBinContent(xi,yi); // 1/Rrel = RMC/Rdata
175 prod->SetBinContent(xi,yi,c_rel*inv_R_abs);
176 }
177 }
178 return prod;
179}
180
181
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
Handle class for reading a decoration on an object.
Header file for AthHistogramAlgorithm.
std::unique_ptr< const TH2 > combineCalibration(const TH2 *h2d, const TH1 *h)
ToolHandleArray< JetHelper::IVarTool > m_histTool_Abs
Gaudi::Property< std::string > m_jetInScale
Gaudi::Property< bool > m_CalibrateMC
StatusCode retrieveEventInfo(unsigned int &r) const
virtual StatusCode initialize() override
Dummy implementation of the initialisation function.
virtual StatusCode calibrate(xAOD::JetContainer &) const override
Apply calibration to a jet container.
std::vector< double > m_ptMax_vec
std::vector< double > m_ptMin_vec
std::vector< std::unique_ptr< const TH2 > > m_insituCorr_vec
Gaudi::Property< std::string > m_jetOutScale
std::vector< double > m_etaMin_vec
ToolHandleArray< JetHelper::IVarTool > m_histTool_EtaInter
ToolHandle< JetHelper::IVarTool > m_vartool2
StatusCode getInsituCorr(const xAOD::Jet &jet, JetHelper::JetContext &jc, unsigned int periodIndex, double &scale) const
std::vector< double > m_etaMax_vec
SG::ReadHandleKey< xAOD::EventInfo > m_evtInfoKey
ToolHandle< JetHelper::IVarTool > m_vartool1
InSituCalibStep(const std::string &name="InSituCalibStep")
Constructor with parameters:
Gaudi::Property< bool > m_useOriginalHistCombination
Gaudi::Property< std::vector< unsigned int > > m_RunNumBoundaries
Gaudi::Property< bool > m_isMC
Class HistoInputBase This class implement common function used by HistoInput1D and HistoInput2D.
TH1 & getHistogram()
Returns the underlying histogram.
Class JetContext Designed to read AOD information related to the event, N vertices,...
Definition JetContext.h:24
virtual bool isValid() override final
Can the handle be successfully dereferenced?
const_pointer_type cptr()
Dereference the pointer.
AsgTool(const std::string &name)
Constructor specifying the tool instance's name.
Definition AsgTool.cxx:58
uint32_t runNumber() const
The current event's run number.
int r
Definition globals.cxx:22
Jet_v1 Jet
Definition of the current "jet version".
EventInfo_v1 EventInfo
Definition of the latest event info 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