ATLAS Offline Software
Loading...
Searching...
No Matches
APWeightSumEnsemble.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
3*/
4
5#define APWeightSumEnsemble_cxx
7#include <cmath>
8#include <iostream>
9#include "TH1F.h"
10#include "TVirtualFitter.h"
12
21
25
27 m_rel_prec = rel_prec;
28 m_ensembleTest_done = false;
29}
30
36
37double APWeightSumEnsemble::GetQuantile(const double prob) {
38 if (!m_ensembleTest_done) Compute();
39 double prob_in[1] = {prob};
40 double quant[1];
41 m_pdf->GetQuantiles(1, quant, prob_in);
42 return quant[0];
43}
44
46 if (!m_ensembleTest_done) Compute();
47 return m_pdf->GetRandom();
48}
49
54
59
61 if (!m_ensembleTest_done) Compute();
62 return m_pdf;
63}
64
65void APWeightSumEnsemble::FinishEvt(double ext_weight) {
66 APWeightSum::FinishEvt(ext_weight);
68 m_ext_weights.push_back(ext_weight);
69 m_current_evt_pdfs.clear();
70}
71
72void APWeightSumEnsemble::Compute() {
73 if ( m_current_evt_pdfs.size() > 0 ) std::cout << "ERROR in APWeightSumEnsemble::Compute: Last event was not finished and will not be taken into account for the calculation!" << std::endl;
74 //Improve estimation of range
75 if (m_pdf != 0) delete m_pdf;
76 m_pdf = new TH1F("", "", 1000, m_k_evt_weight - 6. * sqrt(m_k_evt_weight), m_k_evt_weight + 6. * sqrt(m_k_evt_weight));
77
78 for (unsigned int i = 0; i < 100000000; ++i) { //variable n_iter
79 double sum_weights = 0.0;
80 for (unsigned int j = 0; j < m_k_evt_orig; ++j) {
81 double weight = 1.0;
82 for (unsigned int l = 0, L = m_weight_vector[j].size(); l < L; ++l) {
83 weight *= (1. - (m_weight_vector[j][l])->GetRandom());
84 }
85 sum_weights += m_ext_weights[j] * (1. - weight);
86 }
87 m_pdf->Fill(sum_weights);
88 if (i % 100 == 0) {
89 m_pdf->Fit("gaus", "0Q");
90 TVirtualFitter *fitter = TVirtualFitter::GetFitter();
91 if ( fitter->GetParError(2) / fitter->GetParameter(2) < m_rel_prec ) break;
92 }
93 }
94
95 m_pdf->Fit("gaus", "0Q");
96 TVirtualFitter *fitter = TVirtualFitter::GetFitter();
97 m_ensemble_mode = fitter->GetParameter(1);
98 m_ensemble_sigma = fitter->GetParameter(2);
100}
static int quant(double min, double max, unsigned nSteps, double val)
Class to store a single weight entry (one bin).
TH1F * m_pdf
Holds the TH1F instance from the arrays if computed.
ClassDef(APWeightSumEnsemble, 1) private std::vector< std::vector< APWeightEntry * > > m_weight_vector
< Performs the ensemble test to model final PDF.
double m_rel_prec
Holds the relative precision of the gaussian fit to stop the ensemble test at.
TH1F * GetPDF()
Returns the calculated PDF.
double m_ensemble_mode
Holds the Mode (= sum of weights from PDF) from ensemble test.
bool m_ensembleTest_done
Flag if the ensemble test has been performed with the current set of weights.
std::vector< double > m_ext_weights
Holds the external event weight provided when finishing the respective events.
APWeightSumEnsemble()
Default constructor.
virtual ~APWeightSumEnsemble()
Default destructor.
double GetQuantile(const double prob)
Returns the quantlile for p=prob for the distribution.
double GetRandom()
Returns a random value according to the pdf.
double m_ensemble_sigma
Holds the standard deviation from ensemble test.
void SetRelPrecision(double rel_prec)
Sets the relative precision of the gaussian fit to stop the ensemble test at.
void AddWeightToEvt(APWeightEntry *weight)
Adds a weight to the sum of weights.
double GetEnsemblePDFStdDev()
Returns the standard deviation from PDF .
double GetEnsemblePDFMode()
Returns the Mode (= sum of weights from PDF).
std::vector< APWeightEntry * > m_current_evt_pdfs
Holds the weight objects for the current event.
void FinishEvt(double ext_weight=1.0)
Finishes the current event and calculates the event weight.
double m_k_evt_weight
Holds the sum of weights.
Definition APWeightSum.h:60
unsigned long int m_k_evt_orig
Holds the original amount of unweighted counts ("sum of 1's").
Definition APWeightSum.h:59
void AddWeightToEvt(APWeightEntry *weight)
Adds a weight to the sum of weights.
void FinishEvt(double ext_weight=1.0)
Finishes the current event and calculates the event weight.
const ShapeFitter * fitter