Jpp 19.3.0-rc.4
the software that should make you happy
Loading...
Searching...
No Matches
JCALIBRATE::JFit Class Reference

Fit. More...

#include <JFitK40.hh>

Classes

struct  result_type
 Result type. More...
 

Public Types

typedef std::shared_ptr< JMEstimatorestimator_type
 

Public Member Functions

 JFit (const int option, const int debug)
 Constructor.
 
result_type operator() (const data_type &data)
 Fit.
 

Public Attributes

int debug
 
estimator_type estimator
 M-Estimator function.
 
double lambda
 
JModel value
 
JModel_t error
 
int numberOfIterations
 
JMATH::JMatrixNS V
 

Static Public Attributes

static constexpr int MAXIMUM_ITERATIONS = 100000
 maximal number of iterations.
 
static constexpr double EPSILON = 1.0e-3
 maximal distance to minimum.
 
static constexpr double LAMBDA_MIN = 1.0e-2
 minimal value control parameter
 
static constexpr double LAMBDA_MAX = 1.0e+4
 maximal value control parameter
 
static constexpr double LAMBDA_UP = 10.0
 multiplication factor control parameter
 
static constexpr double LAMBDA_DOWN = 10.0
 multiplication factor control parameter
 
static constexpr double PIVOT = std::numeric_limits<double>::epsilon()
 minimal value diagonal element of matrix
 

Private Member Functions

void evaluate (const data_type &data)
 Evaluation of fit.
 
void seterr (const data_type &data)
 Set errors.
 

Private Attributes

JMATH::JVectorND Y
 
double successor
 
JModel previous
 
std::vector< double > h
 

Detailed Description

Fit.

Definition at line 1275 of file JFitK40.hh.

Member Typedef Documentation

◆ estimator_type

Definition at line 1286 of file JFitK40.hh.

Constructor & Destructor Documentation

◆ JFit()

JCALIBRATE::JFit::JFit ( const int option,
const int debug )
inline

Constructor.

Parameters
optionM-estimator
debugdebug

Definition at line 1295 of file JFitK40.hh.

1295 :
1296 debug(debug)
1297 {
1298 using namespace JPP;
1299
1300 estimator.reset(getMEstimator(option));
1301 }
estimator_type estimator
M-Estimator function.
Definition JFitK40.hh:1498
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).

Member Function Documentation

◆ operator()()

result_type JCALIBRATE::JFit::operator() ( const data_type & data)
inline

Fit.

Parameters
datadata
Returns
chi2, NDF

Definition at line 1310 of file JFitK40.hh.

