19#include "analysis/OrcaKinFit/ParameterScanner.h"
21#include "analysis/OrcaKinFit/BaseFitter.h"
22#include "analysis/OrcaKinFit/NewFitterGSL.h"
23#include "analysis/OrcaKinFit/BaseFitObject.h"
24#include "analysis/OrcaKinFit/BaseHardConstraint.h"
25#include <framework/logging/Logger.h>
29#include <TMultiGraph.h>
32#include <gsl/gsl_vector.h>
46 namespace OrcaKinFit {
48 ParameterScanner::ParameterScanner(BaseFitter& fitter_)
52 void ParameterScanner::doScan(
int xglobal,
61 const char* titleprefix,
65 NewFitterGSL* newfitter =
dynamic_cast<NewFitterGSL*
>(&fitter);
67 FitObjectContainer* fitobjects = fitter.getFitObjects();
68 if (fitobjects == 0)
return;
69 ConstraintContainer* constraints = fitter.getConstraints();
70 if (constraints == 0)
return;
78 for (FitObjectIterator i = fitobjects->begin(); i != fitobjects->end(); ++i) {
79 BaseFitObject* fo = *i;
81 for (
int ilocal = 0; ilocal < fo->getNPar(); ++ilocal) {
82 int iglobal = fo->getGlobalParNum(ilocal);
83 if (iglobal >= idim) idim = iglobal + 1;
84 if (iglobal == xglobal) xname = fo->getParamName(ilocal);
85 if (iglobal == yglobal) yname = fo->getParamName(ilocal);
88 for (ConstraintIterator i = constraints->begin(); i != constraints->end(); ++i) {
89 BaseHardConstraint* c = *i;
91 int iglobal = c->getGlobalNum();
92 if (iglobal >= idim) idim = iglobal + 1;
95 if (xglobal >= idim)
return;
96 if (yglobal >= idim)
return;
98 double* parsave =
new double [idim];
99 double* par =
new double [idim];
102 for (FitObjectIterator i = fitobjects->begin(); i != fitobjects->end(); ++i) {
103 BaseFitObject* fo = *i;
105 for (
int ilocal = 0; ilocal < fo->getNPar(); ++ilocal) {
106 int iglobal = fo->getGlobalParNum(ilocal);
107 assert(iglobal >= 0 && iglobal < idim);
108 parsave[iglobal] = fo->getParam(ilocal);
114 TString idpostfix(
"");
115 idpostfix += xglobal;
117 idpostfix += yglobal;
119 TString titlepostfix(
" vs ");
120 titlepostfix += xname;
121 titlepostfix +=
" and ";
122 titlepostfix += yname;
124 titlepostfix += xname;
126 titlepostfix += yname;
130 TString id(idprefix);
134 TString title(titleprefix);
136 title += titlepostfix;
138 TH2F* hchi2 =
new TH2F(
id, title, nx, xstart, xstop, ny, ystart, ystop);
139 B2INFO(
"Booking Histo '" <<
id <<
"': '" << title <<
"'");
142 TH2F* hlog2alpha = 0;
153 title += titlepostfix;
155 halpha =
new TH2F(
id, title, nx, xstart, xstop, ny, ystart, ystop);
156 B2INFO(
"Booking Histo '" <<
id <<
"': '" << title <<
"'");
163 title +=
"log_{2} (#alpha) ";
164 title += titlepostfix;
166 hlog2alpha =
new TH2F(
id, title, nx, xstart, xstop, ny, ystart, ystop);
167 B2INFO(
"Booking Histo '" <<
id <<
"': '" << title <<
"'");
175 title += titlepostfix;
177 hmu =
new TH2F(
id, title, nx, xstart, xstop, ny, ystart, ystop);
178 B2INFO(
"Booking Histo '" <<
id <<
"': '" << title <<
"'");
185 title +=
"Merit function #phi_{1} ";
186 title += titlepostfix;
188 hphi1 =
new TH2F(
id, title, nx, xstart, xstop, ny, ystart, ystop);
189 B2INFO(
"Booking Histo '" <<
id <<
"': '" << title <<
"'");
194 TMultiGraph* mgstepsfull = 0;
195 TMultiGraph* mgsteps = 0;
205 title +=
"Full Steps ";
206 title += titlepostfix;
208 mgstepsfull =
new TMultiGraph(
id, title);
209 B2INFO(
"Booking Multigraph '" <<
id <<
"': '" << title <<
"'");
217 title += titlepostfix;
219 mgsteps =
new TMultiGraph(
id, title);
220 B2INFO(
"Booking Multigraph '" <<
id <<
"': '" << title <<
"'");
227 title +=
"Step Start Points ";
228 title += titlepostfix;
230 gstep0 =
new TGraph(nx * ny);
231 gstep0 ->SetName(
id);
232 gstep0 ->SetTitle(title);
233 gstep0->SetMarkerStyle(20);
234 gstep0->SetMarkerColor(kBlack);
235 gstep0->SetMarkerSize(0.5);
238 id +=
"stependfull_";
242 title +=
"Full Step End Points ";
243 title += titlepostfix;
245 gstep1 =
new TGraph(nx * ny);
246 gstep1 ->SetName(
id);
247 gstep1 ->SetTitle(title);
248 gstep1->SetMarkerStyle(20);
249 gstep1->SetMarkerColor(kRed);
250 gstep1->SetMarkerSize(0.5);
257 title +=
"Step End Points ";
258 title += titlepostfix;
260 gstep2 =
new TGraph(nx * ny);
261 gstep2 ->SetName(
id);
262 gstep2 ->SetTitle(title);
263 gstep2->SetMarkerStyle(21);
264 gstep2->SetMarkerColor(kGreen);
265 gstep2->SetMarkerSize(0.5);
270 unsigned int ncon = constraints->size();
271 if (ncon > NCONMAX) ncon = NCONMAX;
273 TH2F* hlambda[NCONMAX];
274 for (
unsigned int icon = 0; icon < ncon; ++icon) {
277 BaseConstraint* c = (*constraints)[icon];
286 title +=
"Constraint ";
289 title += c->getName();
290 title += titlepostfix;
292 hcon[icon] =
new TH2F(
id, title, nx, xstart, xstop, ny, ystart, ystop);
293 B2INFO(
"Booking Histo '" <<
id <<
"': '" << title <<
"'");
306 title += c->getName();
307 title += titlepostfix;
309 hlambda[icon] =
new TH2F(
id, title, nx, xstart, xstop, ny, ystart, ystop);
310 B2INFO(
"Booking Histo '" <<
id <<
"': '" << title <<
"'");
321 for (
int ix = 1; ix <= nx; ++ix) {
322 double x = (ix - 0.5) * (xstop - xstart) / nx + xstart;
323 for (
int iy = 1; iy <= ny; ++iy) {
324 double y = (iy - 0.5) * (ystop - ystart) / ny + ystart;
327 for (
int i = 0; i < idim; ++i) par[i] = parsave[i];
330 for (FitObjectIterator i = fitobjects->begin(); i != fitobjects->end(); ++i) {
331 BaseFitObject* fo = *i;
333 fo->updateParams(par, idim);
339 for (FitObjectIterator i = fitobjects->begin(); i != fitobjects->end(); ++i) {
340 BaseFitObject* fo = *i;
342 chi2 += fo->getChi2();
348 newfitter->fillx(newfitter->x);
349 newfitter->assembleConstDer(newfitter->M);
350 newfitter->determineLambdas(newfitter->x, newfitter->M, newfitter->x, newfitter->W, newfitter->v1);
354 hchi2->SetBinContent(ix, iy, chi2);
355 for (
unsigned int icon = 0; icon < ncon; ++icon) {
356 BaseHardConstraint* c = (*constraints)[icon];
357 TH2F* h = hcon[icon];
358 if (c && h) h->SetBinContent(ix, iy, c->getValue());
359 if (c) B2INFO(
"x=" << x <<
", y=" << y <<
": Constraint " << c->getName() <<
": " << c->getValue())
361 if (c && h && newfitter) {
362 int kglobal = c->getGlobalNum();
363 h->SetBinContent(ix, iy, gsl_vector_get(newfitter->x, kglobal));;
369 newfitter->setDebug((ix == 1) ? 4 : 0);
371 double xval[2], yval[2];
374 gstep0->SetPoint((ix - 1)*ny + (iy - 1), xval[0], yval[0]);
375 newfitter->fillx(newfitter->x);
376 newfitter->fillperr(newfitter->perr);
378 newfitter->assembleConstDer(newfitter->M);
379 newfitter->determineLambdas(newfitter->x, newfitter->M, newfitter->x, newfitter->W, newfitter->v1);
381 double phi1 = newfitter->meritFunction(mumerit, newfitter->x, newfitter->perr);
382 if (hphi1) hphi1->SetBinContent(ix, iy, phi1);
384 newfitter->calcNewtonDx(newfitter->dx, newfitter->dxscal, newfitter->x,
385 newfitter->perr, newfitter->M, newfitter->Mscal,
386 newfitter->y, newfitter->yscal, newfitter->W, newfitter->W2,
387 newfitter->permW, newfitter->v1);
388 newfitter->add(newfitter->xnew, newfitter->x, 1, newfitter->dx);
390 xval[1] = gsl_vector_get(newfitter->xnew, xglobal);
391 yval[1] = gsl_vector_get(newfitter->xnew, yglobal);
393 B2INFO(
"ParameterScanner::doScan: full step from (" << xval[0] <<
", " << yval[0]
394 <<
") -> (" << xval[1] <<
", " << yval[1] <<
")");
397 gstep1->SetPoint((ix - 1)*ny + (iy - 1), xval[1], yval[1]);
399 TGraph* g =
new TGraph(2, xval, yval);
400 g->SetLineColor(kRed);
402 if (xval[1] > xstart - 10 * (xstop - xstart) &&
403 xval[1] < xstop + 10 * (xstop - xstart) &&
404 yval[1] > ystart - 10 * (ystop - ystart) &&
405 yval[1] < ystop + 10 * (ystop - ystart)) {
407 mgstepsfull->Add(g,
"L");
408 B2INFO(
" -> added\n");
414 newfitter->calcLimitedDx(alpha, mu, newfitter->xnew, imode,
415 newfitter->x, newfitter->v2, newfitter->dx, newfitter->dxscal,
416 newfitter->perr, newfitter->M, newfitter->Mscal,
417 newfitter->W, newfitter->v1);
419 xval[1] = gsl_vector_get(newfitter->xnew, xglobal);
420 yval[1] = gsl_vector_get(newfitter->xnew, yglobal);
422 B2INFO(
" limited step from (" << xval[0] <<
", " << yval[0]
423 <<
") -> (" << xval[1] <<
", " << yval[1] <<
"), alpha=" << alpha);
425 gstep2->SetPoint((ix - 1)*ny + (iy - 1), xval[1], yval[1]);
427 g =
new TGraph(2, xval, yval);
428 g->SetLineColor(kGreen);
430 if (xval[1] > xstart - 10 * (xstop - xstart) &&
431 xval[1] < xstop + 10 * (xstop - xstart) &&
432 yval[1] > ystart - 10 * (ystop - ystart) &&
433 yval[1] < ystop + 10 * (ystop - ystart)) {
435 mgsteps->Add(g,
"L");
436 B2INFO(
" -> added\n");
438 if (halpha) halpha->SetBinContent(ix, iy, alpha);
439 if (hlog2alpha) hlog2alpha->SetBinContent(ix, iy, std::log(alpha) / std::log(2.));
440 if (hmu) hmu->SetBinContent(ix, iy, mu);
450 for (
unsigned int icon = 0; icon < ncon; ++icon) {
451 if (hcon[icon]) hcon[icon]->Write();
452 if (hlambda[icon]) hcon[icon]->Write();
455 for (FitObjectIterator i = fitobjects->begin(); i != fitobjects->end(); ++i) {
456 BaseFitObject* fo = *i;
458 fo->updateParams(parsave, idim);
461 if (mgstepsfull) mgstepsfull->Write();
462 if (mgsteps) mgsteps->Write();
463 if (gstep0) gstep0->Write();
464 if (gstep1) gstep1->Write();
465 if (gstep2) gstep2->Write();
466 if (halpha) halpha->Write();
467 if (hlog2alpha) hlog2alpha->Write();
468 if (hmu) hmu->Write();
469 if (hphi1) hphi1->Write();
Abstract base class for different kinds of events.