slsDetector/slsDetector.h

Go to the documentation of this file.
00001 
00002 
00003 
00004 #ifndef SLS_DETECTOR_H
00005 #define SLS_DETECTOR_H
00006 
00007 #include "MySocketTCP.h"
00008 #include <iostream>
00009 #include <fstream>
00010 #include <iomanip>
00011 #include <cstring>
00012 #include <string>
00013 #include <sstream>
00014  #include <queue>
00015 extern "C" {
00016  #include <pthread.h>
00017 }
00018  #include <fcntl.h>
00019  #include <unistd.h>
00020  #include <sys/stat.h>
00021  #include <sys/types.h>
00022  #include <sys/uio.h>
00023 
00024 
00025 #define MAX_TIMERS 10
00026 #define MAX_ROIS 100
00027 #define MAX_BADCHANS 2000
00028 #define MAXPOS 50
00029 
00030 #define NMODMAXX 24
00031 #define NMODMAXY 24
00032 #define MAXMODS 36
00033 #define NCHIPSMAX 10
00034 #define NCHANSMAX 65536
00035 #define NDACSMAX 16
00036 
00037 #define DEFAULT_HOSTNAME  "localhost"
00038 #define DEFAULT_SHM_KEY  5678
00039 
00040 #define defaultTDead {170,90,750}
00041 
00042 using namespace std;
00078 class slsDetector {
00079 
00080 
00081 
00082  public:
00083   
00084 
00086   enum {GET_ONLINE_FLAG, 
00087         OFFLINE_FLAG, 
00088         ONLINE_FLAG 
00089   };
00090 
00091 
00092 #include "sls_detector_defs.h"
00093 
00094 
00099 typedef  struct sharedSlsDetector {
00101     int alreadyExisting;
00102     
00104     char hostname[MAX_STR_LENGTH];
00106     int controlPort;
00108     int stopPort;
00110     int dataPort;
00111 
00113     detectorType myDetectorType;
00114 
00115 
00117     char trimDir[MAX_STR_LENGTH];
00119     char calDir[MAX_STR_LENGTH];
00121     int nTrimEn;
00123     int trimEnergies[100];
00124 
00125            
00127     int fileIndex;
00129     char filePath[MAX_STR_LENGTH];
00131     char fileName[MAX_STR_LENGTH];
00132 
00133   /* size of the detector */
00134     
00136     int nMod[2];
00138     int nMods;
00140     int nModMax[2];
00142     int nModsMax;
00144     int nChans;
00146     int nChips;
00148     int nDacs;
00150     int nAdcs;
00152     int dynamicRange;
00154     int dataBytes;
00155     
00157     int correctionMask;
00159     float tDead;
00161     int nBadChans;
00163     int badChansList[MAX_BADCHANS];
00165     int nBadFF;
00167     int badFFList[MAX_BADCHANS];
00168     
00170     angleConversionConstant angOff[MAXMODS];
00172     int angDirection;
00174     float fineOffset;
00176     float globalOffset;
00178     int numberOfPositions;
00180     float detPositions[MAXPOS];
00182   float binSize;
00183 
00184   /* infos necessary for the readout to determine the size of the data */
00186     int nROI;
00188     ROI roiLimits[MAX_ROIS];
00190     readOutFlags roFlags;
00191 
00192  /* detector setup - not needed */
00194     detectorSettings currentSettings;
00196     int currentThresholdEV;
00198     int64_t timerValue[MAX_TIMERS];
00200     int clkDiv;
00201     
00202   /*offsets*/
00204     int ffoff;
00206     int fferroff;
00208     int modoff;
00210     int dacoff;
00212     int adcoff;
00214     int chipoff;
00216     int chanoff;
00217 
00218 } sharedSlsDetector;
00219 
00220 
00221 
00228   slsDetector(detectorType type=GENERIC, int id=0);
00229   //slsDetector(string  const fname);
00230   //  ~slsDetector(){while(dataQueue.size()>0){}};
00232   ~slsDetector(){};
00233 
00234 
00237   int setOnline(int const online);  
00240   int exists() {return thisDetector->alreadyExisting;};
00241 
00254   int readConfigurationFile(string const fname);  
00266   int writeConfigurationFile(string const fname);
00267 
00268 
00269   /* 
00270      It should be possible to dump all the settings of the detector (including trimbits, threshold energy, gating/triggering, acquisition time etc.
00271      in a file and retrieve it for repeating the measurement with identicals ettings, if necessary
00272   */
00278   int dumpDetectorSetup(string const fname);  
00284   int retrieveDetectorSetup(string const fname);
00285 
00297   int setTCPSocket(string const name="", int const control_port=-1, int const stop_port=-1, int const data_port=-1);
00299   char* getHostname() {return thisDetector->hostname;};
00301   int getControlPort() {return  thisDetector->controlPort;};
00303   int getStopPort() {return thisDetector->stopPort;};
00305   int getDataPort() {return thisDetector->dataPort;};
00306  
00307 
00308   /* I/O */
00310   char* getTrimDir() {return thisDetector->trimDir;};
00312   char* setTrimDir(string s) {sprintf(thisDetector->trimDir, s.c_str()); return thisDetector->trimDir;};
00319   int getTrimEn(int *en) {for (int ien=0; ien<thisDetector->nTrimEn; ien++) en[ien]=thisDetector->trimEnergies[ien]; return (thisDetector->nTrimEn);};
00320 
00328   sls_detector_module* readTrimFile(string fname,  sls_detector_module* myMod=NULL);
00329 
00338   int writeTrimFile(string fname, sls_detector_module mod); 
00339   
00347   int writeTrimFile(string fname, int imod);
00348 
00353   char* setFilePath(string s) {sprintf(thisDetector->filePath, s.c_str()); return thisDetector->filePath;};
00354 
00359   char* setFileName(string s) {sprintf(thisDetector->fileName, s.c_str()); return thisDetector->fileName;}; 
00360 
00365   int setFileIndex(int i) {thisDetector->fileIndex=i; return thisDetector->fileIndex;}; 
00366   
00371   char* getFilePath() {return thisDetector->filePath;};
00372   
00377   char* getFileName() {return thisDetector->fileName;};
00378 
00383   int getFileIndex() {return thisDetector->fileIndex;};
00384   
00396   string createFileName();
00397   
00411   int writeDataFile(string fname, float *data, float *err=NULL, float *ang=NULL, char dataformat='f', int nch=-1); 
00412   
00419   int writeDataFile(string fname, int *data);
00420   
00434   int readDataFile(string fname, float *data, float *err=NULL, float *ang=NULL, char dataformat='f', int nch=0);  
00435 
00442   int readDataFile(string fname, int *data);
00443 
00448   char* getCalDir() {return thisDetector->calDir;};
00453   char* setCalDir(string s) {sprintf(thisDetector->calDir, s.c_str()); return thisDetector->calDir;}; 
00461   int readCalibrationFile(string fname, float &gain, float &offset);
00469   int writeCalibrationFile(string fname, float gain, float offset);
00470 
00471 
00477   int readAngularConversion(string fname="");
00483   int writeAngularConversion(string fname="");
00484  
00485 
00486 
00487   /* Communication to server */
00488 
00489   // General purpose functions
00490 
00498   int execCommand(string cmd, string answer);
00499   
00506   int setDetectorType(detectorType type=GET_DETECTOR_TYPE);  
00507 
00514   int setDetectorType(string type);  
00515 
00521   void getDetectorType(char *type);
00522 
00523 
00524   // Detector configuration functions
00531   int setNumberOfModules(int n, dimension d=X); // if n=GET_FLAG returns the number of installed modules
00532 
00533   /*
00534     returns the instrinsic size of the detector (maxmodx, maxmody, nchans, nchips, ndacs
00535     enum numberOf {
00536     MAXMODX,
00537     MAXMODY,
00538     CHANNELS,
00539     CHIPS,
00540     DACS
00541     }
00542   */
00543 
00544 
00550   int getMaxNumberOfModules(dimension d=X); //
00551  
00552  
00559   externalSignalFlag setExternalSignalFlags(externalSignalFlag pol=GET_EXTERNAL_SIGNAL_FLAG , int signalindex=0);
00560 
00561  
00569   externalCommunicationMode setExternalCommunicationMode(externalCommunicationMode pol=GET_EXTERNAL_COMMUNICATION_MODE);
00570 
00571 
00572   // Tests and identification
00573  
00580   int64_t getId(idMode mode, int imod=0);
00587   int digitalTest(digitalTestMode mode, int imod=0);
00596   int* analogTest(analogTestMode mode);
00597   
00603   int enableAnalogOutput(int ichan);
00604   
00610   int enableAnalogOutput(int imod, int ichip, int ichan);
00611 
00620   int giveCalibrationPulse(float vcal, int npulses);
00621 
00622   // Expert Initialization functions
00623  
00624 
00632   int writeRegister(int addr, int val);
00633   
00640   int readRegister(int addr);
00641 
00649   float setDAC(float val, dacIndex index, int imod=-1);
00650   
00657   float getADC(dacIndex index, int imod=0); 
00658  
00668   int setChannel(int64_t reg, int ichan=-1, int ichip=-1, int imod=-1);  
00669 
00675   int setChannel(sls_detector_channel chan);
00676 
00684   sls_detector_channel getChannel(int ichan, int ichip, int imod);
00685   
00686 
00687 
00696   int setChip(int reg, int ichip=-1, int imod=-1);
00697   
00704   int setChip(sls_detector_chip chip); 
00705 
00714   sls_detector_chip getChip(int ichip, int imod);
00715 
00716 
00723   int setModule(int reg, int imod=-1); 
00724 
00731   int setModule(sls_detector_module module);
00732 
00738   sls_detector_module *getModule(int imod);
00739  
00740   // calibration functions
00741   //  int setCalibration(int imod, detectorSettings isettings, float gain, float offset);
00742   //int getCalibration(int imod, detectorSettings isettings, float &gain, float &offset);
00743   
00744 
00745   /*
00746     calibrated setup of the threshold
00747   */  
00753   int getThresholdEnergy(int imod=-1);  
00754 
00762   int setThresholdEnergy(int e_eV, int imod=-1, detectorSettings isettings=GET_SETTINGS); 
00763  
00769   detectorSettings getSettings(int imod=-1);  
00770 
00779   detectorSettings setSettings(detectorSettings isettings, int imod=-1);
00780 
00781 
00782 // Acquisition functions
00783 
00784 
00789   int startAcquisition();
00790 
00795   int stopAcquisition();
00796   
00801   int startReadOut();
00802 
00807   int getRunStatus();
00808 
00814   int* startAndReadAll();
00815   
00820   int startAndReadAllNoWait(); 
00821 
00827   int* getDataFromDetectorNoWait(); 
00828 
00834   int* readFrame(); 
00835 
00841   int* readAll();
00842 
00843   
00849   int* popDataQueue();
00850 
00857   int64_t setTimer(timerIndex index, int64_t t=-1);
00858 
00864   int64_t getTimeLeft(timerIndex index);
00865 
00872   int setClockDivider(int i) {cout << "not implemented any longer!"<< endl; thisDetector->clkDiv=i;return thisDetector->clkDiv;};  
00873 
00880   int getClockDivider() {return thisDetector->clkDiv;};  
00881 
00888   int setSetLength(int i) {cout << "not implemented any longer!"<< endl; thisDetector->clkDiv=i;return 3;}; 
00889 
00895   int getSetLength() {return 3;};
00896   
00897 
00898   // Flags
00906   int setDynamicRange(int n=-1);
00907  
00913   int setROI(int nroi=-1, int *xmin=NULL, int *xmax=NULL, int *ymin=NULL, int *ymax=NULL);
00914   
00915 
00921   int setReadOutFlags(readOutFlags flag);
00922 
00931   int executeTrimming(trimMode mode, int par1, int par2, int imod=-1);
00932 
00933  
00934   //Corrections
00935 
00941   int setFlatFieldCorrection(string fname=""); 
00942 
00949   int getFlatFieldCorrections(float *corr=NULL, float *ecorr=NULL);
00950 
00956   int setRateCorrection(float t=0);
00957 
00958   
00964   int getRateCorrections(float &t);  
00965 
00970   int getRateCorrections();
00971   
00977   int setBadChannelCorrection(string fname="");
00978   
00984   int getBadChannelCorrections(int *bad=NULL);
00985 
00986   
00992   int setAngularConversion(string fname="");
00993 
01000   int getAngularConversion(int &direction,  angleConversionConstant *angconv=NULL);
01001   
01005   float setGlobalOffset(float f){thisDetector->globalOffset=f; return thisDetector->globalOffset;}; 
01006 
01010   float setFineOffset(float f){thisDetector->fineOffset=f; return thisDetector->fineOffset;};
01014   float getFineOffset(){return thisDetector->fineOffset;};
01015   
01019   float getGlobalOffset(){return thisDetector->globalOffset;};
01020 
01027   int setPositions(int nPos, float *pos){thisDetector->numberOfPositions=nPos; for (int ip=0; ip<nPos; ip++) thisDetector->detPositions[ip]=pos[ip]; return thisDetector->numberOfPositions;};
01033   int getPositions(float *pos=NULL){ if (pos ) {for (int ip=0; ip<thisDetector->numberOfPositions; ip++) pos[ip]=thisDetector->detPositions[ip];} return thisDetector->numberOfPositions;};
01034   
01035   
01037   float setBinSize(float bs) {thisDetector->binSize=bs; return thisDetector->binSize;}
01039   float getBinSize() {return thisDetector->binSize;}
01040 
01041 
01042 
01043 
01044 
01045 
01051   float* decodeData(int *datain);
01052 
01053   
01054   
01055   
01066   int flatFieldCorrect(float datain, float errin, float &dataout, float &errout, float ffcoefficient, float fferr);
01067   
01076   int flatFieldCorrect(float* datain, float *errin, float* dataout, float *errout);
01077  
01078 
01079   
01090   int rateCorrect(float datain, float errin, float &dataout, float &errout, float tau, float t);
01091   
01100   int rateCorrect(float* datain, float *errin, float* dataout, float *errout);
01101 
01102   
01110   int resetMerging(float *mp, float *mv,float *me, int *mm);
01121   int addToMerging(float *p1, float *v1, float *e1, float *mp, float *mv,float *me, int *mm);
01122 
01131   int finalizeMerging(float *mp, float *mv,float *me, int *mm);
01132 
01136   int exitServer();
01137 
01141   void* processData(); // thread function
01146   sls_detector_module*  createModule();
01152   void deleteModule(sls_detector_module *myMod);
01153 
01154 
01155   void acquire();
01156 
01157  private:
01158  
01162   sharedSlsDetector *thisDetector;
01163 
01167   int onlineFlag;
01168   
01172   int detId;
01173   
01177   int shmId;
01178 
01182   MySocketTCP *controlSocket;
01183 
01187   MySocketTCP *stopSocket;
01188   
01192   MySocketTCP *dataSocket; 
01193   
01197   queue<int*> dataQueue;
01198   
01202   pthread_t dataProcessingThread;
01203   
01207   float currentPosition;
01208   
01212   float currentPositionIndex;
01213   
01217   float currentI0;
01218   
01219 
01220 
01222   float *mergingBins;
01223 
01225   float *mergingCounts;
01226 
01228   float *mergingErrors;
01229 
01231   int *mergingMultiplicity;
01232   
01233 
01234 
01235 
01236 
01237 
01238  
01240   float *ffcoefficients;
01242   float *fferrors;
01244   sls_detector_module *detectorModules;
01246   float *dacs;
01248   float *adcs;
01250   int *chipregs;
01252   int *chanregs;
01254   int *badChannelMask;
01255 
01261   int* getDataFromDetector();
01262 
01268   int initSharedMemory(detectorType type=GENERIC, int id=0);
01269 
01271   int freeSharedMemory();
01277   int initializeDetectorSize(detectorType type);
01281   int initializeDetectorStructure(); 
01285   int sendChannel(sls_detector_channel*); 
01289   int sendChip(sls_detector_chip*);
01293   int sendModule(sls_detector_module*);
01297   int receiveChannel(sls_detector_channel*);
01301   int receiveChip(sls_detector_chip*);
01305   int receiveModule(sls_detector_module*);
01306   
01310   void startThread();
01311   
01315   int fillBadChannelMask();
01316 };
01317 
01318 
01319 //static void* startProcessData(void *n);
01320 #endif

Generated on Fri Oct 9 16:01:04 2009 by  doxygen 1.4.7