ATLAS Offline Software
Loading...
Searching...
No Matches
APReweightND.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 APReweightND_cxx
9#include <iostream>
10#include <cstdlib>
11#include "TTree.h"
12#include "THnSparse.h"
13#include "TAxis.h"
14
15using namespace std;
16
19 m_n_dim(0),
22{
23 m_isQuiet = false;
24}
25
26APReweightND::APReweightND(THnSparse* denominator_in, THnSparse* numerator_in, bool isTrig) : APReweightBase() {
27 m_empty_weight = new APWeightEntry(0,0,1.);
28 m_denominator_hist = (THnSparse*)denominator_in->Clone("");
29 m_numerator_hist = (THnSparse*)numerator_in->Clone("");
30 m_n_dim = m_denominator_hist->GetNdimensions();
31 for (unsigned int i = 0; i < m_n_dim; ++i) m_axes.push_back( (TAxis*)m_denominator_hist->GetAxis(i)->Clone("") );
32 m_scale = (double)denominator_in->GetEntries() / (double)numerator_in->GetEntries();
33 m_isTrig = isTrig;
34 m_isQuiet = false;
36
37 if( m_isTrig ) {
38 std::vector<int> coords(m_n_dim);
39 for( unsigned int i = 0; i < m_n_dim; ++i ) coords[i] = 1;
40 bool checkComplete = false;
41 while( 1 ) {
42 if( m_numerator_hist->GetBinContent(coords.data()) > m_denominator_hist->GetBinContent(coords.data()) ) {
43 std::cout << "WARNING in APReweightND::~APReweightND(THnSparse* denominator_in, THnSparse* numerator_in, bool isTrig) : Using histograms " << m_numerator_hist->GetName() << " and " << m_denominator_hist->GetName() << " the efficiency is larger than 1 for coordinates [ "; for( unsigned int j = 0; j < m_n_dim; ++j ) { std::cout << coords[j] << " "; } std::cout << " ]! This is inconsisten 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;
44 m_numerator_hist->SetBinContent(coords.data(), 0);
45 }
46
47 checkComplete = true;
48 for( unsigned int j = 0; j < m_n_dim; ++j ) {
49 if( coords[j] != m_numerator_hist->GetAxis(j)->GetNbins() ) checkComplete = false;
50 }
51 if( checkComplete ) break;
52
53 int completeDimension = -1;
54 for( unsigned int j = 0; j < m_n_dim; ++j ) {
55 if( coords[j] == m_numerator_hist->GetAxis(j)->GetNbins() ) {
56 bool isComplete = true;
57 for( int k = (int)j-1; k >= 0; --k ) {
58 if( coords[k] != m_numerator_hist->GetAxis(k)->GetNbins() ) isComplete = false;
59 }
60 if( isComplete ) completeDimension = j;
61 }
62 }
63
64 if( completeDimension != -1 ) {
65 for( int j = 0; j <= completeDimension; ++j ) coords[j] = 1;
66 coords[completeDimension+1] += 1;
67 }
68 else {
69 coords[0] += 1;
70 }
71
72 }
73 }
74
75}
76
77void APReweightND::ReadEfficiency(THnSparse* efficiency_in, THnSparse* err_low_in, THnSparse* err_high_in) {
78 if (err_high_in == 0) err_high_in = err_low_in;
79 m_empty_weight = new APWeightEntry(0,0,1.);
80 m_denominator_hist = new THnSparseD();
81 m_numerator_hist = new THnSparseD();
82 m_n_dim = efficiency_in->GetNdimensions();
83 for (unsigned int i = 0; i < m_n_dim; ++i) m_axes.push_back( (TAxis*)efficiency_in->GetAxis(i)->Clone("") );
84 m_scale = 1.0;
85 m_isTrig = true;
87 vector<int> temp_vec_axes(m_n_dim,0);
88 for (unsigned int j = 0; j < m_n_dim; ++j) temp_vec_axes[j] = efficiency_in->GetAxis(j)->GetNbins();
89 for (int i = 0, I = efficiency_in->GetNbins(); i < I; ++i) {
90 std::vector<int> coords (m_n_dim);
91 double efficiency = efficiency_in->GetBinContent( i,coords.data() );
92 APWeightEntry *temp_entry = new APWeightEntry();
93 temp_entry->ReadEfficiency(efficiency,err_low_in->GetBinContent(err_low_in->GetBin(coords.data())),err_high_in->GetBinContent(err_high_in->GetBin(coords.data())));
94 temp_entry->SetCoordinates(coords,temp_vec_axes);
95 temp_entry->SetID(m_ID);
96 m_weights[coords] = temp_entry;
97 }
98}
99
101 delete m_denominator_hist;
102 delete m_numerator_hist;
103 delete m_empty_weight;
104 for (vector<TAxis*>::reverse_iterator it=m_axes.rbegin(); it != m_axes.rend(); ++it) {
105 delete *it;
106 }
107 for (map< vector<int>, APWeightEntry* >::reverse_iterator it=m_weights.rbegin(); it != m_weights.rend(); ++it) {
108 delete it->second;
109 }
110 m_weights.clear();
111 m_axes.clear();
112}
113
115 vector<int> temp_vec(&bin[0],&bin[m_n_dim]);
116 map< vector<int> , APWeightEntry* >::iterator temp_it = m_weights.find(temp_vec);
117 if ( temp_it == m_weights.end() ) {
118 APWeightEntry* temp_weight = new APWeightEntry((unsigned int)m_denominator_hist->GetBinContent(m_denominator_hist->GetBin(bin)),(unsigned int)m_numerator_hist->GetBinContent(m_numerator_hist->GetBin(bin)),m_scale, m_isTrig);
119 vector<int> temp_vec_axes(m_n_dim,0);
120 for (unsigned int i = 0; i < m_n_dim; ++i) temp_vec_axes[i] = m_denominator_hist->GetAxis(i)->GetNbins();
121 temp_weight->SetCoordinates(temp_vec,temp_vec_axes);
123 temp_weight->SetID(m_ID);
124 m_weights[temp_vec] = temp_weight;
125 return temp_weight;
126 }
127 return (*temp_it).second;
128}
129
131 vector<int> temp_bin;
132 for (unsigned int i = 0; i < m_n_dim; ++i) {
133 if ( value[i] < (m_axes[i])->GetXmin() || value[i] > (m_axes[i])->GetXmax() ) {
134 if (!m_isQuiet) cout << "WARNING in APReweightND::GetBin: Value out of range! Dim: " << i << ", value: " << value[i] << ", return value: " << (m_axes[i])->FindFixBin(value[i]) << endl;
135 }
136 temp_bin.push_back((m_axes[i])->FindFixBin(value[i]));
137 }
138 return GetBinWeight(&temp_bin[0]);
139}
140
141const THnSparse* APReweightND::GetDenominatorHist() const {
142 return m_denominator_hist;
143}
144
145const THnSparse* APReweightND::GetNumeratorHist() const {
146 return m_numerator_hist;
147}
148
150 return m_scale;
151}
152
153unsigned int APReweightND::NBins() const {
154 unsigned int ret = 0;
155 for (unsigned int i = 0; i < m_n_dim; ++i) ret += (m_axes[i])->GetNbins();
156 return ret;
157}
158
159void APReweightND::SetSystUncert(double rel_uncert) {
160 m_syst_uncert_global = rel_uncert;
161 for ( map< vector<int> , APWeightEntry* >::const_iterator temp_it = m_weights.begin(); temp_it != m_weights.end(); ++temp_it ) {
162 ((*temp_it).second)->SetSystUncert(rel_uncert);
163 }
164}
165
166void APReweightND::SetQuietMode(bool isQuiet) {
167 m_isQuiet = isQuiet;
168}
#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.
double m_syst_uncert_global
Holds the global relative (!) systematic uncertainty of all efficiencies/weights.
APReweightBase()
Default constructor.
bool m_isQuiet
Flag to turn off messages.
void SetQuietMode(bool isQuiet=true)
Sets the flag to turn off messages.
const THnSparse * GetDenominatorHist() const
Get original denominator histogram.
std::vector< TAxis * > m_axes
Holds all axes of the APReweightND instance (from input histograms).
std::map< std::vector< int >, APWeightEntry * > m_weights
Holds all weight entries.
APWeightEntry * GetBinWeight(const int bin[])
Get Weight entry for a given n-tuple of bin numbers.
double GetSampleScale() const
Get scale factor that was calculated from sample sizes upon instantiation.
unsigned int NBins() const
Get amount of bins.
void SetSystUncert(double rel_uncert)
Set the global relative (!) systematic uncertainty of all efficiencies/weights.
THnSparse * m_numerator_hist
Holds the original numerator histogram.
virtual ~APReweightND()
Default destructor.
APReweightND()
Default constructor.
void ReadEfficiency(THnSparse *efficiency_in, THnSparse *err_low_in, THnSparse *err_high_in=0)
Read efficiencies and upper/lower uncertainty (if numerator/denominator not applicable (e....
ClassDef(APReweightND, 1) private THnSparse * m_denominator_hist
< Holds the amount of dimensions.
APWeightEntry * GetWeight(double value[])
Get Weight entry for a given n-tuple of values.
const THnSparse * GetNumeratorHist() const
Get original numerator histogram.
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)
void efficiency(std::vector< double > &bins, std::vector< double > &values, const std::vector< std::string > &files, const std::string &histname, const std::string &tplotname, const std::string &label="")
STL namespace.