AlcapDAQ  1
Data Structures | Typedefs | Functions | Variables
mucap_compress.h File Reference

Go to the source code of this file.

Data Structures

struct  io_buffer
 
struct  huffman_table
 
struct  rle_state
 

Typedefs

typedef struct io_buffer io_buffer
 
typedef struct huffman_node huffman_node
 
typedef struct huffman_table huffman_table
 
typedef struct rle_state rle_state
 
typedef struct tdc400_word tdc400_word
 
typedef struct stack_elt stack_elt
 
typedef struct caen_word caen_word
 
typedef struct channel_hit channel_hit
 
typedef union comp_word comp_word
 
typedef struct fadc_word fadc_word
 
typedef struct nfadc_word nfadc_word
 
typedef struct n2fadc_tpc_word n2fadc_tpc_word
 
typedef struct n2fadc_ndet_word n2fadc_ndet_word
 
typedef int(* expand_fcn )(unsigned char *input, int input_size, void *output, int userParam)
 

Functions

void start_output_buffer (io_buffer *buffer, unsigned char *p)
 
int flush_output_buffer (io_buffer *buffer)
 
void io_buffer_put (io_buffer *buffer, unsigned int code_bits, int code_length)
 
void start_input_buffer (io_buffer *buffer, unsigned char *p)
 
unsigned int io_buffer_get (io_buffer *buffer, int num_bits)
 
void huffman_put_symbol (huffman_table *table, io_buffer *buffer, int symbol)
 
int huffman_get_symbol (huffman_table *table, io_buffer *buffer)
 
BOOL huffman_optimize_tree (huffman_table *table)
 
void huffman_init_default (huffman_table *table, int num_symbols)
 
BOOL save_huffman (char *key_dir, huffman_table *table)
 
BOOL load_huffman (char *key_dir, huffman_table *table)
 
void rle_bits_setup ()
 
void rle_put (unsigned char b, rle_state *s, io_buffer *output)
 
int flush_rle (rle_state *s, io_buffer *output)
 
int rle_get (io_buffer *input, rle_state *s)
 
void tdc400_load ()
 
void tdc400_optimize ()
 
int tdc400_compress (tdc400_word *input, int input_size, unsigned char *output, int userParam)
 
int tdc400_expand (unsigned char *input, int input_size, tdc400_word *output, int userParam)
 
void stck_load ()
 
void stck_optimize ()
 
int stck_compress (stack_elt *input, int input_size, unsigned char *output, int userParam)
 
int stck_expand (unsigned char *input, int input_size, stack_elt *output, int userParam)
 
void comp_load ()
 
void comp_optimize ()
 
int caen_compress (caen_word *input, int input_size, unsigned char *output, int userParam)
 
int caen_expand (unsigned char *input, int input_size, caen_word *output, int userParam)
 
void hits_load ()
 
void hits_optimize ()
 
int hits_compress (channel_hit *input, int input_size, unsigned char *output, int userParam)
 
int hits_expand (unsigned char *input, int input_size, channel_hit *output, int userParam)
 
void caen_load ()
 
void caen_optimize ()
 
int cmp_compress (comp_word *input, int input_size, unsigned char *output, int userParam)
 
int cmp_expand (unsigned char *input, int input_size, comp_word *output, int userParam)
 
void fadc_load ()
 
void fadc_optimize ()
 
int fadc_compress (fadc_word *input, int input_size, unsigned char *output, int userParam)
 
int fadc_expand (unsigned char *input, int input_size, fadc_word *output, int userParam)
 
void nfadc_load ()
 
void nfadc_optimize ()
 
int nfadc_compress (unsigned char *input, int input_size, unsigned char *output, int userParam)
 
int nfadc_expand (unsigned char *input, int input_size, unsigned char *output, int userParam)
 
void n2fadc_tpc_load ()
 
void n2fadc_tpc_optimize ()
 
int n2fadc_tpc_compress (unsigned char *input, int input_size, unsigned char *output, int userParam)
 
int n2fadc_tpc_expand (unsigned char *input, int input_size, unsigned char *output, int userParam)
 
void n2fadc_ndet_load ()
 
void n2fadc_ndet_optimize ()
 
int n2fadc_ndet_compress (unsigned char *input, int input_size, unsigned char *output, int userParam)
 
int n2fadc_ndet_expand (unsigned char *input, int input_size, unsigned char *output, int userParam)
 
int read_midas_event (FILE *fp, char *pevent)
 
void compress_event (void *input_event, void *output_event, bool USE_OLD_COMPRESSION=false)
 
void compress_event_skim (void *input_event, void *output_event, bool keep_etc, bool USE_OLD_COMPRESSION=false)
 
void expand_event (void *input_event, void *output_event, bool USE_OLD_COMPRESSION=false)
 
void expand_bank (unsigned char *input_data, int input_size, void *output_event, char *output_name, expand_fcn f, int userParam)
 
void compress_load_all (bool USE_OLD_COMPRESSION=false)
 
void compress_optimize_all (bool USE_OLD_COMPRESSION=false)
 

Variables

huffman_table time_huffman
 
huffman_table bits_huffman [2]
 
BOOL should_compress_tdc400
 
huffman_table caen_channel_huffman
 
BOOL should_compress_caen
 
huffman_table cmp_channel_huffman
 
huffman_table cmp_bits_huffman [2]
 
BOOL should_compress_comp
 
huffman_table fadc_run_huffman
 
huffman_table fadc_small_huffman
 
BOOL should_compress_fadc
 
BOOL should_compress_nfadc
 
BOOL should_compress_n2fadc_tpc
 
BOOL should_compress_n2fadc_ndet
 
HNDLE hDB
 

Typedef Documentation

typedef struct caen_word caen_word

Definition at line 81 of file mucap_compress.h.

typedef struct channel_hit channel_hit

Definition at line 93 of file mucap_compress.h.

typedef union comp_word comp_word

Definition at line 102 of file mucap_compress.h.

typedef int(* expand_fcn)(unsigned char *input, int input_size, void *output, int userParam)

Definition at line 164 of file mucap_compress.h.

typedef struct fadc_word fadc_word

Definition at line 114 of file mucap_compress.h.

typedef struct huffman_node huffman_node

Definition at line 20 of file mucap_compress.h.

typedef struct huffman_table huffman_table
typedef struct io_buffer io_buffer

Definition at line 150 of file mucap_compress.h.

Definition at line 139 of file mucap_compress.h.

typedef struct nfadc_word nfadc_word

Definition at line 126 of file mucap_compress.h.

typedef struct rle_state rle_state
typedef struct stack_elt stack_elt

Definition at line 70 of file mucap_compress.h.

typedef struct tdc400_word tdc400_word

Definition at line 56 of file mucap_compress.h.

Function Documentation

int caen_compress ( caen_word input,
int  input_size,
unsigned char *  output,
int  userParam 
)

Definition at line 117 of file caen_compress.cpp.

References encode_caen(), rle_bits_setup(), and start_output_buffer().

Referenced by compress_event().

118 {
119  input_size /= sizeof(caen_word);
120 
121  rle_bits_setup();
122 
123  io_buffer output_buffer;
124 
125  // reserve space for size of uncompressed data (may be different from
126  // input size, since some hits may be stripped)
127  int *uncompressed_size_p = (int *) output;
128  output += sizeof(int);
129 
130  // reserve space for the size of the compressed data
131  int *compressed_time_size_p = (int *) output;
132  output += sizeof(int);
133 
134  // compress times
135  start_output_buffer(&output_buffer, output);
136  int compressed_time_size =
137  encode_caen(input, input_size, &output_buffer, uncompressed_size_p);
138  output += compressed_time_size;
139 
140  // store size of compressed times
141  *compressed_time_size_p = compressed_time_size;
142 
143  return compressed_time_size + 2 * sizeof(int);
144 }
int caen_expand ( unsigned char *  input,
int  input_size,
caen_word output,
int  userParam 
)

Definition at line 219 of file caen_compress.cpp.

References decode_caen(), rle_bits_setup(), and start_input_buffer().

Referenced by expand_event().

220 {
221  //
222  // Uncompress the data produced by caen_compress().
223  //
224 
225  rle_bits_setup();
226 
227  // Get uncompressed data size
228  int *uncompressed_size_p = (int *) input;
229  int uncompressed_size = *uncompressed_size_p;
230  input += sizeof(int);
231  input_size -= sizeof(int);
232 
233  // Get size of compressed time data
234  int *compressed_time_size_p = (int *) input;
235  int compressed_time_size = *compressed_time_size_p;
236  input += sizeof(int);
237  input_size -= sizeof(int);
238 
239  // Uncompress times
240  io_buffer input_buffer;
241  start_input_buffer(&input_buffer, input);
242  decode_caen(&input_buffer, output);
243  input += compressed_time_size;
244  input_size -= compressed_time_size;
245 
246  return uncompressed_size * sizeof(caen_word);
247 }
void caen_load ( )

Definition at line 25 of file caen_compress.cpp.

References hDB, huffman_init_default(), load_huffman(), should_compress_caen, should_strip_r1, should_strip_trailing_edge, size, and TRUE.

Referenced by compress_load_all().

