12 #include "TRestTrackReconnectionProcess.h"
18 TRestTrackReconnectionProcess::TRestTrackReconnectionProcess() { Initialize(); }
20 TRestTrackReconnectionProcess::TRestTrackReconnectionProcess(
const char* configFilename) {
23 if (LoadConfigFromFile(configFilename) == -1) LoadDefaultConfig();
27 TRestTrackReconnectionProcess::~TRestTrackReconnectionProcess() {
delete fOutputTrackEvent; }
29 void TRestTrackReconnectionProcess::LoadDefaultConfig() {
30 SetName(
"trackReconnectionProcess");
31 SetTitle(
"Default config");
35 SetSectionName(this->ClassName());
36 SetLibraryVersion(LIBRARY_VERSION);
38 fInputTrackEvent =
nullptr;
44 void TRestTrackReconnectionProcess::LoadConfig(
const string& configFilename,
const string& name) {
45 if (LoadConfigFromFile(configFilename, name) == -1) LoadDefaultConfig();
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);
126 BreakTracks(&initialHits, subHitSets, fNSigmas);
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);
148 SetObservableValue(
"branches", trackBranches);
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.push_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.push_back(subHits);
202 cout <<
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" << endl;
205 void 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) {
314 Int_t tck1 = tracks[0][0];
315 Int_t tck2 = tracks[1][0];
318 if (tracks[0][1] == 0 && tracks[1][1] == 0) {
320 for (
int n = nHits[tck1] - 1; n >= 0; n--) newHits.AddHit(hitSets[tck1], n);
322 for (
int n = 0; n < nHits[tck2]; n++) newHits.AddHit(hitSets[tck2], n);
323 }
else if (tracks[0][1] == 1 && tracks[1][1] == 0) {
326 for (
int n = 0; n < nHits[tck1]; n++) newHits.AddHit(hitSets[tck1], n);
328 for (
int n = 0; n < nHits[tck2]; n++) newHits.AddHit(hitSets[tck2], n);
329 }
else if (tracks[0][1] == 0 && tracks[1][1] == 1) {
332 for (
int n = 0; n < nHits[tck2]; n++) newHits.AddHit(hitSets[tck2], n);
334 for (
int n = 0; n < nHits[tck1]; n++) newHits.AddHit(hitSets[tck1], n);
338 for (
int n = 0; n < nHits[tck1]; n++) newHits.AddHit(hitSets[tck1], n);
340 for (
int n = nHits[tck2] - 1; n >= 0; n--) newHits.AddHit(hitSets[tck2], n);
343 hitSets.erase(hitSets.begin() + tck2);
344 hitSets.erase(hitSets.begin() + tck1);
345 hitSets.push_back(newHits);
348 nSubTracks = hitSets.size();
351 cout <<
"New subtracks : " << nSubTracks << endl;
353 cout <<
"AFTER REMOVAL" << endl;
354 cout <<
"--------" << endl;
355 for (
int i = 0; i < nSubTracks; i++) {
356 cout <<
"Subset : " << i << endl;
357 cout <<
" Sub hits : " << nHits[i] << endl;
359 hitSets[i].PrintHits();
361 cout <<
"--------" << endl;
365 if (nSubTracks > 1) ReconnectTracks(hitSets);
368 cout <<
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" << endl;
371 Int_t TRestTrackReconnectionProcess::GetTrackBranches(
TRestHits& h, Double_t nSigma) {
373 Int_t nHits = h.GetNumberOfHits();
375 for (
int n = 1; n < nHits; n++)
376 if (h.GetDistance(n - 1, n) > fMeanDistance + nSigma * fSigma) breaks++;
383 if (GetParameter(
"splitTrack",
"false") ==
"true")
391 void TRestTrackReconnectionProcess::SetDistanceMeanAndSigma(
TRestHits* h) {
392 Int_t nHits = h->GetNumberOfHits();
395 for (
int n = 1; n < nHits; n++) fMeanDistance += h->GetDistance(n - 1, n);
396 fMeanDistance /= nHits;
398 fSigma = TMath::Sqrt(fMeanDistance);
401 cout <<
"-----> Node distance average ; " << fMeanDistance << endl;
402 cout <<
"-----> Node distance sigma : " << fSigma << endl;
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 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.