18#include "Utility/Inform.h"
20#include <boost/filesystem.hpp>
30 const std::string& fileName,
const std::string& imageName,
double intensityCut,
short flags)
38 unsigned short* image =
readFile(fileName, imageName);
68#ifdef TESTLASEREMISSION
77 gsl_histogram2d_pdf_free(
pdf_m);
83 namespace fs = boost::filesystem;
84 if (!fs::exists(fileName)) {
86 "LaserProfile::readFile",
"given file '" + fileName +
"' does not exist");
89 size_t npos = fileName.find_last_of(
'.');
90 std::string ext = fileName.substr(npos + 1);
92 unsigned short* image;
109 unsigned int idx = 0;
110 for (
unsigned int i = 0; i <
sizeX_m; ++i) {
111 for (
unsigned int j = 0; j <
sizeY_m; ++j, ++idx) {
120 const std::string& fileName,
const std::string& imageName) {
121 hid_t h5 = H5Fopen(fileName.c_str(), H5F_ACC_RDONLY, H5P_DEFAULT);
122 hid_t group = H5Gopen2(h5, imageName.c_str(), H5P_DEFAULT);
126 "LaserProfile::readFile",
"given image name '" + imageName +
"' does not exist");
130 hid_t dataSet = H5Dopen2(group,
"CameraData", H5P_DEFAULT);
133 throw OpalException(
"LaserProfile::readFile",
"data set 'CameraData' does not exist");
136 hid_t dataSetSpace = H5Dget_space(dataSet);
137 H5Sget_simple_extent_dims(dataSetSpace, dim,
nullptr);
138 hid_t filespace = H5Screate_simple(2, dim,
nullptr);
143 hsize_t startHyperslab[] = {0, 0};
146 H5Sselect_hyperslab(filespace, H5S_SELECT_SET, startHyperslab,
nullptr, blockCount,
nullptr);
149 hid_t mem = H5Screate_simple(2, blockCount,
nullptr);
151 H5Dread(dataSet, H5T_NATIVE_USHORT, mem, filespace, H5P_DEFAULT, image);
155 H5Dclose(dataSetSpace);
164 unsigned int col2 =
sizeX_m - 1;
165 for (
unsigned int col1 = 0; col1 < col2; ++col1, --col2) {
166 unsigned int pixel1 = col1 *
sizeY_m;
167 unsigned int pixel2 = col2 *
sizeY_m;
168 for (
unsigned int row = 0; row <
sizeY_m; ++row) {
169 std::swap(image[pixel1++], image[pixel2++]);
175 for (
unsigned int col = 0; col <
sizeX_m; ++col) {
176 unsigned int pixel1 = col *
sizeY_m;
177 unsigned int pixel2 = (col + 1) *
sizeY_m - 1;
179 unsigned int row2 =
sizeY_m - 1;
180 for (
unsigned int row1 = 0; row1 < row2; ++row1, --row2) {
181 std::swap(image[pixel1++], image[pixel2--]);
188 for (
unsigned int col = 0; col <
sizeX_m; ++col) {
189 for (
unsigned int row = 0; row <
sizeY_m; ++row) {
190 unsigned int pixel1 = col *
sizeY_m + row;
191 unsigned int pixel2 = row *
sizeX_m + col;
193 copy[pixel2] = image[pixel1];
197 for (
unsigned int pixel = 0; pixel <
sizeX_m *
sizeY_m; ++pixel) {
198 image[pixel] = copy[pixel];
209 hsize_t idxNW = idxN - 1, idxNE = idxN + 1;
210 hsize_t idxW = idx - 1, idxE = idx + 1;
212 hsize_t idxSW = idxS - 1, idxSE = idxS + 1;
214 for (hsize_t i = 1; i <
sizeX_m - 1; ++i) {
215 for (hsize_t j = 1; j <
sizeY_m - 1; ++j) {
216 if (image[idx] > 0) {
217 if (image[idxNW] == 0 && image[idxN] == 0 && image[idxNE] == 0 && image[idxW] == 0
218 && image[idxE] == 0 && image[idxSW] == 0 && image[idxS] == 0
219 && image[idxSE] == 0) {
240 unsigned int pixel = 0;
241 for (
unsigned int col = 0; col <
sizeX_m; ++col) {
242 for (
unsigned int row = 0; row <
sizeY_m; ++row, ++pixel) {
243 double val = (double(image[pixel]) / profileMax - intensityCut) / (1.0 - intensityCut);
245 val = std::max(0.0, val);
246 image[pixel] = std::round(val * profileMax);
252 double totalMass = 0.0;
256 unsigned int pixel = 0;
257 for (
unsigned int col = 0; col <
sizeX_m; ++col) {
258 for (
unsigned int row = 0; row <
sizeY_m; ++row, ++pixel) {
259 double val = image[pixel];
277 unsigned int histSizeX =
sizeX_m;
278 unsigned int histSizeY =
sizeY_m;
284 hist2d_m = gsl_histogram2d_alloc(histSizeX, histSizeY);
285 gsl_histogram2d_set_ranges_uniform(
hist2d_m, -histRangeX, histRangeX, -histRangeY, histRangeY);
287 unsigned int pixel = 0;
288 for (
unsigned int col = 0; col <
sizeX_m; ++col) {
290 x = x + 0.5 * binSizeX;
293 for (
unsigned int row = 0; row <
sizeY_m; ++row, ++pixel) {
294 double val = image[pixel];
296 y = y + 0.5 * binSizeY;
300 gsl_histogram2d_accumulate(
hist2d_m, x, y, val);
311 const gsl_rng_type*
T = gsl_rng_default;
314 pdf_m = gsl_histogram2d_pdf_alloc(histSizeX, histSizeY);
322 "**********************************************************************************\n";
323 *ippl::Info << level3 <<
"* LASER PROFILE \n";
324 *ippl::Info << level3 <<
"* size = " <<
sizeX_m <<
" x " <<
sizeY_m <<
" pixels \n";
325 *ippl::Info << level3 <<
"* center of mass: x = " <<
centerMass_m(0)
331 <<
"* **********************************************************************************"
339 out <<
"P2" << std::endl;
343 for (
unsigned int j = 0; j <
sizeY_m; ++j) {
344 for (
unsigned int i = 0; i <
sizeX_m; ++i) {
345 out << image[i *
sizeY_m + j] <<
" ";
354 FILE* fh = std::fopen(fname.c_str(),
"w");
355 gsl_histogram2d_fprintf(fh,
hist2d_m,
"%g",
"%g");
363 std::ofstream fh(fname);
366 for (
int i = 0; i < 1000000; i++) {
368 fh << x <<
"\t" << y <<
"\n";
375 double u = gsl_rng_uniform(
rng_m);
376 double v = gsl_rng_uniform(
rng_m);
377 gsl_histogram2d_pdf_sample(
pdf_m, u, v, &x, &y);
383 unsigned short maxIntensity = 0;
385 for (
unsigned int i = 0; i < numberPixels; i++) {
386 if (image[i] > maxIntensity)
387 maxIntensity = image[i];
ippl::Vector< T, Dim > Vector_t
std::string combineFilePath(std::initializer_list< std::string > ilist)
static OpalData * getInstance()
std::string getAuxiliaryOutputDirectory() const
get the name of the the additional data directory
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
The base class for all OPAL exceptions.
unsigned int getWidth() const
unsigned int getHeight() const
unsigned short getPixel(unsigned int i, unsigned int j) const