Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
dlist.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file dlist.cc
1 
2 
3 #include <stdlib.h>
4 #include <signal.h>
5 #include <string>
6 
7 #include "fileEventiterator.h"
8 #include "rcdaqEventiterator.h"
9 #include "testEventiterator.h"
10 #include "oncsEventiterator.h"
11 
12 
13 #include <stdio.h>
14 
15 #ifdef HAVE_GETOPT_H
16 #include "getopt.h"
17 #endif
18 
19 #define RCDAQEVENTITERATOR 1
20 #define FILEEVENTITERATOR 2
21 #define TESTEVENTITERATOR 3
22 #define ONCSEVENTITERATOR 4
23 
24 #if defined(SunOS) || defined(Linux) || defined(OSF1)
25 void sig_handler(int);
26 #else
27 void sig_handler(...);
28 #endif
29 
30 // we make it a global variable so the signal; handler can get at it.
31 
33 
34 
35 void exitmsg()
36 {
37  COUT << "** usage: dlist -ecntfTOiIh datastream" << std::endl;
38  COUT << " type dlist -h for more help" << std::endl;
39  exit(0);
40 }
41 
43 {
44  COUT << "** cannot specify both -e and -c!" << std::endl;
45  COUT << " type dlist -h for more help" << std::endl;
46  exit(0);
47 }
48 
49 
50 
51 void exithelp()
52 {
53  COUT << std::endl;
54  int ii = 77;
55  COUT << "test output " << 5 << ii << std::endl;
56 
57  COUT << " dlist lists the packets contained in a given event. The event can come" << std::endl;
58  COUT << " from any of the standard sources, rcdaq, file, or test stream. " << std::endl;
59  COUT << " The default is to get the next available event from a file." << std::endl;
60  COUT << " dlist can optionally identify the event with the -i option." << std::endl;
61  COUT << " You can request a certain event number with the -e option. " << std::endl;
62  COUT << " You can ask for a certain event type (data, beg-run, etc) with -t." << std::endl;
63  COUT << " -t takes a number (e.g. 9 for begin-run) for an exact match," << std::endl;
64  COUT << " or DATA or SPECIAL to selct data or special events (DATA is default)" << std::endl;
65  COUT << " you can abbreviate DATA to D or d and SPECIAL to S or s." << std::endl;
66  COUT << " Example:" << std::endl;
67  COUT << std::endl;
68  COUT << " -T says the input is a test stream (which always has 3 packets):" << std::endl;
69  COUT << std::endl;
70  COUT << " dlist -T" << std::endl;
71  COUT << " Packet 1001 26 0 (Unformatted) 30006 (ID4EVT)" << std::endl;
72  COUT << " Packet 1002 16 0 (Unformatted) 30005 (ID2EVT)" << std::endl;
73  COUT << " Packet 1003 10 0 (Unformatted) 30006 (ID4EVT)" << std::endl;
74  COUT << std::endl;
75  COUT << " The -i option causes the event to be identified. " << std::endl;
76  COUT << std::endl;
77  COUT << " dlist -T -i" << std::endl;
78  COUT << " -- Event 1 Run: 1331 length: 68 frames: 1 type: 1 (Data Event)" << std::endl;
79  COUT << " Packet 1001 26 0 (Unformatted) 30006 (ID4EVT)" << std::endl;
80  COUT << " Packet 1002 16 0 (Unformatted) 30005 (ID2EVT)" << std::endl;
81  COUT << " Packet 1003 10 0 (Unformatted) 30006 (ID4EVT)" << std::endl;
82  COUT << std::endl;
83  COUT << " List of options: " << std::endl;
84  COUT << " usage: dlist -ecntfTOih datastream" << std::endl;
85  COUT << " -e <event number>" << std::endl;
86  COUT << " -c <number> get nth event (-e gives event with number n)" << std::endl;
87  COUT << " -n <number> repeat for n events (0: until end of stream)" << std::endl;
88  COUT << " -t <event type>" << std::endl;
89  COUT << " -i <print event identity>" << std::endl;
90  COUT << " -I <print in-depth packet identity>" << std::endl;
91  COUT << " -f (stream is a file)" << std::endl;
92  COUT << " -T (stream is a test stream)" << std::endl;
93  COUT << " -r (stream is a rcdaq monitoring stream)" << std::endl;
94  COUT << " -O (stream is a legacy ONCS format file)" << std::endl;
95  COUT << " -v verbose" << std::endl;
96  COUT << " -h this message" << std::endl << std::endl;
97  exit(0);
98 }
99 
100 #if defined(SunOS) || defined(Linux) || defined(OSF1)
101 void sig_handler(int i)
102 #else
103  void sig_handler(...)
104 #endif
105 {
106  COUT << "sig_handler: signal seen " << std::endl;
107  if (it) delete it;
108  exit(0);
109 }
110 
111 int
112 main(int argc, char *argv[])
113 {
114  int c;
115  int status;
116 
117  int eventnumber =0;
118  int countnumber =0;
119  int repeatcount =1;
120 
121  int eventtypemin = 1;
122  int eventtypemax = 7;
123  int eventtype = 1;
124  int type_is_a_range = 1;
125 
126  int verbose = 0;
127 
129  int identify = 0;
130  int fullidentify = 0;
131 
132  extern char *optarg;
133  extern int optind;
134 
135  if (argc < 2) exitmsg();
136 
137 
138  while ((c = getopt(argc, argv, "n:c:e:t:iIrfhTOv")) != EOF)
139  switch (c)
140  {
141  case 'e':
142  if ( !sscanf(optarg, "%d", &eventnumber) ) exitmsg();
143  break;
144 
145  case 'c':
146  if ( !sscanf(optarg, "%d", &countnumber) ) exitmsg();
147  break;
148 
149  case 'n':
150  if ( !sscanf(optarg, "%d", &repeatcount) ) exitmsg();
151  break;
152 
153  case 't':
154  if (*optarg == 'S' || *optarg == 's' )
155  {
156  type_is_a_range =1;
157  eventtypemin=8;
158  eventtypemax=20;
159  }
160  else if (*optarg == 'D' || *optarg == 'd')
161  {
162  type_is_a_range =1;
163  eventtypemin=1;
164  eventtypemax=7;
165  }
166  else if (*optarg == 'A' || *optarg == 'a')
167  {
168  type_is_a_range =1;
169  eventtypemin=1;
170  eventtypemax=20;
171  }
172  else
173  {
174  if ( !sscanf(optarg, "%d", &eventtype) ) exitmsg();
175  type_is_a_range =0;
176  }
177  break;
178 
179  case 'i':
180  identify = 1;
181  break;
182 
183  case 'I':
184  fullidentify = 1;
185  break;
186 
187  case 'T':
188  ittype = TESTEVENTITERATOR;
189  break;
190 
191  case 'f':
192  ittype = FILEEVENTITERATOR;
193  break;
194 
195  case 'r':
196  ittype = RCDAQEVENTITERATOR;
197  break;
198 
199  case 'O':
200  ittype = ONCSEVENTITERATOR;
201  break;
202 
203  case 'v': // verbose
204  verbose++;
205  break;
206 
207  case 'h':
208  exithelp();
209  break;
210  }
211 
212 
213 
214  if ( eventnumber && countnumber) evtcountexitmsg();
215 
216 #ifndef WIN32
217 
218  signal(SIGKILL, sig_handler);
219  signal(SIGTERM, sig_handler);
220  signal(SIGINT, sig_handler);
221 
222 #endif
223 
224  // see if we can open the file
225  it = 0;
226  status=0;
227  switch (ittype)
228  {
229 
230  case RCDAQEVENTITERATOR:
231  if ( optind+1>argc)
232  {
233  std::string host = "localhost";
234 
235  if ( getenv("RCDAQHOST") )
236  {
237  host = getenv("RCDAQHOST");
238  }
239 
240  it = new rcdaqEventiterator(host.c_str(), status);
241  }
242  else
243  {
244  it = new rcdaqEventiterator(argv[optind], status);
245  }
246  break;
247 
248  case TESTEVENTITERATOR:
249  it = new testEventiterator();
250  status =0;
251  break;
252 
253  case FILEEVENTITERATOR:
254  if ( optind+1>argc) exitmsg();
255  it = new fileEventiterator(argv[optind], status);
256  break;
257 
258  case ONCSEVENTITERATOR:
259  if ( optind+1>argc) exitmsg();
260  it = new oncsEventiterator(argv[optind], status);
261  break;
262 
263  default:
264  exitmsg();
265  break;
266  }
267 
268  if (status)
269  {
270  delete it;
271  it = 0;
272  COUT << "Could not open input stream" << std::endl;
273  exit(1);
274  }
275 
276  // set the verbosity of the iterator
277  it->setVerbosity(verbose);
278 
279 
280  // ok. now go through the events
281  Event *evt;
282 
283  evt = it->getNextEvent();
284 
285  // first check if our requested event number is already past:
286 
287  // if ( eventnumber && evt->getEvtSequence() > eventnumber)
288  // {
289  // COUT << "requested Event number " << eventnumber<<
290  // " but found " << evt->getEvtSequence() << " already" << std::endl;
291  // delete evt;
292  // evt = 0;
293  // }
294 
295  int take_this;
296  int i;
297 
298  Packet *p[10000];
299  int count = 0;
300  int accepted_count = 0;
301 
302  while (evt) // as long as there is a next event...
303  {
304  take_this = 1;
305  count++;
306 
307  if ( eventnumber )
308  {
309  if ( evt->getEvtSequence() == eventnumber)
310  eventnumber = 0;
311  else
312  take_this = 0;
313  }
314 
315  if ( countnumber && count < countnumber)
316  take_this = 0;
317 
318 
319  if ( type_is_a_range)
320  {
321  if ( (evt->getEvtType() < eventtypemin) ||
322  (evt->getEvtType() > eventtypemax)
323  ) take_this = 0;
324  }
325  else
326  {
327  if (evt->getEvtType() != eventtype) take_this = 0;
328  }
329 
330  if (take_this)
331  {
332  if (identify) evt->identify();
333 
334  int nw = evt->getPacketList(p, 10000);
335  for (i=0; i<nw; i++)
336  {
337  if (fullidentify) p[i]->fullIdentify();
338  else p[i]->identify();
339  delete p[i];
340  }
341  delete evt;
342  if ( repeatcount==0 || ++accepted_count < repeatcount) evt = it->getNextEvent();
343  else evt = 0;
344  }
345  else
346  {
347  delete evt;
348  evt = it->getNextEvent(); // try to get the next event
349  }
350  }
351 
352  delete it;
353  it = 0;
354 
355  return 0;
356 }
357 
358