ATLAS Offline Software
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 
15 using namespace std;
16 
18  : APReweightBase(),
19  m_n_dim(0),
20  m_denominator_hist(0),
21  m_numerator_hist(0)
22 {
23  m_isQuiet = false;
24 }
25 
26 APReweightND::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 
77 void 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);
122  temp_weight->SetSystUncert(m_syst_uncert_global);
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 
141 const THnSparse* APReweightND::GetDenominatorHist() const {
142  return m_denominator_hist;
143 }
144 
145 const THnSparse* APReweightND::GetNumeratorHist() const {
146  return m_numerator_hist;
147 }
148 
150  return m_scale;
151 }
152 
153 unsigned 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 
159 void 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 
166 void APReweightND::SetQuietMode(bool isQuiet) {
167  m_isQuiet = isQuiet;
168 }
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
APReweightND::GetDenominatorHist
const THnSparse * GetDenominatorHist() const
Get original denominator histogram.
Definition: APReweightND.cxx:141
APReweightND::GetWeight
APWeightEntry * GetWeight(double value[])
Get Weight entry for a given n-tuple of values.
Definition: APReweightND.cxx:130
APReweightND::GetBinWeight
APWeightEntry * GetBinWeight(const int bin[])
Get Weight entry for a given n-tuple of bin numbers.
Definition: APReweightND.cxx:114
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
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
skel.it
it
Definition: skel.GENtoEVGEN.py:423
APReweightND::SetSystUncert
void SetSystUncert(double rel_uncert)
Set the global relative (!) systematic uncertainty of all efficiencies/weights.
Definition: APReweightND.cxx:159
bin
Definition: BinsDiffFromStripMedian.h:43
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:122
APReweightBase::m_isQuiet
bool m_isQuiet
Flag to turn off messages.
Definition: APReweightBase.h:38
APReweightBase
Definition: APReweightBase.h:23
efficiency
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="")
Definition: dependence.cxx:128
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:92
APReweightND.h
APReweightBase::m_isTrig
bool m_isTrig
Flag to determine if the class holds trigger efficiencies or "simple" MC weights.
Definition: APReweightBase.h:37
ret
T ret(T t)
Definition: rootspy.cxx:260
APReweightND::GetSampleScale
double GetSampleScale() const
Get scale factor that was calculated from sample sizes upon instantiation.
Definition: APReweightND.cxx:149
APReweightND::NBins
unsigned int NBins() const
Get amount of bins.
Definition: APReweightND.cxx:153
MathTools.h
APReweightND::ReadEfficiency
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....
Definition: APReweightND.cxx:77
APWeightEntry::SetID
void SetID(unsigned int id)
Set the internal ID (used by APReweight/APReweight2D/APReweight3D/APReweightND).
Definition: APWeightEntry.cxx:273
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
APReweightND::~APReweightND
virtual ~APReweightND()
Default destructor.
Definition: APReweightND.cxx:100
APReweightND::m_axes
std::vector< TAxis * > m_axes
Holds all axes of the APReweightND instance (from input histograms).
Definition: APReweightND.h:57
APReweightND::APReweightND
APReweightND()
Default constructor.
Definition: APReweightND.cxx:17
APReweightBase::m_syst_uncert_global
double m_syst_uncert_global
Holds the global relative (!) systematic uncertainty of all efficiencies/weights.
Definition: APReweightBase.h:39
APWeightEntry.h
APReweightND::GetNumeratorHist
const THnSparse * GetNumeratorHist() const
Get original numerator histogram.
Definition: APReweightND.cxx:145
APReweightND::SetQuietMode
void SetQuietMode(bool isQuiet=true)
Sets the flag to turn off messages.
Definition: APReweightND.cxx:166
APReweightND::m_weights
std::map< std::vector< int >, APWeightEntry * > m_weights
Holds all weight entries.
Definition: APReweightND.h:56
I
#define I(x, y, z)
Definition: MD5.cxx:116
fitman.k
k
Definition: fitman.py:528
APReweightND::m_numerator_hist
THnSparse * m_numerator_hist
Holds the original numerator histogram.
Definition: APReweightND.h:55
APReweightND::m_denominator_hist
ClassDef(APReweightND, 1) private THnSparse * m_denominator_hist
< Holds the amount of dimensions.
Definition: APReweightND.h:50