File indexing completed on 2024-04-06 12:23:43
0001 #include "PhysicsTools/NanoAOD/plugins/SummaryTableOutputBranches.h"
0002
0003 template <typename T, typename Col>
0004 void SummaryTableOutputBranches::makeScalarBranches(const std::vector<Col> &tabcols,
0005 TTree &tree,
0006 const std::string &rootType,
0007 std::vector<NamedBranchPtr> &branches) {
0008 for (const auto &col : tabcols) {
0009 if (std::find_if(branches.begin(), branches.end(), [&col](const NamedBranchPtr &x) {
0010 return x.name == col.name;
0011 }) == branches.end()) {
0012 T backFillValue = 0;
0013 auto *br = tree.Branch(col.name.c_str(), &backFillValue, (col.name + "/" + rootType).c_str());
0014 br->SetTitle(col.doc.c_str());
0015 for (unsigned long i = 0; i < m_fills; i++)
0016 br->Fill();
0017 branches.emplace_back(col.name, br);
0018 }
0019 }
0020 }
0021
0022 template <typename Col>
0023 void SummaryTableOutputBranches::makeVectorBranches(const std::vector<Col> &tabcols,
0024 TTree &tree,
0025 const std::string &rootType,
0026 std::vector<NamedVectorBranchPtr> &branches) {
0027 for (const auto &col : tabcols) {
0028 if (std::find_if(branches.begin(), branches.end(), [&col](const NamedBranchPtr &x) {
0029 return x.name == col.name;
0030 }) == branches.end()) {
0031 int backFillValue = 0;
0032 auto *cbr = tree.Branch(("n" + col.name).c_str(), &backFillValue, ("n" + col.name + "/I").c_str());
0033 auto *vbr =
0034 tree.Branch(col.name.c_str(), (void *)nullptr, (col.name + "[n" + col.name + "]/" + rootType).c_str());
0035 cbr->SetTitle(("Number of entries in " + col.name).c_str());
0036 vbr->SetTitle(col.doc.c_str());
0037 for (unsigned long i = 0; i < m_fills; i++) {
0038 cbr->Fill();
0039 vbr->Fill();
0040 }
0041 branches.emplace_back(col.name, cbr, vbr);
0042 }
0043 }
0044 }
0045
0046 template <typename Col>
0047 void SummaryTableOutputBranches::fillScalarBranches(const std::vector<Col> &tabcols,
0048 std::vector<NamedBranchPtr> &branches) {
0049 if (tabcols.size() != branches.size())
0050 throw cms::Exception("LogicError", "Mismatch in table columns");
0051 for (unsigned int i = 0, n = tabcols.size(); i < n; ++i) {
0052 if (tabcols[i].name != branches[i].name)
0053 throw cms::Exception("LogicError", "Mismatch in table columns");
0054 branches[i].branch->SetAddress(const_cast<typename Col::value_type *>(&tabcols[i].value));
0055 }
0056 }
0057
0058 template <typename Col>
0059 void SummaryTableOutputBranches::fillVectorBranches(const std::vector<Col> &tabcols,
0060 std::vector<NamedVectorBranchPtr> &branches) {
0061 if (tabcols.size() != branches.size())
0062 throw cms::Exception("LogicError", "Mismatch in table columns");
0063 for (unsigned int i = 0, n = tabcols.size(); i < n; ++i) {
0064 if (tabcols[i].name != branches[i].name)
0065 throw cms::Exception("LogicError", "Mismatch in table columns");
0066 branches[i].count = tabcols[i].values.size();
0067 branches[i].branch->SetAddress(const_cast<typename Col::element_type *>(&tabcols[i].values.front()));
0068 }
0069 }
0070
0071 void SummaryTableOutputBranches::updateBranches(const nanoaod::MergeableCounterTable &tab, TTree &tree) {
0072 makeScalarBranches<Long64_t>(tab.intCols(), tree, "L", m_intBranches);
0073 makeScalarBranches<Double_t>(tab.floatCols(), tree, "D", m_floatBranches);
0074 makeScalarBranches<Double_t>(tab.floatWithNormCols(), tree, "D", m_floatWithNormBranches);
0075 makeVectorBranches(tab.vintCols(), tree, "L", m_vintBranches);
0076 makeVectorBranches(tab.vfloatCols(), tree, "D", m_vfloatBranches);
0077 makeVectorBranches(tab.vfloatWithNormCols(), tree, "D", m_vfloatWithNormBranches);
0078
0079
0080 for (auto &vbp : m_vintBranches)
0081 vbp.counterBranch->SetAddress(&vbp.count);
0082 for (auto &vbp : m_vfloatBranches)
0083 vbp.counterBranch->SetAddress(&vbp.count);
0084 for (auto &vbp : m_vfloatWithNormBranches)
0085 vbp.counterBranch->SetAddress(&vbp.count);
0086 }
0087
0088 void SummaryTableOutputBranches::fill(const edm::OccurrenceForOutput &iWhatever, TTree &tree) {
0089 edm::Handle<nanoaod::MergeableCounterTable> handle;
0090 iWhatever.getByToken(m_token, handle);
0091 const nanoaod::MergeableCounterTable &tab = *handle;
0092
0093 updateBranches(tab, tree);
0094
0095 fillScalarBranches(tab.intCols(), m_intBranches);
0096 fillScalarBranches(tab.floatCols(), m_floatBranches);
0097 fillScalarBranches(tab.floatWithNormCols(), m_floatWithNormBranches);
0098 fillVectorBranches(tab.vintCols(), m_vintBranches);
0099 fillVectorBranches(tab.vfloatCols(), m_vfloatBranches);
0100 fillVectorBranches(tab.vfloatWithNormCols(), m_vfloatWithNormBranches);
0101 m_fills++;
0102 }