ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ddump.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file ddump.cc
1 #include <stdlib.h>
2 #include <signal.h>
3 #include <string>
4 
5 #include "fileEventiterator.h"
6 #include "testEventiterator.h"
7 #include "rcdaqEventiterator.h"
8 #include "oncsEventiterator.h"
9 #include <stdio.h>
10 
11 #ifdef HAVE_GETOPT_H
12 #include "getopt.h"
13 #endif
14 
15 #include<vector>
16 #include <boost/algorithm/string.hpp>
17 #include <boost/lexical_cast.hpp>
18 
19 
20 
21 #define RCDAQEVENTITERATOR 1
22 #define FILEEVENTITERATOR 2
23 #define TESTEVENTITERATOR 3
24 #define ONCSEVENTITERATOR 4
25 
26 #if defined(SunOS) || defined(Linux) || defined(OSF1)
27 void sig_handler(int);
28 #else
29 void sig_handler(...);
30 #endif
31 
32 // we make it a global variable so the signal; handler can get at it.
33 
35 
36 void exitmsg()
37 {
38  COUT << "** usage: ddump -ecnstdfghiIFTOv datastream" << std::endl;
39  COUT << " type ddump -h for more help" << std::endl;
40  exit(0);
41 }
42 
44 {
45  COUT << "** cannot specify both -e and -c!" << std::endl;
46  COUT << " type dlist -h for more help" << std::endl;
47  exit(0);
48 }
49 
50 
51 void exithelp()
52 {
53 
54  COUT << std::endl;
55  COUT << " ddump dumps selected packets from a given event. The event can come" << std::endl;
56  COUT << " from any of the standard sources, ET pool, file, or test stream. " << std::endl;
57  COUT << " The default is to get the next available event from a ET pool." << std::endl;
58  COUT << std::endl;
59  COUT << " ddump can optionally identify the event with the -i option." << std::endl;
60  COUT << " You can request a certain event number with the -e option. " << std::endl;
61  COUT << " You can ask for a certain event type (data, beg-run, etc) with -t." << std::endl;
62  COUT << " -t takes a number (e.g. 9 for begin-run) for an exact match," << std::endl;
63  COUT << " or DATA or SPECIAL to selct data or special events (DATA is default)" << std::endl;
64  COUT << " you can abbreviate DATA to D or d and SPECIAL to S or s." << std::endl;
65 
66  COUT << std::endl;
67  COUT << " Note: In order to find out which packets are contained in an event, you can" << std::endl;
68  COUT << " use the dlist utility." << std::endl;
69  COUT << std::endl;
70  COUT << " Example:" << std::endl;
71  COUT << std::endl;
72  COUT << " -T says that the input stream is a test stream (where events have 3 " << std::endl;
73  COUT << " predictable packets 1001, 1002, and 1003); we dump packet 1002:" << std::endl;
74  COUT << std::endl;
75  COUT << " > ddump -p 1002 -T" << std::endl;
76  COUT << " Packet 1002 16 0 (Unformatted) 30005 (ID2EVT)" << std::endl;
77  COUT << " 0 | 1 2 3 4 5 6 7 8 " << std::endl;
78  COUT << " 8 | 9 a b c d e f 10 " << std::endl;
79  COUT << " 10 | 11 12 13 14 " << std::endl;
80  COUT << std::endl;
81  COUT << " The -i option also lets the event identify itself." << std::endl;
82  COUT << " The -I option gets you an in-depth identification of the packets." << std::endl;
83 
84  COUT << std::endl;
85  COUT << " > ddump -p 1002 -T -i" << std::endl;
86  COUT << " -- Event 1 Run: 1331 length: 68 frames: 1 type: 1 (Data Event)" << std::endl;
87  COUT << " Packet 1002 16 0 (Unformatted) 30005 (ID2EVT)" << std::endl;
88  COUT << " 0 | 1 2 3 4 5 6 7 8 " << std::endl;
89  COUT << " 8 | 9 a b c d e f 10 " << std::endl;
90  COUT << " 10 | 11 12 13 14 " << std::endl;
91 
92  COUT << " The -F option also causes the frames in the event to be listed." << std::endl;
93  COUT << " If you list a particular packet, you will get the frame where" << std::endl;
94  COUT << " this packet is in." << std::endl;
95 
96  COUT << std::endl;
97 
98  COUT << " The -p option takes a number, a list, or a range of packets" << std::endl;
99  COUT << " You can combine ranges and lists:" << std::endl;
100  COUT << " > ddump -p 1001 -T -i" << std::endl;
101  COUT << " > ddump -p 1001,1002 -T -i" << std::endl;
102  COUT << " > ddump -p 1001-1003 -T -i" << std::endl;
103  COUT << " > ddump -p 1001-1002,1003 -T -i" << std::endl;
104 
105  COUT << std::endl;
106 
107  COUT << " List of options: " << std::endl;
108  COUT << " -e <event number>" << std::endl;
109  COUT << " -c <number> get nth event (-e gives event with number n)" << std::endl;
110  COUT << " -n <number> repeat for n events (0: until end of stream)" << std::endl;
111  COUT << " -p <Packet Id>" << std::endl;
112  COUT << " -t <event type>" << std::endl;
113  COUT << " -i <print event identity>" << std::endl;
114  COUT << " -I <print in-depth packet identity (default is short form)>" << std::endl;
115  COUT << " -f (stream is a file)" << std::endl;
116  COUT << " -T (stream is a test stream)" << std::endl;
117  COUT << " -r (stream is a rcdaq monitoring stream)" << std::endl;
118  COUT << " -O (stream is a legacy ONCS format file)" << std::endl;
119  COUT << " -g use generic dump" << std::endl;
120  COUT << " -d numbers are std::decimal (default std::hex) for generic dump" << std::endl;
121  COUT << " -o numbers are octal (default std::hex) for generic dump" << std::endl;
122  COUT << " -s for a generic dump, send packet raw payloadc to stdout for further manipulation" << std::endl;
123  COUT << " -x like -s, but also include the packet header" << std::endl;
124  COUT << " -v verbose" << std::endl;
125  COUT << " -h this message" << std::endl << std::endl;
126  exit(0);
127 }
128 
129 #if defined(SunOS) || defined(Linux) || defined(OSF1)
130 void sig_handler(int i)
131 #else
132  void sig_handler(...)
133 #endif
134 {
135  COUT << "sig_handler: signal seen " << std::endl;
136  if (it) delete it;
137  exit(0);
138 }
139 
140 int rangeParser ( const std::string string, std::vector<int> &selection)
141 {
142  std::vector<std::string>::const_iterator it, itr;
143  std::vector<std::string> strs,r;
144 
145 // std::vector<int>::const_iterator vit;
146  int low,high,i;
147  boost::split(strs,string, boost::is_any_of(","));
148 
149  for (it= strs.begin(); it!= strs.end(); ++it)
150  {
151  boost::split(r,*it,boost::is_any_of("-"));
152 
153  itr = r.begin();
154  low = high =boost::lexical_cast<int>(r[0]);
155  itr++;
156  if(itr!=r.end())
157  {
158  high = boost::lexical_cast<int>(r[1]);
159  }
160  for(i=low;i<=high;++i)
161  {
162  selection.push_back(i);
163  }
164  }
165 
166  // for(vit= selection.begin(); vit!= selection.end(); ++vit)
167  // {
168  // std::cout<<*vit<<std::endl;
169  // }
170  return 0;
171 
172 }
173 
174 int
175 main(int argc, char *argv[])
176 {
177 
178  if (argc < 2) exitmsg();
179 
180  int c;
181  int status;
182 
183  int eventnumber =0;
184  int countnumber =0;
185  int repeatcount =1;
186  int subeventid =0;
187 
188  int eventtypemin = 1;
189  int eventtypemax = 7;
190  int eventtype = 1;
191  int type_is_a_range = 1;
192  int verbose = 0;
193 
194 
195  int ittype = FILEEVENTITERATOR;
196  int dumpstyle = EVT_HEXADECIMAL;
197  int identify = 0;
198  int fullidentify = 0;
199  int listframe = 0;
200  int listHistory = 0;
201  int listError = 0;
202  int generic = 0;
203 
204  extern char *optarg;
205  extern int optind;
206 
207  std::vector<int> packetSelection;
208 
209  while ((c = getopt(argc, argv, "n:c:e:p:t:idsxfrghIFTOHEv")) != EOF)
210  switch (c)
211  {
212  case 'e':
213  if ( !sscanf(optarg, "%d", &eventnumber) ) exitmsg();
214  break;
215 
216  case 'c':
217  if ( !sscanf(optarg, "%d", &countnumber) ) exitmsg();
218  break;
219 
220  case 'n':
221  if ( !sscanf(optarg, "%d", &repeatcount) ) exitmsg();
222  break;
223 
224  case 'p':
225  // if ( !sscanf(optarg, "%d", &subeventid) ) exitmsg();
226  if ( rangeParser ( optarg, packetSelection) ) exitmsg();
227  subeventid=1; // yes, select
228  break;
229 
230  case 't':
231  if (*optarg == 'S' || *optarg == 's' )
232  {
233  type_is_a_range =1;
234  eventtypemin=8;
235  eventtypemax=20;
236  }
237  else if (*optarg == 'A' || *optarg == 'a')
238  {
239  type_is_a_range =1;
240  eventtypemin=1;
241  eventtypemax=20;
242  }
243  else if (*optarg == 'D' || *optarg == 'd')
244  {
245  type_is_a_range =1;
246  eventtypemin=1;
247  eventtypemax=7;
248  }
249  else
250  {
251  if ( !sscanf(optarg, "%d", &eventtype) ) exitmsg();
252  type_is_a_range =0;
253  }
254  break;
255 
256  case 'i':
257  identify = 1;
258  break;
259 
260  case 'I':
261  fullidentify = 1;
262  break;
263 
264  case 'F':
265  listframe = 1;
266  break;
267 
268  case 'H':
269  listHistory = 1;
270  break;
271 
272  case 'E':
273  listError = 1;
274  break;
275 
276  case 'g':
277  generic = 1;
278  break;
279 
280  case 'T':
281  ittype = TESTEVENTITERATOR;
282  break;
283 
284  case 'f':
285  ittype = FILEEVENTITERATOR;
286  break;
287 
288  case 'r':
289  ittype = RCDAQEVENTITERATOR;
290  break;
291 
292  case 'O':
293  ittype = ONCSEVENTITERATOR;
294  break;
295 
296  case 'o':
297  dumpstyle = EVT_OCTAL;
298  break;
299 
300  case 'd':
301  dumpstyle = EVT_DECIMAL;
302  break;
303 
304  case 's':
305  dumpstyle = EVT_RAW;
306  break;
307 
308  case 'x':
309  dumpstyle = EVT_RAW_WH;
310  break;
311 
312  case 'v': // verbose
313  verbose++;
314  break;
315 
316  case 'h':
317  exithelp();
318  break;
319  }
320 
321 
322 
323  if ( eventnumber && countnumber) evtcountexitmsg();
324 
325  signal(SIGKILL, sig_handler);
326  signal(SIGTERM, sig_handler);
327  signal(SIGINT, sig_handler);
328 
329  // see if we can open the file
330  it = 0;
331  status = 0;
332 
333  switch (ittype)
334  {
335  case RCDAQEVENTITERATOR:
336  if ( optind+1>argc)
337  {
338  std::string host = "localhost";
339 
340  if ( getenv("RCDAQHOST") )
341  {
342  host = getenv("RCDAQHOST");
343  }
344 
345  it = new rcdaqEventiterator(host.c_str(), status);
346 
347  }
348  else
349  {
350  it = new rcdaqEventiterator(argv[optind], status);
351  }
352  break;
353 
354  case TESTEVENTITERATOR:
355  it = new testEventiterator();
356  status =0;
357  break;
358 
359  case FILEEVENTITERATOR:
360  if ( optind+1>argc) exitmsg();
361  it = new fileEventiterator(argv[optind], status);
362  break;
363 
364  case ONCSEVENTITERATOR:
365  if ( optind+1>argc) exitmsg();
366  it = new oncsEventiterator(argv[optind], status);
367  break;
368 
369  status = 1;
370  break;
371 
372  default:
373  exitmsg();
374  break;
375  }
376 
377  if (status)
378  {
379  delete it;
380  it = 0;
381  COUT << "Could not open input stream" << std::endl;
382  exit(1);
383  }
384 
385  // set the verbosity of the iterator
386  it->setVerbosity(verbose);
387 
388  // ok. now go through the events
389  Event *evt;
390  Packet *s;
391  int i;
392 
393  evt = it->getNextEvent();
394 
395  // first check if our requested event number is already past:
396 
397  // if ( eventnumber && evt->getEvtSequence() > eventnumber)
398  // {
399  // COUT << "requested Event number " << eventnumber<<
400  // " but found " << evt->getEvtSequence() << " already" << std::endl;
401  // delete evt;
402  // evt = 0;
403  // }
404 
405  int take_this;
406  int count = 0;
407  int accepted_count = 0;
408 
409  while (evt) // as long as there is a next event...
410  {
411  take_this = 1;
412  count++;
413 
414  if ( eventnumber )
415  {
416  if ( evt->getEvtSequence() == eventnumber)
417  eventnumber = 0;
418  else
419  take_this = 0;
420  }
421 
422 
423  if ( countnumber && count < countnumber)
424  take_this = 0;
425 
426  if ( type_is_a_range)
427  {
428  if ( (evt->getEvtType() < eventtypemin) ||
429  (evt->getEvtType() > eventtypemax)
430  ) take_this = 0;
431  }
432  else
433  {
434  if (evt->getEvtType() != eventtype) take_this = 0;
435  }
436 
437  if (take_this)
438  {
439  if (identify) evt->identify();
440 
441  if ( subeventid)
442  {
443  std::vector<int>::const_iterator vit;
444  for (vit= packetSelection.begin(); vit!= packetSelection.end(); ++vit)
445  {
446  if ( (s = evt->getPacket(*vit)) ) // get the subevent
447  {
448  if (listframe) evt->listFrame (*vit);
449  if (fullidentify) s->fullIdentify();
450  if (generic) s->gdump(dumpstyle);
451  else s->dump();
452  if (listHistory) evt->listHistory();
453  if (listError) evt->listError();
454  delete s;
455  }
456  }
457  }
458  else
459  {
460  if (listframe) evt->listFrame();
461  Packet *p[10000];
462  int nw = evt->getPacketList(p, 10000);
463  for (i=0; i<nw; i++)
464  {
465  if (fullidentify) p[i]->fullIdentify();
466  if (generic) p[i]->gdump(dumpstyle);
467  else p[i]->dump();
468  delete p[i];
469  }
470  if (listHistory) evt->listHistory();
471  if (listError) evt->listError();
472  }
473 
474 
475  delete evt;
476  if ( repeatcount==0 || ++accepted_count < repeatcount) evt = it->getNextEvent();
477  else evt = 0;
478 
479  }
480  else
481  {
482  delete evt;
483  evt = it->getNextEvent(); // try to get the next event
484  }
485  }
486 
487  delete it;
488  it = 0;
489  return 0;
490 }
491