13 #include "Riostream.h"
20 #include "TDecompChol.h"
25 #define RADDEG (180. / TMath::Pi())
26 #define DEGRAD (TMath::Pi() / 180.)
35 #define PARAM_MAXSTUDY 1
36 #define PARAM_SEVERAL 2
37 #define PARAM_RELERR 3
38 #define PARAM_MAXTERMS 4
160 : TNamed(
"multidimfit",
"Multi-dimensional fit object"),
161 fQuantity(dimension),
163 fVariables(dimension * 100),
164 fMeanVariables(dimension),
165 fMaxVariables(dimension),
166 fMinVariables(dimension) {
210 TString
opt = option;
213 if (opt.Contains(
"k"))
215 if (opt.Contains(
"v"))
403 for (i = 0; i <
n; i++) {
406 for (j = 0; j <
m; j++)
475 typedef std::multimap<double, int> cmt;
482 double del_error_abs = 0;
483 int deleted_terms_count = 0;
485 for (cmt::iterator it = m.begin(); it != m.end() && del_error_abs <
error; ++it) {
486 if (TMath::Abs(it->first) + del_error_abs <
error) {
488 del_error_abs = TMath::Abs(it->first) + del_error_abs;
489 deleted_terms_count++;
494 int fNCoefficients_new = fNCoefficients - deleted_terms_count;
495 TVectorD fCoefficients_new(fNCoefficients_new);
496 std::vector<Int_t> fPowerIndex_new;
506 fNCoefficients = fNCoefficients_new;
510 edm::LogInfo(
"TMultiDimFet") << deleted_terms_count <<
" terms removed"
522 s += (epsilon + iv[
i] - 1) / (epsilon +
fMaxPowers[
i] - 1);
546 for (i = 3; i <=
p; i++) {
549 p3 = ((2 * i - 3) * p2 * x - (i - 2) *
p1) / (i - 1);
551 p3 = 2 * x * p2 -
p1;
681 Int_t numberFunctions = 0;
684 Int_t maxNumberFunctions = 1;
705 control[numberFunctions - 1] = Int_t(1.0
e+6 * s);
709 j = (numberFunctions - 1) * fNVariables + i;
721 if (i == fNVariables) {
730 for (j = 0; j <
i; j++)
738 powers[i * fNVariables +
j] =
fPowers[i * fNVariables +
j];
751 fPowers.resize(fMaxFunctions * fNVariables);
754 Double_t
x = control[
i];
759 if (control[j] <= x) {
767 control[
k] = control[
i];
776 fPowers[i * fNVariables + j] = powers[order[i] * fNVariables + j];
799 Double_t
f =
Eval(x, coeff);
837 TString outName(filename);
838 if (!outName.EndsWith(
".C") && !outName.EndsWith(
".cxx"))
860 for (j = 0; j <=
i; j++) {
864 curvatureMatrix(j, i) = curvatureMatrix(i, j);
878 const TVectorD diag = TMatrixDDiag_const(curvatureMatrix);
879 curvatureMatrix.NormByDiag(diag);
881 TDecompChol chol(curvatureMatrix);
882 if (!chol.Decompose())
883 Error(
"MakeCoefficientErrors",
"curvature matrix is singular");
884 chol.Invert(curvatureMatrix);
886 curvatureMatrix.NormByDiag(diag);
903 Int_t
col = 0, row = 0;
907 for (row = col - 1; row > -1; row--) {
909 for (i = row; i <=
col; i++)
980 Double_t xidotXj = 0;
992 k = j * fNVariables +
i;
998 for (j = 0; j <
i; j++) {
1004 l = k * fNVariables +
j;
1005 m = k * fNVariables +
i;
1038 Int_t
p =
fPowers[
function * fNVariables +
k];
1109 TString
opt(option);
1112 if (opt.Length() < 1)
1122 if (opt.Contains(
"x") || opt.Contains(
"a")) {
1125 if (!
fHistograms->FindObject(Form(
"x_%d_orig", i)))
1131 if (opt.Contains(
"d") || opt.Contains(
"a")) {
1138 if (opt.Contains(
"n") || opt.Contains(
"a")) {
1141 if (!
fHistograms->FindObject(Form(
"x_%d_norm", i)))
1142 fHistograms->Add(
new TH1D(Form(
"x_%d_norm", i), Form(
"Normalized variable # %d", i), 100, -1, 1));
1146 if (opt.Contains(
"s") || opt.Contains(
"a")) {
1154 if (opt.Contains(
"r1") || opt.Contains(
"a")) {
1157 if (!
fHistograms->FindObject(Form(
"res_x_%d", i)))
1159 Form(
"Computed residual versus x_%d", i),
1169 if (opt.Contains(
"r2") || opt.Contains(
"a")) {
1173 "Computed residuals vs Quantity",
1183 if (opt.Contains(
"r3") || opt.Contains(
"a")) {
1187 "Computed residuals over training sample",
1192 if (opt.Contains(
"r4") || opt.Contains(
"a")) {
1196 "Distribution of residuals from test",
1249 MakeRealCode(Form(
"%sMDF.cxx", classname), classname, option);
1274 k = i * fNVariables +
j;
1353 if (i == fMaxFunctions) {
1368 edm::LogInfo(
"TMultiDimFet") <<
"Coeff SumSqRes Contrib Angle QM Func"
1369 <<
" Value W^2 Powers"
1375 if (dResidur == 0) {
1404 squareResidual -= dResidur;
1416 << squareResidual <<
" " << std::setw(10) << std::setprecision(4) << dResidur
1417 <<
" " << std::setw(7) << std::setprecision(3) <<
fMaxAngle <<
" "
1418 << std::setw(7) << std::setprecision(3) << s <<
" " << std::setw(5) << i <<
" "
1420 <<
" " << std::setw(10) << std::setprecision(4)
1454 Bool_t isMethod = (classname[0] ==
'\0' ? kFALSE : kTRUE);
1455 const char *
prefix = (isMethod ? Form(
"%s::", classname) :
"");
1456 const char *cv_qual = (isMethod ?
"" :
"static ");
1460 Error(
"MakeRealCode",
"couldn't open output file '%s'", filename);
1465 edm::LogInfo(
"TMultiDimFet") <<
"Writing on file \"" << filename <<
"\" ... " << std::flush;
1470 outFile <<
"// -*- mode: c++ -*-"
1475 <<
"// File " << filename <<
" generated by TMultiDimFet::MakeRealCode"
1479 outFile <<
"// on " << date.AsString() <<
"\n";
1481 outFile <<
"// ROOT version " << gROOT->GetVersion() <<
"\n"
1485 outFile <<
"// This file contains the function "
1489 <<
"// double " << prefix <<
"MDF(double *x); "
1493 <<
"// For evaluating the parameterization obtained"
1495 <<
"// from TMultiDimFet and the point x"
1499 <<
"// See TMultiDimFet class documentation for more "
1507 outFile <<
"#include \"" << classname <<
".h\""
1515 <<
"// Static data variables"
1519 outFile << cv_qual <<
"int " << prefix <<
"gNVariables = " <<
fNVariables <<
";"
1521 outFile << cv_qual <<
"int " << prefix <<
"gNCoefficients = " <<
fNCoefficients <<
";"
1523 outFile << cv_qual <<
"double " << prefix <<
"gDMean = " <<
fMeanQuantity <<
";"
1527 outFile <<
"// Assignment to mean vector."
1529 outFile << cv_qual <<
"double " << prefix <<
"gXMean[] = {"
1532 outFile << (i != 0 ?
", " :
" ") <<
fMeanVariables(i) << std::flush;
1538 outFile <<
"// Assignment to minimum vector."
1540 outFile << cv_qual <<
"double " << prefix <<
"gXMin[] = {"
1543 outFile << (i != 0 ?
", " :
" ") <<
fMinVariables(i) << std::flush;
1549 outFile <<
"// Assignment to maximum vector."
1551 outFile << cv_qual <<
"double " << prefix <<
"gXMax[] = {"
1554 outFile << (i != 0 ?
", " :
" ") <<
fMaxVariables(i) << std::flush;
1560 outFile <<
"// Assignment to coefficients vector."
1562 outFile << cv_qual <<
"double " << prefix <<
"gCoefficient[] = {" << std::flush;
1564 outFile << (i != 0 ?
"," :
"") <<
"\n"
1572 outFile <<
"// Assignment to powers vector."
1574 <<
"// The powers are stored row-wise, that is"
1576 <<
"// p_ij = " << prefix <<
"gPower[i * NVariables + j];"
1578 outFile << cv_qual <<
"int " << prefix <<
"gPower[] = {" << std::flush;
1582 outFile << std::flush <<
" ";
1587 << (i == fNCoefficients - 1 && j == fNVariables - 1 ?
"" :
",") << std::flush;
1600 <<
"// The " << (isMethod ?
"method " :
"function ") <<
" double " << prefix <<
"MDF(double *x)"
1604 outFile <<
"double " << prefix <<
"MDF(double *x) {"
1606 <<
" double returnValue = " << prefix <<
"gDMean;"
1608 <<
" int i = 0, j = 0, k = 0;"
1610 <<
" for (i = 0; i < " << prefix <<
"gNCoefficients ; i++) {"
1612 <<
" // Evaluate the ith term in the expansion"
1614 <<
" double term = " << prefix <<
"gCoefficient[i];"
1616 <<
" for (j = 0; j < " << prefix <<
"gNVariables; j++) {"
1618 <<
" // Evaluate the polynomial in the jth variable."
1620 <<
" int power = " << prefix <<
"gPower[" << prefix <<
"gNVariables * i + j]; "
1622 <<
" double p1 = 1, p2 = 0, p3 = 0, r = 0;"
1624 <<
" double v = 1 + 2. / (" << prefix <<
"gXMax[j] - " << prefix <<
"gXMin[j]) * (x[j] - " << prefix
1627 <<
" // what is the power to use!"
1629 <<
" switch(power) {"
1631 <<
" case 1: r = 1; break; "
1633 <<
" case 2: r = v; break; "
1639 <<
" for (k = 3; k <= power; k++) { "
1644 outFile <<
" p3 = ((2 * i - 3) * p2 * v - (i - 2) * p1)"
1648 outFile <<
" p3 = 2 * v * p2 - p1; "
1650 outFile <<
" p1 = p2; p2 = p3; "
1658 <<
" // multiply this term by the poly in the jth var"
1664 <<
" // Add this term to the final result"
1666 <<
" returnValue += term;"
1670 <<
" return returnValue;"
1677 outFile <<
"// EOF for " << filename <<
"\n";
1702 TString
opt(option);
1705 if (opt.Contains(
"p")) {
1709 <<
"----------------"
1714 <<
" Power Limit Parameter: " <<
fPowerLimit <<
"\n"
1716 <<
" Max functions to study: " <<
fMaxStudy <<
"\n"
1717 <<
" Max angle (optional): " <<
fMaxAngle <<
"\n"
1720 <<
" Maximum Powers: " << std::flush;
1725 <<
" Parameterisation will be done using " << std::flush;
1727 edm::LogInfo(
"TMultiDimFet") <<
"Chebyshev polynomials"
1738 if (opt.Contains(
"s")) {
1742 <<
"------------------"
1744 <<
" D" << std::flush;
1746 edm::LogInfo(
"TMultiDimFet") <<
" " << std::setw(10) << i + 1 << std::flush;
1748 <<
" Max: " << std::setw(10) << std::setprecision(7) <<
fMaxQuantity << std::flush;
1752 <<
" Min: " << std::setw(10) << std::setprecision(7) <<
fMinQuantity << std::flush;
1756 <<
" Mean: " << std::setw(10) << std::setprecision(7) <<
fMeanQuantity << std::flush;
1764 if (opt.Contains(
"r")) {
1765 edm::LogInfo(
"TMultiDimFet") <<
"Results of Parameterisation:"
1767 <<
"----------------------------"
1769 <<
" Total reduction of square residuals " <<
fSumSqResidual <<
"\n"
1770 <<
" Relative precision obtained: " <<
fPrecision <<
"\n"
1771 <<
" Error obtained: " <<
fError <<
"\n"
1777 <<
" Estimated root mean square: " <<
fRMS <<
"\n"
1778 <<
" Maximum powers used: " << std::flush;
1782 <<
" Function codes of candidate functions."
1784 <<
" 1: considered,"
1785 <<
" 2: too little contribution,"
1786 <<
" 3: accepted." << std::flush;
1790 <<
" " << std::flush;
1791 else if (i % 10 == 0)
1796 <<
" Loop over candidates stopped because " << std::flush;
1799 edm::LogInfo(
"TMultiDimFet") <<
"max allowed studies reached"
1803 edm::LogInfo(
"TMultiDimFet") <<
"all candidates considered several times"
1807 edm::LogInfo(
"TMultiDimFet") <<
"wanted relative error obtained"
1811 edm::LogInfo(
"TMultiDimFet") <<
"max number of terms reached"
1822 if (opt.Contains(
"f")) {
1825 <<
"---------------"
1842 if (opt.Contains(
"c")) {
1847 <<
" # Value Error Powers"
1849 <<
" ---------------------------------------"
1864 <<
"-------------------";
1868 if (opt.Contains(
"m")) {
1872 <<
"-----------------"
1874 <<
" Normalised variables: "
1884 if (i != fNVariables - 1)
1904 edm::LogInfo(
"TMultiDimFet") <<
" * L" << p - 1 <<
"(y" << j <<
")";
1907 edm::LogInfo(
"TMultiDimFet") <<
" * C" << p - 1 <<
"(y" << j <<
")";
1910 edm::LogInfo(
"TMultiDimFet") <<
" * y" << j <<
"^" << p - 1;
1927 TString
opt(option);
1930 if (opt.Contains(
"m")) {
1934 <<
"-----------------"
1936 <<
" Normalised variables: "
1946 if (i != fNVariables - 1)
1966 edm::LogInfo(
"TMultiDimFet") <<
"*Leg(" << p - 1 <<
",y" <<
j <<
")";
1969 edm::LogInfo(
"TMultiDimFet") <<
"*C" << p - 1 <<
"(y" << j <<
")";
1972 edm::LogInfo(
"TMultiDimFet") <<
"*y" << j <<
"**" << p - 1;
2004 if (ang >= 90 || ang < 0) {
2005 Warning(
"SetMaxAngle",
"angle must be in [0,90)");
2018 if (ang > 90 || ang <= 0) {
2019 Warning(
"SetMinAngle",
"angle must be in [0,90)");
2043 fPowers[i * fNVariables + j] = powers[i * fNVariables + j] + 1;
Log< level::Info, true > LogVerbatim
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)
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.
void Print(Option_t *option="ps") const override
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)
const uint16_t range(const Frame &aFrame)
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.
ClassImp(AliDaqEventHeader)
TVectorD fOrthFunctionNorms
As above, but orthogonalised.
uint16_t const *__restrict__ x
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.
Log< level::Info, false > LogInfo
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)
tuple size
Write out results.
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