ATLAS Offline Software
Loading...
Searching...
No Matches
TElectronTestAlg.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3*/
4
6
9//
12//
13#include "xAODEgamma/Electron.h"
14//
15#include <cmath>
16#include <optional>
17
19
20 // input
21 ATH_CHECK(m_electronContainer.initialize());
22
23 // Decorations
24 m_pimpl = std::make_unique<Root::TElectronEfficiencyCorrectionTool>(
25 (this->name() + ".TElectronEfficiencyCorrection").c_str());
26 //
27 m_pimpl->msg().setLevel(this->msg().level());
28 //
29 std::string mapFileName = PathResolverFindCalibFile(m_mapFile);
32 std::string value = ElRecomFileHelpers::getValueByKey(mapFileName, key);
33 std::string filename = PathResolverFindCalibFile(value);
34 if (filename.empty()) {
35 ATH_MSG_ERROR("Could NOT resolve file name " << value);
36 return StatusCode::FAILURE;
37 } else {
38 ATH_MSG_INFO(" Using recommendations file = " << filename);
39 }
40 m_pimpl->addFileName(filename);
41 //
42 if (m_mode == Toys || m_mode == All) {
43 m_pimpl->bookCombToyMCScaleFactors(m_number_of_toys);
44 }
45 // We use 0 as failure ...
46 if (m_pimpl->initialize() == 0) {
48 "Could not initialize the TElectronEfficiencyCorrectionTool!");
49 return StatusCode::FAILURE;
50 }
51
52 // Auxiliary info
53 m_numberCorr = m_pimpl->getNSyst();
54 std::map<float, std::vector<float>> tmp;
55 m_pimpl->getNbins(tmp);
56 m_lowestEt = tmp.begin()->first;
57 m_doDetail = (m_mode != Toys && m_mode != Total);
58 m_doToys = (m_mode == Toys || m_mode == All);
59
60 // decorations if requested
61 const std::string baseName = m_electronContainer.key();
62 m_SF = baseName + ".EFF_" + key + "_SF";
63 m_TotalUp = baseName + ".EFF_" + key + "_Total__Up";
64 m_TotalDown = baseName + ".EFF_" + key + "_Total__Down";
65 m_uncorrUp = baseName + ".EFF_" + key + "_UnCorr__Up";
66 m_uncorrDown = baseName + ".EFF_" + key + "_UnCorr__Down";
67 m_HistIndex = baseName + ".EFF_" + key + "_HistIndex";
68 m_HistBin = baseName + ".EFF_" + key + "_HistBin";
69 m_corrUp = baseName + ".EFF_" + key + "_Corr__Up";
70 m_corrDown = baseName + ".EFF_" + key + "_Corr__Down";
71 m_toys = baseName + ".EFF_" + key + "__toys";
72
73 ATH_CHECK(m_SF.initialize(m_decorate));
74 ATH_CHECK(m_TotalUp.initialize(m_decorate));
75 ATH_CHECK(m_TotalDown.initialize(m_decorate));
82 ATH_CHECK(m_toys.initialize(m_decorate && m_doToys));
83
84 return StatusCode::SUCCESS;
85}
86
87StatusCode CP::TElectronTestAlg::execute(const EventContext& ctx) const {
88
89 //
90 unsigned int runNumber = 428648; // use a dummy default for testing
91
92 int dataType = m_dataType;
93 //
95
96 std::optional<SG::WriteDecorHandle<xAOD::ElectronContainer, double>> SF;
97 std::optional<SG::WriteDecorHandle<xAOD::ElectronContainer, double>> TotalUp;
98 std::optional<SG::WriteDecorHandle<xAOD::ElectronContainer, double>>
99 TotalDown;
100 std::optional<SG::WriteDecorHandle<xAOD::ElectronContainer, double>> uncorrUp;
101 std::optional<SG::WriteDecorHandle<xAOD::ElectronContainer, double>>
102 uncorrDown;
103 std::optional<SG::WriteDecorHandle<xAOD::ElectronContainer, int>> HistIndex;
104 std::optional<SG::WriteDecorHandle<xAOD::ElectronContainer, int>> HistBin;
105 std::optional<
107 corrUp;
108 std::optional<
110 corrDown;
111 std::optional<
113 toys;
114
115 if (m_decorate) {
116 SF.emplace(m_SF, ctx);
117 TotalUp.emplace(m_TotalUp, ctx);
118 TotalDown.emplace(m_TotalDown, ctx);
119 if (m_doDetail) {
120 uncorrUp.emplace(m_uncorrUp, ctx);
121 uncorrDown.emplace(m_uncorrDown, ctx);
122 HistIndex.emplace(m_HistIndex, ctx);
123 HistBin.emplace(m_HistBin, ctx);
124 corrUp.emplace(m_corrUp, ctx);
125 corrDown.emplace(m_corrDown, ctx);
126 }
127 if (m_doToys) {
128 toys.emplace(m_toys, ctx);
129 }
130 }
131
132 for (const xAOD::Electron* el : *electrons) {
133
135
136 // In case we fail
137 if (std::abs(el->eta()) > 2.469 || el->pt() < 10000) {
138 // if we decorate we need to set everything to dummy
139 // avoid decorating partially the collection ...
140 if (m_decorate) {
141 SF.value()(*el) = result.SF;
142 TotalUp.value()(*el) = result.SF + result.Total;
143 TotalDown.value()(*el) = result.SF - result.Total;
144 if (m_doDetail) {
145 uncorrUp.value()(*el) = result.SF + result.UnCorr;
146 uncorrDown.value()(*el) = result.SF - result.UnCorr;
147 HistIndex.value()(*el) = result.histIndex;
148 HistBin.value()(*el) = result.histBinNum;
149 corrUp.value()(*el) = result.Corr;
150 corrDown.value()(*el) = result.Corr;
151 }
152 if (m_doToys) {
153 toys.value()(*el) = result.toys;
154 }
155 }
156 continue;
157 }
158
159 // Some logic for validity
160 double cluster_eta(-9999.9);
161 const xAOD::CaloCluster* cluster = el->caloCluster();
162 if (!cluster) {
163 ATH_MSG_ERROR("ERROR no cluster associated to the Electron \n");
164 return StatusCode::FAILURE;
165 }
166 // we need to use different variables for central and forward electrons
167 static const SG::AuxElement::ConstAccessor<uint16_t> accAuthor("author");
168 if (accAuthor.isAvailable(*el) &&
170 cluster_eta = cluster->eta();
171 } else {
172 cluster_eta = cluster->etaBE(2);
173 }
174
175 // use et from cluster because it is immutable under syst variations of
176 // electron energy scale
177 const double energy = cluster->e();
178 const double parEta = el->eta();
179 const double coshEta = std::cosh(parEta);
180 double et = (coshEta != 0.) ? energy / coshEta : 0.;
181 // allow for a 5% margin at the lowest pT bin boundary
182 if (et < m_lowestEt) {
183 et = et * 1.05;
184 }
185
186 // do the calculations
187 const int status = m_pimpl->calculate(
188 static_cast<PATCore::ParticleDataType::DataType>(dataType), runNumber,
189 cluster_eta, et, /* in MeV */
190 result, (m_mode == Total) /* do work only for Total*/);
191 if (!status) {
192 ATH_MSG_ERROR("Something went wrong in the calculation");
193 return StatusCode::FAILURE;
194 }
195
196 // print outs
197 ATH_MSG_INFO("--------------------------------------------");
198 ATH_MSG_INFO("Electron pt : " << el->pt() << " eta " << el->eta());
199 ATH_MSG_INFO("SF = " << result.SF << " +- " << result.Total);
200 if (m_doDetail) {
201 ATH_MSG_INFO("SF = " << result.SF << " +- " << result.UnCorr
202 << " stat only ");
203 ATH_MSG_INFO("At histo " << result.histIndex << " at bin "
204 << result.histBinNum);
205 ATH_MSG_INFO("Num correlated " << result.Corr.size());
206 for (double res : result.Corr) {
207 ATH_MSG_INFO("+- " << res);
208 }
209 }
210 if (m_doToys) {
211 ATH_MSG_INFO("Run " << result.toys.size() << " Toys ");
212 for (double res : result.toys) {
213 ATH_MSG_INFO("+- " << res);
214 }
215 }
216 if (m_decorate) {
217 SF.value()(*el) = result.SF;
218 TotalUp.value()(*el) = result.SF + result.Total;
219 TotalDown.value()(*el) = result.SF - result.Total;
220 if (m_doDetail) {
221 uncorrUp.value()(*el) = result.SF + result.UnCorr;
222 uncorrDown.value()(*el) = result.SF - result.UnCorr;
223 HistIndex.value()(*el) = result.histIndex;
224 HistBin.value()(*el) = result.histBinNum;
225 const size_t corrsize = result.Corr.size();
226 std::vector<double> up(corrsize, 0);
227 std::vector<double> down(corrsize, 0);
228 for (size_t i = 0; i < corrsize; ++i) {
229 up[i] = result.SF + result.Corr[i];
230 down[i] = result.SF - result.Corr[i];
231 }
232 corrUp.value()(*el) = std::move(up);
233 corrDown.value()(*el) = std::move(down);
234 }
235 if (m_doToys) {
236 toys.value()(*el) = std::move(result.toys);
237 }
238 }
239
240 } // end of loop over electrons
241 return StatusCode::SUCCESS;
242}
243
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
Handle class for reading from StoreGate.
Handle class for adding a decoration to an object.
std::pair< std::vector< unsigned int >, bool > res
std::string PathResolverFindCalibFile(const std::string &logical_file_name)
Gaudi::Property< bool > m_decorate
Gaudi::Property< int > m_dataType
SG::ReadHandleKey< xAOD::ElectronContainer > m_electronContainer
Input collection name.
SG::WriteDecorHandleKey< xAOD::ElectronContainer > m_toys
SG::WriteDecorHandleKey< xAOD::ElectronContainer > m_HistIndex
Gaudi::Property< std::string > m_idKey
SG::WriteDecorHandleKey< xAOD::ElectronContainer > m_TotalUp
std::unique_ptr< Root::TElectronEfficiencyCorrectionTool > m_pimpl
Lowest Et for the reccomendations.
SG::WriteDecorHandleKey< xAOD::ElectronContainer > m_corrDown
double m_lowestEt
Number of Correlated syst.
Gaudi::Property< int > m_mode
Gaudi::Property< std::string > m_isoKey
Gaudi::Property< std::string > m_triggerKey
SG::WriteDecorHandleKey< xAOD::ElectronContainer > m_TotalDown
virtual StatusCode execute(const EventContext &ctx) const override final
SG::WriteDecorHandleKey< xAOD::ElectronContainer > m_uncorrDown
SG::WriteDecorHandleKey< xAOD::ElectronContainer > m_HistBin
virtual StatusCode initialize() override final
SG::WriteDecorHandleKey< xAOD::ElectronContainer > m_SF
Gaudi::Property< std::string > m_recoKey
Gaudi::Property< int > m_number_of_toys
Gaudi::Property< std::string > m_mapFile
Label for the collection.
SG::WriteDecorHandleKey< xAOD::ElectronContainer > m_uncorrUp
SG::WriteDecorHandleKey< xAOD::ElectronContainer > m_corrUp
SG::ConstAccessor< T, ALLOC > ConstAccessor
Definition AuxElement.h:569
bool isAvailable(const ELT &e) const
Test to see if this variable exists in the store.
Handle class for adding a decoration to an object.
virtual double eta() const
The pseudorapidity ( ) of the particle.
virtual double e() const
The total energy of the particle.
float etaBE(const unsigned layer) const
Get the eta in one layer of the EM Calo.
std::string getValueByKey(const std::string &mapFile, const std::string &key)
std::string convertToOneKey(const std::string &recokey, const std::string &idkey, const std::string &isokey, const std::string &trigkey)
const uint16_t AuthorFwdElectron
Electron reconstructed by the Forward cluster-based algorithm.
Definition EgammaDefs.h:30
CaloCluster_v1 CaloCluster
Define the latest version of the calorimeter cluster class.
Electron_v1 Electron
Definition of the current "egamma version".