BOSS 7.0.5
BESIII Offline Software System
Loading...
Searching...
No Matches
Hough2D.cxx
Go to the documentation of this file.
1#include "MdcHoughFinder/Hough2D.h"
2
8//double Hough2D::m_dropTrkDrCut=999; //no limit of dr
11double Hough2D::m_dropTrkChi2Cut=99999.;
13
15 //_m_gm = MdcDetector::instance(0);
16 //_m_gm = MdcDetector::instance(0);
17}
18
19//Hough2D::~Hough2D(){
20// for(int i =0;i<_MdcHitCol.size();i++){
21// cout<<" destruct " <<i<<endl;
22// delete _MdcHitCol[i];
23// }
24//}
25
27 _circleR=other._circleR;
28 _circleX=other._circleX;
29 _circleY=other._circleY;
30 _d0=other._d0;
31 _phi0=other._phi0;
32 _omega=other._omega;
33 _charge=other._charge;
34 _pt=other._pt;
35 _nfit=other._nfit;
36 _chi2_aver2D=other._chi2_aver2D;
37 _bunchT0=other._bunchT0;
38 //_m_gm = other._m_gm;
39// _recHitVec=other._recHitVec;
40 _recHitVec=other._recHitVec;
41
42// for(int i =0;i<other._MdcHitCol.size();i++){
43// MdcHit* p_hit= new MdcHit( *(other._MdcHitCol[i]));
44// _MdcHitCol.push_back(p_hit);
45// }
46}
47
48Hough2D::Hough2D(recHitCol hitCol, double bunchtime):_recHitVec(hitCol),_bunchT0(bunchtime){
49 //_m_gm = MdcDetector::instance(0);
50}
51
52//void Hough2D::print(){
53// cout<<"Print Hough2D "<<endl;
54// cout<<"d0: "<<_d0<<endl;
55// cout<<"phi0: "<<_phi0<<endl;
56// cout<<"omega: "<<_omega<<endl;
57// cout<<"charge: "<<_charge<<endl;
58//
59// int size=_recHitVec.size();
60// cout<<"Hit Candi size: "<<size<<endl;
61// for(int i=0;i<size;i++){
62// int layer= _recHitVec[i].getLayerId();
63// int wire = _recHitVec[i].getWireId();
64// std::cout<<"("<<layer<<","<<wire<<") "<<std::endl;
65// }
66//}
67
69 double x_sum=0;
70 double y_sum=0;
71 double x2_sum=0;
72 double y2_sum=0;
73 double x3_sum=0;
74 double y3_sum=0;
75 double x2y_sum=0;
76 double xy2_sum=0;
77 double xy_sum=0;
78 double a1=0;
79 double a2=0;
80 double b1=0;
81 double b2=0;
82 double c1=0;
83 double c2=0;
84 // double x_aver,y_aver,r2;
85 int hitCandiSize=_recHitVec.size();
86 if(hitCandiSize>=3){
87 for(int i=0;i<hitCandiSize;i++){
88 // if( _recHitVec[i].getflag()!=0 ) continue;
89 //cout<<"size of hit in least2d "<<hitCandiSize<<endl;
90 // if(_recHitVec[i]->getSlayerType() !=0 ) continue;
91 x_sum=x_sum+( _recHitVec[i].getMidX() );
92 y_sum=y_sum+( _recHitVec[i].getMidY() );
93 x2_sum=x2_sum+( _recHitVec[i].getMidX() )*( _recHitVec[i].getMidX() );
94 y2_sum=y2_sum+( _recHitVec[i].getMidY() )*( _recHitVec[i].getMidY() );
95 x3_sum=x3_sum+( _recHitVec[i].getMidX() )*( _recHitVec[i].getMidX() )*( _recHitVec[i].getMidX() );
96 y3_sum=y3_sum+( _recHitVec[i].getMidY() )*( _recHitVec[i].getMidY() )*( _recHitVec[i].getMidY() );
97 x2y_sum=x2y_sum+( _recHitVec[i].getMidX() )*( _recHitVec[i].getMidX() )*( _recHitVec[i].getMidY() );
98 xy2_sum=xy2_sum+( _recHitVec[i].getMidX() )*( _recHitVec[i].getMidY() )*( _recHitVec[i].getMidY() );
99 xy_sum=xy_sum+( _recHitVec[i].getMidX() )*( _recHitVec[i].getMidY() );
100 }
101 //hitCandiSize+=1000; //0 constrain
102 a1=x2_sum-x_sum*x_sum/hitCandiSize;
103 a2=xy_sum-x_sum*y_sum/hitCandiSize;
104 b1=a2;
105 b2=y2_sum-y_sum*y_sum/hitCandiSize;
106 c1=(x3_sum+xy2_sum-x_sum*(x2_sum+y2_sum)/hitCandiSize)/2.;
107 c2=(y3_sum+x2y_sum-y_sum*(x2_sum+y2_sum)/hitCandiSize)/2.;
108 //cout<<"a1 a2 b1 b2 c1 c2 "<<a1<<" "<<a2<<" "<<b1<<" "<<b2<<" "<<c1<<" "<<c2<<endl;
109
110 //x_aver=x_sum/hitCandiSize;
111 //y_aver=y_sum/hitCandiSize;
112
113 _circleX =(b1*c2-b2*c1)/(b1*b1-a1*b2);
114 _circleY =(b1*c1-a1*c2)/(b1*b1-a1*b2);
115 _circleR=sqrt( (x2_sum+y2_sum-2*_circleX*x_sum-2*_circleY*y_sum)/hitCandiSize+_circleX*_circleX+_circleY*_circleY );
116 //double r_temp=sqrt(r2);
117 // cout<<"circle: ("<<_circleX<<","<<_circleY<<","<<_circleR<<endl;
118
119 _d0=sqrt(_circleX*_circleX+_circleY*_circleY)-_circleR;
120 _phi0=atan2(_circleY,_circleX)+M_PI/2.;
121 _omega=1/_circleR; //according with Q
122 double pt_temp=_circleR/333.567;
123 if(m_debug>0) cout<<" pt: "<<pt_temp<<endl;
124 if(m_debug>0) cout<<"par: ("<<_d0<<","<<_phi0<<","<<_omega<<")"<<endl;
125 _pt=pt_temp;
126
127 _circleX_least =(b1*c2-b2*c1)/(b1*b1-a1*b2);
128 _circleY_least =(b1*c1-a1*c2)/(b1*b1-a1*b2);
129 _circleR_least=sqrt( (x2_sum+y2_sum-2*_circleX*x_sum-2*_circleY*y_sum)/hitCandiSize+_circleX*_circleX+_circleY*_circleY );
130 _pt_least=pt_temp;
131 return 1;
132 }
133 else {return 0;}
134}
135
137 // replace in houghTrack
138 // if(_charge==-1){
139 // _d0=_d0;
140 // _omega=-1.*fabs(_omega);
141 // }
142 // if(_charge==1){
143 // _d0=-_d0;
144 // _omega=1.*fabs(_omega);
145 // _phi0=_phi0-M_PI;
146 // }
147 TrkExchangePar tt(_d0,_phi0,_omega,0,0);
148 if (m_debug>0) cout<<"q d0 phi0 omega: "<<_charge<<" "<<_d0<<" "<<_phi0<<" "<<_omega<<endl;
149 TrkCircleMaker circlefactory;
150 float chisum =0.;
151 newTrack = circlefactory.makeTrack(tt, chisum, *_context, _bunchT0);
152 bool permitFlips = true;
153 bool lPickHits = true;
154 circlefactory.setFlipAndDrop(*newTrack, permitFlips, lPickHits);
155 //int nDigi = digiToHots(newTrack);
156 TrkHitList* trkHitList = newTrack->hits();
157 int digiId=0;
158 vector<MdcHit*> t_hitCol;
159 std::vector<HoughRecHit>::iterator iter = _recHitVec.begin();
160 for (;iter != _recHitVec.end(); iter++, digiId++) {
161 if( (*iter).getflag()!=0) continue;
162 //if( (*iter).getOuter()==1) continue;
163// const MdcDetector* mgm = Global::m_gm;
164 const MdcDigi* aDigi = (*iter).digi();
165 //MdcHit *hit = new MdcHit(aDigi, _m_gm);
166 //MdcHit *hit = new MdcHit(aDigi, mgm);
167 MdcHit *hit = new MdcHit(aDigi, Global::m_gm);
168// MdcHit mdcHit(aDigi,_m_gm);
169// MdcHit *hit = &mdcHit;
170 t_hitCol.push_back(hit);
171 Identifier id = aDigi->identify();
172 int layer = MdcID::layer(id);
173 int wire = MdcID::wire(id);
174 //if(layer>=12) continue;
175 //if ((layer>=0&&layer<=7)||(layer>=20&&layer<=35)) {continue;}
177 hit->setCountPropTime(true);
178 // new fltLen and ambig
179 int newAmbig = 0;
180 // calc. position of this point
181 MdcRecoHitOnTrack temp(*hit, newAmbig, _bunchT0*1.e9);//m_bunchT0 nano second here
182 MdcHitOnTrack* newhot = &temp;
183 newhot->setFltLen( (*iter).getRet().second); //caculate by mc
184 double distoTrack= (*iter).getDisToTrack();
185
186 //double ddCut=1.0;
187 double ddCut=1.0;
188 int use_in2d=1;
189 if(hit->driftTime(_bunchT0,0)>1000) use_in2d=0; // >1000or800?
190 if(hit->driftDist(_bunchT0,0)>ddCut) use_in2d=0;
191 //if(m_debug>0) cout<<"flt : "<<(*iter).getRet().second <<endl;
192 if(m_debug>0) std::cout<<" ("<<layer<<","<<wire<<",rt "<<hit->rawTime()<<",dt "<<hit->driftTime(_bunchT0,0)<<",dd "<<hit->driftDist(_bunchT0,0)<<") T0 " << _mdcCalibFunSvc->getT0(layer,wire) <<" timewalk "<< _mdcCalibFunSvc->getTimeWalk(layer, aDigi->getChargeChannel())<< "dist: "<<hit->driftDist(_bunchT0,0)<<" disToTrk "<< distoTrack<<" use?: "<<use_in2d<<endl;
193 if(use_in2d==0) continue;
194 trkHitList->appendHot(newhot);
195 }
196 //newTrack->printAll(std::cout);
197
198 TrkHitList* qhits = newTrack->hits();
199 TrkErrCode err=qhits->fit();
200 const TrkFit* newFit = newTrack->fitResult();
201 int nActiveHit = newTrack->hots()->nActive();
202 int fit_stat=false;
203 double chi2=-999.;
204 if (!newFit || (newFit->nActive()<3)||err.failure()!=0) {
205 if(m_debug>0){
206 cout << " global 2d fit failed ";
207 if(newFit) cout <<" nAct "<<newFit->nActive();
208 cout<<"ERR1 failure ="<<err.failure()<<endl;
209 fit_stat=0;
210 }
211 }else{
212 TrkExchangePar par = newFit->helix(0.);
213 if( abs( 1/(par.omega()) )>0.03) {
214 fit_stat = 1;
215 chi2=newFit->chisq();
216 if(m_debug>0) cout<<"chi2 "<<chi2<<endl;
217 }
218 else {
219 fit_stat = 0;
220 chi2=-999;
221 }
222
223 bool badQuality = false;
224 if(fabs(chi2)>m_qualityFactor*m_dropTrkChi2Cut ){
225 if(m_debug>0){
226 std::cout<<__FILE__<<" "<<__LINE__<<" drop track by chi2 "<<chi2<<" > "<<m_qualityFactor<<" * "<<m_dropTrkChi2Cut <<std::endl;
227 }
228 badQuality=1;
229 }
230 if( fabs(par.d0())>m_qualityFactor*m_dropTrkDrCut) {
231 if(m_debug>0){
232 std::cout<<__FILE__<<" "<<__LINE__<<" drop track by d0 "<<par.d0()<<" > "<<m_qualityFactor<<" * "<<m_dropTrkDrCut <<std::endl;
233 }
234 badQuality=1;
235 }
236 if( (fabs(chi2)/qhits->nHit()) > m_qualityFactor*m_dropTrkChi2NdfCut) {
237 if(m_debug>0){
238 std::cout<<__FILE__<<" "<<__LINE__<<" drop track by chi2/ndf"<<(fabs(chi2)/qhits->nHit()) <<" > "<<m_qualityFactor<<" * "<<m_dropTrkChi2NdfCut<<std::endl;
239 }
240 badQuality=1;
241 }
242 if( nActiveHit <= m_dropTrkNhitCut) {
243 if(m_debug>0){
244 std::cout<<__FILE__<<" "<<__LINE__<<" drop track by nhit"<<nActiveHit <<" <= "<<m_dropTrkNhitCut<<std::endl;
245 }
246 badQuality=1;
247 }
248 if( badQuality) fit_stat=0;
249 }
250 if( fit_stat==1 ){
251 if(m_debug>0){
252 cout << " global 2d fit success"<<endl;
253 cout<<__FILE__<<__LINE__<<"AFTER fit 2d "<<endl;
254 newTrack->print(std::cout);
255 }
256 TrkExchangePar par = newFit->helix(0.);
257 double d0=par.d0();
258 double phi0=par.phi0();
259 double omega=par.omega();
260 double r_temp=-1./par.omega();
261 double x_cirtemp = sin(par.phi0()) *(par.d0()+1/par.omega()) * -1.; //z axis verse,x_babar = -x_belle
262 double y_cirtemp = -1.*cos(par.phi0()) *(par.d0()+1/par.omega()) * -1;//???
263 if(m_debug>0){
264 cout<<" circle after globle 2d: "<<"("<<x_cirtemp<<","<<y_cirtemp<<","<<r_temp<<")"<<endl;
265 cout<<"pt_rec: "<<1./omega/333.567<<endl;
266 }
267 _pt=1./omega/333.567;
268 _circleX=x_cirtemp;
269 _circleY=y_cirtemp;
270 _circleR=_charge/omega;
271 _d0=par.d0();
272 _phi0=par.phi0();
273 _omega=par.omega();
274
275 int nfit2d=0;
276 if(m_debug>1) cout<<" hot list:"<<endl;
277 TrkHotList::hot_iterator hotIter= qhits->hotList().begin();
278 int lay=((MdcHit*)(hotIter->hit()))->layernumber();
279 int wir=((MdcHit*)(hotIter->hit()))->wirenumber();
280 int hittemp=lay*1000+wir;
281 while (hotIter!=qhits->hotList().end()) {
282 if(m_debug>1){ cout <<"(" <<((MdcHit*)(hotIter->hit()))->layernumber()
283 <<","<<((MdcHit*)(hotIter->hit()))->wirenumber()
284 <<":"<<hotIter->isActive()<<") ";
285 }
286 if (hotIter->isActive()==1) nfit2d++;
287 hotIter++;
288 }
289 _nfit=nfit2d;
290 }
291 _chi2_aver2D=chi2/_nfit;
292
293 for(int i=0;i<t_hitCol.size();i++){
294 delete t_hitCol[i];
295 }
296 delete newTrack;
297
298 if(m_debug>0) cout<<" in 2D fit number of Active hits : "<<_nfit<<endl;
299 return fit_stat;
300}
301
302/*
303int Hough2D::digiToHots(TrkRecoTrk* newTrack){
304 TrkHitList* trkHitList = newTrack->hits();
305 int digiId=0;
306 std::vector<HoughRecHit>::iterator iter = _recHitVec.begin();
307 for (;iter != _recHitVec.end(); iter++, digiId++) {
308 if( (*iter).getflag()!=0) continue;
309 //if( (*iter).getOuter()==1) continue;
310 const MdcDigi* aDigi = (*iter).digi();
311 //cout<<"Digi in hot "<<aDigi<<endl;
312 MdcHit *hit = new MdcHit(aDigi, _m_gm);
313 _MdcHitCol.push_back(hit);
314 Identifier id = aDigi->identify();
315 int layer = MdcID::layer(id);
316 int wire = MdcID::wire(id);
317 //if ((layer>=0&&layer<=7)||(layer>=20&&layer<=35)) {continue;}
318 hit->setCalibSvc(_mdcCalibFunSvc);
319 hit->setCountPropTime(true);
320 // new fltLen and ambig
321 int newAmbig = 0;
322 // calc. position of this point
323 MdcRecoHitOnTrack temp(*hit, newAmbig, _bunchT0*1.e9);//m_bunchT0 nano second here
324 MdcHitOnTrack* newhot = &temp;
325 newhot->setFltLen( (*iter).getRet().second); //caculate by mc
326
327 double ddCut;
328 if(layer<8) ddCut=1.0;
329 else ddCut=1.0;
330 int use_in2d=1;
331 if(hit->driftTime(_bunchT0,0)>800) use_in2d=0;;
332 if(hit->driftDist(_bunchT0,0)>ddCut) use_in2d=0;
333 //if(m_debug>0) cout<<"flt : "<<(*iter).getRet().second <<endl;
334 if(m_debug>0) std::cout<<" ("<<layer<<","<<wire<<",rt "<<hit->rawTime()<<",dt "<<hit->driftTime(_bunchT0,0)<<") T0 " << _mdcCalibFunSvc->getT0(layer,wire) <<" timewalk "<< _mdcCalibFunSvc->getTimeWalk(layer, aDigi->getChargeChannel())<< "dist: "<<hit->driftDist(_bunchT0,0)<<" use?: "<<use_in2d<<endl;
335 if(use_in2d==0) continue;
336 trkHitList->appendHot(newhot);
337 }
338 return trkHitList->nHit();
339}
340*/
342 cout<<" print hough2d "<<endl;
343 cout<<"par: "<<_d0<<" "<<_phi0<<" "<<_omega<<endl;
344}
345
EvtStreamInputIterator< typename Generator::result_type > iter(Generator gen, int N=0)
double sin(const BesAngle a)
double cos(const BesAngle a)
#define M_PI
Definition: TConstant.h:4
int fitLeast()
Definition: Hough2D.cxx:68
Hough2D()
Definition: Hough2D.cxx:14
void print()
Definition: Hough2D.cxx:341
virtual int fit()
Definition: Hough2D.cxx:136
double getT0(int layid, int cellid) const
double getTimeWalk(int layid, double Q) const
void setCalibSvc(const MdcCalibFunSvc *calibSvc)
Definition: MdcHit.cxx:136
double driftTime(double tof, double z) const
Definition: MdcHit.cxx:142
void setCountPropTime(const bool count)
double driftDist(double, int, double, double, double) const
Definition: MdcHit.cxx:156
static int layer(const Identifier &id)
Values of different levels (failure returns 0)
Definition: MdcID.cxx:49
static int wire(const Identifier &id)
Definition: MdcID.cxx:54
virtual Identifier identify() const
Definition: RawData.cxx:15
unsigned int getChargeChannel() const
Definition: RawData.cxx:45
virtual double chisq() const =0
virtual int nActive() const =0
virtual TrkExchangePar helix(double fltL) const =0
TrkErrCode fit()
Definition: TrkHitList.cxx:59
TrkHitOnTrk * appendHot(const TrkHitOnTrk *theHot)
Definition: TrkHitList.cxx:91
const TrkHotList & hotList() const
Definition: TrkHitList.cxx:51
virtual int nActive(TrkEnums::TrkViewInfo view=TrkEnums::bothView) const =0
virtual void print(std::ostream &) const
const TrkFit * fitResult() const
Definition: TrkRecoTrk.cxx:387
bool setFlipAndDrop(TrkRecoTrk &, bool allowFlips, bool allowDrops) const
TrkRecoTrk * makeTrack(const TrkExchangePar &helix, const double chi2, const TrkContext &, double trackT0) const