00001 #include <iostream>
00002 #include "treematch.h"
00003 #include "treecombine.h"
00004
00005 #include <fstream>
00006 using namespace std;
00007
00013 extern Det *rcDETRegion[2][3][4];
00014
00015
00016
00017
00018
00019 double rcPEval( double vz, double te, double ph, double bend){
00020 cerr << "ERROR : THIS FUNCTION IS ONLY A STUB rcPEval " << endl;
00021 return -1000;
00022 }
00023
00024 double rcZEval( double vz, double te, double ph, double mom, int idx){
00025 cerr << "ERROR : THIS FUNCTION IS ONLY A STUB rcZEval " << endl;
00026 return -1000;
00027 }
00028
00029 Hit *bestWireHit(TreeLine *walk,double bestpos=0){
00030 double pos=9999,newpos;
00031 int ibest;
00032 for(int i =0;i<walk->numhits;i++){
00033 newpos = walk->hits[i]->rPos-bestpos;
00034 if(newpos < 0)newpos = -newpos;
00035 if(newpos < pos){
00036 pos = newpos;
00037 ibest = i;
00038 }
00039 }
00040 return walk->hits[ibest];
00041 }
00042
00043
00044 treematch::treematch(){
00045
00046 }
00047
00048 treematch::~treematch(){
00049
00050 }
00051
00052
00053
00054
00055 TreeLine *treematch::MatchR3(TreeLine *front,TreeLine *back,EUppLow up_low,ERegion region, Edir dir){
00056
00057
00058
00059
00060 TreeLine *combined,*fwalk,*bwalk;
00061 double x[2],y[2],z[3],zp[2];
00062 Hit *fpos,*bpos;
00063 double d,d2,d_uv,xp,B,A;
00064 double pi = acos(-1),theta;
00065 double wirespacingf,wirespacingb,d_to_1st_wire_f,d_to_1st_wire_b;
00066 Det *rd;
00067 int i,j,k;
00068 double slope,intercept,fslope,bslope;
00069 int numflines=0,numblines=0;
00070 double fsloperes,bsloperes;
00071 double bestmatch;
00072 TreeLine *lineptr;
00073 double mx,cx,cov[3],chi;
00074 int nhits,fhits,bhits;
00075 treecombine TreeCombine;
00076 Hit *DetecHits[2*TLAYERS];
00077
00078 ofstream gnu1,gnu2;
00079 gnu1.open("gnu1.dat");
00080 gnu2.open("gnu2.dat");
00081
00082
00083
00084 rd = rcDETRegion[up_low][region-1][dir];
00085 theta = rd->Rot/360*2*pi;
00086
00087 d_to_1st_wire_f = rd->rSin * rd->PosOfFirstWire;
00088
00089 d_to_1st_wire_f = d_to_1st_wire_f - rd->NumOfWires * rd->WireSpacing;
00090 wirespacingf = rd->WireSpacing;
00091 fsloperes = rd->SlopeMatching;
00092 x[0]= rd->center[0];
00093 y[0]= rd->center[1];
00094 z[0]= rd->Zpos;
00095 zp[0] = z[0]-y[0]/tan(theta);
00096
00097 rd = rd->nextsame;
00098 x[1]= rd->center[0];
00099 y[1]= rd->center[1];
00100 z[1]= rd->Zpos;
00101 zp[1] = z[1]-y[1]/tan(theta);
00102
00103 d = (zp[1]-zp[0])*sin(theta);
00104
00105 wirespacingb = rd->WireSpacing;
00106 bsloperes = rd->SlopeMatching;
00107
00108 d_to_1st_wire_b = rd->rSin * rd->PosOfFirstWire;
00109
00110 d_to_1st_wire_b = d_to_1st_wire_b - rd->NumOfWires * rd->WireSpacing;
00111
00112 if(dir == v_dir){
00113 d_uv = (rcDETRegion[up_low][region-1][v_dir]->Zpos-rcDETRegion[up_low][region-1][u_dir]->Zpos);
00114 d_uv*= rcDETRegion[up_low][region-1][u_dir]->rRotCos;
00115 }
00116
00117
00118
00119
00120
00121 z[2] = z[0]+d*sin(theta);
00122
00123 d2 = (y[1]-y[0])/sin(theta)+(zp[1]-zp[0])*cos(theta);
00124
00125
00126
00127
00128
00129 for(int i =0;i<282;i++){
00130 gnu2 << "0 " << d_to_1st_wire_f + i*wirespacingf << " " << d << " " << d_to_1st_wire_b + i*rd->WireSpacing +d2 << endl;
00131 }
00132 for(fwalk = front;fwalk;fwalk = fwalk->next){
00133 numflines++;
00134
00135 for(i=0;i<fwalk->numhits;i++){
00136 fwalk->hits[i]->Zpos = fwalk->hits[i]->wire * wirespacingf + d_to_1st_wire_f;
00137 if(dir == v_dir)fwalk->hits[i]->rPos+= d_uv;
00138 }
00139 }
00140 for(bwalk = back;bwalk;bwalk = bwalk->next){
00141 numblines++;
00142 for(i=0;i<bwalk->numhits;i++){
00143 bwalk->hits[i]->Zpos = (bwalk->hits[i]->wire - rd->NumOfWires) * wirespacingb + d_to_1st_wire_b + d2;
00144 bwalk->hits[i]->rPos = bwalk->hits[i]->rPos + d;
00145 if(dir == v_dir)bwalk->hits[i]->rPos+= d_uv;
00146 }
00147 }
00148 fwalk = front;
00149 bwalk = back;
00150
00151 for(i=0;i<fwalk->numhits;i++){
00152 gnu1 << fwalk->hits[i]->Zpos << " " << fwalk->hits[i]->rPos << endl;
00153 }
00154 for(i=0;i<bwalk->numhits;i++){
00155 gnu1 << bwalk->hits[i]->Zpos << " " << bwalk->hits[i]->rPos << endl;
00156 }
00157
00158
00159 int matches[numflines];
00160 int bmatches[numblines];
00161 double bestmatches[numblines];
00162
00163
00164
00165 for(i=0;i<numblines;i++)bestmatches[i]=99;
00166 i=0;
00167 for(fwalk = front;fwalk;fwalk = fwalk->next){
00168 matches[i]=-1;
00169 if(fwalk->isvoid == 0){
00170 fpos = bestWireHit(fwalk);
00171 j=0;
00172 bestmatch=99;
00173 for(bwalk = back;bwalk;bwalk = bwalk->next){
00174 j++;
00175 if(bwalk->isvoid !=0)continue;
00176 bpos = bestWireHit(bwalk,d);
00177
00178 y[0]=fpos->Zpos;
00179 y[1]=bpos->Zpos;
00180 x[0]=fpos->rPos;
00181 x[1]=bpos->rPos;
00182
00183 slope = (y[1]-y[0])/(x[1]-x[0]);
00184 intercept = y[1]-slope*x[1];
00185
00186 fslope = wirespacingf/fwalk->mx;
00187 bslope = wirespacingb/bwalk->mx;
00188 if(fabs(fslope-slope)<=fsloperes && fabs(bslope-slope)<=bsloperes
00189 && fabs(fslope-slope)+fabs(bslope-slope)<bestmatch){
00190 if(bestmatches[j-1]!=99){
00191 bestmatch = fabs(fslope-slope)+fabs(bslope-slope);
00192 if(bestmatch > bestmatches[j-1])continue;
00193 else matches[bmatches[j-1]]=-1;
00194 }
00195 matches[i]=j-1;
00196 bmatches[j-1]=i;
00197 bestmatch = bestmatches[j-1] = fabs(fslope-slope)+fabs(bslope-slope);
00198
00199
00200 }
00201
00202
00203
00204
00205
00206
00207 }
00208 }
00209 i++;
00210 }
00211
00212
00213
00214 lineptr = (TreeLine*)malloc(sizeof(TreeLine));
00215 assert(lineptr);
00216
00217 for(fwalk = front,i=0;fwalk;fwalk = fwalk->next,i++){
00218 for(bwalk = back,j=0;bwalk;bwalk = bwalk->next,j++){
00219 if(matches[i]==j){
00220
00221 fhits = fwalk->numhits;
00222 bhits = bwalk->numhits;
00223 for(k=0;k<fhits;k++){
00224 DetecHits[k]=fwalk->hits[k];
00225 lineptr->hits[k]=fwalk->hits[k];
00226 }
00227 for(k=0;k<bhits;k++){
00228 DetecHits[k+fhits]=bwalk->hits[k];
00229 lineptr->hits[k+fhits]=bwalk->hits[k];
00230 }
00231 nhits = fhits + bhits;
00232
00233
00234 for(k=0;k<nhits;k++){
00235
00236 }
00237
00238 TreeCombine.weight_lsq_r3(&mx,&cx,cov,&chi,DetecHits,nhits,0,-1,2*TLAYERS);
00239 lineptr->mx = mx;
00240 lineptr->cx = cx;
00241 lineptr->chi = chi;
00242 lineptr->numhits = nhits;
00243 lineptr->nummiss = 2*TLAYERS - nhits;
00244 lineptr->isvoid = false;
00245
00246
00247
00248 lineptr->next = combined;
00249 combined = lineptr;
00250 }
00251 }
00252 }
00253
00254
00255
00256
00257 gnu1.close();
00258 gnu2.close();
00259 combined->next = 0;
00260 return combined;
00261 }
00262
00263 void treematch::TgTrackPar( PartTrack *front, PartTrack *back,double *theta, double *phi, double *bending, double *ZVertex )
00264 {
00265 *theta = atan( front->mx);
00266 *phi = atan( front->my);
00267 if( bending && back )
00268 *bending = atan( back->mx) - *theta;
00269 *ZVertex = - ( ( front->mx * front->x + front->my * front->y )
00270 /( front->mx * front->mx + front->my * front->my));
00271 }
00272
00273
00274
00275 Track * treematch::TgPartMatch( PartTrack *front, PartTrack *back, Track *tracklist,
00276 enum EUppLow upplow)
00277 {
00278 double bestchi = 1e10, chi;
00279 double v1,v2, v3;
00280 Track *ret = 0, *newtrack = 0, *besttrack = 0, *trackwalk, *ytrackwalk;
00281 Bridge *bridge;
00282
00283 double theta, ZVertex, phi, bending, P;
00284
00285
00286
00287
00288 while( back) {
00289
00290 if( back->isvoid == false
00291
00292
00293
00294
00295
00296
00297
00298
00299 ) {
00300
00301 newtrack = new Track;
00302 assert(newtrack);
00303
00304
00305
00306 if( front->bridge )
00307 bridge = front->bridge;
00308 else if( back->bridge )
00309 bridge = back->bridge;
00310 else
00311 bridge = new Bridge;
00312 assert( bridge );
00313 TgTrackPar( front, back, &theta, &phi, &bending, &ZVertex);
00314
00315 ZVertex *= 0.01;
00316 if( ZVertex < DZA )
00317 ZVertex = DZA;
00318 if( ZVertex > DZA + DZW )
00319 ZVertex = DZA+DZW;
00320 P = rcPEval( ZVertex, theta, phi, bending);
00321 if( P > 0.0 ) {
00322 bridge->Momentum = P;
00323 if( bending < 0 )
00324 P = -P;
00325 bridge->xOff = rcZEval( ZVertex, theta, phi, P, 0);
00326 bridge->yOff = rcZEval( ZVertex, theta, phi, P, 1);
00327 bridge->ySOff = rcZEval( ZVertex, theta, phi, P, 2);
00328 } else
00329 bridge->Momentum = 0.0;
00330 bridge->ySlopeMatch = back->my - front->my;
00331 bridge->xMatch = v2 = back->x - front->x + bridge->xOff;
00332 bridge->yMatch = v1 = back->y - front->y + bridge->yOff;
00333 bridge->ySMatch = v3 = back->my - front->my + bridge->ySOff;
00334
00335
00336 double rcSET_rMagnMatchYSl0 = 0.03;
00337 double rcSET_rMagnMatchY0 = 1.0;
00338 double rcSET_rMagnMatchX0 = 3.0;
00339 cerr << "ERROR : INVALID MAGNETIC FIELD VALUES USED " << endl;
00340
00341 if( fabs(v3) > rcSET_rMagnMatchYSl0)
00342 v3 = 1e12;
00343
00344 if( fabs( v2) > rcSET_rMagnMatchY0 ||
00345 fabs( v1) > rcSET_rMagnMatchX0 )
00346 v1 = v2 = 1e12;
00347
00348
00349 newtrack->bridge = bridge;
00350
00351 newtrack->front = front;
00352 newtrack->back = back;
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362 chi = v1 * v1 + v2 * v2 + v3 * v3;
00363 newtrack->chi = sqrt(chi + front->chi*front->chi + back->chi*back->chi);
00364 if(bestchi > chi) {
00365 besttrack = newtrack;
00366 bestchi = chi;
00367 }
00368 newtrack->ynext = ret;
00369 ret = newtrack;
00370 }
00371 back = back->next;
00372 }
00373
00374
00375 if( besttrack ) {
00376 int mtch = 1;
00377 besttrack->Used = true;
00378 besttrack->front->Used = true;
00379 besttrack->back->Used = true;
00380 double rcSET_rXSlopeSep = 0.01;
00381 cerr << "Error : bogus value used" << endl;
00382 for( trackwalk = ret; trackwalk ; trackwalk = trackwalk->next ) {
00383 if( fabs( newtrack->back->mx - besttrack->back->mx ) > rcSET_rXSlopeSep )
00384 mtch ++;
00385 }
00386 besttrack->yTracks = mtch;
00387
00388
00389
00390
00391
00392
00393 }
00394
00395
00396 for( newtrack = ret; newtrack; newtrack = newtrack->ynext ) {
00397 if( !newtrack->Used )
00398 continue;
00399 for( trackwalk = tracklist; trackwalk; trackwalk = trackwalk->next ) {
00400 for( ytrackwalk = trackwalk; ytrackwalk;
00401 ytrackwalk = ytrackwalk->ynext ) {
00402
00403
00404
00405
00406 if(ytrackwalk->Used && ytrackwalk->back == newtrack->back ) {
00407 if( ytrackwalk->chi > newtrack->chi ) {
00408 ytrackwalk->Used = 0;
00409 } else {
00410 newtrack->Used = 0;
00411 }
00412 }
00413 }
00414 }
00415 }
00416
00417 return ret;
00418 }
00419
00420