ATLAS Offline Software
Loading...
Searching...
No Matches
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
13using namespace std;
14
17 m_denominator_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
29APReweight3D::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
75void 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
128APWeightEntry* 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
133APWeightEntry* 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
149unsigned int APReweight3D::NBins() const {
151}
152
153unsigned 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
163unsigned 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
173unsigned 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
183void 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
193void APReweight3D::SetQuietMode(bool isQuiet) {
194 m_isQuiet = isQuiet;
195}
unsigned int m_n_bins_y
Holds the amount of bins in Y.
virtual ~APReweight3D()
Default destructor.
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.
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....
ClassDef(APReweight3D, 1) private TH3D * m_numerator_hist
< Holds the original denominator histogram.
TAxis * m_axis_z
Holds the Z axis of the APReweight3D instance (from input histograms).
std::vector< std::vector< std::vector< APWeightEntry * > > > m_weights
Holds all weight entries.
double GetSampleScale() const
Get scale factor that was calculated from sample sizes upon instantiation.
unsigned int m_n_bins_x
Holds the amount of bins in X.
unsigned int GetBinZ(double value_z) const
Get bin number in Z that corresponds to a given value.
const TH3D * GetNumeratorHist() const
Get original numerator histogram.
TAxis * m_axis_x
Holds the X axis of the APReweight3D instance (from input histograms).
unsigned int NBins() const
Get amount of bins.
APReweight3D()
Default constructor.
unsigned int m_n_bins_z
Holds the amount of bins in Z.
unsigned int GetBinY(double value_y) const
Get bin number in Y that corresponds to a given value.
unsigned int GetBinX(double value_x) const
Get bin number in X that corresponds to a given value.
APWeightEntry * GetWeight(double value_x, double value_y, double value_z) const
Get Weight entry for a given triplet of values.
void SetQuietMode(bool isQuiet=true)
Sets the flag to turn off messages.
void SetSystUncert(double rel_uncert)
Set the global relative (!) systematic uncertainty of all efficiencies/weights.
const TH3D * GetDenominatorHist() const
Get original denominator histogram.
TAxis * m_axis_y
Holds the Y axis of the APReweight3D instance (from input histograms).
double m_scale
Holds the scale factor that was calculated from sample sizes upon instantiation.
APWeightEntry * m_empty_weight
Dummy weight (equals 0.) to return if value out of range is provided.
bool m_isTrig
Flag to determine if the class holds trigger efficiencies or "simple" MC weights.
APReweightBase()
Default constructor.
bool m_isQuiet
Flag to turn off messages.
Class to store a single weight entry (one bin).
void SetID(unsigned int id)
Set the internal ID (used by APReweight/APReweight2D/APReweight3D/APReweightND).
void ReadEfficiency(double efficiency, double err_low, double err_high)
Read efficiencies and upper/lower uncertainty (if numerator/denominator not applicable (e....
void SetSystUncert(double rel_uncert)
Set the relative (!) systematic uncertainty for the efficiency/weight.
void SetCoordinates(const std::vector< int > &coords, const std::vector< int > &n_dim_origin)
STL namespace.