CMS 3D CMS Logo

TriggerBxMonitor.cc
Go to the documentation of this file.
1 // C++ headers
2 #include <cstring>
3 #include <iterator>
4 #include <string>
5 
6 // {fmt} headers
7 #include <fmt/printf.h>
8 
9 // CMSSW headers
28 
29 namespace {
30 
31  struct RunBasedHistograms {
32  public:
34  RunBasedHistograms()
35  : // L1T and HLT configuration
36  hltConfig(),
37  // L1T and HLT results
38  tcds_bx_all(nullptr),
39  l1t_bx_all(nullptr),
40  hlt_bx_all(nullptr),
41  tcds_bx(),
42  l1t_bx(),
43  hlt_bx(),
44  tcds_bx_2d(),
45  l1t_bx_2d(),
46  hlt_bx_2d() {}
47 
48  public:
49  // HLT configuration
51 
52  // L1T and HLT results
53  dqm::reco::MonitorElement* tcds_bx_all;
54  dqm::reco::MonitorElement* l1t_bx_all;
55  dqm::reco::MonitorElement* hlt_bx_all;
56  std::vector<dqm::reco::MonitorElement*> tcds_bx;
57  std::vector<dqm::reco::MonitorElement*> l1t_bx;
58  std::vector<dqm::reco::MonitorElement*> hlt_bx;
59  std::vector<dqm::reco::MonitorElement*> tcds_bx_2d;
60  std::vector<dqm::reco::MonitorElement*> l1t_bx_2d;
61  std::vector<dqm::reco::MonitorElement*> hlt_bx_2d;
62  };
63 
64 } // namespace
65 
66 class TriggerBxMonitor : public DQMGlobalEDAnalyzer<RunBasedHistograms> {
67 public:
68  explicit TriggerBxMonitor(edm::ParameterSet const&);
69  ~TriggerBxMonitor() override = default;
70 
71  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
72 
73 private:
74  void dqmBeginRun(edm::Run const&, edm::EventSetup const&, RunBasedHistograms&) const override;
75  void bookHistograms(DQMStore::IBooker&, edm::Run const&, edm::EventSetup const&, RunBasedHistograms&) const override;
76  void dqmAnalyze(edm::Event const&, edm::EventSetup const&, RunBasedHistograms const&) const override;
77 
78  // number of bunch crossings
79  static const unsigned int s_bx_range = 3564;
80 
81  // TCDS trigger types
82  // see https://twiki.cern.ch/twiki/bin/viewauth/CMS/TcdsEventRecord
83  static constexpr const char* s_tcds_trigger_types[] = {
84  "Empty", // 0 - No trigger
85  "Physics", // 1 - GT trigger
86  "Calibration", // 2 - Sequence trigger (calibration)
87  "Random", // 3 - Random trigger
88  "Auxiliary", // 4 - Auxiliary (CPM front panel NIM input) trigger
89  nullptr, // 5 - reserved
90  nullptr, // 6 - reserved
91  nullptr, // 7 - reserved
92  "Cyclic", // 8 - Cyclic trigger
93  "Bunch-pattern", // 9 - Bunch-pattern trigger
94  "Software", // 10 - Software trigger
95  "TTS", // 11 - TTS-sourced trigger
96  nullptr, // 12 - reserved
97  nullptr, // 13 - reserved
98  nullptr, // 14 - reserved
99  nullptr // 15 - reserved
100  };
101 
102  // module configuration
108  const bool m_make_1d_plots;
109  const bool m_make_2d_plots;
110  const uint32_t m_ls_range;
111 };
112 
113 // definition
115 
118  desc.addUntracked<edm::InputTag>("l1tResults", edm::InputTag("gtStage2Digis"));
119  desc.addUntracked<edm::InputTag>("hltResults", edm::InputTag("TriggerResults"));
120  desc.addUntracked<std::string>("dqmPath", "HLT/TriggerBx");
121  desc.addUntracked<bool>("make1DPlots", true);
122  desc.addUntracked<bool>("make2DPlots", false);
123  desc.addUntracked<uint32_t>("lsRange", 4000);
124  descriptions.add("triggerBxMonitor", desc);
125 }
126 
128  : // module configuration
129  m_l1tMenu_token{esConsumes<edm::Transition::BeginRun>()},
130  m_l1t_results_inputTag{config.getUntrackedParameter<edm::InputTag>("l1tResults")},
131  m_l1t_results_token{consumes(m_l1t_results_inputTag)},
132  m_hlt_results_token{consumes(config.getUntrackedParameter<edm::InputTag>("hltResults"))},
133  m_dqm_path{config.getUntrackedParameter<std::string>("dqmPath")},
134  m_make_1d_plots{config.getUntrackedParameter<bool>("make1DPlots")},
135  m_make_2d_plots{config.getUntrackedParameter<bool>("make2DPlots")},
136  m_ls_range{config.getUntrackedParameter<uint32_t>("lsRange")} {}
137 
139  edm::EventSetup const& setup,
140  RunBasedHistograms& histograms) const {
141  // initialise the TCDS vector
142  if (m_make_1d_plots) {
143  histograms.tcds_bx.clear();
144  histograms.tcds_bx.resize(std::size(s_tcds_trigger_types));
145  }
146  if (m_make_2d_plots) {
147  histograms.tcds_bx_2d.clear();
148  histograms.tcds_bx_2d.resize(std::size(s_tcds_trigger_types));
149  }
150 
151  // cache the L1 trigger menu
152  if (m_make_1d_plots) {
153  histograms.l1t_bx.clear();
155  }
156  if (m_make_2d_plots) {
157  histograms.l1t_bx_2d.clear();
159  }
160 
161  // initialise the HLTConfigProvider
162  bool changed = true;
165  if (histograms.hltConfig.init(run, setup, labels.process, changed)) {
166  if (m_make_1d_plots) {
167  histograms.hlt_bx.clear();
168  histograms.hlt_bx.resize(histograms.hltConfig.size());
169  }
170  if (m_make_2d_plots) {
171  histograms.hlt_bx_2d.clear();
172  histograms.hlt_bx_2d.resize(histograms.hltConfig.size());
173  }
174  } else {
175  // HLTConfigProvider not initialised, skip the the HLT monitoring
176  edm::LogError("TriggerBxMonitor")
177  << "failed to initialise HLTConfigProvider, the HLT bx distribution will not be monitored";
178  }
179 }
180 
182  edm::Run const& run,
183  edm::EventSetup const& setup,
184  RunBasedHistograms& histograms) const {
185  // TCDS trigger type plots
186  {
187  size_t size = std::size(s_tcds_trigger_types);
188 
189  // book 2D histogram to monitor all TCDS trigger types in a single plot
191  histograms.tcds_bx_all = booker.book2D("TCDS Trigger Types",
192  "TCDS Trigger Types vs. bunch crossing",
193  s_bx_range + 1,
194  -0.5,
195  s_bx_range + 0.5,
196  size,
197  -0.5,
198  size - 0.5);
199 
200  // book the individual histograms for the known TCDS trigger types
201  booker.setCurrentFolder(m_dqm_path + "/TCDS");
202  for (unsigned int i = 0; i < size; ++i) {
203  if (s_tcds_trigger_types[i]) {
204  if (m_make_1d_plots) {
205  histograms.tcds_bx.at(i) =
207  }
208  if (m_make_2d_plots) {
209  std::string const& name_ls = std::string(s_tcds_trigger_types[i]) + " vs LS";
210  histograms.tcds_bx_2d.at(i) = booker.book2D(
211  name_ls, name_ls, s_bx_range + 1, -0.5, s_bx_range + 0.5, m_ls_range, 0.5, m_ls_range + 0.5);
212  }
213  histograms.tcds_bx_all->setBinLabel(i + 1, s_tcds_trigger_types[i], 2); // Y axis
214  }
215  }
216  }
217 
218  // L1T plots
219  {
220  // book 2D histogram to monitor all L1 triggers in a single plot
222  histograms.l1t_bx_all = booker.book2D("Level 1 Triggers",
223  "Level 1 Triggers vs. bunch crossing",
224  s_bx_range + 1,
225  -0.5,
226  s_bx_range + 0.5,
228  -0.5,
230 
231  // book the individual histograms for the L1 triggers that are included in the L1 menu
232  booker.setCurrentFolder(m_dqm_path + "/L1T");
233  auto const& l1tMenu = setup.getData(m_l1tMenu_token);
234  for (auto const& keyval : l1tMenu.getAlgorithmMap()) {
235  unsigned int bit = keyval.second.getIndex();
236  std::string const& name = fmt::sprintf("%s (bit %d)", keyval.first, bit);
237  if (m_make_1d_plots) {
238  histograms.l1t_bx.at(bit) = booker.book1D(name, name, s_bx_range + 1, -0.5, s_bx_range + 0.5);
239  }
240  if (m_make_2d_plots) {
241  std::string const& name_ls = name + " vs LS";
242  histograms.l1t_bx_2d.at(bit) =
243  booker.book2D(name_ls, name_ls, s_bx_range + 1, -0.5, s_bx_range + 0.5, m_ls_range, 0.5, m_ls_range + 0.5);
244  }
245  histograms.l1t_bx_all->setBinLabel(bit + 1, keyval.first, 2); // Y axis
246  }
247  }
248 
249  // HLT plots
250  if (histograms.hltConfig.inited()) {
251  // book 2D histogram to monitor all HLT paths in a single plot
253  histograms.hlt_bx_all = booker.book2D("High Level Triggers",
254  "High Level Triggers vs. bunch crossing",
255  s_bx_range + 1,
256  -0.5,
257  s_bx_range + 0.5,
258  histograms.hltConfig.size(),
259  -0.5,
260  histograms.hltConfig.size() - 0.5);
261 
262  // book the individual HLT triggers histograms
263  booker.setCurrentFolder(m_dqm_path + "/HLT");
264  for (unsigned int i = 0; i < histograms.hltConfig.size(); ++i) {
265  std::string const& name = histograms.hltConfig.triggerName(i);
266  if (m_make_1d_plots) {
267  histograms.hlt_bx[i] = booker.book1D(name, name, s_bx_range + 1, -0.5, s_bx_range + 0.5);
268  }
269  if (m_make_2d_plots) {
270  std::string const& name_ls = name + " vs LS";
271  histograms.hlt_bx_2d[i] =
272  booker.book2D(name_ls, name_ls, s_bx_range + 1, -0.5, s_bx_range + 0.5, m_ls_range, 0.5, m_ls_range + 0.5);
273  }
274  histograms.hlt_bx_all->setBinLabel(i + 1, name, 2); // Y axis
275  }
276  }
277 }
278 
280  edm::EventSetup const& setup,
281  RunBasedHistograms const& histograms) const {
282  unsigned int bx = event.bunchCrossing();
283  unsigned int ls = event.luminosityBlock();
284 
285  // monitor the bx distribution for the TCDS trigger types
286  {
287  size_t size = std::size(s_tcds_trigger_types);
288  unsigned int type = event.experimentType();
289  if (type < size) {
290  if (m_make_1d_plots and histograms.tcds_bx.at(type))
291  histograms.tcds_bx[type]->Fill(bx);
292  if (m_make_2d_plots and histograms.tcds_bx_2d.at(type))
293  histograms.tcds_bx_2d[type]->Fill(bx, ls);
294  }
295  histograms.tcds_bx_all->Fill(bx, type);
296  }
297 
298  // monitor the bx distribution for the L1 triggers
299  {
300  auto const& algBlkBxVecHandle = event.getHandle(m_l1t_results_token);
301  if (not algBlkBxVecHandle.isValid()) {
302  edm::LogError("TriggerBxMonitor")
303  << "L1 trigger results with label [" << m_l1t_results_inputTag.encode()
304  << "] not present or invalid. MonitorElements of L1T results not filled for this event.";
305  } else if (algBlkBxVecHandle->isEmpty(0)) {
306  edm::LogError("TriggerBxMonitor")
307  << "L1 trigger results with label [" << m_l1t_results_inputTag.encode()
308  << "] empty for BX=0. MonitorElements of L1T results not filled for this event.";
309  } else {
310  auto const& results = algBlkBxVecHandle->at(0, 0);
311  for (unsigned int i = 0; i < GlobalAlgBlk::maxPhysicsTriggers; ++i) {
312  if (results.getAlgoDecisionFinal(i)) {
313  if (m_make_1d_plots and histograms.l1t_bx.at(i))
314  histograms.l1t_bx[i]->Fill(bx);
315  if (m_make_2d_plots and histograms.l1t_bx_2d.at(i))
316  histograms.l1t_bx_2d[i]->Fill(bx, ls);
317  histograms.l1t_bx_all->Fill(bx, i);
318  }
319  }
320  }
321  }
322 
323  // monitor the bx distribution for the HLT triggers
324  if (histograms.hltConfig.inited()) {
325  auto const& hltResults = event.get(m_hlt_results_token);
326  for (unsigned int i = 0; i < hltResults.size(); ++i) {
327  if (hltResults.at(i).accept()) {
328  if (m_make_1d_plots and histograms.hlt_bx.at(i))
329  histograms.hlt_bx[i]->Fill(bx);
330  if (m_make_2d_plots and histograms.hlt_bx_2d.at(i))
331  histograms.hlt_bx_2d[i]->Fill(bx, ls);
332  histograms.hlt_bx_all->Fill(bx, i);
333  }
334  }
335  }
336 }
337 
338 //define this as a plug-in
size
Write out results.
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
void dqmAnalyze(edm::Event const &, edm::EventSetup const &, RunBasedHistograms const &) const override
const edm::EDGetTokenT< edm::TriggerResults > m_hlt_results_token
virtual void setCurrentFolder(std::string const &fullpath)
Definition: DQMStore.cc:36
std::string encode() const
Definition: InputTag.cc:159
~TriggerBxMonitor() override=default
void dqmBeginRun(edm::Run const &, edm::EventSetup const &, RunBasedHistograms &) const override
Definition: config.py:1
Log< level::Error, false > LogError
void bookHistograms(DQMStore::IBooker &, edm::Run const &, edm::EventSetup const &, RunBasedHistograms &) const override
const edm::ESGetToken< L1TUtmTriggerMenu, L1TUtmTriggerMenuRcd > m_l1tMenu_token
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
static constexpr const char * s_tcds_trigger_types[]
const uint32_t m_ls_range
const edm::InputTag m_l1t_results_inputTag
const edm::EDGetTokenT< GlobalAlgBlkBxCollection > m_l1t_results_token
const std::string m_dqm_path
TriggerBxMonitor(edm::ParameterSet const &)
def ls(path, rec=False)
Definition: eostools.py:349
dqm::legacy::MonitorElement MonitorElement
const bool m_make_1d_plots
MonitorElement * book2D(TString const &name, TString const &title, int nchX, double lowX, double highX, int nchY, double lowY, double highY, FUNC onbooking=NOOP())
Definition: DQMStore.h:212
static const unsigned int s_bx_range
void add(std::string const &label, ParameterSetDescription const &psetDescription)
results
Definition: mysort.py:8
static constexpr unsigned int maxPhysicsTriggers
Definition: GlobalAlgBlk.h:52
MonitorElement * book1D(TString const &name, TString const &title, int const nchX, double const lowX, double const highX, FUNC onbooking=NOOP())
Definition: DQMStore.h:98
const bool m_make_2d_plots
Definition: event.py:1
Definition: Run.h:45
void labelsForToken(EDGetToken iToken, Labels &oLabels) const