StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
daqFileChopper.C
1 //******************************************************
2 //*** Chops events out of .daq file
3 //***
4 //*** usage: daqFileChopper fn.daq arglist
5 //***
6 //*** arglist is passed to the function
7 //*** FilterEvent(), which returns true
8 //*** if the event is to be saved
9 //***
10 //*** output goes to standard out...
11 //*****************************************************
12 
13 #include <stdio.h>
14 #include <unistd.h>
15 #include <getopt.h>
16 #include <sys/types.h>
17 #include <stdlib.h>
18 
19 #include <errno.h>
20 #include <string.h>
21 #include <fcntl.h>
22 
23 #include <rtsLog.h> // for my LOG() call
24 #include <rtsSystems.h>
25 
26 // this needs to be always included
27 #include <DAQ_READER/daqReader.h>
28 #include <DAQ_READER/daq_dta.h>
29 
30 void displayHelp()
31 {
32  LOG(ERR,"Usage: daqFileChopper filename <filterlist>");
33  LOG(ERR," filterlist --> -trg xxx yyy zzz... // list of offline trigger bits");
34  LOG(ERR," -dtrg xxx yyy zzz... // list of daq trigger bits");
35  LOG(ERR," -ndtrg xxx yyy zzz // not list of daq trigger bits");
36  LOG(ERR," -eventnum xxx yyy... // list of event numbers");
37  LOG(ERR," -chop xxx // chop into files of xxx events");
38 }
39 
40 // Example event filter...
41 //
42 // This assumes that the argv list is made up from event numbers.
43 // and passes any event matching the event number.
44 int FilterEvent(daqReader *rdr, int nargs, char *argv[])
45 {
46  int eventNumber = rdr->seq;
47 
48  if(strcmp(argv[0], "-trg")==0) {
49  unsigned int bits = rdr->daqbits;
50  for(int i=0;i<32;i++) {
51  if(bits & (1<<i)) {
52  int trgSat = rdr->getOfflineId(i);
53 
54  for(int j=1;j<nargs;j++) {
55  if(trgSat == atoi(argv[j])) {
56  return 1;
57  }
58  }
59  }
60  }
61  }
62  else if(strcmp(argv[0], "-dtrg")==0) {
63  UINT64 bits = rdr->daqbits64;
64  for(int i=1;i<nargs;i++) {
65  int trg = atoi(argv[i]);
66  if(bits & (1ll<<trg)) return 1;
67  }
68  }
69 
70  else if (strcmp(argv[0], "-ndtrg")==0) {
71  UINT64 bits = rdr->daqbits64;
72  for(int i=1;i<nargs;i++) {
73  int trg = atoi(argv[i]);
74  if((bits & (1ll<<trg)) == 0) return 1;
75  }
76  }
77 
78  else if(strcmp(argv[0], "-eventnum")==0) {
79  for(int i=1;i<nargs;i++) {
80  if(eventNumber == atoi(argv[i])) {
81  return 1;
82  }
83  }
84  }
85  else {
86  displayHelp();
87  exit(0);
88  }
89  return 0;
90 }
91 
92 void doChopInstead(int argc, char *argv[]);
93 
94 
95 int main(int argc, char *argv[])
96 {
97  rtsLogOutput(RTS_LOG_STDERR) ;
98  rtsLogLevel((char *)WARN) ;
99 
100  if(argc < 3) {
101  displayHelp();
102 
103  exit(0);
104  }
105 
106  int filterArgc = argc-2;
107  char **filterArgv = &argv[2];
108 
109 
110  // This is a very ugly way to do this, but chopping the file into separate bits is very different from
111  // The general problem of filtering in semantics
112  // Yet, the utility program is similar in the minds of users, so I simply treat this as
113  // a separate program run from the same entry point...
114  if(strcmp(argv[2], "-chop") == 0) {
115  doChopInstead(argc, argv);
116  return 0;
117  }
118 
119 
120 
121  daqReader *evp;
122  evp = new daqReader(argv[1]) ; // create it with the filename argument..
123 
124  int good=0;
125  int bad=0;
126 
127  for(;;) {
128  char *ret = evp->get(0,EVP_TYPE_ANY);
129 
130  if(ret) {
131  if(evp->status) {
132  LOG(ERR,"evp status is non-null [0x08X, %d dec]",evp->status,evp->status) ;
133  continue ;
134  }
135  good++;
136  }
137  else { // something other than an event, check what.
138  switch(evp->status) {
139  case EVP_STAT_OK: // just a burp!
140  continue;
141  case EVP_STAT_EOR:
142  LOG(OPER, "Done after scanning %d events (%d bad)",good,bad);
143  break; // file, we're done...
144  case EVP_STAT_EVT:
145  bad++;
146  LOG(WARN, "Problem getting event - skipping [good %d, bad %d]",good,bad);
147  continue;
148  case EVP_STAT_CRIT:
149  LOG(CRIT,"evp->status CRITICAL (?)") ;
150  return -1;
151  }
152  }
153 
154  if(evp->status == EVP_STAT_EOR) {
155  LOG(INFO,"Done after scanning %d events (%d bad)",good,bad) ;
156  break;
157  }
158 
159  if(FilterEvent(evp, filterArgc, filterArgv) == 0) {
160  continue;
161  }
162  LOG(INFO, "Keep event #%d (ptr=0x%x sz=%d)",evp->seq,evp->memmap->mem,evp->event_size);
163 
164  write(STDOUT_FILENO, evp->memmap->mem, evp->event_size);
165  }
166 
167  delete evp ;
168  return 0 ;
169 }
170 
171 
172 
173 void doChopInstead(int argc, char *argv[]) {
174  if(argc < 4) {
175  printf("-chop argument requires number of events per file...\n");
176  return;
177  }
178 
179  int nevts_perfile = atoi(argv[3]);
180  int fidx = 0;
181  int nevts_thisfile = 0;
182  int fd = -1;
183 
184  char basename[256];
185 
186  int i=0;
187  for(i=0;i<255;i++) {
188  if(argv[1][i] == '\0') break;
189  if(argv[1][i] == '.') break;
190 
191  basename[i] = argv[1][i];
192  }
193 
194  basename[i] = '\0';
195 
196  char currname[256];
197 
198  daqReader *evp;
199  evp = new daqReader(argv[1]) ; // create it with the filename argument..
200 
201  int good=0;
202  int bad=0;
203 
204  for(;;) {
205  char *ret = evp->get(0,EVP_TYPE_ANY);
206 
207  if(ret) {
208  if(evp->status) {
209  LOG(ERR,"evp status is non-null [0x08X, %d dec]",evp->status,evp->status) ;
210  continue ;
211  }
212  good++;
213  }
214  else { // something other than an event, check what.
215  switch(evp->status) {
216  case EVP_STAT_OK: // just a burp!
217  continue;
218  case EVP_STAT_EOR:
219  LOG(OPER, "Done after scanning %d events (%d bad)",good,bad);
220  break; // file, we're done...
221  case EVP_STAT_EVT:
222  bad++;
223  LOG(WARN, "Problem getting event - skipping [good %d, bad %d]",good,bad);
224  continue;
225  case EVP_STAT_CRIT:
226  LOG(CRIT,"evp->status CRITICAL (?)") ;
227  return ;
228  }
229  }
230 
231  if(evp->status == EVP_STAT_EOR) {
232  if(fd >= 0) {
233  LOG(INFO, "Wrote %d evts to file: %s", nevts_thisfile, currname);
234  close(fd);
235  }
236 
237  LOG(INFO,"Done after scanning %d events (%d bad)",good,bad) ;
238 
239 
240  break;
241  }
242 
243  nevts_thisfile++;
244 
245  if(fd == -1) {
246  fidx++;
247  sprintf(currname, "%s-%d.daq", basename, fidx);
248  fd = open(currname, O_WRONLY | O_CREAT, 0666);
249  if(fd < 0) {
250  LOG(INFO, "Error opening file %s: (%s)", currname, strerror(errno));
251  return;
252  }
253  }
254 
255  LOG(DBG, "Writing event #%d (ptr=0x%x sz=%d) [%d in file %s]",evp->seq,evp->memmap->mem,evp->event_size, nevts_thisfile, currname);
256 
257  write(fd, evp->memmap->mem, evp->event_size);
258 
259  if(nevts_thisfile >= nevts_perfile) {
260  LOG(INFO, "Wrote %d evts to file: %s", nevts_thisfile, currname);
261  nevts_thisfile = 0;
262  close(fd);
263  fd = -1;
264  }
265  }
266 
267  delete evp ;
268  return;
269 
270 
271 }