CFEL - ASG Software Suite  2.5.0
CASS
common_data.h
Go to the documentation of this file.
1 //Copyright (C) 2011,2012,2013 Lutz Foucar
2 
3 /**
4  * @file common_data.h contains the common data for one advanced pixeldetector
5  *
6  * @author Lutz Foucar
7  */
8 
9 #ifndef _COMMON_DATA_H_
10 #define _COMMON_DATA_H_
11 
12 #include <stdint.h>
13 #include <utility>
14 #include <algorithm>
15 #include <list>
16 #include <string>
17 #include <map>
18 #include <tr1/memory>
19 #include <tr1/functional>
20 
21 #include <QtCore/QMutex>
22 #include <QtCore/QReadWriteLock>
23 #include <QtCore/QStringList>
24 
25 #include "pixeldetector.hpp"
26 #include "cass_settings.h"
27 
28 namespace cass
29 {
30 class CASSSettings;
31 
32 namespace pixeldetector
33 {
34 class Frame;
35 class MapCreatorBase;
36 
37 /** retrieve the DetectorName
38  *
39  * @author Lutz Foucar
40  */
42 {
43  /** retrieve it from the casssettings
44  *
45  * @param s the settings to retrieve the name from
46  */
47  static std::string fromSettings(const CASSSettings &s)
48  {
49  return s.group().split("/").at(1).toStdString();
50  }
51 };
52 
53 /** Data used commonly for one AdvancedDetector
54  *
55  * This class hold the data for one AdvancedDetector. There can be multiple
56  * instances of the AdvancedDetector which should use only one common data
57  * for e.g. offsetmaps. This data is contained in this class. To ensure that
58  * one AdvancedDetector will get only one instance the static function
59  * CommonData::instance will return only a new instance when the name does not
60  * exist yet.
61  *
62  * @cassttng PixelDetectors/\%name\%/CorrectionMaps/{MapCreatorType}\n
63  * The type of functor that will create the maps used for correcting
64  * the frames from individual frames. See @ref MapCreators for a list
65  * of all possible values. Default is "none".
66  * @cassttng PixelDetectors/\%name\%/CorrectionMaps/{InputOffsetNoiseFilename}\n
67  * The filename containing the saved noise and offset maps. Default
68  * is "darkcal_%detectorId%.lnk". Which is a link to the most recent
69  * darkcalibration file. If no file is found, the offset will be set to
70  * 0 and the noise will be set to 4000. When the user has chosen a
71  * "NoisyPixelThreshold" lower than 4000 this will result in a completly
72  * masked frame.
73  * @cassttng PixelDetectors/\%name\%/CorrectionMaps/{InputOffsetNoiseFiletype}\n
74  * The filetype that the values are stored in. Default is "hll".
75  * Options are:
76  * - "hll": the filetype used by the semi conductor lab.
77  * - "cass": the filetype formerly used in CASS.
78  * @cassttng PixelDetectors/\%name\%/CorrectionMaps/{OutputOffsetNoiseFilename}\n
79  * The filename where the offset and noise values will be written to.
80  * If the name is "darkcal", the name will be build by the detector id
81  * and the current date and time when it was written. Also when writing
82  * a link to the written file will be created like this:
83  * "darkcal_%detectorID%.lnk". When the name differs from "darkcal",
84  * the values will be written only to the given filename. See
85  * cass::pixeldetector::CommonData::saveOffsetNoiseMaps for more details.
86  * Default is "darkcal".
87  * @cassttng PixelDetectors/\%name\%/CorrectionMaps/{OutputOffsetNoiseFiletype}\n
88  * The filetype that the noise and offset values are stored to.
89  * Default is "hll".
90  * Options are:
91  * - "hll": the filetype used by the semi conductor lab.
92  * - "cass": the filetype formerly used in CASS.
93  * @cassttng PixelDetectors/\%name\%/CorrectionMaps/{GainMapCreatorType}\n
94  * The type of functor that will create the gain used for correcting
95  * the frames from individual frames. See @ref GainMapCreators for a
96  * list of all possible values. Default is "none".
97  * @cassttng PixelDetectors/\%name\%/CorrectionMaps/{InputGainFilename}\n
98  * The filename containing the gain (/cte) values. Default
99  * is "gain_\%detectorId\%.lnk".
100  * @cassttng PixelDetectors/\%name\%/CorrectionMaps/{InputGainFiletype}\n
101  * The filetype that the gain (/cte) values are stored in. Default is
102  * "hll". Options are:
103  * - "hll": the filetype used by the semi conductor lab.
104  * - "cass": the filetype used by CASS.
105  * @cassttng PixelDetectors/\%name\%/CorrectionMaps/{OutputGainFilename}\n
106  * The filename where the gain (/cte) values will be written to. If
107  * the name is "gain", the name will be build by the detector id and
108  * the current date and time when it was written. Also when writing
109  * a link to the written file will be created like this:
110  * "gain_\%detectorID\%.lnk". When the name differs from "gain", the
111  * values will be written only to the given filename. See
112  * cass::pixeldetector::CommonData::saveGainMap for more details.
113  * Default is "gain".
114  * @cassttng PixelDetectors/\%name\%/CorrectionMaps/{OutputGainFiletype}\n
115  * The filetype that the gain (/cte) values are stored in. Default is
116  * "cass". Options are:
117  * - "cass": the filetype used by CASS.
118  * @cassttng PixelDetectors/\%name\%/CorrectionMaps/{NoisyPixelThreshold | LowerNoisyPixelThreshold}\n
119  * The boundaries to identify noisy pixels. Will be used when creating
120  * the correction map. When the noise of the pixel is outside these
121  * exclusive bounds the pixel will be masked. If NoisyPixelThreshold
122  * is -1, good values for the bound will be determined from the noise
123  * map. Default is 40000 | 0
124  * @cassttng PixelDetectors/\%name\%/CorrectionMaps/{AutoMultiplier}\n
125  * When automatically determining the noise threshold, this is the
126  * multiplier by which the autovalue is determined.
127  * \f$ NoisyPixelThreshold = mean_noise + AutoMultiplier + stdv_noise \f$.
128  * \f$ LowerNoisyPixelThreshold = mean_noise - AutoMultiplier + stdv_noise \f$.
129  * Default is 4.
130  * @cassttng PixelDetectors/\%name\%/CorrectionMaps/{nColumns}\n
131  * @cassttng PixelDetectors/\%name\%/CorrectionMaps/{nRows}\n
132  *
133  *
134  * @author Lutz Foucar
135  */
137 {
138 public:
139  /** typedef a shared pointer of this */
140  typedef std::tr1::shared_ptr<CommonData> shared_pointer;
141 
142  /** typedef describing the instances of the helper */
143  typedef std::map<std::string,shared_pointer> instancesmap_t;
144 
145  /** a mask is a vector of bools */
146  typedef std::vector<char> mask_t;
147 
148 public:
149  /** static function creating instance of this.
150  *
151  * return the instance of the common data used with the advanced detector with
152  * the name detector. If the instance is not yet inside the _instances map the
153  * helper instance will be created one and put into the _instances map. So
154  * that later calls to this will return the same shared pointer.
155  *
156  * @return instance of the common data
157  * @param detector key (name) of the detector to find it in the _instances map
158  */
159  static shared_pointer instance(const instancesmap_t::key_type& detector);
160 
161  /** issue a command to the map creators of all instances
162  *
163  * @param command the command to issue
164  */
165  static void controlCalibration(const std::string& command);
166 
167  /** load the settings of this common object
168  *
169  * See the description of this class for the list of possible variables that
170  * will be loaded via this function. Once all parameters are loaded it will
171  * load the Mask for this detector. See pixeldetector::createCASSMask for
172  * options creating the mask. Also the correction map for this dector is
173  * created once all information has been loaded. See createCorrectionMap for
174  * details.
175  *
176  * @param s the object to load the settings for the common data from.
177  */
178  void loadSettings(CASSSettings &s);
179 
180  /** generate the maps from the frame data with help of the functors
181  *
182  * There are several ways of creating the maps available. For a detailed list
183  * see this classes help.
184  *
185  * @param frame The frame data to create the maps from
186  */
187  void generateMaps(const Frame& frame);
188 
189 
190  /** create the correction map
191  *
192  * will create the correction map from the mask, noise and cte/gain values
193  * with the help of the cass::pixeldetector::createCorrectionMap function
194  *
195  * the correction value for a pixel is calculated using the following formular:
196  *
197  * \f[
198  * corval = ctegain \times corval \times maskval \times \begin{cases}
199  * 0, & \text{if } noise < noisethreshold \\
200  * 1, & \text{otherwise} \\
201  * \end{cases}
202  * \f]
203  *
204  * @note we do not need to lock this function since, it will be called by
205  * the map creators only. And their operators are still locked by
206  * this classes createMaps function that will envoke the functors.
207  * @todo make this a friend and protected so only functions that we allow
208  * can call it.
209  */
210  void createCorMap();
211 
212  /** save offset and noise maps
213  *
214  * save the offset and noise calibratioin to file in the user chosen
215  * fileformat. See
216  * cass::pixeldetector::saveCASSOffsetFile or
217  * cass::pixeldetector::saveHLLOffsetFile for details.
218  *
219  * If the filename is "darkcal" for the darkcalibration file, the detector id
220  * and the current time will be appended the filename. A link to the created
221  * files will be generated. The name of the link will be called
222  * "darkcal_\%detectorID\%.lnk". In case the link exists, try to remove it
223  * first.
224  */
225  void saveOffsetNoiseMaps();
226 
227  /** save gain map
228  *
229  * save the gain calibration to file in the user chosen fileformat. See
230  * cass::pixeldetector::saveCASSGaiFile for details.
231  *
232  * If the filename is "gain" for the gain calibration file, the detector id
233  * and the current time will be appended to the filename. A link
234  * to the created files will be generated. The name of the link will be
235  * called "gain_\%detectorID\%.lnk". In case the links exist, try to
236  * remove it first.
237  */
238  void saveGainMap();
239 
240  /** save hot pixel mask
241  *
242  * save the hot pixel mask to file in the user chosen fileformat.
243  *
244  * If the filename is "hotpix" for the gain calibration file, the detector id
245  * and the current time will be appended to the filename. A link
246  * to the created files will be generated. The name of the link will be
247  * called "hotpix_\%detectorID\%.lnk". In case the link exist, try to
248  * remove it first.
249  */
250  void saveHotPixMask();
251 
252  /** lock to synchronize read and write acces to the common data */
254 
255  /** the width of the maps */
256  size_t columns;
257 
258  /** the height of the maps */
259  size_t rows;
260 
261  /** the offset map
262  *
263  * the offset map is the mean value of the individual pixels for given
264  * number of frames
265  */
267 
268  /** the noise map
269  *
270  * the noise map is the standart deviation of the mean value of indidual
271  * pixels for a given number of frames
272  */
274 
275  /** the detector mask
276  *
277  * the mask is a matrix with either 0 or 1 which indicate which pixels
278  * shoud be omitted (1 stands for :take pixel, 0 is for don't take pixel
279  */
280  mask_t mask;
281 
282  /** the hotpixel mask
283  *
284  * a negative value masks pixels permanently. Positive values indicate how
285  * many frames this pixel was high. If its not high anymore the mask value
286  * will be reset to 0.
287  */
288  mask_t hotpixels;
289 
290  /** the gain + cte map
291  *
292  * this is a matrix of values containing correction factors for each
293  * individual pixel of the frame
294  */
296 
297  /** the correction map
298  *
299  * this map contains the correction values calculated from the mask, the gain
300  * and cte map. With this values the indivdual pixels will be mulitplied
301  * in the HLL like processing of the frame
302  */
304 
305  /** the id of the detector that contains the frames whos maps we have here */
306  int32_t detectorId;
307 
308  /** the name of the detector whos maps are contained in this class instance */
309  std::string detectorname;
310 
311 private:
312  /** prevent people from constructing other than using instance().*/
314 
315  /** private constructor.
316  *
317  * unused
318  *
319  * @param detname the name of the detector
320  */
321  CommonData(const instancesmap_t::key_type& detname);
322 
323  /** prevent copy-construction*/
325 
326  /** prevent assingment */
327  CommonData& operator=(const CommonData&) {return *this;}
328 
329  /** the helperclass instances.
330  *
331  * the instances of this class put into map
332  * one instance for each available detector
333  */
334  static instancesmap_t _instances;
335 
336  /** mutex to lock the creation of an instance */
337  static QMutex _mutex;
338 
339 private:
340  /** functor to create the Maps */
341  std::tr1::shared_ptr<MapCreatorBase> _offsetnoiseMapcreator;
342 
343  /** function to write the offset maps */
344  std::tr1::function<void(const std::string&,CommonData&)> _saveNoiseOffsetTo;
345 
346  /** output filename for the offset and noise maps */
348 
349  /** input filename of the offset and noise map */
350  std::string _inputOffsetFilename;
351 
352  /** switch to tell that load settins for this common data was already running */
354 
355  /** functor to create the Maps */
356  std::tr1::shared_ptr<MapCreatorBase> _gainCreator;
357 
358  /** function to write the gain map */
359  std::tr1::function<void(const std::string&,CommonData&)> _saveGainTo;
360 
361  /** the gain correction input filename */
362  std::string _inputGainFilename;
363 
364  /** the gain correction output filename */
365  std::string _outputGainFilename;
366 
367  /** function to write the hot pixel mask */
368  std::tr1::function<void(const std::string&,CommonData&)> _saveHotPixTo;
369 
370  /** the hot pixel mask input filename */
371  std::string _inputHotPixFilename;
372 
373  /** the hot pixel mask output filename */
375 
376  /** the range in adu for masking noisy pixels */
377  std::pair<Detector::pixel_t,Detector::pixel_t> _noiseRange;
378 
379  /** flag whether the noise threshold should be automatically determined */
381 
382  /** the multiplier about which the automatically determined threshold will be defined */
384 };
385 
386 } //end namespace pixeldetector
387 } //end namespace cass
388 
389 
390 #endif
bool _settingsLoaded
switch to tell that load settins for this common data was already running
Definition: common_data.h:353
void createCorMap()
create the correction map
std::string _inputGainFilename
the gain correction input filename
Definition: common_data.h:362
retrieve the DetectorName
Definition: common_data.h:41
void saveOffsetNoiseMaps()
save offset and noise maps
Settings for CASS.
Definition: cass_settings.h:30
static void controlCalibration(const std::string &command)
issue a command to the map creators of all instances
void saveGainMap()
save gain map
std::string detectorname
the name of the detector whos maps are contained in this class instance
Definition: common_data.h:309
std::string _inputOffsetFilename
input filename of the offset and noise map
Definition: common_data.h:350
std::tr1::function< void(const std::string &, CommonData &)> _saveHotPixTo
function to write the hot pixel mask
Definition: common_data.h:368
std::vector< char > mask_t
a mask is a vector of bools
Definition: common_data.h:146
bool _autoNoiseThreshold
flag whether the noise threshold should be automatically determined
Definition: common_data.h:380
CommonData & operator=(const CommonData &)
prevent assingment
Definition: common_data.h:327
float pixel_t
define a pixel of the pixel detector
std::pair< Detector::pixel_t, Detector::pixel_t > _noiseRange
the range in adu for masking noisy pixels
Definition: common_data.h:377
mask_t hotpixels
the hotpixel mask
Definition: common_data.h:288
Detector::pixel_t _autoMultiplier
the multiplier about which the automatically determined threshold will be defined ...
Definition: common_data.h:383
size_t columns
the width of the maps
Definition: common_data.h:256
void saveHotPixMask()
save hot pixel mask
size_t rows
the height of the maps
Definition: common_data.h:259
static instancesmap_t _instances
the helperclass instances.
Definition: common_data.h:334
void loadSettings(CASSSettings &s)
load the settings of this common object
std::vector< pixel_t > frame_t
a frame is a vector of pixels
std::tr1::shared_ptr< MapCreatorBase > _gainCreator
functor to create the Maps
Definition: common_data.h:356
static QMutex _mutex
mutex to lock the creation of an instance
Definition: common_data.h:337
static std::string fromSettings(const CASSSettings &s)
retrieve it from the casssettings
Definition: common_data.h:47
std::string _outputHotPixFilename
the hot pixel mask output filename
Definition: common_data.h:374
Data used commonly for one AdvancedDetector.
Definition: common_data.h:136
Detector::frame_t offsetMap
the offset map
Definition: common_data.h:266
std::tr1::shared_ptr< CommonData > shared_pointer
typedef a shared pointer of this
Definition: common_data.h:140
QReadWriteLock lock
lock to synchronize read and write acces to the common data
Definition: common_data.h:253
A Frame of an advance Pixel Detector.
int32_t detectorId
the id of the detector that contains the frames whos maps we have here
Definition: common_data.h:306
contains container for simple pixel detector data
CommonData()
prevent people from constructing other than using instance().
Definition: common_data.h:313
file contains specialized class that do the settings for cass
Detector::frame_t correctionMap
the correction map
Definition: common_data.h:303
std::tr1::function< void(const std::string &, CommonData &)> _saveGainTo
function to write the gain map
Definition: common_data.h:359
static shared_pointer instance(const instancesmap_t::key_type &detector)
static function creating instance of this.
std::tr1::shared_ptr< MapCreatorBase > _offsetnoiseMapcreator
functor to create the Maps
Definition: common_data.h:341
std::string _inputHotPixFilename
the hot pixel mask input filename
Definition: common_data.h:371
std::tr1::function< void(const std::string &, CommonData &)> _saveNoiseOffsetTo
function to write the offset maps
Definition: common_data.h:344
std::string _outputGainFilename
the gain correction output filename
Definition: common_data.h:365
std::string _outputOffsetFilename
output filename for the offset and noise maps
Definition: common_data.h:347
void generateMaps(const Frame &frame)
generate the maps from the frame data with help of the functors
std::map< std::string, shared_pointer > instancesmap_t
typedef describing the instances of the helper
Definition: common_data.h:143
Electron detector
Definition: hdf5-input.ini:62
Detector::frame_t gain_cteMap
the gain + cte map
Definition: common_data.h:295
mask_t mask
the detector mask
Definition: common_data.h:280
Detector::frame_t noiseMap
the noise map
Definition: common_data.h:273
CommonData(const CommonData &)
prevent copy-construction
Definition: common_data.h:324