Jpp test-rotations-old
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 1251 of file JFitK40.hh.

Member Typedef Documentation

◆ estimator_type

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

1271 :
1272 debug(debug)
1273 {
1274 using namespace JPP;
1275
1276 estimator.reset(getMEstimator(option));
1277 }
estimator_type estimator
M-Estimator function.
Definition JFitK40.hh:1473
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 1286 of file JFitK40.hh.

1287 {
1288 using namespace std;
1289 using namespace JPP;
1290
1291
1292 value.setIndex();
1293
1294 const size_t N = value.getN();
1295
1296 V.resize(N);
1297 Y.resize(N);
1298 h.resize(N);
1299
1300 double xmax = numeric_limits<double>::lowest();
1301 double xmin = numeric_limits<double>::max();
1302
1303 int ndf = 0;
1304
1305 for (data_type::const_iterator ix = data.begin(); ix != data.end(); ++ix) {
1306
1307 const pair_type& pair = ix->first;
1308
1309 if (value.parameters[pair.first ].status &&
1310 value.parameters[pair.second].status) {
1311
1312 ndf += ix->second.size();
1313
1314 for (const rate_type& iy : ix->second) {
1315 if (iy.dt_ns > xmax) { xmax = iy.dt_ns; }
1316 if (iy.dt_ns < xmin) { xmin = iy.dt_ns; }
1317 }
1318 }
1319 }
1320
1321 ndf -= value.getN();
1322
1323 if (ndf < 0) {
1324 return { 0.0, ndf };
1325 }
1326
1327 for (int pmt = 0; pmt != NUMBER_OF_PMTS; ++pmt) {
1328 if (value.parameters[pmt].t0.isFree()) {
1329 value.parameters[pmt].t0.setRange(xmin, xmax);
1330 }
1331 }
1332
1333
1335
1336 double precessor = numeric_limits<double>::max();
1337
1339
1340 DEBUG("step: " << numberOfIterations << endl);
1341
1342 evaluate(data);
1343
1344 DEBUG("lambda: " << FIXED(12,5) << lambda << endl);
1345 DEBUG("chi2: " << FIXED(12,3) << successor << endl);
1346
1347 if (successor < precessor) {
1348
1349 if (numberOfIterations != 0) {
1350
1351 if (fabs(precessor - successor) < EPSILON) {
1352
1353 seterr(data);
1354
1355 return { successor / estimator->getRho(1.0), ndf };
1356 }
1357
1358 if (lambda > LAMBDA_MIN) {
1360 }
1361 }
1362
1363 precessor = successor;
1364 previous = value;
1365
1366 } else {
1367
1368 value = previous;
1369 lambda *= LAMBDA_UP;
1370
1371 if (lambda > LAMBDA_MAX) {
1372 break;
1373 }
1374
1375 evaluate(data);
1376 }
1377
1378 if (debug >= debug_t) {
1379
1380 size_t row = 0;
1381
1382 if (value.R .isFree()) { cout << "R " << FIXED(12,5) << Y[row] << endl; ++row; }
1383 if (value.p1.isFree()) { cout << "p1 " << FIXED(12,5) << Y[row] << endl; ++row; }
1384 if (value.p2.isFree()) { cout << "p2 " << FIXED(12,5) << Y[row] << endl; ++row; }
1385 if (value.p3.isFree()) { cout << "p3 " << FIXED(12,5) << Y[row] << endl; ++row; }
1386 if (value.p4.isFree()) { cout << "p4 " << FIXED(12,5) << Y[row] << endl; ++row; }
1387 if (value.cc.isFree()) { cout << "cc " << FIXED(12,3) << Y[row] << endl; ++row; }
1388
1389 for (int pmt = 0; pmt != NUMBER_OF_PMTS; ++pmt) {
1390 if (value.parameters[pmt].QE .isFree()) { cout << "PMT[" << setw(2) << pmt << "].QE " << FIXED(12,5) << Y[row] << endl; ++row; }
1391 if (value.parameters[pmt].TTS.isFree()) { cout << "PMT[" << setw(2) << pmt << "].TTS " << FIXED(12,5) << Y[row] << endl; ++row; }
1392 if (value.parameters[pmt].t0 .isFree()) { cout << "PMT[" << setw(2) << pmt << "].t0 " << FIXED(12,5) << Y[row] << endl; ++row; }
1393 if (value.parameters[pmt].bg .isFree()) { cout << "PMT[" << setw(2) << pmt << "].bg " << FIXED(12,5) << Y[row] << endl; ++row; }
1394 }
1395 }
1396
1397 // force definite positiveness
1398
1399 for (size_t i = 0; i != N; ++i) {
1400
1401 if (V(i,i) < PIVOT) {
1402 V(i,i) = PIVOT;
1403 }
1404
1405 h[i] = 1.0 / sqrt(V(i,i));
1406 }
1407
1408 // normalisation
1409
1410 for (size_t i = 0; i != N; ++i) {
1411 for (size_t j = 0; j != i; ++j) {
1412 V(j,i) *= h[i] * h[j];
1413 V(i,j) = V(j,i);
1414 }
1415 }
1416
1417 for (size_t i = 0; i != N; ++i) {
1418 V(i,i) = 1.0 + lambda;
1419 }
1420
1421 // solve A x = b
1422
1423 for (size_t col = 0; col != N; ++col) {
1424 Y[col] *= h[col];
1425 }
1426
1427 try {
1428 V.solve(Y);
1429 }
1430 catch (const exception& error) {
1431
1432 ERROR("JGandalf: " << error.what() << endl << V << endl);
1433
1434 break;
1435 }
1436
1437 // update value
1438
1439 const double factor = 2.0;
1440
1441 size_t row = 0;
1442
1443 if (value.R .isFree()) { value.R -= factor * h[row] * Y[row]; ++row; }
1444 if (value.p1.isFree()) { value.p1 -= factor * h[row] * Y[row]; ++row; }
1445 if (value.p2.isFree()) { value.p2 -= factor * h[row] * Y[row]; ++row; }
1446 if (value.p3.isFree()) { value.p3 -= factor * h[row] * Y[row]; ++row; }
1447 if (value.p4.isFree()) { value.p4 -= factor * h[row] * Y[row]; ++row; }
1448 if (value.cc.isFree()) { value.cc -= factor * h[row] * Y[row]; ++row; }
1449
1450 for (int pmt = 0; pmt != NUMBER_OF_PMTS; ++pmt) {
1451 if (value.parameters[pmt].QE .isFree()) { value.parameters[pmt].QE -= factor * h[row] * Y[row]; ++row; }
1452 if (value.parameters[pmt].TTS.isFree()) { value.parameters[pmt].TTS -= factor * h[row] * Y[row]; ++row; }
1453 if (value.parameters[pmt].t0 .isFree()) { value.parameters[pmt].t0 -= factor * h[row] * Y[row]; ++row; }
1454 if (value.parameters[pmt].bg .isFree()) { value.parameters[pmt].bg -= factor * h[row] * Y[row]; ++row; }
1455 }
1456 }
1457
1458 seterr(data);
1459
1460 return { precessor / estimator->getRho(1.0), ndf };
1461 }
#define DEBUG(A)
Message macros.
Definition JMessage.hh:62
#define ERROR(A)
Definition JMessage.hh:66
std::vector< double > h
Definition JFitK40.hh:1660
static constexpr double LAMBDA_MIN
minimal value control parameter
Definition JFitK40.hh:1466
static constexpr double LAMBDA_DOWN
multiplication factor control parameter
Definition JFitK40.hh:1469
void seterr(const data_type &data)
Set errors.
Definition JFitK40.hh:1622
static constexpr double LAMBDA_MAX
maximal value control parameter
Definition JFitK40.hh:1467
static constexpr double LAMBDA_UP
multiplication factor control parameter
Definition JFitK40.hh:1468
JMATH::JMatrixNS V
Definition JFitK40.hh:1479
static constexpr double EPSILON
maximal distance to minimum.
Definition JFitK40.hh:1465
void evaluate(const data_type &data)
Evaluation of fit.
Definition JFitK40.hh:1487
static constexpr int MAXIMUM_ITERATIONS
maximal number of iterations.
Definition JFitK40.hh:1464
static constexpr double PIVOT
minimal value diagonal element of matrix
Definition JFitK40.hh:1470
JMATH::JVectorND Y
Definition JFitK40.hh:1657
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 R
maximal coincidence rate [Hz]
Definition JFitK40.hh:664
JParameter_t p1
1st order angle dependence coincidence rate
Definition JFitK40.hh:665
JParameter_t p2
2nd order angle dependence coincidence rate
Definition JFitK40.hh:666
JParameter_t p3
3rd order angle dependence coincidence rate
Definition JFitK40.hh:667
JParameter_t p4
4th order angle dependence coincidence rate
Definition JFitK40.hh:668
JParameter_t cc
fraction of signal correlated background
Definition JFitK40.hh:669
JPMTParameters_t parameters[NUMBER_OF_PMTS]
Definition JFitK40.hh:797
size_t getN() const
Get number of fit parameters.
Definition JFitK40.hh:1084
void setIndex()
Set index of PMT used for fixed time offset.
Definition JFitK40.hh:1058
JParameter_t t0
time offset [ns]
Definition JFitK40.hh:610
JParameter_t TTS
transition-time spread [ns]
Definition JFitK40.hh:609
JParameter_t bg
background [Hz/ns]
Definition JFitK40.hh:611
JParameter_t QE
relative quantum efficiency [unit]
Definition JFitK40.hh:608
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 1487 of file JFitK40.hh.

