CFEL - ASG Software Suite  2.5.0
CASS
hlltypes.hpp
Go to the documentation of this file.
1 // Copyright (C) 2011 Lutz Foucar
2 
3 /**
4  * @file hlltypes.hpp contains the layout of the hll data types and conversion to
5  * cass data types
6  *
7  * @author Lutz Foucar
8  */
9 
10 #ifndef _HLLTYPES_HPP_
11 #define _HLLTYPES_HPP_
12 
13 #include <stdint.h>
14 #include <vector>
15 #include <fstream>
16 #include <stdexcept>
17 #include <algorithm>
18 #include <iterator>
19 
20 #include <QtCore/QDataStream>
21 
22 namespace cass
23 {
24 namespace hllDataTypes
25 {
26 /** define a pixel */
27 typedef int16_t pixel;
28 
29 /** define a frame */
30 typedef std::vector<pixel> frame_t;
31 
32 #pragma pack(4)
33 /** the file header
34  *
35  * took from fformat.h from pnCCD lib. Size should be 1024 bytes
36  *
37  * @author Peter Holl
38  * @author Nils Kimmel
39  */
41  {
42  /** number of bytes in file header */
43  uint16_t myLength;
44 
45  /** number of bytes in frame header */
46  uint16_t fhLength;
47 
48  /** number of CCD to be read out */
49  uint8_t nCCDs;
50 
51  /** number of channels */
52  uint8_t width;
53 
54  /** (maximum) number of lines */
55  uint8_t maxHeight;
56 
57  /** format version */
58  uint8_t version;
59 
60  /** identification of data */
61  char dataSetID[80];
62 
63 // following lines added for Version 6
64 
65  /** the true number of channels */
66  uint16_t the_width;
67 
68  /** the true (maximum) number of lines */
69  uint16_t the_maxHeight;
70 
71  /** fill up space to get the complete size of 1024 bytes*/
72  char fill[932];
73  } ;
74 
75 /** the header that describe the frames
76  *
77  * took from fformat.h from pnCCD lib. Size should be 64 bytes
78  *
79  * @author Peter Holl
80  * @author Nils Kimmel
81  */
83 {
84  /** starting line, indicates window mode if not */
85  uint8_t start;
86 
87  /** info byte */
88  uint8_t info;
89 
90  /** CCD id */
91  uint8_t id;
92 
93  /** number of lines in following frame */
94  uint8_t height;
95 
96  /** start data taking time in seconds */ // this must not be time_t !!
97  uint32_t tv_sec;
98 
99  /** start data taking time in microseconds */ // PxH from long to int, 17-Aug-07
100  uint32_t tv_usec;
101 
102  /** index number */ // PxH from long to int, 17-Aug-07
103  uint32_t index;
104 
105  /** temperature voltage */
106  double temp;
107 
108  // following lines added for Version 6
109 
110  /** the true starting line, indicates window mode if > 0 */
111  uint16_t the_start;
112 
113  /** the true number of lines in following frame */
114  uint16_t the_height;
115 
116  /** Frame ID from an external trigger, e.g. the bunch ID at DESY/FLASH */
117  uint32_t external_id;
118 
119  /** LCLS bunch ID */
120  uint64_t bunch_id;
121 
122  /** fill up space to get 64 bytes */
123  char fill[24];
124 };
125 
126 /** the file header structure of the hll darkcal file format
127  *
128  * derived from the code within the pnCCD lib
129  *
130  * @author Lutz Foucar
131  */
133 {
134  /** string to identify that it is a hll darkcal file
135  *
136  * should contain "HE pixel statistics map"
137  */
138  char identifystring[24];
139 
140  /** the width of the frames */
141  uint32_t columns;
142 
143  /** the height of the frames */
144  uint32_t rows;
145 
146  /** the overal length of the frame, if the matrix is linearized */
147  uint32_t length;
148 
149  /** empty to fill the header up to 1024 bytes */
150  char fillspace[988];
151 };
152 
153 /** struct describing the statistics saved in a HLL Darkcal file
154  *
155  * copied from the pnCCD lib
156  *
157  * @author Peter Holl
158  * @author Nils Kimmel
159  */
161 {
162  /** internal use */
163  double sum;
164 
165  /** offset mean value of pixel (raw) */
166  double offset;
167 
168  /** noise sigma value of pixel */
169  double sigma;
170 
171  /** internal use */
172  double sumSq;
173 
174  /** internal use */
175  int count;
176 
177  /** offset mean value of pixel (common mode corrected) */
178  int16_t mean;
179 };
180 #pragma pack()
181 
182 /** convert a linearised matrix in the CASS format to the hll format
183  *
184  * the difference between the CASS and HLL is, that the CASS format has all 4
185  * quadrants in a quadrat wheras in HLL they are aligned in a rectangle.
186  *
187  @verbatim
188 
189  -----------
190  | 1 | 2 |
191  | C | D |
192  -----O-----
193  | 0 | 3 |
194  | A | B |
195  -----------
196  --------->
197 
198  |
199  v
200 
201  ----()--------()-----
202  | 0 | 1 | 2 | 3 |
203  | A | C | D | B |
204  ---------------------
205  --------->
206 
207  @endverbatim
208  * The numbers indicate the tile of the frame wihtin the HLL frame format, the
209  * letters indicate the tiles within the CASS format. The arrows indicate the
210  * fast increasing coordinate within the linearised array. The parenthesis and
211  * the 0 indicate where the hole btw the quater holes are in the new tiles.
212  * This implies that the top two tiles in the CASS format have to be rotated by
213  * 180 degrees before adding them to HLL format array. This basically mean that
214  * one has to read these tiles reverseley. This is done in this function by
215  * using reverse iterators that will point at the last element of the tile.
216  *
217  * This function will then read the first row of tile A then the last of tile C
218  * then the last row of tile C, then the first row Tile B to the first row of
219  * the HLL format.
220  * The second row of the HLL format is then build by the 2nd row of Tile A, the
221  * 2nd to last row of Tile C then the 2nd to last row of tile D and the 2nd row
222  * of tile B. This continues until one has read all the rows of the tiles.
223  *
224  * @tparam inputContainerType the type of the container containing the input
225  * @tparam outputContainerType the type of the container containing the output
226  * @param CASSMatrix the container containing the linearised input matrix
227  * @param HLLMatrix the container containing the linearised out matrix
228  * @param quadrantColumns the number of columns in one quadrant
229  * @param quadrantRows the number of rows in one quadrant
230  * @param CASSColumns the number of columns in the CASS input container
231  *
232  * @author Lutz Foucar
233  */
234 template <typename inputContainerType, typename outputContainerType>
235 void CASS2HLL(const inputContainerType& CASSMatrix,
236  outputContainerType& HLLMatrix,
237  size_t quadrantColumns,
238  size_t quadrantRows,
239  size_t CASSColumns)
240 {
241  typename inputContainerType::const_iterator cassquadrant0(CASSMatrix.begin());
242  typename inputContainerType::const_iterator cassquadrant1(CASSMatrix.begin()+quadrantColumns);
243  typename inputContainerType::const_reverse_iterator cassquadrant2(CASSMatrix.rbegin()+quadrantColumns);
244  typename inputContainerType::const_reverse_iterator cassquadrant3(CASSMatrix.rbegin());
245 
246  typename outputContainerType::iterator HLL(HLLMatrix.begin());
247 
248  for (size_t quadrantRow(0); quadrantRow < quadrantRows; ++quadrantRow)
249  {
250  copy(cassquadrant0,cassquadrant0+quadrantColumns,HLL);
251  advance(HLL,quadrantColumns);
252  copy(cassquadrant2,cassquadrant2+quadrantColumns,HLL);
253  advance(HLL,quadrantColumns);
254  copy(cassquadrant3,cassquadrant3+quadrantColumns,HLL);
255  advance(HLL,quadrantColumns);
256  copy(cassquadrant1,cassquadrant1+quadrantColumns,HLL);
257  advance(HLL,quadrantColumns);
258 
259  advance(cassquadrant0,CASSColumns);
260  advance(cassquadrant1,CASSColumns);
261  advance(cassquadrant2,CASSColumns);
262  advance(cassquadrant3,CASSColumns);
263  }
264 }
265 
266 /** convert a linearised matrix in the hll format to the CASS format
267  *
268  * the difference between the CASS and HLL is, that the CASS format has all 4
269  * quadrants in a quadrat wheras in HLL they are aligned in a rectangle.
270  @verbatim
271 
272 
273  ----()--------()-----
274  | 0 | 1 | 2 | 3 |
275  | A | C | D | B |
276  ---------------------
277  --------->
278 
279  |
280  v
281 
282  -----------
283  | 1 | 2 |
284  | C | D |
285  -----O-----
286  | 0 | 3 |
287  | A | B |
288  -----------
289  --------->
290  @endverbatim
291  * The numbers indicate the tile of the frame wihtin the HLL frame format, the
292  * letters indicate the tiles within the CASS format. The arrows indicate the
293  * fast increasing coordinate within the linearised array. The parenthesis and
294  * the 0 indicate where the hole btw the quater holes are in the new tiles.
295  * This implies that the tiles 1 and 2 in the HLL format have to be rotated by
296  * 180 degrees before adding them to CASS format array. This basically mean that
297  * one has to read these tiles reverseley. This is done in this function by
298  * using reverse iterators that will point at the last element of the tile.
299  *
300  * This function will then read the first row of tile 0 then the first rowv of
301  * tile 3. Then the 2nd row of tile 0 and then the 2nd row of tile 3. This
302  * continues until all rows of the tiles have been read.
303  * It then continues with the last of tile 1 and the last row of tile 2. Then
304  * the 2nd to last row of tile 1 and the 2nd to last row of tile 2. This
305  * continues until all rows of the tiles have been read.
306  *
307  * @note The resulting image in the CASS format is rotated by 90 degrees clockwise
308  * to the image as it would look like inside the lab frame if one looks
309  * into the beam.
310  *
311  * @tparam inputContainerType the type of the container containing the input
312  * @tparam outputContainerType the type of the container containing the output
313  * @param HLLMatrix the container containing the linearised input matrix
314  * @param CASSMatrix the container containing the linearised out matrix
315  * @param quadrantColumns the number of columns in one quadrant
316  * @param quadrantRows the number of rows in one quadrant
317  * @param HLLColumns the number of columns in the HLL input container
318  *
319  * @author Lutz Foucar
320  */
321 template <typename inputContainerType, typename outputContainerType>
322 void HLL2CASS(const inputContainerType& HLLMatrix,
323  outputContainerType& CASSMatrix,
324  size_t quadrantColumns,
325  size_t quadrantRows,
326  size_t HLLColumns)
327 {
328  typename inputContainerType::const_iterator hllquadrant0(HLLMatrix.begin());
329  typename inputContainerType::const_reverse_iterator hllquadrant1(HLLMatrix.rbegin()+2*quadrantColumns);
330  typename inputContainerType::const_reverse_iterator hllquadrant2(HLLMatrix.rbegin()+1*quadrantColumns);
331  typename inputContainerType::const_iterator hllquadrant3(HLLMatrix.begin()+3*quadrantColumns);
332 
333  typename outputContainerType::iterator cass(CASSMatrix.begin());
334 
335  //copy quadrant read to right side (lower in CASS)
336  for (size_t quadrantRow(0); quadrantRow < quadrantRows; ++quadrantRow)
337  {
338  copy(hllquadrant0,hllquadrant0+quadrantColumns,cass);
339  advance(cass,quadrantColumns);
340  copy(hllquadrant3,hllquadrant3+quadrantColumns,cass);
341  advance(cass,quadrantColumns);
342 
343  advance(hllquadrant0,HLLColumns);
344  advance(hllquadrant3,HLLColumns);
345  }
346  //copy quadrants read to left side (upper in CASS)
347  for (size_t quadrantRow(0); quadrantRow < quadrantRows; ++quadrantRow)
348  {
349  copy(hllquadrant1,hllquadrant1+quadrantColumns,cass);
350  advance(cass,quadrantColumns);
351  copy(hllquadrant2,hllquadrant2+quadrantColumns,cass);
352  advance(cass,quadrantColumns);
353 
354  advance(hllquadrant1,HLLColumns);
355  advance(hllquadrant2,HLLColumns);
356  }
357 }
358 } //end namespace hlltypes
359 } //end namespace cass
360 #endif
uint32_t tv_sec
start data taking time in seconds
Definition: hlltypes.hpp:97
char dataSetID[80]
identification of data
Definition: hlltypes.hpp:61
uint8_t nCCDs
number of CCD to be read out
Definition: hlltypes.hpp:49
char fillspace[988]
empty to fill the header up to 1024 bytes
Definition: hlltypes.hpp:150
void HLL2CASS(const inputContainerType &HLLMatrix, outputContainerType &CASSMatrix, size_t quadrantColumns, size_t quadrantRows, size_t HLLColumns)
convert a linearised matrix in the hll format to the CASS format
Definition: hlltypes.hpp:322
double sum
internal use
Definition: hlltypes.hpp:163
uint32_t external_id
Frame ID from an external trigger, e.g.
Definition: hlltypes.hpp:117
char fill[932]
fill up space to get the complete size of 1024 bytes
Definition: hlltypes.hpp:72
void CASS2HLL(const inputContainerType &CASSMatrix, outputContainerType &HLLMatrix, size_t quadrantColumns, size_t quadrantRows, size_t CASSColumns)
convert a linearised matrix in the CASS format to the hll format
Definition: hlltypes.hpp:235
the header that describe the frames
Definition: hlltypes.hpp:82
uint16_t the_maxHeight
the true (maximum) number of lines
Definition: hlltypes.hpp:69
double temp
temperature voltage
Definition: hlltypes.hpp:106
double sigma
noise sigma value of pixel
Definition: hlltypes.hpp:169
uint32_t columns
the width of the frames
Definition: hlltypes.hpp:141
uint8_t width
number of channels
Definition: hlltypes.hpp:52
uint16_t myLength
number of bytes in file header
Definition: hlltypes.hpp:43
uint16_t fhLength
number of bytes in frame header
Definition: hlltypes.hpp:46
std::vector< pixel > frame_t
define a frame
Definition: hlltypes.hpp:30
uint8_t version
format version
Definition: hlltypes.hpp:58
char identifystring[24]
string to identify that it is a hll darkcal file
Definition: hlltypes.hpp:138
uint16_t the_height
the true number of lines in following frame
Definition: hlltypes.hpp:114
uint32_t length
the overal length of the frame, if the matrix is linearized
Definition: hlltypes.hpp:147
uint8_t start
starting line, indicates window mode if not
Definition: hlltypes.hpp:85
uint8_t height
number of lines in following frame
Definition: hlltypes.hpp:94
uint32_t tv_usec
start data taking time in microseconds
Definition: hlltypes.hpp:100
uint16_t the_width
the true number of channels
Definition: hlltypes.hpp:66
uint32_t rows
the height of the frames
Definition: hlltypes.hpp:144
uint32_t index
index number
Definition: hlltypes.hpp:103
char fill[24]
fill up space to get 64 bytes
Definition: hlltypes.hpp:123
double sumSq
internal use
Definition: hlltypes.hpp:172
int16_t mean
offset mean value of pixel (common mode corrected)
Definition: hlltypes.hpp:178
uint64_t bunch_id
LCLS bunch ID.
Definition: hlltypes.hpp:120
uint16_t the_start
the true starting line, indicates window mode if > 0
Definition: hlltypes.hpp:111
uint8_t maxHeight
(maximum) number of lines
Definition: hlltypes.hpp:55
int16_t pixel
define a pixel
Definition: hlltypes.hpp:27
struct describing the statistics saved in a HLL Darkcal file
Definition: hlltypes.hpp:160
the file header structure of the hll darkcal file format
Definition: hlltypes.hpp:132
double offset
offset mean value of pixel (raw)
Definition: hlltypes.hpp:166