1 #include "art/Framework/Core/EDAnalyzer.h"
2 #include "art/Framework/Core/ModuleMacros.h"
3 #include "art/Framework/Principal/Event.h"
4 #include "art/Framework/Principal/Handle.h"
5 #include "art/Framework/Principal/Run.h"
6 #include "canvas/Utilities/InputTag.h"
8 #include "artdaq-core/Data/Fragment.hh"
10 #include "otsdaq-demo/Overlays/DataGenFragment.hh"
11 #include "otsdaq-demo/Overlays/FragmentType.hh"
13 #include "cetlib_except/exception.h"
20 #include "TRootCanvas.h"
23 #include "otsdaq/Macros/CoutMacros.h"
24 #include "otsdaq/MessageFacility/MessageFacility.h"
28 #include <initializer_list>
40 explicit WFViewer(fhicl::ParameterSet
const& p);
43 void analyze(art::Event
const& e)
override;
44 void beginRun(art::Run
const&)
override;
45 double calcmean(
const float*);
48 std::unique_ptr<TCanvas> canvas_[2];
49 std::vector<Double_t> x_;
51 art::RunNumber_t current_run_;
53 std::size_t num_x_plots_;
54 std::size_t num_y_plots_;
56 std::vector<std::string> fragment_type_labels_;
57 std::vector<artdaq::Fragment::fragment_id_t> fragment_ids_;
59 std::vector<std::unique_ptr<TGraph>> graphs_;
60 std::vector<std::unique_ptr<TH1D>> histograms_;
62 std::map<artdaq::Fragment::fragment_id_t, std::size_t> id_to_index_;
63 std::string outputFileName_;
69 ots::WFViewer::WFViewer(fhicl::ParameterSet
const& ps)
71 , prescale_(ps.get<int>(
"prescale"))
74 ps.get<std::size_t>(
"num_x_plots", std::numeric_limits<std::size_t>::max()))
76 ps.get<std::size_t>(
"num_y_plots", std::numeric_limits<std::size_t>::max()))
77 , fragment_type_labels_(ps.get<std::vector<std::string>>(
"fragment_type_labels"))
78 , fragment_ids_(ps.get<std::vector<artdaq::Fragment::fragment_id_t>>(
"fragment_ids"))
79 , graphs_(fragment_ids_.size())
80 , histograms_(fragment_ids_.size())
81 , outputFileName_(ps.get<std::string>(
"fileName",
"otsdaqdemo_onmon.root"))
82 , writeOutput_(ps.get<bool>(
"write_to_file", false))
84 __COUT__ <<
"WFViewer CONSTRUCTOR BEGIN!!!!" << std::endl;
86 if(num_x_plots_ == std::numeric_limits<std::size_t>::max() ||
87 num_y_plots_ == std::numeric_limits<std::size_t>::max())
89 switch(fragment_ids_.size())
92 num_x_plots_ = num_y_plots_ = 1;
114 num_x_plots_ = num_y_plots_ =
115 static_cast<std::size_t
>(ceil(sqrt(fragment_type_labels_.size())));
122 for(std::size_t i_f = 0; i_f < fragment_ids_.size(); ++i_f)
124 id_to_index_[fragment_ids_[i_f]] = i_f;
131 sort(fragment_type_labels_.begin(), fragment_type_labels_.end());
132 fragment_type_labels_.erase(
133 unique(fragment_type_labels_.begin(), fragment_type_labels_.end()),
134 fragment_type_labels_.end());
136 gStyle->SetOptStat(
"irm");
137 gStyle->SetMarkerStyle(22);
138 gStyle->SetMarkerColor(4);
140 std::cout << __COUT_HDR_FL__ <<
"WFViewer CONSTRUCTOR END" << std::endl;
143 double ots::WFViewer::calcmean(
const float* data)
149 for(ii = 0; ii < 10; ii++)
155 std::cout <<
"WFViewer mean is " << mean <<
" data[0] is " << data[0]
156 <<
" data[1] is " << data[1] <<
" 2=" << data[2] <<
" 3=" << data[3]
159 std::cout <<
"WFViewer mean is " << mean <<
" data[15997] is " << data[15997]
160 <<
" data[15998] is " << data[15998] <<
" 2=" << data[15999]
161 <<
" 3=" << data[15999] << std::endl
169 void ots::WFViewer::analyze(art::Event
const& e)
171 std::cout << __COUT_HDR_FL__ <<
"WFViewer Analyzing event " << e.event() << std::endl;
172 static std::size_t evt_cntr = -1;
180 artdaq::Fragments fragments;
182 for(
auto label : fragment_type_labels_)
184 art::Handle<artdaq::Fragments> fragments_with_label;
186 e.getByLabel(
"daq", label, fragments_with_label);
196 for(
auto frag : *fragments_with_label)
198 fragments.emplace_back(frag);
216 artdaq::Fragment::sequence_id_t expected_sequence_id =
217 std::numeric_limits<artdaq::Fragment::sequence_id_t>::max();
220 for(
const auto& frag : fragments)
225 std::unique_ptr<DataGenFragment> drPtr;
230 if(expected_sequence_id ==
231 std::numeric_limits<artdaq::Fragment::sequence_id_t>::max())
233 expected_sequence_id = frag.sequenceID();
236 if(expected_sequence_id != frag.sequenceID())
238 TLOG(TLVL_WARNING,
"WFViewer")
239 <<
"Warning in WFViewer: expected fragment with sequence ID "
240 << expected_sequence_id <<
", received one with sequence ID "
241 << frag.sequenceID();
244 FragmentType fragtype =
static_cast<FragmentType
>(frag.type());
257 case FragmentType::DataGen:
258 drPtr.reset(
new DataGenFragment(frag));
261 throw cet::exception(
"Error in WFViewer: unknown fragment type supplied: " +
262 fragmentTypeToString(fragtype));
265 artdaq::Fragment::fragment_id_t fragment_id = frag.fragmentID();
266 std::size_t ind = id_to_index_[fragment_id];
270 if(!histograms_[ind])
272 histograms_[ind] = std::unique_ptr<TH1D>(
273 new TH1D(Form(
"Fragment_%d_hist", fragment_id),
277 (Double_t)std::numeric_limits<uint8_t>::max()));
279 histograms_[ind]->SetTitle(Form(
280 "Frag %d, Type %s", fragment_id, fragmentTypeToString(fragtype).c_str()));
281 histograms_[ind]->GetXaxis()->SetTitle(
"Vector Mean");
291 case FragmentType::DataGen:
294 auto val = drPtr->dataBegin();
295 double the_mean = calcmean(val->data);
296 std::cout << __COUT_HDR_FL__ <<
"DJN WFViewer: Putting datapoint "
297 << the_mean <<
" into histogram" << std::endl;
298 TLOG(TLVL_INFO,
"WFViewer")
299 <<
"Putting datapoint " << the_mean <<
" into histogram";
301 histograms_[ind]->Fill(the_mean);
306 throw cet::exception(
"Error in WFViewer: unknown fragment type supplied: " +
307 fragmentTypeToString(fragtype));
310 if(evt_cntr % prescale_ - 1 && prescale_ > 1)
317 canvas_[0]->cd(ind + 1);
318 histograms_[ind]->Draw();
320 canvas_[0]->Modified();
321 canvas_[0]->Update();
325 canvas_[0]->Write(
"wf0", TObject::kOverwrite);
331 void ots::WFViewer::beginRun(art::Run
const& e)
333 if(e.run() == current_run_)
335 current_run_ = e.run();
339 fFile_ =
new TFile(outputFileName_.c_str(),
"RECREATE");
343 for(
int i = 0; i < 2; i++)
345 for(
auto& x : graphs_)
347 for(
auto& x : histograms_)
350 for(
int i = 0; i < 1; i++)
352 canvas_[i] = std::unique_ptr<TCanvas>(
new TCanvas(Form(
"wf%d", i)));
353 canvas_[i]->Divide(num_x_plots_, num_y_plots_);
354 canvas_[i]->Update();
355 ((TRootCanvas*)canvas_[i]->GetCanvasImp())->DontCallClose();
358 canvas_[0]->SetTitle(
"ADC Value Distribution");