LArSoft  v06_85_00
Liquid Argon Software toolkit - http://larsoft.org/
PmaElement3D.cxx
Go to the documentation of this file.
1 
15 
17 
18 // Impact factors on the objective function: U V Z
19 float pma::Element3D::fOptFactors[3] = { 0.2F, 0.8F, 1.0F };
20 
22  fTPC(-1), fCryo(-1),
23  fFrozen(false),
24  fHitsRadius(0)
25 {
27  for (unsigned int i = 0; i < 3; i++)
28  {
29  fNHits[i] = 0;
30  fNThisHits[i] = 0;
31  fSumHitsQ[i] = 0.0;
32  }
33 }
34 
35 size_t pma::Element3D::NEnabledHits(unsigned int view) const
36 {
37  size_t n = 0;
38  for (size_t i = 0; i < fAssignedHits.size(); i++)
39  if (fAssignedHits[i]->IsEnabled() &&
40  ((view == geo::kUnknown) || (view == fAssignedHits[i]->View2D()))) n++;
41  return n;
42 }
43 
45 {
46  std::sort(fAssignedHits.begin(), fAssignedHits.end(), pma::bTrajectory3DOrderLess());
47 }
48 
50 {
51  fAssignedPoints.clear();
52  fAssignedHits.clear();
53  fHitsRadius = 0.0;
54 }
55 
57 {
58  std::vector< pma::Hit3D* > hitsColl, hitsInd1, hitsInd2;
59  for (size_t i = 0; i < 3; ++i) fNThisHitsEnabledAll = 0;
60  for (auto h : fAssignedHits)
61  {
62  if (h->IsEnabled()) fNThisHitsEnabledAll++;
63  switch (h->View2D())
64  {
65  case geo::kZ: hitsColl.push_back(h); break;
66  case geo::kV: hitsInd2.push_back(h); break;
67  case geo::kU: hitsInd1.push_back(h); break;
68  }
69  }
70  fNThisHits[0] = hitsInd1.size();
71  fNThisHits[1] = hitsInd2.size();
72  fNThisHits[2] = hitsColl.size();
73 
74  pma::SortedObjectBase const * chain = dynamic_cast< pma::SortedObjectBase* >(this);
75  pma::Element3D* el = 0;
76  for (size_t b = 0; b < chain->NextCount(); b++)
77  {
78  el = dynamic_cast< pma::Element3D* >(chain->Next(b));
79  if (el)
80  for (auto h : el->fAssignedHits)
81  {
82  switch (h->View2D())
83  {
84  case geo::kZ: hitsColl.push_back(h); break;
85  case geo::kV: hitsInd2.push_back(h); break;
86  case geo::kU: hitsInd1.push_back(h); break;
87  }
88  }
89  }
90  el = dynamic_cast< pma::Element3D* >(chain->Prev());
91  if (el)
92  {
93  for (auto h : el->fAssignedHits)
94  {
95  switch (h->View2D())
96  {
97  case geo::kZ: hitsColl.push_back(h); break;
98  case geo::kV: hitsInd2.push_back(h); break;
99  case geo::kU: hitsInd1.push_back(h); break;
100  }
101  }
102  }
103 
104  fHitsRadius = GetHitsRadius2D(hitsColl);
105  double r = GetHitsRadius2D(hitsInd2);
106  if (r > fHitsRadius) fHitsRadius = r;
107  r = GetHitsRadius2D(hitsInd1);
108  if (r > fHitsRadius) fHitsRadius = r;
109 
110  float amp, sigmaMax = 0.0F;
111  fSumHitsQ[0] = 0.0; fNHits[0] = hitsInd1.size();
112  for (size_t i = 0; i < hitsInd1.size(); i++)
113  {
114  amp = hitsInd1[i]->GetAmplitude();
115  if (amp > sigmaMax) sigmaMax = amp;
116  fSumHitsQ[0] += amp;
117  }
118  for (size_t i = 0; i < hitsInd1.size(); i++)
119  {
120  if (sigmaMax > 0.0F)
121  {
122  amp = hitsInd1[i]->GetAmplitude();
123  if (amp > 0.0F)
124  hitsInd1[i]->SetSigmaFactor((float)sqrt(amp / sigmaMax));
125  else hitsInd1[i]->SetSigmaFactor(0.01F);
126  }
127  else hitsInd1[i]->SetSigmaFactor(1.0F);
128  }
129 
130  sigmaMax = 0.0F;
131  fSumHitsQ[1] = 0.0; fNHits[1] = hitsInd2.size();
132  for (size_t i = 0; i < hitsInd2.size(); i++)
133  {
134  amp = hitsInd2[i]->GetAmplitude();
135  if (amp > sigmaMax) sigmaMax = amp;
136  fSumHitsQ[1] += amp;
137  }
138  for (size_t i = 0; i < hitsInd2.size(); i++)
139  {
140  if (sigmaMax > 0.0F)
141  {
142  amp = hitsInd2[i]->GetAmplitude();
143  if (amp > 0.0F)
144  hitsInd2[i]->SetSigmaFactor((float)sqrt(amp / sigmaMax));
145  else hitsInd2[i]->SetSigmaFactor(0.01F);
146  }
147  else hitsInd2[i]->SetSigmaFactor(1.0F);
148  }
149 
150  sigmaMax = 0.0F;
151  fSumHitsQ[2] = 0.0; fNHits[2] = hitsColl.size();
152  for (size_t i = 0; i < hitsColl.size(); i++)
153  {
154  amp = hitsColl[i]->SummedADC();
155  if (amp > sigmaMax) sigmaMax = amp;
156  fSumHitsQ[2] += amp;
157  }
158  for (size_t i = 0; i < hitsColl.size(); i++)
159  {
160  if (sigmaMax > 0.0F)
161  {
162  amp = hitsColl[i]->SummedADC();
163  if (amp > 0.0F)
164  hitsColl[i]->SetSigmaFactor((float)sqrt(amp / sigmaMax));
165  else hitsColl[i]->SetSigmaFactor(0.01F);
166  }
167  else hitsColl[i]->SetSigmaFactor(1.0F);
168  }
169 }
170 
171 double pma::Element3D::SumDist2(void) const
172 {
173  if (fTPC < 0)
174  {
175  if (!fAssignedHits.empty()) mf::LogWarning("pma::Element3D") << "Hits assigned to TPC-crossing element.";
176  return 0.0F;
177  }
178 
179  double hit_sum = SumDist2Hits();
180 
181  if (fAssignedPoints.size())
182  {
183  double d, ref_sum = 0.0F;
184  for (auto p : fAssignedPoints)
185  {
186  d = sqrt( GetDistance2To(*p) ) - 0.5; // guide by ref points up to ~ 3D resolution
187  if (d > 0.0) ref_sum += d * d;
188  }
189  if (fAssignedHits.size())
190  {
191  ref_sum *= 0.2 * fAssignedHits.size() / fAssignedPoints.size();
192  }
193  hit_sum += ref_sum;
194  }
195 
196  return hit_sum;
197 }
198 
199 double pma::Element3D::SumDist2(unsigned int view) const
200 {
201  if (fTPC < 0)
202  {
203  if (!fAssignedHits.empty()) mf::LogWarning("pma::Element3D") << "Hits assigned to TPC-crossing element.";
204  return 0.0F;
205  }
206 
207  double hit_sum = 0.0F;
208  for (auto h : fAssignedHits)
209  {
210  if (h->IsEnabled())
211  {
212  unsigned int hitView = h->View2D();
213  if ((view == geo::kUnknown) || (view == hitView))
214  {
215  hit_sum += OptFactor(hitView) * // alpha_i
216  h->GetSigmaFactor() * // hit_amp / hit_max_amp
217  GetDistance2To(h->Point2D(), hitView); // hit_to_fit_dist^2
218  }
219  }
220  }
221  return hit_sum;
222 }
223 
224 double pma::Element3D::HitsRadius3D(unsigned int view) const
225 {
226  if (fTPC < 0)
227  {
228  if (!fAssignedHits.empty()) mf::LogWarning("pma::Element3D") << "Hits assigned to TPC-crossing element.";
229  return 0.0F;
230  }
231 
232  TVector3 mean3D(0, 0, 0);
233  size_t nHits = 0;
234  for (auto h : fAssignedHits)
235  if (h->View2D() == view)
236  { mean3D += h->Point3D(); nHits++; }
237  if (!nHits) return 0.0;
238  mean3D *= (1.0 / nHits);
239 
240  double r2, maxR2 = 0.0;
241  for (auto h : fAssignedHits)
242  if (h->View2D() == view)
243  {
244  r2 = pma::Dist2(h->Point3D(), mean3D);
245  if (r2 > maxR2) maxR2 = r2;
246  }
247  return sqrt(maxR2);
248 }
249 
250 bool pma::Element3D::SelectRndHits(size_t nmax_per_view)
251 {
252  if (!nmax_per_view) { return SelectAllHits(); }
253 
254  size_t nhits[3];
255  for (size_t i = 0; i < 3; ++i) nhits[i] = NHits(i);
256 
257  int m[3], count[3];
258  bool state[3];
259  for (size_t i = 0; i < 3; ++i)
260  {
261  if (nhits[i] >= 2 * nmax_per_view)
262  {
263  m[i] = nhits[i] / nmax_per_view;
264  state[i] = true;
265  }
266  else if (nhits[i] > nmax_per_view)
267  {
268  m[i] = nhits[i] / (nhits[i] - nmax_per_view);
269  state[i] = false;
270  }
271  else { m[i] = 0; state[i] = false; }
272 
273  count[i] = 0;
274  }
275 
276  bool b, changed = false;
277  for (auto h : fAssignedHits)
278  {
279  b = h->IsEnabled();
280 
281  size_t view = h->View2D();
282  if (m[view])
283  {
284  if (count[view] % m[view] == 0) h->SetEnabled(state[view]);
285  else h->SetEnabled(!(state[view]));
286 
287  ++count[view];
288  }
289  else h->SetEnabled(true);
290 
291  changed |= (b != h->IsEnabled());
292  }
293  return changed;
294 }
295 
297 {
298  bool changed = false;
299  for (auto h : fAssignedHits)
300  {
301  changed |= !(h->IsEnabled());
302  h->SetEnabled(true);
303  }
304  return changed;
305 }
306 
double fSumHitsQ[3]
Definition: PmaElement3D.h:118
double GetHitsRadius2D(const std::vector< pma::Hit3D * > &hits, bool exact=false)
Definition: Utilities.cxx:89
double Dist2(const TVector2 &v1, const TVector2 &v2)
Definition: Utilities.cxx:19
Planes which measure V.
Definition: geo_types.h:77
Unknown view.
Definition: geo_types.h:83
Planes which measure Z direction.
Definition: geo_types.h:79
size_t fNThisHits[3]
Definition: PmaElement3D.h:115
double HitsRadius3D(unsigned int view) const
virtual void ClearAssigned(pma::Track3D *trk=0)
Planes which measure U.
Definition: geo_types.h:76
Implementation of the Projection Matching Algorithm.
virtual unsigned int NextCount(void) const
Definition: SortedObjects.h:46
size_t fNHits[3]
Definition: PmaElement3D.h:117
std::vector< TVector3 * > fAssignedPoints
Definition: PmaElement3D.h:114
Float_t d
Definition: plot.C:237
std::vector< pma::Hit3D * > fAssignedHits
Definition: PmaElement3D.h:113
static float fOptFactors[3]
Definition: PmaElement3D.h:121
double SumDist2(void) const
Implementation of the Projection Matching Algorithm.
void SortHits(void)
bool SelectRndHits(size_t nmax_per_view)
size_t fNThisHitsEnabledAll
Definition: PmaElement3D.h:116
double fHitsRadius
Definition: PmaElement3D.h:119
virtual double GetDistance2To(const TVector3 &p3d) const =0
Distance [cm] from the 3D point to the object 3D.
Char_t n[5]
void UpdateHitParams(void)
bool SelectAllHits(void)
virtual pma::SortedObjectBase * Prev(void) const
Definition: SortedObjects.h:44
virtual double SumDist2Hits(void) const =0
size_t NHits(void) const
Definition: PmaElement3D.h:71
virtual pma::SortedObjectBase * Next(unsigned int index=0) const
Definition: SortedObjects.h:45
size_t NEnabledHits(unsigned int view=geo::kUnknown) const
Implementation of the Projection Matching Algorithm.
static float OptFactor(unsigned int view)
Definition: PmaElement3D.h:103