Jpp 20.0.0-195-g190c9e876
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 1318 of file JFitK40.hh.

Member Typedef Documentation

◆ estimator_type

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

1338 :
1339 debug(debug)
1340 {
1341 using namespace JPP;
1342
1343 estimator.reset(getMEstimator(option));
1344 }
estimator_type estimator
M-Estimator function.
Definition JFitK40.hh:1541
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 1353 of file JFitK40.hh.

1354 {
1355 using namespace std;
1356 using namespace JPP;
1357
1358
1359 value.setIndex();
1360
1361 const size_t N = value.getN();
1362
1363 V.resize(N);
1364 Y.resize(N);
1365 h.resize(N);
1366
1367 double xmax = numeric_limits<double>::lowest();
1368 double xmin = numeric_limits<double>::max();
1369
1370 int ndf = 0;
1371
1372 for (data_type::const_iterator ix = data.begin(); ix != data.end(); ++ix) {
1373
1374 const pair_type& pair = ix->first;
1375
1376 if (value.parameters[pair.first ].status &&
1377 value.parameters[pair.second].status) {
1378
1379 ndf += ix->second.size();
1380
1381 for (const rate_type& iy : ix->second) {
1382 if (iy.dt_ns > xmax) { xmax = iy.dt_ns; }
1383 if (iy.dt_ns < xmin) { xmin = iy.dt_ns; }
1384 }
1385 }
1386 }
1387
1388 ndf -= value.getN();
1389
1390 if (ndf < 0) {
1391 return { 0.0, ndf };
1392 }
1393
1394 for (int pmt = 0; pmt != NUMBER_OF_PMTS; ++pmt) {
1395 if (value.parameters[pmt].t0.isFree()) {
1396 value.parameters[pmt].t0.setRange(xmin, xmax);
1397 }
1398 }
1399
1400
1402
1403 double precessor = numeric_limits<double>::max();
1404
1406
1407 DEBUG("step: " << numberOfIterations << endl);
1408
1409 evaluate(data);
1410
1411 DEBUG("lambda: " << FIXED(12,5) << lambda << endl);
1412 DEBUG("chi2: " << FIXED(12,3) << successor << endl);
1413
1414 if (successor < precessor) {
1415
1416 if (numberOfIterations != 0) {
1417
1418 if (fabs(precessor - successor) < EPSILON) {
1419
1420 seterr(data);
1421
1422 return { successor / estimator->getRho(1.0), ndf };
1423 }
1424
1425 if (lambda > LAMBDA_MIN) {
1427 }
1428 }
1429
1430 precessor = successor;
1431 previous = value;
1432
1433 } else {
1434
1435 value = previous;
1436 lambda *= LAMBDA_UP;
1437
1438 if (lambda > LAMBDA_MAX) {
1439 break;
1440 }
1441
1442 evaluate(data);
1443 }
1444
1445 if (debug >= debug_t) {
1446
1447 size_t row = 0;
1448
1449 if (value.R .isFree()) { cout << "R " << FIXED(12,5) << Y[row] << endl; ++row; }
1450 if (value.p1.isFree()) { cout << "p1 " << FIXED(12,5) << Y[row] << endl; ++row; }
1451 if (value.p2.isFree()) { cout << "p2 " << FIXED(12,5) << Y[row] << endl; ++row; }
1452 if (value.p3.isFree()) { cout << "p3 " << FIXED(12,5) << Y[row] << endl; ++row; }
1453 if (value.p4.isFree()) { cout << "p4 " << FIXED(12,5) << Y[row] << endl; ++row; }
1454 if (value.cc.isFree()) { cout << "cc " << FIXED(12,3) << Y[row] << endl; ++row; }
1455
1456 for (int pmt = 0; pmt != NUMBER_OF_PMTS; ++pmt) {
1457 if (value.parameters[pmt].QE .isFree()) { cout << "PMT[" << setw(2) << pmt << "].QE " << FIXED(12,5) << Y[row] << endl; ++row; }
1458 if (value.parameters[pmt].TTS.isFree()) { cout << "PMT[" << setw(2) << pmt << "].TTS " << FIXED(12,5) << Y[row] << endl; ++row; }
1459 if (value.parameters[pmt].t0 .isFree()) { cout << "PMT[" << setw(2) << pmt << "].t0 " << FIXED(12,5) << Y[row] << endl; ++row; }
1460 if (value.parameters[pmt].bg .isFree()) { cout << "PMT[" << setw(2) << pmt << "].bg " << FIXED(12,5) << Y[row] << endl; ++row; }
1461 }
1462 }
1463
1464 // force definite positiveness
1465
1466 for (size_t i = 0; i != N; ++i) {
1467
1468 if (V(i,i) < PIVOT) {
1469 V(i,i) = PIVOT;
1470 }
1471
1472 h[i] = 1.0 / sqrt(V(i,i));
1473 }
1474
1475 // normalisation
1476
1477 for (size_t i = 0; i != N; ++i) {
1478 for (size_t j = 0; j != i; ++j) {
1479 V(j,i) *= h[i] * h[j];
1480 V(i,j) = V(j,i);
1481 }
1482 }
1483
1484 for (size_t i = 0; i != N; ++i) {
1485 V(i,i) = 1.0 + lambda;
1486 }
1487
1488 // solve A x = b
1489
1490 for (size_t col = 0; col != N; ++col) {
1491 Y[col] *= h[col];
1492 }
1493
1494 try {
1495 V.solve(Y);
1496 }
1497 catch (const exception& error) {
1498
1499 ERROR("JGandalf: " << error.what() << endl << V << endl);
1500
1501 break;
1502 }
1503
1504 // update value
1505
1506 const double factor = 2.0;
1507
1508 size_t row = 0;
1509
1510 if (value.R .isFree()) { value.R -= factor * h[row] * Y[row]; ++row; }
1511 if (value.p1.isFree()) { value.p1 -= factor * h[row] * Y[row]; ++row; }
1512 if (value.p2.isFree()) { value.p2 -= factor * h[row] * Y[row]; ++row; }
1513 if (value.p3.isFree()) { value.p3 -= factor * h[row] * Y[row]; ++row; }
1514 if (value.p4.isFree()) { value.p4 -= factor * h[row] * Y[row]; ++row; }
1515 if (value.cc.isFree()) { value.cc -= factor * h[row] * Y[row]; ++row; }
1516 if (value.bc.isFree()) { value.bc -= factor * h[row] * Y[row]; ++row; }
1517
1518 for (int pmt = 0; pmt != NUMBER_OF_PMTS; ++pmt) {
1519 if (value.parameters[pmt].QE .isFree()) { value.parameters[pmt].QE -= factor * h[row] * Y[row]; ++row; }
1520 if (value.parameters[pmt].TTS.isFree()) { value.parameters[pmt].TTS -= factor * h[row] * Y[row]; ++row; }
1521 if (value.parameters[pmt].t0 .isFree()) { value.parameters[pmt].t0 -= factor * h[row] * Y[row]; ++row; }
1522 if (value.parameters[pmt].bg .isFree()) { value.parameters[pmt].bg -= factor * h[row] * Y[row]; ++row; }
1523 }
1524 }
1525
1526 seterr(data);
1527
1528 return { precessor / estimator->getRho(1.0), ndf };
1529 }
#define DEBUG(A)
Message macros.
Definition JMessage.hh:62
#define ERROR(A)
Definition JMessage.hh:66
std::vector< double > h
Definition JFitK40.hh:1732
static constexpr double LAMBDA_MIN
minimal value control parameter
Definition JFitK40.hh:1534
static constexpr double LAMBDA_DOWN
multiplication factor control parameter
Definition JFitK40.hh:1537
void seterr(const data_type &data)
Set errors.
Definition JFitK40.hh:1693
static constexpr double LAMBDA_MAX
maximal value control parameter
Definition JFitK40.hh:1535
static constexpr double LAMBDA_UP
multiplication factor control parameter
Definition JFitK40.hh:1536
JMATH::JMatrixNS V
Definition JFitK40.hh:1547
static constexpr double EPSILON
maximal distance to minimum.
Definition JFitK40.hh:1533
void evaluate(const data_type &data)
Evaluation of fit.
Definition JFitK40.hh:1555
static constexpr int MAXIMUM_ITERATIONS
maximal number of iterations.
Definition JFitK40.hh:1532
static constexpr double PIVOT
minimal value diagonal element of matrix
Definition JFitK40.hh:1538
JMATH::JVectorND Y
Definition JFitK40.hh:1729
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:720
JParameter_t R
maximal coincidence rate [Hz]
Definition JFitK40.hh:714
JParameter_t p1
1st order angle dependence coincidence rate
Definition JFitK40.hh:715
JParameter_t p2
2nd order angle dependence coincidence rate
Definition JFitK40.hh:716
JParameter_t p3
3rd order angle dependence coincidence rate
Definition JFitK40.hh:717
JParameter_t p4
4th order angle dependence coincidence rate
Definition JFitK40.hh:718
JParameter_t cc
fraction of signal correlated background
Definition JFitK40.hh:719
JPMTParameters_t parameters[NUMBER_OF_PMTS]
Definition JFitK40.hh:851
size_t getN() const
Get number of fit parameters.
Definition JFitK40.hh:1140
void setIndex()
Set index of PMT used for fixed time offset.
Definition JFitK40.hh:1114
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 1555 of file JFitK40.hh.

