LArSoft  v06_85_00
Liquid Argon Software toolkit - http://larsoft.org/
CFAlgoTimeOverlap.cxx
Go to the documentation of this file.
1 #ifndef RECOTOOL_CFALGOTIMEOVERLAP_CXX
2 #define RECOTOOL_CFALGOTIMEOVERLAP_CXX
3 
4 #include "CFAlgoTimeOverlap.h"
5 
6 namespace cmtool {
7 
8  //-------------------------------------------------------
10  //-------------------------------------------------------
11  {
12  SetRatioCut(0.001) ; //(0.095) ;
13  SetStartTimeCut(10) ;
14  SetDebug(false) ;
15  SetVerbose(false) ;
16  RequireThreePlanes(true) ;
17  }
18 
19  //-----------------------------
21  //-----------------------------
22  {
23 
24  }
25 
26  //----------------------------------------------------------------------------------------------
27  float CFAlgoTimeOverlap::Float(const std::vector<const cluster::ClusterParamsAlg*> &clusters)
28  //----------------------------------------------------------------------------------------------
29  {
30 
31  // Code-block by Kazu starts
32  // This ensures the algorithm works only if # clusters is > 2 (and not =2)
33  // You may take out this block if you want to allow matching using clusters from only 2 planes.
34  if(_require_3planes && clusters.size()==2) return -1;
35  // Code-block by Kazu ends
36 
37  double ratio = 1;
38  double time_difference = 0;
39  double max_time_difference = 0;
40  double max_charge = 0;
41  double charge_ratio = 1;
42 
43  //Preserve location in time space. Cut clusters that have similar time differences,
44  // but hit wires at very different times.
45  double start_t = 0;
46  double end_t = 0;
47  double prev_start_t = 0;
48  double prev_end_t = 0;
49 
50  double max_hits_1 = 0;
51  double max_hits_2 = 0;
52 
53  for(auto const& c : clusters){
54 
55  auto charge = c->GetParams().sum_charge ;
56 
57  time_difference = c->GetParams().start_point.t - c->GetParams().end_point.t ;
58 
59  if(time_difference < 0)
60  time_difference *= -1;
61 
62  if(max_time_difference < time_difference)
63  max_time_difference = time_difference ;
64 
65  if(max_charge < charge)
66  max_charge = charge ;
67 
68  if(c->GetParams().N_Hits > max_hits_1) {
69  max_hits_2 = max_hits_1;
70  max_hits_1 = c->GetParams().N_Hits;
71  }else if(c->GetParams().N_Hits > max_hits_2)
72  max_hits_2 = c->GetParams().N_Hits;
73 
74  }
75 
76  ratio = 1;
77  charge_ratio = 1;
78  for(size_t c_index=0; c_index<clusters.size(); ++c_index) {
79  auto const& c = clusters[c_index];
80 
81  double length = c->GetParams().length ;
82  //auto charge = c->GetParams().sum_charge ;
83  //Order hits from most to least
84  //SetMaxMiddleMin(hits_0,hits_1,hits_2,max_hits,middle_hits,min_hits);
85 
86  //Make start_t always smaller
87  if(c->GetParams().start_point.t > c->GetParams().end_point.t){
88  start_t = c->GetParams().end_point.t ;
89  end_t = c->GetParams().start_point.t ;
90  }
91  else{
92  start_t = c->GetParams().start_point.t ;
93  end_t = c->GetParams().end_point.t ;
94  }
95 
96  if(prev_start_t ==0)
97  prev_start_t = start_t ;
98  if(prev_end_t ==0)
99  prev_end_t = end_t ;
100 
101  time_difference = end_t - start_t ;
102 
103  ratio *= time_difference/max_time_difference ;
104 
105  charge_ratio = max_hits_2 / max_hits_1;// charge/max_charge ;
106 
107  if(c_index==(clusters.size()-1))
108  ratio *= charge_ratio ;
109 
110  //If current cluster's start time is not within some range of the previous cluster's start time,
111  //modify ratio to disallow matching
112 
113  if( (start_t > (prev_start_t - _start_time_cut) && start_t < (prev_start_t + _start_time_cut))
114  || (end_t > (prev_end_t - _start_time_cut) && end_t < (prev_end_t + _start_time_cut) )
115  || (length >25 && start_t >(prev_start_t - 2*_start_time_cut) && start_t < (prev_start_t + 2*_start_time_cut) ) )
116  ratio *= 1;
117  else
118  ratio*= 0.001;
119 
120  prev_start_t = start_t ;
121  prev_end_t = end_t ;
122 
123  if( _debug && c_index==(clusters.size()-1) && ratio > _time_ratio_cut ){
124  std::cout<<"\nPLANE: "<<c->Plane() ;
125  std::cout<<"\nStart point: "<<start_t<<std::endl;
126  std::cout<<"End Point: "<<end_t <<std::endl;
127  // std::cout<<"Previous start time: "<<prev_start_t<<std::endl;
128  std::cout<<"Time diff: "<<time_difference<<std::endl;
129  std::cout<<"Max time diff: "<<max_time_difference<<std::endl;
130  std::cout<<"Ratio for each cluster: "<<ratio<<std::endl;
131  // std::cout<<"Charge: "<<charge<<std::endl;
132  std::cout<<"Charge Ratio: "<<charge_ratio<<std::endl;
133  //std::cout<<"Hits are: "<<min_hits<<", "<<middle_hits<<", "<<max_hits<<std::endl;
134  // std::cout<<"Adjusted Charge Ratio: "<<adjusted_charge_ratio<<std::endl;
135  std::cout<<"Length and Width: "<<c->GetParams().length<<", "<<c->GetParams().width<<std::endl;
136  }
137 
138  }
139 
140  if(_verbose && ratio> _time_ratio_cut)
141  std::cout<<"**************************FOUND A MATCH . ratio is: "<<ratio<<"\n\n\n"<<std::endl;
142 
143  return (ratio > _time_ratio_cut ? ratio : -1 );
144 
145  }
146 
147  //------------------------------
148  /*
149  void CFAlgoTimeOverlap::SetMaxMiddleMin(const double first,
150  const double second,
151  const double third,
152  double &max,
153  double &middle,
154  double &min)
155  //------------------------------
156  {
157 
158  if(first > second && first > third){
159  max = first;
160  }
161  else if (first > second && first < third){
162  max = third ;
163  middle = first ;
164  min = second ;
165  }
166  else if (first > third && first < second){
167  max = second ;
168  middle = first ;
169  min = third ;
170  }
171  else if(first <second && first <third)
172  min = first ;
173 
174 
175  if (max == first && second > third){
176  middle = second ;
177  min = third ;
178  }
179  else if (max ==first && third > second){
180  middle = third ;
181  min = second ;
182  }
183 
184  if(min ==first && second > third){
185  middle = third ;
186  max = second;
187  }
188  else if(min ==first && third > second){
189  middle = second ;
190  max = third ;
191  }
192 
193 
194  //Very rarely, the angles(or hits) may be equal
195  if( first == second && first > third ){
196  max = first;
197  middle = second ;
198  min = third ;
199  }
200  else if( first == second && first < third){
201  max = third;
202  middle = first ;
203  min = second ;
204  }
205 
206  else if( first ==third && first > second){
207  max = first;
208  middle = third;
209  min = second;
210  }
211 
212  else if( first == third && first < second){
213  max = second ;
214  middle = first;
215  min = third ;
216  }
217 
218  else if( second ==third && second < first){
219  max = first;
220  middle = third;
221  min = second;
222  }
223 
224  else if( second == third && second > first){
225  max = second;
226  middle = third;
227  min = first ;
228  }
229  }
230  */
231 
232 
233 
234 
235 
236  //------------------------------
238  //------------------------------
239  {
240  }
241 
242 }
243 #endif
void SetStartTimeCut(float start_time)
virtual void Reset()
Function to reset the algorithm instance called within CMergeManager/CMatchManager&#39;s Reset() ...
void SetVerbose(bool verbose)
Setter function for verbosity.
virtual float Float(const std::vector< const cluster::ClusterParamsAlg * > &clusters)
void RequireThreePlanes(bool doit)
void SetRatioCut(float ratio)
Class def header for a class CFAlgoTimeOverlap.
CFAlgoTimeOverlap()
Default constructor.