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 #include "sls_detector_defs.h"
00026
00027 #define MAX_TIMERS 10
00028 #define MAX_ROIS 100
00029 #define MAX_BADCHANS 2000
00030 #define MAXPOS 50
00031
00032 #define NMODMAXX 24
00033 #define NMODMAXY 24
00034 #define MAXMODS 36
00035 #define NCHIPSMAX 10
00036 #define NCHANSMAX 65536
00037 #define NDACSMAX 16
00038
00039 #define DEFAULT_HOSTNAME "localhost"
00040 #define DEFAULT_SHM_KEY 5678
00041
00042 #define defaultTDead {170,90,750}
00044 using namespace std;
00045
00084 class slsDetector {
00085
00086
00087
00088 public:
00089
00090
00092 enum {GET_ONLINE_FLAG,
00093 OFFLINE_FLAG,
00094 ONLINE_FLAG
00095 };
00096
00097
00098
00103 typedef struct sharedSlsDetector {
00105 int alreadyExisting;
00106
00108 char hostname[MAX_STR_LENGTH];
00110 int controlPort;
00112 int stopPort;
00114 int dataPort;
00115
00117 detectorType myDetectorType;
00118
00119
00121 char trimDir[MAX_STR_LENGTH];
00123 char calDir[MAX_STR_LENGTH];
00125 int nTrimEn;
00127 int trimEnergies[100];
00128
00129
00131 int fileIndex;
00133 char filePath[MAX_STR_LENGTH];
00135 char fileName[MAX_STR_LENGTH];
00136
00137
00138
00140 int nMod[2];
00142 int nMods;
00144 int nModMax[2];
00146 int nModsMax;
00148 int nChans;
00150 int nChips;
00152 int nDacs;
00154 int nAdcs;
00156 int dynamicRange;
00158 int dataBytes;
00159
00161 int correctionMask;
00163 float tDead;
00165 int nBadChans;
00167 char badChanFile[MAX_STR_LENGTH];
00169 int badChansList[MAX_BADCHANS];
00171 int nBadFF;
00173 int badFFList[MAX_BADCHANS];
00174
00176 char angConvFile[MAX_STR_LENGTH];
00178 angleConversionConstant angOff[MAXMODS];
00180 int angDirection;
00182 float fineOffset;
00184 float globalOffset;
00186 int numberOfPositions;
00188 float detPositions[MAXPOS];
00190 float binSize;
00191
00192
00194 int nROI;
00196 ROI roiLimits[MAX_ROIS];
00198 readOutFlags roFlags;
00199
00200
00202 detectorSettings currentSettings;
00204 int currentThresholdEV;
00206 int64_t timerValue[MAX_TIMERS];
00208 int clkDiv;
00209
00210
00212 int ffoff;
00214 int fferroff;
00216 int modoff;
00218 int dacoff;
00220 int adcoff;
00222 int chipoff;
00224 int chanoff;
00225
00226 } sharedSlsDetector;
00227
00228
00229
00236 slsDetector(detectorType type=GENERIC, int id=0);
00237
00238
00240 ~slsDetector(){};
00241
00242
00245 int setOnline(int const online);
00248 int exists() {return thisDetector->alreadyExisting;};
00249
00256 virtual int readConfigurationFile(string const fname)=0;
00262 virtual int writeConfigurationFile(string const fname)=0;
00263
00264
00265
00266
00267
00268
00275 virtual int dumpDetectorSetup(string const fname)=0;
00281 virtual int retrieveDetectorSetup(string const fname)=0;
00282
00294 int setTCPSocket(string const name="", int const control_port=-1, int const stop_port=-1, int const data_port=-1);
00296 char* getHostname() {return thisDetector->hostname;};
00298 int getControlPort() {return thisDetector->controlPort;};
00300 int getStopPort() {return thisDetector->stopPort;};
00302 int getDataPort() {return thisDetector->dataPort;};
00303
00304
00305
00307 char* getTrimDir() {return thisDetector->trimDir;};
00309 char* setTrimDir(string s) {sprintf(thisDetector->trimDir, s.c_str()); return thisDetector->trimDir;};
00316 int getTrimEn(int *en=NULL) {if (en) {for (int ien=0; ien<thisDetector->nTrimEn; ien++) en[ien]=thisDetector->trimEnergies[ien];} return (thisDetector->nTrimEn);};
00317
00327 virtual sls_detector_module* readTrimFile(string fname, sls_detector_module* myMod=NULL)=0;
00328
00338 virtual int writeTrimFile(string fname, sls_detector_module mod)=0;
00339
00348 virtual int writeTrimFile(string fname, int imod)=0;
00349
00354 char* setFilePath(string s) {sprintf(thisDetector->filePath, s.c_str()); return thisDetector->filePath;};
00355
00360 char* setFileName(string s) {sprintf(thisDetector->fileName, s.c_str()); return thisDetector->fileName;};
00361
00366 int setFileIndex(int i) {thisDetector->fileIndex=i; return thisDetector->fileIndex;};
00367
00372 char* getFilePath() {return thisDetector->filePath;};
00373
00378 char* getFileName() {return thisDetector->fileName;};
00379
00384 int getFileIndex() {return thisDetector->fileIndex;};
00385
00397 string createFileName();
00398
00413 virtual int writeDataFile(string fname, float *data, float *err=NULL, float *ang=NULL, char dataformat='f', int nch=-1)=0;
00414
00423 virtual int writeDataFile(string fname, int *data)=0;
00424
00439 virtual int readDataFile(string fname, float *data, float *err=NULL, float *ang=NULL, char dataformat='f', int nch=0)=0;
00440
00449 virtual int readDataFile(string fname, int *data)=0;
00450
00455 char* getCalDir() {cout << "cal dir is " << thisDetector->calDir; return thisDetector->calDir;};
00456
00457
00462 char* setCalDir(string s) {sprintf(thisDetector->calDir, s.c_str()); return thisDetector->calDir;};
00471 virtual int readCalibrationFile(string fname, float &gain, float &offset)=0;
00480 virtual int writeCalibrationFile(string fname, float gain, float offset)=0;
00481
00482
00489 virtual int readAngularConversion(string fname="")=0;
00496 virtual int writeAngularConversion(string fname="")=0;
00497
00498
00499
00500
00501
00502
00510 int execCommand(string cmd, string answer);
00511
00518 int setDetectorType(detectorType type=GET_DETECTOR_TYPE);
00519
00526 int setDetectorType(string type);
00527
00533 void getDetectorType(char *type);
00534
00535
00536
00544
00551 int setNumberOfModules(int n, dimension d=X);
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00570 int getMaxNumberOfModules(dimension d=X);
00571
00572
00579 externalSignalFlag setExternalSignalFlags(externalSignalFlag pol=GET_EXTERNAL_SIGNAL_FLAG , int signalindex=0);
00580
00581
00589 externalCommunicationMode setExternalCommunicationMode(externalCommunicationMode pol=GET_EXTERNAL_COMMUNICATION_MODE);
00590
00591
00592
00593
00600 int64_t getId(idMode mode, int imod=0);
00607 int digitalTest(digitalTestMode mode, int imod=0);
00616 int* analogTest(analogTestMode mode);
00617
00623 int enableAnalogOutput(int ichan);
00624
00630 int enableAnalogOutput(int imod, int ichip, int ichan);
00631
00640 int giveCalibrationPulse(float vcal, int npulses);
00641
00642
00643
00644
00652 int writeRegister(int addr, int val);
00653
00660 int readRegister(int addr);
00661
00669 float setDAC(float val, dacIndex index, int imod=-1);
00670
00677 float getADC(dacIndex index, int imod=0);
00678
00688 int setChannel(int64_t reg, int ichan=-1, int ichip=-1, int imod=-1);
00689
00695 int setChannel(sls_detector_channel chan);
00696
00704 sls_detector_channel getChannel(int ichan, int ichip, int imod);
00705
00706
00707
00716 int setChip(int reg, int ichip=-1, int imod=-1);
00717
00724 int setChip(sls_detector_chip chip);
00725
00734 sls_detector_chip getChip(int ichip, int imod);
00735
00736
00743 int setModule(int reg, int imod=-1);
00744
00751 int setModule(sls_detector_module module);
00752
00758 sls_detector_module *getModule(int imod);
00759
00760
00761
00762
00763
00764
00765
00766
00767
00773 int getThresholdEnergy(int imod=-1);
00774
00782 int setThresholdEnergy(int e_eV, int imod=-1, detectorSettings isettings=GET_SETTINGS);
00783
00789 detectorSettings getSettings(int imod=-1);
00790
00799 detectorSettings setSettings(detectorSettings isettings, int imod=-1);
00800
00801
00802
00803
00804
00809 int startAcquisition();
00810
00815 int stopAcquisition();
00816
00821 int startReadOut();
00822
00827 int getRunStatus();
00828
00834 int* startAndReadAll();
00835
00840 int startAndReadAllNoWait();
00841
00847 int* getDataFromDetectorNoWait();
00848
00854 int* readFrame();
00855
00861 int* readAll();
00862
00863
00869 int* popDataQueue();
00870
00877 int64_t setTimer(timerIndex index, int64_t t=-1);
00878
00884 int64_t getTimeLeft(timerIndex index);
00885
00886
00887
00888
00895 int setSpeed(speedVariable sp, int value=-1);
00896
00897
00905 int setDynamicRange(int n=-1);
00906
00912 int setROI(int nroi=-1, int *xmin=NULL, int *xmax=NULL, int *ymin=NULL, int *ymax=NULL);
00913
00914
00920 int setReadOutFlags(readOutFlags flag);
00921
00930 int executeTrimming(trimMode mode, int par1, int par2, int imod=-1);
00931
00932
00933
00934
00940 int setFlatFieldCorrection(string fname="");
00941
00948 int getFlatFieldCorrections(float *corr=NULL, float *ecorr=NULL);
00949
00955 int setRateCorrection(float t=0);
00956
00957
00963 int getRateCorrections(float &t);
00964
00969 int getRateCorrections();
00970
00976 int setBadChannelCorrection(string fname="");
00977
00983 int getBadChannelCorrections(int *bad=NULL);
00984
00986 char *getBadChannelCorrectionFile() {return thisDetector->badChanFile;};
00987
00988
00996 virtual int setAngularConversion(string fname="")=0;
00997
01006 virtual int getAngularConversion(int &direction, angleConversionConstant *angconv=NULL)=0;
01007
01008
01013 virtual char *getAngularConversion()=0;
01014
01020 virtual float setGlobalOffset(float f)=0;
01021
01027 virtual float setFineOffset(float f)=0;
01033 virtual float getFineOffset()=0;
01034
01040 virtual float getGlobalOffset()=0;
01041
01050 virtual int setPositions(int nPos, float *pos)=0;
01058 virtual int getPositions(float *pos=NULL)=0;
01059
01060
01067 virtual float setBinSize(float bs)=0;
01068
01073 virtual float getBinSize()=0;
01074
01075
01076
01077
01078
01079
01085 float* decodeData(int *datain);
01086
01087
01088
01089
01100 int flatFieldCorrect(float datain, float errin, float &dataout, float &errout, float ffcoefficient, float fferr);
01101
01110 int flatFieldCorrect(float* datain, float *errin, float* dataout, float *errout);
01111
01112
01113
01124 int rateCorrect(float datain, float errin, float &dataout, float &errout, float tau, float t);
01125
01134 int rateCorrect(float* datain, float *errin, float* dataout, float *errout);
01135
01136
01147 virtual int resetMerging(float *mp, float *mv,float *me, int *mm)=0;
01160 virtual int addToMerging(float *p1, float *v1, float *e1, float *mp, float *mv,float *me, int *mm)=0;
01161
01171 int finalizeMerging(float *mp, float *mv,float *me, int *mm);
01172
01176 int exitServer();
01177
01182 virtual void* processData()=0;
01187 sls_detector_module* createModule();
01193 void deleteModule(sls_detector_module *myMod);
01194
01195
01206 virtual void acquire()=0;
01207
01208 protected:
01209
01213 sharedSlsDetector *thisDetector;
01214
01218 int onlineFlag;
01219
01223 int detId;
01224
01228 int shmId;
01229
01233 MySocketTCP *controlSocket;
01234
01238 MySocketTCP *stopSocket;
01239
01243 MySocketTCP *dataSocket;
01244
01248 queue<int*> dataQueue;
01249
01253 pthread_t dataProcessingThread;
01254
01258 float currentPosition;
01259
01263 float currentPositionIndex;
01264
01268 float currentI0;
01269
01270
01271
01273 float *mergingBins;
01274
01276 float *mergingCounts;
01277
01279 float *mergingErrors;
01280
01282 int *mergingMultiplicity;
01283
01284
01285
01286
01287
01288
01289
01291 float *ffcoefficients;
01293 float *fferrors;
01295 sls_detector_module *detectorModules;
01297 float *dacs;
01299 float *adcs;
01301 int *chipregs;
01303 int *chanregs;
01305 int *badChannelMask;
01306
01312 int* getDataFromDetector();
01313
01319 int initSharedMemory(detectorType type=GENERIC, int id=0);
01320
01322 int freeSharedMemory();
01328 int initializeDetectorSize(detectorType type);
01332 int initializeDetectorStructure();
01336 int sendChannel(sls_detector_channel*);
01340 int sendChip(sls_detector_chip*);
01344 int sendModule(sls_detector_module*);
01348 int receiveChannel(sls_detector_channel*);
01352 int receiveChip(sls_detector_chip*);
01356 int receiveModule(sls_detector_module*);
01357
01361 void startThread();
01362
01366 int fillBadChannelMask();
01367 };
01368
01369
01370
01371 #endif