1311 {
1312 using namespace std;
1313 using namespace JPP;
1314
1315
1316 value.setIndex();
1317
1318 const size_t N = value.getN();
1319
1320 V.resize(N);
1321 Y.resize(N);
1322 h.resize(N);
1323
1324 double xmax = numeric_limits<double>::lowest();
1325 double xmin = numeric_limits<double>::max();
1326
1327 int ndf = 0;
1328
1329 for (data_type::const_iterator ix = data.begin(); ix != data.end(); ++ix) {
1330
1331 const pair_type& pair = ix->first;
1332
1333 if (value.parameters[pair.first ].status &&
1334 value.parameters[pair.second].status) {
1335
1336 ndf += ix->second.size();
1337
1338 for (const rate_type& iy : ix->second) {
1339 if (iy.dt_ns > xmax) { xmax = iy.dt_ns; }
1340 if (iy.dt_ns < xmin) { xmin = iy.dt_ns; }
1341 }
1342 }
1343 }
1344
1345 ndf -= value.getN();
1346
1347 if (ndf < 0) {
1348 return { 0.0, ndf };
1349 }
1350
1351 for (int pmt = 0; pmt != NUMBER_OF_PMTS; ++pmt) {
1352 if (value.parameters[pmt].t0.isFree()) {
1353 value.parameters[pmt].t0.setRange(xmin, xmax);
1354 }
1355 }
1356
1357
1359
1360 double precessor = numeric_limits<double>::max();
1361
1363
1364 DEBUG("step: " << numberOfIterations << endl);
1365
1366 evaluate(data);
1367
1368 DEBUG("lambda: " << FIXED(12,5) << lambda << endl);
1369 DEBUG("chi2: " << FIXED(12,3) << successor << endl);
1370
1371 if (successor < precessor) {
1372
1373 if (numberOfIterations != 0) {
1374
1375 if (fabs(precessor - successor) < EPSILON) {
1376
1377 seterr(data);
1378
1379 return { successor / estimator->getRho(1.0), ndf };
1380 }
1381
1382 if (lambda > LAMBDA_MIN) {
1384 }
1385 }
1386
1387 precessor = successor;
1388 previous = value;
1389
1390 } else {
1391
1392 value = previous;
1393 lambda *= LAMBDA_UP;
1394
1395 if (lambda > LAMBDA_MAX) {
1396 break;
1397 }
1398
1399 evaluate(data);
1400 }
1401
1402 if (debug >= debug_t) {
1403
1404 size_t row = 0;
1405
1406 if (value.R .isFree()) { cout << "R " << FIXED(12,5) << Y[row] << endl; ++row; }
1407 if (value.p1.isFree()) { cout << "p1 " << FIXED(12,5) << Y[row] << endl; ++row; }
1408 if (value.p2.isFree()) { cout << "p2 " << FIXED(12,5) << Y[row] << endl; ++row; }
1409 if (value.p3.isFree()) { cout << "p3 " << FIXED(12,5) << Y[row] << endl; ++row; }
1410 if (value.p4.isFree()) { cout << "p4 " << FIXED(12,5) << Y[row] << endl; ++row; }
1411 if (value.cc.isFree()) { cout << "cc " << FIXED(12,3) << Y[row] << endl; ++row; }
1412
1413 for (int pmt = 0; pmt != NUMBER_OF_PMTS; ++pmt) {
1414 if (value.parameters[pmt].QE .isFree()) { cout << "PMT[" << setw(2) << pmt << "].QE " << FIXED(12,5) << Y[row] << endl; ++row; }
1415 if (value.parameters[pmt].TTS.isFree()) { cout << "PMT[" << setw(2) << pmt << "].TTS " << FIXED(12,5) << Y[row] << endl; ++row; }
1416 if (value.parameters[pmt].t0 .isFree()) { cout << "PMT[" << setw(2) << pmt << "].t0 " << FIXED(12,5) << Y[row] << endl; ++row; }
1417 if (value.parameters[pmt].bg .isFree()) { cout << "PMT[" << setw(2) << pmt << "].bg " << FIXED(12,5) << Y[row] << endl; ++row; }
1418 }
1419 }
1420
1421 // force definite positiveness
1422
1423 for (size_t i = 0; i != N; ++i) {
1424
1425 if (V(i,i) < PIVOT) {
1426 V(i,i) = PIVOT;
1427 }
1428
1429 h[i] = 1.0 / sqrt(V(i,i));
1430 }
1431
1432 // normalisation
1433
1434 for (size_t i = 0; i != N; ++i) {
1435 for (size_t j = 0; j != i; ++j) {
1436 V(j,i) *= h[i] * h[j];
1437 V(i,j) = V(j,i);
1438 }
1439 }
1440
1441 for (size_t i = 0; i != N; ++i) {
1442 V(i,i) = 1.0 + lambda;
1443 }
1444
1445 // solve A x = b
1446
1447 for (size_t col = 0; col != N; ++col) {
1448 Y[col] *= h[col];
1449 }
1450
1451 try {
1452 V.solve(Y);
1453 }
1454 catch (const exception& error) {
1455
1456 ERROR("JGandalf: " << error.what() << endl << V << endl);
1457
1458 break;
1459 }
1460
1461 // update value
1462
1463 const double factor = 2.0;
1464
1465 size_t row = 0;
1466
1467 if (value.R .isFree()) { value.R -= factor * h[row] * Y[row]; ++row; }
1468 if (value.p1.isFree()) { value.p1 -= factor * h[row] * Y[row]; ++row; }
1469 if (value.p2.isFree()) { value.p2 -= factor * h[row] * Y[row]; ++row; }
1470 if (value.p3.isFree()) { value.p3 -= factor * h[row] * Y[row]; ++row; }
1471 if (value.p4.isFree()) { value.p4 -= factor * h[row] * Y[row]; ++row; }
1472 if (value.cc.isFree()) { value.cc -= factor * h[row] * Y[row]; ++row; }
1473 if (value.bc.isFree()) { value.bc -= factor * h[row] * Y[row]; ++row; }
1474
1475 for (int pmt = 0; pmt != NUMBER_OF_PMTS; ++pmt) {
1476 if (value.parameters[pmt].QE .isFree()) { value.parameters[pmt].QE -= factor * h[row] * Y[row]; ++row; }
1477 if (value.parameters[pmt].TTS.isFree()) { value.parameters[pmt].TTS -= factor * h[row] * Y[row]; ++row; }
1478 if (value.parameters[pmt].t0 .isFree()) { value.parameters[pmt].t0 -= factor * h[row] * Y[row]; ++row; }
1479 if (value.parameters[pmt].bg .isFree()) { value.parameters[pmt].bg -= factor * h[row] * Y[row]; ++row; }
1480 }
1481 }
1482
1483 seterr(data);
1484
1485 return { precessor / estimator->getRho(1.0), ndf };
1486 }
#define DEBUG(A)
Message macros.
Definition JMessage.hh:62
#define ERROR(A)
Definition JMessage.hh:66
std::vector< double > h
Definition JFitK40.hh:1689
static constexpr double LAMBDA_MIN
minimal value control parameter
Definition JFitK40.hh:1491
static constexpr double LAMBDA_DOWN
multiplication factor control parameter
Definition JFitK40.hh:1494
void seterr(const data_type &data)
Set errors.
Definition JFitK40.hh:1650
static constexpr double LAMBDA_MAX
maximal value control parameter
Definition JFitK40.hh:1492
static constexpr double LAMBDA_UP
multiplication factor control parameter
Definition JFitK40.hh:1493
JMATH::JMatrixNS V
Definition JFitK40.hh:1504
static constexpr double EPSILON
maximal distance to minimum.
Definition JFitK40.hh:1490
void evaluate(const data_type &data)
Evaluation of fit.
Definition JFitK40.hh:1512
static constexpr int MAXIMUM_ITERATIONS
maximal number of iterations.
Definition JFitK40.hh:1489
static constexpr double PIVOT
minimal value diagonal element of matrix
Definition JFitK40.hh:1495
JMATH::JVectorND Y
Definition JFitK40.hh:1686
bool isFree() const
Check if parameter is free.
Definition JFitK40.hh:240
void setRange(const double xmin, const double xmax)
Set range.
Definition JFitK40.hh:322
const double xmax
const double xmin
int j
Definition JPolint.hh:801
Auxiliary data structure for floating point format specification.
Definition JManip.hh:448
JParameter_t bc
constant background
Definition JFitK40.hh:691
JParameter_t R
maximal coincidence rate [Hz]
Definition JFitK40.hh:685
JParameter_t p1
1st order angle dependence coincidence rate
Definition JFitK40.hh:686
JParameter_t p2
2nd order angle dependence coincidence rate
Definition JFitK40.hh:687
JParameter_t p3
3rd order angle dependence coincidence rate
Definition JFitK40.hh:688
JParameter_t p4
4th order angle dependence coincidence rate
Definition JFitK40.hh:689
JParameter_t cc
fraction of signal correlated background
Definition JFitK40.hh:690
JPMTParameters_t parameters[NUMBER_OF_PMTS]
Definition JFitK40.hh:823
size_t getN() const
Get number of fit parameters.
Definition JFitK40.hh:1107
void setIndex()
Set index of PMT used for fixed time offset.
Definition JFitK40.hh:1081
JParameter_t t0
time offset [ns]
Definition JFitK40.hh:608
JParameter_t TTS
transition-time spread [ns]
Definition JFitK40.hh:607
JParameter_t bg
background [Hz/ns]
Definition JFitK40.hh:609
JParameter_t QE
relative quantum efficiency [unit]
Definition JFitK40.hh:606
Data structure for measured coincidence rate of pair of PMTs.
Definition JFitK40.hh:66
double dt_ns
time difference [ns]
Definition JFitK40.hh:92
void resize(const size_t size)
Resize matrix.
Definition JMatrixND.hh:446
void solve(JVectorND_t &u)
Get solution of equation A x = b.
Definition JMatrixNS.hh:308
Data structure for a pair of indices.

