mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-06-13 05:17:13 +02:00
Adapt get and writeImage methods to storage cell data
This commit is contained in:
@ -213,10 +213,9 @@ int main(int argc, char *argv[]) {
|
|||||||
std::time(&end_time);
|
std::time(&end_time);
|
||||||
std::cout << std::ctime(&end_time) << std::endl;
|
std::cout << std::ctime(&end_time) << std::endl;
|
||||||
|
|
||||||
|
// Validate number of threads for number of storage cells (if applicable)
|
||||||
int nThreads = nthreads;
|
int nThreads = nthreads;
|
||||||
int nSC = 1;
|
int nSC = 1;
|
||||||
|
|
||||||
// Validate number of threads for number of storage cells (if applicable)
|
|
||||||
// Determine the dimensions of the dataset from the first datafile
|
// Determine the dimensions of the dataset from the first datafile
|
||||||
auto firstfileh5 = std::make_unique<HDF5File>();
|
auto firstfileh5 = std::make_unique<HDF5File>();
|
||||||
firstfileh5->SetFrameIndexPath(frameindexpath);
|
firstfileh5->SetFrameIndexPath(frameindexpath);
|
||||||
@ -233,7 +232,7 @@ int main(int argc, char *argv[]) {
|
|||||||
firstfileh5->CloseResources();
|
firstfileh5->CloseResources();
|
||||||
} else {
|
} else {
|
||||||
std::cerr << "Could not open data file " << filenames[0]
|
std::cerr << "Could not open data file " << filenames[0]
|
||||||
<< " for reading " << std::endl;
|
<< " for validating rank " << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
multiThreadedCountingDetector* mt =
|
multiThreadedCountingDetector* mt =
|
||||||
@ -298,7 +297,7 @@ int main(int argc, char *argv[]) {
|
|||||||
// increment (round-robin) the internal thread counter for that storage cell
|
// increment (round-robin) the internal thread counter for that storage cell
|
||||||
mt->nextThread(storageCell);
|
mt->nextThread(storageCell);
|
||||||
// get a free memory address from fifoFree of the active storage cell for the next read operation
|
// get a free memory address from fifoFree of the active storage cell for the next read operation
|
||||||
mt->popFree(buff,storageCell);
|
mt->popFree(buff, storageCell);
|
||||||
/* NOTE: the buff that was popped free from the current thread, will be (likely) pushed into
|
/* NOTE: the buff that was popped free from the current thread, will be (likely) pushed into
|
||||||
* the fifoData of a different thread in the next iteration of the loop! */
|
* the fifoData of a different thread in the next iteration of the loop! */
|
||||||
|
|
||||||
@ -341,25 +340,24 @@ int main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
FILE* of = nullptr;
|
FILE* of = nullptr;
|
||||||
|
|
||||||
//NOTE THAT THE DATA FILES HAVE TO BE IN THE RIGHT ORDER SO THAT PEDESTAL TRACKING WORKS!
|
|
||||||
for (unsigned int ifile = 0; ifile != filenames.size(); ++ifile) {
|
for (unsigned int ifile = 0; ifile != filenames.size(); ++ifile) {
|
||||||
std::cout << "DATA ";
|
std::cout << "DATA ";
|
||||||
std::string fsuffix{};
|
std::string fsuffix{};
|
||||||
std::string const fprefix( getRootString(filenames[ifile]) );
|
std::string const fprefix( getRootString(filenames[ifile]) );
|
||||||
std::string imgfname( createFileName( outdir, fprefix, fsuffix, "tiff" ) );
|
std::string imgfname( createFileName( outdir, fprefix, fsuffix, "tiff" ) );
|
||||||
std::string const cfname( createFileName( outdir, fprefix, fsuffix, "clust" ) );
|
std::string const cfname( createFileName( outdir, fprefix, fsuffix, "clust" ) );
|
||||||
std::cout << filenames[ifile] << " ";
|
std::cout << filenames[ifile] << " ";
|
||||||
std::cout << imgfname << std::endl;
|
std::cout << imgfname << std::endl;
|
||||||
std::time(&end_time);
|
std::time(&end_time);
|
||||||
std::cout << std::ctime(&end_time) << std::endl;
|
std::cout << std::ctime(&end_time) << std::endl;
|
||||||
|
|
||||||
//HDF5File fileh5;
|
//HDF5File fileh5;
|
||||||
auto fileh5 = std::make_unique<HDF5File>();
|
auto fileh5 = std::make_unique<HDF5File>();
|
||||||
fileh5->SetFrameIndexPath(frameindexpath);
|
fileh5->SetFrameIndexPath(frameindexpath);
|
||||||
fileh5->SetImageDataPath(datasetpath);
|
fileh5->SetImageDataPath(datasetpath);
|
||||||
// //open file
|
// //open file
|
||||||
ioutfile = 0;
|
|
||||||
if ( fileh5->OpenResources(filenames[ifile].c_str(), validate_rank) ) {
|
if ( fileh5->OpenResources(filenames[ifile].c_str(), validate_rank) ) {
|
||||||
|
|
||||||
if (of == nullptr) {
|
if (of == nullptr) {
|
||||||
of = fopen(cfname.c_str(), "w");
|
of = fopen(cfname.c_str(), "w");
|
||||||
@ -384,54 +382,56 @@ int main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
|
|
||||||
// //while read frame
|
// //while read frame
|
||||||
framenumber = 0;
|
framenumber = 0;
|
||||||
h5offset[0] = 0;
|
std::fill(h5offset.begin(), h5offset.end(), 0);
|
||||||
ifr = 0;
|
ifr = 0;
|
||||||
//std::cout << "Here! " << framenumber << " ";
|
|
||||||
while ( decoder->readNextFrame(*fileh5, framenumber, h5offset, buff) ) {
|
|
||||||
//std::cout << "Here! " << framenumber << " ";
|
//std::cout << "Here! " << framenumber << " ";
|
||||||
// //push
|
while ( decoder->readNextFrame(*fileh5, framenumber, h5offset, buff) ) {
|
||||||
if ((ifr + 1) % 1000 == 0) {
|
|
||||||
std::cout << " ****"
|
if ((ifr + 1) % 1000 == 0) {
|
||||||
<< decoder->getValue(buff, 20, 20); // << std::endl;
|
std::cout << " ****"
|
||||||
}
|
<< decoder->getValue(buff, 20, 20); // << std::endl;
|
||||||
mt->pushData(buff);
|
}
|
||||||
|
|
||||||
// // //pop
|
int storageCell = 0;
|
||||||
mt->nextThread();
|
hsize_t n_storageCells = 1;
|
||||||
mt->popFree(buff); /* In the last execution of the loop,
|
if (h5rank == 4) {
|
||||||
* this leaves buff outside of the Fifo!
|
storageCell = h5offset[1];
|
||||||
* Free explicitely at the end! */
|
n_storageCells = fileh5->GetDatasetDimensions()[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
// push buff into fifoData for a thread corresponding to the active storage cell
|
||||||
|
mt->pushData(buff, storageCell);
|
||||||
|
// increment (round-robin) the internal thread counter for that storage cell
|
||||||
|
mt->nextThread(storageCell);
|
||||||
|
// get a free memory address from fifoFree of the active storage cell for the next read operation
|
||||||
|
mt->popFree(buff, storageCell); /* In the last execution of the loop,
|
||||||
|
* this leaves buff outside of the Fifo!
|
||||||
|
* Free explicitely at the end! */
|
||||||
|
|
||||||
++ifr;
|
++ifr;
|
||||||
if (ifr % 1000 == 0)
|
if (ifr % 1000 == 0) {
|
||||||
std::cout << " " << ifr << " " << framenumber << " " << h5offset[0]
|
std::cout << " " << ifr << " " << framenumber << " " << h5offset[0];
|
||||||
<< std::endl;
|
if (n_storageCells>1)
|
||||||
if (nframes > 0) {
|
std::cout << " sc " << storageCell;
|
||||||
if (ifr % nframes == 0) {
|
std::cout << "\n";
|
||||||
imgfname = createFileName( outdir, fprefix, fsuffix, "tiff", ioutfile );
|
}
|
||||||
mt->writeImage(imgfname.c_str());
|
|
||||||
mt->clearImage();
|
//framenumber = 0;
|
||||||
++ioutfile;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//framenumber = 0;
|
//std::cout << "aa --" << std::endl;
|
||||||
}
|
fileh5->CloseResources();
|
||||||
|
|
||||||
//std::cout << "aa --" << std::endl;
|
//std::cout << "bb --" << std::endl;
|
||||||
fileh5->CloseResources();
|
while (mt->isBusy()) {
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
//std::cout << "bb --" << std::endl;
|
//std::cout << "cc --" << std::endl;
|
||||||
while (mt->isBusy()) {
|
|
||||||
;
|
imgfname = createFileName( outdir, fprefix, fsuffix, "" );
|
||||||
}
|
std::cout << "Writing tiff to " << imgfname << "_SCxx.tiff" << std::endl;
|
||||||
|
|
||||||
//std::cout << "cc --" << std::endl;
|
|
||||||
if (nframes >= 0) {
|
|
||||||
if (nframes > 0)
|
|
||||||
imgfname = createFileName( outdir, fprefix, fsuffix, "tiff", ioutfile );
|
|
||||||
std::cout << "Writing tiff to " << imgfname << std::endl;
|
|
||||||
mt->writeImage(imgfname.c_str());
|
mt->writeImage(imgfname.c_str());
|
||||||
mt->clearImage();
|
mt->clearImage();
|
||||||
if (of) {
|
if (of) {
|
||||||
@ -439,20 +439,15 @@ int main(int argc, char *argv[]) {
|
|||||||
of = nullptr;
|
of = nullptr;
|
||||||
mt->setFilePointer(nullptr);
|
mt->setFilePointer(nullptr);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
std::time(&end_time);
|
std::time(&end_time);
|
||||||
std::cout << std::ctime(&end_time) << std::endl;
|
std::cout << std::ctime(&end_time) << std::endl;
|
||||||
} else
|
} else {
|
||||||
std::cout << "Could not open " << filenames[ifile] << " for reading "
|
std::cout << "Could not open " << filenames[ifile] << " for reading "
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (nframes < 0) {
|
|
||||||
std::string fprefix( getRootString(filenames[0]) ); //Possibly, non-ideal name choice for file
|
|
||||||
std::string imgfname( createFileName( outdir, fprefix, "sum", "tiff" ) );
|
|
||||||
std::cout << "Writing tiff to " << imgfname << std::endl;
|
|
||||||
mt->writeImage(imgfname.c_str());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
//std::cout << "Calling delete..." << std::endl;
|
//std::cout << "Calling delete..." << std::endl;
|
||||||
/* Info: Previously, 'delete mt' caused crash
|
/* Info: Previously, 'delete mt' caused crash
|
||||||
|
@ -422,22 +422,31 @@ class multiThreadedAnalogDetector {
|
|||||||
dets[i]->newDataSet();
|
dets[i]->newDataSet();
|
||||||
};
|
};
|
||||||
|
|
||||||
virtual int *getImage(int &nnx, int &nny, int &ns, int &nsy) {
|
// Storage cell sensitive
|
||||||
|
virtual int *getImage(int &nnx, int &nny, int &ns, int &nsy, int sc = 0) {
|
||||||
//int *img;
|
//int *img;
|
||||||
// int nnx, nny, ns;
|
// int nnx, nny, ns;
|
||||||
// int nnx, nny, ns;
|
// int nnx, nny, ns;
|
||||||
int nn = dets[0]->getImageSize(nnx, nny, ns, nsy);
|
int nn = dets[0]->getImageSize(nnx, nny, ns, nsy);
|
||||||
if (image) {
|
|
||||||
delete[] image;
|
if (sc_images[sc]) {
|
||||||
image = nullptr;
|
delete[] sc_images[sc];
|
||||||
|
sc_images[sc] = nullptr;
|
||||||
}
|
}
|
||||||
image = new int[nn];
|
|
||||||
|
// Allocate memory for image and zero-initialize
|
||||||
|
sc_images[sc] = new int[nn]();
|
||||||
// int nn=dets[0]->getImageSize(nnx, nny, ns);
|
// int nn=dets[0]->getImageSize(nnx, nny, ns);
|
||||||
// for (i=0; i<nn; i++) image[i]=0;
|
// for (i=0; i<nn; i++) image[i]=0;
|
||||||
|
|
||||||
for (int ii = 0; ii < nThreads; ii++) {
|
// Get the threads assigned to this storage cell
|
||||||
|
auto const& assigned_threads = sc_to_threads[sc];
|
||||||
|
|
||||||
|
// Only iterate over threads assigned to this storage cell
|
||||||
|
for (int thread_id : assigned_threads) {
|
||||||
|
|
||||||
int* tmp_img = dets[ii]->getImage();
|
int* tmp_img = dets[thread_id]->getImage();
|
||||||
|
if (!tmp_img) continue; // Skip if null
|
||||||
|
|
||||||
/* std::cout << "## Thread " << ii
|
/* std::cout << "## Thread " << ii
|
||||||
<< " # image size " << nn
|
<< " # image size " << nn
|
||||||
@ -445,24 +454,20 @@ class multiThreadedAnalogDetector {
|
|||||||
<< " # nny " << nny
|
<< " # nny " << nny
|
||||||
<< " # ns " << ns; */
|
<< " # ns " << ns; */
|
||||||
|
|
||||||
|
// Sum images across threads
|
||||||
for (int i = 0; i < nn; i++) {
|
for (int i = 0; i < nn; i++) {
|
||||||
|
|
||||||
/* std::cout << " # pixel " << i
|
/* std::cout << " # pixel " << i
|
||||||
<< " # value " << tmp_img[i]
|
<< " # value " << tmp_img[i]
|
||||||
<< " ## " << std::endl; */
|
<< " ## " << std::endl; */
|
||||||
|
|
||||||
if (ii == 0)
|
|
||||||
// if (img[i]>0)
|
sc_images[sc][i] += tmp_img[i];
|
||||||
image[i] = tmp_img[i];
|
|
||||||
// else
|
|
||||||
// image[i]=0;
|
|
||||||
else // if (img[i]>0)
|
|
||||||
image[i] += tmp_img[i];
|
|
||||||
// if (img[i]) cout << "det " << ii << " pix " << i << " val
|
// if (img[i]) cout << "det " << ii << " pix " << i << " val
|
||||||
// " << img[i] << " " << image[i] << endl;
|
// " << img[i] << " " << image[i] << endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return image;
|
return sc_images[sc];
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void clearImage() {
|
virtual void clearImage() {
|
||||||
@ -472,7 +477,7 @@ class multiThreadedAnalogDetector {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void *writeImage(const char *imgname, double t = 1) {
|
virtual void *writeImage(char const* base_imgname, double t = 1) {
|
||||||
/* #ifdef SAVE_ALL */
|
/* #ifdef SAVE_ALL */
|
||||||
/* for (int ii=0; ii<nThreads; ii++) { */
|
/* for (int ii=0; ii<nThreads; ii++) { */
|
||||||
/* char tit[10000];cout << "m" <<endl; */
|
/* char tit[10000];cout << "m" <<endl; */
|
||||||
@ -481,11 +486,30 @@ class multiThreadedAnalogDetector {
|
|||||||
/* } */
|
/* } */
|
||||||
/* #endif */
|
/* #endif */
|
||||||
int nnx, nny, ns, nsy;
|
int nnx, nny, ns, nsy;
|
||||||
getImage(nnx, nny, ns, nsy);
|
|
||||||
// int nnx, nny, ns;
|
|
||||||
int nn = dets[0]->getImageSize(nnx, nny, ns, nsy);
|
int nn = dets[0]->getImageSize(nnx, nny, ns, nsy);
|
||||||
float *gm = new float[nn];
|
|
||||||
if (gm) {
|
// Allocate teporary float buffer and zero-initialize
|
||||||
|
std::vector<float> gm(nn);
|
||||||
|
|
||||||
|
// Lambda for pixel conversion
|
||||||
|
auto convert_pixel = [t](int pixel) -> float {
|
||||||
|
return (t > 0) ? static_cast<float>(std::max(0, pixel)) / t : pixel;
|
||||||
|
}; // t ... threshold
|
||||||
|
|
||||||
|
// Loop over each storage cell
|
||||||
|
for (auto const& [sc, _] : sc_to_threads) {
|
||||||
|
std::string imgname(base_imgname);
|
||||||
|
if (nSC > 1) imgname += "_SC" + std::to_string(sc);
|
||||||
|
imgname += ".tiff";
|
||||||
|
|
||||||
|
//Retrieve the image for this storage cell
|
||||||
|
int *image = getImage(nnx, nny, ns, nsy, sc);
|
||||||
|
if (!image) continue; // Skip if null
|
||||||
|
|
||||||
|
// Convert image data to float
|
||||||
|
std::transform(image, image + nn, gm.begin(), convert_pixel);
|
||||||
|
|
||||||
|
/* old loop implementing same logic as convert_pixel
|
||||||
for (int ix = 0; ix < nn; ix++) {
|
for (int ix = 0; ix < nn; ix++) {
|
||||||
if (t) {
|
if (t) {
|
||||||
if (image[ix] < 0)
|
if (image[ix] < 0)
|
||||||
@ -498,15 +522,16 @@ class multiThreadedAnalogDetector {
|
|||||||
// if (image[ix]>0 && ix/nnx<350) cout << ix/nnx << " " <<
|
// if (image[ix]>0 && ix/nnx<350) cout << ix/nnx << " " <<
|
||||||
// ix%nnx << " " << image[ix]<< " " << gm[ix] << endl;
|
// ix%nnx << " " << image[ix]<< " " << gm[ix] << endl;
|
||||||
}
|
}
|
||||||
// cout << "image " << nnx << " " << nny << endl;
|
*/
|
||||||
WriteToTiff(gm, imgname, nnx, nny);
|
|
||||||
delete[] gm;
|
|
||||||
} else
|
|
||||||
std::cout << "Could not allocate float image " << std::endl;
|
|
||||||
|
|
||||||
if(image) {
|
WriteToTiff(gm.data(), imgname.c_str(), nnx, nny);
|
||||||
delete[] image; // Memory cleanup (VH)
|
|
||||||
image = nullptr;
|
// Clean up memory for this storage cell
|
||||||
|
if (sc_images[sc]) {
|
||||||
|
delete[] sc_images[sc];
|
||||||
|
sc_images[sc] = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
@ -822,6 +847,7 @@ class multiThreadedAnalogDetector {
|
|||||||
std::mutex map_mutex; // Ensure thread-safe access to the map
|
std::mutex map_mutex; // Ensure thread-safe access to the map
|
||||||
int nSC{1}; // Number of storage cells
|
int nSC{1}; // Number of storage cells
|
||||||
|
|
||||||
|
std::unordered_map<int, int*> sc_images; // Store images per storage cell
|
||||||
std::unordered_map<int, double*> sc_pedestals; // Store pedestal arrays per storage cell
|
std::unordered_map<int, double*> sc_pedestals; // Store pedestal arrays per storage cell
|
||||||
std::unordered_map<int, double*> sc_pedestals_rms; // Store pedestal RMS arrays per storage cell
|
std::unordered_map<int, double*> sc_pedestals_rms; // Store pedestal RMS arrays per storage cell
|
||||||
// at the moment, these maps could be avoided, but this implementation is more robust in allowing future changes
|
// at the moment, these maps could be avoided, but this implementation is more robust in allowing future changes
|
||||||
|
Reference in New Issue
Block a user