ATLAS Offline Software
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 
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_axis_x(0),
22  m_axis_y(0)
23 {
24  m_isQuiet = false;
25 }
26 
27 APReweight2D::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 
66 void 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 
109 APWeightEntry* 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 
114 APWeightEntry* APReweight2D::GetWeight(double value_x, double value_y) const {
115  return GetBinWeight(GetBinX(value_x), GetBinY(value_y));
116 }
117 
118 const TH2D* APReweight2D::GetDenominatorHist() const {
119  return m_denominator_hist;
120 }
121 
122 const TH2D* APReweight2D::GetNumeratorHist() const {
123  return m_numerator_hist;
124 }
125 
127  return m_scale;
128 }
129 
130 unsigned int APReweight2D::NBins() const {
131  return m_n_bins_x*m_n_bins_y;
132 }
133 
134 unsigned 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 
144 unsigned 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 
154 void 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 
162 void APReweight2D::SetQuietMode(bool isQuiet) {
163  m_isQuiet = isQuiet;
164 }
APReweight2D::GetBinWeight
APWeightEntry * GetBinWeight(unsigned int bin_x, unsigned int bin_y) const
Get Weight entry for a given pair of bin numbers.
Definition: APReweight2D.cxx:109
APReweight2D::APReweight2D
APReweight2D()
Default constructor.
Definition: APReweight2D.cxx:15
APReweight2D::GetNumeratorHist
const TH2D * GetNumeratorHist() const
Get original numerator histogram.
Definition: APReweight2D.cxx:122
APReweight2D::ReadEfficiency
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....
Definition: APReweight2D.cxx:66
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
APReweight2D::GetDenominatorHist
const TH2D * GetDenominatorHist() const
Get original denominator histogram.
Definition: APReweight2D.cxx:118
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
APReweight2D::SetQuietMode
void SetQuietMode(bool isQuiet=true)
Sets the flag to turn off messages.
Definition: APReweight2D.cxx:162
APReweight2D::GetBinX
unsigned int GetBinX(double value_x) const
Get bin number in X that corresponds to a given value.
Definition: APReweight2D.cxx:134
skel.it
it
Definition: skel.GENtoEVGEN.py:396
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:124
APReweightBase::m_isQuiet
bool m_isQuiet
Flag to turn off messages.
Definition: APReweightBase.h:38
APReweight2D::m_axis_y
TAxis * m_axis_y
Holds the Y axis of the APReweight2D instance (from input histograms).
Definition: APReweight2D.h:61
APReweightBase
Definition: APReweightBase.h:23
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:85
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
APReweight2D::m_axis_x
TAxis * m_axis_x
Holds the X axis of the APReweight2D instance (from input histograms).
Definition: APReweight2D.h:60
APReweight2D.h
APWeightEntry::SetID
void SetID(unsigned int id)
Set the internal ID (used by APReweight/APReweight2D/APReweight3D/APReweightND).
Definition: APWeightEntry.cxx:273
APReweight2D::m_n_bins_x
unsigned int m_n_bins_x
Holds the amount of bins in X.
Definition: APReweight2D.h:58
APWeightEntry::SetSystUncert
void SetSystUncert(double rel_uncert)
Set the relative (!) systematic uncertainty for the efficiency/weight.
Definition: APWeightEntry.cxx:268
xAOD::double
double
Definition: CompositeParticle_v1.cxx:159
APReweight2D::SetSystUncert
void SetSystUncert(double rel_uncert)
Set the global relative (!) systematic uncertainty of all efficiencies/weights.
Definition: APReweight2D.cxx:154
APReweight2D::GetWeight
APWeightEntry * GetWeight(double value_x, double value_y) const
Get Weight entry for a given pair of values.
Definition: APReweight2D.cxx:114
APWeightEntry.h
APReweight2D::NBins
unsigned int NBins() const
Get amount of bins.
Definition: APReweight2D.cxx:130
APReweight2D::GetBinY
unsigned int GetBinY(double value_y) const
Get bin number in Y that corresponds to a given value.
Definition: APReweight2D.cxx:144
APReweight2D::m_n_bins_y
unsigned int m_n_bins_y
Holds the amount of bins in Y.
Definition: APReweight2D.h:59
APReweight2D::m_weights
std::vector< std::vector< APWeightEntry * > > m_weights
Holds all weight entries.
Definition: APReweight2D.h:57
APReweight2D::~APReweight2D
virtual ~APReweight2D()
Default destructor.
Definition: APReweight2D.cxx:95
APReweight2D::GetSampleScale
double GetSampleScale() const
Get scale factor that was calculated from sample sizes upon instantiation.
Definition: APReweight2D.cxx:126
APReweight2D::m_numerator_hist
ClassDef(APReweight2D, 1) private TH2D * m_numerator_hist
< Holds the original denominator histogram.
Definition: APReweight2D.h:52