11#include "TRestTrackPathMinimizationProcess.h"
17TRestTrackPathMinimizationProcess::TRestTrackPathMinimizationProcess() {
Initialize(); }
19TRestTrackPathMinimizationProcess::~TRestTrackPathMinimizationProcess() {
delete fOutputTrackEvent; }
25 fInputTrackEvent =
nullptr;
35 cout <<
"TRestTrackPathMinimizationProcess. Number of tracks : "
36 << fInputTrackEvent->GetNumberOfTracks() << endl;
39 for (
int tck = 0; tck < fInputTrackEvent->GetNumberOfTracks(); tck++)
40 fOutputTrackEvent->AddTrack(fInputTrackEvent->GetTrack(tck));
42 for (
int tck = 0; tck < fInputTrackEvent->GetNumberOfTracks(); tck++) {
43 if (!fInputTrackEvent->isTopLevel(tck))
continue;
44 Int_t tckId = fInputTrackEvent->GetTrack(tck)->GetTrackID();
46 TRestVolumeHits* hits = fInputTrackEvent->GetTrack(tck)->GetVolumeHits();
47 const int nHits = hits->GetNumberOfHits();
51 Int_t pId = fInputTrackEvent->GetTrack(tck)->GetParentID();
52 cout <<
"Track : " << tck <<
" TrackID : " << tckId <<
" ParentID : " << pId << endl;
53 cout <<
"-----------------" << endl;
55 cout <<
"-----------------" << endl;
59 RESTDebug <<
"hits : " << nHits <<
RESTendl;
61 std::vector<int> bestPath(nHits);
62 for (
int i = 0; i < nHits; i++) bestPath[i] = i;
64 if (fMinMethod ==
"bruteforce")
66 else if (fMinMethod ==
"closestN")
72 for (
const auto& v : bestPath) bestHitsOrder.AddHit(*hits, v);
76 bestTrack.SetTrackID(fOutputTrackEvent->GetNumberOfTracks() + 1);
77 bestTrack.SetParentID(tckId);
78 bestTrack.SetVolumeHits(bestHitsOrder);
79 fOutputTrackEvent->AddTrack(&bestTrack);
82 fOutputTrackEvent->SetLevels();
83 return fOutputTrackEvent;
92 const int nHits = hits->GetNumberOfHits();
93 vector<vector<double>> dist(nHits, vector<double>(nHits));
94 RESTDebug <<
"Nhits " << nHits <<
RESTendl;
96 if (nHits < 3)
return;
98 for (
int i = 0; i < nHits; i++) {
99 for (
int j = i; j < nHits; j++) {
103 dist[i][j] = hits->GetDistance(i, j);
104 dist[j][i] = dist[i][j];
109 double min_path = 1E9;
110 std::vector<int> current_path(nHits);
113 for (
int s = 0; s < nHits; s++) {
114 std::vector<int> vertex(nHits - 1);
117 for (
int i = 0; i < nHits; i++) {
118 if (i == s)
continue;
124 double current_pathweight = 0;
129 while (!vertex.empty()) {
131 int index = 0, bestIndex = 0;
132 double minDist = 1E9;
133 for (
const auto& v : vertex) {
134 const double d = dist[k][v];
137 current_pathweight += d;
144 current_path[nHits - vertex.size()] = k;
145 vertex.erase(vertex.begin() + bestIndex);
147 if (fCyclic) current_pathweight += dist[k][s];
149 if (current_pathweight < min_path) {
150 min_path = current_pathweight;
151 bestPath = current_path;
157 for (
const auto& v : bestPath) cout << v <<
" ";
158 cout <<
" " << min_path << endl;
168 const int nHits = hits->GetNumberOfHits();
169 vector<vector<double>> dist(nHits, vector<double>(nHits));
170 RESTDebug <<
"Nhits " << nHits <<
RESTendl;
172 if (nHits < 3)
return;
174 for (
int i = 0; i < nHits; i++) {
175 for (
int j = i; j < nHits; j++) {
179 dist[i][j] = hits->GetDistance(i, j);
180 dist[j][i] = dist[i][j];
186 for (
int i = 0; i < nHits; i++) {
187 for (
int j = 0; j < nHits; j++) {
188 cout << dist[i][j] <<
" ";
194 double min_path = 1E9;
197 for (
int s = 0; s < nHits; s++) {
199 std::vector<int> vertex(nHits - 1);
201 for (
int i = 0; i < nHits; i++) {
202 if (i == s)
continue;
211 double current_pathweight = 0;
215 for (
const auto& v : vertex) {
216 current_pathweight += dist[k][v];
220 if (fCyclic) current_pathweight += dist[k][s];
223 if (current_pathweight < min_path) {
224 min_path = current_pathweight;
226 for (
size_t i = 0; i < vertex.size(); i++) bestPath[i + 1] = vertex[i];
230 }
while (std::next_permutation(vertex.begin(), vertex.end()));
235 for (
const auto& v : bestPath) cout << v <<
" ";
236 cout <<
" " << min_path << endl;
246 const int nHits = hits->GetNumberOfHits();
248 if (nHits < 4)
return;
250 Int_t segment_count = nHits * (nHits - 1) / 2;
251 int* elen = (
int*)malloc(segment_count *
sizeof(
int));
263 vector<int> bestP(nHits);
265 for (
int i = 0; i < nHits; i++) {
267 for (
int j = 0; j < i; j++) {
268 TVector3 x0, x1, pos0, pos1;
273 Double_t distance = hits->GetDistance(i, j);
299 elen[k] = (int)(100. * distance);
329 for (
int n = 0; n < segment_count; n++) cout <<
"n : " << n <<
" elen : " << elen[n] << endl;
333 rval = TrackMinimization_segment(nHits, elen, &bestP[0]);
364 if (
GetVerboseLevel() >= TRestStringOutput::REST_Verbose_Level::REST_Warning) {
365 cout <<
"REST WARNING. TRestTrackPathMinimizationProcess. HELDKARP FAILED." << endl;
367 fOutputTrackEvent->SetOK(
false);
371 for (
int i = 0; i < nHits; i++) bestPath[i] = bestP[i];
A base class for any REST event.
TVector3 GetPosition(int n) const
It returns the position of hit number n.
@ REST_Extreme
show everything
@ REST_Debug
+show the defined debug messages
void NearestNeighbour(TRestVolumeHits *hits, std::vector< int > &bestPath)
Return the index with the shortest path solving Travelling Salesman Problem (TSP) using nearest neigh...
void HeldKarp(TRestVolumeHits *hits, std::vector< int > &bestPath)
This function eturn the index with the shortest path Note that this method calls external tsp library...
void EndProcess() override
To be executed at the end of the run (outside event loop)
void InitProcess() override
To be executed at the beginning of the run (outside event loop)
void Initialize() override
Making default settings.
TRestEvent * ProcessEvent(TRestEvent *inputEvent) override
Process one event.
void BruteForce(TRestVolumeHits *hits, std::vector< int > &bestPath)
This function return the index with the shortest path solving Travelling Salesman Problem (TSP) using...
Int_t GetChar(std::string hint="Press a KEY to continue ...")
Helps to pause the program, printing a message before pausing.