| 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 |  | 
|---|
| 17 | struct 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()>3; } | 
|---|
| 32 | int hv() const    { return hv_channel+hv_board*32; } | 
|---|
| 33 |  | 
|---|
| 34 | operator bool() const { return index>=0; } | 
|---|
| 35 | }; | 
|---|
| 36 |  | 
|---|
| 37 | class PixelMap : public std::vector<PixelMapEntry> | 
|---|
| 38 | { | 
|---|
| 39 | public: | 
|---|
| 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 | float   fdummy; | 
|---|
| 66 |  | 
|---|
| 67 | PixelMapEntry entry; | 
|---|
| 68 |  | 
|---|
| 69 | str >> entry.index; | 
|---|
| 70 | str >> entry.cbpx; | 
|---|
| 71 | str >> idummy; | 
|---|
| 72 | str >> idummy; | 
|---|
| 73 | str >> entry.gapd; | 
|---|
| 74 | str >> fdummy; //entry.Vgapd; | 
|---|
| 75 | str >> entry.hv_board; | 
|---|
| 76 | str >> entry.hv_channel; | 
|---|
| 77 | //str >> fdummy; | 
|---|
| 78 | //str >> fdummy; | 
|---|
| 79 | //str >> fdummy; | 
|---|
| 80 |  | 
|---|
| 81 | if (entry.hv_channel+32*entry.hv_board>=416/*BIAS::kNumChannels*/) | 
|---|
| 82 | { | 
|---|
| 83 | #ifdef DEBUG | 
|---|
| 84 | cerr << "Invalid board/channel read from FACTmapV5.txt." << endl; | 
|---|
| 85 | #endif | 
|---|
| 86 | return false; | 
|---|
| 87 | } | 
|---|
| 88 |  | 
|---|
| 89 | (*this)[l++] = entry; | 
|---|
| 90 | } | 
|---|
| 91 |  | 
|---|
| 92 | return l==1440; | 
|---|
| 93 | } | 
|---|
| 94 |  | 
|---|
| 95 | const PixelMapEntry &index(int idx) const | 
|---|
| 96 | { | 
|---|
| 97 | for (std::vector<PixelMapEntry>::const_iterator it=begin(); it!=end(); it++) | 
|---|
| 98 | if (it->index==idx) | 
|---|
| 99 | return *it; | 
|---|
| 100 | #ifdef DEBUG | 
|---|
| 101 | std::cerr << "PixelMap: index " << idx << " not found" << std::endl; | 
|---|
| 102 | #endif | 
|---|
| 103 | return empty; | 
|---|
| 104 | } | 
|---|
| 105 |  | 
|---|
| 106 | const PixelMapEntry &cbpx(int c) const | 
|---|
| 107 | { | 
|---|
| 108 | for (std::vector<PixelMapEntry>::const_iterator it=begin(); it!=end(); it++) | 
|---|
| 109 | if (it->cbpx==c) | 
|---|
| 110 | return *it; | 
|---|
| 111 | #ifdef DEBUG | 
|---|
| 112 | std::cerr << "PixelMap: cbpx " << c << " not found" << std::endl; | 
|---|
| 113 | #endif | 
|---|
| 114 | return empty; | 
|---|
| 115 | } | 
|---|
| 116 |  | 
|---|
| 117 | const PixelMapEntry &cbpx(int c, int b, int p, int px) const | 
|---|
| 118 | { | 
|---|
| 119 | return cbpx(px + p*10 + b*100 + c*1000); | 
|---|
| 120 | } | 
|---|
| 121 |  | 
|---|
| 122 | const PixelMapEntry &hw(int idx) const | 
|---|
| 123 | { | 
|---|
| 124 | return cbpx(idx/360, (idx/36)%10, (idx/9)%4, idx%9); | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 | const PixelMapEntry &hv(int board, int channel) const | 
|---|
| 128 | { | 
|---|
| 129 | for (std::vector<PixelMapEntry>::const_iterator it=begin(); it!=end(); it++) | 
|---|
| 130 | if (it->hv_board==board && it->hv_channel==channel) | 
|---|
| 131 | return *it; | 
|---|
| 132 | #ifdef DEBUG | 
|---|
| 133 | std::cerr << "PixelMap: hv " << board << "/" << channel << " not found" << std::endl; | 
|---|
| 134 | #endif | 
|---|
| 135 | return empty; | 
|---|
| 136 | } | 
|---|
| 137 |  | 
|---|
| 138 | const PixelMapEntry &hv(int idx) const | 
|---|
| 139 | { | 
|---|
| 140 | return hv(idx/32, idx%32); | 
|---|
| 141 | } | 
|---|
| 142 |  | 
|---|
| 143 | /* | 
|---|
| 144 | float Vgapd(int board, int channel) const | 
|---|
| 145 | { | 
|---|
| 146 | float avg = 0; | 
|---|
| 147 | int   num = 0; | 
|---|
| 148 |  | 
|---|
| 149 | for (std::vector<PixelMapEntry>::const_iterator it=begin(); it!=end(); it++) | 
|---|
| 150 | if (it->hv_board==board && it->hv_channel==channel) | 
|---|
| 151 | { | 
|---|
| 152 | avg += it->Vgapd; | 
|---|
| 153 | num ++; | 
|---|
| 154 | } | 
|---|
| 155 |  | 
|---|
| 156 | return num==0 ? 0 : avg/num; | 
|---|
| 157 | } | 
|---|
| 158 |  | 
|---|
| 159 | float Vgapd(int idx) const | 
|---|
| 160 | { | 
|---|
| 161 | return Vgapd(idx/32, idx%32); | 
|---|
| 162 | } | 
|---|
| 163 |  | 
|---|
| 164 | std::vector<float> Vgapd() const | 
|---|
| 165 | { | 
|---|
| 166 | std::vector<float> avg(416); | 
|---|
| 167 | std::vector<int>   num(416); | 
|---|
| 168 |  | 
|---|
| 169 | for (std::vector<PixelMapEntry>::const_iterator it=begin(); it!=end(); it++) | 
|---|
| 170 | { | 
|---|
| 171 | const int ch = it->hv_board*32 + it->hv_channel; | 
|---|
| 172 |  | 
|---|
| 173 | avg[ch] += it->Vgapd; | 
|---|
| 174 | num[ch] ++; | 
|---|
| 175 | } | 
|---|
| 176 |  | 
|---|
| 177 | for (int ch=0; ch<416; ch++) | 
|---|
| 178 | { | 
|---|
| 179 | if (num[ch]) | 
|---|
| 180 | avg[ch] /= num[ch]; | 
|---|
| 181 | } | 
|---|
| 182 |  | 
|---|
| 183 | return avg; | 
|---|
| 184 | }*/ | 
|---|
| 185 | }; | 
|---|
| 186 |  | 
|---|
| 187 | struct BiasMapEntry | 
|---|
| 188 | { | 
|---|
| 189 | int   hv_board;            /// Bias suppply board | 
|---|
| 190 | int   hv_channel;          /// Bias supply channel | 
|---|
| 191 | float Vnom;                /// Channel bias voltage nominal | 
|---|
| 192 | float Voff;                /// Channel bias voltage offset | 
|---|
| 193 |  | 
|---|
| 194 | int hv() const { return hv_channel+hv_board*32; } | 
|---|
| 195 | }; | 
|---|
| 196 |  | 
|---|
| 197 | class BiasMap : public std::vector<BiasMapEntry> | 
|---|
| 198 | { | 
|---|
| 199 | public: | 
|---|
| 200 | static const BiasMapEntry empty; | 
|---|
| 201 |  | 
|---|
| 202 | BiasMap() : std::vector<BiasMapEntry>(416) | 
|---|
| 203 | { | 
|---|
| 204 | } | 
|---|
| 205 |  | 
|---|
| 206 | void Retrieve(const std::string &database); | 
|---|
| 207 | void Read(const std::string &fname); | 
|---|
| 208 |  | 
|---|
| 209 | const BiasMapEntry &hv(int board, int channel) const | 
|---|
| 210 | { | 
|---|
| 211 | for (std::vector<BiasMapEntry>::const_iterator it=begin(); it!=end(); it++) | 
|---|
| 212 | if (it->hv_board==board && it->hv_channel==channel) | 
|---|
| 213 | return *it; | 
|---|
| 214 | #ifdef DEBUG | 
|---|
| 215 | std::cerr << "PixelMap: hv " << board << "/" << channel << " not found" << std::endl; | 
|---|
| 216 | #endif | 
|---|
| 217 | return empty; | 
|---|
| 218 | } | 
|---|
| 219 |  | 
|---|
| 220 | const BiasMapEntry &hv(int idx) const | 
|---|
| 221 | { | 
|---|
| 222 | return hv(idx/32, idx%32); | 
|---|
| 223 | } | 
|---|
| 224 |  | 
|---|
| 225 | /* | 
|---|
| 226 | float Vgapd(int board, int channel) const | 
|---|
| 227 | { | 
|---|
| 228 | const BiasMapEntry &entry = hv(board, channel); | 
|---|
| 229 | return entry.Vnom - entry.Voff; // use this with GAPDmap_20111126.txt | 
|---|
| 230 | } | 
|---|
| 231 |  | 
|---|
| 232 | float Vgapd(int idx) const | 
|---|
| 233 | { | 
|---|
| 234 | return Vgapd(idx/32, idx%32); | 
|---|
| 235 | }*/ | 
|---|
| 236 |  | 
|---|
| 237 | std::vector<float> Vgapd() const | 
|---|
| 238 | { | 
|---|
| 239 | std::vector<float> volt(416); | 
|---|
| 240 |  | 
|---|
| 241 | for (std::vector<BiasMapEntry>::const_iterator it=begin(); it!=end(); it++) | 
|---|
| 242 | { | 
|---|
| 243 | const int ch = it->hv_board*32 + it->hv_channel; | 
|---|
| 244 | volt[ch] = it->Vnom; | 
|---|
| 245 | } | 
|---|
| 246 |  | 
|---|
| 247 | return volt; | 
|---|
| 248 | } | 
|---|
| 249 | std::vector<float> Voffset() const | 
|---|
| 250 | { | 
|---|
| 251 | std::vector<float> volt(416); | 
|---|
| 252 |  | 
|---|
| 253 | for (std::vector<BiasMapEntry>::const_iterator it=begin(); it!=end(); it++) | 
|---|
| 254 | { | 
|---|
| 255 | const int ch = it->hv_board*32 + it->hv_channel; | 
|---|
| 256 | volt[ch] = -it->Voff; | 
|---|
| 257 | } | 
|---|
| 258 |  | 
|---|
| 259 | return volt; | 
|---|
| 260 | } | 
|---|
| 261 | }; | 
|---|
| 262 |  | 
|---|
| 263 | #endif | 
|---|