source: trunk/FACT++/src/PixelMap.h@ 12555

Last change on this file since 12555 was 12531, checked in by tbretz, 13 years ago
Fixed the number of lines expected from a GPAD map file.
File size: 6.8 KB
Line 
1#ifndef FACT_PixelMap
2#define FACT_PixelMap
3
4#include<vector>
5#include<string>
6#include<fstream>
7#include<sstream>
8
9#include "tools.h"
10
11#ifdef DEBUG
12#include<iostream> // cerr -- to be removed?
13#endif
14
15// FIXME: Replace 416 by BIAS::kNumChannels
16
17struct PixelMapEntry
18{
19 int index; /// Software index
20 int cbpx; /// Hardware index as CBPX
21 int gapd; /// gAPD index
22// float Vgapd; /// gAPD Bias voltage
23 int hv_board; /// Bias suppply board
24 int hv_channel; /// Bias supply channel
25
26 int crate() const { return cbpx/1000; }
27 int board() const { return (cbpx/100)%10; }
28 int patch() const { return (cbpx/10)%10; }
29 int pixel() const { return cbpx%10; }
30 int hw() const { return pixel()+patch()*9+board()*36+crate()*360; }
31 int group() const { return pixel()>3; }
32 int hv() const { return hv_channel+hv_board*32; }
33
34 operator bool() const { return index>=0; }
35};
36
37class PixelMap : public std::vector<PixelMapEntry>
38{
39public:
40 static const PixelMapEntry empty;
41
42 PixelMap() : std::vector<PixelMapEntry>(1440)
43 {
44 }
45
46 bool Read(const std::string &fname)
47 {
48 std::ifstream fin(fname);
49
50 int l = 0;
51
52 std::string buf;
53 while (getline(fin, buf, '\n'))
54 {
55 if (l>1439)
56 break;
57
58 buf = Tools::Trim(buf);
59 if (buf[0]=='#')
60 continue;
61
62 std::stringstream str(buf);
63
64 int idummy;
65 float fdummy;
66
67 PixelMapEntry entry;
68
69 str >> entry.index;
70 str >> entry.cbpx;
71 str >> idummy;
72 str >> idummy;
73 str >> entry.gapd;
74 str >> fdummy; //entry.Vgapd;
75 str >> entry.hv_board;
76 str >> entry.hv_channel;
77 //str >> fdummy;
78 //str >> fdummy;
79 //str >> fdummy;
80
81 if (entry.hv_channel+32*entry.hv_board>=416/*BIAS::kNumChannels*/)
82 {
83#ifdef DEBUG
84 cerr << "Invalid board/channel read from FACTmapV5.txt." << endl;
85#endif
86 return false;
87 }
88
89 (*this)[l++] = entry;
90 }
91
92 return l==1440;
93 }
94
95 const PixelMapEntry &index(int idx) const
96 {
97 for (std::vector<PixelMapEntry>::const_iterator it=begin(); it!=end(); it++)
98 if (it->index==idx)
99 return *it;
100#ifdef DEBUG
101 std::cerr << "PixelMap: index " << idx << " not found" << std::endl;
102#endif
103 return empty;
104 }
105
106 const PixelMapEntry &cbpx(int c) const
107 {
108 for (std::vector<PixelMapEntry>::const_iterator it=begin(); it!=end(); it++)
109 if (it->cbpx==c)
110 return *it;
111#ifdef DEBUG
112 std::cerr << "PixelMap: cbpx " << c << " not found" << std::endl;
113#endif
114 return empty;
115 }
116
117 const PixelMapEntry &cbpx(int c, int b, int p, int px) const
118 {
119 return cbpx(px + p*10 + b*100 + c*1000);
120 }
121
122 const PixelMapEntry &hw(int idx) const
123 {
124 return cbpx(idx/360, (idx/36)%10, (idx/9)%4, idx%9);
125 }
126
127 const PixelMapEntry &hv(int board, int channel) const
128 {
129 for (std::vector<PixelMapEntry>::const_iterator it=begin(); it!=end(); it++)
130 if (it->hv_board==board && it->hv_channel==channel)
131 return *it;
132#ifdef DEBUG
133 std::cerr << "PixelMap: hv " << board << "/" << channel << " not found" << std::endl;
134#endif
135 return empty;
136 }
137
138 const PixelMapEntry &hv(int idx) const
139 {
140 return hv(idx/32, idx%32);
141 }
142
143 /*
144 float Vgapd(int board, int channel) const
145 {
146 float avg = 0;
147 int num = 0;
148
149 for (std::vector<PixelMapEntry>::const_iterator it=begin(); it!=end(); it++)
150 if (it->hv_board==board && it->hv_channel==channel)
151 {
152 avg += it->Vgapd;
153 num ++;
154 }
155
156 return num==0 ? 0 : avg/num;
157 }
158
159 float Vgapd(int idx) const
160 {
161 return Vgapd(idx/32, idx%32);
162 }
163
164 std::vector<float> Vgapd() const
165 {
166 std::vector<float> avg(416);
167 std::vector<int> num(416);
168
169 for (std::vector<PixelMapEntry>::const_iterator it=begin(); it!=end(); it++)
170 {
171 const int ch = it->hv_board*32 + it->hv_channel;
172
173 avg[ch] += it->Vgapd;
174 num[ch] ++;
175 }
176
177 for (int ch=0; ch<416; ch++)
178 {
179 if (num[ch])
180 avg[ch] /= num[ch];
181 }
182
183 return avg;
184 }*/
185};
186
187struct BiasMapEntry
188{
189 int hv_board; /// Bias suppply board
190 int hv_channel; /// Bias supply channel
191 float Vnom; /// Channel bias voltage nominal
192 float Voff; /// Channel bias voltage offset
193
194 int hv() const { return hv_channel+hv_board*32; }
195};
196
197class BiasMap : public std::vector<BiasMapEntry>
198{
199public:
200 static const BiasMapEntry empty;
201
202 BiasMap() : std::vector<BiasMapEntry>(416)
203 {
204 }
205
206 bool Read(const std::string &fname)
207 {
208 std::ifstream fin(fname);
209
210 int l = 0;
211
212 std::string buf;
213 while (getline(fin, buf, '\n'))
214 {
215 if (l>416)
216 break;
217
218 buf = Tools::Trim(buf);
219 if (buf[0]=='#')
220 continue;
221
222 std::stringstream str(buf);
223
224 BiasMapEntry entry;
225
226 str >> entry.hv_board;
227 str >> entry.hv_channel;
228 str >> entry.Vnom;
229 str >> entry.Voff;
230
231 if (entry.hv_channel+32*entry.hv_board>=416)
232 {
233#ifdef DEBUG
234 cerr << "Invalid board/channel read from " << fname << "." << endl;
235#endif
236 return false;
237 }
238
239 (*this)[l++] = entry;
240 }
241
242 return l==416;
243 }
244
245 const BiasMapEntry &hv(int board, int channel) const
246 {
247 for (std::vector<BiasMapEntry>::const_iterator it=begin(); it!=end(); it++)
248 if (it->hv_board==board && it->hv_channel==channel)
249 return *it;
250#ifdef DEBUG
251 std::cerr << "PixelMap: hv " << board << "/" << channel << " not found" << std::endl;
252#endif
253 return empty;
254 }
255
256 const BiasMapEntry &hv(int idx) const
257 {
258 return hv(idx/32, idx%32);
259 }
260
261 float Vgapd(int board, int channel) const
262 {
263 const BiasMapEntry &entry = hv(board, channel);
264 return entry.Vnom+entry.Voff;
265 }
266
267 float Vgapd(int idx) const
268 {
269 return Vgapd(idx/32, idx%32);
270 }
271
272 std::vector<float> Vgapd() const
273 {
274 std::vector<float> volt(416);
275
276 for (std::vector<BiasMapEntry>::const_iterator it=begin(); it!=end(); it++)
277 {
278 const int ch = it->hv_board*32 + it->hv_channel;
279 volt[ch] += it->Vnom+it->Voff;
280 }
281
282 return volt;
283 }
284};
285
286#endif
Note: See TracBrowser for help on using the repository browser.