ATLAS Offline Software
Loading...
Searching...
No Matches
TFCSHistoLateralShapeParametrization.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3*/
4
5#include "CLHEP/Random/RandFlat.h"
6#include "CLHEP/Random/RandPoisson.h"
7
12
13#include "TFile.h"
14#include "TMath.h"
15#include "TH2.h"
16
18
19#include <cmath>
20
21//=============================================
22//======= TFCSHistoLateralShapeParametrization =========
23//=============================================
24
31
33#ifdef USE_GPU
34 delete m_LdFH;
35#endif
36}
37
40 if (!m_hist.get_HistoContents().empty()) {
41 int first_fix_bin = -1;
42 for (int i = (int)(m_hist.get_HistoContents().size() - 1); i >= 0; --i) {
43 if (std::isnan(m_hist.get_HistoContents()[i])) {
44 ATH_MSG_DEBUG("nan in histo content for "
45 << GetTitle() << ", bin[" << i
46 << "]=" << m_hist.get_HistoContents()[i] << " -> 1");
47 m_hist.get_HistoContents()[i] = 1;
48 first_fix_bin = i;
49 }
50 }
51 if (first_fix_bin < 0)
52 return;
53
54 if (first_fix_bin == 0) {
55 ATH_MSG_WARNING("nan in histo content for "
56 << GetTitle()
57 << " for all bins. Fixed to probability 1 causing hits "
58 "to be deposited in the shower center");
59 } else {
60 int last_fix_bin = -1;
61 for (size_t i = 0; i < m_hist.get_HistoContents().size(); ++i) {
62 if (std::isnan(m_hist.get_HistoContents()[i])) {
63 ATH_MSG_DEBUG("nan in histo content for "
64 << GetTitle() << ", bin[" << i
65 << "]=" << m_hist.get_HistoContents()[i] << " -> 0");
66 m_hist.get_HistoContents()[i] = 0;
67 last_fix_bin = i;
68 }
69 }
70 ATH_MSG_WARNING("nan in histo content for "
71 << GetTitle() << ". Fixed up to bin " << last_fix_bin
72 << " with probability 0 and beyond bin " << first_fix_bin
73 << " with probability 1.");
74 }
75 }
76}
77
79 TFCSSimulationState & /*simulstate*/, const TFCSTruthState * /*truth*/,
80 const TFCSExtrapolationState * /*extrapol*/) const {
81 // Limit to factor 1000 fluctuations
82 if (m_nhits < 0.001)
83 return 1000;
84 return 1.0 / m_nhits;
85}
86
88 TFCSSimulationState &simulstate, const TFCSTruthState * /*truth*/,
89 const TFCSExtrapolationState * /*extrapol*/) const {
90 if (!simulstate.randomEngine()) {
91 return -1;
92 }
93
94 return CLHEP::RandPoisson::shoot(simulstate.randomEngine(), m_nhits);
95}
96
100
102 Hit &hit, TFCSSimulationState &simulstate, const TFCSTruthState *truth,
103 const TFCSExtrapolationState * /*extrapol*/) {
104 if (!simulstate.randomEngine()) {
105 return FCSFatal;
106 }
107
108 const int pdgId = truth->pdgid();
109 const double charge = MC::charge(pdgId);
110
111 const int cs = calosample();
112 const double center_eta = hit.center_eta();
113 const double center_phi = hit.center_phi();
114 const double center_r = hit.center_r();
115 const double center_z = hit.center_z();
116
117 if (TMath::IsNaN(center_r) or TMath::IsNaN(center_z) or
118 TMath::IsNaN(center_eta) or
119 TMath::IsNaN(center_phi)) { // Check if extrapolation fails
120 return FCSFatal;
121 }
122
123 float alpha, r, rnd1, rnd2;
124 rnd1 = CLHEP::RandFlat::shoot(simulstate.randomEngine());
125 rnd2 = CLHEP::RandFlat::shoot(simulstate.randomEngine());
126 if (is_phi_symmetric()) {
127 if (rnd2 >= 0.5) { // Fill negative phi half of shape
128 rnd2 -= 0.5;
129 rnd2 *= 2;
130 m_hist.rnd_to_fct(alpha, r, rnd1, rnd2);
131 alpha = -alpha;
132 } else { // Fill positive phi half of shape
133 rnd2 *= 2;
134 m_hist.rnd_to_fct(alpha, r, rnd1, rnd2);
135 }
136 } else {
137 m_hist.rnd_to_fct(alpha, r, rnd1, rnd2);
138 }
139 if (TMath::IsNaN(alpha) || TMath::IsNaN(r)) {
140 ATH_MSG_ERROR(" Histogram: "
141 << m_hist.get_HistoBordersx().size() - 1 << "*"
142 << m_hist.get_HistoBordersy().size() - 1
143 << " bins, #hits=" << m_nhits << " alpha=" << alpha
144 << " r=" << r << " rnd1=" << rnd1 << " rnd2=" << rnd2);
145 alpha = 0;
146 r = 0.001;
147
148 ATH_MSG_ERROR(" This error could probably be retried");
149 return FCSFatal;
150 }
151
152 r *= m_r_scale;
153 r += m_r_offset;
154 if (r < 0)
155 r = 0;
156
157 float delta_eta_mm = r * cos(alpha);
158 float delta_phi_mm = r * sin(alpha);
159
160 // Particles with negative eta are expected to have the same shape as those
161 // with positive eta after transformation: delta_eta --> -delta_eta
162 if (center_eta < 0.)
163 delta_eta_mm = -delta_eta_mm;
164 // We derive the shower shapes for electrons and positively charged hadrons.
165 // Particle with the opposite charge are expected to have the same shower shape
166 // after the transformation: delta_phi --> -delta_phi
167 if ((charge < 0. && pdgId!=11) || pdgId==-11)
168 delta_phi_mm = -delta_phi_mm;
169
170 const float dist000 = TMath::Sqrt(center_r * center_r + center_z * center_z);
171 const float eta_jakobi = TMath::Abs(2.0 * TMath::Exp(-center_eta) /
172 (1.0 + TMath::Exp(-2 * center_eta)));
173
174 const float delta_eta = delta_eta_mm / eta_jakobi / dist000;
175 const float delta_phi = delta_phi_mm / center_r;
176
177 hit.setEtaPhiZE(center_eta + delta_eta, center_phi + delta_phi, center_z,
178 hit.E());
179
180 ATH_MSG_DEBUG("HIT: E=" << hit.E() << " cs=" << cs << " eta=" << hit.eta()
181 << " phi=" << hit.phi() << " z=" << hit.z()
182 << " r=" << r << " alpha=" << alpha);
183
184 return FCSSuccess;
185}
186
188 if (!hist)
189 return false;
190 m_hist.Initialize(hist);
191 if (m_hist.get_HistoContents().empty())
192 return false;
193
194 set_number_of_hits(hist->Integral());
195
196 return true;
197}
198
200 const char *histname) {
201 // input file with histogram to fit
202 std::unique_ptr<TFile> inputfile(TFile::Open(filepath, "READ"));
203 if (inputfile == nullptr)
204 return false;
205
206 // histogram with hit pattern
207 TH2 *inputShape = (TH2 *)inputfile->Get(histname);
208 if (inputShape == nullptr)
209 return false;
210
211 bool OK = Initialize(inputShape);
212
213 inputfile->Close();
214
215 return OK;
216}
217
219 TString opt(option);
220 bool shortprint = opt.Index("short") >= 0;
221 bool longprint = msgLvl(MSG::DEBUG) || (msgLvl(MSG::INFO) && !shortprint);
222 TString optprint = opt;
223 optprint.ReplaceAll("short", "");
225
226 if (longprint) {
227 if (is_phi_symmetric()) {
228 ATH_MSG_INFO(optprint
229 << " Histo: " << m_hist.get_HistoBordersx().size() - 1
230 << "*" << m_hist.get_HistoBordersy().size() - 1
231 << " bins, #hits=" << m_nhits << ", r scale=" << m_r_scale
232 << ", r offset=" << m_r_offset << "mm (phi symmetric)");
233 } else {
234 ATH_MSG_INFO(optprint
235 << " Histo: " << m_hist.get_HistoBordersx().size() - 1
236 << "*" << m_hist.get_HistoBordersy().size() - 1
237 << " bins, #hits=" << m_nhits << ", r scale=" << m_r_scale
238 << ", r offset=" << m_r_offset << "mm (not phi symmetric)");
239 }
240 }
241}
242
243#ifdef USE_GPU
244void TFCSHistoLateralShapeParametrization::LoadHistFuncs() {
245
246 if (m_LdFH)
247 return;
248
249 m_LdFH = new LoadGpuFuncHist();
250 FH2D fh = {0, 0, 0, 0, 0};
251
252 fh.nbinsx = m_hist.get_HistoBordersx().size() - 1;
253 fh.nbinsy = m_hist.get_HistoBordersy().size() - 1;
254
255 fh.h_bordersx = &(m_hist.get_HistoBordersx()[0]);
256 fh.h_bordersy = &(m_hist.get_HistoBordersy()[0]);
257
258 fh.h_contents = &(m_hist.get_HistoContents()[0]);
259
260 m_LdFH->set_hf2d(&fh);
261 m_LdFH->LD2D();
262}
263#endif
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
double charge(const T &p)
Definition AtlasPID.h:997
ATLAS-specific HepMC functions.
size_t size() const
Number of registered mappings.
FCSReturnCode
Base class for all FastCaloSim parametrizations Functionality in derivde classes is provided through ...
bool msgLvl(const MSG::Level lvl) const
Check whether the logging system is active at the provided verbosity level.
Definition MLogging.h:222
virtual void set_geometry(ICaloGeometry *geo) override
will actually not store the geometry information, but rather used to check the validity of the 2D sha...
virtual int get_number_of_hits(TFCSSimulationState &simulstate, const TFCSTruthState *truth, const TFCSExtrapolationState *extrapol) const override
default for this class is to simulate get_number_of_expected_hits() hits
virtual FCSReturnCode simulate_hit(Hit &hit, TFCSSimulationState &simulstate, const TFCSTruthState *truth, const TFCSExtrapolationState *extrapol) override
simulated one hit position with weight that should be put into simulstate sometime later all hit weig...
TFCS2DFunctionHistogram m_hist
Histogram to be used for the shape simulation.
TFCSHistoLateralShapeParametrization(const char *name=nullptr, const char *title=nullptr)
void set_number_of_hits(float nhits)
set the integral of the histogram to the desired number of hits
virtual double get_sigma2_fluctuation(TFCSSimulationState &simulstate, const TFCSTruthState *truth, const TFCSExtrapolationState *extrapol) const override
default for this class is to simulate get_number_of_expected_hits() hits, which gives fluctuations si...
void setEtaPhiZE(float eta, float phi, float z, float E)
TFCSLateralShapeParametrizationHitBase(const char *name=nullptr, const char *title=nullptr)
void Print(Option_t *option="") const override
virtual void set_geometry(ICaloGeometry *geo)
Method to set the geometry access pointer.
CLHEP::HepRandomEngine * randomEngine()
int pdgid() const
int r
Definition globals.cxx:22
double charge(const T &p)
float * h_contents
Definition FH_structs.h:34
float * h_bordersy
Definition FH_structs.h:33
int nbinsx
Definition FH_structs.h:30
float * h_bordersx
Definition FH_structs.h:32
int nbinsy
Definition FH_structs.h:31