26 {
28  load_huffman("/Compression/Lossless/CAEN/Huffman/Channel", &caen_channel_huffman);
29 
30  int size = sizeof(BOOL);
31  db_get_value(hDB, 0, "/Compression/Lossless/CAEN/Enabled", &should_compress_caen,
32  &size, TID_BOOL, TRUE);
33 
34  size = sizeof(BOOL);
35  db_get_value(hDB, 0, "/Compression/Lossless/CAEN/Strip trailing edge",
36  &should_strip_trailing_edge, &size, TID_BOOL, TRUE);
37 
38  size = sizeof(BOOL);
39  db_get_value(hDB, 0, "/Compression/Lossless/CAEN/Strip R1",
40  &should_strip_r1, &size, TID_BOOL, TRUE);
41 }
void caen_optimize ( )

Definition at line 19 of file caen_compress.cpp.

References huffman_optimize_tree(), and save_huffman().

Referenced by compress_optimize_all().

20 {
22  save_huffman("/Compression/Lossless/CAEN/Huffman/Channel", &caen_channel_huffman);
23 }
int cmp_compress ( comp_word input,
int  input_size,
unsigned char *  output,
int  userParam 
)

Definition at line 133 of file comp_compress.cpp.

References encode_cmp_bits(), encode_cmp_times(), rle_bits_setup(), and start_output_buffer().

Referenced by compress_event().

134 {
135  //
136  // Encode compressor data, which are in the following format:
137  // | B | A | FPGA number (6 bits) | Time (24 bits) |
138  // ... followed by one or two 32-bit masks indicating which
139  // channels
140  // within the group have fired.
141  //
142 
143  input_size /= sizeof(comp_word);
144 
145  rle_bits_setup();
146 
147  io_buffer output_buffer;
148 
149  // store size of input data
150  int *uncompressed_size_p = (int *) output;
151  *uncompressed_size_p = input_size;
152  output += sizeof(int);
153 
154  // reserve space for the size of the compressed times
155  int *compressed_time_size_p = (int *) output;
156  output += sizeof(int);
157 
158  // compress times
159  start_output_buffer(&output_buffer, output);
160  int compressed_time_size =
161  encode_cmp_times(input, input_size, &output_buffer);
162  output += compressed_time_size;
163 
164  // store size of compressed times
165  *compressed_time_size_p = compressed_time_size;
166 
167  // reserve space for the size of the compressed bit patterns
168  int *compressed_bits_size_p = (int *) output;
169  output += sizeof(int);
170 
171  // compress bit patterns
172  start_output_buffer(&output_buffer, output);
173  int compressed_bits_size =
174  encode_cmp_bits(input, input_size, &output_buffer);
175  output += compressed_time_size;
176 
177  // store size of compressed bit patterns
178  *compressed_bits_size_p = compressed_bits_size;
179 
180  return compressed_time_size + compressed_bits_size + 3 * sizeof(int);
181 }
int cmp_expand ( unsigned char *  input,
int  input_size,
comp_word output,
int  userParam 
)

Definition at line 243 of file comp_compress.cpp.

References decode_cmp_bits(), decode_cmp_times(), rle_bits_setup(), and start_input_buffer().

Referenced by expand_event().

244 {
245  //
246  // Uncompress the data produced by cmp_compress().
247  //
248 
249  rle_bits_setup();
250 
251  // Get uncompressed data size
252  int *uncompressed_size_p = (int *) input;
253  int uncompressed_size = *uncompressed_size_p;
254  input += sizeof(int);
255  input_size -= sizeof(int);
256 
257  // Get size of compressed time data
258  int *compressed_time_size_p = (int *) input;
259  int compressed_time_size = *compressed_time_size_p;
260  input += sizeof(int);
261  input_size -= sizeof(int);
262 
263  // Uncompress times
264  io_buffer input_buffer;
265  start_input_buffer(&input_buffer, input);
266  decode_cmp_times(&input_buffer, output);
267  input += compressed_time_size;
268  input_size -= compressed_time_size;
269 
270  // Get size of compressed bit patterns
271  int *compressed_bits_size_p = (int *) input;
272  int compressed_bits_size = *compressed_bits_size_p;
273  input += sizeof(int);
274  input_size -= sizeof(int);
275 
276  // Uncompress bit patterns
277  start_input_buffer(&input_buffer, input);
278  decode_cmp_bits(&input_buffer, output, uncompressed_size);
279 
280  return uncompressed_size * sizeof(comp_word);
281 }
void comp_load ( )

Definition at line 29 of file comp_compress.cpp.

References hDB, huffman_init_default(), load_huffman(), should_compress_comp, size, and TRUE.

Referenced by compress_load_all().

30 {
34 
35  load_huffman("/Compression/Lossless/COMP/Huffman/Channel", &cmp_channel_huffman);
36  load_huffman("/Compression/Lossless/COMP/Huffman/Bits 0", &cmp_bits_huffman[0]);
37  load_huffman("/Compression/Lossless/COMP/Huffman/Bits 1", &cmp_bits_huffman[1]);
38 
39  int size = sizeof(BOOL);
40  db_get_value(hDB, 0, "/Compression/Lossless/COMP/Enabled", &should_compress_comp,
41  &size, TID_BOOL, TRUE);
42 }
void comp_optimize ( )

Definition at line 18 of file comp_compress.cpp.

References huffman_optimize_tree(), and save_huffman().

Referenced by compress_optimize_all().

19 {
23 
24  save_huffman("/Compression/Lossless/COMP/Huffman/Channel", &cmp_channel_huffman);
25  save_huffman("/Compression/Lossless/COMP/Huffman/Bits 0", &cmp_bits_huffman[0]);
26  save_huffman("/Compression/Lossless/COMP/Huffman/Bits 1", &cmp_bits_huffman[1]);
27 }
void compress_event ( void *  input_event,
void *  output_event,
bool  USE_OLD_COMPRESSION = false 
)

Definition at line 69 of file overall.cpp.

References caen_compress(), cmp_compress(), compress_bank(), fadc_compress(), n2fadc_ndet_compress(), n2fadc_tpc_compress(), nfadc_compress(), printf(), should_compress_caen, should_compress_comp, should_compress_fadc, should_compress_n2fadc_ndet, should_compress_n2fadc_tpc, should_compress_nfadc, should_compress_tdc400, sprintf(), and tdc400_compress().

Referenced by eb_user(), and main().

70 {
71  // If it's not a 32-bit event, just copy from input to output
72  BANK_HEADER *bank_header = (BANK_HEADER *)input_event;
73  BOOL is_32 = (bank_header->flags & BANK_FORMAT_32BIT);
74 
75  if(!is_32) {
76  memcpy(output_event, input_event,
77  bank_header->data_size + sizeof(BANK_HEADER));
78  return;
79  }
80 
81  BANK32 *input_bank = NULL;
82  void *input_data = NULL;
83 
84  while (1) {
85  int input_size =
86  bk_iterate32(input_event, &input_bank, &input_data);
87  if (input_bank == NULL) {
88  break;
89  }
90 
91  char output_name[5];
92  if (should_compress_tdc400 && !strncmp(input_bank->name, "TDC", 3)) {
93  sprintf(output_name, "CTD%c", input_bank->name[3]);
94  compress_bank(input_data, input_size, output_event,
95  output_name, (comp_fcn) tdc400_compress, 0);
96  } else if (should_compress_fadc
97  && !strncmp(input_bank->name, "ADC", 3)) {
98  sprintf(output_name, "CAD%c", input_bank->name[3]);
99  compress_bank(input_data, input_size, output_event,
100  output_name, (comp_fcn) fadc_compress, 0);
101  } else if (USE_OLD_COMPRESSION &&
103  && input_bank->name[0] == 'N'
104  && (input_bank->name[1] == 'a' ||
105  input_bank->name[1] == 'b' ||
106  input_bank->name[1] == 'c' ||
107  input_bank->name[1] == 'd')
108  && isxdigit(input_bank->name[2])
109  && isxdigit(input_bank->name[3])) {
110  printf("Using run10 compression\n");
111  sprintf(output_name, "n%c%c%c", input_bank->name[1],
112  input_bank->name[2], input_bank->name[3]);
113  int board_number = 16*(input_bank->name[2]-'0') + (input_bank->name[3]-'0');
114  compress_bank(input_data, input_size, output_event,
115  output_name, (comp_fcn) nfadc_compress, board_number);
116  } //start paste
117  else if (!USE_OLD_COMPRESSION &&
118  input_bank->name[0] == 'N'
119  && input_bank->name[1] >= 'a'
120  && input_bank->name[1] <= 'h'
121  && isxdigit(input_bank->name[2])
122  && isxdigit(input_bank->name[3])) {
123  printf("Using run11+ compression\n");
124  sprintf(output_name, "n%c%c%c", input_bank->name[1],
125  input_bank->name[2], input_bank->name[3]);
126  int board_number = 16*(input_bank->name[2]-'0') + (input_bank->name[3]-'0');
127  if(board_number < 128 && should_compress_n2fadc_tpc) {
128  compress_bank(input_data, input_size, output_event,
129  output_name, (comp_fcn) n2fadc_tpc_compress, board_number);
130  }else if(should_compress_n2fadc_ndet) {
131  compress_bank(input_data, input_size, output_event,
132  output_name, (comp_fcn) n2fadc_ndet_compress, board_number);
133  }
134  }//end paste
135  else if (should_compress_caen
136  && !strncmp(input_bank->name, "CAE", 3)) {
137  sprintf(output_name, "CCA%c", input_bank->name[3]);
138  compress_bank(input_data, input_size, output_event,
139  output_name, (comp_fcn) caen_compress, 0);
140  } else if (should_compress_comp
141  && !strncmp(input_bank->name, "CMP", 3)) {
142  sprintf(output_name, "CCM%c", input_bank->name[3]);
143  compress_bank(input_data, input_size, output_event,
144  output_name, (comp_fcn) cmp_compress, 0);
145  } else {
146  // Otherwise: if the input and output events are not the same,
147  // just copy the bank whole.
148  if(input_event != output_event) {
149  char *output_data;
150  bk_create(output_event, input_bank->name, input_bank->type,
151  &output_data);
152  memcpy(output_data, input_data, input_size);
153  bk_close(output_event, output_data + input_size);
154  }
155  }
156  }
157 }
void compress_event_skim ( void *  input_event,
void *  output_event,
bool  keep_etc,
bool  USE_OLD_COMPRESSION = false 
)

