#include "mythenDetector.h" #include "usersFunctions.h" using namespace std; string mythenDetector::executeLine(int narg, char *args[], int action) { #ifdef VERBOSE for (int ia=0; ia1) sval=string(args[1]); else sval="none"; float corr[24*1280], ecorr[24*1280]; if (getFlatFieldCorrection(corr,ecorr)) { if (sval!="none") { writeDataFile(sval,corr,ecorr,NULL,'i'); return sval; } return string(getFlatFieldCorrectionFile()); } else { return string("none"); } } } else if (var=="ratecorr") { if (action==PUT_ACTION) { sscanf(args[1],"%f",&fval); setRateCorrection(fval); } float t; if (getRateCorrection(t)) { sprintf(answer,"%f",t); } else { sprintf(answer,"%f",0); } return string(answer); } else if (var=="badchannels") { if (action==PUT_ACTION) { sval=string(args[1]); if (sval=="none") sval=""; setBadChannelCorrection(sval); } else if (action==GET_ACTION) { if (narg>1) sval=string(args[1]); else sval="none"; int bch[24*1280], nbch; if ((nbch=getBadChannelCorrection(bch))) { if (sval!="none") { ofstream outfile; outfile.open (sval.c_str(),ios_base::out); if (outfile.is_open()) { for (int ich=0; ich1) sval=string(args[1]); else sval="none"; int dir; if (getAngularConversion(dir)) { if (sval!="none") { writeAngularConversion(sval.c_str()); return sval; } return string(getAngularConversion()); } else { return string("none"); } } } else if (var=="globaloff") { if (action==PUT_ACTION) { sscanf(args[1],"%f",&fval); setGlobalOffset(fval); } sprintf(answer,"%f",getGlobalOffset()); return string(answer); } else if (var=="fineoff") { if (action==PUT_ACTION) { sscanf(args[1],"%f",&fval); setFineOffset(fval); } sprintf(answer,"%f",getFineOffset()); return string(answer); } else if (var=="binsize") { if (action==PUT_ACTION) { sscanf(args[1],"%f",&fval); setBinSize(fval); } sprintf(answer,"%f",getBinSize()); return string(answer); } else if (var=="positions") { if (action==PUT_ACTION) { sscanf(args[1],"%d",&ival); float pos[ival]; for (int ip=0; ip> ival; externalSignalFlag flag=GET_EXTERNAL_SIGNAL_FLAG; if (action==PUT_ACTION) { sval=string(args[1]); #ifdef VERBOSE cout << "sig " << ival << " flag " << sval; #endif if (sval=="off") flag=SIGNAL_OFF; else if (sval=="gate_in_active_high") flag=GATE_IN_ACTIVE_HIGH; else if (sval=="gate_in_active_low") flag=GATE_IN_ACTIVE_LOW; else if (sval=="trigger_in_rising_edge") flag=TRIGGER_IN_RISING_EDGE; else if (sval=="trigger_in_falling_edge") flag=TRIGGER_IN_FALLING_EDGE; else if (sval=="ro_trigger_in_rising_edge") flag=RO_TRIGGER_IN_RISING_EDGE; else if (sval=="ro_trigger_in_falling_edge") flag=RO_TRIGGER_IN_FALLING_EDGE; else if (sval=="gate_out_active_high") flag=GATE_OUT_ACTIVE_HIGH; else if (sval=="gate_out_active_low") flag=GATE_OUT_ACTIVE_LOW; else if (sval=="trigger_out_rising_edge") flag=TRIGGER_OUT_RISING_EDGE; else if (sval=="trigger_out_falling_edge") flag=TRIGGER_OUT_FALLING_EDGE; else if (sval=="ro_trigger_out_rising_edge") flag=RO_TRIGGER_OUT_RISING_EDGE; else if (sval=="ro_trigger_out_falling_edge") flag=RO_TRIGGER_OUT_FALLING_EDGE; setExternalSignalFlags(flag,ival); } switch (setExternalSignalFlags( GET_EXTERNAL_SIGNAL_FLAG,ival)) { case SIGNAL_OFF: return string( "off"); case GATE_IN_ACTIVE_HIGH: return string( "gate_in_active_high"); case GATE_IN_ACTIVE_LOW: return string( "gate_in_active_low"); case TRIGGER_IN_RISING_EDGE: return string( "trigger_in_rising_edge"); case TRIGGER_IN_FALLING_EDGE: return string( "trigger_in_falling_edge"); case RO_TRIGGER_IN_RISING_EDGE: return string( "ro_trigger_in_rising_edge"); case RO_TRIGGER_IN_FALLING_EDGE: return string( "ro_trigger_in_falling_edge"); case GATE_OUT_ACTIVE_HIGH: return string( "gate_out_active_high"); case GATE_OUT_ACTIVE_LOW: return string( "gate_out_active_low"); case TRIGGER_OUT_RISING_EDGE: return string( "trigger_out_rising_edge"); case TRIGGER_OUT_FALLING_EDGE: return string( "trigger_out_falling_edge"); case RO_TRIGGER_OUT_RISING_EDGE: return string( "ro_trigger_out_rising_edge"); case RO_TRIGGER_OUT_FALLING_EDGE: return string( "ro_trigger_out_falling_edge"); default: return string( "unknown"); } } else if (var=="modulenumber") { if (action==PUT_ACTION) { return string("cannot set"); } istringstream vvstr(var.substr(7)); vvstr >> ival; sprintf(answer,"%x",getId(MODULE_SERIAL_NUMBER,ival)); return string(answer); } else if (var=="moduleversion") { if (action==PUT_ACTION) { return string("cannot set" ); } sprintf(answer,"%x",getId(MODULE_FIRMWARE_VERSION)); return string(answer); } else if (var=="detectornumber") { if (action==PUT_ACTION) { return string("cannot set "); } sprintf(answer,"%x",getId(DETECTOR_SERIAL_NUMBER)); return string(answer); } else if (var=="detectorversion") { if (action==PUT_ACTION) { return string("cannot set "); } sprintf(answer,"%x",getId(DETECTOR_FIRMWARE_VERSION)); return string(answer); } else if (var=="softwareversion") { if (action==PUT_ACTION) { return string("cannot set "); } sprintf(answer,"%x",getId(DETECTOR_SOFTWARE_VERSION)); return string(answer); } else if (var=="digitest") { if (action==PUT_ACTION) { return string("cannot set "); } istringstream vvstr(var.substr(9)); vvstr >> ival; sprintf(answer,"%x",digitalTest(CHIP_TEST, ival)); return string(answer); } else if (var=="bustest") { if (action==PUT_ACTION) { return string("cannot set "); } sprintf(answer,"%x",digitalTest(DETECTOR_BUS_TEST)); return string(answer); } else if (var=="settings") { if (action==PUT_ACTION) { sval=string(args[1]); detectorSettings sett=GET_SETTINGS; if (sval=="standard") sett=STANDARD; else if (sval=="fast") sett=FAST; else if (sval=="highgain") sett=HIGHGAIN; setSettings(sett); } switch (setSettings( GET_SETTINGS)) { case STANDARD: return string("standard"); case FAST: return string("fast"); case HIGHGAIN: return string("highgain"); default: return string("undefined"); } } else if (var=="threshold") { if (action==PUT_ACTION) { sscanf(args[1],"%d",&ival); setThresholdEnergy(ival); } sprintf(answer,"%d",getThresholdEnergy()); return string(answer); } //timers else if (var=="exptime") { if (action==PUT_ACTION) { sscanf(args[1],"%f",&fval);// in seconds! setTimer(ACQUISITION_TIME,fval*1E+9); } sprintf(answer,"%f",(float)setTimer(ACQUISITION_TIME)*1E-9); return string(answer); } else if (var=="period") { if (action==PUT_ACTION) { sscanf(args[1],"%f",&fval);// in seconds! setTimer(FRAME_PERIOD,fval*1E+9); } sprintf(answer,"%f",(float)setTimer(FRAME_PERIOD)*1E-9); return string(answer); } else if (var=="delay") { if (action==PUT_ACTION) { sscanf(args[1],"%f",&fval);// in seconds! setTimer(DELAY_AFTER_TRIGGER,fval*1E+9); } sprintf(answer,"%f",(float)setTimer(DELAY_AFTER_TRIGGER)*1E-9); return string(answer); } else if (var=="gates") { if (action==PUT_ACTION) { sscanf(args[1],"%d",&ival); setTimer( GATES_NUMBER,ival); } sprintf(answer,"%d",setTimer(GATES_NUMBER)); return string(answer); } else if (var=="frames") { if (action==PUT_ACTION) { sscanf(args[1],"%d",&ival); setTimer(FRAME_NUMBER,ival); } sprintf(answer,"%d",setTimer(FRAME_NUMBER)); return string(answer); } else if (var=="cycles") { if (action==PUT_ACTION) { sscanf(args[1],"%d",&ival); setTimer(CYCLES_NUMBER,ival); } sprintf(answer,"%d",setTimer(CYCLES_NUMBER)); return string(answer); } else if (var=="probes") { if (action==PUT_ACTION) { sscanf(args[1],"%d",&ival); setTimer(PROBES_NUMBER,ival); } sprintf(answer,"%d",setTimer(PROBES_NUMBER)); return string(answer); } else if (var=="dr") { if (action==PUT_ACTION) { sscanf(args[1],"%d",&ival); setDynamicRange(ival); } sprintf(answer,"%d",setDynamicRange()); return string(answer); } else if (var=="flags") { if (action==PUT_ACTION) { sval=string(args[1]); readOutFlags flag=GET_READOUT_FLAGS; if (sval=="none") flag=NORMAL_READOUT; else if (sval=="pumpprobe") flag=PUMP_PROBE_MODE; else if (sval=="storeinram") flag=STORE_IN_RAM; setReadOutFlags(flag); } switch (setReadOutFlags(GET_READOUT_FLAGS)) { case NORMAL_READOUT: return string("none"); case STORE_IN_RAM: return string("storeinram"); default: return string("unknown"); } } else if (var=="trimbits") { if (narg>=2) { cout << " writing trimfile " << endl; int nm=setNumberOfModules(GET_FLAG,X)*setNumberOfModules(GET_FLAG,Y); sls_detector_module *myMod=NULL; sval=string(args[1]); for (int im=0; immodule=im; setModule(*myMod); deleteModule(myMod); } } } cout << " rturning trimfile " << endl; return string(getTrimFile()); } else if (var.find("trim")==0) { if (action==GET_ACTION) { trimMode mode=NOISE_TRIMMING; int par1=0, par2=0; if (var.substr(5)=="noise") { // par1 is countlim; par2 is nsigma mode=NOISE_TRIMMING; par1=500; par2=4; } else if (var.substr(5)=="beam") { // par1 is countlim; par2 is nsigma mode=BEAM_TRIMMING; par1=1000; par2=4; } else if (var.substr(5)=="improve") { // par1 is maxit; if par2!=0 vthresh will be optimized mode=IMPROVE_TRIMMING; par1=5; par2=0; } else if (var.substr(5)=="fix") { // par1 is countlim; if par2<0 then trimwithlevel else trim with median mode=FIXEDSETTINGS_TRIMMING; par1=1000; par2=1; } else if (var.substr(5)=="offline") { mode=OFFLINE_TRIMMING; } else { return string("Unknown trim mode ")+var.substr(5); } executeTrimming(mode, par1, par2); sval=string(args[1]); sls_detector_module *myMod=NULL; int nm=setNumberOfModules(GET_FLAG,X)*setNumberOfModules(GET_FLAG,Y); for (int im=0; im> sargname; if (ssstr.good()) { strcpy(args[iargval],sargname.c_str()); iargval++; } } ans=executeLine(iargval,args,PUT_ACTION); #ifdef VERBOSE cout << ans << endl; #endif } iline++; } infile.close(); } else { cout << "Error opening configuration file " << fname << " for reading" << endl; return FAIL; } #ifdef VERBOSE cout << "Read configuration file of " << iline << " lines" << endl; #endif return iline; }; int mythenDetector::writeConfigurationFile(string const fname){ string names[]={\ "hostname",\ "caldir",\ "trimdir",\ "trimen",\ "outdir",\ "nmod",\ "badchannels",\ "angconv",\ "globaloff",\ "binsize",\ "threaded",\ "waitstates",\ "setlength",\ "clkdivider"}; int nvar=14; ofstream outfile; int iv=0; char *args[2]; for (int ia=0; ia<2; ia++) { args[ia]=new char[1000]; } int nargs; outfile.open(fname.c_str(),ios_base::out); if (outfile.is_open()) { for (iv=0; iv> sargname; if (ssstr.good()) { strcpy(args[iargval],sargname.c_str()); iargval++; } } if (level==2) { executeLine(iargval,args,PUT_ACTION); } else { if (string(args[0])==string("flatfield")) ; else if (string(args[0])==string("badchannels")) ; else if (string(args[0])==string("angconv")) ; else if (string(args[0])==string("trimbits")) ; else executeLine(iargval,args,PUT_ACTION); } } iline++; } infile.close(); } else { cout << "Error opening " << fname << " for reading" << endl; return FAIL; } #ifdef VERBOSE cout << "Read " << iline << " lines" << endl; #endif return iline; }; /* I/O */ sls_detector_module* mythenDetector::readTrimFile(string fname, sls_detector_module *myMod){ int nflag=0; if (myMod==NULL) { myMod=createModule(); nflag=1; } string myfname; string str; ifstream infile; ostringstream oss; int isgood=1; int iline=0; string names[]={"Vtrim", "Vthresh", "Rgsh1", "Rgsh2", "Rgpr", "Vcal", "outBuffEnable"}; string sargname; int ival; int ichan=0, ichip=0, idac=0; #ifdef VERBOSE cout << "reading trimfile for module number "<< myMod->module << endl; #endif //im=myMod->module; //myMod->module=im; // myMod->serialnumber=getId(MODULE_SERIAL_NUMBER, im); // if (im<0) myfname=fname; /* else { // oss << fname << ".sn" << setfill('0') <serialnumber oss << fname << ".sn" << setfill('0') <module; myfname=oss.str(); }*/ #ifdef VERBOSE cout << "trim file name is "<< myfname << endl; #endif infile.open(myfname.c_str(), ios_base::in); if (infile.is_open()) { // while (infile.good() && isgood==1) { for (int iarg=0; iargnDacs; iarg++) { getline(infile,str); iline++; #ifdef VERBOSE // cout << str << endl; #endif istringstream ssstr(str); ssstr >> sargname >> ival; #ifdef VERBOSE cout << sargname << " dac nr. " << idac << " is " << ival << endl; #endif myMod->dacs[idac]=ival; idac++; } for (ichip=0; ichipnChips; ichip++) { getline(infile,str); iline++; #ifdef VERBOSE // cout << str << endl; #endif istringstream ssstr(str); ssstr >> sargname >> ival; #ifdef VERBOSE // cout << "chip " << ichip << " " << sargname << " is " << ival << endl; #endif myMod->chipregs[ichip]=ival; for (ichan=0; ichannChans; ichan++) { getline(infile,str); #ifdef VERBOSE // cout << str << endl; #endif istringstream ssstr(str); #ifdef VERBOSE // cout << "channel " << ichan+ichip*thisDetector->nChans <<" iline " << iline<< endl; #endif iline++; myMod->chanregs[ichip*thisDetector->nChans+ichan]=0; for (int iarg=0; iarg<6 ; iarg++) { ssstr >> ival; //if (ssstr.good()) { switch (iarg) { case 0: #ifdef VERBOSE // cout << "trimbits " << ival ; #endif myMod->chanregs[ichip*thisDetector->nChans+ichan]|=ival&0x3f; break; case 1: #ifdef VERBOSE //cout << " compen " << ival ; #endif myMod->chanregs[ichip*thisDetector->nChans+ichan]|=ival<<9; break; case 2: #ifdef VERBOSE //cout << " anen " << ival ; #endif myMod->chanregs[ichip*thisDetector->nChans+ichan]|=ival<<8; break; case 3: #ifdef VERBOSE //cout << " calen " << ival ; #endif myMod->chanregs[ichip*thisDetector->nChans+ichan]|=ival<<7; break; case 4: #ifdef VERBOSE //cout << " outcomp " << ival ; #endif myMod->chanregs[ichip*thisDetector->nChans+ichan]|=ival<<10; break; case 5: #ifdef VERBOSE //cout << " counts " << ival << endl; #endif myMod->chanregs[ichip*thisDetector->nChans+ichan]|=ival<<11; break; default: cout << " too many columns" << endl; break; } } } // } } #ifdef VERBOSE cout << "read " << ichan*ichip << " channels" <trimFile,fname.c_str()); return myMod; } else { cout << "could not open file " << endl; if (nflag) deleteModule(myMod); return NULL; } }; int mythenDetector::writeTrimFile(string fname, sls_detector_module mod){ ofstream outfile; string names[]={"Vtrim", "Vthresh", "Rgsh1", "Rgsh2", "Rgpr", "Vcal", "outBuffEnable"}; int iv, ichan, ichip; int iv1, idac; int nb; outfile.open(fname.c_str(), ios_base::out); if (outfile.is_open()) { for (idac=0; idacnChans; ichan++) { iv=mod.chanregs[ichip*thisDetector->nChans+ichan]; iv1= (iv&0x3f); outfile <>nb); outfile << iv1 << " "; nb=8; iv1=((iv&(1<>nb); outfile << iv1 << " "; nb=7; iv1=((iv&(1<>nb); outfile <>nb); outfile << iv1 << " "; nb=11; iv1= ((iv&0xfffff800)>>nb); outfile << iv1 << endl; } } outfile.close(); return OK; } else { cout << "could not open file " << fname << endl; return FAIL; } }; int mythenDetector::writeTrimFile(string fname, int imod){ return writeTrimFile(fname,detectorModules[imod]); }; int mythenDetector::writeDataFile(string fname, float *data, float *err, float *ang, char dataformat, int nch){ if (nch==-1) nch=thisDetector->nChans*thisDetector->nChips*thisDetector->nMods; ofstream outfile; int idata; if (data==NULL) return FAIL; #ifdef VERBOSE cout << "writing data to file " << fname << endl; #endif // args|=0x10; // one line per channel! outfile.open (fname.c_str(),ios_base::out); if (outfile.is_open()) { #ifdef VERBOSE cout << "Writing to file " << fname << endl; #endif for (int ichan=0; ichannChans*thisDetector->nChips*thisDetector->nMods; ichan++) outfile << ichan << " " << *(data+ichan) << endl; outfile.close(); return OK; } else { cout << "Could not open file " << fname << "for writing"<< endl; return FAIL; } }; int mythenDetector::readDataFile(string fname, float *data, float *err, float *ang, char dataformat, int nch){ ifstream infile; int ichan, iline=0; int interrupt=0; float fdata, ferr, fang; int maxchans; int ich; string str; if (nch==0) maxchans=thisDetector->nChans*thisDetector->nChips*thisDetector->nMods; else if (nch<0) maxchans=0xfffffff; else maxchans=nch; #ifdef VERBOSE cout << "Opening file "<< fname << endl; #endif infile.open(fname.c_str(), ios_base::in); if (infile.is_open()) { while (infile.good() and interrupt==0) { getline(infile,str); #ifdef VERBOSE cout << str << endl; #endif istringstream ssstr(str); if (ang==NULL) { ssstr >> ichan >> fdata; ich=ichan; if (ich!=iline) cout << "Channel number " << ichan << " does not match with line number " << iline << endl; } else { ssstr >> fang >> fdata; ich=iline; } if (!ssstr.good()) { interrupt=1; break; } if (err) ssstr >> ferr; if (!ssstr.good()) { interrupt=1; break; } if (ich> ichan >> idata; if (!ssstr.good()) { interrupt=1; break; } if (ichan!=iline) { cout << " Expected channel "<< iline <<" but read channel "<< ichan << endl; interrupt=1; break; } else { if (ilinenChans*thisDetector->nChips*thisDetector->nMods) { data[iline]=idata; iline++; } else { interrupt=1; break; } } } } else { cout << "Could not read file " << fname << endl; return -1; } return iline; }; int mythenDetector::readCalibrationFile(string fname, float &gain, float &offset){ char line[500]; string str; ifstream infile; #ifdef VERBOSE cout << "Opening file "<< fname << endl; #endif infile.open(fname.c_str(), ios_base::in); if (infile.is_open()) { getline(infile,str); #ifdef VERBOSE cout << str << endl; #endif istringstream ssstr(str); ssstr >> offset >> gain; } else { cout << "Could not open calibration file "<< fname << endl; gain=0.; offset=0.; return -1; } return 0; }; int mythenDetector::writeCalibrationFile(string fname, float gain, float offset){ cout << "Function not yet implemented " << endl; }; /* Communication to server */ // calibration functions /* really needed? int mythenDetector::setCalibration(int imod, detectorSettings isettings, float gain, float offset){ cout << "function not yet implemented " << endl; return OK; } int mythenDetector::getCalibration(int imod, detectorSettings isettings, float &gain, float &offset){ cout << "function not yet implemented " << endl; } */ /* int mythenDetector::setROI(int nroi, int *xmin, int *xmax, int *ymin, int *ymax){ }; */ //Corrections int mythenDetector::setAngularConversion(string fname) { if (fname=="") { thisDetector->correctionMask&=~(1<< ANGULAR_CONVERSION); //strcpy(thisDetector->angConvFile,"none"); } else { if (fname=="default") fname=string(thisDetector->angConvFile); if (readAngularConversion(fname)>=0) { thisDetector->correctionMask|=(1<< ANGULAR_CONVERSION); strcpy(thisDetector->angConvFile,fname.c_str()); } } return thisDetector->correctionMask&(1<< ANGULAR_CONVERSION); } int mythenDetector::getAngularConversion(int &direction, angleConversionConstant *angconv) { direction=thisDetector->angDirection; if (angconv) { for (int imod=0; imodnMods; imod++) { (angconv+imod)->center=thisDetector->angOff[imod].center; (angconv+imod)->r_conversion=thisDetector->angOff[imod].r_conversion; (angconv+imod)->offset=thisDetector->angOff[imod].offset; (angconv+imod)->ecenter=thisDetector->angOff[imod].ecenter; (angconv+imod)->er_conversion=thisDetector->angOff[imod].er_conversion; (angconv+imod)->eoffset=thisDetector->angOff[imod].eoffset; } } if (thisDetector->correctionMask&(1<< ANGULAR_CONVERSION)) { return 1; } else { return 0; } } int mythenDetector::readAngularConversion(string fname) { char line[500]; string str; ifstream infile; int mod; float center, ecenter; float r_conv, er_conv; float off, eoff; string ss; int interrupt=0; //" module %i center %E +- %E conversion %E +- %E offset %f +- %f \n" #ifdef VERBOSE cout << "Opening file "<< fname << endl; #endif infile.open(fname.c_str(), ios_base::in); if (infile.is_open()) { while (infile.good() and interrupt==0) { getline(infile,str); #ifdef VERBOSE cout << str << 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 (modnModsMax && mod>=0) { thisDetector->angOff[mod].center=center; thisDetector->angOff[mod].r_conversion=r_conv; thisDetector->angOff[mod].offset=off; thisDetector->angOff[mod].ecenter=ecenter; thisDetector->angOff[mod].er_conversion=er_conv; thisDetector->angOff[mod].eoffset=eoff; } } } else { cout << "Could not open calibration file "<< fname << endl; return -1; } return 0; } int mythenDetector:: writeAngularConversion(string fname) { ofstream outfile; outfile.open (fname.c_str(),ios_base::out); if (outfile.is_open()) { for (int imod=0; imodnMods; imod++) { outfile << " module " << imod << " center "<< thisDetector->angOff[imod].center<<" +- "<< thisDetector->angOff[imod].ecenter<<" conversion "<< thisDetector->angOff[imod].r_conversion << " +- "<< thisDetector->angOff[imod].er_conversion << " offset "<< thisDetector->angOff[imod].offset << " +- "<< thisDetector->angOff[imod].eoffset << endl; } outfile.close(); } else { cout << "Could not open file " << fname << "for writing"<< endl; return -1; } //" module %i center %E +- %E conversion %E +- %E offset %f +- %f \n" return 0; } int mythenDetector::resetMerging(float *mp, float *mv, float *me, int *mm) { float binsize; if (thisDetector->binSize>0) binsize=thisDetector->binSize; else return FAIL; for (int ibin=0; ibin<(360./binsize); ibin++) { mp[ibin]=0; mv[ibin]=0; me[ibin]=0; mm[ibin]=0; } return OK; } int mythenDetector::finalizeMerging(float *mp, float *mv, float *me, int *mm) { float binsize; int np=0; if (thisDetector->binSize>0) binsize=thisDetector->binSize; else return FAIL; for (int ibin=0; ibin<(360./binsize); ibin++) { if (mm[ibin]>0) { 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 OK; } int mythenDetector::addToMerging(float *p1, float *v1, float *e1, float *mp, float *mv,float *me, int *mm) { float binsize; float binmi=-180., binma; int ibin=0; int imod; float ang; if (thisDetector->binSize>0) binsize=thisDetector->binSize; else return FAIL; binmi=-180.; binma=binmi+binsize; if (thisDetector->angDirection>0) { for (int ip=0; ipnChans*thisDetector->nChips*thisDetector->nMods; ip++) { if (thisDetector->correctionMask&DISCARD_BAD_CHANNELS) { if (badChannelMask[ip]) continue; } imod=ip/(thisDetector->nChans*thisDetector->nChips); if (p1) ang=p1[ip]; else ang=angle(ip,currentPosition,thisDetector->fineOffset+thisDetector->globalOffset,thisDetector->angOff[imod].r_conversion,thisDetector->angOff[imod].center, thisDetector->angOff[imod].offset,thisDetector->angOff[imod].tilt,thisDetector->angDirection); while (binmanChans*thisDetector->nChips*thisDetector->nMods-1; ip>=0; ip--) { if (thisDetector->correctionMask&(1<< DISCARD_BAD_CHANNELS)) { if (badChannelMask[ip]) continue; } while (binmanumberOfPositions>0) np=thisDetector->numberOfPositions; currentPositionIndex=0; for (int ip=0; ipnumberOfPositions>0) { go_to_position (thisDetector->detPositions[ip]); currentPositionIndex=ip+1; #ifdef VERBOSE cout << "moving to position" << endl; #endif } if (thisDetector->correctionMask&(1<< I0_NORMALIZATION)) currentI0=get_i0(); //write header before? if (thisDetector->threadedProcessing) startThread(); startAndReadAll(); //write header after? if (thisDetector->correctionMask&(1<< I0_NORMALIZATION)) currentI0=get_i0(); if (thisDetector->correctionMask&(1<< ANGULAR_CONVERSION)) currentPosition=get_position(); if (thisDetector->threadedProcessing==0) processData(); while (!dataQueue.empty()){ ; //#ifdef VERBOSE //cout << "waiting for the data to be postprocessed before moving" << endl; //#endif } #ifdef VERBOSE cout << "queue empty" << endl; #endif if (thisDetector->threadedProcessing) { while (pthread_cancel(dataProcessingThread)) { ; } #ifdef VERBOSE cout << "process canceled" << endl; #endif } if (thisDetector->stoppedFlag) { #ifdef VERBOSE cout << "exiting since the detector has been stopped" << endl; #endif break; } } } void* mythenDetector::processData(int delflag) { int *myData; float *fdata; float *rcdata=NULL, *rcerr=NULL; float *ffcdata=NULL, *ffcerr=NULL; float *ang=NULL; float bs=0.004; int i=0; int imod; int nb; int np; detectorData *thisData; int outer_c_s; int dum=1; #ifdef VERBOSE cout << " processing data - threaded mode " << thisDetector->threadedProcessing; #endif //pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED,&outer_c_s ); //#ifdef VERBOSE // cout << "impossible to cancel process " << endl; //#endif // pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &outer_c_s); while(dum | thisDetector->threadedProcessing) { // ???????????????????????? while( !dataQueue.empty() ) { /** Pop data queue */ myData=dataQueue.front(); // get the data from the queue //dataQueue.pop(); //remove the data from the queue if (myData) { //process data /** decode data */ fdata=decodeData(myData); //delete [] myData; // myData=NULL; /** write raw data file */ writeDataFile (createFileName().append(".raw"), fdata, NULL, NULL, 'i'); #ifdef VERBOSE #endif /** rate correction */ if (thisDetector->correctionMask&(1<nChans*thisDetector->nChips*thisDetector->nMods]; rcerr=new float[thisDetector->nChans*thisDetector->nChips*thisDetector->nMods]; rateCorrect(fdata,NULL,rcdata,rcerr); delete [] fdata; } else { rcdata=fdata; fdata=NULL; } /** flat field correction */ if (thisDetector->correctionMask&(1<nChans*thisDetector->nChips*thisDetector->nMods]; ffcerr=new float[thisDetector->nChans*thisDetector->nChips*thisDetector->nMods]; flatFieldCorrect(rcdata,rcerr,ffcdata,ffcerr); delete [] rcdata; delete [] rcerr; } else { ffcdata=rcdata; ffcerr=rcerr; rcdata=NULL; rcerr=NULL; } if (thisDetector->correctionMask&(1<< ANGULAR_CONVERSION)) { /** angular conversion */ /** data merging */ // if (thisDetector->numberOfPositions) { if (currentPositionIndex<=1) { if (thisDetector->binSize>0) bs=thisDetector->binSize; else if (thisDetector->angOff[0].r_conversion>0) { bs=180./PI*atan(thisDetector->angOff[0].r_conversion); thisDetector->binSize=bs; } else thisDetector->binSize=bs; nb=(360./bs); mergingBins=new float[nb]; mergingCounts=new float[nb]; mergingErrors=new float[nb]; mergingMultiplicity=new int[nb]; resetMerging(mergingBins, mergingCounts,mergingErrors, mergingMultiplicity); } addToMerging(ang, ffcdata, ffcerr, mergingBins, mergingCounts,mergingErrors, mergingMultiplicity); ang=new float[thisDetector->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,currentPosition,thisDetector->fineOffset+thisDetector->globalOffset,thisDetector->angOff[imod].r_conversion,thisDetector->angOff[imod].center, thisDetector->angOff[imod].offset,thisDetector->angOff[imod].tilt,thisDetector->angDirection); } writeDataFile (createFileName().append(".dat"), ffcdata, ffcerr,ang); if ((currentPositionIndex==thisDetector->numberOfPositions) || (currentPositionIndex==0)) { np=finalizeMerging(mergingBins, mergingCounts,mergingErrors, mergingMultiplicity); /** file writing */ currentPositionIndex++; writeDataFile (createFileName().append(".dat"),mergingCounts, mergingErrors, mergingBins,'f',np); if (delflag) { delete [] mergingBins; delete [] mergingCounts; delete [] mergingErrors; delete [] mergingMultiplicity; } else { thisData=new detectorData(mergingCounts,mergingErrors,mergingBins,thisDetector->fileIndex,(createFileName().append(".dat")).c_str(),np); finalDataQueue.push(thisData); } thisDetector->fileIndex++; } if (ffcdata) delete [] ffcdata; if (ffcerr) delete [] ffcerr; if (ang) delete [] ang; //} } else { writeDataFile (createFileName().append(".dat"), ffcdata, ffcerr); if (delflag) { if (ffcdata) delete [] ffcdata; if (ffcerr) delete [] ffcerr; if (ang) delete [] ang; } else { thisData=new detectorData(ffcdata,ffcerr,NULL,thisDetector->fileIndex,(createFileName().append(".dat")).c_str()); finalDataQueue.push(thisData); } thisDetector->fileIndex++; } dataQueue.pop(); //remove the data from the queue delete [] myData; myData=NULL; } #ifdef VERBOSE else cout << "could not pop data queue " << endl; #endif //#ifdef VERBOSE // cout << "possible to cancel process " << endl; //#endif // pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, &outer_c_s); // pthread_testcancel(); } dum=0; } // ???????????????????????? } void mythenDetector::startThread() { pthread_attr_t tattr, mattr; int ret; int newprio; sched_param param, mparam; void *arg; int policy= SCHED_OTHER; ret = pthread_attr_init(&tattr); // set the priority; others are unchanged //newprio = 30; mparam.sched_priority = 30; param.sched_priority = 1; // scheduling parameters of main thread ret = pthread_setschedparam(pthread_self(), policy, &mparam); printf("current priority is %d\n",param.sched_priority); ret = pthread_create(&dataProcessingThread, NULL,startProcessData, (void*)this); // scheduling parameters of target thread ret = pthread_setschedparam(dataProcessingThread, policy, ¶m); } void* startProcessData(void *n) { //void* processData(void *n) { void *w; mythenDetector *myDet=(mythenDetector*)n; myDet->processData(1); }