Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:10:35

0001 // Author: Enrico Guiraud, 2021
0002 
0003 /*************************************************************************
0004  * Copyright (C) 1995-2021, Rene Brun and Fons Rademakers.               *
0005  * All rights reserved.                                                  *
0006  *                                                                       *
0007  * For the licensing terms see $ROOTSYS/LICENSE.                         *
0008  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
0009  *************************************************************************/
0010 
0011 #ifndef ROOT_RDF_RSAMPLEINFO
0012 #define ROOT_RDF_RSAMPLEINFO
0013 
0014 #include <ROOT/RDF/RSample.hxx>
0015 #include <string_view>
0016 #include <Rtypes.h>
0017 
0018 #include <functional>
0019 #include <stdexcept>
0020 #include <string>
0021 
0022 #include <tuple>
0023 
0024 namespace ROOT {
0025 namespace RDF {
0026 
0027 /// This type represents a sample identifier, to be used in conjunction with RDataFrame features such as
0028 /// \ref ROOT::RDF::RInterface< Proxied, DS_t >::DefinePerSample "DefinePerSample()" and per-sample callbacks.
0029 ///
0030 /// When the input data comes from a TTree, the string representation of RSampleInfo (which is returned by AsString()
0031 /// and that can be queried e.g. with Contains()) is of the form "<filename>/<treename>".
0032 ///
0033 /// In multi-thread runs, different tasks might process different entry ranges of the same sample,
0034 /// so RSampleInfo also provides methods to inspect which part of a sample is being taken into consideration.
0035 class RSampleInfo {
0036    std::string fID;
0037    std::pair<ULong64_t, ULong64_t> fEntryRange;
0038 
0039    const ROOT::RDF::Experimental::RSample *fSample = nullptr; // non-owning
0040 
0041    void ThrowIfNoSample() const
0042    {
0043       if (fSample == nullptr) {
0044          const auto msg = "RSampleInfo: sample data was requested but no samples are available.";
0045          throw std::logic_error(msg);
0046       }
0047    }
0048 
0049 public:
0050    RSampleInfo(std::string_view id, std::pair<ULong64_t, ULong64_t> entryRange,
0051                const ROOT::RDF::Experimental::RSample *sample = nullptr)
0052       : fID(id), fEntryRange(entryRange), fSample(sample)
0053    {
0054    }
0055    RSampleInfo() = default;
0056    RSampleInfo(const RSampleInfo &) = default;
0057    RSampleInfo &operator=(const RSampleInfo &) = default;
0058    RSampleInfo(RSampleInfo &&) = default;
0059    RSampleInfo &operator=(RSampleInfo &&) = default;
0060    ~RSampleInfo() = default;
0061 
0062    /// @brief Get the name of the sample as a string.
0063    const std::string &GetSampleName() const
0064    {
0065       ThrowIfNoSample();
0066       return fSample->GetSampleName();
0067    }
0068 
0069    /// @brief Get the sample id as an int.
0070    unsigned int GetSampleId() const
0071    {
0072       ThrowIfNoSample();
0073       return fSample->GetSampleId();
0074    }
0075 
0076    /// @brief Return the metadata value of type int given the key.
0077    int GetI(const std::string &key) const
0078    {
0079       ThrowIfNoSample();
0080       return fSample->GetMetaData().GetI(key);
0081    }
0082 
0083    /// @brief Return the metadata value of type double given the key.
0084    double GetD(const std::string &key) const
0085    {
0086       ThrowIfNoSample();
0087       return fSample->GetMetaData().GetD(key);
0088    }
0089 
0090    /// @brief Return the metadata value of type string given the key.
0091    std::string GetS(const std::string &key) const
0092    {
0093       ThrowIfNoSample();
0094       return fSample->GetMetaData().GetS(key);
0095    }
0096 
0097    /// @brief Check whether the sample name contains the given substring.
0098    bool Contains(std::string_view substr) const
0099    {
0100       // C++14 needs the conversion from std::string_view to std::string
0101       return fID.find(std::string(substr)) != std::string::npos;
0102    }
0103 
0104    /// @brief Check whether the sample name is empty.
0105    ///
0106    /// This is the case e.g. when using a RDataFrame with no input data, constructed as `RDataFrame(nEntries)`.
0107    bool Empty() const {
0108       return fID.empty();
0109    }
0110 
0111    /// @brief Return a string representation of the sample name.
0112    ///
0113    /// The representation is of the form "<filename>/<treename>" if the input data comes from a TTree or a TChain.
0114    const std::string &AsString() const
0115    {
0116       return fID;
0117    }
0118 
0119    /// @brief Return the entry range in the sample that is being taken into consideration.
0120    ///
0121    /// Multiple multi-threading tasks might process different entry ranges of the same sample.
0122    std::pair<ULong64_t, ULong64_t> EntryRange() const { return fEntryRange; }
0123 
0124    /// @brief Return the number of entries of this sample that is being taken into consideration.
0125    ULong64_t NEntries() const { return fEntryRange.second - fEntryRange.first; }
0126 
0127    bool operator==(const RSampleInfo &other) const { return fID == other.fID; }
0128    bool operator!=(const RSampleInfo &other) const { return !(*this == other); }
0129 };
0130 
0131 /// The type of a data-block callback, registered with an RDataFrame computation graph via e.g.  \ref
0132 /// ROOT::RDF::RInterface< Proxied, DS_t >::DefinePerSample "DefinePerSample()" or by certain actions (e.g. \ref
0133 /// ROOT::RDF::RInterface<Proxied,DataSource>::Snapshot "Snapshot()").
0134 using SampleCallback_t = std::function<void(unsigned int, const ROOT::RDF::RSampleInfo &)>;
0135 
0136 } // namespace RDF
0137 } // namespace ROOT
0138 
0139 #endif