Definition at line 160 of file overall.cpp.

References compress_bank(), fadc_compress(), hits_compress(), n2fadc_ndet_compress(), n2fadc_tpc_compress(), nfadc_compress(), should_compress_n2fadc_ndet, should_compress_n2fadc_tpc, should_compress_nfadc, sprintf(), and stck_compress().

161 {
162  // If it's not a 32-bit event, just copy from input to output
163  BANK_HEADER *bank_header = (BANK_HEADER *)input_event;
164  BOOL is_32 = (bank_header->flags & BANK_FORMAT_32BIT);
165 
166  if(!is_32 || EVENT_ID(input_event) != 1) {
167  memcpy(output_event, input_event,
168  bank_header->data_size + sizeof(BANK_HEADER));
169  return;
170  }
171 
172  BANK32 *input_bank = NULL;
173  void *input_data = NULL;
174 
175  while (1) {
176  int input_size =
177  bk_iterate32(input_event, &input_bank, &input_data);
178  if (input_bank == NULL) {
179  break;
180  }
181 
182  char output_name[5];
183  if (!strncmp(input_bank->name, "RTPC", 4)) {
184  compress_bank(input_data, input_size, output_event,
185  "CTPC", (comp_fcn) stck_compress, 0);
186  } else if (!strncmp(input_bank->name, "RHIT", 4)) {
187  compress_bank(input_data, input_size, output_event,
188  "HITZ", (comp_fcn) hits_compress, 0);
189  } else if (!strncmp(input_bank->name, "SKIM", 4)) {
190  // save bank of gskim triggers
191  if(input_event != output_event) {
192  char *output_data;
193  bk_create(output_event, "GSKM", input_bank->type,
194  &output_data);
195  memcpy(output_data, input_data, input_size);
196  bk_close(output_event, output_data + input_size);
197  }
198  } else if (USE_OLD_COMPRESSION &&
200  && input_bank->name[0] == 'N'
201  && (input_bank->name[1] == 'a' ||
202  input_bank->name[1] == 'b' ||
203  input_bank->name[1] == 'c' ||
204  input_bank->name[1] == 'd')
205  && isxdigit(input_bank->name[2])
206  && isxdigit(input_bank->name[3])) {
207  sprintf(output_name, "n%c%c%c", input_bank->name[1],
208  input_bank->name[2], input_bank->name[3]);
209  int board_number = 16*(input_bank->name[2]-'0') + (input_bank->name[3]-'0');
210  compress_bank(input_data, input_size, output_event,
211  output_name, (comp_fcn) nfadc_compress, board_number);
212  } //start paste
213  else if (!USE_OLD_COMPRESSION &&
214  input_bank->name[0] == 'N'
215  && input_bank->name[1] >= 'a'
216  && input_bank->name[1] <= 'h'
217  && isxdigit(input_bank->name[2])
218  && isxdigit(input_bank->name[3])) {
219  sprintf(output_name, "n%c%c%c", input_bank->name[1],
220  input_bank->name[2], input_bank->name[3]);
221  int board_number = 16*(input_bank->name[2]-'0') + (input_bank->name[3]-'0');
222  if(board_number < 128 && should_compress_n2fadc_tpc) {
223  compress_bank(input_data, input_size, output_event,
224  output_name, (comp_fcn) n2fadc_tpc_compress, board_number);
225  }else if(should_compress_n2fadc_ndet) {
226  compress_bank(input_data, input_size, output_event,
227  output_name, (comp_fcn) n2fadc_ndet_compress, board_number);
228  }
229  }//end paste
230  else if (!strncmp(input_bank->name, "ADC", 3)) {
231  sprintf(output_name, "CAD%c", input_bank->name[3]);
232  compress_bank(input_data, input_size, output_event,
233  output_name, (comp_fcn) fadc_compress, 0);
234  } else if (keep_etc && (!strncmp(input_bank->name, "LAD", 3) ||
235 
236  // !strncmp(input_bank->name, "LSPK", 4) ||
237  !strncmp(input_bank->name, "CAET", 4)))
238  {
239  // Otherwise: if the input and output events are not the same,
240  // just copy the bank whole.
241 
242  if(input_event != output_event) {
243  char *output_data;
244  bk_create(output_event, input_bank->name, input_bank->type,
245  &output_data);
246  memcpy(output_data, input_data, input_size);
247  bk_close(output_event, output_data + input_size);
248  }
249  }
250  }
251 }
void compress_load_all ( bool  USE_OLD_COMPRESSION = false)

Definition at line 373 of file overall.cpp.

References caen_load(), comp_load(), fadc_load(), hits_load(), n2fadc_ndet_load(), n2fadc_tpc_load(), nfadc_load(), stck_load(), and tdc400_load().

Referenced by eb_begin_of_run(), main(), MUnCompressRawData_init(), and process_file().

374 {
375  tdc400_load();
376  fadc_load();
377 
378  if (USE_OLD_COMPRESSION){
379  nfadc_load();}
380  else{
381  n2fadc_tpc_load();
383  }
384  caen_load();
385  comp_load();
386  hits_load();
387  stck_load();
388 }
void compress_optimize_all ( bool  USE_OLD_COMPRESSION = false)

Definition at line 390 of file overall.cpp.

References caen_optimize(), comp_optimize(), fadc_optimize(), n2fadc_ndet_optimize(), n2fadc_tpc_optimize(), nfadc_optimize(), and tdc400_optimize().

Referenced by main().

391 {
392  tdc400_optimize();
393  fadc_optimize();
394  if (USE_OLD_COMPRESSION){
395  nfadc_optimize();
396  }
397  else{
400  }
401  caen_optimize();
402  comp_optimize();
403 }
void expand_bank ( unsigned char *  input_data,
int  input_size,
void *  output_event,
char *  output_name,
expand_fcn  f,
int  userParam 
)

Definition at line 255 of file overall.cpp.

References printf().

Referenced by expand_event().

257 {
258  unsigned char *uncompressed_bank;
259  int bank_type = TID_DWORD;
260  if(output_name[0] == 'N') bank_type = TID_BYTE;
261  bk_create(output_event, output_name, bank_type, &uncompressed_bank);
262 
263  // uncompress the data; "sizeof(int)" accounts for the checksum word
264  int uncompressed_size = (*f) (input_data, input_size - sizeof(int),
265  uncompressed_bank, userParam);
266 
267  // verify the Adler32 checksum
268  int checksum = adler32(0x5555, uncompressed_bank, uncompressed_size);
269  int *checksum_ptr = (int *) &input_data[input_size-sizeof(int)];
270  if(checksum != *checksum_ptr) {
271  printf("*** CHECKSUM TEST FAILED: Bank %s (0x%x -> 0x%x) ***\n",
272  output_name, *checksum_ptr, checksum);
273  } else {
274 
275  // printf("Checksum OK for bank %s (0x%x)\n", output_name, checksum);
276 
277 
278  }
279 
280  bk_close(output_event, uncompressed_bank + uncompressed_size);
281 }
void expand_event ( void *  input_event,
void *  output_event,
bool  USE_OLD_COMPRESSION = false 
)

Definition at line 283 of file overall.cpp.

References caen_expand(), cmp_expand(), expand_bank(), fadc_expand(), hits_expand(), n2fadc_ndet_expand(), n2fadc_tpc_expand(), nfadc_expand(), sprintf(), stck_expand(), and tdc400_expand().

Referenced by main(), MUnCompressRawData(), and process_file().

