source: branches/Mars_MC/mcore/PixelMap.h@ 17357

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