ATLAS Offline Software
TFCSHitCellMappingWiggle.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 
7 #include <TClass.h>
8 
9 #include "CLHEP/Random/RandFlat.h"
14 #include "TH1.h"
15 #include "TMath.h"
16 #include "TVector2.h"
17 
18 //=============================================
19 //======= TFCSHitCellMappingWiggle =========
20 //=============================================
21 
23  const char *title,
26 
28  clear();
29 }
30 
32  for (auto *&function : m_functions) {
33  if (function) {
34  delete function;
35  function = nullptr;
36  }
37  }
38  m_functions.clear();
39  m_bin_low_edge.clear();
40 
41 #ifdef USE_GPU
42  delete m_LdFH;
43 #endif
44 }
45 
47  clear();
48  if (!func)
49  return;
50  for (const auto *function : m_functions)
51  if (function)
52  delete function;
53 
54  m_functions.resize(1);
55  m_functions[0] = func;
56 
57  m_bin_low_edge.resize(2);
58  m_bin_low_edge[0] = 0;
60 }
61 
63  const std::vector<const TFCS1DFunction *> &functions,
64  const std::vector<float> &bin_low_edges) {
65  clear();
66  if (functions.size() + 1 != bin_low_edges.size()) {
67  ATH_MSG_ERROR("Using " << functions.size() << " functions needs "
68  << functions.size() + 1 << " bin low edges, but got "
69  << bin_low_edges.size() << "bins");
70  return;
71  }
72  for (const auto *function : m_functions)
73  if (function)
74  delete function;
75  m_functions = functions;
76  m_bin_low_edge = bin_low_edges;
77 }
78 
80  clear();
81  if (!histogram)
82  return;
85  if (xscale != 1) {
86  for (auto &ele : func->get_HistoBordersx())
87  ele *= xscale;
88  }
89  initialize(func);
90 }
91 
93  const std::vector<const TH1 *> &histograms,
94  const std::vector<float> &bin_low_edges, float xscale) {
95  clear();
96  if (histograms.size() + 1 != bin_low_edges.size()) {
97  ATH_MSG_ERROR("Using " << histograms.size() << " histograms needs "
98  << histograms.size() + 1 << " bins, but got "
99  << bin_low_edges.size() << "bins");
100  return;
101  }
102  std::vector<const TFCS1DFunction *> functions(histograms.size());
103  for (unsigned int i = 0; i < histograms.size(); ++i) {
104  if (histograms[i]) {
107  if (xscale != 1) {
108  for (auto &ele : func->get_HistoBordersx())
109  ele *= xscale;
110  }
111  functions[i] = func;
112  } else {
113  functions[i] = nullptr;
114  }
115  }
116 
117  initialize(functions, bin_low_edges);
118 }
119 
121  Hit &hit, TFCSSimulationState &simulstate, const TFCSTruthState *truth,
123  if (!simulstate.randomEngine()) {
124  return FCSFatal;
125  }
126 
127  float eta = fabs(hit.eta());
129  return TFCSHitCellMapping::simulate_hit(hit, simulstate, truth, extrapol);
130  }
131 
132  auto it = std::upper_bound(m_bin_low_edge.begin(), m_bin_low_edge.end(), eta);
133  int bin = std::distance(m_bin_low_edge.begin(), it) - 1;
134 
135  const TFCS1DFunction *func = get_function(bin);
136  if (func) {
137  double rnd = CLHEP::RandFlat::shoot(simulstate.randomEngine());
138 
139  double wiggle = func->rnd_to_fct(rnd);
140 
141  ATH_MSG_DEBUG("HIT: E=" << hit.E() << " cs=" << calosample()
142  << " eta=" << hit.eta() << " phi=" << hit.phi()
143  << " wiggle=" << wiggle << " bin=" << bin << " ["
144  << get_bin_low_edge(bin) << ","
145  << get_bin_up_edge(bin) << "] func=" << func);
146 
147  double hit_phi_shifted = hit.phi() + wiggle;
148  hit.phi() = TVector2::Phi_mpi_pi(hit_phi_shifted);
149  }
150 
151  return TFCSHitCellMapping::simulate_hit(hit, simulstate, truth, extrapol);
152 }
153 
155  const TFCSParametrizationBase &ref) const {
157  return true;
159  return false;
161  return false;
163  return false;
164 
165  return true;
166 }
167 
168 void TFCSHitCellMappingWiggle::Print(Option_t *option) const {
170  TString opt(option);
171  bool shortprint = opt.Index("short") >= 0;
172  bool longprint = msgLvl(MSG::DEBUG) || (msgLvl(MSG::INFO) && !shortprint);
173  TString optprint = opt;
174  optprint.ReplaceAll("short", "");
175 
176  if (longprint) {
177  ATH_MSG(INFO) << optprint << " " << get_number_of_bins()
178  << " functions : ";
179  for (unsigned int i = 0; i < get_number_of_bins(); ++i)
180  ATH_MSG(INFO) << get_bin_low_edge(i) << " < (" << get_function(i)
181  << ") < ";
183  }
184 }
185 
187  const TFCSParametrizationBase &ref) const {
188  if (IsA() != ref.IsA()) {
189  ATH_MSG_DEBUG("compare(): different class types "
190  << IsA()->GetName() << " != " << ref.IsA()->GetName());
191  return false;
192  }
193  const TFCSHitCellMappingWiggle &ref_typed =
194  static_cast<const TFCSHitCellMappingWiggle &>(ref);
195 
196  if (m_bin_low_edge != ref_typed.m_bin_low_edge) {
197  ATH_MSG_DEBUG("operator==(): different bin edges");
198  return false;
199  }
200 
201  for (unsigned int i = 0; i < get_number_of_bins(); ++i) {
202  const TFCS1DFunction *f1 = get_function(i);
203  const TFCS1DFunction *f2 = ref_typed.get_function(i);
204  if (!f1 && !f2)
205  continue;
206  if ((f1 && !f2) || (!f1 && f2)) {
208  "compare(): different only one function pointer is nullptr");
209  return false;
210  }
211  if (f1->IsA() != f2->IsA()) {
212  ATH_MSG_DEBUG("compare(): different class types for function "
213  << i << ": " << f1->IsA()->GetName()
214  << " != " << f2->IsA()->GetName());
215  return false;
216  }
217  if (!(*f1 == *f2)) {
218  ATH_MSG_DEBUG("compare(): difference in functions " << i);
219  return false;
220  }
221  }
222 
223  return true;
224 }
225 
226 void TFCSHitCellMappingWiggle::unit_test ATLAS_NOT_THREAD_SAFE(
227  TFCSSimulationState *simulstate, TFCSTruthState *truth,
229  if (!simulstate)
230  simulstate = new TFCSSimulationState();
231  if (!truth)
232  truth = new TFCSTruthState();
233  if (!extrapol)
235 
236  int nbin = 10;
237  float maxeta = 5.0;
238  std::vector<const TFCS1DFunction *> functions;
239  std::vector<float> bin_low_edges;
240 
241  TFCSHitCellMappingWiggle wiggle_test("WiggleTest", "WiggleTest");
242 
243  for (float eta = 0; eta < maxeta; eta += maxeta / nbin) {
244  TH1 *hist = TFCS1DFunction::generate_histogram_random_gauss(
245  16, 100000, -0.0125, 0.0125, 0, 0.005);
246  bin_low_edges.push_back(eta);
247  functions.push_back(new TFCS1DFunctionInt32Histogram(hist));
248  delete hist;
249  }
250  bin_low_edges.push_back(100);
251  wiggle_test.initialize(functions, bin_low_edges);
252  wiggle_test.set_calosample(2);
253  wiggle_test.setLevel(MSG::DEBUG);
254  wiggle_test.Print();
255 
256 #if 0 // defined(__FastCaloSimStandAlone__)
258 
259 // * load geometry files
260  geo->LoadGeometryFromFile("/afs/cern.ch/atlas/groups/Simulation/FastCaloSimV2/Geometry-ATLAS-R2-2016-01-00-01.root", "ATLAS-R2-2016-01-00-01");
261  TString path_to_fcal_geo_files = "/afs/cern.ch/atlas/groups/Simulation/FastCaloSimV2/";
262  geo->LoadFCalGeometryFromFiles(path_to_fcal_geo_files + "FCal1-electrodes.sorted.HV.09Nov2007.dat", path_to_fcal_geo_files + "FCal2-electrodes.sorted.HV.April2011.dat", path_to_fcal_geo_files + "FCal3-electrodes.sorted.HV.09Nov2007.dat");
263 
264  wiggle_test.set_geometry(geo);
265  for(float eta=-maxeta+0.01;eta<maxeta;eta+=maxeta/nbin) {
266  Hit hit;
267  hit.eta()=eta;
268  hit.phi()=0;
269  hit.E()=1;
270  wiggle_test.simulate_hit(hit,*simulstate,truth,extrapol);
271  }
272 #endif
273 }
274 
275 #ifdef USE_GPU
276 // copy the hist functions to GPU
277 void TFCSHitCellMappingWiggle::LoadHistFuncs() {
278 
279  if (m_LdFH) {
280  return;
281  }
282  m_LdFH = new LoadGpuFuncHist();
283  FHs fhs;
284 
286  fhs.nhist = m_functions.size();
287  fhs.low_edge = &(m_bin_low_edge[0]);
288  fhs.h_szs = (unsigned int *)malloc(fhs.nhist * sizeof(unsigned int));
289  fhs.h_contents = (uint32_t **)malloc(fhs.nhist * sizeof(uint32_t *));
290  fhs.h_borders = (float **)malloc(fhs.nhist * sizeof(float *));
291 
292  for (size_t i = 0; i < fhs.nhist; ++i) {
294  ->get_HistoContents()
295  .size();
297  ->get_HistoContents()[0]);
299  ->get_HistoBordersx()[0]);
300  fhs.mxsz = std::max(fhs.mxsz, fhs.h_szs[i]);
301  }
302 
303  m_LdFH->set_hf(&fhs);
304  m_LdFH->LD();
305 
306  return;
307 }
308 #endif
TFCSHitCellMappingWiggle
Definition: TFCSHitCellMappingWiggle.h:19
FCSReturnCode
FCSReturnCode
Base class for all FastCaloSim parametrizations Functionality in derivde classes is provided through ...
Definition: TFCSParametrizationBase.h:41
TFCSParametrization::compare
bool compare(const TFCSParametrizationBase &ref) const
Definition: TFCSParametrization.cxx:78
TFCSHitCellMappingWiggle::simulate_hit
virtual FCSReturnCode simulate_hit(Hit &hit, TFCSSimulationState &simulstate, const TFCSTruthState *truth, const TFCSExtrapolationState *extrapol) override
modify one hit position to emulate the LAr accordeon shape and then fills all hits into calorimeter c...
Definition: TFCSHitCellMappingWiggle.cxx:120
TFCSParametrizationBase::compare
bool compare(const TFCSParametrizationBase &ref) const
Do not persistify!
Definition: TFCSParametrizationBase.cxx:42
TFCSLateralShapeParametrizationHitBase::Hit::phi
float & phi()
Definition: TFCSLateralShapeParametrizationHitBase.h:87
TFCSHitCellMapping::Print
void Print(Option_t *option) const override
Definition: TFCSHitCellMapping.cxx:61
ATH_MSG
#define ATH_MSG(lvl)
Definition: AthMsgStreamMacros.h:38
TFCSHitCellMappingWiggle::get_bin_low_edge
double get_bin_low_edge(int bin) const
Definition: TFCSHitCellMappingWiggle.h:38
covarianceTool.histograms
dictionary histograms
Definition: covarianceTool.py:53
IsA
#define IsA
Declare the TObject style functions.
Definition: xAODTEventBranch.h:59
TFCSHitCellMappingWiggle::get_bin_up_edge
double get_bin_up_edge(int bin) const
Definition: TFCSHitCellMappingWiggle.h:39
FHs::nhist
unsigned int nhist
Definition: FH_structs.h:13
TFCSHitCellMappingWiggle::get_number_of_bins
unsigned int get_number_of_bins() const
Definition: TFCSHitCellMappingWiggle.h:36
make_coralServer_rep.opt
opt
Definition: make_coralServer_rep.py:19
TFCSHitCellMappingWiggle::TFCSHitCellMappingWiggle
TFCSHitCellMappingWiggle(const char *name=nullptr, const char *title=nullptr, ICaloGeometry *geo=nullptr)
Definition: TFCSHitCellMappingWiggle.cxx:22
xAOD::uint32_t
setEventNumber uint32_t
Definition: EventInfo_v1.cxx:127
eta
Scalar eta() const
pseudorapidity method
Definition: AmgMatrixBasePlugin.h:83
max
constexpr double max()
Definition: ap_fixedTest.cxx:33
plotmaker.hist
hist
Definition: plotmaker.py:148
TFCSHitCellMappingWiggle::compare
bool compare(const TFCSParametrizationBase &ref) const
Definition: TFCSHitCellMappingWiggle.cxx:186
skel.it
it
Definition: skel.GENtoEVGEN.py:396
TFCS1DFunction::rnd_to_fct
virtual void rnd_to_fct(float value[], const float rnd[]) const
Function gets array of random numbers rnd[] in the range [0,1) as arguments and returns function valu...
Definition: TFCS1DFunction.cxx:17
FHs::h_contents
uint32_t ** h_contents
Definition: FH_structs.h:16
bin
Definition: BinsDiffFromStripMedian.h:43
FHs::s_MaxValue
uint32_t s_MaxValue
Definition: FH_structs.h:11
TFCSHitCellMappingWiggle::clear
void clear()
Definition: TFCSHitCellMappingWiggle.cxx:31
TFCSHitCellMapping::set_geometry
virtual void set_geometry(ICaloGeometry *geo) override
Method to set the geometry access pointer.
Definition: TFCSHitCellMapping.h:17
TFCSParametrizationBase::init_eta_max
static constexpr double init_eta_max
Do not persistify!
Definition: TFCSParametrizationBase.h:158
ATLAS_NOT_THREAD_SAFE
void TFCSHitCellMappingWiggle::unit_test ATLAS_NOT_THREAD_SAFE(TFCSSimulationState *simulstate, TFCSTruthState *truth, TFCSExtrapolationState *extrapol)
Definition: TFCSHitCellMappingWiggle.cxx:226
TFCSExtrapolationState
Definition: TFCSExtrapolationState.h:13
TFCSSimulationState::randomEngine
CLHEP::HepRandomEngine * randomEngine()
Definition: TFCSSimulationState.h:36
TFCSLateralShapeParametrizationHitBase::Hit
Definition: TFCSLateralShapeParametrizationHitBase.h:42
RunActsMaterialValidation.extrapol
extrapol
Definition: RunActsMaterialValidation.py:91
Phi_mpi_pi
__HOSTDEV__ double Phi_mpi_pi(double)
Definition: GeoRegion.cxx:7
TFCSHitCellMappingWiggle::initialize
void initialize(TFCS1DFunction *func)
Definition: TFCSHitCellMappingWiggle.cxx:46
read_hist_ntuple.f2
f2
Definition: read_hist_ntuple.py:20
TFCSLateralShapeParametrizationHitBase::Hit::E
float & E()
Definition: TFCSLateralShapeParametrizationHitBase.h:90
TFCSParametrizationBase
Definition: TFCSParametrizationBase.h:46
Hit
Definition: Simulation/ISF/ISF_FastCaloSim/ISF_FastCaloGpu/ISF_FastCaloGpu/Hit.h:16
TFCSHitCellMappingWiggle::Print
void Print(Option_t *option="") const override
Definition: TFCSHitCellMappingWiggle.cxx:168
TFCSHitCellMappingWiggle::m_bin_low_edge
std::vector< float > m_bin_low_edge
Definition: TFCSHitCellMappingWiggle.h:89
CaloGeometryFromFile
Definition: CaloGeometryFromFile.h:11
FHs::mxsz
unsigned int mxsz
Definition: FH_structs.h:14
TFCSLateralShapeParametrization::set_calosample
void set_calosample(int cs)
Definition: TFCSLateralShapeParametrization.cxx:21
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
lumiFormat.i
int i
Definition: lumiFormat.py:85
TFCS1DFunctionInt32Histogram
Definition: TFCS1DFunctionInt32Histogram.h:15
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
TFCSHitCellMappingWiggle::~TFCSHitCellMappingWiggle
~TFCSHitCellMappingWiggle()
Definition: TFCSHitCellMappingWiggle.cxx:27
ICaloGeometry
Definition: ICaloGeometry.h:14
covarianceTool.title
title
Definition: covarianceTool.py:542
LoadGpuFuncHist
Definition: LoadGpuFuncHist.h:10
TrigInDetValidation_Base.malloc
malloc
Definition: TrigInDetValidation_Base.py:132
FCSFatal
@ FCSFatal
Definition: TFCSParametrizationBase.h:41
TFCS1DFunctionInt32Histogram::s_MaxValue
static const HistoContent_t s_MaxValue
Definition: TFCS1DFunctionInt32Histogram.h:28
FHs::h_szs
unsigned int * h_szs
Definition: FH_structs.h:15
Hit::phi
CUDA_HOSTDEV float & phi()
Definition: Simulation/ISF/ISF_FastCaloSim/ISF_FastCaloGpu/ISF_FastCaloGpu/Hit.h:72
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:228
ISF_FCS::MLogging::setLevel
virtual void setLevel(MSG::Level lvl)
Update outputlevel.
Definition: MLogging.cxx:105
TFCSHitCellMappingWiggle::get_function
const TFCS1DFunction * get_function(int bin) const
Definition: TFCSHitCellMappingWiggle.h:43
FHs::low_edge
float * low_edge
Definition: FH_structs.h:12
END_MSG
#define END_MSG(lvl)
Definition: MLogging.h:171
TFCSLateralShapeParametrization::compare
bool compare(const TFCSParametrizationBase &ref) const
Definition: TFCSLateralShapeParametrization.cxx:32
TFCSTruthState.h
TFCSLateralShapeParametrization::calosample
int calosample() const
Definition: TFCSLateralShapeParametrization.h:34
python.Constants.INFO
int INFO
Definition: Control/AthenaCommon/python/Constants.py:16
ref
const boost::regex ref(r_ef)
TFCSExtrapolationState.h
LArCellConditions.geo
bool geo
Definition: LArCellConditions.py:46
DEBUG
#define DEBUG
Definition: page_access.h:11
FHs::h_borders
float ** h_borders
Definition: FH_structs.h:17
TFCSLateralShapeParametrizationHitBase::Hit::eta
float & eta()
Definition: TFCSLateralShapeParametrizationHitBase.h:86
TFCSHitCellMappingWiggle.h
Hit::E
CUDA_HOSTDEV float & E()
Definition: Simulation/ISF/ISF_FastCaloSim/ISF_FastCaloGpu/ISF_FastCaloGpu/Hit.h:75
Hit::eta
CUDA_HOSTDEV float & eta()
Definition: Simulation/ISF/ISF_FastCaloSim/ISF_FastCaloGpu/ISF_FastCaloGpu/Hit.h:71
TFCSSimulationState.h
TFCSHitCellMappingWiggle::operator==
virtual bool operator==(const TFCSParametrizationBase &ref) const override
The == operator compares the content of instances.
Definition: TFCSHitCellMappingWiggle.cxx:154
TFCS1DFunction
Definition: TFCS1DFunction.h:17
TFCS1DFunctionInt32Histogram::get_HistoBordersx
const std::vector< float > & get_HistoBordersx() const
Definition: TFCS1DFunctionInt32Histogram.h:36
Amg::distance
float distance(const Amg::Vector3D &p1, const Amg::Vector3D &p2)
calculates the distance between two point in 3D space
Definition: GeoPrimitivesHelpers.h:54
TFCSHitCellMapping::simulate_hit
virtual FCSReturnCode simulate_hit(Hit &hit, TFCSSimulationState &simulstate, const TFCSTruthState *truth, const TFCSExtrapolationState *extrapol) override
fills all hits into calorimeter cells
Definition: TFCSHitCellMapping.cxx:21
TFCSTruthState
Definition: TFCSTruthState.h:13
TFCSSimulationState
Definition: TFCSSimulationState.h:32
TFCSHitCellMapping
Definition: TFCSHitCellMapping.h:12
FHs
Definition: FH_structs.h:10
histogram
std::string histogram
Definition: chains.cxx:52
ISF_FCS::MLogging::msgLvl
bool msgLvl(const MSG::Level lvl) const
Check whether the logging system is active at the provided verbosity level.
Definition: MLogging.h:222
TFCSHitCellMappingWiggle::m_functions
std::vector< const TFCS1DFunction * > m_functions
Definition: TFCSHitCellMappingWiggle.h:88
TFCS1DFunctionInt32Histogram.h
read_hist_ntuple.f1
f1
Definition: read_hist_ntuple.py:4