Jpp  18.2.0-rc.1
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Classes | Public Types | Public Member Functions | Public Attributes | Static Public Attributes | Private Member Functions | Private Attributes | List of all members
JCALIBRATE::JFit Class Reference

Fit. More...

#include <JFitK40.hh>

Classes

struct  result_type
 Result type. More...
 

Public Types

typedef std::shared_ptr
< JMEstimator
estimator_type
 

Public Member Functions

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

Public Attributes

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

Static Public Attributes

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

Private Member Functions

void evaluate (const data_type &data)
 Evaluation of fit. More...
 

Private Attributes

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

Detailed Description

Fit.

Definition at line 1118 of file JFitK40.hh.

Member Typedef Documentation

Definition at line 1129 of file JFitK40.hh.

Constructor & Destructor Documentation

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

Constructor.

Parameters
optionM-estimator
debugdebug

Definition at line 1138 of file JFitK40.hh.

1138  :
1139  debug(debug)
1140  {
1141  using namespace JPP;
1142 
1143  estimator.reset(getMEstimator(option));
1144  }
estimator_type estimator
M-Estimator function.
Definition: JFitK40.hh:1317
JMEstimator * getMEstimator(const int type)
Get M-Estimator.
Definition: JMEstimator.hh:203

Member Function Documentation

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

Fit.

Parameters
datadata
Returns
chi2, NDF

Definition at line 1153 of file JFitK40.hh.

