ATLAS Offline Software
APReweight3D.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 #define APReweight3D_cxx
9 #include <iostream>
10 #include "TTree.h"
11 #include "TH3.h"
12 
13 using namespace std;
14 
17  m_denominator_hist(0),
18  m_numerator_hist(0),
19  m_n_bins_x(0),
20  m_n_bins_y(0),
21  m_n_bins_z(0),
22  m_axis_x(0),
23  m_axis_y(0),
24  m_axis_z(0)
25 {
26  m_isQuiet = false;
27 }
28 
29 APReweight3D::APReweight3D(TH3* denominator_in, TH3* numerator_in, bool isTrig) : APReweightBase() {
30  m_empty_weight = new APWeightEntry(0, 0, 1.);
31  m_denominator_hist = (TH3D*) denominator_in->Clone("");
32  m_numerator_hist = (TH3D*) numerator_in->Clone("");
33  m_axis_x = (TAxis*) m_denominator_hist->GetXaxis()->Clone("");
34  m_axis_y = (TAxis*) m_denominator_hist->GetYaxis()->Clone("");
35  m_axis_z = (TAxis*) m_denominator_hist->GetZaxis()->Clone("");
36  m_n_bins_x = m_denominator_hist->GetNbinsX();
37  m_n_bins_y = m_denominator_hist->GetNbinsY();
38  m_n_bins_z = m_denominator_hist->GetNbinsZ();
39  m_scale = (double) denominator_in->GetEntries() / (double) numerator_in->GetEntries();
40  m_isTrig = isTrig;
41  m_isQuiet = false;
42  for (unsigned int i = 0; i < m_n_bins_x; ++i) {
43  m_weights.push_back(vector< vector<APWeightEntry*> >());
44  for (unsigned int j = 0; j < m_n_bins_y; ++j) {
45  m_weights[i].push_back(vector<APWeightEntry*>());
46  for (unsigned int k = 0; k < m_n_bins_z; ++k) {
47  APWeightEntry* temp_entry = new APWeightEntry((unsigned int) m_denominator_hist->GetBinContent(i + 1, j + 1, k + 1), (unsigned int) m_numerator_hist->GetBinContent(i + 1, j + 1, k + 1), m_scale, m_isTrig);
48  vector<int> temp_vec(3,0);
49  vector<int> temp_vec_axes(3,0);
50  temp_vec[0] = i+1;
51  temp_vec[1] = j+1;
52  temp_vec[2] = k+1;
53  temp_vec_axes[0] = m_n_bins_x;
54  temp_vec_axes[1] = m_n_bins_y;
55  temp_vec_axes[2] = m_n_bins_z;
56  temp_entry->SetCoordinates(temp_vec,temp_vec_axes);
57  temp_entry->SetID(m_ID);
58  m_weights[i][j].push_back(temp_entry);
59  }
60  }
61  }
62 
63  if( m_isTrig ) {
64  for( int i = 1; i < m_numerator_hist->GetNbinsX()*m_numerator_hist->GetNbinsY()*m_numerator_hist->GetNbinsZ(); ++i ) {
65  if( m_numerator_hist->GetBinContent(i) > m_denominator_hist->GetBinContent(i) ) {
66  std::cout << "WARNING in APReweight3D::~APReweight3D(TH3* denominator_in, TH3* numerator_in, bool isTrig) : Using histograms " << m_numerator_hist->GetName() << " and " << m_denominator_hist->GetName() << " the efficiency is larger than 1 for bin " << i << "! This is inconsistent and can lead to unwanted behaviour (weights > 1, variance < 0 )! Please check your input histograms. In order to avoid negative variances, the efficiency in this bin will be set to 0." << std::endl;
67  m_numerator_hist->SetBinContent(i,0);
68  }
69  }
70  }
71 
72 
73 }
74 
75 void APReweight3D::ReadEfficiency(TH3* efficiency_in, TH3* err_low_in, TH3* err_high_in) {
76  if (err_high_in == 0) err_high_in = err_low_in;
77  m_empty_weight = new APWeightEntry(0, 0, 1.);
78  m_denominator_hist = new TH3D("", "", 1, 0., 1., 1, 0., 1., 1, 0., 1.);
79  m_numerator_hist = new TH3D("", "", 1, 0., 1., 1, 0., 1., 1, 0., 1.);
80  m_axis_x = (TAxis*) efficiency_in->GetXaxis()->Clone("");
81  m_axis_y = (TAxis*) efficiency_in->GetYaxis()->Clone("");
82  m_axis_z = (TAxis*) efficiency_in->GetZaxis()->Clone("");
83  m_n_bins_x = efficiency_in->GetNbinsX();
84  m_n_bins_y = efficiency_in->GetNbinsY();
85  m_n_bins_z = efficiency_in->GetNbinsZ();
86  m_scale = 1.0;
87  m_isTrig = true;
88  for (unsigned int i = 0; i < m_n_bins_x; ++i) {
89  m_weights.push_back(vector< vector<APWeightEntry*> >());
90  for (unsigned int j = 0; j < m_n_bins_y; ++j) {
91  m_weights[i].push_back(vector<APWeightEntry*>());
92  for (unsigned int k = 0; k < m_n_bins_z; ++k) {
93  APWeightEntry *temp_entry = new APWeightEntry();
94  temp_entry->ReadEfficiency(efficiency_in->GetBinContent(i + 1, j + 1, k + 1), err_low_in->GetBinContent(i + 1, j + 1, k + 1), err_high_in->GetBinContent(i + 1, j + 1, k + 1));
95  vector<int> temp_vec(3,0);
96  vector<int> temp_vec_axes(3,0);
97  temp_vec[0] = i+1;
98  temp_vec[1] = j+1;
99  temp_vec[2] = k+1;
100  temp_vec_axes[0] = m_n_bins_x;
101  temp_vec_axes[1] = m_n_bins_y;
102  temp_vec_axes[2] = m_n_bins_z;
103  temp_entry->SetCoordinates(temp_vec,temp_vec_axes);
104  temp_entry->SetID(m_ID);
105  m_weights[i][j].push_back(temp_entry);
106  }
107  }
108  }
109 }
110 
112  delete m_denominator_hist;
113  delete m_numerator_hist;
114  delete m_empty_weight;
115  delete m_axis_x;
116  delete m_axis_y;
117  delete m_axis_z;
118  for (unsigned int i = 0; i < m_n_bins_x; ++i) {
119  for (unsigned int j = 0; j < m_n_bins_y; ++j) {
120  for (vector<APWeightEntry*>::reverse_iterator it = m_weights[i][j].rbegin(); it != m_weights[i][j].rend(); ++it) {
121  delete *it;
122  }
123  }
124  }
125  m_weights.clear();
126 }
127 
128 APWeightEntry* APReweight3D::GetBinWeight(unsigned int bin_x, unsigned int bin_y, unsigned int bin_z) const {
129  if (bin_x == 0 || bin_y == 0 || bin_z == 0) return m_empty_weight;
130  return m_weights[bin_x - 1][bin_y - 1][bin_z - 1];
131 }
132 
133 APWeightEntry* APReweight3D::GetWeight(double value_x, double value_y, double value_z) const {
134  return GetBinWeight(GetBinX(value_x), GetBinY(value_y), GetBinZ(value_z));
135 }
136 
138  return m_denominator_hist;
139 }
140 
142  return m_numerator_hist;
143 }
144 
146  return m_scale;
147 }
148 
149 unsigned int APReweight3D::NBins() const {
151 }
152 
153 unsigned int APReweight3D::GetBinX(double value) const {
154  for (unsigned int i = 1; i <= m_n_bins_x; ++i) {
155  if (value >= m_axis_x->GetBinLowEdge(i) && value < m_axis_x->GetBinUpEdge(i)) {
156  return i;
157  }
158  }
159  if (!m_isQuiet) cout << "WARNING in APReweight3D::GetBinX: Value out of range! Returning 0." << endl;
160  return 0;
161 }
162 
163 unsigned int APReweight3D::GetBinY(double value) const {
164  for (unsigned int i = 1; i <= m_n_bins_y; ++i) {
165  if (value >= m_axis_y->GetBinLowEdge(i) && value < m_axis_y->GetBinUpEdge(i)) {
166  return i;
167  }
168  }
169  if (!m_isQuiet) cout << "WARNING in APReweight3D::GetBinY: Value out of range! Returning 0." << endl;
170  return 0;
171 }
172 
173 unsigned int APReweight3D::GetBinZ(double value) const {
174  for (unsigned int i = 1; i <= m_n_bins_z; ++i) {
175  if (value >= m_axis_z->GetBinLowEdge(i) && value < m_axis_z->GetBinUpEdge(i)) {
176  return i;
177  }
178  }
179  if (!m_isQuiet) cout << "WARNING in APReweight3D::GetBinZ: Value out of range! Returning 0." << endl;
180  return 0;
181 }
182 
183 void APReweight3D::SetSystUncert(double rel_uncert) {
184  for (unsigned int i = 0; i < m_n_bins_x; ++i) {
185  for (unsigned int j = 0; j < m_n_bins_y; ++j) {
186  for (unsigned int k = 0; k < m_n_bins_z; ++k) {
187  GetBinWeight(i, j, k)->SetSystUncert(rel_uncert);
188  }
189  }
190  }
191 }
192 
193 void APReweight3D::SetQuietMode(bool isQuiet) {
194  m_isQuiet = isQuiet;
195 }
APWeightEntry
Definition: APWeightEntry.h:25
APWeightEntry::SetCoordinates
void SetCoordinates(const std::vector< int > &coords, const std::vector< int > &n_dim_origin)
Definition: APWeightEntry.cxx:180
APReweightBase::m_empty_weight
APWeightEntry * m_empty_weight
Dummy weight (equals 0.) to return if value out of range is provided.
Definition: APReweightBase.h:40
APReweight3D::SetSystUncert
void SetSystUncert(double rel_uncert)
Set the global relative (!) systematic uncertainty of all efficiencies/weights.
Definition: APReweight3D.cxx:183
TH3D
Definition: rootspy.cxx:505
APReweight3D::GetDenominatorHist
const TH3D * GetDenominatorHist() const
Get original denominator histogram.
Definition: APReweight3D.cxx:137
APReweight3D::m_n_bins_z
unsigned int m_n_bins_z
Holds the amount of bins in Z.
Definition: APReweight3D.h:61
APReweight3D::m_weights
std::vector< std::vector< std::vector< APWeightEntry * > > > m_weights
Holds all weight entries.
Definition: APReweight3D.h:58
APReweight3D::GetBinWeight
APWeightEntry * GetBinWeight(unsigned int bin_x, unsigned int bin_y, unsigned int bin_z) const
Get Weight entry for a given triplet of bin numbers.
Definition: APReweight3D.cxx:128
skel.it
it
Definition: skel.GENtoEVGEN.py:423
APReweightBase::m_scale
double m_scale
Holds the scale factor that was calculated from sample sizes upon instantiation.
Definition: APReweightBase.h:36
athena.value
value
Definition: athena.py:122
APReweight3D::m_n_bins_y
unsigned int m_n_bins_y
Holds the amount of bins in Y.
Definition: APReweight3D.h:60
APReweight3D::m_axis_x
TAxis * m_axis_x
Holds the X axis of the APReweight3D instance (from input histograms).
Definition: APReweight3D.h:62
APReweightBase::m_isQuiet
bool m_isQuiet
Flag to turn off messages.
Definition: APReweightBase.h:38
APReweightBase
Definition: APReweightBase.h:23
APReweight3D::APReweight3D
APReweight3D()
Default constructor.
Definition: APReweight3D.cxx:15
APReweight3D::m_n_bins_x
unsigned int m_n_bins_x
Holds the amount of bins in X.
Definition: APReweight3D.h:59
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
APReweightBase::m_isTrig
bool m_isTrig
Flag to determine if the class holds trigger efficiencies or "simple" MC weights.
Definition: APReweightBase.h:37
MathTools.h
TH3
Definition: rootspy.cxx:440
APReweight3D::m_numerator_hist
ClassDef(APReweight3D, 1) private TH3D * m_numerator_hist
< Holds the original denominator histogram.
Definition: APReweight3D.h:53
APWeightEntry::SetID
void SetID(unsigned int id)
Set the internal ID (used by APReweight/APReweight2D/APReweight3D/APReweightND).
Definition: APWeightEntry.cxx:273
APWeightEntry::SetSystUncert
void SetSystUncert(double rel_uncert)
Set the relative (!) systematic uncertainty for the efficiency/weight.
Definition: APWeightEntry.cxx:268
APReweight3D::GetNumeratorHist
const TH3D * GetNumeratorHist() const
Get original numerator histogram.
Definition: APReweight3D.cxx:141
APReweight3D::GetWeight
APWeightEntry * GetWeight(double value_x, double value_y, double value_z) const
Get Weight entry for a given triplet of values.
Definition: APReweight3D.cxx:133
APReweight3D::~APReweight3D
virtual ~APReweight3D()
Default destructor.
Definition: APReweight3D.cxx:111
xAOD::double
double
Definition: CompositeParticle_v1.cxx:159
APReweight3D::GetBinX
unsigned int GetBinX(double value_x) const
Get bin number in X that corresponds to a given value.
Definition: APReweight3D.cxx:153
APReweight3D::ReadEfficiency
void ReadEfficiency(TH3 *efficiency_in, TH3 *err_low_in, TH3 *err_high_in=0)
Read efficiencies and upper/lower uncertainty (if numerator/denominator not applicable (e....
Definition: APReweight3D.cxx:75
APReweight3D::GetSampleScale
double GetSampleScale() const
Get scale factor that was calculated from sample sizes upon instantiation.
Definition: APReweight3D.cxx:145
APWeightEntry.h
APReweight3D::NBins
unsigned int NBins() const
Get amount of bins.
Definition: APReweight3D.cxx:149
APReweight3D::m_axis_y
TAxis * m_axis_y
Holds the Y axis of the APReweight3D instance (from input histograms).
Definition: APReweight3D.h:63
APReweight3D.h
APReweight3D::GetBinY
unsigned int GetBinY(double value_y) const
Get bin number in Y that corresponds to a given value.
Definition: APReweight3D.cxx:163
APReweight3D::m_axis_z
TAxis * m_axis_z
Holds the Z axis of the APReweight3D instance (from input histograms).
Definition: APReweight3D.h:64
APReweight3D::SetQuietMode
void SetQuietMode(bool isQuiet=true)
Sets the flag to turn off messages.
Definition: APReweight3D.cxx:193
APReweight3D::GetBinZ
unsigned int GetBinZ(double value_z) const
Get bin number in Z that corresponds to a given value.
Definition: APReweight3D.cxx:173
fitman.k
k
Definition: fitman.py:528