ATLAS Offline Software
APWeightEntry.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 #define APWeightEntry_cxx
8 
10 #include "RootUtils/TRandomTLS.h"
11 #include <cmath>
12 #include <limits>
13 #include <vector>
14 #include "TH1F.h"
15 #include "TRandom3.h"
16 #include "TMath.h"
17 
18 using namespace std;
19 
21  : m_val_denominator(0),
22  m_val_numerator(0),
23  m_expectancy_val(0),
24  m_variance(0),
25  m_stat_uncert_low(0),
26  m_stat_uncert_high(0),
27  m_sys_uncert(0),
28  m_sys_uncert2(0),
29  m_is_trig(0),
30  m_is_nan(0),
31  m_integral(0)
32 {
33  m_hist = 0;
34  m_cumul = 0;
35  m_pdf = 0;
36  m_bins = 0;
37  m_ID = 0;
38  //m_coords = 0;
39 }
40 
41 APWeightEntry::APWeightEntry(unsigned int val_denominator, unsigned int val_numerator, double scale, bool isTrig)
42  : m_stat_uncert_low(0),
43  m_stat_uncert_high(0)
44 {
45  m_val_denominator = val_denominator;
46  double val_denominator_f = (double) val_denominator;
47  m_val_numerator = val_numerator;
48  double val_numerator_f = (double) val_numerator;
49  m_sys_uncert = 0.;
50  m_sys_uncert2 = 0.;
51  m_integral = 0.;
52  m_hist = 0;
53  m_cumul = 0;
54  m_bins = 0;
55  m_pdf = 0;
56  m_is_trig = isTrig;
57  m_ID = 0;
58  //m_coords = 0;
59 
60  if (!isTrig) {
61  if (m_val_numerator == 0 || m_val_denominator == 0) {
62  m_expectancy_val = 0.;
63  m_variance = 0.;
64  m_stat_uncert_low = 0.;
65  m_stat_uncert_high = 0.;
66  m_is_nan = true;
67  } else {
68  m_is_nan = false;
69  m_pdf = new double[1000];
70  m_bins = new double[1001];
71 
72  m_expectancy_val = scale * (val_numerator_f / val_denominator_f);
73  m_variance = (val_numerator_f / (val_denominator_f * val_denominator_f)) + ((val_numerator_f * val_numerator_f) / (val_denominator_f * val_denominator_f * val_denominator_f));
76 
77  double sig_low = (std::max((double) 0.0, (m_expectancy_val - 5. * scale * sqrt(m_variance))));
78  double sig_high = (m_expectancy_val + 5. * scale * sqrt(m_variance));
79  double step = (sig_high - sig_low) * 1e-3;
80  const double inv_scale = 1. / scale;
81  double shift = val_numerator_f * log(m_expectancy_val * inv_scale)+(-val_numerator_f - val_denominator_f - 2.) * log((m_expectancy_val * inv_scale) + 1.);
82  //double shift = ln_factorialApp(m_val_numerator);
83 
84  double temp_value = sig_low;
85  double arr_position = sig_low;
86 
87  for (int i = 0; i < 1000; ++i) {
88  temp_value = exp(m_val_numerator * log(arr_position * inv_scale)+(-val_numerator_f - val_denominator_f - 2.) * log((arr_position * inv_scale) + 1.) - shift);
89  if (temp_value != temp_value) temp_value = 0.;
90  m_pdf[i] = temp_value;
91  m_integral += temp_value;
92  m_bins[i] = arr_position;
93  arr_position += step;
94  }
95  m_bins[1000] = arr_position;
96  }
97  } else {
98  if (m_val_denominator == 0) {
99  m_expectancy_val = 0.;
100  m_variance = 0.;
101  m_stat_uncert_low = 0.;
102  m_stat_uncert_high = 0.;
103  m_is_nan = true;
104  } else {
105  m_is_nan = false;
106  m_pdf = new double[1000];
107  m_bins = new double[1001];
108 
109  const double inv_val_denominator_f = 1. / val_denominator_f;
110  m_expectancy_val = val_numerator_f * inv_val_denominator_f;
111  m_variance = (m_expectancy_val * (1. - m_expectancy_val)) * inv_val_denominator_f;
112  double var_sqrt = sqrt(m_variance);
113  double sig_low = std::max(0., m_expectancy_val - 5. * var_sqrt);
114  double sig_high = std::min(1., m_expectancy_val + 5. * var_sqrt);
115  if (val_numerator_f == 0) {
116  sig_low = 0.;
117  sig_high = std::min(1., (8. * inv_val_denominator_f));
118  }
119  if (val_numerator_f == val_denominator_f) {
120  sig_low = std::max(0., (1. - 8. * inv_val_denominator_f));
121  sig_high = 1.;
122  }
123 
124  double step = fabs(sig_high - sig_low) * 1e-3;
125  double shift = 0.;
126  if (val_numerator_f == 0 || val_numerator_f == val_denominator_f) {
127  shift = 0.;
128  } else {
129  shift = val_numerator_f * log(m_expectancy_val) + (val_denominator_f - val_numerator_f) * log(1. - m_expectancy_val);
130  }
131 
132  double temp_value = sig_low;
133  double arr_position = sig_low;
134 
135  if (val_numerator_f == 0) {
136  for (int i = 0; i < 1000; ++i) {
137  temp_value = exp(val_denominator_f * log(1. - arr_position) - shift);
138  m_pdf[i] = temp_value;
139  m_integral += temp_value;
140  m_bins[i] = arr_position;
141  arr_position += step;
142  }
143  } else {
144  if (val_numerator_f == val_denominator_f) {
145  for (int i = 0; i < 1000; ++i) {
146  temp_value = exp(val_numerator_f * log(arr_position) - shift);
147  m_pdf[i] = temp_value;
148  m_integral += temp_value;
149  m_bins[i] = arr_position;
150  arr_position += step;
151  }
152  } else {
153  for (int i = 0; i < 1000; ++i) {
154  temp_value = exp(val_numerator_f * log(arr_position) + (val_denominator_f - val_numerator_f) * log(1. - arr_position) - shift);
155  m_pdf[i] = temp_value;
156  m_integral += temp_value;
157  m_bins[i] = arr_position;
158  arr_position += step;
159  }
160  }
161  }
162  m_bins[1000] = arr_position;
163  }
164  }
165 
166 }
167 
168 void APWeightEntry::ReadEfficiency(double efficiency, double err_low, double err_high) {
169  m_val_denominator = 0;
170  m_val_numerator = 0;
171  m_sys_uncert = 0.;
172  m_sys_uncert2 = 0.;
174  m_stat_uncert_low = fabs(err_low);
175  m_stat_uncert_high = fabs(err_high);
178 }
179 
180 void APWeightEntry::SetCoordinates(const std::vector<int>& coords,
181  const std::vector<int>& n_dim_origin) {
182  m_coords.clear();
183  m_n_dim_origin.clear();
184  m_coords = coords;
185  m_n_dim_origin = n_dim_origin;
186 }
187 
189  delete [] m_pdf;
190  m_pdf = 0;
191  delete [] m_bins;
192  m_bins = 0;
193  delete [] m_cumul;
194  m_cumul = 0;
195  delete m_hist;
196 }
197 
198 unsigned int APWeightEntry::GetValDenominator() const {
199  return m_val_denominator;
200 }
201 
202 unsigned int APWeightEntry::GetValNumerator() const {
203  return m_val_numerator;
204 }
205 
207  return m_expectancy_val;
208 }
209 
211  return m_variance;
212 }
213 
215  return m_stat_uncert_low;
216 }
217 
219  return m_stat_uncert_high;
220 }
221 
223  return m_sys_uncert;
224 }
225 
227  return m_sys_uncert2;
228 }
229 
231 
233  if (m_is_nan || m_integral < std::numeric_limits<double>::epsilon()) {
234  return 1e-100;
235  }
236  if (!m_cumul) _ComputeCum();
237  double rn = random->Rndm();
238  int ibin = TMath::BinarySearch(1000, m_cumul, rn);
239  return m_bins[ibin];
240  //if (rn > m_cumul[ibin]) x += fabs(m_bins[1]-m_bins[0]) * (rn-m_cumul[ibin])/(m_cumul[ibin+1] - m_cumul[ibin]);
241 }
242 
244  if (!m_hist) _CreateHist();
245  return m_hist;
246 }
247 
248 unsigned int APWeightEntry::GetID() const {
249  return m_ID;
250 }
251 
252 vector<int> APWeightEntry::GetCoords() const {
253  return m_coords;
254 }
255 
257  return m_n_dim_origin;
258 }
259 
260 bool APWeightEntry::IsNaN() const {
261  return m_is_nan;
262 }
263 
264 bool APWeightEntry::IsTrig() const {
265  return m_is_trig;
266 }
267 
268 void APWeightEntry::SetSystUncert(double rel_uncert) {
269  m_sys_uncert = m_expectancy_val*rel_uncert;
271 }
272 
273 void APWeightEntry::SetID(unsigned int id) {
274  m_ID = id;
275 }
276 
278  m_hist = new TH1F("", "", 1000, m_bins);
279  if (m_is_nan == false) {
280  for (int i = 0; i < 1000; ++i) {
281  m_hist->SetBinContent(i + 1, m_pdf[i]);
282  }
283  }
284 }
285 
287  if (m_cumul) delete [] m_cumul;
288  m_cumul = new double[1001];
289  m_cumul[0] = 0.;
290  for (int i = 0; i < 1000; ++i) {
291  m_cumul[i + 1] = m_cumul[i] + m_pdf[i];
292  }
293  const double inv_normalize = 1. / m_cumul[1000];
294  for (int i = 1; i < 1000; ++i) {
295  m_cumul[i] *= inv_normalize;
296  }
297 }
APWeightEntry::m_coords
std::vector< int > m_coords
Holds the coordinates of the current entry in the original histogram.
Definition: APWeightEntry.h:78
APWeightEntry::m_pdf
double * m_pdf
Definition: APWeightEntry.h:76
APWeightEntry::GetID
unsigned int GetID() const
Returns the internal ID (used by APReweight/APReweight2D/APReweight3D/APReweightND).
Definition: APWeightEntry.cxx:248
APWeightEntry::SetCoordinates
void SetCoordinates(const std::vector< int > &coords, const std::vector< int > &n_dim_origin)
Definition: APWeightEntry.cxx:180
max
#define max(a, b)
Definition: cfImp.cxx:41
APWeightEntry::m_is_trig
bool m_is_trig
Flag, set to true if weight entry is trigger based.
Definition: APWeightEntry.h:72
APWeightEntry::GetValDenominator
unsigned int GetValDenominator() const
Get value of original denominator.
Definition: APWeightEntry.cxx:198
RootUtils::TRandomTLS
Thread-local TRandom generator.
Definition: TRandomTLS.h:29
APWeightEntry::GetStatUncertLow
double GetStatUncertLow() const
Get lower bound of asymmetric statistical uncertainty.
Definition: APWeightEntry.cxx:214
APWeightEntry::GetSysUncert2
double GetSysUncert2() const
Get absolute systematic uncertainty squared value of efficiency/weight.
Definition: APWeightEntry.cxx:226
APWeightEntry::m_variance
double m_variance
Holds Variance of efficiency/weight (classical binomial/poisson model).
Definition: APWeightEntry.h:66
APWeightEntry::m_val_denominator
unsigned int m_val_denominator
Holds the value of original denominator.
Definition: APWeightEntry.h:63
APWeightEntry::_ComputeCum
void _ComputeCum()
Calculates the cumulative function of the pdf if necessary.
Definition: APWeightEntry.cxx:286
APWeightEntry::GetRandom
double GetRandom()
Get random number from PDF.
Definition: APWeightEntry.cxx:230
APWeightEntry::GetValNumerator
unsigned int GetValNumerator() const
Get value of original numerator.
Definition: APWeightEntry.cxx:202
APWeightEntry::GetPDF
TH1F * GetPDF()
Returns the calculated PDF.
Definition: APWeightEntry.cxx:243
drawFromPickle.exp
exp
Definition: drawFromPickle.py:36
yodamerge_tmp.scale
scale
Definition: yodamerge_tmp.py:138
APWeightEntry::m_stat_uncert_high
double m_stat_uncert_high
Holds upper bound of asymmetric statistical uncertainty.
Definition: APWeightEntry.h:68
APWeightEntry::~APWeightEntry
virtual ~APWeightEntry()
Default destructor.
Definition: APWeightEntry.cxx:188
APWeightEntry::m_sys_uncert
double m_sys_uncert
Holds absolute systematic uncertainty value of efficiency/weight.
Definition: APWeightEntry.h:69
efficiency
void efficiency(std::vector< double > &bins, std::vector< double > &values, const std::vector< std::string > &files, const std::string &histname, const std::string &tplotname, const std::string &label="")
Definition: dependence.cxx:128
APWeightEntry::ReadEfficiency
void ReadEfficiency(double efficiency, double err_low, double err_high)
Read efficiencies and upper/lower uncertainty (if numerator/denominator not applicable (e....
Definition: APWeightEntry.cxx:168
lumiFormat.i
int i
Definition: lumiFormat.py:92
APWeightEntry::GetVariance
double GetVariance() const
Get Variance of efficiency/weight (classical binomial/poisson model).
Definition: APWeightEntry.cxx:210
APWeightEntry::m_is_nan
bool m_is_nan
Flag, set to true if denominator is zero.
Definition: APWeightEntry.h:73
MathTools.h
APWeightEntry::m_stat_uncert_low
double m_stat_uncert_low
Holds lower bound of asymmetric statistical uncertainty.
Definition: APWeightEntry.h:67
APWeightEntry::SetID
void SetID(unsigned int id)
Set the internal ID (used by APReweight/APReweight2D/APReweight3D/APReweightND).
Definition: APWeightEntry.cxx:273
python.BunchSpacingUtils.rn
rn
Definition: BunchSpacingUtils.py:87
APWeightEntry::SetSystUncert
void SetSystUncert(double rel_uncert)
Set the relative (!) systematic uncertainty for the efficiency/weight.
Definition: APWeightEntry.cxx:268
APWeightEntry::m_expectancy_val
double m_expectancy_val
Holds the Expectancy value of efficiency/weight.
Definition: APWeightEntry.h:65
xAOD::double
double
Definition: CompositeParticle_v1.cxx:159
APWeightEntry::IsNaN
bool IsNaN() const
Returns true if instance is NaN.
Definition: APWeightEntry.cxx:260
APWeightEntry::m_hist
TH1F * m_hist
Holds the TH1F instance from the arrays if computed.
Definition: APWeightEntry.h:77
min
#define min(a, b)
Definition: cfImp.cxx:40
APWeightEntry::m_val_numerator
unsigned int m_val_numerator
Holds the value of original numerator.
Definition: APWeightEntry.h:64
APWeightEntry::m_n_dim_origin
std::vector< int > m_n_dim_origin
Holds the amount of dimensions and bins per axis in the original histogram.
Definition: APWeightEntry.h:79
APWeightEntry::GetOriginalDimensions
std::vector< int > GetOriginalDimensions() const
Returns the dimensions and amounts of bins for each dimension of the original histogram.
Definition: APWeightEntry.cxx:256
APWeightEntry::APWeightEntry
APWeightEntry()
Default constructor.
Definition: APWeightEntry.cxx:20
id
SG::auxid_t id
Definition: Control/AthContainers/Root/debug.cxx:194
APWeightEntry::IsTrig
bool IsTrig() const
Returns true if instance is trigger based.
Definition: APWeightEntry.cxx:264
APWeightEntry::_CreateHist
void _CreateHist()
Creates a TH1F instance from the arrays if necessary.
Definition: APWeightEntry.cxx:277
APWeightEntry::m_sys_uncert2
double m_sys_uncert2
Holds absolute systematic uncertainty squared value of efficiency/weight.
Definition: APWeightEntry.h:70
APWeightEntry::GetExpectancy
double GetExpectancy() const
Get Expectancy value of efficiency/weight.
Definition: APWeightEntry.cxx:206
APWeightEntry::m_integral
double m_integral
Holds the integral of the probability distribution.
Definition: APWeightEntry.h:75
APWeightEntry.h
APWeightEntry::GetCoords
std::vector< int > GetCoords() const
Returns the coordinates of the current entry in the original histogram.
Definition: APWeightEntry.cxx:252
DiTauMassTools::MaxHistStrategyV2::e
e
Definition: PhysicsAnalysis/TauID/DiTauMassTools/DiTauMassTools/HelperFunctions.h:26
APWeightEntry::GetStatUncertHigh
double GetStatUncertHigh() const
Get upper bound of asymmetric statistical uncertainty.
Definition: APWeightEntry.cxx:218
APWeightEntry::m_cumul
double * m_cumul
Histograms to hold the probability distribution and the cumulative distribution.
Definition: APWeightEntry.h:76
python.CaloCondTools.log
log
Definition: CaloCondTools.py:20
APWeightEntry::m_bins
double * m_bins
Definition: APWeightEntry.h:76
LArCellBinning.step
step
Definition: LArCellBinning.py:158
TRandomTLS.h
Thread-local TRandom generator.
python.TrigEgammaMonitorHelper.TH1F
def TH1F(name, title, nxbins, bins_par2, bins_par3=None, path='', **kwargs)
Definition: TrigEgammaMonitorHelper.py:24
APWeightEntry::GetSysUncert
double GetSysUncert() const
Get absolute systematic uncertainty value of efficiency/weight.
Definition: APWeightEntry.cxx:222
ATLAS_THREAD_SAFE
#define ATLAS_THREAD_SAFE
Definition: checker_macros.h:211
checker_macros.h
Define macros for attributes used to control the static checker.
APWeightEntry::m_ID
unsigned int m_ID
Holds internal ID (used by APReweight/APReweight2D/APReweight3D/APReweightND).
Definition: APWeightEntry.h:74