source: drsdaq/DRS/DRS.h@ 154

Last change on this file since 154 was 132, checked in by ogrimm, 15 years ago
Clean-up DRS class by removing unnecessary overloaded function for CT VME controller
File size: 21.9 KB
Line 
1
2#ifndef DRS_H
3#define DRS_H
4
5#include <stdio.h>
6#include <math.h>
7#include <string.h>
8#include <stdlib.h>
9#include <time.h>
10#include <sys/time.h>
11#include <assert.h>
12#include <algorithm>
13#include <sys/stat.h>
14#include <unistd.h>
15#include <sys/ioctl.h>
16
17#include "mxml.h"
18#include "strlcpy.h"
19
20// Concurrent Technologies VME single board computer
21#ifdef CT_VME
22 #include "rcc_error/rcc_error.h" // Error reporting
23 #include "vme_rcc/vme_rcc.h" // VME access
24 #include "cmem_rcc/cmem_rcc.h" // Allocation of contiguous memory
25 #include "rcc_time_stamp/tstamp.h" // Time stamp library
26#endif
27
28// Struck VME interface
29#ifdef STRUCK_VME
30 #include "mvmestd.h"
31#endif
32
33// Control register bit definitions
34#define BIT_START_TRIG (1<<0) // Write a "1" to start domino wave
35#define BIT_REINIT_TRIG (1<<1) // Write a "1" to stop & reset DRS
36#define BIT_SOFT_TRIG (1<<2) // Write a "1" to stop and read data to RAM
37#define BIT_FLASH_TRIG (1<<3) // Write a "1" to write DAC0 & DAC1 into serial EEPROM
38#define BIT_AUTOSTART (1<<16)
39#define BIT_DMODE (1<<17) // 0: single shot, 1: circular
40#define BIT_LED (1<<18) // 1=on, 0=blink during readout
41#define BIT_TCAL_EN (1<<19) // Switch on (1) / off (0) for 33 MHz calib signal
42#define BIT_ZERO_SUPP (1<<20)
43#define BIT_FREQ_AUTO_ADJ (1<<21)
44#define BIT_ENABLE_TRIGGER (1<<22)
45#define BIT_LONG_START_PULSE (1<<23) // (*DRS2*) 0:short start pulse (> 0.8 GHz), 1:long start pulse (< 0.8 GHz)
46#define BIT_READOUT_MODE (1<<23) // (*DRS3*) 0:start from first bin, 1:start from domino stop
47#define BIT_DELAYED_START (1<<24) // Start domino wave 400 ns after soft trigger, used for waveform
48 // Generator startup
49#define BIT_ACAL_EN (1<<25) // Connect DRS to inputs (0) or to DAC6 (1)
50#define BIT_TRIGGER_DELAYED (1<<26) // Select delayed trigger from trigger bus
51#define BIT_DACTIVE (1<<27) // Keep domino wave running during readout
52
53// Status register bit definitions
54#define BIT_RUNNING (1<<0) // One if domino wave running or readout in progress
55#define BIT_NEW_FREQ1 (1<<1) // One if new frequency measurement available
56#define BIT_NEW_FREQ2 (1<<2)
57
58enum DRSBoardConstants {
59 kNumberOfChannels = 10,
60 kNumberOfCalibChannels = 10,
61 kNumberOfBins = 1024,
62 kNumberOfChips = 2,
63 kFrequencyCacheSize = 10,
64 kBSplineOrder = 4,
65 kPreCaliculatedBSplines = 1000,
66 kPreCaliculatedBSplineGroups = 5,
67 kNumberOfADCBins = 4096,
68 kBSplineXMinOffset = 20,
69 kMaxNumberOfClockCycles = 100,
70};
71
72enum DRSErrorCodes {
73 kSuccess = 0,
74 kInvalidTriggerSignal = -1,
75 kWrongChannelOrChip = -2,
76 kInvalidTransport = -3,
77 kZeroSuppression = -4,
78 kWaveNotAvailable = -5
79};
80
81class DRSBoard;
82
83
84class ResponseCalibration {
85 protected:
86
87 class CalibrationData {
88 public:
89 class CalibrationDataChannel {
90 public:
91 unsigned char fLimitGroup[kNumberOfBins]; //!
92 float fMin[kNumberOfBins]; //!
93 float fRange[kNumberOfBins]; //!
94 short fOffset[kNumberOfBins]; //!
95 short fGain[kNumberOfBins]; //!
96 unsigned short fOffsetADC[kNumberOfBins]; //!
97 short *fData[kNumberOfBins]; //!
98 unsigned char *fLookUp[kNumberOfBins]; //!
99 unsigned short fLookUpOffset[kNumberOfBins]; //!
100 unsigned char fNumberOfLookUpPoints[kNumberOfBins]; //!
101 float *fTempData; //!
102
103 private:
104 CalibrationDataChannel(const CalibrationDataChannel &c); // Not implemented
105 CalibrationDataChannel &operator=(const CalibrationDataChannel &rhs); // Not implemented
106
107 public:
108 CalibrationDataChannel(int numberOfGridPoints)
109 :fTempData(new float[numberOfGridPoints]) {
110 int i;
111 for (i = 0; i < kNumberOfBins; i++) {
112 fData[i] = new short[numberOfGridPoints];
113 fLookUp[i] = NULL;
114 }
115 }
116 ~CalibrationDataChannel() {
117 int i;
118 delete fTempData;
119 for (i = 0; i < kNumberOfBins; i++) {
120 delete fData[i];
121 delete fLookUp[i];
122 }
123 }
124 };
125
126 bool fRead; //!
127 CalibrationDataChannel *fChannel[kNumberOfCalibChannels]; //!
128 unsigned char fNumberOfGridPoints; //!
129 int fHasOffsetCalibration; //!
130 float fStartTemperature; //!
131 float fEndTemperature; //!
132 int *fBSplineOffsetLookUp[kNumberOfADCBins]; //!
133 float **fBSplineLookUp[kNumberOfADCBins]; //!
134 float fMin; //!
135 float fMax; //!
136 unsigned char fNumberOfLimitGroups; //!
137 static float fIntRevers[2 * kBSplineOrder - 2];
138
139 private:
140 CalibrationData(const CalibrationData &c); // Not implemented
141 CalibrationData &operator=(const CalibrationData &rhs); // Not implemented
142
143 public:
144 CalibrationData(int numberOfGridPoints);
145 ~CalibrationData();
146 static int CalculateBSpline(int nGrid, float value, float *bsplines);
147 void PreCalculateBSpline();
148 void DeletePreCalculatedBSpline();
149 };
150
151 // General Fields
152 DRSBoard *fBoard;
153
154 double fPrecision;
155
156 // Fields for creating the Calibration
157 bool fInitialized;
158 bool fRecorded;
159 bool fFitted;
160 bool fOffset;
161 bool fCalibrationValid[2];
162
163 int fNumberOfPointsLowVolt;
164 int fNumberOfPoints;
165 int fNumberOfMode2Bins;
166 int fNumberOfSamples;
167 int fNumberOfGridPoints;
168 int fNumberOfXConstPoints;
169 int fNumberOfXConstGridPoints;
170 double fTriggerFrequency;
171 int fShowStatistics;
172 FILE *fCalibFile;
173
174 int fCurrentLowVoltPoint;
175 int fCurrentPoint;
176 int fCurrentSample;
177 int fCurrentFitChannel;
178 int fCurrentFitBin;
179
180 float *fResponseX[kNumberOfCalibChannels][kNumberOfBins];
181 float *fResponseY;
182 unsigned short **fWaveFormMode3[kNumberOfCalibChannels];
183 unsigned short **fWaveFormMode2[kNumberOfCalibChannels];
184 short **fWaveFormOffset[kNumberOfCalibChannels];
185 unsigned short **fWaveFormOffsetADC[kNumberOfCalibChannels]; // Is this used?
186 unsigned short *fSamples;
187 int *fSampleUsed;
188
189 float *fPntX[2];
190 float *fPntY[2];
191 float *fUValues[2];
192 float *fRes[kNumberOfBins];
193 float *fResX[kNumberOfBins];
194
195 double *fXXFit;
196 double *fYYFit;
197 double *fWWFit;
198 double *fYYFitRes;
199 double *fYYSave;
200 double *fXXSave;
201
202 float **fStatisticsApprox;
203 float **fStatisticsApproxExt;
204
205 // Fields for applying the Calibration
206 CalibrationData *fCalibrationData[kNumberOfChips];
207
208 private:
209 ResponseCalibration(const ResponseCalibration &c); // Not implemented
210 ResponseCalibration &operator=(const ResponseCalibration &rhs); // Not implemented
211
212 public:
213 ResponseCalibration(DRSBoard* board);
214 ~ResponseCalibration();
215
216 void SetCalibrationParameters(int numberOfPointsLowVolt, int numberOfPoints, int numberOfMode2Bins,
217 int numberOfSamples, int numberOfGridPoints, int numberOfXConstPoints,
218 int numberOfXConstGridPoints, double triggerFrequency, int showStatistics = 0);
219 void ResetCalibration();
220 bool RecordCalibrationPoints(int chipNumber);
221 bool RecordCalibrationPointsV3(int chipNumber);
222 bool RecordCalibrationPointsV4(int chipNumber);
223 bool FitCalibrationPoints(int chipNumber);
224 bool FitCalibrationPointsV3(int chipNumber);
225 bool FitCalibrationPointsV4(int chipNumber);
226 bool OffsetCalibration(int chipNumber);
227 double GetTemperature(unsigned int chipIndex);
228
229 bool WriteCalibration(unsigned int chipIndex);
230 bool WriteCalibrationV3(unsigned int chipIndex);
231 bool WriteCalibrationV4(unsigned int chipIndex);
232 bool ReadCalibration(unsigned int chipIndex);
233 bool ReadCalibrationV3(unsigned int chipIndex);
234 bool ReadCalibrationV4(unsigned int chipIndex);
235 bool Calibrate(unsigned int chipIndex, unsigned int channel, float *adcWaveform,
236 float *uWaveform, float threshold);
237 bool Calibrate(unsigned int chipIndex, unsigned int channel, unsigned short *adcWaveform, short *uWaveform,
238 int triggerCell, float threshold);
239 bool SubtractADCOffset(unsigned int chipIndex, unsigned int channel, unsigned short *adcWaveform,
240 unsigned short *adcCalibratedWaveform, unsigned short newBaseLevel);
241 bool IsRead(int chipIndex) const { return fCalibrationValid[chipIndex]; }
242 double GetPrecision() const { return fPrecision; };
243
244 double GetOffsetAt(int chip,int chn,int bin) const { return fCalibrationData[chip]->fChannel[chn]->fOffset[bin]; };
245 double GetGainAt(int chip,int chn,int bin) const { return fCalibrationData[chip]->fChannel[chn]->fGain[bin]; };
246 double GetMeasPointXAt(int ip) const { return fXXSave[ip]; };
247 double GetMeasPointYAt(int ip) const { return fYYSave[ip]; };
248
249 protected:
250 void InitFields(int numberOfPointsLowVolt, int numberOfPoints, int numberOfMode2Bins, int numberOfSamples,
251 int numberOfGridPoints, int numberOfXConstPoints, int numberOfXConstGridPoints,
252 double triggerFrequency, int showStatistics);
253 void DeleteFields();
254 void CalibrationTrigger(int mode, double voltage);
255 void CalibrationStart(double voltage);
256
257 static float GetValue(float *coefficients, float u, int n);
258 static int Approx(float *p, float *uu, int np, int nu, float *coef);
259 static void LeastSquaresAccumulation(float **matrix, int nb, int *ip, int *ir, int mt, int jt);
260 static int LeastSquaresSolving(float **matrix, int nb, int ip, int ir, float *x, int n);
261 static void Housholder(int lpivot, int l1, int m, float **u, int iU1, int iU2, float *up, float **c, int iC1,
262 int iC2, int ice, int ncv);
263
264 static int MakeDir(const char *path);
265 static void Average(int method,float *samples,int numberOfSamples,float &mean,float &error,float sigmaBoundary);
266};
267
268
269
270class DRSBoard {
271 protected:
272 class TimeData {
273 public:
274 class FrequencyData {
275 public:
276 int fFrequency;
277 double fBin[kNumberOfBins];
278 };
279
280 enum {
281 kMaxNumberOfFrequencies = 4000
282 };
283 int fChip;
284 int fNumberOfFrequencies;
285 FrequencyData *fFrequency[kMaxNumberOfFrequencies];
286
287 private:
288 TimeData(const TimeData &c); // Not implemented
289 TimeData &operator=(const TimeData &rhs); // Not implemented
290
291 public:
292 TimeData()
293 :fChip(0)
294 ,fNumberOfFrequencies(0) {
295 }
296 ~TimeData() {
297 int i;
298 for (i = 0; i < fNumberOfFrequencies; i++) {
299 delete fFrequency[i];
300 }
301 }
302 };
303
304 public:
305 // DAC channels (CMC Version 1 : DAC_COFSA,DAC_COFSB,DAC_DRA,DAC_DSA,DAC_TLEVEL,DAC_ACALIB,DAC_DSB,DAC_DRB)
306 unsigned int fDAC_COFSA;
307 unsigned int fDAC_COFSB;
308 unsigned int fDAC_DRA;
309 unsigned int fDAC_DSA;
310 unsigned int fDAC_TLEVEL;
311 unsigned int fDAC_ACALIB;
312 unsigned int fDAC_DSB;
313 unsigned int fDAC_DRB;
314 // DAC channels (CMC Version 2+3 : DAC_COFS,DAC_DSA,DAC_DSB,DAC_TLEVEL,DAC_ADCOFS,DAC_CLKOFS,DAC_ACALIB)
315 unsigned int fDAC_COFS;
316 unsigned int fDAC_ADCOFS;
317 unsigned int fDAC_CLKOFS;
318 // DAC channels (CMC Version 4 : DAC_ROFS_1,DAC_DSA,DAC_DSB,DAC_ROFS_2,DAC_ADCOFS,DAC_ACALIB,DAC_INOFS,DAC_BIAS)
319 unsigned int fDAC_ROFS_1;
320 unsigned int fDAC_ROFS_2;
321 unsigned int fDAC_INOFS;
322 unsigned int fDAC_BIAS;
323
324 private:
325#ifdef CT_VME
326 VME_ErrorCode_t ErrorCode;
327 VME_BlockTransferList_t BLT_List;
328 char ErrorString[VME_MAXSTRING];
329 int CMEM_SegIdentifier;
330 unsigned long PCIAddress; // Physical address of contiguous buffer
331 unsigned long VirtualAddress; // Virtual address of contiguous buffer
332 unsigned int fBaseAddress;
333 unsigned int fBoardAddress;
334 int fMasterMapping;
335
336 unsigned int GetBaseAddress() const {return fBaseAddress; }
337 unsigned int GetBoardAddress() const {return fBoardAddress; }
338
339 int AllocateSegmentCMEM(unsigned int SegSize, int *CMEM_SegIdentifier);
340 int AssignPhysicalSegAddressCMEM(int CMEM_SegIdentifier, unsigned long* PCIAddress);
341 int AssignVirtualSegAddressCMEM(int CMEM_SegIdentifier, unsigned long* VirtualAddress);
342 int FreeSegmentCMEM(int CMEM_SegIdentifier);
343#endif
344#ifdef STRUCK_VME
345 mvme_addr_t fBaseAddress;
346 MVME_INTERFACE *fVMEInterface;
347#endif
348
349 protected:
350 // Fields for DRS
351 int fRequiredFirmwareVersion;
352 int fFirmwareVersion;
353 int fChipVersion;
354 int fBoardVersion;
355 int fCMCSerialNumber;
356 unsigned int fTransport;
357 unsigned int fCtrlBits;
358 int fNumberOfReadoutChannels;
359 double fExternalClockFrequency;
360
361 int fSlotNumber;
362 double fFrequency;
363 int fDominoMode;
364 int fReadoutMode;
365 int fTriggerEnable;
366 int fDelayedStart;
367 int fTriggerCell;
368 unsigned char fWaveforms[kNumberOfChips * kNumberOfChannels * 2 * kNumberOfBins];
369
370 // Fields for Calibration
371 int fMaxChips;
372 char fCalibDirectory[1000];
373
374 // Fields for Response Calibration
375 ResponseCalibration *fResponseCalibration;
376
377 // Fields for Time Calibration
378 TimeData **fTimeData;
379 int fNumberOfTimeData;
380
381 // General debugging flag
382 int fDebug;
383
384 // Fields for wave transfer
385 bool fWaveTransferred[kNumberOfChips * kNumberOfChannels];
386
387 // Waveform Rotation
388 int fTriggerStartBin; // Start bin of the trigger
389 bool kRotateWave;
390
391 public:
392 ~DRSBoard();
393
394 void SetCMCSerialNumber(unsigned int serialNumber) { fCMCSerialNumber = serialNumber; }
395 int GetCMCSerialNumber() const { return fCMCSerialNumber; }
396 int GetFirmwareVersion() const { return fFirmwareVersion; }
397 int GetRequiredFirmwareVersion() const { return fRequiredFirmwareVersion; }
398 int GetChipVersion() const { return fChipVersion; }
399 int GetCMCVersion() const { return fBoardVersion; }
400
401 // VME
402 int GetSlotNumber() const { return fSlotNumber; }
403 int Read(int type, void *data, unsigned int addr, int size);
404 int Write(int type, unsigned int addr, void *data, int size);
405
406 void RegisterTest(void);
407 int RAMTest(int flag);
408 unsigned int GetCtrlReg(void);
409 unsigned int GetStatusReg(void);
410
411 void SetLED(int state);
412
413 void SetChannelConfig(int firstChannel, int lastChannel, int nConfigChannels);
414 void SetNumberOfChannels(int nChannels);
415 int EnableTrigger(int mode);
416 int SetDelayedStart(int flag);
417 int IsBusy(void);
418 int IsNewFreq(unsigned char chipIndex);
419 int SetDAC(unsigned char channel, double value);
420 int ReadDAC(unsigned char channel, double *value);
421 int GetRegulationDAC(double *value);
422
423 int StartDomino();
424 int Reinit();
425 int Init();
426
427 void SetDebug(int debug) { fDebug = debug; }
428
429 int SetDominoMode(unsigned char mode);
430
431 int SetDominoActive(unsigned char mode);
432 int SetReadoutMode(unsigned char mode);
433
434 int SoftTrigger(void);
435 int ReadFrequency(unsigned char chipIndex, double *f);
436 int SetFrequency(double freq);
437 double VoltToFreq(double volt);
438 double FreqToVolt(double freq);
439 double GetFrequency() const { return fFrequency; }
440
441 int RegulateFrequency(double freq);
442 int SetExternalClockFrequency(double frequencyMHz);
443 double GetExternalClockFrequency();
444
445 void SetVoltageOffset(double offset1, double offset2);
446
447
448 int TestRead(unsigned int n, int type);
449
450 int TransferWaves(int numberOfChannels = kNumberOfChips * kNumberOfChannels);
451 int TransferWaves(unsigned char *p, int numberOfChannels = kNumberOfChips * kNumberOfChannels);
452 int TransferWaves(unsigned char *p, int firstChannel, int lastChannel);
453 int TransferWaves(int firstChannel, int lastChannel);
454
455 int DecodeWave(unsigned char *waveforms, unsigned int chipIndex, unsigned char channel,
456 unsigned short *waveform);
457 int DecodeWave(unsigned int chipIndex, unsigned char channel, unsigned short *waveform);
458
459 int GetWave(unsigned char *waveforms, unsigned int chipIndex, unsigned char channel, short *waveform,
460 bool responseCalib = false, int triggerCell = -1, bool adjustToClock = false,
461 float threshold = 0);
462 int GetWave(unsigned char *waveforms, unsigned int chipIndex, unsigned char channel, float *waveform,
463 bool responseCalib = false, int triggerCell = -1, bool adjustToClock = false,
464 float threshold = 0);
465 int GetWave(unsigned int chipIndex, unsigned char channel, short *waveform, bool responseCalib = false,
466 int triggerCell = -1, bool adjustToClock = false, float threshold = 0);
467 int GetWave(unsigned int chipIndex, unsigned char channel, float *waveform, bool responseCalib = false,
468 int triggerCell = -1, bool adjustToClock = false, float threshold = 0);
469 int GetADCWave(unsigned int chipIndex, unsigned char channel, unsigned short *waveform);
470 int GetADCWave(unsigned char *waveforms,unsigned int chipIndex, unsigned char channel,
471 unsigned short *waveform);
472
473 void RotateWave(int triggerCell, short *waveform);
474 void RotateWave(int triggerCell, float *waveform);
475 void SetRotation(bool r) {kRotateWave = r;}
476
477 int GetTime(unsigned int chipIndex, int frequencyMHz, float *time, int triggerCell);
478 int GetTriggerCell(unsigned int chipIndex);
479 int GetTriggerCell(unsigned char *waveforms,unsigned int chipIndex);
480 int GetTriggerCell(float *waveform);
481
482 void TestDAC(int channel);
483 void MeasureSpeed();
484 void InteractSpeed();
485 void MonitorFrequency();
486 int EnableTcal(int flag);
487 int EnableAcal(int mode, double voltage);
488 int SetCalibVoltage(double value);
489 int SetCalibTiming(int t1, int t2);
490 double GetTemperature();
491 int GetTriggerBus();
492 int FlashEEPROM(unsigned short serial_cmc);
493 bool HasCorrectFirmware();
494
495 bool InitTimeCalibration(unsigned int chipIndex);
496 void SetCalibrationDirectory(const char *calibrationDirectoryPath);
497 void GetCalibrationDirectory(char *calibrationDirectoryPath);
498
499 ResponseCalibration *GetResponseCalibration() const { return fResponseCalibration; }
500
501 int GetStoredTriggerCell() const { return fTriggerCell; }
502 double GetPrecision() const { return fResponseCalibration->GetPrecision(); }
503 int CalibrateWaveform(unsigned int chipIndex, unsigned char channel, unsigned short *adcWaveform,
504 short *waveform, bool responseCalib, int triggerCell, bool adjustToClock,
505 float threshold);
506
507 static void LinearRegression(double *x, double *y, int n, double *a, double *b);
508
509 protected:
510 void ConstructBoard();
511 void ReadSerialNumber();
512
513 TimeData *GetTimeCalibration(unsigned int chipIndex, bool reinit = false);
514 int GetStretchedTime(float *time, float *measurement, int numberOfMeasurements, float period);
515
516 public:
517#ifdef CT_VME
518 DRSBoard(int MasterMapping, unsigned int BaseAddress, unsigned int BoardAddress, int SlotNumber);
519#endif
520#ifdef STRUCK_VME
521 DRSBoard(MVME_INTERFACE * MVME_Interface, mvme_addr_t BaseAddress, int SlotNumber);
522 MVME_INTERFACE *GetVMEInterface() const { return fVMEInterface; };
523#endif
524
525 void PrintBinary32(unsigned int i);
526 long int GetMicroSeconds();
527
528};
529
530
531
532class DRS {
533
534 protected:
535 enum {
536 kMaxNumberOfBoards = 40
537 };
538
539 protected:
540
541 DRSBoard *fBoard[kMaxNumberOfBoards];
542 int fNumberOfBoards;
543
544#ifdef STRUCK_VME
545 MVME_INTERFACE *fVMEInterface;
546#endif
547
548 private:
549 DRS(const DRS &c); // Not implemented
550 DRS &operator=(const DRS &rhs); // Not implemented
551
552#ifdef CT_VME
553 VME_MasterMap_t MasterMap;
554 VME_ErrorCode_t ErrorCode;
555 char ErrorString[VME_MAXSTRING];
556 int MasterMapping[kMaxNumberOfBoards];
557
558 int OpenVME();
559 int MasterMapVME(int* MMap);
560 int MasterUnMapVME(int MMap);
561 int CloseVME();
562 int OpenCMEM();
563 int CloseCMEM();
564#endif
565
566 int First_VME_Slot;
567 int Last_VME_Slot;
568
569 public:
570 // Public Methods
571 DRS();
572 ~DRS();
573
574 DRSBoard *GetBoard(int i) { return fBoard[i]; }
575 DRSBoard **GetBoards() { return fBoard; }
576 int GetNumberOfBoards() const { return fNumberOfBoards; }
577
578#ifdef STRUCK_VME
579 MVME_INTERFACE *GetVMEInterface() const { return fVMEInterface; };
580#endif
581
582 void InitialScan();
583 void SetFirstVMESlot(int s) { First_VME_Slot = s; }
584 void SetLastVMESlot(int s) { Last_VME_Slot = s; }
585 int GetFirstVMESlot() { return First_VME_Slot; }
586 int GetLastVMESlot() { return Last_VME_Slot; }
587};
588
589#endif // DRS_H
Note: See TracBrowser for help on using the repository browser.