284 {
285  // If it's not a 32-bit event, just copy from input to output
286  BANK_HEADER *bank_header = (BANK_HEADER *)input_event;
287  BOOL is_32 = (bank_header->flags & BANK_FORMAT_32BIT);
288 
289  if(!is_32) {
290  memcpy(output_event, input_event,
291  bank_header->data_size + sizeof(BANK_HEADER));
292  return;
293  }
294 
295  BANK32 *input_bank = NULL;
296  unsigned char *input_data = NULL;
297 
298  while (1) {
299  int input_size =
300  bk_iterate32(input_event, &input_bank, &input_data);
301  if (input_bank == NULL) {
302  break;
303  }
304 
305  char output_name[5];
306  if (!strncmp(input_bank->name, "CTD", 3)) {
307  sprintf(output_name, "TDC%c", input_bank->name[3]);
308  expand_bank(input_data, input_size, output_event,
309  output_name, (expand_fcn) tdc400_expand, 0);
310  } else if(USE_OLD_COMPRESSION &&
311  input_bank->name[0] == 'n'
312  && (input_bank->name[1] == 'a' ||
313  input_bank->name[1] == 'b' ||
314  input_bank->name[1] == 'c' ||
315  input_bank->name[1] == 'd')
316  && isxdigit(input_bank->name[2])
317  && isxdigit(input_bank->name[3])) {
318  sprintf(output_name, "N%c%c%c", input_bank->name[1],
319  input_bank->name[2], input_bank->name[3]);
320  int board_number = 16*(input_bank->name[2]-'0') + (input_bank->name[3]-'0');
321  expand_bank(input_data, input_size, output_event,
322  output_name, (expand_fcn) nfadc_expand, board_number);
323  }
324  else if(!USE_OLD_COMPRESSION &&
325  input_bank->name[0] == 'n'
326  && input_bank->name[1] >= 'a'
327  && input_bank->name[1] <= 'h'
328  && isxdigit(input_bank->name[2])
329  && isxdigit(input_bank->name[3])) {
330  sprintf(output_name, "N%c%c%c", input_bank->name[1],
331  input_bank->name[2], input_bank->name[3]);
332  int board_number = 16*(input_bank->name[2]-'0') + (input_bank->name[3]-'0');
333  if(board_number < 128) {
334  expand_bank(input_data, input_size, output_event,
335  output_name, (expand_fcn) n2fadc_tpc_expand, board_number);
336  } else {
337  expand_bank(input_data, input_size, output_event,
338  output_name, (expand_fcn) n2fadc_ndet_expand, board_number);
339  }
340  }
341  else if (!strncmp(input_bank->name, "CAD", 3)) {
342  sprintf(output_name, "ADC%c", input_bank->name[3]);
343  expand_bank(input_data, input_size, output_event,
344  output_name, (expand_fcn) fadc_expand, 0);
345  } else if (!strncmp(input_bank->name, "CCA", 3)) {
346  sprintf(output_name, "CAE%c", input_bank->name[3]);
347  expand_bank(input_data, input_size, output_event,
348  output_name, (expand_fcn) caen_expand, 0);
349  } else if (!strncmp(input_bank->name, "CCM", 3)) {
350  sprintf(output_name, "CMP%c", input_bank->name[3]);
351  expand_bank(input_data, input_size, output_event,
352  output_name, (expand_fcn) cmp_expand, 0);
353  } else if (!strncmp(input_bank->name, "HITZ", 4)) {
354  expand_bank(input_data, input_size, output_event,
355  "HITS", (expand_fcn) hits_expand, 0);
356  } else if (!strncmp(input_bank->name, "CTPC", 4)) {
357  expand_bank(input_data, input_size, output_event,
358  "STCK", (expand_fcn) stck_expand, 0);
359  } else {
360  // Otherwise: if the input and output events are not the same,
361  // just copy the bank whole.
362  if(input_event != output_event) {
363  char *output_data;
364  bk_create(output_event, input_bank->name, input_bank->type,
365  &output_data);
366  memcpy(output_data, input_data, input_size);
367  bk_close(output_event, output_data + input_size);
368  }
369  }
370  }
371 }
int fadc_compress ( fadc_word input,
int  input_size,
unsigned char *  output,
int  userParam 
)

Definition at line 92 of file fadc_compress.cpp.

References encode_fadc(), rle_bits_setup(), and start_output_buffer().

Referenced by compress_event(), and compress_event_skim().

93 {
94  input_size /= sizeof(fadc_word);
95 
97 
98  io_buffer output_buffer;
99 
100  // store size of input data
101  int *uncompressed_size_p = (int *) output;
102  *uncompressed_size_p = input_size;
103  output += sizeof(int);
104 
105  // reserve space for the size of the compressed data
106  int *compressed_time_size_p = (int *) output;
107  output += sizeof(int);
108 
109  // compress samples
110  start_output_buffer(&output_buffer, output);
111  int compressed_size = encode_fadc(input, input_size, &output_buffer);
112  output += compressed_size;
113 
114  // store size of compressed times
115  *compressed_time_size_p = compressed_size;
116 
117  return compressed_size + 2 * sizeof(int);
118 }
int fadc_expand ( unsigned char *  input,
int  input_size,
fadc_word output,
int  userParam 
)

Definition at line 165 of file fadc_compress.cpp.

References decode_fadc(), rle_bits_setup(), and start_input_buffer().

Referenced by expand_event().

166 {
167  //
168  // Uncompress the data produced by caen_compress().
169  //
170 
171  rle_bits_setup();
172 
173  // Get uncompressed data size
174  int *uncompressed_size_p = (int *) input;
175  int uncompressed_size = *uncompressed_size_p;
176  input += sizeof(int);
177  input_size -= sizeof(int);
178 
179  // Get size of compressed data
180  int *compressed_size_p = (int *) input;
181  int compressed_size = *compressed_size_p;
182  input += sizeof(int);
183  input_size -= sizeof(int);
184 
185  // Uncompress data
186  io_buffer input_buffer;
187  start_input_buffer(&input_buffer, input);
188  decode_fadc(&input_buffer, output, uncompressed_size);
189  input += compressed_size;
190  input_size -= compressed_size;
191 
192  return uncompressed_size * sizeof(fadc_word);
193 }
void fadc_load ( )

Definition at line 29 of file fadc_compress.cpp.

References hDB, huffman_init_default(), load_huffman(), should_compress_fadc, size, and TRUE.

Referenced by compress_load_all().

30 {
33 
34  load_huffman("/Compression/Lossless/FADC/Huffman/Run", &fadc_run_huffman);
35  load_huffman("/Compression/Lossless/FADC/Huffman/Small", &fadc_small_huffman);
36 
37  int size = sizeof(BOOL);
38  db_get_value(hDB, 0, "/Compression/Lossless/FADC/Enabled", &should_compress_fadc,
39  &size, TID_BOOL, TRUE);
40 }
void fadc_optimize ( )

Definition at line 20 of file fadc_compress.cpp.

References huffman_optimize_tree(), and save_huffman().

Referenced by compress_optimize_all().

21 {
24 
25  save_huffman("/Compression/Lossless/FADC/Huffman/Run", &fadc_run_huffman);
26  save_huffman("/Compression/Lossless/FADC/Huffman/Small", &fadc_small_huffman);
27 }
int flush_output_buffer ( io_buffer buffer)

Definition at line 23 of file io_buffer.cpp.

References io_buffer::num_codes, io_buffer::p, io_buffer::start, io_buffer::w, and io_buffer::w_bits.

Referenced by encode_caen(), encode_cmp_times(), encode_fadc(), encode_hits(), encode_n2fadc_ndet(), encode_n2fadc_tpc(), encode_nfadc(), encode_times(), and flush_rle().

24 {
25  // store the last word
26  *((buffer->p)++) = buffer->w << buffer->w_bits;
27 
28  // remember the length of the buffer, in bytes and in codes
29  *(buffer->start) = buffer->num_codes;
30 
31  //return ((unsigned int) buffer->p) - ((unsigned int) buffer->start);
32  return (int) (((char *) buffer->p) - ((char *) buffer->start));
33 }
int flush_rle ( rle_state s,
io_buffer output 
)

Definition at line 103 of file rle.cpp.

References flush_output_buffer(), rle_state::huffman, rle_state::huffman_length, huffman_put_symbol(), io_buffer_put(), rle_state::run_length, and rle_state::running_digit.

Referenced by encode_bits(), and encode_cmp_bits().

104 {
105  if (s->run_length != 0) {
106  if(s->run_length < s->huffman_length[s->running_digit]) {
107  huffman_put_symbol(&s->huffman[s->running_digit], output, s->run_length);
108  } else {
110  io_buffer_put(output, s->run_length, 24);
111  }
112  }
113 
114  return flush_output_buffer(output);
115 }
int hits_compress ( channel_hit input,
int  input_size,
unsigned char *  output,
int  userParam 
)

Definition at line 77 of file hits_compress.cpp.

References encode_hits(), rle_bits_setup(), and start_output_buffer().

Referenced by compress_event_skim().

78 {
79  input_size /= sizeof(channel_hit);
80 
82 
83  io_buffer output_buffer;
84 
85  // reserve space for size of uncompressed data (may be different from
86  // input size, since some hits may be stripped)
87  int *uncompressed_size_p = (int *) output;
88  output += sizeof(int);
89 
90  // reserve space for the size of the compressed data
91  int *compressed_time_size_p = (int *) output;
92  output += sizeof(int);
93 
94  // compress times
95  start_output_buffer(&output_buffer, output);
96  int compressed_time_size =
97  encode_hits(input, input_size, &output_buffer, uncompressed_size_p);
98  output += compressed_time_size;
99 
100  // store size of compressed times
101  *compressed_time_size_p = compressed_time_size;
102 
103  return compressed_time_size + 2 * sizeof(int);
104 }
int hits_expand ( unsigned char *  input,
int  input_size,
channel_hit output,
int  userParam 
)

Definition at line 148 of file hits_compress.cpp.

References decode_hits(), rle_bits_setup(), and start_input_buffer().

Referenced by expand_event().

