ATLAS Offline Software
Loading...
Searching...
No Matches
APReweight2D.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 APReweight2D_cxx
9#include <iostream>
10#include "TTree.h"
11#include "TH2.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_axis_x(0),
22 m_axis_y(0)
23{
24 m_isQuiet = false;
25}
26
27APReweight2D::APReweight2D(TH2* denominator_in, TH2* numerator_in, bool isTrig) : APReweightBase() {
28 m_empty_weight = new APWeightEntry(0, 0, 1.);
29 m_denominator_hist = (TH2D*) denominator_in->Clone("");
30 m_numerator_hist = (TH2D*) numerator_in->Clone("");
31 m_axis_x = (TAxis*) m_denominator_hist->GetXaxis()->Clone("");
32 m_axis_y = (TAxis*) m_denominator_hist->GetYaxis()->Clone("");
33 m_n_bins_x = m_denominator_hist->GetNbinsX();
34 m_n_bins_y = m_denominator_hist->GetNbinsY();
35 m_scale = (double) denominator_in->GetEntries() / (double) numerator_in->GetEntries();
36 m_isTrig = isTrig;
37 m_isQuiet = false;
38 for (unsigned int i = 0; i < m_n_bins_x; ++i) {
39 m_weights.push_back(vector<APWeightEntry*>());
40 for (unsigned int j = 0; j < m_n_bins_y; ++j) {
41 APWeightEntry* temp_entry = new APWeightEntry((unsigned int) m_denominator_hist->GetBinContent(i + 1, j + 1), (unsigned int) m_numerator_hist->GetBinContent(i + 1, j + 1), m_scale, m_isTrig);
42 vector<int> temp_vec(2,0);
43 vector<int> temp_vec_axes(2,0);
44 temp_vec[0] = i+1;
45 temp_vec[1] = j+1;
46 temp_vec_axes[0] = m_n_bins_x;
47 temp_vec_axes[1] = m_n_bins_y;
48 temp_entry->SetCoordinates(temp_vec,temp_vec_axes);
49 temp_entry->SetID(m_ID);
50 m_weights[i].push_back(temp_entry);
51 }
52 }
53
54 if( m_isTrig ) {
55 for( int i = 1; i < m_numerator_hist->GetNbinsX()*m_numerator_hist->GetNbinsY(); ++i ) {
56 if( m_numerator_hist->GetBinContent(i) > m_denominator_hist->GetBinContent(i) ) {
57 std::cout << "WARNING in APReweight2D::~APReweight2D(TH2* denominator_in, TH2* 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;
58 m_numerator_hist->SetBinContent(i,0);
59 }
60 }
61 }
62
63
64}
65
66void APReweight2D::ReadEfficiency(TH2* efficiency_in, TH2* err_low_in, TH2* err_high_in) {
67 if (err_high_in == 0) err_high_in = err_low_in;
68 m_empty_weight = new APWeightEntry(0, 0, 1.);
69 m_denominator_hist = new TH2D("", "", 1, 0., 1., 1, 0., 1.);
70 m_numerator_hist = new TH2D("", "", 1, 0., 1., 1, 0., 1.);
71 m_axis_x = (TAxis*) efficiency_in->GetXaxis()->Clone("");
72 m_axis_y = (TAxis*) efficiency_in->GetYaxis()->Clone("");
73 m_n_bins_x = efficiency_in->GetNbinsX();
74 m_n_bins_y = efficiency_in->GetNbinsY();
75 m_scale = 1.0;
76 m_isTrig = true;
77 for (unsigned int i = 0; i < m_n_bins_x; ++i) {
78 m_weights.push_back(vector<APWeightEntry*>());
79 for (unsigned int j = 0; j < m_n_bins_y; ++j) {
80 APWeightEntry *temp_entry = new APWeightEntry();
81 temp_entry->ReadEfficiency(efficiency_in->GetBinContent(i + 1, j + 1), err_low_in->GetBinContent(i + 1, j + 1), err_high_in->GetBinContent(i + 1, j + 1));
82 vector<int> temp_vec(2,0);
83 vector<int> temp_vec_axes(2,0);
84 temp_vec[0] = i+1;
85 temp_vec[1] = j+1;
86 temp_vec_axes[0] = m_n_bins_x;
87 temp_vec_axes[1] = m_n_bins_y;
88 temp_entry->SetCoordinates(temp_vec,temp_vec_axes);
89 temp_entry->SetID(m_ID);
90 m_weights[i].push_back(temp_entry);
91 }
92 }
93}
94
96 delete m_denominator_hist;
97 delete m_numerator_hist;
98 delete m_axis_x;
99 delete m_axis_y;
100 delete m_empty_weight;
101 for (unsigned int i = 0; i < m_n_bins_x; ++i) {
102 for (vector<APWeightEntry*>::reverse_iterator it = m_weights[i].rbegin(); it != m_weights[i].rend(); ++it) {
103 delete *it;
104 }
105 }
106 m_weights.clear();
107}
108
109APWeightEntry* APReweight2D::GetBinWeight(unsigned int bin_x, unsigned int bin_y) const {
110 if (bin_x == 0 || bin_y == 0) return m_empty_weight;
111 return m_weights[bin_x - 1][bin_y - 1];
112}
113
114APWeightEntry* APReweight2D::GetWeight(double value_x, double value_y) const {
115 return GetBinWeight(GetBinX(value_x), GetBinY(value_y));
116}
117
119 return m_denominator_hist;
120}
121
123 return m_numerator_hist;
124}
125
127 return m_scale;
128}
129
130unsigned int APReweight2D::NBins() const {
131 return m_n_bins_x*m_n_bins_y;
132}
133
134unsigned int APReweight2D::GetBinX(double value) const {
135 for (unsigned int i = 1; i <= m_n_bins_x; ++i) {
136 if (value >= m_axis_x->GetBinLowEdge(i) && value < m_axis_x->GetBinUpEdge(i)) {
137 return i;
138 }
139 }
140 if (!m_isQuiet) cout << "WARNING in APReweight2D::GetBinX: Value out of range! Returning 0." << endl;
141 return 0;
142}
143
144unsigned int APReweight2D::GetBinY(double value) const {
145 for (unsigned int i = 1; i <= m_n_bins_y; ++i) {
146 if (value >= m_axis_y->GetBinLowEdge(i) && value < m_axis_y->GetBinUpEdge(i)) {
147 return i;
148 }
149 }
150 if (!m_isQuiet) cout << "WARNING in APReweight2D::GetBinY: Value out of range! Returning 0." << endl;
151 return 0;
152}
153
154void APReweight2D::SetSystUncert(double rel_uncert) {
155 for (unsigned int i = 0; i < m_n_bins_x; ++i) {
156 for (unsigned int j = 0; j < m_n_bins_y; ++j) {
157 GetBinWeight(i,j)->SetSystUncert(rel_uncert);
158 }
159 }
160}
161
162void APReweight2D::SetQuietMode(bool isQuiet) {
163 m_isQuiet = isQuiet;
164}
APWeightEntry * GetWeight(double value_x, double value_y) const
Get Weight entry for a given pair of values.
virtual ~APReweight2D()
Default destructor.
const TH2D * GetDenominatorHist() const
Get original denominator histogram.
unsigned int m_n_bins_y
Holds the amount of bins in Y.
unsigned int GetBinY(double value_y) const
Get bin number in Y that corresponds to a given value.
double GetSampleScale() const
Get scale factor that was calculated from sample sizes upon instantiation.
void SetSystUncert(double rel_uncert)
Set the global relative (!) systematic uncertainty of all efficiencies/weights.
unsigned int m_n_bins_x
Holds the amount of bins in X.
TAxis * m_axis_x
Holds the X axis of the APReweight2D instance (from input histograms).
TAxis * m_axis_y
Holds the Y axis of the APReweight2D instance (from input histograms).
ClassDef(APReweight2D, 1) private TH2D * m_numerator_hist
< Holds the original denominator histogram.
APReweight2D()
Default constructor.
const TH2D * GetNumeratorHist() const
Get original numerator histogram.
std::vector< std::vector< APWeightEntry * > > m_weights
Holds all weight entries.
void SetQuietMode(bool isQuiet=true)
Sets the flag to turn off messages.
unsigned int NBins() const
Get amount of bins.
unsigned int GetBinX(double value_x) const
Get bin number in X that corresponds to a given value.
void ReadEfficiency(TH2 *efficiency_in, TH2 *err_low_in, TH2 *err_high_in=0)
Read efficiencies and upper/lower uncertainty (if numerator/denominator not applicable (e....
APWeightEntry * GetBinWeight(unsigned int bin_x, unsigned int bin_y) const
Get Weight entry for a given pair of bin numbers.
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.