CMS 3D CMS Logo

L1TriggerResultsConverter.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: PhysicsTools/NanoAOD
4 // Class: L1TriggerResultsConverter
5 //
13 //
14 // Original Author: Andrea Rizzi
15 // Created: Mon, 11 Aug 2017 11:20:30 GMT
16 //
17 //
18 
19 // system include files
20 #include <memory>
21 #include <algorithm>
22 
23 // user include files
33 
36 
39 
41 
44 
46 //
47 // class declaration
48 //
49 
51 public:
53  ~L1TriggerResultsConverter() override;
54 
55  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
56 
57 private:
58  void produce(edm::Event&, const edm::EventSetup&) override;
59  void beginRun(edm::Run const&, edm::EventSetup const&) override;
60 
61  // ----------member data ---------------------------
62  const bool legacyL1_;
70  std::vector<std::string> names_;
71  std::vector<unsigned int> mask_;
72  std::vector<unsigned int> indices_;
73 };
74 
75 //
76 // constructors and destructor
77 //
79  : legacyL1_(params.getParameter<bool>("legacyL1")),
80  store_unprefireable_bits_(!legacyL1_ ? params.getParameter<bool>("storeUnprefireableBits") : false),
81  tokenLegacy_(legacyL1_ ? consumes<L1GlobalTriggerReadoutRecord>(params.getParameter<edm::InputTag>("src"))
82  : edm::EDGetTokenT<L1GlobalTriggerReadoutRecord>()),
83  token_(!legacyL1_ ? consumes<GlobalAlgBlkBxCollection>(params.getParameter<edm::InputTag>("src"))
84  : edm::EDGetTokenT<GlobalAlgBlkBxCollection>()),
85  token_ext_(store_unprefireable_bits_
86  ? consumes<GlobalExtBlkBxCollection>(params.getParameter<edm::InputTag>("src_ext"))
87  : edm::EDGetTokenT<GlobalExtBlkBxCollection>()),
88  l1gtmenuToken_(esConsumes<edm::Transition::BeginRun>()),
89  l1gtalgoMaskToken_(esConsumes<edm::Transition::BeginRun>()),
90  l1utmTrigToken_(esConsumes<edm::Transition::BeginRun>()) {
91  produces<edm::TriggerResults>();
92 }
93 
95  // do anything here that needs to be done at destruction time
96  // (e.g. close files, deallocate resources etc.)
97 }
98 
99 //
100 // member functions
101 //
102 
104  mask_.clear();
105  names_.clear();
106  indices_.clear();
107  if (legacyL1_) {
108  auto const& mapping = setup.getHandle(l1gtmenuToken_)->gtAlgorithmAliasMap();
109  for (auto const& keyval : mapping) {
110  names_.push_back(keyval.first);
111  indices_.push_back(keyval.second.algoBitNumber());
112  }
113  mask_ = setup.getHandle(l1gtalgoMaskToken_)->gtTriggerMask();
114  } else {
115  auto const& mapping = setup.getHandle(l1utmTrigToken_)->getAlgorithmMap();
116  for (auto const& keyval : mapping) {
117  names_.push_back(keyval.first);
118  indices_.push_back(keyval.second.getIndex());
119  }
121  names_.push_back("L1_UnprefireableEvent_TriggerRules");
122  names_.push_back("L1_UnprefireableEvent_FirstBxInTrain");
123  names_.push_back("L1_FinalOR_BXmin1");
124  names_.push_back("L1_FinalOR_BXmin2");
125  }
126  }
127 }
128 
129 // ------------ method called to produce the data ------------
130 
132  const std::vector<bool>* wordp = nullptr;
133  const std::vector<bool>* wordp_bxmin1 = nullptr;
134  const std::vector<bool>* wordp_bxmin2 = nullptr;
135  bool unprefireable_bit_triggerrules = false;
136  bool unprefireable_bit_firstbxintrain = false;
137  bool l1FinalOR_bxmin1 = false;
138  bool l1FinalOR_bxmin2 = false;
139 
140  if (!legacyL1_) {
141  const auto& resultsProd = iEvent.get(token_);
142  if (not resultsProd.isEmpty(0)) {
143  wordp = &resultsProd.at(0, 0).getAlgoDecisionFinal();
144  }
145  if (not resultsProd.isEmpty(-1)) {
146  wordp_bxmin1 = &resultsProd.at(-1, 0).getAlgoDecisionFinal();
147  }
148  if (not resultsProd.isEmpty(-2)) {
149  wordp_bxmin2 = &resultsProd.at(-2, 0).getAlgoDecisionFinal();
150  }
152  auto handleExtResults = iEvent.getHandle(token_ext_);
153  if (handleExtResults.isValid()) {
154  if (not handleExtResults->isEmpty(0)) {
155  //Stores the unprefirable event decision corresponding to trigger rules (e.g.: BX0 is unprefirable because BX-3 fired and therefore BX-2/-1 are masked).
156  unprefireable_bit_triggerrules =
157  handleExtResults->at(0, 0).getExternalDecision(GlobalExtBlk::maxExternalConditions - 1);
158  }
159  } else {
160  LogDebug("Unprefirable bit (trigger rules) not found, always set to false");
161  }
162  }
163  } else {
164  // Legacy access
165  const auto& resultsProd = iEvent.get(tokenLegacy_);
166  wordp = &resultsProd.decisionWord();
167  }
168  edm::HLTGlobalStatus l1bitsAsHLTStatus(names_.size());
169  unsigned indices_size = indices_.size();
170  for (size_t nidx = 0; nidx < indices_size; nidx++) {
171  unsigned int const index = indices_[nidx];
172  bool result = wordp ? wordp->at(index) : false;
173  bool result_bxmin1 = wordp_bxmin1 ? wordp_bxmin1->at(index) : false;
174  bool result_bxmin2 = wordp_bxmin2 ? wordp_bxmin2->at(index) : false;
175  if (not mask_.empty())
176  result &= (mask_.at(index) != 0);
177  l1bitsAsHLTStatus[nidx] = edm::HLTPathStatus(result ? edm::hlt::Pass : edm::hlt::Fail);
178  //Stores the unprefirable event decision corresponding to events in the first bx of a train.
179  //In 2022/2023 the bx before that was manually masked.
180  //Technically this was done by enabling the L1_FirstBunchBeforeTrain bit in the L1 menu, and vetoing that bit after L1 Final OR is evaluated.
181  if (!legacyL1_) {
182  if (names_[nidx] == "L1_FirstBunchBeforeTrain")
183  unprefireable_bit_firstbxintrain = result_bxmin1;
184  //Checks if any other seed was fired in BX-1 or -2
185  else if (result_bxmin1) {
186  l1FinalOR_bxmin1 = true;
187  } else if (result_bxmin2) {
188  l1FinalOR_bxmin2 = true;
189  }
190  }
191  }
193  l1bitsAsHLTStatus[indices_size] =
194  edm::HLTPathStatus(unprefireable_bit_triggerrules ? edm::hlt::Pass : edm::hlt::Fail);
195  l1bitsAsHLTStatus[indices_size + 1] =
196  edm::HLTPathStatus(unprefireable_bit_firstbxintrain ? edm::hlt::Pass : edm::hlt::Fail);
197  l1bitsAsHLTStatus[indices_size + 2] = edm::HLTPathStatus(l1FinalOR_bxmin1 ? edm::hlt::Pass : edm::hlt::Fail);
198  l1bitsAsHLTStatus[indices_size + 3] = edm::HLTPathStatus(l1FinalOR_bxmin2 ? edm::hlt::Pass : edm::hlt::Fail);
199  }
200  //mimic HLT trigger bits for L1
201  auto out = std::make_unique<edm::TriggerResults>(l1bitsAsHLTStatus, names_);
202  iEvent.put(std::move(out));
203 }
204 
205 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
208  desc.add<bool>("legacyL1")->setComment("is legacy L1");
209  desc.add<edm::InputTag>("src")->setComment(
210  "L1 input (L1GlobalTriggerReadoutRecord if legacy, GlobalAlgBlkBxCollection otherwise)");
211  desc.add<bool>("storeUnprefireableBits", false)
212  ->setComment("Activate storage of L1 unprefireable bits (needs L1 external decision input)");
213  desc.add<edm::InputTag>("src_ext", edm::InputTag(""))
214  ->setComment("L1 external decision input (GlobalExtBlkBxCollection, only supported if not legacy");
215  descriptions.add("L1TriggerResultsConverter", desc);
216 }
217 
218 //define this as a plug-in
std::vector< unsigned int > indices_
std::vector< std::string > names_
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
static const unsigned int maxExternalConditions
Definition: GlobalExtBlk.h:43
reject
Definition: HLTenums.h:19
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
void beginRun(edm::Run const &, edm::EventSetup const &) override
void produce(edm::Event &, const edm::EventSetup &) override
int iEvent
Definition: GenABIO.cc:224
accept
Definition: HLTenums.h:18
const edm::EDGetTokenT< GlobalAlgBlkBxCollection > token_
Transition
Definition: Transition.h:12
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
L1TriggerResultsConverter(const edm::ParameterSet &)
const edm::EDGetTokenT< GlobalExtBlkBxCollection > token_ext_
const edm::EDGetTokenT< L1GlobalTriggerReadoutRecord > tokenLegacy_
void add(std::string const &label, ParameterSetDescription const &psetDescription)
std::vector< unsigned int > mask_
HLT enums.
edm::ESGetToken< L1TUtmTriggerMenu, L1TUtmTriggerMenuRcd > l1utmTrigToken_
edm::ESGetToken< L1GtTriggerMask, L1GtTriggerMaskAlgoTrigRcd > l1gtalgoMaskToken_
def move(src, dest)
Definition: eostools.py:511
edm::ESGetToken< L1GtTriggerMenu, L1GtTriggerMenuRcd > l1gtmenuToken_
Definition: Run.h:45
#define LogDebug(id)