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  : AthAlgTool(t, n, p)
26 {
27  declareInterface<DerivationFramework::IAugmentationTool>(this);
28  declareProperty("decoration_pattern",
29  m_decorationPattern = "{info}_Lr{layer}_{gain}G");
30  declareProperty("gain_names",
31  m_gainNames = { { CaloGain::LARHIGHGAIN, "Hi" },
32  { CaloGain::LARMEDIUMGAIN, "Med" },
33  { CaloGain::LARLOWGAIN, "Low" } });
34  declareProperty("layers", m_layers = { 0, 1, 2, 3 });
35 
36  // Define the names for the decorations
37  for (const auto& kv : m_gainNames)
38  for (const auto layer : m_layers) {
39  std::string name = m_decorationPattern;
40  name.replace(name.find("{layer}"),
41  std::string("{layer}").size(),
43  name.replace(
44  name.find("{gain}"), std::string("{gain}").size(), kv.second);
45  std::string name_E(name), name_rnoW(name), name_nCells(name);
46  name_E.replace(name_E.find("{info}"), std::string("{info}").size(), "E");
47  name_rnoW.replace(name_rnoW.find("{info}"), std::string("{info}").size(), "rnoW");
48  name_nCells.replace(
49  name_nCells.find("{info}"), std::string("{info}").size(), "nCells");
50 
51  std::pair<int, int> key(kv.first, layer);
52  m_names_E[key] = name_E;
53  m_names_rnoW[key] = name_rnoW;
54  m_names_nCells[key] = name_nCells;
55  }
56 
57  for (const auto& kv : m_names_E) {
58  ATH_MSG_DEBUG("Decorating (layer, gain): " << kv.first << " " << kv.second);
59  }
60  for (const auto& kv : m_names_nCells) {
61  ATH_MSG_DEBUG("Decorating (layer, gain): " << kv.first << " " << kv.second);
62  }
63 }
64 
65 // Destructor
67 
68 // Athena initialize and finalize
71 {
72  ATH_MSG_VERBOSE("initialize() ...");
73 
74  if (m_SGKey_photons.key().empty() && m_SGKey_electrons.key().empty()) {
75  ATH_MSG_FATAL("No e-gamma collection provided for thinning. At least one "
76  "egamma collection (photons/electrons) must be provided!");
77  return StatusCode::FAILURE;
78  }
79 
80  if (!m_SGKey_electrons.key().empty()) {
81  ATH_MSG_DEBUG("Using " << m_SGKey_electrons << " for electrons");
82  ATH_CHECK(m_SGKey_electrons.initialize());
83 
84  const std::string containerKey = m_SGKey_electrons.key();
85  for (const auto& kv : m_gainNames) {
86  for (const auto layer : m_layers) {
87  std::pair<int, int> key(kv.first, layer);
88  m_SGKey_electrons_decorations.emplace_back(containerKey + "." +
89  m_names_E[key]);
90  m_SGKey_electrons_decorations.emplace_back(containerKey + "." +
91  m_names_rnoW[key]);
92  m_SGKey_electrons_decorations.emplace_back(containerKey + "." +
93  m_names_nCells[key]);
94  }
95  }
96  ATH_CHECK(m_SGKey_electrons_decorations.initialize());
97  }
98 
99  if (!m_SGKey_photons.key().empty()) {
100  ATH_MSG_DEBUG("Using " << m_SGKey_photons << " for photons");
101  ATH_CHECK(m_SGKey_photons.initialize());
102 
103  const std::string containerKey = m_SGKey_photons.key();
104  for (const auto& kv : m_gainNames) {
105  for (const auto layer : m_layers) {
106  std::pair<int, int> key(kv.first, layer);
107  m_SGKey_photons_decorations.emplace_back(containerKey + "." +
108  m_names_E[key]);
109  m_SGKey_photons_decorations.emplace_back(containerKey + "." +
110  m_names_rnoW[key]);
111  m_SGKey_photons_decorations.emplace_back(containerKey + "." +
112  m_names_nCells[key]);
113  }
114  }
115  ATH_CHECK(m_SGKey_photons_decorations.initialize());
116  }
117 
118  return StatusCode::SUCCESS;
119 }
120 
123 {
124 
125  return StatusCode::SUCCESS;
126 }
127 
128 // The decoration itself
131 {
132  const EventContext& ctx = Gaudi::Hive::currentContext();
133 
134  // Photon decorations
135 
136  if (!m_SGKey_photons.key().empty()) {
137 
138  // Retrieve photon container
140  const xAOD::EgammaContainer* importedPhotons = photonContainer.ptr();
141 
142  // Setup vectors of photon decorations
143  std::vector<SG::WriteDecorHandle<xAOD::EgammaContainer, float>>
144  decorations_E;
145  std::vector<SG::WriteDecorHandle<xAOD::EgammaContainer, float>>
146  decorations_rnoW;
147  std::vector<SG::WriteDecorHandle<xAOD::EgammaContainer, char>>
148  decorations_nCells;
149  int i(0);
150  for (const auto& kv : m_gainNames) {
151  for (const auto layer : m_layers) {
152  std::pair<int, int> key(kv.first, layer);
153  decorations_E.emplace_back(
154 
155  m_SGKey_photons_decorations[i * 3], ctx);
156  decorations_rnoW.emplace_back(
157 
158  m_SGKey_photons_decorations[i * 3 + 1], ctx);
159  decorations_nCells.emplace_back(
160 
161  m_SGKey_photons_decorations[i * 3 + 2], ctx);
162  i++;
163  }
164  }
165 
166  // Decorate photons
167  for (const auto* photon : *importedPhotons) {
169  decorateObject(photon);
170  i = 0;
171  for (const auto& kv : m_gainNames) {
172  for (const auto layer : m_layers) {
173  std::pair<int, int> key(kv.first, layer);
174  decorations_E[i](*photon) = res.E[key];
175  decorations_rnoW[i](*photon) =
176  res.EnoW[key] != 0 ? res.E[key]/res.EnoW[key] : 1;
177  decorations_nCells[i](*photon) = res.nCells[key];
178  i++;
179  }
180  }
181  }
182  }
183 
184  // Electron decorations
185 
186  if (!m_SGKey_electrons.key().empty()) {
187 
188  // Retrieve electron container
190  ctx);
191  const xAOD::EgammaContainer* importedElectrons = electronContainer.ptr();
192 
193  // Setup vectors of electron decorations
194  std::vector<SG::WriteDecorHandle<xAOD::EgammaContainer, float>>
195  decorations_E;
196  std::vector<SG::WriteDecorHandle<xAOD::EgammaContainer, float>>
197  decorations_rnoW;
198  std::vector<SG::WriteDecorHandle<xAOD::EgammaContainer, char>>
199  decorations_nCells;
200  int i(0);
201  for (const auto& kv : m_gainNames) {
202  for (const auto layer : m_layers) {
203  std::pair<int, int> key(kv.first, layer);
204  decorations_E.emplace_back(
205 
206  m_SGKey_electrons_decorations[i * 3], ctx);
207  decorations_rnoW.emplace_back(
208 
209  m_SGKey_electrons_decorations[i * 3 + 1], ctx);
210  decorations_nCells.emplace_back(
211 
212  m_SGKey_electrons_decorations[i * 3 + 2], ctx);
213  i++;
214  }
215  }
216 
217  // Decorate electrons
218  for (const auto* electron : *importedElectrons) {
220  decorateObject(electron);
221  i = 0;
222  for (const auto& kv : m_gainNames) {
223  for (const auto layer : m_layers) {
224  std::pair<int, int> key(kv.first, layer);
225  decorations_E[i](*electron) = res.E[key];
226  decorations_rnoW[i](*electron) =
227  res.EnoW[key] != 0 ? res.E[key]/res.EnoW[key] : 1;
228  decorations_nCells[i](*electron) = res.nCells[key];
229  i++;
230  }
231  }
232  }
233  }
234 
235  return StatusCode::SUCCESS;
236 }
237 
240  const xAOD::Egamma*& egamma) const
241 {
242 
243  // Compute energy and number of cells per gain per layer
244  // Set the initial values to 0 (needed?)
246  for (const auto& kv : m_names_E) {
247  result.E[kv.first] = 0.;
248  result.EnoW[kv.first] = 0.;
249  result.nCells[kv.first] = 0;
250  }
251 
252  // Skip the computation for missing cell links (like topo-seeded photons)
253  // but decorate anyway
254  const CaloClusterCellLink* cellLinks =
255  egamma->caloCluster() ? egamma->caloCluster()->getCellLinks() : nullptr;
256  if (cellLinks) {
258  itE = cellLinks->end();
259  for (; it != itE; ++it) {
260  const CaloCell *cell = *it;
261  if (!cell)
262  continue;
263  std::pair<int, int> key(static_cast<int>(cell->gain()), getLayer(cell));
264  // Increment the corresponding entry (not important if it is not
265  // initialised)
266  double weight = it.weight();
267  result.E[key] += cell->energy()*weight;
268  result.EnoW[key] += cell->energy();
269  result.nCells[key]++;
270  }
271  }
272 
273  return result;
274 }
275 
276 int
278 {
279  int sampling =
280  (cell && cell->caloDDE() ? cell->caloDDE()->getSampling() : -1);
281  if (sampling == CaloSampling::PreSamplerB ||
282  sampling == CaloSampling::PreSamplerE)
283  return 0;
284  if (sampling == CaloSampling::EMB1 || sampling == CaloSampling::EME1)
285  return 1;
286  if (sampling == CaloSampling::EMB2 || sampling == CaloSampling::EME2)
287  return 2;
288  if (sampling == CaloSampling::EMB3 || sampling == CaloSampling::EME3)
289  return 3;
290  return -1;
291 }
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
python.PerfMonSerializer.p
def p
Definition: PerfMonSerializer.py:743
DerivationFramework::GainDecorator::finalize
StatusCode finalize()
Definition: GainDecorator.cxx:122
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
AthCommonDataStore< AthCommonMsg< AlgTool > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
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:423
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:64
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:172
TruthTest.itE
itE
Definition: TruthTest.py:25
egamma
Definition: egamma.h:58
dqt_zlumi_pandas.weight
int weight
Definition: dqt_zlumi_pandas.py:200
DerivationFramework::GainDecorator::calculation
Definition: GainDecorator.h:42
DerivationFramework::GainDecorator::m_names_rnoW
std::map< std::pair< int, int >, std::string > m_names_rnoW
Definition: GainDecorator.h:65
DerivationFramework::GainDecorator::getLayer
static int getLayer(const CaloCell *cell)
Definition: GainDecorator.cxx:277
DerivationFramework::GainDecorator::m_decorationPattern
std::string m_decorationPattern
Definition: GainDecorator.h:59
constants.EMB2
int EMB2
Definition: Calorimeter/CaloClusterCorrection/python/constants.py:54
lumiFormat.i
int i
Definition: lumiFormat.py:92
beamspotman.n
n
Definition: beamspotman.py:731
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:14
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:581
photonContainer
xAOD::PhotonContainer * photonContainer
Definition: TrigGlobEffCorrValidation.cxx:189
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:192
DerivationFramework::GainDecorator::m_names_nCells
std::map< std::pair< int, int >, std::string > m_names_nCells
Definition: GainDecorator.h:66
DerivationFramework::GainDecorator::m_gainNames
std::map< int, std::string > m_gainNames
Definition: GainDecorator.h:60
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:199
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
Pass the thinning service
Definition: GainDecorator.cxx:130
GainDecorator.h
xAOD::EgammaParameters::electron
@ electron
Definition: EgammaEnums.h:18
DerivationFramework::GainDecorator::initialize
StatusCode initialize()
Definition: GainDecorator.cxx:70
AthAlgTool
Definition: AthAlgTool.h:26
CaloGain::LARLOWGAIN
@ LARLOWGAIN
Definition: CaloGain.h:18
DerivationFramework::GainDecorator::m_layers
std::vector< unsigned int > m_layers
Definition: GainDecorator.h:61
CaloCell_ID_FCS::EMB3
@ EMB3
Definition: FastCaloSim_CaloCell_ID.h:22
DerivationFramework::GainDecorator::decorateObject
calculation decorateObject(const xAOD::Egamma *&egamma) const
Definition: GainDecorator.cxx:239
constants.EME2
int EME2
Definition: Calorimeter/CaloClusterCorrection/python/constants.py:56
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37