#ifndef ANALOGDETECTOR_H #define ANALOGDETECTOR_H //#include #include #include "slsDetectorData.h" #include "pedestalSubtraction.h" #include "commonModeSubtraction.h" #include "tiffIO.h" using namespace std; #ifndef FRAMEMODE_DEF #define FRAMEMODE_DEF /** enum to define the flags of the data set, which are needed to seect the type of processing it should undergo: frame, pedestal, flat */ enum frameMode { eFrame, ePedestal, eFlat }; #endif template class analogDetector { /** @short class to perform pedestal subtraction etc. for an analog detector */ public: /** Constructor (no error checking if datasize and offsets are compatible!) \param d detector data structure to be used - if null it is assumed that the data are in ordered ip=iy*nx+ix \param sign is the sign of the data \param nped number of samples for pedestal averaging \param cm common mode subtraction algorithm, if any. Defaults to NULL i.e. none \param nnx detector size in x - must be specified if no data structure is defined, otherwise defaults to the size of the data structure. \param nny detector size in y - must be specified if no data structure is defined, otherwise defaults to the size of the data structure. \param gm pointer to tha gain map matrix */ analogDetector(slsDetectorData *d, int sign=1, commonModeSubtraction *cm=NULL, int nped=1000, int nnx=-1, int nny=-1, double *gm=NULL) : det(d), nx(nnx), ny(nny), stat(NULL), cmSub(cm), iframe(-1), dataSign(sign), gmap(gm), id(0) { if (det) det->getDetectorSize(nx,ny); stat=new pedestalSubtraction*[ny]; for (int i=0; idet; nx=orig->nx; ny=orig->ny; dataSign=orig->dataSign; iframe=orig->iframe; gmap=orig->gmap; cmSub=orig->cmSub; id=orig->id; xmin=orig->xmin; xmax=orig->xmax; ymin=orig->ymin; ymax=orig->ymax; thr=orig->thr; // nSigma=orig->nSigma; fMode=orig->fMode; myFile=orig->myFile; fm=orig->fm; stat=new pedestalSubtraction*[ny]; for (int i=0; iSetNPedestals(); //cout << nped << " " << orig->getPedestal(ix,iy) << orig->getPedestalRMS(ix,iy) << endl; for (int iy=0; iygetPedestal(ix,iy),orig->getPedestalRMS(ix,iy),orig->GetNPedestals(ix,iy)); } } image=new int[nx*ny]; } /** clone. Must be virtual! \returns a clone of the original analog detector */ virtual analogDetector *Clone() { return new analogDetector(this); } /** Gives an id to the structure. For debugging purposes in case of multithreading. \param i is to be set \returns current id */ int setId(int i){id=i; return id;}; /** Returns id of the structure. For debugging purposes in case of multithreading. \returns current id */ int getId() {return id; }; /** Returns data size of the detector data structure \returns data size of the detector data structurein bytes */ int getDataSize(){return det->getDataSize();}; /** Returns data size of the detector image matrix \param nnx reference to image size in x \param nny reference to image size in y \param nns reference to number of subpixels for interpolating detector, will always be 1 in this case \returns number of pixels of the detector image */ virtual int getImageSize(int &nnx, int &nny, int &nns){nnx=nx; nny=ny; nns=1; return nx*ny;}; /** Returns data size of the detector image matrix \param nnx reference to pixel size in x \param nny reference to pixel size in y \returns number of pixels of the detector image */ virtual int getDetectorSize(int &nnx, int &nny){nnx=nx; nny=ny; return nx*ny;}; /** set gain map \param gm pointer to gain map matrix to be set - NULL unsets \returns pointer to current gain map */ double *setGainMap(double *gm) {gmap=gm; return gmap;}; /** return gain map \returns pointer to current gain map */ double *getGainMap() {return gmap;}; /** reads a 32 bit tiff file of the size of the detector and sets its values as gain map for the detector. If file does not exist returns NULL, but does not change gainmap compared to previous settings. \param imgname complete name of the file containing the gain map data \returns pointer to current gain map is file reading succeeded, NULL is file reading didn't work. */ double *readGainMap(const char * imgname) { uint32 nnx, nny; float *gm=ReadFromTiff( imgname, nny, nnx); if (gm) { if (gmap) delete [] gmap; gmap=new double[nnx*nny]; for (int ix=0; ixClear(); }; /** resets the commonModeSubtraction and increases the frame index */ virtual void newFrame(){iframe++; if (cmSub) cmSub->newFrame();}; /** sets the commonModeSubtraction algorithm to be used \param cm commonModeSubtraction algorithm to be used (NULL unsets) \returns pointer to the actual common mode subtraction algorithm */ commonModeSubtraction *setCommonModeSubtraction(commonModeSubtraction *cm) {cmSub=cm; return cmSub;}; /** gets the commonModeSubtraction algorithm to be used \returns pointer to the actual common mode subtraction algorithm */ commonModeSubtraction *getCommonModeSubtraction() {return cmSub;}; /** sets the sign of the data \param sign 1 means positive values for photons, -1 negative, 0 gets \returns current sign for the data */ int setDataSign(int sign=0) {if (sign==1 || sign==-1) dataSign=sign; return dataSign;}; /** adds value to pedestal (and common mode) for the given pixel \param val value to be added \param ix pixel x coordinate \param iy pixel y coordinate \param cm 1 adds the value to common mod, 0 skips it. Defaults to 0. - not properly implemented */ virtual void addToPedestal(double val, int ix, int iy=0, int cm=0){ if (ix>=0 && ix=0 && iy0) { val-= getCommonMode(ix, iy); } stat[iy][ix].addToPedestal(val); /* if (cmSub && cm>0) { */ /* if (det) if (det->isGood(ix, iy)==0) return; */ /* cmSub->addToCommonMode(val, ix, iy); */ /* }; */ }; } double getCommonMode(int ix, int iy) { if (cmSub) return cmSub->getCommonMode(ix, iy); else return 0; } virtual void addToCommonMode(double val, int ix, int iy=0){ if (ix>=0 && ix=0 && iyisGood(ix, iy)==0) return; if (getNumpedestals(ix,iy)>0) cmSub->addToCommonMode(val-getPedestal(ix,iy), ix, iy); }; } } virtual void addToCommonMode(char *data){ if (cmSub) { for (int ix=xmin; ix0) addToCommonMode(data, ix, iy); } } cout << "cm " << getCommonMode(0,0) << " " << getCommonMode(1,0) << endl; } } virtual void addToCommonMode(char *data, int ix, int iy=0){ if (cmSub) { if (det) if (det->isGood(ix, iy)==0) return; if (getNumpedestals(ix,iy)>0) cmSub->addToCommonMode(subtractPedestal(data,ix,iy,0), ix, iy); } } /** gets pedestal (and common mode) \param ix pixel x coordinate \param iy pixel y coordinate \param cm 0 (default) without common mode subtraction, 1 with common mode subtraction (if defined) \returns pedestal value */ virtual double getPedestal(int ix, int iy, int cm=0){ if (ix>=0 && ix=0 && iy0) return stat[iy][ix].getPedestal()+getCommonMode(ix,iy); else return stat[iy][ix].getPedestal(); else return -1; }; /** gets pedestal rms (i.e. noise) \param ix pixel x coordinate \param iy pixel y coordinate \returns pedestal rms */ virtual double getPedestalRMS(int ix, int iy){ if (ix>=0 && ix=0 && iy=0 && ix=0 && iy=0 && ix=0 && iy=0 && ix=0 && iynx) nnx=nx; if (nny>ny) nny=ny; if (gm) { for (int ix=0; ixnx) nnx=nx; if (nny>ny) nny=ny; if (gm) { for (int ix=0; ixnx) nnx=nx; if (nny>ny) nny=ny; if (gm) { for (int ix=0; ix=0 && xmi<=nx) xmin=xmi; if (xma>=0 && xma<=nx) xmax=xma; if (xmax=0 && ymi<=ny) ymin=ymi; if (yma>=0 && yma<=ny) ymax=yma; if (ymax=0 && ix=0 && iygetValue(data, ix, iy); else val=((double*)data)[iy*nx+ix]; if (cm && cmSub) val-=getCommonMode(ix,iy); addToPedestal(val,ix,iy); } return ; }; /** Subtracts pedestal from the data array in the region of interest \param data pointer to the data \param val pointer where the pedestal subtracted data should be added. If NULL, the internal image is used \returns pointer to the pedestal subtracted data */ virtual double *subtractPedestal(char *data, double *val=NULL, int cm=0) { newFrame(); if (val==NULL) val=new double[nx*ny]; for (int ix=xmin; ix=0 && ix=0 && iygetValue(data, ix, iy) << " " << getPedestal(ix,iy) << " " << (dataSign*det->getValue(data, ix, iy)-getPedestal(ix,iy))/g << endl; return (dataSign*det->getValue(data, ix, iy)-getPedestal(ix,iy, cm))/g; } else return (((double*)data)[iy*nx+ix]-getPedestal(ix,iy,cm))/g; } }; /** sets threshold value for conversion into number of photons \param t threshold to be set \returns threshold value */ double setThreshold(double t){thr=t; return thr;}; /** gets threshold value for conversion into number of photons \returns threshold value */ double getThreshold(){return thr;}; /** converts the data into number of photons for the selected pixel \param data pointer to the data \param ix pixel x coordinate \param iy pixel y coordinate \returns converted number of photons. If no threshold is set, returns gain converted pedestal subtracted data. */ virtual int getNPhotons(char *data, int ix, int iy=0) { int nph=0; double v; int cm=0; if (cmSub) cm=1; if (ix>=0 && ix=0 && iy0) { v+=0.5*thr; nph=v/thr; return nph; } else { // cout << v << endl; //if (v>0) return v; //else //return 0; } } return 0; }; /** converts the data into number of photons for all pixels \param data pointer to the data \param nph pointer where the photons should added. If NULL,the internal image is used \returns pointer to array containing the number of photons */ int *getNPhotons(char *data, int *nph=NULL) { // double val; if (nph==NULL) nph=image; newFrame(); addToCommonMode(data); for (int ix=xmin; ix0) for (ix=0; ix=0 && ix=0 && iy=0 && ix=0 && iy *det; /**< slsDetectorData to be used */ int nx; /**< Size of the detector in x direction */ int ny; /**< Size of the detector in y direction */ pedestalSubtraction **stat; /**< pedestalSubtraction class */ commonModeSubtraction *cmSub;/**< commonModeSubtraction class */ int dataSign; /**< sign of the data i.e. 1 if photon is positive, -1 if negative */ int iframe; /**< frame number (not from file but incremented within the dataset every time newFrame is called */ double *gmap; int *image; int id; //int xmin, xmax, ymin, ymax; int xmin; /**< minimum x of the region of interest */ int xmin; /**< minimum x of the region of interest */ int xmax; /**< maximum x of the region of interest */ int ymin;/**< minimum y of the region of interest */ int ymax;/**< maximum y of the region of interest */ double thr; /**< threshold to be used for conversion into number of photons */ // int nSigma; /**< number of sigma to be used for conversion into number of photons if threshold is undefined */ frameMode fMode; /**< current detector frame mode */ FILE *myFile; /**< file pointer to write to */ pthread_mutex_t *fm; }; #endif