diff --git a/DB_Track.cpp b/DB_Track.cpp index 628a90f..d3d0191 100644 --- a/DB_Track.cpp +++ b/DB_Track.cpp @@ -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; - 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(); - std::vector ch_id; - std::vector sec; - std::vector nsec; - std::vector amp; - - std::vector x_v; - std::vector y_v; - - int iterator = 0; - int null = 0; - - canvas -> cd(0); TrackFinder finder; @@ -40,59 +28,17 @@ int main(int argc, char* argv[]) { if(std::cin.get() == '\n') { - twoDhisto -> Reset(); - x_v.clear(); - y_v.clear(); - - if(finder.FindTrack()) + if(finder.FindAndDrawNextTrack(twodhisto, canvas)) { - finder.GetTrack(ch_id, sec, nsec, amp); - for(int i = 0; i < ch_id.size(); i++) - { - if(ch_id[i] < 32) - { - 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]); - } - } - } + gSystem -> ProcessEvents(); + } + else + { + std::cout << "Reached the end of the database" << std::endl; } - iterator++; - twoDhisto -> Draw("colz"); - canvas -> Update(); - gSystem -> ProcessEvents(); } } - std::cout << iterator << std::endl; - App.Run(); return 0; } diff --git a/include/SQLHandler.hpp b/include/SQLHandler.hpp new file mode 100644 index 0000000..b9cd896 --- /dev/null +++ b/include/SQLHandler.hpp @@ -0,0 +1,41 @@ +// +// SQLHandler.hpp +// DB_Track +// +// Created by Baranyai David on 2018. 10. 13.. +// + +#ifndef SQLHandler_hpp +#define SQLHandler_hpp + +#include +#include +#include +#include +#include +#include + +struct Hit +{ + unsigned int channel_id; + unsigned int elapsed_time; + unsigned int amplitude_peak; +}; + +class SQLHandler +{ +public: + SQLHandler(); + ~SQLHandler(); + + std::vector QueryNext(); + void SetRange(unsigned int); +private: + unsigned int range; + uint64_t latest_value; + MYSQL *server; + + unsigned int min_hits; +}; + +#endif /* SQLHandler_hpp */ diff --git a/include/TrackFinder.hpp b/include/TrackFinder.hpp index 3a56333..ea5a979 100644 --- a/include/TrackFinder.hpp +++ b/include/TrackFinder.hpp @@ -13,33 +13,21 @@ #include #include #include +#include "SQLHandler.hpp" +#include "TH2D.h" +#include "TCanvas.h" class TrackFinder { public: TrackFinder(); ~TrackFinder(); - bool FindTrack(); - void GetTrack(std::vector&, std::vector&, std::vector&, std::vector&); + bool FindAndDrawNextTrack(TH2D*, TCanvas*); private: - std::ifstream file; - std::string line; - int interval = 900000; //nanosec + std::vector track_data[4]; //4 chambers - int ch_id_1 = 0; - int sec_1 = 0; - int nsec_1 = 0; - int amp_1 = 0; - - int line_data[4]; - - std::vector ch_id; - std::vector amp; - std::vector sec; - std::vector nsec; - - bool istrackavailable = false; + SQLHandler sqlHandler; }; #endif /* TrackFinder_hpp */ diff --git a/source/SQLHandler.cpp b/source/SQLHandler.cpp new file mode 100644 index 0000000..951ebfd --- /dev/null +++ b/source/SQLHandler.cpp @@ -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 < 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_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 <(); + } +} + +void SQLHandler::SetRange(unsigned int n_range) +{ + range = n_range; +} diff --git a/source/TrackFinder.cpp b/source/TrackFinder.cpp index badb8a0..f644515 100644 --- a/source/TrackFinder.cpp +++ b/source/TrackFinder.cpp @@ -10,101 +10,55 @@ 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() { - file.close(); + } -bool TrackFinder::FindTrack() +bool TrackFinder::FindAndDrawNextTrack(TH2D* histo, TCanvas *canvas) { - 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()); - while(!file.eof()) + std::vector track; + track.clear(); + + for(int i = 0; i < 4; i++) { - getline(file, line); - std::istringstream is(line); + track_data[i].clear(); + } + + 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++) { - is >> line_data[i]; - } - long old = ((line_data[1]*1000000)+line_data[2]); - 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; + for(int j = 0; j < track_data[i].size(); j++) + { + histo -> Fill(track_data[i][j].channel_id, i, track_data[i][j].amplitude_peak); + } } + histo -> Draw("colz"); + canvas -> Update(); + + return true; } - return false; } -void TrackFinder::GetTrack(std::vector &ch, std::vector &s, std::vector &ns, std::vector &a) -{ - if(istrackavailable) - { - ch = ch_id; - s = sec; - ns = nsec; - a = amp; - } -}