diff --git a/slsDetectorSoftware/Makefile b/slsDetectorSoftware/Makefile index 8a9c67fcf..42f9df5ac 100644 --- a/slsDetectorSoftware/Makefile +++ b/slsDetectorSoftware/Makefile @@ -1,12 +1,12 @@ CFLAGS= -DC_ONLY FLAGS= #-DVERBOSE -INCLUDES= -I commonFiles -I slsDetector -I MySocketTCP -I eigerDetector -ImythenDetector -IgotthardDetector -I usersFunctions +INCLUDES= -I commonFiles -I slsDetector -I MySocketTCP -I eigerDetector -ImythenDetector -IgotthardDetector -I usersFunctions -I multiSlsDetector #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 +SRC_CLNT= slsDetector/slsDetector.cpp MySocketTCP/MySocketTCP.cxx usersFunctions/usersFunctions.c mythenDetector/mythenDetector.cpp eigerDetector/eigerDetector.cpp gotthardDetector/gotthardDetector.cpp multiSlsDetector/multiSlsDetector.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 @@ -19,7 +19,7 @@ doc: $(SRC_H) $(SRC_CLNT) mythenServer: $(SRC_MYTHEN_SVC) - $(CC) $(SRC_MYTHEN_SVC) $(CFLAGS) $(FLAGS) $(INCLUDES) -ImythenDetectorServer -DVIRTUAL -lm -D MCB_FUNCS -DC_ONLY + $(CC) $(SRC_MYTHEN_SVC) $(CFLAGS) $(FLAGS) $(INCLUDES) -ImythenDetectorServer -DVIRTUAL -lm -D MCB_FUNCS -DC_ONLY -DVERBOSE mv a.out mythenServer @@ -37,7 +37,8 @@ package: $(SRC_CLNT) $(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) -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 -lc $(INCLUDES) $(FLAGS) $(EPICSFLAGS) + $(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 diff --git a/slsDetectorSoftware/commonFiles/communication_funcs.c b/slsDetectorSoftware/commonFiles/communication_funcs.c index 681095938..2b20fa055 100755 --- a/slsDetectorSoftware/commonFiles/communication_funcs.c +++ b/slsDetectorSoftware/commonFiles/communication_funcs.c @@ -10,11 +10,13 @@ + //int socketDescriptor, file_des; -int socketDescriptor, file_des; const int send_rec_max_size=SEND_REC_MAX_SIZE; extern int errno; + + //struct sockaddr_in address; //#define VERBOSE @@ -23,7 +25,8 @@ int bindSocket(unsigned short int port_number) { int i; struct sockaddr_in addressS; - + int socketDescriptor; + int file_des; file_des= -1; socketDescriptor = socket(AF_INET, SOCK_STREAM,0); //tcp @@ -68,7 +71,7 @@ int bindSocket(unsigned short int port_number) { - + printf(" %s %s\n",lastClientIP, thisClientIP); return socketDescriptor; @@ -76,54 +79,20 @@ int bindSocket(unsigned short int port_number) { } -/* -only client funcs -*/ -/* -#ifndef C_ONLY - -MySocketTCP::MySocketTCP(const char* const host_ip_or_name, unsigned short int const port_number): - last_keep_connection_open_action_was_a_send(0), file_des(-1), send_rec_max_size(SEND_REC_MAX_SIZE), is_a_server(0), portno(DEFAULT_PORTNO), socketDescriptor(-1) -{ // sender (client): where to? ip - //is_a_server = 0; - // SetupParameters(); - strcpy(hostname,host_ip_or_name); - portno=port_number; - struct hostent *hostInfo = gethostbyname(host_ip_or_name); - if (hostInfo == NULL){ - cerr << "Exiting: Problem interpreting host: " << host_ip_or_name << "\n"; - } else { - // Set some fields in the serverAddress structure. - serverAddress.sin_family = hostInfo->h_addrtype; - memcpy((char *) &serverAddress.sin_addr.s_addr, - hostInfo->h_addr_list[0], hostInfo->h_length); - serverAddress.sin_port = htons(port_number); - socketDescriptor=0; //You can use send and recv, //would it work????? - } -} - - -int MySocketTCP::getHostname(char *name) { - if (is_a_server==0) { - strcpy(name,hostname); - } - return is_a_server; -}; -#endif -*/ - -int getServerError() +int getServerError(int socketDescriptor) { if (socketDescriptor<0) return 1; else return 0; }; -int acceptConnection() { +int acceptConnection(int socketDescriptor) { struct sockaddr_in addressC; + int file_des; + //socklen_t address_length; size_t address_length=sizeof(struct sockaddr_in); @@ -193,9 +162,24 @@ int acceptConnection() { socketDescriptor=-1; } + inet_ntop(AF_INET, &(addressC.sin_addr), thisClientIP, INET_ADDRSTRLEN); + #ifdef VERBOSE - printf("client connected %d\n", file_des); + printf("client connected %d\n", file_des); + + printf("addressC %s\n", thisClientIP); + printf("addressC %s\n", lastClientIP); #endif + + + + // struct sockaddr_in + //{ + //short int sin_family; /* Famyly of the address*/ + //unsigned short int sin_port; /* Port */ + //struct in_addr sin_addr; /* Network address */ + //unsigned char sin_zero[8]; /* Same size of struct sockaddr */ + //}; } @@ -208,7 +192,7 @@ int acceptConnection() { -void closeConnection() { +void closeConnection(int file_des) { //fflush(stdout); //printf("Closing file_des %d\n", file_des); //sleep(1); @@ -216,10 +200,13 @@ void closeConnection() { #endif if(file_des>=0) close(file_des); + if (lockStatus==0) { + strcpy(lastClientIP,thisClientIP); + } file_des=-1; } -void exitServer() { +void exitServer(int socketDescriptor) { if (socketDescriptor>=0) close(socketDescriptor); #ifdef VERY_VERBOSE @@ -252,7 +239,7 @@ void MySocketTCP::Disconnect(){ */ -int sendDataOnly(void* buf,int length) { + int sendDataOnly(int file_des, void* buf,int length) { /* int total_sent=0; int nsending; @@ -279,7 +266,7 @@ int sendDataOnly(void* buf,int length) { } - int receiveDataOnly(void* buf,int length) { + int receiveDataOnly(int file_des, void* buf,int length) { int total_received=0; int nreceiving; @@ -327,44 +314,44 @@ int sendDataOnly(void* buf,int length) { -int sendChannel(sls_detector_channel *myChan) { - return sendDataOnly(myChan, sizeof(sls_detector_channel)); +int sendChannel(int file_des, sls_detector_channel *myChan) { + return sendDataOnly(file_des,myChan, sizeof(sls_detector_channel)); } -int sendChip(sls_detector_chip *myChip) { +int sendChip(int file_des, sls_detector_chip *myChip) { int ts=0; int nChans=myChip->nchan; - ts+=sendDataOnly(myChip,sizeof(sls_detector_chip)); - ts+=sendDataOnly(myChip->chanregs,nChans*sizeof(int)); + ts+=sendDataOnly(file_des,myChip,sizeof(sls_detector_chip)); + ts+=sendDataOnly(file_des,myChip->chanregs,nChans*sizeof(int)); return ts; } -int sendModule(sls_detector_module *myMod) { +int sendModule(int file_des, sls_detector_module *myMod) { int ts=0; int idac; int nChips=myMod->nchip; int nChans=myMod->nchan; int nAdcs=myMod->nadc; int nDacs=myMod->ndac; - ts+= sendDataOnly(myMod,sizeof(sls_detector_module)); + ts+= sendDataOnly(file_des,myMod,sizeof(sls_detector_module)); #ifdef VERBOSE printf("module %d of size %d sent\n",myMod->module, ts); #endif - ts+= sendDataOnly(myMod->dacs,sizeof(float)*nDacs); + ts+= sendDataOnly(file_des,myMod->dacs,sizeof(float)*nDacs); #ifdef VERBOSE printf("dacs %d of size %d sent\n",myMod->module, ts); for (idac=0; idac< nDacs; idac++) printf("dac %d is %d\n",idac,myMod->dacs[idac]); #endif - ts+= sendDataOnly(myMod->adcs,sizeof(float)*nAdcs); + ts+= sendDataOnly(file_des,myMod->adcs,sizeof(float)*nAdcs); #ifdef VERBOSE printf("adcs %d of size %d sent\n",myMod->module, ts); #endif - ts+=sendDataOnly(myMod->chipregs,sizeof(int)*nChips); + ts+=sendDataOnly(file_des,myMod->chipregs,sizeof(int)*nChips); #ifdef VERBOSE printf("chips %d of size %d sent\n",myMod->module, ts); #endif - ts+=sendDataOnly(myMod->chanregs,sizeof(int)*nChans); + ts+=sendDataOnly(file_des,myMod->chanregs,sizeof(int)*nChans); #ifdef VERBOSE printf("chans %d of size %d sent - %d\n",myMod->module, ts, myMod->nchan); #endif @@ -374,17 +361,17 @@ int sendModule(sls_detector_module *myMod) { return ts; } -int receiveChannel(sls_detector_channel *myChan) { - return receiveDataOnly(myChan,sizeof(sls_detector_channel)); +int receiveChannel(int file_des, sls_detector_channel *myChan) { + return receiveDataOnly(file_des,myChan,sizeof(sls_detector_channel)); } -int receiveChip(sls_detector_chip* myChip) { +int receiveChip(int file_des, sls_detector_chip* myChip) { int *ptr=myChip->chanregs; int ts=0; int nChans, nchanold=myChip->nchan, chdiff; - ts+= receiveDataOnly(myChip,sizeof(sls_detector_chip)); + ts+= receiveDataOnly(file_des,myChip,sizeof(sls_detector_chip)); myChip->chanregs=ptr; @@ -401,12 +388,12 @@ int receiveChip(sls_detector_chip* myChip) { #endif if (chdiff<=0) - ts+=receiveDataOnly(myChip->chanregs, sizeof(int)*nChans); + ts+=receiveDataOnly(file_des,myChip->chanregs, sizeof(int)*nChans); else { ptr=malloc(chdiff*sizeof(int)); myChip->nchan=nchanold; - ts+=receiveDataOnly(myChip->chanregs, sizeof(int)*nchanold); - ts+=receiveDataOnly(ptr, sizeof(int)*chdiff); + ts+=receiveDataOnly(file_des,myChip->chanregs, sizeof(int)*nchanold); + ts+=receiveDataOnly(file_des,ptr, sizeof(int)*chdiff); free(ptr); return FAIL; } @@ -417,7 +404,7 @@ int receiveChip(sls_detector_chip* myChip) { return ts; } -int receiveModule(sls_detector_module* myMod) { +int receiveModule(int file_des, sls_detector_module* myMod) { float *dacptr=myMod->dacs; @@ -430,7 +417,7 @@ int receiveModule(sls_detector_module* myMod) { int nAdcs, naold=myMod->nadc, nadcdiff; - ts+= receiveDataOnly(myMod,sizeof(sls_detector_module)); + ts+= receiveDataOnly(file_des,myMod,sizeof(sls_detector_module)); myMod->dacs=dacptr; myMod->adcs=adcptr; @@ -478,57 +465,57 @@ int receiveModule(sls_detector_module* myMod) { printf("received %d adcs\n",nAdcs); #endif if (ndacdiff<=0) { - ts+=receiveDataOnly(myMod->dacs, sizeof(float)*nDacs); + ts+=receiveDataOnly(file_des,myMod->dacs, sizeof(float)*nDacs); #ifdef VERBOSE printf("dacs received\n"); #endif } else { dacptr=malloc(ndacdiff*sizeof(float)); myMod->ndac=ndold; - ts+=receiveDataOnly(myMod->dacs, sizeof(float)*ndold); - ts+=receiveDataOnly(dacptr, sizeof(float)*ndacdiff); + ts+=receiveDataOnly(file_des,myMod->dacs, sizeof(float)*ndold); + ts+=receiveDataOnly(file_des,dacptr, sizeof(float)*ndacdiff); free(dacptr); return FAIL; } if (nadcdiff<=0) { - ts+=receiveDataOnly(myMod->adcs, sizeof(float)*nAdcs); + ts+=receiveDataOnly(file_des,myMod->adcs, sizeof(float)*nAdcs); #ifdef VERBOSE printf("adcs received\n"); #endif } else { adcptr=malloc(nadcdiff*sizeof(float)); myMod->nadc=naold; - ts+=receiveDataOnly(myMod->adcs, sizeof(float)*naold); - ts+=receiveDataOnly(adcptr, sizeof(float)*nadcdiff); + ts+=receiveDataOnly(file_des,myMod->adcs, sizeof(float)*naold); + ts+=receiveDataOnly(file_des,adcptr, sizeof(float)*nadcdiff); free(adcptr); return FAIL; } if (nchipdiff<=0) { - ts+=receiveDataOnly(myMod->chipregs, sizeof(int)*nChips); + ts+=receiveDataOnly(file_des,myMod->chipregs, sizeof(int)*nChips); #ifdef VERBOSE printf("chips received\n"); #endif } else { chipptr=malloc(nchipdiff*sizeof(int)); myMod->nchip=nchipold; - ts+=receiveDataOnly(myMod->chipregs, sizeof(int)*nchipold); - ts+=receiveDataOnly(chipptr, sizeof(int)*nchipdiff); + ts+=receiveDataOnly(file_des,myMod->chipregs, sizeof(int)*nchipold); + ts+=receiveDataOnly(file_des,chipptr, sizeof(int)*nchipdiff); free(chipptr); return FAIL; } if (nchandiff<=0) { - ts+=receiveDataOnly(myMod->chanregs, sizeof(int)*nChans); + ts+=receiveDataOnly(file_des,myMod->chanregs, sizeof(int)*nChans); #ifdef VERBOSE printf("chans received\n"); #endif } else { chanptr=malloc(nchandiff*sizeof(int)); myMod->nchan=nchanold; - ts+=receiveDataOnly(myMod->chanregs, sizeof(int)*nchanold); - ts+=receiveDataOnly(chanptr, sizeof(int)*nchandiff); + ts+=receiveDataOnly(file_des,myMod->chanregs, sizeof(int)*nchanold); + ts+=receiveDataOnly(file_des,chanptr, sizeof(int)*nchandiff); free(chanptr); return FAIL; } diff --git a/slsDetectorSoftware/commonFiles/communication_funcs.h b/slsDetectorSoftware/commonFiles/communication_funcs.h index 9f4faaadf..959851167 100755 --- a/slsDetectorSoftware/commonFiles/communication_funcs.h +++ b/slsDetectorSoftware/commonFiles/communication_funcs.h @@ -14,19 +14,23 @@ #include "sls_detector_defs.h" -int bindSocket(unsigned short int port_number); -int acceptConnection(); -void closeConnection(); -void exitServer(); -int sendDataOnly(void* buf,int length); -int receiveDataOnly(void* buf,int length); +char lastClientIP[INET_ADDRSTRLEN]; +char thisClientIP[INET_ADDRSTRLEN]; +int lockStatus; -int getServerError(); -int sendChannel(sls_detector_channel *myChan); -int sendChip(sls_detector_chip *myChip); -int sendModule(sls_detector_module *myMod); -int receiveChannel(sls_detector_channel *myChan); -int receiveChip(sls_detector_chip* myChip); -int receiveModule(sls_detector_module* myMod); +int bindSocket(unsigned short int port_number); +int acceptConnection(int socketDescriptor); +void closeConnection(int file_Des); +void exitServer(int socketDescriptor); +int sendDataOnly(int file_des, void* buf,int length); +int receiveDataOnly(int file_des, void* buf,int length); + +int getServerError(int socketDescriptor); +int sendChannel(int file_des, sls_detector_channel *myChan); +int sendChip(int file_des, sls_detector_chip *myChip); +int sendModule(int file_des, sls_detector_module *myMod); +int receiveChannel(int file_des, sls_detector_channel *myChan); +int receiveChip(int file_des, sls_detector_chip* myChip); +int receiveModule(int file_des, sls_detector_module* myMod); #endif diff --git a/slsDetectorSoftware/commonFiles/sls_detector_defs.h b/slsDetectorSoftware/commonFiles/sls_detector_defs.h index f9c2a7aee..c212795f5 100755 --- a/slsDetectorSoftware/commonFiles/sls_detector_defs.h +++ b/slsDetectorSoftware/commonFiles/sls_detector_defs.h @@ -383,6 +383,12 @@ enum correctionFlags { I0_NORMALIZATION }; +enum portType { + CONTROL_PORT, /**< control port */ + STOP_PORT, /**alreadyExisting==0) { - thisMultiDetector->onlineFlag=ONLINE; + thisMultiDetector->onlineFlag = slsDetector::ONLINE_FLAG; thisMultiDetector->numberOfDetectors=0; for (int id=0; iddetectorIds[id]=-1; + thisMultiDetector->offsetX[id]=0; + thisMultiDetector->offsetY[id]=0; } - thisMultiDetector->masterId=-1; + thisMultiDetector->masterPosition=-1; thisMultiDetector->dataBytes=0; + thisMultiDetector->numberOfChannels=0; thisMultiDetector->alreadyExisting=1; } @@ -107,6 +110,12 @@ multiSlsDetector::multiSlsDetector(int id) : shmId(-1) } for (int i=thisMultiDetector->numberOfDetectors; ilastPID=getpid(); + } multiSlsDetector::~multiSlsDetector() { @@ -114,7 +123,7 @@ multiSlsDetector::~multiSlsDetector() { } -int multiSlsDetector::addSlsDetector(int id, int pos) { +int multiSlsDetector::addSlsDetector(int id, int pos, int ox, int oy) { int j=thisMultiDetector->numberOfDetectors; if (pos<0) @@ -133,15 +142,51 @@ int multiSlsDetector::addSlsDetector(int id, int pos) { thisMultiDetector->numberOfDetectors++; thisMultiDetector->dataBytes+=detectors[pos]->getDataBytes(); - + thisMultiDetector->numberOfChannels+=detectors[pos]->getNChans()*detectors[pos]->getNChips()*detectors[pos]->getNMods(); + + return thisMultiDetector->numberOfDetectors; } +int multiSlsDetector::getDetectorOffset(int pos, int &ox, int &oy) { + ox=-1; + oy=-1; + int ret=FAIL; + if (pos>=0 && posnumberOfDetectors) { + if (detectors[pos]) { + ox=thisMultiDetector->offsetX[pos]; + oy=thisMultiDetector->offsetY[pos]; + ret=OK; + } + } + return ret; +} + +int multiSlsDetector::setDetectorOffset(int pos, int ox, int oy) { + + + int ret=FAIL; + + if (pos>=0 && posnumberOfDetectors) { + if (detectors[pos]) { + if (ox!=-1) + thisMultiDetector->offsetX[pos]=ox; + if (oy!=-1) + thisMultiDetector->offsetY[pos]=oy; + ret=OK; + } + } + return ret; +} + + + + int multiSlsDetector::removeSlsDetector(int pos) { - int j, found=0; + int j; if (pos<0 ) pos=thisMultiDetector->numberOfDetectors-1; @@ -152,6 +197,10 @@ int multiSlsDetector::removeSlsDetector(int pos) { j=pos; if (detectors[j]) { + + thisMultiDetector->dataBytes-=detectors[j]->getDataBytes(); + thisMultiDetector->numberOfChannels-=detectors[j]->getNChans()*detectors[pos]->getNChips()*detectors[pos]->getNMods(); + delete detectors[j]; thisMultiDetector->numberOfDetectors--; @@ -175,69 +224,70 @@ int multiSlsDetector::removeSlsDetector(int pos) { - int setMaster(int i=-1) { - if (i>=0 && inumberOfDetectors) - if (detectors[i]) - thisMultiDetector->masterPosition=i; +int multiSlsDetector::setMaster(int i) { + if (i>=0 && inumberOfDetectors) + if (detectors[i]) + thisMultiDetector->masterPosition=i; - switch (thisMultiDetector->syncMode) { - case MASTER_GATES: - for (int i=0; inumberOfDetectors; i++) { - if (i!=thisMultiDetector->masterPosition) { - if (detector[i]) { - detector[i]->setExternalSignalFlags(GATE_IN_ACTIVE_HIGH, 0); - detector[i]->setTimer(NUMBER_OF_GATES, 1); - detector[i]->setExternalSignalFlags(OFF, 1); + switch (thisMultiDetector->syncMode) { + case MASTER_GATES: + for (int i=0; inumberOfDetectors; i++) { + if (i!=thisMultiDetector->masterPosition) { + if (detectors[i]) { + detectors[i]->setExternalSignalFlags(GATE_IN_ACTIVE_HIGH, 0); + detectors[i]->setTimer(GATES_NUMBER, 1); + detectors[i]->setExternalSignalFlags(SIGNAL_OFF, 1); } - } else { - detector[i]->setExternalSignalFlags(GATE_OUT_ACTIVE_HIGH, 2); + } else { + detectors[i]->setExternalSignalFlags(GATE_OUT_ACTIVE_HIGH, 2); } - } - break; - - case MASTER_TRIGGERS: + } + break; + + case MASTER_TRIGGERS: for (int i=0; inumberOfDetectors; i++) { if (i!=thisMultiDetector->masterPosition) { - if (detector[i]) { - detector[i]->setExternalSignalFlags(OFF, 0); - detector[i]->setExternalSignalFlags(TRIGGER_IN_RISING_EDGE, 1); + if (detectors[i]) { + detectors[i]->setExternalSignalFlags(SIGNAL_OFF, 0); + detectors[i]->setExternalSignalFlags(TRIGGER_IN_RISING_EDGE, 1); } } else { - detector[i]->setExternalSignalFlags(GATE_OUT_ACTIVE_HIGH, 2); + detectors[i]->setExternalSignalFlags(GATE_OUT_ACTIVE_HIGH, 2); } } break; - - case SLAVE_STARTS_WHEN_MASTER_STOPS: - for (int i=0; inumberOfDetectors; i++) { - if (detector[i]) { - detector[i]->setExternalSignalFlags(OFF, 0); - detector[i]->setExternalSignalFlags(TRIGGER_IN_FALLING_EDGE, 1); - } - } else { - detector[i]->setExternalSignalFlags(GATE_OUT_ACTIVE_HIGH, 2); - } - } - break; - - - default: - for (int i=0; inumberOfDetectors; i++) { - if (detector[i]) { - detector[i]->setExternalSignalFlags(OFF, 0); - detector[i]->setExternalSignalFlags(OFF, 1); - } - } + case SLAVE_STARTS_WHEN_MASTER_STOPS: + for (int i=0; inumberOfDetectors; i++) { + if (detectors[i]) { + if (i!=thisMultiDetector->masterPosition) { + detectors[i]->setExternalSignalFlags(SIGNAL_OFF, 0); + detectors[i]->setExternalSignalFlags(TRIGGER_IN_FALLING_EDGE, 1); + } else { + detectors[i]->setExternalSignalFlags(GATE_OUT_ACTIVE_HIGH, 2); + } + } } + break; + + + default: + for (int i=0; inumberOfDetectors; i++) { + if (detectors[i]) { + detectors[i]->setExternalSignalFlags(SIGNAL_OFF, 0); + detectors[i]->setExternalSignalFlags(SIGNAL_OFF, 1); + } + } + + } - - return thisMultiDetector->masterPosition; - } + + return thisMultiDetector->masterPosition; +} // enum synchronyzationMode { -// GET_SYNHRONIZATION_MODE=-1, /**< the multidetector will return its synchronization mode */ +// GET_SYNCHRONIZATION_MODE=-1, /**< the multidetector will return its synchronization mode */ // NONE, /**< all detectors are independent (no cabling) */ // MASTER_GATES, /**< the master gates the other detectors */ // MASTER_TRIGGERS, /**< the master triggers the other detectors */ @@ -252,8 +302,8 @@ int multiSlsDetector::removeSlsDetector(int pos) { \param sync syncronization mode \returns current syncronization mode */ -synchronizationMode setSyncronization(synchronizationMode sync=GET_SYNHRONIZATION_MODE) { - if (sync>GET_SYNHRONIZATION_MODE) { +synchronizationMode multiSlsDetector::setSynchronization(synchronizationMode sync) { + if (sync>GET_SYNCHRONIZATION_MODE) { switch (sync) { @@ -262,13 +312,13 @@ synchronizationMode setSyncronization(synchronizationMode sync=GET_SYNHRONIZATIO if (thisMultiDetector->masterPosition>=0 && thisMultiDetector->masterPositionnumberOfDetectors) { for (int i=0; inumberOfDetectors; i++) { if (i!=thisMultiDetector->masterPosition) { - if (detector[i]) { - detector[i]->setExternalSignalFlags(GATE_IN_ACTIVE_HIGH, 0); - detector[i]->setTimer(NUMBER_OF_GATES, 1); - detector[i]->setExternalSignalFlags(OFF, 1); + if (detectors[i]) { + detectors[i]->setExternalSignalFlags(GATE_IN_ACTIVE_HIGH, 0); + detectors[i]->setTimer(GATES_NUMBER, 1); + detectors[i]->setExternalSignalFlags(SIGNAL_OFF, 1); } } else { - detector[i]->setExternalSignalFlags(GATE_OUT_ACTIVE_HIGH, 2); + detectors[i]->setExternalSignalFlags(GATE_OUT_ACTIVE_HIGH, 2); } } thisMultiDetector->syncMode=sync; @@ -279,12 +329,12 @@ synchronizationMode setSyncronization(synchronizationMode sync=GET_SYNHRONIZATIO if (thisMultiDetector->masterPosition>=0 && thisMultiDetector->masterPositionnumberOfDetectors) { for (int i=0; inumberOfDetectors; i++) { if (i!=thisMultiDetector->masterPosition) { - if (detector[i]) { - detector[i]->setExternalSignalFlags(OFF, 0); - detector[i]->setExternalSignalFlags(TRIGGER_IN_RISING_EDGE, 1); + if (detectors[i]) { + detectors[i]->setExternalSignalFlags(SIGNAL_OFF, 0); + detectors[i]->setExternalSignalFlags(TRIGGER_IN_RISING_EDGE, 1); } } else { - detector[i]->setExternalSignalFlags(GATE_OUT_ACTIVE_HIGH, 2); + detectors[i]->setExternalSignalFlags(GATE_OUT_ACTIVE_HIGH, 2); } } thisMultiDetector->syncMode=sync; @@ -294,29 +344,31 @@ synchronizationMode setSyncronization(synchronizationMode sync=GET_SYNHRONIZATIO case SLAVE_STARTS_WHEN_MASTER_STOPS: if (thisMultiDetector->masterPosition>=0 && thisMultiDetector->masterPositionnumberOfDetectors) { for (int i=0; inumberOfDetectors; i++) { - if (detector[i]) { - detector[i]->setExternalSignalFlags(OFF, 0); - detector[i]->setExternalSignalFlags(TRIGGER_IN_FALLING_EDGE, 1); + if (detectors[i]) { + if (i!=thisMultiDetector->masterPosition) { + detectors[i]->setExternalSignalFlags(SIGNAL_OFF, 0); + detectors[i]->setExternalSignalFlags(TRIGGER_IN_FALLING_EDGE, 1); + } + } else { + detectors[i]->setExternalSignalFlags(GATE_OUT_ACTIVE_HIGH, 2); } - } else { - detector[i]->setExternalSignalFlags(GATE_OUT_ACTIVE_HIGH, 2); + } + thisMultiDetector->syncMode=sync; } - } - thisMultiDetector->syncMode=sync; - } - break; + break; - default: - for (int i=0; inumberOfDetectors; i++) { - if (detector[i]) { - detector[i]->setExternalSignalFlags(OFF, 0); - detector[i]->setExternalSignalFlags(OFF, 1); + default: + for (int i=0; inumberOfDetectors; i++) { + if (detectors[i]) { + detectors[i]->setExternalSignalFlags(SIGNAL_OFF, 0); + detectors[i]->setExternalSignalFlags(SIGNAL_OFF, 1); + } } + thisMultiDetector->syncMode=sync; } - thisMultiDetector->syncMode=sync; - } - + } + return thisMultiDetector->syncMode; } @@ -349,12 +401,13 @@ synchronizationMode setSyncronization(synchronizationMode sync=GET_SYNHRONIZATIO int multiSlsDetector::setOnline(int off) { - if (off!=GET_ONLINE_FLAG) { + if (off!=slsDetector::GET_ONLINE_FLAG) { thisMultiDetector->onlineFlag=off; for (int i=0; inumberOfDetectors+1; i++) { if (detectors[i]) - detectors[i]->setOnlineFlag(off); + detectors[i]->setOnline(off); } + } return thisMultiDetector->onlineFlag; }; @@ -391,9 +444,130 @@ int multiSlsDetector::getVariablesFromFileName(string fname, int &index, int &p_ + // Initialization functions + + +int multiSlsDetector::getThresholdEnergy(int pos) { + + + int i, posmin, posmax; + int ret1=-100, ret; + + if (pos<0) { + posmin=0; + posmax=thisMultiDetector->numberOfDetectors; + } else { + posmin=pos; + posmax=pos+1; + } + + for (i=posmin; igetThresholdEnergy(); + if (ret1==-100) + ret1=ret; + else if (ret!=ret1) + ret1=FAIL; + + } + + } + thisMultiDetector->currentThresholdEV=ret1; + return ret1; + + +} + + +int multiSlsDetector::setThresholdEnergy(int e_eV, int pos, detectorSettings isettings) { + + int i, posmin, posmax; + int ret1=-100, ret; + + if (pos<0) { + posmin=0; + posmax=thisMultiDetector->numberOfDetectors; + } else { + posmin=pos; + posmax=pos+1; + } + + for (i=posmin; isetThresholdEnergy(e_eV,-1,isettings); + if (ret1==-100) + ret1=ret; + else if (ret!=ret1) + ret1=FAIL; + + } + + } + thisMultiDetector->currentThresholdEV=ret1; + return ret1; + +} + +detectorSettings multiSlsDetector::getSettings(int pos) { + + int i, posmin, posmax; + detectorSettings ret1=GET_SETTINGS, ret; + + if (pos<0) { + posmin=0; + posmax=thisMultiDetector->numberOfDetectors; + } else { + posmin=pos; + posmax=pos+1; + } + + for (i=posmin; igetSettings(); + if (ret1==GET_SETTINGS) + ret1=ret; + else if (ret!=ret1) + ret1=GET_SETTINGS; + + } + + } + thisMultiDetector->currentSettings=ret1; + return ret1; +} + +detectorSettings multiSlsDetector::setSettings(detectorSettings isettings, int pos) { + + + int i, posmin, posmax; + detectorSettings ret1=GET_SETTINGS, ret; + + if (pos<0) { + posmin=0; + posmax=thisMultiDetector->numberOfDetectors; + } else { + posmin=pos; + posmax=pos+1; + } + + for (i=posmin; isetSettings(isettings); + if (ret1==GET_SETTINGS) + ret1=ret; + else if (ret!=ret1) + ret1=GET_SETTINGS; + + } + + } + thisMultiDetector->currentSettings=ret1; + return ret1; + +} @@ -480,13 +654,13 @@ int multiSlsDetector::startReadOut(){ int i=0; int ret=OK, ret1=OK; i=thisMultiDetector->masterPosition; - if (i>=0) + if (i>=0) { if (detectors[i]) { ret=detectors[i]->startReadOut(); if (ret!=OK) ret1=FAIL; } - } + } for (i=0; inumberOfDetectors; i++) { if (detectors[i]) { ret=detectors[i]->startReadOut(); @@ -496,97 +670,89 @@ int multiSlsDetector::startReadOut(){ } return ret1; - + }; +int* multiSlsDetector::getDataFromDetector() { -int* multiSlsDetector::readFrame(){ + int nel=thisMultiDetector->dataBytes/sizeof(int); + int n; + int* retval=new int[nel]; + int *retdet, *p=retval; + + - int fnum=F_READ_FRAME; - int* retval=NULL; - -#ifdef VERBOSE - std::cout<< "slsDetector: Reading frame "<< std::endl; -#endif - if (thisDetector->onlineFlag==ONLINE_FLAG) { - if (controlSocket) { - if (controlSocket->Connect()>=0) { - controlSocket->SendDataOnly(&fnum,sizeof(fnum)); - retval=getDataFromDetector(); - if (retval) { - dataQueue.push(retval); - controlSocket->Disconnect(); - } + for (int id=0; idnumberOfDetectors; id++) { + if (detectors[id]) { + retdet=detectors[id]->getDataFromDetector(); + if (retdet) { + n=detectors[id]->getDataBytes(); + memcpy(p,retdet,n); + delete [] retdet; + p+=n/sizeof(int); + } else { + cout << "Detector " << id << " does not have data left " << endl; + delete [] retval; + return NULL; } } } return retval; }; -int* multiSlsDetector::getDataFromDetector(){ - int nel=thisDetector->dataBytes/sizeof(int); +int* multiSlsDetector::readFrame(){ + int nel=thisMultiDetector->dataBytes/sizeof(int); int n; int* retval=new int[nel]; - int ret=FAIL; - char mess[100]="Nothing"; -#ifdef VERY_VERBOSE - int i; -#endif + int *retdet, *p=retval; + + /** probably it's always better to have one integer per channel in any case! */ -#ifdef VERBOSE - // std::cout<< "getting data "<< std::endl; -#endif - controlSocket->ReceiveDataOnly(&ret,sizeof(ret)); - if (ret!=OK) { - n= controlSocket->ReceiveDataOnly(mess,sizeof(mess)); - if (ret==FAIL) { - thisDetector->stoppedFlag=1; - std::cout<< "Detector returned: " << mess << " " << n << std::endl; - } else { - ; -#ifdef VERBOSE - std::cout<< "Detector successfully returned: " << mess << " " << n << std::endl; -#endif - } - delete [] retval; - retval=NULL; - } else { - n=controlSocket->ReceiveDataOnly(retval,thisDetector->dataBytes); + for (int id=0; idnumberOfDetectors; id++) { + if (detectors[id]) { + retdet=detectors[id]->readFrame(); + if (retdet) { + n=detectors[id]->getDataBytes(); + memcpy(p,retdet,n); + delete [] retdet; + p+=n/sizeof(int); - //#ifdef VERBOSE - std::cout<< "Received "<< n << " data bytes" << std::endl; - //#endif - if (n!=thisDetector->dataBytes) { - std::cout<< "wrong data size received: received " << n << " but expected " << thisDetector->dataBytes << std::endl; - thisDetector->stoppedFlag=1; - ret=FAIL; + } else { + cout << "Detector " << id << " does not have data left " << endl; delete [] retval; - retval=NULL; + return NULL; } } + } + return retval; - return retval; }; int* multiSlsDetector::readAll(){ - int fnum=F_READ_ALL; + /** Thread for each detector?!?!?! */ + + // int fnum=F_READ_ALL; int* retval; // check what we return! + // int ret=OK, ret1=OK; int i=0; #ifdef VERBOSE std::cout<< "Reading all frames "<< std::endl; #endif - if (thisDetector->onlineFlag==ONLINE_FLAG) { - if (controlSocket) { - if (controlSocket->Connect()>=0) { - controlSocket->SendDataOnly(&fnum,sizeof(fnum)); + if (thisMultiDetector->onlineFlag==slsDetector::ONLINE_FLAG) { + + for (int id=0; idnumberOfDetectors; id++) { + if (detectors[id]) { + detectors[id]->readAllNoWait(); + } + } while ((retval=getDataFromDetector())){ i++; #ifdef VERBOSE @@ -594,10 +760,14 @@ int* multiSlsDetector::readAll(){ #endif dataQueue.push(retval); } - controlSocket->Disconnect(); - } - } - } + for (int id=0; idnumberOfDetectors; id++) { + if (detectors[id]) { + detectors[id]->disconnectControl(); + } + } + + } + #ifdef VERBOSE std::cout<< "received "<< i<< " frames" << std::endl; #endif @@ -607,75 +777,64 @@ int* multiSlsDetector::readAll(){ int* multiSlsDetector::startAndReadAll(){ + /** Thread for each detector?!?!?! */ int* retval; int i=0; - startAndReadAllNoWait(); - while ((retval=getDataFromDetector())){ + if (thisMultiDetector->onlineFlag==slsDetector::ONLINE_FLAG) { + + startAndReadAllNoWait(); + + while ((retval=getDataFromDetector())){ i++; //#ifdef VERBOSE std::cout<< i << std::endl; //#endif dataQueue.push(retval); - } - controlSocket->Disconnect(); + } + + for (int id=0; idnumberOfDetectors; id++) { + if (detectors[id]) { + detectors[id]->disconnectControl(); + } + } + + } //#ifdef VERBOSE std::cout<< "recieved "<< i<< " frames" << std::endl; //#endif return dataQueue.front(); // check what we return! -/* while ((retval=getDataFromDetectorNoWait())) - i++; - #ifdef VERBOSE - std::cout<< "Received " << i << " frames"<< std::endl; -#endif - return dataQueue.front(); // check what we return! - */ + }; - int multiSlsDetector::startAndReadAllNoWait(){ - int fnum= F_START_AND_READ_ALL; - -#ifdef VERBOSE - std::cout<< "Starting and reading all frames "<< std::endl; -#endif - thisDetector->stoppedFlag=0; - if (thisDetector->onlineFlag==ONLINE_FLAG) { - if (controlSocket) { - if (controlSocket->Connect()>=0) { - controlSocket->SendDataOnly(&fnum,sizeof(fnum)); - return OK; - } + + int i=0; + int ret=OK, ret1=OK; + + for (i=0; inumberOfDetectors; i++) { + if (i!=thisMultiDetector->masterPosition) + if (detectors[i]) { + ret=detectors[i]->startAndReadAllNoWait(); + if (ret!=OK) + ret1=FAIL; + } } - } - return FAIL; -}; - -int* multiSlsDetector::getDataFromDetectorNoWait() { - int *retval=getDataFromDetector(); - if (thisDetector->onlineFlag==ONLINE_FLAG) { - if (controlSocket) { - if (retval==NULL){ - controlSocket->Disconnect(); - -#ifdef VERBOSE - std::cout<< "Run finished "<< std::endl; -#endif - } else { -#ifdef VERBOSE - std::cout<< "Frame received "<< std::endl; -#endif - } + i=thisMultiDetector->masterPosition; + if (thisMultiDetector->masterPosition>=0) { + if (detectors[i]) { + ret=detectors[i]->startAndReadAllNoWait(); + if (ret!=OK) + ret1=FAIL; + } } - } - return retval; // check what we return! -}; - + return ret1; +} @@ -717,6 +876,8 @@ void multiSlsDetector::resetFinalDataQueue() { } + + /* set or read the acquisition timers enum timerIndex { @@ -731,198 +892,159 @@ void multiSlsDetector::resetFinalDataQueue() { } */ int64_t multiSlsDetector::setTimer(timerIndex index, int64_t t){ - + int i; + int64_t ret1=-100, ret; - int fnum=F_SET_TIMER; - int64_t retval; - uint64_t ut; - char mess[100]; - int ret=OK; - int n=0; - - -#ifdef VERBOSE - std::cout<< "Setting timer "<< index << " to " << t << "ns" << std::endl; -#endif - ut=t; - if (thisDetector->onlineFlag==ONLINE_FLAG) { - if (controlSocket) { - if (controlSocket->Connect()>=0) { - controlSocket->SendDataOnly(&fnum,sizeof(fnum)); - controlSocket->SendDataOnly(&index,sizeof(index)); - n=controlSocket->SendDataOnly(&t,sizeof(t)); - controlSocket->ReceiveDataOnly(&ret,sizeof(ret)); - if (ret!=OK) { - controlSocket->ReceiveDataOnly(mess,sizeof(mess)); - std::cout<< "Detector returned error: " << mess << std::endl; - } else { - controlSocket->ReceiveDataOnly(&retval,sizeof(retval)); - thisDetector->timerValue[index]=retval; - } - controlSocket->Disconnect(); + if (index!=ACQUISITION_TIME) { + for (i=0; inumberOfDetectors; i++) { + if (detectors[i]) { + ret=detectors[i]->setTimer(index,t); + if (ret1==-100) + ret1=ret; + else if (ret!=ret1) + ret1=FAIL; + } } } else { - //std::cout<< "offline " << std::endl; - if (t>=0) - thisDetector->timerValue[index]=t; - -} -#ifdef VERBOSE - std::cout<< "Timer " << index << " set to "<< thisDetector->timerValue[index] << "ns" << std::endl; -#endif - if (index==PROBES_NUMBER) { - setDynamicRange(); - //cout << "Changing probes: data size = " << thisDetector->dataBytes <timerValue[index]; - -}; - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -int64_t multiSlsDetector::getTimeLeft(timerIndex index){ - - - int fnum=F_GET_TIME_LEFT; - int64_t retval; - char mess[100]; - int ret=OK; - -#ifdef VERBOSE - std::cout<< "Getting timer "<< index << std::endl; -#endif - if (thisDetector->onlineFlag==ONLINE_FLAG) { -// if (controlSocket) { -// if (controlSocket->Connect()>=0) { -// controlSocket->SendDataOnly(&fnum,sizeof(fnum)); -// controlSocket->SendDataOnly(&index,sizeof(index)); -// controlSocket->ReceiveDataOnly(&ret,sizeof(ret)); -// if (ret!=OK) { -// controlSocket->ReceiveDataOnly(mess,sizeof(mess)); -// std::cout<< "Detector returned error: " << mess << std::endl; -// } else { -// controlSocket->ReceiveDataOnly(&retval,sizeof(retval)); -// // thisDetector->timerValue[index]=retval; -// } -// controlSocket->Disconnect(); -// } -// } - if (stopSocket) { - if (stopSocket->Connect()>=0) { - stopSocket->SendDataOnly(&fnum,sizeof(fnum)); - stopSocket->SendDataOnly(&index,sizeof(index)); - stopSocket->ReceiveDataOnly(&ret,sizeof(ret)); - if (ret!=OK) { - stopSocket->ReceiveDataOnly(mess,sizeof(mess)); - std::cout<< "Detector returned error: " << mess << std::endl; - } else { - stopSocket->ReceiveDataOnly(&retval,sizeof(retval)); - // thisDetector->timerValue[index]=retval; - } - stopSocket->Disconnect(); - } + switch(thisMultiDetector->syncMode) { + case MASTER_GATES: + for (i=0; inumberOfDetectors; i++) { + if (i!=thisMultiDetector->masterPosition) + if (detectors[i]) { + ret=detectors[i]->setTimer(GATES_NUMBER,1); + } + } + + i=thisMultiDetector->masterPosition; + if (thisMultiDetector->masterPosition>=0) { + if (detectors[i]) { + ret=detectors[i]->setTimer(index,t); + ret1=ret; + } + } + break; + + default: + for (i=0; inumberOfDetectors; i++) { + if (detectors[i]) { + ret=detectors[i]->setTimer(index,t); + if (ret1==-100) + ret1=ret; + else if (ret!=ret1) + ret1=FAIL; + } + } } } -#ifdef VERBOSE - std::cout<< "Time left is "<< retval << std::endl; -#endif - return retval; - + +// check return values!!! + + thisMultiDetector->timerValue[index]=ret1; + + return ret1; }; + + + + + + + + + + + + + + + + + + + + +// int64_t multiSlsDetector::getTimeLeft(timerIndex index){ + + +// int fnum=F_GET_TIME_LEFT; +// int64_t retval; +// char mess[100]; +// int ret=OK; + +// #ifdef VERBOSE +// std::cout<< "Getting timer "<< index << std::endl; +// #endif +// if (thisDetector->onlineFlag==ONLINE_FLAG) { +// // if (controlSocket) { +// // if (controlSocket->Connect()>=0) { +// // controlSocket->SendDataOnly(&fnum,sizeof(fnum)); +// // controlSocket->SendDataOnly(&index,sizeof(index)); +// // controlSocket->ReceiveDataOnly(&ret,sizeof(ret)); +// // if (ret!=OK) { +// // controlSocket->ReceiveDataOnly(mess,sizeof(mess)); +// // std::cout<< "Detector returned error: " << mess << std::endl; +// // } else { +// // controlSocket->ReceiveDataOnly(&retval,sizeof(retval)); +// // // thisDetector->timerValue[index]=retval; +// // } +// // controlSocket->Disconnect(); +// // } +// // } +// if (stopSocket) { +// if (stopSocket->Connect()>=0) { +// stopSocket->SendDataOnly(&fnum,sizeof(fnum)); +// stopSocket->SendDataOnly(&index,sizeof(index)); +// stopSocket->ReceiveDataOnly(&ret,sizeof(ret)); +// if (ret!=OK) { +// stopSocket->ReceiveDataOnly(mess,sizeof(mess)); +// std::cout<< "Detector returned error: " << mess << std::endl; +// } else { +// stopSocket->ReceiveDataOnly(&retval,sizeof(retval)); +// // thisDetector->timerValue[index]=retval; +// } +// stopSocket->Disconnect(); +// } +// } +// } +// #ifdef VERBOSE +// std::cout<< "Time left is "<< retval << std::endl; +// #endif +// return retval; + +// }; + + // Flags -int multiSlsDetector::setDynamicRange(int n){ +int multiSlsDetector::setDynamicRange(int n, int pos){ - int fnum=F_SET_DYNAMIC_RANGE; - int retval=-1; - char mess[100]; - int ret=OK; + int imi, ima, i; + int ret, ret1=-100; -#ifdef VERBOSE - std::cout<< "Setting dynamic range to "<< n << std::endl; -#endif - if (n==24) - n=32; - if (thisDetector->onlineFlag==ONLINE_FLAG) { - if (controlSocket) { - if (controlSocket->Connect()>=0) { - controlSocket->SendDataOnly(&fnum,sizeof(fnum)); - controlSocket->SendDataOnly(&n,sizeof(n)); - controlSocket->ReceiveDataOnly(&ret,sizeof(ret)); - if (ret!=OK) { - controlSocket->ReceiveDataOnly(mess,sizeof(mess)); - std::cout<< "Detector returned error: " << mess << std::endl; - } else { - controlSocket->ReceiveDataOnly(&retval,sizeof(retval)); - } - controlSocket->Disconnect(); - } - } + if (pos<0) { + imi=0; + ima=thisMultiDetector->numberOfDetectors; } else { - if (n>0) - thisDetector->dynamicRange=n; - retval=thisDetector->dynamicRange; + imi=pos; + ima=pos+1; } - - if (ret==OK && retval>0) { - /* checking the number of probes to chose the data size */ - if (thisDetector->timerValue[PROBES_NUMBER]==0) { - thisDetector->dataBytes=thisDetector->nMod[X]*thisDetector->nMod[Y]*thisDetector->nChips*thisDetector->nChans*retval/8; - } else { - thisDetector->dataBytes=thisDetector->nMod[X]*thisDetector->nMod[Y]*thisDetector->nChips*thisDetector->nChans*4; + + for (i=imi; idataBytes-=detectors[i]->getDataBytes(); + ret=detectors[i]->setDynamicRange(n); + if (ret1==-100) + ret1=ret; + else if (ret!=ret1) + ret1=FAIL; + thisMultiDetector->dataBytes+=detectors[i]->getDataBytes(); } - - if (retval==32) - thisDetector->dynamicRange=24; - else - thisDetector->dynamicRange=retval; - - -#ifdef VERBOSE - std::cout<< "Dynamic range set to "<< thisDetector->dynamicRange << std::endl; - std::cout<< "Data bytes "<< thisDetector->dataBytes << std::endl; -#endif - - } - return thisDetector->dynamicRange; + } + + return thisMultiDetector->dataBytes; }; /* @@ -933,202 +1055,26 @@ int multiSlsDetector::setROI(int nroi, int *xmin, int *xmax, int *ymin, int *yma }; */ - /* - -enum readOutFlags { - NORMAL_READOUT, - setReadOutFlags(STORE_IN_RAM, - READ_HITS, - ZERO_COMPRESSION, - BACKGROUND_CORRECTION -}{}; - - */ - -int multiSlsDetector::setReadOutFlags(readOutFlags flag){ - - - int fnum=F_SET_READOUT_FLAGS; - readOutFlags retval; - char mess[100]; - int ret=OK; - -#ifdef VERBOSE - std::cout<< "Setting readout flags to "<< flag << std::endl; -#endif - - if (thisDetector->onlineFlag==ONLINE_FLAG) { - if (controlSocket) { - if (controlSocket->Connect()>=0) { - controlSocket->SendDataOnly(&fnum,sizeof(fnum)); - controlSocket->SendDataOnly(&flag,sizeof(flag)); - controlSocket->ReceiveDataOnly(&ret,sizeof(ret)); - if (ret!=OK) { - controlSocket->ReceiveDataOnly(mess,sizeof(mess)); - std::cout<< "Detector returned error: " << mess << std::endl; - } else { - controlSocket->ReceiveDataOnly(&retval,sizeof(retval)); - thisDetector->roFlags=retval; - } - controlSocket->Disconnect(); - } - } - } else { - if (flag!=GET_READOUT_FLAGS) - thisDetector->roFlags=flag; - } - -#ifdef VERBOSE - std::cout<< "Readout flag set to "<< retval << std::endl; -#endif - return thisDetector->roFlags; -}; - - //Trimming - /* -enum trimMode { - NOISE_TRIMMING, - BEAM_TRIMMING, - IMPROVE_TRIMMING, - FIXEDSETTINGS_TRIMMING, - OFFLINE_TRIMMING -}{}; - */ -int multiSlsDetector::executeTrimming(trimMode mode, int par1, int par2, int imod){ - - int fnum= F_EXECUTE_TRIMMING; - int retval=FAIL; - char mess[100]; - int ret=OK; - int arg[3]; - arg[0]=imod; - arg[1]=par1; - arg[2]=par2; - - -#ifdef VERBOSE - std::cout<< "Trimming module " << imod << " with mode "<< mode << " parameters " << par1 << " " << par2 << std::endl; -#endif - - if (thisDetector->onlineFlag==ONLINE_FLAG) { - if (controlSocket) { - if (controlSocket->Connect()>=0) { - controlSocket->SendDataOnly(&fnum,sizeof(fnum)); - std::cout<< "sending mode bytes= "<< controlSocket->SendDataOnly(&mode,sizeof(mode)) << std::endl; - controlSocket->SendDataOnly(arg,sizeof(arg)); - controlSocket->ReceiveDataOnly(&ret,sizeof(ret)); - if (ret!=OK) { - controlSocket->ReceiveDataOnly(mess,sizeof(mess)); - std::cout<< "Detector returned error: " << mess << std::endl; - } else { -#ifdef VERBOSE - std::cout<< "Detector trimmed "<< ret << std::endl; -#endif - /* - controlSocket->ReceiveDataOnly(&retval,sizeof(retval)); - thisDetector->roFlags=retval; - */ - retval=ret; - } - controlSocket->Disconnect(); - } - } - } - return retval; - -}; float* multiSlsDetector::decodeData(int *datain) { - float *dataout=new float[thisDetector->nChans*thisDetector->nChips*thisDetector->nMods]; - const int bytesize=8; + float *dataout=new float[thisMultiDetector->numberOfChannels]; + int ich=0; + float *detp; + int *datap=datain; - int ival=0; - char *ptr=(char*)datain; - char iptr; - int nbits=thisDetector->dynamicRange; - int ipos=0, ichan=0, ibyte; - if (thisDetector->timerValue[PROBES_NUMBER]==0) { - switch (nbits) { - case 1: - for (ibyte=0; ibytedataBytes; ibyte++) { - iptr=ptr[ibyte]&0x1; - for (ipos=0; ipos<8; ipos++) { - // dataout[ibyte*2+ichan]=((iptr&((0xf)<>ichan)&0xf; - ival=(iptr>>(ipos))&0x1; - dataout[ichan]=ival; - ichan++; - } - } - break; - case 4: - for (ibyte=0; ibytedataBytes; ibyte++) { - iptr=ptr[ibyte]&0xff; - for (ipos=0; ipos<2; ipos++) { - // dataout[ibyte*2+ichan]=((iptr&((0xf)<>ichan)&0xf; - ival=(iptr>>(ipos*4))&0xf; - dataout[ichan]=ival; - ichan++; - } + for (int i=0; inumberOfDetectors; i++) { + if (detectors[i]) { + detp=detectors[i]->decodeData(datap); + datap+=detectors[i]->getDataBytes(); + for (int j=0; jgetNChans()*detectors[i]->getNChips()*detectors[i]->getNMods(); j++) { + dataout[ich]=detp[j]; + ich++; } - break; - case 8: - for (ichan=0; ichandataBytes; ichan++) { - ival=ptr[ichan]&0xff; - dataout[ichan]=ival; - } - break; - case 16: - for (ichan=0; ichannChans*thisDetector->nChips*thisDetector->nMods; ichan++) { - // dataout[ichan]=0; - ival=0; - for (ibyte=0; ibyte<2; ibyte++) { - iptr=ptr[ichan*2+ibyte]; - ival|=((iptr<<(ibyte*bytesize))&(0xff<<(ibyte*bytesize))); - } - dataout[ichan]=ival; - } - break; - default: - for (ichan=0; ichannChans*thisDetector->nChips*thisDetector->nMods; ichan++) { - ival=datain[ichan]&0xffffff; - dataout[ichan]=ival; - } - } - } else { - for (ichan=0; ichannChans*thisDetector->nChips*thisDetector->nMods; ichan++) { - dataout[ichan]=datain[ichan]; + delete [] detp; } } - /* - - if (nbits==32) { - for (ichan=0; ichannChans*thisDetector->nChips*thisDetector->nMods; ichan++) - dataout[ichan]=(datain[ichan]&0xffffff); - } else { - for (int ibyte=0; ibytedataBytes; ibyte++) { - for (int ibit=0; ibit>ibit)<dynamicRange) { - ipos=0; - dataout[ichan]=ival; - ichan++; - ival=0; - if (ichan>thisDetector->nChans*thisDetector->nChips*thisDetector->nMods){ - std::cout<< "error: decoding too many channels!" << ichan; - break; - } - } - } - } - } - */ -#ifdef VERBOSE - std::cout<< "decoded "<< ichan << " channels" << std::endl; -#endif - - return dataout; } @@ -1143,32 +1089,61 @@ float* multiSlsDetector::decodeData(int *datain) { } */ -int multiSlsDetector::setFlatFieldCorrection(string fname){ - float data[thisDetector->nModMax[X]*thisDetector->nModMax[Y]*thisDetector->nChans*thisDetector->nChips]; - //float err[thisDetector->nModMax[X]*thisDetector->nModMax[Y]*thisDetector->nChans*thisDetector->nChips]; - float xmed[thisDetector->nModMax[X]*thisDetector->nModMax[Y]*thisDetector->nChans*thisDetector->nChips]; - int nmed=0; - int im=0; - int nch; - thisDetector->nBadFF=0; - char ffffname[MAX_STR_LENGTH*2]; +/////////////////////////////////////////////////////////////////////////////////////////////////////// +////////////////////////////////////////////////////////////////////////////////////////////////////// +///////////////////////////////////////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////////////////////////////////////// + + + + + + + +int multiSlsDetector::setFlatFieldCorrection(string fname){ + float data[thisMultiDetector->numberOfChannels], xmed[thisMultiDetector->numberOfChannels]; + float ffcoefficients[thisMultiDetector->numberOfChannels], fferrors[thisMultiDetector->numberOfChannels]; + int nmed=0; + int idet=0, ichdet=-1; + char ffffname[MAX_STR_LENGTH*2]; + int nbad=0, nch; + int badlist[MAX_BADCHANS]; + int im=0; + if (fname=="") { #ifdef VERBOSE std::cout<< "disabling flat field correction" << std::endl; #endif - thisDetector->correctionMask&=~(1<flatFieldFile,"none"); + thisMultiDetector->correctionMask&=~(1<flatFieldFile,"none"); + for (int i=0; inumberOfDetectors; i++) { + if (detectors[i]) + detectors[i]->setFlatFieldCorrection(NULL, NULL); + } } else { #ifdef VERBOSE std::cout<< "Setting flat field correction from file " << fname << std::endl; #endif - sprintf(ffffname,"%s/%s",thisDetector->flatFieldDir,fname.c_str()); + sprintf(ffffname,"%s/%s",thisMultiDetector->flatFieldDir,fname.c_str()); nch=readDataFile(string(ffffname),data); if (nch>0) { - strcpy(thisDetector->flatFieldFile,fname.c_str()); + strcpy(thisMultiDetector->flatFieldFile,fname.c_str()); + + + for (int ichan=0; ichan=detectors[idet]->getNChans()*detectors[idet]->getNChips()*detectors[idet]->getNMods()) { + ichdet=0; + detectors[idet]->setBadChannelCorrection(nbad,badlist,1); + idet++; + nbad=0; + } else + ichdet++; + } + if (data[ichan]>0) { /* add to median */ im=0; @@ -1179,26 +1154,38 @@ int multiSlsDetector::setFlatFieldCorrection(string fname){ xmed[im]=data[ichan]; nmed++; } else { - //add the channel to the ff bad channel list - if (thisDetector->nBadFFbadFFList[thisDetector->nBadFF]=ichan; - (thisDetector->nBadFF)++; -#ifdef VERBOSE - std::cout<< "Channel " << ichan << " added to the bad channel list" << std::endl; -#endif - } else - std::cout<< "Too many bad channels " << std::endl; - - } + badlist[nbad]=ichdet; + nbad++; } - + } + if (detectors[idet]) + detectors[idet]->setBadChannelCorrection(nbad,badlist,1); + if (nmed>1 && xmed[nmed/2]>0) { #ifdef VERBOSE std::cout<< "Flat field median is " << xmed[nmed/2] << " calculated using "<< nmed << " points" << std::endl; #endif - thisDetector->correctionMask|=(1<correctionMask|=(1<=detectors[idet]->getNChans()*detectors[idet]->getNChips()*detectors[idet]->getNMods()) { + detectors[idet]->setFlatFieldCorrection(ffcoefficients+ichdet, fferrors+ichdet); + ichdet=ichan; + idet++; + } + } + + if (data[ichan]>0) { ffcoefficients[ichan]=xmed[nmed/2]/data[ichan]; fferrors[ichan]=ffcoefficients[ichan]*sqrt(data[ichan])/data[ichan]; @@ -1206,128 +1193,119 @@ int multiSlsDetector::setFlatFieldCorrection(string fname){ ffcoefficients[ichan]=0.; fferrors[ichan]=1.; } - } - for (int ichan=nch; ichannMod[X]*thisDetector->nMod[Y]*thisDetector->nChans*thisDetector->nChips; ichan++) { - ffcoefficients[ichan]=1.; - fferrors[ichan]=0.; } - - fillBadChannelMask(); - + if (detectors[idet]) { + detectors[idet]->setFlatFieldCorrection(ffcoefficients+ichdet, fferrors+ichdet); + } } else { std::cout<< "Flat field data from file " << fname << " are not valid (" << nmed << "///" << xmed[nmed/2] << std::endl; + thisMultiDetector->correctionMask&=~(1<numberOfDetectors; i++) { + if (detectors[i]) + detectors[i]->setFlatFieldCorrection(NULL, NULL); + } return -1; } } else { - std::cout<< "Flat field from file " << fname << " is not valid " << nch << std::endl; - return -1; + std::cout<< "Flat field from file " << fname << " is not valid " << nch << std::endl; + thisMultiDetector->correctionMask&=~(1<numberOfDetectors; i++) { + if (detectors[i]) + detectors[i]->setFlatFieldCorrection(NULL, NULL); + } + return -1; } } - return thisDetector->correctionMask&(1<correctionMask&(1<correctionMask&(1<nMod[X]*thisDetector->nMod[Y]*thisDetector->nChans*thisDetector->nChips; ichan++) { - corr[ichan]=(ffcoefficients[ichan]*ffcoefficients[ichan])/(fferrors[ichan]*fferrors[ichan]); - if (ecorr) { - ecorr[ichan]=ffcoefficients[ichan]/fferrors[ichan]; - } - } + int ichdet=0; + float *p, *ep; + for (int idet=0; idetnumberOfDetectors; idet++) { + if (detectors[idet]) { + if (corr!=NULL) + p=corr+ichdet; + else + p=NULL; + if (ecorr!=NULL) + ep=ecorr+ichdet; + else + ep=NULL; + detectors[idet]->getFlatFieldCorrection(p, ep); + ichdet+=detectors[idet]->getNChans()*detectors[idet]->getNChips()*detectors[idet]->getNMods(); } - return 1; - } else { -#ifdef VERBOSE - std::cout<< "Flat field correction is disabled" << std::endl; -#endif - if (corr) - for (int ichan=0; ichannMod[X]*thisDetector->nMod[Y]*thisDetector->nChans*thisDetector->nChips; ichan++) { - corr[ichan]=1; - if (ecorr) - ecorr[ichan]=0; - } - return 0; } + return 0; } int multiSlsDetector::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; + return slsDetector::flatFieldCorrect(datain, errin, dataout, errout, ffcoefficient, fferr); }; -int multiSlsDetector::flatFieldCorrect(float* datain, float *errin, float* dataout, float *errout){ -#ifdef VERBOSE - std::cout<< "Flat field correcting data" << std::endl; -#endif - float e, eo; - if (thisDetector->correctionMask&(1<nMod[X]*thisDetector->nMod[Y]*thisDetector->nChans*thisDetector->nChips; ichan++) { - if (errin==NULL) - e=0; - else - e=errin[ichan]; - - flatFieldCorrect(datain[ichan],e,dataout[ichan],eo,ffcoefficients[ichan],fferrors[ichan]); - if (errout) - errout[ichan]=eo; + + +int multiSlsDetector::flatFieldCorrect(float* datain, float *errin, float* dataout, float *errout){ + + int ichdet=0; + for (int idet=0; idetnumberOfDetectors; idet++) { + if (detectors[idet]) { + detectors[idet]->flatFieldCorrect(datain+ichdet, errin+ichdet, dataout+ichdet, errout+ichdet); + ichdet+=detectors[idet]->getNChans()*detectors[idet]->getNChips()*detectors[idet]->getNMods(); } } return 0; - }; + + + + + int multiSlsDetector::setRateCorrection(float t){ float tdead[]=defaultTDead; if (t==0) { -#ifdef VERBOSE - std::cout<< "unsetting rate correction" << std::endl; -#endif - thisDetector->correctionMask&=~(1<correctionMask&=~(1<correctionMask|=(1<0) - thisDetector->tDead=t; - else { - if (thisDetector->currentSettings<3 && thisDetector->currentSettings>-1) - thisDetector->tDead=tdead[thisDetector->currentSettings]; - else - thisDetector->tDead=0; + thisMultiDetector->correctionMask|=(1<numberOfDetectors; idet++) { + + if (detectors[idet]) { + detectors[idet]->setRateCorrection(t); + } } #ifdef VERBOSE std::cout<< "Setting rate correction with dead time "<< thisDetector->tDead << std::endl; #endif } - return thisDetector->correctionMask&(1<correctionMask&(1<correctionMask&(1<correctionMask&(1<tDead << std::endl; #endif - t=thisDetector->tDead; + //which t should we return if they are all different? return 1; } else t=0; @@ -1339,12 +1317,12 @@ int multiSlsDetector::getRateCorrection(float &t){ float multiSlsDetector::getRateCorrectionTau(){ - if (thisDetector->correctionMask&(1<correctionMask&(1<tDead << std::endl; #endif - return thisDetector->tDead; - //return 1; + //which t should we return if they are all different? + return 1; } else #ifdef VERBOSE std::cout<< "Rate correction is disabled " << std::endl; @@ -1360,7 +1338,7 @@ float multiSlsDetector::getRateCorrectionTau(){ int multiSlsDetector::getRateCorrection(){ - if (thisDetector->correctionMask&(1<correctionMask&(1<=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; + return slsDetector::rateCorrect(datain, errin, dataout, errout, tau, t); }; int multiSlsDetector::rateCorrect(float* datain, float *errin, float* dataout, float *errout){ - float tau=thisDetector->tDead; - float t=thisDetector->timerValue[ACQUISITION_TIME]; - // float data; - float e; - if (thisDetector->correctionMask&(1<nMod[X]*thisDetector->nMod[Y]*thisDetector->nChans*thisDetector->nChips; ichan++) { - - if (errin==NULL) { - e=sqrt(datain[ichan]); - } else - e=errin[ichan]; - - rateCorrect(datain[ichan], e, dataout[ichan], errout[ichan], tau, t); + + int ichdet=0; + for (int idet=0; idetnumberOfDetectors; idet++) { + if (detectors[idet]) { + detectors[idet]->rateCorrect(datain+ichdet, errin+ichdet, dataout+ichdet, errout+ichdet); + ichdet+=detectors[idet]->getNChans()*detectors[idet]->getNChips()*detectors[idet]->getNMods(); } } - return 0; }; int multiSlsDetector::setBadChannelCorrection(string fname){ - 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=="") { - thisDetector->correctionMask&=~(1<< DISCARD_BAD_CHANNELS); - thisDetector->nBadChans=0; - } else { - if (fname=="default") - fname=string(thisDetector->badChanFile); - infile.open(fname.c_str(), ios_base::in); - if (infile.is_open()==0) { - std::cout << "could not open file " << fname <nBadChans=0; - while (infile.good() and interrupt==0) { - getline(infile,str); -#ifdef VERBOSE - std::cout << str << std::endl; -#endif - istringstream ssstr; - ssstr.str(str); - if (!ssstr.good() || infile.eof()) { - interrupt=1; - break; - } - if (str.find('-')!=string::npos) { - ssstr >> 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 (thisDetector->nBadChansbadChansList[thisDetector->nBadChans]=ich; - thisDetector->nBadChans++; -#ifdef VERBOSE - std::cout<< thisDetector->nBadChans << " Found bad channel "<< ich << std::endl; -#endif - } else - interrupt=1; + int badlist[MAX_BADCHANS], badlistdet[MAX_BADCHANS]; + int nbad=0, nbaddet=0, choff=0, idet=0; + + if (fname=="default") + fname=string(thisMultiDetector->badChanFile); + + int ret=slsDetector::setBadChannelCorrection(fname, nbad, badlist); + if (ret) { + thisMultiDetector->correctionMask|=(1<=detectors[idet]->getNChans()*detectors[idet]->getNChips()*detectors[idet]->getNMods()) { + detectors[idet]->setBadChannelCorrection(nbaddet,badlist,0); + choff+=detectors[idet]->getNChans()*detectors[idet]->getNChips()*detectors[idet]->getNMods(); + nbaddet=0; + idet++; + if (detectors[idet]==NULL) + break; } - } else { - ssstr >> ich; -#ifdef VERBOSE - std::cout << "channel "<< ich << std::endl; -#endif - if (thisDetector->nBadChansbadChansList[thisDetector->nBadChans]=ich; - thisDetector->nBadChans++; -#ifdef VERBOSE - std::cout << thisDetector->nBadChans << " Found bad channel "<< ich << std::endl; -#endif - } else - interrupt=1; + badlistdet[nbaddet]=(badlist[ich]-choff); + nbaddet++; } - - - } - if (thisDetector->nBadChans>0 && thisDetector->nBadChanscorrectionMask|=(1<< DISCARD_BAD_CHANNELS); - strcpy(thisDetector->badChanFile,fname.c_str()); - } - } - infile.close(); -#ifdef VERBOSE - std::cout << "found " << thisDetector->nBadChans << " badchannels "<< std::endl; -#endif - fillBadChannelMask(); -#ifdef VERBOSE - std::cout << " badchannels mask filled"<< std::endl; -#endif - return thisDetector->nBadChans; -} - -int multiSlsDetector::getBadChannelCorrection(int *bad) { - int ichan; - if (thisDetector->correctionMask&(1<< DISCARD_BAD_CHANNELS)) { - if (bad) { - for (ichan=0; ichannBadChans; ichan++) - bad[ichan]=thisDetector->badChansList[ichan]; - for (int ich=0; ichnBadFF; ich++) - bad[ichan+ich]=thisDetector->badFFList[ich]; - } - return thisDetector->nBadChans+thisDetector->nBadFF; - } else - return 0; -} - - -int multiSlsDetector::fillBadChannelMask() { - - if (thisDetector->correctionMask&(1<< DISCARD_BAD_CHANNELS)) { - if (badChannelMask) - delete [] badChannelMask; - badChannelMask=new int[thisDetector->nChans*thisDetector->nChips*thisDetector->nMods]; - for (int ichan=0; ichannChans*thisDetector->nChips*thisDetector->nMods; ichan++) - badChannelMask[ichan]=0; - for (int ichan=0; ichannBadChans; ichan++) { - if (thisDetector->badChansList[ichan]nChans*thisDetector->nChips*thisDetector->nMods) { - badChannelMask[thisDetector->badChansList[ichan]]=1; -#ifdef VERBOSE - std::cout << ichan << " badchannel "<< ichan << std::endl; -#endif - } - } - for (int ichan=0; ichannBadFF; ichan++) { - if (thisDetector->badFFList[ichan]nChans*thisDetector->nChips*thisDetector->nMods) { - badChannelMask[thisDetector->badFFList[ichan]]=1; -#ifdef VERBOSE - std::cout << ichan << "ff badchannel "<< thisDetector->badFFList[ichan] << std::endl; -#endif + } + nbaddet=0; + for (int i=idet; inumberOfDetectors; i++) { + if (detectors[i]) { + detectors[i]->setBadChannelCorrection(nbaddet,badlist,0); } } - } else { - if (badChannelMask) { - delete [] badChannelMask; - badChannelMask=NULL; + } else { + nbaddet=0; + for (int i=0; i<numberOfDetectors; i++) { + if (detectors[idet]) { + detectors[idet]->setBadChannelCorrection(nbaddet,badlist,0); + } } - } - return thisDetector->nBadFF; - + thisMultiDetector->correctionMask&=~(1<correctionMask&(1<onlineFlag==ONLINE_FLAG) { - if (controlSocket) { - controlSocket->Connect(); - controlSocket->SendDataOnly(&fnum,sizeof(fnum)); - controlSocket->ReceiveDataOnly(&retval,sizeof(retval)); - controlSocket->Disconnect(); + + + + + + + +int multiSlsDetector::getBadChannelCorrection(int *bad) { + int nbad=0, nbaddet=0, choff=0; + int detbad[MAX_BADCHANS]; + for (int idet=0; idetnumberOfDetectors; idet++) { + + if (detectors[idet]) { + nbaddet=detectors[idet]->getBadChannelCorrection(detbad); + for (int ich=0; ichgetNChans()*detectors[idet]->getNChips()*detectors[idet]->getNMods(); + } } - } - if (retval==OK) { - std::cout<< std::endl; - std::cout<< "Shutting down the server" << std::endl; - std::cout<< std::endl; - } - return retval; -}; + return nbad; +} @@ -1589,47 +1457,47 @@ int multiSlsDetector::setAction(int iaction, string fname, string par) { if (iaction>=0 && iactionactionMode[iaction]=0; + thisMultiDetector->actionMode[iaction]=0; } else if (fname=="none") { - thisDetector->actionMode[iaction]=0; - strcpy(thisDetector->actionScript[iaction],fname.c_str()); + thisMultiDetector->actionMode[iaction]=0; + strcpy(thisMultiDetector->actionScript[iaction],fname.c_str()); } else { - strcpy(thisDetector->actionScript[iaction],fname.c_str()); - thisDetector->actionMode[iaction]=1; + strcpy(thisMultiDetector->actionScript[iaction],fname.c_str()); + thisMultiDetector->actionMode[iaction]=1; } if (par!="") { - strcpy(thisDetector->actionParameter[iaction],par.c_str()); + strcpy(thisMultiDetector->actionParameter[iaction],par.c_str()); } - if (thisDetector->actionMode[iaction]) { + if (thisMultiDetector->actionMode[iaction]) { #ifdef VERBOSE - cout << iaction << " " << hex << (1 << iaction) << " " << thisDetector->actionMask << dec; + cout << iaction << " " << hex << (1 << iaction) << " " << thisMultiDetector->actionMask << dec; #endif - thisDetector->actionMask |= (1 << iaction); + thisMultiDetector->actionMask |= (1 << iaction); #ifdef VERBOSE - cout << " set " << hex << thisDetector->actionMask << dec << endl; + cout << " set " << hex << thisMultiDetector->actionMask << dec << endl; #endif } else { #ifdef VERBOSE - cout << iaction << " " << hex << thisDetector->actionMask << dec; + cout << iaction << " " << hex << thisMultiDetector->actionMask << dec; #endif - thisDetector->actionMask &= ~(1 << iaction); + thisMultiDetector->actionMask &= ~(1 << iaction); #ifdef VERBOSE - cout << " unset " << hex << thisDetector->actionMask << dec << endl; + cout << " unset " << hex << thisMultiDetector->actionMask << dec << endl; #endif } #ifdef VERBOSE - cout << iaction << " Action mask set to " << hex << thisDetector->actionMask << dec << endl; + cout << iaction << " Action mask set to " << hex << thisMultiDetector->actionMask << dec << endl; #endif - return thisDetector->actionMode[iaction]; + return thisMultiDetector->actionMode[iaction]; } else return -1; } @@ -1648,16 +1516,16 @@ int multiSlsDetector::setActionParameter(int iaction, string par) { if (iaction>=0 && iactionactionParameter[iaction],par.c_str()); + strcpy(thisMultiDetector->actionParameter[iaction],par.c_str()); } - if (thisDetector->actionMode[iaction]) { - thisDetector->actionMask |= (1 << iaction); + if (thisMultiDetector->actionMode[iaction]) { + thisMultiDetector->actionMask |= (1 << iaction); } else { - thisDetector->actionMask &= ~(1 << iaction); + thisMultiDetector->actionMask &= ~(1 << iaction); } - return thisDetector->actionMode[iaction]; + return thisMultiDetector->actionMode[iaction]; } else return -1; } @@ -1669,7 +1537,7 @@ int multiSlsDetector::setActionParameter(int iaction, string par) { */ string multiSlsDetector::getActionScript(int iaction){ if (iaction>=0 && iactionactionScript[iaction]); + return string(thisMultiDetector->actionScript[iaction]); else return string("wrong index"); }; @@ -1681,7 +1549,7 @@ string multiSlsDetector::getActionScript(int iaction){ */ string multiSlsDetector::getActionParameter(int iaction){ if (iaction>=0 && iactionactionParameter[iaction]); + return string(thisMultiDetector->actionParameter[iaction]); else return string("wrong index"); } @@ -1694,9 +1562,9 @@ string multiSlsDetector::getActionParameter(int iaction){ int multiSlsDetector::getActionMode(int iaction){ if (iaction>=0 && iactionactionMode[iaction] << endl; + cout << "slsDetetctor : action " << iaction << " mode is " << thisMultiDetector->actionMode[iaction] << endl; #endif - return thisDetector->actionMode[iaction]; + return thisMultiDetector->actionMode[iaction]; } else { #ifdef VERBOSE cout << "slsDetetctor : wrong action index " << iaction << endl; @@ -1716,19 +1584,19 @@ int multiSlsDetector::setScan(int iscan, string script, int nvalues, float *valu if (iscan>=0 && iscanscanMode[iscan]=0; + thisMultiDetector->scanMode[iscan]=0; } else { - strcpy(thisDetector->scanScript[iscan],script.c_str()); + strcpy(thisMultiDetector->scanScript[iscan],script.c_str()); if (script=="none") { - thisDetector->scanMode[iscan]=0; + thisMultiDetector->scanMode[iscan]=0; } else if (script=="energy") { - thisDetector->scanMode[iscan]=1; + thisMultiDetector->scanMode[iscan]=1; } else if (script=="threshold") { - thisDetector->scanMode[iscan]=2; + thisMultiDetector->scanMode[iscan]=2; } else if (script=="trimbits") { - thisDetector->scanMode[iscan]=3; + thisMultiDetector->scanMode[iscan]=3; } else { - thisDetector->scanMode[iscan]=4; + thisMultiDetector->scanMode[iscan]=4; } } @@ -1738,31 +1606,31 @@ int multiSlsDetector::setScan(int iscan, string script, int nvalues, float *valu if (par!="") - strcpy(thisDetector->scanParameter[iscan],par.c_str()); + strcpy(thisMultiDetector->scanParameter[iscan],par.c_str()); if (nvalues>=0) { if (nvalues==0) - thisDetector->scanMode[iscan]=0; + thisMultiDetector->scanMode[iscan]=0; else { - thisDetector->nScanSteps[iscan]=nvalues; + thisMultiDetector->nScanSteps[iscan]=nvalues; if (nvalues>MAX_SCAN_STEPS) - thisDetector->nScanSteps[iscan]=MAX_SCAN_STEPS; + thisMultiDetector->nScanSteps[iscan]=MAX_SCAN_STEPS; } } - if (values && thisDetector->scanMode[iscan]>0 ) { - for (int iv=0; ivnScanSteps[iscan]; iv++) { - thisDetector->scanSteps[iscan][iv]=values[iv]; + if (values && thisMultiDetector->scanMode[iscan]>0 ) { + for (int iv=0; ivnScanSteps[iscan]; iv++) { + thisMultiDetector->scanSteps[iscan][iv]=values[iv]; } } if (precision>=0) - thisDetector->scanPrecision[iscan]=precision; + thisMultiDetector->scanPrecision[iscan]=precision; - if (thisDetector->scanMode[iscan]>0){ - thisDetector->actionMask |= 1<< (iscan+MAX_ACTIONS); + if (thisMultiDetector->scanMode[iscan]>0){ + thisMultiDetector->actionMask |= 1<< (iscan+MAX_ACTIONS); } else { - thisDetector->actionMask &= ~(1 << (iscan+MAX_ACTIONS)); + thisMultiDetector->actionMask &= ~(1 << (iscan+MAX_ACTIONS)); } @@ -1778,7 +1646,7 @@ int multiSlsDetector::setScan(int iscan, string script, int nvalues, float *valu - return thisDetector->scanMode[iscan]; + return thisMultiDetector->scanMode[iscan]; } else return -1; @@ -1787,26 +1655,26 @@ int multiSlsDetector::setScan(int iscan, string script, int nvalues, float *valu int multiSlsDetector::setScanScript(int iscan, string script) { if (iscan>=0 && iscanscanMode[iscan]=0; + thisMultiDetector->scanMode[iscan]=0; } else { - strcpy(thisDetector->scanScript[iscan],script.c_str()); + strcpy(thisMultiDetector->scanScript[iscan],script.c_str()); if (script=="none") { - thisDetector->scanMode[iscan]=0; + thisMultiDetector->scanMode[iscan]=0; } else if (script=="energy") { - thisDetector->scanMode[iscan]=1; + thisMultiDetector->scanMode[iscan]=1; } else if (script=="threshold") { - thisDetector->scanMode[iscan]=2; + thisMultiDetector->scanMode[iscan]=2; } else if (script=="trimbits") { - thisDetector->scanMode[iscan]=3; + thisMultiDetector->scanMode[iscan]=3; } else { - thisDetector->scanMode[iscan]=4; + thisMultiDetector->scanMode[iscan]=4; } } - if (thisDetector->scanMode[iscan]>0){ - thisDetector->actionMask |= (1 << (iscan+MAX_ACTIONS)); + if (thisMultiDetector->scanMode[iscan]>0){ + thisMultiDetector->actionMask |= (1 << (iscan+MAX_ACTIONS)); } else { - thisDetector->actionMask &= ~(1 << (iscan+MAX_ACTIONS)); + thisMultiDetector->actionMask &= ~(1 << (iscan+MAX_ACTIONS)); } setTotalProgress(); @@ -1829,9 +1697,9 @@ int multiSlsDetector::setScanScript(int iscan, string script) { #ifdef VERBOSE - cout << "Action mask is " << hex << thisDetector->actionMask << dec << endl; + cout << "Action mask is " << hex << thisMultiDetector->actionMask << dec << endl; #endif - return thisDetector->scanMode[iscan]; + return thisMultiDetector->scanMode[iscan]; } else @@ -1846,8 +1714,8 @@ int multiSlsDetector::setScanParameter(int iscan, string par) { if (iscan>=0 && iscanscanParameter[iscan],par.c_str()); - return thisDetector->scanMode[iscan]; + strcpy(thisMultiDetector->scanParameter[iscan],par.c_str()); + return thisMultiDetector->scanMode[iscan]; } else return -1; @@ -1857,8 +1725,8 @@ int multiSlsDetector::setScanParameter(int iscan, string par) { int multiSlsDetector::setScanPrecision(int iscan, int precision) { if (iscan>=0 && iscan=0) - thisDetector->scanPrecision[iscan]=precision; - return thisDetector->scanMode[iscan]; + thisMultiDetector->scanPrecision[iscan]=precision; + return thisMultiDetector->scanMode[iscan]; } else return -1; @@ -1870,35 +1738,35 @@ int multiSlsDetector::setScanSteps(int iscan, int nvalues, float *values) { if (nvalues>=0) { if (nvalues==0) - thisDetector->scanMode[iscan]=0; + thisMultiDetector->scanMode[iscan]=0; else { - thisDetector->nScanSteps[iscan]=nvalues; + thisMultiDetector->nScanSteps[iscan]=nvalues; if (nvalues>MAX_SCAN_STEPS) - thisDetector->nScanSteps[iscan]=MAX_SCAN_STEPS; + thisMultiDetector->nScanSteps[iscan]=MAX_SCAN_STEPS; } } if (values) { - for (int iv=0; ivnScanSteps[iscan]; iv++) { - thisDetector->scanSteps[iscan][iv]=values[iv]; + for (int iv=0; ivnScanSteps[iscan]; iv++) { + thisMultiDetector->scanSteps[iscan][iv]=values[iv]; } } - if (thisDetector->scanMode[iscan]>0){ - thisDetector->actionMask |= (1 << (iscan+MAX_ACTIONS)); + if (thisMultiDetector->scanMode[iscan]>0){ + thisMultiDetector->actionMask |= (1 << (iscan+MAX_ACTIONS)); } else { - thisDetector->actionMask &= ~(1 << (iscan+MAX_ACTIONS)); + thisMultiDetector->actionMask &= ~(1 << (iscan+MAX_ACTIONS)); } #ifdef VERBOSE - cout << "Action mask is " << hex << thisDetector->actionMask << dec << endl; + cout << "Action mask is " << hex << thisMultiDetector->actionMask << dec << endl; #endif setTotalProgress(); - return thisDetector->scanMode[iscan]; + return thisMultiDetector->scanMode[iscan]; } else @@ -1918,8 +1786,8 @@ int multiSlsDetector::setScanSteps(int iscan, int nvalues, float *values) { */ string multiSlsDetector::getScanScript(int iscan){ if (iscan>=0 && iscanscanMode[iscan]) - return string(thisDetector->scanScript[iscan]); + if (thisMultiDetector->scanMode[iscan]) + return string(thisMultiDetector->scanScript[iscan]); else return string("none"); } else @@ -1934,8 +1802,8 @@ string multiSlsDetector::getScanScript(int iscan){ */ string multiSlsDetector::getScanParameter(int iscan){ if (iscan>=0 && iscanscanMode[iscan]) - return string(thisDetector->scanParameter[iscan]); + if (thisMultiDetector->scanMode[iscan]) + return string(thisMultiDetector->scanParameter[iscan]); else return string("none"); } else @@ -1950,7 +1818,7 @@ string multiSlsDetector::getScanParameter(int iscan){ */ int multiSlsDetector::getScanMode(int iscan){ if (iscan>=0 && iscanscanMode[iscan]; + return thisMultiDetector->scanMode[iscan]; else return -1; } @@ -1966,30 +1834,16 @@ int multiSlsDetector::getScanSteps(int iscan, float *v) { if (iscan>=0 && iscannScanSteps[iscan]; iv++) { - v[iv]=thisDetector->scanSteps[iscan][iv]; + for (int iv=0; ivnScanSteps[iscan]; iv++) { + v[iv]=thisMultiDetector->scanSteps[iscan][iv]; } } setTotalProgress(); - - - - - - - - - - - - - - - if (thisDetector->scanMode[iscan]) - return thisDetector->nScanSteps[iscan]; + if (thisMultiDetector->scanMode[iscan]) + return thisMultiDetector->nScanSteps[iscan]; else return 0; } else @@ -1999,13 +1853,65 @@ int multiSlsDetector::getScanSteps(int iscan, float *v) { int multiSlsDetector::getScanPrecision(int iscan){ if (iscan>=0 && iscanscanPrecision[iscan]; + return thisMultiDetector->scanPrecision[iscan]; } else return -1; } +int multiSlsDetector::setTotalProgress() { + + int nf=1, npos=1, nscan[MAX_SCAN_LEVELS]={1,1}, nc=1; + + + + + + if (thisMultiDetector->timerValue[FRAME_NUMBER]) + nf=thisMultiDetector->timerValue[FRAME_NUMBER]; + + if (thisMultiDetector->timerValue[CYCLES_NUMBER]>0) + nc=thisMultiDetector->timerValue[CYCLES_NUMBER]; + + + + + + + + + if (thisMultiDetector->numberOfPositions>0) + npos=thisMultiDetector->numberOfPositions; + + if ((thisMultiDetector->nScanSteps[0]>0) && (thisMultiDetector->actionMask & (1 << MAX_ACTIONS))) + nscan[0]=thisMultiDetector->nScanSteps[0]; + + if ((thisMultiDetector->nScanSteps[1]>0) && (thisMultiDetector->actionMask & (1 << (MAX_ACTIONS+1)))) + nscan[1]=thisMultiDetector->nScanSteps[1]; + + thisMultiDetector->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 " << thisMultiDetector->totalProgress << endl; +#endif + return thisMultiDetector->totalProgress; +} + + +float multiSlsDetector::getCurrentProgress() { + + return 100.*((float)thisMultiDetector->progressIndex)/((float)thisMultiDetector->totalProgress); +} + + + @@ -2021,4 +1927,3 @@ int multiSlsDetector::getScanPrecision(int iscan){ - diff --git a/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.h b/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.h index b4cb412b5..63684d0d1 100644 --- a/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.h +++ b/slsDetectorSoftware/multiSlsDetector/multiSlsDetector.h @@ -15,11 +15,24 @@ ID: $Id$ #include "slsDetector.h" +#include "sls_detector_defs.h" + #define MAXDET 100 //using namespace std; + /** synchronization of the various detectors (should be set for each detector individually?!?!?) */ + + enum synchronizationMode { + GET_SYNCHRONIZATION_MODE=-1, /**< the multidetector will return its synchronization mode */ + NONE, /**< all detectors are independent (no cabling) */ + MASTER_GATES, /**< the master gates the other detectors */ + MASTER_TRIGGERS, /**< the master triggers the other detectors */ + SLAVE_STARTS_WHEN_MASTER_STOPS /**< the slave acquires when the master finishes, to avoid deadtime */ + }; + + /** @@ -38,8 +51,18 @@ class multiSlsDetector { typedef struct sharedMultiSlsDetector { + + + + /** already existing flag. If the detector does not yet exist (alreadyExisting=0) the sharedMemory will be created, otherwise it will simly be linked */ int alreadyExisting; + + + /** last process id accessing the shared memory */ + pid_t lastPID; + + /** online flag - is set if the detector is connected, unset if socket connection is not possible */ int onlineFlag; @@ -54,7 +77,13 @@ class multiSlsDetector { /** Ids of the detectors to be operated at once */ int detectorIds[MAXDET]; - /** id of the master detector */ + + /** Detectors offset in the X direction (in number of channels)*/ + int offsetX[MAXDET]; + /** Detectors offsets in the Y direction (in number of channels) */ + int offsetY[MAXDET]; + + /** position of the master detector */ int masterPosition; /** type of synchronization between detectors */ @@ -63,15 +92,27 @@ class multiSlsDetector { /** size of the data that are transfered from all detectors */ int dataBytes; + /** total number of channels for all detectors */ + int numberOfChannels; + + /** timer values */ + int64_t timerValue[MAX_TIMERS]; + /** detector settings (standard, fast, etc.) */ + detectorSettings currentSettings; + /** detector threshold (eV) */ + int currentThresholdEV; /** indicator for the acquisition progress - set to 0 at the beginning of the acquisition and incremented every time that the data are written to file */ int progressIndex; /** total number of frames to be acquired */ int totalProgress; - /** current index of the output file */ + + + + /** current index of the output file */ int fileIndex; /** path of the output files */ char filePath[MAX_STR_LENGTH]; @@ -85,21 +126,33 @@ class multiSlsDetector { int threadedProcessing; /** dead time (in ns) for rate corrections */ float tDead; + + + /** directory where the flat field files are stored */ char flatFieldDir[MAX_STR_LENGTH]; /** file used for flat field corrections */ char flatFieldFile[MAX_STR_LENGTH]; - /** number of bad channels from bad channel list */ - int nBadChans; + + /** file with the bad channels */ char badChanFile[MAX_STR_LENGTH]; - /** list of bad channels */ - int badChansList[MAX_BADCHANS]; - /** number of bad channels from flat field i.e. channels which read 0 in the flat field file */ - int nBadFF; - /** list of bad channels from flat field i.e. channels which read 0 in the flat field file */ - int badFFList[MAX_BADCHANS]; - + + /** array of angular conversion constants for each module \see ::angleConversionConstant */ + angleConversionConstant angOff[MAXMODS]; + /** angular direction (1 if it corresponds to the encoder direction i.e. channel 0 is 0, maxchan is positive high angle, 0 otherwise */ + int angDirection; + /** beamline fine offset (of the order of mdeg, might be adjusted for each measurements) */ + float fineOffset; + /** beamline offset (might be a few degrees beacuse of encoder offset - normally it is kept fixed for a long period of time) */ + float globalOffset; + /** number of positions at which the detector should acquire */ + int numberOfPositions; + /** list of encoder positions at which the detector should acquire */ + float detPositions[MAXPOS]; + /** bin size for data merging */ + float binSize; + /** Scans and scripts */ @@ -119,7 +172,7 @@ class multiSlsDetector { -} + }; @@ -137,8 +190,8 @@ class multiSlsDetector { public: - + /** @short Structure allocated in shared memory to store detector settings and be accessed in parallel by several applications (take care of possible conflicts!) @@ -160,13 +213,13 @@ class multiSlsDetector { int freeSharedMemory() ; /** allocates the shared memory occpied for the sharedMultiSlsDetector structure */ - int initSharedMemory() ; + int initSharedMemory(int) ; /** adds the detector with ID id in postion pos \param id of the detector to be added (should already exist!) \param pos position where it should be added (normally at the end of the list (default to -1) \return the actual number of detectors*/ - int addSlsDetector(int id, int pos=-1); + int addSlsDetector(int id, int pos=-1, int oX=-1, int oY=-1); /**removes the detector in position pos from the multidetector \param pos position of the detector to be removed from the multidetector system (defaults to -1 i.e. last detector) @@ -184,6 +237,25 @@ class multiSlsDetector { \returns number of detectors */ int getNumberOfDetectors() {return thisMultiDetector->numberOfDetectors;}; + + /** returns the detector offset (in number of channels) + \param pos position of the detector + \param ox reference to the offset in x + \param oy reference to the offset in y + \returns OK/FAIL if the detector does not exist + */ + int getDetectorOffset(int pos, int &ox, int &oy); + + /** sets the detector offset (in number of channels) + \param pos position of the detector + \param ox offset in x (-1 does not change) + \param oy offset in y (-1 does not change) + \returns OK/FAIL if the detector does not exist + */ + int setDetectorOffset(int pos, int ox=-1, int oy=-1); + + + /** sets the detector in position i as master of the structure (e.g. it gates the other detectors and therefore must be started as last.
Assumes that signal 0 is gate in, signal 1 is trigger in, signal 2 is gate out \param i position of master (-1 gets) \return master's position (-1 none) @@ -195,17 +267,7 @@ class multiSlsDetector { \param sync syncronization mode \returns current syncronization mode */ - synchronizationMode setSyncronization(synchronizationMode sync=GET_SYNHRONIZATION_MODE); - - /** synchronization of the various detectors (should be set for each detector individually?!?!?) */ - - enum synchronyzationMode { - GET_SYNHRONIZATION_MODE=-1, /**< the multidetector will return its synchronization mode */ - NONE, /**< all detectors are independent (no cabling) */ - MASTER_GATES, /**< the master gates the other detectors */ - MASTER_TRIGGERS, /**< the master triggers the other detectors */ - SLAVE_STARTS_WHEN_MASTER_STOPS /**< the slave acquires when the master finishes, to avoid deadtime */ - } + synchronizationMode setSynchronization(synchronizationMode sync=GET_SYNCHRONIZATION_MODE); @@ -214,7 +276,7 @@ class multiSlsDetector { \param off can be: GET_ONLINE_FLAG, returns wether the detector is in online or offline state; OFFLINE_FLAG, detector in offline state (i.e. no communication to the detector - using only local structure - no data acquisition possible!); ONLINE_FLAG detector in online state (i.e. communication to the detector updating the local structure) \returns online/offline status */ - int setOnline(int const online=GET_ONLINE_FLAG); + int setOnline(int const online=slsDetector::GET_ONLINE_FLAG); /** sets the onlineFlag \returns 1 if the detector structure has already be initlialized with the given idand belongs to this multiDetector instance, 0 otherwise */ int exists() ; @@ -274,7 +336,7 @@ class multiSlsDetector { \sa slsDetector::getFileIndexFromFileName */ - int multiSlsDetector::getFileIndexFromFileName(string fname) ; + int getFileIndexFromFileName(string fname) ; /** @@ -288,7 +350,7 @@ class multiSlsDetector { \sa slsDetector::getVariablesFromFileName */ - int multiSlsDetector::getVariablesFromFileName(string fname, int &index, int &p_index, float &sv0, float &sv1) ; + int getVariablesFromFileName(string fname, int &index, int &p_index, float &sv0, float &sv1) ; @@ -443,14 +505,7 @@ class multiSlsDetector { - // calibration functions - // int setCalibration(int imod, detectorSettings isettings, float gain, float offset); - //int getCalibration(int imod, detectorSettings isettings, float &gain, float &offset); - - /* - calibrated setup of the threshold - */ /** get threshold energy \param imod module number (-1 all) @@ -482,7 +537,7 @@ class multiSlsDetector { in this function trimbits and calibration files are searched in the trimDir and calDir directories and the detector is initialized */ - virtual detectorSettings setSettings(detectorSettings isettings, int imod=-1); + detectorSettings setSettings(detectorSettings isettings, int imod=-1); @@ -557,7 +612,13 @@ class multiSlsDetector { \returns pointer to the data or NULL. If NULL disconnects the socket \sa getDataFromDetector */ - int* getDataFromDetectorNoWait(); + //int* getDataFromDetectorNoWait(); + /** + receives a data frame from the detector socket + \returns pointer to the data or NULL. If NULL disconnects the socket + \sa getDataFromDetector + */ + int* getDataFromDetector(); /** asks and receives a data frame from the detector and puts it in the data queue @@ -619,24 +680,27 @@ class multiSlsDetector { */ int64_t setTimer(timerIndex index, int64_t t=-1); - /** - get current timer value - \param index timer index - \returns elapsed time value in ns or number of...(e.g. frames, gates, probes) - */ - int64_t getTimeLeft(timerIndex index); +/* /\** */ +/* get current timer value */ +/* \param index timer index */ +/* \returns elapsed time value in ns or number of...(e.g. frames, gates, probes) */ +/* *\/ */ +/* int64_t getTimeLeft(timerIndex index); */ // Flags /** - set/get dynamic range + set/get dynamic range and updates the number of dataBytes \param n dynamic range (-1 get) + \param pos detector position (-1 all detectors) \returns current dynamic range updates the size of the data expected from the detector \sa sharedSlsDetector */ - int setDynamicRange(int n=-1); + int setDynamicRange(int n=-1, int pos=-1); + + /** set roi @@ -646,22 +710,6 @@ class multiSlsDetector { int setROI(int nroi=-1, int *xmin=NULL, int *xmax=NULL, int *ymin=NULL, int *ymax=NULL); - /** - set/get readout flags - \param flag readout flag to be set - \returns current flag - */ - int setReadOutFlags(readOutFlags flag); - - /** - execute trimming - \param mode trim mode - \param par1 if noise, beam or fixed setting trimming it is count limit, if improve maximum number of iterations - \param par2 if noise or beam nsigma, if improve par2!=means vthreshold will be optimized, if fixed settings par2<0 trimwith median, par2>=0 trim with level - \param imod module number (-1 all) - \returns OK or FAIl (FAIL also if some channel are 0 or 63 - */ - int executeTrimming(trimMode mode, int par1, int par2, int imod=-1); //Corrections @@ -673,7 +721,7 @@ s \returns thread flag */ - int setThreadedProcessing(int b=-1) {if (b>=0) thisDetector->threadedProcessing=b; return thisDetector->threadedProcessing;} + int setThreadedProcessing(int b=-1) {if (b>=0) thisMultiDetector->threadedProcessing=b; return thisMultiDetector->threadedProcessing;} /** set flat field corrections @@ -694,18 +742,18 @@ s get flat field corrections file directory \returns flat field correction file directory */ - char *getFlatFieldCorrectionDir(){return thisDetector->flatFieldDir;}; + char *getFlatFieldCorrectionDir(){return thisMultiDetector->flatFieldDir;}; /** set flat field corrections file directory \param flat field correction file directory */ - void setFlatFieldCorrectionDir(string dir){strcpy(thisDetector->flatFieldDir,dir.c_str());}; + void setFlatFieldCorrectionDir(string dir){strcpy(thisMultiDetector->flatFieldDir,dir.c_str());}; /** get flat field corrections file name \returns flat field correction file name */ - char *getFlatFieldCorrectionFile(){ if (thisDetector->correctionMask&(1<flatFieldFile; else return "none";}; + char *getFlatFieldCorrectionFile(){ if (thisMultiDetector->correctionMask&(1<flatFieldFile; else return "none";}; /** set rate correction @@ -749,7 +797,7 @@ s int getBadChannelCorrection(int *bad=NULL); /** returns the bad channel list file */ - string getBadChannelCorrectionFile() {if (thisDetector->correctionMask&(1<< DISCARD_BAD_CHANNELS)) return string(thisDetector->badChanFile); else return string("none");}; + string getBadChannelCorrectionFile() {if (thisMultiDetector->correctionMask&(1<< DISCARD_BAD_CHANNELS)) return string(thisMultiDetector->badChanFile); else return string("none");}; /** @@ -1063,14 +1111,36 @@ s protected: - + /** Shared memory ID */ int shmId; + /** pointers to the slsDetector structures */ slsDetector *detectors[MAXDET]; - + /** Shared memory structure */ sharedMultiSlsDetector *thisMultiDetector; + + + + + + /** + data queue + */ + queue dataQueue; + /** + queue containing the postprocessed data + */ + queue finalDataQueue; + + + + + + + + /** current position of the detector */ diff --git a/slsDetectorSoftware/mythenDetector/mythenDetector.cpp b/slsDetectorSoftware/mythenDetector/mythenDetector.cpp index 4380a435a..f8f40f49b 100644 --- a/slsDetectorSoftware/mythenDetector/mythenDetector.cpp +++ b/slsDetectorSoftware/mythenDetector/mythenDetector.cpp @@ -679,7 +679,7 @@ int mythenDetector::readDataFile(string fname, float *data, float *err, float *a if (ang==NULL) { ssstr >> ichan >> fdata; ich=ichan; - if (!ssstr.good()) { + if (ssstr.fail() || ssstr.bad()) { interrupt=1; break; } @@ -689,13 +689,13 @@ int mythenDetector::readDataFile(string fname, float *data, float *err, float *a ssstr >> fang >> fdata; ich=iline; } - if (!ssstr.good()) { + if (ssstr.fail() || ssstr.bad()) { interrupt=1; break; } if (err) ssstr >> ferr; - if (!ssstr.good()) { + if (ssstr.fail() || ssstr.bad()) { interrupt=1; break; } @@ -707,7 +707,7 @@ int mythenDetector::readDataFile(string fname, float *data, float *err, float *a if (err) err[ich]=ferr; iline++; - } else { + } else { std::cout<< " too many lines in file: "<< iline << " instead of " << maxchans << std::endl; interrupt=1; break; @@ -744,7 +744,7 @@ int mythenDetector::readDataFile(string fname, int *data){ #endif istringstream ssstr(str); ssstr >> ichan >> idata; - if (!ssstr.good()) { + if (ssstr.fail() || ssstr.bad()) { interrupt=1; break; } diff --git a/slsDetectorSoftware/mythenDetectorServer/firmware_funcs.c b/slsDetectorSoftware/mythenDetectorServer/firmware_funcs.c index a1badb391..2cc0e56a0 100755 --- a/slsDetectorSoftware/mythenDetectorServer/firmware_funcs.c +++ b/slsDetectorSoftware/mythenDetectorServer/firmware_funcs.c @@ -975,14 +975,13 @@ u_int32_t* fifo_read_event() #ifdef VERYVERBOSE printf("Copying to ptr %x %d\n",now_ptr, dataBytes); -#endif -#ifdef VERYVERBOSE printf("after readout %08x %08x\n", runState(), bus_r(LOOK_AT_ME_REG)); for (ichip=0; ichip0) { now_ptr+=dataBytes; } diff --git a/slsDetectorSoftware/mythenDetectorServer/server.c b/slsDetectorSoftware/mythenDetectorServer/server.c index bdba6b9cb..58e7962a0 100755 --- a/slsDetectorSoftware/mythenDetectorServer/server.c +++ b/slsDetectorSoftware/mythenDetectorServer/server.c @@ -5,7 +5,7 @@ #include -int sockfd; +extern int sockfd; void error(char *msg) @@ -18,6 +18,8 @@ int main(int argc, char *argv[]) int portno, b; char cmd[100]; int retval=OK; + int sd, fd; + if (argc==1) { portno = DEFAULT_PORTNO; @@ -37,8 +39,12 @@ int main(int argc, char *argv[]) init_detector(b); - bindSocket(portno); - if (getServerError()) { + sd=bindSocket(portno); + + sockfd=sd; + + + if (getServerError(sd)) { printf("server error!\n"); return -1; } @@ -58,21 +64,21 @@ int main(int argc, char *argv[]) #ifdef VERY_VERBOSE printf("Waiting for client call\n"); #endif - acceptConnection(); + fd=acceptConnection(sockfd); #ifdef VERY_VERBOSE printf("Conenction accepted\n"); #endif - retval=decode_function(); -#ifdef VERY_VERBOSE + retval=decode_function(fd); + //#ifdef VERY_VERBOSE printf("function executed\n"); -#endif - closeConnection(); + //#endif + closeConnection(fd); #ifdef VERY_VERBOSE printf("connection closed\n"); #endif } - exitServer(); + exitServer(sockfd); printf("Goodbye!\n"); return 0; diff --git a/slsDetectorSoftware/mythenDetectorServer/server_defs.h b/slsDetectorSoftware/mythenDetectorServer/server_defs.h index 8a35e6273..a9a058b16 100755 --- a/slsDetectorSoftware/mythenDetectorServer/server_defs.h +++ b/slsDetectorSoftware/mythenDetectorServer/server_defs.h @@ -38,5 +38,5 @@ #define CLK_FREQ 100E+6 -#define THIS_SOFTWARE_VERSION 0x20100429 +#define THIS_SOFTWARE_VERSION 0x20111125 #endif diff --git a/slsDetectorSoftware/mythenDetectorServer/server_funcs.c b/slsDetectorSoftware/mythenDetectorServer/server_funcs.c index daf9b29ae..f0ebdd68a 100755 --- a/slsDetectorSoftware/mythenDetectorServer/server_funcs.c +++ b/slsDetectorSoftware/mythenDetectorServer/server_funcs.c @@ -28,6 +28,10 @@ extern int dataBytes; extern int dynamicRange; extern int storeInRAM; +extern int lockStatus; +extern char lastClientIP[INET_ADDRSTRLEN]; +extern char thisClientIP[INET_ADDRSTRLEN]; + /* global variables for optimized readout */ extern int *ram_values; @@ -62,19 +66,22 @@ int init_detector( int b) { } #endif strcpy(mess,"dummy message"); + strcpy(lastClientIP,"none"); + strcpy(thisClientIP,"none1"); + lockStatus=0; return OK; } -int decode_function() { +int decode_function(int file_des) { int fnum,n; int retval=FAIL; #ifdef VERBOSE printf( "receive data\n"); #endif - n = receiveDataOnly(&fnum,sizeof(fnum)); + n = receiveDataOnly(file_des,&fnum,sizeof(fnum)); if (n <= 0) { - printf("ERROR reading from socket %d\n", n); + printf("ERROR reading from socket %d, %d %d\n", n, fnum, file_des); return FAIL; } #ifdef VERBOSE @@ -86,7 +93,7 @@ int decode_function() { #endif if (fnum<0 || fnum>255) fnum=255; - retval=(*flist[fnum])(fnum); + retval=(*flist[fnum])(file_des); if (retval==FAIL) printf( "Error executing the function = %d \n",fnum); return retval; @@ -134,6 +141,9 @@ int function_table() { flist[F_SET_SPEED]=&set_speed; flist[F_SET_READOUT_FLAGS]=&set_readout_flags; flist[F_EXECUTE_TRIMMING]=&execute_trimming; + flist[F_LOCK_SERVER]=&lock_server; + flist[F_SET_PORT]=&set_port; + flist[F_GET_LAST_CLIENT_IP]=&get_last_client_ip; #ifdef VERBOSE /* for (i=0;i<256;i++){ printf("function %d located at %x\n",i,flist[i]); @@ -143,27 +153,27 @@ int function_table() { } -int M_nofunc(int fnum){ +int M_nofunc(int file_des){ int retval=FAIL; - sprintf(mess,"Unrecognized Function %d\n",fnum); + sprintf(mess,"Unrecognized Function\n"); printf(mess); - sendDataOnly(&retval,sizeof(retval)); - sendDataOnly(mess,sizeof(mess)); + sendDataOnly(file_des,&retval,sizeof(retval)); + sendDataOnly(file_des,mess,sizeof(mess)); return GOODBYE; } -int exit_server(int fnum) { +int exit_server(int file_des) { int retval=FAIL; - sendDataOnly(&retval,sizeof(retval)); + sendDataOnly(file_des,&retval,sizeof(retval)); printf("closing server."); sprintf(mess,"closing server"); - sendDataOnly(mess,sizeof(mess)); + sendDataOnly(file_des,mess,sizeof(mess)); return GOODBYE; } -int exec_command(int fnum) { +int exec_command(int file_des) { char cmd[MAX_STR_LENGTH]; char answer[MAX_STR_LENGTH]; int retval=OK; @@ -171,7 +181,7 @@ int exec_command(int fnum) { int n=0; /* receive arguments */ - n = receiveDataOnly(cmd,MAX_STR_LENGTH); + n = receiveDataOnly(file_des,cmd,MAX_STR_LENGTH); if (n < 0) { sprintf(mess,"Error reading from socket\n"); retval=FAIL; @@ -195,7 +205,7 @@ int exec_command(int fnum) { } /* send answer */ - n = sendDataOnly(answer,MAX_STR_LENGTH); + n = sendDataOnly(file_des,answer,MAX_STR_LENGTH); if (n < 0) { sprintf(mess,"Error writing to socket"); retval=FAIL; @@ -209,7 +219,7 @@ int exec_command(int fnum) { -int get_detector_type(int fnum) { +int get_detector_type(int file_des) { int n=0; enum detectorType ret; int retval=OK; @@ -227,12 +237,12 @@ int get_detector_type(int fnum) { /* send answer */ /* send OK/failed */ - n += sendDataOnly(&retval,sizeof(retval)); + n += sendDataOnly(file_des,&retval,sizeof(retval)); if (retval==OK) { /* send return argument */ - n += sendDataOnly(&ret,sizeof(ret)); + n += sendDataOnly(file_des,&ret,sizeof(ret)); } else { - n += sendDataOnly(mess,sizeof(mess)); + n += sendDataOnly(file_des,mess,sizeof(mess)); } /*return ok/fail*/ return retval; @@ -241,7 +251,7 @@ int get_detector_type(int fnum) { } -int set_number_of_modules(int fnum) { +int set_number_of_modules(int file_des) { int n; int arg[2], ret=0; int retval=OK; @@ -250,7 +260,7 @@ int set_number_of_modules(int fnum) { sprintf(mess,"Can't set number of modules\n"); /* receive arguments */ - n = receiveDataOnly(&arg,sizeof(arg)); + n = receiveDataOnly(file_des,&arg,sizeof(arg)); if (n < 0) { sprintf(mess,"Error reading from socket %d", n); retval=GOODBYE; @@ -287,12 +297,12 @@ int set_number_of_modules(int fnum) { /* send answer */ /* send OK/failed */ - n = sendDataOnly(&retval,sizeof(retval)); + n = sendDataOnly(file_des,&retval,sizeof(retval)); if (retval==OK) { /* send return argument */ - n += sendDataOnly(&ret,sizeof(ret)); + n += sendDataOnly(file_des,&ret,sizeof(ret)); } else { - n += sendDataOnly(mess,sizeof(mess)); + n += sendDataOnly(file_des,mess,sizeof(mess)); } /*return ok/fail*/ return retval; @@ -300,7 +310,7 @@ int set_number_of_modules(int fnum) { } -int get_max_number_of_modules(int fnum) { +int get_max_number_of_modules(int file_des) { int n; int ret; int retval=OK; @@ -308,7 +318,7 @@ int get_max_number_of_modules(int fnum) { sprintf(mess,"Can't get max number of modules\n"); /* receive arguments */ - n = receiveDataOnly(&arg,sizeof(arg)); + n = receiveDataOnly(file_des,&arg,sizeof(arg)); if (n < 0) { sprintf(mess,"Error reading from socket\n"); retval=FAIL; @@ -339,12 +349,12 @@ int get_max_number_of_modules(int fnum) { /* send answer */ /* send OK/failed */ - n = sendDataOnly(&retval,sizeof(retval)); + n = sendDataOnly(file_des,&retval,sizeof(retval)); if (retval==OK) { /* send return argument */ - n += sendDataOnly(&ret,sizeof(ret)); + n += sendDataOnly(file_des,&ret,sizeof(ret)); } else { - n += sendDataOnly(mess,sizeof(mess)); + n += sendDataOnly(file_des,mess,sizeof(mess)); } @@ -359,7 +369,7 @@ int get_max_number_of_modules(int fnum) { //index 2 is out gate //index 3 is out trigger -int set_external_signal_flag(int fnum) { +int set_external_signal_flag(int file_des) { int n; int arg[2]; int ret=OK; @@ -369,7 +379,7 @@ int set_external_signal_flag(int fnum) { sprintf(mess,"Can't set external signal flag\n"); /* receive arguments */ - n = receiveDataOnly(&arg,sizeof(arg)); + n = receiveDataOnly(file_des,&arg,sizeof(arg)); if (n < 0) { sprintf(mess,"Error reading from socket\n"); ret=FAIL; @@ -401,12 +411,12 @@ int set_external_signal_flag(int fnum) { /* send answer */ /* send OK/failed */ - n = sendDataOnly(&ret,sizeof(ret)); + n = sendDataOnly(file_des,&ret,sizeof(ret)); if (ret==OK) { /* send return argument */ - n += sendDataOnly(&retval,sizeof(retval)); + n += sendDataOnly(file_des,&retval,sizeof(retval)); } else { - n += sendDataOnly(mess,sizeof(mess)); + n += sendDataOnly(file_des,mess,sizeof(mess)); } @@ -416,7 +426,7 @@ int set_external_signal_flag(int fnum) { } -int set_external_communication_mode(int fnum) { +int set_external_communication_mode(int file_des) { int n; enum externalCommunicationMode arg, ret; int retval=OK; @@ -425,7 +435,7 @@ int set_external_communication_mode(int fnum) { /* receive arguments */ - n = receiveDataOnly(&arg,sizeof(arg)); + n = receiveDataOnly(file_des,&arg,sizeof(arg)); if (n < 0) { sprintf(mess,"Error reading from socket\n"); retval=FAIL; @@ -462,12 +472,12 @@ enum externalCommunicationMode{ /* send answer */ /* send OK/failed */ - n = sendDataOnly(&retval,sizeof(retval)); + n = sendDataOnly(file_des,&retval,sizeof(retval)); if (retval==OK) { /* send return argument */ - n += sendDataOnly(&ret,sizeof(ret)); + n += sendDataOnly(file_des,&ret,sizeof(ret)); } else { - n += sendDataOnly(mess,sizeof(mess)); + n += sendDataOnly(file_des,mess,sizeof(mess)); } /*return ok/fail*/ @@ -478,7 +488,7 @@ enum externalCommunicationMode{ -int get_id(int fnum) { +int get_id(int file_des) { // sends back 64 bits! int64_t retval; int ret=OK; @@ -489,7 +499,7 @@ int get_id(int fnum) { sprintf(mess,"Can't return id\n"); /* receive arguments */ - n = receiveDataOnly(&arg,sizeof(arg)); + n = receiveDataOnly(file_des,&arg,sizeof(arg)); if (n < 0) { sprintf(mess,"Error reading from socket\n"); ret=FAIL; @@ -501,7 +511,7 @@ int get_id(int fnum) { switch (arg) { case MODULE_SERIAL_NUMBER: - n = receiveDataOnly(&imod,sizeof(imod)); + n = receiveDataOnly(file_des,&imod,sizeof(imod)); if (n < 0) { sprintf(mess,"Error reading from socket\n"); retval=FAIL; @@ -545,12 +555,12 @@ int get_id(int fnum) { /* send answer */ /* send OK/failed */ - n = sendDataOnly(&ret,sizeof(ret)); + n = sendDataOnly(file_des,&ret,sizeof(ret)); if (ret==OK) { /* send return argument */ - n += sendDataOnly(&retval,sizeof(retval)); + n += sendDataOnly(file_des,&retval,sizeof(retval)); } else { - n += sendDataOnly(mess,sizeof(mess)); + n += sendDataOnly(file_des,mess,sizeof(mess)); } /*return ok/fail*/ @@ -558,7 +568,7 @@ int get_id(int fnum) { } -int digital_test(int fnum) { +int digital_test(int file_des) { int retval; int ret=OK; @@ -570,7 +580,7 @@ int digital_test(int fnum) { sprintf(mess,"Can't send digital test\n"); - n = receiveDataOnly(&arg,sizeof(arg)); + n = receiveDataOnly(file_des,&arg,sizeof(arg)); if (n < 0) { sprintf(mess,"Error reading from socket\n"); ret=FAIL; @@ -582,7 +592,7 @@ int digital_test(int fnum) { switch (arg) { case CHIP_TEST: - n = receiveDataOnly(&imod,sizeof(imod)); + n = receiveDataOnly(file_des,&imod,sizeof(imod)); if (n < 0) { sprintf(mess,"Error reading from socket\n"); retval=FAIL; @@ -652,12 +662,12 @@ int digital_test(int fnum) { /* send answer */ /* send OK/failed */ - n = sendDataOnly(&ret,sizeof(ret)); + n = sendDataOnly(file_des,&ret,sizeof(ret)); if (ret==OK) { /* send return argument */ - n += sendDataOnly(&retval,sizeof(retval)); + n += sendDataOnly(file_des,&retval,sizeof(retval)); } else { - n += sendDataOnly(mess,sizeof(mess)); + n += sendDataOnly(file_des,mess,sizeof(mess)); } /*return ok/fail*/ @@ -665,7 +675,7 @@ int digital_test(int fnum) { } -int write_register(int fnum) { +int write_register(int file_des) { int retval; int ret=OK; @@ -676,7 +686,7 @@ int write_register(int fnum) { sprintf(mess,"Can't write to register\n"); - n = receiveDataOnly(arg,sizeof(arg)); + n = receiveDataOnly(file_des,arg,sizeof(arg)); if (n < 0) { sprintf(mess,"Error reading from socket\n"); ret=FAIL; @@ -704,12 +714,12 @@ int write_register(int fnum) { /* send answer */ /* send OK/failed */ - n = sendDataOnly(&ret,sizeof(ret)); + n = sendDataOnly(file_des,&ret,sizeof(ret)); if (ret==OK) { /* send return argument */ - n += sendDataOnly(&retval,sizeof(retval)); + n += sendDataOnly(file_des,&retval,sizeof(retval)); } else { - n += sendDataOnly(mess,sizeof(mess)); + n += sendDataOnly(file_des,mess,sizeof(mess)); } /*return ok/fail*/ @@ -717,7 +727,7 @@ int write_register(int fnum) { } -int read_register(int fnum) { +int read_register(int file_des) { int retval; int ret=OK; @@ -728,7 +738,7 @@ int read_register(int fnum) { sprintf(mess,"Can't read register\n"); - n = receiveDataOnly(&arg,sizeof(arg)); + n = receiveDataOnly(file_des,&arg,sizeof(arg)); if (n < 0) { sprintf(mess,"Error reading from socket\n"); ret=FAIL; @@ -755,12 +765,12 @@ int read_register(int fnum) { /* send answer */ /* send OK/failed */ - n = sendDataOnly(&ret,sizeof(ret)); + n = sendDataOnly(file_des,&ret,sizeof(ret)); if (ret==OK) { /* send return argument */ - n += sendDataOnly(&retval,sizeof(retval)); + n += sendDataOnly(file_des,&retval,sizeof(retval)); } else { - n += sendDataOnly(mess,sizeof(mess)); + n += sendDataOnly(file_des,mess,sizeof(mess)); } /*return ok/fail*/ @@ -768,7 +778,7 @@ int read_register(int fnum) { } -int set_dac(int fnum) { +int set_dac(int file_des) { float retval; int ret=OK; @@ -782,7 +792,7 @@ int set_dac(int fnum) { sprintf(mess,"Can't set DAC\n"); - n = receiveDataOnly(arg,sizeof(arg)); + n = receiveDataOnly(file_des,arg,sizeof(arg)); if (n < 0) { sprintf(mess,"Error reading from socket\n"); ret=FAIL; @@ -790,7 +800,7 @@ int set_dac(int fnum) { ind=arg[0]; imod=arg[1]; - n = receiveDataOnly(&val,sizeof(val)); + n = receiveDataOnly(file_des,&val,sizeof(val)); if (n < 0) { sprintf(mess,"Error reading from socket\n"); ret=FAIL; @@ -857,12 +867,12 @@ add possible potentiometers like in chiptest board!!!!!!!!!!!!!!! /* send answer */ /* send OK/failed */ - n = sendDataOnly(&ret,sizeof(ret)); + n = sendDataOnly(file_des,&ret,sizeof(ret)); if (ret==OK) { /* send return argument */ - n += sendDataOnly(&retval,sizeof(retval)); + n += sendDataOnly(file_des,&retval,sizeof(retval)); } else { - n += sendDataOnly(mess,sizeof(mess)); + n += sendDataOnly(file_des,mess,sizeof(mess)); } /* Maybe this is done inside the initialization funcs */ @@ -874,7 +884,7 @@ add possible potentiometers like in chiptest board!!!!!!!!!!!!!!! -int get_adc(int fnum) { +int get_adc(int file_des) { float retval; int ret=OK; @@ -887,7 +897,7 @@ int get_adc(int fnum) { sprintf(mess,"Can't read ADC\n"); - n = receiveDataOnly(arg,sizeof(arg)); + n = receiveDataOnly(file_des,arg,sizeof(arg)); if (n < 0) { sprintf(mess,"Error reading from socket\n"); ret=FAIL; @@ -943,12 +953,12 @@ int get_adc(int fnum) { /* send answer */ /* send OK/failed */ - n = sendDataOnly(&ret,sizeof(ret)); + n = sendDataOnly(file_des,&ret,sizeof(ret)); if (ret==OK) { /* send return argument */ - n += sendDataOnly(&retval,sizeof(retval)); + n += sendDataOnly(file_des,&retval,sizeof(retval)); } else { - n += sendDataOnly(mess,sizeof(mess)); + n += sendDataOnly(file_des,mess,sizeof(mess)); } /*return ok/fail*/ @@ -956,7 +966,7 @@ int get_adc(int fnum) { } -int set_channel(int fnum) { +int set_channel(int file_des) { int ret=OK; sls_detector_channel myChan; int retval; @@ -968,7 +978,7 @@ int set_channel(int fnum) { #ifdef VERBOSE printf("Setting channel\n"); #endif - ret=receiveChannel(&myChan); + ret=receiveChannel(file_des, &myChan); if (ret>=0) ret=OK; else @@ -1001,12 +1011,12 @@ int set_channel(int fnum) { /* send answer */ /* send OK/failed */ - n = sendDataOnly(&ret,sizeof(ret)); + n = sendDataOnly(file_des,&ret,sizeof(ret)); if (ret==OK) { /* send return argument */ - n += sendDataOnly(&retval,sizeof(retval)); + n += sendDataOnly(file_des,&retval,sizeof(retval)); } else { - n += sendDataOnly(mess,sizeof(mess)); + n += sendDataOnly(file_des,mess,sizeof(mess)); } @@ -1018,7 +1028,7 @@ int set_channel(int fnum) { -int get_channel(int fnum) { +int get_channel(int file_des) { int ret=OK; sls_detector_channel retval; @@ -1031,7 +1041,7 @@ int get_channel(int fnum) { - n = receiveDataOnly(arg,sizeof(arg)); + n = receiveDataOnly(file_des,arg,sizeof(arg)); if (n < 0) { sprintf(mess,"Error reading from socket\n"); ret=FAIL; @@ -1072,12 +1082,12 @@ int get_channel(int fnum) { /* send answer */ /* send OK/failed */ - n = sendDataOnly(&ret,sizeof(ret)); + n = sendDataOnly(file_des,&ret,sizeof(ret)); if (ret==OK) { /* send return argument */ - ret=sendChannel(&retval); + ret=sendChannel(file_des, &retval); } else { - n += sendDataOnly(mess,sizeof(mess)); + n += sendDataOnly(file_des,mess,sizeof(mess)); } @@ -1089,7 +1099,7 @@ int get_channel(int fnum) { } -int set_chip(int fnum) { +int set_chip(int file_des) { sls_detector_chip myChip; int ch[NCHAN]; @@ -1107,7 +1117,7 @@ int set_chip(int fnum) { #ifdef VERBOSE printf("Setting chip\n"); #endif - ret=receiveChip(&myChip); + ret=receiveChip(file_des, &myChip); #ifdef VERBOSE printf("Chip received\n"); #endif @@ -1135,19 +1145,19 @@ int set_chip(int fnum) { /* send answer */ /* send OK/failed */ - n = sendDataOnly(&ret,sizeof(ret)); + n = sendDataOnly(file_des,&ret,sizeof(ret)); if (ret==OK) { /* send return argument */ - n += sendDataOnly(&retval,sizeof(retval)); + n += sendDataOnly(file_des,&retval,sizeof(retval)); } else { - n += sendDataOnly(mess,sizeof(mess)); + n += sendDataOnly(file_des,mess,sizeof(mess)); } return ret; } -int get_chip(int fnum) { +int get_chip(int file_des) { int ret=OK; @@ -1158,7 +1168,7 @@ int get_chip(int fnum) { - n = receiveDataOnly(arg,sizeof(arg)); + n = receiveDataOnly(file_des,arg,sizeof(arg)); if (n < 0) { sprintf(mess,"Error reading from socket\n"); ret=FAIL; @@ -1193,12 +1203,12 @@ int get_chip(int fnum) { /* send answer */ /* send OK/failed */ - n = sendDataOnly(&ret,sizeof(ret)); + n = sendDataOnly(file_des,&ret,sizeof(ret)); if (ret==OK) { /* send return argument */ - ret=sendChip(&retval); + ret=sendChip(file_des, &retval); } else { - n += sendDataOnly(mess,sizeof(mess)); + n += sendDataOnly(file_des,mess,sizeof(mess)); } @@ -1208,7 +1218,7 @@ int get_chip(int fnum) { } -int set_module(int fnum) { +int set_module(int file_des) { sls_detector_module myModule; int *myChip=malloc(NCHIP*sizeof(int)); int *myChan=malloc(NCHIP*NCHAN*sizeof(int)); @@ -1254,7 +1264,7 @@ int set_module(int fnum) { #ifdef VERBOSE printf("Setting module\n"); #endif - ret=receiveModule(&myModule); + ret=receiveModule(file_des, &myModule); if (ret>=0) @@ -1287,12 +1297,12 @@ int set_module(int fnum) { /* send answer */ /* send OK/failed */ - n = sendDataOnly(&ret,sizeof(ret)); + n = sendDataOnly(file_des,&ret,sizeof(ret)); if (ret==OK) { /* send return argument */ - n += sendDataOnly(&retval,sizeof(retval)); + n += sendDataOnly(file_des,&retval,sizeof(retval)); } else { - n += sendDataOnly(mess,sizeof(mess)); + n += sendDataOnly(file_des,mess,sizeof(mess)); } free(myChip); free(myChan); @@ -1308,7 +1318,7 @@ int set_module(int fnum) { -int get_module(int fnum) { +int get_module(int file_des) { int ret=OK; @@ -1361,7 +1371,7 @@ int get_module(int fnum) { - n = receiveDataOnly(&arg,sizeof(arg)); + n = receiveDataOnly(file_des,&arg,sizeof(arg)); if (n < 0) { sprintf(mess,"Error reading from socket\n"); ret=FAIL; @@ -1384,12 +1394,12 @@ int get_module(int fnum) { } /* send answer */ /* send OK/failed */ - n = sendDataOnly(&ret,sizeof(ret)); + n = sendDataOnly(file_des,&ret,sizeof(ret)); if (ret==OK) { /* send return argument */ - ret=sendModule(&myModule); + ret=sendModule(file_des, &myModule); } else { - n += sendDataOnly(mess,sizeof(mess)); + n += sendDataOnly(file_des,mess,sizeof(mess)); } @@ -1410,14 +1420,14 @@ int get_module(int fnum) { return ret; } -int get_threshold_energy(int fnum) { +int get_threshold_energy(int file_des) { int retval; int ret=OK; int n; int imod; - n = receiveDataOnly(&imod,sizeof(imod)); + n = receiveDataOnly(file_des,&imod,sizeof(imod)); if (n < 0) { sprintf(mess,"Error reading from socket\n"); ret=FAIL; @@ -1439,11 +1449,11 @@ int get_threshold_energy(int fnum) { /* send answer */ - n = sendDataOnly(&ret,sizeof(ret)); + n = sendDataOnly(file_des,&ret,sizeof(ret)); if (ret!=OK) { - n += sendDataOnly(mess,sizeof(mess)); + n += sendDataOnly(file_des,mess,sizeof(mess)); } else - n += sendDataOnly(&retval,sizeof(retval)); + n += sendDataOnly(file_des,&retval,sizeof(retval)); /* Maybe this is done inside the initialization funcs */ @@ -1453,7 +1463,7 @@ int get_threshold_energy(int fnum) { } -int set_threshold_energy(int fnum) { +int set_threshold_energy(int file_des) { int retval; int ret=OK; int arg[3]; @@ -1462,7 +1472,7 @@ int set_threshold_energy(int fnum) { enum detectorSettings isett; - n = receiveDataOnly(&arg,sizeof(arg)); + n = receiveDataOnly(file_des,&arg,sizeof(arg)); if (n < 0) { sprintf(mess,"Error reading from socket\n"); ret=FAIL; @@ -1493,11 +1503,11 @@ int set_threshold_energy(int fnum) { /* send answer */ - n = sendDataOnly(&ret,sizeof(ret)); + n = sendDataOnly(file_des,&ret,sizeof(ret)); if (ret!=OK) { - n += sendDataOnly(mess,sizeof(mess)); + n += sendDataOnly(file_des,mess,sizeof(mess)); } else - n += sendDataOnly(&retval,sizeof(retval)); + n += sendDataOnly(file_des,&retval,sizeof(retval)); /* Maybe this is done inside the initialization funcs */ @@ -1507,7 +1517,7 @@ int set_threshold_energy(int fnum) { } -int set_settings(int fnum) { +int set_settings(int file_des) { int retval; int ret=OK; @@ -1517,7 +1527,7 @@ int set_settings(int fnum) { enum detectorSettings isett; - n = receiveDataOnly(&arg,sizeof(arg)); + n = receiveDataOnly(file_des,&arg,sizeof(arg)); if (n < 0) { sprintf(mess,"Error reading from socket\n"); ret=FAIL; @@ -1545,11 +1555,11 @@ int set_settings(int fnum) { } /* send answer */ - n = sendDataOnly(&ret,sizeof(ret)); + n = sendDataOnly(file_des,&ret,sizeof(ret)); if (ret!=OK) { - n += sendDataOnly(mess,sizeof(mess)); + n += sendDataOnly(file_des,mess,sizeof(mess)); } else - n += sendDataOnly(&retval,sizeof(retval)); + n += sendDataOnly(file_des,&retval,sizeof(retval)); @@ -1558,7 +1568,7 @@ int set_settings(int fnum) { } -int start_acquisition(int fnum) { +int start_acquisition(int file_des) { int ret=OK; int n; @@ -1574,15 +1584,15 @@ int start_acquisition(int fnum) { if (ret!=OK) sprintf(mess,"Start acquisition failed\n"); - n = sendDataOnly(&ret,sizeof(ret)); + n = sendDataOnly(file_des,&ret,sizeof(ret)); if (ret!=OK) { - n += sendDataOnly(mess,sizeof(mess)); + n += sendDataOnly(file_des,mess,sizeof(mess)); } return ret; } -int stop_acquisition(int fnum) { +int stop_acquisition(int file_des) { int ret=OK; int n; @@ -1598,16 +1608,16 @@ int stop_acquisition(int fnum) { if (ret!=OK) sprintf(mess,"Stop acquisition failed\n"); - n = sendDataOnly(&ret,sizeof(ret)); + n = sendDataOnly(file_des,&ret,sizeof(ret)); if (ret!=OK) { - n += sendDataOnly(mess,sizeof(mess)); + n += sendDataOnly(file_des,mess,sizeof(mess)); } return ret; } -int start_readout(int fnum) { +int start_readout(int file_des) { int ret=OK; @@ -1623,9 +1633,9 @@ int start_readout(int fnum) { if (ret!=OK) sprintf(mess,"Start readout failed\n"); - n = sendDataOnly(&ret,sizeof(ret)); + n = sendDataOnly(file_des,&ret,sizeof(ret)); if (ret!=OK) { - n += sendDataOnly(mess,sizeof(mess)); + n += sendDataOnly(file_des,mess,sizeof(mess)); } return ret; @@ -1633,7 +1643,7 @@ int start_readout(int fnum) { } -int get_run_status(int fnum) { +int get_run_status(int file_des) { int ret=OK; int n; @@ -1668,11 +1678,11 @@ int get_run_status(int fnum) { printf("get status failed\n"); } - n = sendDataOnly(&ret,sizeof(ret)); + n = sendDataOnly(file_des,&ret,sizeof(ret)); if (ret!=OK) { - n += sendDataOnly(mess,sizeof(mess)); + n += sendDataOnly(file_des,mess,sizeof(mess)); } else { - n += sendDataOnly(&s,sizeof(s)); + n += sendDataOnly(file_des,&s,sizeof(s)); } return ret; @@ -1680,7 +1690,7 @@ int get_run_status(int fnum) { } -int read_frame(int fnum) { +int read_frame(int file_des) { /* int *retval=NULL; char *ptr=NULL; @@ -1697,13 +1707,10 @@ int read_frame(int fnum) { #ifdef VERYVERBOSE printf("Sending ptr %x %d\n",dataretval, dataBytes); #endif - sendDataOnly(&dataret,sizeof(dataret)); + sendDataOnly(file_des,&dataret,sizeof(dataret)); + sendDataOnly(file_des,dataretval,dataBytes); #ifdef VERBOSE - n=sendDataOnly(dataretval,dataBytes); - printf("sent %d bytes\n",n); -#else - - sendDataOnly(dataretval,dataBytes); + printf("sent %d bytes\n",n); #endif return OK; } else { @@ -1720,8 +1727,8 @@ int read_frame(int fnum) { #ifdef VERYVERBOSE printf("%d %d %x %s\n",sizeof(mess),strlen(mess), mess,mess); #endif - sendDataOnly(&dataret,sizeof(dataret)); - sendDataOnly(mess,sizeof(mess));//sizeof(mess));//sizeof(mess)); + sendDataOnly(file_des,&dataret,sizeof(dataret)); + sendDataOnly(file_des,mess,sizeof(mess));//sizeof(mess));//sizeof(mess)); #ifdef VERYVERBOSE printf("message sent\n",mess); #endif @@ -1738,11 +1745,11 @@ int read_frame(int fnum) { printf("sending data of %d frames\n",nframes); #endif for (iframes=0; iframes-2) { @@ -1757,8 +1764,8 @@ int read_frame(int fnum) { #ifdef VERBOSE printf("Frames left %d\n",getFrames()); #endif - sendDataOnly(&dataret,sizeof(dataret)); - sendDataOnly(mess,sizeof(mess)); + sendDataOnly(file_des,&dataret,sizeof(dataret)); + sendDataOnly(file_des,mess,sizeof(mess)); return dataret; } @@ -1772,7 +1779,7 @@ int read_frame(int fnum) { -int read_all(int fnum) { +int read_all(int file_des) { while(read_frame(0)==OK) { @@ -1790,7 +1797,7 @@ int read_all(int fnum) { } -int start_and_read_all(int fnum) { +int start_and_read_all(int file_des) { // int ret=OK; #ifdef VERBOSE printf("Starting and reading all frames\n"); @@ -1800,8 +1807,8 @@ int start_and_read_all(int fnum) { /* ret=startStateMachine(); if (ret!=OK) { sprintf(mess,"could not start state machine\n"); - sendDataOnly(&ret,sizeof(ret)); - sendDataOnly(mess,sizeof(mess)); + sendDataOnly(file_des,&ret,sizeof(ret)); + sendDataOnly(file_des,mess,sizeof(mess)); #ifdef VERBOSE printf("could not start state machine\n"); @@ -1819,7 +1826,7 @@ int start_and_read_all(int fnum) { } -int set_timer(int fnum) { +int set_timer(int file_des) { enum timerIndex ind; int64_t tns; int n; @@ -1829,13 +1836,13 @@ int set_timer(int fnum) { sprintf(mess,"can't set timer\n"); - n = receiveDataOnly(&ind,sizeof(ind)); + n = receiveDataOnly(file_des,&ind,sizeof(ind)); if (n < 0) { sprintf(mess,"Error reading from socket\n"); ret=FAIL; } - n = receiveDataOnly(&tns,sizeof(tns)); + n = receiveDataOnly(file_des,&tns,sizeof(tns)); if (n < 0) { sprintf(mess,"Error reading from socket\n"); ret=FAIL; @@ -1893,19 +1900,19 @@ int set_timer(int fnum) { sprintf(mess, "could not allocate RAM for %lld frames\n", tns); } - n = sendDataOnly(&ret,sizeof(ret)); + n = sendDataOnly(file_des,&ret,sizeof(ret)); if (ret!=OK) { #ifdef VERBOSE printf("returning error\n"); #endif - n = sendDataOnly(mess,sizeof(mess)); + n = sendDataOnly(file_des,mess,sizeof(mess)); } else { #ifdef VERBOSE printf("returning ok %d\n",sizeof(retval)); #endif - n = sendDataOnly(&retval,sizeof(retval)); + n = sendDataOnly(file_des,&retval,sizeof(retval)); } return ret; @@ -1919,7 +1926,7 @@ int set_timer(int fnum) { -int get_time_left(int fnum) { +int get_time_left(int file_des) { enum timerIndex ind; int n; @@ -1927,7 +1934,7 @@ int get_time_left(int fnum) { int ret=OK; sprintf(mess,"can't get timer\n"); - n = receiveDataOnly(&ind,sizeof(ind)); + n = receiveDataOnly(file_des,&ind,sizeof(ind)); if (n < 0) { sprintf(mess,"Error reading from socket\n"); ret=FAIL; @@ -1987,11 +1994,11 @@ int get_time_left(int fnum) { printf("time left on timer %d is %lld\n",ind, retval); #endif - n = sendDataOnly(&ret,sizeof(ret)); + n = sendDataOnly(file_des,&ret,sizeof(ret)); if (ret!=OK) { - n += sendDataOnly(mess,sizeof(mess)); + n += sendDataOnly(file_des,mess,sizeof(mess)); } else { - n = sendDataOnly(&retval,sizeof(retval)); + n = sendDataOnly(file_des,&retval,sizeof(retval)); } #ifdef VERBOSE @@ -2003,7 +2010,7 @@ int get_time_left(int fnum) { } -int set_dynamic_range(int fnum) { +int set_dynamic_range(int file_des) { @@ -2016,7 +2023,7 @@ int set_dynamic_range(int fnum) { sprintf(mess,"can't set dynamic range\n"); - n = receiveDataOnly(&dr,sizeof(dr)); + n = receiveDataOnly(file_des,&dr,sizeof(dr)); if (n < 0) { sprintf(mess,"Error reading from socket\n"); ret=FAIL; @@ -2066,28 +2073,28 @@ int set_dynamic_range(int fnum) { sprintf(mess,"Could not allocate RAM for the dynamic range selected\n"); } - n = sendDataOnly(&ret,sizeof(ret)); + n = sendDataOnly(file_des,&ret,sizeof(ret)); if (ret!=OK) { - n = sendDataOnly(mess,sizeof(mess)); + n = sendDataOnly(file_des,mess,sizeof(mess)); } else { - n = sendDataOnly(&retval,sizeof(retval)); + n = sendDataOnly(file_des,&retval,sizeof(retval)); } return ret; } -int set_roi(int fnum) { +int set_roi(int file_des) { return FAIL; } -int get_roi(int fnum) { +int get_roi(int file_des) { return FAIL; } -int set_speed(int fnum) { +int set_speed(int file_des) { enum speedVariable arg; int val, n; @@ -2097,12 +2104,12 @@ int set_speed(int fnum) { sprintf(mess,"can't set speed variable\n"); - n = receiveDataOnly(&arg,sizeof(arg)); + n = receiveDataOnly(file_des,&arg,sizeof(arg)); if (n < 0) { sprintf(mess,"Error reading from socket\n"); ret=FAIL; } - n = receiveDataOnly(&val,sizeof(val)); + n = receiveDataOnly(file_des,&val,sizeof(val)); if (n < 0) { sprintf(mess,"Error reading from socket\n"); ret=FAIL; @@ -2147,18 +2154,18 @@ int set_speed(int fnum) { ret=FAIL; } } - n = sendDataOnly(&ret,sizeof(ret)); + n = sendDataOnly(file_des,&ret,sizeof(ret)); if (ret!=OK) { - n = sendDataOnly(mess,sizeof(mess)); + n = sendDataOnly(file_des,mess,sizeof(mess)); } else { - n = sendDataOnly(&retval,sizeof(retval)); + n = sendDataOnly(file_des,&retval,sizeof(retval)); } return ret; } -int set_readout_flags(int fnum) { +int set_readout_flags(int file_des) { enum readOutFlags retval; enum readOutFlags arg; @@ -2170,7 +2177,7 @@ int set_readout_flags(int fnum) { sprintf(mess,"can't set readout flags\n"); - n = receiveDataOnly(&arg,sizeof(arg)); + n = receiveDataOnly(file_des,&arg,sizeof(arg)); if (n < 0) { sprintf(mess,"Error reading from socket\n"); ret=FAIL; @@ -2228,11 +2235,11 @@ int set_readout_flags(int fnum) { printf("set readout flags failed\n"); sprintf(mess,"Could not allocate RAM\n"); } - n = sendDataOnly(&ret,sizeof(ret)); + n = sendDataOnly(file_des,&ret,sizeof(ret)); if (ret!=OK) { - n = sendDataOnly(mess,sizeof(mess)); + n = sendDataOnly(file_des,mess,sizeof(mess)); } else { - n = sendDataOnly(&retval,sizeof(retval)); + n = sendDataOnly(file_des,&retval,sizeof(retval)); } return ret; } @@ -2241,7 +2248,7 @@ int set_readout_flags(int fnum) { -int execute_trimming(int fnum) { +int execute_trimming(int file_des) { int arg[3]; int n; @@ -2251,14 +2258,14 @@ int execute_trimming(int fnum) { sprintf(mess,"can't set execute trimming\n"); - n = receiveDataOnly(&mode,sizeof(mode)); + n = receiveDataOnly(file_des,&mode,sizeof(mode)); if (n < 0) { sprintf(mess,"Error reading from socket\n"); printf("Error reading from socket (mode)\n"); ret=FAIL; } - n = receiveDataOnly(arg,sizeof(arg)); + n = receiveDataOnly(file_des,arg,sizeof(arg)); if (n < 0) { sprintf(mess,"Error reading from socket\n"); printf("Error reading from socket (args)\n"); @@ -2311,16 +2318,110 @@ int execute_trimming(int fnum) { if (ret!=OK) { printf("trimming failed\n"); } - n = sendDataOnly(&ret,sizeof(ret)); + n = sendDataOnly(file_des,&ret,sizeof(ret)); if (ret!=OK) { - n = sendDataOnly(mess,sizeof(mess)); + n = sendDataOnly(file_des,mess,sizeof(mess)); } return ret; } - - +int lock_server(int file_des) { + int n; + int ret=OK; + + int lock; + n = receiveDataOnly(file_des,&lock,sizeof(lock)); + if (n < 0) { + sprintf(mess,"Error reading from socket\n"); + printf("Error reading from socket (lock)\n"); + ret=FAIL; + } + if (lock>=0) { + if (lockStatus==0 || strcmp(lastClientIP,thisClientIP)==0 || strcmp(lastClientIP,"none")==0) + lockStatus=lock; + else { + ret=FAIL; + sprintf(mess,"Server already locked by %s\n", lastClientIP); + } + } + + + n = sendDataOnly(file_des,&ret,sizeof(ret)); + if (ret!=OK) { + n = sendDataOnly(file_des,mess,sizeof(mess)); + } else + n = sendDataOnly(file_des,&lockStatus,sizeof(lockStatus)); + + return ret; + +} + +int set_port(int file_des) { + int n; + int ret=OK; + int sd; + + enum portType p_type; /** data? control? stop? Unused! */ + int p_number; /** new port number */ + + n = receiveDataOnly(file_des,&p_type,sizeof(p_type)); + if (n < 0) { + sprintf(mess,"Error reading from socket\n"); + printf("Error reading from socket (ptype)\n"); + ret=FAIL; + } + + n = receiveDataOnly(file_des,&p_number,sizeof(p_number)); + if (n < 0) { + sprintf(mess,"Error reading from socket\n"); + printf("Error reading from socket (pnum)\n"); + ret=FAIL; + } + + if (p_number<1024) { + sprintf(mess,"Too low port number %d\n", p_number); + printf("\n"); + ret=FAIL; + } + + printf("set port %d to %d\n",p_type, p_number); + + sd=bindSocket(p_number); + + if (sd>=0) { + ret=OK; + } else { + ret=FAIL; + sprintf(mess,"Could not bind port %d\n", p_number); + printf("Could not bind port %d\n", p_number); + } + + n = sendDataOnly(file_des,&ret,sizeof(ret)); + if (ret!=OK) { + n = sendDataOnly(file_des,mess,sizeof(mess)); + } else { + n = sendDataOnly(file_des,&p_number,sizeof(p_number)); + closeConnection(file_des); + exitServer(sockfd); + sockfd=sd; + + } + + return ret; + +} + +int get_last_client_ip(int file_des) { + int ret=OK; + int n; + + n = sendDataOnly(file_des,&ret,sizeof(ret)); + n = sendDataOnly(file_des,lastClientIP,sizeof(lastClientIP)); + + return ret; + +} diff --git a/slsDetectorSoftware/mythenDetectorServer/server_funcs.h b/slsDetectorSoftware/mythenDetectorServer/server_funcs.h index ae1042597..bfc237da4 100755 --- a/slsDetectorSoftware/mythenDetectorServer/server_funcs.h +++ b/slsDetectorSoftware/mythenDetectorServer/server_funcs.h @@ -12,9 +12,12 @@ #define GOODBYE -200 + +int sockfd; + int function_table(); -int decode_function(); +int decode_function(int); int init_detector(int); @@ -64,7 +67,9 @@ int get_roi(int); int set_speed(int); int set_readout_flags(int); int execute_trimming(int); - +int lock_server(int); +int set_port(int); +int get_last_client_ip(int); diff --git a/slsDetectorSoftware/slsDetector/slsDetector.cpp b/slsDetectorSoftware/slsDetector/slsDetector.cpp index 0f1988edc..36f417ff0 100644 --- a/slsDetectorSoftware/slsDetector/slsDetector.cpp +++ b/slsDetectorSoftware/slsDetector/slsDetector.cpp @@ -37,11 +37,17 @@ int slsDetector::initSharedMemory(detectorType type, int id) { nc=10; nd=13; // dacs+adcs break; - default: + case EIGER: nch=65535; // one EIGER module nm=1; //modules/detector nc=8; //chips nd=16; //dacs+adcs + default: + nch=0; // one EIGER module + nm=0; //modules/detector + nc=0; //chips + nd=0; //dacs+adcs + } /** The size of the shared memory is: @@ -159,7 +165,6 @@ slsDetector::slsDetector(detectorType type, int id): - pthread_mutex_t mp1 = PTHREAD_MUTEX_INITIALIZER; mp=mp1; @@ -258,6 +263,33 @@ detectorType slsDetector::getDetectorType(char *name, int cport) { } +// detectorType slsDetector::getDetectorType(int id) { + +// detectorType t=GENERIC; +// initSharedMemory(GENERIC, id); +// sharedSlsDetector* det = (sharedSlsDetector*) shmat(shm_id, NULL, 0); +// if (det == (void*)-1) { +// std::cout<<"*** shmat error - detector id not found ***" << std::endl; +// return t; +// } + + +// return t; + +// } + + + + + + + + + + + + + detectorType slsDetector::getDetectorType(int id) { detectorType t=GENERIC; @@ -351,7 +383,7 @@ int slsDetector::initializeDetectorSize(detectorType type) { thisDetector->nChips=12; thisDetector->nDacs=6; thisDetector->nAdcs=0; - thisDetector->nModMax[X]=24; + thisDetector->nModMax[X]=6; thisDetector->nModMax[Y]=1; thisDetector->dynamicRange=24; break; @@ -497,6 +529,11 @@ int slsDetector::initializeDetectorSize(detectorType type) { } /** fill the BadChannelMask \sa fillBadChannelMask */ fillBadChannelMask(); + + + /** modifies the last PID accessing the detector */ + thisDetector->lastPID=getpid(); + return OK; } @@ -834,6 +871,46 @@ int slsDetector::setTCPSocket(string const name, int const control_port, int con }; + /** connect to the control port */ +int slsDetector::connectControl() { + if (controlSocket) + return controlSocket->Connect(); +} + /** disconnect from the control port */ +int slsDetector::disconnectControl() { + if (controlSocket) + controlSocket->Disconnect(); + return OK; +} + + + + /** connect to the data port */ + int slsDetector::connectData() { + if (dataSocket) + return dataSocket->Connect(); +}; + /** disconnect from the data port */ + int slsDetector::disconnectData(){ + if (dataSocket) + dataSocket->Disconnect(); + return OK; +} +; + + /** connect to the stop port */ + int slsDetector::connectStop() { + if (stopSocket) + return stopSocket->Connect(); +}; + /** disconnect from the stop port */ + int slsDetector::disconnectStop(){ + if (stopSocket) + stopSocket->Disconnect(); + return OK; +} +; + @@ -2917,6 +2994,36 @@ int* slsDetector::readAll(){ }; + + + + +int slsDetector::readAllNoWait(){ + + int fnum= F_READ_ALL; + +#ifdef VERBOSE + std::cout<< "Reading all frames "<< std::endl; +#endif + + if (thisDetector->onlineFlag==ONLINE_FLAG) { + if (controlSocket) { + if (controlSocket->Connect()>=0) { + controlSocket->SendDataOnly(&fnum,sizeof(fnum)); + return OK; + } + } + } + return FAIL; +}; + + + + + + + + int* slsDetector::startAndReadAll(){ @@ -2971,25 +3078,25 @@ int slsDetector::startAndReadAllNoWait(){ return FAIL; }; -int* slsDetector::getDataFromDetectorNoWait() { - int *retval=getDataFromDetector(); - if (thisDetector->onlineFlag==ONLINE_FLAG) { - if (controlSocket) { - if (retval==NULL){ - controlSocket->Disconnect(); +// int* slsDetector::getDataFromDetectorNoWait() { +// int *retval=getDataFromDetector(); +// if (thisDetector->onlineFlag==ONLINE_FLAG) { +// if (controlSocket) { +// if (retval==NULL){ +// controlSocket->Disconnect(); -#ifdef VERBOSE - std::cout<< "Run finished "<< std::endl; -#endif - } else { -#ifdef VERBOSE - std::cout<< "Frame received "<< std::endl; -#endif - } - } - } - return retval; // check what we return! -}; +// #ifdef VERBOSE +// std::cout<< "Run finished "<< std::endl; +// #endif +// } else { +// #ifdef VERBOSE +// std::cout<< "Frame received "<< std::endl; +// #endif +// } +// } +// } +// return retval; // check what we return! +// }; @@ -3105,6 +3212,195 @@ int64_t slsDetector::setTimer(timerIndex index, int64_t t){ }; +int slsDetector::lockServer(int lock) { + int fnum=F_LOCK_SERVER; + int retval=-1; + int ret=OK; + char mess[100]; + + if (thisDetector->onlineFlag==ONLINE_FLAG) { + if (controlSocket) { + if (controlSocket->Connect()>=0) { + controlSocket->SendDataOnly(&fnum,sizeof(fnum)); + controlSocket->SendDataOnly(&lock,sizeof(lock)); + controlSocket->ReceiveDataOnly(&ret,sizeof(ret)); + if (ret!=OK) { + controlSocket->ReceiveDataOnly(mess,sizeof(mess)); + std::cout<< "Detector returned error: " << mess << std::endl; + } else { + controlSocket->ReceiveDataOnly(&retval,sizeof(retval)); + } + controlSocket->Disconnect(); + } + } + } + + return retval; + + +} + + +string slsDetector::getLastClientIP() { + + int fnum=F_GET_LAST_CLIENT_IP; + char clientName[INET_ADDRSTRLEN]; + char mess[100]; + int ret=OK; + + if (thisDetector->onlineFlag==ONLINE_FLAG) { + if (controlSocket) { + if (controlSocket->Connect()>=0) { + controlSocket->SendDataOnly(&fnum,sizeof(fnum)); + controlSocket->ReceiveDataOnly(&ret,sizeof(ret)); + if (ret!=OK) { + controlSocket->ReceiveDataOnly(mess,sizeof(mess)); + std::cout<< "Detector returned error: " << mess << std::endl; + } else { + controlSocket->ReceiveDataOnly(clientName,sizeof(clientName)); + } + controlSocket->Disconnect(); + } + } + } + + return string(clientName); + +} + + +int slsDetector::setPort(portType index, int num){ + + + int fnum=F_SET_PORT; + int retval; + uint64_t ut; + char mess[100]; + int ret=OK; + int n=0; + + MySocketTCP *s; + + + switch(index) { + case CONTROL_PORT: + s=controlSocket; + retval=thisDetector->controlPort; + break; + case DATA_PORT: + s=dataSocket; + retval=thisDetector->dataPort; + break; + case STOP_PORT: + s=stopSocket; + retval=thisDetector->stopPort; + break; + default: + s=NULL; + } + + + if (thisDetector->onlineFlag==ONLINE_FLAG) { + if (s) { + if (s->Connect()>=0) { + s->SendDataOnly(&fnum,sizeof(fnum)); + s->SendDataOnly(&index,sizeof(index)); + n=s->SendDataOnly(&num,sizeof(num)); + s->ReceiveDataOnly(&ret,sizeof(ret)); + if (ret!=OK) { + s->ReceiveDataOnly(mess,sizeof(mess)); + std::cout<< "Detector returned error: " << mess << std::endl; + } else { + s->ReceiveDataOnly(&retval,sizeof(retval)); + } + s->Disconnect(); + } + } else { + + switch(index) { + case CONTROL_PORT: + thisDetector->controlPort=num; + break; + case DATA_PORT: + thisDetector->dataPort=num; + break; + case STOP_PORT: + thisDetector->stopPort=num; + break; + default: + ; + } + + return num; + } + } else { + + switch(index) { + case CONTROL_PORT: + thisDetector->controlPort=num; + break; + case DATA_PORT: + thisDetector->dataPort=num; + break; + case STOP_PORT: + thisDetector->stopPort=num; + break; + default: + ; + } + + return num; + } + + + + + if (ret==OK) { + + switch(index) { + case CONTROL_PORT: + thisDetector->controlPort=retval; + break; + case DATA_PORT: + thisDetector->dataPort=retval; + break; + case STOP_PORT: + thisDetector->stopPort=retval; + break; + default: + ; + } + + + + } + // setTCPSocket(); + + + + + + return retval; + +}; + + + + + + + + + + + + + + + + + + @@ -3634,6 +3930,35 @@ int slsDetector::setFlatFieldCorrection(string fname){ } return thisDetector->correctionMask&(1<nMod[X]*thisDetector->nMod[Y]*thisDetector->nChans*thisDetector->nChips; ichan++) { + ffcoefficients[ichan]=corr[ichan]; + if (ecorr!=NULL) + fferrors[ichan]=ecorr[ichan]; + else + fferrors[ichan]=1; + } + thisDetector->correctionMask|=(1<correctionMask&=~(1<correctionMask&(1<correctionMask&(1<badChanFile); + int ret=setBadChannelCorrection(fname, thisDetector->nBadChans, thisDetector->badChansList); + + if (ret) { + thisDetector->correctionMask|=(1<badChanFile,fname.c_str()); + } else + thisDetector->correctionMask&=~(1<correctionMask&(1<correctionMask&=~(1<nBadChans=0; + nbad=0; + return 0; } else { - if (fname=="default") - fname=string(thisDetector->badChanFile); infile.open(fname.c_str(), ios_base::in); if (infile.is_open()==0) { std::cout << "could not open file " << fname <nBadChans=0; + nbad=0; while (infile.good() and interrupt==0) { getline(infile,str); #ifdef VERBOSE @@ -3852,7 +4190,7 @@ int slsDetector::setBadChannelCorrection(string fname){ #endif istringstream ssstr; ssstr.str(str); - if (!ssstr.good() || infile.eof()) { + if (ssstr.bad() || ssstr.fail() || infile.eof()) { interrupt=1; break; } @@ -3864,11 +4202,11 @@ int slsDetector::setBadChannelCorrection(string fname){ std::cout << "channels between"<< chmin << " and " << chmax << std::endl; #endif for (ich=chmin; ich<=chmax; ich++) { - if (thisDetector->nBadChansbadChansList[thisDetector->nBadChans]=ich; - thisDetector->nBadChans++; + if (nbadnBadChans << " Found bad channel "<< ich << std::endl; + std::cout<< nbad << " Found bad channel "<< ich << std::endl; #endif } else interrupt=1; @@ -3878,11 +4216,11 @@ int slsDetector::setBadChannelCorrection(string fname){ #ifdef VERBOSE std::cout << "channel "<< ich << std::endl; #endif - if (thisDetector->nBadChansbadChansList[thisDetector->nBadChans]=ich; - thisDetector->nBadChans++; + if (nbadnBadChans << " Found bad channel "<< ich << std::endl; + std::cout << nbad << " Found bad channel "<< ich << std::endl; #endif } else interrupt=1; @@ -3890,20 +4228,39 @@ int slsDetector::setBadChannelCorrection(string fname){ } - if (thisDetector->nBadChans>0 && thisDetector->nBadChanscorrectionMask|=(1<< DISCARD_BAD_CHANNELS); - strcpy(thisDetector->badChanFile,fname.c_str()); - } } infile.close(); -#ifdef VERBOSE - std::cout << "found " << thisDetector->nBadChans << " badchannels "<< std::endl; -#endif + if (nbad>0 && nbadnBadChans=nch; + for (int ich=0 ;ichbadChansList[ich]=chs[ich]; + } + } + } else { + if (nchnBadFF=nch; + for (int ich=0 ;ichbadFFList[ich]=chs[ich]; + } + } + } fillBadChannelMask(); -#ifdef VERBOSE - std::cout << " badchannels mask filled"<< std::endl; -#endif - return thisDetector->nBadChans; + if (thisDetector->correctionMask&(1<< DISCARD_BAD_CHANNELS)) { + return thisDetector->nBadChans+thisDetector->nBadFF; + } else + return 0; + } int slsDetector::getBadChannelCorrection(int *bad) { @@ -4509,25 +4866,6 @@ string slsDetector::executeLine(int narg, char *args[], int action) { } strcpy(answer, getHostname()); return string(answer); - } else if (var=="port") { - if (action==PUT_ACTION) { - if (sscanf(args[1],"%d",&ival)) { - sval=""; - setTCPSocket(sval,ival); - } - } - sprintf(answer,"%d",getControlPort()); - return string(answer); - } else if (var=="stopport") { - if (action==PUT_ACTION) { - if (sscanf(args[1],"%d",&ival)) { - sval=""; - setTCPSocket(sval,-1,ival); - } - } - sprintf(answer,"%d",getStopPort()); - return string(answer); - } else if (var=="flatfield") { if (action==PUT_ACTION) { sval=string(args[1]); @@ -4981,6 +5319,55 @@ string slsDetector::executeLine(int narg, char *args[], int action) { if (setOnline()) setTCPSocket(); + + if (var=="port") { + if (action==PUT_ACTION) { + sscanf(args[1],"%d",&ival); + setPort(CONTROL_PORT, ival); + } else + ival=-1; + sprintf(answer,"%d",getControlPort()); + return string(answer); + } else if (var=="stopport") { + if (action==PUT_ACTION) { + sscanf(args[1],"%d",&ival); + setPort(STOP_PORT, ival); + } else + ival=-1; + sprintf(answer,"%d",getStopPort()); + return string(answer); + + } else if (var=="dataport") { + if (action==PUT_ACTION) { + sscanf(args[1],"%d",&ival); + setPort(DATA_PORT, ival); + } else + ival=-1; + sprintf(answer,"%d",getDataPort()); + return string(answer); + + } else if (var=="lock") { + if (action==PUT_ACTION) { + sscanf(args[1],"%d",&ival); + } else + ival=-1; + sprintf(answer,"%d",lockServer(ival)); + return string(answer); + } else if (var=="lastclient") { + if (action==PUT_ACTION) { + return string("cannot set"); + } else + return getLastClientIP(); + } + + + + + + + + + if (var=="nmod") { if (action==PUT_ACTION) { sscanf(args[1],"%d",&ival); diff --git a/slsDetectorSoftware/slsDetector/slsDetector.h b/slsDetectorSoftware/slsDetector/slsDetector.h index f4be796e8..8a069dacb 100644 --- a/slsDetectorSoftware/slsDetector/slsDetector.h +++ b/slsDetectorSoftware/slsDetector/slsDetector.h @@ -21,7 +21,6 @@ extern "C" { #include #include - #include "sls_detector_defs.h" #define MAX_TIMERS 10 @@ -140,6 +139,17 @@ class slsDetector { typedef struct sharedSlsDetector { /** already existing flag. If the detector does not yet exist (alreadyExisting=0) the sharedMemory will be created, otherwise it will simly be linked */ int alreadyExisting; + + + + + /** last process id accessing the shared memory */ + + pid_t lastPID; + + + + /** online flag - is set if the detector is connected, unset if socket connection is not possible */ int onlineFlag; @@ -321,6 +331,14 @@ typedef struct sharedSlsDetector { */ slsDetector(detectorType type=GENERIC, int id=0); +/** constructor + + \param id is the detector index which is needed to define the shared memory id. Different physical detectors should have different IDs in order to work independently + + +*/ + slsDetector(int id){slsDetector(getDetectorType(id),id);}; + slsDetector(char *name, int id=0, int cport=DEFAULT_PORTNO); //slsDetector(string const fname); @@ -382,6 +400,26 @@ typedef struct sharedSlsDetector { \sa sharedSlsDetector */ int setTCPSocket(string const name="", int const control_port=-1, int const stop_port=-1, int const data_port=-1); + + /** + changes/gets the port number + \param type port type + \param num new port number (-1 gets) + \returns actual port number + */ + int setPort(portType type, int num=-1); + + /** Locks/Unlocks the connection to the server + /param lock sets (1), usets (0), gets (-1) the lock + /returns lock status of the server + */ + int lockServer(int lock=-1); + + /** + Returns the IP of the last client connecting to the detector + */ + string getLastClientIP(); + /** returns the detector hostname \sa sharedSlsDetector */ char* getHostname() {return thisDetector->hostname;}; /** returns the detector control port \sa sharedSlsDetector */ @@ -391,6 +429,21 @@ typedef struct sharedSlsDetector { /** returns the detector data port \sa sharedSlsDetector */ int getDataPort() {return thisDetector->dataPort;}; + /** connect to the control port */ + int connectControl(); + /** disconnect from the control port */ + int disconnectControl(); + + /** connect to the data port */ + int connectData(); + /** disconnect from the data port */ + int disconnectData(); + + /** connect to the stop port */ + int connectStop(); + /** disconnect from the stop port */ + int disconnectStop(); + /* I/O */ /** returns the detector trimbit/settings directory \sa sharedSlsDetector */ @@ -649,12 +702,15 @@ typedef struct sharedSlsDetector { */ int writeAngularConversion(string fname=""); - /** Returns the number of channels per chip */ + /** Returns the number of channels per chip (without connecting to the detector) */ int getNChans(){return thisDetector->nChans;}; // - /** Returns the number of chips per module */ + /** Returns the number of chips per module (without connecting to the detector) */ int getNChips(){return thisDetector->nChips;}; // + /** Returns the number of modules (without connecting to the detector) */ + int getNMods(){return thisDetector->nMods;}; // + /* Communication to server */ @@ -994,17 +1050,18 @@ typedef struct sharedSlsDetector { int* startAndReadAll(); /** - start detector acquisition and read out, but does not read data from socket + start detector acquisition and read out, but does not read data from socket leaving socket opened + \returns OK or FAIL */ int startAndReadAllNoWait(); - /** - receives a data frame from the detector socket - \returns pointer to the data or NULL. If NULL disconnects the socket - \sa getDataFromDetector - */ - int* getDataFromDetectorNoWait(); +/* /\** */ +/* receives a data frame from the detector socket */ +/* \returns pointer to the data or NULL. If NULL disconnects the socket */ +/* \sa getDataFromDetector */ +/* *\/ */ +/* int* getDataFromDetectorNoWait(); */ /** asks and receives a data frame from the detector and puts it in the data queue @@ -1020,6 +1077,12 @@ typedef struct sharedSlsDetector { */ int* readAll(); + /** + asks and receives all data from the detector and leaves the socket opened + \returns OK or FAIL + */ + int readAllNoWait(); + /** pops the data from the data queue @@ -1157,6 +1220,14 @@ s */ int getFlatFieldCorrection(float *corr=NULL, float *ecorr=NULL); + /** + set flat field corrections + \param corr if !=NULL the flat field corrections will be filled with corr (NULL usets ff corrections) + \param ecorr if !=NULL the flat field correction errors will be filled with ecorr (1 otherwise) + \returns 0 if ff correction disabled, >0 otherwise + */ + int setFlatFieldCorrection(float *corr=NULL, float *ecorr=NULL); + /** get flat field corrections file directory \returns flat field correction file directory @@ -1200,6 +1271,15 @@ s \returns 0 if rate correction disabled, >0 otherwise */ int getRateCorrection(); + + /** + set bad channels correction + \param fname file with bad channel list ("" disable) + \param nbad reference to number of bad channels + \param badlist array of badchannels + \returns 0 if bad channel disabled, >0 otherwise + */ + static int setBadChannelCorrection(string fname, int &nbad, int *badlist); /** set bad channels correction @@ -1208,6 +1288,18 @@ s */ int setBadChannelCorrection(string fname=""); + /** + set bad channels correction + \param nch number of bad channels + \param chs array of channels + \param ff 0 if normal bad channels, 1 if ff bad channels + \returns 0 if bad channel disabled, >0 otherwise + */ + int setBadChannelCorrection(int nch, int *chs, int ff=0); + + + + /** get bad channels correction \param bad pointer to array that if bad!=NULL will be filled with the bad channel list @@ -1430,7 +1522,7 @@ s \param fferr erro on ffcoefficient \returns 0 */ - int flatFieldCorrect(float datain, float errin, float &dataout, float &errout, float ffcoefficient, float fferr); + static int flatFieldCorrect(float datain, float errin, float &dataout, float &errout, float ffcoefficient, float fferr); /** flat field correct data @@ -1454,7 +1546,7 @@ s \param t acquisition time (in ns) \returns 0 */ - int rateCorrect(float datain, float errin, float &dataout, float &errout, float tau, float t); + static int rateCorrect(float datain, float errin, float &dataout, float &errout, float tau, float t); /** rate correct data @@ -1581,23 +1673,34 @@ enum {GET_ACTION, PUT_ACTION, READOUT_ACTION}; \param action can be PUT_ACTION or GET_ACTION (from text client even READOUT_ACTION for acquisition) */ static detectorType getDetectorType(char *name, int cport=DEFAULT_PORTNO); - + /** returns the detector type from hostname and controlport \param \param action can be PUT_ACTION or GET_ACTION (from text client even READOUT_ACTION for acquisition) */ static detectorType getDetectorType(int id); + + /** + Returns detector id + \return pointer to the data (or NULL if failed) + */ int getDetectorId() { return detId;}; + /** + Receives a data frame from the detector socket + \return pointer to the data (or NULL if failed) + + */ + int* getDataFromDetector(); protected: - static const int64_t thisSoftwareVersion=0x20110113; + static const int64_t thisSoftwareVersion=0x20111124; /** address of the detector structure in shared memory @@ -1707,12 +1810,6 @@ enum {GET_ACTION, PUT_ACTION, READOUT_ACTION}; /** pointer to bad channel mask 0 is channel is good 1 if it is bad \sa fillBadChannelMask() */ int *badChannelMask; - /** - Receives a data frame from the detector socket - \return pointer to the data (or NULL if failed) - - */ - int* getDataFromDetector(); /** Initializes the shared memory \param type is needed to define the size of the shared memory