multiwires_h

/*
**	Copyright 2012-2016, Fermi Research Alliance, LLC.  All rights reserved.
*/
/******************************************************************************/
/* multiwires.h
**
**	This file contains public definitions for ul_multiwire.
**
*/
/******************************************************************************/

#ifndef MULTIWIRES_DEFINED
#define	MULTIWIRES_DEFINED

#include <time.h>

#ifdef NULL
#undef NULL
#endif
#define NULL			0

#define MW_TYPE			1
#define SWIC_TYPE		2
#define REAL_SWIC_TYPE		3
#define SEM_TYPE		4

#define NORMAL_SEM_TYPE         1   /* nominal strip size is <= 2 */
#define TARGET_SEM_TYPE         2   /* nominal strip size is 15 */
#define PROFILE_SEM_TYPE        3   /* nominal strip size is 7 */

#define	MW_DEVICE_NAME_LEN 	32

#define	MW_POSITION_OUT		0
#define	MW_POSITION_IN		2  //for REAL_SWIC_TYPE only has IN or OUT
#define	MW_POSITION_HORZ	1
#define	MW_POSITION_VERT       -1
#define	MW_POSITION_INDETERM   -2  //indeterminent position
#define	MW_POSITION_UNKNOWN	4  //probably hardware problem

#define	MW_SET_OFF		0  /* turn off */
#define	MW_SET_ON		1  /* turn on */

#define	MW_IS_OFF		0  /* parameter is off */
#define	MW_IS_ON		1  /* parameter is on */

#define MW_MISSING_WIRE		1
#define MW_NOISY_WIRE		2

#define MW_NOT_IN_BEAM         -11 /* Multiwire is OUT */

#define MW_CALCLED_NOT_READY	1  /* calculations not finished */
#define MW_DPM_IO_ERROR	       -1  /* DPM-detected I/O error.*/
#define MW_BUF_WRONG_NUM_WIRE  -2  /* Buffer contains wrong number of wires */
#define MW_BUF_WRONG_CHNL_NUM  -3  /* Buffer contains wrong channel number */
#define MW_BUF_WRONG_SCAN_NUM  -4  /* Buffer contains wrong scan number */
#define MW_BUF_NOT_SINGLE_SCAN -5  /* Buffer is not a single-scan data buffer */
#define MW_NOT_AVERAGE_BUFFER  -6  /* Buffer is not an average buffer */
#define MW_BUF_WRONG_WIRE_POS  -7  /* Buffer is for wrong chamber position */
#define MW_AVG_CALC_NOT_READY  -8  /* Average calculations did not finish */
#define MW_BUF_2_ZERO_WORDS    -9  /* First two words in buffer are zero */
#define MW_BUF_3_ZERO_WORDS    -10 /* First three words in buffer are zero */

#define MW_RAW_DATA		1
#define MW_SCALED_DATA		2
#define	MW_MAX_WIRES		48

