Jpp 20.0.0-72-g597b30bc9
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 1290 of file JFitK40.hh.

Member Typedef Documentation

◆ estimator_type

Definition at line 1301 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 1310 of file JFitK40.hh.

1310 :
1311 debug(debug)
1312 {
1313 using namespace JPP;
1314
1315 estimator.reset(getMEstimator(option));
1316 }
estimator_type estimator
M-Estimator function.
Definition JFitK40.hh:1513
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 1325 of file JFitK40.hh.

1326 {
1327 using namespace std;
1328 using namespace JPP;
1329
1330
1331 value.setIndex();
1332
1333 const size_t N = value.getN();
1334
1335 V.resize(N);
1336 Y.resize(N);
1337 h.resize(N);
1338
1339 double xmax = numeric_limits<double>::lowest();
1340 double xmin = numeric_limits<double>::max();
1341
1342 int ndf = 0;
1343
1344 for (data_type::const_iterator ix = data.begin(); ix != data.end(); ++ix) {
1345
1346 const pair_type& pair = ix->first;
1347
1348 if (value.parameters[pair.first ].status &&
1349 value.parameters[pair.second].status) {
1350
1351 ndf += ix->second.size();
1352
1353 for (const rate_type& iy : ix->second) {
1354 if (iy.dt_ns > xmax) { xmax = iy.dt_ns; }
1355 if (iy.dt_ns < xmin) { xmin = iy.dt_ns; }
1356 }
1357 }
1358 }
1359
1360 ndf -= value.getN();
1361
1362 if (ndf < 0) {
1363 return { 0.0, ndf };
1364 }
1365
1366 for (int pmt = 0; pmt != NUMBER_OF_PMTS; ++pmt) {
1367 if (value.parameters[pmt].t0.isFree()) {
1368 value.parameters[pmt].t0.setRange(xmin, xmax);
1369 }
1370 }
1371
1372
1374
1375 double precessor = numeric_limits<double>::max();
1376
1378
1379 DEBUG("step: " << numberOfIterations << endl);
1380
1381 evaluate(data);
1382
1383 DEBUG("lambda: " << FIXED(12,5) << lambda << endl);
1384 DEBUG("chi2: " << FIXED(12,3) << successor << endl);
1385
1386 if (successor < precessor) {
1387
1388 if (numberOfIterations != 0) {
1389
1390 if (fabs(precessor - successor) < EPSILON) {
1391
1392 seterr(data);
1393
1394 return { successor / estimator->getRho(1.0), ndf };
1395 }
1396
1397 if (lambda > LAMBDA_MIN) {
1399 }
1400 }
1401
1402 precessor = successor;
1403 previous = value;
1404
1405 } else {
1406
1407 value = previous;
1408 lambda *= LAMBDA_UP;
1409
1410 if (lambda > LAMBDA_MAX) {
1411 break;
1412 }
1413
1414 evaluate(data);
1415 }
1416
1417 if (debug >= debug_t) {
1418
1419 size_t row = 0;
1420
1421 if (value.R .isFree()) { cout << "R " << FIXED(12,5) << Y[row] << endl; ++row; }
1422 if (value.p1.isFree()) { cout << "p1 " << FIXED(12,5) << Y[row] << endl; ++row; }
1423 if (value.p2.isFree()) { cout << "p2 " << FIXED(12,5) << Y[row] << endl; ++row; }
1424 if (value.p3.isFree()) { cout << "p3 " << FIXED(12,5) << Y[row] << endl; ++row; }
1425 if (value.p4.isFree()) { cout << "p4 " << FIXED(12,5) << Y[row] << endl; ++row; }
1426 if (value.cc.isFree()) { cout << "cc " << FIXED(12,3) << Y[row] << endl; ++row; }
1427
1428 for (int pmt = 0; pmt != NUMBER_OF_PMTS; ++pmt) {
1429 if (value.parameters[pmt].QE .isFree()) { cout << "PMT[" << setw(2) << pmt << "].QE " << FIXED(12,5) << Y[row] << endl; ++row; }
1430 if (value.parameters[pmt].TTS.isFree()) { cout << "PMT[" << setw(2) << pmt << "].TTS " << FIXED(12,5) << Y[row] << endl; ++row; }
1431 if (value.parameters[pmt].t0 .isFree()) { cout << "PMT[" << setw(2) << pmt << "].t0 " << FIXED(12,5) << Y[row] << endl; ++row; }
1432 if (value.parameters[pmt].bg .isFree()) { cout << "PMT[" << setw(2) << pmt << "].bg " << FIXED(12,5) << Y[row] << endl; ++row; }
1433 }
1434 }
1435
1436 // force definite positiveness
1437
1438 for (size_t i = 0; i != N; ++i) {
1439
1440 if (V(i,i) < PIVOT) {
1441 V(i,i) = PIVOT;
1442 }
1443
1444 h[i] = 1.0 / sqrt(V(i,i));
1445 }
1446
1447 // normalisation
1448
1449 for (size_t i = 0; i != N; ++i) {
1450 for (size_t j = 0; j != i; ++j) {
1451 V(j,i) *= h[i] * h[j];
1452 V(i,j) = V(j,i);
1453 }
1454 }
1455
1456 for (size_t i = 0; i != N; ++i) {
1457 V(i,i) = 1.0 + lambda;
1458 }
1459
1460 // solve A x = b
1461
1462 for (size_t col = 0; col != N; ++col) {
1463 Y[col] *= h[col];
1464 }
1465
1466 try {
1467 V.solve(Y);
1468 }
1469 catch (const exception& error) {
1470
1471 ERROR("JGandalf: " << error.what() << endl << V << endl);
1472
1473 break;
1474 }
1475
1476 // update value
1477
1478 const double factor = 2.0;
1479
1480 size_t row = 0;
1481
1482 if (value.R .isFree()) { value.R -= factor * h[row] * Y[row]; ++row; }
1483 if (value.p1.isFree()) { value.p1 -= factor * h[row] * Y[row]; ++row; }
1484 if (value.p2.isFree()) { value.p2 -= factor * h[row] * Y[row]; ++row; }
1485 if (value.p3.isFree()) { value.p3 -= factor * h[row] * Y[row]; ++row; }
1486 if (value.p4.isFree()) { value.p4 -= factor * h[row] * Y[row]; ++row; }
1487 if (value.cc.isFree()) { value.cc -= factor * h[row] * Y[row]; ++row; }
1488 if (value.bc.isFree()) { value.bc -= factor * h[row] * Y[row]; ++row; }
1489
1490 for (int pmt = 0; pmt != NUMBER_OF_PMTS; ++pmt) {
1491 if (value.parameters[pmt].QE .isFree()) { value.parameters[pmt].QE -= factor * h[row] * Y[row]; ++row; }
1492 if (value.parameters[pmt].TTS.isFree()) { value.parameters[pmt].TTS -= factor * h[row] * Y[row]; ++row; }
1493 if (value.parameters[pmt].t0 .isFree()) { value.parameters[pmt].t0 -= factor * h[row] * Y[row]; ++row; }
1494 if (value.parameters[pmt].bg .isFree()) { value.parameters[pmt].bg -= factor * h[row] * Y[row]; ++row; }
1495 }
1496 }
1497
1498 seterr(data);
1499
1500 return { precessor / estimator->getRho(1.0), ndf };
1501 }
#define DEBUG(A)
Message macros.
Definition JMessage.hh:62
#define ERROR(A)
Definition JMessage.hh:66
std::vector< double > h
Definition JFitK40.hh:1704
static constexpr double LAMBDA_MIN
minimal value control parameter
Definition JFitK40.hh:1506
static constexpr double LAMBDA_DOWN
multiplication factor control parameter
Definition JFitK40.hh:1509
void seterr(const data_type &data)
Set errors.
Definition JFitK40.hh:1665
static constexpr double LAMBDA_MAX
maximal value control parameter
Definition JFitK40.hh:1507
static constexpr double LAMBDA_UP
multiplication factor control parameter
Definition JFitK40.hh:1508
JMATH::JMatrixNS V
Definition JFitK40.hh:1519
static constexpr double EPSILON
maximal distance to minimum.
Definition JFitK40.hh:1505
void evaluate(const data_type &data)
Evaluation of fit.
Definition JFitK40.hh:1527
static constexpr int MAXIMUM_ITERATIONS
maximal number of iterations.
Definition JFitK40.hh:1504
static constexpr double PIVOT
minimal value diagonal element of matrix
Definition JFitK40.hh:1510
JMATH::JVectorND Y
Definition JFitK40.hh:1701
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
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:1112
void setIndex()
Set index of PMT used for fixed time offset.
Definition JFitK40.hh:1086
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 1527 of file JFitK40.hh.

