source: trunk/FACT++/src/PixelMap.cc@ 13609

Last change on this file since 13609 was 13167, checked in by tbretz, 13 years ago
Moved read to cc file; implemented first version of a database interface.
File size: 2.9 KB
Line 
1#include "PixelMap.h"
2
3#include <boost/regex.hpp>
4
5//#ifdef HAS_SQL
6#include <mysql++/mysql++.h>
7//#endif
8
9using namespace std;
10
11const PixelMapEntry PixelMap::empty = { -1, 0, 0, 0, 0 };
12const BiasMapEntry BiasMap::empty = { -1, 0, 0, 0 };
13
14void BiasMap::Read(const std::string &fname)
15{
16 std::ifstream fin(fname);
17
18 int l = 0;
19
20 std::string buf;
21 while (getline(fin, buf, '\n'))
22 {
23 if (l>416)
24 break;
25
26 buf = Tools::Trim(buf);
27 if (buf[0]=='#')
28 continue;
29
30 std::stringstream str(buf);
31
32 BiasMapEntry entry;
33
34 str >> entry.hv_board;
35 str >> entry.hv_channel;
36 str >> entry.Vnom;
37 str >> entry.Voff;
38
39 if (entry.hv_channel+32*entry.hv_board>=416)
40 throw runtime_error("Invalid board/channel read from "+fname+".");
41
42 (*this)[entry.hv()] = entry;
43
44 l++;
45 }
46
47 if (l!=416)
48 throw runtime_error("Number of lines read from "+fname+" does not match 416.");
49
50 if (size()!=416)
51 throw runtime_error("Number of entries read from "+fname+" does not match 416.");
52}
53
54void BiasMap::Retrieve(const std::string &database)
55{
56 static const boost::regex expr("(([[:word:].-]+)(:(.+))?@)?([[:word:].-]+)(:([[:digit:]]+))?(/([[:word:].-]+))");
57 // 2: user
58 // 4: pass
59 // 5: server
60 // 7: port
61 // 9: db
62
63 boost::smatch what;
64 if (!boost::regex_match(database, what, expr, boost::match_extra))
65 throw runtime_error("Couldn't parse '"+database+"'.");
66
67 if (what.size()!=10)
68 throw runtime_error("Error parsing '"+database+"'.");
69
70 const string user = what[2];
71 const string passwd = what[4];
72 const string server = what[5];
73 const string db = what[9];
74 const int port = atoi(string(what[7]).c_str());
75
76 mysqlpp::Connection conn(db.c_str(), server.c_str(), user.c_str(), passwd.c_str(), port);
77
78 const mysqlpp::StoreQueryResult res =
79 conn.query("SELECT fPatchNumber, AVG(fVoltageNom), fOffset "
80 " FROM GapdVoltages "
81 " LEFT JOIN BiasOffsets USING(fPatchNumber) "
82 " GROUP BY fPatchNumber").store();
83
84 clear();
85
86 int l = 0;
87 for (vector<mysqlpp::Row>::const_iterator v=res.begin(); v<res.end(); v++)
88 {
89 const int id = (*v)[0];
90
91 if (id<0 || id>416)
92 {
93 ostringstream str;
94 str << "Invalid channel id " << id << " received from database.";
95 throw runtime_error(str.str());
96 }
97
98 BiasMapEntry entry;
99
100 entry.hv_board = id/32;
101 entry.hv_channel = id%32;
102 entry.Vnom = (*v)[1];
103 entry.Voff = (*v)[2];
104
105 (*this)[id] = entry;
106
107 l++;
108 }
109
110 if (l!=416)
111 throw runtime_error("Number of rows retrieved from the database does not match 416.");
112
113 if (size()!=416)
114 throw runtime_error("Number of entries retrived from database does not match 416.");
115}
Note: See TracBrowser for help on using the repository browser.