{ *************************************************************************** }
MODULE mod_def_physics_tables ;
{  Created  29-APR-1989   MICHIGAN STATE UNIVERSITY, TRIGGER CONTROL SOFTWARE }
{ *************************************************************************** }
{ *************************************************************************** }
INCLUDE 
  mod_handle_tracing,
  mod_def_hardware_tables ; 
{ *************************************************************************** }
EXPORT                                                                         
  def_sptrg_table,  {PROCEDURE define spec trigger table                      }
  def_geosec_table, {PROCEDURE define geographic section table                }
  def_TrgTwr_table, {PROCEDURE define calorimeter trigger tower table         }
  def_threshold_table, {PROCEDURE define calorimeter trigger thresholds table }

  trc_err_init_odb_FW, {FLAG trace error while initializing framework         }
  trc_err_init_odb_CT, {FLAG trace error while initializing cal trigger       }

  force_db_sptrg,{INTEGER sptrg number used to force data blocks              } 
  force_db_mode,{BOOLEAN true if a sptrg has been defined to force dblocks    }

  possible_allocation, {ENUMERATED TYPE possible object allocation            }
  object_allocation,   {TYPE of object allocation                             }
  obey_ignore,         {ENUMERATED TYPE of fstd emable mask programming       } 
  sptrg_enable_mask,   {TYPE fstd enable mask bits per byte                   }
  set_of_sptrg,        {TYPE set of spec trigger longword                     }
  signal_status,       {ENUMERATED TYPE of strt digit signal mode             } 

  spec_trig,           {TYPE of specific trigger physics object               }
  sptrg,               {ARRAY OF 32 pointers to sptrg objects                 } 
  geographic_section,  {TYPE is geographic section for acquisition signals    }
  geosec,              {ARRAY of 32 pointers to geo sect objects              }
  targ_acqbandw,       {target use of acquisition bandwidth for autotune      }

  firstetasign,        {CONST smallest existing eta sign                      }
  lastetasign,         {CONST largest  existing eta sign                      }
  firstetamagn,        {CONST smallest existing eta magnitude                 }
  lastetamagn,         {CONST largest  existing eta magnitude                 }
  firstphival,         {CONST smallest existing phi value                     }
  lastphival,          {CONST largest  existing phi value                     }
  first_lt_eta,        {CONST smallest existing Large Tile Eta                }
  last_lt_eta,         {CONST largest  existing Large Tile Eta                }
  first_lt_phi,        {CONST smallest existing Large Tile Phi                }
  last_lt_phi,         {CONST largest  existing Large Tile Phi                }
  firstemrefset,       {CONST smallest existing em reference set              }
  lastemrefset,        {CONST largest  existing em reference set              }
  firsttotrefset,      {CONST smallest existing tot reference set             }
  lasttotrefset,       {CONST largest  existing tot reference set             }
  firstltrefset,       {CONST smallest existing tot reference set             }
  lastltrefset,        {CONST largest  existing tot reference set             }
  
  tree_offset,         {ARRAY of energy adder tree offsets                    }
  tree_correction,     {ARRAY of tier 3 and 4 correction for tree offsets     }

  threshold,           {TYPE   threshold record                               }
  energy_threshold,    {ARRAY of energy threshold records                     }
  count_threshold,     {ARRAY of count  threshold records                     }

  ADC_MeV_per_count,   {     ADC energy scale                                 }
  Energy_MeV_per_count,{ARRAY of energy scales                                }

  cal_trig_tower,      {TYPE of Calorimeter Trigger Tower object              }
  trgtwr,              {ARRAY OF 2*24*32=1536 pointers to cal trigger towers  }

  Cal_Large_Tile,      {TYPE of Calorimeter Trigger Large Tile Object         }
  LgTile,              {ARRAY OF 2*5*4=40 pointers to calorimeter Large Tiles }

  sp_trg_vs_ref_set,   {TYPE mask of 32 bits, one for each sptrg              }
  participation,       {binary ENUMARATED TYPE participate/no_participate     }
  jet_list_progr;      {ARRAY of sets of sptrg numbers                        }

