ATLAS Offline Software
GainDecorator.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 // GainDecorator.cxx, (c) ATLAS Detector software
8 // Author: Simone Mazza (simone.mazza@mi.infn.it),
9 // Bruno Lenzi,
10 // Giovanni Marchiori (giovanni.marchiori@cern.ch)
11 // Decorate egamma objects with the energy and number of cells per layer per
12 // gain
13 
15 
16 #include <regex>
17 #include <string>
18 #include <vector>
19 namespace {}
20 
21 // Constructor
23  const std::string& n,
24  const IInterface* p)
25  : base_class(t, n, p)
26 {
27  declareProperty("decoration_pattern",
28  m_decorationPattern = "{info}_Lr{layer}_{gain}G");
29  declareProperty("gain_names",
30  m_gainNames = { { CaloGain::LARHIGHGAIN, "Hi" },
31  { CaloGain::LARMEDIUMGAIN, "Med" },
32  { CaloGain::LARLOWGAIN, "Low" } });
33  declareProperty("layers", m_layers = { 0, 1, 2, 3 });
34 
35  // Define the names for the decorations
36  for (const auto& kv : m_gainNames)
37  for (const auto layer : m_layers) {
38  std::string name = m_decorationPattern;
39  name.replace(name.find("{layer}"),
40  std::string("{layer}").size(),
42  name.replace(
43  name.find("{gain}"), std::string("{gain}").size(), kv.second);
44  std::string name_E(name), name_rnoW(name), name_nCells(name);
45  name_E.replace(name_E.find("{info}"), std::string("{info}").size(), "E");
46  name_rnoW.replace(name_rnoW.find("{info}"), std::string("{info}").size(), "rnoW");
47  name_nCells.replace(
48  name_nCells.find("{info}"), std::string("{info}").size(), "nCells");
49 
50  std::pair<int, int> key(kv.first, layer);
51  m_names_E[key] = name_E;
52  m_names_rnoW[key] = name_rnoW;
53  m_names_nCells[key] = name_nCells;
54  }
55 
56  for (const auto& kv : m_names_E) {
57  ATH_MSG_DEBUG("Decorating (layer, gain): " << kv.first << " " << kv.second);
58  }
59  for (const auto& kv : m_names_nCells) {
60  ATH_MSG_DEBUG("Decorating (layer, gain): " << kv.first << " " << kv.second);
61  }
62 }
63 
64 // Destructor
66 
67 // Athena initialize and finalize
70 {
71  ATH_MSG_VERBOSE("initialize() ...");
72 
73  if (m_SGKey_photons.key().empty() && m_SGKey_electrons.key().empty()) {
74  ATH_MSG_FATAL("No e-gamma collection provided for thinning. At least one "
75  "egamma collection (photons/electrons) must be provided!");
76  return StatusCode::FAILURE;
77  }
78 
79  if (!m_SGKey_electrons.key().empty()) {
80  ATH_MSG_DEBUG("Using " << m_SGKey_electrons << " for electrons");
81  ATH_CHECK(m_SGKey_electrons.initialize());
82 
83  const std::string containerKey = m_SGKey_electrons.key();
84  for (const auto& kv : m_gainNames) {
85  for (const auto layer : m_layers) {
86  std::pair<int, int> key(kv.first, layer);
87  m_SGKey_electrons_decorations.emplace_back(containerKey + "." +
88  m_names_E[key]);
89  m_SGKey_electrons_decorations.emplace_back(containerKey + "." +
90  m_names_rnoW[key]);
91  m_SGKey_electrons_decorations.emplace_back(containerKey + "." +
92  m_names_nCells[key]);
93  }
94  }
95  ATH_CHECK(m_SGKey_electrons_decorations.initialize());
96  }
97 
98  if (!m_SGKey_photons.key().empty()) {
99  ATH_MSG_DEBUG("Using " << m_SGKey_photons << " for photons");
100  ATH_CHECK(m_SGKey_photons.initialize());
101 
102  const std::string containerKey = m_SGKey_photons.key();
103  for (const auto& kv : m_gainNames) {
104  for (const auto layer : m_layers) {
105  std::pair<int, int> key(kv.first, layer);
106  m_SGKey_photons_decorations.emplace_back(containerKey + "." +
107  m_names_E[key]);
108  m_SGKey_photons_decorations.emplace_back(containerKey + "." +
109  m_names_rnoW[key]);
110  m_SGKey_photons_decorations.emplace_back(containerKey + "." +
111  m_names_nCells[key]);
112  }
113  }
114  ATH_CHECK(m_SGKey_photons_decorations.initialize());
115  }
116 
117  return StatusCode::SUCCESS;
118 }
119 
122 {
123 
124  return StatusCode::SUCCESS;
125 }
126 
127 // The decoration itself
130 {
131  const EventContext& ctx = Gaudi::Hive::currentContext();
132 
133  // Photon decorations
134 
135  if (!m_SGKey_photons.key().empty()) {
136 
137  // Retrieve photon container
139  const xAOD::EgammaContainer* importedPhotons = photonContainer.ptr();
140 
141  // Setup vectors of photon decorations
142  std::vector<SG::WriteDecorHandle<xAOD::EgammaContainer, float>>
143  decorations_E;
144  std::vector<SG::WriteDecorHandle<xAOD::EgammaContainer, float>>
145  decorations_rnoW;
146  std::vector<SG::WriteDecorHandle<xAOD::EgammaContainer, char>>
147  decorations_nCells;
148  int i(0);
149  for (const auto& kv : m_gainNames) {
150  for (const auto layer : m_layers) {
151  std::pair<int, int> key(kv.first, layer);
152  decorations_E.emplace_back(
153 
154  m_SGKey_photons_decorations[i * 3], ctx);
155  decorations_rnoW.emplace_back(
156 
157  m_SGKey_photons_decorations[i * 3 + 1], ctx);
158  decorations_nCells.emplace_back(
159 
160  m_SGKey_photons_decorations[i * 3 + 2], ctx);
161  i++;
162  }
163  }
164 
165  // Decorate photons
166  for (const auto* photon : *importedPhotons) {
168  decorateObject(photon);
169  i = 0;
170  for (const auto& kv : m_gainNames) {
171  for (const auto layer : m_layers) {
172  std::pair<int, int> key(kv.first, layer);
173  decorations_E[i](*photon) = res.E[key];
174  decorations_rnoW[i](*photon) =
175  res.EnoW[key] != 0 ? res.E[key]/res.EnoW[key] : 1;
176  decorations_nCells[i](*photon) = res.nCells[key];
177  i++;
178  }
179  }
180  }
181  }
182 
183  // Electron decorations
184 
185  if (!m_SGKey_electrons.key().empty()) {
186 
187  // Retrieve electron container
189  ctx);
190  const xAOD::EgammaContainer* importedElectrons = electronContainer.ptr();
191 
192  // Setup vectors of electron decorations
193  std::vector<SG::WriteDecorHandle<xAOD::EgammaContainer, float>>
194  decorations_E;
195  std::vector<SG::WriteDecorHandle<xAOD::EgammaContainer, float>>
196  decorations_rnoW;
197  std::vector<SG::WriteDecorHandle<xAOD::EgammaContainer, char>>
198  decorations_nCells;
199  int i(0);
200  for (const auto& kv : m_gainNames) {
201  for (const auto layer : m_layers) {
202  std::pair<int, int> key(kv.first, layer);
203  decorations_E.emplace_back(
204 
205  m_SGKey_electrons_decorations[i * 3], ctx);
206  decorations_rnoW.emplace_back(
207 
208  m_SGKey_electrons_decorations[i * 3 + 1], ctx);
209  decorations_nCells.emplace_back(
210 
211  m_SGKey_electrons_decorations[i * 3 + 2], ctx);
212  i++;
213  }
214  }
215 
216  // Decorate electrons
217  for (const auto* electron : *importedElectrons) {
219  decorateObject(electron);
220  i = 0;
221  for (const auto& kv : m_gainNames) {
222  for (const auto layer : m_layers) {
223  std::pair<int, int> key(kv.first, layer);
224  decorations_E[i](*electron) = res.E[key];
225  decorations_rnoW[i](*electron) =
226  res.EnoW[key] != 0 ? res.E[key]/res.EnoW[key] : 1;
227  decorations_nCells[i](*electron) = res.nCells[key];
228  i++;
229  }
230  }
231  }
232  }
233 
234  return StatusCode::SUCCESS;
235 }
236 
239  const xAOD::Egamma*& egamma) const
240 {
241 
242  // Compute energy and number of cells per gain per layer
243  // Set the initial values to 0 (needed?)
245  for (const auto& kv : m_names_E) {
246  result.E[kv.first] = 0.;
247  result.EnoW[kv.first] = 0.;
248  result.nCells[kv.first] = 0;
249  }
250 
251  // Skip the computation for missing cell links (like topo-seeded photons)
252  // but decorate anyway
253  const CaloClusterCellLink* cellLinks =
254  egamma->caloCluster() ? egamma->caloCluster()->getCellLinks() : nullptr;
255  if (cellLinks) {
257  itE = cellLinks->end();
258  for (; it != itE; ++it) {
259  const CaloCell *cell = *it;
260  if (!cell)
261  continue;
262  std::pair<int, int> key(static_cast<int>(cell->gain()), getLayer(cell));
263  // Increment the corresponding entry (not important if it is not
264  // initialised)
265  double weight = it.weight();
266  result.E[key] += cell->energy()*weight;
267  result.EnoW[key] += cell->energy();
268  result.nCells[key]++;
269  }
270  }
271 
272  return result;
273 }
274 
275 int
277 {
278  int sampling =
279  (cell && cell->caloDDE() ? cell->caloDDE()->getSampling() : -1);
280  if (sampling == CaloSampling::PreSamplerB ||
281  sampling == CaloSampling::PreSamplerE)
282  return 0;
283  if (sampling == CaloSampling::EMB1 || sampling == CaloSampling::EME1)
284  return 1;
285  if (sampling == CaloSampling::EMB2 || sampling == CaloSampling::EME2)
286  return 2;
287  if (sampling == CaloSampling::EMB3 || sampling == CaloSampling::EME3)
288  return 3;
289  return -1;
290 }
electronContainer
xAOD::ElectronContainer * electronContainer
Definition: TrigGlobEffCorrValidation.cxx:187
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
get_generator_info.result
result
Definition: get_generator_info.py:21
constants.EMB1
int EMB1
Definition: Calorimeter/CaloClusterCorrection/python/constants.py:53
ReadCellNoiseFromCool.cell
cell
Definition: ReadCellNoiseFromCool.py:53
DerivationFramework::GainDecorator::finalize
StatusCode finalize()
Definition: GainDecorator.cxx:121
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:67
DerivationFramework::GainDecorator::GainDecorator
GainDecorator(const std::string &t, const std::string &n, const IInterface *p)
Definition: GainDecorator.cxx:22
skel.it
it
Definition: skel.GENtoEVGEN.py:407
xAOD::Egamma_v1
Definition: Egamma_v1.h:56
DerivationFramework::GainDecorator::m_names_E
std::map< std::pair< int, int >, std::string > m_names_E
Definition: GainDecorator.h:62
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
module_driven_slicing.getLayer
def getLayer(trk, hit_idx)
Definition: module_driven_slicing.py:171
TruthTest.itE
itE
Definition: TruthTest.py:25
egamma
Definition: egamma.h:58
dqt_zlumi_pandas.weight
int weight
Definition: dqt_zlumi_pandas.py:190
DerivationFramework::GainDecorator::calculation
Definition: GainDecorator.h:40
DerivationFramework::GainDecorator::m_names_rnoW
std::map< std::pair< int, int >, std::string > m_names_rnoW
Definition: GainDecorator.h:63
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:209
DerivationFramework::GainDecorator::getLayer
static int getLayer(const CaloCell *cell)
Definition: GainDecorator.cxx:276
DerivationFramework::GainDecorator::m_decorationPattern
std::string m_decorationPattern
Definition: GainDecorator.h:57
constants.EMB2
int EMB2
Definition: Calorimeter/CaloClusterCorrection/python/constants.py:54
lumiFormat.i
int i
Definition: lumiFormat.py:85
beamspotman.n
n
Definition: beamspotman.py:727
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
TRT::Hit::layer
@ layer
Definition: HitInfo.h:79
res
std::pair< std::vector< unsigned int >, bool > res
Definition: JetGroupProductTest.cxx:11
constants.EME1
int EME1
Definition: Calorimeter/CaloClusterCorrection/python/constants.py:55
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
DerivationFramework::GainDecorator::~GainDecorator
~GainDecorator()
DataVector
Derived DataVector<T>.
Definition: DataVector.h:794
photonContainer
xAOD::PhotonContainer * photonContainer
Definition: TrigGlobEffCorrValidation.cxx:189
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:240
DerivationFramework::GainDecorator::m_names_nCells
std::map< std::pair< int, int >, std::string > m_names_nCells
Definition: GainDecorator.h:64
DerivationFramework::GainDecorator::m_gainNames
std::map< int, std::string > m_gainNames
Definition: GainDecorator.h:58
CaloCell_ID_FCS::EME3
@ EME3
Definition: FastCaloSim_CaloCell_ID.h:26
ActsTrk::to_string
std::string to_string(const DetectorType &type)
Definition: GeometryDefs.h:34
CaloGain::LARHIGHGAIN
@ LARHIGHGAIN
Definition: CaloGain.h:18
CaloGain::LARMEDIUMGAIN
@ LARMEDIUMGAIN
Definition: CaloGain.h:18
xAOD::photon
@ photon
Definition: TrackingPrimitives.h:200
CaloCell
Data object for each calorimeter readout cell.
Definition: CaloCell.h:57
CaloCell_ID_FCS::PreSamplerE
@ PreSamplerE
Definition: FastCaloSim_CaloCell_ID.h:23
CaloCell_ID_FCS::PreSamplerB
@ PreSamplerB
Definition: FastCaloSim_CaloCell_ID.h:19
DerivationFramework::GainDecorator::addBranches
virtual StatusCode addBranches() const
Definition: GainDecorator.cxx:129
GainDecorator.h
xAOD::EgammaParameters::electron
@ electron
Definition: EgammaEnums.h:18
DerivationFramework::GainDecorator::initialize
StatusCode initialize()
Definition: GainDecorator.cxx:69
CaloGain::LARLOWGAIN
@ LARLOWGAIN
Definition: CaloGain.h:18
DerivationFramework::GainDecorator::m_layers
std::vector< unsigned int > m_layers
Definition: GainDecorator.h:59
CaloCell_ID_FCS::EMB3
@ EMB3
Definition: FastCaloSim_CaloCell_ID.h:22
DerivationFramework::GainDecorator::decorateObject
calculation decorateObject(const xAOD::Egamma *&egamma) const
Definition: GainDecorator.cxx:238
constants.EME2
int EME2
Definition: Calorimeter/CaloClusterCorrection/python/constants.py:56
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37