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