00001 #include <math.h>
00002 #include "dio_trk.hh"
00003 #include "mEmcGeaMakeRaw.h"
00004 #include "emlLib.h"
00005
00016 long type_of_call mEmcGeaMakeRaw_(
00017 TABLE_HEAD_ST *header_h, HEADER_ST *header ,
00018 TABLE_HEAD_ST *dEmcGeaHit_h, DEMCGEAHIT_ST *dEmcGeaHit ,
00019 TABLE_HEAD_ST *dEmcGeaParams_h, DEMCGEAPARAMS_ST *dEmcGeaParams ,
00020 TABLE_HEAD_ST *dEmcRespPar_h, DEMCRESPPAR_ST *dEmcRespPar ,
00021 TABLE_HEAD_ST *dEmcGeometry_h, DEMCGEOMETRY_ST *dEmcGeometry ,
00022 TABLE_HEAD_ST *dEmcGeaTrackTower_h, DEMCGEATRACKTOWER_ST *dEmcGeaTrackTower ,
00023 TABLE_HEAD_ST *dEmcGeaTowerTrack_h, DEMCGEATOWERTRACK_ST *dEmcGeaTowerTrack ,
00024 TABLE_HEAD_ST *dEmcRawData_h, DEMCRAWDATA_ST *dEmcRawData )
00025 {
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049 #define max(x,y) ( ( (x) > (y) ) ? (x) : (y) )
00050 #define min(x,y) ( ( (x) < (y) ) ? (x) : (y) )
00051
00052 int tempsize;
00053
00054 #ifndef TRUE
00055 #define TRUE 1
00056 #endif
00057
00058 #ifndef FALSE
00059 #define FALSE 0
00060 #endif
00061
00062
00063
00064 static int l_first = TRUE;
00065
00066
00067
00068 static int iwall;
00069 static int itype,iarm;
00070 static float dele;
00071 static float pos_x;
00072 static float pos_y;
00073 static float pos_z;
00074 static float tof;
00075 static int i_index[3];
00076 static int numed;
00077
00078
00079
00080 static float r_tdecay;
00081 static float r_ttune;
00082 static int i_tdelay;
00083 static float r_tfac;
00084 static float r_thresh;
00085 static float r_tmaxval;
00086
00087
00088
00089 static float r_slew_e;
00090 static float r_meas_t;
00091 static float r_slew_t;
00092
00093 #define p_timemax 1000
00094
00095 static float r_timeoffset = 17.0;
00096 static float r_talpha;
00097 static float r_t;
00098 static float r_speedoflight = 30.0;
00099 static float r_earliest_valid_tof;
00100 static float r_timethresh = 0.05;
00101
00102
00103
00104 static int emc_debug;
00105 static float rproj;
00106 static float r_distance;
00107 static float dele_star;
00108 static float tof_star;
00109 static int iz_off;
00110 static int iy_off;
00111 static int iz_rel;
00112 static int iy_rel;
00113
00114
00115
00116
00117
00118
00119 static int l_correct_unif = FALSE;
00120 static float emc_pos_unif[24][24];
00121 static float r_cellsize;
00122 static float r_disty,r_distz;
00123 static float r_centx,r_centy,r_centz;
00124 static float ra_unif_par[4] = {1.0, -0.4, 0.0, -1.5 };
00125
00126 static int i_tofindex;
00127 static int l_plotted = FALSE;
00128 static float r_work;
00129 static float r_work1;
00130 static float r_work2;
00131 static float r_work3;
00132 static float pedestal_emc = 100.0;
00133 static float r_timebin = 0.05;
00134
00135 #define max_chanz 96
00136 #define max_chany 48
00137 #define i_detmax 8
00138
00139 static float emc_dele_cal[i_detmax][max_chany][max_chanz];
00140 static float emc_tof[i_detmax][max_chany][max_chanz];
00141 static float emc_tof_first[i_detmax][max_chany][max_chanz];
00142
00143 static float emc_geom[i_detmax][max_chany][max_chanz][3];
00144
00145
00146
00147
00148
00149 static float emc_parent[i_detmax][max_chany][max_chanz][4][3];
00150
00151 static int i_lopre,i_hipre,i_lopost,i_hipost,i_tof;
00152
00153 static float ra_det[120][8];
00154
00155 static int i_gtotr_id;
00156
00157 static int nfile,isubevt;
00158 static int itrack_prev,isubevt_prev,nfile_prev;
00159 static float r_lowgain_convfac;
00160 static float r_highgain_convfac;
00161 static float r_tdc_convfac;
00162 static int i_low_ped;
00163 static int i_high_ped;
00164 #define r_lowgain_convfac 0.001
00165 #define r_highgain_convfac 0.008
00166 #define r_tdc_convfac 0.05
00167
00168
00169
00170
00171
00172 #define i_low_ped 4000
00173 #define i_high_ped 4000
00174 #define i_minvalue 100
00175
00176
00177
00178
00179 static int p_maxtimebin;
00180
00181
00182 #define p_maxtimebin 1200
00183 static float ra_pulse[i_detmax][max_chany][max_chanz][p_maxtimebin];
00184 static float ra_pulserecon[2*p_maxtimebin];
00185
00186 static int idpart,itrack;
00187
00188 static int i,j,k,k1;
00189 static int i1;
00190 static int iy,iz;
00191
00192 static int l_found;
00193
00194 static int l_continue;
00195 static int is1,is2,i_work;
00196
00197 static int istaf,iout_ok;
00198
00199 #define p_maxtowerhit 30
00200
00201
00202
00203
00204 static int i_twrkey;
00205 static int i_gtrto_id;
00206 static int i_last_trkno;
00207 static int ia_twrkey[p_maxtowerhit];
00208 static float ra_edep[p_maxtowerhit];
00209
00210 int l_last_hit;
00211
00212 static int true_track;
00213 static int true_track_current;
00214 static int true_track_prev;
00215
00216 static float p_gatemax;
00217 #define p_gatemax 127.0
00218
00219 double d_work,d_work1,d_work2,d_work3,d_work4;
00220
00221 float xyz[3],newxyz[3];
00222
00223 long ismodz,ismody,irelz,irely,ismoffset,isector,ihwkey;
00224 long itower;
00225
00226 static int sim_timing = 0;
00227 static int pbgl_response = 0;
00228 static float pbgl_resolution = 0.06;
00229 static float pbgl_noise = 0.01;
00230 static float pbgl_tofresolution = 0.25;
00231 static float pbgl_tofoffset = 2.5;
00232 static float pbgl_rescale = 1.18;
00233
00234
00235
00236
00237
00238
00239 static float pbgl_ctrk_rescale = 1.0;
00240
00241 static float noise = 0.0;
00242 static int n_len = 1;
00243
00244 static float pbsc_noise = 0.003;
00245
00246 static int next_tower = 0;
00247
00248
00249
00250 float r_etracking,r_eexp,r_porig;
00251
00252 int status;
00253 float ptot,ptheta,pphi,r_vertex,z_vertex,theta_vertex,phi_vertex;
00254 int itparent,idparent;
00255
00256
00257
00258
00259
00260
00261 int isubevent;
00262 int ntrack;
00263 int error;
00264 int ll;
00265
00266
00267
00268 static float r_e_cutoff = 0.00001;
00269
00270
00271
00272
00273
00274 static float r_e_rescale = 1.08;
00275 static float r_dele_small = 0.0005;
00276 static float r_dele_max = 0.002;
00277
00278 float dele_sum;
00279 int i_key[2];
00280
00281
00282
00283
00284
00285
00286
00287 if(l_first)
00288 {
00289 l_first = FALSE;
00290
00291 if(dEmcRespPar_h->nok > 0)
00292 {
00293 if(dEmcRespPar[0].anyset == 1)
00294 {
00295 sim_timing = dEmcRespPar[0].sim_timing;
00296 pbgl_response = dEmcRespPar[0].pbgl_response;
00297 }
00298 }
00299
00300 if(dEmcGeaParams_h->nok <= 0)
00301 {
00302 printf(" error in memcgeamakeraw: missing params \n");
00303 return ( STAFCV_OK );
00304 }
00305 for ( j = 0; j< dEmcGeaParams_h->nok; j++ )
00306 {
00307 for ( i = 0; i < 120; i++ )
00308 {
00309 ra_det[i][j] = dEmcGeaParams[j].detarray[i];
00310 }
00311 }
00312
00313
00314
00315
00316
00317 if(ra_det[79][6] == 1.0)
00318 {
00319 pbgl_response = 3;
00320 }
00321
00322 for (ll = 0; ll < sizeof(emc_geom)/sizeof(emc_geom[0][0][0][0]); ll++)
00323 {
00324 *((float *)emc_geom + ll) = 0.0;
00325 }
00326 for ( j = 0; j < dEmcGeometry_h->nok; j++ )
00327 {
00328
00329
00330
00331
00332
00333 iz = dEmcGeometry[j].ind[0];
00334 iy = dEmcGeometry[j].ind[1];
00335 if(dEmcGeometry[j].arm == 0)
00336 {
00337 i1 = dEmcGeometry[j].sector;
00338 }
00339 else
00340 {
00341 i1 = 7 - dEmcGeometry[j].sector;
00342 }
00343
00344 emc_geom[i1][iy][iz][0] = dEmcGeometry[j].nomxyz[0];
00345 emc_geom[i1][iy][iz][1] = dEmcGeometry[j].nomxyz[1];
00346 emc_geom[i1][iy][iz][2] = dEmcGeometry[j].nomxyz[2];
00347 }
00348 }
00349
00350
00351
00352 for (ll = 0; ll < sizeof(emc_dele_cal)/sizeof(emc_dele_cal[0][0][0]);
00353 ll++)
00354 {
00355 *((float *)emc_dele_cal + ll) = 0.0;
00356 }
00357 for (ll = 0; ll < sizeof(emc_tof)/sizeof(emc_tof[0][0][0]); ll++)
00358 {
00359 *((float *)emc_tof + ll) = 0.0;
00360 }
00361 for (ll = 0; ll < sizeof(emc_tof_first)/sizeof(emc_tof_first[0][0][0]);
00362 ll++)
00363 {
00364 *((float *)emc_tof_first + ll) = 0.0;
00365 }
00366 for (ll = 0; ll < sizeof(ra_pulse)/sizeof(ra_pulse[0][0][0][0]); ll++)
00367 {
00368 *((float *)ra_pulse + ll) = 0.0;
00369 }
00370 for (ll = 0; ll < sizeof(emc_parent)/sizeof(emc_parent[0][0][0][0][0]); ll++)
00371 {
00372 *((float *)emc_parent + ll) = 0.0;
00373 }
00374 for (ll = 0; ll < sizeof(ia_twrkey)/sizeof(ia_twrkey[0]); ll++)
00375 {
00376 *(ia_twrkey + ll) = 0;
00377 }
00378 for (ll = 0; ll < sizeof(ra_edep)/sizeof(ra_edep[0]); ll++)
00379 {
00380 *(ra_edep + ll) = 0.0;
00381 }
00382 i_gtrto_id = 0;
00383 i_gtotr_id = 0;
00384 l_last_hit = FALSE ;
00385
00386 itrack_prev = 0;
00387 isubevt_prev = 0;
00388 nfile_prev = 0;
00389
00390 true_track_prev = 0;
00391
00392
00393
00394
00395
00396
00397
00398 if(dEmcGeaHit_h->nok <= 0)
00399 {
00400
00401 return ( STAFCV_OK );
00402 }
00403
00404 i_key[0] = -1;
00405 i_key[1] = -1;
00406 dele_sum = 0.0;
00407
00408 for ( istaf = 0;istaf < dEmcGeaHit_h->nok; istaf++ )
00409 {
00410
00411
00412
00413
00414 i1 = dEmcGeaHit[istaf].sector -1;
00415 iwall = dEmcGeaHit[istaf].sector -1;
00416 itype = dEmcGeaHit[istaf].type;
00417 dele = dEmcGeaHit[istaf].deltae;
00418 pos_x = dEmcGeaHit[istaf].xyz[0];
00419 pos_y = dEmcGeaHit[istaf].xyz[1];
00420 pos_z = dEmcGeaHit[istaf].xyz[2];
00421 tof = dEmcGeaHit[istaf].tof;
00422 i_index[0] = dEmcGeaHit[istaf].smodind;
00423 i_index[1] = dEmcGeaHit[istaf].towerind;
00424 numed = dEmcGeaHit[istaf].numed;
00425 idpart = dEmcGeaHit[istaf].partid;
00426 itrack = dEmcGeaHit[istaf].itrack;
00427 isubevt = dEmcGeaHit[istaf].isubevt;
00428 nfile = dEmcGeaHit[istaf].nfile;
00429 true_track_current = dEmcGeaHit[istaf].true_track;
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450 if( true_track_prev == 0)
00451 {
00452 true_track_prev = true_track_current;
00453 l_found = FALSE;
00454 xyz[0] = pos_x;
00455 xyz[1] = pos_y;
00456 xyz[2] = pos_z;
00457 }
00458
00459
00460
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470 if (istaf < dEmcGeaHit_h->nok - 1) {
00471 next_tower = 100000*(dEmcGeaHit[istaf+1].sector)
00472 +1000*(dEmcGeaHit[istaf+1].smodind)
00473 +(dEmcGeaHit[istaf+1].towerind);
00474 }
00475
00476
00477
00478 if( true_track_prev != true_track_current) l_found = FALSE;
00479
00480 if(istaf == dEmcGeaHit_h->nok - 1 ) l_last_hit = TRUE ;
00481
00482
00483
00484
00485
00486
00487 if((l_found != 0 && l_last_hit == 0)&&(i1<6))
00488 {
00489 if(dEmcGeaHit[istaf].tof > p_gatemax) goto out_of_gate;
00490 if(dEmcGeaHit[istaf].deltae < r_e_cutoff) goto out_of_gate;
00491 }
00492
00493 i_key[0] = 100000 * (i1 + 1) + 1000 * i_index[0] + i_index[1];
00494 if( i_key[1] == -1) i_key[1] = i_key[0];
00495
00496
00497
00498
00499
00500 if((i_key[0] == i_key[1])&&(next_tower == i_key[0])&&(l_last_hit==0))
00501 {
00502 if( dele <= r_dele_small )
00503 {
00504 if(dele_sum <= r_dele_max)
00505 {
00506 dele_sum = dele_sum + dele;
00507 if(l_found != 0 && l_last_hit == 0) goto out_of_gate;
00508 }
00509 else
00510 {
00511 dele = dele + dele_sum;
00512 dele_sum = 0.0;
00513 }
00514 }
00515 else
00516 {
00517 dele = dele + dele_sum;
00518 dele_sum = 0.0;
00519 }
00520 }
00521 else
00522 {
00523 dele = dele + dele_sum;
00524 dele_sum = 0.0;
00525
00526
00527
00528 i_key[1] = next_tower;
00529 }
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556 if( l_found == 0)
00557 {
00558 l_found = TRUE;
00559 true_track = true_track_current;
00560 true_track_prev = true_track_current;
00561 newxyz[0] = pos_x;
00562 newxyz[1] = pos_y;
00563 newxyz[2] = pos_z;
00564 }
00565
00566
00567
00568 if( l_found == 0)
00569 {
00570 printf ("Error in mEmcGeaMakeRaw: True track not found \n");
00571 }
00572
00573 if(i_gtrto_id == 0)
00574 {
00575 i_gtrto_id = 1;
00576 i_last_trkno = true_track;
00577 }
00578
00579 if(istaf == dEmcGeaHit_h->nok - 1 ) l_last_hit = TRUE ;
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593 if(dele >= r_dele_small)
00594 {
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607 if( i1 > 5 && pbgl_response == 2)
00608 {
00609 dele_star = dele;
00610 tof_star = tof;
00611 }
00612
00613 else
00614 if( i1 > 5 && pbgl_response == 3)
00615 {
00616 dele_star = dele;
00617 tof_star = tof;
00618 }
00619 else
00620 {
00621
00622 d_work1 = pos_x * ra_det[80][i1];
00623 d_work2 = pos_y * ra_det[81][i1];
00624 d_work = fabs(d_work1) + fabs(d_work2);
00625 rproj = d_work;
00626
00627
00628
00629
00630
00631 r_distance =
00632 max(0.0, ra_det[5][i1] + ra_det[8][i1] +
00633 fabs(ra_det[14][i1]) - rproj);
00634 r_work = ( - r_distance / ra_det[82][i1] );
00635 dele_star = dele * exp (r_work);
00636 tof_star = tof + r_distance / ra_det[83][i1];
00637 }
00638
00639 }
00640 else
00641 {
00642 dele_star = dele;
00643 tof_star = 0.0;
00644 }
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694 switch(itype)
00695 {
00696
00697
00698 case 1:
00699
00700 iz_off = ( (i_index[0] - 1) /
00701 ( int ) ra_det[13][i1] ) * ( int ) ra_det[10][i1];
00702 iy_off = ( (i_index[0]-1) %
00703 ( int ) ra_det[13][i1] ) * ( int ) ra_det[11][i1];
00704
00705
00706 iz_rel = 1 + (i_index[1] - 1) / ra_det[11][i1];
00707 iy_rel = i_index[1] - (iz_rel - 1) * ( int ) (ra_det[11][i1]);
00708
00709 iz = iz_rel + iz_off -1;
00710 iy = iy_rel + iy_off -1;
00711
00712
00713
00714 if(i1 < 4 && iz <= 71) iz = 71 - iz;
00715
00716 if(iz <= -1 || iy <= -1) printf ("wrong indices %d %d \n",iz,iy );
00717 if(itype == 1 && (iz > 71 || iy > 35)) printf ("wrong indices %d %d \n",iz,iy );
00718
00719 break;
00720
00721
00722 case 2:
00723 iz_off = ( (i_index[0] - 1) /
00724 ( int ) ra_det[13][i1] ) * ( int ) ra_det[10][i1];
00725 iy_off = ( (i_index[0]-1) %
00726 ( int ) ra_det[13][i1] ) * ( int ) ra_det[11][i1];
00727
00728
00729 iy_rel = 1 + (i_index[1] - 1) / ra_det[10][i1];
00730 iz_rel = i_index[1] - (iy_rel - 1) * ra_det[10][i1];
00731
00732 iz = iz_rel + iz_off -1;
00733 iy = iy_rel + iy_off -1;
00734
00735
00736
00737 if(i1 < 4 && iz <= 71) iz = 71 - iz;
00738
00739 if(iz <= -1 || iy <= -1) printf ("wrong indices %d %d \n",iz,iy );
00740 if(itype == 1 && (iz > 71 || iy > 35)) printf ("wrong indices %d %d \n",iz,iy );
00741
00742 break;
00743
00744 }
00745
00746
00747
00748 if(dele > 0.0)
00749 {
00750
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760
00761 if(l_correct_unif && itype == 1)
00762 {
00763 r_centx = (pos_x - emc_geom[i1][iy][iz][0]);
00764
00765 if(ra_det[80][i1] != 0.0)
00766 {
00767 r_centy = emc_geom[i1][iy][iz][1] +
00768 ra_det[81][i1] * r_centx / ra_det[80][i1];
00769 }
00770 else
00771 {
00772 r_centy = emc_geom[i1][iy][iz][1];
00773 }
00774
00775 r_centz = emc_geom[i1][iy][iz][2];
00776 r_disty = pos_y - r_centy;
00777 r_distz = pos_z - r_centz;
00778
00779 j = 12 + r_disty * 24.0 / r_cellsize;
00780 k = 12 + r_distz * 24.0 / r_cellsize;
00781
00782 j = max(1,min(j,24)) -1;
00783 k = max(1,min(k,24)) -1;
00784
00785 dele_star = dele_star * emc_pos_unif[j][k];
00786 }
00787
00788
00789
00790
00791
00792
00793
00794
00795 if ( i1 > 5) dele_star = dele;
00796
00797 if(i1 < 4)
00798 {
00799 i_twrkey = 10000 * i1 + 100 * iy + iz;
00800 }
00801 else
00802 {
00803 i_twrkey = 100000 + 10000 * (7 - i1) + 100 * iy + iz;
00804 }
00805
00806
00807
00808 while ( l_last_hit || i_last_trkno != true_track )
00809 {
00810
00811
00812
00813
00814
00815
00816
00817
00818
00819 l_continue = TRUE;
00820 is1 = 0;
00821 while( l_continue && is1 < p_maxtowerhit)
00822 {
00823 l_continue = FALSE;
00824 is1 = is1 + 1;
00825 for(is2 = 0; is2 < p_maxtowerhit-2; is2++)
00826 {
00827 if( ia_twrkey[is2] > 0 )
00828 {
00829 if( ra_edep[is2] < ra_edep[is2+1] )
00830 {
00831 l_continue = TRUE;
00832 i_work = ia_twrkey[is2];
00833 r_work1 = ra_edep[is2];
00834 ia_twrkey[is2] = ia_twrkey[is2+1];
00835 ra_edep[is2] = ra_edep[is2+1];
00836 ia_twrkey[is2+1] = i_work;
00837 ra_edep[is2+1] = r_work1;
00838 }
00839 }
00840 }
00841 }
00842
00843 k = 0;
00844 while(ia_twrkey[k] > 0 && k < p_maxtowerhit)
00845 {
00846
00847
00848 dEmcGeaTrackTower[i_gtrto_id-1].id = i_gtrto_id;
00849 dEmcGeaTrackTower[i_gtrto_id-1].trkno = i_last_trkno;
00850 dEmcGeaTrackTower[i_gtrto_id-1].input = 1;
00851
00852 for ( k1 = 0; k1 < 3; k1++ )
00853 {
00854 dEmcGeaTrackTower[i_gtrto_id-1].xyz[k1] = xyz[k1];
00855
00856 dEmcGeaTrackTower[i_gtrto_id-1].twrkey[k1] = ia_twrkey[k+k1];
00857
00858
00859
00860
00861
00862
00863
00864 dEmcGeaTrackTower[i_gtrto_id-1].edep[k1] =
00865 ra_edep[k+k1] * r_e_rescale;
00866 }
00867
00868 k = k + 3;
00869 dEmcGeaTrackTower[i_gtrto_id-1].nextid = 0;
00870
00871 if(k < p_maxtowerhit && ia_twrkey[k] > 0)
00872 {
00873 dEmcGeaTrackTower[i_gtrto_id-1].nextid = i_gtrto_id + 1;
00874 }
00875
00876 i_gtrto_id = i_gtrto_id + 1;
00877 }
00878
00879
00880
00881 for(k = 0; k < 3; k++) xyz[k] = newxyz[k];
00882
00883 for (ll = 0; ll < sizeof(ia_twrkey)/sizeof(ia_twrkey[0]); ll++)
00884 {
00885 *(ia_twrkey + ll) = 0;
00886 }
00887 for (ll = 0; ll < sizeof(ra_edep)/sizeof(ra_edep[0]); ll++)
00888 {
00889 *(ra_edep + ll) = 0.0;
00890 }
00891
00892 if( ! l_last_hit )
00893 {
00894 i_last_trkno = true_track;
00895 }
00896 else
00897 {
00898 l_last_hit = FALSE;
00899 }
00900
00901 }
00902
00903
00904
00905
00906
00907
00908 j = 0;
00909
00910
00911
00912
00913
00914
00915
00916
00917
00918 while(ia_twrkey[j] > 0 && ia_twrkey[j] != i_twrkey && j < p_maxtowerhit)
00919 {
00920 j = j + 1;
00921 }
00922
00923
00924
00925
00926
00927
00928
00929
00930
00931
00932
00933
00934
00935 if(j >= p_maxtowerhit)
00936 {
00937 }
00938 else
00939 {
00940
00941
00942
00943 ra_edep[j] = ra_edep[j] + dele_star / ra_det[84][i1];
00944 if(ia_twrkey[j] == 0) ia_twrkey[j] = i_twrkey;
00945 }
00946
00947
00948
00949
00950
00951
00952 k1 = -1;
00953 for ( j = 0; j < 4; j++ )
00954 {
00955 if(k1 == -1 && emc_parent[i1][iy][iz][j][1] == 0.0)
00956 {
00957 k1 = j;
00958 emc_parent[i1][iy][iz][j][1] = true_track;
00959 }
00960 if(emc_parent[i1][iy][iz][j][1] == true_track) k1 = j;
00961 }
00962
00963 if(k1 >= 0)
00964 {
00965 j = k1;
00966
00967
00968 emc_parent[i1][iy][iz][j][0] =
00969 emc_parent[i1][iy][iz][j][0] + dele_star / ra_det[84][i1];
00970
00971
00972 if(emc_parent[i1][iy][iz][j][2] == 0.0)
00973 emc_parent[i1][iy][iz][j][2] = tof;
00974 if(emc_parent[i1][iy][iz][j][2] > tof)
00975 emc_parent[i1][iy][iz][j][2] = tof;
00976 }
00977
00978
00979
00980
00981
00982
00983
00984
00985 i_tofindex = max(1,
00986 min(p_maxtimebin,( int )
00987 ((tof_star-r_timeoffset)/r_timebin))) -1;
00988 ra_pulse[i1][iy][iz][i_tofindex] =
00989 ra_pulse[i1][iy][iz][i_tofindex] + dele_star / ra_det[84][i1];
00990 }
00991
00992
00993
00994 emc_dele_cal[i1][iy][iz] = emc_dele_cal[i1][iy][iz] + dele_star;
00995
00996
00997
00998
00999
01000
01001
01002
01003 if(tof_star > 0.0)
01004 {
01005 if(emc_tof_first[i1][iy][iz] == 0.0)
01006 {
01007 emc_tof_first[i1][iy][iz] = tof;
01008 }
01009 else
01010 {
01011 if(emc_tof_first[i1][iy][iz] > tof_star)
01012 emc_tof_first[i1][iy][iz] = tof;
01013 }
01014 }
01015
01016 lead_glass:
01017 continue;
01018
01019 out_of_gate:
01020 continue;
01021
01022 }
01023
01024
01025
01026
01027
01028 for ( i1 = 0; i1 < 8; i1++ )
01029 {
01030 for ( iy = 0; iy < ra_det[11][i1]*ra_det[13][i1]; iy++ )
01031 {
01032 for ( iz = 0; iz < ra_det[10][i1]*ra_det[12][i1]; iz++ )
01033 {
01034 if(emc_parent[i1][iy][iz][1][0] > 0.0)
01035
01036 {
01037 for( i = 0; i < 3; i++)
01038 {
01039 for( j = 0; j < 3; j++)
01040 {
01041 if(emc_parent[i1][iy][iz][j+1][0] >
01042 emc_parent[i1][iy][iz][j][0])
01043 {
01044 for( k = 0; k < 3; k++)
01045 {
01046 r_work1 = emc_parent[i1][iy][iz][j][k];
01047 emc_parent[i1][iy][iz][j][k] =
01048 emc_parent[i1][iy][iz][j+1][k];
01049 emc_parent[i1][iy][iz][j+1][k] = r_work1;
01050 }
01051 }
01052 }
01053 }
01054 }
01055 }
01056 }
01057 }
01058
01059
01060
01061
01062
01063
01064
01065
01066
01067 for ( i1 = 0; i1 < 6; i1++ )
01068
01069 {
01070 for ( iy = 0; iy < ra_det[11][i1]*ra_det[13][i1]; iy++ )
01071
01072 {
01073 for ( iz = 0; iz < ra_det[10][i1]*ra_det[12][i1]; iz++ )
01074
01075
01076 {
01077 if(emc_dele_cal[i1][iy][iz] > 0.0)
01078 {
01079
01080 emc_dele_cal[i1][iy][iz] =
01081 emc_dele_cal[i1][iy][iz] * r_e_rescale;
01082
01083
01084
01085 if(ra_det[84][i1] > 0.0) emc_dele_cal[i1][iy][iz] =
01086 emc_dele_cal[i1][iy][iz] / ra_det[84][i1];
01087
01088
01089
01090
01091
01092
01093 if(ra_det[85][i1] > 0.0)
01094 {
01095 r_work2 = emc_dele_cal[i1][iy][iz] * ra_det[85][i1];
01096
01097 d_work1 = r_work2;
01098 d_work = sqrt(d_work1);
01099 r_work3 = d_work;
01100
01101 norran_(&r_work1);
01102 r_work2 = r_work2 + r_work3 * r_work1;
01103 emc_dele_cal[i1][iy][iz] = r_work2 / ra_det[85][i1];
01104 }
01105
01106
01107
01108
01109
01110
01111
01112
01113
01114
01115 norran_(&r_work1);
01116
01117
01118 emc_dele_cal[i1][iy][iz] =
01119 emc_dele_cal[i1][iy][iz] + r_work1 * pbsc_noise;
01120
01121
01122
01123 switch (sim_timing)
01124 {
01125 case 0:
01126
01127 if(emc_dele_cal[i1][iy][iz] > r_timethresh)
01128 {
01129 for (ll = 0; ll < sizeof(ra_pulserecon) /
01130 sizeof(ra_pulserecon[0]); ll++)
01131 {
01132 *(ra_pulserecon + ll) = 0.0;
01133 }
01134
01135 r_tdecay = ra_det[89][i1];
01136 r_tdecay = 0.5;
01137 r_ttune = ra_det[90][i1];
01138 i_tdelay = ra_det[91][i1] / r_timebin;
01139 r_tfac = ra_det[92][i1];
01140 r_thresh = ra_det[93][i1];
01141
01142 r_talpha = r_tfac ;
01143
01144
01145
01146
01147
01148 d_work4 = r_talpha;
01149 d_work3 = exp(-d_work4);
01150 d_work2 = r_talpha / r_tdecay;
01151 d_work1 = pow(d_work2,d_work4);
01152 d_work= d_work1 * d_work3;
01153 r_tmaxval = d_work;
01154
01155
01156
01157
01158
01159
01160
01161 r_earliest_valid_tof = 0.0;
01162
01163 for ( i_tofindex = 0; i_tofindex < p_maxtimebin; i_tofindex++ )
01164 {
01165
01166
01167
01168
01169
01170
01171
01172
01173
01174
01175
01176
01177
01178
01179
01180
01181
01182
01183
01184
01185
01186
01187
01188
01189
01190 if(ra_pulse[i1][iy][iz][i_tofindex] > ra_det[86][i1])
01191 {
01192
01193
01194
01195 for ( k = i_tofindex; k < 2*p_maxtimebin-1; k++ )
01196 {
01197 r_t = (k+1-i_tofindex) * r_timebin;
01198
01199 if((r_t * r_tdecay) <= 75.0)
01200 {
01201
01202
01203
01204
01205
01206
01207 d_work4 = ra_pulse[i1][iy][iz][i_tofindex] / r_tmaxval;
01208 d_work3 = r_tdecay;
01209 d_work2 = r_talpha;
01210 d_work1 = r_t;
01211 ra_pulserecon[k] = ra_pulserecon[k] +
01212 pow(d_work1,d_work2) * exp(- d_work1*d_work3)
01213 * d_work4;
01214
01215 }
01216
01217 if(ra_pulserecon[k] > 1.3*ra_det[92][i1])
01218 {
01219
01220 if(r_earliest_valid_tof == 0.0)
01221 {
01222 r_earliest_valid_tof = (i_tofindex) *
01223 r_timebin + r_timeoffset;
01224 }
01225 }
01226 }
01227 }
01228 }
01229
01230 i_tofindex = 0;
01231
01232 for ( k = 1; k < 2 * p_maxtimebin - 1; k++ )
01233 {
01234
01235 if(i_tofindex == 0 && ra_pulserecon[k] > r_timethresh)
01236 {
01237 i_tofindex = k;
01238 emc_tof[i1][iy][iz] = (i_tofindex +1)
01239 * r_timebin + r_timeoffset;
01240 }
01241 }
01242
01243 if(i_tofindex == 0)
01244 {
01245 emc_tof[i1][iy][iz] = - 99.0;
01246 }
01247 }
01248 break;
01249
01250 case 1:
01251
01252
01253
01254 emc_tof[i1][iy][iz] = emc_tof_first[i1][i][j];
01255 break;
01256
01257 case 2:
01258
01259
01260
01261
01262
01263
01264 if(emc_dele_cal[i1][iy][iz] > r_timethresh)
01265 {
01266 for (ll = 0; ll < sizeof(ra_pulserecon) /
01267 sizeof(ra_pulserecon[0]); ll++)
01268 {
01269 *(ra_pulserecon + ll) = 0.0;
01270 }
01271
01272 r_tdecay = ra_det[89][i1];
01273 r_tdecay = 0.5;
01274 r_ttune = ra_det[90][i1];
01275 i_tdelay = ra_det[91][i1] / r_timebin;
01276 r_tfac = ra_det[92][i1];
01277 r_thresh = ra_det[93][i1];
01278
01279 r_talpha = r_tfac ;
01280
01281 d_work4 = r_talpha;
01282 d_work3 = exp(-d_work4);
01283 d_work2 = r_talpha / r_tdecay;
01284 d_work1 = pow(d_work2,d_work4);
01285 d_work= d_work1 * d_work3;
01286 r_tmaxval = d_work;
01287
01288
01289
01290
01291
01292
01293
01294 r_earliest_valid_tof = 0.0;
01295
01296 for ( i_tofindex = 0; i_tofindex < p_maxtimebin; i_tofindex++ )
01297 {
01298
01299
01300
01301
01302
01303
01304
01305 if(ra_pulse[i1][iy][iz][i_tofindex] > ra_det[86][i1])
01306 {
01307
01308
01309
01310 for ( k = i_tofindex; k < 2*p_maxtimebin-1; k++ )
01311 {
01312 r_t = (float) (k+1-i_tofindex) * r_timebin;
01313
01314 if((r_t * r_tdecay) <= 75.0)
01315 {
01316
01317 d_work4 = ra_pulse[i1][iy][iz][i_tofindex] / r_tmaxval;
01318 d_work3 = r_tdecay;
01319 d_work2 = r_talpha;
01320 d_work1 = r_t;
01321 ra_pulserecon[k] = ra_pulserecon[k] +
01322 pow(d_work1,d_work2) * exp(- d_work1*d_work3)
01323 * d_work4;
01324
01325 }
01326
01327 }
01328 }
01329 }
01330
01331 i_tofindex = 0;
01332
01333
01334
01335
01336 for ( k = 1; k < 2 * p_maxtimebin - 2; k++ )
01337 {
01338
01339
01340
01341 if(i_tofindex == 0 && ra_pulserecon[k] > r_timethresh)
01342 {
01343 if(ra_pulserecon[k] > ra_pulserecon[k+1]
01344 && ra_pulserecon[k] > ra_pulserecon[k+2])
01345 {
01346
01347 i_tofindex = k;
01348 emc_tof[i1][iy][iz] = (float) (i_tofindex +1)
01349 * r_timebin + r_timeoffset;
01350 }
01351
01352 }
01353 }
01354
01355 if(i_tofindex == 0)
01356 {
01357 emc_tof[i1][iy][iz] = - 99.0;
01358 }
01359 }
01360
01361 break;
01362
01363 }
01364
01365 }
01366
01367 }
01368 }
01369 }
01370
01371
01372
01373
01374
01375 for(i1 = 6; i1 < 8; i1++)
01376 {
01377 for ( iy = 0; iy < ( int ) (ra_det[11][i1]*ra_det[13][i1]); iy++ )
01378
01379 {
01380 for ( iz = 0; iz < ( int ) (ra_det[10][i1]*ra_det[12][i1]); iz++ )
01381
01382
01383 {
01384 if(emc_dele_cal[i1][iy][iz] > 0.0)
01385
01386 {
01387
01388 switch(pbgl_response)
01389 {
01390 case 0:
01391
01392 break;
01393
01394 case 1:
01395
01396
01397 if(ra_det[85][i1] > 0.0)
01398 {
01399
01400 r_work2 = emc_dele_cal[i1][iy][iz] * ra_det[85][i1];
01401 r_work3 = sqrtf(r_work2);
01402 norran_(&r_work1);
01403 r_work2 = r_work2 + r_work3 * r_work1;
01404 emc_dele_cal[i1][iy][iz] = r_work2 / ra_det[85][i1];
01405 }
01406
01407
01408 norran_(&r_work1);
01409 r_work2 = sqrtf(emc_dele_cal[i1][iy][iz]);
01410 emc_dele_cal[i1][iy][iz] = emc_dele_cal[i1][iy][iz] +
01411 r_work2 * r_work1 * pbgl_resolution;
01412
01413
01414 norran_(&r_work1);
01415 emc_dele_cal[i1][iy][iz] = emc_dele_cal[i1][iy][iz]
01416 + r_work1 * ra_det[86][i1];
01417
01418
01419
01420 emc_dele_cal[i1][iy][iz] =
01421 emc_dele_cal[i1][iy][iz] * pbgl_rescale;
01422
01423
01424 emc_tof[i1][iy][iz] = emc_tof_first[i1][iy][iz]
01425 + pbgl_tofoffset;
01426 norran_(&r_work1);
01427 emc_tof[i1][iy][iz] = emc_tof[i1][iy][iz] +
01428 r_work2 * r_work1 * pbgl_tofresolution;
01429
01430 break;
01431
01432 case 2:
01433
01434
01435
01436 true_track = (int) emc_parent[i1][iy][iz][0][1];
01437
01438
01439
01440 status = dio_ptrkstack(&true_track, &nfile, &error,
01441 &ptot, &ptheta, &pphi,
01442 &r_vertex, &z_vertex,
01443 &theta_vertex, &phi_vertex,
01444 &itparent, &idparent, &idpart);
01445
01446 r_eexp = emc_dele_cal[i1][iy][iz];
01447 r_porig = ptot;
01448 r_etracking = r_eexp;
01449
01450 if( idpart == 1)
01451 {
01452 r_etracking =
01453 r_eexp * (1.003 -
01454 0.0531*exp(0.2905*r_porig));
01455
01456 r_etracking = r_etracking / 0.88;
01457 }
01458
01459 if( idpart == 2 || idpart == 3)
01460 {
01461 r_etracking =
01462 r_eexp * (0.9777 -
01463 0.0469*exp(0.4567*r_porig));
01464
01465 r_etracking = r_etracking / 0.88;
01466 }
01467
01468 if( idpart >= 5 && idpart <= 9)
01469 {
01470 if(r_porig > 0.5)
01471 { if(r_eexp < 0.35)
01472
01473 {
01474
01475 r_etracking = r_eexp * 1.85;
01476 }
01477 else
01478 {
01479
01480 r_etracking = r_etracking * 0.82;
01481 }
01482 }
01483 }
01484
01485 if( idpart >= 11 && idpart <= 16)
01486 {
01487 if(r_porig > 0.9)
01488 { if(r_eexp < 0.4)
01489 {
01490
01491 r_etracking = r_eexp *
01492 (2.055 - 0.03 * r_porig);
01493 }
01494 else
01495 {
01496
01497 r_etracking = r_etracking * 0.9;
01498 }
01499 }
01500 }
01501
01502
01503 emc_dele_cal[i1][iy][iz] = r_etracking;
01504
01505
01506
01507 emc_tof[i1][iy][iz] = emc_tof_first[i1][iy][iz]
01508 + pbgl_tofoffset;
01509 norran_(&r_work1);
01510 emc_tof[i1][iy][iz] = emc_tof[i1][iy][iz] +
01511 r_work2 * r_work1 * pbgl_tofresolution;
01512
01513 break;
01514 case 3:
01515
01516
01517
01518
01519
01520 emc_dele_cal[i1][iy][iz] =
01521 emc_dele_cal[i1][iy][iz] * pbgl_ctrk_rescale;
01522
01523
01524
01525 emc_tof[i1][iy][iz] = emc_tof_first[i1][iy][iz]
01526 + pbgl_tofoffset;
01527 norran_(&r_work1);
01528 emc_tof[i1][iy][iz] = emc_tof[i1][iy][iz] +
01529 r_work2 * r_work1 * pbgl_tofresolution;
01530
01531 break;
01532
01533 }
01534
01535 }
01536
01537 }
01538
01539 }
01540
01541 }
01542
01543
01544
01545 iout_ok = 0;
01546 for ( iz = 0; iz < max_chanz; iz++ )
01547 {
01548 for ( iy = 0; iy < max_chany; iy++ )
01549 {
01550 for ( i1 = 0; i1 < i_detmax; i1++ )
01551 {
01552
01553
01554
01555
01556
01557
01558 if(emc_dele_cal[i1][iy][iz] > 0.001)
01559
01560 {
01561 iarm = 1;
01562 if(i1 < 6)
01563 {
01564 itype = 1;
01565 if(i1 < 4) iarm = 0;
01566 }
01567 else
01568 {
01569 itype = 2;
01570 }
01571
01572 i_lopre = i_low_ped;
01573 i_hipre = i_high_ped;
01574 i_lopost = i_lopre -
01575 ( int ) (emc_dele_cal[i1][iy][iz] / r_lowgain_convfac);
01576 i_hipost = i_hipre -
01577 ( int ) (emc_dele_cal[i1][iy][iz] / r_highgain_convfac);
01578
01579
01580
01581
01582
01583
01584 if(sim_timing==0&&itype==1) {
01585 emc_tof[i1][iy][iz] = emc_tof[i1][iy][iz] - 2.2;
01586
01587
01588
01589
01590 r_slew_e = emc_dele_cal[i1][iy][iz];
01591 r_meas_t = emc_tof[i1][iy][iz];
01592 r_slew_t = r_meas_t - 0.35 / pow(r_slew_e-0.015,0.75) - r_slew_e / 15.0;
01593 emc_tof[i1][iy][iz] = r_slew_t;
01594
01595 norran_(&noise);
01596
01597
01598
01599
01600
01601
01602
01603
01604
01605
01606 if(i1>=0&&i1<=5)
01607 emc_tof[i1][iy][iz] = emc_tof[i1][iy][iz] + 0.280 + 0.150*noise;
01608
01609
01610 norran_(&noise);
01611
01612
01613
01614
01615
01616
01617 }
01618
01619 if(sim_timing==0&&itype==2) {
01620 emc_tof[i1][iy][iz] = emc_tof[i1][iy][iz] - 3.03;
01621 norran_(&noise);
01622 emc_tof[i1][iy][iz] = emc_tof[i1][iy][iz] - 0.6*noise;
01623 }
01624
01625
01626
01627 i_tof = ( int ) (emc_tof[i1][iy][iz] / r_tdc_convfac);
01628
01629
01630
01631 if(i1>=6)
01632 {
01633 norran_(&noise);
01634 i_lopost = i_lopost - (int)(8.3*noise);
01635 i_hipost = i_hipost - (int)(1.04*noise);
01636 }
01637
01638
01639
01640 if(i_lopost < i_minvalue) i_lopost = i_minvalue;
01641 if(i_hipost < i_minvalue) i_hipost = i_minvalue;
01642
01643
01644 i_lopost = i_lopost & 0x00000FFF;
01645 i_hipost = i_hipost & 0x00000FFF;
01646 i_tof = i_tof & 0x00000FFF;
01647
01648
01649
01650
01651
01652
01653
01654 dEmcRawData[iout_ok].id = iout_ok +1;
01655 dEmcRawData[iout_ok].evno = header[0].event;
01656
01657
01658 switch(itype)
01659 {
01660 case 1:
01661
01662 ismodz = iz / 12;
01663 ismody = iy / 12;
01664 irelz = iz % 12;
01665 irely = iy % 12;
01666 ismoffset = 432 * ismodz + 144 * ismody;
01667 itower = (ismoffset + 12 * irely + irelz) & 0x1FFF;
01668 break;
01669
01670 case 2:
01671 ismodz = iz / 6;
01672 ismody = iy / 4;
01673 irelz = iz % 6;
01674 irely = iy % 4;
01675 ismoffset = 288 * ismodz + 24 * ismody;
01676 itower = (ismoffset + 4 * irely + irelz) & 0x1FFF;
01677 break;
01678 }
01679
01680 isector = i1 * 0x2000;
01681 ihwkey = isector + itower;
01682
01683 dEmcRawData[iout_ok].hwkey = ihwkey;
01684 dEmcRawData[iout_ok].type = itype;
01685
01686 if(i1 < 4)
01687 {
01688 i_twrkey = 10000 * i1 + 100 * iy + iz;
01689 }
01690 else
01691 {
01692 i_twrkey = 100000 + 10000 * (7 - i1) + 100 * iy + iz;
01693 }
01694
01695 dEmcRawData[iout_ok].swkey = i_twrkey;
01696 dEmcRawData[iout_ok].adclopre = i_lopre;
01697 dEmcRawData[iout_ok].adchipre = i_hipre;
01698 dEmcRawData[iout_ok].adclopost = i_lopost;
01699 dEmcRawData[iout_ok].adchipost = i_hipost;
01700 dEmcRawData[iout_ok].tdc = i_tof;
01701
01702 iout_ok = iout_ok + 1;
01703 }
01704 }
01705 }
01706 }
01707
01708 dEmcRawData_h->nok = iout_ok;
01709 dEmcGeaTrackTower_h->nok = i_gtrto_id - 1;
01710
01711 i_gtotr_id = 0;
01712
01713 for ( i1 = 0; i1 < i_detmax; i1++ )
01714 {
01715 for ( iy = 0; iy < max_chany; iy++ )
01716 {
01717 for ( iz = 0; iz < max_chanz; iz++ )
01718 {
01719 if(emc_parent[i1][iy][iz][0][0] > 0.0)
01720 {
01721 if(i1 < 4)
01722 {
01723 i_twrkey = 10000 * i1 + 100 * iy + iz;
01724 }
01725 else
01726 {
01727 i_twrkey = 100000 + 10000 * (7 - i1) + 100 * iy + iz;
01728 }
01729
01730 dEmcGeaTowerTrack[i_gtotr_id].id = i_gtotr_id +1;
01731 dEmcGeaTowerTrack[i_gtotr_id].twrkey = i_twrkey;
01732 dEmcGeaTowerTrack[i_gtotr_id].input = 1;
01733
01734 for ( j = 0; j < 3; j++ )
01735 {
01736 dEmcGeaTowerTrack[i_gtotr_id].trkno[j] =
01737 emc_parent[i1][iy][iz][j][1];
01738
01739
01740
01741
01742
01743
01744
01745
01746 dEmcGeaTowerTrack[i_gtotr_id].edep[j] =
01747 emc_parent[i1][iy][iz][j][0] * r_e_rescale;
01748
01749 dEmcGeaTowerTrack[i_gtotr_id].toffirst[j] =
01750 emc_parent[i1][iy][iz][j][2];
01751 }
01752
01753 i_gtotr_id = i_gtotr_id + 1;
01754 }
01755 }
01756 }
01757 }
01758
01759 dEmcGeaTowerTrack_h->nok = i_gtotr_id;
01760
01761 return ( STAFCV_OK );
01762 }