{ *************************************************************************** }
IMPORT 

  state_type, {ON, OFF,}              {from module MOD_HANDLE_TRACING         }
  handle_trc_sys,                     {from module MOD_HANDLE_TRACING         }

  sptrg_number,                       {from module MOD_DEF_HARDWARE_TABLES    }
  eta_polarity,                       {from module MOD_DEF_HARDWARE_TABLES    }
  neg_e, pos_e,
  eta_magnitude,                      {from module MOD_DEF_HARDWARE_TABLES    }
 {e_0,} e_1, {e_2,  e_3,  e_4,  e_5,  e_6,  e_7,  e_8,  e_9,
  e_10, e_11, e_12, e_13, e_14, e_15, e_16, e_17, e_18, e_19,}
  e_20,{e_21, e_22, e_23, e_24,}
  phi_value,                          {from module MOD_DEF_HARDWARE_TABLES    }
 {p_0,} p_1, {p_2,  p_3,  p_4,  p_5,  p_6,  p_7,  p_8,  p_9,
  p_10, p_11, p_12, p_13, p_14, p_15, p_16, p_17, p_18, p_19,
  p_20, p_21, p_22, p_23, p_24, p_25, p_26, p_27, p_28, p_29,
  p_30, p_31,}p_32,
  magn_eta_per_fe_cell, e_1_4, e_17_20, 
  phi_per_fe_half_cell, p_1_8, p_25_32,
 {threshold_reference_set_type,}      {from module MOD_DEF_HARDWARE_TABLES    }
  EMEt_ref,{HDEt_veto,}TOTEt_ref,     {from module MOD_DEF_HARDWARE_TABLES    }
  threshold_reference_set_number,     {from module MOD_DEF_HARDWARE_TABLES    }
  ref_0, ref_3, ref_7,                {from module MOD_DEF_HARDWARE_TABLES    }
  EM_PROM,{HD_PROM, Px_PROM,}Py_PROM, {from module MOD_DEF_HARDWARE_TABLES    }
 {lookup_data_topic,} Et,{L2,}P,      {from module MOD_DEF_HARDWARE_TABLES    }
 {ADC_data_type_per_CTFE_channel,}  EMEtZ0, HDEtZ0,
 {prom_page_value,}  prom_page_1, prom_page_8,
  threshold_comparison_type,          {from module MOD_DEF_HARDWARE_TABLES    }
  EMEt_cmp, TOTEt_cmp, LgTile_cmp,    {from module MOD_DEF_HARDWARE_TABLES    }
  Global_EMEt, Global_TotL2,          {from module MOD_DEF_HARDWARE_TABLES    }
  global_energy_thresholds,           {from module MOD_DEF_HARDWARE_TABLES    }
  Thresh_EMEt, Thresh_HDEt, Thresh_TotEt, 
  Thresh_EML2, Thresh_HDL2, Thresh_TotL2,
  Thresh_MisPt,                       {from module MOD_DEF_HARDWARE_TABLES    }

  object_status ;                     {from module MOD_DEF_HARDWARE_TABLES    }
{ *************************************************************************** }
{ *************************************************************************** }

  %INCLUDE 'SITE_DEPENDENT.CST/LIST'

TYPE

  bit  = [BIT(1)] 0..1     ;
  byte = [BYTE]   0..255   ;

  %INCLUDE 'TABLE_SPEC_TRIG.TYP/LIST'
  %INCLUDE 'TABLE_GEO_SECT.TYP/LIST'
  %INCLUDE 'TABLE_JET_LIST_PROGR.TYP/LIST'
  %INCLUDE 'TABLE_CALTRG_TOWER.TYP/LIST'
  %INCLUDE 'TABLE_LARGE_TILE.TYP/LIST'
  %INCLUDE 'TABLE_THRESHOLD.TYP/LIST'

