From 35f41bdaa71404f2aaff42664b15b19d5ad93420 Mon Sep 17 00:00:00 2001 From: kreczko Date: Sun, 1 Mar 2015 16:56:54 +0000 Subject: [PATCH] added k->tau --- ci/test.sh | 4 +-- include/TauSVDUnfold.h | 5 ++-- src/TauSVDUnfold.cxx | 46 ++++++++++++++++++++----------- test/TestTVectorD.cpp | 13 ++++----- test/TestTauSVDUnfold.cpp | 58 ++++++++++++++++++++++++++++----------- 5 files changed, 83 insertions(+), 43 deletions(-) diff --git a/ci/test.sh b/ci/test.sh index f9225a5..e1affe5 100644 --- a/ci/test.sh +++ b/ci/test.sh @@ -11,8 +11,8 @@ echo "Checking PyROOT installation" time python -c 'import ROOT; ROOT.TBrowser()' # Check that RooUnfold can be imported -echo "Checking RooUnfold library" +echo "Checking RooUnfold library in PyROOT" time python -c 'from ROOT import gSystem; gSystem.Load("libRooUnfold.so");from ROOT import RooUnfoldResponse' - +time python test/test_pyroot.py echo "Executing unit tests" make test diff --git a/include/TauSVDUnfold.h b/include/TauSVDUnfold.h index cefd518..80efec6 100644 --- a/include/TauSVDUnfold.h +++ b/include/TauSVDUnfold.h @@ -65,10 +65,11 @@ class TauSVDUnfold: public TSVDUnfold_local { */ static TH2D* get_data_covariance_matrix(const TH1D* data_histogram); - static double get_global_correlation(const TH1D* data_histogram); + static double get_global_correlation(const TH2D* stat_cov_hist, const TH1D* data_histogram); - static TH1D* get_global_correlation_hist(const TH1D* data_histogram); + static TH1D* get_global_correlation_hist(const TH2D* covariance_hist, const TH1D* data_histogram); + TVectorD getASV() const; protected: double fTau; double fCurv; diff --git a/src/TauSVDUnfold.cxx b/src/TauSVDUnfold.cxx index 73c5afe..7f92a07 100644 --- a/src/TauSVDUnfold.cxx +++ b/src/TauSVDUnfold.cxx @@ -138,9 +138,11 @@ TH1D* TauSVDUnfold::Unfold(double tau) { TDecompSVD ASVD(mA * mCinv); TMatrixD Uort = ASVD.GetU(); TMatrixD Vort = ASVD.GetV(); - // TODO: This part is failing for tau unfolding + // A = fU fSig fV^T TVectorD ASV = ASVD.GetSig(); -// fASV = TVectorD(ASV); + fASV.Clear(); + fASV.ResizeTo(ASV.GetNoElements()); + fASV = ASV; if (!fToyMode && !fMatToyMode) { V2H(ASV, *fSVHist); } @@ -242,7 +244,7 @@ TH1D* TauSVDUnfold::Unfold(double tau) { double TauSVDUnfold::kToTau(int kreg) const { double tau(0.); - if (fASV.NonZeros() == 0 && kreg > 0) + if (fASV.NonZeros() > 0 && kreg > 0) tau = fASV(kreg); return tau; } @@ -271,10 +273,8 @@ TH2D* TauSVDUnfold::get_data_covariance_matrix(const TH1D* data_histogram) { return data_covariance_matrix; } -TH1D* TauSVDUnfold::get_global_correlation_hist(const TH1D* data_histogram) { - TH2D* covariance_hist = (TH2D*) TauSVDUnfold::get_data_covariance_matrix(data_histogram); - - unsigned int n_bins = data_histogram->GetNbinsX(); +TH1D* TauSVDUnfold::get_global_correlation_hist(const TH2D* covariance_hist, const TH1D* data_histogram) { + uint32_t n_bins = data_histogram->GetNbinsX(); vector bin_map; bin_map.resize(n_bins); @@ -282,7 +282,7 @@ TH1D* TauSVDUnfold::get_global_correlation_hist(const TH1D* data_histogram) { unsigned int bin_counter(0); // all bins except underflow (0) and overflow (nbins + 1) - for (auto i = 1; i <= n_bins; ++i) { + for (uint32_t i = 1; i <= n_bins; ++i) { double data = data_histogram->GetBinContent(i); if (data <= 0.) { // Through out bin with no data @@ -291,7 +291,7 @@ TH1D* TauSVDUnfold::get_global_correlation_hist(const TH1D* data_histogram) { } else { // Search for bins with empty rows/columns bool skip_bin = true; - for (auto j = 1; j <= n_bins; ++j) { + for (uint32_t j = 1; j <= n_bins; ++j) { double value = covariance_hist->GetBinContent(i, j); if (value != 0.) { skip_bin = false; @@ -383,10 +383,6 @@ TH1D* TauSVDUnfold::get_global_correlation_hist(const TH1D* data_histogram) { if (cov_prod > 0) var = 1. / cov_prod; double global_correlation_squared = 0.; -// cout << "cov: " << cov << endl; -// cout << "covinv: " << covinv << endl; -// cout << "cov_prod: " << cov_prod << endl; -// cout << "var: " << var << endl; if (var > 0.) global_correlation_squared = 1. - var; @@ -403,13 +399,13 @@ TH1D* TauSVDUnfold::get_global_correlation_hist(const TH1D* data_histogram) { global_correlation_hist->SetBinContent(i, global_correlation); global_correlation_hist->SetBinError(i, 0.); } - delete covariance_hist; return global_correlation_hist; } -double TauSVDUnfold::get_global_correlation(const TH1D* data_histogram) { - TH1D * global_correlation_hist = TauSVDUnfold::get_global_correlation_hist(data_histogram); +double TauSVDUnfold::get_global_correlation(const TH2D* covariance_hist, const TH1D* data_histogram) { + + TH1D * global_correlation_hist = TauSVDUnfold::get_global_correlation_hist(covariance_hist, data_histogram); double sum = 0.; double average = 0.; @@ -623,3 +619,21 @@ TH2D* TauSVDUnfold::GetAdetCovMatrix(Int_t ntoys, Int_t seed = 1) { return unfcov; } + +double TauSVDUnfold::GetLcurveX() const { + return 0; +} + +double TauSVDUnfold::GetLcurveY() const { + return 0; +} + +TGraph* TauSVDUnfold::ScanLCurve(unsigned int n_point, double tau_min, double tau_max){ + return 0; +} + +TVectorD TauSVDUnfold::getASV() const { + return fASV; +} + + diff --git a/test/TestTVectorD.cpp b/test/TestTVectorD.cpp index 3ede3c8..9f4663e 100644 --- a/test/TestTVectorD.cpp +++ b/test/TestTVectorD.cpp @@ -8,24 +8,23 @@ #include #include "TVectorD.h" #include - +#define N 3 using namespace std; BOOST_AUTO_TEST_SUITE (TVectorDTestSuite) BOOST_AUTO_TEST_CASE(test_assign) { - uint32_t n(3); - double elements[n] = { 1., 2., 3. }; - TVectorD vector = TVectorD(n, elements); - BOOST_CHECK_EQUAL(vector.GetNoElements(), n); + double elements[N] = { 1., 2., 3. }; + TVectorD vector = TVectorD(N, elements); + BOOST_CHECK_EQUAL(vector.GetNoElements(), N); // initialise vector with default constructor TVectorD copy = TVectorD(); copy.Clear(); // does not work without resize - copy.ResizeTo(n); + copy.ResizeTo(N); // this returns "vectors not compatible" without copy.ResizeTo(n); copy = vector; - BOOST_CHECK_EQUAL(copy.GetNoElements(), n); + BOOST_CHECK_EQUAL(copy.GetNoElements(), N); BOOST_CHECK_EQUAL(copy[0], 1.); BOOST_CHECK_EQUAL(copy[1], 2.); BOOST_CHECK_EQUAL(copy[2], 3.); diff --git a/test/TestTauSVDUnfold.cpp b/test/TestTauSVDUnfold.cpp index a481fdb..163aa68 100644 --- a/test/TestTauSVDUnfold.cpp +++ b/test/TestTauSVDUnfold.cpp @@ -9,7 +9,6 @@ #include "../include/RooUnfoldSvd.h" #include "../include/RooUnfoldResponse.h" #include -#include "TROOT.h" using namespace std; @@ -23,12 +22,10 @@ struct TauSVDUnfoldSetup { gen_var(new TH1D("gen_var", "gen_var", nbins, 0, nbins)), reco_var(new TH1D("reco_var", "reco_var", nbins, 0, nbins)), gen_vs_reco(new TH2D("gen_vs_reco", "gen_vs_reco", nbins, 0, nbins, nbins, 0, nbins)), + cov_hist(), roo_response(), roounfold_svd_k(), - roounfold_svd_tau(){ - gROOT->SetBatch(1); -// gROOT->ProcessLine("gErrorIgnoreLevel = 1001;"); - gROOT->ProcessLine("gErrorAbortLevel = 1001;"); + roounfold_svd_tau() { // from toy MC 1 data->SetBinContent(1, 365); data->SetBinContent(2, 578); @@ -45,7 +42,7 @@ struct TauSVDUnfoldSetup { reco_var->SetBinContent(4, 195); reco_var->SetBinContent(5, 61); reco_var->SetBinContent(6, 29); - set_sqrt_N_error (reco_var); + set_sqrt_N_error(reco_var); gen_var->SetBinContent(1, 3441); gen_var->SetBinContent(2, 5181); @@ -88,15 +85,18 @@ struct TauSVDUnfoldSetup { roounfold_svd_k = new RooUnfoldSvd(roo_response, data, kreg, n_toy); roounfold_svd_tau = new RooUnfoldSvd(roo_response, data, taureg, n_toy); + roounfold_svd_tau->Hreco(RooUnfold::kNoError); + TH2D* cov = TauSVDUnfold::get_data_covariance_matrix(data); + cov_hist = roounfold_svd_tau->Impl()->GetUnfoldCovMatrix(cov, n_toy); + delete cov; } ~TauSVDUnfoldSetup() { delete data; delete gen_var; delete reco_var; delete gen_vs_reco; + delete cov_hist; delete roo_response; -// delete tau_svd_unfold; - delete roounfold_svd_k; delete roounfold_svd_tau; } @@ -114,14 +114,14 @@ struct TauSVDUnfoldSetup { } } - unsigned int nbins; + uint32_t nbins; unsigned int n_toy; int kreg; double taureg; TH1D* data, *gen_var, *reco_var; TH2D* gen_vs_reco; + TH2D* cov_hist; RooUnfoldResponse* roo_response; -// TauSVDUnfold* tau_svd_unfold; RooUnfoldSvd* roounfold_svd_k; RooUnfoldSvd* roounfold_svd_tau; @@ -130,7 +130,7 @@ struct TauSVDUnfoldSetup { BOOST_AUTO_TEST_SUITE (TauSVDUnfoldTestSuite) BOOST_FIXTURE_TEST_CASE(test_get_data_covariance_matrix, TauSVDUnfoldSetup) { TH2D* cov = TauSVDUnfold::get_data_covariance_matrix(data); - for (auto i = 1; i <= nbins; ++i) { + for (uint32_t i = 1; i <= nbins; ++i) { double data_error = data->GetBinError(i); BOOST_CHECK_EQUAL(cov->GetBinContent(i, i), data_error * data_error); } @@ -138,16 +138,42 @@ BOOST_FIXTURE_TEST_CASE(test_get_data_covariance_matrix, TauSVDUnfoldSetup) { } BOOST_FIXTURE_TEST_CASE(test_get_global_correlation, TauSVDUnfoldSetup) { - double corr = TauSVDUnfold::get_global_correlation(data); - BOOST_CHECK_EQUAL(corr, -1.); + double corr = TauSVDUnfold::get_global_correlation(cov_hist, data); + BOOST_CHECK_CLOSE(corr, 99, 1); } BOOST_FIXTURE_TEST_CASE(test_get_global_correlation_hist, TauSVDUnfoldSetup) { - TH1D* corr_hist = TauSVDUnfold::get_global_correlation_hist(data); - for (auto i = 1; i <= nbins; ++i) { - BOOST_CHECK_EQUAL(corr_hist->GetBinContent(i), i * i); + TH1D* corr_hist = TauSVDUnfold::get_global_correlation_hist(cov_hist, data); + // for the above example, the first bin should be around 95 % + BOOST_CHECK_CLOSE(corr_hist->GetBinContent(1), 95, 1); + // for all others it should be between 99 and 100 % + for (uint32_t i = 2; i <= nbins; ++i) { + BOOST_CHECK_CLOSE(corr_hist->GetBinContent(i), 99, 1); } delete corr_hist; } +BOOST_FIXTURE_TEST_CASE(test_k_to_tau, TauSVDUnfoldSetup) { + const TauSVDUnfold* tau_svd = (TauSVDUnfold*) roounfold_svd_tau->Impl(); + double tau = tau_svd->GetTau(); + BOOST_CHECK_EQUAL(tau, taureg); + TVectorD ASV = tau_svd->getASV(); + uint32_t n_elements = ASV.GetNoElements(); + BOOST_CHECK_EQUAL(n_elements, nbins + 1); + tau = tau_svd->kToTau(kreg); + BOOST_CHECK_CLOSE(tau, 3.83, 0.1); +} + +BOOST_FIXTURE_TEST_CASE(test_get_tau, TauSVDUnfoldSetup) { + double tau = ((TauSVDUnfold*) roounfold_svd_tau->Impl())->GetTau(); + BOOST_CHECK_EQUAL(tau, taureg); +} + +BOOST_FIXTURE_TEST_CASE(test_get_ASV, TauSVDUnfoldSetup) { + const TauSVDUnfold* tau_svd = (TauSVDUnfold*) roounfold_svd_tau->Impl(); + TVectorD ASV = tau_svd->getASV(); + uint32_t n_elements = ASV.GetNoElements(); + BOOST_CHECK_EQUAL(n_elements, nbins + 1); + BOOST_CHECK(ASV.NonZeros() > 0); +} BOOST_AUTO_TEST_SUITE_END()