21 #include "l3CoordinateTransformer.h"
27 #include <sys/types.h>
35 l3CoordinateTransformer::l3CoordinateTransformer()
41 transformation_errors =0;
44 Set_parameters_by_hand() ;
55 l3CoordinateTransformer::~l3CoordinateTransformer()
63 if(TPCmap) free(TPCmap);
69 int l3CoordinateTransformer::LoadTPCLookupTable(
char *)
95 void *file = (
void *)mapdotbinfile;
98 int type = ((
int *)file)[0];
99 if ((type != 3) && (type != 100)) {
101 LOG(ERR,
"No valid map found header file",0,0,0,0,0);
105 int headerSize = ((
int *)file)[1];
107 dpad = ((
float *)file)[3];
108 dtb = ((
float *)file)[4];
109 maxtb = ((
float *)file)[5];
111 npad = (int) ceil( 182. / dpad);
112 ntb = (int) ceil( maxtb / dtb);
114 TPCmap = (
float *)malloc( MAPDOTBINFILE_SZ - headerSize*4);
115 if (TPCmap == NULL) {
116 LOG(ERR,
"Cannot allocate memory for lookup table.\n");
118 }
else if (TPCmap == (
float*)-1) {
119 LOG(ERR,
"TPCmap=-1. What's that??? 0x%x", TPCmap);
122 memcpy(TPCmap, ((
float *)file)+headerSize, MAPDOTBINFILE_SZ - headerSize*4);
132 raw_to_local(raw, local);
133 local_to_global(raw, local, global);
192 double pitch = (raw.Getr()<14) ? innerSectorPadPitch : outerSectorPadPitch;
193 double pads2move = raw.Getp() - numberOfPadsAtRow[(int)raw.Getr()-1]/2;
194 local.Setx( pitch * (pads2move - 0.5) ) ;
195 local.Sety( radialDistanceAtRow[(
int)(raw.Getr())-1] ) ;
198 if( raw.Getr() <= 13 )
200 local.Setz(drift_length_inner - raw.Gett() * lengthPerTb) ;
201 if (raw.Gett()>max_tb_inner) {
202 transformation_errors++;
207 local.Setz(drift_length_outer - raw.Gett() * lengthPerTb) ;
208 if (raw.Gett()>max_tb_outer) {
209 transformation_errors++;
218 int sector = (int) raw.Gets();
226 double x = SectorCos[sector-1] * local.Getx() + SectorSin[sector-1] * local.Gety();
227 if (sector>12) x = -x;
228 double y = -1.* SectorSin[sector-1] * local.Getx() +
229 SectorCos[sector-1] * local.Gety();
230 double z = (sector<13) ? local.Getz() : -local.Getz() ;
232 global.Setxyz(x,y,z);
236 void l3CoordinateTransformer::global_to_raw(
const l3xyzCoordinate &global,
240 global_to_local( global, local, raw ) ;
243 local_to_raw( global ,local ,raw ) ;
246 void l3CoordinateTransformer::global_to_raw(
int sector,
int row,
251 global_to_local( sector, row, global, local ) ;
252 local_to_raw( row ,local ,raw ) ;
255 void l3CoordinateTransformer::global_to_local(
const l3xyzCoordinate &global,
262 double y = global.Gety() ;
263 if (y== 0) y= 0.000000001;
269 local.Setz(global.Getz());
273 x = -(global.Getx()) ;
274 local.Setz(-(global.Getz()));
278 double pi = 3.14159265358979323846;
279 double sec_border = tan(pi/12) ;
280 double turn_angle = -pi/6 ;
281 double sin_turn_angle = sin(turn_angle);
282 double cos_turn_angle = cos(turn_angle);
285 if (y>=0 && fabs(x/y)<=sec_border)
293 while( y<0 || (fabs(x/y)>sec_border))
295 double xn = x*cos_turn_angle + y*sin_turn_angle ;
296 double yn = -x*sin_turn_angle + y*cos_turn_angle ;
318 void l3CoordinateTransformer::global_to_local(
int sector,
int row,
329 double xGlobal = global.Getx();
330 if (sector>12) xGlobal = -xGlobal;
332 double x = SectorCos[sector-1] * xGlobal - SectorSin[sector-1] * global.Gety() ;
333 double y = SectorSin[sector-1] * xGlobal + SectorCos[sector-1] * global.Gety() ;
334 double z = fabs(global.Getz());
341 void l3CoordinateTransformer::local_to_raw(
const l3xyzCoordinate &global,
346 double y = local.Gety() ;
349 while( (fabs(radialDistanceAtRow[row_index]-y) > 0.5) && (row_index < 46) )
353 if (row_index==45 || y<59)
366 double x = local.Getx();
367 double pitch = (row<=13) ? innerSectorPadPitch : outerSectorPadPitch ;
368 int half_num_pads_this_row = numberOfPadsAtRow[row-1]/2 ;
369 double pad = half_num_pads_this_row + x/pitch + 0.5 ;
373 double z = local.Getz();
376 bucket = ( drift_length_inner - z )/lengthPerTb ;
377 if (z>drift_length_inner) {
378 transformation_errors++;
383 bucket = ( drift_length_outer - z )/lengthPerTb ;
384 if (z>drift_length_outer) {
385 transformation_errors++;
395 void l3CoordinateTransformer::local_to_raw(
int row ,
400 double x = local.Getx();
401 double pitch = (row<=13) ? innerSectorPadPitch : outerSectorPadPitch ;
402 int half_num_pads_this_row = numberOfPadsAtRow[row-1]/2 ;
403 double pad = half_num_pads_this_row + x/pitch + 0.5 ;
407 double z = local.Getz();
410 bucket = ( drift_length_inner - z )/lengthPerTb ;
411 if (z>drift_length_inner) {
412 transformation_errors++;
417 bucket = ( drift_length_outer - z )/lengthPerTb ;
418 if (z>drift_length_outer) {
419 transformation_errors++;
429 void l3CoordinateTransformer::Set_parameters_by_hand(
const double mlengthPerTb,
430 const double mdrift_length_inner,
431 const double mdrift_length_outer)
433 lengthPerTb = mlengthPerTb ;
434 drift_length_inner = mdrift_length_inner ;
435 drift_length_outer = mdrift_length_outer ;
438 max_tb_inner = drift_length_inner/lengthPerTb;
439 max_tb_outer = drift_length_outer/lengthPerTb;
444 void l3CoordinateTransformer::Use_transformation_provided_by_db()
455 double tau3 = 3 * (gStTpcDb->Electronics()->tau() * 1e-09)
456 * (gStTpcDb->DriftVelocity()) ;
462 padco.setTimeBucket(100);
465 double z_100 = glo.position().z();
466 padco.setTimeBucket(0);
468 double z_0 = glo.position().z();
469 double lengthPerTb_inner = fabs((z_0-z_100)/100) ;
470 drift_length_inner = fabs(glo.position().z()+tau3);
480 padco.setTimeBucket(100);
482 z_100 = glo.position().z();
483 padco.setTimeBucket(0);
485 z_0 = glo.position().z();
486 double lengthPerTb_outer = fabs((z_0-z_100)/100) ;
487 drift_length_outer = fabs(glo.position().z()+tau3) ;
494 if (lengthPerTb_outer == lengthPerTb_inner )
496 lengthPerTb = lengthPerTb_outer ;
500 cerr <<
"Different driftvelocities in TPC observed -> Set to 0. " << endl;
506 max_tb_inner = drift_length_inner/lengthPerTb;
507 max_tb_outer = drift_length_outer/lengthPerTb;
510 cout <<
"This is not functional online.\n" ;
517 void l3CoordinateTransformer::Get_parameters_from_db()
521 double driftvelocity = gStTpcDb->DriftVelocity();
522 double inner_effective_driftlength =
523 gStTpcDb->Dimensions()->innerEffectiveDriftDistance();
524 double outer_effective_driftlength =
525 gStTpcDb->Dimensions()->outerEffectiveDriftDistance();
526 double gatingrid = gStTpcDb->Dimensions()->gatingGridZ();
527 double t0pad = gStTpcDb->T0(1)->getT0(1,1);
528 double zinneroffset = gStTpcDb->Dimensions()->zInnerOffset();
529 double zouteroffset = gStTpcDb->Dimensions()->zOuterOffset();
530 double frequency = gStTpcDb->Electronics()->samplingFrequency();
531 double tzero = gStTpcDb->Electronics()->tZero();
532 double tau = gStTpcDb->Electronics()->tau();
533 double shapingtime = gStTpcDb->Electronics()->shapingTime();
537 cout <<
"Fresh from the db we got : \n" ;
538 cout <<
"The driftvelocity : " << driftvelocity << endl;
539 cout <<
"The effective innerdrift (not used) : "
540 << inner_effective_driftlength << endl ;
541 cout <<
"The effective outerdrift (not used) : "
542 << outer_effective_driftlength << endl ;
543 cout <<
"Innerzoffset : " << zinneroffset << endl ;
544 cout <<
"Outerzoffset : " << zouteroffset << endl ;
545 cout <<
"Gatinggrid : " << gatingrid << endl ;
546 cout <<
"t0pad : " << t0pad << endl ;
547 cout <<
"The tzero : " << tzero << endl;
548 cout <<
"Frequency ist set to : " << frequency << endl;
549 cout <<
"tau : " << tau << endl ;
550 cout <<
"shapingtime (not used) : " << shapingtime << endl ;
554 lengthPerTb = 1 / frequency / (1E6) * driftvelocity ;
560 drift_length_inner = gatingrid-driftvelocity*1e-6*(-tzero+0.5/frequency)+
561 zinneroffset+t0pad+3*tau*1e-09*driftvelocity;
562 drift_length_outer = gatingrid-driftvelocity*1e-6*(-tzero+0.5/frequency)+
563 zouteroffset+t0pad+3*tau*1e-09*driftvelocity;
568 cout <<
"inner drift length : " << drift_length_inner << endl;
569 cout <<
"outer drift length : " << drift_length_outer << endl;
570 cout <<
"lengthPerTb : " << lengthPerTb << endl;
574 max_tb_inner = drift_length_inner/lengthPerTb;
575 max_tb_outer = drift_length_outer/lengthPerTb;
578 cout <<
"This is not functional online.\n" ;
585 void l3CoordinateTransformer::Print_parameters()
587 cout <<
"l3CoordinateTransformer: Parameters used <== " << endl ;
588 cout <<
" Used parameters : " << endl ;
589 cout <<
" Length per tb : " << lengthPerTb << endl ;
590 cout <<
" drift_length_inner: " << drift_length_inner << endl ;
591 cout <<
" drift_length_outer: " << drift_length_outer << endl ;
592 cout <<
" max_tb_inner : " << max_tb_inner << endl ;
593 cout <<
" max_tb_outer : " << max_tb_outer << endl ;
600 double l3CoordinateTransformer::outerSectorPadPitch = 0.67;
601 double l3CoordinateTransformer::innerSectorPadPitch = 0.335;
604 int l3CoordinateTransformer::numberOfPadsAtRow[] = {
605 88,96,104,112,118,126,134,142,150,158,166,174,182,
606 98,100,102,104,106,106,108,110,112,112,114,116,
607 118,120,122,122,124,126,128,128,130,132,134,136,
608 138,138,140,142,144,144,144,144
611 double l3CoordinateTransformer::radialDistanceAtRow[] = {
612 60.0, 64.8, 69.6, 74.4, 79.2, 84.0, 88.8, 93.60,
613 98.8, 104., 109.20, 114.4, 119.6,
614 127.195, 129.195, 131.195, 133.195, 135.195,
615 137.195, 139.195, 141.195, 143.195, 145.195,
616 147.195, 149.195, 151.195, 153.195, 155.195,
617 157.195, 159.195, 161.195, 163.195, 165.195,
618 167.195, 169.195, 171.195, 173.195, 175.195,
619 177.195, 179.195, 181.195, 183.195, 185.195,
623 double l3CoordinateTransformer::SectorSin[] = {
638 double l3CoordinateTransformer::SectorCos[] = {