Data read from mysql server

This commit is contained in:
David Baranyai 2018-10-16 13:05:13 +02:00
parent 9a2d26fc3b
commit f13cb7cb51
5 changed files with 219 additions and 163 deletions

View File

@ -19,20 +19,8 @@ int main(int argc, char* argv[])
std::cout << "Press enter for the next track." << std::endl << "Press Ctrl + C to exit." << std::endl; std::cout << "Press enter for the next track." << std::endl << "Press Ctrl + C to exit." << std::endl;
TH2D *twoDhisto = new TH2D("2Dhisto","2Dhisto", 100, 0, 32, 100, 0, 32); TH2D *twodhisto = new TH2D("twodhisto", "twodhisto", 100, 0, 32, 100, 0, 4);
TCanvas *canvas = new TCanvas(); TCanvas *canvas = new TCanvas();
std::vector<int> ch_id;
std::vector<int> sec;
std::vector<int> nsec;
std::vector<int> amp;
std::vector<int> x_v;
std::vector<int> y_v;
int iterator = 0;
int null = 0;
canvas -> cd(0);
TrackFinder finder; TrackFinder finder;
@ -40,59 +28,17 @@ int main(int argc, char* argv[])
{ {
if(std::cin.get() == '\n') if(std::cin.get() == '\n')
{ {
twoDhisto -> Reset(); if(finder.FindAndDrawNextTrack(twodhisto, canvas))
x_v.clear();
y_v.clear();
if(finder.FindTrack())
{ {
finder.GetTrack(ch_id, sec, nsec, amp); gSystem -> ProcessEvents();
for(int i = 0; i < ch_id.size(); i++) }
{ else
if(ch_id[i] < 32) {
{ std::cout << "Reached the end of the database" << std::endl;
x_v.push_back(ch_id[i]);
}
else
{
y_v.push_back(ch_id[i] - 32);
}
}
if(x_v.size() == 0)
{
for(int i = 0; i < y_v.size(); i++)
{
twoDhisto -> Fill(null, y_v[i]);
}
}
if(y_v.size() == 0)
{
for(int i = 0; i < x_v.size(); i++)
{
twoDhisto -> Fill(x_v[i], null);
}
}
if(x_v.size() > 0 && y_v.size() > 0)
{
for(int z = 0; z < x_v.size(); z++)
{
for(int j = 0; j < y_v.size(); j++)
{
twoDhisto -> Fill(x_v[z], y_v[j]);
}
}
}
} }
iterator++;
twoDhisto -> Draw("colz");
canvas -> Update();
gSystem -> ProcessEvents();
} }
} }
std::cout << iterator << std::endl;
App.Run(); App.Run();
return 0; return 0;
} }

41
include/SQLHandler.hpp Normal file
View File

@ -0,0 +1,41 @@
//
// SQLHandler.hpp
// DB_Track
//
// Created by Baranyai David on 2018. 10. 13..
//
#ifndef SQLHandler_hpp
#define SQLHandler_hpp
#include <stdio.h>
#include <iostream>
#include <mysql.h>
#include <vector>
#include <sstream>
#include <string>
struct Hit
{
unsigned int channel_id;
unsigned int elapsed_time;
unsigned int amplitude_peak;
};
class SQLHandler
{
public:
SQLHandler();
~SQLHandler();
std::vector<Hit> QueryNext();
void SetRange(unsigned int);
private:
unsigned int range;
uint64_t latest_value;
MYSQL *server;
unsigned int min_hits;
};
#endif /* SQLHandler_hpp */

View File