VAR

  trc_err_init_odb_FW : state_type ; 
  trc_err_init_odb_CT : state_type ; 

  sptrg  : ARRAY [0..31] OF ^spec_trig ;
  geosec : ARRAY [0..31] OF ^geographic_section ;

  targ_acqbandw : INTEGER ; {target use of acquizition bandwidth for autotune }

  force_db_sptrg : INTEGER ;       {this sptrg is used to force data blocks   }
                                   {when the data block spy times out}
  force_db_mode : BOOLEAN := FALSE ; {shows a sptrg has been defined       }

  firstetasign   : eta_polarity ;
  lastetasign    : eta_polarity ;
  firstetamagn   : eta_magnitude ;
  lastetamagn    : eta_magnitude ;
  firstphival    : phi_value ;
  lastphival     : phi_value ;
  first_lt_eta   : magn_eta_per_fe_cell ;
  last_lt_eta    : magn_eta_per_fe_cell ;
  first_lt_phi   : phi_per_fe_half_cell ;
  last_lt_phi    : phi_per_fe_half_cell ;
  firstemrefset  : threshold_reference_set_number ;
  lastemrefset   : threshold_reference_set_number ;
  firsttotrefset : threshold_reference_set_number ;
  lasttotrefset  : threshold_reference_set_number ;
  firstltrefset  : threshold_reference_set_number ;
  lastltrefset   : threshold_reference_set_number ;

  tree_offset    : ARRAY [Global_EMEt..Global_TotL2] OF INTEGER ;
  tree_correction: ARRAY [Global_EMEt..Global_TotL2] OF INTEGER ;

  energy_threshold : ARRAY [Thresh_EMEt..Thresh_MisPt,0..31] OF threshold ;
  count_threshold  : ARRAY [EMEt_cmp..TOTEt_cmp,ref_0..ref_3,0..15] OF threshold ;

  ADC_MeV_per_count    : INTEGER ;
  Energy_MeV_per_count : ARRAY [Global_EMEt..Global_TotL2] OF INTEGER ;

  trgtwr         : ARRAY [pos_e..neg_e,e_1..e_20,p_1..p_32] OF ^cal_trig_tower ;

  LgTiLe         : ARRAY [pos_e..neg_e,e_1_4..e_17_20,p_1_8..p_25_32] 
                          OF ^Cal_Large_Tile ;

  jet_list_progr : ARRAY [EMEt_cmp..LgTile_cmp,ref_0..ref_7] 
                          OF sp_trg_vs_ref_set ;

{ *************************************************************************** }
{ *************************************************************************** }
PROCEDURE def_sptrg_table ; 

VAR 

  sptrgnum  : INTEGER ;
  aotermnum : INTEGER ;

BEGIN

  trc_err_init_odb_FW := CONVERT(state_type, ini_FW_odb_trc_err ) ;
  trc_err_init_odb_CT := CONVERT(state_type, ini_CT_odb_trc_err ) ;

  handle_trc_sys ( TAG := 'INI/ODB%',
               MESSAGE := ' Define Specific Trigger Description Data Base ' ) ;

  FOR  sptrgnum := 0 TO 31 
  DO BEGIN
    NEW ( sptrg[sptrgnum] ) ;
    sptrg[sptrgnum]^ := zero ;
  END ;

  {reminder: firstsptrg and lastsptrg must correspond to }
  { an integral number of fstd cells} 
  FOR  sptrgnum := firstsptrg TO lastsptrg 
  DO BEGIN

    sptrg[sptrgnum]^.status.implemented := TRUE ;
    sptrg[sptrgnum]^.status.working     := TRUE ;
    sptrg[sptrgnum]^.status.in_config   := TRUE ;
    sptrg[sptrgnum]^.allocated          := ALL_NONE ;
    sptrg[sptrgnum]^.status.initialized := FALSE ;

    FOR  aotermnum := 0 TO 255 
    DO sptrg[sptrgnum]^.aoterm[aotermnum].excluded := 1 ;

  END ;

END ;
{ *************************************************************************** }
{ *************************************************************************** }
PROCEDURE def_geosec_table ; 

VAR geosecnum : INTEGER ;

BEGIN

  handle_trc_sys ( TAG := 'INI/ODB%',
               MESSAGE := ' Define Geographic Section Description Data Base ' );

  FOR  geosecnum := 0 TO 31 
  DO BEGIN
    NEW ( geosec[geosecnum] ) ;
    geosec[geosecnum]^ := zero ;
  END ;

  FOR  geosecnum := firstgeosec TO lastgeosec 
  DO BEGIN
    geosec[geosecnum]^.status.implemented := TRUE ;
    geosec[geosecnum]^.status.working     := TRUE ;
    geosec[geosecnum]^.status.in_config   := TRUE ;
    geosec[geosecnum]^.allocated          := ALL_NONE ;
    geosec[geosecnum]^.status.initialized := FALSE ;
  END ;

END ;
{ *************************************************************************** }
{ *************************************************************************** }
PROCEDURE def_trgtwr_table ; 

VAR 

  eta_pol          : eta_polarity ;  
  eta_mag          : eta_magnitude ; 
  phi_val          : phi_value ;     
  lt_eta           : magn_eta_per_fe_cell ;
  lt_phi           : phi_per_fe_half_cell ;