1556 {
1557 using namespace std;
1558 using namespace JPP;
1559
1560 typedef JModel::real_type real_type;
1561
1562
1563 successor = 0.0;
1564
1565 V.reset();
1566 Y.reset();
1567
1568
1569 // model parameter indices
1570
1571 const struct M_t {
1572 M_t(const JModel& model)
1573 {
1574 R = model.getIndex(&JK40Parameters_t::R);
1575 p1 = model.getIndex(&JK40Parameters_t::p1);
1576 p2 = model.getIndex(&JK40Parameters_t::p2);
1577 p3 = model.getIndex(&JK40Parameters_t::p3);
1578 p4 = model.getIndex(&JK40Parameters_t::p4);
1579 cc = model.getIndex(&JK40Parameters_t::cc);
1580 bc = model.getIndex(&JK40Parameters_t::bc);
1581 }
1582
1583 int R;
1584 int p1;
1585 int p2;
1586 int p3;
1587 int p4;
1588 int cc;
1589 int bc;
1590
1591 } M(value);
1592
1593
1594 // PMT parameter indices
1595
1596 struct I_t {
1597 I_t(const JModel& model, const int pmt) :
1598 QE (INVALID_INDEX),
1599 TTS(INVALID_INDEX),
1600 t0 (INVALID_INDEX),
1601 bg (INVALID_INDEX)
1602 {
1603 const int index = model.getIndex(pmt);
1604
1605 int N = 0;
1606
1607 if (model.parameters[pmt].QE .isFree()) { QE = index + N; ++N; }
1608 if (model.parameters[pmt].TTS.isFree()) { TTS = index + N; ++N; }
1609 if (model.parameters[pmt].t0 .isFree()) { t0 = index + N; ++N; }
1610 if (model.parameters[pmt].bg .isFree()) { bg = index + N; ++N; }
1611 }
1612
1613 int QE;
1614 int TTS;
1615 int t0;
1616 int bg;
1617 };
1618
1619
1621
1622 buffer_type buffer;
1623
1624 for (data_type::const_iterator ix = data.begin(); ix != data.end(); ++ix) {
1625
1626 const pair_type& pair = ix->first;
1627
1628 if (value.parameters[pair.first ].status &&
1629 value.parameters[pair.second].status) {
1630
1631 const real_type& real = value.getReal(pair);
1632
1633 const JBell bell(real.t0, real.sigma, real.signal, 0.0, BELL_SHAPE);
1634
1635 const double R1 = value.getValue (real.ct);
1636 const JK40Parameters_t& R1p = value.getGradient(real.ct);
1637
1638 const std::pair<I_t, I_t> PMT(I_t(value, pair.first),
1639 I_t(value, pair.second));
1640
1641 for (const rate_type& iy : ix->second) {
1642
1643 const double R2 = bell.getValue (iy.dt_ns);
1644 const JBell_t& R2p = bell.getGradient(iy.dt_ns);
1645
1646 const double R = real.bc + real.background + R1 * (real.cc + R2);
1647 const double u = (iy.value - R) / iy.error;
1648 const double w = -estimator->getPsi(u) / iy.error;
1649
1650 successor += estimator->getRho(u);
1651
1652 buffer.clear();
1653
1654 if (M.R != INVALID_INDEX) { buffer.push_back({M.R, w * (value.cc() + R2) * R1p.R () * value.R .getDerivative()}); }
1655 if (M.p1 != INVALID_INDEX) { buffer.push_back({M.p1, w * (value.cc() + R2) * R1p.p1() * value.p1.getDerivative()}); }
1656 if (M.p2 != INVALID_INDEX) { buffer.push_back({M.p2, w * (value.cc() + R2) * R1p.p2() * value.p2.getDerivative()}); }
1657 if (M.p3 != INVALID_INDEX) { buffer.push_back({M.p3, w * (value.cc() + R2) * R1p.p3() * value.p3.getDerivative()}); }
1658 if (M.p4 != INVALID_INDEX) { buffer.push_back({M.p4, w * (value.cc() + R2) * R1p.p4() * value.p4.getDerivative()}); }
1659 if (M.cc != INVALID_INDEX) { buffer.push_back({M.cc, w * R1 * R1p.cc() * value.cc.getDerivative()}); }
1660 if (M.bc != INVALID_INDEX) { buffer.push_back({M.bc, w * R1p.bc() * value.bc.getDerivative()}); }
1661
1662 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()}); }
1663 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()}); }
1664 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}); }
1665 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}); }
1666 if (PMT.first .t0 != INVALID_INDEX) { buffer.push_back({PMT.first .t0, w * R1 * R2p.mean * value.parameters[pair.first ].t0 .getDerivative() * +1.0}); }
1667 if (PMT.second.t0 != INVALID_INDEX) { buffer.push_back({PMT.second.t0, w * R1 * R2p.mean * value.parameters[pair.second].t0 .getDerivative() * -1.0}); }
1668 if (PMT.first .bg != INVALID_INDEX) { buffer.push_back({PMT.first .bg, w * value.parameters[pair.first ].bg .getDerivative()}); }
1669 if (PMT.second.bg != INVALID_INDEX) { buffer.push_back({PMT.second.bg, w * value.parameters[pair.second].bg .getDerivative()}); }
1670
1671 for (buffer_type::const_iterator row = buffer.begin(); row != buffer.end(); ++row) {
1672
1673 Y[row->first] += row->second;
1674
1675 V[row->first][row->first] += row->second * row->second;
1676
1677 for (buffer_type::const_iterator col = buffer.begin(); col != row; ++col) {
1678 V[row->first][col->first] += row->second * col->second;
1679 V[col->first][row->first] = V[row->first][col->first];
1680 }
1681 }
1682 }
1683 }
1684 }
1685 }
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:897
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 1693 of file JFitK40.hh.