@ -13,33 +13,21 @@
#include <vector> #include <vector>
#include <string> #include <string>
#include <sstream> #include <sstream>
#include "SQLHandler.hpp"
#include "TH2D.h"
#include "TCanvas.h"
class TrackFinder class TrackFinder
{ {
public: public:
TrackFinder(); TrackFinder();
~TrackFinder(); ~TrackFinder();
bool FindTrack(); bool FindAndDrawNextTrack(TH2D*, TCanvas*);
void GetTrack(std::vector<int>&, std::vector<int>&, std::vector<int>&, std::vector<int>&);
private: private:
std::ifstream file; std::vector<Hit> track_data[4]; //4 chambers
std::string line;
int interval = 900000; //nanosec
int ch_id_1 = 0; SQLHandler sqlHandler;
int sec_1 = 0;
int nsec_1 = 0;
int amp_1 = 0;
int line_data[4];
std::vector<int> ch_id;
std::vector<int> amp;
std::vector<int> sec;
std::vector<int> nsec;
bool istrackavailable = false;
}; };
#endif /* TrackFinder_hpp */ #endif /* TrackFinder_hpp */

127
source/SQLHandler.cpp Normal file
View File

@ -0,0 +1,127 @@
//
// SQLHandler.cpp
// DB_Track
//
// Created by Baranyai David on 2018. 10. 13..
//
#include "SQLHandler.hpp"
SQLHandler::SQLHandler()
{
latest_value = 0;
range = 9000000000;
min_hits = 4; //need at least 4 hits
server = mysql_init(server);
try
{
if (!mysql_real_connect(server, "localhost", "root", "raspberry", "adatok", 0, NULL, 0))
{
throw "Can't connect to the server";
}
else
{
std::cout << "-----------------------------" << std::endl;
std::cout << "Successfully connected" << std::endl;
std::cout << "Host info: " << mysql_get_host_info(server) << std::endl;
std::cout << "Client info: " << mysql_get_client_info() << std::endl;
std::cout << "-----------------------------" << std::endl << std::endl;
std::cout << "Getting the first value..." << std::endl;
std::string query = "SELECT min(time_sum) FROM (SELECT *, (time_sec * (power(10, 7)) + time_ns) time_sum FROM fb55) sum";
if (!mysql_query(server, query.c_str()))
{
MYSQL_RES *result = mysql_store_result(server);
if (mysql_num_fields(result) == 1) //1 oszlop
{
MYSQL_ROW row;
if(mysql_num_rows(result) == 1) //1 érték
{
row = mysql_fetch_row(result);
latest_value = std::stoll(row[0]);
std::cout << "Min value = " << latest_value <<std::endl;
}
else throw "More than one row error";
}
else throw "More than one column error";
}
}
} catch (char const* s)
{
std::cout << s <<std::endl;
}
}
SQLHandler::~SQLHandler()
{
mysql_close(server);
delete server;
}
//MAIN QUERY:
//SELECT * FROM (SELECT ch_id, time_sec, time_ns, (time_sec * (power(10, 7)) + time_ns) time_sum FROM fb55) s WHERE time_sum BETWEEN 2521025900 AND 2570247800 ORDER BY time_sum ASC
std::vector<Hit> SQLHandler::QueryNext()
{
try
{
std::cout << "Getting next between " << latest_value << " and " << latest_value + range << std::endl;
std::stringstream query;
unsigned int nRow = 0;
std::vector<Hit> hit_vector;
while(nRow < min_hits)
{
hit_vector.clear();
double max = latest_value + range;
query << "SELECT * FROM (SELECT ch_id, peak_amp, (time_sec * (power(10, 7)) + time_ns) time_sum FROM fb55) s WHERE time_sum BETWEEN " << latest_value << " AND " << max << " ORDER BY time_sum ASC";
if (!mysql_query(server, query.str().c_str())) //Start query
{
MYSQL_RES *result = mysql_store_result(server); //store result
unsigned int nColumn = mysql_num_fields(result); //store column number
if (nColumn == 3) //3 oszlop, se több, se kevesebb
{
MYSQL_ROW row;
nRow = mysql_num_rows(result);
if(nRow > min_hits) //at least
{
Hit hit_helper;
for(int i = 0; i < nRow; i++)
{
row = mysql_fetch_row(result);
hit_helper.channel_id = atoi(row[0]);
hit_helper.amplitude_peak = atoi(row[1]);
hit_helper.elapsed_time = std::stoll(row[2]);
hit_vector.push_back(hit_helper);
}
latest_value = atof(row[2]);
//std::cout << "Min value = " << latest_value <<std::endl;
return hit_vector;
}
else if(nRow != 0)
{
for(int i = 0; i < nRow; i++) //seek to the end
{
row = mysql_fetch_row(result);
}
latest_value = atoi(row[2]); //store the latest value
}
}
else throw "More than three column error";
}
}
return hit_vector;
}
catch (char const* s)
{
std::cout << s << std::endl;
return std::vector<Hit>();
}
}
void SQLHandler::SetRange(unsigned int n_range)
{
range = n_range;
}