#define DI_TYPE_EVM		0
#define DI_TYPE_SEQ		1
#define DI_TYPE_DIS		2
#define DI_TYPE_DIG		3
#define DI_TYPE_HV		4
#define DI_TYPE_MEA		5
#define DI_TYPE_RBT		6
#define DI_TYPE_ZER		7
#define DI_TYPE_TUN		8
#define DI_TYPE_LOG		9
#define DI_TYPE_CRD		10
#define DI_TYPE_CRF		11
#define DI_TYPE_CRX		12
#define DI_TYPE_INH		13
#define DI_TYPE_INV		14
#define DI_TYPE_POS		15
#define DI_TYPE_PWR		16
#define DI_TYPE_TYP		17
#define DI_TYPE_SET_SEQ_ON_OFF		18
#define NUM_DI_TYPE     	18
#define LEN_TIME_STAMP		28
#define ALL_MACHINES		0
//#define	MW_MACHINE_LINAC	(MACHINE_LINAC)
//#define	MW_MACHINE_400_MEV_LINE	(MACHINE_400_MEV_LINE)
//#define	MW_MACHINE_BOOSTER_DUMP	(MACHINE_BOOSTER_DUMP)
//#define	MW_MACHINE_MI8_LINE	(MACHINE_MI8_LINE)
//#define	MW_MACHINE_P1_LINE	(MACHINE_P1_LINE)
//#define	MW_MACHINE_P3_LINE	(MACHINE_P3_LINE)
//#define	MW_MACHINE_TEVATRON	(MACHINE_TEVATRON)
//#define	MW_MACHINE_SWITCHYARD	(MACHINE_SWITCHYARD)
//#define	MW_MACHINE_MESON_LINE	(MACHINE_MESON_LINE)
//#define	MW_MACHINE_R20_LINE	(MACHINE_R20_LINE)
//#define	MW_MACHINE_A1_LINE	(MACHINE_A1_LINE)
//#define	MW_MACHINE_ELECTRON_COOLING	(MACHINE_ELECTRON_COOLING)
//#define	MW_MACHINE_DEBUNCHER	(MACHINE_DEBUNCHER)
//#define	MW_MACHINE_MINIBOONE	(MACHINE_MINIBOONE)
//#define	MW_MACHINE_NUMI		(MACHINE_NUMI)
//#define	MW_MACHINE_AP1_LINE	(MACHINE_AP1_LINE)
//#define	MW_MACHINE_AP2_LINE	(MACHINE_AP2_LINE)
//#define	MW_MACHINE_AP3_LINE	(MACHINE_AP3_LINE)
//#define	MW_MACHINE_D_TO_A_LINE	(MACHINE_D_TO_A_LINE)

#define MACHINE_NUMI_AAT	884	/* NuMI AAT SWICs */
/* #define MACHINE_MESON_CENTER	885   MESON Center (replaced by definition in machine.h) */

#define MACHINE_SY_TO_KTEV	889
#define MACHINE_MTA_LINE	890	/* MTA multiwires */
#define MACHINE_RR_INJECTION	891	/* Recycler Ring Injection */
#define MACHINE_RR_EXTRACTION	892	/* Recycler Ring Extraction */


/* 09/24/15 added */
#define MW_MACHINE_M2_LINE	200
#define MW_MACHINE_M3_LINE	300
#define MW_MACHINE_DELIVERY_RING	400
#define MACHINE_UPSTREAM_M4M5	500
#define MACHINE_ABORT_LINE	600


#define MACHINE_MI_TO_DEB	886	/* AP2+AP1 line */
#define MACHINE_ACCUM_TO_MI	887	/* AP3+AP1 line */

#define MACHINE_SWYD_PAGE2	998
#define MACHINE_TEST_USE	999
#define MACHINE_OBSOLETE	9999

#define MW_OPT_NONE				0
#define MW_OPT_RELATIVE_TO_CENTER_MEAN		1

//multiwire systems
#define MW_SYS_8GEV_LINE	0
#define MW_SYS_400MEV_LINE	1
#define MW_SYS_MI8_LINE		2
#define MW_SYS_P1_P2		3
#define MW_SYS_A1_LINE		4
#define MW_SYS_TEVATRON		5
#define MW_SYS_MINIBOONE	6
#define MW_SYS_R20_LINE		7
#define MW_SYS_P3_TO_SY		8
#define MW_SYS_SY_XFER_DUMP	9
#define MW_SYS_SY_MESON		10
#define MW_SYS_MESON_LINE	11
#define MW_SYS_D_TO_A		12
#define MW_SYS_E_COOLING	13
#define MW_SYS_SY_NOMINALS	14
#define MW_SYS_AP3_LINE		15
#define MW_SYS_AP2_LINE		16
#define MW_SYS_AP1_LINE		17
#define MW_SYS_MI_TO_DEB	18
#define MW_SYS_ACCUM_TO_MI	19
#define MW_SYS_MESON_CENTER	20
#define MW_SYS_NUMI_LINE	21
#define MW_SYS_NUMI_AAT		22
#define MW_SYS_MTA_LINE		23
#define MW_SYS_SY_TO_KTEV	24
#define MW_SYS_LINAC_LINE	25
#define MW_SYS_RR_INJECTION	26
#define MW_SYS_RR_EXTRACTION	27