◆ evaluate()

void JCALIBRATE::JFit::evaluate ( const data_type & data)
inlineprivate

Evaluation of fit.

Parameters
datadata

Definition at line 1512 of file JFitK40.hh.

1513 {
1514 using namespace std;
1515 using namespace JPP;
1516
1517 typedef JModel::real_type real_type;
1518
1519
1520 successor = 0.0;
1521
1522 V.reset();
1523 Y.reset();
1524
1525
1526 // model parameter indices
1527
1528 const struct M_t {
1529 M_t(const JModel& model)
1530 {
1531 R = model.getIndex(&JK40Parameters_t::R);
1532 p1 = model.getIndex(&JK40Parameters_t::p1);
1533 p2 = model.getIndex(&JK40Parameters_t::p2);
1534 p3 = model.getIndex(&JK40Parameters_t::p3);
1535 p4 = model.getIndex(&JK40Parameters_t::p4);
1536 cc = model.getIndex(&JK40Parameters_t::cc);
1537 bc = model.getIndex(&JK40Parameters_t::bc);
1538 }
1539
1540 int R;
1541 int p1;
1542 int p2;
1543 int p3;
1544 int p4;
1545 int cc;
1546 int bc;
1547
1548 } M(value);
1549
1550
1551 // PMT parameter indices
1552
1553 struct I_t {
1554 I_t(const JModel& model, const int pmt) :
1555 QE (INVALID_INDEX),
1556 TTS(INVALID_INDEX),
1557 t0 (INVALID_INDEX),
1558 bg (INVALID_INDEX)
1559 {
1560 const int index = model.getIndex(pmt);
1561
1562 int N = 0;
1563
1564 if (model.parameters[pmt].QE .isFree()) { QE = index + N; ++N; }
1565 if (model.parameters[pmt].TTS.isFree()) { TTS = index + N; ++N; }
1566 if (model.parameters[pmt].t0 .isFree()) { t0 = index + N; ++N; }
1567 if (model.parameters[pmt].bg .isFree()) { bg = index + N; ++N; }
1568 }
1569
1570 int QE;
1571 int TTS;
1572 int t0;
1573 int bg;
1574 };
1575
1576
1578
1579 buffer_type buffer;
1580
1581 for (data_type::const_iterator ix = data.begin(); ix != data.end(); ++ix) {
1582
1583 const pair_type& pair = ix->first;
1584
1585 if (value.parameters[pair.first ].status &&
1586 value.parameters[pair.second].status) {
1587
1588 const real_type& real = value.getReal(pair);
1589
1590 const JGauss gauss(real.t0, real.sigma, real.signal);
1591
1592 const double R1 = value.getValue (real.ct);
1593 const JK40Parameters_t& R1p = value.getGradient(real.ct);
1594
1595 const std::pair<I_t, I_t> PMT(I_t(value, pair.first),
1596 I_t(value, pair.second));
1597
1598 for (const rate_type& iy : ix->second) {
1599
1600 const double R2 = gauss.getValue (iy.dt_ns);
1601 const JGauss& R2p = gauss.getGradient(iy.dt_ns);
1602
1603 const double R = real.bc + real.background + R1 * (real.cc + R2);
1604 const double u = (iy.value - R) / iy.error;
1605 const double w = -estimator->getPsi(u) / iy.error;
1606
1607 successor += estimator->getRho(u);
1608
1609 buffer.clear();
1610
1611 if (M.R != INVALID_INDEX) { buffer.push_back({M.R, w * (value.cc() + R2) * R1p.R () * value.R .getDerivative()}); }
1612 if (M.p1 != INVALID_INDEX) { buffer.push_back({M.p1, w * (value.cc() + R2) * R1p.p1() * value.p1.getDerivative()}); }
1613 if (M.p2 != INVALID_INDEX) { buffer.push_back({M.p2, w * (value.cc() + R2) * R1p.p2() * value.p2.getDerivative()}); }
1614 if (M.p3 != INVALID_INDEX) { buffer.push_back({M.p3, w * (value.cc() + R2) * R1p.p3() * value.p3.getDerivative()}); }
1615 if (M.p4 != INVALID_INDEX) { buffer.push_back({M.p4, w * (value.cc() + R2) * R1p.p4() * value.p4.getDerivative()}); }
1616 if (M.cc != INVALID_INDEX) { buffer.push_back({M.cc, w * R1 * R1p.cc() * value.cc.getDerivative()}); }
1617 if (M.bc != INVALID_INDEX) { buffer.push_back({M.bc, w * R1p.bc() * value.bc.getDerivative()}); }
1618
1619 if (PMT.first .QE != INVALID_INDEX) { buffer.push_back({PMT.first .QE , w * R1 * R2p.signal * value.parameters[pair.second].QE () * value.parameters[pair.first ].QE .getDerivative()}); }
1620 if (PMT.second.QE != INVALID_INDEX) { buffer.push_back({PMT.second.QE , w * R1 * R2p.signal * value.parameters[pair.first ].QE () * value.parameters[pair.second].QE .getDerivative()}); }
1621 if (PMT.first .TTS != INVALID_INDEX) { buffer.push_back({PMT.first .TTS, w * R1 * R2p.sigma * value.parameters[pair.first ].TTS() * value.parameters[pair.first ].TTS.getDerivative() / real.sigma}); }
1622 if (PMT.second.TTS != INVALID_INDEX) { buffer.push_back({PMT.second.TTS, w * R1 * R2p.sigma * value.parameters[pair.second].TTS() * value.parameters[pair.second].TTS.getDerivative() / real.sigma}); }
1623 if (PMT.first .t0 != INVALID_INDEX) { buffer.push_back({PMT.first .t0, w * R1 * R2p.mean * value.parameters[pair.first ].t0 .getDerivative() * +1.0}); }
1624 if (PMT.second.t0 != INVALID_INDEX) { buffer.push_back({PMT.second.t0, w * R1 * R2p.mean * value.parameters[pair.second].t0 .getDerivative() * -1.0}); }
1625 if (PMT.first .bg != INVALID_INDEX) { buffer.push_back({PMT.first .bg, w * value.parameters[pair.first ].bg .getDerivative()}); }
1626 if (PMT.second.bg != INVALID_INDEX) { buffer.push_back({PMT.second.bg, w * value.parameters[pair.second].bg .getDerivative()}); }
1627
1628 for (buffer_type::const_iterator row = buffer.begin(); row != buffer.end(); ++row) {
1629
1630 Y[row->first] += row->second;
1631
1632 V[row->first][row->first] += row->second * row->second;
1633
1634 for (buffer_type::const_iterator col = buffer.begin(); col != row; ++col) {
1635 V[row->first][col->first] += row->second * col->second;
1636 V[col->first][row->first] = V[row->first][col->first];
1637 }
1638 }
1639 }
1640 }
1641 }
1642 }
TPaveText * p1
static const int INVALID_INDEX
invalid index
Definition JFitK40.hh:60
if((p==this->begin() &&this->getDistance(x,(p++) ->getX()) > distance_type::precision)||(p==this->end() &&this->getDistance((--p) ->getX(), x) > distance_type::precision))
Template base class for polynomial interpolations with polynomial result.
Definition JPolint.hh:775
Model for fit to acoustics data.
Auxiliary data structure for derived quantities of a given PMT pair.
Definition JFitK40.hh:866
JMatrixND & reset()
Set matrix to the null matrix.
Definition JMatrixND.hh:459
void reset()
Reset.
Definition JVectorND.hh:45