149 {
150  //
151  // Uncompress the data produced by hits_compress().
152  //
153 
154  rle_bits_setup();
155 
156  // Get uncompressed data size
157  int *uncompressed_size_p = (int *) input;
158  int uncompressed_size = *uncompressed_size_p;
159  input += sizeof(int);
160  input_size -= sizeof(int);
161 
162  // Get size of compressed time data
163  int *compressed_time_size_p = (int *) input;
164  int compressed_time_size = *compressed_time_size_p;
165  input += sizeof(int);
166  input_size -= sizeof(int);
167 
168  // Uncompress times
169  io_buffer input_buffer;
170  start_input_buffer(&input_buffer, input);
171  decode_hits(&input_buffer, output);
172  input += compressed_time_size;
173  input_size -= compressed_time_size;
174 
175  return uncompressed_size * sizeof(channel_hit);
176 }
void hits_load ( )

Definition at line 24 of file hits_compress.cpp.

References hDB, huffman_init_default(), load_huffman(), should_compress_hits, size, and TRUE.

Referenced by compress_load_all().

25 {
27  load_huffman("/Compression/Lossless/HITS/Huffman/Parameter", &hits_parameter_huffman);
28 
29  int size = sizeof(BOOL);
30  db_get_value(hDB, 0, "/Compression/Lossless/HITS/Enabled", &should_compress_hits,
31  &size, TID_BOOL, TRUE);
32 }
void hits_optimize ( )

Definition at line 18 of file hits_compress.cpp.

References huffman_optimize_tree(), and save_huffman().

Referenced by compress_optimize_skim().

19 {
21  save_huffman("/Compression/Lossless/HITS/Huffman/Parameter", &hits_parameter_huffman);
22 }
int huffman_get_symbol ( huffman_table table,
io_buffer buffer 
)

Definition at line 87 of file huffman.cpp.

References huffman_node::branch0, huffman_node::branch1, huffman_table::code_length, huffman_table::decode_symbols, io_buffer::num_codes, io_buffer::p, huffman_node::symbol, huffman_table::tree, io_buffer::w, and io_buffer::w_bits.

Referenced by decode_caen(), decode_cmp_times(), decode_fadc(), decode_hits(), decode_times(), and rle_get().

88 {
89  // Get the next symbol from a Huffman-coded buffer. Again, this
90  // function is in an inner loop and therefore performance-critical.
91 
92  if (buffer->num_codes <= 0) {
93  return -1;
94  }
95  buffer->num_codes--;
96 
97  // If we have at least 8 bits pre-read from the buffer, then check the
98  // table of pre-computed short symbols.
99  if(buffer->w_bits >= 8) {
100  unsigned int byte = (buffer->w >> 24) & 0xff;
101  int symbol = table->decode_symbols[byte];
102  if(symbol != -1) {
103  int code_length = table->code_length[symbol];
104  buffer->w <<= code_length;
105  buffer->w_bits -= code_length;
106  return symbol;
107  }
108  }
109 
110  // Otherwise, do it the slow way, walking bit-by-bit through the tree
111  huffman_node *node = table->tree;
112 
113  while (1) {
114  // We may already be there...
115  if (node->symbol != -1) {
116  return node->symbol;
117  } else {
118  // If not, consume a bit from the input and follow the tree
119  if (buffer->w_bits == 0) {
120  buffer->w = *((buffer->p)++);
121  buffer->w_bits = 32;
122  }
123 
124  unsigned int bit = buffer->w & (1u << 31);
125  buffer->w <<= 1;
126  buffer->w_bits--;
127 
128  if (bit == 0) {
129  node = node->branch0;
130  } else {
131  node = node->branch1;
132  }
133  }
134  }
135 }
void huffman_init_default ( huffman_table table,
int  num_symbols 
)

Definition at line 305 of file huffman.cpp.

References huffman_table::code_bits, huffman_table::code_length, huffman_table::frequency, huffman_optimize_tree(), i, huffman_table::num_symbols, and huffman_table::tree.

Referenced by caen_load(), comp_load(), fadc_load(), hits_load(), stck_load(), and tdc400_load().

306 {
307  table->num_symbols = num_symbols;
308  table->frequency = new int[num_symbols];
309  table->code_bits = new unsigned int[num_symbols];
310  table->code_length = new int[num_symbols];
311  table->tree = NULL;
312 
313  // Assume that all symbols have equal probability
314  for (int i = 0; i < num_symbols; i++) {
315  table->frequency[i] = 1;
316  }
317 
318  huffman_optimize_tree(table);
319 }
BOOL huffman_optimize_tree ( huffman_table table)

Definition at line 200 of file huffman.cpp.

References huffman_node::branch0, huffman_node::branch1, huffman_table::code_length, huffman_table::frequency, huffman_node::frequency, huffman_node_compare(), huffman_precompute_decode(), huffman_visit(), i, huffman_node::next, huffman_table::num_symbols, SUCCESS, huffman_node::symbol, and huffman_table::tree.

Referenced by caen_optimize(), comp_optimize(), fadc_optimize(), hits_optimize(), huffman_init_default(), stck_optimize(), and tdc400_optimize().

