2023-06-02 18:13:47 -04:00
|
|
|
#ifndef HISTOGRAM_1D_H
|
|
|
|
#define HISTOGRAM_1D_H
|
|
|
|
|
|
|
|
#include "qcustomplot.h"
|
2024-03-22 16:47:33 -04:00
|
|
|
#include "macro.h"
|
2023-06-02 18:13:47 -04:00
|
|
|
|
2023-06-23 13:50:21 -04:00
|
|
|
#define MaxNHist 10
|
|
|
|
|
2023-06-02 18:13:47 -04:00
|
|
|
//^==============================================
|
|
|
|
//^==============================================
|
|
|
|
class Histogram1D : public QCustomPlot{
|
2023-06-05 19:06:07 -04:00
|
|
|
Q_OBJECT
|
2023-06-02 18:13:47 -04:00
|
|
|
public:
|
|
|
|
Histogram1D(QString title, QString xLabel, int xbin, double xmin, double xmax, QWidget * parent = nullptr) : QCustomPlot(parent){
|
2024-03-22 17:20:07 -04:00
|
|
|
// DebugPrint("%s", "Histogram1D");
|
2023-12-14 15:45:02 -05:00
|
|
|
isLogY = false;
|
|
|
|
|
2024-08-20 12:22:26 -04:00
|
|
|
for( int i = 0; i < MaxNHist; i++ ) showHist[i] = true;
|
|
|
|
|
2023-06-09 13:51:02 -04:00
|
|
|
for( int i = 0; i < 3; i ++) txt[i] = nullptr;
|
2023-06-23 13:50:21 -04:00
|
|
|
nData = 1;
|
2023-06-02 18:13:47 -04:00
|
|
|
Rebin(xbin, xmin, xmax);
|
|
|
|
|
|
|
|
xAxis->setLabel(xLabel);
|
|
|
|
|
|
|
|
legend->setVisible(true);
|
|
|
|
QPen borderPen = legend->borderPen();
|
|
|
|
borderPen.setWidth(0);
|
|
|
|
borderPen.setColor(Qt::transparent);
|
|
|
|
legend->setBorderPen(borderPen);
|
|
|
|
legend->setFont(QFont("Helvetica", 9));
|
|
|
|
|
|
|
|
addGraph();
|
|
|
|
graph(0)->setName(title);
|
|
|
|
graph(0)->setPen(QPen(Qt::blue));
|
|
|
|
graph(0)->setBrush(QBrush(QColor(0, 0, 255, 20)));
|
|
|
|
|
|
|
|
xAxis2->setVisible(true);
|
|
|
|
xAxis2->setTickLabels(false);
|
|
|
|
yAxis2->setVisible(true);
|
|
|
|
yAxis2->setTickLabels(false);
|
|
|
|
// make left and bottom axes always transfer their ranges to right and top axes:
|
|
|
|
connect(xAxis, SIGNAL(rangeChanged(QCPRange)), xAxis2, SLOT(setRange(QCPRange)));
|
|
|
|
connect(yAxis, SIGNAL(rangeChanged(QCPRange)), yAxis2, SLOT(setRange(QCPRange)));
|
|
|
|
|
2023-06-23 13:50:21 -04:00
|
|
|
graph(0)->setData(xList, yList[0]);
|
2023-06-02 18:13:47 -04:00
|
|
|
|
|
|
|
//setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
|
|
|
|
//setInteractions( QCP::iRangeDrag | QCP::iRangeZoom );
|
|
|
|
|
|
|
|
//setSelectionRectMode(QCP::SelectionRectMode::srmZoom);
|
|
|
|
|
|
|
|
rescaleAxes();
|
|
|
|
yAxis->setRangeLower(0);
|
|
|
|
yAxis->setRangeUpper(10);
|
|
|
|
|
|
|
|
for( int i = 0; i < 3; i ++){
|
|
|
|
txt[i] = new QCPItemText(this);
|
|
|
|
txt[i]->setPositionAlignment(Qt::AlignLeft);
|
|
|
|
txt[i]->position->setType(QCPItemPosition::ptAxisRectRatio);
|
|
|
|
txt[i]->position->setCoords(0.1, 0.1 + 0.1*i);;
|
|
|
|
txt[i]->setFont(QFont("Helvetica", 9));
|
|
|
|
}
|
2023-06-09 13:51:02 -04:00
|
|
|
txt[0]->setText("Under Flow : 0");
|
|
|
|
txt[1]->setText("Total Entry : 0");
|
|
|
|
txt[2]->setText("Over Flow : 0");
|
2023-06-02 18:13:47 -04:00
|
|
|
|
|
|
|
usingMenu = false;
|
|
|
|
|
|
|
|
connect(this, &QCustomPlot::mouseMove, this, [=](QMouseEvent *event){
|
|
|
|
double x = this->xAxis->pixelToCoord(event->pos().x());
|
|
|
|
double bin = (x - xMin)/dX;
|
2023-06-23 13:50:21 -04:00
|
|
|
double z = yList[0][2*qFloor(bin) + 1];
|
2023-06-02 18:13:47 -04:00
|
|
|
|
|
|
|
QString coordinates = QString("Bin: %1, Value: %2").arg(qFloor(bin)).arg(z);
|
|
|
|
QToolTip::showText(event->globalPosition().toPoint(), coordinates, this);
|
|
|
|
});
|
|
|
|
|
|
|
|
connect(this, &QCustomPlot::mousePress, this, [=](QMouseEvent * event){
|
|
|
|
if (event->button() == Qt::LeftButton && !usingMenu){
|
|
|
|
setSelectionRectMode(QCP::SelectionRectMode::srmZoom);
|
|
|
|
}
|
|
|
|
if (event->button() == Qt::RightButton) {
|
|
|
|
usingMenu = true;
|
|
|
|
setSelectionRectMode(QCP::SelectionRectMode::srmNone);
|
|
|
|
|
|
|
|
QMenu menu(this);
|
|
|
|
|
|
|
|
QAction * a1 = menu.addAction("UnZoom");
|
2023-12-14 15:45:02 -05:00
|
|
|
QAction * a5 = menu.addAction("Set/UnSet Log-y");
|
2024-08-20 12:22:26 -04:00
|
|
|
QAction * a6 = nullptr;
|
|
|
|
if( nData > 1 ) a6 = menu.addAction("Toggle lines display");
|
2023-06-02 18:13:47 -04:00
|
|
|
QAction * a2 = menu.addAction("Clear hist.");
|
|
|
|
QAction * a3 = menu.addAction("Toggle Stat.");
|
2023-06-05 19:06:07 -04:00
|
|
|
QAction * a4 = menu.addAction("Rebin (clear histogram)");
|
2023-06-02 18:13:47 -04:00
|
|
|
//TODO fitGuass
|
|
|
|
|
|
|
|
QAction *selectedAction = menu.exec(event->globalPosition().toPoint());
|
2024-08-20 12:22:26 -04:00
|
|
|
//*========================================== UnZoom
|
2023-06-02 18:13:47 -04:00
|
|
|
if( selectedAction == a1 ){
|
|
|
|
xAxis->setRangeLower(xMin);
|
|
|
|
xAxis->setRangeUpper(xMax);
|
|
|
|
yAxis->setRangeLower(0);
|
|
|
|
yAxis->setRangeUpper(yMax * 1.2 > 10 ? yMax * 1.2 : 10);
|
|
|
|
replot();
|
|
|
|
usingMenu = false;
|
|
|
|
}
|
|
|
|
|
2024-08-20 12:22:26 -04:00
|
|
|
//*========================================== Clear Hist
|
2023-06-02 18:13:47 -04:00
|
|
|
if( selectedAction == a2 ){
|
|
|
|
Clear();
|
|
|
|
usingMenu = false;
|
|
|
|
}
|
|
|
|
|
2024-08-20 12:22:26 -04:00
|
|
|
//*========================================== Toggle Stat.
|
2023-06-02 18:13:47 -04:00
|
|
|
if( selectedAction == a3 ){
|
|
|
|
for( int i = 0; i < 3; i++){
|
|
|
|
txt[i]->setVisible( !txt[i]->visible());
|
|
|
|
}
|
|
|
|
replot();
|
|
|
|
usingMenu = false;
|
|
|
|
}
|
2024-08-20 12:22:26 -04:00
|
|
|
//*========================================== Rebin
|
2023-06-05 19:06:07 -04:00
|
|
|
if( selectedAction == a4 ){
|
|
|
|
QDialog dialog(this);
|
|
|
|
dialog.setWindowTitle("Rebin histogram");
|
|
|
|
|
|
|
|
QFormLayout layout(&dialog);
|
|
|
|
|
|
|
|
QLabel * info = new QLabel(&dialog);
|
|
|
|
info->setStyleSheet("color:red;");
|
|
|
|
info->setText("This will also clear histogram!!");
|
|
|
|
layout.addRow(info);
|
|
|
|
|
|
|
|
QStringList nameList = {"Num. Bin", "x-Min", "x-Max"};
|
|
|
|
QLineEdit* lineEdit[3];
|
|
|
|
|
|
|
|
for (int i = 0; i < 3; ++i) {
|
|
|
|
lineEdit[i] = new QLineEdit(&dialog);
|
|
|
|
layout.addRow(nameList[i] + " : ", lineEdit[i]);
|
|
|
|
}
|
2023-06-09 13:51:02 -04:00
|
|
|
lineEdit[0]->setText(QString::number(xBin));
|
|
|
|
lineEdit[1]->setText(QString::number(xMin));
|
|
|
|
lineEdit[2]->setText(QString::number(xMax));
|
2023-06-05 19:06:07 -04:00
|
|
|
|
|
|
|
QLabel * msg = new QLabel(&dialog);
|
|
|
|
msg->setStyleSheet("color:red;");
|
|
|
|
layout.addRow(msg);
|
|
|
|
|
|
|
|
QDialogButtonBox buttonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, &dialog);
|
|
|
|
layout.addRow(&buttonBox);
|
|
|
|
|
|
|
|
double number[3];
|
|
|
|
|
|
|
|
QObject::connect(&buttonBox, &QDialogButtonBox::accepted, [&]() {
|
2024-08-20 12:22:26 -04:00
|
|
|
int OKcount = 0;
|
|
|
|
bool conversionOk = true;
|
|
|
|
for( int i = 0; i < 3; i++ ){
|
|
|
|
number[i] = lineEdit[i]->text().toDouble(&conversionOk);
|
|
|
|
if( conversionOk ){
|
|
|
|
OKcount++;
|
|
|
|
}else{
|
|
|
|
msg->setText(nameList[i] + " is invalid.");
|
|
|
|
return;
|
2023-06-05 19:06:07 -04:00
|
|
|
}
|
2024-08-20 12:22:26 -04:00
|
|
|
}
|
2023-06-05 19:06:07 -04:00
|
|
|
|
2024-08-20 12:22:26 -04:00
|
|
|
if( OKcount == 3 ) {
|
|
|
|
if( number[2] > number[1] ) {
|
|
|
|
dialog.accept();
|
|
|
|
}else{
|
|
|
|
msg->setText(nameList[2] + " is smaller than " + nameList[1]);
|
2023-06-05 19:06:07 -04:00
|
|
|
}
|
2024-08-20 12:22:26 -04:00
|
|
|
}
|
2023-06-05 19:06:07 -04:00
|
|
|
});
|
|
|
|
QObject::connect(&buttonBox, &QDialogButtonBox::rejected, [&]() { dialog.reject();});
|
|
|
|
|
|
|
|
if( dialog.exec() == QDialog::Accepted ){
|
|
|
|
Rebin((int)number[0], number[1], number[2]);
|
|
|
|
emit ReBinned();
|
|
|
|
UpdatePlot();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2023-12-14 15:45:02 -05:00
|
|
|
|
2024-08-20 12:22:26 -04:00
|
|
|
//*========================================== Toggle line Display
|
|
|
|
if( selectedAction == a6 ){
|
|
|
|
QDialog dialog(this);
|
|
|
|
dialog.setWindowTitle("Toggle lines Display");
|
|
|
|
|
|
|
|
QFormLayout layout(&dialog);
|
|
|
|
|
|
|
|
QCheckBox ** cbline = new QCheckBox *[nData];
|
|
|
|
for( int i = 0; i < nData; i++ ){
|
|
|
|
cbline[i] = new QCheckBox(graph(i)->name(), &dialog);
|
|
|
|
layout.addRow(cbline[i]);
|
|
|
|
if( showHist[i] ) cbline[i]->setChecked(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
QDialogButtonBox buttonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel, Qt::Horizontal, &dialog);
|
|
|
|
layout.addRow(&buttonBox);
|
|
|
|
|
|
|
|
QObject::connect(&buttonBox, &QDialogButtonBox::accepted, [&]() {
|
|
|
|
for( int i = 0; i < nData; i++ ){
|
|
|
|
showHist[i] = cbline[i]->isChecked();
|
|
|
|
}
|
|
|
|
dialog.accept();
|
|
|
|
});
|
|
|
|
QObject::connect(&buttonBox, &QDialogButtonBox::rejected, [&]() { dialog.reject();});
|
|
|
|
|
|
|
|
if( dialog.exec() == QDialog::Accepted ){
|
|
|
|
UpdatePlot();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//*========================================== Set Log y
|
|
|
|
if( selectedAction == a5 ){
|
2023-12-14 15:45:02 -05:00
|
|
|
if( !isLogY ){
|
|
|
|
this->yAxis->setScaleType(QCPAxis::stLogarithmic);
|
|
|
|
isLogY = true;
|
|
|
|
}else{
|
|
|
|
this->yAxis->setScaleType(QCPAxis::stLinear);
|
|
|
|
isLogY = false;
|
|
|
|
}
|
|
|
|
this->replot();
|
|
|
|
}
|
2023-06-02 18:13:47 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int GetNBin() const {return xBin;}
|
|
|
|
double GetXMin() const {return xMin;}
|
|
|
|
double GetXMax() const {return xMax;}
|
|
|
|
|
2023-06-23 13:50:21 -04:00
|
|
|
void SetColor(QColor color, unsigned short ID = 0) {
|
2024-03-22 16:47:33 -04:00
|
|
|
DebugPrint("%s", "Histogram1D");
|
2023-06-23 13:50:21 -04:00
|
|
|
graph(ID)->setPen(QPen(color));
|
|
|
|
QColor haha = color;
|
|
|
|
haha.setAlpha(20);
|
|
|
|
graph(ID)->setBrush(QBrush(haha));
|
|
|
|
}
|
|
|
|
void AddDataList(QString title, QColor color){
|
|
|
|
nData ++;
|
|
|
|
addGraph();
|
|
|
|
graph(nData - 1)->setName(title);
|
|
|
|
SetColor(color, nData-1);
|
2024-08-23 18:26:56 -04:00
|
|
|
yList[nData-1].clear();
|
|
|
|
for( int i = 0; i < xList.count(); i++) yList[nData-1].append(0);
|
2023-06-23 13:50:21 -04:00
|
|
|
}
|
|
|
|
|
2023-06-02 18:13:47 -04:00
|
|
|
void UpdatePlot(){
|
2024-03-22 16:47:33 -04:00
|
|
|
DebugPrint("%s", "Histogram1D");
|
2024-08-20 12:22:26 -04:00
|
|
|
for( int ID = 0 ; ID < nData; ID ++) {
|
|
|
|
graph(ID)->setVisible(showHist[ID]);
|
|
|
|
graph(ID)->setData(xList, yList[ID]);
|
|
|
|
}
|
2023-06-02 18:13:47 -04:00
|
|
|
xAxis->setRangeLower(xMin);
|
|
|
|
xAxis->setRangeUpper(xMax);
|
|
|
|
yAxis->setRangeLower(0);
|
|
|
|
yAxis->setRangeUpper(yMax * 1.2 > 10 ? yMax * 1.2 : 10);
|
|
|
|
replot();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Clear(){
|
2024-03-22 16:47:33 -04:00
|
|
|
DebugPrint("%s", "Histogram1D");
|
2023-06-23 13:50:21 -04:00
|
|
|
for( int ID = 0 ; ID < nData; ID ++) {
|
2024-08-23 18:26:56 -04:00
|
|
|
for( int i = 0; i < xList.count(); i++) yList[ID][i] = 0;
|
2023-06-23 13:50:21 -04:00
|
|
|
}
|
2023-06-02 18:13:47 -04:00
|
|
|
yMax = 0;
|
|
|
|
txt[0]->setText("Under Flow : 0");
|
|
|
|
txt[1]->setText("Total Entry : 0");
|
|
|
|
txt[2]->setText("Over Flow : 0");
|
2023-08-16 17:54:35 -04:00
|
|
|
totalEntry = 0;
|
|
|
|
underFlow = 0;
|
|
|
|
overFlow = 0;
|
2023-06-02 18:13:47 -04:00
|
|
|
UpdatePlot();
|
|
|
|
}
|
2023-11-16 19:20:29 -05:00
|
|
|
|
2024-09-30 18:38:48 -04:00
|
|
|
void SetLineTitle(QString title, int lineID = 0) { graph(lineID)->setName(title); }
|
2024-06-26 17:35:09 -04:00
|
|
|
void SetXTitle(QString xTitle) { xAxis->setLabel(xTitle);}
|
2023-06-02 18:13:47 -04:00
|
|
|
|
|
|
|
void Rebin(int xbin, double xmin, double xmax){
|
2024-03-22 17:20:07 -04:00
|
|
|
// DebugPrint("%s", "Histogram1D");
|
2023-06-02 18:13:47 -04:00
|
|
|
xMin = xmin;
|
|
|
|
xMax = xmax;
|
|
|
|
xBin = xbin;
|
2024-09-30 18:38:48 -04:00
|
|
|
if( xBin > 1000) xBin = 1000;
|
2023-06-02 18:13:47 -04:00
|
|
|
|
|
|
|
dX = (xMax - xMin)/(xBin);
|
|
|
|
|
|
|
|
xList.clear();
|
2023-06-23 13:50:21 -04:00
|
|
|
for( int i = 0 ; i < nData ; i ++) yList[i].clear();
|
2023-06-02 18:13:47 -04:00
|
|
|
|
|
|
|
for( int i = 0; i <= xBin; i ++ ){
|
|
|
|
xList.append(xMin + i*dX-(dX)*0.000001);
|
|
|
|
xList.append(xMin + i*dX);
|
2023-06-23 13:50:21 -04:00
|
|
|
for( int ID = 0 ; ID < nData; ID ++ ){
|
|
|
|
yList[ID].append(0);
|
|
|
|
yList[ID].append(0);
|
|
|
|
}
|
2023-06-02 18:13:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
yMax = 0;
|
|
|
|
|
|
|
|
totalEntry = 0;
|
|
|
|
underFlow = 0;
|
|
|
|
overFlow = 0;
|
2023-06-09 13:51:02 -04:00
|
|
|
|
|
|
|
if( txt[0] ) txt[0]->setText("Under Flow : 0");
|
|
|
|
if( txt[1] ) txt[1]->setText("Total Entry : 0");
|
|
|
|
if( txt[2] ) txt[2]->setText("Over Flow : 0");
|
2023-06-02 18:13:47 -04:00
|
|
|
}
|
|
|
|
|
2023-06-23 13:50:21 -04:00
|
|
|
void Fill(double value, unsigned int ID = 0){
|
2024-03-22 16:47:33 -04:00
|
|
|
// DebugPrint("%s", "Histogram1D");
|
2023-06-23 13:50:21 -04:00
|
|
|
if( ID == 0 ){
|
|
|
|
totalEntry ++;
|
|
|
|
txt[1]->setText("Total Entry : "+ QString::number(totalEntry));
|
2023-06-02 18:13:47 -04:00
|
|
|
|
2023-06-23 13:50:21 -04:00
|
|
|
if( value < xMin ) {
|
|
|
|
underFlow ++;
|
|
|
|
txt[0]->setText("Under Flow : "+ QString::number(underFlow));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if( value > xMax ) {
|
|
|
|
overFlow ++;
|
|
|
|
txt[2]->setText("Over Flow : "+ QString::number(overFlow));
|
|
|
|
return;
|
|
|
|
}
|
2024-08-23 18:26:56 -04:00
|
|
|
}else{
|
|
|
|
if( value < xMin || value > xMax ) return;
|
2023-06-02 18:13:47 -04:00
|
|
|
}
|
|
|
|
|
2024-08-23 18:26:56 -04:00
|
|
|
int bin = qFloor((value - xMin)/dX);
|
|
|
|
int index1 = 2*bin + 1;
|
2023-06-02 18:13:47 -04:00
|
|
|
int index2 = index1 + 1;
|
|
|
|
|
2023-06-23 13:50:21 -04:00
|
|
|
if( 0 <= index1 && index1 <= 2*xBin) yList[ID][index1] += 1;
|
|
|
|
if( 0 <= index1 && index2 <= 2*xBin) yList[ID][index2] += 1;
|
2023-06-02 18:13:47 -04:00
|
|
|
|
2024-08-20 12:31:36 -04:00
|
|
|
if( showHist[ID] && yList[ID][index1] > yMax ) yMax = yList[ID][index1];
|
2023-06-02 18:13:47 -04:00
|
|
|
}
|
|
|
|
|
2023-06-23 13:50:21 -04:00
|
|
|
void Print(unsigned int ID = 0){
|
2023-06-02 18:13:47 -04:00
|
|
|
for( int i = 0; i < xList.count(); i++){
|
2023-06-23 13:50:21 -04:00
|
|
|
printf("%f %f\n", xList[i], yList[ID][i]);
|
2023-06-02 18:13:47 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-05 19:06:07 -04:00
|
|
|
signals:
|
|
|
|
void ReBinned(); //ONLY for right click rebin
|
|
|
|
|
2023-06-02 18:13:47 -04:00
|
|
|
private:
|
|
|
|
double xMin, xMax, dX;
|
|
|
|
int xBin;
|
|
|
|
|
|
|
|
double yMax;
|
|
|
|
|
|
|
|
int totalEntry;
|
|
|
|
int underFlow;
|
|
|
|
int overFlow;
|
|
|
|
|
2023-12-14 15:45:02 -05:00
|
|
|
bool isLogY;
|
|
|
|
|
2023-06-23 13:50:21 -04:00
|
|
|
unsigned short nData;
|
|
|
|
QVector<double> xList;
|
|
|
|
QVector<double> yList[MaxNHist];
|
2023-06-02 18:13:47 -04:00
|
|
|
|
|
|
|
QCPItemText * txt[3];
|
|
|
|
|
|
|
|
bool usingMenu;
|
|
|
|
|
2024-08-20 12:22:26 -04:00
|
|
|
bool showHist[MaxNHist];
|
|
|
|
|
2023-06-23 13:50:21 -04:00
|
|
|
|
2023-06-02 18:13:47 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|