13 #include "Riostream.h" 20 #include "TDecompChol.h" 24 #define RADDEG (180. / TMath::Pi()) 25 #define DEGRAD (TMath::Pi() / 180.) 34 #define PARAM_MAXSTUDY 1 35 #define PARAM_SEVERAL 2 36 #define PARAM_RELERR 3 37 #define PARAM_MAXTERMS 4 159 : TNamed(
"multidimfit",
"Multi-dimensional fit object"),
212 if (opt.Contains(
"k"))
214 if (opt.Contains(
"v"))
402 for (i = 0; i <
n; i++) {
405 for (j = 0; j <
m; j++)
474 typedef std::multimap<double, int> cmt;
481 double del_error_abs = 0;
482 int deleted_terms_count = 0;
484 for (cmt::iterator it = m.begin(); it != m.end() && del_error_abs <
error; ++it) {
487 del_error_abs =
TMath::Abs(it->first) + del_error_abs;
488 deleted_terms_count++;
493 int fNCoefficients_new = fNCoefficients - deleted_terms_count;
494 TVectorD fCoefficients_new(fNCoefficients_new);
495 std::vector<Int_t> fPowerIndex_new;
505 fNCoefficients = fNCoefficients_new;
509 edm::LogInfo(
"TMultiDimFet") << deleted_terms_count <<
" terms removed" 521 s += (epsilon + iv[
i] - 1) / (epsilon +
fMaxPowers[
i] - 1);
545 for (i = 3; i <=
p; i++) {
548 p3 = ((2 * i - 3) * p2 * x - (i - 2) *
p1) / (i - 1);
550 p3 = 2 * x * p2 -
p1;
680 Int_t numberFunctions = 0;
683 Int_t maxNumberFunctions = 1;
704 control[numberFunctions - 1] = Int_t(1.0
e+6 * s);
708 j = (numberFunctions - 1) * fNVariables + i;
720 if (i == fNVariables) {
729 for (j = 0; j <
i; j++)
737 powers[i * fNVariables +
j] =
fPowers[i * fNVariables +
j];
750 fPowers.resize(fMaxFunctions * fNVariables);
753 Double_t
x = control[
i];
758 if (control[j] <= x) {
766 control[
k] = control[
i];
775 fPowers[i * fNVariables + j] = powers[order[i] * fNVariables + j];
798 Double_t
f =
Eval(x, coeff);
837 if (!outName.EndsWith(
".C") && !outName.EndsWith(
".cxx"))
859 for (j = 0; j <=
i; j++) {
863 curvatureMatrix(j, i) = curvatureMatrix(i, j);
877 const TVectorD diag = TMatrixDDiag_const(curvatureMatrix);
878 curvatureMatrix.NormByDiag(diag);
880 TDecompChol chol(curvatureMatrix);
881 if (!chol.Decompose())
882 Error(
"MakeCoefficientErrors",
"curvature matrix is singular");
883 chol.Invert(curvatureMatrix);
885 curvatureMatrix.NormByDiag(diag);
902 Int_t
col = 0, row = 0;
906 for (row = col - 1; row > -1; row--) {
908 for (i = row; i <=
col; i++)
979 Double_t xidotXj = 0;
991 k = j * fNVariables +
i;
997 for (j = 0; j <
i; j++) {
1003 l = k * fNVariables +
j;
1004 m = k * fNVariables +
i;
1037 Int_t
p =
fPowers[
function * fNVariables +
k];
1108 TString
opt(option);
1111 if (opt.Length() < 1)
1121 if (opt.Contains(
"x") || opt.Contains(
"a")) {
1124 if (!
fHistograms->FindObject(Form(
"x_%d_orig", i)))
1130 if (opt.Contains(
"d") || opt.Contains(
"a")) {
1137 if (opt.Contains(
"n") || opt.Contains(
"a")) {
1140 if (!
fHistograms->FindObject(Form(
"x_%d_norm", i)))
1141 fHistograms->Add(
new TH1D(Form(
"x_%d_norm", i), Form(
"Normalized variable # %d", i), 100, -1, 1));
1145 if (opt.Contains(
"s") || opt.Contains(
"a")) {
1153 if (opt.Contains(
"r1") || opt.Contains(
"a")) {
1156 if (!
fHistograms->FindObject(Form(
"res_x_%d", i)))
1158 Form(
"Computed residual versus x_%d", i),
1168 if (opt.Contains(
"r2") || opt.Contains(
"a")) {
1172 "Computed residuals vs Quantity",
1182 if (opt.Contains(
"r3") || opt.Contains(
"a")) {
1186 "Computed residuals over training sample",
1191 if (opt.Contains(
"r4") || opt.Contains(
"a")) {
1195 "Distribution of residuals from test",
1248 MakeRealCode(Form(
"%sMDF.cxx", classname), classname, option);
1273 k = i * fNVariables +
j;
1352 if (i == fMaxFunctions) {
1367 edm::LogInfo(
"TMultiDimFet") <<
"Coeff SumSqRes Contrib Angle QM Func" 1368 <<
" Value W^2 Powers" 1374 if (dResidur == 0) {
1403 squareResidual -= dResidur;
1415 << squareResidual <<
" " << std::setw(10) << std::setprecision(4) << dResidur
1416 <<
" " << std::setw(7) << std::setprecision(3) <<
fMaxAngle <<
" " 1417 << std::setw(7) << std::setprecision(3) << s <<
" " << std::setw(5) << i <<
" " 1419 <<
" " << std::setw(10) << std::setprecision(4)
1453 Bool_t isMethod = (classname[0] ==
'\0' ? kFALSE : kTRUE);
1454 const char *
prefix = (isMethod ? Form(
"%s::", classname) :
"");
1455 const char *cv_qual = (isMethod ?
"" :
"static ");
1459 Error(
"MakeRealCode",
"couldn't open output file '%s'", filename);
1464 edm::LogInfo(
"TMultiDimFet") <<
"Writing on file \"" << filename <<
"\" ... " << std::flush;
1469 outFile <<
"// -*- mode: c++ -*-" 1474 <<
"// File " << filename <<
" generated by TMultiDimFet::MakeRealCode" 1478 outFile <<
"// on " << date.AsString() <<
"\n";
1480 outFile <<
"// ROOT version " << gROOT->GetVersion() <<
"\n" 1484 outFile <<
"// This file contains the function " 1488 <<
"// double " << prefix <<
"MDF(double *x); " 1492 <<
"// For evaluating the parameterization obtained" 1494 <<
"// from TMultiDimFet and the point x" 1498 <<
"// See TMultiDimFet class documentation for more " 1506 outFile <<
"#include \"" << classname <<
".h\"" 1514 <<
"// Static data variables" 1518 outFile << cv_qual <<
"int " << prefix <<
"gNVariables = " <<
fNVariables <<
";" 1520 outFile << cv_qual <<
"int " << prefix <<
"gNCoefficients = " <<
fNCoefficients <<
";" 1522 outFile << cv_qual <<
"double " << prefix <<
"gDMean = " <<
fMeanQuantity <<
";" 1526 outFile <<
"// Assignment to mean vector." 1528 outFile << cv_qual <<
"double " << prefix <<
"gXMean[] = {" 1531 outFile << (i != 0 ?
", " :
" ") <<
fMeanVariables(i) << std::flush;
1537 outFile <<
"// Assignment to minimum vector." 1539 outFile << cv_qual <<
"double " << prefix <<
"gXMin[] = {" 1542 outFile << (i != 0 ?
", " :
" ") <<
fMinVariables(i) << std::flush;
1548 outFile <<
"// Assignment to maximum vector." 1550 outFile << cv_qual <<
"double " << prefix <<
"gXMax[] = {" 1553 outFile << (i != 0 ?
", " :
" ") <<
fMaxVariables(i) << std::flush;
1559 outFile <<
"// Assignment to coefficients vector." 1561 outFile << cv_qual <<
"double " << prefix <<
"gCoefficient[] = {" << std::flush;
1563 outFile << (i != 0 ?
"," :
"") <<
"\n" 1571 outFile <<
"// Assignment to powers vector." 1573 <<
"// The powers are stored row-wise, that is" 1575 <<
"// p_ij = " << prefix <<
"gPower[i * NVariables + j];" 1577 outFile << cv_qual <<
"int " << prefix <<
"gPower[] = {" << std::flush;
1581 outFile << std::flush <<
" ";
1586 << (i == fNCoefficients - 1 && j == fNVariables - 1 ?
"" :
",") << std::flush;
1599 <<
"// The " << (isMethod ?
"method " :
"function ") <<
" double " << prefix <<
"MDF(double *x)" 1603 outFile <<
"double " << prefix <<
"MDF(double *x) {" 1605 <<
" double returnValue = " << prefix <<
"gDMean;" 1607 <<
" int i = 0, j = 0, k = 0;" 1609 <<
" for (i = 0; i < " << prefix <<
"gNCoefficients ; i++) {" 1611 <<
" // Evaluate the ith term in the expansion" 1613 <<
" double term = " << prefix <<
"gCoefficient[i];" 1615 <<
" for (j = 0; j < " << prefix <<
"gNVariables; j++) {" 1617 <<
" // Evaluate the polynomial in the jth variable." 1619 <<
" int power = " << prefix <<
"gPower[" << prefix <<
"gNVariables * i + j]; " 1621 <<
" double p1 = 1, p2 = 0, p3 = 0, r = 0;" 1623 <<
" double v = 1 + 2. / (" << prefix <<
"gXMax[j] - " << prefix <<
"gXMin[j]) * (x[j] - " << prefix
1626 <<
" // what is the power to use!" 1628 <<
" switch(power) {" 1630 <<
" case 1: r = 1; break; " 1632 <<
" case 2: r = v; break; " 1638 <<
" for (k = 3; k <= power; k++) { " 1643 outFile <<
" p3 = ((2 * i - 3) * p2 * v - (i - 2) * p1)" 1647 outFile <<
" p3 = 2 * v * p2 - p1; " 1649 outFile <<
" p1 = p2; p2 = p3; " 1657 <<
" // multiply this term by the poly in the jth var" 1663 <<
" // Add this term to the final result" 1665 <<
" returnValue += term;" 1669 <<
" return returnValue;" 1676 outFile <<
"// EOF for " << filename <<
"\n";
1701 TString
opt(option);
1704 if (opt.Contains(
"p")) {
1708 <<
"----------------" 1713 <<
" Power Limit Parameter: " <<
fPowerLimit <<
"\n" 1715 <<
" Max functions to study: " <<
fMaxStudy <<
"\n" 1716 <<
" Max angle (optional): " <<
fMaxAngle <<
"\n" 1719 <<
" Maximum Powers: " << std::flush;
1724 <<
" Parameterisation will be done using " << std::flush;
1726 edm::LogInfo(
"TMultiDimFet") <<
"Chebyshev polynomials" 1737 if (opt.Contains(
"s")) {
1741 <<
"------------------" 1743 <<
" D" << std::flush;
1745 edm::LogInfo(
"TMultiDimFet") <<
" " << std::setw(10) << i + 1 << std::flush;
1747 <<
" Max: " << std::setw(10) << std::setprecision(7) <<
fMaxQuantity << std::flush;
1751 <<
" Min: " << std::setw(10) << std::setprecision(7) <<
fMinQuantity << std::flush;
1755 <<
" Mean: " << std::setw(10) << std::setprecision(7) <<
fMeanQuantity << std::flush;
1763 if (opt.Contains(
"r")) {
1764 edm::LogInfo(
"TMultiDimFet") <<
"Results of Parameterisation:" 1766 <<
"----------------------------" 1768 <<
" Total reduction of square residuals " <<
fSumSqResidual <<
"\n" 1769 <<
" Relative precision obtained: " <<
fPrecision <<
"\n" 1770 <<
" Error obtained: " <<
fError <<
"\n" 1776 <<
" Estimated root mean square: " <<
fRMS <<
"\n" 1777 <<
" Maximum powers used: " << std::flush;
1781 <<
" Function codes of candidate functions." 1783 <<
" 1: considered," 1784 <<
" 2: too little contribution," 1785 <<
" 3: accepted." << std::flush;
1789 <<
" " << std::flush;
1790 else if (i % 10 == 0)
1795 <<
" Loop over candidates stopped because " << std::flush;
1798 edm::LogInfo(
"TMultiDimFet") <<
"max allowed studies reached" 1802 edm::LogInfo(
"TMultiDimFet") <<
"all candidates considered several times" 1806 edm::LogInfo(
"TMultiDimFet") <<
"wanted relative error obtained" 1810 edm::LogInfo(
"TMultiDimFet") <<
"max number of terms reached" 1821 if (opt.Contains(
"f")) {
1824 <<
"---------------" 1841 if (opt.Contains(
"c")) {
1846 <<
" # Value Error Powers" 1848 <<
" ---------------------------------------" 1863 <<
"-------------------";
1867 if (opt.Contains(
"m")) {
1871 <<
"-----------------" 1873 <<
" Normalised variables: " 1883 if (i != fNVariables - 1)
1903 edm::LogInfo(
"TMultiDimFet") <<
" * L" << p - 1 <<
"(y" << j <<
")";
1906 edm::LogInfo(
"TMultiDimFet") <<
" * C" << p - 1 <<
"(y" << j <<
")";
1909 edm::LogInfo(
"TMultiDimFet") <<
" * y" << j <<
"^" << p - 1;
1926 TString
opt(option);
1929 if (opt.Contains(
"m")) {
1933 <<
"-----------------" 1935 <<
" Normalised variables: " 1945 if (i != fNVariables - 1)
1965 edm::LogInfo(
"TMultiDimFet") <<
"*Leg(" << p - 1 <<
",y" <<
j <<
")";
1968 edm::LogInfo(
"TMultiDimFet") <<
"*C" << p - 1 <<
"(y" << j <<
")";
1971 edm::LogInfo(
"TMultiDimFet") <<
"*y" << j <<
"**" << p - 1;
2003 if (ang >= 90 || ang < 0) {
2004 Warning(
"SetMaxAngle",
"angle must be in [0,90)");
2017 if (ang > 90 || ang <= 0) {
2018 Warning(
"SetMinAngle",
"angle must be in [0,90)");
2042 fPowers[i * fNVariables + j] = powers[i * fNVariables + j] + 1;
edm::ErrorSummaryEntry Error
TMatrixD fCorrelationMatrix
Multi Correlation coefficient.
Double_t fSumSqQuantity
Min value of dependent quantity.
Double_t fMinAngle
Size of test sample.
TVectorD fTestVariables
Test sample, Error in quantity.
const TMultiDimFet & operator=(const TMultiDimFet &in)
Int_t fTestSampleSize
Test sample, independent variables.
Double_t fSumSqAvgQuantity
SumSquare of dependent quantity.
Double_t fMinQuantity
Max value of dependent quantity.
virtual void MakeCoefficientErrors()
void ZeroDoubiousCoefficients(double error)
void SetPowerLimit(Double_t limit=1e-3)
Int_t fParameterisationCode
Chi square of fit.
Double_t fMinResidual
Max redsidual value.
EMDFPolyType fPolyType
Bit pattern of hisograms used.
TMatrixD fFunctions
Control parameter.
Double_t fTestCorrelationCoeff
Correlation matrix.
void SetMaxPowers(const Int_t *powers)
virtual Double_t EvalControl(const Int_t *powers)
void Print(Option_t *option="ps") const override
TMatrixD fOrthFunctions
max functions to study
Byte_t fHistogramMask
List of histograms.
Double_t fMaxAngle
Min angle for acepting new function.
std::vector< Int_t > fPowers
Double_t fPowerLimit
maximum powers, ex-array
Double_t fPrecision
Error from test.
virtual void MakeHistograms(Option_t *option="A")
TVectorD fCoefficientsRMS
virtual void MakeParameterization()
virtual Double_t MakeGramSchmidt(Int_t function)
Int_t fMaxResidualRow
Min redsidual value.
Double_t fTestError
Error from parameterization.
virtual void AddTestRow(const Double_t *x, Double_t D, Double_t E=0)
void SetMinRelativeError(Double_t error)
virtual void MakeNormalized()
virtual Bool_t TestFunction(Double_t squareResidual, Double_t dResidur)
virtual Double_t EvalFactor(Int_t p, Double_t x) const
virtual void SetPowers(const Int_t *powers, Int_t terms)
Int_t fNVariables
Training sample, independent variables.
virtual void MakeCandidates()
Double_t fChi2
Root mean square of fit.
Int_t fMinResidualRow
Row giving max residual.
TVectorD fTestSqError
Test sample, dependent quantity.
Double_t fMinRelativeError
virtual void MakeCorrelation()
TVectorD fOrthCoefficients
Double_t fRMS
Vector of RMS of coefficients.
void Fill(HcalDetId &id, double val, std::vector< TH2F > &depth)
std::vector< Int_t > fPowerIndex
Double_t fMeanQuantity
Training sample, error in quantity.
TVectorD fOrthFunctionNorms
As above, but orthogonalised.
virtual void MakeCode(const char *functionName="MDF", Option_t *option="")
Double_t fSumSqResidual
Row giving min residual.
virtual void AddRow(const Double_t *x, Double_t D, Double_t E=0)
virtual Double_t MakeChi2(const Double_t *coeff=nullptr)
Double_t fMaxResidual
Vector of the final residuals.
virtual void MakeRealCode(const char *filename, const char *classname, Option_t *option="")
virtual Bool_t Select(const Int_t *iv)
TVectorD fMaxVariables
mean value of independent variables
virtual void FindParameterization(double precision)
Double_t fTestPrecision
Relative precision of param.
TList * fHistograms
Multi Correlation coefficient.
Int_t fMaxTerms
Max angle for acepting new function.
TVectorD fTestQuantity
Size of training sample.
DecomposeProduct< arg, typename Div::arg > D
TVectorD fVariables
Sum of squares away from mean.
Int_t fMaxFunctions
Functions evaluated over sample.
Double_t fError
Exit code of parameterisation.
Int_t fNCoefficients
Sum of Square residuals.
Int_t fMaxFunctionsTimesNVariables
maximum powers from fit, ex-array
std::vector< Int_t > fFunctionCodes
TVectorD fSqError
Training sample, dependent quantity.
void SetMinAngle(Double_t angle=1)
Double_t fCorrelationCoeff
Relative precision of test.
TMatrixD fOrthCurvatureMatrix
The model coefficients.
virtual void MakeCoefficients()
std::vector< Int_t > fMaxPowers
Min relative error accepted.
void Clear(Option_t *option="") override
virtual void MakeMethod(const Char_t *className="MDF", Option_t *option="")
void ReducePolynomial(double error)
std::vector< Int_t > fMaxPowersFinal
Norm of the evaluated functions.
virtual void PrintPolynomialsSpecial(Option_t *option="m") const
uint32_t dimension(pat::CandKinResolution::Parametrization parametrization)
Returns the number of free parameters in a parametrization (3 or 4)
void SetMaxAngle(Double_t angle=0)
TVectorD fCoefficients
Model matrix.
Int_t fMaxStudy
acceptance code, ex-array
virtual Double_t Eval(const Double_t *x, const Double_t *coeff=nullptr) const