201 {
202  // Start by allocating terminal nodes for the symbols.
203  huffman_node *terminals[table->num_symbols];
204  for (int i = 0; i < table->num_symbols; i++) {
205  huffman_node *node = new huffman_node;
206  node->branch0 = NULL;
207  node->branch1 = NULL;
208  node->next = NULL;
209  node->symbol = i;
210  node->frequency = table->frequency[i];
211  terminals[i] = node;
212  }
213 
214  // Now sort the array of terminals by frequency.
215  qsort(terminals, table->num_symbols,
217 
218  // Set up the linked list pointers
219  for (int i = 0; i < table->num_symbols - 1; i++) {
220  terminals[i]->next = terminals[i + 1];
221  }
222  huffman_node *root = terminals[0];
223 
224  // Now repeat: draw the two least probable nodes
225  // and combine them, sorting the resulting combination
226  // into the correct position in the list.
227  while (root->next != NULL) {
228  huffman_node *node0 = root;
229  huffman_node *node1 = node0->next;
230  root = node1->next;
231 
232  huffman_node *combined = new huffman_node;
233  combined->branch0 = node0;
234  combined->branch1 = node1;
235  combined->next = NULL;
236  combined->symbol = -1;
237  combined->frequency = node0->frequency + node1->frequency;
238 
239  if (root == NULL) {
240  root = combined;
241  } else {
242  // Search through the list to find the right position for the
243  // new
244  // node, and put it there.
245  huffman_node *pos = root;
246  huffman_node *prev = NULL;
247  while (pos != NULL) {
248  if (combined->frequency <= pos->frequency) {
249  combined->next = pos;
250  if (prev != NULL) {
251  prev->next = combined;
252  } else {
253  root = combined;
254  }
255  break;
256  }
257  prev = pos;
258  pos = pos->next;
259  }
260 
261  if (pos == NULL) {
262  combined->next = NULL;
263  prev->next = combined;
264  }
265 
266 #ifdef DEBUG
267  // Check that we didn't screw up
268  pos = root;
269  prev = NULL;
270  int node_count = 0;
271  while (pos != NULL) {
272  if (prev != NULL && pos->frequency < prev->frequency) {
273  cm_msg(MERROR, "huffman_optimize_tree", "List out of order");
274  return !SUCCESS;
275  }
276  prev = pos;
277  pos = pos->next;
278  node_count++;
279  }
280 #endif
281  }
282  }
283 
284  table->tree = root;
285 
286  // Now we have the tree; read off the codes by recursively visiting
287  // each node.
288  huffman_visit(table, root, 0, 0);
289 
290  // Check that the number of bits used by each key is less than 32.
291  for (int i = 0; i < table->num_symbols; i++) {
292  if(table->code_length[i] > 32) {
293  cm_msg(MERROR, "huffman_optimize_tree",
294  "Code length for symbol %d is too long (%d bits)", i,
295  table->code_length[i]);
296  return !SUCCESS;
297  }
298  }
299 
301 
302  return SUCCESS;
303 }
void huffman_put_symbol ( huffman_table table,
io_buffer buffer,
int  symbol 
)

Definition at line 28 of file huffman.cpp.

References huffman_table::code_bits, huffman_table::code_length, huffman_table::frequency, io_buffer_put(), io_buffer::num_codes, and huffman_node::symbol.

Referenced by encode_caen(), encode_cmp_times(), encode_fadc(), encode_hits(), encode_times(), flush_rle(), and rle_put().

29 {
30  // Huffman encode a single symbol and send it to an output buffer.
31  // This function is in an inner loop and therefore
32  // performance-critical.
33 
34  // Update the frequency count for this symbol.
35  table->frequency[symbol]++;
36 
37  // Get the code corresponding to this symbol.
38  unsigned int code_bits = table->code_bits[symbol];
39  int code_length = table->code_length[symbol];
40 
41  // Put it in the buffer
42  io_buffer_put(buffer, code_bits, code_length);
43 
44  buffer->num_codes++;
45 }
unsigned int io_buffer_get ( io_buffer buffer,
int  num_bits 
)

Definition at line 70 of file io_buffer.cpp.

References io_buffer_get(), io_buffer::p, io_buffer::w, and io_buffer::w_bits.

Referenced by decode_caen(), decode_cmp_times(), decode_fadc(), decode_hits(), decode_n2fadc_ndet(), decode_n2fadc_tpc(), decode_nfadc(), decode_times(), io_buffer_get(), and rle_get().

71 {
72  if (buffer->w_bits == 0) {
73  buffer->w = *((buffer->p)++);
74  buffer->w_bits = 32;
75  }
76 
77  int w_bits = buffer->w_bits;
78 
79  unsigned int retval;
80  if (num_bits <= w_bits) {
81  retval = buffer->w >> (32 - num_bits);
82  buffer->w = buffer->w << num_bits;
83  buffer->w_bits = w_bits - num_bits;
84 
85  } else {
86  unsigned int first_part =
87  io_buffer_get(buffer, w_bits) << (num_bits - w_bits);
88  retval = first_part | io_buffer_get(buffer, num_bits - w_bits);
89  }
90 
91  return retval;
92 }
void io_buffer_put ( io_buffer buffer,
unsigned int  code_bits,
int  code_length 
)

Definition at line 36 of file io_buffer.cpp.

References io_buffer::p, io_buffer::w, and io_buffer::w_bits.

Referenced by encode_caen(), encode_cmp_times(), encode_fadc(), encode_hits(), encode_n2fadc_ndet(), encode_n2fadc_tpc(), encode_nfadc(), encode_times(), flush_rle(), huffman_put_symbol(), and rle_put().

37 {
38 
39  assert(code_length > 0 && code_length < 32);
40 
41  // Does this symbol fit in our buffer word?
42  if (code_length < buffer->w_bits) {
43  // OK, so put it there...
44  buffer->w = (buffer->w << code_length) | code_bits;
45  buffer->w_bits -= code_length;
46  } else if (code_length == buffer->w_bits) {
47  // Only barely...write out the buffer word
48  buffer->w = (buffer->w << code_length) | code_bits;
49  *((buffer->p)++) = buffer->w;
50  buffer->w_bits = 32;
51  } else {
52  // No, we'll have to split the code across two words.
53  unsigned int first_code_bits =
54  code_bits >> (code_length - buffer->w_bits);
55  *((buffer->p)++) = (buffer->w << buffer->w_bits) | first_code_bits;
56  buffer->w = code_bits;
57  buffer->w_bits = 32 - code_length + buffer->w_bits;
58  }
59 }
BOOL load_huffman ( char *  key_dir,
huffman_table table 
)

Definition at line 429 of file huffman.cpp.

References huffman_table::code_bits, huffman_table::code_length, FALSE, huffman_table::frequency, hDB, huffman_build_tree(), i, huffman_table::num_symbols, size, sprintf(), and SUCCESS.

Referenced by caen_load(), comp_load(), fadc_load(), hits_load(), stck_load(), and tdc400_load().

430 {
431  char key_name[MAX_ODB_PATH];
432  int size;
433 
434  for(int i = 0; i < table->num_symbols; i++) {
435  table->frequency[i] = 1;
436  }
437 
438  sprintf(key_name, "%s/code_bits", key_dir);
439  size = table->num_symbols * sizeof(DWORD);
440  int stat = db_get_value(hDB, 0, key_name, table->code_bits, &size,
441  TID_DWORD, FALSE);
442  if(stat != DB_SUCCESS || size != table->num_symbols * sizeof(DWORD)) {
443  cm_msg(MERROR, "load_huffman", "Failed to load ODB key %s", key_name);
444  return !SUCCESS;
445  }
446 
447  sprintf(key_name, "%s/code_length", key_dir);
448  size = table->num_symbols * sizeof(DWORD);
449  db_get_value(hDB, 0, key_name, table->code_length, &size, TID_DWORD, FALSE);
450  if(stat != DB_SUCCESS || size != table->num_symbols * sizeof(DWORD)) {
451  cm_msg(MERROR, "load_huffman", "Failed to load ODB key %s", key_name);
452  return !SUCCESS;
453  }
454 
455  huffman_build_tree(table);
456 
457  return SUCCESS;
458 }
int n2fadc_ndet_compress ( unsigned char *  input,
int  input_size,
unsigned char *  output,
int  userParam 
)

Definition at line 97 of file n2fadc_ndet_compress.cpp.

References encode_n2fadc_ndet(), and start_output_buffer().

Referenced by compress_event(), and compress_event_skim().

98 {
99  io_buffer output_buffer;
100 
101  // store size of input data
102  int *uncompressed_size_p = (int *) output;
103  *uncompressed_size_p = input_size;
104  output += sizeof(int);
105 
106  // compress samples
107  start_output_buffer(&output_buffer, output);
108  int compressed_size = encode_n2fadc_ndet(input, input_size, &output_buffer, userParam);
109  output += compressed_size;
110 
111  return compressed_size + sizeof(int);
112 }
int n2fadc_ndet_expand ( unsigned char *  input,
int  input_size,
unsigned char *  output,
int  userParam 
)

Definition at line 166 of file n2fadc_ndet_compress.cpp.

References decode_n2fadc_ndet(), and start_input_buffer().

Referenced by expand_event().

167 {
168  //
169  // Uncompress the data produced by n2fadc_ndet_compress().
170  //
171 
172  // Get uncompressed data size
173  int *uncompressed_size_p = (int *) input;
174  int uncompressed_size = *uncompressed_size_p;
175  input += sizeof(int);
176  input_size -= sizeof(int);
177 
178  // Uncompress data
179  io_buffer input_buffer;
180  start_input_buffer(&input_buffer, input);
181  decode_n2fadc_ndet(&input_buffer, output, uncompressed_size, userParam);
182 
183  return uncompressed_size;
184 }
void n2fadc_ndet_load ( )

Definition at line 24 of file n2fadc_ndet_compress.cpp.

References hDB, should_compress_n2fadc_ndet, size, and TRUE.

Referenced by compress_load_all().

25 {
26  int size = sizeof(BOOL);
27  db_get_value(hDB, 0, "/Compression/Lossless/NFADC/Enabled", &should_compress_n2fadc_ndet,
28  &size, TID_BOOL, TRUE);
29 }
void n2fadc_ndet_optimize ( )

Definition at line 20 of file n2fadc_ndet_compress.cpp.

Referenced by compress_optimize_all().

21 {
22 }
int n2fadc_tpc_compress ( unsigned char *  input,
int  input_size,
unsigned char *  output,
int  userParam 
)

Definition at line 104 of file n2fadc_tpc_compress.cpp.

References encode_n2fadc_tpc(), and start_output_buffer().

Referenced by compress_event(), and compress_event_skim().

105 {
106  io_buffer output_buffer;
107 
108  // store size of input data
109  int *uncompressed_size_p = (int *) output;
110  *uncompressed_size_p = input_size;
111  output += sizeof(int);
112 
113  // compress samples
114  start_output_buffer(&output_buffer, output);
115  int compressed_size = encode_n2fadc_tpc(input, input_size, &output_buffer, userParam);
116  output += compressed_size;
117 
118  return compressed_size + sizeof(int);
119 }
int n2fadc_tpc_expand ( unsigned char *  input,
int  input_size,
unsigned char *  output,
int  userParam 
)

Definition at line 172 of file n2fadc_tpc_compress.cpp.

References decode_n2fadc_tpc(), and start_input_buffer().

Referenced by expand_event().

173 {
174  //
175  // Uncompress the data produced by n2fadc_tpc_compress().
176  //
177 
178  // Get uncompressed data size
179  int *uncompressed_size_p = (int *) input;
180  int uncompressed_size = *uncompressed_size_p;
181  input += sizeof(int);
182  input_size -= sizeof(int);
183 
184  // Uncompress data
185  io_buffer input_buffer;
186  start_input_buffer(&input_buffer, input);
187  decode_n2fadc_tpc(&input_buffer, output, uncompressed_size, userParam);
188 
189  return uncompressed_size;
190 }
void n2fadc_tpc_load ( )

Definition at line 24 of file n2fadc_tpc_compress.cpp.

References hDB, should_compress_n2fadc_tpc, size, and TRUE.

Referenced by compress_load_all().

25 {
26  int size = sizeof(BOOL);
27  db_get_value(hDB, 0, "/Compression/Lossless/NFADC/Enabled", &should_compress_n2fadc_tpc,
28  &size, TID_BOOL, TRUE);
29 }
void n2fadc_tpc_optimize ( )

Definition at line 20 of file n2fadc_tpc_compress.cpp.

Referenced by compress_optimize_all().

21 {
22 }
int nfadc_compress ( unsigned char *  input,
int  input_size,
unsigned char *  output,
int  userParam 
)

Definition at line 108 of file nfadc_compress.cpp.

References encode_nfadc(), and start_output_buffer().

Referenced by compress_event(), and compress_event_skim().

109 {
110  io_buffer output_buffer;
111 
112  // store size of input data
113  int *uncompressed_size_p = (int *) output;
114  *uncompressed_size_p = input_size;
115  output += sizeof(int);
116 
117  // compress samples
118  start_output_buffer(&output_buffer, output);
119  int compressed_size = encode_nfadc(input, input_size, &output_buffer, userParam);
120  output += compressed_size;
121 
122  return compressed_size + sizeof(int);
123 }
int nfadc_expand ( unsigned char *  input,
int  input_size,
unsigned char *  output,
int  userParam 
)

Definition at line 181 of file nfadc_compress.cpp.

References decode_nfadc(), and start_input_buffer().

Referenced by expand_event().

182 {
183  //
184  // Uncompress the data produced by nfadc_compress().
185  //
186 
187  // Get uncompressed data size
188  int *uncompressed_size_p = (int *) input;
189  int uncompressed_size = *uncompressed_size_p;
190  input += sizeof(int);
191  input_size -= sizeof(int);
192 
193  // Uncompress data
194  io_buffer input_buffer;
195  start_input_buffer(&input_buffer, input);
196  decode_nfadc(&input_buffer, output, uncompressed_size, userParam);
197 
198  return uncompressed_size;
199 }
void nfadc_load ( )

Definition at line 24 of file nfadc_compress.cpp.

References hDB, should_compress_nfadc, size, and TRUE.

Referenced by compress_load_all().

25 {
26  int size = sizeof(BOOL);
27  db_get_value(hDB, 0, "/Compression/Lossless/NFADC/Enabled", &should_compress_nfadc,
28  &size, TID_BOOL, TRUE);
29 }
void nfadc_optimize ( )

Definition at line 20 of file nfadc_compress.cpp.

Referenced by compress_optimize_all().

21 {
22 }
int read_midas_event ( FILE *  fp,
char *  pevent 
)

Definition at line 24 of file overall.cpp.

References data_size, le32_to_cpu, and SUCCESS.

Referenced by main().

25 {
26 
27  int event_size, size_read;
28 
29  size_read = fread(pevent, 1, sizeof(EVENT_HEADER), fp);
30  if (size_read != sizeof(EVENT_HEADER))
31  return !SUCCESS;
32 
33  event_size = le32_to_cpu(((EVENT_HEADER *) pevent)->data_size);
34 
35  size_read = fread(pevent + sizeof(EVENT_HEADER), 1, event_size, fp);
36  if (size_read != event_size)
37  return !SUCCESS;
38 
39  bk_swap(pevent, 0);
40 
41  return SUCCESS;
42 }
void rle_bits_setup ( )

Definition at line 41 of file rle.cpp.

References i, num_leading_0, num_leading_1, num_leading_bits(), ones, and rle_bits_setup_done.

Referenced by caen_compress(), caen_expand(), cmp_compress(), cmp_expand(), fadc_compress(), fadc_expand(), hits_compress(), hits_expand(), stck_compress(), stck_expand(), tdc400_compress(), and tdc400_expand().

42 {
44  return;
45  }
46 
47  //
48  // Cache the number of leading 0s and 1s for all 8-bit values.
49  //
50  for (int i = 0; i < 256; i++) {
53  }
54 
55  //
56  // Cache the bit-strings consisting of all 1s, with lengths from 1 to
57  // 8.
58  //
59  short o = 0;
60  for (int i = 0; i < 9; i++) {
61  ones[i] = o;
62  o = (o << 1) | 1;
63  }
64 }
int rle_get ( io_buffer input,
rle_state s 
)

