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 
132  // Photon decorations
133 
134  if (!m_SGKey_photons.key().empty()) {
135 
136  // Retrieve photon container
138  const xAOD::EgammaContainer* importedPhotons = photonContainer.ptr();
139 
140  // Setup vectors of photon decorations
141  std::vector<SG::WriteDecorHandle<xAOD::EgammaContainer, float>>
142  decorations_E;
143  std::vector<SG::WriteDecorHandle<xAOD::EgammaContainer, float>>
144  decorations_rnoW;
145  std::vector<SG::WriteDecorHandle<xAOD::EgammaContainer, char>>
146  decorations_nCells;
147  int i(0);
148  for (const auto& kv : m_gainNames) {
149  for (const auto layer : m_layers) {
150  std::pair<int, int> key(kv.first, layer);
151  decorations_E.emplace_back(
152 
153  m_SGKey_photons_decorations[i * 3], ctx);
154  decorations_rnoW.emplace_back(
155 
156  m_SGKey_photons_decorations[i * 3 + 1], ctx);
157  decorations_nCells.emplace_back(
158 
159  m_SGKey_photons_decorations[i * 3 + 2], ctx);
160  i++;
161  }
162  }
163 
164  // Decorate photons
165  for (const auto* photon : *importedPhotons) {
167  decorateObject(photon);
168  i = 0;
169  for (const auto& kv : m_gainNames) {
170  for (const auto layer : m_layers) {
171  std::pair<int, int> key(kv.first, layer);
172  decorations_E[i](*photon) = res.E[key];
173  decorations_rnoW[i](*photon) =
174  res.EnoW[key] != 0 ? res.E[key]/res.EnoW[key] : 1;
175  decorations_nCells[i](*photon) = res.nCells[key];
176  i++;
177  }
178  }
179  }
180  }
181 
182  // Electron decorations
183 
184  if (!m_SGKey_electrons.key().empty()) {
185 
186  // Retrieve electron container
188  ctx);
189  const xAOD::EgammaContainer* importedElectrons = electronContainer.ptr();
190 
191  // Setup vectors of electron decorations
192  std::vector<SG::WriteDecorHandle<xAOD::EgammaContainer, float>>
193  decorations_E;
194  std::vector<SG::WriteDecorHandle<xAOD::EgammaContainer, float>>
195  decorations_rnoW;
196  std::vector<SG::WriteDecorHandle<xAOD::EgammaContainer, char>>
197  decorations_nCells;
198  int i(0);
199  for (const auto& kv : m_gainNames) {
200  for (const auto layer : m_layers) {
201  std::pair<int, int> key(kv.first, layer);
202  decorations_E.emplace_back(
203 
204  m_SGKey_electrons_decorations[i * 3], ctx);
205  decorations_rnoW.emplace_back(
206 
207  m_SGKey_electrons_decorations[i * 3 + 1], ctx);
208  decorations_nCells.emplace_back(
209 
210  m_SGKey_electrons_decorations[i * 3 + 2], ctx);
211  i++;
212  }
213  }
214 
215  // Decorate electrons
216  for (const auto* electron : *importedElectrons) {
218  decorateObject(electron);
219  i = 0;
220  for (const auto& kv : m_gainNames) {
221  for (const auto layer : m_layers) {
222  std::pair<int, int> key(kv.first, layer);
223  decorations_E[i](*electron) = res.E[key];
224  decorations_rnoW[i](*electron) =
225  res.EnoW[key] != 0 ? res.E[key]/res.EnoW[key] : 1;
226  decorations_nCells[i](*electron) = res.nCells[key];
227  i++;
228  }
229  }
230  }
231  }
232 
233  return StatusCode::SUCCESS;
234 }
235 
238  const xAOD::Egamma*& egamma) const
239 {
240 
241  // Compute energy and number of cells per gain per layer
242  // Set the initial values to 0 (needed?)
244  for (const auto& kv : m_names_E) {
245  result.E[kv.first] = 0.;
246  result.EnoW[kv.first] = 0.;
247  result.nCells[kv.first] = 0;
248  }
249 
250  // Skip the computation for missing cell links (like topo-seeded photons)
251  // but decorate anyway
252  const CaloClusterCellLink* cellLinks =
253  egamma->caloCluster() ? egamma->caloCluster()->getCellLinks() : nullptr;
254  if (cellLinks) {
256  itE = cellLinks->end();
257  for (; it != itE; ++it) {
258  const CaloCell *cell = *it;
259  if (!cell)
260  continue;
261  std::pair<int, int> key(static_cast<int>(cell->gain()), getLayer(cell));
262  // Increment the corresponding entry (not important if it is not
263  // initialised)
264  double weight = it.weight();
265  result.E[key] += cell->energy()*weight;
266  result.EnoW[key] += cell->energy();
267  result.nCells[key]++;
268  }
269  }
270 
271  return result;
272 }
273 
274 int
276 {
277  int sampling =
278  (cell && cell->caloDDE() ? cell->caloDDE()->getSampling() : -1);
279  if (sampling == CaloSampling::PreSamplerB ||
280  sampling == CaloSampling::PreSamplerE)
281  return 0;
282  if (sampling == CaloSampling::EMB1 || sampling == CaloSampling::EME1)
283  return 1;
284  if (sampling == CaloSampling::EMB2 || sampling == CaloSampling::EME2)
285  return 2;
286  if (sampling == CaloSampling::EMB3 || sampling == CaloSampling::EME3)
287  return 3;
288  return -1;
289 }
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
MuonR4::to_string
std::string to_string(const SectorProjector proj)
Definition: MsTrackSeeder.cxx:66
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:275
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:795
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
DerivationFramework::GainDecorator::addBranches
virtual StatusCode addBranches(const EventContext &ctx) const
Definition: GainDecorator.cxx:129
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
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:237
constants.EME2
int EME2
Definition: Calorimeter/CaloClusterCorrection/python/constants.py:56
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37