1 |
|
---|
2 | /************************************************************************* \
|
---|
3 |
|
---|
4 | Name: HVCalib.cc
|
---|
5 |
|
---|
6 | Created by: Luisa Sabrina Stark Schneebeli, April 2009
|
---|
7 | sabrina.stark@phys.ethz.ch
|
---|
8 |
|
---|
9 | Contents: Class reading the calibration table for DAC to HV values
|
---|
10 |
|
---|
11 | \*************************************************************************/
|
---|
12 |
|
---|
13 |
|
---|
14 | #include <stdio.h>
|
---|
15 | #include <iostream>
|
---|
16 | #include <fstream>
|
---|
17 | #include <math.h>
|
---|
18 |
|
---|
19 | #include "HVCalib.h"
|
---|
20 |
|
---|
21 | using namespace std;
|
---|
22 |
|
---|
23 |
|
---|
24 | HVCalib::HVCalib(HVConfig* hvConfig) {
|
---|
25 |
|
---|
26 | char calibfile[80], dLine[6000];
|
---|
27 | int idac, idacOld, istep, ihv1000, ihvOld;
|
---|
28 | bool first = true;
|
---|
29 | float hv1000,dac;
|
---|
30 | double intpart;
|
---|
31 |
|
---|
32 | Config = hvConfig;
|
---|
33 |
|
---|
34 | iDACMin = Config->DACMin;
|
---|
35 | fHVCalibSlope = Config->fHVCalibSlope;
|
---|
36 | NDACValues = Config->DACMax - Config->DACMin;
|
---|
37 | NHVValues = 10*NDACValues;
|
---|
38 |
|
---|
39 | HVArray = new double*** [Config->NumHVBoards];
|
---|
40 | DACArray = new int*** [Config->NumHVBoards];
|
---|
41 | for(int i=0; i<Config->NumHVBoards; i++) {
|
---|
42 | HVArray[i] = new double** [NUM_CHAINS];
|
---|
43 | DACArray[i] = new int** [NUM_CHAINS];
|
---|
44 | for(int j=0; j<NUM_CHAINS; j++) {
|
---|
45 | HVArray[i][j] = new double* [NUM_CHANNELS];
|
---|
46 | DACArray[i][j] = new int* [NUM_CHANNELS];
|
---|
47 | for(int k=0; k<NUM_CHANNELS; k++) {
|
---|
48 | HVArray[i][j][k] = new double [NDACValues];
|
---|
49 | DACArray[i][j][k] = new int [NHVValues];
|
---|
50 | }
|
---|
51 | }
|
---|
52 | }
|
---|
53 |
|
---|
54 | for(int i=0; i<Config->NumHVBoards; i++){
|
---|
55 | for(int j=0; j<NUM_CHAINS; j++){
|
---|
56 | sprintf(calibfile,"Calib/%s_%c%d.txt",hvConfig->fUSBDevice[i],65+j,1);
|
---|
57 | ifstream fin(calibfile);
|
---|
58 |
|
---|
59 | while(fin){
|
---|
60 | fin >> dac;
|
---|
61 | idacOld = 0;
|
---|
62 | if ((int) dac >= Config->DACMin) {
|
---|
63 | if (first){
|
---|
64 | iDACMin = (int) dac;
|
---|
65 | first = false;
|
---|
66 | }
|
---|
67 | idac = (int)dac - iDACMin;
|
---|
68 | istep = idac - idacOld;
|
---|
69 | for(int k=0; k<NUM_CHANNELS; k++){
|
---|
70 | if (idac>=NDACValues) break;
|
---|
71 |
|
---|
72 | fin>>HVArray[i][j][k][idac];
|
---|
73 |
|
---|
74 | for (int l=1; l<istep;l++){
|
---|
75 | HVArray[i][j][k][idac-istep+l] = HVArray[i][j][k][idac-istep] + l*(HVArray[i][j][k][idac]-HVArray[i][j][k][idac-istep])/istep;
|
---|
76 | // printf(" HVArray: %d %f \n", idac-istep+l, HVArray[i][j][0][idac-istep+l]);
|
---|
77 | // if (idac > 11240-iDACMin)
|
---|
78 | // return;
|
---|
79 | }
|
---|
80 | }
|
---|
81 | idacOld = idac;
|
---|
82 | }
|
---|
83 | else{
|
---|
84 | fin.getline(dLine,6000,'\n'); // Skip low voltage region
|
---|
85 | }
|
---|
86 | }
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | for(int i=0; i<Config->NumHVBoards; i++){
|
---|
91 | for(int j=0; j<NUM_CHAINS; j++){
|
---|
92 | for(int k=0; k<NUM_CHANNELS; k++){
|
---|
93 | ihvOld = 0;
|
---|
94 | first = true;
|
---|
95 | for (int l=0; l<NDACValues; l++){
|
---|
96 | if (l == 0){
|
---|
97 | ihvOld = 0;
|
---|
98 | DACArray[i][j][k][0] = iDACMin;
|
---|
99 | // printf("first : DACArray[i][j][k][0] = %d, ihvOld = %d, i %d, j %d, k %d\n", DACArray[i][j][k][0], ihvOld,i,j,k);
|
---|
100 | }
|
---|
101 | hv1000 = (HVArray[i][j][k][l] - HVArray[i][j][k][0]) * 1000;
|
---|
102 | if (modf(hv1000,&intpart) >= 0.5){
|
---|
103 | ihv1000 = (int)((hv1000>=0)?ceil(hv1000):floor(hv1000));
|
---|
104 | }
|
---|
105 | else ihv1000 = (int)((hv1000<0)?ceil(hv1000):floor(hv1000));
|
---|
106 |
|
---|
107 | if (ihv1000 <= ihvOld) continue;
|
---|
108 | else if (ihv1000 > ihvOld){
|
---|
109 | for (int m=0; m<(ihv1000-ihvOld); m++){
|
---|
110 | DACArray[i][j][k][ihvOld+m] = l+iDACMin;
|
---|
111 | //printf("HV = %d, HVArr = %f, DACArray = %d, m = %d. \n",ihvOld+m,HVArray[i][j][k][l],DACArray[i][j][k][ihvOld+m],m);
|
---|
112 | // printf("DACArray[i][j][k][0] = %d\n", DACArray[i][j][k][0]);
|
---|
113 | }
|
---|
114 | ihvOld = ihv1000;
|
---|
115 | }
|
---|
116 | }
|
---|
117 | }
|
---|
118 | }
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | /////////////////////////////////////////////////////////////////////////
|
---|
123 |
|
---|
124 | HVCalib::~HVCalib() {
|
---|
125 |
|
---|
126 | for(int i=0; i<Config->NumHVBoards; i++) {
|
---|
127 | for(int j=0; j<NUM_CHAINS; j++) {
|
---|
128 | for(int k=0; k<NUM_CHANNELS; k++) {
|
---|
129 | delete[] HVArray[i][j][k];
|
---|
130 | delete[] DACArray[i][j][k];
|
---|
131 | }
|
---|
132 | delete[] HVArray[i][j];
|
---|
133 | delete[] DACArray[i][j];
|
---|
134 | }
|
---|
135 | delete[] HVArray[i];
|
---|
136 | delete[] DACArray[i];
|
---|
137 | }
|
---|
138 | delete[] HVArray;
|
---|
139 | delete[] DACArray;
|
---|
140 | }
|
---|
141 |
|
---|
142 | /////////////////////////////////////////////////////////////////////////
|
---|
143 |
|
---|
144 | double HVCalib::DACToHV(int dac, int board, int chain, int channel) {
|
---|
145 | if (dac < iDACMin){
|
---|
146 | return HVArray[board][chain][channel][0] + (dac - iDACMin)*fHVCalibSlope;
|
---|
147 | }
|
---|
148 | else
|
---|
149 | return HVArray[board][chain][channel][dac-iDACMin];
|
---|
150 |
|
---|
151 | }
|
---|
152 | /////////////////////////////////////////////////////////////////////////
|
---|
153 |
|
---|
154 | int HVCalib::HVToDAC(double hv, int board, int chain, int channel) {
|
---|
155 | if (hv < HVArray[board][chain][channel][0]){
|
---|
156 | return iDACMin + (int)((hv - HVArray[board][chain][channel][0])/fHVCalibSlope);
|
---|
157 | }
|
---|
158 | else{
|
---|
159 | int ihv = 0;
|
---|
160 | double intpart;
|
---|
161 | double hv1000 = (hv-HVArray[board][chain][channel][0])*1000.;
|
---|
162 | if (modf(hv1000,&intpart) >= 0.5){
|
---|
163 | ihv = (int) (hv1000>=0 ? ceil(hv1000) : floor(hv1000));
|
---|
164 | }
|
---|
165 | else {
|
---|
166 | ihv = (int) (hv1000<0 ? ceil(hv1000) : floor(hv1000));
|
---|
167 | }
|
---|
168 | if (ihv >= NHVValues) ihv = NHVValues;
|
---|
169 | return DACArray[board][chain][channel][ihv];
|
---|
170 | }
|
---|
171 | }
|
---|