mEmcGeaMakeRaw.c

Go to the documentation of this file.
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         Drastic changes, based on PISORP EMC_USER_NEW
00028         Dec 19, 1997 G. David
00029 
00030         August 95, G. David
00031 
00032         Main steering routine for EMC in PISORP
00033 
00034         The code has been converted to C on Feb 27, 1998.
00035         By : 
00036                 PhoolChand,             phool@phenix.barc.ernet.in
00037                 Dipanwita Dutta,        dipa@phenix.barc.ernet.in
00038 
00039         Updated with changes since Jan 98 in memcgeamakeraw.F
00040         and put in the repository  May 21, 98 G. David
00041 
00042         Updated Sep. 7, 98 - G. David
00043                 (Cleanup, comments, fkin search dropped for dio_ptrkstack,
00044 
00045         Updated Sep. 20, 98 - G. David
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         /*      bit unpacking variables */
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         /*      variables for doing the pulse shape timing      */
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    /* variables for Year-2 slewing */
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         /*      other variables from emc_digi in PISA   */
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                 This array is for response corrections for different impact
00116                 positions
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;              /*        50 ps bins     */
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    /* emc_parent[...][0] = energy
00146       emc_parent[...][1] = true track number
00147       emc_parent[...][2] = tof_first
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;    /* Counter of dEmcGeaTowerTrack rows */
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      #define i_low_ped 100
00170      #define i_high_ped 100
00171    */
00172 #define i_low_ped 4000
00173 #define i_high_ped 4000
00174 #define i_minvalue 100
00175 
00176         /*      Pulse reconstruction from flash + 10 ns
00177                 mostly (17 to 27 ns) in 50 ps bins      */
00178 
00179    static       int     p_maxtimebin;
00180 
00181    /* #define   p_maxtimebin    300 */
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    /*#define    p_maxtowerhit   100 */
00201 
00202         /*      Variables needed to build dEmcGeaTrackTower     */
00203 
00204    static       int     i_twrkey;                       /*       Translates iz,iy,i1 to single number tower key */
00205    static       int     i_gtrto_id;                     /*       ID number in dEmcGeaTrackTower */
00206    static       int     i_last_trkno;                   /*       Last valid track number        */
00207    static       int     ia_twrkey[p_maxtowerhit];       /*       Temp. storage for hit tower keys       */
00208    static       float   ra_edep[p_maxtowerhit];         /*       Temp. storage for hit tower E  */
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        /* 17 ns + 110 ns for clock */
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;    /* Steering timing simulation */
00227    static int pbgl_response = 0;    /* Steering PbGl response simulation */
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    // Try to implement the safer part of Markus's changes
00235    // G. David, Dec. 2, 2000
00236 
00237    //   Do not eliminate the possibility of rescale, just 
00238    //   set it to 1.0 right now 
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    // End changes Dec. 2, 2000
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  /* For dio_truetrack call */
00257  /* dio_truetrack thrown out, since now the root output of PISA99
00258     provides you also with true_track number, even if files are
00259     merged   Jan. 3, 2000, Gabor David  */
00260 
00261  int isubevent;
00262  int ntrack;
00263  int error;
00264  int ll;
00265 
00266  /* Cutoff low energy GEANT hits */
00267 
00268  static float r_e_cutoff      =    0.00001;
00269  /* Change rescale - calibration - factor because the attenuation
00270     length has been changed,  Dec. 5, 2000,  G. David
00271  static float r_e_rescale     =    1.02;
00272  New rescale factor based upon 1.5 GeV photons: 1.02 * 1.08
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                         Executable
00285         ******************************************************/
00286 
00287    if(l_first)  /*       Read in        */
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         If Cherenkov photons were generated in PISA, disallow user
00315         to steer PbGl response
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         /* Changed indexing in dEmcGeometry Nov. 20, 1998 G. David 
00329          iz = dEmcGeometry[j].ind[0]    -1;
00330          iy = dEmcGeometry[j].ind[1]    -1;
00331          i1 = dEmcGeometry[j].sector    -1;
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    /* Fudge factors to make up for leaving out low energy hits */
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;       /*      Always start with zero */
00383    i_gtotr_id = 0;       /*      Always start with zero */
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    /*   Loop over all subevents         */
00393 
00394    /*
00395      CFM: May 28, 2005
00396      Single particle events can miss EMC entirely
00397    */
00398    if(dEmcGeaHit_h->nok <= 0) 
00399    {
00400       /* printf (" Error in mEmcGeaMakeRaw: no hits \n");  (use verbosity or iDebug check, CFM) */
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      /* You can skip very small hits, but only if they are not
00411         the very last entry in the hit table for this part. - otherwise you
00412         get screwed with the TrackTower table */
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       /* Got here the first time */
00432 
00433       /* This part deleted, now true_track comes from PISA99 root
00434          output - Jan. 3, G. David */
00435       /*
00436       if( itrack_prev == 0 && isubevt_prev == 0
00437          && nfile_prev ==0 )
00438       {
00439          itrack_prev = itrack;
00440          isubevt_prev = isubevt;
00441          nfile_prev = nfile;
00442          l_found = FALSE;
00443          xyz[0] = pos_x;
00444          xyz[1] = pos_y;
00445          xyz[2] = pos_z;
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       /* Check if (itrack,isubevt,nfile) changed */
00460 
00461       /* Modified: true track now comes from PISA99 root output
00462          directly  Jan. 3, 2000,  G. David */
00463       /*
00464       if( itrack_prev != itrack || isubevt_prev != isubevt ||
00465          nfile_prev != nfile) l_found = FALSE;
00466       */
00467 
00468       /* Added Markus's changes, Dec. 17, 2000 G. D. */
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       /* End Markus's change */
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          /*  Take care of readout gate and hits that are too small 
00483              but only if this is not the last hit from a track */
00484       /* Don't do it for PbGl, says Markus, Dec 17, 2000, G. David 
00485       if(l_found != 0 && l_last_hit == 0)
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       /* Markus's correction: check if next tower will
00497          be the same
00498       if( i_key[0] == i_key[1])
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   /* single step dele too big */
00516             {
00517               dele = dele + dele_sum;
00518               dele_sum = 0.0;
00519             }
00520         }
00521       else  /* new tower hit, not the same as before */
00522         {
00523           dele = dele + dele_sum;
00524           dele_sum = 0.0;
00525           /* Follow Markus's logic, Dec. 17, 2000 G. David 
00526           i_key[1] = i_key[0];
00527           */
00528           i_key[1] = next_tower;
00529         }
00530 
00531       /*      if( i1 > 5) goto lead_glass; */ /* Different response */
00532 
00533       /* Previous track, subevt, file doesn't match current */
00534       /*
00535       if( l_found == 0)  
00536       {
00537 
00538         ntrack = itrack;
00539         isubevent = isubevt;
00540 
00541         error = -1;
00542         true_track = dio_truetrack(&ntrack, &isubevent, &nfile, &error);
00543         if( error == 0)
00544           {
00545             l_found = TRUE;
00546 
00547             itrack_prev = itrack;
00548             isubevt_prev = isubevt;
00549             nfile_prev = nfile;
00550             newxyz[0] = pos_x;
00551             newxyz[1] = pos_y;
00552             newxyz[2] = pos_z;
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       /* End if l_found = 0 */
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           Process hit information (taken from emc_digi.f in PISA as of 8/15/96)
00583 
00584           Get the distance from the readout device.  This is
00585           the difference (RPOS + LSIZ) - RPROJ, where LSIZ is
00586           the (full) longitudinal size of the cell (PbGl, crystal,
00587           whatever), RPOS is the distance of the center of the
00588           front face of the detector, RPROJ is the distance of the
00589           hit PROJected on the vector pointing to the center of
00590           the detector
00591           ********************************************* ******************************  */
00592 
00593       if(dele >= r_dele_small)
00594         {
00595 
00596       /*  ********************************************************************
00597           Recalculate energy and time of flight: correct time of flight
00598           with light propagation speed (TOF_STAR), correct energy
00599           with light attenuation over the distance between the current
00600           position (where energy was deposited) and the end of the
00601           module, where light is read out.
00602                 *************************************** ******************************  */
00603 
00604           /* If PbGl and response parametrization is chosen, do NOT
00605              attenuate */
00606 
00607           if( i1 > 5 && pbgl_response == 2)
00608             {
00609               dele_star = dele;
00610               tof_star = tof;
00611             }
00612           /* If Cherenkov photons were generated, do NOT attenuate! */
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               /* Failed to account for global translations in the
00628                  geometry; this is still not quite clean
00629                  but works   Aug. 14, 2000  GD 
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           Figure out indices (in z and y direction) in the corresponding
00649           subdetector arrays. This is trivial for PbGl, more complicated for
00650           Shish-Kebab, particularly when crystals are in the middle
00651 
00652           ccfm        i_index(1), i_index(2), and numed are retrieved during unpacking
00653 
00654           i_index[1] = nubv[1][i];      ! Volume numbers
00655           i_index[2] = nubv[2][i];
00656           x_d[1] = 0.0;
00657           numed = 0;                 ! Reset medium before GMEDIA call
00658           CALL GMEDIA(x_m,numed);    ! Get medium where hit is registered
00659 
00660           Lead glass: the returned volume indices are the indices
00661           in the energy deposition array itself
00662 
00663           Unfold the volume indices (cell, supermodule) and map
00664           them to a simple (geometric) array / wall.  In the
00665           subsequent analysis you want to make sure that adjacent
00666           cells have adjacent indices in the energy and TOF array.
00667           Get the relative "position" (i.e. integer index, in which
00668           cell it is) within a supermodule.
00669 
00670           i_index(1) is the number of supermodule within the octant
00671           recall, octants are built up vertically, and from -z
00672           so the supermodule number map of a wall is this
00673 
00674           18   15   12    9     6    3
00675           17   14   11    8     5    2
00676           18   13   10    7     4    1  (-200.cm in z)
00677 
00678           i_index(2) is the number of cell within the supermodule
00679           recall: supermodules are built up vertically, starting at -z
00680           so the cell numbers are
00681 
00682           144 132 120 108  96  84  72  60  48  36  24  12
00683           .............
00684           133 121 109  97  85  73  61  49  37  25  13   1 (-z, smallest y)
00685 
00686           Get the cell array index offset due to supermodules before the
00687           current one ("before": see numbering scheme above)
00688 
00689           **************************************************************************  */
00690 
00691       /*        correct if you feel index is not calculated correctaly By phool chand   */
00692       /* No, it wasn't quite right... GD */
00693 
00694       switch(itype)
00695         {
00696 
00697           /* This guy is lead scintillator */
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          /*     Get the cell array indices within the supermodule       */
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          /* Numbering in iz goes the other way (from positive z to negative)
00713             in the West Arm */
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           /* This guy is lead glass */
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          /*     Get the cell array indices within the supermodule       */
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          /* Numbering in iz goes the other way (from positive z to negative)
00736             in the West Arm */
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         }  /* End of different treatment to get array indices for PbGl, PbSc */
00745 
00746       /*         Diagnostic plots       */
00747 
00748       if(dele > 0.0)
00749       {
00750 
00751          /* ************************************************************************** 
00752             Charlie is right: position uniformity correction should come
00753             before the pulse shape reconstruction
00754 
00755             If position nonuniformity correction is requested,
00756             do it now (for Shish-Kebab only)
00757 
00758             IF we include this in here, we need dEmcGeometry
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              Work on track-to-tower and tower-to-track table
00790              dele_star is final now, it's value isn't changed from now on
00791              *************************************** ******************************  */
00792 
00793          /* Restore dele_star to dele in case of PbGl and leave the
00794             response to Muenster */
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          /* Code below this line has been rearranged by PhoolChand      */
00807 
00808          while (  l_last_hit || i_last_trkno != true_track  )
00809          {                              
00810             /*  ************************************************** 
00811                 ! New track.  Save current 
00812                 ! data in dEmcGeaTrackTower 
00813                 ! and start cumulating data
00814                 ! for the next track
00815                 ****************************************************  */
00816 
00817             /*  At first sort this stupid thing */
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                /*       Write a new entry into dEmcGeaTrackTower table  */
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;               /*       For now        */
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                      dEmcGeaTrackTower[i_gtrto_id-1].edep[k1] =ra_edep[k+k1];
00860                      Need to rescale it, too...  It worked until now because
00861                      the rescale factor was essentially 1
00862                      Dec. 21, 2000, G. David
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;   /*  Assume there is no more        */
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             }       /*   Loop over k, max. number of track-to-tower entries     */
00878 
00879             /*  ! or this is the last hit       */
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          }              /*        i_last_trkno != itrack (new track started)    */
00902 
00903          /*  ************************************************** 
00904              ! Still add to the current
00905              ! track-to-tower table
00906              ****************************************************  */
00907 
00908          j = 0;
00909 
00910          /*  ************************************************** 
00911              ! Find a match in the ia_twrkey array (it means
00912              ! that this is NOT the first energy deposition
00913              ! from this track into this tower)
00914              ! or find the first UNUSED index in ia_twrkey
00915              ! for a new tower
00916              ********************* ******************************  */
00917 
00918          while(ia_twrkey[j] > 0  &&  ia_twrkey[j] != i_twrkey  &&  j < p_maxtowerhit)
00919          {
00920             j = j + 1;
00921          }
00922 
00923          /* Code above this line has been rearranged by phool Chand     */
00924 
00925          /*  ************************************************** 
00926              ! Therefore, you ad to write out data from
00927              ! previous track into dEmcGeaTrackTower
00928              ! and clear it.
00929 
00930              ! Now it is time to increment contents of
00931              ! track cumulating arrays with the
00932              ! current energy deposit
00933              ********************* ******************************  */
00934 
00935          if(j >= p_maxtowerhit)
00936          {
00937          }
00938          else
00939          {
00940             /*  dele_star is before sampling fraction correction        */
00941             /*  therefore...    */
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              ! Now take care of the other direction:
00949              ! which track deposited into a particular tower?
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            /* Correcto for sampling fraction */
00968             emc_parent[i1][iy][iz][j][0] = 
00969               emc_parent[i1][iy][iz][j][0] + dele_star / ra_det[84][i1];
00970 
00971             /* Carry on time of earliest GEANT hit */
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           *  New arrays for later use in time pulse shape
00980           *  reconstruction Note that PbScint dele_star is modifed
00981           *  below with emc_pos_unif factor, but this modified value
00982           *  was not used in timing reconstruction??
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       } /*       check that DELE > 0    */
00991 
00992       /*        Deposit to 'i1' (This may be any of the subdetectors)   */
00993 
00994       emc_dele_cal[i1][iy][iz] = emc_dele_cal[i1][iy][iz] + dele_star;  /*       Modified dE    */
00995 
00996       /*  *************************************************************** 
00997           Time of flight is now the earliest time in the module
00998           (this is NOT the real, measured TOF, but it will serve
00999           as a starting point when we build up the pulse-shape to
01000           get a good simulation of timing)
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    }/*   Loop over istaf = 1, dEmcGeaHit->nok   */
01023 
01024    /* Sort emc_parent for decreasing energy; you will need the
01025       particle with highest deposited energy - the "dominant contributor"
01026       to make the empirical correction to the PbGl response */
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              /* You got more than one contributor */
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          } /* End loop over iz, modules in z direction */
01056      } /* End loop over iy, modules in y direction */
01057    } /* End loop over i1, sectors */
01058 
01059    /*  ****************************************************************************** 
01060        Make different corrections to the (total) deposited
01061        energy.  At this point energy is still strictly non-negative
01062        ************************************************* ******************************  */
01063 
01064    /* First deal with PbSc response */
01065 
01066    /*   for ( i1 = 0; i1 < i_detmax; i1++ ) */
01067    for ( i1 = 0; i1 < 6; i1++ )
01068 
01069    {
01070      for ( iy = 0; iy <  ra_det[11][i1]*ra_det[13][i1]; iy++ )    
01071 /*       Max. no. of modules, y dir.    */ 
01072      {
01073        for ( iz = 0; iz <  ra_det[10][i1]*ra_det[12][i1]; iz++  )      
01074 /*       Max. no. of modules, z dir     */
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                /*       Multiply by inverse of sampling fraction        */
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                    If number of photons/GeV is specified, we can generate
01090                    the statistical term in the energy resolution
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 /*                r_work3 = sqrt(r_work2);     */
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;  /*     Mean + ran*sigma       */
01103                   emc_dele_cal[i1][iy][iz] = r_work2 / ra_det[85][i1];
01104                }
01105 
01106                /*  ****************************************************************** 
01107                    If noise is specified, put uncorrelated noise on top of
01108                    the signal in every channel.  Channels with no signal are
01109                    deliberately omitted (left as 0.0).  If you really want to
01110                    have noise in every channel (no matter if it is hit or not),
01111                    change the code, the zeroing of the array EMC_DELE at the
01112                    beginning of this routine.  Who cares?  Its your CPU time.
01113                    ************************************* ******************************  */
01114 
01115                norran_(&r_work1);
01116                //              emc_dele_cal[i1][iy][iz] = 
01117                //                emc_dele_cal[i1][iy][iz] + r_work1 * ra_det[86][i1];
01118                emc_dele_cal[i1][iy][iz] = 
01119                  emc_dele_cal[i1][iy][iz] + r_work1 * pbsc_noise;
01120 
01121                /*       DO pulse reconstruction         */
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];       /*       Pulse decay time       */
01136                        r_tdecay = 0.5;           /*      Hardwire for now (Sep. 24, 97) */
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];       /*       TOF threshold  */
01141 
01142                        r_talpha = r_tfac ;
01143 
01144                   /*              
01145                   r_tmaxval = powf( (r_talpha / r_tdecay), r_talpha ) * exp( - r_talpha);
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                       The "time history" of the energy deposition (propagated to
01157                       the PMT's and attenuated) is in ra_pulse(index,i,j,i1)
01158                       Generate this "partial pulse" in the remaining time-bins
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                          Here you are reconstructing the pulse itself, then compare
01169                          it with the timing threshold (ra_det[94][i1])
01170                          This takes lots of time; we shortcut the pulse reconstruction
01171                          FROM A SPECIFIC E DEPOSIT AT A SPECIFIC TIME, 
01172                          if the pulse already passes the timing threshold.
01173                          (Watch out: you cannot just stop and say: that's my time, because
01174                          the time can still be pushed EARLIER, particularly for very
01175                          low energies with large slewing.  But it definitely cannot be
01176                          pushed LATER; therefore, one you have a valid threshold crossing,
01177                          you can stop reconstructing the rest of the pulse.)
01178                          NOTE THAT THIS WOULD NOT WORK WITH A CFD OR A TRAILING EDGE
01179                          TIMING  --  AS IS THE CASE FOR LEAD GLASS!
01180 
01181                          ! You already had a pulse crossing
01182                          ! the threshold, and now waited
01183                          ! 5 ns, more than the maximum slewing
01184                          ! no pulse, however big, that comes
01185                          ! later could influence your measurement
01186                          ! and result in earlier times
01187 
01188                          ************************************************* ******************************  */
01189 
01190                            if(ra_pulse[i1][iy][iz][i_tofindex] > ra_det[86][i1])        /*       A.P.   */
01191                              {
01192 
01193                         /*      Do not generate pulse from noise!       */
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                                ra_pulserecon[k] = ra_pulserecon[k] +
01203                                powf (r_t, r_talpha) * exp( - r_t * r_tdecay)
01204                                * ra_pulse[i_tofindex][i][j][i1] / r_tmaxval;
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                                      }  /*       floating point protection for Alpha    */
01216 
01217                                    if(ra_pulserecon[k] > 1.3*ra_det[92][i1])  /* check is it 92 or 93 */
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                                  }              /*       Loop over k    */
01227                              }
01228                          }                /*     Loop over i_tofindex   */
01229 
01230                        i_tofindex = 0;
01231 
01232                        for ( k = 1;  k < 2 * p_maxtimebin - 1; k++  )
01233                          {
01234                            /* if(i_tofindex == 0 && ra_pulserecon[k] > ra_det[93][i1]) */
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                          }           /*  Loop over k in ra_pulserecon   */
01242 
01243                        if(i_tofindex == 0)              /*       Missed something, put high val.        */
01244                          {
01245                            emc_tof[i1][iy][iz] = - 99.0;
01246                          }
01247                      }        /*         Total Es exceed TOF threshold  */
01248                    break;     /*  End of full pulseshape reconstruction */
01249 
01250                  case 1:
01251                   /* Skip full pulseshape reconstruction */
01252                   /* Put in first arrival */
01253 
01254                    emc_tof[i1][iy][iz] = emc_tof_first[i1][i][j];
01255                    break;
01256 
01257                  case 2:
01258                   /* Do "constant fraction" a la PHENIX */
01259                   /* First implementation: just look for the first
01260                      peak of the signal (true story: the signal
01261                      is differentiated and the zero crossing of the
01262                      derivative is measured) */
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];       /*       Pulse decay time       */
01273                        r_tdecay = 0.5;           /*      Hardwire for now (Sep. 24, 97) */
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];       /*       TOF threshold  */
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                       The "time history" of the energy deposition (propagated to
01290                       the PMT's and attenuated) is in ra_pulse(index,i,j,i1)
01291                       Generate this "partial pulse" in the remaining time-bins
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                       *  Here you are reconstructing the pulse itself
01301                       */
01302 
01303                            /* ra_det[86][i1] is the noise level, set in mEmcGeaParams.c  */
01304 
01305                            if(ra_pulse[i1][iy][iz][i_tofindex] > ra_det[86][i1])
01306                              {
01307 
01308                         /*      Do not generate pulse from noise!       */
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                                      }  /*       floating point protection for Alpha    */
01326 
01327                                  }              /*       Loop over k    */
01328                              }
01329                          }                /*     Loop over i_tofindex   */
01330 
01331                        i_tofindex = 0;
01332 
01333                        /* Here is where you look for the peak of the pulse 
01334                           */
01335 
01336                        for ( k = 1;  k < 2 * p_maxtimebin - 2; k++  )
01337                          {
01338 
01339                            /* You still want to keep r_timethresh to avoid timing on channels
01340                               with too low energy */
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                          }           /*  Loop over k in ra_pulserecon   */
01354 
01355                        if(i_tofindex == 0)              /*       Missed something, put high val.        */
01356                          {
01357                            emc_tof[i1][iy][iz] = - 99.0;
01358                          }
01359                      }        /*         Total Es exceed TOF threshold  */
01360 
01361                    break;           /* case 2, constant fraction a la PHENIX */
01362 
01363                  }
01364 
01365             }                       /*   EMC_DELE_CAL(i1,i,j) > 0.0     */
01366 
01367          }            /*         Loop over iz, modules, z dir.  */
01368       }               /*         Loop over iy, modules, y direction     */
01369    }  /*         loop over i1, PbSc sectors     */
01370 
01371    /* Now deal with PbGl response 
01372       Individual hits have already been propagated and attenuated 
01373       exponentially */
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 /*       Max. no. of modules, y dir.    */ 
01379          {
01380            for ( iz = 0; iz < ( int ) (ra_det[10][i1]*ra_det[12][i1]); iz++  )      
01381 /*       Max. no. of modules, z dir     */
01382 
01383              {
01384                if(emc_dele_cal[i1][iy][iz] > 0.0)
01385 
01386                  {
01387 
01388                    switch(pbgl_response)
01389                      {
01390                      case 0:     
01391                        /* Leave everything unchanged; just exp. attenuation */
01392                        break;
01393 
01394                      case 1:    
01395                        /* Just smear for photoelectrons, resolution, noise */
01396 
01397                        if(ra_det[85][i1] > 0.0)  /* Photoelectrons */
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;  /*    Mean + ran*sigma       */
01404                            emc_dele_cal[i1][iy][iz] = r_work2 / ra_det[85][i1];
01405                          }
01406 
01407                        /* Overall resolution */
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                        /* Uncorrelated noise */
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                        /* Rescale to get photons right
01419                           February 15, 2000  G. David */
01420                        emc_dele_cal[i1][iy][iz] = 
01421                          emc_dele_cal[i1][iy][iz] * pbgl_rescale;
01422 
01423                        /* Take care of TOF */
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                        /* GEANT energies have been added.
01434                           Now it is time for corrections, a la Henner 
01435                           First you have to figure out particle ID */
01436                        true_track = (int) emc_parent[i1][iy][iz][0][1];
01437 
01438                        /* The following call IS necessary because
01439                           Henner needs the total momentum of incident part */ 
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;    /* Default value */
01449 
01450                        if( idpart == 1)      /* Photons */
01451                          {
01452                            r_etracking =
01453                              r_eexp * (1.003 -
01454                                        0.0531*exp(0.2905*r_porig));
01455                            /* Fudge factor */
01456                            r_etracking = r_etracking / 0.88;
01457                          }
01458 
01459                        if( idpart == 2 || idpart == 3)      /* Electrons */
01460                          {
01461                            r_etracking =
01462                              r_eexp * (0.9777 -
01463                                        0.0469*exp(0.4567*r_porig));
01464                            /* Fudge factor */
01465                            r_etracking = r_etracking / 0.88;
01466                          }
01467 
01468                        if( idpart >= 5 && idpart <= 9)      /* Muons, pions */
01469                          {
01470                            if(r_porig > 0.5)
01471                              { if(r_eexp < 0.35) 
01472                                /* May be min.ion. */
01473                                {
01474                                  /* r_etracking = r_eexp * 1.663; */
01475                                  r_etracking = r_eexp * 1.85;
01476                                }
01477                              else
01478                                {
01479                                  /* Fudge factor */
01480                                  r_etracking = r_etracking * 0.82;
01481                                }
01482                              }
01483                          }
01484 
01485                        if( idpart >= 11 && idpart <= 16)   /* kaon, proton */
01486                          {
01487                            if(r_porig > 0.9)
01488                              { if(r_eexp < 0.4)
01489                                {
01490                                  /* May be min.ion. */
01491                                  r_etracking = r_eexp * 
01492                                    (2.055 - 0.03 * r_porig);
01493                                }
01494                              else
01495                                {
01496                                  /* Fudge factor */
01497                                  r_etracking = r_etracking * 0.9;
01498                                }
01499                              }
01500                          }
01501 
01502                        /* Put in array */
01503                        emc_dele_cal[i1][iy][iz] = r_etracking;
01504 
01505                        /* Take care of TOF */
01506                          /* + 0.3 * sqrt(abs(r_etracking)) */
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                        /* Cherenkov photons were generated; leave it alone */
01516 
01517                        /* Fudge factor added when Markus introduced
01518                           new parametrization: should be double-checked */
01519 
01520                        emc_dele_cal[i1][iy][iz] = 
01521                          emc_dele_cal[i1][iy][iz] * pbgl_ctrk_rescale;
01522 
01523                        /* Take care of TOF */
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                  }  /* Module has non-zero energy */
01536 
01537              }  /* End loop over iz, modules in z direction, PbGl sectors */
01538 
01539          }  /* End loop over iy, modules in y direction, PbGl sectors */
01540 
01541      }   /* End loop over i1, lead glass sectors */
01542 
01543    /*   Fake transformation to "raw data"       */
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            //  Change to include PbGl noise G. David, Dec 2, 2000
01553            //       if(emc_dele_cal[i1][iy][iz] > 0.001)
01554            //       if((emc_dele_cal[i1][iy][iz] > 0.001)||i1>=6)
01555            //  But only for channels where there is some energy already!
01556            // Otherwise it just blows up the output  G. David Dec. 2, 2000
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                // If full pulseshape reconstruction and LED timing, subtract
01580                // offset due to delay and risetime in order to make simulated
01581                // and real data directly comparable
01582                // Also, smear resolution to the observed one:
01583 
01584                if(sim_timing==0&&itype==1) {
01585                  emc_tof[i1][iy][iz] = emc_tof[i1][iy][iz] - 2.2;
01586                  // Add slewing correction for Year-2, because real data now
01587                  // come out as in principle slewing corrected
01588                  // the last term is not really slewing: it corrects for shifts
01589                  // connected to increasing shower depths - not tested beyond 10 GeV!
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                  // Year-2 sector dependent resolution
01598                  if(i1==0) emc_tof[i1][iy][iz] = emc_tof[i1][iy][iz] + 0.375*noise;
01599                  if(i1==1) emc_tof[i1][iy][iz] = emc_tof[i1][iy][iz] + 0.400*noise;
01600                  if(i1==2) emc_tof[i1][iy][iz] = emc_tof[i1][iy][iz] + 0.430*noise;
01601                  if(i1==3) emc_tof[i1][iy][iz] = emc_tof[i1][iy][iz] + 0.540*noise;
01602                  if(i1==4) emc_tof[i1][iy][iz] = emc_tof[i1][iy][iz] + 0.360*noise;
01603                  if(i1==5) emc_tof[i1][iy][iz] = emc_tof[i1][iy][iz] + 0.400*noise;
01604                  */
01605                  // Correction for Year-4 62 GeV effective resolution
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                  // Upper 2 rows in each FEM have slightly worse resolution
01613                  if(iy==10 || iy==11 || iy==22 || iy==23 || iy==34 || iy==35) {
01614                    emc_tof[i1][iy][iz] = emc_tof[i1][iy][iz] + 0.2*noise;
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                // Insert Markus's suggestion for PbGl noise
01630                // G. David, Dec. 2, 2000
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                // End insert
01639 
01640                if(i_lopost < i_minvalue) i_lopost = i_minvalue;
01641                if(i_hipost < i_minvalue) i_hipost = i_minvalue;
01642 
01643                /* Allow only 12 bits for data */
01644                i_lopost = i_lopost & 0x00000FFF;
01645                i_hipost = i_hipost & 0x00000FFF;
01646                i_tof    = i_tof    & 0x00000FFF;
01647 
01648                /*  ************************************************** 
01649                    c
01650                    c            Write out a record
01651                    c
01652                    ********************* ******************************  */
01653 
01654                dEmcRawData[iout_ok].id = iout_ok +1;
01655                dEmcRawData[iout_ok].evno = header[0].event;
01656 
01657                /* Build hardware key            */
01658                switch(itype)
01659                  {
01660                  case 1:
01661                    /* Lead scintillator */
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;  /* Shift 13 bits */
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                   /* Have to rescale it in order to account
01740                      for decreased attenuation length
01741                      Dec. 21, G. David
01742                   dEmcGeaTowerTrack[i_gtotr_id].edep[j] = 
01743                     emc_parent[i1][iy][iz][j][0];
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 }