mirror of
https://github.com/slsdetectorgroup/slsDetectorPackage.git
synced 2025-06-14 22:07:12 +02:00
Implement copy constructor in analogDetector
- avoid default shallow copy of pointer members triggered by copy of derived class - as a result avoid double-free bugs
This commit is contained in:
@ -8,9 +8,9 @@
|
|||||||
#include "commonModeSubtractionNew.h"
|
#include "commonModeSubtractionNew.h"
|
||||||
#include "ghostSummation.h"
|
#include "ghostSummation.h"
|
||||||
#include "pedestalSubtraction.h"
|
#include "pedestalSubtraction.h"
|
||||||
|
#include "sls/tiffIO.h"
|
||||||
#include "slsDetectorData.h"
|
#include "slsDetectorData.h"
|
||||||
#include "slsInterpolation.h"
|
#include "slsInterpolation.h"
|
||||||
#include "sls/tiffIO.h"
|
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
|
|
||||||
#ifdef ROOTSPECTRUM
|
#ifdef ROOTSPECTRUM
|
||||||
@ -91,7 +91,7 @@ template <class dataType> class analogDetector {
|
|||||||
ymax = ny;
|
ymax = ny;
|
||||||
fMode = ePedestal;
|
fMode = ePedestal;
|
||||||
dMode = eInterpolating;
|
dMode = eInterpolating;
|
||||||
//std::cout << "dMode " << dMode << std::endl;
|
// std::cout << "dMode " << dMode << std::endl;
|
||||||
thr = 0;
|
thr = 0;
|
||||||
myFile = NULL;
|
myFile = NULL;
|
||||||
#ifdef ROOTSPECTRUM
|
#ifdef ROOTSPECTRUM
|
||||||
@ -113,38 +113,47 @@ template <class dataType> class analogDetector {
|
|||||||
destructor. Deletes the pdestalSubtraction array and the image
|
destructor. Deletes the pdestalSubtraction array and the image
|
||||||
*/
|
*/
|
||||||
virtual ~analogDetector() {
|
virtual ~analogDetector() {
|
||||||
std::cout << "#### Debug: Destructing analogDetector! ####" << std::endl;
|
std::cout << "#### Debug: Destructing analogDetector! ####"
|
||||||
|
<< std::endl;
|
||||||
for (int i = 0; i < ny; i++) {
|
for (int i = 0; i < ny; i++) {
|
||||||
std::cout << " # " << i;
|
std::cout << " # " << i;
|
||||||
if (stat[i]) {
|
if (stat[i]) {
|
||||||
if (i==0) {
|
if (i == 0) {
|
||||||
std::cout << "#### Debug: Deleting analogDetector member stat[" << i << "] at " << stat[i] << " ####" << std::endl;
|
std::cout
|
||||||
|
<< "#### Debug: Deleting analogDetector member stat["
|
||||||
|
<< i << "] at " << stat[i] << " ####" << std::endl;
|
||||||
}
|
}
|
||||||
delete[] stat[i];
|
delete[] stat[i];
|
||||||
if (i==0) {
|
if (i == 0) {
|
||||||
std::cout << "#### Debug: Deleted analogDetector member stat[" << i << "]! ####" << std::endl;
|
std::cout
|
||||||
|
<< "#### Debug: Deleted analogDetector member stat["
|
||||||
|
<< i << "]! ####" << std::endl;
|
||||||
}
|
}
|
||||||
stat[i] = nullptr;
|
stat[i] = nullptr;
|
||||||
}
|
}
|
||||||
//delete[] stat[i];
|
// delete[] stat[i];
|
||||||
/* delete [] pedMean[i]; */
|
/* delete [] pedMean[i]; */
|
||||||
/* delete [] pedVariance[i]; */
|
/* delete [] pedVariance[i]; */
|
||||||
}
|
}
|
||||||
std::cout << " #\n";
|
std::cout << " #\n";
|
||||||
/* delete [] pedMean; */
|
/* delete [] pedMean; */
|
||||||
/* delete [] pedVariance; */
|
/* delete [] pedVariance; */
|
||||||
//delete[] stat;
|
// delete[] stat;
|
||||||
//delete[] image;
|
// delete[] image;
|
||||||
if (stat) {
|
if (stat) {
|
||||||
std::cout << "#### Debug: Deleting analogDetector member stat at " << stat << " ####" << std::endl;
|
std::cout << "#### Debug: Deleting analogDetector member stat at "
|
||||||
|
<< stat << " ####" << std::endl;
|
||||||
delete[] stat;
|
delete[] stat;
|
||||||
std::cout << "#### Debug: Deleted analogDetector member stat! ####" << std::endl;
|
std::cout << "#### Debug: Deleted analogDetector member stat! ####"
|
||||||
|
<< std::endl;
|
||||||
stat = nullptr;
|
stat = nullptr;
|
||||||
}
|
}
|
||||||
if (image) {
|
if (image) {
|
||||||
std::cout << "#### Debug: Deleting analogDetector member image at " << image << " ####" << std::endl;
|
std::cout << "#### Debug: Deleting analogDetector member image at "
|
||||||
|
<< image << " ####" << std::endl;
|
||||||
delete[] image;
|
delete[] image;
|
||||||
std::cout << "#### Debug: Deleted analogDetector member image! ####" << std::endl;
|
std::cout << "#### Debug: Deleted analogDetector member image! ####"
|
||||||
|
<< std::endl;
|
||||||
image = nullptr;
|
image = nullptr;
|
||||||
}
|
}
|
||||||
#ifdef ROOTSPECTRUM
|
#ifdef ROOTSPECTRUM
|
||||||
@ -164,7 +173,8 @@ template <class dataType> class analogDetector {
|
|||||||
*/
|
*/
|
||||||
analogDetector(analogDetector *orig) {
|
analogDetector(analogDetector *orig) {
|
||||||
/* copy construction from orig*/
|
/* copy construction from orig*/
|
||||||
std::cout << "#### Debug: Calling analogDetector cloning method! ####" << std::endl;
|
std::cout << "#### Debug: Calling analogDetector cloning method! ####"
|
||||||
|
<< std::endl;
|
||||||
det = orig->det;
|
det = orig->det;
|
||||||
nx = orig->nx;
|
nx = orig->nx;
|
||||||
ny = orig->ny;
|
ny = orig->ny;
|
||||||
@ -181,7 +191,7 @@ template <class dataType> class analogDetector {
|
|||||||
// nSigma=orig->nSigma;
|
// nSigma=orig->nSigma;
|
||||||
fMode = orig->fMode;
|
fMode = orig->fMode;
|
||||||
dMode = orig->dMode;
|
dMode = orig->dMode;
|
||||||
//std::cout << "dMode " << dMode << std::endl;
|
// std::cout << "dMode " << dMode << std::endl;
|
||||||
myFile = orig->myFile;
|
myFile = orig->myFile;
|
||||||
|
|
||||||
stat = new pedestalSubtraction *[ny];
|
stat = new pedestalSubtraction *[ny];
|
||||||
@ -246,6 +256,59 @@ template <class dataType> class analogDetector {
|
|||||||
ghSum = NULL;
|
ghSum = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
constructor creating a deep copy of another analog detector
|
||||||
|
\param other analog Detector structure to be copied
|
||||||
|
*/
|
||||||
|
analogDetector(const analogDetector &other)
|
||||||
|
: det(other.det), nx(other.nx), ny(other.ny), dataSign(other.dataSign),
|
||||||
|
iframe(other.iframe), gmap(other.gmap), id(other.id),
|
||||||
|
xmin(other.xmin), xmax(other.xmax), ymin(other.ymin),
|
||||||
|
ymax(other.ymax), thr(other.thr), fMode(other.fMode),
|
||||||
|
dMode(other.dMode), myFile(NULL) {
|
||||||
|
|
||||||
|
std::cout << "#### Debug: Calling analogDetector copy constructor! ####"
|
||||||
|
<< std::endl;
|
||||||
|
|
||||||
|
// Deep copy the stat array
|
||||||
|
stat = new pedestalSubtraction *[ny];
|
||||||
|
for (int i = 0; i < ny; i++) {
|
||||||
|
stat[i] = new pedestalSubtraction[nx];
|
||||||
|
std::copy(other.stat[i], other.stat[i] + nx, stat[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Deep copy image array
|
||||||
|
image = new int[nx * ny];
|
||||||
|
std::copy(other.image, other.image + (nx * ny), image);
|
||||||
|
|
||||||
|
// Copy common-mode subtraction object (if it exists)
|
||||||
|
if (other.cmSub) {
|
||||||
|
cmSub = other.cmSub->Clone();
|
||||||
|
std::cout << "Copying cmSub" << std::endl;
|
||||||
|
} else {
|
||||||
|
cmSub = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Copy ghost summation object (if it exists)
|
||||||
|
if (other.ghSum) {
|
||||||
|
ghSum = other.ghSum->Clone();
|
||||||
|
std::cout << "Copying ghSum" << std::endl;
|
||||||
|
} else {
|
||||||
|
ghSum = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure pedestal values are copied properly
|
||||||
|
int nped = other.GetNPedestals(0, 0);
|
||||||
|
for (int iy = 0; iy < ny; ++iy) {
|
||||||
|
for (int ix = 0; ix < nx; ++ix) {
|
||||||
|
stat[iy][ix].SetNPedestals(nped);
|
||||||
|
setPedestal(ix, iy, other.getPedestal(ix, iy),
|
||||||
|
other.getPedestalRMS(ix, iy),
|
||||||
|
other.GetNPedestals(ix, iy));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
clone. Must be virtual!
|
clone. Must be virtual!
|
||||||
\returns a clone of the original analog detector
|
\returns a clone of the original analog detector
|
||||||
@ -254,10 +317,11 @@ template <class dataType> class analogDetector {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
Deep copy. Must be virtual!
|
Deep copy. Must be virtual!
|
||||||
This is a new addition because of multithreaded storage cell data (where each sc has its own mutex).
|
This is a new addition because of multithreaded storage cell data (where
|
||||||
If the pure virtual function exists here, EVERY derived class has to overwrite it!
|
each sc has its own mutex). If the pure virtual function exists here,
|
||||||
That means a Copy() function must also be implemented in any derived class.
|
EVERY derived class has to overwrite it! That means a Copy() function
|
||||||
\returns a deep copy of the original analog detector
|
must also be implemented in any derived class. \returns a deep copy of
|
||||||
|
the original analog detector
|
||||||
*/
|
*/
|
||||||
virtual analogDetector *Copy() = 0;
|
virtual analogDetector *Copy() = 0;
|
||||||
|
|
||||||
@ -587,6 +651,11 @@ template <class dataType> class analogDetector {
|
|||||||
return ped;
|
return ped;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Const version for use in the copy constructor
|
||||||
|
virtual double getPedestal(int ix, int iy, int cm = 0) const {
|
||||||
|
return stat[iy][ix].getPedestal();
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
gets pedestal rms (i.e. noise)
|
gets pedestal rms (i.e. noise)
|
||||||
\param ix pixel x coordinate
|
\param ix pixel x coordinate
|
||||||
@ -605,6 +674,11 @@ template <class dataType> class analogDetector {
|
|||||||
return ped;
|
return ped;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Const version for use in the copy constructor
|
||||||
|
virtual double getPedestalRMS(int ix, int iy) const {
|
||||||
|
return stat[iy][ix].getPedestalRMS();
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
sets pedestal
|
sets pedestal
|
||||||
\param ix pixel x coordinate
|
\param ix pixel x coordinate
|
||||||
@ -1265,7 +1339,7 @@ template <class dataType> class analogDetector {
|
|||||||
/** gets number of samples for moving average pedestal calculation
|
/** gets number of samples for moving average pedestal calculation
|
||||||
\returns actual number of samples
|
\returns actual number of samples
|
||||||
*/
|
*/
|
||||||
int GetNPedestals(int ix, int iy) {
|
int GetNPedestals(int ix, int iy) const {
|
||||||
if (ix >= 0 && ix < nx && iy >= 0 && iy < ny)
|
if (ix >= 0 && ix < nx && iy >= 0 && iy < ny)
|
||||||
return stat[iy][ix].GetNPedestals();
|
return stat[iy][ix].GetNPedestals();
|
||||||
else
|
else
|
||||||
|
Reference in New Issue
Block a user