1528 {
1529 using namespace std;
1530 using namespace JPP;
1531
1532 typedef JModel::real_type real_type;
1533
1534
1535 successor = 0.0;
1536
1537 V.reset();
1538 Y.reset();
1539
1540
1541 // model parameter indices
1542
1543 const struct M_t {
1544 M_t(const JModel& model)
1545 {
1546 R = model.getIndex(&JK40Parameters_t::R);
1547 p1 = model.getIndex(&JK40Parameters_t::p1);
1548 p2 = model.getIndex(&JK40Parameters_t::p2);
1549 p3 = model.getIndex(&JK40Parameters_t::p3);
1550 p4 = model.getIndex(&JK40Parameters_t::p4);
1551 cc = model.getIndex(&JK40Parameters_t::cc);
1552 bc = model.getIndex(&JK40Parameters_t::bc);
1553 }
1554
1555 int R;
1556 int p1;
1557 int p2;
1558 int p3;
1559 int p4;
1560 int cc;
1561 int bc;
1562
1563 } M(value);
1564
1565
1566 // PMT parameter indices
1567
1568 struct I_t {
1569 I_t(const JModel& model, const int pmt) :
1570 QE (INVALID_INDEX),
1571 TTS(INVALID_INDEX),
1572 t0 (INVALID_INDEX),
1573 bg (INVALID_INDEX)
1574 {
1575 const int index = model.getIndex(pmt);
1576
1577 int N = 0;
1578
1579 if (model.parameters[pmt].QE .isFree()) { QE = index + N; ++N; }
1580 if (model.parameters[pmt].TTS.isFree()) { TTS = index + N; ++N; }
1581 if (model.parameters[pmt].t0 .isFree()) { t0 = index + N; ++N; }
1582 if (model.parameters[pmt].bg .isFree()) { bg = index + N; ++N; }
1583 }
1584
1585 int QE;
1586 int TTS;
1587 int t0;
1588 int bg;
1589 };
1590
1591
1593
1594 buffer_type buffer;
1595
1596 for (data_type::const_iterator ix = data.begin(); ix != data.end(); ++ix) {
1597
1598 const pair_type& pair = ix->first;
1599
1600 if (value.parameters[pair.first ].status &&
1601 value.parameters[pair.second].status) {
1602
1603 const real_type& real = value.getReal(pair);
1604
1605 const JBell bell(real.t0, real.sigma, real.signal, 0.0, BELL_SHAPE);
1606
1607 const double R1 = value.getValue (real.ct);
1608 const JK40Parameters_t& R1p = value.getGradient(real.ct);
1609
1610 const std::pair<I_t, I_t> PMT(I_t(value, pair.first),
1611 I_t(value, pair.second));
1612
1613 for (const rate_type& iy : ix->second) {
1614
1615 const double R2 = bell.getValue (iy.dt_ns);
1616 const JBell_t& R2p = bell.getGradient(iy.dt_ns);
1617
1618 const double R = real.bc + real.background + R1 * (real.cc + R2);
1619 const double u = (iy.value - R) / iy.error;
1620 const double w = -estimator->getPsi(u) / iy.error;
1621
1622 successor += estimator->getRho(u);
1623
1624 buffer.clear();
1625
1626 if (M.R != INVALID_INDEX) { buffer.push_back({M.R, w * (value.cc() + R2) * R1p.R () * value.R .getDerivative()}); }
1627 if (M.p1 != INVALID_INDEX) { buffer.push_back({M.p1, w * (value.cc() + R2) * R1p.p1() * value.p1.getDerivative()}); }
1628 if (M.p2 != INVALID_INDEX) { buffer.push_back({M.p2, w * (value.cc() + R2) * R1p.p2() * value.p2.getDerivative()}); }
1629 if (M.p3 != INVALID_INDEX) { buffer.push_back({M.p3, w * (value.cc() + R2) * R1p.p3() * value.p3.getDerivative()}); }
1630 if (M.p4 != INVALID_INDEX) { buffer.push_back({M.p4, w * (value.cc() + R2) * R1p.p4() * value.p4.getDerivative()}); }
1631 if (M.cc != INVALID_INDEX) { buffer.push_back({M.cc, w * R1 * R1p.cc() * value.cc.getDerivative()}); }
1632 if (M.bc != INVALID_INDEX) { buffer.push_back({M.bc, w * R1p.bc() * value.bc.getDerivative()}); }
1633
1634 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()}); }
1635 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()}); }
1636 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}); }
1637 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}); }
1638 if (PMT.first .t0 != INVALID_INDEX) { buffer.push_back({PMT.first .t0, w * R1 * R2p.mean * value.parameters[pair.first ].t0 .getDerivative() * +1.0}); }
1639 if (PMT.second.t0 != INVALID_INDEX) { buffer.push_back({PMT.second.t0, w * R1 * R2p.mean * value.parameters[pair.second].t0 .getDerivative() * -1.0}); }
1640 if (PMT.first .bg != INVALID_INDEX) { buffer.push_back({PMT.first .bg, w * value.parameters[pair.first ].bg .getDerivative()}); }
1641 if (PMT.second.bg != INVALID_INDEX) { buffer.push_back({PMT.second.bg, w * value.parameters[pair.second].bg .getDerivative()}); }
1642
1643 for (buffer_type::const_iterator row = buffer.begin(); row != buffer.end(); ++row) {
1644
1645 Y[row->first] += row->second;
1646
1647 V[row->first][row->first] += row->second * row->second;
1648
1649 for (buffer_type::const_iterator col = buffer.begin(); col != row; ++col) {
1650 V[row->first][col->first] += row->second * col->second;
1651 V[col->first][row->first] = V[row->first][col->first];
1652 }
1653 }
1654 }
1655 }
1656 }
1657 }
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:869
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 1665 of file JFitK40.hh.

