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}
00049 class detectorData {
00050 public:
00059 detectorData(float *val=NULL, float *err=NULL, float *ang=NULL, int f_ind=-1, char *fname="", int np=-1) : values(val), errors(err), angles(ang), fileIndex(f_ind), npoints(np){strcpy(fileName,fname);};
00064 ~detectorData() {if (values) delete [] values; if (errors) delete [] errors; if (angles) delete [] angles;};
00065
00066 float *values;
00067 float *errors;
00068 float *angles;
00069 int fileIndex;
00070 char fileName[1000];
00071 int npoints;
00072 };
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083 using namespace std;
00123 class slsDetector {
00124
00125
00126
00127 public:
00128
00129
00131 enum {GET_ONLINE_FLAG,
00132 OFFLINE_FLAG,
00133 ONLINE_FLAG
00134 };
00135
00136
00137
00142 typedef struct sharedSlsDetector {
00144 int alreadyExisting;
00145
00147 char hostname[MAX_STR_LENGTH];
00149 int controlPort;
00151 int stopPort;
00153 int dataPort;
00154
00156 detectorType myDetectorType;
00157
00158
00160 char trimDir[MAX_STR_LENGTH];
00162 char calDir[MAX_STR_LENGTH];
00164 int nTrimEn;
00166 int trimEnergies[100];
00167
00168
00170 int fileIndex;
00172 char filePath[MAX_STR_LENGTH];
00174 char fileName[MAX_STR_LENGTH];
00175
00176
00177
00179 int nMod[2];
00181 int nMods;
00183 int nModMax[2];
00185 int nModsMax;
00187 int nChans;
00189 int nChips;
00191 int nDacs;
00193 int nAdcs;
00195 int dynamicRange;
00197 int dataBytes;
00198
00200 int correctionMask;
00202 float tDead;
00204 int nBadChans;
00206 char badChanFile[MAX_STR_LENGTH];
00208 int badChansList[MAX_BADCHANS];
00210 int nBadFF;
00212 int badFFList[MAX_BADCHANS];
00213
00215 char angConvFile[MAX_STR_LENGTH];
00217 angleConversionConstant angOff[MAXMODS];
00219 int angDirection;
00221 float fineOffset;
00223 float globalOffset;
00225 int numberOfPositions;
00227 float detPositions[MAXPOS];
00229 float binSize;
00230
00231
00233 int nROI;
00235 ROI roiLimits[MAX_ROIS];
00237 readOutFlags roFlags;
00238
00239
00241 detectorSettings currentSettings;
00243 int currentThresholdEV;
00245 int64_t timerValue[MAX_TIMERS];
00247 int clkDiv;
00248
00249
00251 int ffoff;
00253 int fferroff;
00255 int modoff;
00257 int dacoff;
00259 int adcoff;
00261 int chipoff;
00263 int chanoff;
00264
00265 } sharedSlsDetector;
00266
00267
00268
00275 slsDetector(detectorType type=GENERIC, int id=0);
00276
00277
00279 ~slsDetector(){};
00280
00281
00284 int setOnline(int const online);
00287 int exists() {return thisDetector->alreadyExisting;};
00288
00295 virtual int readConfigurationFile(string const fname)=0;
00301 virtual int writeConfigurationFile(string const fname)=0;
00302
00303
00304
00305
00306
00307
00314 virtual int dumpDetectorSetup(string const fname)=0;
00320 virtual int retrieveDetectorSetup(string const fname)=0;
00321
00333 int setTCPSocket(string const name="", int const control_port=-1, int const stop_port=-1, int const data_port=-1);
00335 char* getHostname() {return thisDetector->hostname;};
00337 int getControlPort() {return thisDetector->controlPort;};
00339 int getStopPort() {return thisDetector->stopPort;};
00341 int getDataPort() {return thisDetector->dataPort;};
00342
00343
00344
00346 char* getTrimDir() {return thisDetector->trimDir;};
00348 char* setTrimDir(string s) {sprintf(thisDetector->trimDir, s.c_str()); return thisDetector->trimDir;};
00355 int getTrimEn(int *en=NULL) {if (en) {for (int ien=0; ien<thisDetector->nTrimEn; ien++) en[ien]=thisDetector->trimEnergies[ien];} return (thisDetector->nTrimEn);};
00356
00366 virtual sls_detector_module* readTrimFile(string fname, sls_detector_module* myMod=NULL)=0;
00367
00377 virtual int writeTrimFile(string fname, sls_detector_module mod)=0;
00378
00387 virtual int writeTrimFile(string fname, int imod)=0;
00388
00393 char* setFilePath(string s) {sprintf(thisDetector->filePath, s.c_str()); return thisDetector->filePath;};
00394
00399 char* setFileName(string s) {sprintf(thisDetector->fileName, s.c_str()); return thisDetector->fileName;};
00400
00405 int setFileIndex(int i) {thisDetector->fileIndex=i; return thisDetector->fileIndex;};
00406
00411 char* getFilePath() {return thisDetector->filePath;};
00412
00417 char* getFileName() {return thisDetector->fileName;};
00418
00423 int getFileIndex() {return thisDetector->fileIndex;};
00424
00436 string createFileName();
00437
00452 virtual int writeDataFile(string fname, float *data, float *err=NULL, float *ang=NULL, char dataformat='f', int nch=-1)=0;
00453
00462 virtual int writeDataFile(string fname, int *data)=0;
00463
00478 virtual int readDataFile(string fname, float *data, float *err=NULL, float *ang=NULL, char dataformat='f', int nch=0)=0;
00479
00488 virtual int readDataFile(string fname, int *data)=0;
00489
00494 char* getCalDir() {cout << "cal dir is " << thisDetector->calDir; return thisDetector->calDir;};
00495
00496
00501 char* setCalDir(string s) {sprintf(thisDetector->calDir, s.c_str()); return thisDetector->calDir;};
00510 virtual int readCalibrationFile(string fname, float &gain, float &offset)=0;
00519 virtual int writeCalibrationFile(string fname, float gain, float offset)=0;
00520
00521
00528 virtual int readAngularConversion(string fname="")=0;
00535 virtual int writeAngularConversion(string fname="")=0;
00536
00537
00538
00539
00540
00541
00549 int execCommand(string cmd, string answer);
00550
00557 int setDetectorType(detectorType type=GET_DETECTOR_TYPE);
00558
00565 int setDetectorType(string type);
00566
00572 void getDetectorType(char *type);
00573
00574
00575
00583
00590 int setNumberOfModules(int n, dimension d=X);
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00609 int getMaxNumberOfModules(dimension d=X);
00610
00611
00618 externalSignalFlag setExternalSignalFlags(externalSignalFlag pol=GET_EXTERNAL_SIGNAL_FLAG , int signalindex=0);
00619
00620
00628 externalCommunicationMode setExternalCommunicationMode(externalCommunicationMode pol=GET_EXTERNAL_COMMUNICATION_MODE);
00629
00630
00631
00632
00639 int64_t getId(idMode mode, int imod=0);
00646 int digitalTest(digitalTestMode mode, int imod=0);
00655 int* analogTest(analogTestMode mode);
00656
00662 int enableAnalogOutput(int ichan);
00663
00669 int enableAnalogOutput(int imod, int ichip, int ichan);
00670
00679 int giveCalibrationPulse(float vcal, int npulses);
00680
00681
00682
00683
00691 int writeRegister(int addr, int val);
00692
00699 int readRegister(int addr);
00700
00708 float setDAC(float val, dacIndex index, int imod=-1);
00709
00716 float getADC(dacIndex index, int imod=0);
00717
00727 int setChannel(int64_t reg, int ichan=-1, int ichip=-1, int imod=-1);
00728
00734 int setChannel(sls_detector_channel chan);
00735
00743 sls_detector_channel getChannel(int ichan, int ichip, int imod);
00744
00745
00746
00755 int setChip(int reg, int ichip=-1, int imod=-1);
00756
00763 int setChip(sls_detector_chip chip);
00764
00773 sls_detector_chip getChip(int ichip, int imod);
00774
00775
00782 int setModule(int reg, int imod=-1);
00783
00790 int setModule(sls_detector_module module);
00791
00797 sls_detector_module *getModule(int imod);
00798
00799
00800
00801
00802
00803
00804
00805
00806
00812 int getThresholdEnergy(int imod=-1);
00813
00821 int setThresholdEnergy(int e_eV, int imod=-1, detectorSettings isettings=GET_SETTINGS);
00822
00828 detectorSettings getSettings(int imod=-1);
00829
00838 detectorSettings setSettings(detectorSettings isettings, int imod=-1);
00839
00840
00841
00842
00843
00848 int startAcquisition();
00849
00854 int stopAcquisition();
00855
00860 int startReadOut();
00861
00866 int getRunStatus();
00867
00873 int* startAndReadAll();
00874
00879 int startAndReadAllNoWait();
00880
00886 int* getDataFromDetectorNoWait();
00887
00893 int* readFrame();
00894
00900 int* readAll();
00901
00902
00908 int* popDataQueue();
00909
00915 detectorData* popFinalDataQueue();
00922 int64_t setTimer(timerIndex index, int64_t t=-1);
00923
00929 int64_t getTimeLeft(timerIndex index);
00930
00931
00932
00933
00940 int setSpeed(speedVariable sp, int value=-1);
00941
00942
00950 int setDynamicRange(int n=-1);
00951
00957 int setROI(int nroi=-1, int *xmin=NULL, int *xmax=NULL, int *ymin=NULL, int *ymax=NULL);
00958
00959
00965 int setReadOutFlags(readOutFlags flag);
00966
00975 int executeTrimming(trimMode mode, int par1, int par2, int imod=-1);
00976
00977
00978
00979
00985 int setFlatFieldCorrection(string fname="");
00986
00993 int getFlatFieldCorrection(float *corr=NULL, float *ecorr=NULL);
00994
01000 int setRateCorrection(float t=0);
01001
01002
01008 int getRateCorrection(float &t);
01009
01014 int getRateCorrection();
01015
01021 int setBadChannelCorrection(string fname="");
01022
01028 int getBadChannelCorrection(int *bad=NULL);
01029
01031 char *getBadChannelCorrectionFile() {return thisDetector->badChanFile;};
01032
01033
01041 virtual int setAngularConversion(string fname="")=0;
01042
01051 virtual int getAngularConversion(int &direction, angleConversionConstant *angconv=NULL)=0;
01052
01053
01058 virtual char *getAngularConversion()=0;
01059
01065 virtual float setGlobalOffset(float f)=0;
01066
01072 virtual float setFineOffset(float f)=0;
01078 virtual float getFineOffset()=0;
01079
01085 virtual float getGlobalOffset()=0;
01086
01095 virtual int setPositions(int nPos, float *pos)=0;
01103 virtual int getPositions(float *pos=NULL)=0;
01104
01105
01112 virtual float setBinSize(float bs)=0;
01113
01118 virtual float getBinSize()=0;
01119
01120
01121
01122
01123
01124
01130 float* decodeData(int *datain);
01131
01132
01133
01134
01145 int flatFieldCorrect(float datain, float errin, float &dataout, float &errout, float ffcoefficient, float fferr);
01146
01155 int flatFieldCorrect(float* datain, float *errin, float* dataout, float *errout);
01156
01157
01158
01169 int rateCorrect(float datain, float errin, float &dataout, float &errout, float tau, float t);
01170
01179 int rateCorrect(float* datain, float *errin, float* dataout, float *errout);
01180
01181
01192 virtual int resetMerging(float *mp, float *mv,float *me, int *mm)=0;
01205 virtual int addToMerging(float *p1, float *v1, float *e1, float *mp, float *mv,float *me, int *mm)=0;
01206
01216 int finalizeMerging(float *mp, float *mv,float *me, int *mm);
01217
01221 int exitServer();
01222
01228 virtual void* processData(int delflag=1)=0;
01233 sls_detector_module* createModule();
01239 void deleteModule(sls_detector_module *myMod);
01240
01241
01253 virtual void acquire(int delflag=1)=0;
01254
01255 protected:
01256
01260 sharedSlsDetector *thisDetector;
01261
01265 int onlineFlag;
01266
01270 int detId;
01271
01275 int shmId;
01276
01280 MySocketTCP *controlSocket;
01281
01285 MySocketTCP *stopSocket;
01286
01290 MySocketTCP *dataSocket;
01291
01295 queue<int*> dataQueue;
01299 queue<detectorData*> finalDataQueue;
01300
01301
01302
01303
01307 float currentPosition;
01308
01312 float currentPositionIndex;
01313
01317 float currentI0;
01318
01319
01320
01322 float *mergingBins;
01323
01325 float *mergingCounts;
01326
01328 float *mergingErrors;
01329
01331 int *mergingMultiplicity;
01332
01333
01334
01335
01336
01337
01338
01340 float *ffcoefficients;
01342 float *fferrors;
01344 sls_detector_module *detectorModules;
01346 float *dacs;
01348 float *adcs;
01350 int *chipregs;
01352 int *chanregs;
01354 int *badChannelMask;
01355
01361 int* getDataFromDetector();
01362
01368 int initSharedMemory(detectorType type=GENERIC, int id=0);
01369
01371 int freeSharedMemory();
01377 int initializeDetectorSize(detectorType type);
01381 int initializeDetectorStructure();
01385 int sendChannel(sls_detector_channel*);
01389 int sendChip(sls_detector_chip*);
01393 int sendModule(sls_detector_module*);
01397 int receiveChannel(sls_detector_channel*);
01401 int receiveChip(sls_detector_chip*);
01405 int receiveModule(sls_detector_module*);
01406
01410 void startThread();
01411
01415 int fillBadChannelMask();
01416 };
01417
01418
01419
01420 #endif