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_int8Branches.emplace_back(var, tab.columnDoc(i), "B");
24  break;
26  m_uint8Branches.emplace_back(var, tab.columnDoc(i), "b");
27  break;
29  m_uint8Branches.emplace_back(var, tab.columnDoc(i), "O");
30  break;
32  m_uint32Branches.emplace_back(var, tab.columnDoc(i), "i");
33  break;
35  m_doubleBranches.emplace_back(var, tab.columnDoc(i), "D");
36  break;
37  default:
38  throw cms::Exception("LogicError", "Unsupported type");
39  }
40  }
41 }
42 
44  if (!m_singleton) {
45  if (m_extension == IsExtension) {
46  m_counterBranch = tree.FindBranch(("n" + m_baseName).c_str());
47  if (!m_counterBranch) {
48  throw cms::Exception("LogicError",
49  "Trying to save an extension table for " + m_baseName +
50  " before having saved the corresponding main table\n");
51  }
52  } else {
53  if (tree.FindBranch(("n" + m_baseName).c_str()) != nullptr) {
54  throw cms::Exception("LogicError", "Trying to save multiple main tables for " + m_baseName + "\n");
55  }
56  m_counterBranch = tree.Branch(("n" + m_baseName).c_str(), &m_counter, ("n" + m_baseName + "/i").c_str());
57  m_counterBranch->SetTitle(m_doc.c_str());
58  }
59  }
60  std::string varsize = m_singleton ? "" : "[n" + m_baseName + "]";
61  for (std::vector<NamedBranchPtr> *branches :
63  for (auto &pair : *branches) {
64  std::string branchName = makeBranchName(m_baseName, pair.name);
65  pair.branch =
66  tree.Branch(branchName.c_str(), (void *)nullptr, (branchName + varsize + "/" + pair.rootTypeCode).c_str());
67  pair.branch->SetTitle(pair.title.c_str());
68  }
69  }
70 }
71 
72 void TableOutputBranches::fill(const edm::OccurrenceForOutput &iWhatever, TTree &tree, bool extensions) {
74  if (extensions != m_extension)
75  return; // do nothing, wait to be called with the proper flag
76  }
77 
79  iWhatever.getByToken(m_token, handle);
80  const nanoaod::FlatTable &tab = *handle;
81  m_counter = tab.size();
82  m_singleton = tab.singleton();
83  if (!m_branchesBooked) {
85  if (extensions != m_extension)
86  return; // do nothing, wait to be called with the proper flag
88  m_doc = tab.doc();
89  m_branchesBooked = true;
90  branch(tree);
91  }
92  if (!m_singleton && m_extension == IsExtension) {
93  if (m_counter != *reinterpret_cast<UInt_t *>(m_counterBranch->GetAddress())) {
94  throw cms::Exception("LogicError",
95  "Mismatch in number of entries between extension and main table for " + tab.name());
96  }
97  }
98  for (auto &pair : m_floatBranches)
99  fillColumn<float>(pair, tab);
100  for (auto &pair : m_intBranches)
101  fillColumn<int>(pair, tab);
102  for (auto &pair : m_int8Branches)
103  fillColumn<int8_t>(pair, tab);
104  for (auto &pair : m_uint8Branches)
105  fillColumn<uint8_t>(pair, tab);
106  for (auto &pair : m_uint32Branches)
107  fillColumn<uint32_t>(pair, tab);
108  for (auto &pair : m_doubleBranches)
109  fillColumn<double>(pair, tab);
110 }
std::vector< NamedBranchPtr > m_uint32Branches
const std::string & columnName(unsigned int col) const
Definition: FlatTable.h:62
void defineBranchesFromFirstEvent(const nanoaod::FlatTable &tab)
bool extension() const
Definition: FlatTable.h:59
BasicHandle getByToken(EDGetToken token, TypeID const &typeID) const
const std::string & name() const
Definition: FlatTable.h:60
std::vector< NamedBranchPtr > m_intBranches
const std::string & doc() const
Definition: FlatTable.h:68
std::vector< NamedBranchPtr > m_int8Branches
std::vector< NamedBranchPtr > m_uint8Branches
void fill(const edm::OccurrenceForOutput &iWhatever, TTree &tree, bool extensions)
void branch(TTree &tree)
edm::EDGetToken m_token
enum TableOutputBranches::@874 m_extension
bool singleton() const
Definition: FlatTable.h:58
ColumnType columnType(unsigned int col) const
Definition: FlatTable.h:65
unsigned int nColumns() const
Definition: FlatTable.h:55
const std::string & columnDoc(unsigned int col) const
Definition: FlatTable.h:69
Definition: tree.py:1
std::vector< NamedBranchPtr > m_floatBranches
unsigned int size() const
Definition: FlatTable.h:57
std::vector< NamedBranchPtr > m_doubleBranches