Adapt get and writeImage methods to storage cell data

This commit is contained in:
2025-02-07 15:33:31 +01:00
parent fcbb87b71a
commit 7b1e74f4a4
2 changed files with 123 additions and 102 deletions

View File

@ -213,10 +213,9 @@ int main(int argc, char *argv[]) {
std::time(&end_time);
std::cout << std::ctime(&end_time) << std::endl;
// Validate number of threads for number of storage cells (if applicable)
int nThreads = nthreads;
int nSC = 1;
// Validate number of threads for number of storage cells (if applicable)
// Determine the dimensions of the dataset from the first datafile
auto firstfileh5 = std::make_unique<HDF5File>();
firstfileh5->SetFrameIndexPath(frameindexpath);
@ -233,7 +232,7 @@ int main(int argc, char *argv[]) {
firstfileh5->CloseResources();
} else {
std::cerr << "Could not open data file " << filenames[0]
<< " for reading " << std::endl;
<< " for validating rank " << std::endl;
}
multiThreadedCountingDetector* mt =
@ -298,7 +297,7 @@ int main(int argc, char *argv[]) {
// 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);
mt->popFree(buff, storageCell);
/* 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! */
@ -341,7 +340,6 @@ int main(int argc, char *argv[]) {
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) {
std::cout << "DATA ";
std::string fsuffix{};
@ -358,7 +356,7 @@ int main(int argc, char *argv[]) {
fileh5->SetFrameIndexPath(frameindexpath);
fileh5->SetImageDataPath(datasetpath);
// //open file
ioutfile = 0;
if ( fileh5->OpenResources(filenames[ifile].c_str(), validate_rank) ) {
if (of == nullptr) {
@ -385,35 +383,38 @@ int main(int argc, char *argv[]) {
// //while read frame
framenumber = 0;
h5offset[0] = 0;
std::fill(h5offset.begin(), h5offset.end(), 0);
ifr = 0;
//std::cout << "Here! " << framenumber << " ";
while ( decoder->readNextFrame(*fileh5, framenumber, h5offset, buff) ) {
//std::cout << "Here! " << framenumber << " ";
// //push
if ((ifr + 1) % 1000 == 0) {
std::cout << " ****"
<< decoder->getValue(buff, 20, 20); // << std::endl;
}
mt->pushData(buff);
// // //pop
mt->nextThread();
mt->popFree(buff); /* In the last execution of the loop,
int storageCell = 0;
hsize_t n_storageCells = 1;
if (h5rank == 4) {
storageCell = h5offset[1];
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;
if (ifr % 1000 == 0)
std::cout << " " << ifr << " " << framenumber << " " << h5offset[0]
<< std::endl;
if (nframes > 0) {
if (ifr % nframes == 0) {
imgfname = createFileName( outdir, fprefix, fsuffix, "tiff", ioutfile );
mt->writeImage(imgfname.c_str());
mt->clearImage();
++ioutfile;
}
if (ifr % 1000 == 0) {
std::cout << " " << ifr << " " << framenumber << " " << h5offset[0];
if (n_storageCells>1)
std::cout << " sc " << storageCell;
std::cout << "\n";
}
//framenumber = 0;
@ -428,10 +429,9 @@ int main(int argc, char *argv[]) {
}
//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;
imgfname = createFileName( outdir, fprefix, fsuffix, "" );
std::cout << "Writing tiff to " << imgfname << "_SCxx.tiff" << std::endl;
mt->writeImage(imgfname.c_str());
mt->clearImage();
if (of) {
@ -439,18 +439,13 @@ int main(int argc, char *argv[]) {
of = nullptr;
mt->setFilePointer(nullptr);
}
}
std::time(&end_time);
std::cout << std::ctime(&end_time) << std::endl;
} else
} else {
std::cout << "Could not open " << filenames[ifile] << " for reading "
<< 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());
}

View File

@ -422,22 +422,31 @@ class multiThreadedAnalogDetector {
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 nnx, nny, ns;
// int nnx, nny, ns;
int nn = dets[0]->getImageSize(nnx, nny, ns, nsy);
if (image) {
delete[] image;
image = nullptr;
if (sc_images[sc]) {
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);
// 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];
int* tmp_img = dets[ii]->getImage();
// Only iterate over threads assigned to this storage cell
for (int thread_id : assigned_threads) {
int* tmp_img = dets[thread_id]->getImage();
if (!tmp_img) continue; // Skip if null
/* std::cout << "## Thread " << ii
<< " # image size " << nn
@ -445,24 +454,20 @@ class multiThreadedAnalogDetector {
<< " # nny " << nny
<< " # ns " << ns; */
// Sum images across threads
for (int i = 0; i < nn; i++) {
/* std::cout << " # pixel " << i
<< " # value " << tmp_img[i]
<< " ## " << std::endl; */
if (ii == 0)
// if (img[i]>0)
image[i] = tmp_img[i];
// else
// image[i]=0;
else // if (img[i]>0)
image[i] += tmp_img[i];
sc_images[sc][i] += tmp_img[i];
// if (img[i]) cout << "det " << ii << " pix " << i << " val
// " << img[i] << " " << image[i] << endl;
}
}
return image;
return sc_images[sc];
}
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 */
/* for (int ii=0; ii<nThreads; ii++) { */
/* char tit[10000];cout << "m" <<endl; */
@ -481,11 +486,30 @@ class multiThreadedAnalogDetector {
/* } */
/* #endif */
int nnx, nny, ns, nsy;
getImage(nnx, nny, ns, nsy);
// int nnx, nny, ns;
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++) {
if (t) {
if (image[ix] < 0)
@ -498,15 +522,16 @@ class multiThreadedAnalogDetector {
// if (image[ix]>0 && ix/nnx<350) cout << ix/nnx << " " <<
// 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;
*/
WriteToTiff(gm.data(), imgname.c_str(), nnx, nny);
// Clean up memory for this storage cell
if (sc_images[sc]) {
delete[] sc_images[sc];
sc_images[sc] = nullptr;
}
if(image) {
delete[] image; // Memory cleanup (VH)
image = nullptr;
}
return NULL;
@ -822,6 +847,7 @@ class multiThreadedAnalogDetector {
std::mutex map_mutex; // Ensure thread-safe access to the map
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_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