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

Last change on this file since 12064 was 12026, checked in by tbretz, 13 years ago
Fixed the hw() code.
File size: 4.5 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
35class PixelMap : public std::vector<PixelMapEntry>
36{
37public:
38 static const PixelMapEntry empty;
39
40 PixelMap() : std::vector<PixelMapEntry>(1440)
41 {
42 }
43
44 bool Read(const std::string &fname)
45 {
46 std::ifstream fin(fname);
47
48 int l = 0;
49
50 std::string buf;
51 while (getline(fin, buf, '\n'))
52 {
53 if (l>1439)
54 break;
55
56 buf = Tools::Trim(buf);
57 if (buf[0]=='#')
58 continue;
59
60 std::stringstream str(buf);
61
62 int idummy;
63
64 PixelMapEntry entry;
65
66 str >> entry.index;
67 str >> entry.cbpx;
68 str >> idummy;
69 str >> idummy;
70 str >> entry.gapd;
71 str >> entry.Vgapd;
72 str >> entry.hv_board;
73 str >> entry.hv_channel;
74 //str >> fdummy;
75 //str >> fdummy;
76 //str >> fdummy;
77
78 if (entry.hv_channel+32*entry.hv_board>=416/*BIAS::kNumChannels*/)
79 {
80#ifdef DEBUG
81 cerr << "Invalid board/channel read from FACTmapV5.txt." << endl;
82#endif
83 return false;
84 }
85
86 (*this)[l++] = entry;
87 }
88
89 return l==1440;
90 }
91
92 const PixelMapEntry &index(int idx) const
93 {
94 for (std::vector<PixelMapEntry>::const_iterator it=begin(); it!=end(); it++)
95 if (it->index==idx)
96 return *it;
97#ifdef DEBUG
98 std::cerr << "PixelMap: index " << idx << " not found" << std::endl;
99#endif
100 return empty;
101 }
102
103 const PixelMapEntry &cbpx(int c) const
104 {
105 for (std::vector<PixelMapEntry>::const_iterator it=begin(); it!=end(); it++)
106 if (it->cbpx==c)
107 return *it;
108#ifdef DEBUG
109 std::cerr << "PixelMap: cbpx " << c << " not found" << std::endl;
110#endif
111 return empty;
112 }
113
114 const PixelMapEntry &cbpx(int c, int b, int p, int px) const
115 {
116 return cbpx(px + p*10 + b*100 + c*1000);
117 }
118
119 const PixelMapEntry &hw(int idx) const
120 {
121 return cbpx(idx/360, (idx/36)%10, (idx/9)%4, idx%9);
122 }
123
124 const PixelMapEntry &hv(int board, int channel) const
125 {
126 for (std::vector<PixelMapEntry>::const_iterator it=begin(); it!=end(); it++)
127 if (it->hv_board==board && it->hv_channel==channel)
128 return *it;
129#ifdef DEBUG
130 std::cerr << "PixelMap: hv " << board << "/" << channel << " not found" << std::endl;
131#endif
132 return empty;
133 }
134
135 const PixelMapEntry &hv(int idx) const
136 {
137 return hv(idx/32, idx%32);
138 }
139
140 float Vgapd(int board, int channel) const
141 {
142 float avg = 0;
143 int num = 0;
144
145 for (std::vector<PixelMapEntry>::const_iterator it=begin(); it!=end(); it++)
146 if (it->hv_board==board && it->hv_channel==channel)
147 {
148 avg += it->Vgapd;
149 num ++;
150 }
151
152 return num==0 ? 0 : avg/num;
153 }
154
155 float Vgapd(int idx) const
156 {
157 return Vgapd(idx/32, idx%32);
158 }
159
160 std::vector<float> Vgapd() const
161 {
162 std::vector<float> avg(416);
163 std::vector<int> num(416);
164
165 for (std::vector<PixelMapEntry>::const_iterator it=begin(); it!=end(); it++)
166 {
167 const int ch = it->hv_board*32 + it->hv_channel;
168
169 avg[ch] += it->Vgapd;
170 num[ch] ++;
171 }
172
173 for (int ch=0; ch<416; ch++)
174 {
175 if (num[ch])
176 avg[ch] /= num[ch];
177 }
178
179 return avg;
180 }
181};
182
183const PixelMapEntry PixelMap::empty = { 0, 0, 0, 0, 0, 0 };
184
185#endif
Note: See TracBrowser for help on using the repository browser.