#include "CountRateAnalysis.h" #include "ui_CountRateAnalysis.h" #include #include #include #include #include #include #include #include #include #include #include #include "CustomQwtPlot.h" #include #include CountRateAnalysis::CountRateAnalysis(QWidget *parent) : MeasureAnalysisView(parent), ui(new Ui::CountRateAnalysis) { ui->setupUi(this); InitUi(); } CountRateAnalysis::~CountRateAnalysis() { delete ui; } void CountRateAnalysis::InitViewWorkspace(const QString &project_name) { } void CountRateAnalysis::SetAnalyzeDataFilename(const QMap &data_files_set) { if(!data_files_set.isEmpty()) { m_AllData = getParticleInjectTimeData(data_files_set.first().toString()); setData(m_AllData); } } void CountRateAnalysis::setData(QVector 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 x; QVector y; const ParticleInjectTime& lostdt = data.last(); const qint64 totalNanoseconds = lostdt.dTime * 5; const int nAllS = static_cast(totalNanoseconds / NS_PER_SECOND) + 1; const int nVecSize = nAllS / nInv; QVector 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(ns / nInv); // 整数除法,速度快 if (nidx < nVecSize) ++vec[nidx]; // 使用 ++ 而非 vec[nidx]++ } QVector vecXpt(vec.size()); for (int i = 0; i < vec.size(); ++i) vecXpt[i] = static_cast(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 CountRateAnalysis::getParticleInjectTimeData(QString path) { QVector records; QFile file(path); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { qWarning() << "无法打开文件:" << file.errorString(); return records; } QTextStream stream(&file); stream.setCodec("UTF-8"); int lineNumber = 0; while (!stream.atEnd()) { QString line = stream.readLine().trimmed(); lineNumber++; // 跳过空行 if (line.isEmpty()) continue; // 按逗号分割 QStringList fields = line.split(','); // 检查字段数量是否正确(应该为4) if (fields.size() != 4) { qWarning() << "行" << lineNumber << "字段数量不正确,跳过:" << line; continue; } //获取板卡号通道号 int bd = fields[0].toInt(); int ch = fields[1].toInt(); int detector = bd + ch * 8; if(detector >= 32) { continue; } bool ok; ParticleInjectTime rec; rec.bd = bd; rec.ch = ch; rec.index = lineNumber; rec.dEnergy = fields[2].toDouble(&ok); rec.dTime = fields[3].toLongLong(&ok); records.append(rec); } file.close(); return records; } void CountRateAnalysis::InitUi() { plot = new CustomQwtPlot(); plot->SetXaxisDragScale(true); setupPlot(); ui->verticalLayout_2->addWidget(plot); } void CountRateAnalysis::setupPlot() { plot->setCanvasBackground(Qt::white); QwtPlotCanvas* canvas = qobject_cast(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); // 设置QWT图例 // QwtLegend* legend = new QwtLegend(); // legend->setDefaultItemMode(QwtLegendData::ReadOnly); // plot->insertLegend(legend, QwtPlot::RightLegend); plot->SetXaxisDragScale(true); }