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/Fragments.hh"
10 #include "otsdaq-demo/Overlays/DataGenFragment.hh"
11 #include "otsdaq-demo/Overlays/FragmentType.hh"
13 #include "cetlib/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_;
70 ots::WFViewer::WFViewer(fhicl::ParameterSet
const& ps)
72 , prescale_(ps.get<int>(
"prescale"))
75 ps.get<std::size_t>(
"num_x_plots", std::numeric_limits<std::size_t>::max()))
77 ps.get<std::size_t>(
"num_y_plots", std::numeric_limits<std::size_t>::max()))
78 , fragment_type_labels_(ps.get<std::vector<std::string>>(
"fragment_type_labels"))
79 , fragment_ids_(ps.get<std::vector<artdaq::Fragment::fragment_id_t>>(
"fragment_ids"))
80 , graphs_(fragment_ids_.size())
81 , histograms_(fragment_ids_.size())
82 , outputFileName_(ps.get<std::string>(
"fileName",
"otsdaqdemo_onmon.root"))
83 , writeOutput_(ps.get<bool>(
"write_to_file", false))
85 __COUT__ <<
"WFViewer CONSTRUCTOR BEGIN!!!!" << std::endl;
87 if(num_x_plots_ == std::numeric_limits<std::size_t>::max() ||
88 num_y_plots_ == std::numeric_limits<std::size_t>::max())
90 switch(fragment_ids_.size())
93 num_x_plots_ = num_y_plots_ = 1;
115 num_x_plots_ = num_y_plots_ =
116 static_cast<std::size_t
>(ceil(sqrt(fragment_type_labels_.size())));
123 for(std::size_t i_f = 0; i_f < fragment_ids_.size(); ++i_f)
125 id_to_index_[fragment_ids_[i_f]] = i_f;
132 sort(fragment_type_labels_.begin(), fragment_type_labels_.end());
133 fragment_type_labels_.erase(
134 unique(fragment_type_labels_.begin(), fragment_type_labels_.end()),
135 fragment_type_labels_.end());
137 gStyle->SetOptStat(
"irm");
138 gStyle->SetMarkerStyle(22);
139 gStyle->SetMarkerColor(4);
141 std::cout << __COUT_HDR_FL__ <<
"WFViewer CONSTRUCTOR END" << std::endl;
144 double ots::WFViewer::calcmean(
const float* data)
150 for(ii = 0; ii < 10; ii++)
156 std::cout <<
"WFViewer mean is " << mean <<
" data[0] is " << data[0]
157 <<
" data[1] is " << data[1] <<
" 2=" << data[2] <<
" 3=" << data[3]
160 std::cout <<
"WFViewer mean is " << mean <<
" data[15997] is " << data[15997]
161 <<
" data[15998] is " << data[15998] <<
" 2=" << data[15999]
162 <<
" 3=" << data[15999] << std::endl
170 void ots::WFViewer::analyze(art::Event
const& e)
172 std::cout << __COUT_HDR_FL__ <<
"WFViewer Analyzing event " << e.event() << std::endl;
173 static std::size_t evt_cntr = -1;
181 artdaq::Fragments fragments;
183 for(
auto label : fragment_type_labels_)
185 art::Handle<artdaq::Fragments> fragments_with_label;
187 e.getByLabel(
"daq", label, fragments_with_label);
197 for(
auto frag : *fragments_with_label)
199 fragments.emplace_back(frag);
217 artdaq::Fragment::sequence_id_t expected_sequence_id =
218 std::numeric_limits<artdaq::Fragment::sequence_id_t>::max();
221 for(
const auto& frag : fragments)
226 std::unique_ptr<DataGenFragment> drPtr;
231 if(expected_sequence_id ==
232 std::numeric_limits<artdaq::Fragment::sequence_id_t>::max())
234 expected_sequence_id = frag.sequenceID();
237 if(expected_sequence_id != frag.sequenceID())
239 TLOG(TLVL_WARNING,
"WFViewer")
240 <<
"Warning in WFViewer: expected fragment with sequence ID "
241 << expected_sequence_id <<
", received one with sequence ID "
242 << frag.sequenceID();
245 FragmentType fragtype =
static_cast<FragmentType
>(frag.type());
258 case FragmentType::DataGen:
259 drPtr.reset(
new DataGenFragment(frag));
262 throw cet::exception(
"Error in WFViewer: unknown fragment type supplied: " +
263 fragmentTypeToString(fragtype));
266 artdaq::Fragment::fragment_id_t fragment_id = frag.fragmentID();
267 std::size_t ind = id_to_index_[fragment_id];
272 if(!histograms_[ind])
274 histograms_[ind] = std::unique_ptr<TH1D>(
275 new TH1D(Form(
"Fragment_%d_hist", fragment_id),
279 (Double_t)std::numeric_limits<uint8_t>::max()));
281 histograms_[ind]->SetTitle(Form(
282 "Frag %d, Type %s", fragment_id, fragmentTypeToString(fragtype).c_str()));
283 histograms_[ind]->GetXaxis()->SetTitle(
"Vector Mean");
293 case FragmentType::DataGen:
296 auto val = drPtr->dataBegin();
297 double the_mean = calcmean(val->data);
298 std::cout << __COUT_HDR_FL__ <<
"DJN WFViewer: Putting datapoint "
299 << the_mean <<
" into histogram" << std::endl;
300 TLOG(TLVL_INFO,
"WFViewer")
301 <<
"Putting datapoint " << the_mean <<
" into histogram";
303 histograms_[ind]->Fill(the_mean);
308 throw cet::exception(
"Error in WFViewer: unknown fragment type supplied: " +
309 fragmentTypeToString(fragtype));
312 if(evt_cntr % prescale_ - 1 && prescale_ > 1)
319 canvas_[0]->cd(ind + 1);
320 histograms_[ind]->Draw();
322 canvas_[0]->Modified();
323 canvas_[0]->Update();
327 canvas_[0]->Write(
"wf0", TObject::kOverwrite);
333 void ots::WFViewer::beginRun(art::Run
const& e)
335 if(e.run() == current_run_)
337 current_run_ = e.run();
341 fFile_ =
new TFile(outputFileName_.c_str(),
"RECREATE");
345 for(
int i = 0; i < 2; i++)
347 for(
auto& x : graphs_)
349 for(
auto& x : histograms_)
352 for(
int i = 0; i < 1; i++)
354 canvas_[i] = std::unique_ptr<TCanvas>(
new TCanvas(Form(
"wf%d", i)));
355 canvas_[i]->Divide(num_x_plots_, num_y_plots_);
356 canvas_[i]->Update();
357 ((TRootCanvas*)canvas_[i]->GetCanvasImp())->DontCallClose();
360 canvas_[0]->SetTitle(
"ADC Value Distribution");