File_IO added

git-svn-id: file:///afs/psi.ch/project/sls_det_software/svn/slsDetectorSoftware@207 951219d9-93cf-4727-9268-0efd64621fa3
This commit is contained in:
ramani_n
2012-08-02 14:37:37 +00:00
parent 5aebcd4888
commit 1982b25d89
21 changed files with 2223 additions and 108 deletions

View File

@ -1,4 +1,4 @@
#include "angularConversion.h"
#include "AngularConversion_Standalone.h"
#include <iostream>
#include <fstream>
@ -18,8 +18,9 @@ angularConversion::angularConversion( int *np, double *pos, double *bs, double
{
//angleFunctionPointer=0;
registerAngleFunctionCallback(&defaultAngleFunction);
registerAngleFunctionCallback(&defaultAngleFunction,NULL);
registerCallBackGetChansPerMod(&defaultGetChansPerMod,this);
registerCallBackGetNumberofChannel(&defaultGetNumberofChannel,this);
}
angularConversion::~angularConversion(){
@ -49,28 +50,28 @@ double* angularConversion::convertAngles(double pos) {
angleConversionConstant *p=NULL;
int ch0=0;
int chlast=chansPerMod[imod];
int nchmod=chansPerMod[imod];
p=angConvs[imod];
int chlast=getChansPerMods(imod);
int nchmod=getChansPerMods(imod);
p=angConvs+imod;
if (moveFlag[imod]==0)
enc=0;
else
enc=pos;
for (int ip=0; ip<totalNumberOfChannels; ip++) {
for (int ip=0; ip<getTotalNumberofChannels(); ip++) {
#ifdef VERBOSE
// cout << "ip " << ip << " ch0 " << ch0 << " chlast " << chlast << " imod " << imod << endl;
#endif
if (ip>=chlast) {
imod++;
p=angConvs[imod];
p=angConvs+imod;
if (moveFlag[imod]==0)
enc=0;
else
enc=pos;
ch0=chlast;
nchmod=chansPerMod[imod];
nchmod=getChansPerMods(imod);
if (nchmod>0)
chlast+=nchmod;
}
@ -150,10 +151,7 @@ int angularConversion::readAngularConversion( ifstream& infile, int nmod, angleC
if (nm>=nmod)
break;
}
}
return nm;
}
@ -379,7 +377,7 @@ int angularConversion::addToMerging(double *p1, double *v1, double *e1, double
}
int ret=addToMerging(p1, v1, e1, mp, mv,me, mm,totalNumberOfChannels, *binSize,nBins, badChanMask );
int ret=addToMerging(p1, v1, e1, mp, mv,me, mm,getTotalNumberofChannels(), *binSize,nBins, badChanMask );
if (del) {
@ -468,7 +466,7 @@ double angularConversion::getAngularConversionParameter(angleConversionParameter
/**
int angularConversion::setAngularConversionFile(string fname) {
if (fname=="") {
setAngularCorrectionMask(0);
@ -492,7 +490,7 @@ int angularConversion::setAngularConversionFile(string fname) {
}
*/
/*

View File

@ -1,6 +1,6 @@
#ifndef ANGULARCONVERSION_H
#define ANGULARCONVERSION_H
#ifndef ANGULARCONVERSIONSTD_H
#define ANGULARCONVERSIONSTD_H
//#include "slsDetectorBase.h"
@ -319,7 +319,7 @@ class angularConversion : public virtual slsDetectorDefs { //: public virtual sl
/* \param angconv array that will be filled with the angular conversion constants */
/* \returns 0 if angular conversion disabled, >0 otherwise */
/* *\/ */
/* virtual int getAngularConversion(int &direction, angleConversionConstant *angconv=NULL)=0; */
virtual int getAngularConversion(int &direction, angleConversionConstant *angoff=NULL);
/* /\** */
/* pure virtual function */
@ -340,7 +340,15 @@ class angularConversion : public virtual slsDetectorDefs { //: public virtual sl
/* \param imod module number */
/* \returns number of channels in the module */
/* *\/ */
/* virtual int getChansPerMod(int imod=0)=0; */
static int defaultGetNumberofChannel(int nch, void *p=NULL ) { ((angularConversion*)p)->setTotalNumberOfChannels(nch); return 0;};
static int defaultGetChansPerMod(int imod=0, void *p=NULL){ ((angularConversion*)p)->setChansPerMod(0,imod); return 0;};
int setChansPerMod(int nch, int imod=0){if (nch<0) return -1; if (imod>=0 && imod<MAXMODS*MAXDET) {chansPerMod[imod]=nch; return chansPerMod[imod];} else return -1;};
int setTotalNumberOfChannels(int i){if (i>=0){ totalNumberOfChannels=i; return totalNumberOfChannels;} else return -1;};
/* /\** */
/* get the angular conversion contant of one modules */
@ -381,15 +389,28 @@ class angularConversion : public virtual slsDetectorDefs { //: public virtual sl
*/
int getNumberOfPositions() {return *numberOfPositions;};
int getChansPerMods(int imod) { return chansPerMod[imod];};
int getTotalNumberofChannels(){ return totalNumberOfChannels;};
int setTotalNumberOfChannels(int i){if (i>=0) totalNumberOfChannels=i; return totalNumberOfChannels;};
void incrementPositionIndex() {currentPositionIndex++;};
void registerCallBackGetChansPerMod(int (*func)(int, void *),void *arg){ getChansPerModule=func;pChpermod=arg;};
void registerCallBackGetNumberofChannel(int (*func)(int, void *),void *arg){ getNumberofChannel=func;pNumberofChannel=arg;};
protected:
private:
int (*getChansPerModule)(int, void*);
int (*getNumberofChannel)(int, void*);
void *pChpermod,*angPtr,*pNumberofChannel;
/** merging bins */
double *mergingBins;
@ -403,6 +424,7 @@ class angularConversion : public virtual slsDetectorDefs { //: public virtual sl
int *mergingMultiplicity;
double (*angle)(double, double, double, double, double, double, double, int);
int totalNumberOfChannels;
@ -420,17 +442,17 @@ class angularConversion : public virtual slsDetectorDefs { //: public virtual sl
double *binSize;
int *correctionMask;
int chansPerMod;
int chansPerMod[MAXMODS*MAXDET];
int nMod;
angleConversionConstant angConvs[MAXMODS*MAXDET];
int directions[MAXMODS*MAXDET];
/** pointer to beamlien fine offset*/
/** pointer to beamline fine offset*/
double *fineOffset;
/** pointer to beamlien global offset*/
/** pointer to beamline global offset*/
double *globalOffset;
/** pointer to beamlien angular direction*/
/** pointer to beamline angular direction*/
int *angDirection;
/** pointer to detector move flag (1 moves with encoder, 0 not)*/
@ -457,9 +479,10 @@ class angularConversion : public virtual slsDetectorDefs { //: public virtual sl
int getCurrentPositionIndex() {return currentPositionIndex;};
void registerAngleFunctionCallback(double( *fun)(double, double, double, double, double, double, double, int)) {angle = fun;};
void registerAngleFunctionCallback(double( *fun)(double, double, double, double, double, double, double, int),void* arg) {angle = fun; angPtr=arg;};
};

View File

@ -0,0 +1,648 @@
#include "FileIO_Standalone.h"
string fileIO::createFileName(char *filepath, char *filename, int aMask, double sv0, int prec0, double sv1, int prec1, int pindex, int npos, int findex) {
ostringstream osfn;
// string fn;
/*directory name +root file name */
osfn << filepath << "/" << filename;
// cout << osfn.str() << endl;
// scan level 0
if ( aMask& (1 << (MAX_ACTIONS)))
osfn << "_S" << fixed << setprecision(prec0) << sv0;
//cout << osfn.str() << endl;
//scan level 1
if (aMask & (1 << (MAX_ACTIONS+1)))
osfn << "_s" << fixed << setprecision(prec1) << sv1;
//cout << osfn.str() << endl;
//position
if (pindex>0 && pindex<=npos)
osfn << "_p" << pindex;
//cout << osfn.str() << endl;
// file index
osfn << "_" << findex;
//cout << osfn.str() << endl;
#ifdef VERBOSE
cout << "created file name " << osfn.str() << endl;
#endif
//cout << osfn.str() << endl;
//fn=oosfn.str()sfn.str();
return osfn.str();
}
/* I/O */
/* generates file name without extension*/
string fileIO::createFileName(int aMask, double sv0, int prec0, double sv1, int prec1, int pindex, int npos) {
currentFileName=createFileName(filePath, \
fileName, \
aMask, \
sv0, \
prec0, \
sv1, \
prec1, \
pindex, \
npos, \
*fileIndex \
);
return currentFileName;
}
int fileIO::getFileIndexFromFileName(string fname) {
int i;
size_t dot=fname.rfind(".");
if (dot==string::npos)
return -1;
size_t uscore=fname.rfind("_");
if (uscore==string::npos)
return -1;
if (sscanf( fname.substr(uscore+1,dot-uscore-1).c_str(),"%d",&i)) {
return i;
}
//#ifdef VERBOSE
cout << "******************************** cannot parse file index" << endl;
//#endif
return 0;
}
int fileIO::getVariablesFromFileName(string fname, int &index, int &p_index, double &sv0, double &sv1) {
int i;
double f;
string s;
index=-1;
p_index=-1;
sv0=-1;
sv1=-1;
// size_t dot=fname.rfind(".");
//if (dot==string::npos)
// return -1;
size_t uscore=fname.rfind("_");
if (uscore==string::npos)
return -1;
s=fname;
//if (sscanf(s.substr(uscore+1,dot-uscore-1).c_str(),"%d",&i)) {
if (sscanf(s.substr(uscore+1,s.size()-uscore-1).c_str(),"%d",&i)) {
index=i;
#ifdef VERBOSE
cout << "******************************** file index is " << index << endl;
#endif
//return i;
s=fname.substr(0,uscore);
}
else
cout << "******************************** cannot parse file index" << endl;
#ifdef VERBOSE
cout << s << endl;
#endif
uscore=s.rfind("_");
if (sscanf( s.substr(uscore+1,s.size()-uscore-1).c_str(),"p%d",&i)) {
p_index=i;
#ifdef VERBOSE
cout << "******************************** position index is " << p_index << endl;
#endif
s=fname.substr(0,uscore);
}
else
cout << "******************************** cannot parse position index" << endl;
#ifdef VERBOSE
cout << s << endl;
#endif
uscore=s.rfind("_");
if (sscanf( s.substr(uscore+1,s.size()-uscore-1).c_str(),"s%f",&f)) {
sv1=f;
#ifdef VERBOSE
cout << "******************************** scan variable 1 is " << sv1 << endl;
#endif
s=fname.substr(0,uscore);
}
else
cout << "******************************** cannot parse scan varable 1" << endl;
#ifdef VERBOSE
cout << s << endl;
#endif
uscore=s.rfind("_");
if (sscanf( s.substr(uscore+1,s.size()-uscore-1).c_str(),"S%f",&f)) {
sv0=f;
#ifdef VERBOSE
cout << "******************************** scan variable 0 is " << sv0 << endl;
#endif
}
else
cout << "******************************** cannot parse scan varable 0" << endl;
#ifdef VERBOSE
#endif
return index;
}
int fileIO::writeDataFile(string fname, int nch, double *data, double *err, double *ang, char dataformat){
ofstream outfile;
// int idata;
if (data==NULL)
return FAIL;
// args|=0x10; // one line per channel!
outfile.open (fname.c_str(),ios_base::out);
if (outfile.is_open())
{
writeDataFile(outfile, nch, data, err, ang, dataformat, 0);
outfile.close();
return OK;
} else {
std::cout<< "Could not open file " << fname << "for writing"<< std::endl;
return FAIL;
}
};
int fileIO::writeDataFile(ofstream &outfile, int nch, double *data, double *err, double *ang, char dataformat, int offset){
int idata;
if (data==NULL)
return FAIL;
// args|=0x10; // one line per channel!
for (int ichan=0; ichan<nch; ichan++) {
if (ang==NULL) {
outfile << ichan+offset << " ";
} else {
outfile << ang[ichan] << " ";
}
switch (dataformat) {
case 'f':
outfile << *(data+ichan)<< " ";
break;
case 'i':
default:
idata=(int)(*(data+ichan));
outfile << idata << " ";
}
if (err) {
outfile << *(err+ichan)<< " ";
}
// if (args&0x10) {
outfile << std::endl;
// }
}
return OK;
}
/*writes raw data file */
int fileIO::writeDataFile(string fname, int nch, int *data){
ofstream outfile;
if (data==NULL)
return FAIL;
outfile.open (fname.c_str(),ios_base::out);
if (outfile.is_open())
{
writeDataFile(outfile, nch, data, 0);
outfile.close();
return OK;
} else {
std::cout<< "Could not open file " << fname << "for writing"<< std::endl;
return FAIL;
}
};
/*writes raw data file */
int fileIO::writeDataFile(ofstream &outfile, int nch, int *data, int offset){
if (data==NULL)
return FAIL;
for (int ichan=0; ichan<nch; ichan++)
outfile << ichan+offset << " " << *(data+ichan) << std::endl;
return OK;
};
/*writes raw data file */
int fileIO::writeDataFile(string fname, int nch, short int *data){
ofstream outfile;
if (data==NULL)
return FAIL;
outfile.open (fname.c_str(),ios_base::out);
if (outfile.is_open())
{
writeDataFile(outfile, nch, data, 0);
outfile.close();
return OK;
} else {
std::cout<< "Could not open file " << fname << "for writing"<< std::endl;
return FAIL;
}
};
/*writes raw data file */
int fileIO::writeDataFile(ofstream &outfile, int nch, short int *data, int offset){
if (data==NULL)
return FAIL;
for (int ichan=0; ichan<nch; ichan++)
outfile << ichan+offset << " " << *(data+ichan) << std::endl;
return OK;
};
int fileIO::readDataFile(int nch, string fname, double *data, double *err, double *ang, char dataformat){
ifstream infile;
int iline=0;//ichan,
//int interrupt=0;
//double fdata, ferr, fang;
int maxchans;
//int ich;
string str;
maxchans=nch;
#ifdef VERBOSE
std::cout<< "Opening file "<< fname << std::endl;
#endif
infile.open(fname.c_str(), ios_base::in);
if (infile.is_open()) {
iline=readDataFile(nch, infile, data, err, ang, dataformat, 0);
infile.close();
} else {
std::cout<< "Could not read file " << fname << std::endl;
return -1;
}
return iline;
};
int fileIO::readDataFile(int nch, ifstream &infile, double *data, double *err, double *ang, char dataformat, int offset){
int ichan,iline=0;
int interrupt=0;
double fdata, ferr, fang;
int maxchans;
int ich;
string str;
maxchans=nch;
while (infile.good() and interrupt==0) {
getline(infile,str);
#ifdef VERBOSE
std::cout<< str << std::endl;
#endif
istringstream ssstr(str);
if (ang==NULL) {
ssstr >> ichan >> fdata;
//ich=ichan;
if (ssstr.fail() || ssstr.bad()) {
interrupt=1;
break;
}
// if (ich!=iline)
// std::cout<< "Channel number " << ichan << " does not match with line number " << iline << " " << dataformat << std::endl;
ich=iline;
if (ichan<offset)
continue;
} else {
ssstr >> fang >> fdata;
ich=iline;
}
if (ssstr.fail() || ssstr.bad()) {
interrupt=1;
break;
}
if (err)
ssstr >> ferr;
if (ssstr.fail() || ssstr.bad()) {
interrupt=1;
break;
}
if (ich<maxchans) {
if (ang) {
ang[ich]=fang;
}
data[ich]=fdata;
if (err)
err[ich]=ferr;
iline++;
} // else {
// std::cout<< " too many lines in file: "<< iline << " instead of " << maxchans << std::endl;
// interrupt=1;
// break;
// }
if (iline>=nch) {
interrupt=1;
break;
}
}
return iline;
};
int fileIO::readDataFile(string fname, int *data, int nch){
ifstream infile;
int iline=0;//ichan, idata,
//int interrupt=0;
string str;
#ifdef VERBOSE
std::cout<< "Opening file "<< fname << std::endl;
#endif
infile.open(fname.c_str(), ios_base::in);
if (infile.is_open()) {
iline=readDataFile(infile, data, nch, 0);
infile.close();
} else {
std::cout<< "Could not read file " << fname << std::endl;
return -1;
}
return iline;
};
int fileIO::readDataFile(ifstream &infile, int *data, int nch, int offset){
int ichan, idata, iline=0;
int interrupt=0;
string str;
while (infile.good() and interrupt==0) {
getline(infile,str);
#ifdef VERBOSE
std::cout<< str << std::endl;
#endif
istringstream ssstr(str);
ssstr >> ichan >> idata;
if (ssstr.fail() || ssstr.bad()) {
interrupt=1;
break;
}
// if (ichan!=iline) {
// std::cout<< " Expected channel "<< iline <<" but read channel "<< ichan << std::endl;
// interrupt=1;
// break;
// } else {
if (iline<nch) {
if (ichan>=offset) {
data[iline]=idata;
iline++;
}
} else {
interrupt=1;
break;
}
// }
}
return iline;
};
int fileIO::readDataFile(string fname, short int *data, int nch){
ifstream infile;
int iline=0;//ichan,
//int interrupt=0;
string str;
#ifdef VERBOSE
std::cout<< "Opening file "<< fname << std::endl;
#endif
infile.open(fname.c_str(), ios_base::in);
if (infile.is_open()) {
iline=readDataFile(infile, data, nch, 0);
infile.close();
} else {
std::cout<< "Could not read file " << fname << std::endl;
return -1;
}
return iline;
};
int fileIO::readDataFile(ifstream &infile, short int *data, int nch, int offset){
int ichan, iline=0;
short int idata;
int interrupt=0;
string str;
while (infile.good() and interrupt==0) {
getline(infile,str);
#ifdef VERBOSE
;//std::cout<< str << std::endl;
#endif
istringstream ssstr(str);
ssstr >> ichan >> idata;
if (ssstr.fail() || ssstr.bad()) {
interrupt=1;
break;
}
// if (ichan!=iline) {
// std::cout<< " Expected channel "<< iline <<" but read channel "<< ichan << std::endl;
// interrupt=1;
// break;
// } else {
if (iline<nch) {
if (ichan>=offset) {
data[iline]=idata;
iline++;
}
} else {
interrupt=1;
break;
}
// }
#ifdef VERBOSE
;//std::cout<< "read " << iline <<" channels " << std::endl;
#endif
}
return iline;
}
/*writes raw data file */
int fileIO::writeDataFile(string fname, double *data, double *err, double *ang, char dataformat, int nch){
if (nch==-1)
nch=getTotalNumberofChannels();//getTotalNumberOfChannels();
return writeDataFile(fname, nch, data, err, ang, dataformat);
}
int fileIO::writeDataFile(ofstream &outfile, double *data, double *err, double *ang, char dataformat, int nch, int offset){
if (nch==-1)
nch=getTotalNumberofChannels();
return writeDataFile(outfile, nch, data, err, ang, dataformat, offset);
}
int fileIO::writeDataFile(string fname, int *data){
return writeDataFile(fname, getTotalNumberofChannels(), data);
}
int fileIO::writeDataFile(ofstream &outfile, int *data, int offset){
return writeDataFile(outfile, getTotalNumberofChannels(), data, offset);
}
int fileIO::writeDataFile(string fname, short int *data){
return writeDataFile(fname, getTotalNumberofChannels(), data);
}
int fileIO::writeDataFile(ofstream &outfile, short int *data, int offset){
return writeDataFile(outfile,getTotalNumberofChannels() , data, offset);
}
int fileIO::readDataFile(string fname, double *data, double *err, double *ang, char dataformat) {
return readDataFile(getTotalNumberofChannels(), fname, data, err, ang, dataformat);
}
int fileIO::readDataFile(ifstream &infile, double *data, double *err, double *ang, char dataformat, int offset) {
return readDataFile(getTotalNumberofChannels(), infile, data, err, ang, dataformat, offset);
}
int fileIO::readDataFile(string fname, int *data){
return readDataFile(fname, data, getTotalNumberofChannels());
};
int fileIO::readDataFile(ifstream &infile, int *data, int offset){
return readDataFile(infile, data, getTotalNumberofChannels(), offset);
};
int fileIO::readDataFile(string fname, short int *data){
return readDataFile(fname, data, getTotalNumberofChannels());
};
int fileIO::readDataFile(ifstream &infile, short int *data, int offset){
return readDataFile(infile, data, getTotalNumberofChannels(),offset);
};

View File

@ -0,0 +1,410 @@
#ifndef FILEIOSTD_H
#define FILEIOSTD_H
//#include "slsDetectorBase.h"
#include "sls_detector_defs.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstring>
#include <string>
#include <sstream>
#include <queue>
#include <math.h>
using namespace std;
/**
@short class handling the data file I/O flags
*/
class fileIO : public virtual slsDetectorDefs {//public virtual slsDetectorBase {
public:
/** default constructor */
fileIO(){};
/** virtual destructor */
virtual ~fileIO(){};
int setTotalNumberofChannels(int i){ if (i>=0) {totalNumberofChannels=i; return totalNumberofChannels;} else return -1;};
int getTotalNumberofChannels(){ return totalNumberofChannels; };
/**
sets the default output files path
\param s file path
\return actual file path
*/
string setFilePath(string s) {sprintf(filePath, s.c_str()); return string(filePath);};
/**
sets the default output files root name
\param s file name to be set
\returns actual file name
*/
string setFileName(string s) {sprintf(fileName, s.c_str()); return string(fileName);};
/**
sets the default output file index
\param i start file index to be set
\returns actual file index
*/
int setFileIndex(int i) {*fileIndex=i; return *fileIndex;};
/**
\returns the output files path
*/
string getFilePath() {return string(filePath);};
/**
\returns the output files root name
*/
string getFileName() {return string(fileName);};
/**
\returns the output file index
*/
int getFileIndex() {return *fileIndex;};
/** generates file name without extension
always appends to file path and file name the run index.
in case also appends the position index and the two level of scan varaibles with the specified precision
Filenames will be of the form: filepath/filename(_Sy_sw_px)_i
where y is the scan0 variable, W is the scan 1 variable, x is the position index and i is the run index
\param filepath outdir
\param filename file root name
\param aMask action mask (scans, positions)
\param sv0 scan variable 0
\param prec0 scan precision 0
\param sv1 scan variable 1
\param prec1 scan precision 1
\param pindex position index
\param number of positions
\param findex file index
\returns file name without extension
*/
static string createFileName(char *filepath, char *filename, int aMask, double sv0, int prec0, double sv1, int prec1, int pindex, int npos, int findex);
string createFileName(int aMask, double sv0, int prec0, double sv1, int prec1, int pindex, int npos);
/** static function that returns the file index from the file name
\param fname file name
\returns file index
*/
int getFileIndexFromFileName(string fname);
/** static function that returns the variables from the file name
\param fname file name
\param index reference to index
\param p_index reference to position index
\param sv0 reference to scan variable 0
\param sv1 reference to scan variable 1
\returns file index
*/
static int getVariablesFromFileName(string fname, int &index, int &p_index, double &sv0, double &sv1);
/**
writes a data file
\param fname of the file to be written
\param data array of data values
\param err array of arrors on the data. If NULL no errors will be written
\param ang array of angular values. If NULL data will be in the form chan-val(-err) otherwise ang-val(-err)
\param dataformat format of the data: can be 'i' integer or 'f' double (default)
\param nch number of channels to be written to file. if -1 defaults to the number of installed channels of the detector
\returns OK or FAIL if it could not write the file or data=NULL
*/
virtual int writeDataFile(string fname, double *data, double *err=NULL, double *ang=NULL, char dataformat='f', int nch=-1);
/**
writes a data file
\paramoutfile output file stream
\param data array of data values
\param err array of arrors on the data. If NULL no errors will be written
\param ang array of angular values. If NULL data will be in the form chan-val(-err) otherwise ang-val(-err)
\param dataformat format of the data: can be 'i' integer or 'f' double (default)
\param nch number of channels to be written to file. if -1 defaults to the number of installed channels of the detector
\param offset start channel number
\returns OK or FAIL if it could not write the file or data=NULL
*/
int writeDataFile(ofstream &outfile, double *data, double *err=NULL, double *ang=NULL, char dataformat='f', int nch=-1, int offset=0);
/**
writes a data file
\param fname of the file to be written
\param data array of data values
\returns OK or FAIL if it could not write the file or data=NULL
*/
virtual int writeDataFile(string fname, int *data);
/**
writes a data file
\param outfile output file stream
\param data array of data values
\param offset start channel number
\returns OK or FAIL if it could not write the file or data=NULL
*/
int writeDataFile(ofstream &outfile, int *data, int offset=0);
/**
writes a data file of short ints
\param fname of the file to be written
\param data array of data values
\returns OK or FAIL if it could not write the file or data=NULL
*/
virtual int writeDataFile(string fname, short int *data);
/**
writes a data file of short ints
\param outfile output file stream
\param data array of data values
\param offset start channel number
\returns OK or FAIL if it could not write the file or data=NULL
*/
int writeDataFile(ofstream &outfile, short int *data, int offset=0);
/**
reads a data file
\param fname of the file to be read
\param data array of data values to be filled
\param err array of arrors on the data. If NULL no errors are expected on the file
\param ang array of angular values. If NULL data are expected in the form chan-val(-err) otherwise ang-val(-err)
\param dataformat format of the data: can be 'i' integer or 'f' double (default)
\returns OK or FAIL if it could not read the file or data=NULL
*/
virtual int readDataFile(string fname, double *data, double *err=NULL, double *ang=NULL, char dataformat='f');
/**
reads a data file
\param ifstream input file stream
\param data array of data values to be filled
\param err array of arrors on the data. If NULL no errors are expected on the file
\param ang array of angular values. If NULL data are expected in the form chan-val(-err) otherwise ang-val(-err)
\param offset start channel number to be expected
\returns OK or FAIL if it could not read the file or data=NULL
*/
int readDataFile(ifstream& infile, double *data, double *err=NULL, double *ang=NULL, char dataformat='f', int offset=0);
/**
reads a raw data file
\param fname of the file to be read
\param data array of data values
\returns OK or FAIL if it could not read the file or data=NULL
*/
virtual int readDataFile(string fname, int *data); /**
reads a raw data file
\param infile input file stream
\param data array of data values
\param offset first channel number to be expected
\returns OK or FAIL if it could not read the file or data=NULL
*/
int readDataFile(ifstream &infile, int *data, int offset=0);
/**
reads a short int raw data file
\param fname of the file to be read
\param data array of data values
\returns OK or FAIL if it could not read the file or data=NULL
*/
virtual int readDataFile(string fname, short int *data);
/**
reads a short int raw data file
\param infile input file stream
\param data array of data values
\param offset first channel number to be expected
\returns OK or FAIL if it could not read the file or data=NULL
*/
int readDataFile(ifstream &infile, short int *data, int offset=0);
/**
writes a data file
\param fname of the file to be written
\param nch number of channels to be written
\param data array of data values
\param err array of arrors on the data. If NULL no errors will be written
\param ang array of angular values. If NULL data will be in the form chan-val(-err) otherwise ang-val(-err)
\param dataformat format of the data: can be 'i' integer or 'f' double (default)
\returns OK or FAIL if it could not write the file or data=NULL
*/
static int writeDataFile(string fname, int nch, double *data, double *err=NULL, double *ang=NULL, char dataformat='f');
/**
writes a data file
\param outfile output file stream
\param nch number of channels to be written
\param data array of data values
\param err array of arrors on the data. If NULL no errors will be written
\param ang array of angular values. If NULL data will be in the form chan-val(-err) otherwise ang-val(-err)
\param dataformat format of the data: can be 'i' integer or 'f' double (default)
\param offset start channel number
\returns OK or FAIL if it could not write the file or data=NULL
*/
static int writeDataFile(ofstream &outfile, int nch, double *data, double *err=NULL, double *ang=NULL, char dataformat='f', int offset=0);
/**
writes a raw data file
\param fname of the file to be written
\param nch number of channels
\param data array of data values
\returns OK or FAIL if it could not write the file or data=NULL
*/
static int writeDataFile(string fname,int nch, int *data);
/**
writes a raw data file
\param outfile output file stream
\param nch number of channels
\param data array of data values
\param offset start channel number
\returns OK or FAIL if it could not write the file or data=NULL
*/
static int writeDataFile(ofstream &outfile,int nch, int *data, int offset=0);
/**
writes a short int raw data file
\param fname of the file to be written
\param nch number of channels
\param data array of data values
\returns OK or FAIL if it could not write the file or data=NULL
*/
static int writeDataFile(string fname,int nch, short int *data);
/**
writes a short int raw data file
\param outfile output file stream
\param nch number of channels
\param data array of data values
\param offset start channel number
\returns OK or FAIL if it could not write the file or data=NULL
*/
static int writeDataFile(ofstream &outfile,int nch, short int *data, int offset=0);
/**
reads a data file
\param nch number of channels
\param fname of the file to be read
\param data array of data values to be filled
\param err array of arrors on the data. If NULL no errors are expected on the file
\param ang array of angular values. If NULL data are expected in the form chan-val(-err) otherwise ang-val(-err)
\param dataformat format of the data: can be 'i' integer or 'f' double (default)
\returns number of channels read or -1 if it could not read the file or data=NULL
*/
static int readDataFile(int nch, string fname, double *data, double *err=NULL, double *ang=NULL, char dataformat='f');
/**
reads a data file
\param nch number of channels
\param infile input file stream
\param data array of data values to be filled
\param err array of arrors on the data. If NULL no errors are expected on the file
\param ang array of angular values. If NULL data are expected in the form chan-val(-err) otherwise ang-val(-err)
\param dataformat format of the data: can be 'i' integer or 'f' double (default)
\param offset start channel number
\returns number of channels read or -1 if it could not read the file or data=NULL
*/
static int readDataFile(int nch, ifstream &infile, double *data, double *err=NULL, double *ang=NULL, char dataformat='f', int offset=0);
/**
reads a raw data file
\param fname of the file to be read
\param data array of data values
\param nch number of channels
\returns OK or FAIL if it could not read the file or data=NULL
*/
static int readDataFile(string fname, int *data, int nch);
/**
reads a raw data file
\param infile input file stream
\param data array of data values
\param nch number of channels
\param offset start channel value
\returns OK or FAIL if it could not read the file or data=NULL
*/
static int readDataFile(ifstream &infile, int *data, int nch, int offset);
/**
reads a short int rawdata file
\param name of the file to be read
\param data array of data values
\param nch number of channels
\returns OK or FAIL if it could not read the file or data=NULL
*/
static int readDataFile(string fname, short int *data, int nch);
/**
reads a short int raw data file
\param infile input file stream
\param data array of data values
\param nch number of channels
\param offset start channel value
\returns OK or FAIL if it could not read the file or data=NULL
*/
static int readDataFile(ifstream &infile, short int *data, int nch, int offset);
void incrementFileIndex() { (*fileIndex)++;};
string getCurrentFileName(){return currentFileName;};
protected:
string currentFileName;
int mask_action;
double currentscan_variable[1];
int scan_precision[1];
int currentpostion_i;
int noposition;
/** output directory */
char *filePath;
/** file root name */
char *fileName;
/** file index */
int *fileIndex;
private:
int totalNumberofChannels;
};
#endif

View File

@ -14,6 +14,8 @@ postProcessing::postProcessing(){
pCallbackArg = 0;
registerDataCallback(&defaultDataReadyFunc, NULL);
//cout << "done "<< endl;
}
@ -429,7 +431,7 @@ int postProcessing::fillBadChannelMask() {
//badChannelMask=NULL;
}
}
#ifdef VERBOSE
cout << "number of bad channels is " << nbad << endl;
#endif

View File

@ -0,0 +1,602 @@
#include "postProcessingFileIO_Standalone.h"
#include "usersFunctions.h"
//#include "externPostProcessing.h"
postProcessing::postProcessing(){
pthread_mutex_t mp1 = PTHREAD_MUTEX_INITIALIZER;
mp=mp1;
pthread_mutex_init(&mp, NULL);
mg=mp1;
pthread_mutex_init(&mg, NULL);
//cout << "reg callback "<< endl;
dataReady = 0;
pCallbackArg = 0;
registerDataCallback(&defaultDataReadyFunc, NULL);
//cout << "done "<< endl;
angConv=new angularConversion(numberOfPositions,detPositions,binSize, fineOffset, globalOffset);
IOfile= new fileIO();
//registerCallBackGetChansPerMod(&getChannelPerMod,this);
registerCallBackGetNumberofChannel(&defaultGetTotalNumberofChannels,this);
//registerAngularConversionCallback(&defaultAngularConversion,this);
}
int postProcessing::flatFieldCorrect(double datain, double errin, double &dataout, double &errout, double ffcoefficient, double fferr){
double e;
dataout=datain*ffcoefficient;
if (errin==0 && datain>=0)
e=sqrt(datain);
else
e=errin;
if (dataout>0)
errout=sqrt(e*ffcoefficient*e*ffcoefficient+datain*fferr*datain*fferr);
else
errout=1.0;
return 0;
};
int postProcessing::rateCorrect(double datain, double errin, double &dataout, double &errout, double tau, double t){
// double data;
double e;
dataout=(datain*exp(tau*datain/t));
if (errin==0 && datain>=0)
e=sqrt(datain);
else
e=errin;
if (dataout>0)
errout=e*dataout*sqrt((1/(datain*datain)+tau*tau/(t*t)));
else
errout=1.;
return 0;
};
int postProcessing::setBadChannelCorrection(ifstream &infile, int &nbad, int *badlist, int moff){
int interrupt=0;
int ich;
int chmin,chmax;
string str;
nbad=0;
while (infile.good() and interrupt==0) {
getline(infile,str);
#ifdef VERBOSE
std::cout << str << std::endl;
#endif
istringstream ssstr;
ssstr.str(str);
if (ssstr.bad() || ssstr.fail() || infile.eof()) {
interrupt=1;
break;
}
if (str.find('-')!=string::npos) {
ssstr >> chmin ;
ssstr.str(str.substr(str.find('-')+1,str.size()));
ssstr >> chmax;
#ifdef VERBOSE
std::cout << "channels between"<< chmin << " and " << chmax << std::endl;
#endif
for (ich=chmin; ich<=chmax; ich++) {
if (nbad<MAX_BADCHANS) {
badlist[nbad]=ich;
nbad++;
#ifdef VERBOSE
std::cout<< nbad << " Found bad channel "<< ich << std::endl;
#endif
} else
interrupt=1;
}
} else {
ssstr >> ich;
#ifdef VERBOSE
std::cout << "channel "<< ich << std::endl;
#endif
if (nbad<MAX_BADCHANS) {
badlist[nbad]=ich;
nbad++;
#ifdef VERBOSE
std::cout << nbad << " Found bad channel "<< ich << std::endl;
#endif
} else
interrupt=1;
}
}
for (int ich=0; ich<nbad; ich++) {
badlist[ich]=badlist[ich]+moff;
}
return nbad;
}
void postProcessing::processFrame(int *myData, int delflag) {
string fname;
// double *fdata=NULL;
incrementProgress();
/** decode data */
fdata=decodeData(myData, fdata);
fname=IOfile->createFileName(getActionMask(),getCurrentScanVariable(0),getScanPrecision(0),getCurrentScanVariable(1),getScanPrecision(1),getCurrentPositionIndex(),getNumberOfPositions());
//Checking for write flag
if(*correctionMask&(1<<WRITE_FILE))
{
//uses static function?!?!?!?
IOfile->writeDataFile (fname+string(".raw"),fdata, NULL, NULL, 'i');
}
doProcessing(fdata,delflag, fname);
delete [] myData;
myData=NULL;
fdata=NULL;
#ifdef VERBOSE
cout << "Pop data queue " << *fileIndex << endl;
#endif
pthread_mutex_lock(&mp);
dataQueue.pop(); //remove the data from the queue
queuesize=dataQueue.size();
pthread_mutex_unlock(&mp);
}
void postProcessing::doProcessing(double *lfdata, int delflag, string fname) {
// /** write raw data file */
// if (*correctionMask==0 && delflag==1) {
// // delete [] fdata;
// ;
// } else {
double *rcdata=NULL, *rcerr=NULL;
double *ffcdata=NULL, *ffcerr=NULL;
double *ang=NULL;
// int imod;
int np;
//string fname;
detectorData *thisData;
string ext=".dat";
// fname=createFileName();
/** rate correction */
if (*correctionMask&(1<<RATE_CORRECTION)) {
rcdata=new double[getTotalNumberOfChannels()];
rcerr=new double[getTotalNumberOfChannels()];
rateCorrect(lfdata,NULL,rcdata,rcerr);
delete [] lfdata;
} else {
rcdata=lfdata;
}
lfdata=NULL;
/** flat field correction */
if (*correctionMask&(1<<FLAT_FIELD_CORRECTION)) {
ffcdata=new double[getTotalNumberOfChannels()];
ffcerr=new double[getTotalNumberOfChannels()];
flatFieldCorrect(rcdata,rcerr,ffcdata,ffcerr);
delete [] rcdata;
rcdata=NULL;
if (rcerr)
delete [] rcerr;
rcerr=NULL;
} else {
ffcdata=rcdata;
ffcerr=rcerr;
rcdata=NULL;
rcerr=NULL;
}
// writes angualr converted files
if (*correctionMask!=0) {
if (*correctionMask&(1<< ANGULAR_CONVERSION))
ang=convertAngles();
if(*correctionMask&(1<<WRITE_FILE))
{
IOfile->writeDataFile (fname+ext, ffcdata, ffcerr,ang);}
}
if (*correctionMask&(1<< ANGULAR_CONVERSION) && getNumberOfPositions()>0) {
#ifdef VERBOSE
cout << "**************Current position index is " << getCurrentPositionIndex() << endl;
#endif
// if (*numberOfPositions>0) {setTotalNumberOfChannels
if (getCurrentPositionIndex()<=1) {
#ifdef VERBOSE
cout << "reset merging " << endl;
#endif
angConv->resetMerging();
}
#ifdef VERBOSE
cout << "add to merging "<< getCurrentPositionIndex() << endl;
#endif
angConv->addToMerging(ang, ffcdata, ffcerr, badChannelMask );
#ifdef VERBOSE
cout << getCurrentPositionIndex() << " " << getNumberOfPositions() << endl;
#endif
// cout << "lock 1" << endl;
pthread_mutex_lock(&mp);
if ((getCurrentPositionIndex()>=getNumberOfPositions() && posfinished==1 && queuesize==1)) {
#ifdef VERBOSE
cout << "finalize merging " << getCurrentPositionIndex()<< endl;
#endif
np=angConv->finalizeMerging();
/** file writing */
angConv->incrementPositionIndex();
// cout << "unlock 1" << endl;
pthread_mutex_unlock(&mp);
fname=IOfile->createFileName(getActionMask(),getCurrentScanVariable(0),getScanPrecision(0),getCurrentScanVariable(1),getScanPrecision(1),getCurrentPositionIndex(),getNumberOfPositions());
#ifdef VERBOSE
cout << "writing merged data file" << endl;
#endif
if(*correctionMask&(1<<WRITE_FILE))
{
IOfile->writeDataFile (fname+ext,np,angConv->getMergedCounts(), angConv->getMergedErrors(), angConv->getMergedPositions(),'f');}
#ifdef VERBOSE
cout << " done" << endl;
#endif
// if (delflag) {
// deleteMerging();
// } else {
thisData=new detectorData(angConv->getMergedCounts(),angConv->getMergedErrors(),angConv->getMergedPositions(),getCurrentProgress(),(fname+ext).c_str(),np);
// // cout << "lock 2" << endl;
// pthread_mutex_lock(&mg);
// finalDataQueue.push(thisData);
// // cout << "unlock 2" << endl;
// pthread_mutex_unlock(&mg);
if (dataReady) {
dataReady(thisData, pCallbackArg);
delete thisData;
}
// }
// cout << "lock 3" << endl;
pthread_mutex_lock(&mp);
}
// cout << "unlock 3" << endl;
pthread_mutex_unlock(&mp);
if (ffcdata)
delete [] ffcdata;
ffcdata=NULL;
if (ffcerr)
delete [] ffcerr;
ffcerr=NULL;
if (ang)
delete [] ang;
ang=NULL;
} else {
// if (delflag) {
// if (ffcdata)
// delete [] ffcdata;
// if (ffcerr)
// delete [] ffcerr;
// if ( ang)
// delete [] ang;
// } else {
thisData=new detectorData(ffcdata,ffcerr,NULL,getCurrentProgress(),(fname+ext).c_str(),getTotalNumberOfChannels());
if (dataReady) {
dataReady(thisData, pCallbackArg);
delete thisData;
}
// pthread_mutex_lock(&mg);
// finalDataQueue.push(thisData);
// pthread_mutex_unlock(&mg);
// }
}
//}
if(*correctionMask&(1<<WRITE_FILE))
{IOfile->incrementFileIndex();}
#ifdef VERBOSE
cout << "fdata is " << fdata << endl;
#endif
}
int postProcessing::fillBadChannelMask() {
int nbad=0;
if (*correctionMask&(1<< DISCARD_BAD_CHANNELS)) {
nbad=getBadChannelCorrection();
#ifdef VERBOSE
cout << "number of bad channels is " << nbad << endl;
#endif
if (nbad>0) {
int *badChansList=new int[nbad];
getBadChannelCorrection(badChansList);
if (badChannelMask)
delete [] badChannelMask;
badChannelMask=new int[getTotalNumberOfChannels()];
#ifdef VERBOSE
cout << " pointer to bad channel mask is " << badChannelMask << endl;
#endif
for (int ichan=0; ichan<getTotalNumberOfChannels(); ichan++)
badChannelMask[ichan]=0;
#ifdef VERBOSE
cout << " badChanMask has be reset" << badChannelMask << endl;
#endif
for (int ichan=0; ichan<nbad; ichan++) {
if (badChansList[ichan]<getTotalNumberOfChannels() && badChansList[ichan]>=0 ) {
if (badChannelMask[badChansList[ichan]]==0)
nbad++;
badChannelMask[badChansList[ichan]]=1;
}
}
delete [] badChansList;
} else {
if (badChannelMask) {
#ifdef VERBOSE
cout << "deleting bad channel mask beacuse number of bad channels is 0" << endl;
#endif
delete [] badChannelMask;
badChannelMask=NULL;
}
}
} else {
#ifdef VERBOSE
cout << "bad channel correction is disabled " << nbad << endl;
#endif
if (badChannelMask) {
#ifdef VERBOSE
cout << "deleting bad channel mask beacuse no bad channel correction is selected" << endl;
#endif
delete [] badChannelMask;
badChannelMask=NULL;
}
}
#ifdef VERBOSE
cout << "number of bad channels is " << nbad << endl;
#endif
return nbad;
}
void* postProcessing::processData(int delflag) {
#ifdef VERBOSE
std::cout<< " processing data - threaded mode " << *threadedProcessing << endl;
#endif
angConv->setTotalNumberOfChannels(getTotalNumberOfChannels());
IOfile->setTotalNumberofChannels(getTotalNumberOfChannels());
setTotalProgress();
pthread_mutex_lock(&mp);
queuesize=dataQueue.size();
pthread_mutex_unlock(&mp);
int *myData;
int dum=1;
fdata=NULL;
while(dum | *threadedProcessing) { // ????????????????????????
/* IF THERE ARE DATA PROCESS THEM*/
pthread_mutex_lock(&mp);
while((queuesize=dataQueue.size())>0) {
/** Pop data queue */
myData=dataQueue.front(); // get the data from the queue
pthread_mutex_unlock(&mp);
if (myData) {
processFrame(myData,delflag);
//usleep(1000);
}
pthread_mutex_lock(&mp);
}
pthread_mutex_unlock(&mp);
/* IF THERE ARE NO DATA look if acquisition is finished */
pthread_mutex_lock(&mp);
if (jointhread) {
if (dataQueue.size()==0) {
pthread_mutex_unlock(&mp);
break;
}
pthread_mutex_unlock(&mp);
} else {
pthread_mutex_unlock(&mp);
}
dum=0;
}
if (fdata) {
#ifdef VERBOSE
cout << "delete fdata" << endl;
#endif
delete [] fdata;
#ifdef VERBOSE
cout << "done " << endl;
#endif
}
return 0;
}
int* postProcessing::popDataQueue() {
int *retval=NULL;
if( !dataQueue.empty() ) {
retval=dataQueue.front();
dataQueue.pop();
}
return retval;
}
detectorData* postProcessing::popFinalDataQueue() {
detectorData *retval=NULL;
pthread_mutex_unlock(&mg);
if( !finalDataQueue.empty() ) {
retval=finalDataQueue.front();
finalDataQueue.pop();
}
pthread_mutex_unlock(&mg);
return retval;
}
void postProcessing::resetDataQueue() {
int *retval=NULL;
while( !dataQueue.empty() ) {
retval=dataQueue.front();
dataQueue.pop();
delete [] retval;
}
}
void postProcessing::resetFinalDataQueue() {
detectorData *retval=NULL;
pthread_mutex_lock(&mg);
while( !finalDataQueue.empty() ) {
retval=finalDataQueue.front();
finalDataQueue.pop();
delete retval;
}
pthread_mutex_unlock(&mg);
}
void postProcessing::startThread(int delflag) {
pthread_attr_t tattr;
int ret;
sched_param param, mparam;
int policy= SCHED_OTHER;
// set the priority; others are unchanged
//newprio = 30;
mparam.sched_priority =1;
param.sched_priority =1;
/* Initialize and set thread detached attribute */
pthread_attr_init(&tattr);
pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_JOINABLE);
// param.sched_priority = 5;
// scheduling parameters of main thread
ret = pthread_setschedparam(pthread_self(), policy, &mparam);
//#ifdef VERBOSE
// printf("current priority is %d\n",param.sched_priority);
//#endif
if (delflag)
ret = pthread_create(&dataProcessingThread, &tattr,startProcessData, (void*)this);
else
ret = pthread_create(&dataProcessingThread, &tattr,startProcessDataNoDelete, (void*)this);
pthread_attr_destroy(&tattr);
// scheduling parameters of target thread
ret = pthread_setschedparam(dataProcessingThread, policy, &param);
}

View File

@ -0,0 +1,391 @@
#ifndef POSTPROCESSING_H
#define POSTPROCESSING_H
#include "detectorData.h"
#include "sls_detector_defs.h"
#include "slsDetectorBase.h"
#include "slsDetectorUsers.h"
#include "FileIO_Standalone.h"
#include "AngularConversion_Standalone.h"
//#include "externPostProcessing.h"
#include <string>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstring>
#include <string>
#include <sstream>
#include <queue>
#include <math.h>
class angularConversion;
class fileIO;
using namespace std;
#define MAX_BADCHANS 2000
#define defaultTDead {170,90,750} /**< should be changed in order to have it separate for the different detector types */
/**
@short methods for data postprocessing
(including thread for writing data files and plotting in parallel with the acquisition)
*/
class postProcessing : public virtual slsDetectorBase {
//: public angularConversion, public fileIO
public:
postProcessing();
virtual ~postProcessing(){};
/**
get bad channels correction
\param bad pointer to array that if bad!=NULL will be filled with the bad channel list
\returns 0 if bad channel disabled or no bad channels, >0 otherwise
*/
virtual int getBadChannelCorrection(int *bad=NULL)=0;
/**
get flat field corrections
\param corr if !=NULL will be filled with the correction coefficients
\param ecorr if !=NULL will be filled with the correction coefficients errors
\returns 0 if ff correction disabled, >0 otherwise
*/
virtual int getFlatFieldCorrection(double *corr=NULL, double *ecorr=NULL)=0;
/**
set flat field corrections
\param corr if !=NULL the flat field corrections will be filled with corr (NULL usets ff corrections)
\param ecorr if !=NULL the flat field correction errors will be filled with ecorr (1 otherwise)
\returns 0 if ff correction disabled, >0 otherwise
*/
virtual int setFlatFieldCorrection(double *corr, double *ecorr=NULL)=0;
/**
set bad channels correction
\param fname file with bad channel list ("" disable)
\returns 0 if bad channel disabled, >0 otherwise
*/
virtual int setBadChannelCorrection(string fname="")=0;
static int setBadChannelCorrection(ifstream &infile, int &nbad, int *badlist, int moff=0);
/**
set bad channels correction
\param fname file with bad channel list ("" disable)
\param nbad reference to number of bad channels
\param badlist array of badchannels
\returns 0 if bad channel disabled, >0 otherwise
*/
virtual int setBadChannelCorrection(string fname, int &nbad, int *badlist, int off=0)=0;
/**
set bad channels correction
\param nch number of bad channels
\param chs array of channels
\param ff 0 if normal bad channels, 1 if ff bad channels
\returns 0 if bad channel disabled, >0 otherwise
*/
virtual int setBadChannelCorrection(int nch, int *chs, int ff=0)=0;
/**
flat field correct data
\param datain data
\param errin error on data (if<=0 will default to sqrt(datain)
\param dataout corrected data
\param errout error on corrected data
\param ffcoefficient flat field correction coefficient
\param fferr erro on ffcoefficient
\returns 0
*/
static int flatFieldCorrect(double datain, double errin, double &dataout, double &errout, double ffcoefficient, double fferr);
/**
rate correct data
\param datain data
\param errin error on data (if<=0 will default to sqrt(datain)
\param dataout corrected data
\param errout error on corrected data
\param tau dead time 9in ns)
\param t acquisition time (in ns)
\returns 0
*/
static int rateCorrect(double datain, double errin, double &dataout, double &errout, double tau, double t);
int enableWriteToFile(int i=-1) {if (i>0) ((*correctionMask)|=(1<<WRITE_FILE)); if(i==0) ((*correctionMask)&=~(1<< WRITE_FILE)); return ((*correctionMask)&(1<< WRITE_FILE));};
int setAngularCorrectionMask(int i=-1){if (i==0) (*correctionMask)&=~(1<< ANGULAR_CONVERSION); if (i>0) (*correctionMask)|=(1<< ANGULAR_CONVERSION); return ((*correctionMask)&(1<< ANGULAR_CONVERSION));};
int enableAngularConversion(int i=-1) {if (i>0) return setAngularConversionFile("default"); if (i==0) return setAngularConversionFile(""); return setAngularCorrectionMask();};
int enableBadChannelCorrection(int i=-1) {if (i>0) return setBadChannelCorrection("default"); if (i==0) return setBadChannelCorrection(""); return ((*correctionMask)&(1<< DISCARD_BAD_CHANNELS));};
/** returns the bad channel list file */
string getBadChannelCorrectionFile() {if ((*correctionMask)&(1<< DISCARD_BAD_CHANNELS)) return string(badChanFile); else return string("none");};
/**
get flat field corrections file directory
\returns flat field correction file directory
*/
string getFlatFieldCorrectionDir(){return string(flatFieldDir);};
/**
set flat field corrections file directory
\param flat field correction file directory
\returns flat field correction file directory
*/
string setFlatFieldCorrectionDir(string dir){strcpy(flatFieldDir,dir.c_str()); return string(flatFieldDir);};
/**
get flat field corrections file name
\returns flat field correction file name
*/
string getFlatFieldCorrectionFile(){ if ((*correctionMask)&(1<<FLAT_FIELD_CORRECTION)) return string(flatFieldFile); else return string("none");};
int getAngularConversion(int &direction, angleConversionConstant *angoff=NULL);
/**
set/get if the data processing and file writing should be done by a separate thread
s
\param b 0 sequencial data acquisition and file writing, 1 separate thread, -1 get
\returns thread flag
*/
int setThreadedProcessing(int b=-1) {if (b>=0) *threadedProcessing=b; return *threadedProcessing;};
/** processes the data
\param delflag 0 leaves the data in the final data queue
\returns nothing
*/
void *processData(int delflag);
/** processes the data
\param delflag 0 leaves the data in the final data queue
\returns nothing
*/
void processFrame(int* myData, int delflag);
/** processes the data
\param delflag 0 leaves the data in the final data queue
\returns nothing
*/
void doProcessing(double* myData, int delflag, string fname);
/**
pops the data from the data queue
\returns pointer to the popped data or NULL if the queue is empty.
\sa dataQueue
*/
int* popDataQueue();
/**
pops the data from thepostprocessed data queue
\returns pointer to the popped data or NULL if the queue is empty.
\sa finalDataQueue
*/
detectorData* popFinalDataQueue();
/**
resets the raw data queue
\sa dataQueue
*/
void resetDataQueue();
/**
resets the postprocessed data queue
\sa finalDataQueue
*/
void resetFinalDataQueue();
int fillBadChannelMask();
virtual int rateCorrect(double*, double*, double*, double*)=0;
virtual int flatFieldCorrect(double*, double*, double*, double*)=0;
//void registerAngularConversionCallback(int (*sAngularConversion)(int & ,angleConversionConstant* ,void *), void *arg){seAngularConversion=sAngularConversion; pAngular= arg; };
void registerDataCallback(int( *userCallback)(detectorData*, void*), void *pArg) {dataReady = userCallback; pCallbackArg = pArg;};
void registerCallBackGetNumberofChannel(int (*func)(int, void *),void *arg){ getNoChannel=func;pNumberofChannel=arg;};
/**
sets the angular conversion file
\param fname file to read
\returns angular conversion flag
*/
int setAngularConversionFile(string fname);
//static int defaultAngularConversion(int &direc, angleConversionConstant *angoff,void *p=NULL){ return ((postProcessing *)p)->getAngularConversion( direc,angleConversionConstant *angoff=NULL);};
/**
returns the angular conversion file
*/
string getAngularConversionFile(){if (setAngularCorrectionMask()) return string(angConvFile); else return string("none");};
//static int setAngularConversion();
static int defaultGetTotalNumberofChannels (int nChannel, void *p=NULL){ if(nChannel>=0){ return ((postProcessing*)p)->getTotalNumberOfChannels();} else return -1;};
protected:
int *threadedProcessing;
int *correctionMask;
char *flatFieldDir;
char *flatFieldFile;
char *badChanFile;
int *nBadChans;
int *badChansList;
int *nBadFF;
int *badFFList;
int *direction;
/** pointer to angular conversion file name*/
char *angConvFile;
/** mutex to synchronize main and data processing threads */
pthread_mutex_t mp;
/** mutex to synchronizedata processing and plotting threads */
pthread_mutex_t mg;
/** sets when the acquisition is finished */
int jointhread;
/** sets when the position is finished */
int posfinished;
/**
data queue
*/
queue<int*> dataQueue;
/**
queue containing the postprocessed data
*/
queue<detectorData*> finalDataQueue;
/** data queue size */
int queuesize;
/**
start data processing thread
*/
void startThread(int delflag=1); //
/** the data processing thread */
pthread_t dataProcessingThread;
/** pointer to bad channel mask 0 is channel is good 1 if it is bad \sa fillBadChannelMask() */
int *badChannelMask;
/**
I0 measured
*/
double currentI0;
double *fdata;
int (*seAngularConversion)(int & ,angleConversionConstant* ,void*);
int (*getNoChannel)(int ,void*);
int (*dataReady)(detectorData*,void*);
void *pCallbackArg, *pChpermod,*pNumberofChannel,*pAngular;
private:
angularConversion *angConv;
fileIO *IOfile;
/** pointer to beamlien fine offset*/
double *fineOffset;
/** pointer to beamlien global offset*/
double *globalOffset;
/** pointer to number of positions for the acquisition*/
int *numberOfPositions;
/** pointer to the detector positions for the acquisition*/
double *detPositions;
angleConversionConstant angcc[MAXMODS*MAXDET];
/** pointer to angular bin size*/
double *binSize;
};
static void* startProcessData(void *n){\
postProcessing *myDet=(postProcessing*)n;\
myDet->processData(1);\
pthread_exit(NULL);\
};
static void* startProcessDataNoDelete(void *n){\
postProcessing *myDet=(postProcessing*)n;\
myDet->processData(0);\
pthread_exit(NULL);\
};
#endif

View File

@ -1,7 +1,8 @@
#include "postProcessing.h"
#include "usersFunctions.h"
#include "angularConversion.h"
//#include "angularConversion.h"
//#include "AngularConversion_Standalone.h"
postProcessing::postProcessing(){
pthread_mutex_t mp1 = PTHREAD_MUTEX_INITIALIZER;
@ -13,6 +14,7 @@ postProcessing::postProcessing(){
dataReady = 0;
pCallbackArg = 0;
registerDataCallback(&defaultDataReadyFunc, NULL);
//tregisterCallBackGetChansPerMod(&defaultGetChansPerMod,NULL);
//cout << "done "<< endl;
angConv=new angularConversion(numberOfPositions,detPositions,binSize, fineOffset, globalOffset);
}

View File

@ -58,7 +58,7 @@ class postProcessing : public virtual fileIO {
\param ecorr if !=NULL will be filled with the correction coefficients errors
\returns 0 if ff correction disabled, >0 otherwise
*/
virtual int getFlatFieldCorrection(double *corr=NULL, double *ecorr=NULL)=0;
virtual int getFlatFieldCorrection(float *corr=NULL, float *ecorr=NULL)=0;
/**
set flat field corrections
@ -66,7 +66,7 @@ class postProcessing : public virtual fileIO {
\param ecorr if !=NULL the flat field correction errors will be filled with ecorr (1 otherwise)
\returns 0 if ff correction disabled, >0 otherwise
*/
virtual int setFlatFieldCorrection(double *corr, double *ecorr=NULL)=0;
virtual int setFlatFieldCorrection(float *corr, float *ecorr=NULL)=0;
/**
set bad channels correction
@ -79,6 +79,8 @@ class postProcessing : public virtual fileIO {
/**
set bad channels correction
\param fname file with bad channel list ("" disable)
ff
\param nbad reference to number of bad channels
\param badlist array of badchannels
\returns 0 if bad channel disabled, >0 otherwise
@ -105,7 +107,7 @@ class postProcessing : public virtual fileIO {
\param fferr erro on ffcoefficient
\returns 0
*/
static int flatFieldCorrect(double datain, double errin, double &dataout, double &errout, double ffcoefficient, double fferr);
static int flatFieldCorrect(float datain, float errin, float &dataout, float &errout, float ffcoefficient, float fferr);
/**
rate correct data
@ -117,7 +119,7 @@ class postProcessing : public virtual fileIO {
\param t acquisition time (in ns)
\returns 0
*/
static int rateCorrect(double datain, double errin, double &dataout, double &errout, double tau, double t);
static int rateCorrect(float datain, float errin, float &dataout, float &errout, float tau, float t);
int enableWriteToFile(int i=-1) {if (i>0) ((*correctionMask)|=(1<<WRITE_FILE)); if(i==0) ((*correctionMask)&=~(1<< WRITE_FILE)); return ((*correctionMask)&(1<< WRITE_FILE));};
@ -190,7 +192,7 @@ s
\returns nothing
*/
void doProcessing(double* myData, int delflag, string fname);
void doProcessing(float* myData, int delflag, string fname);
/**
@ -229,8 +231,8 @@ s
virtual int rateCorrect(double*, double*, double*, double*)=0;
virtual int flatFieldCorrect(double*, double*, double*, double*)=0;
virtual int rateCorrect(float*, float*, float*, float*)=0;
virtual int flatFieldCorrect(float*, float*, float*, float*)=0;
@ -239,7 +241,7 @@ s
void registerDataCallback(int( *userCallback)(detectorData*, void*), void *pArg) {dataReady = userCallback; pCallbackArg = pArg;};
//void registerCallBackGetChansPerMod(int (*func)(int, void *),void *arg){ getChansPerMod=func;pChpermod=arg;}
@ -326,14 +328,16 @@ s
/**
I0 measured
*/
double currentI0;
float currentI0;
double *fdata;
float *fdata;
//int (*getChansPerMod)(int, void*);
int (*dataReady)(detectorData*,void*);
void *pCallbackArg;
void *pCallbackArg, *pChpermod;