AlcapDAQ  1
mucap_expand.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, "Compression", NULL);
20  cm_get_experiment_database(&hDB, NULL);
21 
22  /* Not really necessary */
23  // Determine which compression to use
24  char key_name[80];
25  int runnumber=-1;
26  int size=sizeof(int);
27  sprintf(key_name, "Runinfo/Run number");
28  db_get_value(hDB,0,key_name, &runnumber, &size, TID_INT,1);
29  bool USE_OLD_COMPRESSION=false;
30  if (runnumber > 30000 && runnumber < 50000)
31  {
32  USE_OLD_COMPRESSION=true;
33  }
34  printf("Determined that we should use old compression %d\n", USE_OLD_COMPRESSION);
35  // Done determining which decompression to use
36 
37  compress_load_all(USE_OLD_COMPRESSION);
38  /* Done with unnecessary code */
40  if(argc != 3) {
41  printf("Usage: mucap_expand uncompressed_input.mid uncompressed_output.mid\n");
42  exit(1);
43  }
44 
45  FILE *fp = fopen(argv[1], "r");
46  if (!fp) {
47  printf("Unable to open %s\n", argv[1]);
48  exit(1);
49  }
50 
51  FILE *output_fp = fopen(argv[2], "w");
52  if (!fp) {
53  printf("Unable to open %s\n", argv[2]);
54  exit(1);
55  }
56 
57  char *input_event = new char[MAX_EVENT_SIZE];
58  char *output_event = new char[MAX_EVENT_SIZE];
59 
60  while (read_midas_event(fp, input_event) == SUCCESS) {
61  EVENT_HEADER header = *((EVENT_HEADER *) input_event);
62 
63  int event_id = header.event_id;
64 
65  if (event_id == EVENTID_BOR || event_id == EVENTID_EOR ||
66  event_id == EVENTID_MESSAGE) {
67 
68  // For non-ordinary events, just write out the raw data
69  fwrite(input_event, 1, sizeof(EVENT_HEADER) + header.data_size,
70  output_fp);
71  } else {
72 
73  bk_init32(output_event);
74  expand_event(input_event + sizeof(EVENT_HEADER), output_event,USE_OLD_COMPRESSION);
75  header.data_size = bk_size(output_event);
76 
77  fwrite(&header, sizeof(EVENT_HEADER), 1, output_fp);
78  fwrite(output_event, 1, header.data_size, output_fp);
79  }
80  }
81 
82  fclose(fp);
83  fclose(output_fp);
84 
85  cm_disconnect_experiment();
86 }