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

Last change on this file since 12360 was 12164, checked in by tbretz, 13 years ago
Added bool-cast operator to PixelMapEntry.
File size: 4.4 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()>4; }
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
66 PixelMapEntry entry;
67
68 str >> entry.index;
69 str >> entry.cbpx;
70 str >> idummy;
71 str >> idummy;
72 str >> entry.gapd;
73 str >> entry.Vgapd;
74 str >> entry.hv_board;
75 str >> entry.hv_channel;
76 //str >> fdummy;
77 //str >> fdummy;
78 //str >> fdummy;
79
80 if (entry.hv_channel+32*entry.hv_board>=416/*BIAS::kNumChannels*/)
81 {
82#ifdef DEBUG
83 cerr << "Invalid board/channel read from FACTmapV5.txt." << endl;
84#endif
85 return false;
86 }
87
88 (*this)[l++] = entry;
89 }
90
91 return l==1440;
92 }
93
94 const PixelMapEntry &index(int idx) const
95 {
96 for (std::vector<PixelMapEntry>::const_iterator it=begin(); it!=end(); it++)
97 if (it->index==idx)
98 return *it;
99#ifdef DEBUG
100 std::cerr << "PixelMap: index " << idx << " not found" << std::endl;
101#endif
102 return empty;
103 }
104
105 const PixelMapEntry &cbpx(int c) const
106 {
107 for (std::vector<PixelMapEntry>::const_iterator it=begin(); it!=end(); it++)
108 if (it->cbpx==c)
109 return *it;
110#ifdef DEBUG
111 std::cerr << "PixelMap: cbpx " << c << " not found" << std::endl;
112#endif
113 return empty;
114 }
115
116 const PixelMapEntry &cbpx(int c, int b, int p, int px) const
117 {
118 return cbpx(px + p*10 + b*100 + c*1000);
119 }
120
121 const PixelMapEntry &hw(int idx) const
122 {
123 return cbpx(idx/360, (idx/36)%10, (idx/9)%4, idx%9);
124 }
125
126 const PixelMapEntry &hv(int board, int channel) const
127 {
128 for (std::vector<PixelMapEntry>::const_iterator it=begin(); it!=end(); it++)
129 if (it->hv_board==board && it->hv_channel==channel)
130 return *it;
131#ifdef DEBUG
132 std::cerr << "PixelMap: hv " << board << "/" << channel << " not found" << std::endl;
133#endif
134 return empty;
135 }
136
137 const PixelMapEntry &hv(int idx) const
138 {
139 return hv(idx/32, idx%32);
140 }
141
142 float Vgapd(int board, int channel) const
143 {
144 float avg = 0;
145 int num = 0;
146
147 for (std::vector<PixelMapEntry>::const_iterator it=begin(); it!=end(); it++)
148 if (it->hv_board==board && it->hv_channel==channel)
149 {
150 avg += it->Vgapd;
151 num ++;
152 }
153
154 return num==0 ? 0 : avg/num;
155 }
156
157 float Vgapd(int idx) const
158 {
159 return Vgapd(idx/32, idx%32);
160 }
161
162 std::vector<float> Vgapd() const
163 {
164 std::vector<float> avg(416);
165 std::vector<int> num(416);
166
167 for (std::vector<PixelMapEntry>::const_iterator it=begin(); it!=end(); it++)
168 {
169 const int ch = it->hv_board*32 + it->hv_channel;
170
171 avg[ch] += it->Vgapd;
172 num[ch] ++;
173 }
174
175 for (int ch=0; ch<416; ch++)
176 {
177 if (num[ch])
178 avg[ch] /= num[ch];
179 }
180
181 return avg;
182 }
183};
184
185#endif
Note: See TracBrowser for help on using the repository browser.