1666 {
1667 using namespace std;
1668
1669 error.reset();
1670
1671 evaluate(data);
1672
1673 try {
1674 V.invert();
1675 }
1676 catch (const exception& error) {}
1677
1678#define SQRT(X) (X >= 0.0 ? sqrt(X) : std::numeric_limits<double>::max())
1679
1680 size_t i = 0;
1681
1682 if (value.R .isFree()) { error.R = SQRT(V(i,i)); ++i; }
1683 if (value.p1.isFree()) { error.p1 = SQRT(V(i,i)); ++i; }
1684 if (value.p2.isFree()) { error.p2 = SQRT(V(i,i)); ++i; }
1685 if (value.p3.isFree()) { error.p3 = SQRT(V(i,i)); ++i; }
1686 if (value.p4.isFree()) { error.p4 = SQRT(V(i,i)); ++i; }
1687 if (value.cc.isFree()) { error.cc = SQRT(V(i,i)); ++i; }
1688 if (value.bc.isFree()) { error.bc = SQRT(V(i,i)); ++i; }
1689
1690 for (int pmt = 0; pmt != NUMBER_OF_PMTS; ++pmt) {
1691 if (value.parameters[pmt].QE .isFree()) { error.parameters[pmt].QE = SQRT(V(i,i)); ++i; }
1692 if (value.parameters[pmt].TTS.isFree()) { error.parameters[pmt].TTS = SQRT(V(i,i)); ++i; }
1693 if (value.parameters[pmt].t0 .isFree()) { error.parameters[pmt].t0 = SQRT(V(i,i)); ++i; }
1694 if (value.parameters[pmt].bg .isFree()) { error.parameters[pmt].bg = SQRT(V(i,i)); ++i; }
1695 }
1696
1697#undef SQRT
1698 }
#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 1504 of file JFitK40.hh.