1154  {
1155  using namespace std;
1156  using namespace JPP;
1157 
1158 
1159  value.setIndex();
1160 
1161  const size_t N = value.getN();
1162 
1163  V.resize(N);
1164  Y.resize(N);
1165  h.resize(N);
1166 
1167  int ndf = 0;
1168 
1169  for (data_type::const_iterator ix = data.begin(); ix != data.end(); ++ix) {
1170 
1171  const pair_type& pair = ix->first;
1172 
1173  if (value.parameters[pair.first ].status &&
1174  value.parameters[pair.second].status) {
1175 
1176  ndf += ix->second.size();
1177  }
1178  }
1179 
1180  ndf -= value.getN();
1181 
1182 
1183  lambda = LAMBDA_MIN;
1184 
1185  double precessor = numeric_limits<double>::max();
1186 
1188 
1189  DEBUG("step: " << numberOfIterations << endl);
1190 
1191  evaluate(data);
1192 
1193  DEBUG("lambda: " << FIXED(12,5) << lambda << endl);
1194  DEBUG("chi2: " << FIXED(12,3) << successor << endl);
1195 
1196  if (successor < precessor) {
1197 
1198  if (numberOfIterations != 0) {
1199 
1200  if (fabs(precessor - successor) < EPSILON*fabs(precessor)) {
1201  return { successor / estimator->getRho(1.0), ndf };
1202  }
1203 
1204  if (lambda > LAMBDA_MIN) {
1205  lambda /= LAMBDA_DOWN;
1206  }
1207  }
1208 
1209  precessor = successor;
1210  previous = value;
1211 
1212  } else {
1213 
1214  value = previous;
1215  lambda *= LAMBDA_UP;
1216 
1217  if (lambda > LAMBDA_MAX) {
1218  return { precessor / estimator->getRho(1.0), ndf }; // no improvement found
1219  }
1220 
1221  evaluate(data);
1222  }
1223 
1224  if (debug >= debug_t) {
1225 
1226  size_t row = 0;
1227 
1228  if (value.R .isFree()) { cout << "R " << FIXED(12,5) << Y[row] << endl; ++row; }
1229  if (value.p1.isFree()) { cout << "p1 " << FIXED(12,5) << Y[row] << endl; ++row; }
1230  if (value.p2.isFree()) { cout << "p2 " << FIXED(12,5) << Y[row] << endl; ++row; }
1231  if (value.p3.isFree()) { cout << "p3 " << FIXED(12,5) << Y[row] << endl; ++row; }
1232  if (value.p4.isFree()) { cout << "p4 " << FIXED(12,5) << Y[row] << endl; ++row; }
1233  if (value.bg.isFree()) { cout << "bg " << FIXED(12,3) << Y[row] << endl; ++row; }
1234  if (value.cc.isFree()) { cout << "cc " << FIXED(12,3) << Y[row] << endl; ++row; }
1235 
1236  for (int pmt = 0; pmt != NUMBER_OF_PMTS; ++pmt) {
1237  if (value.parameters[pmt].QE .isFree()) { cout << "PMT[" << setw(2) << pmt << "].QE " << FIXED(12,5) << Y[row] << endl; ++row; }
1238  if (value.parameters[pmt].TTS.isFree()) { cout << "PMT[" << setw(2) << pmt << "].TTS " << FIXED(12,5) << Y[row] << endl; ++row; }
1239  if (value.parameters[pmt].t0 .isFree()) { cout << "PMT[" << setw(2) << pmt << "].t0 " << FIXED(12,5) << Y[row] << endl; ++row; }
1240  }
1241  }
1242 
1243  // force definite positiveness
1244 
1245  for (size_t i = 0; i != N; ++i) {
1246 
1247  if (V(i,i) < PIVOT) {
1248  V(i,i) = PIVOT;
1249  }
1250 
1251  h[i] = 1.0 / sqrt(V(i,i));
1252  }
1253 
1254  // normalisation
1255 
1256  for (size_t i = 0; i != N; ++i) {
1257  for (size_t j = 0; j != i; ++j) {
1258  V(j,i) *= h[i] * h[j];
1259  V(i,j) = V(j,i);
1260  }
1261  }
1262 
1263  for (size_t i = 0; i != N; ++i) {
1264  V(i,i) = 1.0 + lambda;
1265  }
1266 
1267  // solve A x = b
1268 
1269  for (size_t col = 0; col != N; ++col) {
1270  Y[col] *= h[col];
1271  }
1272 
1273  try {
1274  V.solve(Y);
1275  }
1276  catch (const exception& error) {
1277 
1278  ERROR("JGandalf: " << error.what() << endl << V << endl);
1279 
1280  break;
1281  }
1282 
1283  // update value
1284 
1285  const double factor = 2.0;
1286 
1287  size_t row = 0;
1288 
1289  if (value.R .isFree()) { value.R -= factor * h[row] * Y[row]; ++row; }
1290  if (value.p1.isFree()) { value.p1 -= factor * h[row] * Y[row]; ++row; }
1291  if (value.p2.isFree()) { value.p2 -= factor * h[row] * Y[row]; ++row; }
1292  if (value.p3.isFree()) { value.p3 -= factor * h[row] * Y[row]; ++row; }
1293  if (value.p4.isFree()) { value.p4 -= factor * h[row] * Y[row]; ++row; }
1294  if (value.bg.isFree()) { value.bg -= factor * h[row] * Y[row]; ++row; }
1295  if (value.cc.isFree()) { value.cc -= factor * h[row] * Y[row]; ++row; }
1296 
1297  for (int pmt = 0; pmt != NUMBER_OF_PMTS; ++pmt) {
1298  if (value.parameters[pmt].QE .isFree()) { value.parameters[pmt].QE -= factor * h[row] * Y[row]; ++row; }
1299  if (value.parameters[pmt].TTS.isFree()) { value.parameters[pmt].TTS -= factor * h[row] * Y[row]; ++row; }
1300  if (value.parameters[pmt].t0 .isFree()) { value.parameters[pmt].t0 -= factor * h[row] * Y[row]; ++row; }
1301  }
1302  }
1303 
1304  return { precessor / estimator->getRho(1.0), ndf };
1305  }
std::vector< double > h
Definition: JFitK40.hh:1460
debug
Definition: JMessage.hh:29
JParameter_t t0
time offset [ns]
Definition: JFitK40.hh:552
JParameter_t R
maximal coincidence rate [Hz]
Definition: JFitK40.hh:584
void setIndex()
Set index of PMT used for fixed time offset.
Definition: JFitK40.hh:918
Data structure for a pair of indices.
Auxiliary data structure for floating point format specification.
Definition: JManip.hh:446
void resize(const size_t size)
Resize matrix.
Definition: JMatrixND.hh:443
static constexpr double LAMBDA_MAX
maximal value control parameter
Definition: JFitK40.hh:1311
JMATH::JMatrixNS V
Definition: JFitK40.hh:1322
static constexpr double EPSILON
maximal distance to minimum.
Definition: JFitK40.hh:1309
JParameter_t QE
relative quantum efficiency [unit]
Definition: JFitK40.hh:550
JParameter_t p3
3rd order angle dependence coincidence rate
Definition: JFitK40.hh:587
int numberOfIterations
Definition: JFitK40.hh:1321
void evaluate(const data_type &data)
Evaluation of fit.
Definition: JFitK40.hh:1330
estimator_type estimator
M-Estimator function.
Definition: JFitK40.hh:1317
#define ERROR(A)
Definition: JMessage.hh:66
JParameter_t TTS
transition-time spread [ns]
Definition: JFitK40.hh:551
static constexpr double LAMBDA_MIN
minimal value control parameter
Definition: JFitK40.hh:1310
JParameter_t p4
4th order angle dependence coincidence rate
Definition: JFitK40.hh:588
JMATH::JVectorND Y
Definition: JFitK40.hh:1457
size_t getN() const
Get number of fit parameters.
Definition: JFitK40.hh:944
static constexpr int MAXIMUM_ITERATIONS
maximal number of iterations.
Definition: JFitK40.hh:1308
then usage $script< input file >[option[primary[working directory]]] nWhere option can be N
Definition: JMuonPostfit.sh:40
bool isFree() const
Check if parameter is free.
Definition: JFitK40.hh:237
JParameter_t p2
2nd order angle dependence coincidence rate
Definition: JFitK40.hh:586
JParameter_t bg
constant background [Hz]
Definition: JFitK40.hh:589
void solve(JVectorND_t &u)
Get solution of equation A x = b.
Definition: JMatrixNS.hh:308
int j
Definition: JPolint.hh:703
static constexpr double LAMBDA_UP
multiplication factor control parameter
Definition: JFitK40.hh:1312
static const int NUMBER_OF_PMTS
Total number of PMTs in module.
Definition: JDAQ.hh:26
double successor
Definition: JFitK40.hh:1458
JPMTParameters_t parameters[NUMBER_OF_PMTS]
Definition: JFitK40.hh:1105
static constexpr double LAMBDA_DOWN
multiplication factor control parameter
Definition: JFitK40.hh:1313
static constexpr double PIVOT
minimal value diagonal element of matrix
Definition: JFitK40.hh:1314
#define DEBUG(A)
Message macros.
Definition: JMessage.hh:62
JParameter_t p1
1st order angle dependence coincidence rate
Definition: JFitK40.hh:585
JParameter_t cc
fraction of signal correlated background
Definition: JFitK40.hh:590
void JCALIBRATE::JFit::evaluate ( const data_type data)
inlineprivate

