File indexing completed on 2024-12-22 03:35:49

0001 /*
0002     File                 : ROOTFilter.h
0003     Project              : LabPlot
0004     Description          : ROOT(CERN) I/O-filter
0005     --------------------------------------------------------------------
0006     SPDX-FileCopyrightText: 2018 Christoph Roick <chrisito@gmx.de>
0007     SPDX-FileCopyrightText: 2022 Stefan Gerlach <stefan.gerlach@uni.kn>
0008     SPDX-License-Identifier: GPL-2.0-or-later
0009 */
0010 
0011 #ifndef ROOTFILTER_H
0012 #define ROOTFILTER_H
0013 
0014 #include "backend/datasources/filters/AbstractFileFilter.h"
0015 
0016 class ROOTFilterPrivate;
0017 class QIODevice;
0018 
0019 /// Manages the importing of histograms from ROOT files
0020 class ROOTFilter : public AbstractFileFilter {
0021     Q_OBJECT
0022 
0023 public:
0024     ROOTFilter();
0025     ~ROOTFilter() override;
0026 
0027     // UNUSED enum class ColumnTypes {Center = 1, Low = 2, Content = 4, Error = 8};
0028 
0029     static QString fileInfoString(const QString&);
0030 
0031     /**
0032      * @brief Read data from the currently selected histogram
0033      *
0034      * The ROOT file is kept open until the file name is changed
0035      */
0036     void
0037     readDataFromFile(const QString& fileName, AbstractDataSource* = nullptr, AbstractFileFilter::ImportMode = AbstractFileFilter::ImportMode::Replace) override;
0038     /// Currently writing to ROOT files is not supported
0039     void write(const QString& fileName, AbstractDataSource*) override;
0040 
0041     /// Internal directory structure in a ROOT file
0042     struct Directory {
0043         QString name;
0044         QVector<QPair<QString, quint64>> content;
0045         QVector<Directory> children;
0046     };
0047 
0048     /// List names of histograms contained in ROOT file
0049     Directory listHistograms(const QString& fileName) const;
0050     /// List names of trees contained in ROOT file
0051     Directory listTrees(const QString& fileName) const;
0052     /// List names of leaves contained in ROOT tree
0053     QVector<QStringList> listLeaves(const QString& fileName, qint64 pos) const;
0054 
0055     /// Set the current histograms, which is one out of listHistograms
0056     void setCurrentObject(const QString&);
0057     /// Get the name of the currently set object
0058     const QString currentObject() const;
0059 
0060     /// Get preview data of the currently set object
0061     QVector<QStringList> previewCurrentObject(const QString& fileName, int first, int last) const;
0062 
0063     /// Get the number of rows in the current object
0064     int rowsInCurrentObject(const QString& fileName) const;
0065 
0066     /**
0067      * @brief Set the last bin of the object to be read
0068      *
0069      * -1 skips the underflow bin of histograms
0070      */
0071     void setStartRow(const int bin);
0072     /// Get the index of the first row to be read
0073     int startRow() const;
0074     /**
0075      * @brief Set the last row of the object to be read
0076      *
0077      * -1 skips the overflow bin of histograms
0078      */
0079     void setEndRow(const int bin);
0080     /// Get the index of the last row to be read
0081     int endRow() const;
0082 
0083     /**
0084      * @brief Set the columns of the object to be read
0085      *
0086      * For histograms the following are available: center, low, content, error
0087      */
0088     void setColumns(const QVector<QStringList>& columns);
0089     /**
0090      * @brief Get the columns to be read
0091      *
0092      * For histograms, the identifiers for location, content and error are given
0093      * as the first part, the corresponding translation as the second part.
0094      * For trees, the branch name and the leaf name are returned.
0095      *
0096      * @return A pair of strings with different content depending on the object type
0097      */
0098     QVector<QStringList> columns() const;
0099 
0100     /// Save bin limitation settings
0101     void save(QXmlStreamWriter*) const override;
0102     /// Load bin limitation settings
0103     bool load(XmlStreamReader*) override;
0104 
0105 private:
0106     std::unique_ptr<ROOTFilterPrivate> const d;
0107     friend class ROOTFilterPrivate;
0108 };
0109 
0110 #endif