AlcapDAQ  1
Functions | Variables
n2fadc_tpc_compress.cpp File Reference
#include <stdio.h>
#include "midas.h"
#include "mucap_compress.h"
#include "mucap_structures.h"

Go to the source code of this file.

Functions

void n2fadc_tpc_dump (int num_fadc_words, unsigned char *data)
 
void n2fadc_tpc_optimize ()
 
void n2fadc_tpc_load ()
 
int encode_n2fadc_tpc (unsigned char *input, int input_size, io_buffer *output, int boardNumber)
 
int n2fadc_tpc_compress (unsigned char *input, int input_size, unsigned char *output, int userParam)
 
int decode_n2fadc_tpc (io_buffer *input, unsigned char *output, int input_size, int boardNumber)
 
int n2fadc_tpc_expand (unsigned char *input, int input_size, unsigned char *output, int userParam)
 

Variables

BOOL should_compress_n2fadc_tpc = TRUE
 
const int num_n2fadc_tpc_channels = 4
 

Function Documentation

int decode_n2fadc_tpc ( io_buffer input,
unsigned char *  output,
int  input_size,
int  boardNumber 
)

Definition at line 121 of file n2fadc_tpc_compress.cpp.

References io_buffer_get().

Referenced by n2fadc_tpc_expand().

122 {
123  int num_fadc_words = input_size/9;
124 
125  // first retrieve the timestamps
126  int j = 0;
127  while(j < num_fadc_words) {
128 
129  int timestamp = io_buffer_get(input, 20);
130  int run_length = io_buffer_get(input, 8);
131 
132  for(int k = 0; k < run_length; k++) {
133  int t2 = timestamp + k;
134  output[(j+k)*9+0] = (t2 >> 12) & 0xff;
135  output[(j+k)*9+1] = (t2 >> 4) & 0xff;
136  output[(j+k)*9+2] = (t2 & 0xf) << 4;
137  }
138  j += run_length;
139 
140  }
141 
142  // now the FADC codes
143  int lastSample = 0;
144  for(int j = 0; j < num_fadc_words; j++) {
145  int sample[4];
146  for(int k = 0; k < 4; k++) {
147  int encodedDiff = io_buffer_get(input, 4) & 0xf;
148  if(encodedDiff == 0) {
149  sample[k] = io_buffer_get(input, 13) & 0x1fff;
150  } else {
151  sample[k] = lastSample + encodedDiff - 8;
152  }
153  lastSample = sample[k];
154  }
155 
156  int overflows = ((sample[0] & 0x1000) ? 0x01 : 0) |
157  ((sample[1] & 0x1000) ? 0x02 : 0) |
158  ((sample[2] & 0x1000) ? 0x04 : 0) |
159  ((sample[3] & 0x1000) ? 0x08 : 0);
160  output[j*9+2] |= overflows;
161  output[j*9+3] = (sample[3] >> 4) & 0xff;
162  output[j*9+4] = ((sample[3] & 0xf) << 4) | ((sample[2] >> 8) & 0xf);
163  output[j*9+5] = (sample[2] & 0xff);
164  output[j*9+6] = (sample[1] >> 4) & 0xff;
165  output[j*9+7] = ((sample[1] & 0xf) << 4) | ((sample[0] >> 8) & 0xf);
166  output[j*9+8] = (sample[0] & 0xff);
167  }
168 
169  return input_size;
170 }
int encode_n2fadc_tpc ( unsigned char *  input,
int  input_size,
io_buffer output,
int  boardNumber 
)

Definition at line 31 of file n2fadc_tpc_compress.cpp.

References flush_output_buffer(), and io_buffer_put().

Referenced by n2fadc_tpc_compress().

32 {
33  int num_words = input_size/9;
34 
35  // first deal with the timestamps
36  int run_length = 0;
37  int last_timestamp = -2;
38  int island_timestamp = -1;
39  int last_island_timestamp = 0;
40  for(int j = 0; j < num_words; j++) {
41  int timestamp = (input[j*9+0] << 12) |
42  (input[j*9+1] << 4) |
43  (input[j*9+2] >> 4);
44 
45  if(timestamp != last_timestamp + 1 || run_length == 255) {
46  if(island_timestamp != -1) {
47  io_buffer_put(output, island_timestamp, 20);
48  io_buffer_put(output, run_length, 8);
49  }
50 
51  island_timestamp = timestamp;
52  run_length = 0;
53  }
54 
55  last_timestamp = timestamp;
56  run_length++;
57  }
58 
59  // final run
60  if(island_timestamp != -1) {
61  io_buffer_put(output, island_timestamp, 20);
62  io_buffer_put(output, run_length, 8);
63  }
64 
65  // now the FADC codes
66  int lastSample = 0;
67  for(int j = 0; j < num_words; j++) {
68  int sample[4];
69 
70  bool overflowB0 = ((input[j*9+2] & 0x08) != 0);
71  bool overflowA0 = ((input[j*9+2] & 0x04) != 0);
72  bool overflowB1 = ((input[j*9+2] & 0x02) != 0);
73  bool overflowA1 = ((input[j*9+2] & 0x01) != 0);
74  sample[3] = (overflowB0 << 12) |
75  (input[j*9+3] << 4) |
76  (input[j*9+4] >> 4);
77  sample[2] = (overflowA0 << 12) |
78  ((input[j*9+4] & 0xf) << 8) |
79  (input[j*9+5]);
80  sample[1] = (overflowB1 << 12) |
81  (input[j*9+6] << 4) |
82  (input[j*9+7] >> 4);
83  sample[0] = (overflowA1 << 12) |
84  ((input[j*9+7] & 0xf) << 8) |
85  (input[j*9+8]);
86 
87  for(int k = 0; k < 4; k++) {
88  int diff = sample[k] - lastSample;
89  lastSample = sample[k];
90 
91 
92  if(diff <= 7 && diff >= -7) {
93  io_buffer_put(output, diff+8, 4);
94  } else {
95  io_buffer_put(output, 0, 4);
96  io_buffer_put(output, sample[k], 13);
97  }
98  }
99  }
100 
101  return flush_output_buffer(output);
102 }
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 }
void n2fadc_tpc_dump ( int  num_fadc_words,
unsigned char *  data 
)

Definition at line 192 of file n2fadc_tpc_compress.cpp.

References i, and printf().

193 {
194  for(int i = 0; i < num_fadc_words; i++) {
195  printf("%d: ", i);
196  for(int j = 0; j < 9; j++) {
197  printf("%02x ", data[i*9+j]);
198  }
199  printf("\n");
200  }
201 }
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 }

Variable Documentation

const int num_n2fadc_tpc_channels = 4

Definition at line 16 of file n2fadc_tpc_compress.cpp.

BOOL should_compress_n2fadc_tpc = TRUE

Definition at line 14 of file n2fadc_tpc_compress.cpp.

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