ATLAS Offline Software
Loading...
Searching...
No Matches
InSituCalibStep.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// 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_INFO("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 for(unsigned int i = 0 ; i < nHist ; i++){
41 JetHelper::HistoInputBase* histoTool_EtaInter = dynamic_cast<JetHelper::HistoInputBase*>( &(*m_histTool_EtaInter[i]) );
42 JetHelper::HistoInputBase* histoTool_Abs = dynamic_cast<JetHelper::HistoInputBase*>( &(*m_histTool_Abs[i]) );
43 TH1D *h_Abs = dynamic_cast<TH1D*>( &histoTool_Abs->getHistogram());
44 TH2D *h_EtaInter = dynamic_cast<TH2D*>( &histoTool_EtaInter->getHistogram());
45 // combine relative and absolute calibration (central + eta-intercalibration)
46 std::unique_ptr<const TH2> h_insituCorr = combineCalibration(h_EtaInter, h_Abs);
47 m_insituCorr_vec.push_back(std::move(h_insituCorr) );
48 // combined histogram boundaries
49 double etaMax = m_insituCorr_vec[i] -> GetYaxis() -> GetBinUpEdge(m_insituCorr_vec[i] -> GetYaxis() -> GetLast());
50 double etaMin = m_insituCorr_vec[i] -> GetYaxis() -> GetBinLowEdge(m_insituCorr_vec[i] -> GetYaxis() -> GetFirst());
51 double ptMax = m_insituCorr_vec[i] -> GetXaxis() -> GetBinUpEdge(m_insituCorr_vec[i] -> GetXaxis() -> GetLast());
52 double ptMin = m_insituCorr_vec[i] -> GetXaxis() -> GetBinLowEdge(m_insituCorr_vec[i] -> GetXaxis() -> GetFirst());
53 m_etaMax_vec.push_back(etaMax);
54 m_etaMin_vec.push_back(etaMin);
55 m_ptMax_vec.push_back(ptMax);
56 m_ptMin_vec.push_back(ptMin);
57 }
58 return StatusCode::SUCCESS;
59}
60
61
63
64 ATH_MSG_DEBUG("calibrating jet collection.");
65 // Retrieve EventInfo object, for time-dependent calibration and isMC flag
66 unsigned int rNumber = 0;
67 if (retrieveEventInfo(rNumber).isFailure())
68 return StatusCode::FAILURE;
69 if( (bool)(m_isMC) && !( (bool)(m_CalibrateMC)) ) //no calibration
70 return StatusCode::SUCCESS;
71
72 unsigned int runNumber = static_cast<unsigned int>(rNumber+0.5);
73 // Pick up the correct time-dependent histogram
74 unsigned int periodInd = 9999;
75 unsigned int currentInd = 0;
76 for(const auto &p: m_RunNumBoundaries){
77 unsigned int firstRun = static_cast<unsigned int>( p + 1.5);
78 unsigned int lastRun = static_cast<unsigned int>( *(&p + 1) +0.5);
79 if(lastRun > 0 && (runNumber < firstRun || runNumber > lastRun))
80 continue;
81 periodInd = currentInd;
82 currentInd++;
83 }
84 if( periodInd >= m_histTool_Abs.size() ) //outside run numbers, return no calibration
85 return StatusCode::SUCCESS;
87 for (xAOD::Jet* jet : jets){
88 const xAOD::JetFourMom_t jetStartP4 = jet->getAttribute<xAOD::JetFourMom_t>(m_jetInScale);
89 jet->setJetP4(jetStartP4);
90 double s = 1.0; // scale
91 if (getInsituCorr(*jet, jc, periodInd, s).isFailure())
92 return StatusCode::FAILURE;
93 xAOD::JetFourMom_t calibP4=jet->jetP4();
94 calibP4 = calibP4 * s;
95 // Set the output scale
96 jet->setAttribute<xAOD::JetFourMom_t>(m_jetOutScale,calibP4);
97 jet->setJetP4(calibP4);
98 }
99 return StatusCode::SUCCESS;
100}
101StatusCode InSituCalibStep::retrieveEventInfo(unsigned int &r) const {
102
103 const xAOD::EventInfo * eventObj = nullptr;
104 static std::atomic<unsigned int> eventInfoWarnings = 0;
106 if ( rhEvtInfo.isValid() ) {
107 eventObj = rhEvtInfo.cptr();
108 r = eventObj->runNumber();
109 } else {
110 ++eventInfoWarnings;
111 if ( eventInfoWarnings < 20 )
112 ATH_MSG_ERROR(" InSituCalibStep::calibrate : Failed to retrieve event information.");
113 return StatusCode::SUCCESS; //error is recoverable, so return SUCCESS
114 }
115 return StatusCode::SUCCESS;
116}
117
118StatusCode InSituCalibStep::getInsituCorr(const xAOD::Jet& jet, JetHelper::JetContext& jc, unsigned int periodIndex, double &scale) const{
119 scale = 1.0 ;
120 double v1 = m_vartool1->getValue(jet,jc);
121 double v2 = m_vartool2->getValue(jet,jc);
122 //protection against values outside the histogram range for pT, snap back to the lowest/highest bin edge
123 if ( v1 <= m_ptMin_vec[periodIndex] ) v1 = m_ptMin_vec[periodIndex] + 1e-6;
124 else if ( v1 >= m_ptMax_vec[periodIndex] ) v1 = m_ptMax_vec[periodIndex] - 1e-6;
125 // do not calibrate if eta is outside the validity range
126 if( v2 >= m_etaMax_vec[periodIndex] ) scale = 1.0;
127 else if( v2 <= m_etaMin_vec[periodIndex] ) scale = 1.0;
128 else scale = m_insituCorr_vec[periodIndex] -> Interpolate(v1,v2);
129 return StatusCode::SUCCESS;
130}
131
132
133std::unique_ptr<const TH2> InSituCalibStep::combineCalibration(const TH2* h2d, const TH1* h) {
134 std::unique_ptr<TH2> prod(static_cast<TH2*>( h2d->Clone() ) );
135 for (int xi=1;xi<=prod->GetNbinsX();xi++) {
136 double pt=prod->GetXaxis()->GetBinCenter(xi);
137 const double R_abs=h->Interpolate(pt); // Rdata/RMC for the absolute scale
138 const double inv_R_abs = 1. / R_abs;
139 //printf("pT = %7.1f GeV, abs calib: %.4f\n",pt,abs);
140 for (int yi=1;yi<=prod->GetNbinsY();yi++) {
141 double c_rel = h2d->GetBinContent(xi,yi); // 1/Rrel = RMC/Rdata
142 prod->SetBinContent(xi,yi,c_rel*inv_R_abs);
143 }
144 }
145 return prod;
146}
147
148
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(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< 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