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