1 | #include "PixelMap.h"
|
---|
2 |
|
---|
3 | #ifdef __EXCEPTIONS
|
---|
4 | #include <stdexcept>
|
---|
5 | #endif
|
---|
6 |
|
---|
7 | #ifdef __MARS__
|
---|
8 | #include "MLog.h"
|
---|
9 | #include "MLogManip.h"
|
---|
10 | #endif
|
---|
11 |
|
---|
12 | using namespace std;
|
---|
13 |
|
---|
14 | const PixelMapEntry PixelMap::empty = { -1, 0, 0, 0, 0 };
|
---|
15 | const BiasMapEntry BiasMap::empty = { -1, 0, 0, 0 };
|
---|
16 |
|
---|
17 | bool BiasMap::Read(const std::string &fname)
|
---|
18 | {
|
---|
19 | std::ifstream fin(fname);
|
---|
20 |
|
---|
21 | int l = 0;
|
---|
22 |
|
---|
23 | std::string buf;
|
---|
24 | while (getline(fin, buf, '\n'))
|
---|
25 | {
|
---|
26 | if (l>416)
|
---|
27 | break;
|
---|
28 |
|
---|
29 | buf = Tools::Trim(buf);
|
---|
30 | if (buf[0]=='#')
|
---|
31 | continue;
|
---|
32 |
|
---|
33 | std::stringstream str(buf);
|
---|
34 |
|
---|
35 | BiasMapEntry entry;
|
---|
36 |
|
---|
37 | str >> entry.hv_board;
|
---|
38 | str >> entry.hv_channel;
|
---|
39 | str >> entry.Vnom;
|
---|
40 | str >> entry.Voff;
|
---|
41 |
|
---|
42 | #ifdef __EXCEPTIONS
|
---|
43 | if (entry.hv_channel+32*entry.hv_board>=416)
|
---|
44 | throw runtime_error("Invalid board/channel read from "+fname+".");
|
---|
45 | #endif
|
---|
46 | #ifdef __MARS__
|
---|
47 | if (entry.hv_channel+32*entry.hv_board>=416)
|
---|
48 | {
|
---|
49 | gLog << err << "Invalid board/channel read from " << fname << "." << endl;
|
---|
50 | return false;
|
---|
51 | }
|
---|
52 | #endif
|
---|
53 |
|
---|
54 | (*this)[entry.hv()] = entry;
|
---|
55 |
|
---|
56 | l++;
|
---|
57 | }
|
---|
58 |
|
---|
59 | #ifdef __EXCEPTIONS
|
---|
60 | if (l!=416)
|
---|
61 | throw runtime_error("Number of lines read from "+fname+" does not match 416.");
|
---|
62 |
|
---|
63 | if (size()!=416)
|
---|
64 | throw runtime_error("Number of entries read from "+fname+" does not match 416.");
|
---|
65 | #endif
|
---|
66 |
|
---|
67 | #ifdef __MARS__
|
---|
68 | if (l!=416)
|
---|
69 | {
|
---|
70 | gLog << err << "Number of lines read from " << fname << " does not match 416." << endl;
|
---|
71 | return false;
|
---|
72 | }
|
---|
73 |
|
---|
74 | if (size()!=416)
|
---|
75 | {
|
---|
76 | gLog << "Number of entries read from " << fname << " does not match 416." << endl;
|
---|
77 | return false;
|
---|
78 | }
|
---|
79 | #endif
|
---|
80 |
|
---|
81 | return true;
|
---|
82 | }
|
---|
83 |
|
---|
84 | #ifndef __MARS__
|
---|
85 | #include <boost/regex.hpp>
|
---|
86 | #include <mysql++/mysql++.h>
|
---|
87 |
|
---|
88 | void BiasMap::Retrieve(const std::string &database)
|
---|
89 | {
|
---|
90 | static const boost::regex expr("(([[:word:].-]+)(:(.+))?@)?([[:word:].-]+)(:([[:digit:]]+))?(/([[:word:].-]+))");
|
---|
91 | // 2: user
|
---|
92 | // 4: pass
|
---|
93 | // 5: server
|
---|
94 | // 7: port
|
---|
95 | // 9: db
|
---|
96 |
|
---|
97 | boost::smatch what;
|
---|
98 | if (!boost::regex_match(database, what, expr, boost::match_extra))
|
---|
99 | throw runtime_error("Couldn't parse '"+database+"'.");
|
---|
100 |
|
---|
101 | if (what.size()!=10)
|
---|
102 | throw runtime_error("Error parsing '"+database+"'.");
|
---|
103 |
|
---|
104 | const string user = what[2];
|
---|
105 | const string passwd = what[4];
|
---|
106 | const string server = what[5];
|
---|
107 | const string db = what[9];
|
---|
108 | const int port = atoi(string(what[7]).c_str());
|
---|
109 |
|
---|
110 | mysqlpp::Connection conn(db.c_str(), server.c_str(), user.c_str(), passwd.c_str(), port);
|
---|
111 |
|
---|
112 | const mysqlpp::StoreQueryResult res =
|
---|
113 | conn.query("SELECT fPatchNumber, AVG(fVoltageNom), fOffset "
|
---|
114 | " FROM GapdVoltages "
|
---|
115 | " LEFT JOIN BiasOffsets USING(fPatchNumber) "
|
---|
116 | " GROUP BY fPatchNumber").store();
|
---|
117 |
|
---|
118 | clear();
|
---|
119 |
|
---|
120 | int l = 0;
|
---|
121 | for (vector<mysqlpp::Row>::const_iterator v=res.begin(); v<res.end(); v++)
|
---|
122 | {
|
---|
123 | const int id = (*v)[0];
|
---|
124 |
|
---|
125 | if (id<0 || id>416)
|
---|
126 | {
|
---|
127 | ostringstream str;
|
---|
128 | str << "Invalid channel id " << id << " received from database.";
|
---|
129 | throw runtime_error(str.str());
|
---|
130 | }
|
---|
131 |
|
---|
132 | BiasMapEntry entry;
|
---|
133 |
|
---|
134 | entry.hv_board = id/32;
|
---|
135 | entry.hv_channel = id%32;
|
---|
136 | entry.Vnom = (*v)[1];
|
---|
137 | entry.Voff = (*v)[2];
|
---|
138 |
|
---|
139 | (*this)[id] = entry;
|
---|
140 |
|
---|
141 | l++;
|
---|
142 | }
|
---|
143 |
|
---|
144 | if (l!=416)
|
---|
145 | throw runtime_error("Number of rows retrieved from the database does not match 416.");
|
---|
146 |
|
---|
147 | if (size()!=416)
|
---|
148 | throw runtime_error("Number of entries retrived from database does not match 416.");
|
---|
149 | }
|
---|
150 | #endif
|
---|