Initialization
This commit is contained in:
commit
3ba3a9de84
208
.clang-format
Normal file
208
.clang-format
Normal file
@ -0,0 +1,208 @@
|
||||
---
|
||||
Language: Cpp
|
||||
# BasedOnStyle: Google
|
||||
AccessModifierOffset: -4
|
||||
AlignAfterOpenBracket: Align
|
||||
AlignArrayOfStructures: None
|
||||
AlignConsecutiveMacros: None
|
||||
AlignConsecutiveAssignments: None
|
||||
AlignConsecutiveBitFields: None
|
||||
AlignConsecutiveDeclarations: None
|
||||
AlignEscapedNewlines: Left
|
||||
AlignOperands: Align
|
||||
AlignTrailingComments: true
|
||||
AllowAllArgumentsOnNextLine: true
|
||||
AllowAllConstructorInitializersOnNextLine: true
|
||||
AllowAllParametersOfDeclarationOnNextLine: true
|
||||
AllowShortEnumsOnASingleLine: true
|
||||
AllowShortBlocksOnASingleLine: Never
|
||||
AllowShortCaseLabelsOnASingleLine: false
|
||||
AllowShortFunctionsOnASingleLine: All
|
||||
AllowShortLambdasOnASingleLine: All
|
||||
AllowShortIfStatementsOnASingleLine: WithoutElse
|
||||
AllowShortLoopsOnASingleLine: true
|
||||
AlwaysBreakAfterDefinitionReturnType: None
|
||||
AlwaysBreakAfterReturnType: None
|
||||
AlwaysBreakBeforeMultilineStrings: true
|
||||
AlwaysBreakTemplateDeclarations: Yes
|
||||
AttributeMacros:
|
||||
- __capability
|
||||
BinPackArguments: true
|
||||
BinPackParameters: true
|
||||
BraceWrapping:
|
||||
AfterCaseLabel: false
|
||||
AfterClass: false
|
||||
AfterControlStatement: Never
|
||||
AfterEnum: false
|
||||
AfterFunction: false
|
||||
AfterNamespace: false
|
||||
AfterObjCDeclaration: false
|
||||
AfterStruct: false
|
||||
AfterUnion: false
|
||||
AfterExternBlock: false
|
||||
BeforeCatch: false
|
||||
BeforeElse: false
|
||||
BeforeLambdaBody: false
|
||||
BeforeWhile: false
|
||||
IndentBraces: false
|
||||
SplitEmptyFunction: true
|
||||
SplitEmptyRecord: true
|
||||
SplitEmptyNamespace: true
|
||||
BreakBeforeBinaryOperators: None
|
||||
BreakBeforeConceptDeclarations: true
|
||||
BreakBeforeBraces: Attach
|
||||
BreakBeforeInheritanceComma: false
|
||||
BreakInheritanceList: BeforeColon
|
||||
BreakBeforeTernaryOperators: true
|
||||
BreakConstructorInitializersBeforeComma: false
|
||||
BreakConstructorInitializers: BeforeColon
|
||||
BreakAfterJavaFieldAnnotations: false
|
||||
BreakStringLiterals: true
|
||||
ColumnLimit: 100
|
||||
CommentPragmas: '^ IWYU pragma:'
|
||||
CompactNamespaces: false
|
||||
ConstructorInitializerAllOnOneLineOrOnePerLine: true
|
||||
ConstructorInitializerIndentWidth: 4
|
||||
ContinuationIndentWidth: 4
|
||||
Cpp11BracedListStyle: true
|
||||
DeriveLineEnding: true
|
||||
DerivePointerAlignment: true
|
||||
DisableFormat: false
|
||||
EmptyLineAfterAccessModifier: Never
|
||||
EmptyLineBeforeAccessModifier: LogicalBlock
|
||||
ExperimentalAutoDetectBinPacking: false
|
||||
FixNamespaceComments: true
|
||||
ForEachMacros:
|
||||
- foreach
|
||||
- Q_FOREACH
|
||||
- BOOST_FOREACH
|
||||
IfMacros:
|
||||
- KJ_IF_MAYBE
|
||||
IncludeBlocks: Regroup
|
||||
IncludeCategories:
|
||||
- Regex: '^[<"].*\.h[>"]'
|
||||
Priority: 0
|
||||
SortPriority: 0
|
||||
CaseSensitive: false
|
||||
- Regex: '^[<"].*\.hh[>"]'
|
||||
Priority: 1
|
||||
SortPriority: 1
|
||||
CaseSensitive: false
|
||||
- Regex: '.*'
|
||||
Priority: 2
|
||||
SortPriority: 2
|
||||
CaseSensitive: false
|
||||
IncludeIsMainRegex: '([-_](test|unittest))?$'
|
||||
IncludeIsMainSourceRegex: ''
|
||||
IndentAccessModifiers: false
|
||||
IndentCaseLabels: true
|
||||
IndentCaseBlocks: false
|
||||
IndentGotoLabels: true
|
||||
IndentPPDirectives: None
|
||||
IndentExternBlock: AfterExternBlock
|
||||
IndentRequires: false
|
||||
IndentWidth: 4
|
||||
IndentWrappedFunctionNames: false
|
||||
InsertTrailingCommas: None
|
||||
JavaScriptQuotes: Leave
|
||||
JavaScriptWrapImports: true
|
||||
KeepEmptyLinesAtTheStartOfBlocks: false
|
||||
LambdaBodyIndentation: Signature
|
||||
MacroBlockBegin: ''
|
||||
MacroBlockEnd: ''
|
||||
MaxEmptyLinesToKeep: 1
|
||||
NamespaceIndentation: None
|
||||
ObjCBinPackProtocolList: Never
|
||||
ObjCBlockIndentWidth: 2
|
||||
ObjCBreakBeforeNestedBlockParam: true
|
||||
ObjCSpaceAfterProperty: false
|
||||
ObjCSpaceBeforeProtocolList: true
|
||||
PenaltyBreakAssignment: 2
|
||||
PenaltyBreakBeforeFirstCallParameter: 1
|
||||
PenaltyBreakComment: 300
|
||||
PenaltyBreakFirstLessLess: 120
|
||||
PenaltyBreakString: 1000
|
||||
PenaltyBreakTemplateDeclaration: 10
|
||||
PenaltyExcessCharacter: 1000000
|
||||
PenaltyReturnTypeOnItsOwnLine: 200
|
||||
PenaltyIndentedWhitespace: 0
|
||||
PointerAlignment: Left
|
||||
PPIndentWidth: -1
|
||||
RawStringFormats:
|
||||
- Language: Cpp
|
||||
Delimiters:
|
||||
- cc
|
||||
- CC
|
||||
- cpp
|
||||
- Cpp
|
||||
- CPP
|
||||
- 'c++'
|
||||
- 'C++'
|
||||
CanonicalDelimiter: ''
|
||||
BasedOnStyle: google
|
||||
- Language: TextProto
|
||||
Delimiters:
|
||||
- pb
|
||||
- PB
|
||||
- proto
|
||||
- PROTO
|
||||
EnclosingFunctions:
|
||||
- EqualsProto
|
||||
- EquivToProto
|
||||
- PARSE_PARTIAL_TEXT_PROTO
|
||||
- PARSE_TEST_PROTO
|
||||
- PARSE_TEXT_PROTO
|
||||
- ParseTextOrDie
|
||||
- ParseTextProtoOrDie
|
||||
- ParseTestProto
|
||||
- ParsePartialTestProto
|
||||
CanonicalDelimiter: pb
|
||||
BasedOnStyle: google
|
||||
ReferenceAlignment: Pointer
|
||||
ReflowComments: true
|
||||
ShortNamespaceLines: 1
|
||||
SortIncludes: CaseSensitive
|
||||
SortJavaStaticImport: Before
|
||||
SortUsingDeclarations: true
|
||||
SpaceAfterCStyleCast: false
|
||||
SpaceAfterLogicalNot: false
|
||||
SpaceAfterTemplateKeyword: true
|
||||
SpaceBeforeAssignmentOperators: true
|
||||
SpaceBeforeCaseColon: false
|
||||
SpaceBeforeCpp11BracedList: false
|
||||
SpaceBeforeCtorInitializerColon: true
|
||||
SpaceBeforeInheritanceColon: true
|
||||
SpaceBeforeParens: ControlStatements
|
||||
SpaceAroundPointerQualifiers: Default
|
||||
SpaceBeforeRangeBasedForLoopColon: true
|
||||
SpaceInEmptyBlock: false
|
||||
SpaceInEmptyParentheses: false
|
||||
SpacesBeforeTrailingComments: 2
|
||||
SpacesInAngles: Never
|
||||
SpacesInConditionalStatement: false
|
||||
SpacesInContainerLiterals: true
|
||||
SpacesInCStyleCastParentheses: false
|
||||
SpacesInLineCommentPrefix:
|
||||
Minimum: 1
|
||||
Maximum: -1
|
||||
SpacesInParentheses: false
|
||||
SpacesInSquareBrackets: false
|
||||
SpaceBeforeSquareBrackets: false
|
||||
BitFieldColonSpacing: Both
|
||||
Standard: Auto
|
||||
StatementAttributeLikeMacros:
|
||||
- Q_EMIT
|
||||
StatementMacros:
|
||||
- Q_UNUSED
|
||||
- QT_REQUIRE_VERSION
|
||||
TabWidth: 8
|
||||
UseCRLF: false
|
||||
UseTab: Never
|
||||
WhitespaceSensitiveMacros:
|
||||
- STRINGIZE
|
||||
- PP_STRINGIZE
|
||||
- BOOST_PP_STRINGIZE
|
||||
- NS_SWIFT_NAME
|
||||
- CF_SWIFT_NAME
|
||||
...
|
||||
|
7
.gitignore
vendored
Normal file
7
.gitignore
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
# data file
|
||||
*.root
|
||||
*.log
|
||||
|
||||
# build cache
|
||||
build/
|
||||
2016Q3D/
|
23
.vscode/c_cpp_properties.json
vendored
Normal file
23
.vscode/c_cpp_properties.json
vendored
Normal file
@ -0,0 +1,23 @@
|
||||
{
|
||||
"configurations": [
|
||||
{
|
||||
"name": "Win32",
|
||||
"includePath": [
|
||||
"${default}",
|
||||
"${workspaceFolder}/include"
|
||||
],
|
||||
"defines": [
|
||||
"_DEBUG",
|
||||
"UNICODE",
|
||||
"_UNICODE"
|
||||
],
|
||||
"windowsSdkVersion": "10.0.19041.0",
|
||||
"cStandard": "c17",
|
||||
"cppStandard": "c++17",
|
||||
"intelliSenseMode": "windows-msvc-x64",
|
||||
"configurationProvider": "ms-vscode.cmake-tools",
|
||||
"compilerPath": "D:/Microsoft/VisualStudio/2022/Community/VC/Tools/MSVC/14.31.31103/bin/Hostx64/x64/cl.exe"
|
||||
}
|
||||
],
|
||||
"version": 4
|
||||
}
|
0
.vscode/settings.json
vendored
Normal file
0
.vscode/settings.json
vendored
Normal file
30
.vscode/tasks.json
vendored
Normal file
30
.vscode/tasks.json
vendored
Normal file
@ -0,0 +1,30 @@
|
||||
{
|
||||
"tasks": [
|
||||
{
|
||||
"type": "cppbuild",
|
||||
"label": "C/C++: g++.exe 生成活动文件",
|
||||
"command": "D:\\Scoop\\apps\\gcc\\current\\bin\\g++.exe",
|
||||
"args": [
|
||||
"-fdiagnostics-color=always",
|
||||
"-g",
|
||||
"${file}",
|
||||
"-o",
|
||||
"${fileDirname}\\${fileBasenameNoExtension}.exe",
|
||||
"-I",
|
||||
"${fileDirname}\\include\\",
|
||||
],
|
||||
"options": {
|
||||
"cwd": "${fileDirname}"
|
||||
},
|
||||
"problemMatcher": [
|
||||
"$gcc"
|
||||
],
|
||||
"group": {
|
||||
"kind": "build",
|
||||
"isDefault": true
|
||||
},
|
||||
"detail": "调试器生成的任务。"
|
||||
}
|
||||
],
|
||||
"version": "2.0.0"
|
||||
}
|
19
CMakeLists.txt
Normal file
19
CMakeLists.txt
Normal file
@ -0,0 +1,19 @@
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
|
||||
project(Q3D)
|
||||
set(Eigen3_DIR D:/Microsoft/vcpkg/installed/x64-windows/share/eigen3)
|
||||
SET(CMAKE_TOOLCHAIN_FILE D:/Microsoft/vcpkg/scripts/buildsystems/vcpkg.cmake)
|
||||
|
||||
find_package(ROOT REQUIRED)
|
||||
find_package(Eigen3 CONFIG REQUIRED)
|
||||
|
||||
include(${ROOT_USE_FILE})
|
||||
include_directories(${PROJECT_SOURCE_DIR}/include)
|
||||
|
||||
file(GLOB sources ${PROJECT_SOURCE_DIR}/src/*.cpp)
|
||||
file(GLOB headers ${PROJECT_SOURCE_DIR}/include/*.h)
|
||||
|
||||
add_executable(Q3D main.cpp ${sources} ${headers})
|
||||
target_link_libraries(Q3D PRIVATE ${ROOT_LIBRARIES} Eigen3::Eigen)
|
||||
|
||||
install(TARGETS Q3D DESTINATION F:/NuclearAstroPhy/Q3D-Calibration)
|
106
Q3D.code-workspace
Normal file
106
Q3D.code-workspace
Normal file
@ -0,0 +1,106 @@
|
||||
{
|
||||
"folders": [
|
||||
{
|
||||
"path": "."
|
||||
}
|
||||
],
|
||||
"settings": {
|
||||
"files.associations": {
|
||||
"Offlinecal201609副本.C": "cpp",
|
||||
"Offline201609.C": "cpp",
|
||||
"Offlinecal201609.C": "cpp",
|
||||
"Trans.C": "cpp",
|
||||
"Trans1.C": "cpp",
|
||||
"Trans2.C": "cpp",
|
||||
"Transcal.C": "cpp",
|
||||
"Offlinecal201609_Li.C": "cpp",
|
||||
"Offlinecal201609_d.C": "cpp",
|
||||
"hadd1.C": "cpp",
|
||||
"hadd.C": "cpp",
|
||||
"Filter5.C": "cpp",
|
||||
"Filter4.C": "cpp",
|
||||
"Filter3.C": "cpp",
|
||||
"Filter2.C": "cpp",
|
||||
"Filter.C": "cpp",
|
||||
"Er.C": "cpp",
|
||||
"xstring": "cpp",
|
||||
"array": "cpp",
|
||||
"chrono": "cpp",
|
||||
"compare": "cpp",
|
||||
"format": "cpp",
|
||||
"memory": "cpp",
|
||||
"ratio": "cpp",
|
||||
"tuple": "cpp",
|
||||
"type_traits": "cpp",
|
||||
"utility": "cpp",
|
||||
"xmemory": "cpp",
|
||||
"xtr1common": "cpp",
|
||||
"xutility": "cpp",
|
||||
"span": "cpp",
|
||||
"vector": "cpp",
|
||||
"cmath": "cpp",
|
||||
"iostream": "cpp",
|
||||
"core": "cpp",
|
||||
"dense": "cpp",
|
||||
"algorithm": "cpp",
|
||||
"atomic": "cpp",
|
||||
"bit": "cpp",
|
||||
"cctype": "cpp",
|
||||
"charconv": "cpp",
|
||||
"clocale": "cpp",
|
||||
"complex": "cpp",
|
||||
"concepts": "cpp",
|
||||
"condition_variable": "cpp",
|
||||
"cstdarg": "cpp",
|
||||
"cstddef": "cpp",
|
||||
"cstdint": "cpp",
|
||||
"cstdio": "cpp",
|
||||
"cstdlib": "cpp",
|
||||
"cstring": "cpp",
|
||||
"ctime": "cpp",
|
||||
"cwchar": "cpp",
|
||||
"deque": "cpp",
|
||||
"exception": "cpp",
|
||||
"forward_list": "cpp",
|
||||
"functional": "cpp",
|
||||
"initializer_list": "cpp",
|
||||
"iomanip": "cpp",
|
||||
"ios": "cpp",
|
||||
"iosfwd": "cpp",
|
||||
"istream": "cpp",
|
||||
"iterator": "cpp",
|
||||
"limits": "cpp",
|
||||
"list": "cpp",
|
||||
"locale": "cpp",
|
||||
"map": "cpp",
|
||||
"mutex": "cpp",
|
||||
"new": "cpp",
|
||||
"optional": "cpp",
|
||||
"ostream": "cpp",
|
||||
"queue": "cpp",
|
||||
"set": "cpp",
|
||||
"sstream": "cpp",
|
||||
"stdexcept": "cpp",
|
||||
"stop_token": "cpp",
|
||||
"streambuf": "cpp",
|
||||
"string": "cpp",
|
||||
"system_error": "cpp",
|
||||
"thread": "cpp",
|
||||
"typeinfo": "cpp",
|
||||
"unordered_map": "cpp",
|
||||
"unordered_set": "cpp",
|
||||
"xfacet": "cpp",
|
||||
"xhash": "cpp",
|
||||
"xiosbase": "cpp",
|
||||
"xlocale": "cpp",
|
||||
"xlocbuf": "cpp",
|
||||
"xlocinfo": "cpp",
|
||||
"xlocmes": "cpp",
|
||||
"xlocmon": "cpp",
|
||||
"xlocnum": "cpp",
|
||||
"xloctime": "cpp",
|
||||
"xstddef": "cpp",
|
||||
"xtree": "cpp"
|
||||
}
|
||||
}
|
||||
}
|
89
include/GaussNewton.h
Normal file
89
include/GaussNewton.h
Normal file
@ -0,0 +1,89 @@
|
||||
#pragma once
|
||||
|
||||
#ifndef GNM_h
|
||||
#define GNM_h
|
||||
|
||||
#include "utils.h"
|
||||
|
||||
#include <Eigen/Dense>
|
||||
|
||||
class GaussNewton {
|
||||
public:
|
||||
GaussNewton(int L_, double* parma_, double (*Func_)(double, double*),
|
||||
double* (*Gunc_)(double, double*));
|
||||
~GaussNewton();
|
||||
|
||||
public:
|
||||
void addData(double x, double y);
|
||||
double* solve();
|
||||
|
||||
public:
|
||||
double eps = 1e-5;
|
||||
double* parma;
|
||||
double (*Func)(double, double*);
|
||||
double* (*Gunc)(double, double*);
|
||||
int L, maxIter = 10;
|
||||
std::vector<Eigen::Vector2d> data;
|
||||
|
||||
private:
|
||||
void calmJ_vF();
|
||||
void calmH_vG();
|
||||
|
||||
private:
|
||||
Eigen::MatrixXd mJ; // 雅克比矩阵
|
||||
Eigen::MatrixXd mH; // H矩阵
|
||||
Eigen::VectorXd vF; // 误差向量
|
||||
Eigen::Vector3d vG; // 反馈向量
|
||||
};
|
||||
|
||||
GaussNewton::GaussNewton(int L_, double* parma_, double (*Func_)(double, double*),
|
||||
double* (*Gunc_)(double, double*)) {
|
||||
L = L_;
|
||||
parma = parma_;
|
||||
Func = Func_;
|
||||
Gunc = Gunc_;
|
||||
}
|
||||
|
||||
GaussNewton::~GaussNewton() {}
|
||||
|
||||
void GaussNewton::addData(double x, double y) { data.push_back(Eigen::Vector2d(x, y)); }
|
||||
|
||||
void GaussNewton::calmJ_vF() {
|
||||
double x, y;
|
||||
double* resJ;
|
||||
|
||||
mJ.resize(data.size(), L);
|
||||
vF.resize(data.size());
|
||||
|
||||
for (int i = 0; i < data.size(); i++) {
|
||||
Eigen::Vector2d& point = data.at(i);
|
||||
x = point(0), y = point(1);
|
||||
resJ = (*Gunc)(x, parma);
|
||||
for (int j = 0; j < L; j++) mJ(i, j) = resJ[j];
|
||||
vF(i) = y - (*Func)(x, parma);
|
||||
}
|
||||
}
|
||||
|
||||
void GaussNewton::calmH_vG() {
|
||||
mH = mJ.transpose() * mJ;
|
||||
vG = -mJ.transpose() * vF;
|
||||
}
|
||||
|
||||
double* GaussNewton::solve() {
|
||||
Eigen::VectorXd vX;
|
||||
vX.resize(L);
|
||||
|
||||
for (int k = 0; k < maxIter; k++) {
|
||||
calmJ_vF();
|
||||
calmH_vG();
|
||||
|
||||
vX = mH.ldlt().solve(vG);
|
||||
if (vX.norm() <= eps) return parma;
|
||||
|
||||
for (int i = 0; i < L; i++) parma[i] += vX[i];
|
||||
}
|
||||
|
||||
return parma;
|
||||
}
|
||||
|
||||
#endif
|
121
include/LevenbergMarquardt.h
Normal file
121
include/LevenbergMarquardt.h
Normal file
@ -0,0 +1,121 @@
|
||||
#pragma once
|
||||
|
||||
#ifndef LMM_h
|
||||
#define LMM_h
|
||||
|
||||
#include "utils.h"
|
||||
|
||||
#include <Eigen/Dense>
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
|
||||
|
||||
class LevenbergMarquardt {
|
||||
public:
|
||||
LevenbergMarquardt(int L_, double* parma_, double (*Func_)(double, double*),
|
||||
double* (*Gunc_)(double, double*), int type_ = 0);
|
||||
~LevenbergMarquardt();
|
||||
|
||||
public:
|
||||
void addData(double x, double y);
|
||||
double* solve();
|
||||
|
||||
public:
|
||||
double mu = 1., eps = 1e-5;
|
||||
double* parma;
|
||||
double (*Func)(double, double*);
|
||||
double* (*Gunc)(double, double*);
|
||||
int L, type_, maxIter = 20;
|
||||
std::vector<Eigen::Vector2d> data;
|
||||
|
||||
private:
|
||||
void calmJ_vF();
|
||||
void calmH_vG();
|
||||
double calMse(double* parma_);
|
||||
|
||||
private:
|
||||
Eigen::MatrixXd mJ; // 雅克比矩阵
|
||||
Eigen::MatrixXd mH; // H矩阵
|
||||
Eigen::VectorXd vF; // 误差向量
|
||||
Eigen::VectorXd vG; // 左侧
|
||||
};
|
||||
|
||||
LevenbergMarquardt::LevenbergMarquardt(int L_, double* parma_, double (*Func_)(double, double*),
|
||||
double* (*Gunc_)(double, double*), int type_) {
|
||||
L = L_;
|
||||
parma = parma_;
|
||||
Func = Func_;
|
||||
Gunc = Gunc_;
|
||||
this->type_ = type_;
|
||||
}
|
||||
|
||||
LevenbergMarquardt::~LevenbergMarquardt() {}
|
||||
|
||||
void LevenbergMarquardt::addData(double x, double y) { data.push_back(Eigen::Vector2d(x, y)); }
|
||||
|
||||
void LevenbergMarquardt::calmJ_vF() {
|
||||
double x, y;
|
||||
double* resJ;
|
||||
|
||||
mJ.resize(data.size(), L);
|
||||
vF.resize(data.size());
|
||||
|
||||
for (int i = 0; i < data.size(); i++) {
|
||||
Eigen::Vector2d& point = data.at(i);
|
||||
x = point(0), y = point(1);
|
||||
resJ = (*Gunc)(x, parma);
|
||||
for (int j = 0; j < L; j++) mJ(i, j) = resJ[j];
|
||||
vF(i) = y - (*Func)(x, parma);
|
||||
}
|
||||
}
|
||||
|
||||
void LevenbergMarquardt::calmH_vG() {
|
||||
mH = mJ.transpose() * mJ;
|
||||
vG = -mJ.transpose() * vF;
|
||||
}
|
||||
|
||||
double LevenbergMarquardt::calMse(double* parma_) {
|
||||
int n = data.size();
|
||||
double x, y, mse = 0;
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
Eigen::Vector2d& point = data.at(i);
|
||||
x = point(0), y = point(1);
|
||||
mse += pow(y - (*Func)(x, parma_), 2);
|
||||
}
|
||||
|
||||
return mse / n;
|
||||
}
|
||||
|
||||
double* LevenbergMarquardt::solve() {
|
||||
double v = 2, cost;
|
||||
double* parmaNew;
|
||||
|
||||
Eigen::VectorXd vX;
|
||||
Eigen::MatrixXd mT, mD = Eigen::MatrixXd::Zero();
|
||||
vX.resize(L);
|
||||
mD.resize(L, L);
|
||||
for (int i = 0; i < L; i++) mD(i, i) = 1;
|
||||
|
||||
for (int k = 0; k < maxIter; k++) {
|
||||
calmJ_vF();
|
||||
calmH_vG();
|
||||
|
||||
if (type_ == 1) {
|
||||
mT = mJ * mJ.transpose();
|
||||
for (int i = 0; i < L; i++) mD(i, i) = sqrt(mT(i, i));
|
||||
}
|
||||
|
||||
mH = mH + mu * mD;
|
||||
vX = mH.ldlt().solve(vG);
|
||||
if (vX.norm() <= eps) return parma;
|
||||
|
||||
for (int i = 0; i < L; i++) parmaNew[i] = parma[i] + vX[i];
|
||||
cost = calMse(parma) - calMse(parmaNew);
|
||||
cost /= vX.transpose() * (mu * vX + vG);
|
||||
}
|
||||
|
||||
return parma;
|
||||
}
|
||||
|
||||
#endif
|
92
include/clip.h
Normal file
92
include/clip.h
Normal file
@ -0,0 +1,92 @@
|
||||
#pragma once
|
||||
|
||||
#ifndef clip_h
|
||||
#define clip_h
|
||||
|
||||
#include <cmath>
|
||||
|
||||
#define INF 1e9
|
||||
|
||||
class SigmaClip {
|
||||
private:
|
||||
int maxiters;
|
||||
double sigma, minValue, maxValue;
|
||||
double (*cenF)(double *, int) = nullptr;
|
||||
double (*stdF)(double *, int) = nullptr;
|
||||
|
||||
public:
|
||||
SigmaClip(double sigma = 3, int maxiters = 5, double (*cenF)(double *, int) = nullptr,
|
||||
double (*stdF)(double *, int) = nullptr);
|
||||
~SigmaClip();
|
||||
double *clip(double *data, int L);
|
||||
|
||||
private:
|
||||
void computeBound(double *data, int L);
|
||||
};
|
||||
|
||||
double GetAvg(double *data, int L) {
|
||||
double sum = 0;
|
||||
for (int i = 0; i < L; i++) sum += data[i];
|
||||
return sum / L;
|
||||
}
|
||||
|
||||
double GetStd(double *data, int L) {
|
||||
double sum = 0;
|
||||
double mean = GetAvg(data, L);
|
||||
for (int i = 0; i < L; i++) sum += (data[i] - mean) * (data[i] - mean);
|
||||
return sqrt(sum / L);
|
||||
}
|
||||
|
||||
SigmaClip::SigmaClip(double sigma, int maxiters, double (*cenF)(double *, int), double (*stdF)(double *, int)) {
|
||||
this->sigma = sigma;
|
||||
this->maxiters = maxiters;
|
||||
this->minValue = INF;
|
||||
this->maxValue = -INF;
|
||||
|
||||
this->cenF = cenF == nullptr ? GetAvg : cenF;
|
||||
this->stdF = stdF == nullptr ? GetStd : stdF;
|
||||
}
|
||||
|
||||
SigmaClip::~SigmaClip() {}
|
||||
|
||||
void SigmaClip::computeBound(double *data, int L) {
|
||||
double std = (*stdF)(data, L);
|
||||
double mean = (*cenF)(data, L);
|
||||
|
||||
minValue = mean - (std * sigma);
|
||||
maxValue = mean + (std * sigma);
|
||||
}
|
||||
|
||||
double *SigmaClip::clip(double *data, int L) {
|
||||
int tmp, cnt;
|
||||
double *value;
|
||||
|
||||
for (int i = 1; i <= maxiters; i++) {
|
||||
computeBound(data, L);
|
||||
|
||||
cnt = 0;
|
||||
for (int j = 0; j < L; j++)
|
||||
if (data[j] >= minValue && data[j] <= maxValue) cnt += 1;
|
||||
|
||||
tmp = L, L = cnt, cnt = 0;
|
||||
value = new double[L];
|
||||
for (int j = 0; j < tmp; j++)
|
||||
if (data[j] >= minValue && data[j] <= maxValue) {
|
||||
value[cnt] = data[j];
|
||||
cnt += 1;
|
||||
}
|
||||
|
||||
delete data;
|
||||
data = new double[L];
|
||||
for (int j = 0; j < L; j++) data[j] = value[j];
|
||||
delete[] value;
|
||||
}
|
||||
|
||||
value = new double[L + 1];
|
||||
value[0] = L;
|
||||
for (int j = 0; j < L; j++) value[j + 1] = data[j];
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
#endif
|
192
include/getADC.h
Normal file
192
include/getADC.h
Normal file
@ -0,0 +1,192 @@
|
||||
#include "LevenbergMarquardt.h"
|
||||
#include "utils.h"
|
||||
#include <TCanvas.h>
|
||||
#include <TF1.h>
|
||||
#include <TFile.h>
|
||||
#include <TH1F.h>
|
||||
#include <TTree.h>
|
||||
|
||||
#include <Eigen/Dense>
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
using Eigen::MatrixXd;
|
||||
using Eigen::VectorXd;
|
||||
|
||||
void getADC(const char *fin) {
|
||||
// read file
|
||||
TFile *fRun = new TFile(fin);
|
||||
TTree *t = (TTree *)fRun->Get("Tree1");
|
||||
|
||||
// data numbers
|
||||
Long64_t ntot = t->GetEntriesFast();
|
||||
|
||||
// five X-4 block
|
||||
UInt_t bArray[16];
|
||||
UInt_t cArray[16];
|
||||
UInt_t dArray[16];
|
||||
UInt_t eArray[16];
|
||||
UInt_t fArray[16];
|
||||
|
||||
// calibration coefficient
|
||||
Float_t CalC[5][8];
|
||||
Float_t Calk1[5][8];
|
||||
Float_t Calk2[5][8];
|
||||
|
||||
// read adc data
|
||||
t->SetBranchAddress("adc0ch0", &bArray[0]);
|
||||
t->SetBranchAddress("adc0ch1", &bArray[1]);
|
||||
t->SetBranchAddress("adc0ch2", &bArray[2]);
|
||||
t->SetBranchAddress("adc0ch3", &bArray[3]);
|
||||
t->SetBranchAddress("adc0ch4", &bArray[4]);
|
||||
t->SetBranchAddress("adc0ch5", &bArray[5]);
|
||||
t->SetBranchAddress("adc0ch6", &bArray[6]);
|
||||
t->SetBranchAddress("adc0ch7", &bArray[7]);
|
||||
t->SetBranchAddress("adc0ch8", &bArray[8]);
|
||||
t->SetBranchAddress("adc0ch9", &bArray[9]);
|
||||
t->SetBranchAddress("adc0ch10", &bArray[10]);
|
||||
t->SetBranchAddress("adc0ch11", &bArray[11]);
|
||||
t->SetBranchAddress("adc0ch12", &bArray[12]);
|
||||
t->SetBranchAddress("adc0ch13", &bArray[13]);
|
||||
t->SetBranchAddress("adc0ch14", &bArray[14]);
|
||||
t->SetBranchAddress("adc0ch15", &bArray[15]);
|
||||
|
||||
t->SetBranchAddress("adc0ch16", &cArray[0]);
|
||||
t->SetBranchAddress("adc0ch17", &cArray[1]);
|
||||
t->SetBranchAddress("adc0ch18", &cArray[2]);
|
||||
t->SetBranchAddress("adc0ch19", &cArray[3]);
|
||||
t->SetBranchAddress("adc0ch20", &cArray[4]);
|
||||
t->SetBranchAddress("adc0ch21", &cArray[5]);
|
||||
t->SetBranchAddress("adc0ch22", &cArray[6]);
|
||||
t->SetBranchAddress("adc0ch23", &cArray[7]);
|
||||
t->SetBranchAddress("adc0ch24", &cArray[8]);
|
||||
t->SetBranchAddress("adc0ch25", &cArray[9]);
|
||||
t->SetBranchAddress("adc0ch26", &cArray[10]);
|
||||
t->SetBranchAddress("adc0ch27", &cArray[11]);
|
||||
t->SetBranchAddress("adc0ch28", &cArray[12]);
|
||||
t->SetBranchAddress("adc0ch29", &cArray[13]);
|
||||
t->SetBranchAddress("adc0ch30", &cArray[14]);
|
||||
t->SetBranchAddress("adc0ch31", &cArray[15]);
|
||||
|
||||
t->SetBranchAddress("adc1ch0", &dArray[0]);
|
||||
t->SetBranchAddress("adc1ch1", &dArray[1]);
|
||||
t->SetBranchAddress("adc1ch2", &dArray[2]);
|
||||
t->SetBranchAddress("adc1ch3", &dArray[3]);
|
||||
t->SetBranchAddress("adc1ch4", &dArray[4]);
|
||||
t->SetBranchAddress("adc1ch5", &dArray[5]);
|
||||
t->SetBranchAddress("adc1ch6", &dArray[6]);
|
||||
t->SetBranchAddress("adc1ch7", &dArray[7]);
|
||||
t->SetBranchAddress("adc1ch8", &dArray[8]);
|
||||
t->SetBranchAddress("adc1ch9", &dArray[9]);
|
||||
t->SetBranchAddress("adc1ch10", &dArray[10]);
|
||||
t->SetBranchAddress("adc1ch11", &dArray[11]);
|
||||
t->SetBranchAddress("adc1ch12", &dArray[12]);
|
||||
t->SetBranchAddress("adc1ch13", &dArray[13]);
|
||||
t->SetBranchAddress("adc1ch14", &dArray[14]);
|
||||
t->SetBranchAddress("adc1ch15", &dArray[15]);
|
||||
|
||||
t->SetBranchAddress("adc1ch16", &eArray[0]);
|
||||
t->SetBranchAddress("adc1ch17", &eArray[1]);
|
||||
t->SetBranchAddress("adc1ch18", &eArray[2]);
|
||||
t->SetBranchAddress("adc1ch19", &eArray[3]);
|
||||
t->SetBranchAddress("adc1ch20", &eArray[4]);
|
||||
t->SetBranchAddress("adc1ch21", &eArray[5]);
|
||||
t->SetBranchAddress("adc1ch22", &eArray[6]);
|
||||
t->SetBranchAddress("adc1ch23", &eArray[7]);
|
||||
t->SetBranchAddress("adc1ch24", &eArray[8]);
|
||||
t->SetBranchAddress("adc1ch25", &eArray[9]);
|
||||
t->SetBranchAddress("adc1ch26", &eArray[10]);
|
||||
t->SetBranchAddress("adc1ch27", &eArray[11]);
|
||||
t->SetBranchAddress("adc1ch28", &eArray[12]);
|
||||
t->SetBranchAddress("adc1ch29", &eArray[13]);
|
||||
t->SetBranchAddress("adc1ch30", &eArray[14]);
|
||||
t->SetBranchAddress("adc1ch31", &eArray[15]);
|
||||
|
||||
t->SetBranchAddress("adc2ch0", &fArray[0]);
|
||||
t->SetBranchAddress("adc2ch1", &fArray[1]);
|
||||
t->SetBranchAddress("adc2ch2", &fArray[2]);
|
||||
t->SetBranchAddress("adc2ch3", &fArray[3]);
|
||||
t->SetBranchAddress("adc2ch4", &fArray[4]);
|
||||
t->SetBranchAddress("adc2ch5", &fArray[5]);
|
||||
t->SetBranchAddress("adc2ch6", &fArray[6]);
|
||||
t->SetBranchAddress("adc2ch7", &fArray[7]);
|
||||
t->SetBranchAddress("adc2ch8", &fArray[8]);
|
||||
t->SetBranchAddress("adc2ch9", &fArray[9]);
|
||||
t->SetBranchAddress("adc2ch10", &fArray[10]);
|
||||
t->SetBranchAddress("adc2ch11", &fArray[11]);
|
||||
t->SetBranchAddress("adc2ch12", &fArray[12]);
|
||||
t->SetBranchAddress("adc2ch13", &fArray[13]);
|
||||
t->SetBranchAddress("adc2ch14", &fArray[14]);
|
||||
t->SetBranchAddress("adc2ch15", &fArray[15]);
|
||||
|
||||
TH1F *Left = new TH1F("Left", "Left", 300, 0, 300);
|
||||
TH1F *Right = new TH1F("Right", "Right", 300, 0, 300);
|
||||
|
||||
for (int i = 0; i < ntot; i++) {
|
||||
t->GetEntry(i);
|
||||
Left->Fill(bArray[0]);
|
||||
Right->Fill(bArray[8]);
|
||||
}
|
||||
|
||||
// use matrix and least square method to get coefficient
|
||||
// SigmaClip *clip = new SigmaClip(5);
|
||||
int cnt = 0;
|
||||
double vX[300];
|
||||
for (int i = 2; i < 300; i++)
|
||||
if (Left->GetBinContent(i) > 0) {
|
||||
vX[cnt] = i;
|
||||
cnt += 1;
|
||||
}
|
||||
|
||||
VectorXd mY(cnt);
|
||||
MatrixXd mX(cnt, 3);
|
||||
MatrixXd mB(3, 1);
|
||||
double n;
|
||||
double *parma = new double[3];
|
||||
double sigma, b0, b1, b2;
|
||||
|
||||
cnt = 0;
|
||||
for (int i = 2; i < 300; i++) {
|
||||
n = Left->GetBinContent(i);
|
||||
if (n == 0) continue;
|
||||
mY(cnt, 0) = log(n);
|
||||
mX(cnt, 0) = 1, mX(cnt, 1) = i, mX(cnt, 2) = i * i;
|
||||
cnt += 1;
|
||||
}
|
||||
|
||||
mB = (mX.transpose() * mX).inverse() * mX.transpose() * mY;
|
||||
b0 = mB(0, 0), b1 = mB(1, 0), b2 = mB(2, 0);
|
||||
sigma = -1 / b2;
|
||||
parma[1] = b1 * sigma / 2;
|
||||
parma[0] = exp(b0 + parma[1] * parma[1] / sigma);
|
||||
parma[2] = sqrt(sigma);
|
||||
|
||||
cout << parma[0] << ", " << parma[1] << ", " << parma[2] << endl;
|
||||
|
||||
LevenbergMarquardt LM(3, parma, Gaussian, GaussianJacobian);
|
||||
for (int i = 2; i < 300; i++) {
|
||||
n = Left->GetBinContent(i);
|
||||
if (n == 0) continue;
|
||||
LM.addData(i, n);
|
||||
}
|
||||
parma = LM.solve();
|
||||
cout << parma[0] << ", " << parma[1] << ", " << parma[2] << endl;
|
||||
|
||||
// TCanvas *c1 = new TCanvas("c1", "Fitting with Gaussian function");
|
||||
// c1->Divide(1, 2);
|
||||
|
||||
// c1->cd(1);
|
||||
// c1->SetGrid();
|
||||
// Left->Draw();
|
||||
|
||||
// c1->cd(2);
|
||||
// c1->SetGrid();
|
||||
// Right->Draw();
|
||||
|
||||
// TF1 *GausFunc = new TF1("GausFunc", "[0]*exp(-(x-[1])*(x-[1])/2.0/[2]/[2])", 0, 300);
|
||||
// GausFunc->SetParLimits(0, 1000, 2400);
|
||||
// GausFunc->SetParLimits(1, 100, 200);
|
||||
// GausFunc->SetParLimits(2, 0, 15);
|
||||
// Left->Fit(GausFunc, "R");
|
||||
// Right->Fit(GausFunc, "R");
|
||||
}
|
20
include/utils.h
Normal file
20
include/utils.h
Normal file
@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#ifndef utils_h
|
||||
#define utils_h
|
||||
|
||||
#include <cmath>
|
||||
|
||||
double Gaussian(double x, double* p) {
|
||||
return p[0] * exp(-(x - p[1]) * (x - p[1]) / (2 * p[2] * p[2]));
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
#endif
|
6
main.cpp
Normal file
6
main.cpp
Normal file
@ -0,0 +1,6 @@
|
||||
#include "getADC.h"
|
||||
|
||||
int main() {
|
||||
getADC("2016Q3D/root/raw/201609Q3D1002.root");
|
||||
return 0;
|
||||
}
|
9
process.cpp
Normal file
9
process.cpp
Normal file
@ -0,0 +1,9 @@
|
||||
#include <TInterpreter.h>
|
||||
#include <TROOT.h>
|
||||
#include <TSystem.h>
|
||||
|
||||
void process() {
|
||||
gInterpreter->AddIncludePath("./include");
|
||||
gROOT->ProcessLine(".L getADC.cpp");
|
||||
gROOT->ProcessLine("getADC(\"2016Q3D/root/raw/201609Q3D1002.root\")");
|
||||
}
|
Loading…
Reference in New Issue
Block a user