Jpp in_tag_pdf_generation
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 1288 of file JFitK40.hh.

Member Typedef Documentation

◆ estimator_type

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

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

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

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

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

◆ EPSILON

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

maximal distance to minimum.

Definition at line 1503 of file JFitK40.hh.

◆ LAMBDA_MIN

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

minimal value control parameter

Definition at line 1504 of file JFitK40.hh.

◆ LAMBDA_MAX

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

maximal value control parameter

Definition at line 1505 of file JFitK40.hh.

◆ LAMBDA_UP

double JCALIBRATE::JFit::LAMBDA_UP = 10.0
staticconstexpr

multiplication factor control parameter

Definition at line 1506 of file JFitK40.hh.

◆ LAMBDA_DOWN

double JCALIBRATE::JFit::LAMBDA_DOWN = 10.0
staticconstexpr

multiplication factor control parameter

Definition at line 1507 of file JFitK40.hh.

◆ PIVOT

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

minimal value diagonal element of matrix

Definition at line 1508 of file JFitK40.hh.

◆ debug

int JCALIBRATE::JFit::debug

Definition at line 1510 of file JFitK40.hh.

◆ estimator

estimator_type JCALIBRATE::JFit::estimator

M-Estimator function.

Definition at line 1511 of file JFitK40.hh.

◆ lambda

double JCALIBRATE::JFit::lambda

Definition at line 1513 of file JFitK40.hh.

◆ value

JModel JCALIBRATE::JFit::value

Definition at line 1514 of file JFitK40.hh.

◆ error

JModel_t JCALIBRATE::JFit::error

Definition at line 1515 of file JFitK40.hh.

◆ numberOfIterations

int JCALIBRATE::JFit::numberOfIterations

Definition at line 1516 of file JFitK40.hh.

◆ V

JMATH::JMatrixNS JCALIBRATE::JFit::V

Definition at line 1517 of file JFitK40.hh.

◆ Y

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

Definition at line 1699 of file JFitK40.hh.

◆ successor

double JCALIBRATE::JFit::successor
private

Definition at line 1700 of file JFitK40.hh.

◆ previous

JModel JCALIBRATE::JFit::previous
private

Definition at line 1701 of file JFitK40.hh.

◆ h

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

Definition at line 1702 of file JFitK40.hh.


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