source: fact/tools/pyscripts/sandbox/dneise/fact_compress/c++/factcompress.cpp@ 14269

Last change on this file since 14269 was 14268, checked in by neise, 12 years ago
evolving
File size: 8.8 KB
Line 
1#include <iostream>
2#include <fstream>
3#include <vector>
4#include <string.h>
5
6// I will use the fits class by TB for reading the interesting data from the files
7
8#include "izstream.h"
9
10#define HAVE_ZLIB
11#include "fits.h"
12
13using namespace std;
14
15ifstream::pos_type size;
16char * memblock;
17
18int main (int argc, char * argv[]) {
19
20
21 if (argc < 3)
22 {
23 cout << "Usage: " << argv[0] << " data-file-name calib-file-name [ouput-file-name]" << endl;
24 cout << "" << endl;
25 }
26
27 char * data_file_name = argv[1];
28 char * calib_file_name = argv[2];
29 char * out_file_name = 0;
30 if (argc == 4)
31 {
32 out_file_name = argv[3];
33 }
34 else
35 {
36 out_file_name = new char[strlen(data_file_name)+4];
37 strcpy(out_file_name, data_file_name);
38 strcpy(out_file_name+strlen(data_file_name),".fc");
39 out_file_name[strlen(data_file_name)+3] = 0;
40 }
41
42
43
44
45 //=========================================================================
46 // CALIBRATION CONSTANTS
47 //=========================================================================
48 fits * calib =new fits(calib_file_name);
49 calib->PrintKeys();
50 calib->PrintColumns();
51
52 int size_of_offset = 1440*1024;
53 float * offset_mV = new float[size_of_offset];
54 short * offset = new short[size_of_offset];
55 calib->SetPtrAddress("BaselineMean",offset_mV, size_of_offset);
56
57 calib->GetNextRow();
58 for (int i =0 ; i<size_of_offset; i++)
59 {
60 offset[i] = short(offset_mV[i] / 2000. * 4096 - 0.5);
61 }
62 cout << endl;
63 delete[] offset_mV;
64 delete calib;
65 calib = NULL;
66 //=========================================================================
67 // END OF CALIBRATION CONSTANTS
68 //=========================================================================
69
70
71 ifstream datafile (data_file_name, ios::in|ios::binary);
72 ofstream out (out_file_name, ios::out|ios::binary|ios::trunc);
73 if (datafile.is_open() && out.is_open())
74 {
75 // create our own header
76 //out.write( "FACT_COMPRESS ", 80);
77 //out.write( "END. ", 80);
78
79 // copy first 0x2d00 bytes to new file
80 const int ascii_header_size = 0x2d00;
81 char * memblock = new char [ascii_header_size];
82 datafile.read(memblock, ascii_header_size);
83 out.write(memblock, ascii_header_size);
84 delete[] memblock;
85
86 for ( int event_id = 0 ; event_id < 1000; event_id++)
87 //while ( datafile.good() )
88 {
89 // copy binary header to new file
90 const int bin_header_size = 3390;
91 char * memblock = new char [bin_header_size];
92 datafile.read(memblock, bin_header_size);
93 out.write(memblock, bin_header_size);
94 delete[] memblock;
95
96 cout << "aaaaaaaaaaaaaaaa" << hex << datafile.tellg() << endl;
97
98 // read 300 shorts out of the file
99 for (int chid = 0 ; chid < 1440; chid++)
100 {
101 const int data_size = 300;
102 const int diff_size = data_size;
103 char * memblock = new char [data_size*sizeof(short)];
104 short * data = (short *)memblock;
105 short * diffs = new short [diff_size];
106 unsigned char * sizes = new unsigned char [diff_size];
107
108 datafile.read(memblock, data_size*sizeof(short) );
109
110 for ( int i = 0; i<diff_size; i++)
111 {
112 diffs[i] = data[i]-data[i];
113 //~ if (diffs[i] >= -32 && diffs[i] <= 31)
114 //~ sizes[i] = 6;
115 //~ else if (diffs[i] >= -128 && diffs[i] <= 127)
116 //~ sizes[i] = 8;
117 //~ else if (diffs[i] >= -512 && diffs[i] <= 511)
118 //~ sizes[i] = 10;
119 //~ else
120 //~ sizes[i] = 16;
121 if (diffs[i] >= -128 && diffs[i] <= 127)
122 sizes[i] = 8;
123 else
124 sizes[i] = 16;
125 }
126
127 // calculate group sizes
128 int counter = 0;
129 unsigned char last_size = sizes[0];
130 vector<int> group_sizes;
131 vector<unsigned char> groups;
132
133 for (int i=0 ; i < diff_size; i++)
134 {
135
136 if (sizes[i] != last_size)
137 {
138 group_sizes.push_back( counter );
139 groups.push_back( last_size );
140 last_size = sizes[i];
141 }
142 counter++;
143 }
144 groups.push_back( last_size );
145 group_sizes.push_back( counter );
146
147
148 // measure how large the stuff, is
149 int cs =0;
150 // for all groups write header and group
151 short header = 0;
152 int diff_index = 0;
153 for ( int i = 0 ; i < (int)groups.size() ; i++)
154 {
155 // write header
156 if (groups[i] == 8)
157 {
158 header = short(group_sizes[i]);
159 }
160 else
161 {
162 header = 0x8000 | short(group_sizes[i] );
163 }
164 cs += sizeof(short);
165
166 // write group
167 if (groups[i] == 8)
168 {
169 for (int j = 0; j<group_sizes[i]; j++)
170 {
171 cs += 1;
172 }
173 }
174 else
175 {
176 for (int j = 0; j<group_sizes[i]; j++)
177 {
178 cs += 2;
179 }
180 }
181 }
182 //cout << "compressed size:" << cs << endl;
183 out.write( (char*)&cs, sizeof(cs));
184
185 cout << "sizeof(cs):" << sizeof(cs) << endl;
186
187 // write the first short
188 out.write(memblock, 1*sizeof(short) );
189 printf("first short: 0x%X \t %d \n", ( (short*)memblock )[0], ( (short*)memblock )[0] );
190 // for all groups write header and group
191 header = 0;
192 diff_index = 0;
193 for ( int i = 0 ; i < (int)groups.size() ; i++)
194 {
195 // write header
196 if (groups[i] == 8)
197 {
198 header = short(group_sizes[i]);
199 }
200 else
201 {
202 header = 0x8000 | short(group_sizes[i] );
203 }
204 out.write( (char*)&header, sizeof(short));
205
206 // write group
207 if (groups[i] == 8)
208 {
209 for (int j = 0; j<group_sizes[i]; j++)
210 {
211 out.write( (char*)&(diffs[diff_index++]), 1);
212 }
213 }
214 else
215 {
216 for (int j = 0; j<group_sizes[i]; j++)
217 {
218 out.write( (char*)&(diffs[diff_index++]), 2);
219 }
220 }
221 }
222
223 //out.write(memblock, data_size*sizeof(short) );
224
225 delete[] memblock;
226 delete[] diffs;
227 delete[] sizes;
228 }
229 }
230 cout << "finished with 1000 events." << endl;
231 long after_address = datafile.tellg();
232 datafile.seekg (0, ios::end);
233 long end = datafile.tellg();
234 datafile.seekg (after_address, ios::beg);
235
236 cout << "between last event and end:" << end - after_address << endl;
237 // read the last piece...
238 const int rest_size = end-after_address;
239 char * memblock2 = new char [rest_size];
240 datafile.read(memblock2, rest_size);
241 cout << "first char in memblock: " << int(memblock2[0]) << endl;
242 char lastchar = memblock2[0];
243 for (int i =0 ; i<rest_size; i++)
244 {
245 if (memblock2[i] != lastchar)
246 {
247 cout << "new char at: " << i << " with value:" << int(memblock[0]) << endl;
248 lastchar = memblock2[i];
249 }
250 }
251
252 out.write(memblock2, rest_size);
253 delete[] memblock2;
254
255
256 datafile.close();
257 out.close();
258 } //end of if file open
259} //end of main
Note: See TracBrowser for help on using the repository browser.