19#include "analysis/OrcaKinFit/RootTracer.h"
20#include "analysis/OrcaKinFit/BaseFitter.h"
21#include "analysis/OrcaKinFit/BaseFitObject.h"
22#include "analysis/OrcaKinFit/BaseHardConstraint.h"
23#include "analysis/OrcaKinFit/BaseSoftConstraint.h"
24#include <framework/logging/Logger.h>
37 namespace OrcaKinFit {
39 RootTracer::RootTracer(
const char* filename,
const char* option)
40 : file(0), tree(0), eventtree(0),
41 istep(0), isubstep(0),
42 eventnumber(0), stepnumber(0), substepnumber(0), chi2(0)
44 file =
new TFile(filename, option);
45 tree =
new TTree(
"trace",
"Fit Tracing");
51 RootTracer::~RootTracer()
58 void RootTracer::initialize(BaseFitter& fitter)
60 B2INFO(
"=============== Starting fit ======================\n");
62 printFitObjects(fitter);
63 printConstraints(fitter);
70 TString name(
"event");
71 TString title(
"Event ");
74 eventtree =
new TTree(name, title);
75 CreateEventBranches(fitter);
78 BaseTracer::initialize(fitter);
81 void RootTracer::step(BaseFitter& fitter)
84 B2INFO(
"--------------- Step " << istep <<
" --------------------\n");
86 printFitObjects(fitter);
87 printConstraints(fitter);
90 substepnumber = isubstep;
91 chi2 = fitter.getChi2();
92 B2INFO(
"chi2=" << chi2);
93 FillParameterValues(fitter);
99 BaseTracer::step(fitter);
102 void RootTracer::substep(BaseFitter& fitter,
int flag)
104 B2INFO(
"---- Substep " << istep <<
"." << isubstep <<
" ----\n");
106 printFitObjects(fitter);
107 printConstraints(fitter);
110 BaseTracer::substep(fitter, flag);
113 void RootTracer::finish(BaseFitter& fitter)
116 B2INFO(
"=============== Final result ======================\n");
117 printFitObjects(fitter);
118 printConstraints(fitter);
120 B2INFO(
"=============== Finished fit ======================\n");
123 BaseTracer::finish(fitter);
126 void RootTracer::printFitObjects(BaseFitter& fitter)
128 FitObjectContainer* fitobjects = fitter.getFitObjects();
129 if (!fitobjects)
return;
130 B2INFO(
"Fit objects:\n");
131 for (FitObjectIterator i = fitobjects->begin(); i != fitobjects->end(); ++i) {
132 BaseFitObject* fo = *i;
134 B2INFO(fo->getName() <<
": " << *fo <<
", chi2=" << fo->getChi2());
137 void RootTracer::printConstraints(BaseFitter& fitter)
139 ConstraintContainer* constraints = fitter.getConstraints();
140 if (!constraints)
return;
141 B2INFO(
"(Hard) Constraints:\n");
142 for (ConstraintIterator i = constraints->begin(); i != constraints->end(); ++i) {
143 BaseHardConstraint* c = *i;
145 B2INFO(i - constraints->begin() <<
" " << c->getName() <<
": " << c->getValue() <<
"+-" << c->getError());
147 SoftConstraintContainer* softconstraints = fitter.getSoftConstraints();
148 if (!softconstraints)
return;
149 B2INFO(
"Soft Constraints:\n");
150 for (SoftConstraintIterator i = softconstraints->begin(); i != softconstraints->end(); ++i) {
151 BaseSoftConstraint* c = *i;
153 B2INFO(i - softconstraints->begin() <<
" " << c->getName() <<
": " << c->getValue() <<
"+-" << c->getError());
157 void RootTracer::SetBranchAddresses()
159 tree->SetBranchAddress(
"event", &eventnumber);
160 tree->SetBranchAddress(
"step", &stepnumber);
161 tree->SetBranchAddress(
"substep", &substepnumber);
165 void RootTracer::CreateBranches()
168 tree->Branch(
"event", &eventnumber,
"event/I");
169 tree->Branch(
"step", &stepnumber,
"step/I");
170 tree->Branch(
"substep", &substepnumber,
"substep/I");
172 void RootTracer::CreateEventBranches(BaseFitter& fitter)
175 eventtree->Branch(
"step", &stepnumber,
"step/I");
176 eventtree->SetBranchAddress(
"step", &stepnumber);
177 eventtree->Branch(
"substep", &substepnumber,
"substep/I");
178 eventtree->SetBranchAddress(
"substep", &substepnumber);
179 eventtree->Branch(
"chi2", &chi2,
"chi2/D");
180 eventtree->SetBranchAddress(
"chi2", &chi2);
181 FitObjectContainer* fitobjects = fitter.getFitObjects();
182 if (!fitobjects)
return;
183 B2INFO(
"RootTracer: Fit objects:\n");
184 for (FitObjectIterator i = fitobjects->begin(); i != fitobjects->end(); ++i) {
185 BaseFitObject* fo = *i;
187 TString foname = fo->getName();
188 for (
int ilocal = 0; ilocal < fo->getNPar(); ilocal++) {
189 int iglobal = fo->getGlobalParNum(ilocal);
190 if (iglobal >= 0 && iglobal < NPARMAX) {
191 TString parname =
"Par";
194 parname += fo->getParamName(ilocal);
195 B2INFO(
"Parameter " << iglobal <<
" '" << parname <<
"'\n");
196 eventtree->Branch(parname, parvalue + iglobal, parname +
"/D");
197 eventtree->SetBranchAddress(parname, parvalue + iglobal);
202 B2INFO(
"RootTracer: (Hard) Constraints:\n");
203 ConstraintContainer* constraints = fitter.getConstraints();
204 for (ConstraintIterator i = constraints->begin(); i != constraints->end(); ++i) {
205 BaseHardConstraint* c = *i;
207 int iglobal = c->getGlobalNum();
208 if (iglobal >= 0 && iglobal < NPARMAX) {
209 TString cname =
"Const";
210 cname += (i - constraints->begin());
212 cname += c->getName();
213 eventtree->Branch(cname, parvalue + iglobal, cname +
"/D");
214 eventtree->SetBranchAddress(cname, parvalue + iglobal);
215 B2INFO(
"Constraint " << (i - constraints->begin()) <<
" '" << cname);
238 void RootTracer::FillParameterValues(BaseFitter& fitter)
240 FitObjectContainer* fitobjects = fitter.getFitObjects();
241 if (!fitobjects)
return;
242 for (FitObjectIterator i = fitobjects->begin(); i != fitobjects->end(); ++i) {
243 BaseFitObject* fo = *i;
245 B2INFO(
"fo " << fo->getName());
246 for (
int ilocal = 0; ilocal < fo->getNPar(); ilocal++) {
247 int iglobal = fo->getGlobalParNum(ilocal);
248 if (iglobal >= 0 && iglobal < NPARMAX) {
249 parvalue[iglobal] = fo->getParam(ilocal);
250 B2INFO(
"Par " << ilocal <<
": global " << iglobal <<
" = " << parvalue[iglobal]);
254 ConstraintContainer* constraints = fitter.getConstraints();
255 for (ConstraintIterator i = constraints->begin(); i != constraints->end(); ++i) {
256 BaseHardConstraint* c = *i;
258 int iglobal = c->getGlobalNum();
259 if (iglobal >= 0 && iglobal < NPARMAX) {
260 parvalue[iglobal] = c->getValue();
261 B2INFO(
"Const " << c->getName() <<
": global " << iglobal <<
" = " << parvalue[iglobal]);
Abstract base class for different kinds of events.