DABC (Data Acquisition Backbone Core)  2.9.9
mbsprint.cxx
Go to the documentation of this file.
1 // $Id: mbsprint.cxx 4479 2020-04-15 14:30:52Z linev $
2 
3 /************************************************************
4  * The Data Acquisition Backbone Core (DABC) *
5  ************************************************************
6  * Copyright (C) 2009 - *
7  * GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
8  * Planckstr. 1, 64291 Darmstadt, Germany *
9  * Contact: http://dabc.gsi.de *
10  ************************************************************
11  * This software can be used under the GPL license *
12  * agreements as stated in LICENSE.txt file *
13  * which is part of the distribution. *
14  ************************************************************/
15 
16 #include <cstdio>
17 #include <cstring>
18 #include <ctime>
19 
20 #include "dabc/Url.h"
21 #include "dabc/api.h"
22 
23 #include "mbs/api.h"
24 
25 int usage(const char* errstr = nullptr)
26 {
27  if (errstr!=0) {
28  printf("Error: %s\n\n", errstr);
29  }
30 
31  printf("utility for printing MBS events\n");
32  printf("mbsprint source [args]\n\n");
33  printf("Following source kinds are supported:\n");
34  printf(" lmd://path/file.lmd - LMD file reading\n");
35  printf(" mbs://mbsnode/Transport - MBS transport server\n");
36  printf(" mbs://mbsnode/Stream - MBS stream server\n");
37  printf("Additional arguments:\n");
38  printf(" -tmout value - maximal time in seconds for waiting next event (default 5)\n");
39  printf(" -maxage value - maximal age time for events, if expired queue are cleaned (default off)\n");
40  printf(" -num number - number of events to print\n");
41  printf(" -hex - print raw data in hex form\n");
42  printf(" -dec - print raw data in decimal form\n");
43  printf(" -long - print raw data in 4-bytes format\n");
44  printf(" -short - print raw data in 2-bytes format\n");
45  printf(" -slow subevid - print subevents with id as slow control record (see mbs/SlowControlData.h file)\n");
46 
47  return errstr ? 1 : 0;
48 }
49 
51 {
53  if (!rec.Read(sub->RawData(), sub->RawDataSize())) {
54  printf(" SlowControl record format failure\n");
55  return;
56  }
57 
58  time_t tm = rec.GetEventTime();
59 
60  printf(" SlowControl data evid:%u longs:%u doubles:%u time:%s",
61  (unsigned) rec.GetEventId(), rec.NumLongs(), rec.NumDoubles(), ctime(&tm));
62 
63  for (unsigned num=0;num<rec.NumLongs();num++) {
64  printf(" %s = %ld\n", rec.GetLongName(num).c_str(), (long int) rec.GetLongValue(num));
65  }
66 
67  for (unsigned num=0;num<rec.NumDoubles();num++) {
68  printf(" %s = %f\n", rec.GetDoubleName(num).c_str(), rec.GetDoubleValue(num));
69  }
70 }
71 
72 
73 
74 int main(int argc, char* argv[])
75 {
76  if (argc<2) return usage();
77 
78  long number = 10;
79  double tmout(5.), maxage(-1.), debug_delay(-1.);
80  unsigned slowsubevid(0);
81 
82  bool printdata(false), ashex(true), aslong(true), showrate(false), reconnect(false);
83 
84  int n = 1;
85  while (++n<argc) {
86  if (strcmp(argv[n],"-hex")==0) { printdata = true; ashex = true; } else
87  if (strcmp(argv[n],"-dec")==0) { printdata = true; ashex = false; } else
88  if (strcmp(argv[n],"-long")==0) { printdata = true; aslong = true; } else
89  if (strcmp(argv[n],"-short")==0) { printdata = true; aslong = false; } else
90  if (strcmp(argv[n],"-rate")==0) { showrate = true; reconnect = true; } else
91  if ((strcmp(argv[n],"-num")==0) && (n+1<argc)) { dabc::str_to_lint(argv[++n], &number); } else
92  if ((strcmp(argv[n],"-tmout")==0) && (n+1<argc)) { dabc::str_to_double(argv[++n], &tmout); } else
93  if ((strcmp(argv[n],"-maxage")==0) && (n+1<argc)) { dabc::str_to_double(argv[++n], &maxage); } else
94  if ((strcmp(argv[n],"-delay")==0) && (n+1<argc)) { dabc::str_to_double(argv[++n], &debug_delay); } else
95  if ((strcmp(argv[n],"-slow")==0) && (n+1<argc)) { dabc::str_to_uint(argv[++n], &slowsubevid); } else
96  if ((strcmp(argv[n],"-help")==0) || (strcmp(argv[n],"?")==0)) return usage(); else
97  return usage("Unknown option");
98  }
99 
100  std::string src = argv[1];
101  dabc::Url url(src);
102 
103  if (url.IsValid()) {
104  if (url.GetProtocol().empty()) {
105 
106  if (src.find(".lmd") != std::string::npos)
107  src = std::string("lmd://") + src;
108  else
109  src = std::string("mbss://") + src;
110  }
111 
112  if (reconnect && !url.HasOption("reconnect")) {
113  if (url.GetOptions().empty())
114  src+="?reconnect";
115  else
116  src+="&reconnect";
117  }
118  }
119 
121 
122  if (ref.null()) return 1;
123 
124  mbs::EventHeader* evnt(0);
125 
126  long cnt(0), lastcnt(0);
127 
128  dabc::TimeStamp last = dabc::Now();
129  dabc::TimeStamp first = last;
130  dabc::TimeStamp lastevtm = last;
131 
133 
134  while (!dabc::CtrlCPressed()) {
135 
136  evnt = ref.NextEvent(maxage > 0 ? maxage/2. : 1., maxage);
137  if (debug_delay>0) dabc::Sleep(debug_delay);
138 
139  dabc::TimeStamp curr = dabc::Now();
140 
141  if (evnt!=0) {
142  cnt++;
143  lastevtm = curr;
144  } else
145  if (curr - lastevtm > tmout) break;
146 
147  if (showrate) {
148  double tm = curr - last;
149  if (tm>=0.3) {
150  printf("\rTm:%6.1fs Ev:%8ld Rate:%8.2f Ev/s", first.SpentTillNow(), cnt, (cnt-lastcnt)/tm);
151  fflush(stdout);
152  last = curr;
153  lastcnt = cnt;
154  }
155 
156  continue;
157  }
158 
159  if (evnt==0) continue;
160 
161  evnt->PrintHeader();
162  mbs::SubeventHeader* sub = 0;
163  while ((sub = evnt->NextSubEvent(sub)) != 0) {
164  sub->PrintHeader();
165  if ((slowsubevid!=0) && (sub->fFullId==slowsubevid)) {
166  PrintSlowSubevent(sub);
167  } else
168  if (printdata) sub->PrintData(ashex, aslong);
169  }
170 
171  if (cnt >= number) break;
172  }
173 
174  if (showrate) {
175  printf("\n");
176  fflush(stdout);
177  }
178 
179  ref.Disconnect();
180 
181  return 0;
182 }
Uniform Resource Locator interpreter.
Definition: Url.h:33
bool HasOption(const std::string &optname) const
Definition: Url.h:70
std::string GetProtocol() const
Definition: Url.h:57
std::string GetOptions() const
Definition: Url.h:63
bool IsValid() const
Definition: Url.h:55
Handle to organize readout of MBS data source.
Definition: api.h:74
static ReadoutHandle Connect(const std::string &url)
Connect with MBS server.
Definition: api.h:79
bool null() const
Check if handle is initialized.
Definition: api.h:85
mbs::EventHeader * NextEvent(double tmout=1.0, double maxage=-1.)
Retrieve next event from the server One could specify timeout (how long one should wait for next even...
Definition: api.cxx:190
bool Disconnect()
Disconnect from MBS server.
Definition: api.cxx:171
Record for manipulation with slow control data.
std::string GetDoubleName(unsigned indx)
int64_t GetLongValue(unsigned indx)
uint32_t GetEventId() const
std::string GetLongName(unsigned indx)
unsigned NumLongs() const
bool Read(void *buf, unsigned bufsize)
uint32_t GetEventTime() const
double GetDoubleValue(unsigned indx)
unsigned NumDoubles() const
bool reconnect
Definition: hldprint.cxx:994
bool showrate
Definition: hldprint.cxx:994
int main(int argc, char *argv[])
Definition: mbsprint.cxx:74
int usage(const char *errstr=nullptr)
Definition: mbsprint.cxx:25
void PrintSlowSubevent(mbs::SubeventHeader *sub)
Definition: mbsprint.cxx:50
void Sleep(double tm)
Definition: timing.cxx:129
bool InstallSignalHandlers()
Method is used to install DABC-specific Ctrl-C handler It allows to correctly stop program execution ...
Definition: api.cxx:83
bool CtrlCPressed()
Returns true when CtrlC was pressed in handler.
Definition: api.cxx:106
bool str_to_uint(const char *val, unsigned *res)
Convert string to unsigned integer value One could use hexadecimal (in form 0xabc100) or decimal form...
Definition: string.cxx:184
TimeStamp Now()
Definition: timing.h:260
bool str_to_double(const char *val, double *res)
Convert string to double value.
Definition: string.cxx:216
bool str_to_lint(const char *val, long *res)
Convert string to long integer value.
Definition: string.cxx:162
Class for acquiring and holding timestamps.
Definition: timing.h:40
double SpentTillNow() const
Method return time in second, spent from the time kept in TimeStamp instance If time was not set befo...
Definition: timing.h:144
SubeventHeader * NextSubEvent(SubeventHeader *prev) const
Definition: MbsTypeDefs.h:136
MBS subevent
Definition: MbsTypeDefs.h:40
void * RawData() const
Definition: MbsTypeDefs.h:75
void PrintData(bool ashex=true, bool aslong=true)
Prints sub-event data in hex/decimal and long/short form.
Definition: MbsTypeDefs.cxx:69
void PrintHeader()
Prints sub-event header.
Definition: MbsTypeDefs.cxx:63
uint32_t RawDataSize() const
Definition: MbsTypeDefs.h:78