From 9a93a5f01969d49e59f9d9fa9567923862b68881 Mon Sep 17 00:00:00 2001 From: bergamaschi Date: Tue, 10 Jan 2012 14:14:52 +0000 Subject: [PATCH] all detectors merged into slsDetector git-svn-id: file:///afs/psi.ch/project/sls_det_software/svn/slsDetectorSoftware@83 951219d9-93cf-4727-9268-0efd64621fa3 --- slsDetectorSoftware/Makefile | 27 +- .../commonFiles/communication_funcs.c | 3 - .../multiSlsDetector/multiSlsDetector.cpp | 255 +++- .../multiSlsDetector/multiSlsDetector.h | 63 +- .../mythenDetectorServer/Makefile | 3 +- .../mythenDetectorServer/firmware_funcs.c | 6 +- .../mythenDetectorServer/mcb_funcs.c | 9 +- .../mythenDetectorServer/server_funcs.c | 15 +- .../mythenDetectorServer/trimming_funcs.c | 136 ++- .../mythenDetectorServer/trimming_funcs.h | 2 +- .../slsDetector/slsDetector.cpp | 1067 ++++++++++++++++- slsDetectorSoftware/slsDetector/slsDetector.h | 65 +- 12 files changed, 1509 insertions(+), 142 deletions(-) diff --git a/slsDetectorSoftware/Makefile b/slsDetectorSoftware/Makefile index 42f9df5ac..e36f2fb8d 100644 --- a/slsDetectorSoftware/Makefile +++ b/slsDetectorSoftware/Makefile @@ -1,13 +1,12 @@ CFLAGS= -DC_ONLY -FLAGS= -#-DVERBOSE -INCLUDES= -I commonFiles -I slsDetector -I MySocketTCP -I eigerDetector -ImythenDetector -IgotthardDetector -I usersFunctions -I multiSlsDetector - +FLAGS=-DVERBOSE +INCLUDES= -I commonFiles -I slsDetector -I MySocketTCP -I usersFunctions -I multiSlsDetector +#-I eigerDetector -ImythenDetector -IgotthardDetector #EPICSFLAGS=-D EPICS -I/usr/local/epics/base/include/ -I /usr/local/epics/base/include/os/Linux/ -L /usr/local/epics/base/lib/SL5-x86/ -Wl,-R/usr/local/epics/base/lib/SL5-x86 -lca -lCom -SRC_CLNT= slsDetector/slsDetector.cpp MySocketTCP/MySocketTCP.cxx usersFunctions/usersFunctions.c mythenDetector/mythenDetector.cpp eigerDetector/eigerDetector.cpp gotthardDetector/gotthardDetector.cpp multiSlsDetector/multiSlsDetector.cpp - +SRC_CLNT= slsDetector/slsDetector.cpp MySocketTCP/MySocketTCP.cxx usersFunctions/usersFunctions.c multiSlsDetector/multiSlsDetector.cpp +#mythenDetector/mythenDetector.cpp eigerDetector/eigerDetector.cpp gotthardDetector/gotthardDetector.cpp SRC_MYTHEN_SVC = mythenDetectorServer/server.c mythenDetectorServer/server_funcs.c mythenDetectorServer/communication_funcs.c mythenDetectorServer/firmware_funcs.c mythenDetectorServer/mcb_funcs.c mythenDetectorServer/trimming_funcs.c #mythenDetectorServer/sharedmemory.c @@ -30,18 +29,20 @@ picassoServer: $(SRC_MYTHEN_SVC) package: $(SRC_CLNT) -# $(CXX) -fPIC -g -o objs/multiSlsDetector.o -c -Wall multiSlsDetector/multiSlsDetector.cpp $(INCLUDES) $(FLAGS) + $(CXX) -fPIC -g -o objs/multiSlsDetector.o -c -Wall multiSlsDetector/multiSlsDetector.cpp $(INCLUDES) $(FLAGS) $(CXX) -fPIC -g -o objs/slsDetector.o -c -Wall slsDetector/slsDetector.cpp $(INCLUDES) $(FLAGS) # $(CXX) -fPIC -g -o objs/eigerDetector.o -c -Wall eigerDetector/eigerDetector.cpp $(INCLUDES) $(FLAGS) - $(CXX) -fPIC -g -o objs/mythenDetector.o -c -Wall mythenDetector/mythenDetector.cpp $(INCLUDES) $(FLAGS) - $(CXX) -fPIC -g -o objs/gotthardDetector.o -c -Wall gotthardDetector/gotthardDetector.cpp $(INCLUDES) $(FLAGS) +# $(CXX) -fPIC -g -o objs/mythenDetector.o -c -Wall mythenDetector/mythenDetector.cpp $(INCLUDES) $(FLAGS) +# $(CXX) -fPIC -g -o objs/gotthardDetector.o -c -Wall gotthardDetector/gotthardDetector.cpp $(INCLUDES) $(FLAGS) $(CXX) -fPIC -g -o objs/usersFunctions.o -c -Wall usersFunctions/usersFunctions.c $(INCLUDES) $(FLAGS) $(EPICSFLAGS) $(CXX) -fPIC -g -o objs/MySocketTCP.o -c -Wall MySocketTCP/MySocketTCP.cpp $(INCLUDES) $(FLAGS) $(CXX) -fPIC -g -o objs/multiSDetector.o -c -Wall multiSlsDetector/multiSlsDetector.cpp $(INCLUDES) $(FLAGS) - $(CXX) -shared -Wl,-soname,libSlsDetector.so.1 -o libSlsDetector.so.1.0.1 objs/slsDetector.o objs/mythenDetector.o objs/gotthardDetector.o objs/usersFunctions.o objs/MySocketTCP.o objs/multiSDetector.o -lc $(INCLUDES) $(FLAGS) $(EPICSFLAGS) - ln -sf libSlsDetector.so.1.0.1 libSlsDetector.so - ln -sf libSlsDetector.so.1 libSlsDetector.so - ar rcs libSlsDetector.a objs/slsDetector.o objs/mythenDetector.o objs/gotthardDetector.o objs/usersFunctions.o objs/MySocketTCP.o + $(CXX) -shared -Wl,-soname,libSlsDetector.so.1 -o libSlsDetector.so.1.0.1 objs/slsDetector.o objs/usersFunctions.o objs/MySocketTCP.o objs/multiSDetector.o -lc $(INCLUDES) $(FLAGS) $(EPICSFLAGS) +#objs/mythenDetector.o objs/gotthardDetector.o + ln -sf libSlsDetector.so.1.0.1 libSlsDetector.so + ln -sf libSlsDetector.so.1 libSlsDetector.so + ar rcs libSlsDetector.a objs/slsDetector.o objs/usersFunctions.o objs/MySocketTCP.o objs/multiSlsDetector.o +#objs/mythenDetector.o objs/gotthardDetector.o clean: rm -rf libSlsDetector.so.1.0.1 libSlsDetector.so core objs/* docs/* diff --git a/slsDetectorSoftware/commonFiles/communication_funcs.c b/slsDetectorSoftware/commonFiles/communication_funcs.c index 4f6bef997..5968f27fb 100755 --- a/slsDetectorSoftware/commonFiles/communication_funcs.c +++ b/slsDetectorSoftware/commonFiles/communication_funcs.c @@ -192,9 +192,6 @@ void closeConnection(int file_des) { #endif if(file_des>=0) close(file_des); - if (lockStatus==0) { - strcpy(lastClientIP,thisClientIP); - } file_des=-1; } diff --git a/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.cpp b/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.cpp index bdf999b1c..55bc7fe73 100644 --- a/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.cpp +++ b/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.cpp @@ -15,7 +15,9 @@ ID: $Id$ #include #include #include - +#include +#include +using namespace std; @@ -50,7 +52,7 @@ int multiSlsDetector::initSharedMemory(int id=0) { #ifdef VERBOSE - std::cout<<"multiSlsDetector: Size of shared memory is "<< sz << std::endl; + std::cout<<"multiSlsDetector: Size of shared memory is "<< sz << " - id " << mem_key << std::endl; #endif shm_id = shmget(mem_key,sz,IPC_CREAT | 0666); // allocate shared memory @@ -106,6 +108,7 @@ multiSlsDetector::multiSlsDetector(int id) : shmId(-1) for (int i=0; inumberOfDetectors; i++) { + cout << thisMultiDetector->detectorIds[i] << endl; detectors[i]=new slsDetector(thisMultiDetector->detectorIds[i]); } for (int i=thisMultiDetector->numberOfDetectors; inumberOfDetectors; +#ifdef VERBOSE + cout << "Adding detector " << id << " in position " << pos << endl; +#endif + if (pos<0) pos=j; if (pos>j) - return thisMultiDetector->numberOfDetectors; + pos=thisMultiDetector->numberOfDetectors; + - for (int ip=thisMultiDetector->numberOfDetectors-1; ip>pos; ip--) { - thisMultiDetector->detectorIds[ip+1]=thisMultiDetector->detectorIds[ip]; - detectors[ip+1]=detectors[ip]; + if (pos!=thisMultiDetector->numberOfDetectors) { + for (int ip=thisMultiDetector->numberOfDetectors-1; ip>=pos; ip--) { +#ifdef VERBOSE + cout << "Moving detector " << thisMultiDetector->detectorIds[ip] << " from position " << ip << " to " << ip+1 << endl; +#endif + thisMultiDetector->detectorIds[ip+1]=thisMultiDetector->detectorIds[ip]; + detectors[ip+1]=detectors[ip]; + } } +#ifdef VERBOSE + cout << "Creating new detector " << pos << endl; +#endif - detectorType t=slsDetector::getDetectorType(id); - detectors[pos]=new slsDetector(t,id); + // detectorType t=slsDetector::getDetectorType(id); + detectors[pos]=new slsDetector(id); + thisMultiDetector->detectorIds[pos]=detectors[pos]->getDetectorId(); thisMultiDetector->numberOfDetectors++; - thisMultiDetector->dataBytes+=detectors[pos]->getDataBytes(); - thisMultiDetector->numberOfChannels+=detectors[pos]->getNChans()*detectors[pos]->getNChips()*detectors[pos]->getNMods(); + + thisMultiDetector->dataBytes+=detectors[pos]->getDataBytes(); + + thisMultiDetector->numberOfChannels+=detectors[pos]->getNChans()*detectors[pos]->getNChips()*detectors[pos]->getNMods(); + +#ifdef VERBOSE + cout << "Detector added " << thisMultiDetector->numberOfDetectors<< endl; + + for (int ip=0; ipnumberOfDetectors; ip++) { + cout << "Detector " << thisMultiDetector->detectorIds[ip] << " position " << ip << " " << detectors[ip]->getHostname() << endl; + } +#endif + return thisMultiDetector->numberOfDetectors; } @@ -188,6 +216,10 @@ int multiSlsDetector::setDetectorOffset(int pos, int ox, int oy) { int multiSlsDetector::removeSlsDetector(int pos) { int j; +#ifdef VERBOSE + cout << "Removing detector in position " << pos << endl; +#endif + if (pos<0 ) pos=thisMultiDetector->numberOfDetectors-1; @@ -1128,7 +1160,11 @@ int multiSlsDetector::setFlatFieldCorrection(string fname){ #endif sprintf(ffffname,"%s/%s",thisMultiDetector->flatFieldDir,fname.c_str()); nch=readDataFile(string(ffffname),data); - if (nch>0) { + + if (nch>thisMultiDetector->numberOfChannels) + nch=thisMultiDetector->numberOfChannels; + + if (nch>0) { strcpy(thisMultiDetector->flatFieldFile,fname.c_str()); @@ -1292,7 +1328,7 @@ int multiSlsDetector::setRateCorrection(float t){ } } #ifdef VERBOSE - std::cout<< "Setting rate correction with dead time "<< thisDetector->tDead << std::endl; + std::cout<< "Setting rate correction with dead time "<< thisMultiDetector->tDead << std::endl; #endif } return thisMultiDetector->correctionMask&(1<correctionMask&(1<tDead << std::endl; + std::cout<< "Rate correction is enabled with dead time "<< thisMultiDetector->tDead << std::endl; #endif //which t should we return if they are all different? return 1; @@ -1319,7 +1355,7 @@ float multiSlsDetector::getRateCorrectionTau(){ if (thisMultiDetector->correctionMask&(1<tDead << std::endl; + std::cout<< "Rate correction is enabled with dead time "<< thisMultiDetector->tDead << std::endl; #endif //which t should we return if they are all different? return 1; @@ -1915,6 +1951,197 @@ float multiSlsDetector::getCurrentProgress() { +string multiSlsDetector::executeLine(int narg, char *args[], int action) { + + ostringstream os; + + string var=string(args[0]), sval; + int ival, ivar; + int id; + int myId=-1; + + os << "Executing " ; + for (int ia=0; ia> ivar; + if (vvstr.fail()) + ivar=-1; //append at the end + //return string("syntax is add:i where i is the detector position"); + } + if (action==slsDetector::PUT_ACTION) { + // if (ivar>thisMultiDetector->numberOfDetectors || ivar<0) { + // ivar=thisMultiDetector->numberOfDetectors; + // cout << "Appending detector " << endl; + //} + + if (sscanf(args[1],"%d",&ival)) { + // add by detector id +#ifdef VERBOSE + cout << "Add detector by id " << thisMultiDetector->numberOfDetectors << endl; +#endif + + for (id=0; idnumberOfDetectors; id++) { + //check that it is not already in the list, in that case move to new position + if (detectors[id]) { + if (detectors[id]->getDetectorId()==ival) { + cout << "Detector " << id << "exists!" << endl; + if (id==ivar) + break; + if (id==(thisMultiDetector->numberOfDetectors-1) && ivar==-1) + break; + removeSlsDetector(id); + addSlsDetector(ival, ivar); + break; + } + } + } + if (id==thisMultiDetector->numberOfDetectors) { + if (slsDetector::exists(ivar)==0) { + return string("Detector does not exist - You should first create it to determine type etc."); + } + addSlsDetector(ival, ivar); + } + // if it does not already exist create it + } else { + //add by hostname +#ifdef VERBOSE + cout << "Adding " << args[1] << " in position " << ivar << endl; +#endif + + for (id=0; idnumberOfDetectors; id++) { + //check that it is not already in the list, in that case move to new position + if (detectors[id]) { +#ifdef VERBOSE + cout << "Detector " << id << " is " << detectors[id]->getHostname() << endl; +#endif + if (detectors[id]->getHostname()==string(args[1])) { +#ifdef VERBOSE + cout << "Detector " << id << " exists!" << endl; +#endif + if (id==ivar) + break; + if (id==(thisMultiDetector->numberOfDetectors-1) && ivar==-1) + break; + myId=detectors[id]->getDetectorId(); + removeSlsDetector(id); + addSlsDetector(myId, ivar); + break; + } + } + } + if (id==thisMultiDetector->numberOfDetectors) { + detectorType t=slsDetector::getDetectorType(args[1], DEFAULT_PORTNO); + if (t==GENERIC) + return string("could not connect to detector to determine type"); +#ifdef VERBOSE + else + cout << "Detector type is " << t << endl; +#endif + myId=10; + slsDetector *s=NULL; + while (slsDetector::exists(myId)>0) { + cout << myId << endl; + s=new slsDetector(myId); + if (s->getHostname()==string(args[1])) + break; + delete s; + s=NULL; + myId++; + } + // if it does not already exist create it + if (s==NULL) { +#ifdef VERBOSE + cout << "Creating new detector with id " << myId << endl; +#endif + s=new slsDetector(t, myId); + s->setTCPSocket(args[1]); + delete s; + } + addSlsDetector(myId, ivar); + } + + } + + } + + + + + } else if (var.find("hostname")==0) { + + if (var.size()<=9) + return string("syntax is hostname:i where i is the detector id"); + istringstream vvstr(var.substr(9)); + vvstr >> ival; + if (vvstr.fail()) + return string("syntax is hostname:i where i is the detector id"); + if (action==slsDetector::PUT_ACTION) { + sval=string(args[1]); + os << "setting hostname of detector " << ival << " to " << sval << endl; + //cout << slsDetector::getDetectorType(args[1], DEFAULT_PORTNO) << endl; + + //controlla che non esista gia' + // se esiste modifica l'hostname, altrimenti aggiungilo in coda + + + + } + os << "getting hostname of detector " << ival << endl; + } + + + + + + + + + + return os.str(); +} + + + +string multiSlsDetector::helpLine(int action) { + ostringstream os; + + os << "This is the help line of action " << action << endl; + + return os.str(); +} + + + + diff --git a/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.h b/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.h index 63684d0d1..d8124ee6d 100644 --- a/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.h +++ b/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.h @@ -431,7 +431,6 @@ class multiSlsDetector { int writeDataFile(string fname, int *data); /** - reads a data file \param name of the file to be read \param data array of data values to be filled @@ -444,7 +443,7 @@ class multiSlsDetector { \sa mythenDetector::readDataFile */ - int readDataFile(string fname, float *data, float *err=NULL, float *ang=NULL, char dataformat='f', int nch=0); + int readDataFile(string fname, float *data, float *err=NULL, float *ang=NULL, char dataformat='f', int nch=0) {return slsDetector::readDataFile(nch, fname, data, err, ang, dataformat);} /** @@ -454,7 +453,7 @@ class multiSlsDetector { \returns OK or FAIL if it could not read the file or data=NULL \sa mythenDetector::readDataFile */ - int readDataFile(string fname, int *data); + int readDataFile(string fname, int *data){slsDetector::readDataFile(fname,data,thisMultiDetector->numberOfChannels);}; /** @@ -463,7 +462,7 @@ class multiSlsDetector { \param fname file to be read \sa angleConversionConstant mythenDetector::readAngularConversion */ - virtual int readAngularConversion(string fname="", int id=-1); + /////////////////////////////// virtual int readAngularConversion(string fname="", int id=-1); @@ -473,7 +472,7 @@ class multiSlsDetector { \param fname file to be written \sa angleConversionConstant mythenDetector::writeAngularConversion */ - virtual int writeAngularConversion(string fname="", int id=-1); + /////////////////////////////////////////virtual int writeAngularConversion(string fname="", int id=-1); @@ -807,7 +806,7 @@ s \returns 0 if angular conversion disabled, >0 otherwise \sa mythenDetector::setAngularConversion */ - virtual int setAngularConversion(string fname="")=0; + /////////////////////////////////////////////////// virtual int setAngularConversion(string fname=""); /** pure virtual function @@ -817,41 +816,41 @@ s \returns 0 if angular conversion disabled, >0 otherwise \sa mythenDetector::getAngularConversion */ - virtual int getAngularConversion(int &direction, angleConversionConstant *angconv=NULL)=0; + /////////////////////////////////////////////////// virtual int getAngularConversion(int &direction, angleConversionConstant *angconv=NULL); /** pure virtual function returns the angular conversion file \sa mythenDetector::getAngularConversion */ - virtual string getAngularConversion()=0; + /////////////////////////////////////////////////// virtual string getAngularConversion(); /** pure virtual function set detector global offset \sa mythenDetector::setGlobalOffset */ - virtual float setGlobalOffset(float f)=0; + /////////////////////////////////////////////////// virtual float setGlobalOffset(float f); /** pure virtual function set detector fine offset \sa mythenDetector::setFineOffset */ - virtual float setFineOffset(float f)=0; + /////////////////////////////////////////////////// virtual float setFineOffset(float f); /** pure virtual function get detector fine offset \sa mythenDetector::getFineOffset */ - virtual float getFineOffset()=0; + /////////////////////////////////////////////////// virtual float getFineOffset(); /** pure virtual function get detector global offset \sa mythenDetector::getGlobalOffset */ - virtual float getGlobalOffset()=0; + /////////////////////////////////////////////////// virtual float getGlobalOffset(); /** pure virtual function @@ -861,7 +860,7 @@ s \returns number of positions \sa mythenDetector::setPositions */ - virtual int setPositions(int nPos, float *pos)=0; + /////////////////////////////////////////////////// virtual int setPositions(int nPos, float *pos); /** pure virtual function get positions for the acquisition @@ -869,7 +868,7 @@ s \returns number of positions \sa mythenDetector::getPositions */ - virtual int getPositions(float *pos=NULL)=0; + /////////////////////////////////////////////////// virtual int getPositions(float *pos=NULL); /** pure virtual function @@ -878,13 +877,13 @@ s \returns current bin size \sa mythenDetector::setBinSize */ - virtual float setBinSize(float bs)=0; + /////////////////////////////////////////////////// virtual float setBinSize(float bs); /** pure virtual function return detector bin size used for merging (approx angular resolution) \sa mythenDetector::getBinSize */ - virtual float getBinSize()=0; + /////////////////////////////////////////////////// virtual float getBinSize(); @@ -1058,7 +1057,7 @@ s \returns OK or FAIL \sa mythenDetector::resetMerging */ - virtual int resetMerging(float *mp, float *mv,float *me, int *mm)=0; + /////////////////////////////////////////////////// virtual int resetMerging(float *mp, float *mv,float *me, int *mm); /** pure virtual function merge dataset @@ -1071,7 +1070,7 @@ s \param mm multiplicity of merged arrays \sa mythenDetector::addToMerging */ - virtual int addToMerging(float *p1, float *v1, float *e1, float *mp, float *mv,float *me, int *mm)=0; + /////////////////////////////////////////////////// virtual int addToMerging(float *p1, float *v1, float *e1, float *mp, float *mv,float *me, int *mm); /** pure virtual function calculates the "final" positions, data value and errors for the emrged data @@ -1089,15 +1088,15 @@ s */ int exitServer(); - /** pure virtual function + /** pure /////////////////////////////////////////////////// virtual function function for processing data /param delflag if 1 the data are processed, written to file and then deleted. If 0 they are added to the finalDataQueue \sa mythenDetector::processData */ - virtual void* processData(int delflag=1)=0; // thread function + /////////////////////////////////////////////////// virtual void* processData(int delflag=1); // thread function - virtual void acquire(int delflag=1)=0; + /////////////////////////////////////////////////// virtual void acquire(int delflag=1); /** calcualtes the total number of steps of the acquisition. called when number of frames, number of cycles, number of positions and scan steps change @@ -1108,6 +1107,28 @@ s float getCurrentProgress(); + + + + + + + + + + + string executeLine(int narg, char *args[], int action=slsDetector::GET_ACTION); + + + + static string helpLine(int action=slsDetector::GET_ACTION); + + + + + + + protected: diff --git a/slsDetectorSoftware/mythenDetectorServer/Makefile b/slsDetectorSoftware/mythenDetectorServer/Makefile index 41c0ff11f..d0aca04a8 100755 --- a/slsDetectorSoftware/mythenDetectorServer/Makefile +++ b/slsDetectorSoftware/mythenDetectorServer/Makefile @@ -13,7 +13,8 @@ INSTMODE= 0777 SRCS= server.c server_funcs.c communication_funcs.c firmware_funcs.c mcb_funcs.c trimming_funcs.c sharedmemory.c OBJS= $(SRCS:%.c=%.o) -CFLAGS+= -Wall -DC_ONLY -DMCB_FUNCS -DVERBOSE +CFLAGS+= -Wall -DC_ONLY -DMCB_FUNCS +#-DVERBOSE #-DVERYVERBOSE #-Werror diff --git a/slsDetectorSoftware/mythenDetectorServer/firmware_funcs.c b/slsDetectorSoftware/mythenDetectorServer/firmware_funcs.c index 874d216d9..a561fdeb6 100755 --- a/slsDetectorSoftware/mythenDetectorServer/firmware_funcs.c +++ b/slsDetectorSoftware/mythenDetectorServer/firmware_funcs.c @@ -434,11 +434,11 @@ int getNModBoard() { u_int32_t val; val=bus_r(FPGA_VERSION_REG)&0xff000000; - printf("version register %08x\n",val); + // printf("version register %08x\n",val); nmodboard=val >> 24; - //#ifdef VERY_VERBOSE + #ifdef VERY_VERBOSE printf("The board hosts %d modules\n",nmodboard); - //#endif + #endif nModBoard=nmodboard; //getNModBoard()=nmodboard; return nmodboard; diff --git a/slsDetectorSoftware/mythenDetectorServer/mcb_funcs.c b/slsDetectorSoftware/mythenDetectorServer/mcb_funcs.c index e30f8e5df..5c58f5e0e 100755 --- a/slsDetectorSoftware/mythenDetectorServer/mcb_funcs.c +++ b/slsDetectorSoftware/mythenDetectorServer/mcb_funcs.c @@ -20,6 +20,7 @@ #undef DEBUGOUT extern int nModX; +extern int nModBoard; extern int dataBytes; extern int dynamicRange; const int nChans=NCHAN; @@ -1024,11 +1025,13 @@ int getChannelbyNumber(sls_detector_channel* myChan) { int getTrimbit(int imod, int ichip, int ichan) { if (detectorChans) { - if (imod=0) + if (imod=0) if (ichip<(detectorModules+imod)->nchip && ichan<(detectorModules+imod)->nchan/(detectorModules+imod)->nchip) return (detectorChans[imod*NCHAN*NCHIP+ichip*NCHAN+ichan] & TRIM_DR); - } else - return -1; + } //else + return -1; + + } int initChannel(int ft,int cae, int ae, int coe, int ocoe, int counts, int imod){ diff --git a/slsDetectorSoftware/mythenDetectorServer/server_funcs.c b/slsDetectorSoftware/mythenDetectorServer/server_funcs.c index 5852b4584..37452438e 100755 --- a/slsDetectorSoftware/mythenDetectorServer/server_funcs.c +++ b/slsDetectorSoftware/mythenDetectorServer/server_funcs.c @@ -2484,11 +2484,16 @@ int execute_trimming(int file_des) { } } - if (ret!=OK) { - printf("trimming failed\n"); + + if (ret<0) { + sprintf(mess,"can't set execute trimming\n"); + ret=FAIL; + } else if (ret>0) { + sprintf(mess,"Could not trim %d channels\n", ret); + ret=FAIL; } else if (differentClients) ret=FORCE_UPDATE; - + n = sendDataOnly(file_des,&ret,sizeof(ret)); if (ret==FAIL) { n = sendDataOnly(file_des,mess,sizeof(mess)); @@ -2638,6 +2643,10 @@ int send_update(int file_des) { retval=setTrains(tns); n = sendDataOnly(file_des,&retval,sizeof(int64_t)); + if (lockStatus==0) { + strcpy(lastClientIP,thisClientIP); + } + return ret; diff --git a/slsDetectorSoftware/mythenDetectorServer/trimming_funcs.c b/slsDetectorSoftware/mythenDetectorServer/trimming_funcs.c index 97131d023..be87001ca 100755 --- a/slsDetectorSoftware/mythenDetectorServer/trimming_funcs.c +++ b/slsDetectorSoftware/mythenDetectorServer/trimming_funcs.c @@ -62,17 +62,20 @@ int trim_with_noise(int countlim, int nsigma, int im) #ifdef VERBOSE printf("trimming with noise.....\n"); #endif - retval2=trim_with_level(countlim, im); - + if (retval1==OK) + retval2=trim_with_level(countlim, im); + else + retval2=-1; + #ifdef DEBUGOUT printf("done\n"); #endif - if (retval1==OK && retval2==OK) - retval=OK; - else - retval=FAIL; + //if (retval1==OK && retval2==OK) + // retval=OK; + //else + // retval=FAIL; - return retval; + return retval2; } @@ -94,18 +97,20 @@ int trim_with_beam(int countlim, int nsigma, int im) //rpc #endif retval1=choose_vthresh_and_vtrim(countlim,nsigma,im); - retval2=trim_with_median(TRIM_DR, im); + if (retval1==OK) + retval2=trim_with_median(TRIM_DR, im); + else return -1; #ifdef DEBUGOUT printf("done\n"); #endif - if (retval1==OK && retval2==OK) - retval=OK; - else - retval=FAIL; + // if (retval1==OK && retval2==OK) + // retval=OK; + //else + // retval=FAIL; - return retval; + return retval2; } @@ -126,17 +131,19 @@ int trim_improve(int maxit, int par2, int im) //rpc if (par2!=0 && im==ALLMOD) retval1=choose_vthresh(); - - retval2=trim_with_median(2*maxit+1, im); + if (retval1==OK) + retval2=trim_with_median(2*maxit+1, im); + else + return -1; #ifdef DEBUGOUT printf("done\n"); #endif - if (retval1==OK && retval2==OK) - retval=OK; - else - retval=FAIL; + // if (retval1==OK && retval2==OK) + // retval=OK; + //else + //retval=FAIL; - return retval; + return retval2; } @@ -328,7 +335,7 @@ int trim_with_level(int countlim, int im) { u_int32_t *scan; int *inttrim; int modma, modmi, nm; - int retval=OK; + int retval=0; int *fifodata; sls_detector_channel myChan; printf("trimming module number %d", im); @@ -411,10 +418,13 @@ int trim_with_level(int countlim, int im) { for (ichan=0; ichanTRIM_DR) { trim=63; printf("can't trim channel %d chip %d module %d to trim %d\n",ich, ichip, imod, trim); - retval=FAIL; + retval++; } if (trim<0) { printf("can't trim channel %d chip %d module %d to trim %d\n",ich, ichip, imod, trim); trim=0; - retval=FAIL; + retval++; } initChannel(trim,0,0,1,0,0,imod); } diff --git a/slsDetectorSoftware/mythenDetectorServer/trimming_funcs.h b/slsDetectorSoftware/mythenDetectorServer/trimming_funcs.h index c41451008..ac73dcaa1 100755 --- a/slsDetectorSoftware/mythenDetectorServer/trimming_funcs.h +++ b/slsDetectorSoftware/mythenDetectorServer/trimming_funcs.h @@ -13,5 +13,5 @@ int trim_with_level(int countlim, int imod); int trim_with_median(int stop, int imod); int calcthr_from_vcal(int vcal); int calccal_from_vthr(int vthr); - +int median(int *a,int n); #endif diff --git a/slsDetectorSoftware/slsDetector/slsDetector.cpp b/slsDetectorSoftware/slsDetector/slsDetector.cpp index f15f5a41f..b091b6b53 100644 --- a/slsDetectorSoftware/slsDetector/slsDetector.cpp +++ b/slsDetectorSoftware/slsDetector/slsDetector.cpp @@ -58,7 +58,7 @@ int slsDetector::initSharedMemory(detectorType type, int id) { sz=sizeof(sharedSlsDetector)+nm*(2*nch*nc*sizeof(float)+sizeof(sls_detector_module)+sizeof(int)*nc+sizeof(float)*nd+sizeof(int)*nch*nc); #ifdef VERBOSE - std::cout<<"Size of shared memory is "<< sz << std::endl; + std::cout<<"Size of shared memory is "<< sz << "(type " << type << " - id " << mem_key << ")"<< std::endl; #endif shm_id = shmget(mem_key,sz,IPC_CREAT | 0666); // allocate shared memory @@ -118,13 +118,73 @@ int slsDetector::freeSharedMemory() { +slsDetector::slsDetector(int id) : + thisDetector(NULL), + detId(id), + shmId(-1), + controlSocket(NULL), + stopSocket(NULL), + dataSocket(NULL), + currentPosition(0), + currentPositionIndex(0), + currentI0(0), + mergingBins(NULL), + mergingCounts(NULL), + mergingErrors(NULL), + mergingMultiplicity(NULL), + ffcoefficients(NULL), + fferrors(NULL), + detectorModules(NULL), + dacs(NULL), + adcs(NULL), + chipregs(NULL), + chanregs(NULL), + badChannelMask(NULL) + + +{ + detectorType type=(detectorType)getDetectorType(id); + + + while (shmId<0) { + /**Initlializes shared memory \sa initSharedMemory + + if it fails the detector id is incremented until it succeeds + */ + shmId=initSharedMemory(type,id); + id++; + } + id--; +#ifdef VERBOSE + std::cout<< "Detector id is " << id << std::endl; +#endif + detId=id; + + + /**Initializes the detector stucture \sa initializeDetectorSize + */ + initializeDetectorSize(type); + + + + + pthread_mutex_t mp1 = PTHREAD_MUTEX_INITIALIZER; + + mp=mp1; + + pthread_mutex_init(&mp, NULL); + + + +}; + slsDetector::slsDetector(detectorType type, int id): thisDetector(NULL), - detId(0), + detId(id), shmId(-1), controlSocket(NULL), stopSocket(NULL), @@ -155,7 +215,7 @@ slsDetector::slsDetector(detectorType type, int id): } id--; #ifdef VERBOSE - std::cout<< "Detector id is " << id << std::endl; + std::cout<< "Detector id is " << id << " type is " << type << std::endl; #endif detId=id; @@ -175,11 +235,21 @@ slsDetector::slsDetector(detectorType type, int id): } -slsDetector::~slsDetector(){}; +slsDetector::~slsDetector(){ + + // Detach Memory address + if (shmdt(thisDetector) == -1) { + perror("shmdt failed\n"); + printf("Could not detach shared memory %d\n", shmId); + } else + printf("Shared memory %d detached\n", shmId); + + +}; slsDetector::slsDetector(char *name, int id, int cport) : thisDetector(NULL), - detId(0), + detId(id), shmId(-1), controlSocket(NULL), stopSocket(NULL), @@ -243,7 +313,7 @@ detectorType slsDetector::getDetectorType(char *name, int cport) { detectorType t=GENERIC; int fnum=F_GET_DETECTOR_TYPE; MySocketTCP *s= new MySocketTCP(name, cport); - char m[1000]; + char m[100]; if (s->Connect()>=0) { s->SendDataOnly(&fnum,sizeof(fnum)); @@ -287,6 +357,68 @@ detectorType slsDetector::getDetectorType(char *name, int cport) { +int slsDetector::exists(int id) { + + key_t mem_key=DEFAULT_SHM_KEY+id; + int shm_id; + int sz; + + sz=sizeof(sharedSlsDetector); + + +#ifdef VERBOSE + cout << "getDetectorType: generic shared memory of size " << sz << endl; +#endif + shm_id = shmget(mem_key,sz,IPC_CREAT | 0666); // allocate shared memory + + if (shm_id < 0) { + std::cout<<"*** shmget error (server) ***"<< shm_id << std::endl; + return -1; + } + + /** + thisDetector pointer is set to the memory address of the shared memory + */ + + sharedSlsDetector* det = (sharedSlsDetector*) shmat(shm_id, NULL, 0); /* attach */ + + if (det == (void*)-1) { + std::cout<<"*** shmat error (server) ***" << std::endl; + return -1; + } + /** + shm_id returns -1 is shared memory initialization fails + */ + //shmId=shm_id; + + + + + if (det->alreadyExisting==0) { + // Detach Memory address + if (shmdt(det) == -1) { + perror("shmdt failed\n"); + return 0; + } +#ifdef VERBOSE + printf("Shared memory %d detached\n", shm_id); +#endif + // remove shared memory + if (shmctl(shm_id, IPC_RMID, 0) == -1) { + perror("shmctl(IPC_RMID) failed\n"); + return 0; + } +#ifdef VERBOSE + printf("Shared memory %d deleted\n", shm_id); +#endif + return 0; + } + + return 1; + + + +} @@ -303,6 +435,10 @@ detectorType slsDetector::getDetectorType(int id) { sz=sizeof(sharedSlsDetector); + +#ifdef VERBOSE + cout << "getDetectorType: generic shared memory of size " << sz << endl; +#endif shm_id = shmget(mem_key,sz,IPC_CREAT | 0666); // allocate shared memory if (shm_id < 0) { @@ -334,14 +470,23 @@ detectorType slsDetector::getDetectorType(int id) { perror("shmdt failed\n"); return t; } - //printf("Shared memory %d detached\n", shmId); +#ifdef VERBOSE + printf("Shared memory %d detached\n", shm_id); +#endif // remove shared memory if (shmctl(shm_id, IPC_RMID, 0) == -1) { perror("shmctl(IPC_RMID) failed\n"); return t; } - //printf("Shared memory %d deleted\n", shmId); +#ifdef VERBOSE + printf("Shared memory %d deleted\n", shm_id); +#endif } + +#ifdef VERBOSE + cout << "Detector type is " << t << endl; +#endif + return t; @@ -516,8 +661,8 @@ int slsDetector::initializeDetectorSize(detectorType type) { fferrors=(float*)(goff+thisDetector->fferroff); detectorModules=(sls_detector_module*)(goff+ thisDetector->modoff); #ifdef VERBOSE - for (int imod=0; imod< thisDetector->nModsMax; imod++) - std::cout<< hex << detectorModules+imod << dec <nModsMax; imod++) + // std::cout<< hex << detectorModules+imod << dec <dacoff); adcs=(float*)(goff+thisDetector->adcoff); @@ -535,7 +680,9 @@ int slsDetector::initializeDetectorSize(detectorType type) { /** modifies the last PID accessing the detector */ thisDetector->lastPID=getpid(); - +#ifdef VERBOSE + cout << "Det size initialized " << endl; +#endif return OK; } @@ -3061,11 +3208,13 @@ int* slsDetector::getDataFromDetector(){ int i; #endif - //#ifdef VERBOSE +#ifdef VERBOSE std::cout<< "getting data "<< thisDetector->dataBytes << " " << nel<< std::endl; - //#endif +#endif controlSocket->ReceiveDataOnly(&ret,sizeof(ret)); +#ifdef VERBOSE cout << "ret=" << ret << endl; +#endif if (ret!=OK) { n= controlSocket->ReceiveDataOnly(mess,sizeof(mess)); if (ret==FAIL) { @@ -5043,8 +5192,8 @@ string slsDetector::executeLine(int narg, char *args[], int action) { if (action==PUT_ACTION) { setTCPSocket(args[1]); } - strcpy(answer, getHostname()); - return string(answer); + //strcpy(answer, getHostname()); + return getHostname();//string(answer); } else if (var=="flatfield") { if (action==PUT_ACTION) { sval=string(args[1]); @@ -6155,7 +6304,8 @@ string slsDetector::executeLine(int narg, char *args[], int action) { deleteModule(myMod); } } - } else if (action==PUT_ACTION) { + return string("done"); + } else if (action==PUT_ACTION) { return string("cannot set "); } } else if (var=="clkdivider") { @@ -7589,4 +7739,889 @@ int slsDetector::getPositions(float *pos){ +int slsDetector::readConfigurationFile(string const fname){ + + + + string ans; + string str; + ifstream infile; + int iargval; + int interrupt=0; + char *args[100]; + for (int ia=0; ia<100; ia++) { + args[ia]=new char[1000]; + } + + + string sargname, sargval; + int iline=0; + std::cout<< "config file name "<< fname << std::endl; + infile.open(fname.c_str(), ios_base::in); + if (infile.is_open()) { + while (infile.good() and interrupt==0) { + sargname="none"; + sargval="0"; + getline(infile,str); + iline++; +#ifdef VERBOSE + std::cout<< str << std::endl; +#endif + if (str.find('#')!=string::npos) { +#ifdef VERBOSE + std::cout<< "Line is a comment " << std::endl; + std::cout<< str << std::endl; +#endif + continue; + } else if (str.length()<2) { +#ifdef VERBOSE + std::cout<< "Empty line " << std::endl; +#endif + continue; + } else { + istringstream ssstr(str); + iargval=0; + while (ssstr.good()) { + ssstr >> sargname; + //if (ssstr.good()) { +#ifdef VERBOSE + std::cout<< iargval << " " << sargname << std::endl; +#endif + strcpy(args[iargval],sargname.c_str()); + iargval++; + //} + } + ans=executeLine(iargval,args,PUT_ACTION); +#ifdef VERBOSE + std::cout<< ans << std::endl; +#endif + } + iline++; + } + infile.close(); + } else { + std::cout<< "Error opening configuration file " << fname << " for reading" << std::endl; + return FAIL; + } +#ifdef VERBOSE + std::cout<< "Read configuration file of " << iline << " lines" << std::endl; +#endif + return iline; + +} + + +int slsDetector::writeConfigurationFile(string const fname){ + + + string names[]={ \ + "hostname", \ + "caldir", \ + "settingsdir", \ + "trimen", \ + "outdir", \ + "ffdir", \ + "headerbefore", \ + "headerafter", \ + "headerbeforepar", \ + "headerafterpar", \ + "nmod", \ + "badchannels", \ + "angconv", \ + "globaloff", \ + "binsize", \ + "threaded", \ + "waitstates", \ + "setlength", \ + "clkdivider"}; + + switch (thisDetector->myDetectorType) { + case MYTHEN: + names[2]="trimdir"; + break; + default: + ; + } + + + int nvar=19; + ofstream outfile; + int iv=0; + char *args[100]; + for (int ia=0; ia<100; ia++) { + args[ia]=new char[1000]; + } + + + 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()); +#ifdef VERBOSE + std::cout<< args[iargval] << std::endl; +#endif + 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 { + std::cout<< "Error opening " << fname << " for reading" << std::endl; + return FAIL; + } +#ifdef VERBOSE + std::cout<< "Read " << iline << " lines" << std::endl; +#endif + return iline; + +}; + + + + /* I/O */ + + + sls_detector_module* slsDetector::readSettingsFile(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 iline=0; + // string names[]={"Vtrim", "Vthresh", "Rgsh1", "Rgsh2", "Rgpr", "Vcal", "outBuffEnable"}; + string sargname; + int ival; + int ichan=0, ichip=0, idac=0; + + + +#ifdef VERBOSE + std::cout<< "reading settings file for module number "<< myMod->module << std::endl; +#endif + myfname=fname; +#ifdef VERBOSE + std::cout<< "trim file name is "<< myfname << std::endl; +#endif + infile.open(myfname.c_str(), ios_base::in); + if (infile.is_open()) { + + + switch (thisDetector->myDetectorType) { + + case MYTHEN: + + for (int iarg=0; iargnDacs; iarg++) { + getline(infile,str); + iline++; + istringstream ssstr(str); + ssstr >> sargname >> ival; +#ifdef VERBOSE + std::cout<< sargname << " dac nr. " << idac << " is " << ival << std::endl; +#endif + myMod->dacs[idac]=ival; + idac++; + } + for (ichip=0; ichipnChips; ichip++) { + getline(infile,str); + iline++; +#ifdef VERBOSE + // std::cout<< str << std::endl; +#endif + istringstream ssstr(str); + ssstr >> sargname >> ival; +#ifdef VERBOSE + // std::cout<< "chip " << ichip << " " << sargname << " is " << ival << std::endl; +#endif + + myMod->chipregs[ichip]=ival; + for (ichan=0; ichannChans; ichan++) { + getline(infile,str); +#ifdef VERBOSE + // std::cout<< str << std::endl; +#endif + istringstream ssstr(str); + +#ifdef VERBOSE + // std::cout<< "channel " << ichan+ichip*thisDetector->nChans <<" iline " << iline<< std::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 + // std::cout<< "trimbits " << ival ; +#endif + myMod->chanregs[ichip*thisDetector->nChans+ichan]|=ival&0x3f; + break; + case 1: +#ifdef VERBOSE + //std::cout<< " compen " << ival ; +#endif + myMod->chanregs[ichip*thisDetector->nChans+ichan]|=ival<<9; + break; + case 2: +#ifdef VERBOSE + //std::cout<< " anen " << ival ; +#endif + myMod->chanregs[ichip*thisDetector->nChans+ichan]|=ival<<8; + break; + case 3: +#ifdef VERBOSE + //std::cout<< " calen " << ival ; +#endif + myMod->chanregs[ichip*thisDetector->nChans+ichan]|=ival<<7; + break; + case 4: +#ifdef VERBOSE + //std::cout<< " outcomp " << ival ; +#endif + myMod->chanregs[ichip*thisDetector->nChans+ichan]|=ival<<10; + break; + case 5: +#ifdef VERBOSE + //std::cout<< " counts " << ival << std::endl; +#endif + myMod->chanregs[ichip*thisDetector->nChans+ichan]|=ival<<11; + break; + default: + std::cout<< " too many columns" << std::endl; + break; + } + } + } + // } + } +#ifdef VERBOSE + std::cout<< "read " << ichan*ichip << " channels" <nDacs; iarg++) { + getline(infile,str); + iline++; +#ifdef VERBOSE + std::cout<< str << std::endl; +#endif + istringstream ssstr(str); + ssstr >> sargname >> ival; +#ifdef VERBOSE + std::cout<< sargname << " dac nr. " << idac << " is " << ival << std::endl; +#endif + myMod->dacs[idac]=ival; + idac++; + } + break; + + + default: + std::cout<< "Unknown detector type - don't know how to read file" << myfname << std::endl; + infile.close(); + deleteModule(myMod); + return NULL; + + } + + infile.close(); + strcpy(thisDetector->settingsFile,fname.c_str()); + return myMod; + } else { + std::cout<< "could not open trim file " << myfname << std::endl; + + if (nflag) + deleteModule(myMod); + return NULL; + } + +}; + + +int slsDetector::writeSettingsFile(string fname, sls_detector_module mod){ + + ofstream outfile; + + string names[100]; + int id=0; + switch (thisDetector->myDetectorType) { + case MYTHEN: + names[id++]="Vtrim"; + names[id++]="Vthresh"; + names[id++]="Rgsh1"; + names[id++]="Rgsh2"; + names[id++]="Rgpr"; + names[id++]="Vcal"; + names[id++]="outBuffEnable"; + break; + case GOTTHARD: + names[id++]="Vref"; + names[id++]="VcascN"; + names[id++]="VcascP"; + names[id++]="Vout"; + names[id++]="Vcasc"; + names[id++]="Vin"; + names[id++]="Vref_comp"; + names[id++]="Vib_test"; + names[id++]="config"; + names[id++]="HV"; + names[id++]="macaddress"; + names[id++]="ipaddress"; + break; + default: + cout << "Unknown detector type - unknown format for settings file" << endl; + return FAIL; + } + + 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 << std::endl; + } + } + outfile.close(); + return OK; + } else { + std::cout<< "could not open SETTINGS file " << fname << std::endl; + return FAIL; + } + +}; + + + + +int slsDetector::writeSettingsFile(string fname, int imod){ + + return writeSettingsFile(fname,detectorModules[imod]); + +}; + + +int slsDetector::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; + + // args|=0x10; // one line per channel! + + outfile.open (fname.c_str(),ios_base::out); + if (outfile.is_open()) + { +#ifdef VERBOSE + std::cout<< "writeDataFile Writing to file " << fname << std::endl; +#endif + for (int ichan=0; ichannChans*thisDetector->nChips*thisDetector->nMods; ichan++) + outfile << ichan << " " << *(data+ichan) << std::endl; + outfile.close(); + return OK; + } else { + std::cout<< "Could not open file " << fname << "for writing"<< std::endl; + return FAIL; + } +}; + + + +int slsDetector::readDataFile(string fname, float *data, float *err, float *ang, char dataformat) { + return readDataFile(thisDetector->nChans*thisDetector->nChips*thisDetector->nMods, fname, data, err, ang, dataformat); + +} + + int slsDetector::readDataFile(int nch, string fname, float *data, float *err, float *ang, char dataformat){ + + + ifstream infile; + int ichan, iline=0; + int interrupt=0; + float fdata, ferr, fang; + int maxchans; + int ich; + string str; + + + maxchans=nch; + +#ifdef VERBOSE + std::cout<< "Opening file "<< fname << std::endl; +#endif + infile.open(fname.c_str(), ios_base::in); + if (infile.is_open()) { + while (infile.good() and interrupt==0) { + getline(infile,str); +#ifdef VERBOSE + std::cout<< str << std::endl; +#endif + istringstream ssstr(str); + if (ang==NULL) { + ssstr >> ichan >> fdata; + ich=ichan; + if (ssstr.fail() || ssstr.bad()) { + interrupt=1; + break; + } + if (ich!=iline) + std::cout<< "Channel number " << ichan << " does not match with line number " << iline << " " << dataformat << std::endl; + } 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 (ichnChans*thisDetector->nChips*thisDetector->nMods); +}; + +int slsDetector::readDataFile(string fname, int *data, int nch){ + + ifstream infile; + int ichan, idata, iline=0; + int interrupt=0; + string str; + +#ifdef VERBOSE + std::cout<< "Opening file "<< fname << std::endl; +#endif + infile.open(fname.c_str(), ios_base::in); + if (infile.is_open()) { + while (infile.good() and interrupt==0) { + getline(infile,str); +#ifdef VERBOSE + std::cout<< str << std::endl; +#endif + istringstream ssstr(str); + ssstr >> ichan >> idata; + if (ssstr.fail() || ssstr.bad()) { + interrupt=1; + break; + } + if (ichan!=iline) { + std::cout<< " Expected channel "<< iline <<" but read channel "<< ichan << std::endl; + interrupt=1; + break; + } else { + if (iline> offset >> gain; + infile.close(); + } else { + std::cout<< "Could not open calibration file "<< fname << std::endl; + gain=0.; + offset=0.; + return -1; + } + return 0; +}; + +int slsDetector::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; +}; + + + + + + + diff --git a/slsDetectorSoftware/slsDetector/slsDetector.h b/slsDetectorSoftware/slsDetector/slsDetector.h index 4bec9e84a..5fcf6dadf 100644 --- a/slsDetectorSoftware/slsDetector/slsDetector.h +++ b/slsDetectorSoftware/slsDetector/slsDetector.h @@ -334,7 +334,7 @@ typedef struct sharedSlsDetector { */ - slsDetector(int id){slsDetector(getDetectorType(id),id);}; + slsDetector(int id); slsDetector(char *name, int id=0, int cport=DEFAULT_PORTNO); @@ -351,6 +351,14 @@ typedef struct sharedSlsDetector { \returns 1 if the detector structure has already be initlialized, 0 otherwise */ int exists() {return thisDetector->alreadyExisting;}; + /** + checks if detector id exists + \param i detector id + \returns 1 if the detector already exists, 0 otherwise + + + */ + static int exists(int i); /** Purely virtual function @@ -358,13 +366,13 @@ typedef struct sharedSlsDetector { /sa mythenDetector::readConfigurationFile */ - virtual int readConfigurationFile(string const fname){}; + virtual int readConfigurationFile(string const fname); /** Purely virtual function Should be implemented in the specific detector class /sa mythenDetector::writeConfigurationFile */ - virtual int writeConfigurationFile(string const fname){}; + virtual int writeConfigurationFile(string const fname); /* @@ -377,13 +385,13 @@ typedef struct sharedSlsDetector { Should be implemented in the specific detector class /sa mythenDetector::dumpDetectorSetup */ - virtual int dumpDetectorSetup(string const fname, int level=0){}; + virtual int dumpDetectorSetup(string const fname, int level=0); /** Purely virtual function Should be implemented in the specific detector class /sa mythenDetector::retrieveDetectorSetup */ - virtual int retrieveDetectorSetup(string const fname, int level=0){}; + virtual int retrieveDetectorSetup(string const fname, int level=0); /** configure the socket communication and initializes the socket instances @@ -418,7 +426,7 @@ typedef struct sharedSlsDetector { string getLastClientIP(); /** returns the detector hostname \sa sharedSlsDetector */ - char* getHostname() {return thisDetector->hostname;}; + string getHostname() {return string(thisDetector->hostname);}; /** returns the detector control port \sa sharedSlsDetector */ int getControlPort() {return thisDetector->controlPort;}; /** returns the detector stop port \sa sharedSlsDetector */ @@ -486,7 +494,7 @@ typedef struct sharedSlsDetector { \sa mythenDetector::readSettingsFile */ - virtual sls_detector_module* readSettingsFile(string fname, sls_detector_module* myMod=NULL){}; + virtual sls_detector_module* readSettingsFile(string fname, sls_detector_module* myMod=NULL); /** Pure virtual function @@ -497,7 +505,7 @@ typedef struct sharedSlsDetector { \sa ::sls_detector_module mythenDetector::writeSettingsFile(string, sls_detector_module) */ - virtual int writeSettingsFile(string fname, sls_detector_module mod){}; + virtual int writeSettingsFile(string fname, sls_detector_module mod); /** Pure virtual function @@ -507,7 +515,7 @@ typedef struct sharedSlsDetector { \returns OK or FAIL if the file could not be written \sa ::sls_detector_module sharedSlsDetector mythenDetector::writeSettingsFile(string, int) */ - virtual int writeSettingsFile(string fname, int imod){}; + virtual int writeSettingsFile(string fname, int imod); /** @@ -624,7 +632,7 @@ typedef struct sharedSlsDetector { \sa mythenDetector::writeDataFile */ - virtual int writeDataFile(string fname, float *data, float *err=NULL, float *ang=NULL, char dataformat='f', int nch=-1){}; + virtual int writeDataFile(string fname, float *data, float *err=NULL, float *ang=NULL, char dataformat='f', int nch=-1); /** @@ -634,7 +642,7 @@ typedef struct sharedSlsDetector { \returns OK or FAIL if it could not write the file or data=NULL \sa mythenDetector::writeDataFile */ - virtual int writeDataFile(string fname, int *data){}; + virtual int writeDataFile(string fname, int *data); /** @@ -650,7 +658,7 @@ typedef struct sharedSlsDetector { \sa mythenDetector::readDataFile */ - virtual int readDataFile(string fname, float *data, float *err=NULL, float *ang=NULL, char dataformat='f', int nch=0){}; + int readDataFile(string fname, float *data, float *err=NULL, float *ang=NULL, char dataformat='f'); /** @@ -660,7 +668,32 @@ typedef struct sharedSlsDetector { \returns OK or FAIL if it could not read the file or data=NULL \sa mythenDetector::readDataFile */ - virtual int readDataFile(string fname, int *data){}; + int readDataFile(string fname, int *data); + /** + + reads a data file + \param name of the file to be read + \param data array of data values to be filled + \param err array of arrors on the data. If NULL no errors are expected on the file + + \param ang array of angular values. If NULL data are expected in the form chan-val(-err) otherwise ang-val(-err) + \param dataformat format of the data: can be 'i' integer or 'f' float (default) + \param nch number of channels to be written to file. if <=0 defaults to the number of installed channels of the detector + \returns number of channels read or -1 if it could not read the file or data=NULL + + \sa mythenDetector::readDataFile + */ + static int readDataFile(int nch, string fname, float *data, float *err=NULL, float *ang=NULL, char dataformat='f'); + + /** + + reads a data file + \param name of the file to be read + \param data array of data values + \returns OK or FAIL if it could not read the file or data=NULL + \sa mythenDetector::readDataFile + */ + static int readDataFile(string fname, int *data, int nch); /** returns the location of the calibration files @@ -682,7 +715,7 @@ typedef struct sharedSlsDetector { \offset reference to the offset variable \sa sharedSlsDetector mythenDetector::readCalibrationFile */ - virtual int readCalibrationFile(string fname, float &gain, float &offset){}; + virtual int readCalibrationFile(string fname, float &gain, float &offset); /** writes a calibration file @@ -691,7 +724,7 @@ typedef struct sharedSlsDetector { \param offset \sa sharedSlsDetector mythenDetector::writeCalibrationFile */ - virtual int writeCalibrationFile(string fname, float gain, float offset){}; + virtual int writeCalibrationFile(string fname, float gain, float offset); /** @@ -1712,7 +1745,7 @@ enum {GET_ACTION, PUT_ACTION, READOUT_ACTION}; \return pointer to the data (or NULL if failed) */ - int getDetectorId() { return detId;}; + int getDetectorId() {return detId;}; /** Receives a data frame from the detector socket