Evaluation of fit.

Parameters
datadata

Definition at line 1330 of file JFitK40.hh.

1331  {
1332  using namespace std;
1333  using namespace JPP;
1334 
1335  typedef JModel::real_type real_type;
1336 
1337 
1338  successor = 0.0;
1339 
1340  V.reset();
1341  Y.reset();
1342 
1343 
1344  // model parameter indices
1345 
1346  const struct M_t {
1347  M_t(const JModel& model)
1348  {
1349  R = model.getIndex(&JK40Parameters_t::R);
1350  p1 = model.getIndex(&JK40Parameters_t::p1);
1351  p2 = model.getIndex(&JK40Parameters_t::p2);
1352  p3 = model.getIndex(&JK40Parameters_t::p3);
1353  p4 = model.getIndex(&JK40Parameters_t::p4);
1354  bg = model.getIndex(&JK40Parameters_t::bg);
1355  cc = model.getIndex(&JK40Parameters_t::cc);
1356  }
1357 
1358  int R;
1359  int p1;
1360  int p2;
1361  int p3;
1362  int p4;
1363  int bg;
1364  int cc;
1365 
1366  } M(value);
1367 
1368 
1369  // PMT parameter indices
1370 
1371  struct I_t {
1372  I_t(const JModel& model, const int pmt) :
1373  QE (INVALID_INDEX),
1374  TTS(INVALID_INDEX),
1375  t0 (INVALID_INDEX)
1376  {
1377  const int index = model.getIndex(pmt);
1378 
1379  int N = 0;
1380 
1381  if (model.parameters[pmt].QE .isFree()) { QE = index + N; ++N; }
1382  if (model.parameters[pmt].TTS.isFree()) { TTS = index + N; ++N; }
1383  if (model.parameters[pmt].t0 .isFree()) { t0 = index + N; ++N; }
1384  }
1385 
1386  int QE;
1387  int TTS;
1388  int t0;
1389  };
1390 
1391 
1392  typedef vector< pair<int, double> > buffer_type;
1393 
1394  buffer_type buffer;
1395 
1396  for (data_type::const_iterator ix = data.begin(); ix != data.end(); ++ix) {
1397 
1398  const pair_type& pair = ix->first;
1399 
1400  if (value.parameters[pair.first ].status &&
1401  value.parameters[pair.second].status) {
1402 
1403  const real_type& real = value.getReal(pair);
1404 
1405  const JGauss gauss(real.t0, real.sigma, real.signal);
1406 
1407  const double R1 = value.getValue (real.ct);
1408  const JK40Parameters_t& R1p = value.getGradient(real.ct);
1409 
1410  const std::pair<I_t, I_t> PMT(I_t(value, pair.first),
1411  I_t(value, pair.second));
1412 
1413  for (const rate_type& iy : ix->second) {
1414 
1415  const double R2 = gauss.getValue (iy.dt_ns);
1416  const JGauss& R2p = gauss.getGradient(iy.dt_ns);
1417 
1418  const double R = value.bg() + R1 * (value.cc() + R2);
1419  const double u = (iy.value - R) / iy.error;
1420  const double w = -estimator->getPsi(u) / iy.error;
1421 
1422  successor += estimator->getRho(u);
1423 
1424  buffer.clear();
1425 
1426  if (M.R != INVALID_INDEX) { buffer.push_back({M.R, w * (value.cc() + R2) * R1p.R () * value.R .getDerivative()}); }
1427  if (M.p1 != INVALID_INDEX) { buffer.push_back({M.p1, w * (value.cc() + R2) * R1p.p1() * value.p1.getDerivative()}); }
1428  if (M.p2 != INVALID_INDEX) { buffer.push_back({M.p2, w * (value.cc() + R2) * R1p.p2() * value.p2.getDerivative()}); }
1429  if (M.p3 != INVALID_INDEX) { buffer.push_back({M.p3, w * (value.cc() + R2) * R1p.p3() * value.p3.getDerivative()}); }
1430  if (M.p4 != INVALID_INDEX) { buffer.push_back({M.p4, w * (value.cc() + R2) * R1p.p4() * value.p4.getDerivative()}); }
1431  if (M.bg != INVALID_INDEX) { buffer.push_back({M.bg, w * value.bg.getDerivative()}); }
1432  if (M.cc != INVALID_INDEX) { buffer.push_back({M.cc, w * R1 * R1p.cc() * value.cc.getDerivative()}); }
1433 
1434  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()}); }
1435  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()}); }
1436  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}); }
1437  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}); }
1438  if (PMT.first .t0 != INVALID_INDEX) { buffer.push_back({PMT.first .t0, w * R1 * R2p.mean * value.parameters[pair.first ].t0 .getDerivative() * +1.0}); }
1439  if (PMT.second.t0 != INVALID_INDEX) { buffer.push_back({PMT.second.t0, w * R1 * R2p.mean * value.parameters[pair.second].t0 .getDerivative() * -1.0}); }
1440 
1441  for (buffer_type::const_iterator row = buffer.begin(); row != buffer.end(); ++row) {
1442 
1443  Y[row->first] += row->second;
1444 
1445  V[row->first][row->first] += row->second * row->second;
1446 
1447  for (buffer_type::const_iterator col = buffer.begin(); col != row; ++col) {
1448  V[row->first][col->first] += row->second * col->second;
1449  V[col->first][row->first] = V[row->first][col->first];
1450  }
1451  }
1452  }
1453  }
1454  }
1455  }
Data structure for measured coincidence rate of pair of PMTs.
Definition: JFitK40.hh:62
data_type w[N+1][M+1]
Definition: JPolint.hh:778
const real_type & getReal(const pair_type &pair) const
Get derived parameters.
Definition: JFitK40.hh:1015
JParameter_t t0
time offset [ns]
Definition: JFitK40.hh:552
TPaveText * p1
JParameter_t R
maximal coincidence rate [Hz]
Definition: JFitK40.hh:584
static const int INVALID_INDEX
invalid index
Definition: JFitK40.hh:56
const JK40Parameters_t & getGradient(const double ct) const
Get gradient.
Definition: JFitK40.hh:715
static const JPBS_t PMT(3, 4, 2, 3)
PBS of photo-multiplier tube (PMT)
double getDerivative() const
Get derivative of value.
Definition: JFitK40.hh:331
Data structure for a pair of indices.
void reset()
Reset.
Definition: JVectorND.hh:45
#define R1(x)
int getIndex() const
Get index of PMT used for fixed time offset.
Definition: JFitK40.hh:909
JMatrixND & reset()
Set matrix to the null matrix.
Definition: JMatrixND.hh:456
JMATH::JMatrixNS V
Definition: JFitK40.hh:1322
JParameter_t QE
relative quantum efficiency [unit]
Definition: JFitK40.hh:550
JParameter_t p3
3rd order angle dependence coincidence rate
Definition: JFitK40.hh:587
estimator_type estimator
M-Estimator function.
Definition: JFitK40.hh:1317
JParameter_t TTS
transition-time spread [ns]
Definition: JFitK40.hh:551
JParameter_t p4
4th order angle dependence coincidence rate
Definition: JFitK40.hh:588
p2
Definition: module-Z:fit.sh:74
JMATH::JVectorND Y
Definition: JFitK40.hh:1457
then JCookie sh JDataQuality D $DETECTOR_ID R
Definition: JDataQuality.sh:41
then usage $script< input file >[option[primary[working directory]]] nWhere option can be N
Definition: JMuonPostfit.sh:40
bool isFree() const
Check if parameter is free.
Definition: JFitK40.hh:237
JParameter_t p2
2nd order angle dependence coincidence rate
Definition: JFitK40.hh:586
Auxiliary data structure for derived quantities of a given PMT pair.
Definition: JFitK40.hh:756
then set_variable NUMBER_OF_TESTS else set_variable NUMBER_OF_TESTS fi function gauss()
JParameter_t bg
constant background [Hz]
Definition: JFitK40.hh:589
double getValue(const pair_type &pair, const double dt_ns) const
Get K40 coincidence rate.
Definition: JFitK40.hh:1040
Fit parameters for two-fold coincidence rate due to K40.
Definition: JFitK40.hh:559
double u[N+1]
Definition: JPolint.hh:776
Fit model.
Definition: JFitK40.hh:743
double successor
Definition: JFitK40.hh:1458
JPMTParameters_t parameters[NUMBER_OF_PMTS]
Definition: JFitK40.hh:1105
p3
Definition: module-Z:fit.sh:74
JParameter_t p1
1st order angle dependence coincidence rate
Definition: JFitK40.hh:585
JParameter_t cc
fraction of signal correlated background
Definition: JFitK40.hh:590