◆ EPSILON

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

maximal distance to minimum.

Definition at line 1505 of file JFitK40.hh.

◆ LAMBDA_MIN

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

minimal value control parameter

Definition at line 1506 of file JFitK40.hh.

◆ LAMBDA_MAX

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

maximal value control parameter

Definition at line 1507 of file JFitK40.hh.

◆ LAMBDA_UP

double JCALIBRATE::JFit::LAMBDA_UP = 10.0
staticconstexpr

multiplication factor control parameter

Definition at line 1508 of file JFitK40.hh.

◆ LAMBDA_DOWN

double JCALIBRATE::JFit::LAMBDA_DOWN = 10.0
staticconstexpr

multiplication factor control parameter

Definition at line 1509 of file JFitK40.hh.

◆ PIVOT

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

minimal value diagonal element of matrix

Definition at line 1510 of file JFitK40.hh.

◆ debug

int JCALIBRATE::JFit::debug

Definition at line 1512 of file JFitK40.hh.

◆ estimator

estimator_type JCALIBRATE::JFit::estimator

M-Estimator function.

Definition at line 1513 of file JFitK40.hh.

◆ lambda

double JCALIBRATE::JFit::lambda

Definition at line 1515 of file JFitK40.hh.

◆ value

JModel JCALIBRATE::JFit::value

Definition at line 1516 of file JFitK40.hh.

◆ error

JModel_t JCALIBRATE::JFit::error

Definition at line 1517 of file JFitK40.hh.

◆ numberOfIterations

int JCALIBRATE::JFit::numberOfIterations

Definition at line 1518 of file JFitK40.hh.

◆ V

JMATH::JMatrixNS JCALIBRATE::JFit::V

Definition at line 1519 of file JFitK40.hh.

◆ Y

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

Definition at line 1701 of file JFitK40.hh.

◆ successor

double JCALIBRATE::JFit::successor
private

Definition at line 1702 of file JFitK40.hh.

◆ previous

JModel JCALIBRATE::JFit::previous
private

Definition at line 1703 of file JFitK40.hh.

◆ h

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

Definition at line 1704 of file JFitK40.hh.


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