50 using std::resetiosflags;
51 using std::setiosflags;
68 : _base_constrainer_args (defs)
211 bool solve_linear_system (
const Matrix&
H,
238 int nconstraints = H.num_row();
239 int nbadvars = Y.num_row();
242 Matrix A (nconstraints+nbadvars, nconstraints+nbadvars);
245 A.sub (nconstraints+1, nconstraints+1, Y);
246 A.sub (1, nconstraints+1, By.T());
247 A.sub (nconstraints+1, 1, By);
259 Ai = A.inverse (ierr);
262 for (
int i=1;
i<=nconstraints;
i++) {
264 for (
int j=1; j<=nconstraints; j++) {
275 if (!allzero)
return false;
284 W = Ai.sub (1, nconstraints, 1, nconstraints);
286 U = Ai.sub (nconstraints+1, nconstraints+nbadvars,
287 nconstraints+1, nconstraints+nbadvars);
288 V = Ai.sub (nconstraints+1, nconstraints+nbadvars, 1, nconstraints);
289 d = xx.sub (nconstraints+1, nconstraints+nbadvars);
292 alpha = xx.sub (1, nconstraints);
364 int nvars = x.num_row();
365 assert (nvars == G_i.num_col());
366 assert (nvars == xm.num_row());
368 int nbadvars = y.num_row();
369 assert (nbadvars == Y.num_col());
370 assert (nbadvars == ym.num_row());
377 G = G_i.inverse (ierr);
381 int nconstraints = constraint_calculator.
nconstraints ();
385 Matrix Bx (nvars, nconstraints);
386 Matrix By (nbadvars, nconstraints);
398 double constraint_sum_last = -1000;
399 double chisq_last = -1000;
400 bool near_convergence =
false;
401 double last_step_cutsize = 1;
409 Matrix E (nvars, nconstraints);
410 Matrix W (nconstraints, nconstraints);
412 Matrix V (nbadvars, nconstraints);
426 if (!solve_linear_system (H, Y, By, r,
427 alpha, d1, W, U, V)) {
435 double chisq = -
scalar (r * alpha);
446 double this_step_cutsize = 1;
447 double constraint_sum = -1;
460 ((constraint_sum = norm_infinity (F))
463 constraint_sum > constraint_sum_last))
468 cout <<
"(" << chisq <<
" " << chisq_last <<
") ";
481 cout <<
" directed step ";
487 solve_linear_system (H, Y, save_By, save_negF,
488 beta, delta, W, U, V);
500 (constraint_sum = norm_infinity (F)) > 0 &&
501 (constraint_sum < constraint_sum_last)) {
505 chisq = chisq_last -
scalar ((-save_negF + r*2) * beta);
517 psi_cut =
scalar ((save_negF - r) * alpha);
532 if (ncut == 1 && last_step_cutsize < 1) {
533 this_cutsize = 2 * last_step_cutsize;
539 this_step_cutsize *= this_cutsize;
549 double cutleft = 1 - this_cutsize;
550 c1 = c1 * this_cutsize + c * cutleft;
551 d1 = d1 * this_cutsize + d * cutleft;
554 if (chisq_last >= 0) {
555 chisq = this_cutsize*this_cutsize * chisq +
556 cutleft*cutleft * chisq_last +
557 2*this_cutsize*cutleft * psi_cut;
558 psi_cut = this_cutsize * psi_cut + cutleft * chisq_last;
565 cout << constraint_sum <<
" cut " << ncut <<
" size " 566 << setiosflags (ios_base::scientific)
567 << this_cutsize <<
" tot size " << this_step_cutsize
568 << resetiosflags (ios_base::scientific)
569 <<
" " << chisq <<
"\n";
581 last_step_cutsize = this_step_cutsize;
590 cout << chisq <<
" " << chisq_b
591 <<
"lost precision?\n";
598 cout << chisq <<
" ";
600 cout << chisq_b <<
" ";
603 double z2 =
abs (chisq - chisq_last);
606 cout << constraint_sum <<
" " << z2 <<
"\n";
612 constraint_sum_last = constraint_sum;
619 if (near_convergence)
break;
620 near_convergence =
true;
623 near_convergence =
false;
644 for (
int i=1;
i<=nvars;
i++) {
655 for (
int i=1;
i<=nbadvars;
i++) {
const Base_Constrainer_Args & base_constrainer_args() const
Define an abstract interface for getting parameter settings.
double min_tot_cutsize() const
CLHEP::HepVector Column_Vector
Abstract base class for evaluating constraints. Users derive from this class and implement the eval()...
Chisq_Constrainer(const Chisq_Constrainer_Args &args)
double constraint_sum_eps() const
Base class for constrained fitter.
Chisq_Constrainer_Args(const Defaults &defs)
Constructor, creates an instance of Chisq_Constrainer_Args from a Defaults object.
double chisq_diff_eps() const
const Base_Constrainer_Args _base_constrainer_args
Minimize a subject to a set of constraints, based on the SQUAW algorithm.
double fit(Constraint_Calculator &constraint_calculator, const Column_Vector &xm, Column_Vector &x, const Column_Vector &ym, Column_Vector &y, const Matrix &G_i, const Diagonal_Matrix &Y, Column_Vector &pullx, Column_Vector &pully, Matrix &Q, Matrix &R, Matrix &S) override
Abs< T >::type abs(const T &t)
bool call_constraint_fcn(Constraint_Calculator &constraint_calculator, const Column_Vector &x, const Column_Vector &y, Row_Vector &F, Matrix &Bx, Matrix &By) const
Helper function to evaluate constraints. This takes care of checking what the user function returns a...
Hold on to parameters for the Chisq_Constrainer class.
std::ostream & print(std::ostream &s) const override
Print the state of this instance of Chisq_Constrainer.
double chisq_test_eps() const
Hold on to parameters for the Base_Constrainer class.
CLHEP::HepDiagMatrix Diagonal_Matrix
virtual double get_float(std::string name) const =0
virtual bool get_bool(std::string name) const =0
Define an interface for getting parameter settings.
const Chisq_Constrainer_Args _args
virtual int get_int(std::string name) const =0
double _constraint_sum_eps
static uInt32 F(BLOWFISH_CTX *ctx, uInt32 x)
virtual std::ostream & print(std::ostream &s) const
Print out internal state to output stream.
Row-vector class. CLHEP doesn't have a row-vector class, so HitFit uses its own. This is only a simpl...
double scalar(const CLHEP::HepGenMatrix &m)
Return the matrix as a scalar. Raise an assertion if the matris is not .