◆ seterr()

void JCALIBRATE::JFit::seterr ( const data_type & data)
inlineprivate

Set errors.

Parameters
datadata

Definition at line 1650 of file JFitK40.hh.

1651 {
1652 using namespace std;
1653
1654 error.reset();
1655
1656 evaluate(data);
1657
1658 try {
1659 V.invert();
1660 }
1661 catch (const exception& error) {}
1662
1663#define SQRT(X) (X >= 0.0 ? sqrt(X) : std::numeric_limits<double>::max())
1664
1665 size_t i = 0;
1666
1667 if (value.R .isFree()) { error.R = SQRT(V(i,i)); ++i; }
1668 if (value.p1.isFree()) { error.p1 = SQRT(V(i,i)); ++i; }
1669 if (value.p2.isFree()) { error.p2 = SQRT(V(i,i)); ++i; }
1670 if (value.p3.isFree()) { error.p3 = SQRT(V(i,i)); ++i; }
1671 if (value.p4.isFree()) { error.p4 = SQRT(V(i,i)); ++i; }
1672 if (value.cc.isFree()) { error.cc = SQRT(V(i,i)); ++i; }
1673 if (value.bc.isFree()) { error.bc = SQRT(V(i,i)); ++i; }
1674
1675 for (int pmt = 0; pmt != NUMBER_OF_PMTS; ++pmt) {
1676 if (value.parameters[pmt].QE .isFree()) { error.parameters[pmt].QE = SQRT(V(i,i)); ++i; }
1677 if (value.parameters[pmt].TTS.isFree()) { error.parameters[pmt].TTS = SQRT(V(i,i)); ++i; }
1678 if (value.parameters[pmt].t0 .isFree()) { error.parameters[pmt].t0 = SQRT(V(i,i)); ++i; }
1679 if (value.parameters[pmt].bg .isFree()) { error.parameters[pmt].bg = SQRT(V(i,i)); ++i; }
1680 }
1681
1682#undef SQRT
1683 }
#define SQRT(X)
void invert()
Invert matrix according LDU decomposition.
Definition JMatrixNS.hh:75

