-
Notifications
You must be signed in to change notification settings - Fork 3
/
Scaler.cxx
481 lines (391 loc) · 15.7 KB
/
Scaler.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
#include <chrono>
#include <thread>
#include <future>
#include <map>
#include <iostream>
#include <boost/format.hpp>
#include <cmath>
#include <fairmq/runDevice.h>
#include "TimeFrameHeader.h"
#include "SubTimeFrameHeader.h"
#include "STFBuilder.h"
#include "AmQStrTdcData.h"
#include "utility/Reporter.h"
#include "utility/HexDump.h"
#include "utility/MessageUtil.h"
#include "Scaler.h"
namespace bpo = boost::program_options;
//______________________________________________________________________________
void addCustomOptions(bpo::options_description& options)
{
{
using opt = Scaler::OptionKey;
options.add_options()
(opt::NumSource.data(), bpo::value<std::string>()->default_value("1"), "Number of source endpoint")
(opt::BufferTimeoutInMs.data(), bpo::value<std::string>()->default_value("100000"), "Buffer timeout in milliseconds")
(opt::RunNumber.data(), bpo::value<std::string>()->default_value("1"), "Run number (integer) given by DAQ plugin")
(opt::InputChannelName.data(), bpo::value<std::string>()->default_value("in"), "Name of the input channel")
(opt::OutputChannelName.data(), bpo::value<std::string>()->default_value("out"), "Name of the output channel")
(opt::UpdateInterval.data(), bpo::value<std::string>()->default_value("1000"), "Canvas update rate in milliseconds")
(opt::ServerUri.data(), bpo::value<std::string>()->default_value("tcp://127.0.0.1:6379/3"), "Redis server URI (if empty, the same URI of the service registry is used.)")
;
}
{ // FileUtil's options ------------------------------
using fu = nestdaq::FileUtil;
//using fopt = fu::OptionKey;
//using sopt = fu::SplitOption;
//using oopt = fu::OpenmodeOption;
auto &desc = fu::GetDescriptions();
fu::AddOptions(options);
}
}
//______________________________________________________________________________
std::unique_ptr<fair::mq::Device> getDevice(fair::mq::ProgOptions& /*config*/)
{
return std::make_unique<Scaler>();
}
//______________________________________________________________________________
Scaler::Scaler()
: FairMQDevice()
{
}
//______________________________________________________________________________
bool Scaler::HandleData(FairMQParts& parts, int index)
{
namespace STF = SubTimeFrame;
namespace Data = AmQStrTdc::Data;
using Bits = Data::Bits;
auto stfHeader = reinterpret_cast<STF::Header*>(parts.At(0)->GetData());
int nCh = 0;
if(stfHeader->femType == 1 || stfHeader->femType == 3 || stfHeader->femType == 6){
nCh = 128;
}else if(stfHeader->femType == 2 || stfHeader->femType == 5){
nCh = 64;
}
if (nCh != hScaler->GetNBins()){
hScaler->Reset();
hScaler->SetNBins(nCh);
hScaler->SetMinimum(0.);
hScaler->SetMaximum((double)nCh);
hScalerPrev->Reset();
hScalerPrev->SetNBins(nCh);
hScalerPrev->SetMinimum(0.);
hScalerPrev->SetMaximum((double)nCh);
}
(void)index;
assert(parts.Size()>=2);
Reporter::AddInputMessageSize(parts);
{
auto dt = std::chrono::steady_clock::now() - fPrevUpdate;
if (std::chrono::duration_cast<std::chrono::milliseconds>(dt).count() > fUpdateIntervalInMs) {
try {
fTsHeartbeatCounterKey = join({"scaler", fdevId, "heartbeatCounter"}, fSeparator);
data_store->ts_add(fTsHeartbeatCounterKey, "*", std::to_string(tsHeartbeatCounter));
fTsHeartbeatFlagKey = join({"scaler", fdevId, "heartbeatFlag"}, fSeparator);
data_store->ts_add(fTsHeartbeatFlagKey, "*", std::to_string(tsHeartbeatFlag));
std::string fCountsVsCh = join({"scaler", fdevId, "counts-vs-ch"}, fSeparator);
data_store->write(fCountsVsCh,Slowdashify(*hScaler));
uint64_t deltaHeartbeatCounter = tsHeartbeatCounter - fPrevHeartbeatCounter;
double dt_in_sec = deltaHeartbeatCounter * 0.000524; /* 1 heart beat = 512 us */
if (dt_in_sec == 0.) {
dt_in_sec = 1.;
}
UH1Book hCountRate(*hScaler);
hCountRate.Subtract(*hScalerPrev);
for (int i=1; i <= hScaler->GetNBins(); i++) {
hCountRate.SetBinContent(i, hCountRate.GetBinContent(i)/dt_in_sec);
}
std::string fRateVsCh = join({"scaler", fdevId, "rate-vs-ch"}, fSeparator);
data_store->write(fRateVsCh,Slowdashify(hCountRate));
// for checking the flag bit for AmQ
std::string fFlagBit = join({"flag", fdevId, "bit"}, fSeparator);
data_store->write(fFlagBit,Slowdashify(*hFlag));
LOG(info) << "tsHeartbeatCounter: " << tsHeartbeatCounter;
LOG(info) << "tsHeartbeatFlag : " << tsHeartbeatFlag;
} catch (const std::exception& e) {
LOG(error) << "Scaler " << __FUNCTION__ << " exception : what() " << e.what();
} catch (...) {
LOG(error) << "Scaler " << __FUNCTION__ << " exception : unknown ";
}
fPrevUpdate = std::chrono::steady_clock::now();
fPrevHeartbeatCounter = tsHeartbeatCounter;
*hScalerPrev = *hScaler;
hFlag->Print();
hFlag->Draw();
hScaler->Print();
hScaler->Draw();
}
}
// Check(stfs);
auto nmsg = parts.Size();
// LOG(debug) <<"nmsg: " << nmsg;
for(int imsg = 1; imsg < nmsg; ++imsg){
const auto& msg = parts.At(imsg);
auto mSize = msg->GetSize();
auto nword = mSize / sizeof(uint64_t);
auto msgStart = reinterpret_cast<uint64_t *>(msg->GetData());
uint64_t htype[2];
htype[1] = *(msgStart + 2); // if nword > 2 (tdc hits are inclued), data start.
htype[0] = *(msgStart + (nword - 2));
// LOG(debug) << "htype[0]: " << std::hex << htype[0];
// LOG(debug) << "htype[1]: " << std::hex << htype[1];
int nchk;
if(htype[0]!=htype[1]){
nchk = 2;
}else if(htype[0]==htype[1]){
nchk = 1;
}
// nchk = 1; only for checking heartbeat frame
for(int iw = 0; iw < nchk; iw++){
auto wb = reinterpret_cast<Bits*>(&htype[iw]);
// LOG(debug) << "msgStart: " << *msgStart;
// LOG(debug) << " word =" << std::hex << wb->raw << std::dec;
// LOG(debug) << " head =" << std::hex << wb->head << std::dec;
switch (wb->head) {
case Data::Heartbeat:
{
// LOG(info) << "HBF comes: " << std::hex << wb->raw;
if(fDebug){
LOG(debug) << "== Data::Heartbeat --> ";
LOG(debug) << "hbframe#: " << std::hex << wb->hbframe << std::dec;
LOG(debug) << "toffset : " << std::hex << wb->toffset << std::dec;
LOG(debug) << "hbfalg: " << std::hex << wb->hbflag << std::dec;
LOG(debug) << "hbtype1: " << std::hex << wb->hbtype1 << std::dec;
LOG(debug) << "head =" << std::hex << wb->head << std::dec;
LOG(debug) << "== scaler --> ";
}
if(fDebug){
LOG(debug) << "============================";
LOG(debug) << "HB frame : " << wb->hbframe;
LOG(debug) << "timeFrameId : " << stfHeader->timeFrameId;
LOG(debug) << "# of HB: " << wb->hbframe - stfHeader->timeFrameId;
LOG(debug) << "hbflag: " << wb->hbflag;
}
tsHeartbeatCounter++;
for(int i=0; i<16; i++){
auto bit_sum = (wb->hbflag >> i) & 0x01;
FlagSum[i] = fpreFlagSum[i] + bit_sum;
if (bit_sum) {
hFlag->Fill(i);
}
}
tsHeartbeatFlag = wb->hbflag;
for(int i=0; i<16; i++)
fpreFlagSum[i] = FlagSum[i];
// LOG(info) << "HBF Count: " << tsHeartbeatCounter;
// LOG(info) << "HBF Flag: " << tsHeartbeatFlag;
break;
}
case Data::Heartbeat2nd:
{
if(fDebug){
LOG(debug) << "== Data::Heartbeat2nd --> ";
LOG(debug) << "transSize: " << std::hex << wb->transSize << std::dec;
LOG(debug) << "geneSize : " << std::hex << wb->geneSize << std::dec;
LOG(debug) << "userReg: " << std::hex << wb->userReg << std::dec;
LOG(debug) << "hbtype2: " << std::hex << wb->hbtype2 << std::dec;
LOG(debug) << "head =" << std::hex << wb->head << std::dec;
LOG(debug) << "== scaler --> ";
}
break;
}
case Data::Data:
{
auto msgBegin = reinterpret_cast<Data::Word*>(msg->GetData());
auto msgSize = msg->GetSize();
auto nWord = msgSize / sizeof(uint64_t);
for(long unsigned int i = 0; i < nWord; ++i){
auto iwb = reinterpret_cast<Bits*>(msgBegin+i);
if(fDebug){
if( (stfHeader->femType == 1) || (stfHeader->femType==3) || (stfHeader->femType==6) )
LOG(debug) << "LRtdc: " << std::hex << iwb->tdc << std::dec;
if(stfHeader->femType == 2 || (stfHeader->femType==5))
LOG(debug) << "HRtdc: " << std::hex << iwb->hrtdc<< std::dec;
}
if( (stfHeader->femType == 1) || (stfHeader->femType==3) || (stfHeader->femType==6) ) {
hScaler->Fill(static_cast<int>(iwb->ch)+1);
}else if(stfHeader->femType == 2 || (stfHeader->femType==5)) {
hScaler->Fill(static_cast<int>(iwb->ch)+1);
}
}//
}
break;
case Data::Trailer:
break;
case Data::ThrottlingT1Start:
break;
case Data::ThrottlingT1End:
break;
case Data::ThrottlingT2Start:
break;
case Data::ThrottlingT2End:
break;
default:
LOG(error) << " unknown Head : " << std::hex << wb->head << std::dec
<< " FEMId: " << std::hex << stfHeader->femId;
break;
}
}
}
/* make scaler data */
auto outdata = std::make_unique<std::vector<uint64_t>>();
auto scHeader = std::make_unique<STF::Header>();
for ( int ich = 0; ich < nCh; ++ich){
outdata->push_back(hScaler->GetBinContent(ich+1));
}
/* send data to Filesink */
FairMQParts outParts;
fFEMId = stfHeader->femId;
// auto femid = stfHeader->femId & 0xff;
scHeader->femType = 200;
scHeader->femId = stfHeader->femId;
scHeader->length = outdata->size()*sizeof(uint64_t) + sizeof(STF::Header);
scHeader->timeFrameId = stfHeader->timeFrameId;
scHeader->numMessages = stfHeader->numMessages;
scHeader->timeSec = stfHeader->timeSec;
scHeader->timeUSec = stfHeader->timeUSec;
FairMQMessagePtr tmsg = MessageUtil::NewMessage(*this, std::move(scHeader));
#if 0
auto n = tmsg->GetSize()/sizeof(uint64_t);
LOG(debug) << "tmsg: " << n << " " << tmsg->GetSize();
std::for_each(reinterpret_cast<uint64_t*>(tmsg->GetData()),
reinterpret_cast<uint64_t*>(tmsg->GetData()) + n,
::HexDump{4});
#endif
outParts.AddPart(std::move(tmsg));
//
FairMQMessagePtr smsg;
if(outdata->size() > 0){
smsg = MessageUtil::NewMessage(*this, std::move(outdata));
#if 0
auto m = smsg->GetSize()/sizeof(uint64_t);
LOG(debug) << "smsg: " << m << " " << smsg->GetSize();
std::for_each(reinterpret_cast<uint64_t*>(smsg->GetData()),
reinterpret_cast<uint64_t*>(smsg->GetData()) + m,
::HexDump{4});
#endif
outParts.AddPart(std::move(smsg));
}
while(Send(outParts, fOutputChannelName) < 0){
if (NewStatePending()) {
LOG(info) << "Device is not RUNNING";
return false;
}
LOG(error) << "Failed to enqueue OutputChannel";
}
return true;
}
//______________________________________________________________________________
void Scaler::Init()
{
Reporter::Instance(fConfig);
}
//______________________________________________________________________________
void Scaler::InitTask()
{
using opt = OptionKey;
fBufferTimeoutInMs = std::stoi(fConfig->GetProperty<std::string>(opt::BufferTimeoutInMs.data()));
LOG(debug) << "fBufferTimeoutInMs: "<< fBufferTimeoutInMs ;
fInputChannelName = fConfig->GetProperty<std::string>(opt::InputChannelName.data());
fOutputChannelName = fConfig->GetProperty<std::string>(opt::OutputChannelName.data());
auto numSubChannels = GetNumSubChannels(fInputChannelName);
fNumSource = 0;
for (auto i=0u; i<numSubChannels; ++i) {
fNumSource += GetNumberOfConnectedPeers(fInputChannelName, i);
}
LOG(debug) << "fNumSource: "<< fNumSource;
LOG(debug) << " input channel : name = " << fInputChannelName
<< " num = " << GetNumSubChannels(fInputChannelName)
<< " num peer = " << GetNumberOfConnectedPeers(fInputChannelName,0);
LOG(debug) << " output channel : name = " << fOutputChannelName;
LOG(debug) << " number of source = " << fNumSource;
fUpdateIntervalInMs = std::stoi(fConfig->GetProperty<std::string>(opt::UpdateInterval.data()));
LOG(debug) << "fUpdateIntervalInMs: "<< fUpdateIntervalInMs ;
fHbc.resize(fNumSource);
using opt = Scaler::OptionKey;
std::string serverUri = fConfig->GetProperty<std::string>(opt::ServerUri.data());
LOG(debug) << "serverUri: " << serverUri;
data_store = new RedisDataStore(serverUri);
fdevId = fConfig->GetProperty<std::string>("id");
LOG(debug) << "fdevId: " << fdevId;
fTopPrefix = "scaler";
fSeparator = fConfig->GetProperty<std::string>("separator");
LOG(debug) << "fSeparator: " << fSeparator;
const auto fCreatedTimeKey = join({fTopPrefix, opt::CreatedTimePrefix.data()}, fSeparator);
const auto fHostNameKey = join({fTopPrefix, opt::HostnamePrefix.data()}, fSeparator);
const auto fIpAddressKey = join({fTopPrefix, opt::HostIpAddressPrefix.data()}, fSeparator);
data_store->hset(fCreatedTimeKey, fdevId, "n/a");
data_store->hset(fHostNameKey, fdevId, fConfig->GetProperty<std::string>("hostname"));
data_store->hset(fIpAddressKey, fdevId, fConfig->GetProperty<std::string>("host-ip"));
Reporter::Reset();
fFile = std::make_unique<nestdaq::FileUtil>();
fFile->Init(fConfig->GetVarMap());
fFile->Print();
fFileExtension = fFile->GetExtension();
//
LOG(debug) << "Handle";
OnData(fInputChannelName, &Scaler::HandleData);
}
//______________________________________________________________________________
void Scaler::PreRun()
{
using opt = Scaler::OptionKey;
if (fConfig->Count(opt::RunNumber.data())) {
fRunNumber = std::stoll(fConfig->GetProperty<std::string>(opt::RunNumber.data()));
LOG(debug) << " RUN number = " << fRunNumber;
}
fFile->SetRunNumber(fRunNumber);
fFile->ClearBranch();
fFile->Open();
for(int i=0; i<10; ++i){
FlagSum[i] = 0;
fpreFlagSum[i] = 0;
}
hScaler = new UH1Book("ScalerHisto",128,0.,128.);
hScalerPrev = new UH1Book("ScalerHistoPrev",128,0.,128.);
hFlag = new UH1Book("FlagHisto",16,0.,16.);
hFlagPrev = new UH1Book("FlagHistoPrev",16,0.,16.);
}
//______________________________________________________________________________
void Scaler::PostRun()
{
int nrecv=0;
if (fChannels.count(fInputChannelName) > 0) {
auto n = fChannels.count(fInputChannelName);
for (auto i = 0u; i < n; ++i) {
if (fDebug) {
LOG(debug) << " #i : "<< i;
}
while(true) {
FairMQParts part;
if (Receive(part, fInputChannelName, i, 1000) <= 0) {
// LOG(debug) << __func__ << " no data received " << nrecv;
++nrecv;
if (nrecv > 10) {
break;
}
std::this_thread::sleep_for(std::chrono::milliseconds(200));
} else {
// LOG(debug) << __func__ << " data comes..";
}
}
}
}// for clean up
std::stringstream ss;
ss << "Scaler data at PostRun()" << std::endl;
ss << "== scaler --> module: " << std::hex << fFEMId << std::endl;
for (int i = 0; i < hScaler->GetNBins(); i++){
ss << "ch: " << i << " sc: " << hScaler->GetBinContent(i+1) << std::endl;
}
ss << " --> scaler ==" << std::endl;
fFile->Write(reinterpret_cast<const char *>(ss.str().c_str()), ss.str().length());
ss.str("");
ss.clear(std::stringstream::goodbit);
fFile->Close();
fFile->ClearBranch();
delete hScaler;
delete hScalerPrev;
delete hFlag;
delete hFlagPrev;
LOG(debug) << __func__ << " done";
}