BEGIN

  firstetasign   := CONVERT(eta_polarity                   , first_eta_sign   );
  lastetasign    := CONVERT(eta_polarity                   , last_eta_sign    );
  firstetamagn   := CONVERT(eta_magnitude                  , first_eta_magn   );
  lastetamagn    := CONVERT(eta_magnitude                  , last_eta_magn    );
  firstphival    := CONVERT(phi_value                      , first_phi_val    );
  lastphival     := CONVERT(phi_value                      , last_phi_val     );
  firstemrefset  := CONVERT(threshold_reference_set_number , first_em_ref_set );
  lastemrefset   := CONVERT(threshold_reference_set_number , last_em_ref_set  );
  firsttotrefset := CONVERT(threshold_reference_set_number , first_tot_ref_set);
  lasttotrefset  := CONVERT(threshold_reference_set_number , last_tot_ref_set );
  firstltrefset  := CONVERT(threshold_reference_set_number , first_lt_ref_set );
  lastltrefset   := CONVERT(threshold_reference_set_number , last_lt_ref_set  );

  first_lt_eta   := CONVERT( magn_eta_per_fe_cell, 
                               (ORD(firstetamagn)-1) DIV eta_per_lt ) ;
  last_lt_eta    := CONVERT( magn_eta_per_fe_cell, 
                               (ORD(lastetamagn)-1)  DIV eta_per_lt )  ;
  first_lt_phi   := CONVERT( phi_per_fe_half_cell, 
                               (ORD(firstphival)-1)  DIV phi_per_lt )  ;
  last_lt_phi    := CONVERT( phi_per_fe_half_cell, 
                               (ORD(lastphival)-1)   DIV phi_per_lt )  ;

  handle_trc_sys ( TAG := 'INI/ODB%',
               MESSAGE := ' Define Trigger Tower Description Data Base ' );

  FOR    phi_val := p_1 TO p_32
  DO FOR eta_mag := e_1 TO e_20 
  DO FOR eta_pol := pos_e TO neg_e
  DO BEGIN
    NEW ( trgtwr[eta_pol,eta_mag,phi_val] ) ;
    trgtwr[eta_pol,eta_mag,phi_val]^ := ZERO ;
    trgtwr[eta_pol,eta_mag,phi_val]^.status[EMEtz0].implemented := TRUE ;
    trgtwr[eta_pol,eta_mag,phi_val]^.status[EMEtz0].working     := TRUE ;
    trgtwr[eta_pol,eta_mag,phi_val]^.status[EMEtz0].in_config   := TRUE ;
    trgtwr[eta_pol,eta_mag,phi_val]^.status[EMEtz0].initialized := FALSE ;
    trgtwr[eta_pol,eta_mag,phi_val]^.status[HDEtz0].implemented := TRUE ;
    trgtwr[eta_pol,eta_mag,phi_val]^.status[HDEtz0].working     := TRUE ;
    trgtwr[eta_pol,eta_mag,phi_val]^.status[HDEtz0].in_config   := TRUE ;
    trgtwr[eta_pol,eta_mag,phi_val]^.status[HDEtz0].initialized := FALSE ;
  END ;

  handle_trc_sys ( TAG := 'INI/ODB%',
               MESSAGE := ' Define Large Tile Description Data Base ' );

  FOR    lt_phi  := p_1_8 TO p_25_32
  DO FOR lt_eta  := e_1_4 TO e_17_20 
  DO FOR eta_pol := pos_e TO neg_e
  DO BEGIN
    NEW ( LgTile[eta_pol,lt_eta,lt_phi] ) ;
    LgTile[eta_pol,lt_eta,lt_phi]^ := ZERO ;
    LgTile[eta_pol,lt_eta,lt_phi]^.status.implemented := TRUE ;
    LgTile[eta_pol,lt_eta,lt_phi]^.status.working     := TRUE ;
    LgTile[eta_pol,lt_eta,lt_phi]^.status.in_config   := TRUE ;
    LgTile[eta_pol,lt_eta,lt_phi]^.status.initialized := FALSE ;
  END ;

END ;
{ *************************************************************************** }
{ *************************************************************************** }
PROCEDURE def_threshold_table ; 

VAR
 thrshnum : INTEGER ;
 glob_thr : global_energy_thresholds ;
 cmp_typ  : threshold_comparison_type ;
 ref_num  : threshold_reference_set_number ;