1694 {
1695 using namespace std;
1696
1697 error.reset();
1698
1699 evaluate(data);
1700
1701 try {
1702 V.invert();
1703 }
1704 catch (const exception& error) {}
1705
1706#define SQRT(X) (X >= 0.0 ? sqrt(X) : std::numeric_limits<double>::max())
1707
1708 size_t i = 0;
1709
1710 if (value.R .isFree()) { error.R = SQRT(V(i,i)); ++i; }
1711 if (value.p1.isFree()) { error.p1 = SQRT(V(i,i)); ++i; }
1712 if (value.p2.isFree()) { error.p2 = SQRT(V(i,i)); ++i; }
1713 if (value.p3.isFree()) { error.p3 = SQRT(V(i,i)); ++i; }
1714 if (value.p4.isFree()) { error.p4 = SQRT(V(i,i)); ++i; }
1715 if (value.cc.isFree()) { error.cc = SQRT(V(i,i)); ++i; }
1716 if (value.bc.isFree()) { error.bc = SQRT(V(i,i)); ++i; }
1717
1718 for (int pmt = 0; pmt != NUMBER_OF_PMTS; ++pmt) {
1719 if (value.parameters[pmt].QE .isFree()) { error.parameters[pmt].QE = SQRT(V(i,i)); ++i; }
1720 if (value.parameters[pmt].TTS.isFree()) { error.parameters[pmt].TTS = SQRT(V(i,i)); ++i; }
1721 if (value.parameters[pmt].t0 .isFree()) { error.parameters[pmt].t0 = SQRT(V(i,i)); ++i; }
1722 if (value.parameters[pmt].bg .isFree()) { error.parameters[pmt].bg = SQRT(V(i,i)); ++i; }
1723 }
1724
1725#undef SQRT
1726 }
#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 1532 of file JFitK40.hh.

