File indexing completed on 2024-04-06 12:23:43
0001 #include "PhysicsTools/NanoAOD/plugins/TableOutputBranches.h"
0002
0003 #include <iostream>
0004 #include <limits>
0005
0006 namespace {
0007 std::string makeBranchName(const std::string &baseName, const std::string &leafName) {
0008 return baseName.empty() ? leafName : (leafName.empty() ? baseName : baseName + "_" + leafName);
0009 }
0010 }
0011
0012 void TableOutputBranches::defineBranchesFromFirstEvent(const nanoaod::FlatTable &tab) {
0013 m_baseName = tab.name();
0014 for (size_t i = 0; i < tab.nColumns(); i++) {
0015 const std::string &var = tab.columnName(i);
0016 switch (tab.columnType(i)) {
0017 case nanoaod::FlatTable::ColumnType::UInt8:
0018 m_uint8Branches.emplace_back(var, tab.columnDoc(i), "b");
0019 break;
0020 case nanoaod::FlatTable::ColumnType::Int16:
0021 m_int16Branches.emplace_back(var, tab.columnDoc(i), "S");
0022 break;
0023 case nanoaod::FlatTable::ColumnType::UInt16:
0024 m_uint16Branches.emplace_back(var, tab.columnDoc(i), "s");
0025 break;
0026 case nanoaod::FlatTable::ColumnType::Int32:
0027 m_int32Branches.emplace_back(var, tab.columnDoc(i), "I");
0028 break;
0029 case nanoaod::FlatTable::ColumnType::UInt32:
0030 m_uint32Branches.emplace_back(var, tab.columnDoc(i), "i");
0031 break;
0032 case nanoaod::FlatTable::ColumnType::Bool:
0033 m_uint8Branches.emplace_back(var, tab.columnDoc(i), "O");
0034 break;
0035 case nanoaod::FlatTable::ColumnType::Float:
0036 m_floatBranches.emplace_back(var, tab.columnDoc(i), "F");
0037 break;
0038 case nanoaod::FlatTable::ColumnType::Double:
0039 m_doubleBranches.emplace_back(var, tab.columnDoc(i), "D");
0040 break;
0041 default:
0042 throw cms::Exception("LogicError", "Unsupported type");
0043 }
0044 }
0045 }
0046
0047 void TableOutputBranches::branch(TTree &tree) {
0048 if (!m_singleton) {
0049 if (m_extension == IsExtension) {
0050 m_counterBranch = tree.FindBranch(("n" + m_baseName).c_str());
0051 if (!m_counterBranch) {
0052 throw cms::Exception("LogicError",
0053 "Trying to save an extension table for " + m_baseName +
0054 " before having saved the corresponding main table\n");
0055 }
0056 } else {
0057 if (tree.FindBranch(("n" + m_baseName).c_str()) != nullptr) {
0058 throw cms::Exception("LogicError", "Trying to save multiple main tables for " + m_baseName + "\n");
0059 }
0060 m_counterBranch = tree.Branch(("n" + m_baseName).c_str(), &m_counter, ("n" + m_baseName + "/I").c_str());
0061 m_counterBranch->SetTitle(m_doc.c_str());
0062 }
0063 }
0064 std::string varsize = m_singleton ? "" : "[n" + m_baseName + "]";
0065 for (std::vector<NamedBranchPtr> *branches : {&m_uint8Branches,
0066 &m_int16Branches,
0067 &m_uint16Branches,
0068 &m_int32Branches,
0069 &m_uint32Branches,
0070 &m_floatBranches,
0071 &m_doubleBranches}) {
0072 for (auto &pair : *branches) {
0073 std::string branchName = makeBranchName(m_baseName, pair.name);
0074 pair.branch =
0075 tree.Branch(branchName.c_str(), (void *)nullptr, (branchName + varsize + "/" + pair.rootTypeCode).c_str());
0076 pair.branch->SetTitle(pair.title.c_str());
0077 }
0078 }
0079 }
0080
0081 void TableOutputBranches::fill(const edm::OccurrenceForOutput &iWhatever, TTree &tree, bool extensions) {
0082 if (m_extension != DontKnowYetIfMainOrExtension) {
0083 if (extensions != m_extension)
0084 return;
0085 }
0086
0087 edm::Handle<nanoaod::FlatTable> handle;
0088 iWhatever.getByToken(m_token, handle);
0089 const nanoaod::FlatTable &tab = *handle;
0090 auto size = tab.size();
0091
0092
0093 if (size > std::numeric_limits<CounterType>::max()) {
0094 throw cms::Exception("Table " + tab.name() + " size is " + std::to_string(size) +
0095 ", is too large for ROOT native array branch");
0096 }
0097 m_counter = size;
0098 m_singleton = tab.singleton();
0099 if (!m_branchesBooked) {
0100 m_extension = tab.extension() ? IsExtension : IsMain;
0101 if (extensions != m_extension)
0102 return;
0103 defineBranchesFromFirstEvent(tab);
0104 m_doc = tab.doc();
0105 m_branchesBooked = true;
0106 branch(tree);
0107 }
0108 if (!m_singleton && m_extension == IsExtension) {
0109 if (m_counter != *reinterpret_cast<CounterType *>(m_counterBranch->GetAddress())) {
0110 throw cms::Exception("LogicError",
0111 "Mismatch in number of entries between extension and main table for " + tab.name());
0112 }
0113 }
0114 for (auto &pair : m_uint8Branches)
0115 fillColumn<uint8_t>(pair, tab);
0116 for (auto &pair : m_int16Branches)
0117 fillColumn<int16_t>(pair, tab);
0118 for (auto &pair : m_uint16Branches)
0119 fillColumn<uint16_t>(pair, tab);
0120 for (auto &pair : m_int32Branches)
0121 fillColumn<int32_t>(pair, tab);
0122 for (auto &pair : m_uint32Branches)
0123 fillColumn<uint32_t>(pair, tab);
0124 for (auto &pair : m_floatBranches)
0125 fillColumn<float>(pair, tab);
0126 for (auto &pair : m_doubleBranches)
0127 fillColumn<double>(pair, tab);
0128 }