CFAlgo3DAngle.cxx
Go to the documentation of this file.
1 #include "CFAlgo3DAngle.h"
2 
4 
5 namespace cmtool {
6 
7  //-------------------------------------------------------
9  //-------------------------------------------------------
10  {
11 
12  SetDebug(false) ;
13  SetThetaCut(30) ;
14  SetPhiCut(40) ;
15  SetRatio(0.0005) ;
16 
17  }
18 
19  //-----------------------------
21  //-----------------------------
22  {
23 
24  }
25 
26  //----------------------------------------------------------------------------------------------
27  float CFAlgo3DAngle::Float(const std::vector<const cluster::ClusterParamsAlg*> &clusters)
28  //----------------------------------------------------------------------------------------------
29  {
30  // Code-block by Kazu starts
31  // This ensures the algorithm works only if # clusters is > 2 (and not =2)
32  // You may take out this block if you want to allow matching using clusters from only 2 planes.
33  if(clusters.size()==2 || clusters.size()==1) return -1;
34  // Code-block by Kazu ends
35 
36 
37  int plane_0 = clusters.at(0)->Plane();
38  int plane_1 = clusters.at(1)->Plane();
39  int plane_2 = clusters.at(2)->Plane();
40  double angle_2d_0 = clusters.at(0)->GetParams().angle_2d;
41  double angle_2d_1 = clusters.at(1)->GetParams().angle_2d;
42  double angle_2d_2 = clusters.at(2)->GetParams().angle_2d;
43  auto hits_0 = clusters.at(0)->GetParams().N_Hits ;
44  auto hits_1 = clusters.at(1)->GetParams().N_Hits ;
45  auto hits_2 = clusters.at(2)->GetParams().N_Hits ;
46 
47  auto sumCharge0 = clusters.at(0)->GetParams().sum_charge;
48  auto sumCharge1 = clusters.at(1)->GetParams().sum_charge;
49  auto sumCharge2 = clusters.at(2)->GetParams().sum_charge;
50 
51  //Calculate angles theta and phi for cluster pairs across 2 planes
52  //Theta goes from -90 to 90, phi from -180 to 180
53  double phi_01 = 0;
54  double theta_01 = 0;
55  double phi_12 = 0;
56  double theta_12 = 0;
57  double phi_02 = 0;
58  double theta_02 = 0;
59 
60  double max_phi(0), middle_phi(0), min_phi(0);
61  double max_theta(0), middle_theta(0), min_theta(0);
62  //When theta1, theta2, phi ratios are calcualted, find best 2
63  double max_ratio(0), middle_ratio(0), min_ratio(0);
64 
65  //For ordering hits and rejecting pairs which have a large difference in hit number
66  double max_hits(0), middle_hits(0), min_hits(0) ;
67 // double max_hits1(0), middle_hits1(0), min_hits1(0) ;
68 
69  //Calculate phi and theta from first 2 planes; check if third plane is consistent
71  geou.Get3DaxisN(plane_0,plane_1,angle_2d_0,angle_2d_1,phi_01,theta_01);
72  geou.Get3DaxisN(plane_1,plane_2,angle_2d_1,angle_2d_2,phi_12,theta_12);
73  geou.Get3DaxisN(plane_2,plane_0,angle_2d_2,angle_2d_0,phi_02,theta_02);
74 
75  //Adjust the range of phis/thetas that are bigger than 360 or less than 0.
76  FixPhiTheta(phi_01,theta_01);
77  FixPhiTheta(phi_12,theta_12);
78  FixPhiTheta(phi_02,theta_02);
79 
80  //Order phi and theta for ratio calculation later
81  SetMaxMiddleMin(phi_01,phi_12,phi_02,max_phi,middle_phi,min_phi);
82  SetMaxMiddleMin(theta_01,theta_12,theta_02,max_theta,middle_theta,min_theta);
83 
84  //Order hits from most to least
85  SetMaxMiddleMin(hits_0,hits_1,hits_2,max_hits,middle_hits,min_hits);
86 
87  //Ratio for hits
88  double ratio_max_min = 1;
89  double ratio_max_middle = 1;
90 
91  //Ratio for theta angles
92  double ratio_theta1 = 1;
93  double ratio_theta2 = 1;
94 
95  //Ratio for phi--only 1 ratio because 2 of the angles come from collection plane 2
96  //and are thus the same.
97  double ratio_phi = 1;
98 
99  //Total ratio
100  double ratio = 1;
101 
102  //This takes into account the fact that 0 and 360 having the same relative value (for phi; 0 and 180 for theta)
103  for(int i=0; i<2 ; i++){
104  while(min_phi + 360 < max_phi +_phi_cut && min_phi +360 > max_phi - _phi_cut)
105  {
106  min_phi +=360 ;
107  SetMaxMiddleMin(max_phi, middle_phi, min_phi,max_phi,middle_phi,min_phi);
108  }
109  }
110 
111  ratio_theta2 = min_theta / max_theta;
112  ratio_theta1 = middle_theta / max_theta;
113  ratio_phi = min_phi / max_phi ;
114 
115  //Get the biggest ratios for the thetas and phi
116  SetMaxMiddleMin(ratio_phi, ratio_theta1, ratio_theta2, max_ratio, middle_ratio, min_ratio);
117 
118  ratio_max_min = min_hits / max_hits ;
119  ratio_max_middle = middle_hits / max_hits ;
120 
121  ratio = max_ratio * sumCharge1* sumCharge2 * sumCharge0; //* ; //* ratio_phi ; //* ratio_max_middle ;
122 
123  //Test to make sure that max hits is not too much bigger than min
124  if( ratio_max_min <0.3)
125  ratio *= ratio_max_min ;
126 
127  //GeometryUtilities returns theta=-999 when 2d Angle=0--Don't know
128  //how else to deal with that. This seems to work.
129  if( theta_01 == -999 || theta_12 == -999 || theta_02 == -999)
130  return -1 ;
131 
132 
133  if(_debug && ratio > _ratio_cut ){
134  std::cout<<"\nNhits planes 0, 1, 2: " <<clusters.at(0)->GetParams().N_Hits<<", "<<clusters.at(1)->GetParams().N_Hits
135  <<", "<<clusters.at(2)->GetParams().N_Hits ;
136  std::cout<<"\nTheta1 , Theta2 : "<<ratio_theta1<<", "<<ratio_theta2;
137  std::cout<<"\nPhi Ratio: "<<ratio_phi;
138  std::cout<<"\nHits ratio mid : "<<ratio_max_middle ;
139  // std::cout<<"\nHits ratio min : "<<ratio_max_min ;
140  std::cout<<"\nTotal is: " <<ratio<<" ***************";
141 
142 
143  std::cout<<"\n\n0: 2dAngle: "<<clusters.at(0)->GetParams().cluster_angle_2d<<std::endl;
144  std::cout<<"1: 2dAngle: "<<clusters.at(1)->GetParams().cluster_angle_2d<<std::endl;
145  std::cout<<"2: 2dAngle: "<<clusters.at(2)->GetParams().cluster_angle_2d<<std::endl;
146  std::cout<<"\nTheta,Phi MaxMM : "<<max_theta<<", "<<middle_theta<<", "<<min_theta<<"\n\t\t"
147  <<max_phi<<", "<<middle_phi<<", "<<min_phi;
148 
149  std::cout<<"\nStart End Points: "<<clusters.at(0)->GetParams().start_point.t<<", "<<clusters.at(0)->GetParams().end_point.t<<"\n\t\t"
150  <<clusters.at(1)->GetParams().start_point.t<<", "<<clusters.at(1)->GetParams().end_point.t<<"\n\t\t "
151  <<clusters.at(2)->GetParams().start_point.t<<", "<<clusters.at(2)->GetParams().end_point.t;
152 
153 /* std::cout<<"\nFor planes 0 and 1: "<<std::endl ;
154  std::cout<<"\tPhi : "<<phi_01<<std::endl;
155  std::cout<<"\tTheta : "<<theta_01<<std::endl ;
156  std::cout<<"For planes 1 and 2: "<<std::endl ;
157  std::cout<<"\tPhi : "<<phi_12<<std::endl;
158  std::cout<<"\tTheta : "<<theta_12<<std::endl ;
159  std::cout<<"For plane 0 and 2: "<<std::endl ;
160  std::cout<<"\tPhi : "<<phi_02<<std::endl ;
161  std::cout<<"\tTheta : "<<theta_02<<std::endl; */
162  // std::cout<<"\nNEW CLUSTERS PAIRS NOW\n\n\n"<<std::endl<<std::endl;
163  std::cout<<"\n\n\n";
164  }
165 
166  return(ratio > _ratio_cut ? ratio : -1) ;
167  }
168 
169  //--------------------------------
170  void CFAlgo3DAngle::FixPhiTheta(double &phi, double &theta)
171  //--------------------------------
172  {
173  // while(phi <= 0)
174  // phi += 360 ;
175  // while(phi > 720)
176  // phi -= 360 ;
177 // if(phi <=0)
178 
179  phi+=360;
180 
181  if(theta != -999)
182  theta += 180 ;
183 
184  }
185 
186 
187  //------------------------------
188  void CFAlgo3DAngle::SetMaxMiddleMin(const double first, const double second, const double third, double &max, double &middle, double &min)
189  //------------------------------
190  {
191 
192  if(first > second && first > third){
193  max = first;
194  }
195  else if (first > second && first < third){
196  max = third ;
197  middle = first ;
198  min = second ;
199  }
200  else if (first > third && first < second){
201  max = second ;
202  middle = first ;
203  min = third ;
204  }
205  else if(first <second && first <third)
206  min = first ;
207 
208 
209  if (max == first && second > third){
210  middle = second ;
211  min = third ;
212  }
213  else if (max ==first && third > second){
214  middle = third ;
215  min = second ;
216  }
217 
218  if(min ==first && second > third){
219  middle = third ;
220  max = second;
221  }
222  else if(min ==first && third > second){
223  middle = second ;
224  max = third ;
225  }
226 
227 
228  //Very rarely, the angles(or hits) may be equal
229  if( first == second && first > third ){
230  max = first;
231  middle = second ;
232  min = third ;
233  }
234  else if( first == second && first < third){
235  max = third;
236  middle = first ;
237  min = second ;
238  }
239 
240  else if( first ==third && first > second){
241  max = first;
242  middle = third;
243  min = second;
244  }
245 
246  else if( first == third && first < second){
247  max = second ;
248  middle = first;
249  min = third ;
250  }
251 
252  else if( second ==third && second < first){
253  max = first;
254  middle = third;
255  min = second;
256  }
257 
258  else if( second == third && second > first){
259  max = second;
260  middle = third;
261  min = first ;
262  }
263 
264 
265 }
266 
267 
268  //------------------------------
270  //------------------------------
271  {
272 
273  }
274 
275 }
void SetRatio(float ratio)
Definition: CFAlgo3DAngle.h:56
Class def header for a class CFAlgo3DAngle.
virtual void SetMaxMiddleMin(const double first, const double second, const double third, double &most, double &middle, double &least)
CFAlgo3DAngle()
Default constructor.
static int max(int a, int b)
void SetDebug(bool debug)
Definition: CFAlgo3DAngle.h:50
T min(sqlite3 *const db, std::string const &table_name, std::string const &column_name)
Definition: statistics.h:55
Definition: cfalgo.cc:3
virtual float Float(const std::vector< const cluster::ClusterParamsAlg * > &clusters)
void SetThetaCut(float theta_cut)
Definition: CFAlgo3DAngle.h:52
void SetPhiCut(float phi_cut)
Definition: CFAlgo3DAngle.h:54
second_as<> second
Type of time stored in seconds, in double precision.
Definition: spacetime.h:85
virtual void FixPhiTheta(double &phi, double &theta)
Int_t Get3DaxisN(Int_t iplane0, Int_t iplane1, Double_t omega0, Double_t omega1, Double_t &phi, Double_t &theta) const
virtual void Reset()
Function to reset the algorithm instance called within CMergeManager/CMatchManager&#39;s Reset() ...
QTextStream & endl(QTextStream &s)