ATTPCROOT  0.3.0-alpha
A ROOT-based framework for analyzing data from active target detectors
AtTrackFinderTC.cxx
Go to the documentation of this file.
1 #include "AtTrackFinderTC.h"
2 
3 #include "AtEvent.h" // for AtEvent
4 #include "AtHit.h" // for AtHit
5 #include "AtPatternEvent.h" // for AtPatternEvent
6 #include "AtTrack.h" // for AtTrack
7 #include "AtTrackTransformer.h" // for AtTrackTransformer
8 
9 #include <Math/Point3D.h> // for PositionVector3D
10 
11 #include "dnn.h"
12 #include "graph.h"
13 #include "option.h"
14 #include "pointcloud.h"
15 #include "triplet.h" // for triplet, generate_triplets
16 
17 #include <algorithm>
18 #include <cmath> // for sqrt
19 #include <iostream> // for cout, cerr
20 #include <memory> // for allocator_traits<>::value_...
21 #include <utility> // for move
22 
23 constexpr auto cRED = "\033[1;31m";
24 constexpr auto cYELLOW = "\033[1;33m";
25 constexpr auto cNORMAL = "\033[0m";
26 constexpr auto cGREEN = "\033[1;32m";
27 
29 
30 std::unique_ptr<AtPatternEvent> AtPATTERN::AtTrackFinderTC::FindTracks(AtEvent &event)
31 {
32  Opt opt_params;
33  int opt_verbose = opt_params.get_verbosity();
34 
35  opt_params.set_parameters(inputParams.s, inputParams.k, inputParams.n, inputParams.m, inputParams.r, inputParams.a,
36  inputParams.t);
37 
38  PointCloud cloud_xyz;
39  eventToClusters(event, cloud_xyz);
40 
41  if (cloud_xyz.size() == 0) {
42  std::cerr << "[Error] empty cloud " << std::endl;
43 
44  return nullptr;
45  }
46 
47  if (opt_params.needs_dnn()) {
48  double dnn = std::sqrt(first_quartile(cloud_xyz));
49  if (opt_verbose > 0) {
50  std::cout << "AtPATTERN::AtTrackFinderTC - [Info] computed dnn: " << dnn << std::endl;
51  }
52  opt_params.set_dnn(dnn);
53  if (dnn == 0.0) {
54  std::cerr << "AtPATTERN::AtTrackFinderTC - [Error] dnn computed as zero. "
55  << "Suggestion: remove doublets, e.g. with 'sort -u'" << std::endl;
56  return nullptr;
57  }
58  }
59 
60  // Step 1) smoothing by position averaging of neighboring points
61  PointCloud cloud_xyz_smooth;
62  smoothen_cloud(cloud_xyz, cloud_xyz_smooth, opt_params.get_r());
63 
64  // Step 2) finding triplets of approximately collinear points
65  std::vector<triplet> triplets;
66  generate_triplets(cloud_xyz_smooth, triplets, opt_params.get_k(), opt_params.get_n(), opt_params.get_a());
67 
68  // Step 3) single link hierarchical clustering of the triplets
69  cluster_group cl_group;
70  compute_hc(cloud_xyz_smooth, cl_group, triplets, opt_params.get_s(), opt_params.get_t(), opt_params.is_tauto(),
71  opt_params.get_dmax(), opt_params.is_dmax(), opt_params.get_linkage(), opt_verbose);
72 
73  // Step 4) pruning by removal of small clusters ...
74  cleanup_cluster_group(cl_group, opt_params.get_m(), opt_verbose);
75  cluster_triplets_to_points(triplets, cl_group);
76  // .. and (optionally) by splitting up clusters at gaps > dmax
77  if (opt_params.is_dmax()) {
78  cluster_group cleaned_up_cluster_group;
79  for (auto &cl : cl_group) {
80  max_step(cleaned_up_cluster_group, cl, cloud_xyz, opt_params.get_dmax(), opt_params.get_m() + 2);
81  }
82  cl_group = cleaned_up_cluster_group;
83  }
84 
85  // store cluster labels in points
86  add_clusters(cloud_xyz, cl_group, opt_params.is_gnuplot());
87 
88  // Adapt clusters to AtTrack
89  return clustersToTrack(cloud_xyz, cl_group, event);
90 }
91 
92 void AtPATTERN::AtTrackFinderTC::eventToClusters(AtEvent &event, PointCloud &cloud)
93 {
94  Int_t nHits = event.GetNumHits();
95 
96  for (Int_t iHit = 0; iHit < nHits; iHit++) {
97  Point point;
98  const AtHit hit = event.GetHit(iHit);
99  auto position = hit.GetPosition();
100  point.x = position.X();
101  point.y = position.Y();
102  point.z = position.Z();
103  point.SetID(iHit);
104  cloud.push_back(point);
105  }
106 }
107 
108 std::unique_ptr<AtPatternEvent>
109 AtPATTERN::AtTrackFinderTC::clustersToTrack(PointCloud &cloud, const std::vector<cluster_t> &clusters, AtEvent &event)
110 {
111 
112  std::vector<AtTrack> tracks;
113  // std::vector<Point> points = cloud;
114  auto points = cloud;
115 
116  for (size_t cluster_index = 0; cluster_index < clusters.size(); ++cluster_index) {
117 
118  AtTrack track; // One track per cluster
119 
120  const std::vector<size_t> &point_indices = clusters[cluster_index];
121  if (point_indices.size() == 0)
122  continue;
123 
124  // add points
125  for (auto ind : point_indices) {
126 
127  const Point &point = cloud[ind];
128 
129  track.AddHit(event.GetHit(point.GetID()));
130 
131  // remove current point from vector points
132  for (auto p = points.begin(); p != points.end(); p++) {
133  if (*p == point) {
134  points.erase(p);
135  break;
136  }
137  }
138 
139  } // Point indices
140 
141  track.SetTrackID(cluster_index);
142 
143  fTrackTransformer->ClusterizeSmooth3D(track, fClusterRadius, fClusterDistance);
144 
145  if (kSetPrunning)
146  PruneTrack(track);
147 
148  tracks.push_back(track);
149 
150  } // Clusters loop
151 
152  std::cout << cRED << " Tracks found " << tracks.size() << cNORMAL << "\n";
153 
154  // Dump noise into pattern event
155  auto retEvent = std::make_unique<AtPatternEvent>();
156  for (const auto &point : points)
157  retEvent->AddNoise(event.GetHit(point.GetID()));
158 
159  for (auto &track : tracks) {
160  if (track.GetHitArray().size() > 0)
161  SetTrackInitialParameters(track);
162  retEvent->AddTrack(std::move(track));
163  }
164 
165  return retEvent;
166 }
AtEvent::GetHit
const AtHit & GetHit(Int_t hitNo) const
Definition: AtEvent.h:114
Opt::is_dmax
bool is_dmax()
Definition: option.cxx:324
Opt::get_t
double get_t()
Definition: option.cxx:320
Point::GetID
int GetID() const
Definition: pointcloud.h:40
AtEvent.h
cGREEN
constexpr auto cGREEN
Definition: AtTrackFinderTC.cxx:26
AtPATTERN
Definition: AtEventDrawTaskS800.h:41
dnn.h
Opt::needs_dnn
bool needs_dnn()
Definition: option.cxx:276
cRED
constexpr auto cRED
Definition: AtTrackFinderTC.cxx:23
Opt::set_parameters
void set_parameters(double _s, double _k, double _n, double _m, double _r, double _a, double _t)
Definition: option.cxx:53
cYELLOW
constexpr auto cYELLOW
Definition: AtTrackFinderTC.cxx:24
Opt::set_dnn
void set_dnn(double dnn)
Definition: option.cxx:245
AtPATTERN::AtPRA
Find patterns in hit clouds.
Definition: AtPRA.h:32
Opt::get_k
size_t get_k()
Definition: option.cxx:300
AtEvent
Definition: AtEvent.h:22
Opt::is_gnuplot
bool is_gnuplot()
Definition: option.cxx:280
AtPATTERN::AtTrackFinderTC::AtTrackFinderTC
AtTrackFinderTC()
Definition: AtTrackFinderTC.cxx:28
AtTrackFinderTC.h
msd::first_quartile
float first_quartile(pcl::PointCloud< pcl::PointXYZ >::Ptr cloud)
Definition: msd.cxx:65
Point::x
float x
Definition: main.cpp:72
AtHit::GetPosition
const XYZPoint & GetPosition() const
Definition: AtHit.h:79
AtHit.h
Opt::get_m
size_t get_m()
Definition: option.cxx:336
Point
Definition: main.cpp:71
AtTrack
Definition: AtTrack.h:25
AtTrack.h
AtPatternEvent.h
pointcloud.h
Opt::get_n
size_t get_n()
Definition: option.cxx:304
triplet.h
AtPATTERN::AtTrackFinderTC::FindTracks
std::unique_ptr< AtPatternEvent > FindTracks(AtEvent &event) override
Definition: AtTrackFinderTC.cxx:30
Opt::is_tauto
bool is_tauto()
Definition: option.cxx:316
Opt::get_verbosity
int get_verbosity()
Definition: option.cxx:292
max_step
void max_step(std::vector< std::vector< size_t >> &new_clusters, const std::vector< size_t > &cluster, const PointCloud &cloud, double dmax, size_t min_size)
Definition: graph.cxx:125
generate_triplets
void generate_triplets(const PointCloud &cloud, std::vector< triplet > &triplets, size_t k, size_t n, double a)
Definition: triplet.cxx:27
PointCloud
Definition: pointcloud.h:60
Opt::get_dmax
double get_dmax()
Definition: option.cxx:328
graph.h
Point::y
float y
Definition: main.cpp:73
hc::compute_hc
cluster_group compute_hc(pcl::PointCloud< pcl::PointXYZI >::ConstPtr cloud, std::vector< triplet > const &triplets, ScaleTripletMetric triplet_metric, float t, int opt_verbose)
Definition: hc.cxx:139
smoothen_cloud
void smoothen_cloud(const PointCloud &cloud, PointCloud &result_cloud, double r)
Definition: pointcloud.cxx:254
AtTrackTransformer.h
cleanup_cluster_group
void cleanup_cluster_group(cluster_group &cl_group, size_t m, int opt_verbose)
Definition: cluster.cxx:165
cluster_triplets_to_points
void cluster_triplets_to_points(const std::vector< triplet > &triplets, cluster_group &cl_group)
Definition: cluster.cxx:185
option.h
Opt::get_r
double get_r()
Definition: option.cxx:296
Opt::get_linkage
Linkage get_linkage()
Definition: option.cxx:332
add_clusters
void add_clusters(PointCloud &cloud, cluster_group &cl_group, bool gnuplot)
Definition: cluster.cxx:212
Point::SetID
void SetID(int pid)
Definition: pointcloud.h:39
cNORMAL
constexpr auto cNORMAL
Definition: AtTrackFinderTC.cxx:25
AtTrack::GetHitArray
HitVector & GetHitArray()
Definition: AtTrack.h:79
Opt::get_a
double get_a()
Definition: option.cxx:308
AtTrack::SetTrackID
void SetTrackID(Int_t val)
Definition: AtTrack.h:96
Opt
Definition: option.h:18
cluster_group
std::vector< cluster_t > cluster_group
Definition: cluster.h:25
AtHit
Point in space with charge.
Definition: AtHit.h:27
Opt::get_s
double get_s()
Definition: option.cxx:312
Point::z
float z
Definition: main.cpp:74
AtTrack::AddHit
void AddHit(const AtHit &hit)
Definition: AtTrack.h:97