CMS 3D CMS Logo

TableOutputBranches.cc
Go to the documentation of this file.
2 
3 #include <iostream>
4 
5 namespace {
6  std::string makeBranchName(const std::string &baseName, const std::string &leafName) {
7  return baseName.empty() ? leafName : (leafName.empty() ? baseName : baseName + "_" + leafName);
8  }
9 } // namespace
10 
12  m_baseName = tab.name();
13  for (size_t i = 0; i < tab.nColumns(); i++) {
14  const std::string &var = tab.columnName(i);
15  switch (tab.columnType(i)) {
17  m_floatBranches.emplace_back(var, tab.columnDoc(i), "F");
18  break;
20  m_intBranches.emplace_back(var, tab.columnDoc(i), "I");
21  break;
23  m_uint8Branches.emplace_back(var, tab.columnDoc(i), "b");
24  break;
26  m_uint8Branches.emplace_back(var, tab.columnDoc(i), "O");
27  break;
28  }
29  }
30 }
31 
33  if (!m_singleton) {
34  if (m_extension == IsExtension) {
35  m_counterBranch = tree.FindBranch(("n" + m_baseName).c_str());
36  if (!m_counterBranch) {
37  throw cms::Exception("LogicError",
38  "Trying to save an extension table for " + m_baseName +
39  " before having saved the corresponding main table\n");
40  }
41  } else {
42  if (tree.FindBranch(("n" + m_baseName).c_str()) != nullptr) {
43  throw cms::Exception("LogicError", "Trying to save multiple main tables for " + m_baseName + "\n");
44  }
45  m_counterBranch = tree.Branch(("n" + m_baseName).c_str(), &m_counter, ("n" + m_baseName + "/i").c_str());
46  m_counterBranch->SetTitle(m_doc.c_str());
47  }
48  }
49  std::string varsize = m_singleton ? "" : "[n" + m_baseName + "]";
50  for (std::vector<NamedBranchPtr> *branches : {&m_floatBranches, &m_intBranches, &m_uint8Branches}) {
51  for (auto &pair : *branches) {
52  std::string branchName = makeBranchName(m_baseName, pair.name);
53  pair.branch =
54  tree.Branch(branchName.c_str(), (void *)nullptr, (branchName + varsize + "/" + pair.rootTypeCode).c_str());
55  pair.branch->SetTitle(pair.title.c_str());
56  }
57  }
58 }
59 
60 void TableOutputBranches::fill(const edm::EventForOutput &iEvent, TTree &tree, bool extensions) {
62  if (extensions != m_extension)
63  return; // do nothing, wait to be called with the proper flag
64  }
65 
67  iEvent.getByToken(m_token, handle);
68  const nanoaod::FlatTable &tab = *handle;
69  m_counter = tab.size();
70  m_singleton = tab.singleton();
71  if (!m_branchesBooked) {
73  if (extensions != m_extension)
74  return; // do nothing, wait to be called with the proper flag
76  m_doc = tab.doc();
77  m_branchesBooked = true;
78  branch(tree);
79  }
80  if (!m_singleton && m_extension == IsExtension) {
81  if (m_counter != *reinterpret_cast<UInt_t *>(m_counterBranch->GetAddress())) {
82  throw cms::Exception("LogicError",
83  "Mismatch in number of entries between extension and main table for " + tab.name());
84  }
85  }
86  for (auto &pair : m_floatBranches)
87  fillColumn<float>(pair, tab);
88  for (auto &pair : m_intBranches)
89  fillColumn<int>(pair, tab);
90  for (auto &pair : m_uint8Branches)
91  fillColumn<uint8_t>(pair, tab);
92 }
enum TableOutputBranches::@849 m_extension
BasicHandle getByToken(EDGetToken token, TypeID const &typeID) const
bool extension() const
Definition: FlatTable.h:52
ColumnType columnType(unsigned int col) const
Definition: FlatTable.h:58
void defineBranchesFromFirstEvent(const nanoaod::FlatTable &tab)
const std::string & doc() const
Definition: FlatTable.h:61
void fill(const edm::EventForOutput &iEvent, TTree &tree, bool extensions)
std::vector< NamedBranchPtr > m_intBranches
int iEvent
Definition: GenABIO.cc:224
std::vector< NamedBranchPtr > m_uint8Branches
const std::string & name() const
Definition: FlatTable.h:53
void branch(TTree &tree)
edm::EDGetToken m_token
const std::string & columnName(unsigned int col) const
Definition: FlatTable.h:55
unsigned int nColumns() const
Definition: FlatTable.h:48
unsigned int size() const
Definition: FlatTable.h:50
Definition: tree.py:1
bool singleton() const
Definition: FlatTable.h:51
std::vector< NamedBranchPtr > m_floatBranches
const std::string & columnDoc(unsigned int col) const
Definition: FlatTable.h:62