1488 {
1489 using namespace std;
1490 using namespace JPP;
1491
1492 typedef JModel::real_type real_type;
1493
1494
1495 successor = 0.0;
1496
1497 V.reset();
1498 Y.reset();
1499
1500
1501 // model parameter indices
1502
1503 const struct M_t {
1504 M_t(const JModel& model)
1505 {
1506 R = model.getIndex(&JK40Parameters_t::R);
1507 p1 = model.getIndex(&JK40Parameters_t::p1);
1508 p2 = model.getIndex(&JK40Parameters_t::p2);
1509 p3 = model.getIndex(&JK40Parameters_t::p3);
1510 p4 = model.getIndex(&JK40Parameters_t::p4);
1511 cc = model.getIndex(&JK40Parameters_t::cc);
1512 }
1513
1514 int R;
1515 int p1;
1516 int p2;
1517 int p3;
1518 int p4;
1519 int cc;
1520
1521 } M(value);
1522
1523
1524 // PMT parameter indices
1525
1526 struct I_t {
1527 I_t(const JModel& model, const int pmt) :
1528 QE (INVALID_INDEX),
1529 TTS(INVALID_INDEX),
1530 t0 (INVALID_INDEX),
1531 bg (INVALID_INDEX)
1532 {
1533 const int index = model.getIndex(pmt);
1534
1535 int N = 0;
1536
1537 if (model.parameters[pmt].QE .isFree()) { QE = index + N; ++N; }
1538 if (model.parameters[pmt].TTS.isFree()) { TTS = index + N; ++N; }
1539 if (model.parameters[pmt].t0 .isFree()) { t0 = index + N; ++N; }
1540 if (model.parameters[pmt].bg .isFree()) { bg = index + N; ++N; }
1541 }
1542
1543 int QE;
1544 int TTS;
1545 int t0;
1546 int bg;
1547 };
1548
1549
1551
1552 buffer_type buffer;
1553
1554 for (data_type::const_iterator ix = data.begin(); ix != data.end(); ++ix) {
1555
1556 const pair_type& pair = ix->first;
1557
1558 if (value.parameters[pair.first ].status &&
1559 value.parameters[pair.second].status) {
1560
1561 const real_type& real = value.getReal(pair);
1562
1563 const JGauss gauss(real.t0, real.sigma, real.signal);
1564
1565 const double R1 = value.getValue (real.ct);
1566 const JK40Parameters_t& R1p = value.getGradient(real.ct);
1567
1568 const std::pair<I_t, I_t> PMT(I_t(value, pair.first),
1569 I_t(value, pair.second));
1570
1571 for (const rate_type& iy : ix->second) {
1572
1573 const double R2 = gauss.getValue (iy.dt_ns);
1574 const JGauss& R2p = gauss.getGradient(iy.dt_ns);
1575
1576 const double R = real.background + R1 * (value.cc() + R2);
1577 const double u = (iy.value - R) / iy.error;
1578 const double w = -estimator->getPsi(u) / iy.error;
1579
1580 successor += estimator->getRho(u);
1581
1582 buffer.clear();
1583
1584 if (M.R != INVALID_INDEX) { buffer.push_back({M.R, w * (value.cc() + R2) * R1p.R () * value.R .getDerivative()}); }
1585 if (M.p1 != INVALID_INDEX) { buffer.push_back({M.p1, w * (value.cc() + R2) * R1p.p1() * value.p1.getDerivative()}); }
1586 if (M.p2 != INVALID_INDEX) { buffer.push_back({M.p2, w * (value.cc() + R2) * R1p.p2() * value.p2.getDerivative()}); }
1587 if (M.p3 != INVALID_INDEX) { buffer.push_back({M.p3, w * (value.cc() + R2) * R1p.p3() * value.p3.getDerivative()}); }
1588 if (M.p4 != INVALID_INDEX) { buffer.push_back({M.p4, w * (value.cc() + R2) * R1p.p4() * value.p4.getDerivative()}); }
1589 if (M.cc != INVALID_INDEX) { buffer.push_back({M.cc, w * R1 * R1p.cc() * value.cc.getDerivative()}); }
1590
1591 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()}); }
1592 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()}); }
1593 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}); }
1594 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}); }
1595 if (PMT.first .t0 != INVALID_INDEX) { buffer.push_back({PMT.first .t0, w * R1 * R2p.mean * value.parameters[pair.first ].t0 .getDerivative() * +1.0}); }
1596 if (PMT.second.t0 != INVALID_INDEX) { buffer.push_back({PMT.second.t0, w * R1 * R2p.mean * value.parameters[pair.second].t0 .getDerivative() * -1.0}); }
1597 if (PMT.first .bg != INVALID_INDEX) { buffer.push_back({PMT.first .bg, w * value.parameters[pair.first ].bg .getDerivative()}); }
1598 if (PMT.second.bg != INVALID_INDEX) { buffer.push_back({PMT.second.bg, w * value.parameters[pair.second].bg .getDerivative()}); }
1599
1600 for (buffer_type::const_iterator row = buffer.begin(); row != buffer.end(); ++row) {
1601
1602 Y[row->first] += row->second;
1603
1604 V[row->first][row->first] += row->second * row->second;
1605
1606 for (buffer_type::const_iterator col = buffer.begin(); col != row; ++col) {
1607 V[row->first][col->first] += row->second * col->second;
1608 V[col->first][row->first] = V[row->first][col->first];
1609 }
1610 }
1611 }
1612 }
1613 }
1614 }
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:845
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 1622 of file JFitK40.hh.

