18#include "Utility/Inform.h"
30 const std::string& fileName,
const std::string& imageName,
double intensityCut,
short flags)
37 standardDeviation_m(0.0) {
38 unsigned short* image =
readFile(fileName, imageName);
66#ifdef TESTLASEREMISSION
81 namespace fs = std::filesystem;
82 if (!fs::exists(fileName)) {
84 "LaserProfile::readFile",
"given file '" + fileName +
"' does not exist");
87 size_t npos = fileName.find_last_of(
'.');
88 std::string ext = fileName.substr(npos + 1);
90 unsigned short* image;
107 unsigned int idx = 0;
108 for (
unsigned int i = 0; i <
sizeX_m; ++i) {
109 for (
unsigned int j = 0; j <
sizeY_m; ++j, ++idx) {
118 const std::string& fileName,
const std::string& imageName) {
119 hid_t h5 = H5Fopen(fileName.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
120 hid_t group = H5Gopen2(h5, imageName.c_str(), H5P_DEFAULT);
124 "LaserProfile::readFile",
"given image name '" + imageName +
"' does not exist");
128 hid_t dataSet = H5Dopen2(group,
"CameraData", H5P_DEFAULT);
131 throw OpalException(
"LaserProfile::readFile",
"data set 'CameraData' does not exist");
134 hid_t dataSetSpace = H5Dget_space(dataSet);
135 H5Sget_simple_extent_dims(dataSetSpace, dim,
nullptr);
136 hid_t filespace = H5Screate_simple(2, dim,
nullptr);
141 hsize_t startHyperslab[] = {0, 0};
144 H5Sselect_hyperslab(filespace, H5S_SELECT_SET, startHyperslab,
nullptr, blockCount,
nullptr);
147 hid_t mem = H5Screate_simple(2, blockCount,
nullptr);
149 H5Dread(dataSet, H5T_NATIVE_USHORT, mem, filespace, H5P_DEFAULT, image);
153 H5Dclose(dataSetSpace);
162 unsigned int col2 =
sizeX_m - 1;
163 for (
unsigned int col1 = 0; col1 < col2; ++col1, --col2) {
164 unsigned int pixel1 = col1 *
sizeY_m;
165 unsigned int pixel2 = col2 *
sizeY_m;
166 for (
unsigned int row = 0; row <
sizeY_m; ++row) {
167 std::swap(image[pixel1++], image[pixel2++]);
173 for (
unsigned int col = 0; col <
sizeX_m; ++col) {
174 unsigned int pixel1 = col *
sizeY_m;
175 unsigned int pixel2 = (col + 1) *
sizeY_m - 1;
177 unsigned int row2 =
sizeY_m - 1;
178 for (
unsigned int row1 = 0; row1 < row2; ++row1, --row2) {
179 std::swap(image[pixel1++], image[pixel2--]);
186 for (
unsigned int col = 0; col <
sizeX_m; ++col) {
187 for (
unsigned int row = 0; row <
sizeY_m; ++row) {
188 unsigned int pixel1 = col *
sizeY_m + row;
189 unsigned int pixel2 = row *
sizeX_m + col;
191 copy[pixel2] = image[pixel1];
195 for (
unsigned int pixel = 0; pixel <
sizeX_m *
sizeY_m; ++pixel) {
196 image[pixel] = copy[pixel];
207 hsize_t idxNW = idxN - 1, idxNE = idxN + 1;
208 hsize_t idxW = idx - 1, idxE = idx + 1;
210 hsize_t idxSW = idxS - 1, idxSE = idxS + 1;
212 for (hsize_t i = 1; i <
sizeX_m - 1; ++i) {
213 for (hsize_t j = 1; j <
sizeY_m - 1; ++j) {
214 if (image[idx] > 0) {
215 if (image[idxNW] == 0 && image[idxN] == 0 && image[idxNE] == 0 && image[idxW] == 0
216 && image[idxE] == 0 && image[idxSW] == 0 && image[idxS] == 0
217 && image[idxSE] == 0) {
238 unsigned int pixel = 0;
239 for (
unsigned int col = 0; col <
sizeX_m; ++col) {
240 for (
unsigned int row = 0; row <
sizeY_m; ++row, ++pixel) {
241 double val = (double(image[pixel]) / profileMax - intensityCut) / (1.0 - intensityCut);
243 val = std::max(0.0, val);
244 image[pixel] = std::round(val * profileMax);
250 double totalMass = 0.0;
254 unsigned int pixel = 0;
255 for (
unsigned int col = 0; col <
sizeX_m; ++col) {
256 for (
unsigned int row = 0; row <
sizeY_m; ++row, ++pixel) {
257 double val = image[pixel];
275 unsigned int histSizeX =
sizeX_m;
276 unsigned int histSizeY =
sizeY_m;
285 unsigned int pixel = 0;
286 for (
unsigned int col = 0; col <
sizeX_m; ++col) {
288 x = x + 0.5 * binSizeX;
291 for (
unsigned int row = 0; row <
sizeY_m; ++row, ++pixel) {
292 double val = image[pixel];
294 y = y + 0.5 * binSizeY;
316 *ippl::Info << level3
318 "*******************************************************************************"
320 *ippl::Info << level3 <<
"* LASER PROFILE \n";
321 *ippl::Info << level3 <<
"* size = " <<
sizeX_m <<
" x " <<
sizeY_m <<
" pixels \n";
322 *ippl::Info << level3 <<
"* center of mass: x = " <<
centerMass_m(0)
329 "**********************************************************************************"
338 out <<
"P2" << std::endl;
342 for (
unsigned int j = 0; j <
sizeY_m; ++j) {
343 for (
unsigned int i = 0; i <
sizeX_m; ++i) {
344 out << image[i *
sizeY_m + j] <<
" ";
353 FILE* fh = std::fopen(fname.c_str(),
"w");
362 std::ofstream fh(fname);
365 for (
int i = 0; i < 1000000; i++) {
367 fh << x <<
"\t" << y <<
"\n";
382 unsigned short maxIntensity = 0;
384 for (
unsigned int i = 0; i < numberPixels; i++) {
385 if (image[i] > maxIntensity) maxIntensity = image[i];
ippl::Vector< T, Dim > Vector_t
gsl_histogram2d * gsl_histogram2d_alloc(size_t nx, size_t ny)
Allocate a 2D histogram with nx by ny bins.
void gsl_histogram2d_pdf_free(gsl_histogram2d_pdf *p)
Free a 2D histogram PDF.
void gsl_histogram2d_fprintf(FILE *fh, const gsl_histogram2d *h, const char *xformat, const char *yformat)
Print the 2D histogram in a simple text table.
void gsl_histogram2d_accumulate(gsl_histogram2d *h, double x, double y, double weight)
Accumulate a weighted sample into the 2D histogram.
void gsl_histogram2d_pdf_init(gsl_histogram2d_pdf *p, const gsl_histogram2d *h)
Initialize a PDF from a histogram.
void gsl_histogram2d_pdf_sample(const gsl_histogram2d_pdf *p, double u, double v, double *x, double *y)
Sample from the PDF using two uniform variates.
void gsl_histogram2d_set_ranges_uniform(gsl_histogram2d *h, double xmin, double xmax, double ymin, double ymax)
Set uniform x/y bin edges.
gsl_histogram2d_pdf * gsl_histogram2d_pdf_alloc(size_t, size_t)
Allocate a 2D histogram PDF.
void gsl_histogram2d_free(gsl_histogram2d *h)
Free a 2D histogram.
double gsl_rng_uniform(gsl_rng *rng)
void gsl_rng_free(gsl_rng *rng)
gsl_rng * gsl_rng_alloc(const gsl_rng_type *)
void normalizeProfileData(double intensityCut, unsigned short *image)
Vector_t< double, 3 > centerMass_m
void getXY(double &x, double &y)
void saveData(const std::string &fname, unsigned short *image)
void fillHistrogram(unsigned short *image)
void flipY(unsigned short *image)
LaserProfile(const std::string &fileName, const std::string &imageName, double intensityCut, short flags)
void flipX(unsigned short *image)
gsl_histogram2d * hist2d_m
unsigned short * readPGMFile(const std::string &fileName)
unsigned short * readFile(const std::string &fileName, const std::string &imageName)
void swapXY(unsigned short *image)
unsigned short getProfileMax(unsigned short *image)
unsigned short * readHDF5File(const std::string &fileName, const std::string &imageName)
void computeProfileStatistics(unsigned short *image)
gsl_histogram2d_pdf * pdf_m
void filterSpikes(unsigned short *image)
Vector_t< double, 3 > standardDeviation_m
static OpalData * getInstance()
std::string getAuxiliaryOutputDirectory() const
get the name of the the additional data directory
unsigned int getWidth() const
unsigned int getHeight() const
unsigned short getPixel(unsigned int i, unsigned int j) const
size_t nx() const
Number of x bins.
size_t ny() const
Number of y bins.
std::string combineFilePath(std::initializer_list< std::string > ilist)