BEGIN

  
  handle_trc_sys ( TAG := 'INI/ODB%',
               MESSAGE := ' Define Global Threshold Description Data Base ' );

  energy_threshold := ZERO ;

  FOR thrshnum := first_emet_sum_cmp TO last_emet_sum_cmp 
  DO BEGIN
    energy_threshold[Thresh_EMEt,thrshnum].status.implemented := TRUE ;
    energy_threshold[Thresh_EMEt,thrshnum].status.working     := TRUE ;
    energy_threshold[Thresh_EMEt,thrshnum].status.in_config   := TRUE ;
    energy_threshold[Thresh_EMEt,thrshnum].allocated          := ALL_NONE ;
  END ;
  FOR thrshnum := first_hdet_sum_cmp TO last_hdet_sum_cmp 
  DO BEGIN
    energy_threshold[Thresh_HDEt,thrshnum].status.implemented := TRUE ;
    energy_threshold[Thresh_HDEt,thrshnum].status.working     := TRUE ;
    energy_threshold[Thresh_HDEt,thrshnum].status.in_config   := TRUE ;
    energy_threshold[Thresh_HDEt,thrshnum].allocated          := ALL_NONE ;
  END ;
  FOR thrshnum := first_totet_sum_cmp TO last_totet_sum_cmp 
  DO BEGIN
    energy_threshold[Thresh_TotEt,thrshnum].status.implemented := TRUE ;
    energy_threshold[Thresh_TotEt,thrshnum].status.working     := TRUE ;
    energy_threshold[Thresh_TotEt,thrshnum].status.in_config   := TRUE ;
    energy_threshold[Thresh_TotEt,thrshnum].allocated          := ALL_NONE ;
  END ;
  FOR thrshnum := first_eml2_sum_cmp TO last_eml2_sum_cmp 
  DO BEGIN
    energy_threshold[Thresh_EML2,thrshnum].status.implemented := TRUE ;
    energy_threshold[Thresh_EML2,thrshnum].status.working     := TRUE ;
    energy_threshold[Thresh_EML2,thrshnum].status.in_config   := TRUE ;
    energy_threshold[Thresh_EML2,thrshnum].allocated          := ALL_NONE ;
  END ;
  FOR thrshnum := first_hdl2_sum_cmp TO last_hdl2_sum_cmp 
  DO BEGIN
    energy_threshold[Thresh_HDL2,thrshnum].status.implemented := TRUE ;
    energy_threshold[Thresh_HDL2,thrshnum].status.working     := TRUE ;
    energy_threshold[Thresh_HDL2,thrshnum].status.in_config   := TRUE ;
    energy_threshold[Thresh_HDL2,thrshnum].allocated          := ALL_NONE ;
  END ;
  FOR thrshnum := first_totl2_sum_cmp TO last_totl2_sum_cmp 
  DO BEGIN
    energy_threshold[Thresh_TotL2,thrshnum].status.implemented := TRUE ;
    energy_threshold[Thresh_TotL2,thrshnum].status.working     := TRUE ;
    energy_threshold[Thresh_TotL2,thrshnum].status.in_config   := TRUE ;
    energy_threshold[Thresh_TotL2,thrshnum].allocated          := ALL_NONE ;
  END ;
  FOR thrshnum := first_mispt_sum_cmp TO last_mispt_sum_cmp 
  DO BEGIN
    energy_threshold[Thresh_MisPt,thrshnum].status.implemented := TRUE ;
    energy_threshold[Thresh_MisPt,thrshnum].status.working     := TRUE ;
    energy_threshold[Thresh_MisPt,thrshnum].status.in_config   := TRUE ;
    energy_threshold[Thresh_MisPt,thrshnum].allocated          := ALL_NONE ;
  END ;

  count_threshold := ZERO ;

  FOR ref_num := ref_0 TO ref_3
  DO BEGIN

    FOR thrshnum := first_emet_cnt_cmp  TO last_emet_cnt_cmp   
    DO BEGIN
      count_threshold[EMEt_cmp,ref_num,thrshnum].status.implemented := TRUE ;
      count_threshold[EMEt_cmp,ref_num,thrshnum].status.working     := TRUE ;
      count_threshold[EMEt_cmp,ref_num,thrshnum].status.in_config   := TRUE ;
      count_threshold[EMEt_cmp,ref_num,thrshnum].allocated          := ALL_NONE ;
    END ;

    FOR thrshnum := first_totet_cnt_cmp TO last_totet_cnt_cmp 
    DO BEGIN
      count_threshold[TotEt_cmp,ref_num,thrshnum].status.implemented := TRUE ;
      count_threshold[TotEt_cmp,ref_num,thrshnum].status.working     := TRUE ;
      count_threshold[TotEt_cmp,ref_num,thrshnum].status.in_config   := TRUE ;
      count_threshold[TotEt_cmp,ref_num,thrshnum].allocated          := ALL_NONE ;
    END ;

  END ;

END ;
{ *************************************************************************** }
{ *************************************************************************** }
END . 