Member Data Documentation

constexpr int JCALIBRATE::JFit::MAXIMUM_ITERATIONS = 10000
static

maximal number of iterations.

Definition at line 1308 of file JFitK40.hh.

constexpr double JCALIBRATE::JFit::EPSILON = 1.0e-4
static

maximal distance to minimum.

Definition at line 1309 of file JFitK40.hh.

constexpr double JCALIBRATE::JFit::LAMBDA_MIN = 0.01
static

minimal value control parameter

Definition at line 1310 of file JFitK40.hh.

constexpr double JCALIBRATE::JFit::LAMBDA_MAX = 100.0
static

maximal value control parameter

Definition at line 1311 of file JFitK40.hh.

constexpr double JCALIBRATE::JFit::LAMBDA_UP = 10.0
static

multiplication factor control parameter

Definition at line 1312 of file JFitK40.hh.

constexpr double JCALIBRATE::JFit::LAMBDA_DOWN = 10.0
static

multiplication factor control parameter

Definition at line 1313 of file JFitK40.hh.

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

minimal value diagonal element of matrix

Definition at line 1314 of file JFitK40.hh.

int JCALIBRATE::JFit::debug

Definition at line 1316 of file JFitK40.hh.

estimator_type JCALIBRATE::JFit::estimator

M-Estimator function.

Definition at line 1317 of file JFitK40.hh.

double JCALIBRATE::JFit::lambda

Definition at line 1319 of file JFitK40.hh.

JModel JCALIBRATE::JFit::value

Definition at line 1320 of file JFitK40.hh.

int JCALIBRATE::JFit::numberOfIterations

Definition at line 1321 of file JFitK40.hh.

JMATH::JMatrixNS JCALIBRATE::JFit::V

Definition at line 1322 of file JFitK40.hh.

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

Definition at line 1457 of file JFitK40.hh.

double JCALIBRATE::JFit::successor
private

Definition at line 1458 of file JFitK40.hh.

JModel JCALIBRATE::JFit::previous
private

Definition at line 1459 of file JFitK40.hh.

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

Definition at line 1460 of file JFitK40.hh.


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