◆ EPSILON

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

maximal distance to minimum.

Definition at line 1533 of file JFitK40.hh.

◆ LAMBDA_MIN

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

minimal value control parameter

Definition at line 1534 of file JFitK40.hh.

◆ LAMBDA_MAX

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

maximal value control parameter

Definition at line 1535 of file JFitK40.hh.

◆ LAMBDA_UP

double JCALIBRATE::JFit::LAMBDA_UP = 10.0
staticconstexpr

multiplication factor control parameter

Definition at line 1536 of file JFitK40.hh.

◆ LAMBDA_DOWN

double JCALIBRATE::JFit::LAMBDA_DOWN = 10.0
staticconstexpr

multiplication factor control parameter

Definition at line 1537 of file JFitK40.hh.

◆ PIVOT

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

minimal value diagonal element of matrix

Definition at line 1538 of file JFitK40.hh.

◆ debug

int JCALIBRATE::JFit::debug

Definition at line 1540 of file JFitK40.hh.

◆ estimator

estimator_type JCALIBRATE::JFit::estimator

M-Estimator function.

Definition at line 1541 of file JFitK40.hh.

◆ lambda

double JCALIBRATE::JFit::lambda

Definition at line 1543 of file JFitK40.hh.

◆ value

JModel JCALIBRATE::JFit::value

Definition at line 1544 of file JFitK40.hh.

◆ error

JModel_t JCALIBRATE::JFit::error

Definition at line 1545 of file JFitK40.hh.

◆ numberOfIterations

int JCALIBRATE::JFit::numberOfIterations

Definition at line 1546 of file JFitK40.hh.

◆ V

JMATH::JMatrixNS JCALIBRATE::JFit::V

Definition at line 1547 of file JFitK40.hh.

◆ Y

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

Definition at line 1729 of file JFitK40.hh.

◆ successor

double JCALIBRATE::JFit::successor
private

Definition at line 1730 of file JFitK40.hh.

◆ previous

JModel JCALIBRATE::JFit::previous
private

Definition at line 1731 of file JFitK40.hh.

◆ h

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

Definition at line 1732 of file JFitK40.hh.


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