File indexing completed on 2024-10-13 06:30:12

0001 /*
0002     File                 : ROOTFilterTest.cpp
0003     Project              : LabPlot
0004     Description          : Tests for the ROOT filter
0005     --------------------------------------------------------------------
0006     SPDX-FileCopyrightText: 2022 Stefan Gerlach <stefan.gerlach@uni.kn>
0007 
0008     SPDX-License-Identifier: GPL-2.0-or-later
0009 */
0010 
0011 #include "ROOTFilterTest.h"
0012 #include "backend/datasources/filters/ROOTFilter.h"
0013 #include "backend/spreadsheet/Spreadsheet.h"
0014 
0015 #include <gsl/gsl_randist.h>
0016 #include <gsl/gsl_rng.h>
0017 
0018 void ROOTFilterTest::importFile1() {
0019     const QString& fileName = QFINDTESTDATA(QLatin1String("data/advanced_zlib.root"));
0020 
0021     Spreadsheet spreadsheet(QStringLiteral("test"), false);
0022     ROOTFilter filter;
0023     filter.setStartRow(1);
0024     // filter.setEndRow(100);
0025     filter.setCurrentObject(QStringLiteral("Hist:variableBinHist;2"));
0026     QVector<QStringList> columns{{QStringLiteral("center")}, {QStringLiteral("content")}, {QStringLiteral("error")}};
0027     filter.setColumns(columns);
0028     filter.readDataFromFile(fileName, &spreadsheet);
0029 
0030     QCOMPARE(spreadsheet.columnCount(), 3);
0031     QCOMPARE(spreadsheet.rowCount(), 101);
0032 
0033     //  WARN(spreadsheet.column(0)->valueAt(0))
0034     QCOMPARE(spreadsheet.column(0)->valueAt(0), -4.95495);
0035     QCOMPARE(spreadsheet.column(1)->valueAt(0), 0);
0036     QCOMPARE(spreadsheet.column(1)->valueAt(0), 0);
0037     QCOMPARE(spreadsheet.column(0)->valueAt(1), -4.86475);
0038     QCOMPARE(spreadsheet.column(1)->valueAt(1), 0);
0039     QCOMPARE(spreadsheet.column(2)->valueAt(1), 0);
0040     QCOMPARE(spreadsheet.column(0)->valueAt(99), 4.94505);
0041     QCOMPARE(spreadsheet.column(1)->valueAt(99), 0);
0042     QCOMPARE(spreadsheet.column(2)->valueAt(99), 0);
0043     QCOMPARE(spreadsheet.column(0)->valueAt(100), INFINITY);
0044     QCOMPARE(spreadsheet.column(1)->valueAt(100), 0);
0045     QCOMPARE(spreadsheet.column(2)->valueAt(100), 0);
0046 
0047     ROOTFilter filter2;
0048     filter2.setStartRow(0);
0049     filter2.setEndRow(9); // TODO: automatic?
0050     filter2.setCurrentObject(QStringLiteral("Tree:tree"));
0051     QVector<QStringList> columns2{{QStringLiteral("doubleTest")}, {QStringLiteral("structTest"), QStringLiteral("double")}};
0052     filter2.setColumns(columns2);
0053     filter2.readDataFromFile(fileName, &spreadsheet);
0054 
0055     QCOMPARE(spreadsheet.columnCount(), 2);
0056     QCOMPARE(spreadsheet.rowCount(), 10);
0057 
0058     QCOMPARE(spreadsheet.column(0)->valueAt(0), 0);
0059     QCOMPARE(spreadsheet.column(1)->valueAt(0), 81);
0060     QCOMPARE(spreadsheet.column(0)->valueAt(1), 1);
0061     QCOMPARE(spreadsheet.column(1)->valueAt(1), 64);
0062     QCOMPARE(spreadsheet.column(0)->valueAt(8), 8);
0063     QCOMPARE(spreadsheet.column(1)->valueAt(8), 1);
0064     QCOMPARE(spreadsheet.column(0)->valueAt(9), 9);
0065     QCOMPARE(spreadsheet.column(1)->valueAt(9), 0);
0066 }
0067 
0068 void ROOTFilterTest::importFile2() {
0069     const QString& fileName = QFINDTESTDATA(QLatin1String("data/basic_lz4.root"));
0070 
0071     Spreadsheet spreadsheet(QStringLiteral("test"), false);
0072     ROOTFilter filter;
0073     filter.setStartRow(1);
0074     // filter.setEndRow(100);
0075     filter.setCurrentObject(QStringLiteral("Hist:doubleHist;1"));
0076     QVector<QStringList> columns{{QStringLiteral("center")}, {QStringLiteral("content")}, {QStringLiteral("error")}};
0077     filter.setColumns(columns);
0078     filter.readDataFromFile(fileName, &spreadsheet);
0079 
0080     QCOMPARE(spreadsheet.columnCount(), 3);
0081     QCOMPARE(spreadsheet.rowCount(), 101);
0082 
0083     QCOMPARE(spreadsheet.column(0)->valueAt(0), -4.95);
0084     QCOMPARE(spreadsheet.column(1)->valueAt(0), 0);
0085     QCOMPARE(spreadsheet.column(1)->valueAt(0), 0);
0086     QCOMPARE(spreadsheet.column(0)->valueAt(1), -4.85);
0087     QCOMPARE(spreadsheet.column(1)->valueAt(1), 0);
0088     QCOMPARE(spreadsheet.column(2)->valueAt(1), 0);
0089     QCOMPARE(spreadsheet.column(0)->valueAt(99), 4.95);
0090     QCOMPARE(spreadsheet.column(1)->valueAt(99), 0);
0091     QCOMPARE(spreadsheet.column(2)->valueAt(99), 0);
0092     QCOMPARE(spreadsheet.column(0)->valueAt(100), INFINITY);
0093     QCOMPARE(spreadsheet.column(1)->valueAt(100), 0);
0094     QCOMPARE(spreadsheet.column(2)->valueAt(100), 0);
0095 }
0096 
0097 // BENCHMARKS
0098 
0099 /*
0100 void ROOTFilterTest::benchDoubleImport_data() {
0101     QTest::addColumn<long>("lineCount");
0102     // can't transfer file name since needed in clean up
0103 
0104     QTemporaryFile file;
0105     if (!file.open()) // needed to generate file name
0106         return;
0107     file.close(); // only file name is used
0108 
0109     benchDataFileName = file.fileName();
0110     benchDataFileName.append(".fits");
0111 
0112     QString testName(QString::number(paths) + QLatin1String(" random double paths"));
0113 
0114     QTest::newRow(testName.toLatin1()) << lines;
0115     DEBUG("CREATE DATA FILE " << STDSTRING(benchDataFileName) << ", lines = " << lines)
0116 
0117     gsl_rng_env_setup();
0118     gsl_rng* r = gsl_rng_alloc(gsl_rng_default);
0119     gsl_rng_set(r, 12345);
0120 
0121     // create file
0122 
0123     // create data
0124     double path[paths] = {0.0};
0125     double* data = new double[paths * lines];
0126 
0127     const double delta = 0.25;
0128     const int dt = 1;
0129     const double sigma = delta * delta * dt;
0130     for (long i = 0; i < lines; ++i) {
0131         // std::cout << "line " << i+1 << std::endl;
0132 
0133         for (int p = 0; p < paths; ++p) {
0134             path[p] += gsl_ran_gaussian_ziggurat(r, sigma);
0135             data[p + i * paths] = path[p];
0136         }
0137     }
0138 
0139     // write data and clean up
0140 
0141     delete[] data;
0142 
0143     DEBUG(Q_FUNC_INFO << ", DONE")
0144 }
0145 
0146 void ROOTFilterTest::benchDoubleImport() {
0147     Spreadsheet spreadsheet("test", false);
0148     ROOTFilter filter;
0149 
0150     const int p = paths; // need local variable
0151     QBENCHMARK {
0152         filter.readDataFromFile(benchDataFileName, &spreadsheet);
0153 
0154         QCOMPARE(spreadsheet.columnCount(), p);
0155         QCOMPARE(spreadsheet.rowCount(), lines);
0156 
0157         QCOMPARE(spreadsheet.column(0)->valueAt(0), 0.120997813055);
0158         QCOMPARE(spreadsheet.column(1)->valueAt(0), 0.119301077563219);
0159         QCOMPARE(spreadsheet.column(2)->valueAt(0), -0.0209979608555485);
0160     }
0161 }
0162 
0163 void ROOTFilterTest::benchDoubleImport_cleanup() {
0164     DEBUG("REMOVE DATA FILE " << STDSTRING(benchDataFileName))
0165     QFile::remove(benchDataFileName);
0166 }
0167 */
0168 
0169 QTEST_MAIN(ROOTFilterTest)