ATLAS Offline Software
Loading...
Searching...
No Matches
APReweight.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 APReweight_cxx
9#include <iostream>
10#include "TTree.h"
11#include "TH1.h"
12
13using namespace std;
14
17 m_denominator_hist(0),
19 m_n_bins(0),
20 m_axis(0)
21{
22 m_isQuiet = false;
23}
24
25APReweight::APReweight(TTree* denominator, const std::string& denominator_branch, TTree* numerator, const std::string& numerator_branch, unsigned int n_bins, double x_min, double x_max, bool isTrig) : APReweightBase() {
26 m_empty_weight = new APWeightEntry(0, 0, 1.);
27 m_denominator_hist = new TH1D("", "denominator_hist", n_bins, x_min, x_max);
28 m_numerator_hist = new TH1D("", "numerator_hist", n_bins, x_min, x_max);
29 m_axis = (TAxis*) m_denominator_hist->GetXaxis()->Clone("");
30 m_n_bins = m_denominator_hist->GetNbinsX();
31
32 //Readout from Trees and fill histograms
33 vector<double> *denominator_vec = 0, *numerator_vec = 0;
34 TBranch *b_denominator_vec, *b_numerator_vec;
35 denominator->SetBranchAddress(denominator_branch.c_str(), &denominator_vec, &b_denominator_vec);
36 numerator->SetBranchAddress(numerator_branch.c_str(), &numerator_vec, &b_numerator_vec);
37 unsigned long nentries_denominator = denominator->GetEntries(), nentries_numerator = numerator->GetEntries(), counter = 0;
38 while (counter < nentries_denominator) {
39 denominator->GetEntry(counter);
40 for (unsigned int i = 0, I = denominator_vec->size(); i < I; ++i) m_denominator_hist->Fill((*denominator_vec)[i]);
41 ++counter;
42 }
43 counter = 0;
44 while (counter < nentries_numerator) {
45 numerator->GetEntry(counter);
46 for (unsigned int i = 0, I = numerator_vec->size(); i < I; ++i) m_numerator_hist->Fill((*numerator_vec)[i]);
47 ++counter;
48 }
49
50 //Cleanup
51 denominator->ResetBranchAddress(b_denominator_vec);
52 numerator->ResetBranchAddress(b_numerator_vec);
53 delete b_denominator_vec;
54 delete b_numerator_vec;
55 denominator_vec->clear();
56 delete denominator_vec;
57 numerator_vec->clear();
58 delete numerator_vec;
59
60 m_scale = (double) nentries_denominator / (double) nentries_numerator;
61 m_isTrig = isTrig;
62 m_isQuiet = false;
63 for (unsigned int i = 0; i < m_n_bins; ++i) {
64 APWeightEntry* temp_entry = new APWeightEntry((unsigned int) m_denominator_hist->GetBinContent(i + 1), (unsigned int) m_numerator_hist->GetBinContent(i + 1), m_scale, m_isTrig);
65 temp_entry->SetCoordinates(vector<int>(1,i+1),vector<int>(1,m_n_bins));
66 temp_entry->SetID(m_ID);
67 m_weights.push_back(temp_entry);
68 }
69}
70
71APReweight::APReweight(vector<double> denominator, vector<double> numerator, unsigned int n_bins, double x_min, double x_max, bool isTrig) : APReweightBase() {
72 m_empty_weight = new APWeightEntry(0, 0, 1.);
73 m_denominator_hist = new TH1D("", "denominator_hist", n_bins, x_min, x_max);
74 m_numerator_hist = new TH1D("", "numerator_hist", n_bins, x_min, x_max);
75 m_axis = (TAxis*) m_denominator_hist->GetXaxis()->Clone("");
76 m_n_bins = m_denominator_hist->GetNbinsX();
77
78 //Readout from vectors and fill histograms
79 for (unsigned long i = 0, I = denominator.size(); i < I; ++i) m_denominator_hist->Fill(denominator[i]);
80 for (unsigned long i = 0, I = numerator.size(); i < I; ++i) m_numerator_hist->Fill(numerator[i]);
81
82 m_scale = (double) denominator.size() / (double) numerator.size();
83 m_isTrig = isTrig;
84 m_isQuiet = false;
85 for (unsigned int i = 0; i < m_n_bins; ++i) {
86 APWeightEntry* temp_entry = new APWeightEntry((unsigned int) m_denominator_hist->GetBinContent(i + 1), (unsigned int) m_numerator_hist->GetBinContent(i + 1), m_scale, m_isTrig);
87 temp_entry->SetCoordinates(vector<int>(1,i+1),vector<int>(1,m_n_bins));
88 temp_entry->SetID(m_ID);
89 m_weights.push_back(temp_entry);
90 }
91}
92
93APReweight::APReweight(TH1* denominator_in, TH1* numerator_in, bool isTrig) : APReweightBase() {
94 m_empty_weight = new APWeightEntry(0, 0, 1.);
95 m_denominator_hist = (TH1D*) denominator_in->Clone("");
96 m_numerator_hist = (TH1D*) numerator_in->Clone("");
97 m_axis = (TAxis*) m_denominator_hist->GetXaxis()->Clone("");
98 m_n_bins = m_denominator_hist->GetNbinsX();
99 m_scale = (double) denominator_in->GetEntries() / (double) numerator_in->GetEntries();
100 m_isTrig = isTrig;
101 m_isQuiet = false;
102 for (unsigned int i = 0; i < m_n_bins; ++i) {
103 APWeightEntry* temp_entry = new APWeightEntry((unsigned int) m_denominator_hist->GetBinContent(i + 1), (unsigned int) m_numerator_hist->GetBinContent(i + 1), m_scale, m_isTrig);
104 temp_entry->SetCoordinates(vector<int>(1,i+1),vector<int>(1,m_n_bins));
105 temp_entry->SetID(m_ID);
106 m_weights.push_back(temp_entry);
107 }
108
109 if( m_isTrig ) {
110 for( int i = 1; i < m_numerator_hist->GetNbinsX(); ++i ) {
111 if( m_numerator_hist->GetBinContent(i) > m_denominator_hist->GetBinContent(i) ) {
112 std::cout << "WARNING in APReweight::~APReweight(TH1* denominator_in, TH1* 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;
113 m_numerator_hist->SetBinContent(i,0);
114 }
115 }
116 }
117
118}
119
120void APReweight::ReadEfficiency(TH1* efficiency_in, TH1* err_low_in, TH1* err_high_in) {
121 if (err_high_in == 0) err_high_in = err_low_in;
122 m_empty_weight = new APWeightEntry(0, 0, 1.);
123 m_denominator_hist = new TH1D("", "", 1, 0., 1.);
124 m_numerator_hist = new TH1D("", "", 1, 0., 1.);
125 m_axis = (TAxis*) efficiency_in->GetXaxis()->Clone("");
126 m_n_bins = efficiency_in->GetNbinsX();
127 m_scale = 1.0;
128 m_isTrig = true;
129 for (unsigned int i = 0; i < m_n_bins; ++i) {
130 APWeightEntry *temp_entry = new APWeightEntry();
131 temp_entry->ReadEfficiency(efficiency_in->GetBinContent(i + 1), err_low_in->GetBinContent(i + 1), err_high_in->GetBinContent(i + 1));
132 temp_entry->SetCoordinates(vector<int>(1,i+1),vector<int>(1,m_n_bins));
133 temp_entry->SetID(m_ID);
134 m_weights.push_back(temp_entry);
135 }
136}
137
139 delete m_denominator_hist;
140 delete m_numerator_hist;
141 delete m_empty_weight;
142 delete m_axis;
143 for (vector<APWeightEntry*>::reverse_iterator it = m_weights.rbegin(); it != m_weights.rend(); ++it) {
144 delete *it;
145 }
146 m_weights.clear();
147}
148
150 if (bin == 0) return m_empty_weight;
151 return m_weights[bin - 1];
152}
153
155 return GetBinWeight(GetBin(value));
156}
157
159 return m_denominator_hist;
160}
161
162const TH1D* APReweight::GetNumeratorHist() const {
163 return m_numerator_hist;
164}
165
167 return m_scale;
168}
169
170unsigned int APReweight::NBins() const {
171 return m_n_bins;
172}
173
174unsigned int APReweight::GetBin(double value) const {
175 for (unsigned int i = 1; i <= m_n_bins; ++i) {
176 if (value >= m_axis->GetBinLowEdge(i) && value <= m_axis->GetBinUpEdge(i)) {
177 return i;
178 }
179 }
180 if (!m_isQuiet) cout << "WARNING in APReweight::GetBin: Value out of range! Returning 0." << endl;
181 return 0;
182}
183
184void APReweight::SetSystUncert(double rel_uncert) {
185 for (unsigned int i = 1; i <= m_n_bins; ++i) {
186 GetBinWeight(i)->SetSystUncert(rel_uncert);
187 }
188}
189
190void APReweight::SetQuietMode(bool isQuiet) {
191 m_isQuiet = isQuiet;
192}
#define I(x, y, z)
Definition MD5.cxx:116
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.
const TH1D * GetDenominatorHist() const
Get original denominator histogram.
APReweight()
Default constructor.
virtual ~APReweight()
Default destructor.
void SetQuietMode(bool isQuiet=true)
Sets the flag to turn off messages.
unsigned int m_n_bins
Holds the amount of bins.
Definition APReweight.h:60
void ReadEfficiency(TH1 *efficiency_in, TH1 *err_low_in, TH1 *err_high_in=0)
Read efficiencies and upper/lower uncertainty (if numerator/denominator not applicable (e....
ClassDef(APReweight, 1) private TH1D * m_numerator_hist
< Holds the original denominator histogram.
Definition APReweight.h:54
double GetSampleScale() const
Get scale factor that was calculated from sample sizes upon instantiation.
APWeightEntry * GetWeight(double value) const
Get Weight entry for a given value.
APWeightEntry * GetBinWeight(unsigned int bin) const
Get Weight entry for a given bin number.
std::vector< APWeightEntry * > m_weights
Holds all weight entries.
Definition APReweight.h:59
unsigned int NBins() const
Get amount of bins.
const TH1D * GetNumeratorHist() const
Get original numerator histogram.
unsigned int GetBin(double value) const
Get bin number that corresponds to a given value.
TAxis * m_axis
Holds the axis of the APReweight instance (from input histograms).
Definition APReweight.h:61
void SetSystUncert(double rel_uncert)
Set the global relative (!) systematic uncertainty of all efficiencies/weights.
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.