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 tabel 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];
|
---|
27 | int idac, idacOld;
|
---|
28 | int istep, ihv1000, ihvOld;
|
---|
29 | char dLine[6000];
|
---|
30 | bool first = true;
|
---|
31 | float hv1000,dac;
|
---|
32 | double intpart;
|
---|
33 |
|
---|
34 | iDACMin = hvConfig->DACMin;
|
---|
35 | fHVCalibSlope = hvConfig->fHVCalibSlope;
|
---|
36 | NDACValues = hvConfig->DACMax - hvConfig->DACMin;
|
---|
37 | NHVValues = 10*NDACValues;
|
---|
38 |
|
---|
39 |
|
---|
40 | HVArray = new double***[hvConfig->NumHVBoards];
|
---|
41 | for(int i=0; i<hvConfig->NumHVBoards; i++) {
|
---|
42 | HVArray[i] = new double**[NUM_CHAINS];
|
---|
43 | for(int j=0; j<NUM_CHAINS; j++) {
|
---|
44 | HVArray[i][j] = new double*[NUM_CHANNELS];
|
---|
45 | for(int k=0; k<NUM_CHANNELS; k++) {
|
---|
46 | HVArray[i][j][k] = new double[NDACValues];
|
---|
47 | }
|
---|
48 | }
|
---|
49 | }
|
---|
50 |
|
---|
51 | DACArray = new int***[hvConfig->NumHVBoards];
|
---|
52 | for(int i=0; i<hvConfig->NumHVBoards; i++) {
|
---|
53 | DACArray[i] = new int**[NUM_CHAINS];
|
---|
54 | for(int j=0; j<NUM_CHAINS; j++) {
|
---|
55 | DACArray[i][j] = new int*[NUM_CHANNELS];
|
---|
56 | for(int k=0; k<NUM_CHANNELS; k++) {
|
---|
57 | DACArray[i][j][k] = new int[NHVValues];
|
---|
58 | }
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | for(int i=0; i<hvConfig->NumHVBoards; i++){
|
---|
63 | for(int j=0; j<NUM_CHAINS; j++){
|
---|
64 | sprintf(calibfile,"Calib/%s_%c%d.txt",hvConfig->fUSBDevice[i],65+j,1);
|
---|
65 | ifstream fin(calibfile);
|
---|
66 |
|
---|
67 | while(fin){
|
---|
68 | fin >> dac;
|
---|
69 | idacOld = 0;
|
---|
70 | if ((int) dac >= hvConfig->DACMin) {
|
---|
71 | if (first){
|
---|
72 | iDACMin = (int) dac;
|
---|
73 | first = false;
|
---|
74 | }
|
---|
75 | idac = (int)dac - iDACMin;
|
---|
76 | istep = idac - idacOld;
|
---|
77 | for(int k=0; k<NUM_CHANNELS; k++){
|
---|
78 | fin>>HVArray[i][j][k][idac];
|
---|
79 | for (int l=1; l<istep;l++){
|
---|
80 | 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;
|
---|
81 | // printf(" HVArray: %d %f \n", idac-istep+l, HVArray[i][j][0][idac-istep+l]);
|
---|
82 | // if (idac > 11240-iDACMin)
|
---|
83 | // return;
|
---|
84 | }
|
---|
85 | }
|
---|
86 | idacOld = idac;
|
---|
87 | }
|
---|
88 | else{
|
---|
89 | fin.getline(dLine,6000,'\n'); // Skip low voltage region
|
---|
90 | }
|
---|
91 | }
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | for(int i=0; i<hvConfig->NumHVBoards; i++){
|
---|
96 | for(int j=0;j<NUM_CHAINS;j++){
|
---|
97 | for(int k=0;k<NUM_CHANNELS;k++){
|
---|
98 | ihvOld = 0;
|
---|
99 | first = true;
|
---|
100 | for (int l=0; l<NDACValues;l++){
|
---|
101 | if (l == 0){
|
---|
102 | ihvOld = 0;
|
---|
103 | DACArray[i][j][k][0] = iDACMin;
|
---|
104 | // 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);
|
---|
105 | }
|
---|
106 | hv1000 = (HVArray[i][j][k][l] - HVArray[i][j][k][0]) * 1000;
|
---|
107 | if (modf(hv1000,&intpart) >= 0.5){
|
---|
108 | ihv1000 = (int)((hv1000>=0)?ceil(hv1000):floor(hv1000));
|
---|
109 | }
|
---|
110 | else ihv1000 = (int)((hv1000<0)?ceil(hv1000):floor(hv1000));
|
---|
111 |
|
---|
112 | if (ihv1000 <= ihvOld) continue;
|
---|
113 | else if (ihv1000 > ihvOld){
|
---|
114 | for (int m=0; m<(ihv1000-ihvOld); m++){
|
---|
115 | DACArray[i][j][k][ihvOld+m] = l+iDACMin;
|
---|
116 | //printf("HV = %d, HVArr = %f, DACArray = %d, m = %d. \n",ihvOld+m,HVArray[i][j][k][l],DACArray[i][j][k][ihvOld+m],m);
|
---|
117 | // printf("DACArray[i][j][k][0] = %d\n", DACArray[i][j][k][0]);
|
---|
118 | }
|
---|
119 | ihvOld = ihv1000;
|
---|
120 | }
|
---|
121 | }
|
---|
122 | }
|
---|
123 | }
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | /////////////////////////////////////////////////////////////////////////
|
---|
128 |
|
---|
129 | HVCalib::~HVCalib() {
|
---|
130 |
|
---|
131 |
|
---|
132 | for (int i=0; i<MAX_NUM_HVBOARDS; i++){
|
---|
133 | for (int j=0; j<NUM_CHAINS;j++){
|
---|
134 | for (int k=0; k<NUM_CHANNELS;k++){
|
---|
135 | delete [] HVArray[i][j][k];
|
---|
136 | delete [] HVArray;
|
---|
137 | }
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | for (int i=0; i<MAX_NUM_HVBOARDS; i++){
|
---|
142 | for (int j=0; j<NUM_CHAINS;j++){
|
---|
143 | for (int k=0; k<NUM_CHANNELS;k++){
|
---|
144 | delete [] DACArray[i][j][k];
|
---|
145 | delete [] DACArray;
|
---|
146 | }
|
---|
147 | }
|
---|
148 | }
|
---|
149 | }
|
---|
150 |
|
---|
151 | /////////////////////////////////////////////////////////////////////////
|
---|
152 |
|
---|
153 | double HVCalib::DACToHV(int dac, int board, int chain, int channel) {
|
---|
154 | if (dac < iDACMin){
|
---|
155 | return HVArray[board][chain][channel][0] + (dac - iDACMin)*fHVCalibSlope;
|
---|
156 | }
|
---|
157 | else
|
---|
158 | return HVArray[board][chain][channel][dac-iDACMin];
|
---|
159 |
|
---|
160 | }
|
---|
161 | /////////////////////////////////////////////////////////////////////////
|
---|
162 |
|
---|
163 | int HVCalib::HVToDAC(double hv, int board, int chain, int channel) {
|
---|
164 | if (hv < HVArray[board][chain][channel][0]){
|
---|
165 | return iDACMin + (int)((hv - HVArray[board][chain][channel][0])/fHVCalibSlope);
|
---|
166 | }
|
---|
167 | else{
|
---|
168 | int ihv = 0;
|
---|
169 | double intpart;
|
---|
170 | double hv1000 = (hv-HVArray[board][chain][channel][0])*1000.;
|
---|
171 | if (modf(hv1000,&intpart) >= 0.5){
|
---|
172 | ihv = (int) (hv1000>=0 ? ceil(hv1000) : floor(hv1000));
|
---|
173 | }
|
---|
174 | else {
|
---|
175 | ihv = (int) (hv1000<0 ? ceil(hv1000) : floor(hv1000));
|
---|
176 | }
|
---|
177 | return DACArray[board][chain][channel][ihv];
|
---|
178 | }
|
---|
179 | }
|
---|
180 |
|
---|