Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2025-08-12 02:28:50

0001 #ifndef DataFormats_L1Scouting_OrbitFlatTable_h
0002 #define DataFormats_L1Scouting_OrbitFlatTable_h
0003 
0004 /**
0005  * A cross-breed of a FlatTable and an OrbitCollection
0006  */
0007 
0008 #include "DataFormats/NanoAOD/interface/FlatTable.h"
0009 
0010 #include <cstdint>
0011 #include <vector>
0012 #include <string>
0013 #include <type_traits>
0014 
0015 namespace l1ScoutingRun3 {
0016 
0017   class OrbitFlatTable : public nanoaod::FlatTable {
0018   public:
0019     static constexpr unsigned int NBX = 3564;
0020 
0021     OrbitFlatTable() : nanoaod::FlatTable(), bxOffsets_(orbitBufferSize_ + 1, 0) {}
0022 
0023     OrbitFlatTable(std::vector<unsigned> bxOffsets,
0024                    const std::string &name,
0025                    bool singleton = false,
0026                    bool extension = false)
0027         : nanoaod::FlatTable(bxOffsets.back(), name, singleton, extension), bxOffsets_(bxOffsets) {
0028       if (bxOffsets.size() != orbitBufferSize_ + 1) {
0029         throw cms::Exception("LogicError") << "Mismatch between bxOffsets.size() " << bxOffsets.size()
0030                                            << " and orbitBufferSize_ + 1" << (orbitBufferSize_ + 1);
0031       }
0032     }
0033 
0034     ~OrbitFlatTable() {}
0035 
0036     using FlatTable::nRows;
0037     using FlatTable::size;
0038 
0039     /// number of rows for single BX
0040     unsigned int nRows(unsigned bx) const {
0041       if (bx >= orbitBufferSize_)
0042         throwBadBx(bx);
0043       return bxOffsets_[bx + 1] - bxOffsets_[bx];
0044     };
0045     unsigned int size(unsigned bx) const { return nRows(bx); }
0046 
0047     /// get a column by index (const)
0048     template <typename T>
0049     auto columnData(unsigned int column) const {
0050       return nanoaod::FlatTable::columnData<T>(column);
0051     }
0052 
0053     /// get a column by index and bx (const)
0054     template <typename T>
0055     auto columnData(unsigned int column, unsigned bx) const {
0056       if (bx >= orbitBufferSize_)
0057         throwBadBx(bx);
0058       auto begin = beginData<T>(column);
0059       return std::span(begin + bxOffsets_[bx], begin + bxOffsets_[bx + 1]);
0060     }
0061 
0062     /// get a column by index (non-const)
0063     template <typename T>
0064     auto columnData(unsigned int column) {
0065       return nanoaod::FlatTable::columnData<T>(column);
0066     }
0067 
0068     /// get a column by index and bx (non-const)
0069     template <typename T>
0070     auto columnData(unsigned int column, unsigned bx) {
0071       if (bx >= orbitBufferSize_)
0072         throwBadBx(bx);
0073       auto begin = beginData<T>(column);
0074       return std::span(begin + bxOffsets_[bx], begin + bxOffsets_[bx + 1]);
0075     }
0076 
0077     /// get a column value for singleton (const)
0078     template <typename T>
0079     const auto &columValue(unsigned int column, unsigned bx) const {
0080       if (!singleton())
0081         throw cms::Exception("LogicError", "columnValue works only for singleton tables");
0082       if (bx >= orbitBufferSize_ || bxOffsets_[bx + 1] == bxOffsets_[bx])
0083         throwBadBx(bx);
0084       auto begin = beginData<T>(column);
0085       return *(begin + bxOffsets_[bx]);
0086     }
0087 
0088   private:
0089     std::vector<unsigned> bxOffsets_;
0090 
0091     // there are 3564 BX in one orbtit [1,3564], one extra
0092     // count added to keep first entry of the vector
0093     static constexpr int orbitBufferSize_ = NBX + 1;
0094 
0095     [[noreturn]] void throwBadBx(unsigned bx) const {
0096       throw cms::Exception("OrbitFlatTable") << "Trying to access bad bx " << bx;
0097     }
0098   };
0099 
0100 }  // namespace l1ScoutingRun3
0101 
0102 #endif