diff --git a/slsDetectorSoftware/Makefile b/slsDetectorSoftware/Makefile index d9f9d1fe7..a75850477 100644 --- a/slsDetectorSoftware/Makefile +++ b/slsDetectorSoftware/Makefile @@ -9,7 +9,7 @@ INCLUDES= -IcommonFiles -IslsDetector -IMySocketTCP -IusersFunctions -ImultiSlsD CC=g++ -SRC_CLNT= slsDetectorAnalysis/fileIO.cpp MySocketTCP/MySocketTCP.cpp usersFunctions/usersFunctions.cpp slsDetector/slsDetectorUtils.cpp slsDetector/slsDetectorCommand.cpp slsDetectorAnalysis/angularConversion.cpp slsDetectorAnalysis/angularConversionStatic.cpp slsDetectorAnalysis/energyConversion.cpp slsDetector/slsDetectorActions.cpp slsDetectorAnalysis/postProcessing.cpp slsDetector/slsDetector.cpp multiSlsDetector/multiSlsDetector.cpp slsDetector/slsDetectorUsers.cpp +SRC_CLNT= slsDetectorAnalysis/fileIO.cpp MySocketTCP/MySocketTCP.cpp usersFunctions/usersFunctions.cpp slsDetector/slsDetectorUtils.cpp slsDetector/slsDetectorCommand.cpp slsDetectorAnalysis/angularConversion.cpp slsDetectorAnalysis/angularConversionStatic.cpp slsDetectorAnalysis/energyConversion.cpp slsDetector/slsDetectorActions.cpp slsDetectorAnalysis/postProcessing.cpp slsDetector/slsDetector.cpp multiSlsDetector/multiSlsDetector.cpp slsDetector/slsDetectorUsers.cpp slsDetectorAnalysis/postProcessingFuncs.cpp OBJS = $(SRC_CLNT:.cpp=.o) diff --git a/slsDetectorSoftware/commonFiles/communication_funcs.c b/slsDetectorSoftware/commonFiles/communication_funcs.c index 683819dc2..18fa431dd 100755 --- a/slsDetectorSoftware/commonFiles/communication_funcs.c +++ b/slsDetectorSoftware/commonFiles/communication_funcs.c @@ -225,19 +225,7 @@ int acceptConnection(int socketDescriptor) { } } } - /* for (j=0; j 0) */ - - + return file_des; @@ -312,8 +300,8 @@ void exitServer(int socketDescriptor) { if (total_received>0) strcpy(thisClientIP,dummyClientIP); - if (strcmp(lastClientIP,"none")==0) - strcpy(lastClientIP,thisClientIP); + //if (strcmp(lastClientIP,"none")==0) + //strcpy(lastClientIP,thisClientIP); if (strcmp(lastClientIP,thisClientIP)) differentClients=1; diff --git a/slsDetectorSoftware/commonFiles/sls_detector_defs.h b/slsDetectorSoftware/commonFiles/sls_detector_defs.h index 4ab38efb8..33e63f884 100755 --- a/slsDetectorSoftware/commonFiles/sls_detector_defs.h +++ b/slsDetectorSoftware/commonFiles/sls_detector_defs.h @@ -497,7 +497,9 @@ enum angleConversionParameter { GLOBAL_OFFSET, /**< global offset of the diffractometer */ FINE_OFFSET, /**< fine offset of the diffractometer */ BIN_SIZE, /**< angular bin size */ - MOVE_FLAG /**< wether the detector moves with the motor or not in a multi detector system */ + MOVE_FLAG, /**< wether the detector moves with the motor or not in a multi detector system */ + SAMPLE_X, /**< sample displacement in the beam direction */ + SAMPLE_Y /**< sample displacement orthogonal to the beam */ }; //typedef struct { diff --git a/slsDetectorSoftware/f90Interface/externPostProcessing.cpp b/slsDetectorSoftware/f90Interface/externPostProcessing.cpp index 0ce755e59..92b83c576 100644 --- a/slsDetectorSoftware/f90Interface/externPostProcessing.cpp +++ b/slsDetectorSoftware/f90Interface/externPostProcessing.cpp @@ -23,26 +23,25 @@ int externPostProcessing::InitDataset(int *nModules,int *chPerMod,int moduleMask { init_dataset(nModules,chPerMod,moduleMask,badChans,ffcoeff,fferr,tDead,angRadius,angOffset,angCentre,totalOffset,binSize,sampleX,sampleY); - return 0; } -int externPostProcessing::finalizeDataset(double ang[], double val[], double err[]) +int externPostProcessing::finalizeDataset(double ang[], double val[], double err[], int *np) { -cout<<"Finalize Dataset"< @@ -34,13 +30,10 @@ void calculate_flat_field(int *nMod, int *chPerMod, int *modMask,int *badChanMas -class externPostProcessing : public virtual slsDetectorBase1 +class externPostProcessing { - - - public: externPostProcessing(){}; virtual ~externPostProcessing(){}; @@ -50,11 +43,11 @@ class externPostProcessing : public virtual slsDetectorBase1 - int finalizeDataset(double ang[], double val[], double err[]); + static int finalizeDataset(double ang[], double val[], double err[], int *np); - int addFrame(double data[], double *pos, double *IO, double expTime, const char *filename, int *var=0); + static int addFrame(double data[], double *pos, double *IO, double expTime, const char *filename, int *var=0); - int calculateFlatField(int* nModules, int badChannelMask[], double ffData[], double ffCoeff[], double ffErr[]); + static int calculateFlatField(int* nModules, int *chPerMod, int moduleMask[], int badChannelMask[], double ffData[], double ffCoeff[], double ffErr[]); diff --git a/slsDetectorSoftware/f90Interface/sls_detector_analysis_f.f90 b/slsDetectorSoftware/f90Interface/sls_detector_analysis_f.f90 index 0016d7ec1..201371ef4 100644 --- a/slsDetectorSoftware/f90Interface/sls_detector_analysis_f.f90 +++ b/slsDetectorSoftware/f90Interface/sls_detector_analysis_f.f90 @@ -106,10 +106,10 @@ end subroutine finalizedataset -subroutine addframe(data, pos, i0, fname, var) bind(c, name='add_frame') +subroutine addframe(data, pos, i0, exptime, fname, var) bind(c, name='add_frame') IMPLICIT NONE real(C_DOUBLE), intent(IN) :: data(totalnumberofchannels) -real(C_DOUBLE), intent(IN) :: pos, i0, var +real(C_DOUBLE), intent(IN) :: pos, i0, exptime, var character(kind=c_char), dimension(*), intent(IN) :: fname integer :: l integer :: i @@ -132,6 +132,9 @@ print*,pos print*,'I0: ' print*,i0 +print*,'Exposure time: ' +print*,exptime + print*,'Var: ' print*,var diff --git a/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.cpp b/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.cpp index b067bbd6d..19a6deeb8 100644 --- a/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.cpp +++ b/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.cpp @@ -14,6 +14,7 @@ ID: $Id$ #include "slsDetector.h" #include "multiSlsDetectorCommand.h" #include "multiSlsDetectorClient.h" +#include "postProcessingFuncs.h" #include "usersFunctions.h" #include #include @@ -205,40 +206,12 @@ multiSlsDetector::multiSlsDetector(int id) : slsDetectorUtils(), shmId(-1) for (int i=thisMultiDetector->numberOfDetectors; ilastPID=getpid(); -// getPointers(&thisMultiDetector->stoppedFlag, \ -// &thisMultiDetector->threadedProcessing, \ -// &thisMultiDetector->actionMask, \ -// thisMultiDetector->actionScript, \ -// thisMultiDetector->actionParameter, \ -// thisMultiDetector->nScanSteps, \ -// thisMultiDetector->scanMode, \ -// thisMultiDetector->scanScript, \ -// thisMultiDetector->scanParameter, \ -// thisMultiDetector->scanSteps, \ -// thisMultiDetector->scanPrecision, \ -// &thisMultiDetector->numberOfPositions, \ -// thisMultiDetector->detPositions, \ -// thisMultiDetector->angConvFile, \ -// &thisMultiDetector->correctionMask, \ -// &thisMultiDetector->binSize, \ -// &thisMultiDetector->fineOffset, \ -// &thisMultiDetector->globalOffset, \ -// &thisMultiDetector->angDirection, \ -// thisMultiDetector->flatFieldDir, \ -// thisMultiDetector->flatFieldFile, \ -// thisMultiDetector->badChanFile, \ -// thisMultiDetector->timerValue, \ -// &thisMultiDetector->currentSettings, \ -// &thisMultiDetector->currentThresholdEV, \ -// thisMultiDetector->filePath, \ -// thisMultiDetector->fileName, \ -// &thisMultiDetector->fileIndex); @@ -272,6 +245,10 @@ multiSlsDetector::multiSlsDetector(int id) : slsDetectorUtils(), shmId(-1) fileIndex=&thisMultiDetector->fileIndex; moveFlag=NULL; + sampleDisplacement=thisMultiDetector->sampleDisplacement; + + getNMods(); + getMaxMods(); } @@ -337,7 +314,9 @@ int multiSlsDetector::addSlsDetector(int id, int pos) { thisMultiDetector->numberOfChannels+=detectors[pos]->getTotalNumberOfChannels(); thisMultiDetector->maxNumberOfChannels-=detectors[j]->getMaxNumberOfChannels(); - + getMaxMods(); + getNMods(); + #ifdef VERBOSE cout << "Detector added " << thisMultiDetector->numberOfDetectors<< endl; @@ -376,6 +355,11 @@ string multiSlsDetector::setHostname(const char* name, int pos){ } } } +#ifdef VERBOSE + cout << "-----------------------------set online!" << endl; +#endif + setOnline(ONLINE_FLAG); + return getHostname(pos); } @@ -583,8 +567,10 @@ int multiSlsDetector::addSlsDetector(const char *name, int pos) { cout << "Creating detector " << id << " of type " << getDetectorType(t) << endl; #endif s=new slsDetector(t, id); - if (online) + if (online) { s->setTCPSocket(name); + setOnline(ONLINE_FLAG); + } delete s; } #ifdef VERBOSE @@ -871,7 +857,8 @@ slsDetectorDefs::synchronizationMode multiSlsDetector::setSynchronization(synchr int multiSlsDetector::setOnline(int off) { - + int retdet; + if (off!=GET_ONLINE_FLAG) { thisMultiDetector->onlineFlag=off; for (int i=0; inumberOfDetectors; i++) { @@ -1182,11 +1169,6 @@ int* multiSlsDetector::getDataFromDetector() { #ifdef VERBOSE cout << "Detector " << id << " returned " << n << " bytes " << endl; #endif - // memcpy(p,retdet,n); - //#ifdef VERBOSE - //cout << "Copied to pointer "<< p << endl; - //#endif - // delete [] retdet; } else { nodatadet=id; #ifdef VERBOSE @@ -1296,7 +1278,7 @@ int* multiSlsDetector::readAll(){ //#else // std::cout << std::endl; #endif - return dataQueue.front(); // check what we return! + return dataQueueFront(); // check what we return! }; @@ -1337,7 +1319,7 @@ int* multiSlsDetector::startAndReadAll(){ //#else // std::cout << std::endl; #endif - return dataQueue.front(); // check what we return! + return dataQueueFront(); // check what we return! }; @@ -1637,6 +1619,13 @@ double* multiSlsDetector::decodeData(int *datain, double *fdata) { + + + + + + + int multiSlsDetector::setFlatFieldCorrection(string fname){ double data[thisMultiDetector->numberOfChannels], xmed[thisMultiDetector->numberOfChannels]; double ffcoefficients[thisMultiDetector->numberOfChannels], fferrors[thisMultiDetector->numberOfChannels]; @@ -1647,12 +1636,18 @@ int multiSlsDetector::setFlatFieldCorrection(string fname){ int badlist[MAX_BADCHANS]; int im=0; - if (fname=="default") { - fname=string(thisMultiDetector->flatFieldFile); - } + if (fname=="default") { + fname=string(thisMultiDetector->flatFieldFile); + } + + + thisMultiDetector->correctionMask&=~(1<correctionMask&=~(1<flatFieldFile,"none"); @@ -1662,118 +1657,41 @@ int multiSlsDetector::setFlatFieldCorrection(string fname){ } } else { #ifdef VERBOSE - std::cout<< "Setting flat field correction from file " << fname << std::endl; + std::cout<< "Setting flat field correction from file " << fname << std::endl; #endif - sprintf(ffffname,"%s/%s",thisMultiDetector->flatFieldDir,fname.c_str()); - nch=readDataFile(string(ffffname),data); - - if (nch>thisMultiDetector->numberOfChannels) - nch=thisMultiDetector->numberOfChannels; - - if (nch>0) { - strcpy(thisMultiDetector->flatFieldFile,fname.c_str()); - - - - for (int ichan=0; ichan=detectors[idet]->getTotalNumberOfChannels()) { - ichdet=0; - detectors[idet]->setBadChannelCorrection(nbad,badlist,1); - idet++; - nbad=0; - } else - ichdet++; - } - - if (data[ichan]>0) { - /* add to median */ - im=0; - while ((imim; i--) - xmed[i]=xmed[i-1]; - xmed[im]=data[ichan]; - nmed++; - } else { - if (nbadflatFieldDir,fname.c_str()); + nch=readDataFile(string(ffffname),data); + + if (nch>thisMultiDetector->numberOfChannels) + nch=thisMultiDetector->numberOfChannels; + + if (nch>0) { + + //???? bad ff chans? + int nm=getNMods(); + int chpm[nm]; + int mMask[nm]; + for (int i=0; isetBadChannelCorrection(nbad,badlist,1); + + if ((postProcessingFuncs::calculateFlatField(&nm, chpm, mMask, badChannelMask, data, ffcoefficients, fferrors))) { + strcpy(thisMultiDetector->flatFieldFile,fname.c_str()); - if (nmed>1 && xmed[nmed/2]>0) { -#ifdef VERBOSE - std::cout<< "Flat field median is " << xmed[nmed/2] << " calculated using "<< nmed << " points" << std::endl; -#endif thisMultiDetector->correctionMask|=(1<=detectors[idet]->getTotalNumberOfChannels()) { -#ifdef VERBOSE - cout << "Set flat field detector " << idet << "(offset "<< detoff << ")" << endl; -#endif - detectors[idet]->setFlatFieldCorrection(ffcoefficients+detoff, fferrors+detoff); - ichdet=0;//ichan; - detoff=ichan; - idet++; - } - } - - - if (data[ichan]>0) { - ffcoefficients[ichan]=xmed[nmed/2]/data[ichan]; - fferrors[ichan]=ffcoefficients[ichan]*sqrt(data[ichan])/data[ichan]; - } else { - ffcoefficients[ichan]=0.; - fferrors[ichan]=1.; - } - ichdet++; - } - if (detectors[idet]) { -#ifdef VERBOSE - cout << "**Set flat field detector " << idet << "(offset "<< detoff << ")" << endl; -#endif - detectors[idet]->setFlatFieldCorrection(ffcoefficients+detoff, fferrors+detoff); - } - } else { - std::cout<< "Flat field data from file " << fname << " are not valid (" << nmed << "///" << xmed[nmed/2] << std::endl; - thisMultiDetector->correctionMask&=~(1<numberOfDetectors; i++) { - if (detectors[i]) - detectors[i]->setFlatFieldCorrection(NULL, NULL); - } - return -1; - } - } else { - std::cout<< "Flat field from file " << fname << " is not valid " << nch << std::endl; - thisMultiDetector->correctionMask&=~(1<numberOfDetectors; i++) { - if (detectors[i]) - detectors[i]->setFlatFieldCorrection(NULL, NULL); - } - return -1; - } + + setFlatFieldCorrection(ffcoefficients, fferrors); + + } + } else { + std::cout<< "Flat field from file " << fname << " is not valid " << nch << std::endl; + } + } return thisMultiDetector->correctionMask&(1<numberOfDetectors; idet++) { if (detectors[idet]) { if (corr!=NULL) @@ -1817,11 +1736,13 @@ int multiSlsDetector::getFlatFieldCorrection(double *corr, double *ecorr) { ep=ecorr+ichdet; else ep=NULL; - detectors[idet]->getFlatFieldCorrection(p, ep); + dff=detectors[idet]->getFlatFieldCorrection(p, ep); + if (dff==0) + ff=0; ichdet+=detectors[idet]->getTotalNumberOfChannels(); } } - return 0; + return ff; } @@ -1845,6 +1766,10 @@ int multiSlsDetector::getNMods(){ nm+=detectors[idet]->getNMods(); } } +#ifdef VERBOSE + cout << "total number of modules is " << nm << endl; +#endif + return nm; } diff --git a/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.h b/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.h index 97078bff4..0d1141e85 100644 --- a/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.h +++ b/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.h @@ -155,6 +155,10 @@ class multiSlsDetector : public slsDetectorUtils { double binSize; + + //X and Y displacement + double sampleDisplacement[2]; + /** number of positions at which the detector should acquire */ int numberOfPositions; /** list of encoder positions at which the detector should acquire */ diff --git a/slsDetectorSoftware/multiSlsDetector/multiSlsDetectorClient.h b/slsDetectorSoftware/multiSlsDetector/multiSlsDetectorClient.h index 0662fcc52..3639126c8 100644 --- a/slsDetectorSoftware/multiSlsDetector/multiSlsDetectorClient.h +++ b/slsDetectorSoftware/multiSlsDetector/multiSlsDetectorClient.h @@ -34,17 +34,20 @@ class multiSlsDetectorClient { char *c; \ char cmd[100]; \ if (action==slsDetectorDefs::PUT_ACTION && argc<2) { \ - cout << "Wrong usage - should be: "<< argv[0] << "[id-][pos:]channel arg" << endl; \ + cout << "Wrong usage - should be: "<< argv[0] << \ + "[id-][pos:]channel arg" << endl; \ cout << endl; \ return; \ + if (del) delete myDetector; \ }; if (action==slsDetectorDefs::GET_ACTION && argc<1) { \ - cout << "Wrong usage - should be: "<< argv[0] << "[id-][pos:]channel arg" << endl; \ + cout << "Wrong usage - should be: "<< argv[0] << \ + "[id-][pos:]channel arg" << endl; \ cout << endl; \ - + if (del) delete myDetector; \ return; \ }; \ - if (myDetector==NULL) { + if (myDetector==NULL) { \ iv=sscanf(argv[0],"%d-%s",&id, cmd); \ if (iv==2 && id>=0) { \ myDetector=new multiSlsDetector(id); \ @@ -52,8 +55,8 @@ class multiSlsDetectorClient { cout << id << "-" ; \ } else { \ myDetector=new multiSlsDetector(); \ - }; - del=1; + }; \ + del=1; \ } \ iv=sscanf(argv[0],"%d:%s",&pos, cmd); \ if (iv==2 && pos>=0) { \ diff --git a/slsDetectorSoftware/mythenDetectorServer/server_funcs.c b/slsDetectorSoftware/mythenDetectorServer/server_funcs.c index 7b3e066ea..a423f0e04 100755 --- a/slsDetectorSoftware/mythenDetectorServer/server_funcs.c +++ b/slsDetectorSoftware/mythenDetectorServer/server_funcs.c @@ -2640,7 +2640,12 @@ int send_update(int file_des) { n = sendDataOnly(file_des,lastClientIP,sizeof(lastClientIP)); n = sendDataOnly(file_des,&nModX,sizeof(nModX)); - n = sendDataOnly(file_des,&nModY,sizeof(nModY)); + // n = sendDataOnly(file_des,&nModY,sizeof(nModY)); + //sends back max modules instead of nmodulesY! + + thr = getNModBoard(); + sendDataOnly(file_des,&thr,sizeof(thr)); + n = sendDataOnly(file_des,&dynamicRange,sizeof(dynamicRange)); n = sendDataOnly(file_des,&dataBytes,sizeof(dataBytes)); t=setSettings(GET_SETTINGS); diff --git a/slsDetectorSoftware/slsDetector/slsDetector.cpp b/slsDetectorSoftware/slsDetector/slsDetector.cpp index a349d6792..3b8eec979 100644 --- a/slsDetectorSoftware/slsDetector/slsDetector.cpp +++ b/slsDetectorSoftware/slsDetector/slsDetector.cpp @@ -1,6 +1,7 @@ #include "slsDetector.h" #include "usersFunctions.h" #include "slsDetectorCommand.h" +#include "postProcessingFuncs.h" #include #include #include @@ -662,9 +663,10 @@ int slsDetector::initializeDetectorSize(detectorType type) { fileName=thisDetector->fileName; fileIndex=&thisDetector->fileIndex; moveFlag=&thisDetector->moveFlag; - + sampleDisplacement=NULL; settingsFile=thisDetector->settingsFile; + // setAngularConversionPointer(thisDetector->angOff,&thisDetector->nMods, thisDetector->nChans*thisDetector->nChips); #ifdef VERBOSE @@ -900,10 +902,16 @@ int slsDetector::receiveModule(sls_detector_module* myMod) { int slsDetector::setOnline(int off) { + int old=thisDetector->onlineFlag; if (off!=GET_ONLINE_FLAG) { thisDetector->onlineFlag=off; - if (thisDetector->onlineFlag==ONLINE_FLAG) + if (thisDetector->onlineFlag==ONLINE_FLAG) { setTCPSocket(); + if (thisDetector->onlineFlag==ONLINE_FLAG && old==OFFLINE_FLAG) { + cout << "Detector connecting for the first time - updating!" << endl; + updateDetector(); + } + } } return thisDetector->onlineFlag; } @@ -911,7 +919,7 @@ int slsDetector::setOnline(int off) { string slsDetector::checkOnline() { - string retval = ""; + string retval = string(""); if(!controlSocket){ //this already sets the online/offline flag setTCPSocket(); @@ -927,7 +935,7 @@ string slsDetector::checkOnline() { thisDetector->onlineFlag=OFFLINE_FLAG; delete controlSocket; controlSocket=NULL; - retval = thisDetector->hostname; + retval = string(thisDetector->hostname); #ifdef VERBOSE std::cout<< "offline!" << std::endl; #endif @@ -2854,7 +2862,17 @@ int slsDetector::updateDetectorNoWait() { n = controlSocket->ReceiveDataOnly(&nm,sizeof(nm)); thisDetector->nMod[X]=nm; n = controlSocket->ReceiveDataOnly( &nm,sizeof(nm)); - thisDetector->nMod[Y]=nm; + /// Should be overcome at a certain point! + + if (thisDetector->myDetectorType==MYTHEN) { + thisDetector->nModMax[X]=nm; + thisDetector->nModMax[Y]=1; + thisDetector->nModsMax=thisDetector->nModMax[Y]*thisDetector->nModMax[X]; + thisDetector->nMod[Y]=1; + } else { + thisDetector->nMod[Y]=nm; + } + thisDetector->nMods=thisDetector->nMod[Y]*thisDetector->nMod[X]; if (thisDetector->nModsMaxnMods) thisDetector->nModsMax=thisDetector->nMods; @@ -3342,8 +3360,13 @@ int64_t slsDetector::setTimer(timerIndex index, int64_t t){ thisDetector->timerValue[index]=retval; } controlSocket->Disconnect(); - if (ret==FORCE_UPDATE) + if (ret==FORCE_UPDATE) { updateDetector(); +#ifdef VERBOSE + std::cout<< "Updated!" << std::endl; +#endif + + } } } } else { @@ -4035,7 +4058,7 @@ int slsDetector::setFlatFieldCorrection(string fname) char ffffname[MAX_STR_LENGTH*2]; if (fname=="default") { - fname=string("thisDetector->flatFieldFile"); + fname=string(thisDetector->flatFieldFile); } if (fname=="") { @@ -4043,77 +4066,50 @@ int slsDetector::setFlatFieldCorrection(string fname) std::cout<< "disabling flat field correction" << std::endl; #endif thisDetector->correctionMask&=~(1<flatFieldFile,"none"); + + + } else { #ifdef VERBOSE - std::cout<< "Setting flat field correction from file " << fname << std::endl; -#endif - sprintf(ffffname,"%s/%s",thisDetector->flatFieldDir,fname.c_str()); - nch=readDataFile(string(ffffname),data); - if (nch>0) { - strcpy(thisDetector->flatFieldFile,fname.c_str()); - for (int ichan=0; ichan0) { - /* add to median */ - im=0; - while ((imim; i--) - xmed[i]=xmed[i-1]; - xmed[im]=data[ichan]; - nmed++; - } else { - //add the channel to the ff bad channel list - if (thisDetector->nBadFFbadFFList[thisDetector->nBadFF]=ichan; - (thisDetector->nBadFF)++; -#ifdef VERBOSE - std::cout<< "Channel " << ichan << " added to the bad channel list" << std::endl; -#endif - } else - std::cout<< "Too many bad channels " << std::endl; - - } - } - - if (nmed>1 && xmed[nmed/2]>0) { -#ifdef VERBOSE - std::cout<< "Flat field median is " << xmed[nmed/2] << " calculated using "<< nmed << " points" << std::endl; + std::cout<< "Setting flat field correction from file " << fname << std::endl; #endif + + + sprintf(ffffname,"%s/%s",thisDetector->flatFieldDir,fname.c_str()); + nch=readDataFile(string(ffffname),data); + if (nch>0) { + + //???? bad ff chans? + int nm=getNMods(); + int chpm[nm]; + int mMask[nm]; + for (int i=0; iflatFieldFile,fname.c_str()); + - thisDetector->correctionMask|=(1<0) { - ffcoefficients[ichan]=xmed[nmed/2]/data[ichan]; - fferrors[ichan]=ffcoefficients[ichan]*sqrt(data[ichan])/data[ichan]; - } else { - ffcoefficients[ichan]=0.; - fferrors[ichan]=1.; - } - } - for (int ichan=nch; ichannMod[X]*thisDetector->nMod[Y]*thisDetector->nChans*thisDetector->nChips; ichan++) { - ffcoefficients[ichan]=1.; - fferrors[ichan]=0.; - } + thisDetector->correctionMask|=(1<correctionMask&(1<correctionMask&(1<nMod[X]*thisDetector->nChans*thisDetector->nChips; ichan++) { @@ -4129,7 +4125,11 @@ int slsDetector::setFlatFieldCorrection(double *corr, double *ecorr) { thisDetector->correctionMask|=(1<correctionMask&=~(1<correctionMask)&(1<correctionMask&(1<correctionMask&(1<nChans*thisDetector->nChips*thisDetector->nMods]; -// for (int ip=0; ipnChans*thisDetector->nChips*thisDetector->nMods; ip++) { -// imod=ip/(thisDetector->nChans*thisDetector->nChips); -// ang[ip]=angle(ip%(thisDetector->nChans*thisDetector->nChips),\ -// pos, \ -// thisDetector->fineOffset+thisDetector->globalOffset, \ -// thisDetector->angOff[imod].r_conversion, \ -// thisDetector->angOff[imod].center, \ -// thisDetector->angOff[imod].offset, \ -// thisDetector->angOff[imod].tilt, \ -// thisDetector->angDirection -// ); -// } -// return ang; -// } - - - - - int slsDetector:: writeAngularConversion(string fname) { return writeAngularConversion(fname, thisDetector->nMods, thisDetector->angOff); @@ -4843,12 +4809,7 @@ int slsDetector::readConfigurationFile(string const fname){ string ans; string str; ifstream infile; - // int iargval; - // int interrupt=0; char *args[1000]; -// for (int ia=0; ia<100; ia++) { -// args[ia]=new char[1000]; -// } string sargname, sargval; int iline=0; diff --git a/slsDetectorSoftware/slsDetector/slsDetector.h b/slsDetectorSoftware/slsDetector/slsDetector.h index 158d68b78..a4cfd5ea1 100644 --- a/slsDetectorSoftware/slsDetector/slsDetector.h +++ b/slsDetectorSoftware/slsDetector/slsDetector.h @@ -11,7 +11,7 @@ #include "angleConversionConstant.h" #include "MySocketTCP.h" - +#include "angleConversionConstant.h"; /** @@ -244,6 +244,7 @@ typedef struct sharedSlsDetector { /** online flag - is set if the receiver is connected, unset if socket connection is not possible */ int receiverOnlineFlag; + } sharedSlsDetector; diff --git a/slsDetectorSoftware/slsDetector/slsDetectorCommand.cpp b/slsDetectorSoftware/slsDetector/slsDetectorCommand.cpp index 7c69592e9..8024efc0f 100644 --- a/slsDetectorSoftware/slsDetector/slsDetectorCommand.cpp +++ b/slsDetectorSoftware/slsDetector/slsDetectorCommand.cpp @@ -123,6 +123,15 @@ slsDetectorCommand::slsDetectorCommand(slsDetectorUtils *det) { descrToFuncMap[i].m_pFuncName="moveflag" ;// descrToFuncMap[i].m_pFuncPtr=&slsDetectorCommand::cmdAngConv; i++; + + descrToFuncMap[i].m_pFuncName="samplex" ;// + descrToFuncMap[i].m_pFuncPtr=&slsDetectorCommand::cmdAngConv; + i++; + + + descrToFuncMap[i].m_pFuncName="sampley" ;// + descrToFuncMap[i].m_pFuncPtr=&slsDetectorCommand::cmdAngConv; + i++; @@ -1682,7 +1691,14 @@ string slsDetectorCommand::cmdAngConv(int narg, char *args[], int action){ } else if (string(args[0])==string("moveflag")) { c=MOVE_FLAG; - } else + } else if (string(args[0])==string("samplex")) { + c=SAMPLE_X; + } else if (string(args[0])==string("sampley")) { + c=SAMPLE_Y; + } + + + else return string("could not decode angular conversion parameter ")+cmd; @@ -1712,6 +1728,10 @@ string slsDetectorCommand::helpAngConv(int narg, char *args[], int action){ t=4; } else if (string(args[0])==string("binsize")) { t=8; + } else if (string(args[0])==string("samplex")) { + t=16; + } else if (string(args[0])==string("sampley")) { + t=32; } if (t&1) { if (action==GET_ACTION || action==HELP_ACTION) @@ -1739,11 +1759,19 @@ string slsDetectorCommand::helpAngConv(int narg, char *args[], int action){ if (action==PUT_ACTION || action==HELP_ACTION) os << string("binsize f\t sets the bin size used for the angular conversion \n"); - - - } - + if (t&16) { + if (action==GET_ACTION || action==HELP_ACTION) + os << string("samplex \t gets the sample displacement in th direction parallel to the beam \n"); + if (action==PUT_ACTION || action==HELP_ACTION) + os << string("samplex f\t sets the sample displacement in th direction parallel to the beam \n"); + } + if (t&32) { + if (action==GET_ACTION || action==HELP_ACTION) + os << string("sampley \t gets the sample displacement in the direction orthogonal to the beam \n"); + if (action==PUT_ACTION || action==HELP_ACTION) + os << string("sampley f\t sets the sample displacement in the direction orthogonal to the beam \n"); + } return os.str(); } diff --git a/slsDetectorSoftware/slsDetector/slsDetectorUtils.cpp b/slsDetectorSoftware/slsDetector/slsDetectorUtils.cpp index ca60be302..9b6960d36 100644 --- a/slsDetectorSoftware/slsDetector/slsDetectorUtils.cpp +++ b/slsDetectorSoftware/slsDetector/slsDetectorUtils.cpp @@ -10,8 +10,7 @@ #include using namespace std; - -slsDetectorUtils::slsDetectorUtils() { +slsDetectorUtils::slsDetectorUtils() { #ifdef VERBOSE @@ -34,13 +33,13 @@ slsDetectorUtils::slsDetectorUtils() { cout << "done " << endl; #endif + expTime=timerValue+ACQUISITION_TIME; }; - void slsDetectorUtils::acquire(int delflag){ // setTotalProgress(); @@ -80,13 +79,36 @@ void slsDetectorUtils::acquire(int delflag){ - pthread_mutex_lock(&mp); - resetFinalDataQueue(); - resetDataQueue(); - jointhread=0; - queuesize=0; - posfinished=0; - pthread_mutex_unlock(&mp); + + setJoinThread(0); + positionFinished(0); + + + + + int nm=timerValue[MEASUREMENTS_NUMBER]; + if (nm<1) + nm=1; + + int np=getNumberOfPositions(); + if (np<1) + np=1; + + + int ns0=1; + if (*actionMask & (1 << MAX_ACTIONS)) { + ns0=getScanSteps(0); + if (ns0<1) + ns0=1; + } + + int ns1=1; + if (*actionMask & (1 << (MAX_ACTIONS+1))) { + ns1=getScanSteps(1); + if (ns1<1) + ns1=1; + } + if (*threadedProcessing) { startThread(delflag); @@ -96,48 +118,21 @@ void slsDetectorUtils::acquire(int delflag){ #endif - int nm=1; - - if (timerValue[MEASUREMENTS_NUMBER]>0) - nm=timerValue[MEASUREMENTS_NUMBER]; for(int im=0;im0) - np=*numberOfPositions; - - int ns0=1; - if (*actionMask & (1 << MAX_ACTIONS)) { - ns0=nScanSteps[0]; - } - - if (ns0<1) - ns0=1; - - int ns1=1; - if (*actionMask & (1 << (MAX_ACTIONS+1))) { - ns1=nScanSteps[1]; - } - - if (ns1<1) - ns1=1; + //loop measurements + pthread_mutex_lock(&mp); setStartIndex(*fileIndex); + pthread_mutex_unlock(&mp); //cout << "action at start" << endl; if (*stoppedFlag==0) { @@ -166,14 +161,14 @@ void slsDetectorUtils::acquire(int delflag){ } else break; - currentPositionIndex=0; + ResetPositionIndex(); for (int ip=0; ip0) { + if (getNumberOfPositions()>0) { moveDetector(detPositions[ip]); - currentPositionIndex=ip+1; + IncrementPositionIndex(); #ifdef VERBOSE std::cout<< "moving to position" << std::endl; #endif @@ -200,23 +195,15 @@ void slsDetectorUtils::acquire(int delflag){ executeAction(headerBefore); - if (*correctionMask&(1<< ANGULAR_CONVERSION)) { - pthread_mutex_lock(&mp); - currentPosition=getDetectorPosition(); - posfinished=0; - pthread_mutex_unlock(&mp); + if (*correctionMask&(1<< ANGULAR_CONVERSION) || aclog || eclog) { + positionFinished(0); + setCurrentPosition(getDetectorPosition()); } - if (aclog) { - if ((*correctionMask&(1<< ANGULAR_CONVERSION))==0) { - pthread_mutex_lock(&mp); - currentPosition=getDetectorPosition(); - posfinished=0; - pthread_mutex_unlock(&mp); - } - - aclog->addStep(currentPosition, getCurrentFileName()); - } + + if (aclog) + aclog->addStep(getCurrentPosition(), getCurrentFileName()); + if (eclog) eclog->addStep(setDAC(-1,THRESHOLD), getCurrentFileName()); @@ -229,16 +216,23 @@ void slsDetectorUtils::acquire(int delflag){ } startAndReadAll(); +#ifdef VERBOSE + cout << "returned! " << endl; +#endif if (*correctionMask&(1<< I0_NORMALIZATION)) { if (get_i0) currentI0=get_i0(1,IOarg); // this is the correct i0!!!!! } +#ifdef VERBOSE + cout << "pos finished? " << endl; +#endif + + positionFinished(1); - pthread_mutex_lock(&mp); - posfinished=1; - pthread_mutex_unlock(&mp); - +#ifdef VERBOSE + cout << "done! " << endl; +#endif if (*threadedProcessing==0){ @@ -254,15 +248,11 @@ void slsDetectorUtils::acquire(int delflag){ // wait until data processing thread has finished the data - pthread_mutex_lock(&mp); - while (queuesize){ - pthread_mutex_unlock(&mp); + while (dataQueueSize()){ usleep(100000); - pthread_mutex_lock(&mp); - } - pthread_mutex_unlock(&mp); - + pthread_mutex_lock(&mp); + if (*stoppedFlag==0) { executeAction(headerAfter); setLastIndex(*fileIndex); @@ -270,7 +260,7 @@ void slsDetectorUtils::acquire(int delflag){ setLastIndex(*fileIndex); break; } - + pthread_mutex_unlock(&mp); if (*stoppedFlag) { @@ -279,7 +269,9 @@ void slsDetectorUtils::acquire(int delflag){ #endif break; } else if (ip<(np-1)) { + pthread_mutex_lock(&mp); *fileIndex=setStartIndex(); + pthread_mutex_unlock(&mp); } } // loop on position finished @@ -296,7 +288,9 @@ void slsDetectorUtils::acquire(int delflag){ #endif break; } else if (is1<(ns1-1)) { + pthread_mutex_lock(&mp); *fileIndex=setStartIndex(); + pthread_mutex_unlock(&mp); } } @@ -309,12 +303,16 @@ void slsDetectorUtils::acquire(int delflag){ #endif break; } else if (is0<(ns0-1)) { - *fileIndex=setStartIndex(); + pthread_mutex_lock(&mp); + *fileIndex=setStartIndex(); + pthread_mutex_unlock(&mp); } } //end scan0 loop is0 - *fileIndex=setLastIndex(); + pthread_mutex_lock(&mp); + *fileIndex=setLastIndex(); + pthread_mutex_unlock(&mp); if (*stoppedFlag==0) { executeAction(stopScript); } else @@ -335,9 +333,7 @@ void slsDetectorUtils::acquire(int delflag){ // waiting for the data processing thread to finish! if (*threadedProcessing) { - pthread_mutex_lock(&mp); - jointhread=1; - pthread_mutex_unlock(&mp); + setJoinThread(1); pthread_join(dataProcessingThread, &status); } @@ -349,17 +345,15 @@ void slsDetectorUtils::acquire(int delflag){ if (aclog) delete aclog; - + if (eclog) delete eclog; - - + + if (acquisition_finished) acquisition_finished(getCurrentProgress(),getDetectorStatus(),acqFinished_p); - - } @@ -491,17 +485,21 @@ int slsDetectorUtils::setBadChannelCorrection(string fname, int &nbadtot, int *b double slsDetectorUtils::getCurrentProgress() { + pthread_mutex_lock(&mp); #ifdef VERBOSE cout << progressIndex << " / " << totalProgress << endl; #endif return 100.*((double)progressIndex)/((double)totalProgress); + pthread_mutex_unlock(&mp); } void slsDetectorUtils::incrementProgress() { + pthread_mutex_lock(&mp); progressIndex++; - cout << fixed << setprecision(2) << setw (6) << getCurrentProgress() << " \%"; + cout << fixed << setprecision(2) << setw (6) << 100.*((double)progressIndex)/((double)totalProgress) << " \%"; + pthread_mutex_unlock(&mp); #ifdef VERBOSE cout << endl; #else @@ -838,115 +836,3 @@ int slsDetectorUtils::dumpDetectorSetup(string const fname, int level){ } -// int slsDetectorUtils::setFlatFieldCorrectionFile(string fname){ -// int tch=getTotalNumberOfChannels(); - - - -// double data[tch], xmed[tch]; -// double ffcoefficients[tch], fferrors[tch]; -// int nmed=0; -// int idet=0, ichdet=-1; -// char ffffname[MAX_STR_LENGTH*2]; -// int nbad=0, nch; -// int badlist[MAX_BADCHANS]; -// int im=0; - -// if (fname=="default") { -// fname=string(flatFieldFile); -// } -// if (fname=="") { -// #ifdef VERBOSE -// std::cout<< "disabling flat field correction" << std::endl; -// #endif -// (*correctionMask)&=~(1<flatFieldFile,"none"); - -// setFlatFieldCorrection(NULL, NULL); -// } else { - -// #ifdef VERBOSE -// std::cout<< "Setting flat field correction from file " << fname << std::endl; -// #endif -// sprintf(ffffname,"%s/%s",flatFieldDir,fname.c_str()); -// nch=readDataFile(string(ffffname),data); - -// if (nch>tch) -// nch=tch; - -// if (nch>0) { -// strcpy(flatFieldFile,fname.c_str()); - - -// nbad=0; -// for (int ichan=0; ichan0) { -// /* add to median */ -// im=0; -// while ((imim; i--) -// xmed[i]=xmed[i-1]; -// xmed[im]=data[ichan]; -// nmed++; -// } else { -// if (nbad1 && xmed[nmed/2]>0) { -// #ifdef VERBOSE -// std::cout<< "Flat field median is " << xmed[nmed/2] << " calculated using "<< nmed << " points" << std::endl; -// #endif - -// thisMultiDetector->correctionMask|=(1<0) { -// ffcoefficients[ichan]=xmed[nmed/2]/data[ichan]; -// fferrors[ichan]=ffcoefficients[ichan]*sqrt(data[ichan])/data[ichan]; -// } else { -// ffcoefficients[ichan]=0.; -// fferrors[ichan]=1.; -// } - -// } - -// setFlatFieldCorrection(ffcoefficients, fferrors); - - -// } else { -// std::cout<< "Flat field data from file " << fname << " are not valid (" << nmed << "///" << xmed[nmed/2] << std::endl; -// thisMultiDetector->correctionMask&=~(1<numberOfDetectors; i++) { -// if (detectors[i]) -// detectors[i]->setFlatFieldCorrection(NULL, NULL); -// } -// return -1; -// } -// } else { -// std::cout<< "Flat field from file " << fname << " is not valid " << nch << std::endl; -// thisMultiDetector->correctionMask&=~(1<numberOfDetectors; i++) { -// if (detectors[i]) -// detectors[i]->setFlatFieldCorrection(NULL, NULL); -// } -// return -1; -// } -// } -// return thisMultiDetector->correctionMask&(1<center; \ ecenter=acc->ecenter; \ r_conversion=acc->r_conversion; \ diff --git a/slsDetectorSoftware/slsDetectorAnalysis/angularConversion.cpp b/slsDetectorSoftware/slsDetectorAnalysis/angularConversion.cpp index 99c2b10b3..f6a048565 100644 --- a/slsDetectorSoftware/slsDetectorAnalysis/angularConversion.cpp +++ b/slsDetectorSoftware/slsDetectorAnalysis/angularConversion.cpp @@ -190,6 +190,18 @@ double angularConversion::setAngularConversionParameter(angleConversionParameter return *moveFlag; } return -1; + case SAMPLE_X: + if (sampleDisplacement) { + sampleDisplacement[X]=v; + return sampleDisplacement[X]; + } + return 0; + case SAMPLE_Y: + if (sampleDisplacement) { + sampleDisplacement[Y]=v; + return sampleDisplacement[Y]; + } + return 0; default: return 0; } diff --git a/slsDetectorSoftware/slsDetectorAnalysis/angularConversion.h b/slsDetectorSoftware/slsDetectorAnalysis/angularConversion.h index ee76388e1..9eab9c895 100644 --- a/slsDetectorSoftware/slsDetectorAnalysis/angularConversion.h +++ b/slsDetectorSoftware/slsDetectorAnalysis/angularConversion.h @@ -345,6 +345,8 @@ class angularConversion : public virtual slsDetectorBase, public angularConversi + double *sampleDisplacement; + /** current position of the detector */ @@ -367,6 +369,8 @@ class angularConversion : public virtual slsDetectorBase, public angularConversi */ int getCurrentPositionIndex() {return currentPositionIndex;}; void incrementPositionIndex() {currentPositionIndex++;}; + void resetPositionIndex() {currentPositionIndex=0;}; + @@ -385,7 +389,6 @@ class angularConversion : public virtual slsDetectorBase, public angularConversi - /** merging bins */ double *mergingBins; diff --git a/slsDetectorSoftware/slsDetectorAnalysis/angularConversionStatic.cpp b/slsDetectorSoftware/slsDetectorAnalysis/angularConversionStatic.cpp index 28b7e93d5..dbd3446c7 100644 --- a/slsDetectorSoftware/slsDetectorAnalysis/angularConversionStatic.cpp +++ b/slsDetectorSoftware/slsDetectorAnalysis/angularConversionStatic.cpp @@ -30,7 +30,7 @@ double* angularConversionStatic::convertAngles(double pos, int nch, int *chansPe angleConversionConstant *p=NULL; int ch0=0; - int chlast=chansPerMod[0]; + int chlast=chansPerMod[0]-1; int nchmod=chansPerMod[0]; p=angOff[imod]; if (mF[imod]==0) @@ -42,7 +42,7 @@ double* angularConversionStatic::convertAngles(double pos, int nch, int *chansPe #ifdef VERBOSE // cout << "ip " << ip << " ch0 " << ch0 << " chlast " << chlast << " imod " << imod << endl; #endif - if (ip>=chlast) { + if (ip>chlast) { imod++; p=angOff[imod]; if (mF[imod]==0) @@ -50,10 +50,10 @@ double* angularConversionStatic::convertAngles(double pos, int nch, int *chansPe else enc=pos; - ch0=chlast; + ch0=chlast+1; nchmod=chansPerMod[imod]; if (nchmod>0) - chlast+=nchmod; + chlast=ch0+nchmod-1; } if (p) @@ -69,6 +69,72 @@ double* angularConversionStatic::convertAngles(double pos, int nch, int *chansPe return ang; } +double angularConversionStatic::convertAngle(double pos, int ich, int *chansPerMod, angleConversionConstant **angOff, int *mF, double fo, double go, int angdir) { + + int imod=0; + double ang; + double enc=0, trans=0; + angleConversionConstant *p=NULL; + + int ch0=0; + int chlast=chansPerMod[0]-1; + int nchmod=chansPerMod[0]; + + + + while (ich>chlast) { + imod++; + ch0=chlast+1; + nchmod=chansPerMod[imod]; + chlast=ch0+nchmod-1; + } + + p=angOff[imod]; + + + + switch (mF[imod]) { + case 0: + enc=0; + trans=0; + break; + case 1: + enc=pos; + trans=0; + break; + case -1: + enc=-pos; + trans=0; + break; + case 2: + enc=0; + trans=pos; + break; + case -2: + enc=0; + trans=-pos; + break; + default: + enc=0; + trans=0; + } + + + + if (p) + ang=angle(ich-ch0, \ + enc, \ + fo+go, \ + p->r_conversion, \ + p->center, \ + p->offset, \ + trans, \ + angdir ); + + return ang; + +} + //static! @@ -265,6 +331,53 @@ int angularConversionStatic::addToMerging(double *p1, double *v1, double *e1, d } + return slsDetectorDefs::OK; + +} + +int angularConversionStatic::addPointToMerging(double p1, double v1, double e1, double *mp, double *mv,double *me, int *mm, double binsize,int nbins) { + + + double binmi=-180.; + int ibin=0; + + + if (mp==NULL) //can be changed if we want to use a fixed bin algorithm! + return slsDetectorDefs::FAIL; + + if (mv==NULL) + return slsDetectorDefs::FAIL; + if (me==NULL) + return slsDetectorDefs::FAIL; + if (mm==NULL) + return slsDetectorDefs::FAIL; + + if (binsize<=0) + return slsDetectorDefs::FAIL; + + if (nbins<=0) + return slsDetectorDefs::FAIL; + + + ibin=(int)((p1-binmi)/binsize); + + + if (ibin=0) { + mp[ibin]+=p1; + mv[ibin]+=v1; + if (e1) + me[ibin]+=(e1*e1); + else + me[ibin]+=v1; + mm[ibin]++; + +#ifdef VERBOSE + cout << "add " << ibin << " "<< mm[ibin] << " " << mp[ibin]<< mv[ibin] << me[ibin] << endl; +#endif + } else + return slsDetectorDefs::FAIL; + + return slsDetectorDefs::OK; } diff --git a/slsDetectorSoftware/slsDetectorAnalysis/angularConversionStatic.cpp~ b/slsDetectorSoftware/slsDetectorAnalysis/angularConversionStatic.cpp~ index e6a1bf832..28b7e93d5 100644 --- a/slsDetectorSoftware/slsDetectorAnalysis/angularConversionStatic.cpp~ +++ b/slsDetectorSoftware/slsDetectorAnalysis/angularConversionStatic.cpp~ @@ -4,7 +4,9 @@ #include #include #include +#include "angleConversionConstant.h" +#include "sls_detector_defs.h" #include "angleFunction.h" using namespace std; @@ -180,7 +182,7 @@ int angularConversionStatic::resetMerging(double *mp, double *mv, double *me, in me[ibin]=0; mm[ibin]=0; } - return OK; + return slsDetectorDefs::OK; } @@ -214,25 +216,25 @@ int angularConversionStatic::addToMerging(double *p1, double *v1, double *e1, d if (p1==NULL) return 0; if (v1==NULL) - return FAIL; + return slsDetectorDefs::FAIL; if (mp==NULL) //can be changed if we want to use a fixed bin algorithm! - return FAIL; + return slsDetectorDefs::FAIL; if (mv==NULL) - return FAIL; + return slsDetectorDefs::FAIL; if (me==NULL) - return FAIL; + return slsDetectorDefs::FAIL; if (mm==NULL) - return FAIL; + return slsDetectorDefs::FAIL; if (nchans==0) - return FAIL; + return slsDetectorDefs::FAIL; if (binsize<=0) - return FAIL; + return slsDetectorDefs::FAIL; if (nbins<=0) - return FAIL; + return slsDetectorDefs::FAIL; for (int ip=0; ip #include -#include "angleConversionConstant.h" +//#include "angleConversionConstant.h" //double angle(int ichan, double encoder, double totalOffset, double conv_r, double center, double offset, double tilt, int direction) +class angleConversionConstant; using namespace std; @@ -35,7 +35,8 @@ class containing the methods to set/unset the angular conversion and merge the d The angular conversion itself is defined by the angle() function defined in usersFunctions.cpp */ -class angularConversionStatic : public virtual slsDetectorDefs +class angularConversionStatic +// : public virtual slsDetectorDefs { public: diff --git a/slsDetectorSoftware/slsDetectorAnalysis/badChannelCorrections.h b/slsDetectorSoftware/slsDetectorAnalysis/badChannelCorrections.h index 61a42b0dc..028363a21 100644 --- a/slsDetectorSoftware/slsDetectorAnalysis/badChannelCorrections.h +++ b/slsDetectorSoftware/slsDetectorAnalysis/badChannelCorrections.h @@ -45,6 +45,13 @@ class badChannelCorrections{ } \ } \ return nbad; }; + + + static int setBadChannelCorrection(ifstream &infile, int &nbad, int *badlist, int moff){ \ + int retval=readBadChannelCorrectionFile(infile,nbad,badlist); \ + for (int ich=0; ich=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; - -}; - - - - - - +postProcessing::~postProcessing(){delete ppFun;}; @@ -82,47 +44,62 @@ void postProcessing::processFrame(int *myData, int delflag) { string fname; // double *fdata=NULL; - - incrementProgress(); +#ifdef VERBOSE + cout << "start processing"<< endl; +#endif + + incrementProgress(); + +#ifdef VERBOSE + cout << "prog incremented"<< endl; +#endif /** decode data */ fdata=decodeData(myData, fdata); +#ifdef VERBOSE + cout << "decode"<< endl; +#endif -fname=createFileName(); - -//Checking for write flag -if(*correctionMask&(1<initDataset(); } - lfdata=NULL; +#ifdef VERBOSE + cout << "add frame" << endl; +#endif + + ppFun->addFrame(lfdata, ¤tPosition, ¤tI0, &t, (fname+ext).c_str(), NULL); + + if ((GetCurrentPositionIndex()>=npos && positionFinished() && dataQueueSize()) || npos==0) { + +#ifdef VERBOSE + cout << "finalize dataset" << endl; +#endif + + ppFun->finalizeDataset(ang, val, err, &np); + IncrementPositionIndex(); - - - /** flat field correction */ - if (*correctionMask&(1<0) { - if (*correctionMask&(1<< ANGULAR_CONVERSION)) { -#ifdef VERBOSE - cout << "**************Current position index is " << getCurrentPositionIndex() << endl; -#endif - // if (*numberOfPositions>0) { - if (getCurrentPositionIndex()<=1) { - -#ifdef VERBOSE - cout << "reset merging " << endl; -#endif - resetMerging(); - } - -#ifdef VERBOSE - cout << "add to merging "<< getCurrentPositionIndex() << endl; -#endif - - 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=finalizeMerging(); - /** file writing */ - incrementPositionIndex(); - // cout << "unlock 1" << endl; + pthread_mutex_lock(&mp); + fname=createFileName(); pthread_mutex_unlock(&mp); - - fname=createFileName(); - -#ifdef VERBOSE - cout << "writing merged data file" << endl; -#endif - writeDataFile (fname+ext,np,getMergedCounts(), getMergedErrors(), getMergedPositions(),'f'); -#ifdef VERBOSE - cout << " done" << endl; -#endif - - - -// if (delflag) { -// deleteMerging(); -// } else { - thisData=new detectorData(getMergedCounts(),getMergedErrors(),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(*correctionMask&(1<0) { + while((queuesize=dataQueueSize())>0) { /** Pop data queue */ - myData=dataQueue.front(); // get the data from the queue - pthread_mutex_unlock(&mp); - +#ifdef VERBOSE + cout << "data found"<< endl; +#endif + + myData=dataQueueFront(); // get the data from the queue +#ifdef VERBOSE + cout << "got them"<< endl; +#endif + 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); + if (checkJoinThread()) { + if (dataQueueSize()==0) { 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::checkJoinThread() { + int retval; + pthread_mutex_lock(&mp); + retval=jointhread; + pthread_mutex_unlock(&mp); + return retval; +} + +void postProcessing::setJoinThread( int v) { + pthread_mutex_lock(&mp); + jointhread=v; + pthread_mutex_unlock(&mp); +} + +int* postProcessing::dataQueueFront() { + int *retval=NULL; + pthread_mutex_lock(&mp); + if( !dataQueue.empty() ) { + retval=dataQueue.front(); + } + pthread_mutex_unlock(&mp); + return retval; +} +int postProcessing::dataQueueSize() { + int retval; + pthread_mutex_lock(&mp); + retval=dataQueue.size(); + pthread_mutex_unlock(&mp); + return retval; +} + + int* postProcessing::popDataQueue() { int *retval=NULL; + pthread_mutex_lock(&mp); if( !dataQueue.empty() ) { retval=dataQueue.front(); dataQueue.pop(); } + pthread_mutex_unlock(&mp); return retval; } @@ -502,11 +341,13 @@ detectorData* postProcessing::popFinalDataQueue() { void postProcessing::resetDataQueue() { int *retval=NULL; + pthread_mutex_lock(&mp); while( !dataQueue.empty() ) { retval=dataQueue.front(); dataQueue.pop(); delete [] retval; } + pthread_mutex_unlock(&mp); } @@ -523,12 +364,125 @@ void postProcessing::resetFinalDataQueue() { void postProcessing::startThread(int delflag) { + + /////////////////////////////////// Initialize dataset + + //resetDataQueue(); + + setTotalProgress(); + + int nmod=getNMods(); + int *chPM=new int[nmod]; + int *mM=new int[nmod]; + int totch=0; +#ifdef VERBOSE + cout << "init dataset stuff" << endl; +#endif + + for (int im=0; imr_conversion; + angOff[im]=p->offset; + angCenter[im]=p->center; + } + sx=getAngularConversionParameter(SAMPLE_X); + sy=getAngularConversionParameter(SAMPLE_Y); + + } + + +#ifdef VERBOSE + cout << "init dataset" << endl; +#endif + ppFun->initDataset(&nmod,chPM,mM,badChannelMask, ffcoeff, fferr, &tdead, &angdir, angRad, angOff, angCenter, &to, &bs, &sx, &sy); + +#ifdef VERBOSE + cout << "done" << endl; +#endif + + + if (*correctionMask&(1<< ANGULAR_CONVERSION)) { + arraySize=getNumberOfAngularBins(); + if (arraySize<=0) + arraySize=totch; + } else { + arraySize=totch; + } + + queuesize=dataQueueSize(); + + resetFinalDataQueue(); + resetDataQueue(); + + + /////////////////////////////////// Start thread //////////////////////////////////////////////////////// +#ifdef VERBOSE + cout << "start thread stuff" << endl; +#endif 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; @@ -553,7 +507,8 @@ void postProcessing::startThread(int delflag) { ret = pthread_create(&dataProcessingThread, &tattr,startProcessDataNoDelete, (void*)this); pthread_attr_destroy(&tattr); - // scheduling parameters of target thread + + // scheduling parameters of target thread ret = pthread_setschedparam(dataProcessingThread, policy, ¶m); } diff --git a/slsDetectorSoftware/slsDetectorAnalysis/postProcessing.h b/slsDetectorSoftware/slsDetectorAnalysis/postProcessing.h index ceef57031..a82796415 100644 --- a/slsDetectorSoftware/slsDetectorAnalysis/postProcessing.h +++ b/slsDetectorSoftware/slsDetectorAnalysis/postProcessing.h @@ -18,6 +18,9 @@ #include #include +class postProcessingFuncs; + + using namespace std; @@ -39,7 +42,7 @@ class postProcessing : public angularConversion, public fileIO, public badChanne public: postProcessing(); - virtual ~postProcessing(){}; + virtual ~postProcessing(); @@ -75,7 +78,6 @@ class postProcessing : public angularConversion, public fileIO, public badChanne */ virtual int setBadChannelCorrection(string fname="")=0; - static int setBadChannelCorrection(ifstream &infile, int &nbad, int *badlist, int moff){int retval=readBadChannelCorrectionFile(infile,nbad,badlist); for (int ich=0; ich0 otherwise */ virtual int setBadChannelCorrection(string fname, int &nbad, int *badlist, int off=0)=0; - + using badChannelCorrections::setBadChannelCorrection; /** set bad channels correction @@ -96,30 +98,7 @@ class postProcessing : public angularConversion, public fileIO, public badChanne */ 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) ;}; @@ -201,6 +180,10 @@ s */ int* popDataQueue(); + int* dataQueueFront(); + + int dataQueueSize(); + /** pops the data from thepostprocessed data queue \returns pointer to the popped data or NULL if the queue is empty. @@ -209,6 +192,10 @@ s detectorData* popFinalDataQueue(); + int checkJoinThread(); + void setJoinThread(int v); + + /** resets the raw data queue \sa dataQueue @@ -234,9 +221,14 @@ s virtual int flatFieldCorrect(double*, double*, double*, double*)=0; + virtual int getNMods()=0; - + int GetCurrentPositionIndex(){ pthread_mutex_lock(&mp); int retval=getCurrentPositionIndex(); pthread_mutex_unlock(&mp); return retval;}; + void IncrementPositionIndex(){ pthread_mutex_lock(&mp); incrementPositionIndex(); pthread_mutex_unlock(&mp);}; + void IncrementFileIndex(){ pthread_mutex_lock(&mp); incrementFileIndex(); pthread_mutex_unlock(&mp);}; + + void ResetPositionIndex(){pthread_mutex_lock(&mp); resetPositionIndex(); pthread_mutex_unlock(&mp);}; void registerDataCallback(int( *userCallback)(detectorData*, void*), void *pArg) {dataReady = userCallback; pCallbackArg = pArg;}; @@ -246,8 +238,14 @@ s + virtual double getRateCorrectionTau()=0; + int positionFinished(int v=-1){pthread_mutex_lock(&mp); if (v>=0) posfinished=v; int retval=posfinished; pthread_mutex_unlock(&mp); return retval;}; + + double getCurrentPosition() {double p; pthread_mutex_lock(&mp); p=currentPosition; pthread_mutex_unlock(&mp); return p;} + int setCurrentPosition(double v) { pthread_mutex_lock(&mp); currentPosition=v; pthread_mutex_unlock(&mp); } + protected: @@ -257,7 +255,8 @@ s char *flatFieldDir; char *flatFieldFile; - + + int64_t *expTime; /** mutex to synchronize main and data processing threads */ pthread_mutex_t mp; @@ -309,26 +308,22 @@ s */ double currentI0; + + int arraySize; + + + private: double *fdata; - - // private: - - -/* virtual void incrementProgress()=0; */ -/* virtual double getCurrentProgress()=0; */ -/* virtual void incrementFileIndex()=0; */ -/* virtual int setTotalProgress()=0; */ - - -/* virtual double* decodeData(int *datain, double *fdata=NULL)=0; */ -/* virtual int getTotalNumberOfChannels()=0; */ - int (*dataReady)(detectorData*,void*); void *pCallbackArg; - - int (*rawDataReady)(double*,void*); + + int (*rawDataReady)(double*,void*); void *pRawDataArg; + + + postProcessingFuncs *ppFun; + }; diff --git a/slsDetectorSoftware/slsDetectorAnalysis/postProcessingFuncs.cpp b/slsDetectorSoftware/slsDetectorAnalysis/postProcessingFuncs.cpp new file mode 100644 index 000000000..dae84c374 --- /dev/null +++ b/slsDetectorSoftware/slsDetectorAnalysis/postProcessingFuncs.cpp @@ -0,0 +1,438 @@ +#include "postProcessingFuncs.h" +#include "angleConversionConstant.h" + + +postProcessingFuncs::postProcessingFuncs(int *nModules,int *chPerMod,int modMask[],int badCh[], double ffcoeff[], double fferr[], double* t, int *dir, double angRadius[], double angOffset[], double angCentre[], double* to, double* bs, double *sX, double *sY): + nMods(0), chansPerMod(NULL), moduleMask(NULL), badChannelMask(NULL), ffCoeff(NULL), ffErr(NULL), tDead(0), angDir(1), angConv(NULL), totalOffset(0), binSize(0), sampleX(0), sampleY(0), totalChans(0), nBins(0), mp(NULL), mv(NULL), me(NULL), mm(NULL) +{ + initDataset(nModules, chPerMod,modMask,badCh, ffcoeff, fferr, t, dir, angRadius, angOffset, angCentre, to, bs, sX, sY); + +} + +int postProcessingFuncs::initDataset() { + + if (nBins) { + mp=new double[nBins]; + mv=new double[nBins]; + me=new double[nBins]; + mm=new int[nBins]; + resetMerging(mp,mv,me,mm, nBins); + } else { + mv=new double[totalChans]; + me=new double[totalChans]; + } + totalI0=0; + + return 0; + +} + +int postProcessingFuncs::finalizeDataset(double *ang, double *val, double *err, int *np) { + + if (nBins) + *np=finalizeMerging(mp,mv,me,mm,nBins); + else + *np=totalChans; + + + + if (totalI0<=0) + totalI0=1.; + + + for (int ip=0; ip<(*np); ip++) { + + if (mp) + ang[ip]=mp[ip]; + + if (mv) + val[ip]=mv[ip]*totalI0; + + if (me) + err[ip]=me[ip]*totalI0; + + } + + + + if (mp) + delete [] mp; + if (mv) + delete [] mv; + if (me) + delete [] me; + if (mm) + delete [] mm; + + + return 0; + +} + +int postProcessingFuncs::addFrame(double *data, double *pos, double *I0, double *expTime, const char *filename, int *var) { + + + double p1, vin, ein, vout, eout; + double e=0.; + double i0=*I0; + + if (i0>0) + totalI0+=i0; + + for (int ich=0; ich=0) + eout=sqrt(vout); + else + eout=0; + + //ratecorrect + + if (tDead) { + rateCorrect(vin, ein, vout, eout, tDead, *expTime); + vin=vout; + ein=eout; + } + //ffcorrect + + if (ffCoeff) { + if (ffErr) + e=ffErr[ich]; + else + e=0; + flatFieldCorrect(vin, ein, vout, eout, ffCoeff[ich], e); + } + + + //i0correct + if (i0>0) { + vout/=i0; + eout/=i0; + } + + if (badChannelMask) + if (badChannelMask[ich]) + continue; + if (nBins) { + //angconv + + //check module mask?!?!?!? + + + p1=convertAngle(*pos,ich,chansPerMod,angConv,moduleMask,totalOffset,0,angDir); + + addPointToMerging(p1,vout,eout,mp,mv,me,mm, binSize, nBins); + } else { + mv[ich]+=vout; + me[ich]+=eout*eout; + } + } + +} + + + + + +int postProcessingFuncs::initDataset(int *nModules,int *chPerMod,int modMask[],int badCh[], double ffcoeff[], double fferr[], double* t, int *dir, double angRadius[], double angOffset[], double angCenter[], double* to, double* bs, double *sX, double *sY) { + +#ifdef VERBOSE + cout << "delete pointers " << endl; +#endif + + deletePointers(); + + +#ifdef VERBOSE + cout << "nmod " << endl; +#endif + + if (nModules) + nMods=*nModules; + else + nMods=0; +#ifdef VERBOSE + cout << "tdead " << endl; +#endif + + if (t) + tDead=*t; + else + t=0; + +#ifdef VERBOSE + cout << "toffset " << endl; +#endif + + if (to) + totalOffset=*to; + else + to=0; + + +#ifdef VERBOSE + cout << "binsize " << endl; +#endif + if (bs) + binSize=*bs; + else + binSize=0; + +#ifdef VERBOSE + cout << "samplex " << endl; +#endif + if (sX) + sampleX=*sX; + else + sampleX=0; + +#ifdef VERBOSE + cout << "sampley " << endl; +#endif + if (sY) + sampleY=*sY; + else + sampleY=0; + +#ifdef VERBOSE + cout << "angdir " << endl; +#endif + if (dir) + angDir=*dir; + else + angDir=1; + + totalChans=0; + + + + chansPerMod=new int [nMods]; + + + moduleMask=new int [nMods]; + + nBins=0; + if (angRadius && angOffset && angCenter && (binSize>0)) { + angConv=new angleConversionConstant*[nMods]; + nBins=360./binSize+1; + } + + for (int im=0; im=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 postProcessingFuncs::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 postProcessingFuncs::calculateFlatField(int* nModules, int *chPerMod, int *moduleMask, int *badChannelMask, double *ffData, double *ffCoeff, double *ffErr) { + int nmed=0, im=0; + double *xmed; + + if (chPerMod==NULL) + return -1; + // if (moduleMask==NULL) + // return -1; + if (ffData==NULL) + return -1; + + if (ffErr==NULL) + return -1; + + + int totch=0; + for (int im=0; im<*nModules; im++) { + totch+=chPerMod[im]; + } + + xmed=new double[totch]; + + for (int ich=0; ich0) { + im=0; + while ((imim; i--) + xmed[i]=xmed[i-1]; + + xmed[im]=ffData[ich]; + nmed++; + + } + + + + } + + + if (nmed>1 && xmed[nmed/2]>0) { +#ifdef VERBOSE + std::cout<< "Flat field median is " << xmed[nmed/2] << " calculated using "<< nmed << " points" << std::endl; +#endif + + + + for (int ich=0; ich0) { + ffCoeff[ich]=xmed[nmed/2]/ffData[ich]; + ffErr[ich]=ffCoeff[ich]*sqrt(ffData[ich])/ffData[ich]; + } else { + ffCoeff[ich]=0.; + ffErr[ich]=1.; + } + + } + + } + + delete [] xmed; + + return 0; + +} + + + + + + diff --git a/slsDetectorSoftware/slsDetectorAnalysis/postProcessingFuncs.h b/slsDetectorSoftware/slsDetectorAnalysis/postProcessingFuncs.h new file mode 100644 index 000000000..c99fa827e --- /dev/null +++ b/slsDetectorSoftware/slsDetectorAnalysis/postProcessingFuncs.h @@ -0,0 +1,80 @@ +#ifndef POSTPROCESSINGFUNCS_H +#define POSTPROCESSINGFUNC_H + + + +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "angularConversionStatic.h" +class angleConversionConstant; + +using namespace std; + + +class postProcessingFuncs : public virtual angularConversionStatic + +{ + + public: + postProcessingFuncs(int *nModules=NULL,int *chPerMod=NULL,int *modMask=NULL,int *badChMask=NULL, double *ffcoeff=NULL, double *fferr=NULL, double* t=NULL, int *dir=NULL, double *angRadius=NULL, double *angOffset=NULL, double *angCentre=NULL, double* to=NULL, double* bs=NULL, double *sX=NULL, double *sY=NULL); + + + ~postProcessingFuncs(); + + + int initDataset(int *nModules,int *chPerMod,int modMask[],int badCh[], double ffcoeff[], double fferr[], double* tDead, int *dir, double angRadius[], double angOffset[], double angCentre[], double* to, double* bs, double *sX, double *sY); + + int initDataset(); + + + int finalizeDataset(double ang[], double val[], double err[], int *np); + + int addFrame(double data[], double *pos, double *IO, double *expTime, const char *filename, int *var=0); + + static int calculateFlatField(int* nModules, int *chPerMod, int moduleMask[], int badChannelMask[], double ffData[], double ffCoeff[], double ffErr[]); + + static int flatFieldCorrect(double datain, double errin, double &dataout, double &errout, double ffcoefficient, double fferr); + + + static int rateCorrect(double datain, double errin, double &dataout, double &errout, double tau, double t); + + private: + void deletePointers(); + + + int nMods; + int *chansPerMod; + int *moduleMask; + int *badChannelMask; + double *ffCoeff; + double *ffErr; + double tDead; + int angDir; + angleConversionConstant **angConv; + double totalOffset; + double binSize; + double sampleX; + double sampleY; + int totalChans; + + int nBins; + + double totalI0; + + + double *mp, *mv,*me; + int *mm; + +}; + + +#endif