15 #include "TRACE/tracemf.h"
16 #define TRACE_NAME "DemoViewer"
18 #include "art/Framework/Core/EDAnalyzer.h"
19 #include "art/Framework/Core/ModuleMacros.h"
20 #include "art/Framework/Principal/Event.h"
21 #include "art/Framework/Principal/Handle.h"
22 #include "art/Framework/Principal/Run.h"
23 #include "canvas/Utilities/InputTag.h"
24 #include "cetlib_except/exception.h"
26 #include "artdaq-core/Data/ContainerFragment.hh"
27 #include "artdaq-core/Data/Fragment.hh"
29 #include "artdaq-core-demo/Overlays/FragmentType.hh"
30 #include "artdaq-core-demo/Overlays/ToyFragment.hh"
32 #include <TApplication.h>
39 #include <TRootCanvas.h>
44 #include <initializer_list>
50 #include <unordered_map>
54 #define TRACE_NAME "DemoViewer"
77 explicit DemoViewer(fhicl::ParameterSet
const& p);
81 void analyze(art::Event
const& e)
override;
83 void beginJob()
override;
84 void beginRun(art::Run
const& e)
override;
85 void endRun (art::Run
const& e)
override;
95 std::vector<Double_t> x_;
97 art::RunNumber_t current_run_;
99 size_t max_num_x_plots_;
100 size_t max_num_y_plots_;
101 std::size_t num_x_plots_;
102 std::size_t num_y_plots_;
104 std::string raw_data_label_;
106 std::unordered_map<artdaq::Fragment::fragment_id_t, TGraph*> graphs_;
107 std::unordered_map<artdaq::Fragment::fragment_id_t, TH1D*> histograms_;
109 std::map<artdaq::Fragment::fragment_id_t, std::size_t> id_to_index_;
111 std::string outputFileName_;
126 : art::EDAnalyzer (ps)
127 , prescale_ (ps.get<int> (
"prescale"))
129 , max_num_x_plots_ (ps.get<std::size_t>(
"num_x_plots", std::numeric_limits<std::size_t>::max()))
130 , max_num_y_plots_ (ps.get<std::size_t>(
"num_y_plots", std::numeric_limits<std::size_t>::max()))
133 , raw_data_label_ (ps.get<std::string>(
"raw_data_label",
"daq"))
136 , outputFileName_ (ps.get<std::string>(
"fileName",
"artdaqdemo_onmon.root"))
137 , writeOutput_ (ps.get<bool> (
"write_to_file", false))
139 , dynamicMode_ (ps.get<bool> (
"dynamic_mode", true))
141 gStyle->SetOptStat(
"irm");
142 gStyle->SetMarkerStyle(22);
143 gStyle->SetMarkerColor(4);
145 if (ps.has_key(
"fragment_ids")) {
146 auto fragment_ids = ps.get<std::vector<artdaq::Fragment::fragment_id_t>>(
"fragment_ids");
147 for (
auto&
id : fragment_ids) {
148 auto index = id_to_index_.size();
149 id_to_index_[id] = index;
154 void DemoViewer::beginJob() {
159 tmp_argv =
new char*[2];
160 tmp_argv[0] =
new char[100];
161 tmp_argv[1] =
new char[100];
163 strcpy(tmp_argv[0],
"-b");
164 strcpy(tmp_argv[1],
"--web=server:8877");
166 _app =
new TApplication(
"DemoViewer", &tmp_argc, tmp_argv);
174 void DemoViewer::getXYDims_() {
176 if (max_num_x_plots_ == 0) max_num_x_plots_ = std::numeric_limits<size_t>::max();
177 if (max_num_y_plots_ == 0) max_num_y_plots_ = std::numeric_limits<size_t>::max();
179 num_x_plots_ = num_y_plots_ =
static_cast<std::size_t
>(ceil(sqrt(id_to_index_.size())));
182 if (id_to_index_.size() > max_num_x_plots_ && id_to_index_.size() > max_num_x_plots_ * max_num_y_plots_) {
183 num_x_plots_ = max_num_x_plots_;
184 num_y_plots_ = max_num_y_plots_;
185 auto max = num_x_plots_ * num_y_plots_;
186 auto it = id_to_index_.begin();
187 while (it != id_to_index_.end()) {
188 if (it->second >= max) it = id_to_index_.erase(it);
195 if (max_num_x_plots_ >= 4 && max_num_y_plots_ >= 2)
197 switch (id_to_index_.size())
200 num_x_plots_ = num_y_plots_ = 1;
228 while (num_x_plots_ > max_num_x_plots_)
231 num_y_plots_ =
static_cast<size_t>(ceil(id_to_index_.size() / num_x_plots_));
234 TLOG(TLVL_DEBUG) <<
"id count: " << id_to_index_.size() <<
", num_x_plots_: " << num_x_plots_ <<
" / " << max_num_x_plots_ <<
", num_y_plots_: " << num_y_plots_ <<
" / " << max_num_y_plots_;
240 void DemoViewer::bookCanvas_() {
244 _hCanvas =
new TCanvas(
"wf0");
245 _hCanvas->Divide(num_x_plots_, num_y_plots_);
246 _hCanvas->SetTitle(
"ADC Value Distribution");
249 _gCanvas =
new TCanvas(
"wf1");
250 _gCanvas->Divide(num_x_plots_, num_y_plots_);
251 _gCanvas->SetTitle(
"ADC Values, Event Snapshot");
258 DemoViewer::~DemoViewer() {
260 for (
auto& histogram : histograms_) {
261 histogram.second =
nullptr;
264 for (
auto& graph : graphs_) {
265 graph.second =
nullptr;
278 void DemoViewer::analyze(art::Event
const& e) {
279 static std::size_t evt_cntr = -1;
287 artdaq::Fragments fragments;
288 artdaq::FragmentPtrs containerFragments;
290 std::vector<art::Handle<artdaq::Fragments>> fragmentHandles;
291 fragmentHandles = e.getMany<std::vector<artdaq::Fragment>>();
293 for (
const auto& handle : fragmentHandles) {
294 if (!handle.isValid() || handle->empty()) {
298 if (handle->front().type() == artdaq::Fragment::ContainerFragmentType) {
299 for (
const auto& cont : *handle) {
300 artdaq::ContainerFragment contf(cont);
301 auto ftype = contf.fragment_type();
302 if (ftype != FragmentType::TOY1 && ftype != FragmentType::TOY2)
break;
304 for (
size_t ii = 0; ii < contf.block_count(); ++ii) {
305 containerFragments.push_back(contf[ii]);
306 fragments.push_back(*containerFragments.back());
307 if (newCanvas_ && !id_to_index_.count(fragments.back().fragmentID())) {
308 auto index = id_to_index_.size();
309 id_to_index_[fragments.back().fragmentID()] = index;
315 for (
auto frag : *handle) {
316 fragments.emplace_back(frag);
317 if (newCanvas_ && !id_to_index_.count(fragments.back().fragmentID())) {
318 auto index = id_to_index_.size();
319 id_to_index_[fragments.back().fragmentID()] = index;
343 artdaq::Fragment::sequence_id_t expected_sequence_id = std::numeric_limits<artdaq::Fragment::sequence_id_t>::max();
345 for (
const auto& frag : fragments) {
346 std::unique_ptr<ToyFragment> toyPtr;
348 if (expected_sequence_id == std::numeric_limits<artdaq::Fragment::sequence_id_t>::max()) {
349 expected_sequence_id = frag.sequenceID();
352 if (expected_sequence_id != frag.sequenceID()) {
353 TLOG(TLVL_WARNING) <<
"Warning in DemoViewer: expected fragment with sequence ID " << expected_sequence_id
354 <<
", received one with sequence ID " << frag.sequenceID();
357 auto fragtype =
static_cast<FragmentType
>(frag.type());
358 std::size_t max_adc_count = std::numeric_limits<std::size_t>::max();
359 std::size_t total_adc_values = std::numeric_limits<std::size_t>::max();
362 case FragmentType::TOY1:
363 toyPtr = std::make_unique<ToyFragment>(frag);
364 total_adc_values = toyPtr->total_adc_values();
365 max_adc_count =
static_cast<size_t>(pow(2, frag.template metadata<ToyFragment::Metadata>()->num_adc_bits) - 1);
367 case FragmentType::TOY2:
368 toyPtr = std::make_unique<ToyFragment>(frag);
369 total_adc_values = toyPtr->total_adc_values();
370 max_adc_count =
static_cast<size_t>(pow(2, frag.template metadata<ToyFragment::Metadata>()->num_adc_bits) - 1);
373 throw cet::exception(
"Error in DemoViewer: unknown fragment type supplied");
376 artdaq::Fragment::fragment_id_t fid = frag.fragmentID();
377 if (id_to_index_.count(fid) == 0u) {
378 TLOG(TLVL_WARNING) <<
"Warning in DemoViewer: unexpected Fragment with fragment id " << std::to_string(fid)
383 std::size_t ind = id_to_index_[fid];
387 if (histograms_.count(fid) == 0 || histograms_[fid] ==
nullptr) {
388 histograms_[fid] =
new TH1D(Form(
"Fragment_%d_hist",fid),
"",max_adc_count+1,-0.5,max_adc_count+0.5);
389 histograms_[fid]->SetTitle(Form(
"Frag %d, Type %s",fid, fragmentTypeToString(fragtype).c_str()));
390 histograms_[fid]->GetXaxis()->SetTitle(
"ADC value");
392 _hCanvas->cd(ind + 1);
393 histograms_[fid]->Draw();
397 case FragmentType::TOY1:
398 case FragmentType::TOY2:
399 for (
auto val = toyPtr->dataBeginADCs(); val != toyPtr->dataEndADCs(); ++val) {
400 histograms_[fid]->Fill(*val);
405 TLOG(TLVL_ERROR) <<
"Error in DemoViewer: unknown fragment type supplied";
406 throw cet::exception(
"Error in DemoViewer: unknown fragment type supplied");
409 if (((evt_cntr % prescale_ - 1) != 0u) && prescale_ > 1)
continue;
413 if (x_.size() != total_adc_values) {
414 x_.resize(total_adc_values);
415 std::iota(x_.begin(), x_.end(), 0);
421 if (graphs_.count(fid) == 0 || graphs_[fid] ==
nullptr ||
422 static_cast<std::size_t
>(graphs_[fid]->GetN()) != total_adc_values) {
424 graphs_[fid] =
new TGraph(total_adc_values);
425 graphs_[fid]->SetName(Form(
"Fragment_%d_graph", fid));
426 graphs_[fid]->SetLineColor(4);
427 std::copy(x_.begin(), x_.end(), graphs_[fid]->GetX());
429 _gCanvas->cd(ind + 1);
430 graphs_[fid]->Draw(
"ALP");
435 case FragmentType::TOY1:
436 case FragmentType::TOY2: {
437 std::copy(toyPtr->dataBeginADCs(), toyPtr->dataBeginADCs() + total_adc_values, graphs_[fid]->GetY());
442 TLOG(TLVL_ERROR) <<
"Error in DemoViewer: unknown fragment type supplied";
443 throw cet::exception(
"Error in DemoViewer: unknown fragment type supplied");
447 gSystem->ProcessEvents();
451 void DemoViewer::beginRun(art::Run
const& e) {
452 if (e.run() == current_run_)
return;
453 current_run_ = e.run();
457 for (
auto& x : graphs_ ) x.second =
nullptr;
458 for (
auto& x : histograms_) x.second =
nullptr;
461 if (!dynamicMode_) bookCanvas_();
465 void DemoViewer::endRun(art::Run
const& e) {
466 if (e.run() == current_run_)
return;
467 current_run_ = e.run();
470 fFile_ =
new TFile(outputFileName_.c_str(),
"RECREATE");
472 _hCanvas->Write(
"wf0", TObject::kOverwrite);
473 if (_gCanvas !=
nullptr) {
474 _gCanvas->Write(
"wf1", TObject::kOverwrite);
480 DEFINE_ART_MODULE(DemoViewer)
An example art analysis module which plots events both as histograms and event snapshots (plot of ADC...
DemoViewer(fhicl::ParameterSet const &p)
DemoViewer Constructor.