16 #include <QtCore/QFile>
17 #include <QtCore/QDateTime>
18 #include <QtCore/QFileInfo>
19 #include <QtCore/QtGlobal>
65 using Streaming::operator >>;
66 ifstream hllfile(filename.c_str(),ios::in);
67 if (!hllfile.is_open())
70 "'. Skip loading the offset and noise map.");
75 if ((
string(
"HE pixel statistics map") != header.identifystring) ||
76 (header.columns * header.rows != header.length))
78 throw invalid_argument(
"readHLLOffsetFile(): Error file '" + filename +
79 "' has either wrong header identify string '" +
80 header.identifystring +
"' or the header information"
81 " is inconsistent columns * rows != length '" +
83 "!=" +
toString(header.length) +
"'");
85 vector<hllDataTypes::statistics> pixelStatistics(header.length);
87 hllfile.read(reinterpret_cast<char*>(&pixelStatistics.front()),pixelStatisticsLength);
88 vector<char> badpixmap(header.length);
89 hllfile.read(&badpixmap[0],header.length);
92 Detector::frame_t::iterator hlloffset(hlloffsets.begin());
94 Detector::frame_t::iterator hllnoise(hllnoises.begin());
95 vector<hllDataTypes::statistics>::iterator pixelstatistic(pixelStatistics.begin());
96 for(
size_t i(0); i < header.length; ++i, ++hlloffset, ++hllnoise, ++pixelstatistic )
98 *hlloffset = pixelstatistic->offset;
99 *hllnoise = pixelstatistic->sigma;
104 data.
noiseMap.resize(header.length);
106 data.
mask.resize(header.length);
122 ofstream out(filename.c_str(),ios::out|ios::trunc);
125 throw invalid_argument(
"saveHLLOffsetFile(): Error opening file '" +
130 "HE pixel statistics map",
131 static_cast<uint32_t
>(data.
columns*2),
132 static_cast<uint32_t>(data.
rows*0.5),
142 Detector::frame_t::const_iterator hlloffset(hlloffsets.begin());
143 Detector::frame_t::const_iterator hllnoise(hllnoises.begin());
144 vector<hllDataTypes::statistics> pixelStatistics(header.
length);
145 vector<hllDataTypes::statistics>::iterator pixelstatistic(pixelStatistics.begin());
146 for(; hlloffset != hlloffsets.end(); ++hlloffset, ++hllnoise, ++pixelstatistic )
148 pixelstatistic->offset = *hlloffset;
149 pixelstatistic->sigma = *hllnoise;
152 out.write(reinterpret_cast<char*>(&pixelStatistics.front()),pixelStatisticsLength);
155 const size_t maskLength(
sizeof(
char)*header.
length);
156 out.write(reinterpret_cast<char*>(&hllmask[0]),maskLength);
172 ifstream in(filename.c_str(), ios::binary);
176 "'. Skipping reading the noise and offset maps.");
179 in.seekg(0,std::ios::end);
180 const size_t size = in.tellg() / 2 /
sizeof(double);
181 in.seekg(0,std::ios::beg);
182 vector<double> offsets(size);
183 in.read(reinterpret_cast<char*>(&offsets[0]), size*
sizeof(
double));
184 vector<double> noises(size);
185 in.read(reinterpret_cast<char*>(&noises[0]), size*
sizeof(
double));
188 copy(offsets.begin(),offsets.end(),data.
offsetMap.begin());
190 copy(noises.begin(),noises.end(),data.
noiseMap.begin());
207 ofstream out(filename.c_str(), ios::binary);
210 throw invalid_argument(
"saveCASSOffsetFile(): Error opening file '" +
213 vector<double> offsets(data.
offsetMap.size());
215 out.write(reinterpret_cast<char*>(&offsets[0]), offsets.size()*
sizeof(double));
216 vector<double> noises(data.
noiseMap.size());
218 out.write(reinterpret_cast<char*>(&noises[0]), noises.size()*
sizeof(double));
268 if (filename.empty())
270 Log::add(
Log::WARNING,
"readHLLGainFile: No filename given therefore gain / cte will be set to 1");
273 ifstream in(filename.c_str(), ios::binary);
277 "' does not exist. Skipping reading the Gain/CTE file");
281 in.getline(line, 80);
282 if (line !=
string(
"HE File containing gain and CTE values"))
284 throw runtime_error(
"readHLLGainFile: Wrong file format: " + std::string(line));
286 in.getline(line, 80);
287 if (strncmp(line,
"VERSION 3", 9))
289 throw runtime_error(
"readHLLGainFile: Wrong file format: " + std::string(line));
291 in.getline(line, 80);
307 if ((g !=
'G') || (c !=
'C'))
309 gains.push_back(gain);
315 const size_t rows(512);
316 const size_t columns(gains.size());
317 for (
size_t row(0); row < rows; ++row)
319 for (
size_t column(0); column < columns; ++column)
321 hllgaincteMap.push_back(gains[column] / pow(ctes[column], static_cast<int>(row+1)));
339 ifstream in(filename.c_str(), ios::binary);
343 "'. Skipping reading the gain map.");
346 in.seekg(0,std::ios::end);
348 in.seekg(0,std::ios::beg);
352 in.read(reinterpret_cast<char*>(&gains.front()), size*
sizeof(
Detector::pixel_t));
364 ofstream out(filename.c_str(), ios::binary);
367 throw invalid_argument(
"saveCASSGainFile(): Error opening file '" +
371 out.write(reinterpret_cast<const char*>(&gains.front()), gains.size()*
sizeof(
Detector::pixel_t));
384 ifstream in(filename.c_str(), ios::binary);
388 "'. Skipping reading the hot pixels mask.");
391 in.seekg(0,std::ios::end);
392 const int valuesize(
sizeof(CommonData::mask_t::value_type));
393 const size_t size = in.tellg() / valuesize;
394 in.seekg(0,std::ios::beg);
398 in.read(reinterpret_cast<char*>(&hotpix.front()), size*valuesize);
410 ofstream out(filename.c_str(), ios::binary);
413 throw invalid_argument(
"saveCASSHotPixFile(): Error opening file '" +
417 const int valuesize(
sizeof(CommonData::mask_t::value_type));
418 out.write(&hotpix.front(), hotpix.size()*valuesize);
444 "' to accommodate the frames with size '" +
446 "'. Resizing the offsetMap");
454 "' to accommodate the frames with size '" +
459 if ((frame.
columns * frame.
rows) != static_cast<int>(data.
mask.size()))
463 "' to accommodate the frames with size '" +
479 "' to accommodate the frames with size '" +
481 "'. Resizing the gain_cteMap");
489 "' to accommodate the frames with size '" +
491 "'. Resizing the correctionMap");
499 "' to accommodate the frames with size '" +
501 "'. Resizing the correctionMap");
507 throw runtime_error(
"IsSameSize: Should not happen");
525 if (_instances.find(detector) == _instances.end())
528 string(
" common data container for detector '") + detector +
"'");
537 instancesmap_t::iterator instance(_instances.begin());
538 instancesmap_t::const_iterator end(_instances.end());
539 for (; instance != end; ++instance)
542 instance->first +
"'");
543 if(command ==
"startDarkcal")
544 instance->second->_offsetnoiseMapcreator->controlCalibration(command);
545 else if (command ==
"startGain")
546 instance->second->_gainCreator->controlCalibration(command);
548 throw invalid_argument(
"CommonData::controlCalibration: command '" +
549 command +
"' is unknown.");
557 _settingsLoaded(false),
558 _noiseRange(make_pair(0,0))
572 columns = s.
value(
"nColumns",static_cast<unsigned>(shape.first)).toUInt();
573 rows = s.
value(
"nRows",static_cast<unsigned>(shape.second)).toUInt();
575 "The shape of detector '" +
detectorname +
"' is set to '" +
591 string mapcreatortype(s.
value(
"MapCreatorType",
"none").toString().toStdString());
593 _offsetnoiseMapcreator->loadSettings(s);
598 string offsetfilename(s.
value(
"InputOffsetNoiseFilename",
601 if (offsetfilenameInfo.isSymLink())
603 if (offsetfilenameInfo.exists())
604 offsetfilename = offsetfilenameInfo.symLinkTarget().toStdString();
607 offsetfilename +
"' is a link that referes to a non existing file!");
612 string(
" for detector with name '") +
detectorname +
"' which has id '" +
615 string offsetfiletype(s.
value(
"InputOffsetNoiseFiletype",
"hll").toString().toStdString());
616 if (offsetfiletype ==
"hll")
618 else if(offsetfiletype ==
"cass")
621 throw invalid_argument(
"CommonData::loadSettings: OffsetNoiseFiletype '" +
622 offsetfiletype +
"' does not exist");
627 string outputoffsetfiletype(s.
value(
"OutputOffsetNoiseFiletype",
"hll").toString().toStdString());
628 if (outputoffsetfiletype ==
"hll")
630 else if(outputoffsetfiletype ==
"cass")
633 throw invalid_argument(
"CommonData::loadSettings: OutputOffsetNoiseFiletype '" +
634 outputoffsetfiletype +
"' does not exist");
637 string gainmapcreatortype(s.
value(
"GainMapCreatorType",
"none").toString().toStdString());
639 _gainCreator->loadSettings(s);
642 string gainfilename(s.
value(
"InputGainFilename",
645 if (gainfilenameInfo.isSymLink())
647 if (gainfilenameInfo.exists())
648 gainfilename = gainfilenameInfo.symLinkTarget().toStdString();
651 gainfilename +
"' is a link that referes to a non existing file!");
656 string(
" for detector with name '") +
detectorname +
"' which has id '" +
659 string gainFiletype(s.
value(
"InputGainFiletype",
"hll").toString().toStdString());
660 if (gainFiletype ==
"hll")
662 else if (gainFiletype ==
"cass")
665 throw invalid_argument(
"CommonData::loadSettings: GainFiletype '" +
666 gainFiletype +
"' does not exist");
671 string outputgainfiletype(s.
value(
"OutputGainFiletype",
"cass").toString().toStdString());
672 if (outputgainfiletype ==
"cass")
675 throw invalid_argument(
"CommonData::loadSettings: OutputGainFiletype '" +
676 outputgainfiletype +
"' does not exist");
679 string hotpixfilename(s.
value(
"InputHotPixMaskFilename",
682 if (hotpixfilenameInfo.isSymLink())
684 if (hotpixfilenameInfo.exists())
685 hotpixfilename = hotpixfilenameInfo.symLinkTarget().toStdString();
688 hotpixfilename +
"' is a link that referes to a non existing file!");
693 string(
" for detector with name '") +
detectorname +
"' which has id '" +
696 string hotpixFiletype(s.
value(
"InputHotPixMaskFiletype",
"cass").toString().toStdString());
697 if (hotpixFiletype ==
"cass")
700 throw invalid_argument(
"CommonData::loadSettings: HotPixFiletype '" +
701 hotpixFiletype +
"' does not exist");
706 string outputhotpixfiletype(s.
value(
"OutputHotPixMaskFiletype",
"cass").toString().toStdString());
707 if (outputhotpixfiletype ==
"cass")
710 throw invalid_argument(
"CommonData::loadSettings: OutputHotPixMaskFiletype '" +
711 outputhotpixfiletype +
"' does not exist");
727 if (allmapsize != 6*frame.
data.size())
728 throw runtime_error(
"CommonData::generateMaps: '" +
detectorname +
729 "', at least one of the maps is not resized to fit the " +
733 generateOffsetNoiseMaps(frame);
735 generateGainMap(frame);
753 throw runtime_error(
"CommonData::saveOffsetNoiseMaps: could not remove already existing link '" +
756 throw runtime_error(
"CommonData::saveOffsetNoiseMaps: could not create a link named '"+
757 linkname +
"' that points to the outputfile '" + outname +
"'");
776 throw runtime_error(
"CommonData::saveGainMap: could not remove already existing link '" +
779 throw runtime_error(
"CommonData::saveGainMap: could not create a link named '"+
780 linkname +
"' that points to the outputfile '" + outname +
"'");
799 throw runtime_error(
"CommonData::saveGainMap: could not remove already existing link '" +
802 throw runtime_error(
"CommonData::saveHotPixMask: could not create a link named '"+
803 linkname +
"' that points to the outputfile '" + outname +
"'");
813 Detector::frame_t::const_iterator val(
noiseMap.begin());
814 Detector::frame_t::const_iterator End(
noiseMap.end());
816 stat.addDatum(*val++);
824 Detector::frame_t::const_iterator corvalMapEnd(
correctionMap.end());
826 Detector::frame_t::const_iterator gain(
gain_cteMap.begin());
827 mask_t::const_iterator Mask(
mask.begin());
828 mask_t::const_iterator hotpix(
hotpixels.begin());
831 while (corval != corvalMapEnd)
833 *corval = *gain++ * *corval * *Mask++ *
bool _settingsLoaded
switch to tell that load settins for this common data was already running
void createCorMap()
create the correction map
std::string _inputGainFilename
the gain correction input filename
void saveOffsetNoiseMaps()
save offset and noise maps
file contains declaration of the CASSEvent
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
statistics calculator for a cummulative statistic, removes outliers
contains base class for all correction map creators.
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
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
contains the layout of the hll data types and conversion to cass data types
std::string _inputOffsetFilename
input filename of the offset and noise map
std::pair< size_t, size_t > shape_t
define a shape of an image columnsxrows
things written only at end of run H5Dump ProcessorSummary size
void readHLLOffsetFile(const string &filename, CommonData &data)
will read the file containing the offset and noise map in the hll format
std::tr1::function< void(const std::string &, CommonData &)> _saveHotPixTo
function to write the hot pixel mask
void isSameSize(const Frame &frame, CommonData &data)
check whether the frame has the same size as the maps.
std::vector< char > mask_t
a mask is a vector of bools
bool _autoNoiseThreshold
flag whether the noise threshold should be automatically determined
static void add(Level level, const std::string &line)
add a string to the log
float pixel_t
define a pixel of the pixel detector
static shared_pointer instance(const std::string &type)
create an instance of the requested functor
fromStdString(const std::string &str)
void readHLLGainFile(const string &filename, CommonData &data)
will read the file containing the gain and cte corretion factors in the HLL format ...
std::pair< Detector::pixel_t, Detector::pixel_t > _noiseRange
the range in adu for masking noisy pixels
mask_t hotpixels
the hotpixel mask
Detector::pixel_t _autoMultiplier
the multiplier about which the automatically determined threshold will be defined ...
size_t columns
the width of the maps
void saveHotPixMask()
save hot pixel mask
size_t rows
the height of the maps
static instancesmap_t _instances
the helperclass instances.
void loadSettings(CASSSettings &s)
load the settings of this common object
void saveCASSGainFile(const string &filename, const CommonData &data)
save the gain map to CASS style file
std::vector< pixel_t > frame_t
a frame is a vector of pixels
contains definition of the mask of a pixeldetector
void saveCASSHotPixFile(const string &filename, const CommonData &data)
save the hot pixels to CASS style file
std::tr1::shared_ptr< MapCreatorBase > _gainCreator
functor to create the Maps
uint16_t rows
how many rows
uint16_t columns
how many columns
static QMutex _mutex
mutex to lock the creation of an instance
static std::string fromSettings(const CASSSettings &s)
retrieve it from the casssettings
std::string toString(const Type &t)
convert any type to a string
auxiliary data[Processor]
std::string _outputHotPixFilename
the hot pixel mask output filename
Data used commonly for one AdvancedDetector.
void readCASSOffsetFile(const string &filename, CommonData &data)
will read the file containing the offset and noise map in the former CASS format
void readCASSGainFile(const string &filename, CommonData &data)
read the CASS generated gain file
Detector::frame_t offsetMap
the offset map
value(const QString &key, const QVariant &defaultValue=QVariant()
std::tr1::shared_ptr< CommonData > shared_pointer
typedef a shared pointer of this
QReadWriteLock lock
lock to synchronize read and write acces to the common data
contains declarations of statistic calculators
A Frame of an advance Pixel Detector.
void readCASSHotPixFile(const string &filename, CommonData &data)
read the CASS generated hotpixels file
int32_t detectorId
the id of the detector that contains the frames whos maps we have here
Detector::frame_t data
the frame data
void saveCASSOffsetFile(const string &filename, CommonData &data)
will save the file containing the offset and noise map in the former CASS format
contains the common data for one advanced pixeldetector
void saveHLLOffsetFile(const string &filename, CommonData &data)
save the maps to a hll type darkcal file
CommonData()
prevent people from constructing other than using instance().
file contains specialized class that do the settings for cass
link(const QString &linkName)
Detector::frame_t correctionMap
the correction map
std::tr1::function< void(const std::string &, CommonData &)> _saveGainTo
function to write the gain map
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
std::string _inputHotPixFilename
the hot pixel mask input filename
static Detector::shape_t shapeFromName(const std::string &name)
try to retrive the right shape of the detector from the name
std::tr1::function< void(const std::string &, CommonData &)> _saveNoiseOffsetTo
function to write the offset maps
std::string _outputGainFilename
the gain correction output filename
std::string _outputOffsetFilename
output filename for the offset and noise maps
void generateMaps(const Frame &frame)
generate the maps from the frame data with help of the functors
void createCASSMask(CommonData &data, CASSSettings &s)
create the mask
std::map< std::string, shared_pointer > instancesmap_t
typedef describing the instances of the helper
contains a logger for cass
base class for all correction map creators
struct describing the statistics saved in a HLL Darkcal file
Detector::frame_t gain_cteMap
the gain + cte map
mask_t mask
the detector mask
Detector::frame_t noiseMap
the noise map
beginGroup(const QString &prefix)
set up how to create the noise