Definition at line 117 of file rle.cpp.

References rle_state::huffman, huffman_get_symbol(), rle_state::huffman_length, io_buffer_get(), ones, rle_state::run_length, and rle_state::running_digit.

Referenced by decode_bits(), and decode_cmp_bits().

118 {
119  //
120  // Reverse the encoding performed by rle_bits.
121  //
122 
123  unsigned char output_word = 0;
124  int output_bits = 8;
125 
126  while (1) {
127  if (s->run_length == 0) {
128  s->running_digit = !s->running_digit;
129  int run_length =
130  huffman_get_symbol(&s->huffman[s->running_digit], input);
131 
132  if (run_length < 0) {
133  return -1;
134  } else if(run_length == s->huffman_length[s->running_digit]) {
135  s->run_length = io_buffer_get(input, 24);
136  } else {
137  s->run_length = run_length;
138  }
139  }
140 
141  while (s->run_length > 0) {
142 
143  if (output_bits > s->run_length) {
144  output_word = output_word << s->run_length;
145  if (s->running_digit == 1) {
146  output_word |= ones[s->run_length];
147  }
148  output_bits -= s->run_length;
149  s->run_length = 0;
150  } else {
151  output_word = output_word << output_bits;
152  if (s->running_digit == 1) {
153  output_word |= ones[output_bits];
154  }
155  s->run_length -= output_bits;
156 
157  return output_word;
158  }
159  }
160 
161  }
162 }
void rle_put ( unsigned char  b,
rle_state s,
io_buffer output 
)

Definition at line 66 of file rle.cpp.

References rle_state::huffman, rle_state::huffman_length, huffman_put_symbol(), io_buffer_put(), num_leading_0, num_leading_1, rle_state::run_length, rle_state::running_digit, and rle_state::use_extension_word.

Referenced by encode_bits(), and encode_cmp_bits().

67 {
68  int bits = 8;
69 
70  while (bits > 0) {
71  int nr;
72 
73  if (s->running_digit == 0) {
74  nr = num_leading_0[b];
75  } else {
76  nr = num_leading_1[b];
77  }
78 
79  if (nr > bits) {
80  nr = bits;
81  }
82 
83  s->run_length += nr;
84  bits -= nr;
85 
86  if (bits != 0 || (!s->use_extension_word &&
87  s->run_length >= s->huffman_length[s->running_digit]-8)) {
88  b = b << nr;
89 
90  if(s->run_length < s->huffman_length[s->running_digit]) {
92  } else {
94  io_buffer_put(output,s->run_length, 24);
95 
96  }
98  s->run_length = 0;
99  }
100  }
101 }
BOOL save_huffman ( char *  key_dir,
huffman_table table 
)

Definition at line 321 of file huffman.cpp.

References huffman_table::code_bits, huffman_table::code_length, huffman_table::frequency, hDB, huffman_table::num_symbols, size, sprintf(), and SUCCESS.

Referenced by caen_optimize(), comp_optimize(), fadc_optimize(), hits_optimize(), stck_optimize(), and tdc400_optimize().

322 {
323  char key_name[MAX_ODB_PATH];
324  int size;
325 
326  sprintf(key_name, "%s/code_bits", key_dir);
327  size = table->num_symbols * sizeof(DWORD);
328  int stat = db_set_value(hDB, 0, key_name, table->code_bits, size,
329  table->num_symbols, TID_DWORD);
330  if(stat != DB_SUCCESS) {
331  cm_msg(MERROR, "save_huffman", "Failed to save ODB key %s", key_name);
332  return !SUCCESS;
333  }
334 
335  sprintf(key_name, "%s/code_length", key_dir);
336  stat = db_set_value(hDB, 0, key_name, table->code_length, size,
337  table->num_symbols, TID_DWORD);
338  if(stat != DB_SUCCESS) {
339  cm_msg(MERROR, "save_huffman", "Failed to save ODB key %s", key_name);
340  return !SUCCESS;
341  }
342 
343  sprintf(key_name, "%s/frequency", key_dir);
344  stat = db_set_value(hDB, 0, key_name, table->frequency, size,
345  table->num_symbols, TID_DWORD);
346  if(stat != DB_SUCCESS) {
347  cm_msg(MERROR, "save_huffman", "Failed to save ODB key %s", key_name);
348  return !SUCCESS;
349  }
350 
351  return SUCCESS;
352 }
void start_input_buffer ( io_buffer buffer,
unsigned char *  p 
)

Definition at line 61 of file io_buffer.cpp.

References io_buffer::num_codes, io_buffer::p, io_buffer::start, io_buffer::w, and io_buffer::w_bits.

Referenced by caen_expand(), cmp_expand(), fadc_expand(), hits_expand(), n2fadc_ndet_expand(), n2fadc_tpc_expand(), nfadc_expand(), stck_expand(), and tdc400_expand().

62 {
63  buffer->w = 0;
64  buffer->w_bits = 0;
65  buffer->start = (unsigned int *) p;
66  buffer->p = buffer->start + 1;
67  buffer->num_codes = *(buffer->start);
68 }
void start_output_buffer ( io_buffer buffer,
unsigned char *  p 
)

Definition at line 14 of file io_buffer.cpp.

References io_buffer::num_codes, io_buffer::p, io_buffer::start, io_buffer::w, and io_buffer::w_bits.

Referenced by caen_compress(), cmp_compress(), fadc_compress(), hits_compress(), n2fadc_ndet_compress(), n2fadc_tpc_compress(), nfadc_compress(), stck_compress(), and tdc400_compress().

15 {
16  buffer->w = 0;
17  buffer->w_bits = 32;
18  buffer->start = (unsigned int *) p;
19  buffer->p = buffer->start + 1;
20  buffer->num_codes = 0;
21 }
int stck_compress ( stack_elt input,
int  input_size,
unsigned char *  output,
int  userParam 
)

Definition at line 220 of file stck_compress.cpp.

References encode_bits(), encode_times(), rle_bits_setup(), and start_output_buffer().

Referenced by compress_event_skim().

222 {
223  //
224  // Carry out lossless compression on the stacked TPC data.
225  //
226 
227  input_size /= sizeof(stack_elt);
228 
229  //myprint_TPCstack_interval(input, 0, input_size-1);
230 
231  rle_bits_setup();
232 
233  io_buffer output_buffer;
234 
235  // store size of input data
236  int *uncompressed_size_p = (int *) output;
237  *uncompressed_size_p = input_size;
238  output += sizeof(int);
239 
240  // reserve space for the size of the compressed times
241  int *compressed_time_size_p = (int *) output;
242  output += sizeof(int);
243 
244  // compress times
245  start_output_buffer(&output_buffer, output);
246  int compressed_time_size =
247  encode_times(input, input_size, &output_buffer);
248  output += compressed_time_size;
249 
250  // store size of compressed times
251  *compressed_time_size_p = compressed_time_size;
252 
253  // reserve space for the size of the compressed bit patterns
254  int *compressed_bits_size_p = (int *) output;
255  output += sizeof(int);
256 
257  // compress bit patterns
258  start_output_buffer(&output_buffer, output);
259  int compressed_bits_size =
260  encode_bits(input, input_size, &output_buffer);
261  output += compressed_time_size;
262 
263  // store size of compressed bit patterns
264  *compressed_bits_size_p = compressed_bits_size;
265 
266  return compressed_time_size + compressed_bits_size + 3 * sizeof(int);
267 }
int stck_expand ( unsigned char *  input,
int  input_size,
stack_elt output,
int  userParam 
)

