mirror of
https://github.com/slsdetectorgroup/aare.git
synced 2025-06-15 00:37:13 +02:00
Added fitting, fixed roi etc (#129)
Co-authored-by: Patrick <patrick.sieberer@psi.ch> Co-authored-by: JulianHeymes <julian.heymes@psi.ch>
This commit is contained in:
@ -20,6 +20,12 @@ ClusterFile::ClusterFile(const std::filesystem::path &fname, size_t chunk_size,
|
||||
throw std::runtime_error("Could not open file for writing: " +
|
||||
fname.string());
|
||||
}
|
||||
} else if (mode == "a") {
|
||||
fp = fopen(fname.c_str(), "ab");
|
||||
if (!fp) {
|
||||
throw std::runtime_error("Could not open file for appending: " +
|
||||
fname.string());
|
||||
}
|
||||
} else {
|
||||
throw std::runtime_error("Unsupported mode: " + mode);
|
||||
}
|
||||
@ -34,35 +40,35 @@ void ClusterFile::close() {
|
||||
}
|
||||
}
|
||||
|
||||
void ClusterFile::write_frame(int32_t frame_number,
|
||||
const ClusterVector<int32_t> &clusters) {
|
||||
if (m_mode != "w") {
|
||||
void ClusterFile::write_frame(const ClusterVector<int32_t> &clusters) {
|
||||
if (m_mode != "w" && m_mode != "a") {
|
||||
throw std::runtime_error("File not opened for writing");
|
||||
}
|
||||
if (!(clusters.cluster_size_x() == 3) &&
|
||||
!(clusters.cluster_size_y() == 3)) {
|
||||
throw std::runtime_error("Only 3x3 clusters are supported");
|
||||
}
|
||||
int32_t frame_number = clusters.frame_number();
|
||||
fwrite(&frame_number, sizeof(frame_number), 1, fp);
|
||||
uint32_t n_clusters = clusters.size();
|
||||
fwrite(&n_clusters, sizeof(n_clusters), 1, fp);
|
||||
fwrite(clusters.data(), clusters.element_offset(), clusters.size(), fp);
|
||||
// write clusters
|
||||
// fwrite(clusters.data(), sizeof(Cluster), clusters.size(), fp);
|
||||
fwrite(clusters.data(), clusters.item_size(), clusters.size(), fp);
|
||||
}
|
||||
|
||||
std::vector<Cluster3x3> ClusterFile::read_clusters(size_t n_clusters) {
|
||||
ClusterVector<int32_t> ClusterFile::read_clusters(size_t n_clusters) {
|
||||
if (m_mode != "r") {
|
||||
throw std::runtime_error("File not opened for reading");
|
||||
}
|
||||
std::vector<Cluster3x3> clusters(n_clusters);
|
||||
|
||||
ClusterVector<int32_t> clusters(3,3, n_clusters);
|
||||
|
||||
int32_t iframe = 0; // frame number needs to be 4 bytes!
|
||||
size_t nph_read = 0;
|
||||
uint32_t nn = m_num_left;
|
||||
uint32_t nph = m_num_left; // number of clusters in frame needs to be 4
|
||||
|
||||
auto buf = reinterpret_cast<Cluster3x3 *>(clusters.data());
|
||||
// auto buf = reinterpret_cast<Cluster3x3 *>(clusters.data());
|
||||
auto buf = clusters.data();
|
||||
// if there are photons left from previous frame read them first
|
||||
if (nph) {
|
||||
if (nph > n_clusters) {
|
||||
@ -72,8 +78,8 @@ std::vector<Cluster3x3> ClusterFile::read_clusters(size_t n_clusters) {
|
||||
} else {
|
||||
nn = nph;
|
||||
}
|
||||
nph_read += fread(reinterpret_cast<void *>(buf + nph_read),
|
||||
sizeof(Cluster3x3), nn, fp);
|
||||
nph_read += fread((buf + nph_read*clusters.item_size()),
|
||||
clusters.item_size(), nn, fp);
|
||||
m_num_left = nph - nn; // write back the number of photons left
|
||||
}
|
||||
|
||||
@ -87,8 +93,8 @@ std::vector<Cluster3x3> ClusterFile::read_clusters(size_t n_clusters) {
|
||||
else
|
||||
nn = nph;
|
||||
|
||||
nph_read += fread(reinterpret_cast<void *>(buf + nph_read),
|
||||
sizeof(Cluster3x3), nn, fp);
|
||||
nph_read += fread((buf + nph_read*clusters.item_size()),
|
||||
clusters.item_size(), nn, fp);
|
||||
m_num_left = nph - nn;
|
||||
}
|
||||
if (nph_read >= n_clusters)
|
||||
@ -102,7 +108,7 @@ std::vector<Cluster3x3> ClusterFile::read_clusters(size_t n_clusters) {
|
||||
return clusters;
|
||||
}
|
||||
|
||||
std::vector<Cluster3x3> ClusterFile::read_frame(int32_t &out_fnum) {
|
||||
ClusterVector<int32_t> ClusterFile::read_frame() {
|
||||
if (m_mode != "r") {
|
||||
throw std::runtime_error("File not opened for reading");
|
||||
}
|
||||
@ -110,8 +116,8 @@ std::vector<Cluster3x3> ClusterFile::read_frame(int32_t &out_fnum) {
|
||||
throw std::runtime_error(
|
||||
"There are still photons left in the last frame");
|
||||
}
|
||||
|
||||
if (fread(&out_fnum, sizeof(out_fnum), 1, fp) != 1) {
|
||||
int32_t frame_number;
|
||||
if (fread(&frame_number, sizeof(frame_number), 1, fp) != 1) {
|
||||
throw std::runtime_error("Could not read frame number");
|
||||
}
|
||||
|
||||
@ -119,158 +125,163 @@ std::vector<Cluster3x3> ClusterFile::read_frame(int32_t &out_fnum) {
|
||||
if (fread(&n_clusters, sizeof(n_clusters), 1, fp) != 1) {
|
||||
throw std::runtime_error("Could not read number of clusters");
|
||||
}
|
||||
std::vector<Cluster3x3> clusters(n_clusters);
|
||||
// std::vector<Cluster3x3> clusters(n_clusters);
|
||||
ClusterVector<int32_t> clusters(3, 3, n_clusters);
|
||||
clusters.set_frame_number(frame_number);
|
||||
|
||||
if (fread(clusters.data(), sizeof(Cluster3x3), n_clusters, fp) !=
|
||||
if (fread(clusters.data(), clusters.item_size(), n_clusters, fp) !=
|
||||
static_cast<size_t>(n_clusters)) {
|
||||
throw std::runtime_error("Could not read clusters");
|
||||
}
|
||||
clusters.resize(n_clusters);
|
||||
return clusters;
|
||||
}
|
||||
|
||||
std::vector<Cluster3x3> ClusterFile::read_cluster_with_cut(size_t n_clusters,
|
||||
double *noise_map,
|
||||
int nx, int ny) {
|
||||
if (m_mode != "r") {
|
||||
throw std::runtime_error("File not opened for reading");
|
||||
}
|
||||
std::vector<Cluster3x3> clusters(n_clusters);
|
||||
// size_t read_clusters_with_cut(FILE *fp, size_t n_clusters, Cluster *buf,
|
||||
// uint32_t *n_left, double *noise_map, int
|
||||
// nx, int ny) {
|
||||
int iframe = 0;
|
||||
// uint32_t nph = *n_left;
|
||||
uint32_t nph = m_num_left;
|
||||
// uint32_t nn = *n_left;
|
||||
uint32_t nn = m_num_left;
|
||||
size_t nph_read = 0;
|
||||
|
||||
int32_t t2max, tot1;
|
||||
int32_t tot3;
|
||||
// Cluster *ptr = buf;
|
||||
Cluster3x3 *ptr = clusters.data();
|
||||
int good = 1;
|
||||
double noise;
|
||||
// read photons left from previous frame
|
||||
if (noise_map)
|
||||
printf("Using noise map\n");
|
||||
// std::vector<Cluster3x3> ClusterFile::read_cluster_with_cut(size_t n_clusters,
|
||||
// double *noise_map,
|
||||
// int nx, int ny) {
|
||||
// if (m_mode != "r") {
|
||||
// throw std::runtime_error("File not opened for reading");
|
||||
// }
|
||||
// std::vector<Cluster3x3> clusters(n_clusters);
|
||||
// // size_t read_clusters_with_cut(FILE *fp, size_t n_clusters, Cluster *buf,
|
||||
// // uint32_t *n_left, double *noise_map, int
|
||||
// // nx, int ny) {
|
||||
// int iframe = 0;
|
||||
// // uint32_t nph = *n_left;
|
||||
// uint32_t nph = m_num_left;
|
||||
// // uint32_t nn = *n_left;
|
||||
// uint32_t nn = m_num_left;
|
||||
// size_t nph_read = 0;
|
||||
|
||||
if (nph) {
|
||||
if (nph > n_clusters) {
|
||||
// if we have more photons left in the frame then photons to
|
||||
// read we read directly the requested number
|
||||
nn = n_clusters;
|
||||
} else {
|
||||
nn = nph;
|
||||
}
|
||||
for (size_t iph = 0; iph < nn; iph++) {
|
||||
// read photons 1 by 1
|
||||
size_t n_read =
|
||||
fread(reinterpret_cast<void *>(ptr), sizeof(Cluster3x3), 1, fp);
|
||||
if (n_read != 1) {
|
||||
clusters.resize(nph_read);
|
||||
return clusters;
|
||||
}
|
||||
// TODO! error handling on read
|
||||
good = 1;
|
||||
if (noise_map) {
|
||||
if (ptr->x >= 0 && ptr->x < nx && ptr->y >= 0 && ptr->y < ny) {
|
||||
tot1 = ptr->data[4];
|
||||
analyze_cluster(*ptr, &t2max, &tot3, NULL, NULL, NULL, NULL,
|
||||
NULL);
|
||||
noise = noise_map[ptr->y * nx + ptr->x];
|
||||
if (tot1 > noise || t2max > 2 * noise || tot3 > 3 * noise) {
|
||||
;
|
||||
} else {
|
||||
good = 0;
|
||||
printf("%d %d %f %d %d %d\n", ptr->x, ptr->y, noise,
|
||||
tot1, t2max, tot3);
|
||||
}
|
||||
} else {
|
||||
printf("Bad pixel number %d %d\n", ptr->x, ptr->y);
|
||||
good = 0;
|
||||
}
|
||||
}
|
||||
if (good) {
|
||||
ptr++;
|
||||
nph_read++;
|
||||
}
|
||||
(m_num_left)--;
|
||||
if (nph_read >= n_clusters)
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (nph_read < n_clusters) {
|
||||
// // keep on reading frames and photons until reaching
|
||||
// n_clusters
|
||||
while (fread(&iframe, sizeof(iframe), 1, fp)) {
|
||||
// // printf("%d\n",nph_read);
|
||||
// int32_t t2max, tot1;
|
||||
// int32_t tot3;
|
||||
// // Cluster *ptr = buf;
|
||||
// Cluster3x3 *ptr = clusters.data();
|
||||
// int good = 1;
|
||||
// double noise;
|
||||
// // read photons left from previous frame
|
||||
// if (noise_map)
|
||||
// printf("Using noise map\n");
|
||||
|
||||
if (fread(&nph, sizeof(nph), 1, fp)) {
|
||||
// // printf("** %d\n",nph);
|
||||
m_num_left = nph;
|
||||
for (size_t iph = 0; iph < nph; iph++) {
|
||||
// // read photons 1 by 1
|
||||
size_t n_read = fread(reinterpret_cast<void *>(ptr),
|
||||
sizeof(Cluster3x3), 1, fp);
|
||||
if (n_read != 1) {
|
||||
clusters.resize(nph_read);
|
||||
return clusters;
|
||||
// return nph_read;
|
||||
}
|
||||
good = 1;
|
||||
if (noise_map) {
|
||||
if (ptr->x >= 0 && ptr->x < nx && ptr->y >= 0 &&
|
||||
ptr->y < ny) {
|
||||
tot1 = ptr->data[4];
|
||||
analyze_cluster(*ptr, &t2max, &tot3, NULL, NULL,
|
||||
NULL, NULL, NULL);
|
||||
// noise = noise_map[ptr->y * nx + ptr->x];
|
||||
noise = noise_map[ptr->y + ny * ptr->x];
|
||||
if (tot1 > noise || t2max > 2 * noise ||
|
||||
tot3 > 3 * noise) {
|
||||
;
|
||||
} else
|
||||
good = 0;
|
||||
} else {
|
||||
printf("Bad pixel number %d %d\n", ptr->x, ptr->y);
|
||||
good = 0;
|
||||
}
|
||||
}
|
||||
if (good) {
|
||||
ptr++;
|
||||
nph_read++;
|
||||
}
|
||||
(m_num_left)--;
|
||||
if (nph_read >= n_clusters)
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (nph_read >= n_clusters)
|
||||
break;
|
||||
}
|
||||
}
|
||||
// printf("%d\n",nph_read);
|
||||
clusters.resize(nph_read);
|
||||
return clusters;
|
||||
}
|
||||
// if (nph) {
|
||||
// if (nph > n_clusters) {
|
||||
// // if we have more photons left in the frame then photons to
|
||||
// // read we read directly the requested number
|
||||
// nn = n_clusters;
|
||||
// } else {
|
||||
// nn = nph;
|
||||
// }
|
||||
// for (size_t iph = 0; iph < nn; iph++) {
|
||||
// // read photons 1 by 1
|
||||
// size_t n_read =
|
||||
// fread(reinterpret_cast<void *>(ptr), sizeof(Cluster3x3), 1, fp);
|
||||
// if (n_read != 1) {
|
||||
// clusters.resize(nph_read);
|
||||
// return clusters;
|
||||
// }
|
||||
// // TODO! error handling on read
|
||||
// good = 1;
|
||||
// if (noise_map) {
|
||||
// if (ptr->x >= 0 && ptr->x < nx && ptr->y >= 0 && ptr->y < ny) {
|
||||
// tot1 = ptr->data[4];
|
||||
// analyze_cluster(*ptr, &t2max, &tot3, NULL, NULL, NULL, NULL,
|
||||
// NULL);
|
||||
// noise = noise_map[ptr->y * nx + ptr->x];
|
||||
// if (tot1 > noise || t2max > 2 * noise || tot3 > 3 * noise) {
|
||||
// ;
|
||||
// } else {
|
||||
// good = 0;
|
||||
// printf("%d %d %f %d %d %d\n", ptr->x, ptr->y, noise,
|
||||
// tot1, t2max, tot3);
|
||||
// }
|
||||
// } else {
|
||||
// printf("Bad pixel number %d %d\n", ptr->x, ptr->y);
|
||||
// good = 0;
|
||||
// }
|
||||
// }
|
||||
// if (good) {
|
||||
// ptr++;
|
||||
// nph_read++;
|
||||
// }
|
||||
// (m_num_left)--;
|
||||
// if (nph_read >= n_clusters)
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// if (nph_read < n_clusters) {
|
||||
// // // keep on reading frames and photons until reaching
|
||||
// // n_clusters
|
||||
// while (fread(&iframe, sizeof(iframe), 1, fp)) {
|
||||
// // // printf("%d\n",nph_read);
|
||||
|
||||
// if (fread(&nph, sizeof(nph), 1, fp)) {
|
||||
// // // printf("** %d\n",nph);
|
||||
// m_num_left = nph;
|
||||
// for (size_t iph = 0; iph < nph; iph++) {
|
||||
// // // read photons 1 by 1
|
||||
// size_t n_read = fread(reinterpret_cast<void *>(ptr),
|
||||
// sizeof(Cluster3x3), 1, fp);
|
||||
// if (n_read != 1) {
|
||||
// clusters.resize(nph_read);
|
||||
// return clusters;
|
||||
// // return nph_read;
|
||||
// }
|
||||
// good = 1;
|
||||
// if (noise_map) {
|
||||
// if (ptr->x >= 0 && ptr->x < nx && ptr->y >= 0 &&
|
||||
// ptr->y < ny) {
|
||||
// tot1 = ptr->data[4];
|
||||
// analyze_cluster(*ptr, &t2max, &tot3, NULL, NULL,
|
||||
// NULL, NULL, NULL);
|
||||
// // noise = noise_map[ptr->y * nx + ptr->x];
|
||||
// noise = noise_map[ptr->y + ny * ptr->x];
|
||||
// if (tot1 > noise || t2max > 2 * noise ||
|
||||
// tot3 > 3 * noise) {
|
||||
// ;
|
||||
// } else
|
||||
// good = 0;
|
||||
// } else {
|
||||
// printf("Bad pixel number %d %d\n", ptr->x, ptr->y);
|
||||
// good = 0;
|
||||
// }
|
||||
// }
|
||||
// if (good) {
|
||||
// ptr++;
|
||||
// nph_read++;
|
||||
// }
|
||||
// (m_num_left)--;
|
||||
// if (nph_read >= n_clusters)
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// if (nph_read >= n_clusters)
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// // printf("%d\n",nph_read);
|
||||
// clusters.resize(nph_read);
|
||||
// return clusters;
|
||||
// }
|
||||
|
||||
NDArray<double, 2> calculate_eta2(ClusterVector<int> &clusters) {
|
||||
NDArray<double, 2> eta2({clusters.size(), 2});
|
||||
//TOTO! make work with 2x2 clusters
|
||||
NDArray<double, 2> eta2({static_cast<int64_t>(clusters.size()), 2});
|
||||
for (size_t i = 0; i < clusters.size(); i++) {
|
||||
// int32_t t2;
|
||||
// auto* ptr = reinterpret_cast<int32_t*> (clusters.element_ptr(i) + 2 *
|
||||
// sizeof(int16_t)); analyze_cluster(clusters.at<Cluster3x3>(i), &t2,
|
||||
// nullptr, nullptr, &eta2(i,0), &eta2(i,1) , nullptr, nullptr);
|
||||
auto [x, y] = calculate_eta2(clusters.at<Cluster3x3>(i));
|
||||
eta2(i, 0) = x;
|
||||
eta2(i, 1) = y;
|
||||
auto e = calculate_eta2(clusters.at<Cluster3x3>(i));
|
||||
eta2(i, 0) = e.x;
|
||||
eta2(i, 1) = e.y;
|
||||
}
|
||||
return eta2;
|
||||
}
|
||||
|
||||
std::array<double, 2> calculate_eta2(Cluster3x3 &cl) {
|
||||
std::array<double, 2> eta2{};
|
||||
/**
|
||||
* @brief Calculate the eta2 values for a 3x3 cluster and return them in a Eta2 struct
|
||||
* containing etay, etax and the corner of the cluster.
|
||||
*/
|
||||
Eta2 calculate_eta2(Cluster3x3 &cl) {
|
||||
Eta2 eta{};
|
||||
|
||||
std::array<int32_t, 4> tot2;
|
||||
tot2[0] = cl.data[0] + cl.data[1] + cl.data[3] + cl.data[4];
|
||||
@ -283,39 +294,43 @@ std::array<double, 2> calculate_eta2(Cluster3x3 &cl) {
|
||||
switch (c) {
|
||||
case cBottomLeft:
|
||||
if ((cl.data[3] + cl.data[4]) != 0)
|
||||
eta2[0] =
|
||||
eta.x =
|
||||
static_cast<double>(cl.data[4]) / (cl.data[3] + cl.data[4]);
|
||||
if ((cl.data[1] + cl.data[4]) != 0)
|
||||
eta2[1] =
|
||||
eta.y =
|
||||
static_cast<double>(cl.data[4]) / (cl.data[1] + cl.data[4]);
|
||||
eta.c = cBottomLeft;
|
||||
break;
|
||||
case cBottomRight:
|
||||
if ((cl.data[2] + cl.data[5]) != 0)
|
||||
eta2[0] =
|
||||
eta.x =
|
||||
static_cast<double>(cl.data[5]) / (cl.data[4] + cl.data[5]);
|
||||
if ((cl.data[1] + cl.data[4]) != 0)
|
||||
eta2[1] =
|
||||
eta.y =
|
||||
static_cast<double>(cl.data[4]) / (cl.data[1] + cl.data[4]);
|
||||
eta.c = cBottomRight;
|
||||
break;
|
||||
case cTopLeft:
|
||||
if ((cl.data[7] + cl.data[4]) != 0)
|
||||
eta2[0] =
|
||||
eta.x =
|
||||
static_cast<double>(cl.data[4]) / (cl.data[3] + cl.data[4]);
|
||||
if ((cl.data[7] + cl.data[4]) != 0)
|
||||
eta2[1] =
|
||||
eta.y =
|
||||
static_cast<double>(cl.data[7]) / (cl.data[7] + cl.data[4]);
|
||||
eta.c = cTopLeft;
|
||||
break;
|
||||
case cTopRight:
|
||||
if ((cl.data[5] + cl.data[4]) != 0)
|
||||
eta2[0] =
|
||||
eta.x =
|
||||
static_cast<double>(cl.data[5]) / (cl.data[5] + cl.data[4]);
|
||||
if ((cl.data[7] + cl.data[4]) != 0)
|
||||
eta2[1] =
|
||||
eta.y =
|
||||
static_cast<double>(cl.data[7]) / (cl.data[7] + cl.data[4]);
|
||||
eta.c = cTopRight;
|
||||
break;
|
||||
// default:;
|
||||
// no default to allow compiler to warn about missing cases
|
||||
}
|
||||
return eta2;
|
||||
return eta;
|
||||
}
|
||||
|
||||
int analyze_cluster(Cluster3x3 &cl, int32_t *t2, int32_t *t3, char *quad,
|
||||
|
Reference in New Issue
Block a user