1623 {
1624 using namespace std;
1625
1626 error.reset();
1627
1628 evaluate(data);
1629
1630 try {
1631 V.invert();
1632 }
1633 catch (const exception& error) {}
1634
1635#define SQRT(X) (X >= 0.0 ? sqrt(X) : std::numeric_limits<double>::max())
1636
1637 size_t i = 0;
1638
1639 if (value.R .isFree()) { error.R = SQRT(V(i,i)); ++i; }
1640 if (value.p1.isFree()) { error.p1 = SQRT(V(i,i)); ++i; }
1641 if (value.p2.isFree()) { error.p2 = SQRT(V(i,i)); ++i; }
1642 if (value.p3.isFree()) { error.p3 = SQRT(V(i,i)); ++i; }
1643 if (value.p4.isFree()) { error.p4 = SQRT(V(i,i)); ++i; }
1644 if (value.cc.isFree()) { error.cc = SQRT(V(i,i)); ++i; }
1645
1646 for (int pmt = 0; pmt != NUMBER_OF_PMTS; ++pmt) {
1647 if (value.parameters[pmt].QE .isFree()) { error.parameters[pmt].QE = SQRT(V(i,i)); ++i; }
1648 if (value.parameters[pmt].TTS.isFree()) { error.parameters[pmt].TTS = SQRT(V(i,i)); ++i; }
1649 if (value.parameters[pmt].t0 .isFree()) { error.parameters[pmt].t0 = SQRT(V(i,i)); ++i; }
1650 if (value.parameters[pmt].bg .isFree()) { error.parameters[pmt].bg = SQRT(V(i,i)); ++i; }
1651 }
1652
1653#undef SQRT
1654 }
#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 1464 of file JFitK40.hh.