typedef struct MW_EXT_CALCULATIONS 
{
  float	mean;
  float sigma;
  float intensity;
  float chisq;
  char	time_stamp[LEN_TIME_STAMP];
  //
} __attribute__((packed)) MW_EXT_CALCULATIONS;

typedef struct MW_CONFG_INFO
{
  int   machine;
  int   mw_type;
  char  mw_name[MW_DEVICE_NAME_LEN];
  int   mw_controller_num;
  int   mw_channel;
  int   mw_index;
  int   mw_num_channels;
  char  mw_horz_nominal;
  char  mw_vert_nominal;
  float	mw_horz_space;
  float	mw_vert_space;
  int   mw_first_horz_wire;
  int   mw_num_horz_wires;
  int   mw_first_vert_wire;
  int   mw_num_vert_wires;
  short	mw_mxaddr; //multiplexor address for each controller 
  //
} __attribute__((packed)) MW_CONFG_INFO;

typedef struct MW_DEV_INFOR
{
  int	mw_device_avg_index;
  int	mw_device_raw_index;
  int	mw_device_evx_index;
  int	mw_device_evs_index;
  int	swic_device_evm_index;
  int	swic_device_seq_index;
  int	swic_device_dis_index;
  int	swic_device_dig_index;
  int	swic_device_hv_index;
  int	swic_device_mea_index;
  int	swic_device_rbt_index;
  int	swic_device_zer_index;
  int	swic_device_tun_index;
  int	swic_device_log_index;
  int	swic_device_crd_index;
  int	swic_device_crf_index;
  int	swic_device_crx_index;
  int	swic_device_inh_index;
  int	swic_device_inv_index;
  int	swic_device_pwr_index;
  int	swic_device_pos_index;
  int	swic_device_typ_index;
  int	swic_seq_on_off_index;
} __attribute__((packed)) MW_DEV_INFOR;
//
typedef struct  TIME_SPEC
{
  clib_time_t  tv_sec;  // seconds
  int  tv_nsec;         // nanoseconds 
  //
} __attribute__((packed)) TIME_SPEC;
//
typedef struct MiniBooneTimestamp
{
  TIME_SPEC event8fAdjustedTimestamp;
  TIME_SPEC GPS_Timestamp;
  short lastSamplingEvent; // is hardcoded as "1D" event
  short lastMIResetEvent;  // one of the 20,21,23,29,2a,2b,2d,2e
  short n0cEvents;         // Number of 0C event since the last MI reset
  int millis1fSinceLastMIReset; // delta time in ms since the last MI reset
  short n1dEventSinceMIReset;
  short n1dEventSince00Event;
  //
} __attribute__((packed)) MiniBooneTimestamp;
//
typedef struct ErlangEventData
{
  short SampleNum;      //[2], sequence number
  short TriggerSource;  //[4]
  int   EventStamp;     //[8]
  short NowTimeSpecS1;  //[10]
  short NowTimeSpecS2;  //[12]
  short NowTimeSpecNS1; //[14]
  short NowTimeSpecNS2; //[16]
  short TclkEvent;      //[18]
  int   Timestamp;      //[22]
  int   Count;          //[26]
  int   Reserved;       //[30]
  //
} __attribute__((packed)) ErlangEventData;
//
typedef struct SCALED_DATA
{	
  float	h_data[MW_MAX_WIRES];
  float	v_data[MW_MAX_WIRES];
  //
} __attribute__((packed)) SCALED_DATA;
//
typedef struct SEM_CONFG
{
  int  sem_di;
  int  sem_horz_dir;   // actual horizontal directions
  int  sem_vert_dir;   // actual vertical directions
  unsigned char sem_horz_nominal; // horizontal nominal wire num (x2)
  unsigned char sem_vert_nominal; // vertical nominal wire num (x2)
  float  sem_horz_space;          // horizontal wire spacing in mm
  float  sem_vert_space;          // vertical wire spacing in mm
  int    sem_first_horz_wire;
  int    sem_num_horz_wires;
  int    sem_first_vert_wire;
  int    sem_num_vert_wires;
  short  sem_type; // NORMAL_SEM_TYPE TARGET_SEM_TYPE PROFILE_SEM_TYPE 
  //
} __attribute__((packed)) SEM_CONFG;
	
