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  default:
29  throw cms::Exception("LogicError", "Unsupported type");
30  }
31  }
32 }
33 
35  if (!m_singleton) {
36  if (m_extension == IsExtension) {
37  m_counterBranch = tree.FindBranch(("n" + m_baseName).c_str());
38  if (!m_counterBranch) {
39  throw cms::Exception("LogicError",
40  "Trying to save an extension table for " + m_baseName +
41  " before having saved the corresponding main table\n");
42  }
43  } else {
44  if (tree.FindBranch(("n" + m_baseName).c_str()) != nullptr) {
45  throw cms::Exception("LogicError", "Trying to save multiple main tables for " + m_baseName + "\n");
46  }
47  m_counterBranch = tree.Branch(("n" + m_baseName).c_str(), &m_counter, ("n" + m_baseName + "/i").c_str());
48  m_counterBranch->SetTitle(m_doc.c_str());
49  }
50  }
51  std::string varsize = m_singleton ? "" : "[n" + m_baseName + "]";
52  for (std::vector<NamedBranchPtr> *branches : {&m_floatBranches, &m_intBranches, &m_uint8Branches}) {
53  for (auto &pair : *branches) {
54  std::string branchName = makeBranchName(m_baseName, pair.name);
55  pair.branch =
56  tree.Branch(branchName.c_str(), (void *)nullptr, (branchName + varsize + "/" + pair.rootTypeCode).c_str());
57  pair.branch->SetTitle(pair.title.c_str());
58  }
59  }
60 }
61 
62 void TableOutputBranches::fill(const edm::EventForOutput &iEvent, TTree &tree, bool extensions) {
64  if (extensions != m_extension)
65  return; // do nothing, wait to be called with the proper flag
66  }
67 
69  iEvent.getByToken(m_token, handle);
70  const nanoaod::FlatTable &tab = *handle;
71  m_counter = tab.size();
72  m_singleton = tab.singleton();
73  if (!m_branchesBooked) {
75  if (extensions != m_extension)
76  return; // do nothing, wait to be called with the proper flag
78  m_doc = tab.doc();
79  m_branchesBooked = true;
80  branch(tree);
81  }
82  if (!m_singleton && m_extension == IsExtension) {
83  if (m_counter != *reinterpret_cast<UInt_t *>(m_counterBranch->GetAddress())) {
84  throw cms::Exception("LogicError",
85  "Mismatch in number of entries between extension and main table for " + tab.name());
86  }
87  }
88  for (auto &pair : m_floatBranches)
89  fillColumn<float>(pair, tab);
90  for (auto &pair : m_intBranches)
91  fillColumn<int>(pair, tab);
92  for (auto &pair : m_uint8Branches)
93  fillColumn<uint8_t>(pair, tab);
94 }
nanoaod::FlatTable::ColumnType::Float
TableOutputBranches::m_baseName
std::string m_baseName
Definition: TableOutputBranches.h:29
mps_fire.i
i
Definition: mps_fire.py:428
TableOutputBranches::defineBranchesFromFirstEvent
void defineBranchesFromFirstEvent(const nanoaod::FlatTable &tab)
Definition: TableOutputBranches.cc:11
TableOutputBranches::m_uint8Branches
std::vector< NamedBranchPtr > m_uint8Branches
Definition: TableOutputBranches.h:46
nanoaod::FlatTable::singleton
bool singleton() const
Definition: FlatTable.h:55
patZpeak.handle
handle
Definition: patZpeak.py:23
electrons_cff.branchName
branchName
Definition: electrons_cff.py:521
tree
Definition: tree.py:1
TableOutputBranches::m_singleton
bool m_singleton
Definition: TableOutputBranches.h:30
edm::Handle
Definition: AssociativeIterator.h:50
TableOutputBranches::IsExtension
Definition: TableOutputBranches.h:31
nanoaod::FlatTable::doc
const std::string & doc() const
Definition: FlatTable.h:65
trigObjTnPSource_cfi.var
var
Definition: trigObjTnPSource_cfi.py:21
TableOutputBranches::branch
void branch(TTree &tree)
Definition: TableOutputBranches.cc:34
TableOutputBranches::m_token
edm::EDGetToken m_token
Definition: TableOutputBranches.h:28
nanoaod::FlatTable::columnDoc
const std::string & columnDoc(unsigned int col) const
Definition: FlatTable.h:66
TableOutputBranches::fill
void fill(const edm::EventForOutput &iEvent, TTree &tree, bool extensions)
Definition: TableOutputBranches.cc:62
nanoaod::FlatTable::ColumnType::UInt8
nanoaod::FlatTable::columnType
ColumnType columnType(unsigned int col) const
Definition: FlatTable.h:62
nanoaod::FlatTable::extension
bool extension() const
Definition: FlatTable.h:56
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
TableOutputBranches::m_counter
UInt_t m_counter
Definition: TableOutputBranches.h:33
iEvent
int iEvent
Definition: GenABIO.cc:224
TableOutputBranches::m_floatBranches
std::vector< NamedBranchPtr > m_floatBranches
Definition: TableOutputBranches.h:44
TableOutputBranches::m_extension
enum TableOutputBranches::@879 m_extension
TableOutputBranches::m_intBranches
std::vector< NamedBranchPtr > m_intBranches
Definition: TableOutputBranches.h:45
nanoaod::FlatTable::name
const std::string & name() const
Definition: FlatTable.h:57
nanoaod::FlatTable::ColumnType::Int
TableOutputBranches.h
edm::EventForOutput
Definition: EventForOutput.h:50
nanoaod::FlatTable
Definition: FlatTable.h:38
nanoaod::FlatTable::columnName
const std::string & columnName(unsigned int col) const
Definition: FlatTable.h:59
nanoaod::FlatTable::nColumns
unsigned int nColumns() const
Definition: FlatTable.h:52
Exception
Definition: hltDiff.cc:245
TableOutputBranches::m_branchesBooked
bool m_branchesBooked
Definition: TableOutputBranches.h:47
TableOutputBranches::m_doc
std::string m_doc
Definition: TableOutputBranches.h:32
TableOutputBranches::m_counterBranch
TBranch * m_counterBranch
Definition: TableOutputBranches.h:43
nanoaod::FlatTable::ColumnType::Bool
TableOutputBranches::IsMain
Definition: TableOutputBranches.h:31
TableOutputBranches::DontKnowYetIfMainOrExtension
Definition: TableOutputBranches.h:31
nanoaod::FlatTable::size
unsigned int size() const
Definition: FlatTable.h:54