Changeset 13167 for trunk


Ignore:
Timestamp:
03/22/12 14:51:05 (13 years ago)
Author:
tbretz
Message:
Moved read to cc file; implemented first version of a database interface.
Location:
trunk/FACT++/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/FACT++/src/PixelMap.cc

    r12507 r13167  
    11#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;
    210
    311const PixelMapEntry PixelMap::empty = { -1, 0, 0, 0, 0 };
    412const 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}
  • trunk/FACT++/src/PixelMap.h

    r12699 r13167  
    204204    }
    205205
    206     bool Read(const std::string &fname)
    207     {
    208         std::ifstream fin(fname);
    209 
    210         int l = 0;
    211 
    212         std::string buf;
    213         while (getline(fin, buf, '\n'))
    214         {
    215             if (l>416)
    216                 break;
    217 
    218             buf = Tools::Trim(buf);
    219             if (buf[0]=='#')
    220                 continue;
    221 
    222             std::stringstream str(buf);
    223 
    224             BiasMapEntry entry;
    225 
    226             str >> entry.hv_board;
    227             str >> entry.hv_channel;
    228             str >> entry.Vnom;
    229             str >> entry.Voff;
    230 
    231             if (entry.hv_channel+32*entry.hv_board>=416)
    232             {
    233 #ifdef DEBUG
    234                 cerr << "Invalid board/channel read from " << fname << "." << endl;
    235 #endif
    236                 return false;
    237             }
    238 
    239             (*this)[l++] = entry;
    240         }
    241 
    242         return l==416;
    243     }
     206    void Retrieve(const std::string &database);
     207    void Read(const std::string &fname);
    244208
    245209    const BiasMapEntry &hv(int board, int channel) const
Note: See TracChangeset for help on using the changeset viewer.