12#include "TRestTrackReconnectionProcess.h"
18TRestTrackReconnectionProcess::TRestTrackReconnectionProcess() {
Initialize(); }
20TRestTrackReconnectionProcess::TRestTrackReconnectionProcess(
const char* configFilename) {
27TRestTrackReconnectionProcess::~TRestTrackReconnectionProcess() {
delete fOutputTrackEvent; }
29void TRestTrackReconnectionProcess::LoadDefaultConfig() {
30 SetName(
"trackReconnectionProcess");
31 SetTitle(
"Default config");
38 fInputTrackEvent =
nullptr;
44void TRestTrackReconnectionProcess::LoadConfig(
const string& configFilename,
const string& name) {
53 Int_t trackBranches = 0;
58 for (
int tck = 0; tck < fInputTrackEvent->GetNumberOfTracks(); tck++)
59 fOutputTrackEvent->AddTrack(fInputTrackEvent->GetTrack(tck));
61 for (
int tck = 0; tck < fInputTrackEvent->GetNumberOfTracks(); tck++) {
62 if (!fInputTrackEvent->isTopLevel(tck))
continue;
63 Int_t tckId = fInputTrackEvent->GetTrack(tck)->GetTrackID();
65 TRestVolumeHits* hits = fInputTrackEvent->GetTrack(tck)->GetVolumeHits();
69 cout <<
"TRestTrackReconnectionProcess. Input hits" << endl;
70 Int_t pId = fInputTrackEvent->GetTrack(tck)->GetParentID();
71 cout <<
"Track : " << tck <<
" TrackID : " << tckId <<
" ParentID : " << pId << endl;
72 cout <<
"-----------------" << endl;
76 SetDistanceMeanAndSigma((
TRestHits*)hits);
78 if (fMeanDistance == 0)
continue;
81 vector<TRestVolumeHits> subHitSets;
87 for (
int n = 0; n < 1; n++) {
90 BreakTracks(&initialHits, subHitSets, 1.5 * (n + 1));
91 ReconnectTracks(subHitSets);
94 SetDistanceMeanAndSigma(&resultHits);
95 tBranches = GetTrackBranches(resultHits, fNSigmas);
98 cout <<
"Break and reconnect finished" << endl;
99 cout <<
"Branches : " << tBranches << endl;
103 initialHits = resultHits;
108 if (tBranches > trackBranches) trackBranches = tBranches;
111 for (
unsigned int n = 0; n < subHitSets[0].GetNumberOfHits(); n++) {
112 if (n > 0 && n < subHitSets[0].GetNumberOfHits() - 1) {
115 subHitSets[0].SwapHits(n - 1, n);
117 Double_t distanceAfter = subHitSets[0].GetHitsPathLength(n - 2, n + 2);
119 if (distanceAfter < distance)
continue;
121 subHitSets[0].SwapHits(n - 1, n);
129 cout <<
" **** Splitting track : " << endl;
130 cout <<
"Number of subHitSets : " << subHitSets.size() << endl;
135 for (
unsigned int n = 0; n < subHitSets.size(); n++) {
138 aTrack.SetTrackID(fOutputTrackEvent->GetNumberOfTracks() + 1);
140 aTrack.SetParentID(tckId);
142 aTrack.SetVolumeHits(subHitSets[n]);
144 fOutputTrackEvent->AddTrack(&aTrack);
152 cout <<
"++++++++++++++++++++++++++++++++" << endl;
153 fOutputTrackEvent->PrintEvent();
154 cout <<
"++++++++++++++++++++++++++++++++" << endl;
158 return fOutputTrackEvent;
168 cout <<
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" << endl;
169 cout <<
"BreakTracks. Breaking tracks into hits subsets." << endl;
173 for (
unsigned int n = 0; n < hits->GetNumberOfHits(); n++) {
174 Double_t x = hits->GetX(n);
175 Double_t y = hits->GetY(n);
176 Double_t z = hits->GetZ(n);
177 Double_t sX = hits->GetSigmaX(n);
178 Double_t sY = hits->GetSigmaY(n);
179 Double_t sZ = hits->GetSigmaZ(n);
180 Double_t energy = hits->GetEnergy(n);
183 cout <<
"Distance : " << hits->GetDistance(n - 1, n);
184 if (hits->GetDistance(n - 1, n) > fMeanDistance + nSigma * fSigma) cout <<
" BREAKKKK";
188 if (n > 0 && hits->GetDistance(n - 1, n) > fMeanDistance + nSigma * fSigma) {
189 hitSets.emplace_back(subHits);
193 subHits.AddHit(x, y, z, energy, 0, hits->GetType(n), sX, sY, sZ);
196 cout <<
"H : " << n <<
" X : " << x <<
" Y : " << y <<
" Z : " << z << endl;
199 hitSets.emplace_back(subHits);
202 cout <<
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" << endl;
205void TRestTrackReconnectionProcess::ReconnectTracks(vector<TRestVolumeHits>& hitSets) {
207 cout <<
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" << endl;
208 cout <<
"ReconnectTracks. Connecting back sub tracks " << endl;
211 Int_t nSubTracks = hitSets.size();
213 if (nSubTracks == 1)
return;
215 Double_t minDistance = 1.e10;
217 Int_t tracks[2][2] = {{0, 0}, {0, 0}};
219 vector<Int_t> nHits(nSubTracks);
220 for (
int i = 0; i < nSubTracks; i++) nHits[i] = hitSets[i].GetNumberOfHits();
223 cout <<
"ORIGINAL" << endl;
224 cout <<
"--------" << endl;
225 for (
int i = 0; i < nSubTracks; i++) {
226 cout <<
"Subset : " << i << endl;
227 cout <<
" Sub hits : " << nHits[i] << endl;
229 hitSets[i].PrintHits();
231 cout <<
"--------" << endl;
235 for (
int i = 0; i < nSubTracks; i++) {
236 for (
int j = 0; j < nSubTracks; j++) {
238 Int_t x1_0 = hitSets[i].GetX(0);
239 Int_t x1_1 = hitSets[i].GetX(nHits[i] - 1);
241 Int_t y1_0 = hitSets[i].GetY(0);
242 Int_t y1_1 = hitSets[i].GetY(nHits[i] - 1);
244 Int_t z1_0 = hitSets[i].GetZ(0);
245 Int_t z1_1 = hitSets[i].GetZ(nHits[i] - 1);
247 Int_t x2_0 = hitSets[j].GetX(0);
248 Int_t x2_1 = hitSets[j].GetX(nHits[j] - 1);
250 Int_t y2_0 = hitSets[j].GetY(0);
251 Int_t y2_1 = hitSets[j].GetY(nHits[j] - 1);
253 Int_t z2_0 = hitSets[j].GetZ(0);
254 Int_t z2_1 = hitSets[j].GetZ(nHits[j] - 1);
257 d = TMath::Sqrt((x1_0 - x2_0) * (x1_0 - x2_0) + (y1_0 - y2_0) * (y1_0 - y2_0) +
258 (z1_0 - z2_0) * (z1_0 - z2_0));
260 if (d < minDistance) {
268 d = TMath::Sqrt((x1_0 - x2_1) * (x1_0 - x2_1) + (y1_0 - y2_1) * (y1_0 - y2_1) +
269 (z1_0 - z2_1) * (z1_0 - z2_1));
271 if (d < minDistance) {
279 d = TMath::Sqrt((x1_1 - x2_0) * (x1_1 - x2_0) + (y1_1 - y2_0) * (y1_1 - y2_0) +
280 (z1_1 - z2_0) * (z1_1 - z2_0));
282 if (d < minDistance) {
290 d = TMath::Sqrt((x1_1 - x2_1) * (x1_1 - x2_1) + (y1_1 - y2_1) * (y1_1 - y2_1) +
291 (z1_1 - z2_1) * (z1_1 - z2_1));
293 if (d < minDistance) {
307 Int_t tck1 = tracks[0][0];
308 Int_t tck2 = tracks[1][0];
311 if (tracks[0][1] == 0 && tracks[1][1] == 0) {
313 for (
int n = nHits[tck1] - 1; n >= 0; n--) newHits.AddHit(hitSets[tck1], n);
315 for (
int n = 0; n < nHits[tck2]; n++) newHits.AddHit(hitSets[tck2], n);
316 }
else if (tracks[0][1] == 1 && tracks[1][1] == 0) {
319 for (
int n = 0; n < nHits[tck1]; n++) newHits.AddHit(hitSets[tck1], n);
321 for (
int n = 0; n < nHits[tck2]; n++) newHits.AddHit(hitSets[tck2], n);
322 }
else if (tracks[0][1] == 0 && tracks[1][1] == 1) {
325 for (
int n = 0; n < nHits[tck2]; n++) newHits.AddHit(hitSets[tck2], n);
327 for (
int n = 0; n < nHits[tck1]; n++) newHits.AddHit(hitSets[tck1], n);
331 for (
int n = 0; n < nHits[tck1]; n++) newHits.AddHit(hitSets[tck1], n);
333 for (
int n = nHits[tck2] - 1; n >= 0; n--) newHits.AddHit(hitSets[tck2], n);
336 hitSets.erase(hitSets.begin() + tck2);
337 hitSets.erase(hitSets.begin() + tck1);
338 hitSets.emplace_back(newHits);
340 nSubTracks = hitSets.size();
343 cout <<
"New subtracks : " << nSubTracks << endl;
345 cout <<
"AFTER REMOVAL" << endl;
346 cout <<
"--------" << endl;
347 for (
int i = 0; i < nSubTracks; i++) {
348 cout <<
"Subset : " << i << endl;
349 cout <<
" Sub hits : " << nHits[i] << endl;
351 hitSets[i].PrintHits();
353 cout <<
"--------" << endl;
357 if (nSubTracks > 1) ReconnectTracks(hitSets);
360 cout <<
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" << endl;
363Int_t TRestTrackReconnectionProcess::GetTrackBranches(
TRestHits& h, Double_t nSigma) {
365 Int_t nHits = h.GetNumberOfHits();
367 for (
int n = 1; n < nHits; n++)
368 if (h.GetDistance(n - 1, n) > fMeanDistance + nSigma * fSigma) breaks++;
383void TRestTrackReconnectionProcess::SetDistanceMeanAndSigma(
TRestHits* h) {
384 Int_t nHits = h->GetNumberOfHits();
387 for (
int n = 1; n < nHits; n++) fMeanDistance += h->GetDistance(n - 1, n);
388 fMeanDistance /= nHits;
390 fSigma = TMath::Sqrt(fMeanDistance);
393 cout <<
"-----> Node distance average ; " << fMeanDistance << endl;
394 cout <<
"-----> Node distance sigma : " << fSigma << endl;
void SetObservableValue(const std::string &name, const T &value)
Set observable value for AnalysisTree.
A base class for any REST event.
It saves a 3-coordinate position and an energy for each punctual deposition.
Double_t GetHitsPathLength(Int_t n=0, Int_t m=0) const
It determines the distance required to travel from hit n to hit m adding all the distances of the hit...
@ REST_Extreme
show everything
@ REST_Debug
+show the defined debug messages
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)
TRestEvent * ProcessEvent(TRestEvent *inputEvent) override
Process one event.
void BreakTracks(TRestVolumeHits *hits, std::vector< TRestVolumeHits > &hitSets, Double_t nSigma=2.)
void InitFromConfigFile() override
To make settings from rml file. This method must be implemented in the derived class.
void Initialize() override
Making default settings.
void PrintMetadata() override
Implemented it in the derived metadata class to print out specific metadata information.
void RemoveHits()
It removes all hits inside the class.
Int_t GetChar(std::string hint="Press a KEY to continue ...")
Helps to pause the program, printing a message before pausing.
Double_t StringToDouble(std::string in)
Gets a double from a string.