std::unique_ptr<TH1D> spectrum_to_roothist(const Spectrum & s, const std::string & hname, bool set_errors){ const int n = s.dim(); unique_ptr<TH1D> values(new TH1D(hname.c_str(), hname.c_str(), n, 0, n)); for(int i=0; i<n; ++i){ values->SetBinContent(i+1, s[i]); if(set_errors){ double err = sqrt(s.cov()(i,i)); values->SetBinError(i+1, err); } } return move(values); }
Spectrum RegUnfolder::unfold_nouncertainty(const Spectrum & r) const{ if(tau < 0.0 || L.get_n_rows() != p.gen().dim()) throw invalid_argument("unfold called, but regularization not set up!"); const size_t ngen = p.gen().dim(); // calculate delta1 := R * t - r for the first term: auto delta1 = p.R() * t - poly_matrix(r.get_values()); // calculate delta2 := L * (t - gen) for the second term //auto delta2 = L * (t - poly_matrix(p.gen().get_values())); // chi2 = delta1^T C^{-1} delta1 + tau * delta2^T * delta2, // where C is the covariance matrix of r: Matrix r_cov_inverse = r.cov(); r_cov_inverse.invert_cholesky(); auto chi2 = delta1.transpose() * r_cov_inverse * delta1 + tau * delta2.transpose() * delta2; auto solution = chi2(0,0).argmin(); Spectrum result(ngen); for(size_t i=0; i<ngen; ++i){ result[i] = solution[t(i,0)]; } return result; }
std::pair<std::unique_ptr<TH1D>, std::unique_ptr<TH2D> > spectrum_to_roothists(const Spectrum & s, const std::string & hname, bool set_1d_errors){ return std::pair<std::unique_ptr<TH1D>, std::unique_ptr<TH2D>>(spectrum_to_roothist(s, hname, set_1d_errors), matrix_to_roothist(s.cov(), hname + "_cov")); }