AlcapDAQ  1
mucap_compress.cpp
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <endian.h>
5 #include <byteswap.h>
6 #include "midas.h"
7 
8 #include "mucap_structures.h"
9 #include "mucap_compress.h"
10 
11 HNDLE hDB;
12 
13 int main(int argc, char *argv[])
14 {
15  char host_name[256], exp_name[32];
16 
17  cm_get_environment(host_name, sizeof(host_name), exp_name,
18  sizeof(exp_name));
19  cm_connect_experiment(host_name, exp_name, "mucap_compress", NULL);
20  cm_get_experiment_database(&hDB, NULL);
21 
23 
24  if(argc != 3) {
25  printf("Usage: mucap_compress uncompressed_input.mid compressed_output.mid\n");
26  exit(1);
27  }
28 
29  FILE *fp = fopen(argv[1], "r");
30  if (!fp) {
31  printf("Unable to open %s\n", argv[1]);
32  exit(1);
33  }
34 
35  FILE *output_fp = fopen(argv[2], "w");
36  if (!fp) {
37  printf("Unable to open %s\n", argv[2]);
38  exit(1);
39  }
40 
41  char *input_event = new char[MAX_EVENT_SIZE];
42  char *output_event = new char[MAX_EVENT_SIZE];
43 
44  while (read_midas_event(fp, input_event) == SUCCESS) {
45 
46  EVENT_HEADER header = *((EVENT_HEADER *) input_event);
47  int event_id = header.event_id;
48 
49  if (event_id == EVENTID_BOR || event_id == EVENTID_EOR ||
50  event_id == EVENTID_MESSAGE) {
51 
52  // For non-ordinary events, just write out the raw data
53  fwrite(input_event, 1, sizeof(EVENT_HEADER) + header.data_size,
54  output_fp);
55  } else {
56 
57  bk_init32(output_event);
58  compress_event(input_event + sizeof(EVENT_HEADER), output_event);
59  header.data_size = bk_size(output_event);
60 
61  fwrite(&header, sizeof(EVENT_HEADER), 1, output_fp);
62  fwrite(output_event, 1, header.data_size, output_fp);
63  }
64  }
65 
66  fclose(fp);
67  fclose(output_fp);
68 
69  cm_disconnect_experiment();
70 }