Belle II Software development
TextTracer.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * Forked from https://github.com/iLCSoft/MarlinKinfit *
6 * *
7 * Further information about the fit engine and the user interface *
8 * provided in MarlinKinfit can be found at *
9 * https://www.desy.de/~blist/kinfit/doc/html/ *
10 * and in the LCNotes LC-TOOL-2009-001 and LC-TOOL-2009-004 available *
11 * from http://www-flc.desy.de/lcnotes/ *
12 * *
13 * See git log for contributors and copyright holders. *
14 * This file is licensed under LGPL-3.0, see LICENSE.md. *
15 **************************************************************************/
16
17#include "analysis/OrcaKinFit/TextTracer.h"
18#include "analysis/OrcaKinFit/BaseFitter.h"
19#include "analysis/OrcaKinFit/BaseFitObject.h"
20#include "analysis/OrcaKinFit/BaseConstraint.h"
21#include "analysis/OrcaKinFit/BaseHardConstraint.h"
22#include "analysis/OrcaKinFit/BaseSoftConstraint.h"
23#undef NDEBUG
24#include <cassert>
25#include <cstring>
26#include <cmath>
27
28namespace Belle2 {
33 namespace OrcaKinFit {
34
35 TextTracer::TextTracer(std::ostream& os_)
36 : os(os_),
37 istep(0), isubstep(0), chi2fo(0), chi2sc(0), sumhc(0), sumhcscal(0)
38 {}
39
40 TextTracer::~TextTracer() = default;
41
42
43 void TextTracer::initialize(BaseFitter& fitter)
44 {
45 os << "=============== Starting fit ======================\n";
46
47 printFitObjects(fitter);
48 printConstraints(fitter);
49 printTraceValues(fitter);
50 printSums(fitter);
51
52 istep = 1;
53 isubstep = 0;
54
55 BaseTracer::initialize(fitter);
56 }
57
58 void TextTracer::step(BaseFitter& fitter)
59 {
60 isubstep = 1;
61 os << "--------------- Step " << istep << " --------------------\n";
62
63 printFitObjects(fitter);
64 printConstraints(fitter);
65 printTraceValues(fitter);
66 printSums(fitter);
67
68 ++istep;
69 BaseTracer::step(fitter);
70 }
71
72 void TextTracer::substep(BaseFitter& fitter, int flag)
73 {
74 os << "---- Substep " << istep << "." << isubstep << " ----\n";
75
76 printFitObjects(fitter);
77 printConstraints(fitter);
78 printTraceValues(fitter);
79 printSums(fitter);
80
81 ++isubstep;
82 BaseTracer::substep(fitter, flag);
83 }
84
85 void TextTracer::finish(BaseFitter& fitter)
86 {
87
88 os << "=============== Final result ======================\n";
89 printFitObjects(fitter);
90 printConstraints(fitter);
91 printTraceValues(fitter);
92 printSums(fitter);
93
94 os << "=============== Finished fit ======================\n";
95
96
97 BaseTracer::finish(fitter);
98 }
99
100 void TextTracer::printFitObjects(BaseFitter& fitter)
101 {
102 chi2fo = 0;
103 FitObjectContainer* fitobjects = fitter.getFitObjects();
104 if (!fitobjects) return;
105 os << "Fit objects:\n";
106 for (auto fo : *fitobjects) {
107 assert(fo);
108 os << fo->getName() << ": " << *fo << ", chi2=" << fo->getChi2() << std::endl;
109 chi2fo += fo->getChi2();
110 }
111 }
112 void TextTracer::printConstraints(BaseFitter& fitter)
113 {
114 chi2sc = 0;
115 sumhc = 0;
116 sumhcscal = 0;
117 ConstraintContainer* constraints = fitter.getConstraints();
118 if (constraints && constraints->size() > 0) {
119 os << "Hard Constraints:\n";
120 for (auto i = constraints->begin(); i != constraints->end(); ++i) {
121 BaseConstraint* c = *i;
122 assert(c);
123 os << i - constraints->begin() << " " << c->getName() << ": " << c->getValue() << "+-" << c->getError() << std::endl;
124 sumhc += std::fabs(c->getValue());
125 sumhcscal += std::fabs(c->getValue() / c->getError());
126 }
127 }
128 SoftConstraintContainer* softConstraints = fitter.getSoftConstraints();
129 if (softConstraints && softConstraints->size() > 0) {
130 os << "Soft Constraints:\n";
131 for (auto i = softConstraints->begin(); i != softConstraints->end(); ++i) {
132 BaseConstraint* c = *i;
133 auto* sc = dynamic_cast<BaseSoftConstraint*>(c);
134 assert(c);
135 assert(sc);
136 os << i - softConstraints->begin() << " " << c->getName() << ": " << c->getValue() << "+-" << c->getError()
137 << ", chi2=" << sc->getChi2() << std::endl;
138 chi2sc += sc->getChi2();
139 }
140 }
141 }
142
143 void TextTracer::printTraceValues(const BaseFitter& fitter)
144 {
145 for (auto& traceValue : fitter.traceValues) {
146 std::string name = traceValue.first;
147 double value = traceValue.second;
148 os << "Value of " << name << ": " << value << std::endl;;
149 }
150 }
151 void TextTracer::printSums(BaseFitter& fitter)
152 {
153 os << "Total chi2: " << fitter.getChi2()
154 << " = " << chi2fo + chi2sc << " = " << chi2fo << "(fo) + " << chi2sc << "(sc)"
155 << std::endl;
156 os << "Hard constraints: " << sumhc << ", scaled: " << sumhcscal << std::endl;
157 auto i = fitter.traceValues.find("mu");
158 if (i != fitter.traceValues.end()) {
159 double mu = i->second;
160 os << "Contribution to merit function: " << sumhc* mu << ", scaled: " << sumhcscal* mu << std::endl;
161 os << "Merit function: " << chi2fo + chi2sc + sumhc* mu << ", scaled: " << chi2fo + chi2sc + sumhcscal* mu << std::endl;
162 }
163
164 }
165 }// end OrcaKinFit namespace
167} // end Belle2 namespace
Abstract base class for fitting engines of kinematic fits.
Definition: BaseFitter.h:47
Abstract base class for different kinds of events.