CMS 3D CMS Logo

TableOutputFields.h
Go to the documentation of this file.
1 #ifndef PhysicsTools_NanoAOD_TableOutputFields_h
2 #define PhysicsTools_NanoAOD_TableOutputFields_h
3 
4 #include "RNTupleFieldPtr.h"
5 
9 
10 #include <algorithm>
11 
12 #include <ROOT/RNTuple.hxx>
13 #include <ROOT/RNTupleModel.hxx>
14 using ROOT::Experimental::RCollectionNTupleWriter;
15 using ROOT::Experimental::RNTupleModel;
16 using ROOT::Experimental::RNTupleWriter;
17 
18 template <typename T>
20 public:
21  FlatTableField() = default;
22  FlatTableField(const nanoaod::FlatTable& table, std::size_t i, RNTupleModel& model) {
23  m_flatTableName = table.columnName(i);
24  // case 1: field has a name (table may or may not have a name)
25  if (!table.columnName(i).empty()) {
26  m_field = RNTupleFieldPtr<T>(table.columnName(i), table.columnDoc(i), model);
27  return;
28  }
29  // case 2: field doesn't have a name: use the table name as the RNTuple field name
30  if (table.name().empty()) {
31  throw cms::Exception("LogicError", "Empty FlatTable name and field name");
32  }
33  m_field = RNTupleFieldPtr<T>(table.name(), table.doc(), model);
34  }
35  // For collection fields and singleton fields
36  void fill(const nanoaod::FlatTable& table, std::size_t i) {
37  int col_idx = table.columnIndex(m_flatTableName);
38  if (col_idx == -1) {
39  throw cms::Exception("LogicError", "Missing column in input for " + table.name() + "_" + m_flatTableName);
40  }
41  m_field.fill(table.columnData<T>(col_idx)[i]);
42  }
43  // For vector fields without a collection, we have to buffer the results
44  // internally and then fill the RNTuple field
46  int col_idx = table.columnIndex(m_flatTableName);
47  if (col_idx == -1) {
48  throw cms::Exception("LogicError", "Missing column in input for " + table.name() + "_" + m_flatTableName);
49  }
50  std::vector<typename T::value_type> buf(table.size());
51  for (std::size_t i = 0; i < table.size(); i++) {
52  buf[i] = table.columnData<typename T::value_type>(col_idx)[i];
53  }
54  m_field.fill(buf);
55  }
56  const std::string& getFlatTableName() const { return m_flatTableName; }
57 
58 private:
61 };
62 
64 public:
65  TableOutputFields() = default;
67  void print() const;
68  void createFields(const edm::EventForOutput& event, RNTupleModel& model);
69  void fillEntry(const nanoaod::FlatTable& table, std::size_t i);
70  const edm::EDGetToken& getToken() const;
71 
72 private:
74  std::vector<FlatTableField<float>> m_floatFields;
75  std::vector<FlatTableField<int>> m_intFields;
76  std::vector<FlatTableField<std::uint8_t>> m_uint8Fields;
77  std::vector<FlatTableField<bool>> m_boolFields;
78 };
79 
81 public:
82  TableOutputVectorFields() = default;
84  void createFields(const edm::EventForOutput& event, RNTupleModel& model);
85  void fill(const edm::EventForOutput& event);
86 
87 private:
89  std::vector<FlatTableField<std::vector<float>>> m_vfloatFields;
90  std::vector<FlatTableField<std::vector<int>>> m_vintFields;
91  std::vector<FlatTableField<std::vector<std::uint8_t>>> m_vuint8Fields;
92  std::vector<FlatTableField<std::vector<bool>>> m_vboolFields;
93 };
94 
96 public:
97  TableCollection() = default;
98  // Invariants:
99  // * table has a non-empty base name
100  // * table has at least one column
101  void add(const edm::EDGetToken& table_token, const nanoaod::FlatTable& table);
102  // Invariants:
103  // * m_main not null
104  // * m_collectionName not empty
105  void createFields(const edm::EventForOutput& event, RNTupleModel& eventModel);
106  void fill(const edm::EventForOutput& event);
107  void print() const;
108  bool hasMainTable();
109  const std::string& getCollectionName() const;
110 
111 private:
113  std::shared_ptr<RCollectionNTupleWriter> m_collection;
115  std::vector<TableOutputFields> m_extensions;
116 };
117 
119 public:
120  void add(const edm::EDGetToken& table_token, const nanoaod::FlatTable& table);
121  void createFields(const edm::EventForOutput& event, RNTupleModel& eventModel);
122  void fill(const edm::EventForOutput& event);
123  void print() const;
124 
125 private:
126  // Returns true if the FlatTable has an anonymous column. Throws a cms::Exception
127  // if there is more than one anonymous column.
128  static bool hasAnonymousColumn(const nanoaod::FlatTable& table);
129  std::vector<TableCollection> m_collections;
130  std::vector<TableOutputFields> m_singletonFields;
131  std::vector<TableOutputVectorFields> m_vectorFields;
132 };
133 
134 #endif
RNTupleFieldPtr< T > m_field
TableOutputVectorFields()=default
void fillEntry(const nanoaod::FlatTable &table, std::size_t i)
TableOutputFields m_main
const edm::EDGetToken & getToken() const
std::shared_ptr< RCollectionNTupleWriter > m_collection
TableOutputFields(const edm::EDGetToken &token)
TableOutputVectorFields(const edm::EDGetToken &token)
std::vector< TableOutputFields > m_singletonFields
TableCollection()=default
std::vector< FlatTableField< std::vector< float > > > m_vfloatFields
void createFields(const edm::EventForOutput &event, RNTupleModel &eventModel)
FlatTableField()=default
edm::EDGetToken m_token
std::vector< FlatTableField< bool > > m_boolFields
std::string m_collectionName
std::vector< FlatTableField< std::vector< bool > > > m_vboolFields
const std::string & getCollectionName() const
std::vector< FlatTableField< std::uint8_t > > m_uint8Fields
void fill(const edm::EventForOutput &event)
std::vector< TableOutputVectorFields > m_vectorFields
std::vector< TableOutputFields > m_extensions
std::vector< FlatTableField< int > > m_intFields
const std::string & getFlatTableName() const
std::vector< FlatTableField< std::vector< int > > > m_vintFields
std::string m_flatTableName
void createFields(const edm::EventForOutput &event, RNTupleModel &model)
std::vector< FlatTableField< float > > m_floatFields
void fill(const edm::EventForOutput &event)
void add(const edm::EDGetToken &table_token, const nanoaod::FlatTable &table)
FlatTableField(const nanoaod::FlatTable &table, std::size_t i, RNTupleModel &model)
static bool hasAnonymousColumn(const nanoaod::FlatTable &table)
void add(const edm::EDGetToken &table_token, const nanoaod::FlatTable &table)
std::vector< FlatTableField< std::vector< std::uint8_t > > > m_vuint8Fields
void fill(const edm::EventForOutput &event)
long double T
std::vector< TableCollection > m_collections
void createFields(const edm::EventForOutput &event, RNTupleModel &model)
void fill(const nanoaod::FlatTable &table, std::size_t i)
void createFields(const edm::EventForOutput &event, RNTupleModel &eventModel)
void fillVectored(const nanoaod::FlatTable &table)
Definition: event.py:1
TableOutputFields()=default