typedef struct MW_DSP_DIR
{
  int 	display_index;
  char	display_name[20];	
  int	is_active;   		   
  int	count_names;	//the number of multiwire scanners in the system
  //
} __attribute__((packed)) MW_DSP_DIR;
//
typedef struct MW_DSP_CONFIG_INFOR
{
  int   display_index;
  int   mw_index;
  char  mw_name[32];
  int   type;              
  int   num_h_wires;
  int   num_v_wires;
  float h_space;
  float v_space;
} __attribute__((packed)) MW_DSP_CONFIG_INFOR;

extern "C"
{	

int mw__get_dev_confg(char const *multiwire_name,MW_DEV_INFOR *dev_infor,int *mw_type);
int mw__get_mb_evtbuf_data(char *multiwire_names,SCALED_DATA **scaled_data,
    		      MiniBooneTimestamp **timeInfor,
		      MW_EXT_CALCULATIONS **calculated_data,
		      int **calculated_data_status,
		      int **position,int *num_measure,
		      int *passed_di=(int *)NULL);		      
int mw__get_mb_boosterbuf_data(char *multiwire_name,SCALED_DATA **scaled_data,
    		      MiniBooneTimestamp **timeInfor,
		      MW_EXT_CALCULATIONS **calculated_data,
		      int **calculated_data_status,
		      int **position,int *num_measure);	
int mw__get_mw_config(char const *multiwire_names,MW_CONFG_INFO *confg_infor);
int mw__get_swic_data_from_lj(char const *multiwire_name,unsigned short node,
    			  unsigned int req_time,int req_di,
			  SCALED_DATA *scaled_data,
		      	  MW_EXT_CALCULATIONS calculated_data[2],
			  int calc_data_status[2],
			  int *position,unsigned int *rpy_time);
void mw__get_swic_device_index(char const *swic_names,int num_swic,int *di,
                           int di_type,int *num_rpy);
int mw__get_swic_position(char const *swic_name,int num_swic,int *positions,
			  int *errors=(int *)NULL);      
void mw__get_swic_time_stamp(char const swic_name[MW_DEVICE_NAME_LEN],
    			char time_stamp[LEN_TIME_STAMP]);
int mw__get_real_swic_position(char const *swic_names,int num_swic,int *positions,
                              int *errors=(int *)NULL);
					      
int mw__is_sem_device(char const *sem_names,int num_dev,int *sem_dis);
int multiwire_cancel_get_lst(int *list_id);
int multiwire_get_data_c(char const *multiwire_name,int max_wires,int *position,
    			  void *wire_data,int *wire_status,
			  MW_EXT_CALCULATIONS *calculated_data,
			  int *calculated_data_status,int data_type,
			  int ftd=0, int *rpy_lstid = (int *)NULL,
			  unsigned int mean_flag = MW_OPT_NONE);
int multiwire_get_hv_status_c(char const *multiwire_names,
			     int num_multiwires,
			     int *hv_on, int *errors);
int multiwire_get_list_c(char const *multiwire_names,int max_wires,
			 int *positions, void *wire_data,int *wire_status,
			 MW_EXT_CALCULATIONS *calculated_data,
			 int *calculated_data_status,
			 int data_type,int num_multiwires,
			 int ftd=0, int *rpy_lstid = (int *)NULL,
			 unsigned int mean_flag = MW_OPT_NONE,
			 short *source_data=(short*)NULL);
int multiwire_get_names_c(int machine,char *passed_name,int *num_multiwires,
			  int max_return,int req_type=MW_OPT_NONE,
			  char *passed_types=(char *)NULL,
			  int *machines=(int *) NULL);
int multiwire_get_position_c(char const *multiwire_names, int *positions,
    			      int req_num_multiwires,int *errors=(int *) NULL);
int multiwire_get_power_status_c(char const *multiwire_names,
				int num_multiwires,
				int *power_on, int *errors=(int *) NULL);
int multiwire_get_scans_list_c(char const *multiwire_names,int max_wires,
    				int num_scans,int *positions,
				float *wire_data,float *average_mean,
				float *peak_mean,int *wire_status,
				MW_EXT_CALCULATIONS *calculated_data,
				int *calculated_data_status,
    				int num_multiwires,int ftd=0);
int multiwire_get_sequencer_status_c(char const *multiwire_names,
				     int num_multiwires,
				     int *sequencer_on, int *errors);
int multiwire_menu_c(int top_row, int left_column, int num_rows, 
    		     int *machine, char *ret_mw_name, 
		     char *selected_type=(char *)NULL);
int multiwire_num_wires_c(char *multiwire_names,int *num_horz,int *num_vert,
    			  int req_num_multiwires);
int multiwire_set_hv_status_c(char const *multiwire_name, int setting_value);
int multiwire_set_position_c(char const *multiwire_name, int position);
int multiwire_set_sequencer_status_c(char const *multiwire_name,
				     int setting_value);
int mw__sem_set_position(int sem_di,int position);
int mw__seq_is_on(char const *swic_name,int *seq_on_off,int *swic_seq_on_off_di);
int mw__swic_get_status(char const *swic_names,int num_dev,int *swic_power_on,
    		     int *swic_hv_on,int *swic_power_errors=(int *)NULL,
		     int *swic_hv_errors=(int *)NULL);		     	       
int mw__swic_reboot(char *swic_name);
int mw__swic_set_position(char const *swic_name,int position);
int mw__get_dipm_data(float wire_data[100][96],int wire_status[100][96],
		      int position[100],MW_EXT_CALCULATIONS calc_data[100][2],
		      int calc_status[100][2],int ftd,int *num_scans,
		      char trig_timestamp[100][LEN_TIME_STAMP+1]);
int mw__get_dipm_multibuf(float wire_data[100][96],int wire_status[100][96],
		      int position[100],MW_EXT_CALCULATIONS calc_data[100][2],
		      int calc_status[100][2],int ftd,int *num_scans,
		      char trig_timestamp[100][LEN_TIME_STAMP+1],
		      int fe_nscans[4],TIME_SPEC *timeSpec = (TIME_SPEC *)NULL);

int get__mw_scans_list(int *mw_dis,int *num_wires,int *channel,
		       int *pst,float *wire_data,float *avg_mean,
		       float *peak_mean,int *wire_status,
		       MW_EXT_CALCULATIONS *calculated_data,
		       int *calculated_data_status,int num_req_dis,
		       int max_num_wires,int num_scans,int ftd);
int current_ul_multiwire_installed(void);
int running_current_ul_multiwire(void);
int running_test_ul_multiwire(void);
char *ul_multiwire_date(void);
char *ul_multiwire_tag(void);
int ul_multiwire_type(void);
void ul_multiwire_version_strings(char *name=(char *) NULL,
    				  char *build_date=(char *) NULL,
				  char *build_host=(char *) NULL,
				  char *build_dir=(char *) NULL,
				  char *tag=(char *) NULL,
				  char *tag_date=(char *) NULL);

int multiwire_detector_type_c(char *multiwire_names, int *types,
			     int num_multiwires=1, int *errors=(int *) NULL);
int multiwire_display_dir_select_c(int top_row, int left_column,
				 MW_DSP_DIR  *selected_item,
				 int user_req_sys_id=CLIB_DEFAULT_ARG);
int multiwire_display_config_c(int system_id,int *rpy_num_multiwires,
			    MW_DSP_CONFIG_INFOR **rpy_mw_config);

int get_calc_index(char *swic_name,int meas_num,int pos,
    			   int *rpy_calc_num);

}
#endif