LArSoft  v06_85_00
Liquid Argon Software toolkit - http://larsoft.org/
CMergeManager.cxx
Go to the documentation of this file.
1 #ifndef RECOTOOL_CMERGEMANAGER_CXX
2 #define RECOTOOL_CMERGEMANAGER_CXX
3 
4 #include "CMergeManager.h"
5 
6 namespace cmtool {
7 
9  {
10  _iter_ctr=0;
11  _merge_algo = nullptr;
12  _separate_algo = nullptr;
13  Reset();
14  }
15 
17  {
19  _tmp_merged_clusters.clear();
20  _tmp_merged_indexes.clear();
21  _out_clusters.clear();
23  _book_keeper_v.clear();
26  _iter_ctr = 0;
27  }
28 
31  {
32  // Initialization per event
33  if(!_merge_algo) throw CMTException("No algorithm to run!");
34 
35  // Merging algorithm
38 
39  // Separation algorithm
40  if(_separate_algo) {
43  }
44 
45  // Priority ordering algorithm
46  if(_priority_algo) {
49  }
50 
51  // Verbosity setting
52  if(_debug_mode <= kPerMerging) {
53  _merge_algo->SetVerbose(true);
56  }
57 
58  // Book keeper reinitialization
60 
61  // Clear temporary variables
62  _iter_ctr = 0;
63  _tmp_merged_clusters.clear();
64  _tmp_merged_indexes.clear();
65  _book_keeper_v.clear();
66 
67  }
68 
71  {
72 
73  if(!_iter_ctr) {
74 
78 
79  }else{
80 
84 
85  }
86 
87  if(_debug_mode <= kPerIteration) {
88 
89  size_t nclusters = _tmp_merged_clusters.size();
90 
91  if(!_iter_ctr) nclusters = _in_clusters.size();
92 
93  std::cout
94  << std::endl
95  << Form(" Merging iteration %zu: processing %zu clusters...",_iter_ctr,nclusters)
96  << std::endl;
97 
98  }
99 
100  }
101 
104  {
105 
109 
110  if(_debug_mode <= kPerIteration) {
111 
112  _merge_algo->Report();
115 
116  std::cout
117  << Form(" Input / Output cluster length: %zu/%zu",_tmp_merged_clusters.size(),_out_clusters.size())
118  << std::endl;
119 
120  if(_tmp_merged_clusters.size() == _out_clusters.size())
121 
122  std::cout << " Did not find any newly merged clusters..." <<std::endl;
123 
124  if(_out_clusters.size()==1)
125 
126  std::cout << " Output cluster length is 1 (= no more merging needed)" << std::endl;
127 
129 
130  std::cout << " Non-iterative option: terminating..." << std::endl;
131 
132  }
133 
134  _iter_ctr++;
135  }
136 
139  {
140  // Gather the full book keeping result
141  for(auto const& bk : _book_keeper_v)
142 
143  _book_keeper.Combine(bk);
144 
145  // Call EventEnd for algorithms
149 
150  _book_keeper_v.clear();
151  _tmp_merged_clusters.clear();
152  _tmp_merged_indexes.clear();
153  }
154 
156  {
157 
158  // Configure input for RunMerge
159  CMergeBookKeeper bk;
160 
163  _out_clusters.clear();
164 
165  bk.Reset(_tmp_merged_clusters.size());
166 
167  std::vector<bool> merge_switch(_tmp_merged_clusters.size(),true);
168 
169  for(size_t i=0; i<_tmp_merged_indexes.size(); ++i)
170 
171  if(_tmp_merged_indexes.at(i).size()==1)
172 
173  merge_switch.at(i) = false;
174 
176 
177  // Run separation algorithm
178  if(_separate_algo)
179 
181 
182  // Run merging algorithm
183  RunMerge(_tmp_merged_clusters, merge_switch, bk);
185 
186  _merge_algo->Report();
187 
188  // Save output
190 
191  if(bk.size() == _tmp_merged_indexes.size())
193  else {
194  _out_clusters.reserve(_tmp_merged_indexes.size());
195  for(auto const& indexes_v : _tmp_merged_indexes) {
196 
197  if(indexes_v.size()==1) {
198  _out_clusters.push_back(_tmp_merged_clusters.at(indexes_v.at(0)));
199  continue;
200  }
201 
202  size_t tmp_hit_counts=0;
203  for(auto const& index : indexes_v)
204  tmp_hit_counts += _tmp_merged_clusters.at(index).GetHitVector().size();
205  //std::vector<larutil::PxHit> tmp_hits;
206  std::vector<util::PxHit> tmp_hits;
207  tmp_hits.reserve(tmp_hit_counts);
208 
209  for(auto const& index : indexes_v) {
210  for(auto const& hit : _tmp_merged_clusters.at(index).GetHitVector())
211  tmp_hits.push_back(hit);
212  }
214  (*_out_clusters.rbegin()).SetVerbose(false);
215  (*_out_clusters.rbegin()).DisableFANN();
216 
217  if((*_out_clusters.rbegin()).SetHits(tmp_hits) < 1) continue;
218  (*_out_clusters.rbegin()).FillParams(true,true,true,true,true,false);
219  (*_out_clusters.rbegin()).FillPolygon();
220  }
221  _book_keeper_v.push_back(bk);
222  }
223 
224  // Break if no more merging occurred
225  if(_tmp_merged_clusters.size() == _out_clusters.size())
226 
227  return false;
228 
229  if(_out_clusters.size() == _planes.size())
230 
231  return false;
232 
233  return true;
234  }
235 
236  void CMergeManager::RunMerge(const std::vector<cluster::ClusterParamsAlg> &in_clusters,
237  CMergeBookKeeper &book_keeper) const
238  {
239  RunMerge(in_clusters,
240  std::vector<bool>(in_clusters.size(),true),
241  book_keeper);
242  }
243 
244  void CMergeManager::RunMerge(const std::vector<cluster::ClusterParamsAlg> &in_clusters,
245  const std::vector<bool> &merge_flag,
246  CMergeBookKeeper &book_keeper) const
247  {
248  if(merge_flag.size() != in_clusters.size())
249  throw CMTException(Form("in_clusters (%zu) and merge_flag (%zu) vectors must be of same length!",
250  in_clusters.size(),
251  merge_flag.size()
252  )
253  );
254  if(_debug_mode <= kPerIteration){
255 
256  std::cout
257  << Form(" Calling %s with %zu clusters...",__FUNCTION__,in_clusters.size())
258  <<std::endl;
259 
260  }
261 
262  //
263  // Merging
264  //
265 
266  // Run over clusters and execute merging algorithms
267  for(auto citer1 = _priority.rbegin();
268  citer1 != _priority.rend();
269  ++citer1) {
270 
271  auto citer2 = citer1;
272 
273  UChar_t plane1 = in_clusters.at((*citer1).second).Plane();
274 
275  while(1) {
276  citer2++;
277  if(citer2 == _priority.rend()) break;
278 
279  // Skip if not on the same plane
280  UChar_t plane2 = in_clusters.at((*citer2).second).Plane();
281  if(plane1 != plane2) continue;
282 
283  // Skip if this combination is not meant to be compared
284  if(!(merge_flag.at((*citer2).second)) && !(merge_flag.at((*citer1).second)) ) continue;
285 
286  // Skip if this combination is not allowed to merge
287  if(!(book_keeper.MergeAllowed((*citer1).second,(*citer2).second))) continue;
288 
289  if(_debug_mode <= kPerMerging){
290 
291  std::cout
292  << Form(" \033[93mInspecting a pair (%zu, %zu) for merging... \033[00m",(*citer1).second, (*citer2).second)
293  << std::endl;
294  }
295 
296  bool merge = _merge_algo->Bool(in_clusters.at((*citer1).second),in_clusters.at((*citer2).second));
297 
298  if(_debug_mode <= kPerMerging) {
299 
300  if(merge)
301  std::cout << " \033[93mfound to be merged!\033[00m "
302  << std::endl
303  << std::endl;
304 
305  else
306  std::cout << " \033[93mfound NOT to be merged...\033[00m"
307  << std::endl
308  << std::endl;
309 
310  } // end looping over all sets of algorithms
311 
312  if(merge)
313 
314  book_keeper.Merge((*citer1).second,(*citer2).second);
315 
316  } // end looping over all cluster pairs for citer1
317 
318  } // end looping over clusters
319 
320  if(_debug_mode <= kPerIteration && book_keeper.GetResult().size() != in_clusters.size()) {
321 
322  std::cout << " Found following clusters to be merged..." << std::endl;
323  for(auto const &indexes_v : book_keeper.GetResult()) {
324 
325  if(indexes_v.size()==1) continue;
326  std::cout<<" ";
327  for(auto index : indexes_v)
328 
329  std::cout<<index<<" ";
330  std::cout<<" ... indexes to be merged!"<<std::endl;
331 
332  }
333  }
334 
335  }
336 
337  void CMergeManager::RunSeparate(const std::vector<cluster::ClusterParamsAlg> &in_clusters,
338  CMergeBookKeeper &book_keeper) const
339  {
340  /*
341  if(separate_flag.size() != in_clusters.size())
342  throw CMTException(Form("in_clusters (%zu) and separate_flag (%zu) vectors must be of same length!",
343  in_clusters.size(),
344  separate_flag.size()
345  )
346  );
347  */
348  if(_debug_mode <= kPerIteration){
349 
350  std::cout
351  << Form(" Calling %s with %zu clusters...",__FUNCTION__,in_clusters.size())
352  <<std::endl;
353 
354  }
355 
356  //
357  // Separation
358  //
359 
360  // Run over clusters and execute merging algorithms
361  for(size_t cindex1 = 0; cindex1 < in_clusters.size(); ++cindex1) {
362 
363  UChar_t plane1 = in_clusters.at(cindex1).Plane();
364 
365  for(size_t cindex2 = cindex1+1; cindex2 < in_clusters.size(); ++cindex2) {
366 
367  // Skip if not on the same plane
368  UChar_t plane2 = in_clusters.at(cindex2).Plane();
369  if(plane1 != plane2) continue;
370 
371  // Skip if this combination is not meant to be compared
372  //if(!(separate_flag.at(cindex2))) continue;
373 
374  if(_debug_mode <= kPerMerging){
375 
376  std::cout
377  << Form(" \033[93mInspecting a pair (%zu, %zu) for separation... \033[00m",cindex1,cindex2)
378  << std::endl;
379  }
380 
381  bool separate = _separate_algo->Bool(in_clusters.at(cindex1),in_clusters.at(cindex2));
382 
383  if(_debug_mode <= kPerMerging) {
384 
385  if(separate)
386  std::cout << " \033[93mfound to be separated!\033[00m "
387  << std::endl
388  << std::endl;
389 
390  else
391  std::cout << " \033[93mfound NOT to be separated...\033[00m"
392  << std::endl
393  << std::endl;
394 
395  } // end looping over all sets of algorithms
396 
397  if(separate)
398 
399  book_keeper.ProhibitMerge(cindex1,cindex2);
400 
401  } // end looping over all cluster pairs for citer1
402 
403  } // end looping over clusters
404 
405  }
406 
407 }
408 
409 #endif
virtual bool IterationProcess()
FMWK function called @ iterative loop inside Process()
virtual void EventEnd()
FMWK function called @ end of Process()
Somewhat verbose (cout per merging iteration)
Definition: CMManagerBase.h:38
bool _merge_till_converge
Iteration loop switch.
std::vector< CMergeBookKeeper > _book_keeper_v
TFile * _fout
Output analysis plot TFile.
virtual void Report()
Definition: CMAlgoBase.h:73
std::vector< std::vector< unsigned short > > _tmp_merged_indexes
::cmtool::CBoolAlgoBase * _merge_algo
Merging algorithm.
Definition: CMergeManager.h:97
virtual void EventBegin()
FMWK function called @ beginning of Process()
void ProhibitMerge(unsigned short index1, unsigned short index2)
Method to set a pair of clusters to prohibit from merging.
std::vector< cluster::ClusterParamsAlg > _out_clusters
Output clusters.
Definition: CMergeManager.h:91
std::vector< std::vector< unsigned short > > GetResult() const
std::multimap< float, size_t > _priority
Priority record.
CMergeManager()
Default constructor.
std::vector< cluster::ClusterParamsAlg > _tmp_merged_clusters
virtual void IterationEnd()
FMWK function called @ end of iterative loop inside Process()
void ComputePriority(const std::vector< cluster::ClusterParamsAlg > &clusters)
Function to compute priority.
virtual void EventEnd()
Definition: CMAlgoBase.h:51
virtual void IterationBegin()
FMWK function called @ beginning of iterative loop inside Process()
void RunMerge(const std::vector< cluster::ClusterParamsAlg > &in_clusters, CMergeBookKeeper &book_keeper) const
void RunSeparate(const std::vector< cluster::ClusterParamsAlg > &in_clusters, CMergeBookKeeper &book_keeper) const
virtual bool Bool(const ::cluster::ClusterParamsAlg &cluster1, const ::cluster::ClusterParamsAlg &cluster2)
Definition: CBoolAlgoBase.h:41
virtual void Reset()
Function to reset the algorithm instance called within CMergeManager/CMatchManager&#39;s Reset() ...
Definition: CMAlgoBase.h:40
::cmtool::CBoolAlgoBase * _separate_algo
Separation algorithm.
CMergeBookKeeper _book_keeper
Book keeper instance.
Definition: CMergeManager.h:94
virtual void Reset()
Method to reset itself.
CMMSGLevel_t _debug_mode
Debug mode switch.
void SetAnaFile(TFile *fout)
Setter function for an output plot TFile pointer.
Definition: CMAlgoBase.h:77
void Combine(const CMergeBookKeeper &another)
Detector simulation of raw signals on wires.
virtual void IterationBegin(const std::vector< cluster::ClusterParamsAlg > &clusters)
Definition: CMAlgoBase.h:59
virtual void EventBegin(const std::vector< cluster::ClusterParamsAlg > &clusters)
Definition: CMAlgoBase.h:45
std::vector< cluster::ClusterParamsAlg > _in_clusters
Input clusters.
::cmtool::CPriorityAlgoBase * _priority_algo
Priority algorithm.
void Merge(unsigned short index1, unsigned short index2)
Method to merge 2 clusters via index numbers.
std::set< UChar_t > _planes
A holder for # of unique planes in the clusters, computed in ComputePriority() function.
bool MergeAllowed(unsigned short index1, unsigned short index2)
Method to inqury if a combination is prohibited to merge.
virtual void SetVerbose(bool doit=true)
Setter function for verbosity.
Definition: CMAlgoBase.h:80
Extremely verbose (cout per individual algorithm execution)
Definition: CMManagerBase.h:36
void PassResult(std::vector< std::vector< unsigned short > > &result) const
Class def header for a class CMergeManager.
virtual void IterationEnd()
Definition: CMAlgoBase.h:65
void Reset(unsigned short nclusters=0)
Reset method.
void Reset()
Method to reset itself.