Member Data Documentation

◆ MAXIMUM_ITERATIONS

int JCALIBRATE::JFit::MAXIMUM_ITERATIONS = 100000
staticconstexpr

maximal number of iterations.

Definition at line 1489 of file JFitK40.hh.

◆ EPSILON

double JCALIBRATE::JFit::EPSILON = 1.0e-3
staticconstexpr

maximal distance to minimum.

Definition at line 1490 of file JFitK40.hh.

◆ LAMBDA_MIN

double JCALIBRATE::JFit::LAMBDA_MIN = 1.0e-2
staticconstexpr

minimal value control parameter

Definition at line 1491 of file JFitK40.hh.

◆ LAMBDA_MAX

double JCALIBRATE::JFit::LAMBDA_MAX = 1.0e+4
staticconstexpr

maximal value control parameter

Definition at line 1492 of file JFitK40.hh.

◆ LAMBDA_UP

double JCALIBRATE::JFit::LAMBDA_UP = 10.0
staticconstexpr

multiplication factor control parameter

Definition at line 1493 of file JFitK40.hh.

◆ LAMBDA_DOWN

double JCALIBRATE::JFit::LAMBDA_DOWN = 10.0
staticconstexpr

multiplication factor control parameter

Definition at line 1494 of file JFitK40.hh.

