source: trunk/Mars/mcore/PixelMap.h@ 16966

Last change on this file since 16966 was 16966, checked in by tbretz, 11 years ago
Changed sign of the offset, so that the offset/slope combination represents a line in volt-vs-dac, added slope.
File size: 9.1 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#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
24struct 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
48class PixelMap : public std::vector<PixelMapEntry>
49{
50public:
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
198struct 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 float Vslope; /// Channel bias voltage slope
205
206 BiasMapEntry() : hv_board(-1) { }
207
208 int hv() const { return hv_channel+hv_board*32; }
209
210 operator bool() const { return hv_board>=0; }
211
212 static const BiasMapEntry &empty() { const static BiasMapEntry e; return e; }
213};
214
215class BiasMap : public std::vector<BiasMapEntry>
216{
217public:
218 BiasMap() : std::vector<BiasMapEntry>(416)
219 {
220 }
221
222#ifndef __MARS__
223 void Retrieve(const std::string &database);
224#endif
225 bool Read(const std::string &fname)
226 {
227 std::ifstream fin(fname);
228
229 int l = 0;
230
231 std::string buf;
232 while (getline(fin, buf, '\n'))
233 {
234 if (l>416)
235 break;
236
237 buf.erase(buf.find_last_not_of(' ')+1); //surfixing spaces
238 buf.erase(0, buf.find_first_not_of(' ')); //prefixing spaces
239
240 if (buf.empty() || buf[0]=='#')
241 continue;
242
243 std::stringstream str(buf);
244
245 BiasMapEntry entry;
246
247 str >> entry.hv_board;
248 str >> entry.hv_channel;
249 str >> entry.Vnom;
250 str >> entry.Voff;
251 str >> entry.Vslope;
252
253#ifdef __EXCEPTIONS
254 if (entry.hv_channel+32*entry.hv_board>=416)
255 throw std::runtime_error("Invalid board/channel read from "+fname+".");
256#endif
257#ifdef __MARS__
258 if (entry.hv_channel+32*entry.hv_board>=416)
259 {
260 gLog << err << "Invalid board/channel read from " << fname << "." << std::endl;
261 return false;
262 }
263#endif
264
265 (*this)[entry.hv()] = entry;
266
267 l++;
268 }
269
270#ifdef __EXCEPTIONS
271 if (l!=416)
272 throw std::runtime_error("Number of lines read from "+fname+" does not match 416.");
273
274 if (size()!=416)
275 throw std::runtime_error("Number of entries read from "+fname+" does not match 416.");
276#endif
277
278#ifdef __MARS__
279 if (l!=416)
280 {
281 gLog << err << "Number of lines read from " << fname << " does not match 416." << std::endl;
282 return false;
283 }
284
285 if (size()!=416)
286 {
287 gLog << "Number of entries read from " << fname << " does not match 416." << std::endl;
288 return false;
289 }
290#endif
291
292 return true;
293 }
294
295 const BiasMapEntry &hv(int board, int channel) const
296 {
297 for (std::vector<BiasMapEntry>::const_iterator it=begin(); it!=end(); it++)
298 if (it->hv_board==board && it->hv_channel==channel)
299 return *it;
300#ifdef DEBUG
301 std::cerr << "PixelMap: hv " << board << "/" << channel << " not found" << std::endl;
302#endif
303 return BiasMapEntry::empty();
304 }
305
306 const BiasMapEntry &hv(int idx) const
307 {
308 return hv(idx/32, idx%32);
309 }
310
311 const BiasMapEntry &hv(const PixelMapEntry &p) const
312 {
313 return hv(p.hv_board, p.hv_channel);
314 }
315
316 /*
317 float Vgapd(int board, int channel) const
318 {
319 const BiasMapEntry &entry = hv(board, channel);
320 return entry.Vnom - entry.Voff; // use this with GAPDmap_20111126.txt
321 }
322
323 float Vgapd(int idx) const
324 {
325 return Vgapd(idx/32, idx%32);
326 }*/
327
328 std::vector<float> Vgapd() const
329 {
330 std::vector<float> volt(416);
331
332 for (std::vector<BiasMapEntry>::const_iterator it=begin(); it!=end(); it++)
333 {
334 const int ch = it->hv_board*32 + it->hv_channel;
335 volt[ch] = it->Vnom;
336 }
337
338 return volt;
339 }
340 std::vector<float> Voffset() const
341 {
342 std::vector<float> volt(416);
343
344 for (std::vector<BiasMapEntry>::const_iterator it=begin(); it!=end(); it++)
345 {
346 const int ch = it->hv_board*32 + it->hv_channel;
347 volt[ch] = it->Voff;
348 }
349
350 return volt;
351 }
352
353 std::vector<float> Vslope() const
354 {
355 std::vector<float> slope(416);
356
357 for (std::vector<BiasMapEntry>::const_iterator it=begin(); it!=end(); it++)
358 {
359 const int ch = it->hv_board*32 + it->hv_channel;
360 slope[ch] = it->Vslope;
361 }
362
363 return slope;
364 }
365};
366
367#endif
Note: See TracBrowser for help on using the repository browser.