◆ EPSILON

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

maximal distance to minimum.

Definition at line 1465 of file JFitK40.hh.

◆ LAMBDA_MIN

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

minimal value control parameter

Definition at line 1466 of file JFitK40.hh.

◆ LAMBDA_MAX

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

maximal value control parameter

Definition at line 1467 of file JFitK40.hh.

◆ LAMBDA_UP

double JCALIBRATE::JFit::LAMBDA_UP = 10.0
staticconstexpr

multiplication factor control parameter

Definition at line 1468 of file JFitK40.hh.

◆ LAMBDA_DOWN

double JCALIBRATE::JFit::LAMBDA_DOWN = 10.0
staticconstexpr

multiplication factor control parameter

Definition at line 1469 of file JFitK40.hh.

◆ PIVOT

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

minimal value diagonal element of matrix

Definition at line 1470 of file JFitK40.hh.

◆ debug

int JCALIBRATE::JFit::debug

Definition at line 1472 of file JFitK40.hh.

◆ estimator

estimator_type JCALIBRATE::JFit::estimator

M-Estimator function.

Definition at line 1473 of file JFitK40.hh.

◆ lambda

double JCALIBRATE::JFit::lambda

Definition at line 1475 of file JFitK40.hh.

◆ value

JModel JCALIBRATE::JFit::value

Definition at line 1476 of file JFitK40.hh.

◆ error

JModel_t JCALIBRATE::JFit::error

Definition at line 1477 of file JFitK40.hh.

◆ numberOfIterations

int JCALIBRATE::JFit::numberOfIterations

Definition at line 1478 of file JFitK40.hh.

◆ V

JMATH::JMatrixNS JCALIBRATE::JFit::V

Definition at line 1479 of file JFitK40.hh.

◆ Y

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

Definition at line 1657 of file JFitK40.hh.

◆ successor

double JCALIBRATE::JFit::successor
private

Definition at line 1658 of file JFitK40.hh.

◆ previous

JModel JCALIBRATE::JFit::previous
private

Definition at line 1659 of file JFitK40.hh.

◆ h

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

Definition at line 1660 of file JFitK40.hh.


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