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 
5 #include "CLHEP/Random/RandFlat.h"
6 
12 
13 #include "TH1.h"
14 #include "TVector2.h"
15 #include "TMath.h"
16 #include <TClass.h>
17 
18 //=============================================
19 //======= TFCSHitCellMappingWiggle =========
20 //=============================================
21 
23  const char *title,
26 
28  for (const auto *function : m_functions)
29  delete function;
30 #ifdef USE_GPU
31  delete m_LdFH;
32 #endif
33 }
34 
36  if (!func)
37  return;
38  for (const auto *function : m_functions)
39  if (function)
40  delete function;
41 
42  m_functions.resize(1);
43  m_functions[0] = func;
44 
45  m_bin_low_edge.resize(2);
46  m_bin_low_edge[0] = 0;
48 }
49 
51  const std::vector<const TFCS1DFunction *> &functions,
52  const std::vector<float> &bin_low_edges) {
53  if (functions.size() + 1 != bin_low_edges.size()) {
54  ATH_MSG_ERROR("Using " << functions.size() << " functions needs "
55  << functions.size() + 1 << " bin low edges, but got "
56  << bin_low_edges.size() << "bins");
57  return;
58  }
59  for (const auto *function : m_functions)
60  if (function)
61  delete function;
62  m_functions = functions;
63  m_bin_low_edge = bin_low_edges;
64 }
65 
67  if (!histogram)
68  return;
71  if (xscale != 1) {
72  for (auto &ele : func->get_HistoBordersx())
73  ele *= xscale;
74  }
75  initialize(func);
76 }
77 
79  const std::vector<const TH1 *> &histograms,
80  const std::vector<float> &bin_low_edges, float xscale) {
81  if (histograms.size() + 1 != bin_low_edges.size()) {
82  ATH_MSG_ERROR("Using " << histograms.size() << " histograms needs "
83  << histograms.size() + 1 << " bins, but got "
84  << bin_low_edges.size() << "bins");
85  return;
86  }
87  std::vector<const TFCS1DFunction *> functions(histograms.size());
88  for (unsigned int i = 0; i < histograms.size(); ++i) {
89  if (histograms[i]) {
92  if (xscale != 1) {
93  for (auto &ele : func->get_HistoBordersx())
94  ele *= xscale;
95  }
96  functions[i] = func;
97  } else {
98  functions[i] = nullptr;
99  }
100  }
101 
102  initialize(functions, bin_low_edges);
103 }
104 
106  Hit &hit, TFCSSimulationState &simulstate, const TFCSTruthState *truth,
108  if (!simulstate.randomEngine()) {
109  return FCSFatal;
110  }
111 
112  float eta = fabs(hit.eta());
114  return TFCSHitCellMapping::simulate_hit(hit, simulstate, truth, extrapol);
115  }
116 
117  auto it = std::upper_bound(m_bin_low_edge.begin(), m_bin_low_edge.end(), eta);
118  int bin = std::distance(m_bin_low_edge.begin(), it) - 1;
119 
120  const TFCS1DFunction *func = get_function(bin);
121  if (func) {
122  double rnd = CLHEP::RandFlat::shoot(simulstate.randomEngine());
123 
124  double wiggle = func->rnd_to_fct(rnd);
125 
126  ATH_MSG_DEBUG("HIT: E=" << hit.E() << " cs=" << calosample()
127  << " eta=" << hit.eta() << " phi=" << hit.phi()
128  << " wiggle=" << wiggle << " bin=" << bin << " ["
129  << get_bin_low_edge(bin) << ","
130  << get_bin_up_edge(bin) << "] func=" << func);
131 
132  double hit_phi_shifted = hit.phi() + wiggle;
133  hit.phi() = TVector2::Phi_mpi_pi(hit_phi_shifted);
134  }
135 
136  return TFCSHitCellMapping::simulate_hit(hit, simulstate, truth, extrapol);
137 }
138 
140  const TFCSParametrizationBase &ref) const {
142  return true;
144  return false;
146  return false;
148  return false;
149 
150  return true;
151 }
152 
153 void TFCSHitCellMappingWiggle::Print(Option_t *option) const {
155  TString opt(option);
156  bool shortprint = opt.Index("short") >= 0;
157  bool longprint = msgLvl(MSG::DEBUG) || (msgLvl(MSG::INFO) && !shortprint);
158  TString optprint = opt;
159  optprint.ReplaceAll("short", "");
160 
161  if (longprint) {
162  ATH_MSG(INFO) << optprint << " " << get_number_of_bins()
163  << " functions : ";
164  for (unsigned int i = 0; i < get_number_of_bins(); ++i)
165  ATH_MSG(INFO) << get_bin_low_edge(i) << " < (" << get_function(i)
166  << ") < ";
168  }
169 }
170 
172  const TFCSParametrizationBase &ref) const {
173  if (IsA() != ref.IsA()) {
174  ATH_MSG_DEBUG("compare(): different class types "
175  << IsA()->GetName() << " != " << ref.IsA()->GetName());
176  return false;
177  }
178  const TFCSHitCellMappingWiggle &ref_typed =
179  static_cast<const TFCSHitCellMappingWiggle &>(ref);
180 
181  if (m_bin_low_edge != ref_typed.m_bin_low_edge) {
182  ATH_MSG_DEBUG("operator==(): different bin edges");
183  return false;
184  }
185 
186  for (unsigned int i = 0; i < get_number_of_bins(); ++i) {
187  const TFCS1DFunction *f1 = get_function(i);
188  const TFCS1DFunction *f2 = ref_typed.get_function(i);
189  if (!f1 && !f2)
190  continue;
191  if ((f1 && !f2) || (!f1 && f2)) {
193  "compare(): different only one function pointer is nullptr");
194  return false;
195  }
196  if (f1->IsA() != f2->IsA()) {
197  ATH_MSG_DEBUG("compare(): different class types for function "
198  << i << ": " << f1->IsA()->GetName()
199  << " != " << f2->IsA()->GetName());
200  return false;
201  }
202  if (!(*f1 == *f2)) {
203  ATH_MSG_DEBUG("compare(): difference in functions " << i);
204  return false;
205  }
206  }
207 
208  return true;
209 }
210 
211 void TFCSHitCellMappingWiggle::unit_test
214  if (!simulstate)
215  simulstate = new TFCSSimulationState();
216  if (!truth)
217  truth = new TFCSTruthState();
218  if (!extrapol)
220 
221  int nbin = 10;
222  float maxeta = 5.0;
223  std::vector<const TFCS1DFunction *> functions;
224  std::vector<float> bin_low_edges;
225 
226  TFCSHitCellMappingWiggle wiggle_test("WiggleTest", "WiggleTest");
227 
228  for (float eta = 0; eta < maxeta; eta += maxeta / nbin) {
229  TH1 *hist = TFCS1DFunction::generate_histogram_random_gauss(
230  16, 100000, -0.0125, 0.0125, 0, 0.005);
231  bin_low_edges.push_back(eta);
232  functions.push_back(new TFCS1DFunctionInt32Histogram(hist));
233  delete hist;
234  }
235  bin_low_edges.push_back(100);
236  wiggle_test.initialize(functions, bin_low_edges);
237  wiggle_test.set_calosample(2);
238  wiggle_test.setLevel(MSG::DEBUG);
239  wiggle_test.Print();
240 
241 #if 0 // defined(__FastCaloSimStandAlone__)
243 
244 // * load geometry files
245  geo->LoadGeometryFromFile("/afs/cern.ch/atlas/groups/Simulation/FastCaloSimV2/Geometry-ATLAS-R2-2016-01-00-01.root", "ATLAS-R2-2016-01-00-01");
246  TString path_to_fcal_geo_files = "/afs/cern.ch/atlas/groups/Simulation/FastCaloSimV2/";
247  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");
248 
249  wiggle_test.set_geometry(geo);
250  for(float eta=-maxeta+0.01;eta<maxeta;eta+=maxeta/nbin) {
251  Hit hit;
252  hit.eta()=eta;
253  hit.phi()=0;
254  hit.E()=1;
255  wiggle_test.simulate_hit(hit,*simulstate,truth,extrapol);
256  }
257 #endif
258 }
259 
260 #ifdef USE_GPU
261 // copy the hist functions to GPU
262 void TFCSHitCellMappingWiggle::LoadHistFuncs() {
263 
264  if (m_LdFH) {
265  return;
266  }
267  m_LdFH = new LoadGpuFuncHist();
268  FHs fhs;
269 
271  fhs.nhist = m_functions.size();
272  fhs.low_edge = &(m_bin_low_edge[0]);
273  fhs.h_szs = (unsigned int *)malloc(fhs.nhist * sizeof(unsigned int));
274  fhs.h_contents = (uint32_t **)malloc(fhs.nhist * sizeof(uint32_t *));
275  fhs.h_borders = (float **)malloc(fhs.nhist * sizeof(float *));
276 
277  for (size_t i = 0; i < fhs.nhist; ++i) {
279  ->get_HistoContents()
280  .size();
282  ->get_HistoContents()[0]);
284  ->get_HistoBordersx()[0]);
285  fhs.mxsz = std::max(fhs.mxsz, fhs.h_szs[i]);
286  }
287 
288  m_LdFH->set_hf(&fhs);
289  m_LdFH->LD();
290 
291  return;
292 }
293 #endif
TFCSHitCellMappingWiggle
Definition: TFCSHitCellMappingWiggle.h:20
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:105
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
max
#define max(a, b)
Definition: cfImp.cxx:41
TFCSHitCellMappingWiggle::get_bin_low_edge
double get_bin_low_edge(int bin) const
Definition: TFCSHitCellMappingWiggle.h:37
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:38
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:35
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:79
plotmaker.hist
hist
Definition: plotmaker.py:148
TFCSHitCellMappingWiggle::compare
bool compare(const TFCSParametrizationBase &ref) const
Definition: TFCSHitCellMappingWiggle.cxx:171
skel.it
it
Definition: skel.GENtoEVGEN.py:423
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
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:212
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:90
Phi_mpi_pi
__HOSTDEV__ double Phi_mpi_pi(double)
Definition: GeoRegion.cxx:7
TFCSHitCellMappingWiggle::initialize
void initialize(TFCS1DFunction *func)
Definition: TFCSHitCellMappingWiggle.cxx:35
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:153
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:92
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:131
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:195
pmontree.opt
opt
Definition: pmontree.py:16
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:42
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
TH1
Definition: rootspy.cxx:268
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:139
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