13#include <sys/inotify.h>
17#include <BmnConverterTools.h>
18#include <BmnFileProp.h>
19#include <BmnGemStripDigit.h>
20#include <BmnTof1Digit.h>
21#include <BmnTrigDigit.h>
22#include <BmnVSPDigit.h>
27int ReceivedSignal = 0;
29void signal_handler(
int sig)
33 printf(
"Received signal %d Exiting\n", sig);
50 signal(SIGINT, SIG_DFL);
57 zmq_ctx_destroy(
_ctx);
81 printf(
"!!! Error Could not detect runID\n");
93 LOGF(info,
"InitDecoder runID = %d", runID);
113 (((decoder->
GetRunId() < 7013) && (
fPeriodID == 8)) ?
"_before_run_7013" :
"")));
121 TString(
"TOF400_StripMap_RUN") + PeriodSetupExt);
123 TString(
"TOF701_StripMap_RUN") + PeriodSetupExt);
161 printf(
"Listening to %s\n", endpoint_addr);
187 if (zmq_setsockopt(
_decoSocket, ZMQ_SNDHWM, &hwm,
sizeof(hwm)) == -1)
188 fprintf(stderr,
"Error in zmq_setsockopt of ZMQ_SNDHWM: %s\n", strerror(errno));
189 size_t vl =
sizeof(hwm);
190 if (zmq_getsockopt(
_decoSocket, ZMQ_SNDHWM, &hwm, &vl) == -1)
191 fprintf(stderr,
"Error getting ZMQ socket ZMQ_SNDHWM size: %s\n", strerror(errno));
192 printf(
"ZMQ_SND HWM %d\n", hwm);
205 _ctx = zmq_ctx_new();
219 zmq_ctx_destroy(
_ctx);
228 _ctx = zmq_ctx_new();
231 size_t vl =
sizeof(rcvBuf);
232 if (zmq_getsockopt(
_decoSocket, ZMQ_RCVBUF, &rcvBuf, &vl) == -1)
233 DBGERR(
"zmq_getsockopt of ZMQ_RCVBUF")
234 printf(
"rcvbuf = %d\n", rcvBuf);
256 }
else if (
_curFile.Length() == 0) {
271 zmq_ctx_destroy(
_ctx);
280 LOGF(error,
"Connection failed! Exit!");
285 Int_t conID_size = 0;
287 Int_t frame_size = 0;
295 bool isListening =
true;
299 signal(SIGINT, signal_handler);
302 while ((!ReceivedSignal) && isListening ) {
304 conID_size = zmq_recv(
_socket_data, &conID,
sizeof(conID), ZMQ_DONTWAIT);
306 if (conID_size == -1) {
315 printf(
"Interrupted!\n");
317 printf(
"Exit cycle!\n");
321 printf(
"ID Receive error #%d : %s\n", errno, zmq_strerror(errno));
325 LOGF(debug,
"ID size = %d\n Id:%x\n", conID_size, conID);
334 bool isReceiving =
true;
336 frame_size = zmq_msg_recv(&msg,
_socket_data, ZMQ_DONTWAIT);
339 if (frame_size == -1) {
340 printf(
"Receive error # %d #%s\n", errno, zmq_strerror(errno));
364 printf(
"buf overflow! msg len %u frame size %u\n", msg_len, frame_size);
365 printf(
"Something wrong! Exit!\n");
380 memcpy(
buf + msg_len, zmq_msg_data(&msg), frame_size);
381 msg_len += frame_size;
386 size_t opt_size =
sizeof(recv_more);
387 if (zmq_getsockopt(
_socket_data, ZMQ_RCVMORE, &recv_more, &opt_size) == -1) {
388 printf(
"ZMQ socket options error #%s\n", zmq_strerror(errno));
393 }
while ((!ReceivedSignal) && recv_more && isReceiving);
402 word =
reinterpret_cast<UInt_t*
>(
buf);
403 while (isListening && (!ReceivedSignal) && (iWord < msg_len /
kNBYTESINWORD) && (!evExit)
406 UInt_t val = *(word + iWord);
409 printf(
"iWord = %u\n", iWord);
410 printf(
"start run\n");
411 lenWords = *(word + ++iWord) /
sizeof(UInt_t);
412 printf(
"payLen = %u words\n", lenWords);
414 printf(
"Wrong payload size!\n");
415 lenBytes = iWord *
sizeof(UInt_t);
417 printf(
" %u will move by %u bytes. skip wrong header\n", msg_len, lenBytes);
418 memmove(&
buf[0], &
buf[lenBytes], msg_len);
423 printf(
"runID = %u\n", runID);
430 lenBytes = iWord *
sizeof(UInt_t);
432 memmove(&
buf[0], &
buf[lenBytes], msg_len);
437 printf(
"iWord = %u\n", iWord);
438 printf(
"stop run\n");
439 lenWords = *(word + ++iWord) /
sizeof(UInt_t);
440 printf(
"payLen = %d words\n", lenWords);
442 printf(
"Wrong payload size!\n");
443 lenBytes = iWord *
sizeof(UInt_t);
445 printf(
" %u will move by %u bytes\n", msg_len, lenBytes);
446 memmove(&
buf[0], &
buf[lenBytes], msg_len);
460 lenBytes = iWord *
sizeof(UInt_t);
463 memmove(&
buf[0], &
buf[lenBytes], msg_len);
469 lenBytes = *(word + ++iWord);
481 printf(
"Init without run header!\n");
483 printf(
"\n\tError in InitDecoder !!\n\n");
490 convertResult =
rawDataDecoder->ConvertRawToRootIterate(word + ++iWord, lenWords);
493 printf(
"convert failed\n");
495 lenBytes = (iWord - 1) *
sizeof(UInt_t);
498 printf(
" %u will move by %u bytes. after conv failed\n", msg_len, lenBytes);
499 memmove(&
buf[0], &
buf[lenBytes], msg_len);
510 if (
fRunID != head->GetRunId()) {
520 printf(
"Send error # %d : %s\n", errno, zmq_strerror(errno));
527 lenBytes = iWord *
sizeof(UInt_t);
533 memmove(&
buf[0], &
buf[lenBytes], msg_len);
543 lenBytes = *(word + ++iWord);
555 printf(
"Init without run header!\n");
557 printf(
"\n\tError in InitDecoder !!\n\n");
562 convertResult =
rawDataDecoder->ConvertRawToRootIterate(word + ++iWord, lenWords);
568 printf(
"stat convert failed\n");
570 lenBytes = iWord *
sizeof(UInt_t);
574 memmove(&
buf[0], &
buf[lenBytes], msg_len);
592 printf(
"Send error # %d : %s\n", errno, zmq_strerror(errno));
604 memmove(&
buf[0], &
buf[lenBytes], msg_len);
614 lenBytes = *(word + ++iWord);
623 printf(
"Init without run header!\n");
625 printf(
"\n\tError in InitDecoder !!\n\n");
630 rawDataDecoder->GetRawConverter()->ParseJsonTLV(word + ++iWord, lenBytes);
633 word =
reinterpret_cast<UInt_t*
>(
buf + lenBytes);
636 memmove(&
buf[0], &
buf[lenBytes], msg_len);
651 printf(
"Wrong data, resetting array!\n");
659 printf(
"I: file closed\n\r");
662 signal(SIGINT, SIG_DFL);
668 LOGF(info,
"fRunID %u\n",
fRunID);
675 printf(
"\n\tError in InitDecoder !!\n\n");
696 printf(
"\tEvCnt %d\n", event_count);
697 printf(
"\tStartTime %s\n", fRunStartTime.AsString());
698 printf(
"\t EndTime %s\n", fRunEndTime.AsString());
702 nullptr, &event_count, 1);
704 cout <<
"New run has been successfully created in the Condition Database with number " <<
fPeriodID <<
":"
709 printf(
"Run %8u is already found in the DB.\n",
fRunID);
717 printf(
"writing StartTime %s\n", fFileStartDate.
AsString());
721 printf(
"writing EndTime %s\n", fFileEndDate.
AsString());
724 printf(
"writing EvCnt %d\n", event_count);
738 while (!ReceivedSignal) {
739 convertResult =
rawDataDecoder->GetRawConverter()->ConvertRawToRootIterateFileRead();
745 printf(
"Converting error\n");
755 if (iterDigi.
header == NULL)
764 printf(
"Send error %d #%s\n", errno, zmq_strerror(errno));
778 struct dirent** namelist;
779 regex re(
"\\w+\\.data");
783 n = scandir(dirname.
Data(), &namelist, 0, versionsort);
788 if (regex_match(namelist[
i]->d_name, re))
789 ret = namelist[
i]->d_name;
810 if ((len == -1) && (errno != EAGAIN))
811 DBG(
"inotify read error!")
814 struct inotify_event*
event = (
struct inotify_event*)&evBuf[
i];
816 if ((event->mask & IN_CREATE) && !(event->mask & IN_ISDIR)) {
817 fileName =
TString(event->name);
818 printf(
"File %s was created!\n", fileName.
Data());
822 i +=
sizeof(
struct inotify_event) + event->len;
837 _ctx = zmq_ctx_new();
844 struct dirent** namelist;
845 const regex re(
".*_run_.*_(\\d+).*.data");
848 vector<pair<BmnFileProp, string>> fileNames;
851 n = scandir(
_curDir, &namelist, 0, versionsort);
852 LOGF(info,
"Found %d files for batch decoding",
n);
860 if (regex_match(namelist[
i]->d_name, re)) {
862 string file_name(namelist[
i]->d_name);
865 fileNames.emplace_back(make_pair(move(prop), move(file_name)));
868 std::sort(fileNames.begin(), fileNames.end(), [](
auto& a,
auto& b) {
869 if (a.first.GetRunId() < b.first.GetRunId())
871 else if (a.first.GetRunId() == b.first.GetRunId()) {
872 if (a.first.GetFileId() < b.first.GetFileId())
874 else if ((a.first.GetFileId() == b.first.GetFileId())
875 && (a.first.GetEventOrder() < b.first.GetEventOrder()))
880 for (
auto& pf : fileNames) {
881 LOGF(info,
"raw file: %s", pf.second.data());
883 int32_t runID = pf.first.GetRunId();
884 if ((runID > 7281) || (runID < 1)) {
895 printf(
"\n\tError in InitDecoder !!\n\n");
912 zmq_close(_decoSocket);
913 zmq_ctx_destroy(_ctx);
920 uint32_t data_port = 21323;
921 signal(SIGINT, signal_handler);
923 struct dirent** namelist;
924 const regex re(
".*_run_.*_(\\d+).*.data");
927 vector<pair<BmnFileProp, string>> fileNames;
929 n = scandir(
_curDir, &namelist, 0, versionsort);
930 LOGF(info,
"Found %d files for batch decoding",
n);
938 if (regex_match(namelist[
i]->d_name, re)) {
940 string file_name(namelist[
i]->d_name);
943 fileNames.emplace_back(make_pair(move(prop), move(file_name)));
946 std::sort(fileNames.begin(), fileNames.end(), [](
auto& a,
auto& b) {
947 if (a.first.GetRunId() < b.first.GetRunId())
949 else if (a.first.GetRunId() == b.first.GetRunId()) {
950 if (a.first.GetFileId() < b.first.GetFileId())
952 else if ((a.first.GetFileId() == b.first.GetFileId())
953 && (a.first.GetEventOrder() < b.first.GetEventOrder()))
958 for (
auto& pf : fileNames) {
961 if (pf.first.GetRunId() < 7281)
963 LOGF(info,
"raw file: %s", pf.second.data());
965 int ret = system(
Form(
"socat -u FILE:%s TCP4-LISTEN:%u,reuseaddr", (
_curDir +
_curFile).Data(), data_port));
967 LOGF(info,
"Socat error code: %d", ret);
971 zmq_close(_decoSocket);
972 zmq_ctx_destroy(_ctx);
974 signal(SIGINT, SIG_DFL);
980 TPRegexp re(
".*\\w+_\\w+_\\w+_(\\d+)[^/]*\\.data");
985 Int_t runID = atoi(str.Data());
1005 bool set_bin_content = !FillOccupancy;
1033 TCanvas* canStripGem =
nullptr;
1034 TCanvas* canStripCsc =
nullptr;
1035 TCanvas* canStripSiBT =
nullptr;
1036 TCanvas* canStripVSP =
nullptr;
1037 vector<PadInfo*> canStripPads;
1038 vector<PadInfo*> canStripPadsGem;
1039 vector<PadInfo*> canStripPadsCsc;
1040 vector<PadInfo*> canStripPadsSiBT;
1041 vector<PadInfo*> canStripPadsVSP;
1047 InitCanvas(
"Silicon", stationSetFSD, canStrip, canStripPads, histStrip);
1049 InitCanvas(
"GEM", stationSetGEM, canStripGem, canStripPadsGem, histStripGem);
1051 InitCanvas(
"CSC", stationSetCSC, canStripCsc, canStripPadsCsc, histStripCsc);
1053 InitCanvas(
"SiBT", stationSetSiBT, canStripSiBT, canStripPadsSiBT, histStripSiBT);
1055 InitCanvas(
"VSP", stationSetVSP, canStripVSP, canStripPadsVSP, histStripVSP);
1065 if (fnames[
i].Length() == 0)
1067 files[
i] =
new TFile(fnames[
i],
"READ");
1068 if (files[
i]->IsOpen())
1069 printf(
"file %s opened\n", fnames[
i].Data());
1071 fprintf(stderr,
"file %s open error\n", fnames[
i].Data());
1074 trees[
i] = (
TTree*)files[
i]->Get(treeNames[
i]);
1075 nEvs = Min(nEvs, trees[
i]->GetEntries());
1076 cout <<
"#recorded entries = " << trees[
i]->
GetEntries() << endl;
1088 if (trees[
i] ==
nullptr)
1090 cout <<
"tree # " <<
i << endl;
1091 if (
i > 0 && runHeaders[
i]) {
1092 printf(
"START (event 1):\t%s\n", runHeaders[
i]->GetRunStartTime().AsString());
1093 printf(
"FINISH (event %lld):\t%s\n", nEvs, runHeaders[
i]->GetRunEndTime().AsString());
1095 for (
Int_t iEv = 0; iEv < nEvs; iEv++) {
1097 if (iEv % 10000 == 0)
1098 cout <<
"iEv = " << iEv << endl;
1115 printf(
"Last event %d\n", curEv);
1119 canStrip->
SaveAs(
Form(
"can-run-%d-sil.png", runID));
1123 canStripGem->
SaveAs(
Form(
"can-run-%d-gem.png", runID));
1127 canStripCsc->
SaveAs(
Form(
"can-run-%d-csc.png", runID));
1131 canStripSiBT->
SaveAs(
Form(
"can-run-%d-sibt.png", runID));
1135 canStripVSP->
SaveAs(
Form(
"can-run-%d-vsp.png", runID));
char * Form(const char *fmt,...)
R__EXTERN TStyle * gStyle
R__EXTERN TSystem * gSystem
BmnStatus
Define status code.
@ kBMNPAYLOAD
spill event.
const uint32_t SYNC_RUN_START
const uint32_t SYNC_EVENT
const uint32_t SYNC_RUN_STOP
const uint32_t kNBYTESINWORD
const uint32_t SYNC_EVENT_OLD
static unique_ptr< BmnCSCStationSet > Create(Int_t period, Int_t stp=0)
void SetVspMapping(TString map)
void SetTOF700ReferenceRun(Int_t n)
UInt_t GetPeriodId() const
void SetTof700Geom(TString geom)
void SetFHCalCalibration(TString cal)
void SetFHCalMapping(TString map)
void SetMSCMapping(TString map)
void SetTof701Mapping(TString PlaceMap, TString StripMap)
void SetUseCalibFile(bool v)
void SetNdetCalibration(TString cal)
void SetTrigPlaceMapping(TString map)
void SetGemMapping(TString map)
void SetHodoCalibration(TString cal)
void SetTof700Mapping(TString map)
void SetTrigChannelMapping(TString file)
void SetScWallMapping(TString map)
void SetCSCMapping(TString map)
void SetSiliconMapping(TString map)
void SetTof400Mapping(TString PlaceMap, TString StripMap)
void SetScWallCalibration(TString cal)
void SetHodoMapping(TString map)
void SetNdetMapping(TString map)
void SetSiBTMapping(TString map)
uint32_t GetRunId() const
static unique_ptr< BmnGemStripStationSet > Create(Int_t period, Int_t stp=0)
static void DrawRef(unique_ptr< TCanvas > &canGemStrip, vector< PadInfo * > *canGemStripPads)
static void FillHists(vector< vector< vector< TH1F * > > > &hist_vec, TClonesArray *digs, bool set_bin_content=false)
static void StripView(TString OrigFileName, TString TestFileName, uint32_t periodID=8, uint32_t runID=0, BmnSetup fSetup=kBMNSETUP, bool FillOccupancy=true)
BmnStatus BatchDirectoryToSocket(TString dirname)
BmnStatus ConnectDigiSocket()
BmnStatus Decode(TString dirname, TString startFile, Bool_t runCurrent)
void ProcessFileRun(TString digiName, UInt_t timeLimit=WAIT_LIMIT)
BmnStatus StartNewRun(uint32_t runID)
static Int_t GetRunIdFromName(TString name)
map< DetectorId, bool > fDetectorSetup
BmnStatus BatchDirectory(TString dirname)
static void InitCanvas(TString DetName, unique_ptr< SST > &stationSet, TCanvas *&canStrip, vector< PadInfo * > &canStripPads, vector< vector< vector< TH1F * > > > *histStrip)
BmnStatus InitDecoder(string)
virtual ~BmnOnlineDecoder()
BmnRawDataDecoder * rawDataDecoder
BmnStatus ConnectDataSocket()
static TString WatchNext(TString dirname, TString filename, Int_t cycleWait)
static unique_ptr< BmnSiBTStationSet > Create(Int_t period, Int_t stp=0)
static unique_ptr< BmnSiliconStationSet > Create(Int_t period, Int_t stp=0)
static unique_ptr< BmnVSPStationSet > Create(Int_t period, Int_t stp=0)
Int_t WriteObject(const T *objptr, Bool_t cacheReuse=kTRUE)
const char * AsString() const
Int_t GetEntriesFast() const
void Delete(Option_t *option="") override
TObject * At(Int_t idx) const override
friend friend class TClonesArray
TObjArray * MatchS(const TString &s, const TString &mods="", Int_t start=0, Int_t nMaxMatch=10)
void SaveAs(const char *filename="", Option_t *option="") const override
TSubString Strip(EStripType s=kTrailing, char c=' ') const
const char * Data() const
const char * Data() const
virtual Int_t GetEntry(Long64_t entry, Int_t getall=0)
virtual Long64_t GetEntries() const
Int_t SetBranchAddress(const char *bname, T **add, TBranch **ptr=nullptr)
UniRun.h Class for the table: run_.
static UniRun * GetRun(int period_number, int run_number)
get run from the database
int SetEndDatetime(TDatime *end_datetime)
set end datetime of the current run
TDatime * GetEndDatetime()
get end datetime of the current run
TDatime GetStartDatetime()
get start datetime of the current run
int * GetEventCount()
get event count of the current run
static UniRun * CreateRun(int period_number, int run_number, int run_type, TDatime start_datetime, TDatime *end_datetime, TString beam_particle, TString target_particle, double *energy, double *field_voltage, int *event_count, int run_quality)
add new run to the database
int SetEventCount(int *event_count)
set event count of the current run
int SetStartDatetime(TDatime start_datetime)
set start datetime of the current run
*brief user defined string literal for JSON values *sa std::size_t n
#define RUN_FILE_CHECK_PERIOD
#define MPD_EVENT_HEAD_WORDS_OLD
#define MPD_EVENT_HEAD_WORDS
#define RAW_DECODER_SOCKET_PORT