View File

@ -10,101 +10,55 @@
TrackFinder::TrackFinder() TrackFinder::TrackFinder()
{ {
try
{
file.open("data.txt");
if(!file.is_open())
{
throw "File opening error";
}
getline(file, line);
std::istringstream is(line);
for(int i = 0; i < 4; i++)
{
is >> line_data[i];
}
ch_id_1 = line_data[0];
sec_1 = line_data[1];
nsec_1 = line_data[2];
amp_1 = line_data[3];
ch_id.push_back(ch_id_1);
sec.push_back(sec_1);
nsec.push_back(nsec_1);
amp.push_back(amp_1);
}
catch(char param)
{
std::cout << param << std::endl;
}
} }
TrackFinder::~TrackFinder() TrackFinder::~TrackFinder()
{ {
file.close();
} }
bool TrackFinder::FindTrack() bool TrackFinder::FindAndDrawNextTrack(TH2D* histo, TCanvas *canvas)
{ {
ch_id.erase(ch_id.begin(), ch_id.end()); std::vector<Hit> track;
sec.erase(sec.begin(), sec.end()); track.clear();
nsec.erase(nsec.begin(), nsec.end());
amp.erase(amp.begin(), amp.end()); for(int i = 0; i < 4; i++)
while(!file.eof())
{ {
getline(file, line); track_data[i].clear();
std::istringstream is(line); }
track = sqlHandler.QueryNext();
histo -> Reset();
canvas -> cd(0);
if (track.size() == 0)
{
std::cout << "End of database" << std::endl;
return false;
}
else
{
for(int i = 0; i < track.size(); i++)
{
Hit helper;
helper.amplitude_peak = track[i].amplitude_peak;
helper.elapsed_time = track[i].elapsed_time;
helper.channel_id = track[i].channel_id % 16;
track_data[track[i].channel_id / 16].push_back(helper);
}
for(int i = 0; i < 4; i++) for(int i = 0; i < 4; i++)
{ {
is >> line_data[i]; for(int j = 0; j < track_data[i].size(); j++)
} {
long old = ((line_data[1]*1000000)+line_data[2]); histo -> Fill(track_data[i][j].channel_id, i, track_data[i][j].amplitude_peak);
long nw = ((sec_1*1000000)+nsec_1); }
if( old - nw < interval)
{
ch_id_1 = line_data[0];
sec_1 = line_data[1];
nsec_1 = line_data[2];
amp_1 = line_data[3];
ch_id.push_back(ch_id_1);
sec.push_back(sec_1);
nsec.push_back(nsec_1);
amp.push_back(amp_1);
}
else if(ch_id.size() < 4) //ha négynél kevesebb szál szólalt meg
{
ch_id_1 = line_data[0];
sec_1 = line_data[1];
nsec_1 = line_data[2];
amp_1 = line_data[3];
ch_id.erase(ch_id.begin(), ch_id.end());
sec.erase(sec.begin(), sec.end());
nsec.erase(nsec.begin(), nsec.end());
amp.erase(amp.begin(), amp.end());
ch_id.push_back(ch_id_1);
sec.push_back(sec_1);
nsec.push_back(nsec_1);
amp.push_back(amp_1);
}
else if(ch_id.size() >= 4) //ha négynél több
{
istrackavailable = true;
return true;
} }
histo -> Draw("colz");
canvas -> Update();
return true;
} }
return false;
} }
void TrackFinder::GetTrack(std::vector<int> &ch, std::vector<int> &s, std::vector<int> &ns, std::vector<int> &a)
{
if(istrackavailable)
{
ch = ch_id;
s = sec;
ns = nsec;
a = amp;
}
}