Q3D-Calibration/include/utils.h
2022-07-06 23:42:52 +08:00

141 lines
3.4 KiB
C++

#pragma once
#ifndef utils_h
#define utils_h
#include <TFile.h>
#include <TH1F.h>
#include <TTree.h>
#include <Eigen/Dense>
#include <ios>
#include <iostream>
#define DEBUG 0
#define INF 1e9
#define CHANNEL_NUMBER 4096
using namespace std;
using std::string;
using std::to_string;
double dataMax(std::vector<double> data) {
double x, m = -INF;
for (int i = 0; i < data.size(); i++) {
x = data.at(i);
m = std::max(m, x);
}
return m;
}
double dataAvg(std::vector<double> data) {
double m = 0;
for (int i = 0; i < data.size(); i++) m += data.at(i);
return m / data.size();
}
double dataStd(std::vector<double> data) {
double m = 0;
double mu = dataAvg(data);
for (int i = 0; i < data.size(); i++) m += std::pow(data.at(i) - mu, 2);
return m / (data.size() - 1);
}
double dataMax2D(std::vector<Eigen::Vector2d> data) {
double m = -INF;
for (int i = 0; i < data.size(); i++) {
Eigen::Vector2d &point = data.at(i);
m = std::max(m, point(1));
}
return m;
}
double dataAvg2D(std::vector<Eigen::Vector2d> data) {
int n = 0;
double m = 0;
for (int i = 0; i < data.size(); i++) {
Eigen::Vector2d &point = data.at(i);
n += point(1);
m += point(0) * point(1);
}
return m / n;
}
double dataStd2D(std::vector<Eigen::Vector2d> data) {
int n = 0;
double m = 0;
double mu = dataAvg2D(data);
for (int i = 0; i < data.size(); i++) {
Eigen::Vector2d &point = data.at(i);
n += point(1);
m += std::pow(point(0) - mu, 2) * point(1);
}
return std::sqrt(m / (n - 1));
}
double dataMax2DInd(std::vector<Eigen::Vector2d> data) {
double x = 0, m = -INF;
for (int i = 0; i < data.size(); i++) {
Eigen::Vector2d &point = data.at(i);
if (point(1) > m) {
x = point(0);
m = point(1);
}
}
return x;
}
double dataStd2DSQRT(std::vector<Eigen::Vector2d> data) { return std::sqrt(dataMax2D(data)); }
void readROOTData(const char *fin, std::vector<int> adcValue[6][8][2], int n = 6, int m = 8,
int thMin = 800, int thMax = 4000) {
// 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 dataArray[6][16];
// read adc data
int na, nc;
double x1, x2;
string adc;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
na = i / 2;
nc = j + 2 * m * (i % 2);
adc = "adc" + to_string(na) + "ch" + to_string(nc);
t->SetBranchAddress(adc.c_str(), &dataArray[i][j]);
adc = "adc" + to_string(na) + "ch" + to_string(nc + m);
t->SetBranchAddress(adc.c_str(), &dataArray[i][j + m]);
}
for (int i = 0; i < ntot; i++) {
t->GetEntry(i);
for (int j = 0; j < n; j++)
for (int k = 0; k < m; k++) {
x1 = dataArray[j][k];
x2 = dataArray[j][k + m];
if ((x1 + x2) < thMin || (x1 + x2) > thMax) continue;
adcValue[j][k][0].push_back(dataArray[j][k]);
adcValue[j][k][1].push_back(dataArray[j][k]);
}
}
}
string rmString(string str, string substr) {
int pos;
int len = substr.length();
while (true) {
pos = str.find(substr);
if (pos < 0) break;
str.erase(pos, len);
}
return str;
}
#endif