CMS 3D CMS Logo

List of all members | Public Member Functions | Static Private Member Functions | Private Attributes
TableCollectionSet Class Reference

#include <TableOutputFields.h>

Public Member Functions

void add (const edm::EDGetToken &table_token, const nanoaod::FlatTable &table)
 
void createFields (const edm::EventForOutput &event, RNTupleModel &eventModel)
 
void fill (const edm::EventForOutput &event)
 
void print () const
 

Static Private Member Functions

static bool hasAnonymousColumn (const nanoaod::FlatTable &table)
 

Private Attributes

std::vector< TableCollectionm_collections
 
std::vector< TableOutputFieldsm_singletonFields
 
std::vector< TableOutputVectorFieldsm_vectorFields
 

Detailed Description

Definition at line 118 of file TableOutputFields.h.

Member Function Documentation

◆ add()

void TableCollectionSet::add ( const edm::EDGetToken table_token,
const nanoaod::FlatTable table 
)

Definition at line 199 of file TableOutputFields.cc.

References HltBtagPostValidation_cff::c, universalConfigTemplate::collection, gather_cfg::cout, Exception, hasAnonymousColumn(), m_collections, m_singletonFields, m_vectorFields, and TableParser::table.

Referenced by NanoAODRNTupleOutputModule::initializeNTuple(), and counter.Counter::register().

199  {
200  // skip empty tables -- requirement of RNTuple to define schema before filling
201  if (table.nColumns() == 0) {
202  std::cout << "Warning: skipping empty table: \n";
203  printTable(table);
204  return;
205  }
206  // Can handle either anonymous table or anonymous column but not both
207  // - anonymous table: use column names directly as top-level fields
208  // - anonymous column: use the table name as the field name
209  if (table.name().empty() && hasAnonymousColumn(table)) {
210  throw cms::Exception("LogicError", "Anonymous FlatTable and anonymous field");
211  }
212  // case 1: create a top-level RNTuple field for each table column
213  if (table.name().empty() || hasAnonymousColumn(table)) {
214  if (table.singleton()) {
215  m_singletonFields.emplace_back(TableOutputFields(table_token));
216  } else {
217  m_vectorFields.emplace_back(TableOutputVectorFields(table_token));
218  }
219  return;
220  }
221  // case 2: Named singleton and vector tables are both written as RNTuple collections.
222  auto collection = std::find_if(m_collections.begin(), m_collections.end(), [&](const TableCollection& c) {
223  return c.getCollectionName() == table.name();
224  });
225  if (collection == m_collections.end()) {
226  m_collections.emplace_back(TableCollection());
227  m_collections.back().add(table_token, table);
228  return;
229  }
230  collection->add(table_token, table);
231 }
std::vector< TableOutputFields > m_singletonFields
std::vector< TableOutputVectorFields > m_vectorFields
static bool hasAnonymousColumn(const nanoaod::FlatTable &table)
std::vector< TableCollection > m_collections

◆ createFields()

void TableCollectionSet::createFields ( const edm::EventForOutput event,
RNTupleModel &  eventModel 
)

Definition at line 240 of file TableOutputFields.cc.

References universalConfigTemplate::collection, Exception, m_collections, m_singletonFields, m_vectorFields, and TableParser::table.

Referenced by NanoAODRNTupleOutputModule::initializeNTuple().

240  {
241  for (auto& collection : m_collections) {
242  if (!collection.hasMainTable()) {
243  throw cms::Exception("LogicError",
244  "Trying to save an extension table for " + collection.getCollectionName() +
245  " without the corresponding main table\n");
246  }
247  collection.createFields(event, eventModel);
248  }
249  for (auto& table : m_singletonFields) {
250  table.createFields(event, eventModel);
251  }
252  for (auto& table : m_vectorFields) {
253  table.createFields(event, eventModel);
254  }
255 }
std::vector< TableOutputFields > m_singletonFields
std::vector< TableOutputVectorFields > m_vectorFields
std::vector< TableCollection > m_collections
Definition: event.py:1

◆ fill()

void TableCollectionSet::fill ( const edm::EventForOutput event)

Definition at line 257 of file TableOutputFields.cc.

References universalConfigTemplate::collection, l1GtPatternGenerator_cfi::fields, patZpeak::handle, m_collections, m_singletonFields, m_vectorFields, and TableParser::table.

Referenced by NanoAODRNTupleOutputModule::write().

257  {
258  for (auto& collection : m_collections) {
259  collection.fill(event);
260  }
261  for (auto& fields : m_singletonFields) {
263  event.getByToken(fields.getToken(), handle);
264  const auto& table = *handle;
265  fields.fillEntry(table, 0);
266  }
267  for (auto& fields : m_vectorFields) {
268  fields.fill(event);
269  }
270 }
std::vector< TableOutputFields > m_singletonFields
std::vector< TableOutputVectorFields > m_vectorFields
std::vector< TableCollection > m_collections
Definition: event.py:1

◆ hasAnonymousColumn()

bool TableCollectionSet::hasAnonymousColumn ( const nanoaod::FlatTable table)
staticprivate

Definition at line 272 of file TableOutputFields.cc.

References Exception, mps_fire::i, TableParser::table, and to_string().

Referenced by add().

272  {
273  int num_anon = 0;
274  for (std::size_t i = 0; i < table.nColumns(); i++) {
275  if (table.columnName(i).empty()) {
276  num_anon++;
277  }
278  }
279  if (num_anon > 1) {
280  throw cms::Exception("LogicError",
281  "FlatTable `" + table.name() + "` has " + std::to_string(num_anon) + "anonymous fields");
282  }
283  return num_anon;
284 }
static std::string to_string(const XMLCh *ch)

◆ print()

void TableCollectionSet::print ( void  ) const

Definition at line 233 of file TableOutputFields.cc.

References universalConfigTemplate::collection, gather_cfg::cout, and m_collections.

233  {
234  for (const auto& collection : m_collections) {
235  collection.print();
236  std::cout << "\n";
237  }
238 }
std::vector< TableCollection > m_collections

Member Data Documentation

◆ m_collections

std::vector<TableCollection> TableCollectionSet::m_collections
private

Definition at line 129 of file TableOutputFields.h.

Referenced by add(), createFields(), fill(), and print().

◆ m_singletonFields

std::vector<TableOutputFields> TableCollectionSet::m_singletonFields
private

Definition at line 130 of file TableOutputFields.h.

Referenced by add(), createFields(), and fill().

◆ m_vectorFields

std::vector<TableOutputVectorFields> TableCollectionSet::m_vectorFields
private

Definition at line 131 of file TableOutputFields.h.

Referenced by add(), createFields(), and fill().