2022-07-05 01:35:54 +08:00
|
|
|
#pragma once
|
|
|
|
|
2022-07-05 19:02:59 +08:00
|
|
|
#ifndef gauss_fit_h
|
|
|
|
#define gauss_fit_h
|
2022-07-05 01:35:54 +08:00
|
|
|
|
2022-07-05 19:02:59 +08:00
|
|
|
#include "GaussNewton.h"
|
2022-07-05 01:35:54 +08:00
|
|
|
#include "LevenbergMarquardt.h"
|
2022-07-05 19:02:59 +08:00
|
|
|
#include "clip.h"
|
|
|
|
#include "utils.h"
|
2022-07-06 23:33:12 +08:00
|
|
|
#include <TCanvas.h>
|
|
|
|
#include <TH1F.h>
|
2022-07-05 01:35:54 +08:00
|
|
|
|
|
|
|
#include <Eigen/Dense>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
double Gaussian(double x, double* p) {
|
2022-07-05 19:02:59 +08:00
|
|
|
return p[0] * std::exp(-(x - p[1]) * (x - p[1]) / (2 * p[2] * p[2]));
|
2022-07-05 01:35:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
double* GaussianJacobian(double x, double* p) {
|
|
|
|
double* resJ = new double[3];
|
|
|
|
resJ[0] = -Gaussian(x, p) / p[0];
|
|
|
|
resJ[1] = -(x - p[1]) * Gaussian(x, p) / (p[2] * p[2]);
|
|
|
|
resJ[2] = -(x - p[1]) * (x - p[1]) * Gaussian(x, p) / (p[2] * p[2] * p[2]);
|
|
|
|
return resJ;
|
|
|
|
}
|
|
|
|
|
|
|
|
class GaussFit {
|
|
|
|
public:
|
2022-07-06 23:53:09 +08:00
|
|
|
GaussFit(){};
|
|
|
|
~GaussFit(){};
|
|
|
|
|
|
|
|
public:
|
|
|
|
double* parma = new double[3];
|
|
|
|
std::vector<Eigen::Vector2d> data;
|
2022-07-05 01:35:54 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
void addData(double x, double y);
|
2022-07-05 19:02:59 +08:00
|
|
|
double* fit(int type_ = 0);
|
2022-07-06 23:33:12 +08:00
|
|
|
double RSquare();
|
|
|
|
int getTotal();
|
|
|
|
void draw(std::string title = "./Figure.png");
|
2022-07-05 01:35:54 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
void GaussFit::addData(double x, double y) { data.push_back(Eigen::Vector2d(x, y)); }
|
|
|
|
|
2022-07-05 19:02:59 +08:00
|
|
|
double* GaussFit::fit(int type_) {
|
2022-07-06 23:33:12 +08:00
|
|
|
double x, y;
|
|
|
|
|
|
|
|
SigmaClip* SC2 = new SigmaClip();
|
|
|
|
data = SC2->clip(data);
|
|
|
|
|
|
|
|
parma[0] = dataMax2D(data);
|
|
|
|
parma[1] = dataAvg2D(data);
|
|
|
|
parma[2] = dataStd2D(data);
|
|
|
|
|
|
|
|
if (DEBUG > 1)
|
|
|
|
for (int i = 0; i < data.size(); i++) {
|
|
|
|
Eigen::Vector2d& point = data.at(i);
|
|
|
|
x = point(0), y = point(1);
|
|
|
|
std::cout << x << " " << y << std::endl;
|
|
|
|
}
|
2022-07-05 19:02:59 +08:00
|
|
|
|
|
|
|
if (type_ == 0) {
|
|
|
|
LevenbergMarquardt LM(3, parma, Gaussian, GaussianJacobian);
|
|
|
|
LM.data = data;
|
|
|
|
parma = LM.solve();
|
|
|
|
} else if (type_ == 1) {
|
|
|
|
LevenbergMarquardt LM(3, parma, Gaussian, GaussianJacobian, type_ = type_);
|
|
|
|
LM.data = data;
|
|
|
|
parma = LM.solve();
|
|
|
|
} else {
|
|
|
|
GaussNewton GN(3, parma, Gaussian, GaussianJacobian);
|
|
|
|
GN.data = data;
|
|
|
|
parma = GN.solve();
|
2022-07-05 01:35:54 +08:00
|
|
|
}
|
|
|
|
|
2022-07-06 23:33:12 +08:00
|
|
|
if (DEBUG) std::cout << parma[0] << " " << parma[1] << ", " << parma[2] << std::endl;
|
|
|
|
if (DEBUG) std::cout << RSquare() << std::endl;
|
|
|
|
|
2022-07-06 23:42:52 +08:00
|
|
|
if (RSquare() < 0.6)
|
2022-07-06 23:33:12 +08:00
|
|
|
for (int i = 0; i < 3; i++) parma[i] = 0;
|
|
|
|
|
2022-07-05 01:35:54 +08:00
|
|
|
return parma;
|
|
|
|
}
|
|
|
|
|
2022-07-06 23:33:12 +08:00
|
|
|
double GaussFit::RSquare() {
|
|
|
|
double x, y, mu;
|
|
|
|
double RSS = 0, TSS = 0;
|
|
|
|
|
|
|
|
std::vector<double> yData;
|
|
|
|
for (int i = 0; i < data.size(); i++) yData.push_back(data.at(i)(1));
|
|
|
|
mu = dataAvg(yData);
|
|
|
|
|
|
|
|
for (int i = 0; i < data.size(); i++) {
|
|
|
|
Eigen::Vector2d& point = data.at(i);
|
|
|
|
x = point(0), y = point(1);
|
|
|
|
RSS += std::pow(y - Gaussian(x, parma), 2);
|
|
|
|
TSS += std::pow(y - mu, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1 - RSS / TSS;
|
|
|
|
}
|
|
|
|
|
|
|
|
int GaussFit::getTotal() {
|
|
|
|
int sum = 0;
|
|
|
|
for (int i = 0; i < data.size(); i++) {
|
|
|
|
Eigen::Vector2d& point = data.at(i);
|
|
|
|
sum += point(1);
|
|
|
|
}
|
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GaussFit::draw(std::string title) {
|
|
|
|
TCanvas* c1 = new TCanvas("Gauss Fit", "Gauss Fit", 0, 0, 1600, 1200);
|
|
|
|
TH1F* h1 = new TH1F("", "Raw Data", CHANNEL_NUMBER, 0, CHANNEL_NUMBER);
|
|
|
|
TH1F* h2 = new TH1F("", "Fit Data", CHANNEL_NUMBER, 0, CHANNEL_NUMBER);
|
|
|
|
|
|
|
|
for (int i = 0; i < data.size(); i++) {
|
|
|
|
Eigen::Vector2d& point = data.at(i);
|
|
|
|
h1->SetBinContent(point(0), point(1));
|
|
|
|
}
|
|
|
|
for (int i = 0; i < CHANNEL_NUMBER; i++) h2->SetBinContent(i, Gaussian(i, parma));
|
|
|
|
|
|
|
|
c1->cd(1);
|
|
|
|
h1->Draw("L");
|
|
|
|
h2->SetLineColor(kRed);
|
|
|
|
h2->Draw("SAME");
|
|
|
|
c1->SaveAs(title.c_str());
|
|
|
|
}
|
|
|
|
|
2022-07-05 01:35:54 +08:00
|
|
|
#endif
|