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