BmnRoot
Loading...
Searching...
No Matches
sampler.cxx
Go to the documentation of this file.
1#include <FairRootManager.h>
2#include <RootSerializer.h>
3#include <TFile.h>
4#include <TString.h>
5#include <TTree.h>
6#include <fairmq/Device.h>
7#include <fairmq/runDevice.h>
8#include <string>
9#include <thread>
10
11namespace bpo = boost::program_options;
12
13namespace bmn::online
14{
15struct Sampler : fair::mq::Device
16{
17 protected:
18 static constexpr auto OUTPUT_CHANNEL_NAME = "out-channel";
19
20 void Init() override
21 {
22 fEventsFilePath = fConfig->GetValue<std::string>("file-path");
23 fEventDispatchDelay = fConfig->GetValue<UShort_t>("dispatch-delay");
24 fCurrentEvent = fConfig->GetValue<size_t>("start-event");
25 }
26
27 void InitTask() override
28 {
29 fEventFile = std::unique_ptr<TFile>(TFile::Open(fEventsFilePath, "read"));
30 fEventData = std::unique_ptr<TTree>(fEventFile->Get<TTree>("bmndata"));
31 fNEvents = fEventData->GetEntries();
32
33 LOG(info) << "Number of events in the file: " << fNEvents;
34 }
35
36 Bool_t ConditionalRun() override
37 {
38 LOG(info) << "Event #" << fCurrentEvent;
39
40 if (fCurrentEvent > fNEvents) {
41 return kFALSE;
42 }
43
44 std::unique_ptr<TTree> eventData(fEventData->CopyTree("", "", 1, fCurrentEvent));
45
46 auto message = NewMessage();
47 RootSerializer().Serialize(*message, eventData);
48
49 if (Send(message, OUTPUT_CHANNEL_NAME) < 0) {
50 LOG(error) << "Sending message failed!";
51 return kFALSE;
52 }
53
54 fCurrentEvent++;
55
56 std::this_thread::sleep_for(std::chrono::milliseconds(fEventDispatchDelay));
57
58 return kTRUE;
59 }
60
61 private:
62 TString fEventsFilePath = "";
63 UShort_t fEventDispatchDelay = 1000;
64 size_t fCurrentEvent = 0;
65
66 std::unique_ptr<TFile> fEventFile;
67 std::unique_ptr<TTree> fEventData;
68 size_t fNEvents = 0;
69};
70} // namespace bmn::online
71
72void addCustomOptions(bpo::options_description& options)
73{
74 auto op = options.add_options();
75
76 op("file-path", bpo::value<std::string>()->required(), "Path to the event file");
77 op("dispatch-delay", bpo::value<UShort_t>()->default_value(1000),
78 "Delay before dispatching an event in milliseconds");
79 op("start-event", bpo::value<size_t>()->default_value(0), "Start event number");
80}
81
82std::unique_ptr<fair::mq::Device> getDevice(fair::mq::ProgOptions&)
83{
84 return std::make_unique<bmn::online::Sampler>();
85}
std::unique_ptr< fair::mq::Device > getDevice(fair::mq::ProgOptions &)
Definition sampler.cxx:82
void addCustomOptions(bpo::options_description &options)
Definition sampler.cxx:72
static constexpr auto OUTPUT_CHANNEL_NAME
Definition sampler.cxx:18
void Init() override
Definition sampler.cxx:20
Bool_t ConditionalRun() override
Definition sampler.cxx:36
void InitTask() override
Definition sampler.cxx:27