Pid Framework
Loading...
Searching...
No Matches
FitHelper.h
1#ifndef PID_SRC_FITHELPER_H_
2#define PID_SRC_FITHELPER_H_
3
4#include "Fitter.h"
5#include "ParticleFit.h"
6#include <TCutG.h>
7
8TH2* cutTH2(std::shared_ptr<TH2D> hFull, TCutG* cut) {
9 std::string name = hFull->GetName();
10 name += "_cut";
11 TH2* hCut = (TH2*) hFull->Clone(name.c_str());
12 int nBinsX = hCut->GetNbinsX();
13 int nBinsY = hCut->GetNbinsY();
14 double x, y;
15
16 for (int i = 1; i <= nBinsX; i++) {
17 for (int j = 1; j <= nBinsY; j++) {
18 x = hCut->GetXaxis()->GetBinCenter(i);
19 y = hCut->GetYaxis()->GetBinCenter(j);
20 if (!cut->IsInside(x, y)) {
21 hCut->SetBinContent(i, j, 0.);
22 hCut->SetBinError(i, j, 0.);
23 }
24 }
25 }
26 return hCut;
27}
28
29double asymmGauss(double x, std::array<double, 4> p) {
30 double amp = p[0];
31 double mean = p[1];
32 double sig = p[2];
33 double delta = p[3];
34 if (x > mean) {
35 sig *= 1 + delta;
36 } else {
37 sig *= 1 - delta;
38 }
39 return amp * exp(-0.5 * (x - mean) * (x - mean) / sig / sig);
40}
41
42struct FitParams {
43 std::string name{""};
44 float xmin{0}, xmax{0}, ymin{0}, ymax{0};
45 float chi2_max{0};
46 unsigned int pid;
47 std::vector<std::string> func{"0", "pol9", "pol9"};
48};
49
50Pid::ParticleFit FitParticle(Pid::Fitter& tof, const FitParams& params, const std::shared_ptr<TH2>& hist) {
51
52 std::string name = params.name;
53 std::cout << "\n + " + name + "\n";
54
55 tof.SetChi2Max(params.chi2_max);
56 TF1 fit(("fit_" + name).c_str(), "gaus", params.ymin, params.ymax);
57 fit.SetParNames("p0", "p1", "p2");
58 fit.SetParLimits(0, 0., 4.e6);
59 fit.SetParLimits(1, -.25, 0.03);
60 fit.SetParLimits(2, 0., 0.7);
61
62 TF1 fit_0((name + "_0").c_str(), params.func.at(0).c_str(), params.xmin, params.xmax);
63 TF1 fit_1((name + "_1").c_str(), params.func.at(1).c_str(), params.xmin, params.xmax);
64 TF1 fit_2((name + "_2").c_str(), params.func.at(2).c_str(), params.xmin, params.xmax);
65
66 Pid::ParticleFit particle(params.pid);
67 particle.SetParametrization({fit_0, fit_1, fit_2});
68 particle.SetFitFunction(fit);
69 particle.SetRange(params.xmin, params.xmax);
70 particle.SetIsFitted();
71
72 tof.AddParticle(particle, params.pid);
73 tof.SetHisto2D(hist);
74 tof.SetRangeX(params.xmin, params.xmax);
75 tof.SetRangeY(params.ymin, params.ymax);
76 tof.SetOutputFileName("pionpos.root");
77 tof.Fit();
78 particle = tof.GetParticle(0);
79 tof.Clear();
80
81 return particle;
82}
83
84Pid::ParticleFit FitPionsPos(Pid::Fitter& tof, const std::shared_ptr<TH2D>& hist) {
85
86 FitParams pions;
87 pions.xmin = 0.25;
88 pions.xmax = 10.5;
89 pions.ymin = -2.;
90 pions.ymax = 2.;
91 pions.pid = PidParticles::kPionPos;
92 pions.name = "piplus";
93
94 return FitParticle(tof, pions, hist);
95}
96
97#endif//PID_SRC_FITHELPER_H_
Class to fit 2D histograms.
Definition Fitter.h:22
void Clear()
Definition Fitter.cpp:155
void Fit()
Definition Fitter.cpp:16
Class to store fit resuls for particle specie.
Definition ParticleFit.h:19
Definition FitHelper.h:42