LArSoft  v06_85_00
Liquid Argon Software toolkit - http://larsoft.org/
CMatchManager.cxx
Go to the documentation of this file.
1 #ifndef RECOTOOL_CMATCHMANAGER_CXX
2 #define RECOTOOL_CMATCHMANAGER_CXX
3 
4 #include "CMatchManager.h"
5 
6 namespace cmtool {
7 
9  {
10  throw CMTException("Default ctor needs # planes as an argument!");
11  }
12 
14  {
15  _match_algo = nullptr;
16  _nplanes = nplanes;
17  Reset();
18  }
19 
21  {
26  }
27 
29  {
30  if(_debug_mode <= kPerMerging) {
33  }
34 
37  }
38 
40  {
43  }
44 
46  {
49  }
50 
52  {
55  }
56 
57  unsigned int CMFactorial(unsigned int n)
58  {return (n == 1 || n == 0) ? 1 : CMFactorial(n - 1) * n;}
59 
60  std::vector<std::vector<size_t> > SimpleCombination(size_t n, size_t r) {
61 
62  if(!n || !r) exit(1);
63  if(r > n) exit(1);
64 
65  std::vector<bool> v(n,false);
66  std::fill(v.begin()+n-r,v.end(),true);
67  std::vector<std::vector<size_t> > res;
68  res.reserve(CMFactorial(n)/CMFactorial(n-r)/CMFactorial(r));
69 
70  do {
71  std::vector<size_t> tmp;
72  tmp.reserve(r);
73 
74  for(size_t i=0; i<n; ++i) { if(v[i]) tmp.push_back(i); }
75  res.push_back(tmp);
76  } while (std::next_permutation(v.begin(),v.end()));
77 
78  return res;
79  }
80 
81  std::vector<std::vector<size_t> > ClusterCombinations(const std::vector<size_t>& seed)
82  {
83 
84  std::vector<size_t> ctr(seed.size(),0);
85 
86  std::vector<std::vector<size_t> > res;
87 
88  while(1) {
89 
90  res.push_back(std::vector<size_t>(seed.size(),0));
91  for(size_t index=0; index<ctr.size(); ++index)
92 
93  (*res.rbegin())[index] = ctr.at(index);
94 
95  for(size_t i=0; i<ctr.size(); ++i) {
96 
97  size_t index = (size_t)(ctr.size()-i-1);
98 
99  ctr.at(index) +=1;
100 
101  if(ctr.at(index) < seed.at(index))
102 
103  break;
104 
105  ctr.at(index) = 0;
106 
107  }
108 
109  bool abort = true;
110  for(auto const& value : ctr)
111 
112  abort = abort && (!(value));
113 
114  if(abort) break;
115  }
116  return res;
117  }
118 
119  std::vector<std::vector<std::pair<size_t,size_t> > > PlaneClusterCombinations(const std::vector<size_t>& seed)
120  {
121  // Result container
122  std::vector<std::vector<std::pair<size_t,size_t> > > result;
123 
124  // Loop over N-planes: start from max number of planes => down to 2 planes
125  for(size_t i=0; i<seed.size(); ++i) {
126 
127  // If finish making clusters down to 2 palnes, break
128  if(seed.size() < 2+i) break;
129 
130  // Compute possible N-plane combinations
131  auto const& plane_comb_v = SimpleCombination(seed.size(),seed.size()-i);
132 
133  // Loop over possible N-plane combinations
134  for(auto const& plane_comb : plane_comb_v){
135 
136  // Make a seed for cluster combinations
137  std::vector<size_t> cluster_seed_v;
138  cluster_seed_v.reserve(plane_comb.size());
139  for(auto const& index : plane_comb) cluster_seed_v.push_back(seed[index]);
140 
141  // Compute cluster combinations
142  for(auto const& cluster_comb : ClusterCombinations(cluster_seed_v)) {
143 
144  // Store result
145  result.push_back(std::vector<std::pair<size_t,size_t> >());
146  for(size_t i=0; i<cluster_comb.size(); ++i)
147 
148  (*result.rbegin()).push_back(std::make_pair(plane_comb.at(i),cluster_comb.at(i)));
149  }
150  }
151  }
152  return result;
153 
154  }
155 
157  {
158 
159  TStopwatch localWatch;
160 
161  //
162  // Create plane-by-plane vectors
163  //
165 
166  if(_planes.size()<2) return false;
167 
168  if(_planes.size() > _nplanes)
169 
170  throw CMTException("Found more plane IDs than specified number of planes!");
171 
172  // Index array of clusters per plane
173  std::vector<std::vector<size_t> > cluster_array;
174 
175  // Resize to # of planes w/ clusters
176  cluster_array.reserve(_planes.size());
177 
178  // plane-to-index map
179  std::vector<size_t> plane_to_index(_nplanes,_nplanes);
180 
181  // Fill plane-to-index map
182  for(size_t plane=0; plane<_nplanes; ++plane){
183 
184  if( _planes.find(plane) != _planes.end() ) {
185 
186  plane_to_index[plane] = cluster_array.size();
187 
188  cluster_array.push_back(std::vector<size_t>());
189  }
190  }
191 
192  // Fill cluster_array
193  for(auto riter = _priority.rbegin();
194  riter != _priority.rend();
195  ++riter)
196 
197  cluster_array.at( plane_to_index.at(_in_clusters.at((*riter).second).Plane()) ).push_back((*riter).second);
198 
199  // Find combinations
200  std::vector<size_t> seed;
201  seed.reserve(cluster_array.size());
202  for(auto const& clusters_per_plane : cluster_array)
203 
204  seed.push_back(clusters_per_plane.size());
205 
206  auto const& combinations = PlaneClusterCombinations(seed);
207 
208  // Loop over combinations and call algorithm
209  for(auto const& comb : combinations) {
210 
211  std::vector<const cluster::ClusterParamsAlg*> ptr_v;
212 
213  std::vector<unsigned int> tmp_index_v;
214 
215  tmp_index_v.reserve(comb.size());
216 
217  ptr_v.reserve(comb.size());
218 
219  for(auto const& plane_cluster : comb) {
220 
221  auto const& in_cluster_index = cluster_array.at(plane_cluster.first).at(plane_cluster.second);
222 
223  tmp_index_v.push_back(in_cluster_index);
224 
225  ptr_v.push_back(&(_in_clusters.at(in_cluster_index)));
226 
227  }
228 
229  if(_debug_mode <= kPerMerging){
230 
231  std::cout
232  << " \033[93m"
233  << "Inspecting a pair (";
234  for(auto const& index : tmp_index_v)
235  std::cout << index << " ";
236  std::cout<<") \033[00m" << std::flush;
237 
238  localWatch.Start();
239 
240  }
241 
242  auto const& score = _match_algo->Float(ptr_v);
243 
244  if(_debug_mode <= kPerMerging)
245 
246  std::cout << " ... Time taken = " << localWatch.RealTime() << " [s]" << std::endl;
247 
248  if(score>0)
249 
250  _book_keeper.Match(tmp_index_v,score);
251 
252  }
253 
254  if(_debug_mode <= kPerIteration) {
257  }
258 
259  return false;
260 
261  }
262 
263 }
264 
265 #endif
unsigned int CMFactorial(unsigned int n)
virtual void EventEnd()
FMWK function called @ end of Process()
Somewhat verbose (cout per merging iteration)
Definition: CMManagerBase.h:38
virtual void Reset()
Method to reset itself.
virtual void Report()
Definition: CMAlgoBase.h:73
::cmtool::CFloatAlgoBase * _match_algo
Merging algorithm.
Definition: CMatchManager.h:80
std::multimap< float, size_t > _priority
Priority record.
Float_t tmp
Definition: plot.C:37
CMatchManager()
Default constructor is private because we need an argument to configure w/ # planes in the detector...
std::vector< std::vector< size_t > > ClusterCombinations(const std::vector< size_t > &seed)
CMatchBookKeeper _book_keeper
Book keeper instance.
Definition: CMatchManager.h:77
void ComputePriority(const std::vector< cluster::ClusterParamsAlg > &clusters)
Function to compute priority.
virtual void EventEnd()
Definition: CMAlgoBase.h:51
virtual bool IterationProcess()
FMWK function called @ iterative loop inside Process()
auto vector(Vector const &v)
Returns a manipulator which will print the specified array.
Definition: DumpUtils.h:265
virtual void EventBegin()
FMWK function called @ beginning of Process()
void Match(const std::vector< unsigned int > &matched_indexes, const float &score)
Method to register matched clusters.
virtual void Reset()
Function to reset the algorithm instance called within CMergeManager/CMatchManager&#39;s Reset() ...
Definition: CMAlgoBase.h:40
long seed
Definition: chem4.cc:68
std::vector< std::vector< std::pair< size_t, size_t > > > PlaneClusterCombinations(const std::vector< size_t > &seed)
void fill(const art::PtrVector< recob::Hit > &hits, int only_plane)
virtual float Float(const std::vector< const cluster::ClusterParamsAlg * > &clusters)
CMMSGLevel_t _debug_mode
Debug mode switch.
virtual void IterationBegin(const std::vector< cluster::ClusterParamsAlg > &clusters)
Definition: CMAlgoBase.h:59
std::string value(boost::any const &)
Class def header for a class CMatchManager.
std::vector< cluster::ClusterParamsAlg > _in_clusters
Input clusters.
virtual void IterationEnd()
FMWK function called @ end of iterative loop inside Process()
::cmtool::CPriorityAlgoBase * _priority_algo
Priority algorithm.
std::set< UChar_t > _planes
A holder for # of unique planes in the clusters, computed in ComputePriority() function.
void Reset()
Reset method.
virtual void SetVerbose(bool doit=true)
Setter function for verbosity.
Definition: CMAlgoBase.h:80
Char_t n[5]
Extremely verbose (cout per individual algorithm execution)
Definition: CMManagerBase.h:36
virtual void IterationBegin()
FMWK function called @ beginning of iterative loop inside Process()
std::vector< std::vector< size_t > > SimpleCombination(size_t n, size_t r)
size_t _nplanes
Number of planes.
Definition: CMatchManager.h:83
virtual void IterationEnd()
Definition: CMAlgoBase.h:65
void Reset()
Method to reset itself.