StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
daqFileMerger.C
1 //******************************************************
2 //*** Merges events from .daq files
3 //***
4 //*** usage: daqFileMerger [-omit n] -o output.daq fn1.daq fn2.daq ...
5 //***
6 //*** merges daq files.
7 //*** The events in the file will be ordered such that
8 //*** a) The events from each file will be in the same order as in the original files
9 //*** b) The events' source files will be randomly distributed throughout the run.
10 //*** c) The optional omit parameter will omit n% of the events randomly
11 //***
12 //*****************************************************
13 
14 #include <stdio.h>
15 #include <unistd.h>
16 #include <getopt.h>
17 #include <sys/types.h>
18 #include <stdlib.h>
19 #include <time.h>
20 #include <errno.h>
21 #include <string.h>
22 #include <fcntl.h>
23 
24 #include <rtsLog.h> // for my LOG() call
25 #include <rtsSystems.h>
26 
27 // this needs to be always included
28 #include <DAQ_READER/daqReader.h>
29 #include <DAQ_READER/daq_dta.h>
30 
31 void displayHelp()
32 {
33  printf("Usage: daqFileMerger [-omit n] -o outputfile.daq source1.daq source2.daq ...\n");
34  printf(" The files will be merged such that:\n");
35  printf(" a) The events from each file will be in the same order as in the original files\n");
36  printf(" b) The events' source files will be randomly distributed throughout the run.\n");
37  printf(" c) The optional omit parameter will omit n%% of the events randomly\n");
38 }
39 
40 int omit;
41 int firstSourceFile;
42 char *outputfile;
43 
44 void parseArgs(int argc, char *argv[])
45 {
46  int arg = 1;
47 
48  for(arg=1;arg<argc;arg++) {
49  if(strcmp(argv[arg], "-omit") == 0) {
50  arg++;
51  omit = atoi(argv[arg]);
52  }
53  else if(strcmp(argv[arg], "-o") == 0) {
54  arg++;
55  outputfile = argv[arg];
56  firstSourceFile = arg+1;
57 
58  printf("output file: %s, omit = %d%%\n", outputfile, omit);
59  return;
60  }
61  }
62 
63  displayHelp();
64  exit(0);
65 }
66 
67 int getGoodEvent(daqReader *rdr) {
68  for(;;) {
69  char *ret = rdr->get(0, EVP_TYPE_ANY);
70 
71  if(ret) {
72  if(rdr->status) continue; // Not a good event?
73  return 0;
74  }
75  else { // what happened?
76  if(rdr->status == EVP_STAT_OK) continue;
77  if(rdr->status == EVP_STAT_EOR) return 1;
78 
79  // An error!
80  LOG(ERR, "Error reading!");
81  return -1;
82  }
83  }
84 }
85 
86 struct Spec {
87  int file;
88  int random;
89 };
90 
91 int main(int argc, char *argv[])
92 {
93  srand((unsigned int)time(NULL));
94 
95  rtsLogOutput(RTS_LOG_STDERR) ;
96  rtsLogLevel((char *)WARN) ;
97 
98  parseArgs(argc, argv);
99 
100  int fd = open(outputfile, O_CREAT | O_WRONLY, 0666);
101  if(fd < 0) {
102  printf("Error opening output file: %s", outputfile);
103  exit(0);
104  }
105  int n_sourcefiles = argc - firstSourceFile;
106 
107  int *n_evts = (int *)malloc(sizeof(int) * n_sourcefiles);
108  daqReader **rdrs = (daqReader **)malloc(sizeof(daqReader *) * n_sourcefiles);
109 
110  int tot_evts = 0;
111 
112  for(int i=0;i<n_sourcefiles;i++) {
113  n_evts[i] = 0;
114 
115 
116  rdrs[i] = new daqReader(argv[firstSourceFile + i]);
117  int ret;
118  while((ret = getGoodEvent(rdrs[i])) == 0) {
119  n_evts[i]++;
120  }
121 
122  tot_evts += n_evts[i];
123  printf("File #%02d: %s (%d events)\n",i, argv[firstSourceFile + i], n_evts[i]);
124 
125  delete rdrs[i];
126  rdrs[i] = new daqReader(argv[firstSourceFile + i]);
127  }
128 
129 
130  // Randomize.... First get the events with a random tag
131  Spec *specs = (Spec *)malloc(sizeof(Spec) * tot_evts);
132  int idx = 0;
133  for(int i=0;i<n_sourcefiles;i++) {
134  for(int j=0;j<n_evts[i];j++) {
135  specs[idx].file = i;
136  specs[idx].random = rand();
137  idx++;
138  }
139  }
140 
141  // Now sort by random
142  for(int i=0;i<tot_evts;i++) {
143  for(int j=i+1;j<tot_evts;j++) {
144  if(specs[i].random > specs[j].random) {
145  int t = specs[i].file;
146  specs[i].file = specs[j].file;
147  specs[j].file = t;
148  t = specs[i].random;
149  specs[i].random = specs[j].random;
150  specs[j].random = t;
151  }
152  }
153  }
154 
155  // Now print out an ordering!
156  int k=0;
157  for(int i=0;i<tot_evts;i++) {
158 
159  int file = specs[i].file;
160 
161  int ret = getGoodEvent(rdrs[file]);
162  if(ret != 0) {
163  printf("Error reading from %d: ret = %d", file, ret);
164  continue;
165  }
166 
167 
168  // Omit randomly, then print out!
169  double zz = rand();
170  zz /= (double)RAND_MAX;
171  zz *= 100;
172  if(zz >= omit) {
173  printf("[%07d] File: #%02d Evt: #%07d Seq: #%07d\n", k, file, rdrs[file]->event_number, rdrs[file]->seq);
174 
175  ret = write(fd, rdrs[file]->memmap->mem, rdrs[file]->event_size);
176  if(ret < 0) {
177  printf("Error writing to file %s\n", outputfile);
178  break;
179  }
180 
181  k++;
182  }
183  }
184 
185 
186  close(fd);
187 
188  for(int i=0;i<n_sourcefiles; i++) {
189  int ret = getGoodEvent(rdrs[i]);
190  if(ret != 1) {
191 
192  printf("Error reading EOF from %d: ret = %d\n", file, ret);
193  continue;
194  }
195 
196  delete rdrs[i];
197  }
198 
199  // LOG(INFO, "Keep event #%d (ptr=0x%x sz=%d)",rdr->seq,rdr->memmap->mem,rdr->event_size);
200  // write(STDOUT_FILENO, rdr->memmap->mem, rdr->event_size);
201 
202  return 0;
203 }
204