source: fact/hvcontrol/src/HVCalib.cc@ 17557

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