CMS 3D CMS Logo

TableOutputBranches.cc
Go to the documentation of this file.
2 
3 #include <iostream>
4 #include <limits>
5 
6 namespace {
7  std::string makeBranchName(const std::string &baseName, const std::string &leafName) {
8  return baseName.empty() ? leafName : (leafName.empty() ? baseName : baseName + "_" + leafName);
9  }
10 } // namespace
11 
13  m_baseName = tab.name();
14  for (size_t i = 0; i < tab.nColumns(); i++) {
15  const std::string &var = tab.columnName(i);
16  switch (tab.columnType(i)) {
18  m_uint8Branches.emplace_back(var, tab.columnDoc(i), "b");
19  break;
21  m_int16Branches.emplace_back(var, tab.columnDoc(i), "S");
22  break;
24  m_uint16Branches.emplace_back(var, tab.columnDoc(i), "s");
25  break;
27  m_int32Branches.emplace_back(var, tab.columnDoc(i), "I");
28  break;
30  m_uint32Branches.emplace_back(var, tab.columnDoc(i), "i");
31  break;
33  m_uint8Branches.emplace_back(var, tab.columnDoc(i), "O");
34  break;
36  m_floatBranches.emplace_back(var, tab.columnDoc(i), "F");
37  break;
39  m_doubleBranches.emplace_back(var, tab.columnDoc(i), "D");
40  break;
41  default:
42  throw cms::Exception("LogicError", "Unsupported type");
43  }
44  }
45 }
46 
48  if (!m_singleton) {
49  if (m_extension == IsExtension) {
50  m_counterBranch = tree.FindBranch(("n" + m_baseName).c_str());
51  if (!m_counterBranch) {
52  throw cms::Exception("LogicError",
53  "Trying to save an extension table for " + m_baseName +
54  " before having saved the corresponding main table\n");
55  }
56  } else {
57  if (tree.FindBranch(("n" + m_baseName).c_str()) != nullptr) {
58  throw cms::Exception("LogicError", "Trying to save multiple main tables for " + m_baseName + "\n");
59  }
60  m_counterBranch = tree.Branch(("n" + m_baseName).c_str(), &m_counter, ("n" + m_baseName + "/I").c_str());
61  m_counterBranch->SetTitle(m_doc.c_str());
62  }
63  }
64  std::string varsize = m_singleton ? "" : "[n" + m_baseName + "]";
65  for (std::vector<NamedBranchPtr> *branches : {&m_uint8Branches,
71  &m_doubleBranches}) {
72  for (auto &pair : *branches) {
73  std::string branchName = makeBranchName(m_baseName, pair.name);
74  pair.branch =
75  tree.Branch(branchName.c_str(), (void *)nullptr, (branchName + varsize + "/" + pair.rootTypeCode).c_str());
76  pair.branch->SetTitle(pair.title.c_str());
77  }
78  }
79 }
80 
81 void TableOutputBranches::fill(const edm::OccurrenceForOutput &iWhatever, TTree &tree, bool extensions) {
83  if (extensions != m_extension)
84  return; // do nothing, wait to be called with the proper flag
85  }
86 
88  iWhatever.getByToken(m_token, handle);
89  const nanoaod::FlatTable &tab = *handle;
90  auto size = tab.size();
91  // ROOT native array size branches may only be signed integers,
92  // until this is changed we need to make sure the vector sizes do not exceed that
94  throw cms::Exception("Table " + tab.name() + " size is " + std::to_string(size) +
95  ", is too large for ROOT native array branch");
96  }
97  m_counter = size;
98  m_singleton = tab.singleton();
99  if (!m_branchesBooked) {
101  if (extensions != m_extension)
102  return; // do nothing, wait to be called with the proper flag
104  m_doc = tab.doc();
105  m_branchesBooked = true;
106  branch(tree);
107  }
108  if (!m_singleton && m_extension == IsExtension) {
109  if (m_counter != *reinterpret_cast<CounterType *>(m_counterBranch->GetAddress())) {
110  throw cms::Exception("LogicError",
111  "Mismatch in number of entries between extension and main table for " + tab.name());
112  }
113  }
114  for (auto &pair : m_uint8Branches)
115  fillColumn<uint8_t>(pair, tab);
116  for (auto &pair : m_int16Branches)
117  fillColumn<int16_t>(pair, tab);
118  for (auto &pair : m_uint16Branches)
119  fillColumn<uint16_t>(pair, tab);
120  for (auto &pair : m_int32Branches)
121  fillColumn<int32_t>(pair, tab);
122  for (auto &pair : m_uint32Branches)
123  fillColumn<uint32_t>(pair, tab);
124  for (auto &pair : m_floatBranches)
125  fillColumn<float>(pair, tab);
126  for (auto &pair : m_doubleBranches)
127  fillColumn<double>(pair, tab);
128 }
size
Write out results.
std::vector< NamedBranchPtr > m_uint32Branches
const std::string & columnName(unsigned int col) const
Definition: FlatTable.h:64
void defineBranchesFromFirstEvent(const nanoaod::FlatTable &tab)
bool extension() const
Definition: FlatTable.h:61
BasicHandle getByToken(EDGetToken token, TypeID const &typeID) const
const std::string & name() const
Definition: FlatTable.h:62
static std::string to_string(const XMLCh *ch)
enum TableOutputBranches::@884 m_extension
const std::string & doc() const
Definition: FlatTable.h:70
std::vector< NamedBranchPtr > m_int32Branches
std::vector< NamedBranchPtr > m_uint16Branches
std::vector< NamedBranchPtr > m_uint8Branches
void fill(const edm::OccurrenceForOutput &iWhatever, TTree &tree, bool extensions)
void branch(TTree &tree)
edm::EDGetToken m_token
bool singleton() const
Definition: FlatTable.h:60
std::vector< NamedBranchPtr > m_int16Branches
ColumnType columnType(unsigned int col) const
Definition: FlatTable.h:67
unsigned int nColumns() const
Definition: FlatTable.h:57
const std::string & columnDoc(unsigned int col) const
Definition: FlatTable.h:71
Definition: tree.py:1
std::vector< NamedBranchPtr > m_floatBranches
unsigned int size() const
Definition: FlatTable.h:59
std::vector< NamedBranchPtr > m_doubleBranches