Definition at line 269 of file stck_compress.cpp.

References decode_bits(), decode_times(), rle_bits_setup(), and start_input_buffer().

Referenced by expand_event().

271 {
272  //
273  //
274  // Uncompress the data produced by stck_compress().
275  //
276 
277  rle_bits_setup();
278 
279  // Get uncompressed data size
280  int *uncompressed_size_p = (int *) input;
281  int uncompressed_size = *uncompressed_size_p;
282  input += sizeof(int);
283  input_size -= sizeof(int);
284 
285  // Get size of compressed time data
286  int *compressed_time_size_p = (int *) input;
287  int compressed_time_size = *compressed_time_size_p;
288  input += sizeof(int);
289  input_size -= sizeof(int);
290 
291  // Uncompress times
292  io_buffer input_buffer;
293  start_input_buffer(&input_buffer, input);
294  decode_times(&input_buffer, output);
295  input += compressed_time_size;
296  input_size -= compressed_time_size;
297 
298  // Get size of compressed bit patterns
299  int *compressed_bits_size_p = (int *) input;
300  int compressed_bits_size = *compressed_bits_size_p;
301  input += sizeof(int);
302  input_size -= sizeof(int);
303 
304  // Uncompress bit patterns
305  start_input_buffer(&input_buffer, input);
306  decode_bits(&input_buffer, output, uncompressed_size);
307 
308  //myprint_TPCstack_interval(output, 0, uncompressed_size-1);
309 
310  return uncompressed_size * sizeof(stack_elt);
311 }
void stck_load ( )

Definition at line 32 of file stck_compress.cpp.

References hDB, huffman_init_default(), load_huffman(), should_compress_stck, size, and TRUE.

Referenced by compress_load_all().

33 {
37 
38  load_huffman("/Compression/Lossless/STCK/Huffman/Bits 0", &stck_bits_huffman[0]);
39  load_huffman("/Compression/Lossless/STCK/Huffman/Bits 1", &stck_bits_huffman[1]);
40 
41  int size = sizeof(BOOL);
42  db_get_value(hDB, 0, "/Compression/Lossless/STCK/Enabled", &should_compress_stck,
43  &size, TID_BOOL, TRUE);
44 }
void stck_optimize ( )

Definition at line 22 of file stck_compress.cpp.

References huffman_optimize_tree(), and save_huffman().

Referenced by compress_optimize_skim().

23 {
27 
28  save_huffman("/Compression/Lossless/STCK/Huffman/Bits 0", &stck_bits_huffman[0]);
29  save_huffman("/Compression/Lossless/STCK/Huffman/Bits 1", &stck_bits_huffman[1]);
30 }
int tdc400_compress ( tdc400_word input,
int  input_size,
unsigned char *  output,
int  userParam 
)

Definition at line 207 of file tdc400_compress.cpp.

References encode_bits(), encode_times(), rle_bits_setup(), and start_output_buffer().

Referenced by compress_event().

209 {
210  //
211  // Carry out lossless compression on the TPC data recorded by a
212  // single TDC400.
213  //
214 
215  input_size /= sizeof(tdc400_word);
216 
217  rle_bits_setup();
218 
219  io_buffer output_buffer;
220 
221  // store size of input data
222  int *uncompressed_size_p = (int *) output;
223  *uncompressed_size_p = input_size;
224  output += sizeof(int);
225 
226  // reserve space for the size of the compressed times
227  int *compressed_time_size_p = (int *) output;
228  output += sizeof(int);
229 
230  // compress times
231  start_output_buffer(&output_buffer, output);
232  int compressed_time_size =
233  encode_times(input, input_size, &output_buffer);
234  output += compressed_time_size;
235 
236  // store size of compressed times
237  *compressed_time_size_p = compressed_time_size;
238 
239  // reserve space for the size of the compressed bit patterns
240  int *compressed_bits_size_p = (int *) output;
241  output += sizeof(int);
242 
243  // compress bit patterns
244  start_output_buffer(&output_buffer, output);
245  int compressed_bits_size =
246  encode_bits(input, input_size, &output_buffer);
247  output += compressed_time_size;
248 
249  // store size of compressed bit patterns
250  *compressed_bits_size_p = compressed_bits_size;
251 
252  return compressed_time_size + compressed_bits_size + 3 * sizeof(int);
253 }
int tdc400_expand ( unsigned char *  input,
int  input_size,
tdc400_word output,
int  userParam 
)

Definition at line 255 of file tdc400_compress.cpp.

References decode_bits(), decode_times(), rle_bits_setup(), and start_input_buffer().

Referenced by expand_event().

257 {
258  //
259  // Uncompress the data produced by tdc400_compress().
260  //
261 
262  rle_bits_setup();
263 
264  // Get uncompressed data size
265  int *uncompressed_size_p = (int *) input;
266  int uncompressed_size = *uncompressed_size_p;
267  input += sizeof(int);
268  input_size -= sizeof(int);
269 
270  // Get size of compressed time data
271  int *compressed_time_size_p = (int *) input;
272  int compressed_time_size = *compressed_time_size_p;
273  input += sizeof(int);
274  input_size -= sizeof(int);
275 
276  // Uncompress times
277  io_buffer input_buffer;
278  start_input_buffer(&input_buffer, input);
279  decode_times(&input_buffer, output);
280  input += compressed_time_size;
281  input_size -= compressed_time_size;
282 
283  // Get size of compressed bit patterns
284  int *compressed_bits_size_p = (int *) input;
285  int compressed_bits_size = *compressed_bits_size_p;
286  input += sizeof(int);
287  input_size -= sizeof(int);
288 
289  // Uncompress bit patterns
290  start_input_buffer(&input_buffer, input);
291  decode_bits(&input_buffer, output, uncompressed_size);
292 
293  return uncompressed_size * sizeof(tdc400_word);
294 }
void tdc400_load ( )

Definition at line 30 of file tdc400_compress.cpp.

References hDB, huffman_init_default(), load_huffman(), should_compress_tdc400, size, and TRUE.

Referenced by compress_load_all().

31 {
35 
36  load_huffman("/Compression/Lossless/TDC400/Huffman/Time", &time_huffman);
37  load_huffman("/Compression/Lossless/TDC400/Huffman/Bits 0", &bits_huffman[0]);
38  load_huffman("/Compression/Lossless/TDC400/Huffman/Bits 1", &bits_huffman[1]);
39 
40  int size = sizeof(BOOL);
41  db_get_value(hDB, 0, "/Compression/Lossless/TDC400/Enabled", &should_compress_tdc400,
42  &size, TID_BOOL, TRUE);
43 }
void tdc400_optimize ( )

Definition at line 19 of file tdc400_compress.cpp.

References huffman_optimize_tree(), and save_huffman().

Referenced by compress_optimize_all().

20 {
24 
25  save_huffman("/Compression/Lossless/TDC400/Huffman/Time", &time_huffman);
26  save_huffman("/Compression/Lossless/TDC400/Huffman/Bits 0", &bits_huffman[0]);
27  save_huffman("/Compression/Lossless/TDC400/Huffman/Bits 1", &bits_huffman[1]);
28 }

Variable Documentation

huffman_table bits_huffman[2]

Definition at line 15 of file tdc400_compress.cpp.

huffman_table caen_channel_huffman

Definition at line 14 of file caen_compress.cpp.

huffman_table cmp_bits_huffman[2]

Definition at line 14 of file comp_compress.cpp.

huffman_table cmp_channel_huffman

Definition at line 15 of file comp_compress.cpp.

huffman_table fadc_run_huffman

Definition at line 14 of file fadc_compress.cpp.

huffman_table fadc_small_huffman

Definition at line 15 of file fadc_compress.cpp.

HNDLE hDB

Definition at line 11 of file mucap_compress.cpp.

BOOL should_compress_caen

Definition at line 15 of file caen_compress.cpp.

Referenced by caen_load(), and compress_event().

BOOL should_compress_comp

Definition at line 16 of file comp_compress.cpp.

Referenced by comp_load(), and compress_event().

BOOL should_compress_fadc

Definition at line 16 of file fadc_compress.cpp.

Referenced by compress_event(), and fadc_load().

BOOL should_compress_n2fadc_ndet

Definition at line 14 of file n2fadc_ndet_compress.cpp.

Referenced by compress_event(), compress_event_skim(), and n2fadc_ndet_load().

BOOL should_compress_n2fadc_tpc

Definition at line 14 of file n2fadc_tpc_compress.cpp.

Referenced by compress_event(), compress_event_skim(), and n2fadc_tpc_load().

BOOL should_compress_nfadc

Definition at line 14 of file nfadc_compress.cpp.

Referenced by compress_event(), compress_event_skim(), and nfadc_load().

BOOL should_compress_tdc400

Definition at line 17 of file tdc400_compress.cpp.

Referenced by compress_event(), and tdc400_load().

huffman_table time_huffman

Definition at line 16 of file tdc400_compress.cpp.