1 | #ifndef FACT_PixelMap
|
---|
2 | #define FACT_PixelMap
|
---|
3 |
|
---|
4 | #include <vector>
|
---|
5 | #include <string>
|
---|
6 | #include <fstream>
|
---|
7 | #include <sstream>
|
---|
8 |
|
---|
9 | #ifdef DEBUG
|
---|
10 | #include <iostream> // cerr -- to be removed?
|
---|
11 | #endif
|
---|
12 |
|
---|
13 | #ifdef __EXCEPTIONS
|
---|
14 | #include <stdexcept>
|
---|
15 | #endif
|
---|
16 |
|
---|
17 | #ifdef __MARS__
|
---|
18 | #include "MLog.h"
|
---|
19 | #include "MLogManip.h"
|
---|
20 | #endif
|
---|
21 |
|
---|
22 | // FIXME: Replace 416 by BIAS::kNumChannels
|
---|
23 |
|
---|
24 | struct PixelMapEntry
|
---|
25 | {
|
---|
26 | int index; /// Software index
|
---|
27 | int cbpx; /// Hardware index as CBPX
|
---|
28 | int gapd; /// gAPD index
|
---|
29 | // float Vgapd; /// gAPD Bias voltage
|
---|
30 | int hv_board; /// Bias suppply board
|
---|
31 | int hv_channel;
|
---|
32 |
|
---|
33 | PixelMapEntry() : index(-1) { } /// Bias supply channel
|
---|
34 |
|
---|
35 | int crate() const { return cbpx/1000; }
|
---|
36 | int board() const { return (cbpx/100)%10; }
|
---|
37 | int patch() const { return (cbpx/10)%10; }
|
---|
38 | int pixel() const { return cbpx%10; }
|
---|
39 | int hw() const { return pixel()+patch()*9+board()*36+crate()*360; }
|
---|
40 | int group() const { return pixel()>3; }
|
---|
41 | int hv() const { return hv_channel+hv_board*32; }
|
---|
42 |
|
---|
43 | operator bool() const { return index>=0; }
|
---|
44 |
|
---|
45 | static const PixelMapEntry &empty() { const static PixelMapEntry e; return e; }
|
---|
46 | };
|
---|
47 |
|
---|
48 | class PixelMap : public std::vector<PixelMapEntry>
|
---|
49 | {
|
---|
50 | public:
|
---|
51 | PixelMap() : std::vector<PixelMapEntry>(1440)
|
---|
52 | {
|
---|
53 | }
|
---|
54 |
|
---|
55 | bool Read(const std::string &fname)
|
---|
56 | {
|
---|
57 | std::ifstream fin(fname);
|
---|
58 |
|
---|
59 | int l = 0;
|
---|
60 |
|
---|
61 | std::string buf;
|
---|
62 | while (getline(fin, buf, '\n'))
|
---|
63 | {
|
---|
64 | if (l>1439)
|
---|
65 | break;
|
---|
66 |
|
---|
67 | buf.erase(buf.find_last_not_of(' ')+1); //surfixing spaces
|
---|
68 | buf.erase(0, buf.find_first_not_of(' ')); //prefixing spaces
|
---|
69 |
|
---|
70 | if (buf.empty() || buf[0]=='#')
|
---|
71 | continue;
|
---|
72 |
|
---|
73 | std::stringstream str(buf);
|
---|
74 |
|
---|
75 | int idummy;
|
---|
76 | float fdummy;
|
---|
77 |
|
---|
78 | PixelMapEntry entry;
|
---|
79 |
|
---|
80 | str >> entry.index;
|
---|
81 | str >> entry.cbpx;
|
---|
82 | str >> idummy;
|
---|
83 | str >> idummy;
|
---|
84 | str >> entry.gapd;
|
---|
85 | str >> fdummy; //entry.Vgapd;
|
---|
86 | str >> entry.hv_board;
|
---|
87 | str >> entry.hv_channel;
|
---|
88 | //str >> fdummy;
|
---|
89 | //str >> fdummy;
|
---|
90 | //str >> fdummy;
|
---|
91 |
|
---|
92 | if (entry.hv_channel+32*entry.hv_board>=416/*BIAS::kNumChannels*/)
|
---|
93 | {
|
---|
94 | #ifdef DEBUG
|
---|
95 | cerr << "Invalid board/channel read from FACTmapV5.txt." << endl;
|
---|
96 | #endif
|
---|
97 | return false;
|
---|
98 | }
|
---|
99 |
|
---|
100 | (*this)[l++] = entry;
|
---|
101 | }
|
---|
102 |
|
---|
103 | return l==1440;
|
---|
104 | }
|
---|
105 |
|
---|
106 | const PixelMapEntry &index(int idx) const
|
---|
107 | {
|
---|
108 | for (std::vector<PixelMapEntry>::const_iterator it=begin(); it!=end(); it++)
|
---|
109 | if (it->index==idx)
|
---|
110 | return *it;
|
---|
111 | #ifdef DEBUG
|
---|
112 | std::cerr << "PixelMap: index " << idx << " not found" << std::endl;
|
---|
113 | #endif
|
---|
114 | return PixelMapEntry::empty();
|
---|
115 | }
|
---|
116 |
|
---|
117 | const PixelMapEntry &cbpx(int c) const
|
---|
118 | {
|
---|
119 | for (std::vector<PixelMapEntry>::const_iterator it=begin(); it!=end(); it++)
|
---|
120 | if (it->cbpx==c)
|
---|
121 | return *it;
|
---|
122 | #ifdef DEBUG
|
---|
123 | std::cerr << "PixelMap: cbpx " << c << " not found" << std::endl;
|
---|
124 | #endif
|
---|
125 | return PixelMapEntry::empty();
|
---|
126 | }
|
---|
127 |
|
---|
128 | const PixelMapEntry &cbpx(int c, int b, int p, int px) const
|
---|
129 | {
|
---|
130 | return cbpx(px + p*10 + b*100 + c*1000);
|
---|
131 | }
|
---|
132 |
|
---|
133 | const PixelMapEntry &hw(int idx) const
|
---|
134 | {
|
---|
135 | return cbpx(idx/360, (idx/36)%10, (idx/9)%4, idx%9);
|
---|
136 | }
|
---|
137 |
|
---|
138 | const PixelMapEntry &hv(int board, int channel) const
|
---|
139 | {
|
---|
140 | for (std::vector<PixelMapEntry>::const_iterator it=begin(); it!=end(); it++)
|
---|
141 | if (it->hv_board==board && it->hv_channel==channel)
|
---|
142 | return *it;
|
---|
143 | #ifdef DEBUG
|
---|
144 | std::cerr << "PixelMap: hv " << board << "/" << channel << " not found" << std::endl;
|
---|
145 | #endif
|
---|
146 | return PixelMapEntry::empty();
|
---|
147 | }
|
---|
148 |
|
---|
149 | const PixelMapEntry &hv(int idx) const
|
---|
150 | {
|
---|
151 | return hv(idx/32, idx%32);
|
---|
152 | }
|
---|
153 |
|
---|
154 | /*
|
---|
155 | float Vgapd(int board, int channel) const
|
---|
156 | {
|
---|
157 | float avg = 0;
|
---|
158 | int num = 0;
|
---|
159 |
|
---|
160 | for (std::vector<PixelMapEntry>::const_iterator it=begin(); it!=end(); it++)
|
---|
161 | if (it->hv_board==board && it->hv_channel==channel)
|
---|
162 | {
|
---|
163 | avg += it->Vgapd;
|
---|
164 | num ++;
|
---|
165 | }
|
---|
166 |
|
---|
167 | return num==0 ? 0 : avg/num;
|
---|
168 | }
|
---|
169 |
|
---|
170 | float Vgapd(int idx) const
|
---|
171 | {
|
---|
172 | return Vgapd(idx/32, idx%32);
|
---|
173 | }
|
---|
174 |
|
---|
175 | std::vector<float> Vgapd() const
|
---|
176 | {
|
---|
177 | std::vector<float> avg(416);
|
---|
178 | std::vector<int> num(416);
|
---|
179 |
|
---|
180 | for (std::vector<PixelMapEntry>::const_iterator it=begin(); it!=end(); it++)
|
---|
181 | {
|
---|
182 | const int ch = it->hv_board*32 + it->hv_channel;
|
---|
183 |
|
---|
184 | avg[ch] += it->Vgapd;
|
---|
185 | num[ch] ++;
|
---|
186 | }
|
---|
187 |
|
---|
188 | for (int ch=0; ch<416; ch++)
|
---|
189 | {
|
---|
190 | if (num[ch])
|
---|
191 | avg[ch] /= num[ch];
|
---|
192 | }
|
---|
193 |
|
---|
194 | return avg;
|
---|
195 | }*/
|
---|
196 | };
|
---|
197 |
|
---|
198 | struct BiasMapEntry
|
---|
199 | {
|
---|
200 | int hv_board; /// Bias suppply board
|
---|
201 | int hv_channel; /// Bias supply channel
|
---|
202 | float Vnom; /// Channel bias voltage nominal
|
---|
203 | float Voff; /// Channel bias voltage offset
|
---|
204 |
|
---|
205 | BiasMapEntry() : hv_board(-1) { }
|
---|
206 |
|
---|
207 | int hv() const { return hv_channel+hv_board*32; }
|
---|
208 |
|
---|
209 | operator bool() const { return hv_board>=0; }
|
---|
210 |
|
---|
211 | static const BiasMapEntry &empty() { const static BiasMapEntry e; return e; }
|
---|
212 | };
|
---|
213 |
|
---|
214 | class BiasMap : public std::vector<BiasMapEntry>
|
---|
215 | {
|
---|
216 | public:
|
---|
217 | BiasMap() : std::vector<BiasMapEntry>(416)
|
---|
218 | {
|
---|
219 | }
|
---|
220 |
|
---|
221 | #ifndef __MARS__
|
---|
222 | void Retrieve(const std::string &database);
|
---|
223 | #endif
|
---|
224 | bool Read(const std::string &fname)
|
---|
225 | {
|
---|
226 | std::ifstream fin(fname);
|
---|
227 |
|
---|
228 | int l = 0;
|
---|
229 |
|
---|
230 | std::string buf;
|
---|
231 | while (getline(fin, buf, '\n'))
|
---|
232 | {
|
---|
233 | if (l>416)
|
---|
234 | break;
|
---|
235 |
|
---|
236 | buf.erase(buf.find_last_not_of(' ')+1); //surfixing spaces
|
---|
237 | buf.erase(0, buf.find_first_not_of(' ')); //prefixing spaces
|
---|
238 |
|
---|
239 | if (buf.empty() || buf[0]=='#')
|
---|
240 | continue;
|
---|
241 |
|
---|
242 | std::stringstream str(buf);
|
---|
243 |
|
---|
244 | BiasMapEntry entry;
|
---|
245 |
|
---|
246 | str >> entry.hv_board;
|
---|
247 | str >> entry.hv_channel;
|
---|
248 | str >> entry.Vnom;
|
---|
249 | str >> entry.Voff;
|
---|
250 |
|
---|
251 | #ifdef __EXCEPTIONS
|
---|
252 | if (entry.hv_channel+32*entry.hv_board>=416)
|
---|
253 | throw std::runtime_error("Invalid board/channel read from "+fname+".");
|
---|
254 | #endif
|
---|
255 | #ifdef __MARS__
|
---|
256 | if (entry.hv_channel+32*entry.hv_board>=416)
|
---|
257 | {
|
---|
258 | gLog << err << "Invalid board/channel read from " << fname << "." << std::endl;
|
---|
259 | return false;
|
---|
260 | }
|
---|
261 | #endif
|
---|
262 |
|
---|
263 | (*this)[entry.hv()] = entry;
|
---|
264 |
|
---|
265 | l++;
|
---|
266 | }
|
---|
267 |
|
---|
268 | #ifdef __EXCEPTIONS
|
---|
269 | if (l!=416)
|
---|
270 | throw std::runtime_error("Number of lines read from "+fname+" does not match 416.");
|
---|
271 |
|
---|
272 | if (size()!=416)
|
---|
273 | throw std::runtime_error("Number of entries read from "+fname+" does not match 416.");
|
---|
274 | #endif
|
---|
275 |
|
---|
276 | #ifdef __MARS__
|
---|
277 | if (l!=416)
|
---|
278 | {
|
---|
279 | gLog << err << "Number of lines read from " << fname << " does not match 416." << std::endl;
|
---|
280 | return false;
|
---|
281 | }
|
---|
282 |
|
---|
283 | if (size()!=416)
|
---|
284 | {
|
---|
285 | gLog << "Number of entries read from " << fname << " does not match 416." << std::endl;
|
---|
286 | return false;
|
---|
287 | }
|
---|
288 | #endif
|
---|
289 |
|
---|
290 | return true;
|
---|
291 | }
|
---|
292 |
|
---|
293 | const BiasMapEntry &hv(int board, int channel) const
|
---|
294 | {
|
---|
295 | for (std::vector<BiasMapEntry>::const_iterator it=begin(); it!=end(); it++)
|
---|
296 | if (it->hv_board==board && it->hv_channel==channel)
|
---|
297 | return *it;
|
---|
298 | #ifdef DEBUG
|
---|
299 | std::cerr << "PixelMap: hv " << board << "/" << channel << " not found" << std::endl;
|
---|
300 | #endif
|
---|
301 | return BiasMapEntry::empty();
|
---|
302 | }
|
---|
303 |
|
---|
304 | const BiasMapEntry &hv(int idx) const
|
---|
305 | {
|
---|
306 | return hv(idx/32, idx%32);
|
---|
307 | }
|
---|
308 |
|
---|
309 | const BiasMapEntry &hv(const PixelMapEntry &p) const
|
---|
310 | {
|
---|
311 | return hv(p.hv_board, p.hv_channel);
|
---|
312 | }
|
---|
313 |
|
---|
314 | /*
|
---|
315 | float Vgapd(int board, int channel) const
|
---|
316 | {
|
---|
317 | const BiasMapEntry &entry = hv(board, channel);
|
---|
318 | return entry.Vnom - entry.Voff; // use this with GAPDmap_20111126.txt
|
---|
319 | }
|
---|
320 |
|
---|
321 | float Vgapd(int idx) const
|
---|
322 | {
|
---|
323 | return Vgapd(idx/32, idx%32);
|
---|
324 | }*/
|
---|
325 |
|
---|
326 | std::vector<float> Vgapd() const
|
---|
327 | {
|
---|
328 | std::vector<float> volt(416);
|
---|
329 |
|
---|
330 | for (std::vector<BiasMapEntry>::const_iterator it=begin(); it!=end(); it++)
|
---|
331 | {
|
---|
332 | const int ch = it->hv_board*32 + it->hv_channel;
|
---|
333 | volt[ch] = it->Vnom;
|
---|
334 | }
|
---|
335 |
|
---|
336 | return volt;
|
---|
337 | }
|
---|
338 | std::vector<float> Voffset() const
|
---|
339 | {
|
---|
340 | std::vector<float> volt(416);
|
---|
341 |
|
---|
342 | for (std::vector<BiasMapEntry>::const_iterator it=begin(); it!=end(); it++)
|
---|
343 | {
|
---|
344 | const int ch = it->hv_board*32 + it->hv_channel;
|
---|
345 | volt[ch] = -it->Voff;
|
---|
346 | }
|
---|
347 |
|
---|
348 | return volt;
|
---|
349 | }
|
---|
350 | };
|
---|
351 |
|
---|
352 | #endif
|
---|