TrackReconstruction/source/TrackFinder.cpp

134 lines
3.0 KiB
C++

//
// TrackFinder.cpp
// TrackReconstruction
//
// Created by Baranyai David on 2018. 06. 07..
//
#include "TrackFinder.hpp"
#include <iostream>
std::ostream& operator << (std::ostream& os, const Hit& h)
{
os <<
"Channel ID: " << h.ch_id << std::endl <<
"Chamber ID: " << h.which_chamber << std::endl <<
"Direction: " << h.direction << std::endl;
return os;
}
std::istream& operator >> (std::istream& is, Hit& h)
{
is >> h.ch_id >> h.fine_ts >> h.coarse_ts >> h.ts >> h.width;
int ch_id_helper = 0;
h.which_chamber = h.ch_id / 64; //Determine which chamber's data is it
ch_id_helper = h.ch_id % 64; //Store the channel id from 0 - 32 in it's own direction
/*
the numbering of the channels starts from X 0 - 32 and Y 32-64
*/
if(ch_id_helper < 32)
{
h.direction = "x";
h.ch_id = ch_id_helper;
}
else
{
h.direction = "y";
h.ch_id = ch_id_helper - 32;
}
return is;
}
TrackFinder::TrackFinder()
{
try
{
file.open("data.txt");
if(!file.is_open())
{
throw "File opening error";
}
SortFile();
}
//Check if any errors happen when opening the file
catch(char param)
{
std::cout << param << std::endl;
}
}
void TrackFinder::SortFile()
{
/*
Read the file line by line and store it in the memory.
At the end of the file, sort the data by TS ascending.
*/
std::cout << "Sorting data..." << std::endl;
while (file >> h)
{
hits.push_back(h);
}
std::sort(hits.begin(), hits.end(), [](const Hit& h1, const Hit& h2) {return h1.ts < h2.ts;});
std::cout << "Sorting finished!" << std::endl;
}
TrackFinder::~TrackFinder()
{
file.close();
}
bool TrackFinder::FindTrack()
{
std::cout << "Finding track..." << std::endl;
track.erase(track.begin(), track.end());
/*
If a new track found,
then we don't want to start from the beginning
so the iterator helps us storing the last value.
Set the Interval to the right value for better track reconstruction.
*/
while(iterator < hits.size() - 1)
{
if(hits[iterator+1].ts - hits[iterator].ts < interval)
{
track.push_back(hits[iterator]);
}
else
{
track.push_back(hits[iterator]);
iterator++;
if(track.size() > 4)
{
istrackavailable = true;
std::cout << "Track found!" << std::endl;
return true;
}
else
{
track.erase(track.begin(), track.end());
}
}
iterator++;
}
std::cout << "End reached, no more tracks found!" << std::endl;
return false;
}
void TrackFinder::GetTrack(std::vector<Hit> &h)
{
/*
Copy the track's data into the new vector if available
*/
if(istrackavailable)
{
h = track;
std::cout << "Getting track..." << std::endl;
istrackavailable = false;
}
}