source: hvcontrol/src/HVCalib.cc@ 70

Last change on this file since 70 was 46, checked in by lstark, 16 years ago
path of pixel map integrated in config file
File size: 4.9 KB
Line 
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 "HVCalib.h"
15#include <stdio.h>
16#include <iostream>
17#include <fstream>
18#include <math.h>
19using namespace std;
20
21
22
23
24
25HVCalib::HVCalib(HVConfig* hvConfig) {
26
27 char calibfile[80];
28 int idac, idacOld;
29 int istep, ihv1000, ihvOld;
30 char dLine[6000];
31 bool first = true;
32 float hv1000,dac;
33 double intpart;
34
35 iDACMin = hvConfig->DACMin;
36 fHVCalibSlope = hvConfig->fHVCalibSlope;
37 NDACValues = hvConfig->DACMax - hvConfig->DACMin;
38 NHVValues = 10*NDACValues;
39
40
41 HVArray = new float***[hvConfig->NumHVBoards];
42 for(int i=0; i<hvConfig->NumHVBoards; i++) {
43 HVArray[i] = new float**[NCHAIN];
44 for(int j=0; j<NCHAIN; j++) {
45 HVArray[i][j] = new float*[NCHANNEL];
46 for(int k=0; k<NCHANNEL; k++) {
47 HVArray[i][j][k] = new float[NDACValues];
48 }
49 }
50 }
51
52 DACArray = new int***[hvConfig->NumHVBoards];
53 for(int i=0; i<hvConfig->NumHVBoards; i++) {
54 DACArray[i] = new int**[NCHAIN];
55 for(int j=0; j<NCHAIN; j++) {
56 DACArray[i][j] = new int*[NCHANNEL];
57 for(int k=0; k<NCHANNEL; k++) {
58 DACArray[i][j][k] = new int[NHVValues];
59 }
60 }
61 }
62
63
64
65 for(int i=0; i<hvConfig->NumHVBoards; i++){
66 // for(int i=0; i<1; i++){
67 for(int j=0;j<NCHAIN;j++){
68 //for(int j=0;j<1;j++){
69 sprintf(calibfile,"Calib/%s_%c%d.txt",hvConfig->fUSBDevice[i],65+j,1);
70 // printf("Calibration tabel 0: %s\n", calibfile);
71 ifstream fin(calibfile);
72
73 while(fin){
74 fin >> dac;
75 idacOld = 0;
76 if ((int)dac >= hvConfig->DACMin) {
77 if (first){
78 iDACMin = (int)dac;
79 first = false;
80 }
81 idac = (int)dac - iDACMin;
82 istep = idac - idacOld;
83 for(int k=0;k<NCHANNEL;k++){
84 fin>>HVArray[i][j][k][idac];
85 for (int l=1; l<istep;l++){
86 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;
87 // printf(" HVArray: %d %f \n", idac-istep+l, HVArray[i][j][0][idac-istep+l]);
88 // if (idac > 11240-iDACMin)
89 // return;
90 }
91 }
92 idacOld = idac;
93 }
94 else{
95 fin.getline(dLine,6000,'\n'); // Skip low voltage region
96 }
97 }
98 }
99 }
100
101 for(int i=0; i<hvConfig->NumHVBoards; i++){
102 // for(int i=0; i<1; i++){
103 for(int j=0;j<NCHAIN;j++){
104 // for(int j=0;j<1;j++){
105 for(int k=0;k<NCHANNEL;k++){
106 //for(int k=0;k<1;k++){
107 ihvOld = 0;
108 first = true;
109 for (int l=0; l<NDACValues;l++){
110 if (l == 0){
111 ihvOld = 0;
112 DACArray[i][j][k][0] = iDACMin;
113 // 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);
114 }
115 hv1000 = (HVArray[i][j][k][l] - HVArray[i][j][k][0]) * 1000;
116 if (modf(hv1000,&intpart) >= 0.5){
117 ihv1000 = (int)((hv1000>=0)?ceil(hv1000):floor(hv1000));
118 }
119 else {
120 ihv1000 = (int)((hv1000<0)?ceil(hv1000):floor(hv1000));
121 }
122 if (ihv1000 <= ihvOld){
123 continue;
124 }
125 else if (ihv1000 > ihvOld){
126 for (int m=0; m<(ihv1000-ihvOld);m++){
127 DACArray[i][j][k][ihvOld+m] = l+iDACMin;
128 //printf("HV = %d, HVArr = %f, DACArray = %d, m = %d. \n",ihvOld+m,HVArray[i][j][k][l],DACArray[i][j][k][ihvOld+m],m);
129 // printf("DACArray[i][j][k][0] = %d\n", DACArray[i][j][k][0]);
130 }
131 ihvOld = ihv1000;
132 }
133 }
134 }
135 }
136 }
137}
138
139/////////////////////////////////////////////////////////////////////////
140
141HVCalib::~HVCalib() {
142
143
144 for (int i=0; i<MAX_NUM_HVBOARDS; i++){
145 for (int j=0; j<NCHAIN;j++){
146 for (int k=0; k<NCHANNEL;k++){
147 delete [] HVArray[i][j][k];
148 delete [] HVArray;
149 }
150 }
151 }
152
153 for (int i=0; i<MAX_NUM_HVBOARDS; i++){
154 for (int j=0; j<NCHAIN;j++){
155 for (int k=0; k<NCHANNEL;k++){
156 delete [] DACArray[i][j][k];
157 delete [] DACArray;
158 }
159 }
160 }
161}
162
163/////////////////////////////////////////////////////////////////////////
164
165float HVCalib::DACToHV(int dac, int board, int chain, int channel) {
166 if (dac < iDACMin){
167 return HVArray[board][chain][channel][0] + (dac - iDACMin)*fHVCalibSlope;
168 }
169 else
170 return HVArray[board][chain][channel][dac-iDACMin];
171
172}
173/////////////////////////////////////////////////////////////////////////
174
175int HVCalib::HVToDAC(float hv, int board, int chain, int channel) {
176 if (hv < HVArray[board][chain][channel][0]){
177 return iDACMin + (int)((hv - HVArray[board][chain][channel][0])/fHVCalibSlope);
178 }
179 else{
180 int ihv = 0;
181 double intpart;
182 float hv1000 = (hv-HVArray[board][chain][channel][0])*1000.;
183 if (modf(hv1000,&intpart) >= 0.5){
184 ihv = (int)((hv1000>=0)?ceil(hv1000):floor(hv1000));
185 }
186 else {
187 ihv = (int)((hv1000<0)?ceil(hv1000):floor(hv1000));
188 }
189 return DACArray[board][chain][channel][ihv];
190 }
191}
192
Note: See TracBrowser for help on using the repository browser.