#include "slsDetectorUtils.h" #include "usersFunctions.h" #include #include slsDetectorUtils::slsDetectorUtils() : queuesize(0), currentPosition(0), currentPositionIndex(0), currentI0(0), mergingBins(NULL), mergingCounts(NULL), mergingErrors(NULL), mergingMultiplicity(NULL), badChannelMask(NULL) { pthread_mutex_t mp1 = PTHREAD_MUTEX_INITIALIZER; mp=mp1; pthread_mutex_init(&mp, NULL); } int slsDetectorUtils::getPointers(int * const l_stoppedFlag, \ int * const l_threadedProcessing, \ int * const l_actionMask, \ mystring * const l_actionScript, \ mystring * const l_actionParameter, \ int * const l_nScanSteps, \ int * const l_scanMode, \ mystring * const l_scanScript, \ mystring * const l_scanParameter, \ mysteps * const l_scanSteps, \ int * const l_scanPrecision, \ int * const l_numberOfPositions, \ float * const l_detPositions, \ char * const l_angConvFile, \ int * const l_correctionMask, \ float * const l_binSize, \ float * const l_fineOffset, \ float * const l_globalOffset, \ int * const l_angDirection, \ char * const l_flatFieldDir, \ char * const l_flatFieldFile, \ char * const l_badChanFile, \ int64_t * const l_timerValue, \ detectorSettings * const l_currentSettings, \ int * const l_currentThresholdEV, \ char * const l_filePath, \ char * const l_fileName, \ int * const l_fileIndex) { stoppedFlag=l_stoppedFlag; threadedProcessing=l_threadedProcessing; actionMask=l_actionMask; actionScript=l_actionScript; actionParameter=l_actionParameter; nScanSteps=l_nScanSteps; scanSteps=l_scanSteps; scanScript=l_scanScript; scanParameter=l_scanParameter; scanMode=l_scanMode; scanPrecision=l_scanPrecision; numberOfPositions=l_numberOfPositions; detPositions=l_detPositions; angConvFile=l_angConvFile; correctionMask=l_correctionMask; binSize=l_binSize; fineOffset=l_fineOffset; globalOffset=l_globalOffset; angDirection=l_angDirection; flatFieldDir=l_flatFieldDir; flatFieldFile=l_flatFieldFile; badChanFile=l_badChanFile; // nBadChans=l_nBadChans; // badChansList=l_badChansList; // nBadFF=l_nBadFF; // badFFList=l_badFFList; timerValue=l_timerValue; currentSettings=l_currentSettings; currentThresholdEV=l_currentThresholdEV; filePath=l_filePath; fileName=l_fileName; fileIndex=l_fileIndex; return OK; } string slsDetectorUtils::createFileName(char *filepath, char *filename, int aMask, float sv0, int prec0, float 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 slsDetectorUtils::createFileName() { return createFileName(filePath, \ fileName, \ *actionMask, \ currentScanVariable[0], \ scanPrecision[0], \ currentScanVariable[1], \ scanPrecision[1], \ currentPositionIndex, \ *numberOfPositions, \ *fileIndex\ ); } int slsDetectorUtils::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 slsDetectorUtils::getVariablesFromFileName(string fname, int &index, int &p_index, float &sv0, float &sv1) { int i; float 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); } #ifdef VERBOSE else cout << "******************************** cannot parse file index" << endl; 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); } #ifdef VERBOSE else cout << "******************************** cannot parse position index" << endl; 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); } #ifdef VERBOSE else cout << "******************************** cannot parse scan varable 1" << endl; 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 } #ifdef VERBOSE else cout << "******************************** cannot parse scan varable 0" << endl; #endif return index; } int slsDetectorUtils::flatFieldCorrect(float datain, float errin, float &dataout, float &errout, float ffcoefficient, float fferr){ float 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.; return 0; }; int slsDetectorUtils::rateCorrect(float datain, float errin, float &dataout, float &errout, float tau, float t){ // float data; float 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 slsDetectorUtils::setBadChannelCorrection(string fname, int &nbad, int *badlist){ ifstream infile; string str; int interrupt=0; int ich; int chmin,chmax; #ifdef VERBOSE std::cout << "Setting bad channel correction to " << fname << std::endl; #endif if (fname=="") { nbad=0; return 0; } else { infile.open(fname.c_str(), ios_base::in); if (infile.is_open()==0) { std::cout << "could not open file " << fname <> 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> ich; #ifdef VERBOSE std::cout << "channel "<< ich << std::endl; #endif if (nbad0 && nbad> ss >> mod; ssstr >> ss >> center; ssstr >> ss >> ecenter; ssstr >> ss >> r_conv; ssstr >> ss >> er_conv; ssstr >> ss >> off; ssstr >> ss >> eoff; if (mod=0) { angOff[mod].center=center; angOff[mod].r_conversion=r_conv; angOff[mod].offset=off; angOff[mod].ecenter=ecenter; angOff[mod].er_conversion=er_conv; angOff[mod].eoffset=eoff; nm++; } } return nm; } int slsDetectorUtils:: writeAngularConversion(string fname, int nmod, angleConversionConstant *angOff) { 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; } int slsDetectorUtils:: writeAngularConversion(ofstream& outfile, int nmod, angleConversionConstant *angOff) { for (int imod=0; imod0) { 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; } int slsDetectorUtils::addToMerging(float *p1, float *v1, float *e1, float *mp, float *mv,float *me, int *mm, int nchans, float binsize,int angDir, int cm, int *badChanMask ) { float binmi=-180., binma; int ibin=0; int imod; float ang=0; binmi=-180.; binma=binmi+binsize; if (angDir>0) { for (int ip=0; ip=0; ip--) { if ((cm)&(1<< DISCARD_BAD_CHANNELS)) { if (badChanMask[ip]) continue; } while (binma0) binsize=*binSize; else return FAIL; return resetMerging(mp, mv, me, mm, binsize); } int slsDetectorUtils::finalizeMerging(float *mp, float *mv, float *me, int *mm) { float binsize; if (*binSize>0) binsize=*binSize; else return FAIL; return finalizeMerging(mp, mv, me, mm, binsize); } int slsDetectorUtils::addToMerging(float *p1, float *v1, float *e1, float *mp, float *mv,float *me, int *mm) { float binsize; int ibin=0; int imod; int del=0; if (*binSize>0) binsize=*binSize; else return FAIL; if (p1==NULL) { del=1; p1=convertAngles(currentPosition); } int ret=addToMerging(p1, v1, e1, mp, mv,me, mm,getTotalNumberOfChannels(), binsize,*angDirection, *correctionMask, badChannelMask ); if (del) { delete [] p1; p1=NULL; } return ret; } int slsDetectorUtils::writeDataFile(string fname, int nch, float *data, float *err, float *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); return OK; } else { std::cout<< "Could not open file " << fname << "for writing"<< std::endl; return FAIL; } }; int slsDetectorUtils::writeDataFile(ofstream &outfile, int nch, float *data, float *err, float *ang, char dataformat){ int idata; if (data==NULL) return FAIL; // args|=0x10; // one line per channel! for (int ichan=0; ichan> 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; } 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> 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> 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> offset >> gain; infile.close(); } else { std::cout<< "Could not open calibration file "<< fname << std::endl; gain=0.; offset=0.; return -1; } return 0; }; int slsDetectorUtils::writeCalibrationFile(string fname, float gain, float offset){ //std::cout<< "Function not yet implemented " << std::endl; ofstream outfile; outfile.open (fname.c_str()); // >> i/o operations here << if (outfile.is_open()) { outfile << offset << " " << gain << std::endl; } else { std::cout<< "Could not open calibration file "<< fname << " for writing" << std::endl; return -1; } outfile.close(); return 0; }; void slsDetectorUtils::acquire(int delflag){ void *status; int trimbit; char cmd[MAX_STR_LENGTH]; int startindex=*fileIndex; int nowindex=startindex, lastindex=startindex; string fn; //string sett; if ((*correctionMask&(1<< ANGULAR_CONVERSION)) || (*correctionMask&(1<< I0_NORMALIZATION))) connect_channels(); progressIndex=0; *stoppedFlag=0; resetFinalDataQueue(); resetDataQueue(); //cout << "main mutex lock line 6188" << endl; pthread_mutex_lock(&mp); jointhread=0; queuesize=0; pthread_mutex_unlock(&mp); //cout << "main mutex unlock line 6188" << endl; if (*threadedProcessing) { startThread(delflag); } //cout << "data thread started " << endl; int np=1; if (*numberOfPositions>0) 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; //cout << "action at start" << endl; if (stoppedFlag==0) { if (*actionMask & (1 << startScript)) { //"Custom start script. The arguments are passed as nrun=n par=p."); sprintf(cmd,"%s nrun=%d par=%s",getActionScript(startScript).c_str(),*fileIndex,getActionParameter(startScript).c_str()); #ifdef VERBOSE cout << "Executing start script " << cmd << endl; #endif system(cmd); } } for (int is0=0; is00) { go_to_position (detPositions[ip]); currentPositionIndex=ip+1; #ifdef VERBOSE std::cout<< "moving to position" << std::endl; #endif } } else break; //write header before? //cmd=headerBeforeScript; //Custom script to write the header. \n The arguments will be passed as nrun=n fn=filenam acqtime=t gainmode=g threshold=thr badfile=badf angfile=angf bloffset=blo fineoffset=fo fffile=fffn tau=deadtau par=p") // cout << "aaaaa" << endl; //cout << createFileName() << endl; //cout << "bbbbb" << endl; fn=createFileName(); //cout << fn << endl; nowindex=*fileIndex; if (*stoppedFlag==0) { if (*correctionMask&(1<< I0_NORMALIZATION)) currentI0=get_i0(); // cout << "header " << endl; if (*actionMask & (1 << headerBefore)) { //Custom script after each frame. The arguments are passed as nrun=n fn=filename par=p sv0=scanvar0 sv1=scanvar1 p0=par0 p1=par1" sprintf(cmd,"%s nrun=%d fn=%s acqtime=%f gainmode=%d threshold=%d badfile=%s angfile=%s bloffset=%f fineoffset=%f fffile=%s/%s tau=%f par=%s",\ getActionScript(headerBefore).c_str(),\ nowindex,\ fn.c_str(), \ ((float)timerValue[ACQUISITION_TIME])*1E-9, \ *currentSettings, \ *currentThresholdEV, \ getBadChannelCorrectionFile().c_str(), \ getAngularConversion().c_str(), \ *globalOffset, \ *fineOffset,\ getFlatFieldCorrectionDir(),\ getFlatFieldCorrectionFile(), \ getRateCorrectionTau(),\ getActionParameter(headerBefore).c_str()\ ); #ifdef VERBOSE cout << "Executing header before " << cmd << endl; #endif system(cmd); } } else break; if (*stoppedFlag==0) { // cout << "starting???? " << endl; startAndReadAll(); if (*correctionMask&(1<< ANGULAR_CONVERSION)) currentPosition=get_position(); if (*correctionMask&(1<< I0_NORMALIZATION)) currentI0=get_i0()-currentI0; ////////////////// Reput in in case of unthreaded processing // if (*threadedProcessing==0) // processData(delflag); } else break; pthread_mutex_lock(&mp); while (queuesize){ pthread_mutex_unlock(&mp); usleep(10000); pthread_mutex_lock(&mp); } pthread_mutex_unlock(&mp); if (stoppedFlag==0) { if (*actionMask & (1 << headerAfter)) { //Custom script after each frame. The arguments are passed as nrun=n fn=filename par=p sv0=scanvar0 sv1=scanvar1 p0=par0 p1=par1" sprintf(cmd,"%s nrun=%d fn=%s acqtime=%f gainmode=%d threshold=%d badfile=%s angfile=%s bloffset=%f fineoffset=%f fffile=%s/%s tau=%f par=%s", \ getActionScript(headerAfter).c_str(), \ nowindex, \ fn.c_str(), \ ((float)timerValue[ACQUISITION_TIME])*1E-9, \ *currentSettings, \ *currentThresholdEV, \ getBadChannelCorrectionFile().c_str(), \ getAngularConversion().c_str(), \ *globalOffset, \ *fineOffset, \ getFlatFieldCorrectionDir(), \ getFlatFieldCorrectionFile(), \ getRateCorrectionTau(), \ getActionParameter(headerAfter).c_str()); #ifdef VERBOSE cout << "Executing header after " << cmd << endl; #endif system(cmd); } if (*fileIndex>lastindex) lastindex=*fileIndex; } else { if (*fileIndex>lastindex) lastindex=*fileIndex; break; } if (*stoppedFlag) { #ifdef VERBOSE std::cout<< "exiting since the detector has been stopped" << std::endl; #endif break; } else if (ip<(np-1)) { *fileIndex=startindex; } } // loop on position finished //script after if (*stoppedFlag==0) { if (*actionMask & (1 << scriptAfter)) { //Custom script after each frame. The arguments are passed as nrun=n fn=filename par=p sv0=scanvar0 sv1=scanvar1 p0=par0 p1=par1" sprintf(cmd,"%s nrun=%d fn=%s par=%s sv0=%f sv1=%f p0=%s p1=%s",getActionScript(scriptAfter).c_str(),*fileIndex,createFileName().c_str(),getActionParameter(scriptAfter).c_str(),currentScanVariable[0],currentScanVariable[1],getScanParameter(0).c_str(),getScanParameter(1).c_str()); #ifdef VERBOSE cout << "Executing script after " << cmd << endl; #endif system(cmd); } } else break; if (*stoppedFlag) { #ifdef VERBOSE std::cout<< "exiting since the detector has been stopped" << std::endl; #endif break; } else if (is1<(ns1-1)) { *fileIndex=startindex; } } //end scan1 loop is1 //currentScanVariable[MAX_SCAN_LEVELS]; if (*stoppedFlag) { #ifdef VERBOSE std::cout<< "exiting since the detector has been stopped" << std::endl; #endif break; } else if (is0<(ns0-1)) { *fileIndex=startindex; } } //end scan0 loop is0 *fileIndex=lastindex; if (*stoppedFlag==0) { if (*actionMask & (1 << stopScript)) { //Custom stop script. The arguments are passed as nrun=n par=p. sprintf(cmd,"%s nrun=%d par=%s",getActionScript(stopScript).c_str(),*fileIndex,getActionParameter(stopScript).c_str()); #ifdef VERBOSE cout << "Executing stop script " << cmd << endl; #endif system(cmd); } } if (*threadedProcessing) { //#ifdef VERBOSE // std::cout<< " ***********************waiting for data processing thread to finish " << queuesize <<" " << thisDetector->fileIndex << std::endl ; //#endif //cout << "main mutex lock line 6488" << endl; pthread_mutex_lock(&mp); jointhread=1; pthread_mutex_unlock(&mp); //cout << "main mutex unlock line 6488" << endl; pthread_join(dataProcessingThread, &status); // std::cout<< " ***********************data processing finished " << queuesize <<" " << thisDetector->fileIndex << std::endl ; } if ((*correctionMask&(1<< ANGULAR_CONVERSION)) || (*correctionMask&(1<< I0_NORMALIZATION))) disconnect_channels(); } void* slsDetectorUtils::processData(int delflag) { //cout << "thread mutex lock line 6505" << endl; pthread_mutex_lock(&mp); queuesize=dataQueue.size(); pthread_mutex_unlock(&mp); //cout << "thread mutex unlock line 6505" << endl; int *myData; float *fdata; float *rcdata=NULL, *rcerr=NULL; float *ffcdata=NULL, *ffcerr=NULL; float *ang=NULL; float bs=0.004; int imod; int nb; int np; detectorData *thisData; int dum=1; string ext; string fname; #ifdef ACQVERBOSE std::cout<< " processing data - threaded mode " << *threadedProcessing; #endif if (*correctionMask!=0) { ext=".dat"; } else { ext=".raw"; } while(dum | *threadedProcessing) { // ???????????????????????? // while( !dataQueue.empty() ) { //cout << "thread mutex lock line 6539" << endl; pthread_mutex_lock(&mp); while((queuesize=dataQueue.size())>0) { pthread_mutex_unlock(&mp); //cout << "thread mutex unlock line 6543" << endl; //queuesize=dataQueue.size(); /** Pop data queue */ myData=dataQueue.front(); // get the data from the queue if (myData) { progressIndex++; #ifdef VERBOSE cout << "Progress is " << getCurrentProgress() << " \%" << endl; #endif //process data /** decode data */ fdata=decodeData(myData); fname=createFileName(); /** write raw data file */ if (*correctionMask==0 && delflag==1) { //cout << "line 6570----" << endl; writeDataFile (fname+string(".raw"), getTotalNumberOfChannels(),fdata, NULL, NULL, 'i'); delete [] fdata; } else { //cout << "line 6574----" << endl; writeDataFile (fname+string(".raw"), getTotalNumberOfChannels(),fdata, NULL, NULL, 'i'); /** rate correction */ if (*correctionMask&(1<0) bs=*binSize; //else // if (angOff[0].r_conversion>0) { // bs=180./PI*atan(angOff[0].r_conversion); // *binSize=bs; // } else *binSize=bs; nb=(int)(360./bs); mergingBins=new float[nb]; mergingCounts=new float[nb]; mergingErrors=new float[nb]; mergingMultiplicity=new int[nb]; resetMerging(mergingBins, mergingCounts,mergingErrors, mergingMultiplicity, bs); } /* it would be better to create an ang0 with 0 encoder position and add to merging/write to file simply specifying that offset so that when it cycles writing the data or adding to merging it also calculates the angular position */ ang=convertAngles(currentPosition); if (*correctionMask!=0) { //cout << "line 6633----" << endl; if (*numberOfPositions>1) writeDataFile (fname+string(".dat"), getTotalNumberOfChannels(), ffcdata, ffcerr,ang); } addToMerging(ang, ffcdata, ffcerr, mergingBins, mergingCounts,mergingErrors, mergingMultiplicity, getTotalNumberOfChannels(), bs, *angDirection, *correctionMask, badChannelMask ); if ((currentPositionIndex==*numberOfPositions) || (currentPositionIndex==0)) { np=finalizeMerging(mergingBins, mergingCounts,mergingErrors, mergingMultiplicity, bs); /** file writing */ currentPositionIndex++; fname=createFileName(); if (*correctionMask!=0) { // cout << "line 6643----" << endl; writeDataFile (fname+string(".dat"),np,mergingCounts, mergingErrors, mergingBins,'f'); } if (delflag) { delete [] mergingBins; delete [] mergingCounts; delete [] mergingErrors; delete [] mergingMultiplicity; } else { thisData=new detectorData(mergingCounts,mergingErrors,mergingBins,getCurrentProgress(),(fname+string(ext)).c_str(),np);/* if (thisDetector->*correctionMask!=0) { //thisData=new detectorData(mergingCounts,mergingErrors,mergingBins,thisDetector->progressIndex+1,(fname().append(".dat")).c_str(),np); thisData=new detectorData(mergingCounts,mergingErrors,mergingBins,getCurrentProgress(),(fname().append(".dat")).c_str(),np); } else { thisData=new detectorData(mergingCounts,mergingErrors,mergingBins,getCurrentProgress(),(fname().append(".raw")).c_str(),np); //thisData=new detectorData(mergingCounts,mergingErrors,mergingBins,thisDetector->progressIndex+1,(fname().append(".raw")).c_str(),np); }*/ finalDataQueue.push(thisData); } } if (ffcdata) delete [] ffcdata; if (ffcerr) delete [] ffcerr; if (ang) delete [] ang; } else { if (*correctionMask!=0) { // cout << "line 6672----" << endl; writeDataFile (fname+string(".dat"), getTotalNumberOfChannels(), ffcdata, ffcerr); } if (delflag) { if (ffcdata) delete [] ffcdata; if (ffcerr) delete [] ffcerr; if (ang) delete [] ang; } else { thisData=new detectorData(ffcdata,ffcerr,NULL,getCurrentProgress(),(fname+string(ext)).c_str(),getTotalNumberOfChannels()); /* if (thisDetector->*correctionMask!=0) { thisData=new detectorData(ffcdata,ffcerr,NULL,getCurrentProgress(),(fname().append(".dat")).c_str(),thisDetector->nChans*thisDetector->nChips*thisDetector->nMods); //thisData=new detectorData(ffcdata,ffcerr,NULL,thisDetector->progressIndex+1,(fname().append(".dat")).c_str(),thisDetector->nChans*thisDetector->nChips*thisDetector->nMods); } else { thisData=new detectorData(ffcdata,ffcerr,NULL,getCurrentProgress(),(fname().append(".raw")).c_str(),thisDetector->nChans*thisDetector->nChips*thisDetector->nMods); //thisData=new detectorData(ffcdata,ffcerr,NULL,thisDetector->progressIndex+1,(fname().append(".raw")).c_str(),thisDetector->nChans*thisDetector->nChips*thisDetector->nMods); } */ finalDataQueue.push(thisData); } } } *fileIndex++; /* thisDetector->progressIndex++; #ifdef VERBOSE cout << "Progress is " << getCurrentProgress() << " \%" << endl; #endif */ delete [] myData; myData=NULL; dataQueue.pop(); //remove the data from the queue //cout << "thread mutex lock line 6697" << endl; pthread_mutex_lock(&mp); queuesize=dataQueue.size(); pthread_mutex_unlock(&mp); //cout << "thread mutex unlock line 6697" << endl; usleep(1000); //pthread_mutex_unlock(&mp); } pthread_mutex_unlock(&mp); //cout << "thread mutex unlock line 6706" << endl; usleep(1000); // cout << "PPPPPPPPPPPPPPPPPPPP " << queuesize << " " << thisDetector->fileIndex << endl; } pthread_mutex_unlock(&mp); //cout << "thread mutex unlock line 6711" << endl; //cout << "thread mutex lock line 6711" << endl; pthread_mutex_lock(&mp); if (jointhread) { pthread_mutex_unlock(&mp); //cout << "thread mutex unlock line 6715" << endl; if (dataQueue.size()==0) break; } else pthread_mutex_unlock(&mp); //cout << "thread mutex unlock line 6720" << endl; dum=0; } // ???????????????????????? return 0; } int* slsDetectorUtils::popDataQueue() { int *retval=NULL; if( !dataQueue.empty() ) { retval=dataQueue.front(); dataQueue.pop(); } return retval; } detectorData* slsDetectorUtils::popFinalDataQueue() { detectorData *retval=NULL; if( !finalDataQueue.empty() ) { retval=finalDataQueue.front(); finalDataQueue.pop(); } return retval; } void slsDetectorUtils::resetDataQueue() { int *retval=NULL; while( !dataQueue.empty() ) { retval=dataQueue.front(); dataQueue.pop(); delete [] retval; } } void slsDetectorUtils::resetFinalDataQueue() { detectorData *retval=NULL; while( !finalDataQueue.empty() ) { retval=finalDataQueue.front(); finalDataQueue.pop(); delete retval; } } void slsDetectorUtils::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, ¶m); } int slsDetectorUtils::fillBadChannelMask() { int nbad=0; if (*correctionMask&(1<< DISCARD_BAD_CHANNELS)) { nbad=getBadChannelCorrection(); if (nbad>0) { int *badChansList=new int[nbad]; getBadChannelCorrection(badChansList); if (badChannelMask) delete [] badChannelMask; badChannelMask=new int[getTotalNumberOfChannels()]; for (int ichan=0; ichan=0 ) { if (badChannelMask[badChansList[ichan]]==0) nbad++; badChannelMask[badChansList[ichan]]=1; } } delete [] badChansList; } else { if (badChannelMask) { delete [] badChannelMask; badChannelMask=NULL; } } } else { if (badChannelMask) { delete [] badChannelMask; badChannelMask=NULL; } } return nbad; } /* set positions for the acquisition \param nPos number of positions \param pos array with the encoder positions \returns number of positions */ int slsDetectorUtils::setPositions(int nPos, float *pos){ if (nPos>=0) *numberOfPositions=nPos; for (int ip=0; ip0 otherwise */ int slsDetectorUtils::setAction(int iaction, string fname, string par) { int am; if (iaction>=0 && iaction=0 && iaction=0 && iaction=0 && iaction=0 && iaction0 otherwise */ int slsDetectorUtils::setScan(int iscan, string script, int nvalues, float *values, string par, int precision) { if (iscan>=0 && iscan=0) { if (nvalues==0) scanMode[iscan]=0; else { nScanSteps[iscan]=nvalues; if (nvalues>MAX_SCAN_STEPS) nScanSteps[iscan]=MAX_SCAN_STEPS; } } if (values && scanMode[iscan]>0 ) { for (int iv=0; iv=0) scanPrecision[iscan]=precision; if (scanMode[iscan]>0){ *actionMask |= 1<< (iscan+MAX_ACTIONS); } else { *actionMask &= ~(1 << (iscan+MAX_ACTIONS)); } setTotalProgress(); return scanMode[iscan]; } else return -1; } int slsDetectorUtils::setScanScript(int iscan, string script) { if (iscan>=0 && iscan0){ *actionMask |= (1 << (iscan+MAX_ACTIONS)); } else { *actionMask &= ~(1 << (iscan+MAX_ACTIONS)); } setTotalProgress(); #ifdef VERBOSE cout << "Action mask is " << hex << actionMask << dec << endl; #endif return scanMode[iscan]; } else return -1; } int slsDetectorUtils::setScanParameter(int iscan, string par) { if (iscan>=0 && iscan=0 && iscan=0) scanPrecision[iscan]=precision; return scanMode[iscan]; } else return -1; } int slsDetectorUtils::setScanSteps(int iscan, int nvalues, float *values) { if (iscan>=0 && iscan=0) { if (nvalues==0) scanMode[iscan]=0; else { nScanSteps[iscan]=nvalues; if (nvalues>MAX_SCAN_STEPS) nScanSteps[iscan]=MAX_SCAN_STEPS; } } if (values) { for (int iv=0; iv0){ *actionMask |= (1 << (iscan+MAX_ACTIONS)); } else { *actionMask &= ~(1 << (iscan+MAX_ACTIONS)); } #ifdef VERBOSE cout << "Action mask is " << hex << actionMask << dec << endl; #endif setTotalProgress(); return scanMode[iscan]; } else return -1; } /** returns scan script \param iscan can be (0,1) \returns scan script */ string slsDetectorUtils::getScanScript(int iscan){ if (iscan>=0 && iscan=0 && iscan=0 && iscan=0 && iscan=0 && iscan0) nc=timerValue[CYCLES_NUMBER]; if (*numberOfPositions>0) npos=*numberOfPositions; if ((nScanSteps[0]>0) && (*actionMask & (1 << MAX_ACTIONS))) nscan[0]=nScanSteps[0]; if ((nScanSteps[1]>0) && (*actionMask & (1 << (MAX_ACTIONS+1)))) nscan[1]=nScanSteps[1]; totalProgress=nf*nc*npos*nscan[0]*nscan[1]; #ifdef VERBOSE cout << "nc " << nc << endl; cout << "nf " << nf << endl; cout << "npos " << npos << endl; cout << "nscan[0] " << nscan[0] << endl; cout << "nscan[1] " << nscan[1] << endl; cout << "Set total progress " << totalProgress << endl; #endif return totalProgress; } float slsDetectorUtils::getCurrentProgress() { return 100.*((float)progressIndex)/((float)totalProgress); }