AngularConversion splitted in a static file and in one file dependent on slsDetectorBase

git-svn-id: file:///afs/psi.ch/project/sls_det_software/svn/slsDetectorSoftware@240 951219d9-93cf-4727-9268-0efd64621fa3
This commit is contained in:
bergamaschi
2012-09-05 10:12:49 +00:00
parent ca75754b83
commit 6b34cb8e71
14 changed files with 1636 additions and 380 deletions

View File

@ -4,15 +4,16 @@
#include <fstream>
#include <sstream>
#include <math.h>
#include "usersFunctions.h"
using namespace std;
angularConversion::angularConversion(): currentPosition(0),
currentPositionIndex(0)
angularConversion::angularConversion(): angularConversionStatic(), currentPosition(0),
currentPositionIndex(0)
{
//angleFunctionPointer=0;
registerAngleFunctionCallback(&defaultAngleFunction);
// registerAngleFunctionCallback(&defaultAngleFunction);
}
@ -20,267 +21,33 @@ angularConversion::~angularConversion(){
}
// int angularConversion::setAngularConversionPointer(angleConversionConstant *p, int *nm, int nch, int idet) {
// if (p) {
// angOff[idet]=p;
// nMods[idet]=nm;
// nCh[idet]=nch;
// } else {
// angOff[idet]=NULL;
// nMods[idet]=NULL;
// }
// return OK;
// }
double* angularConversion::convertAngles(double pos) {
int imod=0;
double *ang=new double[getTotalNumberOfChannels()];
double enc=pos;
angleConversionConstant *p=NULL;
int ch0=0;
int chlast=getChansPerMod(0);
int nchmod=getChansPerMod(0);
p=getAngularConversionPointer(imod);
if (getMoveFlag(imod)==0)
enc=0;
else
enc=pos;
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=getAngularConversionPointer(imod);
if (getMoveFlag(imod)==0)
enc=0;
else
enc=pos;
ch0=chlast;
nchmod=getChansPerMod(imod);
if (nchmod>0)
chlast+=nchmod;
}
if (p)
ang[ip]=angle(ip-ch0, \
enc, \
(*fineOffset)+(*globalOffset), \
p->r_conversion, \
p->center, \
p->offset, \
p->tilt, \
*angDirection );
}
return ang;
}
//static!
int angularConversion::readAngularConversion(string fname, int nmod, angleConversionConstant *angOff) {
ifstream infile;
string ss;
#ifdef VERBOSE
std::cout<< "Opening file "<< fname << std::endl;
#endif
infile.open(fname.c_str(), ios_base::in);
if (infile.is_open()) {
readAngularConversion(infile, nmod, angOff);
infile.close();
} else {
std::cout<< "Could not open calibration file "<< fname << std::endl;
return -1;
}
return 0;
}
//static
int angularConversion::readAngularConversion( ifstream& infile, int nmod, angleConversionConstant *angOff) {
string str;
int mod;
double center, ecenter;
double r_conv, er_conv;
double off, eoff;
string ss;
int interrupt=0;
int nm=0;
//" module %i center %E +- %E conversion %E +- %E offset %f +- %f \n"
while (infile.good() and interrupt==0) {
getline(infile,str);
#ifdef VERBOSE
cout << "** mod " << nm << " " ;
std::cout<< str << std::endl;
#endif
istringstream ssstr(str);
ssstr >> ss >> mod;
ssstr >> ss >> center;
ssstr >> ss >> ecenter;
ssstr >> ss >> r_conv;
ssstr >> ss >> er_conv;
ssstr >> ss >> off;
ssstr >> ss >> eoff;
if (nm<nmod && nm>=0 ) {
angOff[nm].center=center;
angOff[nm].r_conversion=r_conv;
angOff[nm].offset=off;
angOff[nm].ecenter=ecenter;
angOff[nm].er_conversion=er_conv;
angOff[nm].eoffset=eoff;
} else
break;
//cout << nm<<" " << angOff[nm].offset << endl;
nm++;
if (nm>=nmod)
break;
int nmod=getNMods();
int *chansPerMod=new int[nmod];
angleConversionConstant **angOff=new angleConversionConstant*[nmod];
int *mF=new int[nmod];
double fo=*fineOffset;
double go=*globalOffset;
int angdir=*angDirection;
for (int im=0; im<nmod; im++) {
angOff[im]=getAngularConversionPointer(im);
mF[im]=getMoveFlag(im);
chansPerMod[im]=getChansPerMod(im);
}
return nm;
}
//static
int angularConversion:: writeAngularConversion(string fname, int nmod, angleConversionConstant *angOff) {
return angularConversionStatic::convertAngles(pos, getTotalNumberOfChannels(), chansPerMod, angOff,mF, fo, go, angdir);
ofstream outfile;
outfile.open (fname.c_str(),ios_base::out);
if (outfile.is_open())
{
writeAngularConversion(outfile, nmod, angOff);
outfile.close();
} else {
std::cout<< "Could not open file " << fname << "for writing"<< std::endl;
return -1;
}
//" module %i center %E +- %E conversion %E +- %E offset %f +- %f \n"
return 0;
}
//static
int angularConversion:: writeAngularConversion(ofstream& outfile, int nmod, angleConversionConstant *angOff) {
for (int imod=0; imod<nmod; imod++) {
outfile << " module " << imod << " center "<< angOff[imod].center<<" +- "<< angOff[imod].ecenter<<" conversion "<< angOff[imod].r_conversion << " +- "<< angOff[imod].er_conversion << " offset "<< angOff[imod].offset << " +- "<< angOff[imod].eoffset << std::endl;
}
return 0;
}
//static
int angularConversion::resetMerging(double *mp, double *mv, double *me, int *mm, int nb) {
#ifdef VERBOSE
cout << "creating merging arrays "<< nb << endl;
#endif
for (int ibin=0; ibin<nb; ibin++) {
mp[ibin]=0;
mv[ibin]=0;
me[ibin]=0;
mm[ibin]=0;
}
return OK;
}
//static
int angularConversion::finalizeMerging(double *mp, double *mv, double *me, int *mm,int nb) {
int np=0;
for (int ibin=0; ibin<nb; ibin++) {
if (mm[ibin]>0) {
#ifdef VERBOSE
cout << "finalize " << ibin << " "<< mm[ibin] << " " << mp[ibin]<< mv[ibin] << me[ibin] << endl;
#endif
mp[np]=mp[ibin]/mm[ibin];
mv[np]=mv[ibin]/mm[ibin];
me[np]=me[ibin]/mm[ibin];
me[np]=sqrt(me[ibin]);
mm[np]=mm[ibin];
np++;
}
}
return np;
}
//static
int angularConversion::addToMerging(double *p1, double *v1, double *e1, double *mp, double *mv,double *me, int *mm, int nchans, double binsize,int nbins, int *badChanMask ) {
double binmi=-180.;
int ibin=0;
if (p1==NULL)
return 0;
if (v1==NULL)
return FAIL;
if (mp==NULL) //can be changed if we want to use a fixed bin algorithm!
return FAIL;
if (mv==NULL)
return FAIL;
if (me==NULL)
return FAIL;
if (mm==NULL)
return FAIL;
if (nchans==0)
return FAIL;
if (binsize<=0)
return FAIL;
if (nbins<=0)
return FAIL;
for (int ip=0; ip<nchans; ip++) {
if (badChanMask) {
if (badChanMask[ip]) {
#ifdef VERBOSE
cout << "channel " << ip << " is bad " << endl;
#endif
continue;
}
}
ibin=(int)((p1[ip]-binmi)/binsize);
if (ibin<nbins && ibin>=0) {
mp[ibin]+=p1[ip];
mv[ibin]+=v1[ip];
if (e1)
me[ibin]+=(e1[ip]*e1[ip]);
else
me[ibin]+=v1[ip];
mm[ibin]++;
#ifdef VERBOSE
cout << "add " << ibin << " "<< mm[ibin] << " " << mp[ibin]<< mv[ibin] << me[ibin] << endl;
#endif
} else
return FAIL;
}
return OK;
}
int angularConversion::deleteMerging() {
@ -317,7 +84,7 @@ int angularConversion::resetMerging() {
int angularConversion::resetMerging(double *mp, double *mv, double *me, int *mm) {
getAngularConversionParameter(BIN_SIZE);
if (nBins)
return resetMerging(mp, mv, me, mm,nBins);
return angularConversionStatic::resetMerging(mp, mv, me, mm,nBins);
else
return FAIL;
}
@ -340,7 +107,7 @@ int angularConversion::finalizeMerging() {
int angularConversion::finalizeMerging(double *mp, double *mv, double *me, int *mm) {
if (nBins)
return finalizeMerging(mp, mv, me, mm, nBins);
return angularConversionStatic::finalizeMerging(mp, mv, me, mm, nBins);
else
return FAIL;
}
@ -373,7 +140,7 @@ int angularConversion::addToMerging(double *p1, double *v1, double *e1, double
}
int ret=addToMerging(p1, v1, e1, mp, mv,me, mm,getTotalNumberOfChannels(), *binSize,nBins, badChanMask );
int ret=angularConversionStatic::addToMerging(p1, v1, e1, mp, mv,me, mm,getTotalNumberOfChannels(), *binSize,nBins, badChanMask );
if (del) {