1#include "MdcHoughFinder/HoughZsFit.h"
40 if( (*iter).getDetectorType()==
MDC && (*iter).getSlayerType() !=0 && (*iter).getflag()==0 ) _recStereoHit.push_back(&(*
iter));
41 if( (*iter).getDetectorType()==
CGEM && (*iter).getCluster()->getflag()==2 ) _recStereoHit.push_back(&(*
iter));
44 _hitSize=_recStereoHit.size();
48 if(
m_debug>0) cout<<
"size of rechit in zs fit "<<_hitSize<<endl;
57 if(_hitSize>=3)
doit();
76 for(
int iline=0;iline<8;iline++){
77 linefit[iline].
_pointCol.push_back( *(_recStereoHit[0]) );
78 linefit[iline].
_pointCol.push_back( *(_recStereoHit[1]) );
79 linefit[iline].
_pointCol.push_back( *(_recStereoHit[2]) );
82 for(
int i =0;i<3;i++){
83 cout<<
" the first 3 hits ("<<i<<
" "<< _recStereoHit[i]->getLayerId()<<
","<<_recStereoHit[i]->getWireId()<<
")"<<endl;
84 cout<<
" left "<<(_recStereoHit[i])->getsAmb(0)<<
" "<<(_recStereoHit[i])->getzAmb(0)<<endl;
85 cout<<
" right "<<(_recStereoHit[i])->getsAmb(1)<<
" "<<(_recStereoHit[i])->getzAmb(1)<<endl;
122 for(
int i=0;i<8;i++){
123 linefit[i].
_ambig.push_back( linefit[i]._pointCol[0].getAmbig() );
124 linefit[i].
_ambig.push_back( linefit[i]._pointCol[1].getAmbig() );
125 linefit[i].
_ambig.push_back( linefit[i]._pointCol[2].getAmbig() );
126 if(
m_debug>0) cout<<
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^begin line "<<i<<endl;
130 for(
int j=3;j<_hitSize;j++){
131 double chi_last=linefit[i].
_chi;
132 double k_last=linefit[i].
_k;
133 double b_last=linefit[i].
_b;
134 if(
m_debug>0) cout<<
"last "<<j<<
" chi k b "<<chi_last<<
" "<<k_last<<
" "<<b_last<<endl;
135 linefit[i].
_pointCol.push_back( *(_recStereoHit[j]) );
139 linefit[i].
_pointCol[line_size].setAmb(0);
140 if(
m_debug>0) cout<<
"Add point left "<<
"("<<linefit[i].
_pointCol[line_size].getLayerId()<<
","<<linefit[i].
_pointCol[line_size].getWireId()<<
") "<<linefit[i].
_pointCol[line_size].getStyle()<<
" "<<linefit[i].
_pointCol[line_size].getsPos()<<
" "<<linefit[i].
_pointCol[line_size].getzPos()<<endl;
142 double chil=linefit[i].
_chi;
143 double kl=linefit[i].
_k;
144 double bl=linefit[i].
_b;
145 if (linefit[i]._pointCol[line_size].getsPos()==-99) {
148 if(
m_debug>0) cout<<
"left "<<line_size<<
" chi k b "<<chil<<
" "<<kl<<
" "<<bl<<endl;
150 linefit[i].
_pointCol[line_size].setAmb(1);
151 if(
m_debug>0) cout<<
"Add point right "<<
"("<<linefit[i].
_pointCol[line_size].getLayerId()<<
","<<linefit[i].
_pointCol[line_size].getWireId()<<
") "<<linefit[i].
_pointCol[line_size].getStyle()<<
" "<<linefit[i].
_pointCol[line_size].getsPos()<<
" "<<linefit[i].
_pointCol[line_size].getzPos()<<endl;
153 double chir=linefit[i].
_chi;
154 double kr=linefit[i].
_k;
155 double br=linefit[i].
_b;
156 if (linefit[i]._pointCol[line_size].getsPos()==-99) {
159 if(
m_debug>0) cout<<
"right "<<line_size<<
" chi k b "<<chir<<
" "<<kr<<
" "<<br<<endl;
162 linefit[i].
_pointCol[line_size].setAmb(0);
163 linefit[i].
_chi=chil;
166 linefit[i].
_ambig.push_back(0);
169 else linefit[i].
_ambig.push_back(1);
174 for(
int ihit=0;ihit<line_size;ihit++){
175 int ambighit = linefit[i].
_pointCol[ihit].getAmbig();
176 int ambigTruth = linefit[i].
_pointCol[ihit].getLrTruth();
177 int layer =linefit[i].
_pointCol[ihit].getLayerId();
178 int wire=linefit[i].
_pointCol[ihit].getWireId();
188 double dChi = chi_last-linefit[i].
_chi;
189 double dChi_n = (linefit[i].
_chi)/(line_size)-(chi_last/line_size-1);
190 if(
m_debug>0) cout<<
"Chi: "<<linefit[i].
_chi<<endl;
191 if(
m_debug>0) cout<<
"dChi: "<<dChi<<endl;
192 if(
m_debug>0) cout<<
"dChi/n: "<<dChi_n<<endl;
195 if( fabs(dChi_n) > 25.)
201 linefit[i].
_chi=chi_last;
202 linefit[i].
_k=k_last;
203 linefit[i].
_b=b_last;
204 linefit[i].
_ambig.at(j)=-999;
210 bool find_best_cluster =
false;
211 for(
int ilayer=2;ilayer>=0;ilayer--){
212 int ncluster = _cgemCluster[ilayer].size();
213 if(ncluster==0)
continue;
214 double chi_last=linefit[i].
_chi;
215 double k_last=linefit[i].
_k;
216 double b_last=linefit[i].
_b;
217 if(
m_debug>0) cout<<
"last chi k b "<<chi_last<<
" "<<k_last<<
" "<<b_last<<endl;
218 double chi_temp=9999;
219 bool find_best_cluster =
false;
221 for(
int icluster=0;icluster<ncluster;icluster++){
222 _cgemCluster[ilayer][icluster]->setflag(-999);
223 Line linefit_temp = linefit[i];
224 linefit_temp.
_pointCol.push_back(*(_cgemCluster[ilayer][icluster]));
226 if(linefit_temp.
_chi<chi_temp){
227 chi_temp = linefit_temp.
_chi;
228 cluster_temp = _cgemCluster[ilayer][icluster];
229 find_best_cluster =
true;
232 if(!find_best_cluster)
continue;
233 linefit[i].
_pointCol.push_back(*cluster_temp);
238 double dChi = chi_last-linefit[i].
_chi;
239 double dChi_n = (linefit[i].
_chi)/(line_size-1)-(chi_last/line_size);
240 if(
m_debug>0) cout<<
"Chi: "<<linefit[i].
_chi<<endl;
241 if(
m_debug>0) cout<<
"dChi: "<<dChi<<endl;
242 if(
m_debug>0) cout<<
"dChi/n: "<<dChi_n<<endl;
244 if( fabs(dChi_n) > 25.)
248 linefit[i].
_chi=chi_last;
249 linefit[i].
_k=k_last;
250 linefit[i].
_b=b_last;
256 if( (linefit[i]._pointCol[0].getsPos()==-99) || (linefit[i]._pointCol[1].getsPos()==-99) || (linefit[i]._pointCol[2].getsPos()==-99) ) {
257 linefit[i].
_chi=99999;
261 for(
int i=0;i<8;i++){
262 if(
m_debug>0) cout<<
"Line :"<<i<<
" chis: "<<linefit[i].
_chi<<
" k,b: "<<linefit[i].
_k<<
" "<<linefit[i].
_b<<endl;
263 int ambig_correct = 0;
264 for(
int j=0;j<_hitSize;j++){
265 int ambig = linefit[i].
_ambig.at(j);
266 int layer= _recStereoHit.at(j)->getLayerId();
267 int wire= _recStereoHit.at(j)->getWireId();
268 int style= _recStereoHit.at(j)->getStyle();
270 if (ambig!=-999) l= _recStereoHit.at(j)->getsAmb(ambig);
272 if (ambig!=-999) z= _recStereoHit.at(j)->getzAmb(ambig);
274 if (l==-99 && z==-99) ambig=-999;
275 if(
m_debug>0) cout<<
"("<<layer<<
" ,"<<wire<<
") style "<<style<<
" ambig "<<ambig<<
" s "<<l<<
" z "<<z<<endl;
277 _recStereoHit[j]->setAmb(ambig);
278 if( ambig ==-999) _recStereoHit[j]->setflag(-999);
279 int ambigTruth = _recStereoHit.at(j)->getLrTruth();
280 if (ambigTruth == -1) ambigTruth=1;
281 else if (ambigTruth == 1) ambigTruth=0;
282 if(ambig ==ambigTruth) ambig_correct++;
284 _pro_correct = (double)ambig_correct/(
double)_hitSize;
286 for(
int icluster=0;icluster<linefit[i].
_clusterCol.size();icluster++){
294 if(
m_debug>0) cout<<
"z0 tanl : "<<_z0<<
" "<<_tanl<<endl;
396 double *
x=
new double[N+1];
397 double *y=
new double[N+1];
398 for(
int i=0;i<N;i++){
407 double k(0),b(0),chi2(0);
426 for(
int i=0;i<nhit;i++){
429 x2_sum=x2_sum+
x[i]*
x[i];
430 y2_sum=y2_sum+y[i]*y[i];
431 xy_sum=xy_sum+
x[i]*y[i];
433 b=(x2_sum*y_sum-xy_sum*x_sum)/(nhit*x2_sum-x_sum*x_sum);
434 k=(nhit*xy_sum-x_sum*y_sum)/(nhit*x2_sum-x_sum*x_sum);
437 for(
int i=0;i<nhit;i++){
440 if(yE[i]!=0) chi2_temp=(y[i]-yE[i])*(y[i]-yE[i])/yE[i]*yE[i];
449 return (*hita).getLayerId() > (*hitb).getLayerId();
452 std::sort (_recStereoHit.begin(),_recStereoHit.end(),
layer_in_track);
455 for(
int i =0;i<_recStereoHit.size();i++){
456 cout<<
"("<<_recStereoHit[i]->getLayerId()<<
","<<_recStereoHit[i]->getWireId()<<
")"<<endl;
461 std::vector<int> fired_layer;
463 for(
int ilayer=2;ilayer>=0;ilayer--){
464 int ncluster = _cgemCluster[ilayer].size();
465 if(ncluster!=0)fired_layer.push_back(ilayer);
467 if(fired_layer.size()<2){
474 double d1 =9999,d2=9999;
475 HoughRecHit *cluster1,*cluster2,*cluster3,*cluster4;
476 for(
int i=0;i<_cgemCluster[fired_layer[0]].size();i++){
477 _cgemCluster[fired_layer[0]][i]->setflag(-999);
478 s1 = _cgemCluster[fired_layer[0]][i]->getsPos();
479 z1 = _cgemCluster[fired_layer[0]][i]->getzPos();
480 if(s1==0 || z1==0)
continue;
481 for(
int j=0;j<_cgemCluster[fired_layer[1]].size();j++){
482 _cgemCluster[fired_layer[1]][j]->setflag(-999);
483 s2 = _cgemCluster[fired_layer[1]][j]->getsPos();
484 z2 = _cgemCluster[fired_layer[1]][j]->getzPos();
485 if(s2==0 || z2==0)
continue;
486 double delta=fabs(s1/s2-z1/z2);
489 cluster1 = _cgemCluster[fired_layer[0]][i];
490 cluster2 = _cgemCluster[fired_layer[1]][j];
493 if(fired_layer.size()==3){
494 for(
int j=0;j<_cgemCluster[fired_layer[2]].size();j++){
495 _cgemCluster[fired_layer[2]][j]->setflag(-999);
496 s2 = _cgemCluster[fired_layer[2]][j]->getsPos();
497 z2 = _cgemCluster[fired_layer[2]][j]->getzPos();
498 if(s2==0 || z2==0)
continue;
499 double delta=fabs(s1/s2-z1/z2);
502 cluster3 = _cgemCluster[fired_layer[0]][i];
503 cluster4 = _cgemCluster[fired_layer[2]][j];
515 if(fired_layer.size()==3){
523 for(
int j=0;j<_hitSize;j++){
524 double chi_last=linefit.
_chi;
525 double k_last=linefit.
_k;
526 double b_last=linefit.
_b;
527 linefit.
_pointCol.push_back( *(_recStereoHit[j]) );
529 if(
m_debug>0) cout<<
"Add point left "<<
"("<<linefit.
_pointCol[line_size].getLayerId()<<
","<<linefit.
_pointCol[line_size].getWireId()<<
") "<<linefit.
_pointCol[line_size].getStyle()<<
" "<<linefit.
_pointCol[line_size].getsPos()<<
" "<<linefit.
_pointCol[line_size].getzPos()<<endl;
531 double chil=linefit.
_chi;
532 double kl=linefit.
_k;
533 double bl=linefit.
_b;
534 if (linefit.
_pointCol[line_size].getsPos()==-99) {
537 if(
m_debug>0) cout<<
"left "<<line_size<<
" chi k b "<<chil<<
" "<<kl<<
" "<<bl<<endl;
540 if(
m_debug>0) cout<<
"Add point right "<<
"("<<linefit.
_pointCol[line_size].getLayerId()<<
","<<linefit.
_pointCol[line_size].getWireId()<<
") "<<linefit.
_pointCol[line_size].getStyle()<<
" "<<linefit.
_pointCol[line_size].getsPos()<<
" "<<linefit.
_pointCol[line_size].getzPos()<<endl;
542 double chir=linefit.
_chi;
543 double kr=linefit.
_k;
544 double br=linefit.
_b;
545 if (linefit.
_pointCol[line_size].getsPos()==-99) {
548 if(
m_debug>0) cout<<
"right "<<line_size<<
" chi k b "<<chir<<
" "<<kr<<
" "<<br<<endl;
555 linefit.
_ambig.push_back(0);
556 _recStereoHit[j]->setAmb(0);
559 linefit.
_ambig.push_back(1);
560 _recStereoHit[j]->setAmb(1);
564 double dChi = chi_last-linefit.
_chi;
565 double dChi_n = (linefit.
_chi)/(line_size-1)-(chi_last/line_size);
566 if(
m_debug>0) cout<<
"dChi: "<<dChi<<endl;
567 if(
m_debug>0) cout<<
"dChi/n: "<<dChi_n<<endl;
570 if( fabs(dChi_n) > 25.)
572 _recStereoHit[j]->setAmb(-999);
573 _recStereoHit[j]->setflag(-999);
575 linefit.
_chi=chi_last;
578 linefit.
_ambig.at(j)=-999;
584 if(
m_debug>0) cout<<
"z0 tanl : "<<_z0<<
" "<<_tanl<<endl;
591 double x_max = 0.93/sqrt(1-0.93*0.93);
599 TH2D* houghSpace =
new TH2D(
"houghSpace",
"houghSpace",x_bin,-x_max,x_max,y_bin,-y_max,y_max);
601 for(
int i=0;i<_hitSize;i++){
603 double x = -x_max - 0.5*x_step;
604 _recStereoHit[i]->setAmb(0);
606 double yl = -_recStereoHit[i]->getsPos()*
x + _recStereoHit[i]->getzPos();
607 double dyl = -_recStereoHit[i]->getsPos()*x_step;
608 _recStereoHit[i]->setAmb(1);
610 double yr = -_recStereoHit[i]->getsPos()*
x + _recStereoHit[i]->getzPos();
611 double dyr = -_recStereoHit[i]->getsPos()*x_step;
616 houghSpace->Fill(
x,yl);
618 houghSpace->Fill(
x,yr);
621 houghSpace->GetMaximumBin(binx,biny,binz);
622 _tanl = houghSpace->GetXaxis()->GetBinCenter(binx);
623 _z0 = houghSpace->GetYaxis()->GetBinCenter(biny);
656 int l[3] = {-1,-1,-1};
657 double dZ[3] = {999,999,999};
660 for(
int i=0;i<_hitSize;i++){
661 _recStereoHit[i]->setAmb(0);
662 double s0 = _recStereoHit[i]->getsPos();
663 double z0 = _recStereoHit[i]->getzPos();
664 _recStereoHit[i]->setAmb(1);
665 double s1 = _recStereoHit[i]->getsPos();
666 double z1 = _recStereoHit[i]->getzPos();
667 double zl = s0*_tanl + _z0;
668 double zr = s1*_tanl + _z0;
670 if(fabs(z0-zl) < fabs(z1-zr)){
673 _recStereoHit[i]->setAmb(0);
677 _recStereoHit[i]->setAmb(1);
679 double deltaZ = (z-(
s*_tanl+_z0));
682 _recStereoHit[i]->setflag(0);
685 _recStereoHit[i]->setflag(-999);
687 if(_recStereoHit[i]->getDetectorType()==
CGEM && _recStereoHit[i]->getflag() ==0){
688 if(fabs(deltaZ) < dZ[_recStereoHit[i]->getLayerId()]){
689 l[_recStereoHit[i]->getLayerId()] = i;
690 dZ[_recStereoHit[i]->getLayerId()] = fabs(deltaZ);
694 for(
int i=0;i<_hitSize;i++){
695 if(_recStereoHit[i]->getDetectorType()==
CGEM){
696 if(i == l[0] || i == l[1] || i== l[2]){
697 _recStereoHit[i]->setflag(0);
700 _recStereoHit[i]->setflag(-999);
double abs(const EvtComplex &c)
bool layer_in_track(const HoughRecHit *hita, const HoughRecHit *hitb)
bool compare_zsfit(const HoughZsFit::Line &a, const HoughZsFit::Line &b)
std::vector< HoughRecHit > recHitCol
bool layer_in_track(const HoughRecHit *hita, const HoughRecHit *hitb)
bool compare_zsfit(const HoughZsFit::Line &a, const HoughZsFit::Line &b)
HoughZsFit(vector< HoughRecHit > *recHitCol)
int leastLine(int, double x[], double y[], double &, double &, double &)
static vector< double > m_cut_deltaZ
void leastFit(Line &linefit, int N)
std::vector< HoughRecHit > _pointCol
std::vector< HoughRecHit * > _clusterCol
std::vector< int > _ambig