◆ PIVOT

double JCALIBRATE::JFit::PIVOT = std::numeric_limits<double>::epsilon()
staticconstexpr

minimal value diagonal element of matrix

Definition at line 1495 of file JFitK40.hh.

◆ debug

int JCALIBRATE::JFit::debug

Definition at line 1497 of file JFitK40.hh.

◆ estimator

estimator_type JCALIBRATE::JFit::estimator

M-Estimator function.

Definition at line 1498 of file JFitK40.hh.

◆ lambda

double JCALIBRATE::JFit::lambda

Definition at line 1500 of file JFitK40.hh.

◆ value

JModel JCALIBRATE::JFit::value

Definition at line 1501 of file JFitK40.hh.

◆ error

JModel_t JCALIBRATE::JFit::error

Definition at line 1502 of file JFitK40.hh.

◆ numberOfIterations

int JCALIBRATE::JFit::numberOfIterations

Definition at line 1503 of file JFitK40.hh.

◆ V

JMATH::JMatrixNS JCALIBRATE::JFit::V

Definition at line 1504 of file JFitK40.hh.

◆ Y

JMATH::JVectorND JCALIBRATE::JFit::Y
private

Definition at line 1686 of file JFitK40.hh.

◆ successor

double JCALIBRATE::JFit::successor
private

Definition at line 1687 of file JFitK40.hh.

◆ previous

JModel JCALIBRATE::JFit::previous
private

Definition at line 1688 of file JFitK40.hh.

◆ h

std::vector<double> JCALIBRATE::JFit::h
private

Definition at line 1689 of file JFitK40.hh.


The documentation for this class was generated from the following file: