File indexing completed on 2023-03-17 11:16:08
0001 #include "PhysicsTools/NanoAOD/plugins/TriggerOutputBranches.h"
0002 #include "FWCore/Framework/interface/MakerMacros.h"
0003 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0004 #include "FWCore/ParameterSet/interface/Registry.h"
0005
0006 #include <iostream>
0007
0008 void TriggerOutputBranches::updateTriggerNames(TTree& tree,
0009 const edm::TriggerNames& names,
0010 const edm::TriggerResults& triggers) {
0011 std::vector<std::string> newNames(triggers.getTriggerNames());
0012 if (newNames.empty()) {
0013 for (unsigned int j = 0; j < triggers.size(); j++) {
0014 newNames.push_back(names.triggerName(j));
0015 }
0016 }
0017
0018 for (auto& existing : m_triggerBranches) {
0019 existing.idx = -1;
0020 existing.buffer = 0;
0021 for (unsigned int j = 0; j < newNames.size(); j++) {
0022 std::string name = newNames[j];
0023 std::size_t vfound = name.rfind("_v");
0024 if (vfound != std::string::npos && (name.compare(0, 3, "HLT") == 0 || name.compare(0, 2, "L1") == 0)) {
0025 name.replace(vfound, name.size() - vfound, "");
0026 }
0027 if (name == existing.name)
0028 existing.idx = j;
0029 }
0030 }
0031
0032 for (unsigned int j = 0; j < newNames.size(); j++) {
0033 std::string name = newNames[j];
0034 std::size_t vfound = name.rfind("_v");
0035 if (vfound != std::string::npos && (name.compare(0, 3, "HLT") == 0 || name.compare(0, 2, "L1") == 0)) {
0036 name.replace(vfound, name.size() - vfound, "");
0037 }
0038 bool found = false;
0039 if (name.compare(0, 3, "HLT") == 0 || name.compare(0, 4, "Flag") == 0 || name.compare(0, 2, "L1") == 0) {
0040 for (auto& existing : m_triggerBranches) {
0041 if (name == existing.name)
0042 found = true;
0043 }
0044 if (!found) {
0045 NamedBranchPtr nb(
0046 name,
0047 std::string("Trigger/flag bit (process: ") + m_processName +
0048 ")");
0049 uint8_t backFillValue = 0;
0050 bool found_duplicate = verifyBranchUniqueName(tree, nb.name);
0051 std::string brname = nb.name + (found_duplicate ? (std::string("_p") + m_processName) : "");
0052 nb.branch = tree.Branch(brname.c_str(), &backFillValue, (brname + "/O").c_str());
0053 nb.branch->SetTitle(nb.title.c_str());
0054 nb.idx = j;
0055 m_triggerBranches.push_back(nb);
0056 for (size_t i = 0; i < m_fills; i++)
0057 nb.branch->Fill();
0058 }
0059 }
0060 }
0061 }
0062
0063 edm::TriggerNames TriggerOutputBranches::triggerNames(const edm::TriggerResults triggerResults) {
0064 edm::pset::Registry* psetRegistry = edm::pset::Registry::instance();
0065 edm::ParameterSet const* pset = nullptr;
0066 if (nullptr != (pset = psetRegistry->getMapped(triggerResults.parameterSetID()))) {
0067 if (pset->existsAs<std::vector<std::string> >("@trigger_paths", true)) {
0068 edm::TriggerNames triggerNames(*pset);
0069
0070
0071 if (triggerNames.size() != triggerResults.size()) {
0072 throw cms::Exception("LogicError") << "edm::EventBase::triggerNames_ Encountered vector\n"
0073 "of trigger names and a TriggerResults object with\n"
0074 "different sizes. This should be impossible.\n"
0075 "Please send information to reproduce this problem to\n"
0076 "the edm developers.\n";
0077 }
0078 return triggerNames;
0079 }
0080 }
0081 return edm::TriggerNames();
0082 }
0083
0084 void TriggerOutputBranches::fill(const edm::EventForOutput& iEvent, TTree& tree) {
0085 edm::Handle<edm::TriggerResults> handle;
0086 iEvent.getByToken(m_token, handle);
0087 const edm::TriggerResults& triggers = *handle;
0088 const edm::TriggerNames& names = triggerNames(triggers);
0089
0090 if (m_lastRun != iEvent.id().run()) {
0091 m_lastRun = iEvent.id().run();
0092 updateTriggerNames(tree, names, triggers);
0093 }
0094 for (auto& pair : m_triggerBranches)
0095 fillColumn<uint8_t>(pair, triggers);
0096 m_fills++;
0097 }
0098
0099 bool TriggerOutputBranches::verifyBranchUniqueName(TTree& tree, std::string name) const {
0100 auto const branches = tree.GetListOfBranches();
0101 for (int i = 0; i < branches->GetEntries(); i++) {
0102 if (name == std::string(branches->At(i)->GetName())) {
0103 edm::LogWarning("TriggerOutputBranches")
0104 << "Found a branch with name " << std::string(branches->At(i)->GetName()) << " already present with title "
0105 << std::string(branches->At(i)->GetTitle()) << ": will add suffix _p" << m_processName
0106 << " to the new branch.\n";
0107 return true;
0108 }
0109 }
0110 return false;
0111 }