CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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;
29  m_uint32Branches.emplace_back(var, tab.columnDoc(i), "i");
30  break;
32  m_doubleBranches.emplace_back(var, tab.columnDoc(i), "D");
33  break;
34  default:
35  throw cms::Exception("LogicError", "Unsupported type");
36  }
37  }
38 }
39 
41  if (!m_singleton) {
42  if (m_extension == IsExtension) {
43  m_counterBranch = tree.FindBranch(("n" + m_baseName).c_str());
44  if (!m_counterBranch) {
45  throw cms::Exception("LogicError",
46  "Trying to save an extension table for " + m_baseName +
47  " before having saved the corresponding main table\n");
48  }
49  } else {
50  if (tree.FindBranch(("n" + m_baseName).c_str()) != nullptr) {
51  throw cms::Exception("LogicError", "Trying to save multiple main tables for " + m_baseName + "\n");
52  }
53  m_counterBranch = tree.Branch(("n" + m_baseName).c_str(), &m_counter, ("n" + m_baseName + "/i").c_str());
54  m_counterBranch->SetTitle(m_doc.c_str());
55  }
56  }
57  std::string varsize = m_singleton ? "" : "[n" + m_baseName + "]";
58  for (std::vector<NamedBranchPtr> *branches :
60  for (auto &pair : *branches) {
61  std::string branchName = makeBranchName(m_baseName, pair.name);
62  pair.branch =
63  tree.Branch(branchName.c_str(), (void *)nullptr, (branchName + varsize + "/" + pair.rootTypeCode).c_str());
64  pair.branch->SetTitle(pair.title.c_str());
65  }
66  }
67 }
68 
69 void TableOutputBranches::fill(const edm::OccurrenceForOutput &iWhatever, TTree &tree, bool extensions) {
71  if (extensions != m_extension)
72  return; // do nothing, wait to be called with the proper flag
73  }
74 
76  iWhatever.getByToken(m_token, handle);
77  const nanoaod::FlatTable &tab = *handle;
78  m_counter = tab.size();
79  m_singleton = tab.singleton();
80  if (!m_branchesBooked) {
82  if (extensions != m_extension)
83  return; // do nothing, wait to be called with the proper flag
85  m_doc = tab.doc();
86  m_branchesBooked = true;
87  branch(tree);
88  }
89  if (!m_singleton && m_extension == IsExtension) {
90  if (m_counter != *reinterpret_cast<UInt_t *>(m_counterBranch->GetAddress())) {
91  throw cms::Exception("LogicError",
92  "Mismatch in number of entries between extension and main table for " + tab.name());
93  }
94  }
95  for (auto &pair : m_floatBranches)
96  fillColumn<float>(pair, tab);
97  for (auto &pair : m_intBranches)
98  fillColumn<int>(pair, tab);
99  for (auto &pair : m_uint8Branches)
100  fillColumn<uint8_t>(pair, tab);
101  for (auto &pair : m_uint32Branches)
102  fillColumn<uint32_t>(pair, tab);
103  for (auto &pair : m_doubleBranches)
104  fillColumn<double>(pair, tab);
105 }
std::vector< NamedBranchPtr > m_uint32Branches
bool extension() const
Definition: FlatTable.h:58
ColumnType columnType(unsigned int col) const
Definition: FlatTable.h:64
void defineBranchesFromFirstEvent(const nanoaod::FlatTable &tab)
const std::string & doc() const
Definition: FlatTable.h:67
std::vector< NamedBranchPtr > m_intBranches
list var
if using global norm cols_to_minmax = [&#39;t_delta&#39;, &#39;t_hmaxNearP&#39;,&#39;t_emaxNearP&#39;, &#39;t_hAnnular&#39;, &#39;t_eAnnular&#39;,&#39;t_pt&#39;,&#39;t_nVtx&#39;,&#39;t_ieta&#39;,&#39;t_eHcal10&#39;, &#39;t_eHcal30&#39;,&#39;t_rhoh&#39;,&#39;t_eHcal&#39;] df[cols_to_minmax] = df[cols_to_minmax].apply(lambda x: (x - x.min()) / (x.max() - x.min()) if (x.max() - x.min() &gt; 0) else 1.0/200.0)
tuple handle
Definition: patZpeak.py:23
std::vector< NamedBranchPtr > m_uint8Branches
void fill(const edm::OccurrenceForOutput &iWhatever, TTree &tree, bool extensions)
const std::string & name() const
Definition: FlatTable.h:59
void branch(TTree &tree)
edm::EDGetToken m_token
const std::string & columnName(unsigned int col) const
Definition: FlatTable.h:61
BasicHandle getByToken(EDGetToken token, TypeID const &typeID) const
unsigned int nColumns() const
Definition: FlatTable.h:54
unsigned int size() const
Definition: FlatTable.h:56
enum TableOutputBranches::@894 m_extension
bool singleton() const
Definition: FlatTable.h:57
std::vector< NamedBranchPtr > m_floatBranches
const std::string & columnDoc(unsigned int col) const
Definition: FlatTable.h:68
std::vector< NamedBranchPtr > m_doubleBranches