/******************************************************************* Date: $Date$ Revision: $Rev$ Author: $Author$ URL: $URL$ ID: $Id$ ********************************************************************/ #include "multiSlsDetector.h" #include "slsDetector.h" #include "multiSlsDetectorCommand.h" #include "multiSlsDetectorClient.h" #include "postProcessingFuncs.h" #include "usersFunctions.h" #include "ThreadPool.h" #include "ZmqSocket.h" #include #include #include #include #include using namespace std; char ans[MAX_STR_LENGTH]; int multiSlsDetector::freeSharedMemory() { // Detach Memory address for (int id=0; idnumberOfDetectors; ++id) { if (detectors[id]) detectors[id]->freeSharedMemory(); } if (shmdt(thisMultiDetector) == -1) { perror("shmdt failed\n"); return FAIL; } #ifdef VERBOSE printf("Shared memory %d detached\n", shmId); #endif // remove shared memory if (shmctl(shmId, IPC_RMID, 0) == -1) { perror("shmctl(IPC_RMID) failed\n"); return FAIL; } printf("Shared memory %d deleted\n", shmId); return OK; } int multiSlsDetector::initSharedMemory(int id=0) { key_t mem_key=DEFAULT_SHM_KEY+MAXDET+id; int shm_id; int sz; sz=sizeof(sharedMultiSlsDetector); #ifdef VERBOSE std::cout<<"multiSlsDetector: Size of shared memory is "<< sz << " - id " << mem_key << std::endl; #endif shm_id = shmget(mem_key,sz,IPC_CREAT | 0666); // allocate shared memory if (shm_id < 0) { std::cout<<"*** shmget error (server) ***"<< shm_id << std::endl; return shm_id; } /** thisMultiDetector pointer is set to the memory address of the shared memory */ thisMultiDetector = (sharedMultiSlsDetector*) shmat(shm_id, NULL, 0); /* attach */ if (thisMultiDetector == (void*)-1) { std::cout<<"*** shmat error (server) ***" << std::endl; return shm_id; } /** shm_id returns -1 is shared memory initialization fails */ return shm_id; } multiSlsDetector::multiSlsDetector(int id) : slsDetectorUtils(), shmId(-1) { while (shmId<0) { shmId=initSharedMemory(id); ++id; } --id; for (int id=0; idalreadyExisting==0) { thisMultiDetector->onlineFlag = ONLINE_FLAG; thisMultiDetector->receiverOnlineFlag = OFFLINE_FLAG; thisMultiDetector->numberOfDetectors=0; for (int id=0; iddetectorIds[id]=-1; thisMultiDetector->offsetX[id]=0; thisMultiDetector->offsetY[id]=0; } thisMultiDetector->masterPosition=-1; thisMultiDetector->dataBytes=0; thisMultiDetector->numberOfChannels=0; thisMultiDetector->numberOfChannel[X]=0; thisMultiDetector->numberOfChannel[Y]=0; thisMultiDetector->maxNumberOfChannels=0; thisMultiDetector->maxNumberOfChannel[X]=0; thisMultiDetector->maxNumberOfChannel[Y]=0; thisMultiDetector->maxNumberOfChannelsPerDetector[X]=-1; thisMultiDetector->maxNumberOfChannelsPerDetector[Y]=-1; /** set trimDsdir, calDir and filePath to default to root directory*/ strcpy(thisMultiDetector->filePath,"/"); /** set fileName to default to run*/ strcpy(thisMultiDetector->fileName,"run"); /** set fileIndex to default to 0*/ thisMultiDetector->fileIndex=0; /** set frames per file to default to 1*/ thisMultiDetector->framesPerFile=1; /** set fileFormat to default to ascii*/ thisMultiDetector->fileFormatType=ASCII; /** set progress Index to default to 0*/ thisMultiDetector->progressIndex=0; /** set total number of frames to be acquired to default to 1*/ thisMultiDetector->totalProgress=1; /** set correction mask to 0*/ thisMultiDetector->correctionMask=1<correctionMask|=(1<tDead=0; /** sets bad channel list file to none */ strcpy(thisMultiDetector->badChanFile,"none"); /** sets flat field correction directory */ strcpy(thisMultiDetector->flatFieldDir,getenv("HOME")); /** sets flat field correction file */ strcpy(thisMultiDetector->flatFieldFile,"none"); /** set angular direction to 1*/ thisMultiDetector->angDirection=1; /** set fine offset to 0*/ thisMultiDetector->fineOffset=0; /** set global offset to 0*/ thisMultiDetector->globalOffset=0; /** set threshold to -1*/ thisMultiDetector->currentThresholdEV=-1; // /** set clockdivider to 1*/ // thisMultiDetector->clkDiv=1; /** set number of positions to 0*/ thisMultiDetector->numberOfPositions=0; /** sets angular conversion file to none */ strcpy(thisMultiDetector->angConvFile,"none"); /** set binsize*/ thisMultiDetector->binSize=0.001; thisMultiDetector->stoppedFlag=0; thisMultiDetector->threadedProcessing=1; thisMultiDetector->actionMask=0; for (int ia=0; iaactionMode[ia]=0; strcpy(thisMultiDetector->actionScript[ia],"none"); strcpy(thisMultiDetector->actionParameter[ia],"none"); } for (int iscan=0; iscanscanMode[iscan]=0; strcpy(thisMultiDetector->scanScript[iscan],"none"); strcpy(thisMultiDetector->scanParameter[iscan],"none"); thisMultiDetector->nScanSteps[iscan]=0; thisMultiDetector->scanPrecision[iscan]=0; } thisMultiDetector->acquiringFlag = false; thisMultiDetector->receiver_upstream = false; thisMultiDetector->alreadyExisting=1; } //assigned before creating detector stoppedFlag=&thisMultiDetector->stoppedFlag; threadedProcessing=&thisMultiDetector->threadedProcessing; actionMask=&thisMultiDetector->actionMask; actionScript=thisMultiDetector->actionScript; actionParameter=thisMultiDetector->actionParameter; nScanSteps=thisMultiDetector->nScanSteps; scanMode=thisMultiDetector->scanMode; scanScript=thisMultiDetector->scanScript; scanParameter=thisMultiDetector->scanParameter; scanSteps=thisMultiDetector->scanSteps; scanPrecision=thisMultiDetector->scanPrecision; numberOfPositions=&thisMultiDetector->numberOfPositions; detPositions=thisMultiDetector->detPositions; angConvFile=thisMultiDetector->angConvFile; correctionMask=&thisMultiDetector->correctionMask; binSize=&thisMultiDetector->binSize; fineOffset=&thisMultiDetector->fineOffset; globalOffset=&thisMultiDetector->globalOffset; angDirection=&thisMultiDetector->angDirection; flatFieldDir=thisMultiDetector->flatFieldDir; flatFieldFile=thisMultiDetector->flatFieldFile; badChanFile=thisMultiDetector->badChanFile; timerValue=thisMultiDetector->timerValue; expTime=&timerValue[ACQUISITION_TIME]; currentSettings=&thisMultiDetector->currentSettings; currentThresholdEV=&thisMultiDetector->currentThresholdEV; moveFlag=NULL; sampleDisplacement=thisMultiDetector->sampleDisplacement; filePath=thisMultiDetector->filePath; fileName=thisMultiDetector->fileName; fileIndex=&thisMultiDetector->fileIndex; framesPerFile=&thisMultiDetector->framesPerFile; fileFormatType=&thisMultiDetector->fileFormatType; for (int i=0; inumberOfDetectors; ++i) { #ifdef VERBOSE cout << thisMultiDetector->detectorIds[i] << endl; #endif detectors[i]=new slsDetector(i, thisMultiDetector->detectorIds[i], this); // setAngularConversionPointer(detectors[i]->getAngularConversionPointer(),detectors[i]->getNModsPointer(),detectors[i]->getNChans()*detectors[i]->getNChips(), i); } // for (int i=thisMultiDetector->numberOfDetectors; ilastPID=getpid(); getNMods(); getMaxMods(); client_downstream = false; for(int i=0;inumberOfDetectors; if(numthreads < 1){ numthreads = 1; //create threadpool anyway, threads initialized only when >1 detector added } threadpool = new ThreadPool(numthreads); switch(threadpool->initialize_threadpool()){ case 0: cerr << "Failed to initialize thread pool!" << endl; return FAIL; case 1: #ifdef VERBOSE cout << "Not initializing threads, not multi detector" << endl; #endif break; default: #ifdef VERBOSE cout << "Initialized Threadpool " << threadpool << endl; #endif break; } return OK; } void multiSlsDetector::destroyThreadPool(){ if(threadpool){ delete threadpool; threadpool=0; #ifdef VERBOSE cout<<"Destroyed Threadpool "<< threadpool << endl; #endif } } int multiSlsDetector::addSlsDetector(int id, int pos) { int j=thisMultiDetector->numberOfDetectors; if (slsDetector::exists(id)==0) { cout << "Detector " << id << " does not exist - You should first create it to determine type etc." << endl; } #ifdef VERBOSE cout << "Adding detector " << id << " in position " << pos << endl; #endif if (pos<0) pos=j; if (pos>j) pos=thisMultiDetector->numberOfDetectors; //check that it is not already in the list for (int i=0; inumberOfDetectors; ++i) { //check that it is not already in the list, in that case move to new position if (detectors[i]) { if (detectors[i]->getDetectorId()==id) { cout << "Detector " << id << "already part of the multiDetector in position " << i << "!" << endl << "Remove it before adding it back in a new position!"<< endl; return -1; } } } if (pos!=thisMultiDetector->numberOfDetectors) { for (int ip=thisMultiDetector->numberOfDetectors-1; ip>=pos; --ip) { #ifdef VERBOSE cout << "Moving detector " << thisMultiDetector->detectorIds[ip] << " from position " << ip << " to " << ip+1 << endl; #endif thisMultiDetector->detectorIds[ip+1]=thisMultiDetector->detectorIds[ip]; detectors[ip+1]=detectors[ip]; } } #ifdef VERBOSE cout << "Creating new detector " << pos << endl; #endif detectors[pos]=new slsDetector(pos, id, this); thisMultiDetector->detectorIds[pos]=detectors[pos]->getDetectorId(); ++thisMultiDetector->numberOfDetectors; thisMultiDetector->dataBytes+=detectors[pos]->getDataBytes(); thisMultiDetector->numberOfChannels+=detectors[pos]->getTotalNumberOfChannels(); thisMultiDetector->maxNumberOfChannels+=detectors[pos]->getMaxNumberOfChannels(); getMaxMods(); getNMods(); getMaxMod(X); getNMod(X); getMaxMod(Y); getNMod(Y); #ifdef VERBOSE cout << "Detector added " << thisMultiDetector->numberOfDetectors<< endl; for (int ip=0; ipnumberOfDetectors; ++ip) { cout << "Detector " << thisMultiDetector->detectorIds[ip] << " position " << ip << " " << detectors[ip]->getHostname() << endl; } #endif //set offsets updateOffsets(); if(createThreadPool() == FAIL) exit(-1); return thisMultiDetector->numberOfDetectors; } void multiSlsDetector::updateOffsets(){//cannot paralllize due to slsdetector calling this via parentdet-> #ifdef VERBOSE cout << endl << "Updating Multi-Detector Offsets" << endl; #endif int offsetX=0, offsetY=0, numX=0, numY=0, maxX=0, maxY=0; int maxChanX = thisMultiDetector->maxNumberOfChannelsPerDetector[X]; int maxChanY = thisMultiDetector->maxNumberOfChannelsPerDetector[Y]; int prevChanX=0; int prevChanY=0; bool firstTime = true; thisMultiDetector->numberOfChannel[X] = 0; thisMultiDetector->numberOfChannel[Y] = 0; thisMultiDetector->maxNumberOfChannel[X] = 0; thisMultiDetector->maxNumberOfChannel[Y] = 0; for (int i=0; inumberOfDetectors; ++i) { if (detectors[i]) { #ifdef VERBOSE cout<<"offsetX:"<getTotalNumberOfChannels(Y) <<" maxChanY:"< 0) && ((offsetX + detectors[i]->getTotalNumberOfChannels(X)) > maxChanX)) cout<<"\nDetector[" << i << "] exceeds maximum channels allowed for complete detector set in X dimension!" << endl; if ((maxChanY > 0) && ((offsetY + detectors[i]->getTotalNumberOfChannels(Y)) > maxChanY)) cout<<"\nDetector[" << i << "] exceeds maximum channels allowed for complete detector set in Y dimension!" << endl; prevChanX = detectors[i]->getTotalNumberOfChannels(X); prevChanY = detectors[i]->getTotalNumberOfChannels(Y); numX += detectors[i]->getTotalNumberOfChannels(X); numY += detectors[i]->getTotalNumberOfChannels(Y); maxX += detectors[i]->getMaxNumberOfChannels(X); maxY += detectors[i]->getMaxNumberOfChannels(Y); #ifdef VERBOSE cout<<"incrementing in both direction"< 0) && ((offsetY + prevChanY + detectors[i]->getTotalNumberOfChannels(Y)) <= maxChanY))){ offsetY += prevChanY; prevChanY = detectors[i]->getTotalNumberOfChannels(Y); numY += detectors[i]->getTotalNumberOfChannels(Y); maxY += detectors[i]->getMaxNumberOfChannels(Y); #ifdef VERBOSE cout<<"incrementing in y direction"< 0) && ((offsetX + prevChanX + detectors[i]->getTotalNumberOfChannels(X)) > maxChanX)) cout<<"\nDetector[" << i << "] exceeds maximum channels allowed for complete detector set in X dimension!" << endl; offsetY = 0; prevChanY = detectors[i]->getTotalNumberOfChannels(Y);; numY = 0; //assuming symmetry with this statement. whats on 1st column should be on 2nd column maxY = 0; offsetX += prevChanX; prevChanX = detectors[i]->getTotalNumberOfChannels(X); numX += detectors[i]->getTotalNumberOfChannels(X); maxX += detectors[i]->getMaxNumberOfChannels(X); #ifdef VERBOSE cout<<"incrementing in x direction"<offsetX[i] = offsetX; thisMultiDetector->offsetY[i] = offsetY; #ifdef VERBOSE cout << "Detector[" << i << "] has offsets (" << thisMultiDetector->offsetX[i] << ", " << thisMultiDetector->offsetY[i] << ")" << endl; #endif //offsetY has been reset sometimes and offsetX the first time, but remember the highest values if(numX > thisMultiDetector->numberOfChannel[X]) thisMultiDetector->numberOfChannel[X] = numX; if(numY > thisMultiDetector->numberOfChannel[Y]) thisMultiDetector->numberOfChannel[Y] = numY; if(maxX > thisMultiDetector->maxNumberOfChannel[X]) thisMultiDetector->maxNumberOfChannel[X] = maxX; if(maxY > thisMultiDetector->maxNumberOfChannel[Y]) thisMultiDetector->maxNumberOfChannel[Y] = maxY; } } #ifdef VERBOSE cout << "Number of Channels in X direction:" << thisMultiDetector->numberOfChannel[X] << endl; cout << "Number of Channels in Y direction:" << thisMultiDetector->numberOfChannel[Y] << endl << endl; #endif } string multiSlsDetector::setHostname(const char* name, int pos){ // int id=0; string s; if (pos>=0) { addSlsDetector(name, pos); if (detectors[pos]) return detectors[pos]->getHostname(); } else { size_t p1=0; s=string(name); size_t p2=s.find('+',p1); char hn[1000]; if (p2==string::npos) { strcpy(hn,s.c_str()); addSlsDetector(hn, pos); } else { while (p2!=string::npos) { strcpy(hn,s.substr(p1,p2-p1).c_str()); addSlsDetector(hn, pos); s=s.substr(p2+1); p2=s.find('+'); } } } #ifdef VERBOSE cout << "-----------------------------set online!" << endl; #endif setOnline(ONLINE_FLAG); return getHostname(pos); } string multiSlsDetector::ssetDetectorsType(string name, int pos) { // int id=0; string s; if (pos>=0) { if (getDetectorType(name)!=GET_DETECTOR_TYPE) addSlsDetector(name.c_str(), pos); } else { removeSlsDetector(); //reset detector list! size_t p1=0; s=string(name); size_t p2=s.find('+',p1); char hn[1000]; if (p2==string::npos) { strcpy(hn,s.c_str()); addSlsDetector(hn, pos); } else { while (p2!=string::npos) { strcpy(hn,s.substr(p1,p2-p1).c_str()); if (getDetectorType(hn)!=GET_DETECTOR_TYPE) addSlsDetector(hn, pos); s=s.substr(p2+1); p2=s.find('+'); } } } return sgetDetectorsType(pos); } string multiSlsDetector::getHostname(int pos) { string s=string(""); #ifdef VERBOSE cout << "returning hostname" << pos << endl; #endif if (pos>=0) { if (detectors[pos]) return detectors[pos]->getHostname(); } else { for (int ip=0; ipnumberOfDetectors; ++ip) { #ifdef VERBOSE cout << "detector " << ip << endl; #endif if (detectors[ip]) { s+=detectors[ip]->getHostname(); s+=string("+"); } #ifdef VERBOSE cout << s <=0) { if (detectors[pos]) return detectors[pos]->getDetectorsType(); } else if (detectors[0]) return detectors[0]->getDetectorsType(); return s; } string multiSlsDetector::sgetDetectorsType(int pos) { string s=string(""); #ifdef VERBOSE cout << "returning type" << pos << endl; #endif if (pos>=0) { if (detectors[pos]) return detectors[pos]->sgetDetectorsType(); } else { for (int ip=0; ipnumberOfDetectors; ++ip) { #ifdef VERBOSE cout << "detector " << ip << endl; #endif if (detectors[ip]) { s+=detectors[ip]->sgetDetectorsType(); s+=string("+"); } #ifdef VERBOSE cout << "type " << s << endl; #endif } } return s; } int multiSlsDetector::getDetectorId(int pos) { #ifdef VERBOSE cout << "Getting detector ID " << pos << endl; #endif if (pos>=0) { if (detectors[pos]) return detectors[pos]->getDetectorId(); } return -1; } int multiSlsDetector::setDetectorId(int ival, int pos){ if (pos>=0) { addSlsDetector(ival, pos); if (detectors[pos]) return detectors[pos]->getDetectorId(); } else { return -1; } return -1; } int multiSlsDetector::addSlsDetector(const char *name, int pos) { detectorType t=getDetectorType(string(name)); int online=0; slsDetector *s=NULL; int id; #ifdef VERBOSE cout << "Adding detector "<numberOfDetectors; ++i) { if (detectors[i]) { if (detectors[i]->getHostname()==string(name)) { cout << "Detector " << name << "already part of the multiDetector in position " << i << "!" << endl<< "Remove it before adding it back in a new position!"<< endl; return -1; } } } //checking that the detector doesn't already exists for (id=0; id0) { #ifdef VERBOSE cout << "Detector " << id << " already exists" << endl; #endif s=new slsDetector(pos, id, this); if (s->getHostname()==string(name)) break; delete s; s=NULL; //++id; } } if (s==NULL) { t=slsDetector::getDetectorType(name, DEFAULT_PORTNO); if (t==GENERIC) { cout << "Detector " << name << "does not exist in shared memory and could not connect to it to determine the type (which is not specified)!" << endl; setErrorMask(getErrorMask()|MULTI_DETECTORS_NOT_ADDED); appendNotAddedList(name); return -1; } #ifdef VERBOSE else cout << "Detector type is " << t << endl; #endif online=1; } } #ifdef VERBOSE else cout << "Adding detector by type " << getDetectorType(t) << endl; #endif if (s==NULL) { for (id=0; idsetTCPSocket(name); setOnline(ONLINE_FLAG); } delete s; } #ifdef VERBOSE cout << "Adding it to the multi detector structure" << endl; #endif return addSlsDetector(id, pos); } int multiSlsDetector::addSlsDetector(detectorType t, int pos) { int id; if (t==GENERIC) { return -1; } for (id=0; idnumberOfDetectors; ++i) { if (thisMultiDetector->offsetX[i] > offsetx) { ++nx; offsetx = thisMultiDetector->offsetX[i]; } if (thisMultiDetector->offsetY[i] > offsety) { ++ny; offsety = thisMultiDetector->offsetY[i]; } } } 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(char *name){ for (int id=0; idnumberOfDetectors; ++id) { if (detectors[id]) { if (detectors[id]->getHostname()==string(name)) { removeSlsDetector(id); } } } return thisMultiDetector->numberOfDetectors; }; int multiSlsDetector::removeSlsDetector(int pos) { int j; #ifdef VERBOSE cout << "Removing detector in position " << pos << endl; #endif int mi=0, ma=thisMultiDetector->numberOfDetectors, single=0; if (pos>=0) { mi=pos; ma=pos+1; single=1; } // if (pos<0 ) // pos=thisMultiDetector->numberOfDetectors-1; if (pos>=thisMultiDetector->numberOfDetectors) return thisMultiDetector->numberOfDetectors; //j=pos; for (j=mi; jdataBytes-=detectors[j]->getDataBytes(); thisMultiDetector->numberOfChannels-=detectors[j]->getTotalNumberOfChannels(); thisMultiDetector->maxNumberOfChannels-=detectors[j]->getMaxNumberOfChannels(); delete detectors[j]; detectors[j]=0; --thisMultiDetector->numberOfDetectors; if (single) { for (int i=j+1; inumberOfDetectors+1; ++i) { detectors[i-1]=detectors[i]; thisMultiDetector->detectorIds[i-1]=thisMultiDetector->detectorIds[i]; } detectors[thisMultiDetector->numberOfDetectors]=NULL; thisMultiDetector->detectorIds[thisMultiDetector->numberOfDetectors]=-1; } } } updateOffsets(); if(createThreadPool() == FAIL) exit(-1); return thisMultiDetector->numberOfDetectors; } int multiSlsDetector::setMaster(int i) { int ret=-1, slave=0; masterFlags f; #ifdef VERBOSE cout << "settin master in position " << i << endl; #endif if (i>=0 && inumberOfDetectors) { if (detectors[i]) { #ifdef VERBOSE cout << "detector position " << i << " "; #endif thisMultiDetector->masterPosition=i; detectors[i]->setMaster(IS_MASTER); if(detectors[i]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; ++id) { if (i!=id) { if (detectors[id]) { #ifdef VERBOSE cout << "detector position " << id << " "; #endif detectors[id]->setMaster(IS_SLAVE); if(detectors[id]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; ++id) { if (detectors[id]) { #ifdef VERBOSE cout << "detector position " << id << " "; #endif detectors[id]->setMaster(NO_MASTER); if(detectors[id]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; ++id) { if (detectors[id]) { #ifdef VERBOSE cout << "detector position " << id << " "; #endif f=detectors[id]->setMaster(GET_MASTER); if(detectors[id]->getErrorMask()) setErrorMask(getErrorMask()|(1<0 && ret<0) ret=-2; if (ret<0) ret=-1; thisMultiDetector->masterPosition=ret; return thisMultiDetector->masterPosition; } // enum synchronyzationMode { // 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 */ // } /** Sets/gets the synchronization mode of the various detectors \param sync syncronization mode \returns current syncronization mode */ slsDetectorDefs::synchronizationMode multiSlsDetector::setSynchronization(synchronizationMode sync) { synchronizationMode ret=GET_SYNCHRONIZATION_MODE, ret1=GET_SYNCHRONIZATION_MODE; for (int id=0; idnumberOfDetectors; ++id) { if (detectors[id]) { ret1=detectors[id]->setSynchronization(sync); if(detectors[id]->getErrorMask()) setErrorMask(getErrorMask()|(1<syncMode=ret; return thisMultiDetector->syncMode; } int multiSlsDetector::setOnline(int off) { if (off!=GET_ONLINE_FLAG) { thisMultiDetector->onlineFlag=off; int ret=-100; if(!threadpool){ cout << "Error in creating threadpool. Exiting" << endl; return -1; }else{ //return storage values int* iret[thisMultiDetector->numberOfDetectors]; for(int idet=0; idetnumberOfDetectors; ++idet){ if(detectors[idet]){ iret[idet]= new int(-1); Task* task = new Task(new func1_t(&slsDetector::setOnline, detectors[idet],off,iret[idet])); threadpool->add_task(task); } } threadpool->startExecuting(); threadpool->wait_for_tasks_to_complete(); for(int idet=0; idetnumberOfDetectors; ++idet){ if(detectors[idet]){ if(iret[idet] != NULL){ if (ret==-100) ret=*iret[idet]; else if (ret!=*iret[idet]) ret=-1; delete iret[idet]; }else ret=-1; if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<onlineFlag=ret; } return thisMultiDetector->onlineFlag; }; string multiSlsDetector::checkOnline() { string retval1 = "",retval; for (int idet=0; idetnumberOfDetectors; ++idet) { if (detectors[idet]) { retval=detectors[idet]->checkOnline(); if(!retval.empty()){ retval1.append(retval); retval1.append("+"); } } } return retval1; }; int multiSlsDetector::activate(int const enable){ int i; int ret1=-100, ret; for (i=0; inumberOfDetectors; ++i) { if (detectors[i]) { ret=detectors[i]->activate(enable); if(detectors[i]->getErrorMask()) setErrorMask(getErrorMask()|(1<alreadyExisting; } // 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(detectors[i]->getErrorMask()) setErrorMask(getErrorMask()|(1<(ret1+200)) ret1=FAIL; } } thisMultiDetector->currentThresholdEV=ret1; return ret1; } int multiSlsDetector::setThresholdEnergy(int e_eV, int pos, detectorSettings isettings, int tb) { int posmin, posmax; int ret=-100; if (pos<0) { posmin=0; posmax=thisMultiDetector->numberOfDetectors; } else { posmin=pos; posmax=pos+1; } if(!threadpool){ cout << "Error in creating threadpool. Exiting" << endl; return -1; }else{ //return storage values int* iret[posmax-posmin]; for(int idet=posmin; idet(&slsDetector::setThresholdEnergy, detectors[idet],e_eV,-1,isettings,tb,iret[idet])); threadpool->add_task(task); } } threadpool->startExecuting(); threadpool->wait_for_tasks_to_complete(); for(int idet=posmin; idet(*iret[idet]+200)) ret=-1; delete iret[idet]; }else ret=-1; if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<currentThresholdEV=ret; return ret; } slsDetectorDefs::detectorSettings multiSlsDetector::getSettings(int pos) { int posmin, posmax; int ret=-100; if (pos<0) { posmin=0; posmax=thisMultiDetector->numberOfDetectors; } else { posmin=pos; posmax=pos+1; } if(!threadpool){ cout << "Error in creating threadpool. Exiting" << endl; return GET_SETTINGS; }else{ //return storage values detectorSettings* iret[posmax-posmin]; for(int idet=posmin; idet(&slsDetector::getSettings, detectors[idet],-1,iret[idet])); threadpool->add_task(task); } } threadpool->startExecuting(); threadpool->wait_for_tasks_to_complete(); for(int idet=posmin; idetgetErrorMask()) setErrorMask(getErrorMask()|(1<currentSettings=(detectorSettings)ret; return (detectorSettings)ret; } slsDetectorDefs::detectorSettings multiSlsDetector::setSettings(detectorSettings isettings, int pos) { int posmin, posmax; int ret=-100; if (pos<0) { posmin=0; posmax=thisMultiDetector->numberOfDetectors; } else { posmin=pos; posmax=pos+1; } if(!threadpool){ cout << "Error in creating threadpool. Exiting" << endl; return GET_SETTINGS; }else{ //return storage values detectorSettings* iret[posmax-posmin]; for(int idet=posmin; idet(&slsDetector::setSettings, detectors[idet],isettings,-1,iret[idet])); threadpool->add_task(task); } } threadpool->startExecuting(); threadpool->wait_for_tasks_to_complete(); for(int idet=posmin; idetgetErrorMask()) setErrorMask(getErrorMask()|(1<currentSettings=(detectorSettings)ret; return (detectorSettings)ret; } int multiSlsDetector::getChanRegs(double* retval,bool fromDetector){ //nChansDet and currentNumChans is because of varying channel size per detector int n = thisMultiDetector->numberOfChannels,nChansDet,currentNumChans=0; double retval1[n]; for (int idet=0; idetnumberOfDetectors; ++idet) { if (detectors[idet]) { // cout << "det " << idet << endl; nChansDet = detectors[idet]->getChanRegs(retval1,fromDetector); // cout << "returned" << endl; if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; if(!threadpool){ cout << "Error in creating threadpool. Exiting" << endl; return FAIL; }else{ int* iret[posmax-posmin]; for(int idet=posmin; idetmasterPosition) && (detectors[idet])){ iret[idet]= new int(OK); Task* task = new Task(new func0_t(&slsDetector::prepareAcquisition, detectors[idet],iret[idet])); threadpool->add_task(task); } } threadpool->startExecuting(); threadpool->wait_for_tasks_to_complete(); for(int idet=posmin; idetmasterPosition) && (detectors[idet])){ if(iret[idet] != NULL){ if(*iret[idet] != OK) ret = FAIL; delete iret[idet]; }else ret = FAIL; if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<masterPosition; if (thisMultiDetector->masterPosition>=0) { if (detectors[i]) { ret1=detectors[i]->prepareAcquisition(); if(detectors[i]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; i=thisMultiDetector->masterPosition; if (thisMultiDetector->masterPosition>=0) { if (detectors[i]) { ret1=detectors[i]->cleanupAcquisition(); if(detectors[i]->getErrorMask()) setErrorMask(getErrorMask()|(1<masterPosition) && (detectors[idet])){ iret[idet]= new int(OK); Task* task = new Task(new func0_t(&slsDetector::cleanupAcquisition, detectors[idet],iret[idet])); threadpool->add_task(task); } } threadpool->startExecuting(); threadpool->wait_for_tasks_to_complete(); for(int idet=posmin; idetmasterPosition) && (detectors[idet])){ if(iret[idet] != NULL){ if(*iret[idet] != OK) ret = FAIL; delete iret[idet]; }else ret = FAIL; if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; if(!threadpool){ cout << "Error in creating threadpool. Exiting" << endl; return FAIL; }else{ int* iret[posmax-posmin]; for(int idet=posmin; idetmasterPosition) && (detectors[idet])){ iret[idet]= new int(OK); Task* task = new Task(new func0_t(&slsDetector::startAcquisition, detectors[idet],iret[idet])); threadpool->add_task(task); } } threadpool->startExecuting(); threadpool->wait_for_tasks_to_complete(); for(int idet=posmin; idetmasterPosition) && (detectors[idet])){ if(iret[idet] != NULL){ if(*iret[idet] != OK) ret = FAIL; delete iret[idet]; }else ret = FAIL; if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<masterPosition; if (thisMultiDetector->masterPosition>=0) { if (detectors[i]) { ret1=detectors[i]->startAcquisition(); if(detectors[i]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; i=thisMultiDetector->masterPosition; if (thisMultiDetector->masterPosition>=0) { if (detectors[i]) { ret1=detectors[i]->stopAcquisition(); if(detectors[i]->getErrorMask()) setErrorMask(getErrorMask()|(1<masterPosition) && (detectors[idet])){ iret[idet]= new int(OK); Task* task = new Task(new func0_t(&slsDetector::stopAcquisition, detectors[idet],iret[idet])); threadpool->add_task(task); } } threadpool->startExecuting(); threadpool->wait_for_tasks_to_complete(); for(int idet=posmin; idetmasterPosition) && (detectors[idet])){ if(iret[idet] != NULL){ if(*iret[idet] != OK) ret = FAIL; delete iret[idet]; }else ret = FAIL; if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<masterPosition; if (i>=0) { if (detectors[i]) { ret=detectors[i]->startReadOut(); if(detectors[i]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; ++i) { if (detectors[i]) { ret=detectors[i]->startReadOut(); if(detectors[i]->getErrorMask()) setErrorMask(getErrorMask()|(1<dataBytes/sizeof(int); int n = 0; int* retval= NULL; int *retdet, *p=retval; int nodatadet=-1; int nodatadetectortype = false; detectorType types = getDetectorsType(); if(types == EIGER || types == JUNGFRAU){ nodatadetectortype = true; } if(!nodatadetectortype) retval=new int[nel]; p=retval; // cout << "multi: " << thisMultiDetector->dataBytes << endl; for (int id=0; idnumberOfDetectors; ++id) { if (detectors[id]) { retdet=detectors[id]->getDataFromDetector(p); if(detectors[id]->getErrorMask()) setErrorMask(getErrorMask()|(1<getDataBytes(); if (retdet) {; #ifdef VERBOSE cout << "Detector " << id << " returned " << n << " bytes " << endl; #endif } else { nodatadet=id; #ifdef VERBOSE cout << "Detector " << id << " does not have data left " << endl; #endif } p+=n/sizeof(int); } } } //eiger returns only null if(nodatadetectortype){ return NULL; } if (nodatadet>=0) { for (int id=0; idnumberOfDetectors; ++id) { if (id!=nodatadet) { if (detectors[id]) { //#ifdef VERBOSE cout << "Stopping detector "<< id << endl; //#endif detectors[id]->stopAcquisition(); if(detectors[id]->getErrorMask()) setErrorMask(getErrorMask()|(1<getDataFromDetector())) { if(detectors[id]->getErrorMask()) setErrorMask(getErrorMask()|(1<getErrorMask()) setErrorMask(getErrorMask()|(1<dataBytes/sizeof(int); int n; int* retval=new int[nel]; int *retdet, *p=retval; /** probably it's always better to have one integer per channel in any case! */ for (int id=0; idnumberOfDetectors; ++id) { if (detectors[id]) { retdet=detectors[id]->readFrame(); if(detectors[id]->getErrorMask()) setErrorMask(getErrorMask()|(1<getDataBytes(); if(detectors[id]->getErrorMask()) setErrorMask(getErrorMask()|(1<onlineFlag==ONLINE_FLAG) { for (int id=0; idnumberOfDetectors; ++id) { if (detectors[id]) { detectors[id]->readAllNoWait(); if(detectors[id]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; ++id) { if (detectors[id]) { detectors[id]->disconnectControl(); } } } #ifdef VERBOSE std::cout<< "received "<< i<< " frames" << std::endl; //#else // std::cout << std::endl; #endif return dataQueueFront(); // check what we return! }; int* multiSlsDetector::startAndReadAll(){ /** Thread for each detector?!?!?! */ #ifdef VERBOSE cout << "Start and read all " << endl; #endif int* retval; int i=0; if (thisMultiDetector->onlineFlag==ONLINE_FLAG) { if(getDetectorsType() == EIGER) { if (prepareAcquisition() == FAIL) return NULL; } startAndReadAllNoWait(); while ((retval=getDataFromDetector())){ ++i; #ifdef VERBOSE std::cout<< i << std::endl; //#else //std::cout << "-" << flush; #endif dataQueue.push(retval); } for (int id=0; idnumberOfDetectors; ++id) { if (detectors[id]) { detectors[id]->disconnectControl(); } } } #ifdef VERBOSE std::cout<< "MMMM recieved "<< i<< " frames" << std::endl; //#else // std::cout << std::endl; #endif return dataQueueFront(); // check what we return! }; int multiSlsDetector::startAndReadAllNoWait(){ pthread_mutex_lock(&mg); // locks due to processing thread using threadpool when in use int i=0; int ret=OK; int posmin=0, posmax=thisMultiDetector->numberOfDetectors; if(!threadpool){ cout << "Error in creating threadpool. Exiting" << endl; return FAIL; }else{ int* iret[posmax-posmin]; for(int idet=posmin; idetmasterPosition) && (detectors[idet])){ iret[idet]= new int(OK); Task* task = new Task(new func0_t(&slsDetector::startAndReadAllNoWait, detectors[idet],iret[idet])); threadpool->add_task(task); } } threadpool->startExecuting(); threadpool->wait_for_tasks_to_complete(); for(int idet=posmin; idetmasterPosition) && (detectors[idet])){ if(iret[idet] != NULL){ if(*iret[idet] != OK) ret = FAIL; delete iret[idet]; }else ret = FAIL; if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<masterPosition; if (thisMultiDetector->masterPosition>=0) { if (detectors[i]) { ret1=detectors[i]->startAndReadAllNoWait(); if(detectors[i]->getErrorMask()) setErrorMask(getErrorMask()|(1<masterPosition>=0) if (detectors[thisMultiDetector->masterPosition]){ s = detectors[thisMultiDetector->masterPosition]->getRunStatus(); if(detectors[thisMultiDetector->masterPosition]->getErrorMask()) setErrorMask(getErrorMask()|(1<masterPosition)); return s; } for (int i=0; inumberOfDetectors; ++i) { s1=detectors[i]->getRunStatus(); if(detectors[i]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors]; for(int idet=0; idetnumberOfDetectors; ++idet){ if(detectors[idet]){ iret[idet]= new int64_t(-1); Task* task = new Task(new func2_t(&slsDetector::setTimer, detectors[idet],index,t,iret[idet])); threadpool->add_task(task); } } threadpool->startExecuting(); threadpool->wait_for_tasks_to_complete(); for(int idet=0; idetnumberOfDetectors; ++idet){ if(detectors[idet]){ if(iret[idet] != NULL){ if (ret==-100) ret=*iret[idet]; else if (ret!=*iret[idet]) ret=-1; delete iret[idet]; }else ret=-1; if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<timerValue[index]=ret; return ret; } int64_t multiSlsDetector::getTimeLeft(timerIndex index){ int i; int64_t ret1=-100, ret; if (thisMultiDetector->masterPosition>=0) if (detectors[thisMultiDetector->masterPosition]){ ret1 = detectors[thisMultiDetector->masterPosition]->getTimeLeft(index); if(detectors[thisMultiDetector->masterPosition]->getErrorMask()) setErrorMask(getErrorMask()|(1<masterPosition)); return ret1; } for (i=0; inumberOfDetectors; ++i) { if (detectors[i]) { ret=detectors[i]->getTimeLeft(index); if(detectors[i]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; ++i) { if (detectors[i]) { ret=detectors[i]->setSpeed(index,value); if(detectors[i]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; ++ip) { if (detectors[ip]) ret+=detectors[ip]->getDataBytes(); } return ret; } // Flags int multiSlsDetector::setDynamicRange(int n, int pos){ // cout << "multi " << endl; int imi, ima, i; int ret, ret1=-100; if (pos<0) { imi=0; ima=thisMultiDetector->numberOfDetectors; } else { imi=pos; ima=pos+1; } for (i=imi; idataBytes-=detectors[i]->getDataBytes(); ret=detectors[i]->setDynamicRange(n); if(detectors[i]->getErrorMask()) setErrorMask(getErrorMask()|(1<dataBytes+=detectors[i]->getDataBytes(); } } //for usability for the user if (getDetectorsType() == EIGER){ if(n == 32){ std::cout << "Setting Clock to Quarter Speed to cope with Dynamic Range of 32" << std::endl; setSpeed(CLOCK_DIVIDER,2); } else if(n == 16){ std::cout << "Setting Clock to Half Speed for Dynamic Range of 16" << std::endl; setSpeed(CLOCK_DIVIDER,1); } } return thisMultiDetector->dataBytes; }; void multiSlsDetector::verifyMinMaxROI(int n, ROI r[]){ int temp; for(int i=0;inumberOfDetectors;++i){ if (detectors[i]) { //check x offset range if ((offsetX >= thisMultiDetector->offsetX[i]) && (offsetX < (thisMultiDetector->offsetX[i]+detectors[i]->getMaxNumberOfChannels(X)))){ if(offsetY==-1){ channelX = offsetX - thisMultiDetector->offsetX[i]; return i; }else{ //check y offset range if((offsetY >= thisMultiDetector->offsetY[i]) && (offsetY< (thisMultiDetector->offsetY[i]+detectors[i]->getMaxNumberOfChannels(Y)))){ channelX = offsetX - thisMultiDetector->offsetX[i]; channelY = offsetY - thisMultiDetector->offsetY[i]; return i; } } } } } return -1; } int multiSlsDetector::setROI(int n,ROI roiLimits[]){ int ret1=-100,ret; int i,xmin,xmax,ymin,ymax,channelX,channelY,idet,lastChannelX,lastChannelY,index,offsetX,offsetY; bool invalidroi=false; int ndet = thisMultiDetector->numberOfDetectors; ROI allroi[ndet][n]; int nroi[ndet]; for(i=0;igetMaxNumberOfChannels(X))-1; lastChannelY = (detectors[idet]->getMaxNumberOfChannels(Y))-1; offsetX = thisMultiDetector->offsetX[idet]; offsetY = thisMultiDetector->offsetY[idet]; //at the end in x dir if ((offsetX + lastChannelX) >= xmax) lastChannelX = xmax - offsetX; //at the end in y dir if ((offsetY + lastChannelY) >= ymax) lastChannelY = ymax - offsetY; #ifdef VERBOSE cout<<"lastChannelX:"<numberOfDetectors;++i){ cout<<"detector "<numberOfDetectors; ++i) { if (detectors[i]){ #ifdef VERBOSE cout << "detector " << i << ":" << endl; #endif ret = detectors[i]->setROI(nroi[i],allroi[i]); if(detectors[i]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; int maxroi = ndet*MAX_ROIS; ROI temproi; ROI roiLimits[maxroi]; ROI* retval = new ROI[maxroi]; ROI* temp=0; int index=0; //get each detector's roi array for (i=0; inumberOfDetectors; ++i){ if (detectors[i]){ temp = detectors[i]->getROI(index); if(detectors[i]->getErrorMask()) setErrorMask(getErrorMask()|(1<offsetX[i]; roiLimits[n].xmax = temp[j].xmax + thisMultiDetector->offsetX[i]; roiLimits[n].ymin = temp[j].ymin + thisMultiDetector->offsetY[i]; roiLimits[n].ymax = temp[j].ymin + thisMultiDetector->offsetY[i]; ++n; } } } } //empty roi if (!n) return NULL; #ifdef VERBOSE cout<<"ROI :"<getDetectorsType()==JUNGFRAUCTB) { nn=thisMultiDetector->dataBytes/2; dataout=new double[nn]; } else { nn=thisMultiDetector->numberOfChannels; dataout=new double[nn]; } } // int ich=0; int n; double *detp=dataout; int *datap=datain; for (int i=0; inumberOfDetectors; ++i) { if (detectors[i]) { detectors[i]->decodeData(datap, n, detp); if(detectors[i]->getErrorMask()) setErrorMask(getErrorMask()|(1<getDataBytes()/sizeof(int); detp+=n; // if (detectors[0]->getDetectorsType()==JUNGFRAUCTB) { // detp+=detectors[i]->getDataBytes()/2; // } else { // detp+=detectors[i]->getTotalNumberOfChannels(); // } #ifdef VERBOSE cout << "done " << endl; #endif // for (int j=0; jgetTotalNumberOfChannels(); ++j) { // dataout[ich]=detp[j]; // ++ich; // } //delete [] detp; } } return dataout; } //Correction /* enum correctionFlags { DISCARD_BAD_CHANNELS, AVERAGE_NEIGHBOURS_FOR_BAD_CHANNELS, FLAT_FIELD_CORRECTION, RATE_CORRECTION, ANGULAR_CONVERSION } */ /////////////////////////////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////// int multiSlsDetector::setFlatFieldCorrection(string fname){ double* data = new double[thisMultiDetector->numberOfChannels];// xmed[thisMultiDetector->numberOfChannels]; double* ffcoefficients = new double[thisMultiDetector->numberOfChannels]; double*fferrors = new double[thisMultiDetector->numberOfChannels]; // int nmed=0; // int idet=0, ichdet=-1; char ffffname[MAX_STR_LENGTH*2]; int nch;//nbad=0, //int badlist[MAX_BADCHANS]; //int im=0; if (fname=="default") { fname=string(thisMultiDetector->flatFieldFile); } thisMultiDetector->correctionMask&=~(1<correctionMask&=~(1<flatFieldFile,"none"); for (int i=0; inumberOfDetectors; ++i) { if (detectors[i]){ detectors[i]->setFlatFieldCorrection(NULL, NULL); if(detectors[i]->getErrorMask()) setErrorMask(getErrorMask()|(1<flatFieldDir,fname.c_str()); nch=readDataFile(string(ffffname),data); if (nch>thisMultiDetector->numberOfChannels) nch=thisMultiDetector->numberOfChannels; if (nch>0) { //???? bad ff chans? int nm=getNMods(); int chpm[nm]; int mMask[nm]; for (int i=0; i=0) { strcpy(thisMultiDetector->flatFieldFile,fname.c_str()); thisMultiDetector->correctionMask|=(1<correctionMask&(1<numberOfDetectors; ++i) { if (detectors[i]) { for (int im=0; imgetNMods(); ++im) { mM[imod]=im+off; ++imod; } off+=detectors[i]->getMaxMods(); } } } return getNMods(); } int multiSlsDetector::setFlatFieldCorrection(double *corr, double *ecorr) { int ichdet=0; double *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]->setFlatFieldCorrection(p, ep); if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<getTotalNumberOfChannels(); } } return 0; } int multiSlsDetector::getFlatFieldCorrection(double *corr, double *ecorr) { int ichdet=0; double *p, *ep; int ff=1, dff; 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; dff=detectors[idet]->getFlatFieldCorrection(p, ep); if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<getTotalNumberOfChannels(); } } return ff; } int multiSlsDetector::getNMods(){ int nm=0; for (int idet=0; idetnumberOfDetectors; ++idet) { if (detectors[idet]) { nm+=detectors[idet]->getNMods(); } } #ifdef VERBOSE cout << "total number of modules is " << nm << endl; #endif return nm; } int multiSlsDetector::getNMod(dimension d){ int nm=0; for (int idet=0; idetnumberOfDetectors; ++idet) { if (detectors[idet]) { nm+=detectors[idet]->getNMod(d); } } #ifdef VERBOSE cout << "total number of modules in dimension " << d << " is " << nm << endl; #endif return nm; } int multiSlsDetector::getChansPerMod(int imod){ int id=-1, im=-1; #ifdef VERBOSE cout << "get chans per mod " << imod << endl; #endif decodeNMod(imod, id, im); if (id >=0) { if (detectors[id]) { return detectors[id]->getChansPerMod(im); } } return -1; } int multiSlsDetector::getMoveFlag(int imod){ int id=-1, im=-1; decodeNMod(imod, id, im); if (id>=0) { if (detectors[id]) { return detectors[id]->getMoveFlag(im); } } //default!!! return 1; } angleConversionConstant * multiSlsDetector::getAngularConversionPointer(int imod){ int id=-1, im=-1; #ifdef VERBOSE cout << "get angular conversion pointer " << endl; #endif if (decodeNMod(imod, id, im)>=0) { if (detectors[id]) { return detectors[id]->getAngularConversionPointer(im); } } return NULL; } int multiSlsDetector::flatFieldCorrect(double* datain, double *errin, double* dataout, double *errout){ int ichdet=0; double *perr=errin;//*pdata, for (int idet=0; idetnumberOfDetectors; ++idet) { if (detectors[idet]) { #ifdef VERBOSE cout << " detector " << idet << " offset " << ichdet << endl; #endif if (errin) perr+=ichdet; detectors[idet]->flatFieldCorrect(datain+ichdet, perr, dataout+ichdet, errout+ichdet); if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<getTotalNumberOfChannels();//detectors[idet]->getNChans()*detectors[idet]->getNChips()*detectors[idet]->getNMods(); } } return 0; }; int multiSlsDetector::setRateCorrection(double t){ #ifdef VERBOSE std::cout<< "Setting rate correction with dead time "<< thisMultiDetector->tDead << std::endl; #endif int ret=OK; int posmax=thisMultiDetector->numberOfDetectors; // eiger return value is ok/fail if (getDetectorsType() == EIGER){ if(!threadpool){ cout << "Error in creating threadpool. Exiting" << endl; return FAIL; }else{ int* iret[posmax]; for(int idet=0; idet(&slsDetector::setRateCorrection, detectors[idet],t,iret[idet])); threadpool->add_task(task); } } threadpool->startExecuting(); threadpool->wait_for_tasks_to_complete(); for(int idet=0; idetgetErrorMask()) setErrorMask(getErrorMask()|(1<correctionMask&=~(1<correctionMask&(1<correctionMask|=(1<(&slsDetector::setRateCorrection, detectors[idet],t,iret[idet])); threadpool->add_task(task); } } threadpool->startExecuting(); threadpool->wait_for_tasks_to_complete(); for(int idet=0; idetgetErrorMask()) setErrorMask(getErrorMask()|(1<correctionMask&(1<correctionMask&(1<tDead << std::endl; #endif return 1; } else t=0; #ifdef VERBOSE std::cout<< "Rate correction is disabled " << std::endl; #endif return 0; }; double multiSlsDetector::getRateCorrectionTau(){ double ret=-100.0; int posmax = thisMultiDetector->numberOfDetectors; if(!threadpool){ cout << "Error in creating threadpool. Exiting" << endl; return -1; }else{ double* iret[posmax]; for(int idet=0; idetnumberOfDetectors; ++idet){ if(detectors[idet]){ iret[idet]= new double(-1); Task* task = new Task(new func0_t(&slsDetector::getRateCorrectionTau, detectors[idet],iret[idet])); threadpool->add_task(task); } } threadpool->startExecuting(); threadpool->wait_for_tasks_to_complete(); for(int idet=0; idet 0.000000001) { std::cout<< "Rate correction is different for different readouts " << std::endl; ret=-1; } delete iret[idet]; }else ret = -1; if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<correctionMask&(1<tDead << std::endl; #endif } else { #ifdef VERBOSE std::cout<< "Rate correction is disabled " << std::endl; #endif ret=0; } return ret; }; int multiSlsDetector::getRateCorrection(){ if (getDetectorsType() == EIGER){ return getRateCorrectionTau(); } if (thisMultiDetector->correctionMask&(1<numberOfDetectors; ++idet) { if (detectors[idet]) { if (errin) perr+=ichdet; detectors[idet]->rateCorrect(datain+ichdet, perr, dataout+ichdet, errout+ichdet); if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<getTotalNumberOfChannels(); } } return 0; }; int multiSlsDetector::setBadChannelCorrection(string fname){ int badlist[MAX_BADCHANS];// badlistdet[MAX_BADCHANS]; int nbad=0;//, nbaddet=0, choff=0, idet=0; int ret=0; cout << thisMultiDetector->badChanFile << endl; if (fname=="default") fname=string(thisMultiDetector->badChanFile); ret=setBadChannelCorrection(fname, nbad, badlist); //#ifdef VERBOSE cout << "multi: file contained " << ret << " badchans" << endl; //#endif if (ret==0) { thisMultiDetector->correctionMask&=~(1<correctionMask|=(1<badChanFile,fname.c_str()); } return setBadChannelCorrection(nbad,badlist,0); } int multiSlsDetector::setBadChannelCorrection(int nbad, int *badlist, int ff) { //#define VERBOSE int badlistdet[MAX_BADCHANS]; int nbaddet=0, choff=0, idet=0; if (nbad<1) badlistdet[0]=0; else badlistdet[0]=badlist[0]; if (nbad>0) { thisMultiDetector->correctionMask|=(1<=detectors[idet]->getMaxNumberOfChannels()) { //#ifdef VERBOSE cout << "setting " << nbaddet << " badchans to detector " << idet << endl; //#endif detectors[idet]->setBadChannelCorrection(nbaddet,badlistdet,0); if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<getMaxNumberOfChannels(); nbaddet=0; ++idet; if (detectors[idet]==NULL) break; } badlistdet[nbaddet]=(badlist[ich]-choff); ++nbaddet; #ifdef VERBOSE cout << nbaddet << " " << badlist[ich] << " " << badlistdet[nbaddet-1] << endl; #endif } } if (nbaddet>0) { if (detectors[idet]) { #ifdef VERBOSE cout << "setting " << nbaddet << " badchans to detector " << idet << endl; #endif detectors[idet]->setBadChannelCorrection(nbaddet,badlistdet,0); if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<getMaxNumberOfChannels(); nbaddet=0; ++idet; } } nbaddet=0; for (int i=idet; inumberOfDetectors; ++i) { #ifdef VERBOSE cout << "setting " << 0 << " badchans to detector " << i << endl; #endif if (detectors[i]) { detectors[i]->setBadChannelCorrection(nbaddet,badlistdet,0); if(detectors[i]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; ++i) { if (detectors[idet]) { #ifdef VERBOSE cout << "setting " << 0 << " badchans to detector " << idet << endl; #endif detectors[idet]->setBadChannelCorrection(nbaddet,badlistdet,0); if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<correctionMask&=~(1<correctionMask&(1<correctionMask&(1<numberOfDetectors; ++idet) { if (detectors[idet]) { #ifdef VERBOSE cout << " detector " << idet << endl; #endif detectors[idet]->readAngularConversion(infile); if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; ++idet) { if (detectors[idet]) { detectors[idet]->writeAngularConversion(outfile); if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; ++idet) { if (detectors[idet]) { detectors[idet]->getAngularConversion(dir1,a1); if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<getNMods(); } } } direction=dir; if (thisMultiDetector->correctionMask&(1<< ANGULAR_CONVERSION)) { return 1; } return 0; } dacs_t multiSlsDetector::setDAC(dacs_t val, dacIndex idac, int mV, int imod) { dacs_t ret = -100; // single { int id=-1, im=-1; if (decodeNMod(imod, id, im)>=0) { if(detectors[id]){ ret = detectors[id]->setDAC(val, idac, mV, im); if(detectors[id]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; dacs_t* iret[posmax-posmin]; for(int idet=posmin; idet(&slsDetector::setDAC, detectors[idet],val, idac, mV, imod, iret[idet])); threadpool->add_task(task); } } threadpool->startExecuting(); threadpool->wait_for_tasks_to_complete(); for(int idet=posmin; idetgetErrorMask()) setErrorMask(getErrorMask()|(1<=0) { if(detectors[id]){ ret = detectors[id]->getADC(idac, im); if(detectors[id]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; dacs_t* iret[posmax-posmin]; for(int idet=posmin; idet(&slsDetector::getADC, detectors[idet],idac, imod, iret[idet])); threadpool->add_task(task); } } threadpool->startExecuting(); threadpool->wait_for_tasks_to_complete(); for(int idet=posmin; idetgetErrorMask()) setErrorMask(getErrorMask()|(1<=0) { if(detectors[id]){ ret = detectors[id]->setThresholdTemperature(val, im); if(detectors[id]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; int* iret[posmax-posmin]; for(int idet=posmin; idet(&slsDetector::setThresholdTemperature, detectors[idet], val, imod, iret[idet])); threadpool->add_task(task); } } threadpool->startExecuting(); threadpool->wait_for_tasks_to_complete(); for(int idet=posmin; idetgetErrorMask()) setErrorMask(getErrorMask()|(1<=0) { if(detectors[id]){ ret = detectors[id]->setTemperatureControl(val, im); if(detectors[id]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; int* iret[posmax-posmin]; for(int idet=posmin; idet(&slsDetector::setTemperatureControl, detectors[idet], val, imod, iret[idet])); threadpool->add_task(task); } } threadpool->startExecuting(); threadpool->wait_for_tasks_to_complete(); for(int idet=posmin; idetgetErrorMask()) setErrorMask(getErrorMask()|(1<=0) { if(detectors[id]){ ret = detectors[id]->setTemperatureEvent(val, im); if(detectors[id]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; int* iret[posmax-posmin]; for(int idet=posmin; idet(&slsDetector::setTemperatureEvent, detectors[idet], val, imod, iret[idet])); threadpool->add_task(task); } } threadpool->startExecuting(); threadpool->wait_for_tasks_to_complete(); for(int idet=posmin; idetgetErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; if (decodeNMod(imod, id, im)>=0) { dmi=id; dma=id+1; } for (int idet=dmi; idetsetChannel(reg, ichan, ichip, im); if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; ++idet) { if (detectors[idet]) { detectors[idet]->setAngularConversionParameter(c,v); if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfChannels]; // double *p=ang; // int choff=0; // for (int idet=0; idetnumberOfDetectors; ++idet) { // if (detectors[idet]) { // #ifdef EPICS // // cout << "convert angle det " << idet << endl; // if (idet<2) // #endif // p=detectors[idet]->convertAngles(pos); // #ifdef EPICS // else //////////// GOOD ONLY AT THE BEAMLINE!!!!!!!!!!!!! // p=detectors[idet]->convertAngles(0); // #endif // for (int ich=0; ichgetTotalNumberOfChannels(); ich++) { // ang[choff+ich]=p[ich]; // } // choff+=detectors[idet]->getTotalNumberOfChannels(); // delete [] p; // } // } // return ang; // } int multiSlsDetector::getBadChannelCorrection(int *bad) { //int ichan; int *bd, nd, ntot=0, choff=0;; if (((thisMultiDetector->correctionMask)&(1<< DISCARD_BAD_CHANNELS))==0) return 0; //else // cout << "bad chans corr enabled "<< thisMultiDetector->correctionMask << endl; for (int idet=0; idetnumberOfDetectors; ++idet) { if (detectors[idet]) { nd=detectors[idet]->getBadChannelCorrection(); // cout << "det " << idet << " nbad " << nd << endl; if (nd>0) { bd = new int[nd]; nd=detectors[idet]->getBadChannelCorrection(bd); if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<getTotalNumberOfChannels()) { if (bad) bad[ntot]=choff+bd[id]; ++ntot; } } choff+=detectors[idet]->getTotalNumberOfChannels(); delete [] bd; } else ntot+=nd; } } return ntot; } int multiSlsDetector::exitServer() { int ival=FAIL, iv; for (int idet=0; idetnumberOfDetectors; ++idet) { if (detectors[idet]) { iv=detectors[idet]->exitServer(); if (iv==OK) ival=iv; } } return ival; } /** returns the detector trimbit/settings directory */ char* multiSlsDetector::getSettingsDir() { string s0="", s1="", s; //char ans[1000]; for (int idet=0; idetnumberOfDetectors; ++idet) { if (detectors[idet]) { s=detectors[idet]->getSettingsDir(); if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; ++idet) { if (detectors[idet]) { detectors[idet]->setSettingsDir(s); if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<setSettingsDir(s.substr(p1,p2-p1)); if(detectors[id]->getErrorMask()) setErrorMask(getErrorMask()|(1<=thisMultiDetector->numberOfDetectors) break; } } return getSettingsDir(); } int multiSlsDetector::setTrimEn(int ne, int *ene) { int ret=-100, ret1; for (int idet=0; idetnumberOfDetectors; ++idet) { if (detectors[idet]) { ret1=detectors[idet]->setTrimEn(ne,ene); if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; ++idet) { if (detectors[idet]) { ret1=detectors[idet]->getTrimEn(ene); if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; ++idet) { if (detectors[idet]) { s=detectors[idet]->getCalDir(); if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; ++idet) { if (detectors[idet]) { detectors[idet]->setCalDir(s); if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<setCalDir(s.substr(p1,p2-p1)); if(detectors[id]->getErrorMask()) setErrorMask(getErrorMask()|(1<=thisMultiDetector->numberOfDetectors) break; } } return getCalDir(); } /** returns the location of the calibration files \sa sharedSlsDetector */ string multiSlsDetector::getNetworkParameter(networkParameter p) { string s0="", s1="",s ; string ans=""; //char ans[1000]; for (int idet=0; idetnumberOfDetectors; ++idet) { if (detectors[idet]) { s=detectors[idet]->getNetworkParameter(p); if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors]; for(int idet=0; idetnumberOfDetectors; ++idet){ if(detectors[idet]){ if (p == RECEIVER_STREAMING_PORT || p == CLIENT_STREAMING_PORT) s.append("multi\0"); sret[idet]=new string("error"); Task* task = new Task(new func2_t(&slsDetector::setNetworkParameter, detectors[idet],p,s,sret[idet])); threadpool->add_task(task); } } threadpool->startExecuting(); threadpool->wait_for_tasks_to_complete(); for(int idet=0; idetnumberOfDetectors; ++idet){ if(detectors[idet]){ if(sret[idet]!= NULL) delete sret[idet]; //doing nothing with the return values if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<setNetworkParameter(p,s.substr(p1,p2-p1)); if(detectors[id]->getErrorMask()) setErrorMask(getErrorMask()|(1<=thisMultiDetector->numberOfDetectors) break; } } return getNetworkParameter(p); } int multiSlsDetector::setPort(portType t, int p) { int ret=-100, ret1; for (int idet=0; idetnumberOfDetectors; ++idet) { if (detectors[idet]) { ret1=detectors[idet]->setPort(t,p); if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; ++idet) { if (detectors[idet]) { ret1=detectors[idet]->lockServer(p); if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; ++idet) { if (detectors[idet]) { s=detectors[idet]->getLastClientIP(); if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; ++idet) { if (detectors[idet]) { ret1=detectors[idet]->setReadOutFlags(flag); if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<setExternalCommunicationMode(pol); if(detectors[0]->getErrorMask()) setErrorMask(getErrorMask()|(1<<0)); for (int idet=1; idetnumberOfDetectors; ++idet) { if (detectors[idet]) { ret1=detectors[idet]->setExternalCommunicationMode(pol); if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<setExternalSignalFlags(pol,signalindex); if(detectors[0]->getErrorMask()) setErrorMask(getErrorMask()|(1<<0)); for (int idet=1; idetnumberOfDetectors; ++idet) { if (detectors[idet]) { ret1=detectors[idet]->setExternalSignalFlags(pol,signalindex); if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; ++idet) { if (detectors[idet]) { s=detectors[idet]->getSettingsFile(); if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; ++idet) { if (detectors[idet]) { ret1=detectors[idet]->configureMAC(); if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfChannels]; ifstream infile; infile.open(fname.c_str(), ios_base::in); if (infile.is_open()) { #ifdef VERBOSE std::cout<< std::endl<< "Loading "; if(!index) std::cout<<"Dark"; else std::cout<<"Gain"; std::cout<<" image from file " << fname << std::endl; #endif for (int idet=0; idetnumberOfDetectors; ++idet) { if (detectors[idet]) { if(detectors[idet]->readDataFile(infile,imageVals)>=0){ ret1=detectors[idet]->sendImageToDetector(index,imageVals); if (ret==-100) ret=ret1; else if (ret!=ret1) ret=-1; } if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfChannels]; ofstream outfile; outfile.open(fname.c_str(), ios_base::out); if (outfile.is_open()) { #ifdef VERBOSE std::cout<< std::endl<< "Reading Counter to \""<numberOfDetectors; ++idet) { if (detectors[idet]) { ret1=detectors[idet]->getCounterBlock(arg,startACQ); if(ret1!=OK) ret=FAIL; else{ ret1=detectors[idet]->writeDataFile(outfile,arg); if(ret1!=OK) ret=FAIL; } if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; ++idet) { if (detectors[idet]) { ret1=detectors[idet]->resetCounterBlock(startACQ); if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; ++idet) if (detectors[idet]){ ret1=detectors[idet]->setCounterBit(i); if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<dataBytes=0; thisMultiDetector->numberOfChannels=0; for (int idet=0; idetnumberOfDetectors; ++idet) { if (detectors[idet]) { ret1=detectors[idet]->setDynamicRange(p); if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<dataBytes+=detectors[idet]->getDataBytes(); // cout << "db " << idet << " " << detectors[idet]->getDataBytes() << endl; thisMultiDetector->numberOfChannels+=detectors[idet]->getTotalNumberOfChannels(); if (ret==-100) ret=ret1; else if (ret!=ret1) ret=-1; } } //for usability for the user if (getDetectorsType() == EIGER){ if(p == 32){ std::cout << "Setting Clock to Quarter Speed to cope with Dynamic Range of 32" << std::endl; setSpeed(CLOCK_DIVIDER,2); } else if(p == 16){ std::cout << "Setting Clock to Half Speed for Dynamic Range of 16" << std::endl; setSpeed(CLOCK_DIVIDER,1); } } return ret; } int multiSlsDetector::getMaxMods() { int ret=0, ret1; for (int idet=0; idetnumberOfDetectors; ++idet) { if (detectors[idet]) { ret1=detectors[idet]->getMaxMods(); #ifdef VERBOSE cout << "detector " << idet << " maxmods " << ret1 << endl; #endif ret+=ret1; } } #ifdef VERBOSE cout << "max mods is " << ret << endl; #endif return ret; } int multiSlsDetector::getTotalNumberOfChannels(){thisMultiDetector->numberOfChannels=0; for (int id=0; id< thisMultiDetector->numberOfDetectors; ++id) thisMultiDetector->numberOfChannels+=detectors[id]->getTotalNumberOfChannels(); return thisMultiDetector->numberOfChannels;}; //int multiSlsDetector::getTotalNumberOfChannels(dimension d){thisMultiDetector->numberOfChannel[d]=0; for (int id=0; id< thisMultiDetector->numberOfDetectors; ++id) thisMultiDetector->numberOfChannel[d]+=detectors[id]->getTotalNumberOfChannels(d); return thisMultiDetector->numberOfChannel[d];}; int multiSlsDetector::getTotalNumberOfChannels(dimension d){return thisMultiDetector->numberOfChannel[d];}; int multiSlsDetector::getMaxNumberOfChannels(){thisMultiDetector->maxNumberOfChannels=0; for (int id=0; id< thisMultiDetector->numberOfDetectors; ++id) thisMultiDetector->maxNumberOfChannels+=detectors[id]->getMaxNumberOfChannels();return thisMultiDetector->maxNumberOfChannels;}; // int multiSlsDetector::getMaxNumberOfChannels(dimension d){thisMultiDetector->maxNumberOfChannel[d]=0; for (int id=0; id< thisMultiDetector->numberOfDetectors; ++id) thisMultiDetector->maxNumberOfChannel[d]+=detectors[id]->getMaxNumberOfChannels(d);return thisMultiDetector->maxNumberOfChannel[d];}; int multiSlsDetector::getMaxNumberOfChannels(dimension d){return thisMultiDetector->maxNumberOfChannel[d];}; int multiSlsDetector::getMaxMod(dimension d){ int ret=0, ret1; for (int idet=0; idetnumberOfDetectors; ++idet) { if (detectors[idet]) { ret1=detectors[idet]->getNMaxMod(d); #ifdef VERBOSE cout << "detector " << idet << " maxmods " << ret1 << " in direction " << d << endl; #endif ret+=ret1; } } #ifdef VERBOSE cout << "max mods in direction "<< d << " is " << ret << endl; #endif return ret; } int multiSlsDetector::getMaxNumberOfModules(dimension d) { int ret=0, ret1; for (int idet=0; idetnumberOfDetectors; ++idet) { if (detectors[idet]) { ret1=detectors[idet]->getMaxNumberOfModules(d); ret+=ret1; } } return ret; } int multiSlsDetector::getFlippedData(dimension d){ int ret=-100,ret1; for (int idet=0; idetnumberOfDetectors; ++idet) if (detectors[idet]){ ret1=detectors[idet]->getFlippedData(d); if(ret==-100) ret=ret1; else if (ret!=ret1) ret=-1; } return ret; } int multiSlsDetector::setNumberOfModules(int p, dimension d) { int ret=0;//, ret1; int nm, mm, nt=p; thisMultiDetector->dataBytes=0; thisMultiDetector->numberOfChannels=0; for (int idet=0; idetnumberOfDetectors; ++idet) { // cout << "detector " << idet << endl; if (detectors[idet]) { if (p<0) nm=p; else { mm=detectors[idet]->getMaxNumberOfModules(); //mm=detectors[idet]->getMaxMods(); if (nt>mm) { nm=mm; nt-=nm; } else { nm=nt; nt-=nm; } } ret+=detectors[idet]->setNumberOfModules(nm); if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<dataBytes+=detectors[idet]->getDataBytes(); thisMultiDetector->numberOfChannels+=detectors[idet]->getTotalNumberOfChannels(); } } if(p != -1) updateOffsets(); return ret; } int multiSlsDetector::setFlippedData(dimension d, int value){ int ret=-100,ret1; for (int idet=0; idetnumberOfDetectors; ++idet) if (detectors[idet]){ ret1=detectors[idet]->setFlippedData(d,value); if(ret==-100) ret=ret1; else if (ret!=ret1) ret=-1; } return ret; } int multiSlsDetector::decodeNMod(int i, int &id, int &im) { #ifdef VERBOSE cout << " Module " << i << " belongs to detector " << id << endl;; cout << getMaxMods(); #endif if (i<0 || i>=getMaxMods()) { id=-1; im=-1; #ifdef VERBOSE cout << " A---------" << id << " position " << im << endl; #endif return -1; } int nm; for (int idet=0; idetnumberOfDetectors; ++idet) { if (detectors[idet]) { nm=detectors[idet]->getNMods(); if (nm>i) { id=idet; im=i; #ifdef VERBOSE cout << " B---------" <=0) { if (detectors[id]) { ret = detectors[id]->getId(mode, im); if(detectors[id]->getErrorMask()) setErrorMask(getErrorMask()|(1<=0) { if (detectors[id]) { ret = detectors[id]->digitalTest(mode, im); if(detectors[id]->getErrorMask()) setErrorMask(getErrorMask()|(1<=0) { if (detectors[id]) { ret = detectors[id]->executeTrimming(mode, par1, par2, im); if(detectors[id]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors]; for(int idet=0; idetnumberOfDetectors; ++idet){ if(detectors[idet]){ iret[idet]= new int(-1); Task* task = new Task(new func4_t(&slsDetector::executeTrimming, detectors[idet],mode,par1,par2,imod,iret[idet])); threadpool->add_task(task); } } threadpool->startExecuting(); threadpool->wait_for_tasks_to_complete(); for(int idet=0; idetnumberOfDetectors; ++idet){ if(detectors[idet]){ if(iret[idet] != NULL){ if (ret==-100) ret=*iret[idet]; else if (ret!=*iret[idet]) ret=-1; delete iret[idet]; }else ret=-1; if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; ++i) { if (detectors[i]) { ret=detectors[i]->programFPGA(fname); if(detectors[i]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; ++i) { if (detectors[i]) { ret=detectors[i]->resetFPGA(); if(detectors[i]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; ++i) { if (detectors[i]) { ret=detectors[i]->powerChip(ival); if(detectors[i]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; ++i) { if (detectors[i]) { ret=detectors[i]->setAutoComparatorDisableMode(ival); if(detectors[i]->getErrorMask()) setErrorMask(getErrorMask()|(1<=0) { if(detectors[id]){ ret = detectors[id]->loadSettingsFile(fname, im); if(detectors[id]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors]; for(int idet=0; idetnumberOfDetectors; ++idet){ if(detectors[idet]){ iret[idet]= new int(OK); Task* task = new Task(new func2_t(&slsDetector::loadSettingsFile, detectors[idet],fname,imod,iret[idet])); threadpool->add_task(task); } } threadpool->startExecuting(); threadpool->wait_for_tasks_to_complete(); for(int idet=0; idetnumberOfDetectors; ++idet){ if(detectors[idet]){ if(iret[idet] != NULL){ if(*iret[idet] != OK) ret = FAIL; delete iret[idet]; }else ret = FAIL; if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<=0) { if (detectors[id]) { ret = detectors[id]->saveSettingsFile(fname, im); if(detectors[id]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; ++idet) { if (detectors[idet]) { ret=detectors[idet]->saveSettingsFile(fname, imod); if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<=0) { if(detectors[id]){ ret = detectors[id]->setAllTrimbits(val,im); if(detectors[id]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors]; for(int idet=0; idetnumberOfDetectors; ++idet){ if(detectors[idet]){ iret[idet]= new int(-1); Task* task = new Task(new func2_t(&slsDetector::setAllTrimbits, detectors[idet],val,imod,iret[idet])); threadpool->add_task(task); } } threadpool->startExecuting(); threadpool->wait_for_tasks_to_complete(); for(int idet=0; idetnumberOfDetectors; ++idet){ if(detectors[idet]){ if(iret[idet] != NULL){ if (ret==-100) ret=*iret[idet]; else if (ret!=*iret[idet]) ret=-1; delete iret[idet]; }else ret=-1; if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<=0) { if(detectors[id]){ ret = detectors[id]->loadCalibrationFile(fname, im); if(detectors[id]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors]; for(int idet=0; idetnumberOfDetectors; ++idet){ if(detectors[idet]){ iret[idet]= new int(OK); Task* task = new Task(new func2_t(&slsDetector::loadCalibrationFile, detectors[idet],fname,imod,iret[idet])); threadpool->add_task(task); } } threadpool->startExecuting(); threadpool->wait_for_tasks_to_complete(); for(int idet=0; idetnumberOfDetectors; ++idet){ if(detectors[idet]){ if(iret[idet] != NULL){ if(*iret[idet] != OK) ret = FAIL; delete iret[idet]; }else ret = FAIL; if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<=0) { if (detectors[id]) { ret = detectors[id]->saveCalibrationFile(fname, im); if(detectors[id]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; ++idet) { if (detectors[idet]) { ret=detectors[idet]->saveCalibrationFile(fname, imod); if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; ++i) { if (detectors[i]) { ret=detectors[i]->writeRegister(addr,val); if(detectors[i]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; ++i) { if (detectors[i]) { ret=detectors[i]->writeAdcRegister(addr,val); if(detectors[i]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; ++i) { if (detectors[i]) { ret=detectors[i]->readRegister(addr); if(detectors[i]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; ++i) { if (detectors[i]) { ret1=detectors[i]->setBit(addr,n); if(detectors[i]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; ++i) { if (detectors[i]) { ret1=detectors[i]->clearBit(addr,n); if(detectors[i]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; ++i) { if (detectors[i]) { std::cout << std::endl << "#Detector " << i << ":" << std::endl; ret=detectors[i]->printReceiverConfiguration(); if(detectors[i]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; for (int i=0; ifreeSharedMemory(); } } thisMultiDetector->numberOfDetectors=0; multiSlsDetectorClient *cmd; setAcquiringFlag(false); clearAllErrorMask(); string ans; string str; ifstream infile; int iargval; int interrupt=0; char *args[1000]; char myargs[1000][1000]; string sargname, sargval; int iline=0; std::cout<< "config file name "<< fname << std::endl; infile.open(fname.c_str(), ios_base::in); if (infile.is_open()) { while (infile.good() and interrupt==0) { sargname="none"; sargval="0"; getline(infile,str); ++iline; #ifdef VERBOSE std::cout<< str << std::endl; #endif if (str.find('#')!=string::npos) { #ifdef VERBOSE std::cout<< "Line is a comment " << std::endl; std::cout<< str << std::endl; #endif continue; } else if (str.length()<2) { #ifdef VERBOSE std::cout<< "Empty line " << std::endl; #endif continue; } else { istringstream ssstr(str); iargval=0; while (ssstr.good()) { ssstr >> sargname; //if (ssstr.good()) { #ifdef VERBOSE std::cout<< iargval << " " << sargname << std::endl; #endif strcpy(myargs[iargval], sargname.c_str()); args[iargval]=myargs[iargval]; #ifdef VERBOSE std::cout<< "--" << iargval << " " << args[iargval] << std::endl; #endif ++iargval; //} } #ifdef VERBOSE cout << endl; for (int ia=0; iaexecuteLine(1,args,GET_ACTION) << std::endl; ++iline; // hostname of the detectors cout << iline << " " << names[iline] << endl; strcpy(args[0],names[iline].c_str()); outfile << names[iline] << " " << cmd->executeLine(1,args,GET_ACTION) << std::endl; ++iline; // single detector configuration for (int i=0; inumberOfDetectors; ++i) { // sprintf(ext,".det%d",i); if (detectors[i]) { outfile << endl; ret1 = detectors[i]->writeConfigurationFile(outfile,i); if(detectors[i]->getErrorMask()) setErrorMask(getErrorMask()|(1<executeLine(1,args,GET_ACTION) << std::endl; ++iline; } delete cmd; outfile.close(); #ifdef VERBOSE std::cout<< "wrote " <numberOfDetectors; ++i) { if (detectors[i]) { n=detectors[i]->getTotalNumberOfChannels(); if (nch_leftwriteDataFile(outfile,n, data+off, pe, pa, dataformat, choff); fileIOStatic::writeDataFile(outfile,n, data+off, pe, pa, dataformat, choff); if(detectors[i]->getErrorMask()) setErrorMask(getErrorMask()|(1<getMaxNumberOfChannels(); off+=n; if (pe) pe=err+off; if (pa) pa=ang+off; } } outfile.close(); return OK; } else { std::cout<< "Could not open file " << fname << "for writing"<< std::endl; return FAIL; } } int multiSlsDetector::writeDataFile(string fname, int *data) { ofstream outfile; int choff=0, off=0; #ifdef VERBOSE cout << "using overloaded multiSlsDetector function to write raw data file " << endl; #endif if (data==NULL) return FAIL; outfile.open (fname.c_str(),ios_base::out); if (outfile.is_open()) { for (int i=0; inumberOfDetectors; ++i) { if (detectors[i]) { #ifdef VERBOSE cout << " write " << i << " position " << off << " offset " << choff << endl; #endif detectors[i]->writeDataFile(outfile, detectors[i]->getTotalNumberOfChannels(), data+off, choff); if(detectors[i]->getErrorMask()) setErrorMask(getErrorMask()|(1<getMaxNumberOfChannels(); off+=detectors[i]->getTotalNumberOfChannels(); } } outfile.close(); return OK; } else { std::cout<< "Could not open file " << fname << "for writing"<< std::endl; return FAIL; } } int multiSlsDetector::readDataFile(string fname, double *data, double *err, double *ang, char dataformat){ #ifdef VERBOSE cout << "using overloaded multiSlsDetector function to read formatted data file " << endl; #endif ifstream infile; int iline=0;//ichan, //int interrupt=0; string str; int choff=0, off=0; double *pe=err, *pa=ang; #ifdef VERBOSE std::cout<< "Opening file "<< fname << std::endl; #endif infile.open(fname.c_str(), ios_base::in); if (infile.is_open()) { for (int i=0; inumberOfDetectors; ++i) { if (detectors[i]) { iline+=detectors[i]->readDataFile(detectors[i]->getTotalNumberOfChannels(), infile, data+off, pe, pa, dataformat, choff); if(detectors[i]->getErrorMask()) setErrorMask(getErrorMask()|(1<getMaxNumberOfChannels(); off+=detectors[i]->getTotalNumberOfChannels(); if (pe) pe=pe+off; if (pa) pa=pa+off; } } infile.close(); } else { std::cout<< "Could not read file " << fname << std::endl; return -1; } return iline; } int multiSlsDetector::readDataFile(string fname, int *data) { #ifdef VERBOSE cout << "using overloaded multiSlsDetector function to read raw data file " << endl; #endif ifstream infile; int iline=0;//ichan, //int interrupt=0; string str; int choff=0, off=0; #ifdef VERBOSE std::cout<< "Opening file "<< fname << std::endl; #endif infile.open(fname.c_str(), ios_base::in); if (infile.is_open()) { for (int i=0; inumberOfDetectors; ++i) { if (detectors[i]) { iline+=detectors[i]->readDataFile(infile, data+off,detectors[i]->getTotalNumberOfChannels(), choff); if(detectors[i]->getErrorMask()) setErrorMask(getErrorMask()|(1<getMaxNumberOfChannels(); off+=detectors[i]->getTotalNumberOfChannels(); } } infile.close(); } else { std::cout<< "Could not read file " << fname << std::endl; return -1; } return iline; } //receiver int multiSlsDetector::setReceiverOnline(int off) { if (off != GET_ONLINE_FLAG) { int ret=-100; if(!threadpool){ cout << "Error in creating threadpool. Exiting" << endl; return -1; }else{ //return storage values int* iret[thisMultiDetector->numberOfDetectors]; for(int idet=0; idetnumberOfDetectors; ++idet){ if(detectors[idet]){ iret[idet]= new int(-1); Task* task = new Task(new func1_t(&slsDetector::setReceiverOnline, detectors[idet],off,iret[idet])); threadpool->add_task(task); } } threadpool->startExecuting(); threadpool->wait_for_tasks_to_complete(); for(int idet=0; idetnumberOfDetectors; ++idet){ if(detectors[idet]){ if(iret[idet] != NULL){ if (ret==-100) ret=*iret[idet]; else if (ret!=*iret[idet]) ret=-1; delete iret[idet]; }else ret=-1; if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<receiverOnlineFlag=ret; } return thisMultiDetector->receiverOnlineFlag; } string multiSlsDetector::checkReceiverOnline() { string retval1 = "",retval; for (int idet=0; idetnumberOfDetectors; ++idet) { if (detectors[idet]) { retval=detectors[idet]->checkReceiverOnline(); if(!retval.empty()){ retval1.append(retval); retval1.append("+"); } } } return retval1; } string multiSlsDetector::setFilePath(string s) { string ret="errorerror", ret1; //if the sls file paths are different, it should be realized by always using setfilepath even if string empty //if(!s.empty()){ for (int idet=0; idetnumberOfDetectors; ++idet) { if (detectors[idet]) { ret1=detectors[idet]->setFilePath(s); if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; if(!s.empty()){ fileIO::setFileName(s); if (thisMultiDetector->receiverOnlineFlag == ONLINE_FLAG) s=createReceiverFilePrefix(); } if(!threadpool){ cout << "Error in creating threadpool. Exiting" << endl; return string(""); } else { string* sret[thisMultiDetector->numberOfDetectors]; for(int idet=0; idet(&slsDetector::setFileName, detectors[idet],s,sret[idet])); threadpool->add_task(task); } } threadpool->startExecuting(); threadpool->wait_for_tasks_to_complete(); for(int idet=0; idetgetErrorMask()) setErrorMask(getErrorMask()|(1<receiverOnlineFlag == ONLINE_FLAG) && ((ret != "error") || (ret != ""))) { #ifdef VERBOSE std::cout << "Complete file prefix from receiver: " << ret << std::endl; #endif fileIO::setFileName(getNameFromReceiverFilePrefix(ret)); } return ret; } slsReceiverDefs::fileFormat multiSlsDetector::setFileFormat(fileFormat f) { int ret=-100, ret1; for (int idet=0; idetnumberOfDetectors; ++idet) { if (detectors[idet]) { ret1=(int)detectors[idet]->setFileFormat(f); if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors]; for(int idet=0; idetnumberOfDetectors; ++idet){ if(detectors[idet]){ iret[idet]= new int(-1); Task* task = new Task(new func1_t(&slsDetector::setFileIndex, detectors[idet],i,iret[idet])); threadpool->add_task(task); } } threadpool->startExecuting(); threadpool->wait_for_tasks_to_complete(); for(int idet=0; idetnumberOfDetectors; ++idet){ if(detectors[idet]){ if(iret[idet] != NULL){ if (ret==-100) ret=*iret[idet]; else if (ret!=*iret[idet]) ret=-1; delete iret[idet]; }else ret=-1; if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; if(!threadpool){ cout << "Error in creating threadpool. Exiting" << endl; return FAIL; }else{ int* iret[posmax-posmin]; for(int idet=posmin; idetmasterPosition) && (detectors[idet])){ iret[idet]= new int(OK); Task* task = new Task(new func0_t(&slsDetector::startReceiver, detectors[idet],iret[idet])); threadpool->add_task(task); } } threadpool->startExecuting(); threadpool->wait_for_tasks_to_complete(); for(int idet=posmin; idetmasterPosition) && (detectors[idet])){ if(iret[idet] != NULL){ if(*iret[idet] != OK) ret = FAIL; delete iret[idet]; }else ret = FAIL; if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<masterPosition; if (thisMultiDetector->masterPosition>=0) { if (detectors[i]) { ret1=detectors[i]->startReceiver(); if(detectors[i]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; i=thisMultiDetector->masterPosition; if (thisMultiDetector->masterPosition>=0) { if (detectors[i]) { ret1=detectors[i]->stopReceiver(); if(detectors[i]->getErrorMask()) setErrorMask(getErrorMask()|(1<masterPosition) && (detectors[idet])){ iret[idet]= new int(OK); Task* task = new Task(new func0_t(&slsDetector::stopReceiver, detectors[idet],iret[idet])); threadpool->add_task(task); } } threadpool->startExecuting(); threadpool->wait_for_tasks_to_complete(); for(int idet=posmin; idetmasterPosition) && (detectors[idet])){ if(iret[idet] != NULL){ if(*iret[idet] != OK) ret = FAIL; delete iret[idet]; }else ret = FAIL; if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<masterPosition; if (thisMultiDetector->masterPosition>=0) { if (detectors[i]) { s1=detectors[i]->startReceiverReadout(); if(detectors[i]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; ++i) { if (detectors[i]) { s=detectors[i]->startReceiverReadout(); if(detectors[i]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; i=thisMultiDetector->masterPosition; if (thisMultiDetector->masterPosition>=0) { if (detectors[i]) { ret=detectors[i]->getReceiverStatus(); if(detectors[i]->getErrorMask()) setErrorMask(getErrorMask()|(1<masterPosition) && (detectors[idet])){ iret[idet]= new runStatus(ERROR); Task* task = new Task(new func0_t(&slsDetector::getReceiverStatus, detectors[idet],iret[idet])); threadpool->add_task(task); } } threadpool->startExecuting(); threadpool->wait_for_tasks_to_complete(); for(int idet=posmin; idetmasterPosition) && (detectors[idet])){ if(iret[idet] != NULL){ if(*iret[idet] == (int)ERROR) ret = ERROR; if(*iret[idet] != IDLE) ret = *iret[idet]; delete iret[idet]; }else ret = ERROR; if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<masterPosition; if (i >=0 ) { if (detectors[i]){ ret=detectors[i]->getFramesCaughtByReceiver(); if(detectors[i]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; ++i) if (detectors[i]){ ret=detectors[i]->getFramesCaughtByReceiver(); if(detectors[i]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; if(!threadpool){ cout << "Error in creating threadpool. Exiting" << endl; return -1; }else{ int* iret[posmax]; for(int idet=0; idetnumberOfDetectors; ++idet){ if(detectors[idet]){ iret[idet]= new int(0); Task* task = new Task(new func0_t(&slsDetector::getFramesCaughtByReceiver, detectors[idet],iret[idet])); threadpool->add_task(task); } } threadpool->startExecuting(); threadpool->wait_for_tasks_to_complete(); for(int idet=0; idetgetErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors) || (ret == -1)) return ret; ret=(int)(ret1/thisMultiDetector->numberOfDetectors); return ret; } int multiSlsDetector::getReceiverCurrentFrameIndex() { int ret=0,ret1=0; for (int i=0; inumberOfDetectors; ++i) if (detectors[i]){ ret1+=detectors[i]->getReceiverCurrentFrameIndex(); if(detectors[i]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors) return ret; ret=(int)(ret1/thisMultiDetector->numberOfDetectors); return ret; } int multiSlsDetector::resetFramesCaught() { int ret=OK; int posmax = thisMultiDetector->numberOfDetectors; if(!threadpool){ cout << "Error in creating threadpool. Exiting" << endl; return FAIL; }else{ int* iret[posmax]; for(int idet=0; idetnumberOfDetectors; ++idet){ if(detectors[idet]){ iret[idet]= new int(OK); Task* task = new Task(new func0_t(&slsDetector::resetFramesCaught, detectors[idet],iret[idet])); threadpool->add_task(task); } } threadpool->startExecuting(); threadpool->wait_for_tasks_to_complete(); for(int idet=0; idetgetErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; int numSocketsPerDetector = 1; if(getDetectorsType() == EIGER){ numSocketsPerDetector = 2; } numSockets *= numSocketsPerDetector; if(destroy){ cprintf(MAGENTA,"Going to destroy data sockets\n"); //close socket for(int i=0;igetClientStreamingPort().c_str(),"%d",&portnum); portnum += (i%numSocketsPerDetector); zmqSocket[i] = new ZmqSocket(detectors[i/numSocketsPerDetector]->getReceiver().c_str(), portnum); if (zmqSocket[i]->IsError()) { cprintf(RED, "Error: Could not create Zmq socket on port %d\n", portnum); createReceivingDataSockets(true); return FAIL; } printf("Zmq Client[%d] at %s\n",i, zmqSocket[i]->GetZmqServerAddress()); } client_downstream = true; cout << "Receiving Data Socket(s) created" << endl; return OK; } int multiSlsDetector::getData(const int isocket, const bool masking, int* image, const int size, uint64_t &acqIndex, uint64_t &frameIndex, uint32_t &subframeIndex, string &filename) { //fail is on parse error or end of acquisition if (!zmqSocket[isocket]->ReceiveHeader(isocket, acqIndex, frameIndex, subframeIndex, filename)) return FAIL; //receiving incorrect size is replaced by 0xFF zmqSocket[isocket]->ReceiveData(isocket, image, size); //jungfrau masking adcval if(masking){ unsigned int snel = size/sizeof(int); for(unsigned int i=0;inumberOfDetectors; int numSocketsPerSLSDetector = 1; bool jungfrau = false; switch(getDetectorsType()){ case EIGER: numSocketsPerSLSDetector = 2; numSockets *= numSocketsPerSLSDetector; maxX = thisMultiDetector->numberOfChannel[X]; maxY = thisMultiDetector->numberOfChannel[Y]; break; case JUNGFRAU: jungfrau = true; break; default: break; } //gui variables uint64_t currentAcquisitionIndex = -1; uint64_t currentFrameIndex = -1; uint32_t currentSubFrameIndex = -1; string currentFileName = ""; //getting sls values int slsdatabytes = 0, slsmaxchannels = 0, slsmaxX = 0, slsmaxY=0, nx=0, ny=0; double bytesperchannel = 0; if(detectors[0]){ slsdatabytes = detectors[0]->getDataBytes(); slsmaxchannels = detectors[0]->getMaxNumberOfChannels(); bytesperchannel = (double)slsdatabytes/(double)slsmaxchannels; slsmaxX = detectors[0]->getTotalNumberOfChannels(X); slsmaxY = detectors[0]->getTotalNumberOfChannels(Y); } //getting multi values nx = getTotalNumberOfChannels(slsDetectorDefs::X); ny = getTotalNumberOfChannels(slsDetectorDefs::Y); //calculating offsets (for eiger interleaving ports) int offsetX[numSockets]; int offsetY[numSockets]; int bottom[numSockets]; if(maxX){ for(int i=0; ioffsetY[i/numSocketsPerSLSDetector] + slsmaxY)) * maxX * bytesperchannel; //the left half or right half if(!(i%numSocketsPerSLSDetector)) offsetX[i] = thisMultiDetector->offsetX[i/numSocketsPerSLSDetector]; else offsetX[i] = thisMultiDetector->offsetX[i/numSocketsPerSLSDetector] + (slsmaxX/numSocketsPerSLSDetector); offsetX[i] *= bytesperchannel; bottom[i] = detectors[i/numSocketsPerSLSDetector]->getFlippedData(X);/*only for eiger*/ } } int expectedslssize = slsdatabytes/numSocketsPerSLSDetector; int* image = new int[(expectedslssize/sizeof(int))](); int nel=(thisMultiDetector->dataBytes)/sizeof(int); if(nel <= 0){ cprintf(RED,"Error: Multislsdetector databytes not valid : %d\n", thisMultiDetector->dataBytes); return; } int* multiframe=new int[nel](); int nch; bool runningList[numSockets]; bool connectList[numSockets]; for(int i = 0; i < numSockets; ++i) { if(!zmqSocket[i]->Connect()) { connectList[i] = true; runningList[i] = true; } else { connectList[i] = false; cprintf(RED,"Error: Could not connect to socket %s\n",zmqSocket[i]->GetZmqServerAddress()); runningList[i] = false; } } int numRunning = numSockets; //wait for real time acquisition to start bool running = true; sem_wait(&sem_newRTAcquisition); if(checkJoinThread()) running = false; //exit when last message for each socket received while(running){ memset(((char*)multiframe),0xFF,slsdatabytes*thisMultiDetector->numberOfDetectors); //reset frame memory //get each frame for(int isocket=0; isocketDisconnect(); //free resources delete [] image; delete[] multiframe; } int multiSlsDetector::lockReceiver(int lock) { int ret=-100, ret1; for (int idet=0; idetnumberOfDetectors; ++idet) { if (detectors[idet]) { ret1=detectors[idet]->lockReceiver(lock); if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; ++idet) { if (detectors[idet]) { s=detectors[idet]->getReceiverLastClientIP(); if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; ++idet) { if (detectors[idet]) { iv=detectors[idet]->exitReceiver(); if (iv==OK) ival=iv; } } return ival; } int multiSlsDetector::enableWriteToFile(int enable){ int ret=-100, ret1; for (int idet=0; idetnumberOfDetectors; ++idet) { if (detectors[idet]) { ret1=detectors[idet]->enableWriteToFile(enable); if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; ++idet) { if (detectors[idet]) { ret1=detectors[idet]->overwriteFile(enable); if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; fileIO::setFrameIndex(index); if(!threadpool){ cout << "Error in creating threadpool. Exiting" << endl; return -1; }else{ //return storage values int* iret[thisMultiDetector->numberOfDetectors]; for(int idet=0; idet(&slsDetector::setFrameIndex, detectors[idet],index,iret[idet])); threadpool->add_task(task); } } threadpool->startExecuting(); threadpool->wait_for_tasks_to_complete(); for(int idet=0; idetnumberOfDetectors; ++idet){ if(detectors[idet]){ if(iret[idet] != NULL){ if (ret==-100) ret=*iret[idet]; else if (ret!=*iret[idet]) ret=-1; delete iret[idet]; }else ret=-1; if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; ++idet) { if (detectors[idet]) { //if the detector has error if(multiMask&(1<getErrorMask(); #ifdef VERYVERBOSE //append sls det error mask sprintf(sNumber,"0x%lx",slsMask); retval.append("Error Mask " + string(sNumber)+string("\n")); #endif //get the error critical level if((slsMask>0xFFFFFFFF)|critical) critical = 1; //append error message retval.append(errorDefs::getErrorMessage(slsMask)); } } } } return retval; } int64_t multiSlsDetector::clearAllErrorMask(){ clearErrorMask(); clearNotAddedList(); for (int idet=0; idetnumberOfDetectors; ++idet) if (detectors[idet]) detectors[idet]->clearErrorMask(); return getErrorMask(); } int multiSlsDetector::calibratePedestal(int frames){ int ret=-100, ret1; for (int idet=0; idetnumberOfDetectors; ++idet) { if (detectors[idet]) { ret1=detectors[idet]->calibratePedestal(frames); if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; ++idet) { if (detectors[idet]) { ret1=detectors[idet]->setReadReceiverFrequency(freq); if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; ++idet) { if (detectors[idet]) { ret1=detectors[idet]->setReceiverReadTimer(time_in_ms); if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<= 0){ //destroy data threads if (!enable) createReceivingDataSockets(true); //create data threads else { if(createReceivingDataSockets() == FAIL){ std::cout << "Could not create data threads in client." << std::endl; //only for the first det as theres no general one setErrorMask(getErrorMask()|(1<<0)); detectors[0]->setErrorMask((detectors[0]->getErrorMask())|(DATA_STREAMING)); } } } return client_downstream; } int multiSlsDetector::enableDataStreamingFromReceiver(int enable){ if(enable >= 0){ int ret=-100; if(!threadpool){ cout << "Error in creating threadpool. Exiting" << endl; return -1; }else{ //return storage values int* iret[thisMultiDetector->numberOfDetectors]; for(int idet=0; idetnumberOfDetectors; ++idet){ if(detectors[idet]){ iret[idet]= new int(-1); Task* task = new Task(new func1_t(&slsDetector::enableDataStreamingFromReceiver, detectors[idet],enable,iret[idet])); threadpool->add_task(task); } } threadpool->startExecuting(); threadpool->wait_for_tasks_to_complete(); for(int idet=0; idetnumberOfDetectors; ++idet){ if(detectors[idet]){ if(iret[idet] != NULL){ if (ret==-100) ret=*iret[idet]; else if (ret!=*iret[idet]) ret=-1; delete iret[idet]; }else ret=-1; if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<receiver_upstream = ret; } return thisMultiDetector->receiver_upstream; } int multiSlsDetector::enableReceiverCompression(int i){ int ret=-100,ret1; for (int idet=0; idetnumberOfDetectors; ++idet) if (detectors[idet]){ ret1=detectors[idet]->enableReceiverCompression(i); if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; ++idet) if (detectors[idet]){ ret1=detectors[idet]->enableTenGigabitEthernet(i); if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; ++idet) if (detectors[idet]){ ret1=detectors[idet]->setReceiverFifoDepth(i); if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; ++idet) if (detectors[idet]){ ret1=detectors[idet]->setReceiverSilentMode(i); if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<0) { while (fread(&word, sizeof(word), 1,fd)) { for (int idet=0; idetnumberOfDetectors; ++idet) if (detectors[idet]){ detectors[idet]->setCTBWord(addr,word); } // cout << hex << addr << " " << word << dec << endl; ++addr; } fclose(fd); } else return -1; return addr; } /** Writes a pattern word to the CTB @param addr address of the word, -1 is I/O control register, -2 is clk control register @param word 64bit word to be written, -1 gets @returns actual value */ uint64_t multiSlsDetector::setCTBWord(int addr,uint64_t word) { uint64_t ret=-100,ret1; for (int idet=0; idetnumberOfDetectors; ++idet) if (detectors[idet]){ ret1=detectors[idet]->setCTBWord(addr, word); if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<=0 @param stop stop address if >=0 @param n number of loops (if level >=0) @returns OK/FAIL */ int multiSlsDetector::setCTBPatLoops(int level,int &start, int &stop, int &n) { int ret=-100,ret1; for (int idet=0; idetnumberOfDetectors; ++idet) if (detectors[idet]){ ret1=detectors[idet]->setCTBPatLoops(level, start, stop, n); if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; ++idet) if (detectors[idet]){ ret1=detectors[idet]->setCTBPatWaitAddr(level, addr); if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors; ++idet) if (detectors[idet]){ ret1=detectors[idet]->setCTBPatWaitTime(level,t); if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors]; for(int idet=0; idetnumberOfDetectors; ++idet){ if(detectors[idet]){ iret[idet]= new int(-1); Task* task = new Task(new func3_t(&slsDetector::pulsePixel, detectors[idet],n,x,y,iret[idet])); threadpool->add_task(task); } } threadpool->startExecuting(); threadpool->wait_for_tasks_to_complete(); for(int idet=0; idetnumberOfDetectors; ++idet){ if(detectors[idet]){ if(iret[idet] != NULL){ if (ret==-100) ret=*iret[idet]; else if (ret!=*iret[idet]) ret=-1; delete iret[idet]; }else ret=-1; if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors]; for(int idet=0; idetnumberOfDetectors; ++idet){ if(detectors[idet]){ iret[idet]= new int(-1); Task* task = new Task(new func3_t(&slsDetector::pulsePixelNMove, detectors[idet],n,x,y,iret[idet])); threadpool->add_task(task); } } threadpool->startExecuting(); threadpool->wait_for_tasks_to_complete(); for(int idet=0; idetnumberOfDetectors; ++idet){ if(detectors[idet]){ if(iret[idet] != NULL){ if (ret==-100) ret=*iret[idet]; else if (ret!=*iret[idet]) ret=-1; delete iret[idet]; }else ret=-1; if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<numberOfDetectors]; for(int idet=0; idetnumberOfDetectors; ++idet){ if(detectors[idet]){ iret[idet]= new int(-1); Task* task = new Task(new func1_t(&slsDetector::pulseChip, detectors[idet],n,iret[idet])); threadpool->add_task(task); } } threadpool->startExecuting(); threadpool->wait_for_tasks_to_complete(); for(int idet=0; idetnumberOfDetectors; ++idet){ if(detectors[idet]){ if(iret[idet] != NULL){ if (ret==-100) ret=*iret[idet]; else if (ret!=*iret[idet]) ret=-1; delete iret[idet]; }else ret=-1; if(detectors[idet]->getErrorMask()) setErrorMask(getErrorMask()|(1<acquiringFlag = b; } bool multiSlsDetector::getAcquiringFlag(){ return thisMultiDetector->acquiringFlag; } bool multiSlsDetector::isAcquireReady() { if (thisMultiDetector->acquiringFlag) { std::cout << "Acquire has already started. If previous acquisition terminated unexpectedly, reset busy flag to restart.(sls_detector_put busy 0)" << std::endl; return FAIL; } thisMultiDetector->acquiringFlag = true; return OK; }