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

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