EnergySpectrumAnalyer/src/CountRateAnalysisView/CountRateAnalysisView.cpp
2026-03-30 18:06:52 +08:00

147 lines
4.2 KiB
C++

#include "CountRateAnalysisView.h"
#include "ui_CountRateAnalysisView.h"
#include <QVBoxLayout>
#include <QPushButton>
#include <QMessageBox>
#include <QwtPlotCurve>
#include <QwtPlotMarker>
#include <QwtPlotZoneItem>
#include <QwtPlotCanvas>
#include <QwtLegend>
#include <QwtText>
#include <cmath>
#include <QtMath>
#include "CustomQwtPlot.h"
#include <QDebug>
#include <GlobalDefine.h>
#include "csv.h"
CountRateAnalysisView::CountRateAnalysisView(QWidget *parent) :
MeasureAnalysisView(parent),
ui(new Ui::CountRateAnalysisView)
{
ui->setupUi(this);
this->setViewType(PlotFrame);
InitUi();
}
CountRateAnalysisView::~CountRateAnalysisView()
{
delete ui;
}
void CountRateAnalysisView::InitViewWorkspace(const QString &project_name)
{
}
void CountRateAnalysisView::SetAnalyzeDataFilename(const QMap<QString, QVariant> &data_files_set)
{
if(!data_files_set.isEmpty())
{
m_AllData = getParticleInjectTimeData(data_files_set.first().toString());
setData(m_AllData);
}
}
void CountRateAnalysisView::setData(QVector<ParticleInjectTime> data)
{
int energyStart = ui->label_energyStart->text().toInt();
int energyEnd = ui->label_energyEnd->text().toInt();
int nInv = 1;
constexpr qint64 NS_PER_SECOND = 1000000000; // 纳秒转秒的基数
if (data.isEmpty()) return; // 添加空列表保护
QVector<double> x;
QVector<double> y;
const ParticleInjectTime& lostdt = data.last();
const qint64 totalNanoseconds = lostdt.dTime * 5;
const int nAllS = static_cast<int>(totalNanoseconds / NS_PER_SECOND) + 1;
const int nVecSize = nAllS / nInv;
QVector<double> vec(nVecSize); // 默认初始化为0
double minValue = 0;
double maxValue = 0;
for(auto info : data)
{
const qint64 ns = (info.dTime * 5) / NS_PER_SECOND;
const int nidx = static_cast<int>(ns / nInv); // 整数除法,速度快
if (nidx < nVecSize)
++vec[nidx]; // 使用 ++ 而非 vec[nidx]++
}
QVector<double> vecXpt(vec.size());
for (int i = 0; i < vec.size(); ++i)
vecXpt[i] = static_cast<double>(i);
// 创建曲线并设置数据
QwtPlotCurve *curve = new QwtPlotCurve();
curve->setSamples(vecXpt, vec);
// 将曲线添加到 CustomQwtPlot 中(会自动分配颜色)
plot->AddCurve(curve);
// 设置坐标轴范围和标题
// plot->setAxisScale(QwtPlot::xBottom, 0, x.last());
// plot->setAxisScale(QwtPlot::yLeft,minValue , maxValue);
LOG_INFO(QStringLiteral(u"%1数据读取完毕.").arg(this->GetViewName()));
// 刷新绘图
plot->replot();
}
QVector<ParticleInjectTime> CountRateAnalysisView::getParticleInjectTimeData(QString path)
{
QVector<ParticleInjectTime> records;
io::CSVReader<4> in(QStrToSysPath(path));
in.read_header(io::ignore_extra_column, QStringLiteral(u"板卡号").toStdString(), QStringLiteral(u"通道号").toStdString(), QStringLiteral(u"能量(KeV)").toStdString(), QStringLiteral(u"时间计数").toStdString());
int board, channel;
double energy, time_count;
int lineNumber = 0;
// 逐行读取
while (in.read_row(board, channel, energy, time_count))
{
int detector = board * 4 + channel + 1;
lineNumber++;
if(detector > 32)
{
continue;
}
ParticleInjectTime rec;
rec.index = lineNumber;
rec.dEnergy = energy;
rec.dTime = time_count;
records.append(rec);
}
return records;
}
void CountRateAnalysisView::InitUi()
{
plot = new CustomQwtPlot();
plot->SetXaxisDragScale(true);
setupPlot();
ui->verticalLayout_2->addWidget(plot);
}
void CountRateAnalysisView::setupPlot()
{
plot->setCanvasBackground(Qt::white);
QwtPlotCanvas* canvas = qobject_cast<QwtPlotCanvas*>(plot->canvas());
canvas->setFrameStyle(QFrame::NoFrame);
plot->setAxisTitle(QwtPlot::xBottom, QStringLiteral(u"时间"));
plot->setAxisTitle(QwtPlot::yLeft, QStringLiteral(u"计数"));
// set axis auto scale
plot->setAxisAutoScale(QwtPlot::xBottom, true);
plot->setAxisAutoScale(QwtPlot::yLeft, true);
// 启用网格线
plot->enableAxis(QwtPlot::xBottom);
plot->enableAxis(QwtPlot::yLeft);
plot->SetXaxisDragScale(true);
}