#include "vme.h"
#include <userint.h>
#include "minirich.h"
#include "toolbox.h"
#include "icfa.h"
#include "H1D.h"
#include "H2D.h"
#include "H3D.h"
#include "HDraw.h"
int p6h;
int hxy=200;
int minirichplothandle=0;
int sa02Printf(const char *format, ...);
typedef struct {
int feb;
int chip;
int ch;
int chx;
int chy;
int pmtx;
int pmty;
int pmtid
} EID2MAPMT;
EID2MAPMT eid2mapmt[144*4];
int icfa_LoadElectronicMap(const char *fname) {
FILE
*fp
= fopen(fname
,"r");
int febidx, feb,chip,ch,chx,chy,pmtx,pmty,pmt;
int nread=0;
int ndim=400;
char line[400];
int nr=0;
// file should be sorted feb [0..3], chip[0..3], ch [0.. 35]
while (fgets(line
,ndim
,fp
)!=NULL
) {
// FEBCH PMTCH X Y PMTX PMTY PMTID
nr
= sscanf(line
,"%d%d%d%d%d",&febidx
,&ch
,&chx
,&chy
,&pmt
);
if (nr) {
chip = febidx / 36;
ch = febidx % 36;
pmtx = pmt / 3;
pmty = pmt % 3;
for (feb = 0; feb<4; feb++){
// 4 febs
// 0 2
// 1 3
int fx = feb %2;
int fy = feb /2;
eid2mapmt[nread+feb*144].feb = feb;
eid2mapmt[nread+feb*144].chip = chip;
eid2mapmt[nread+feb*144].ch = ch ;
eid2mapmt[nread+feb*144].chx = chx + fx * 18;
eid2mapmt[nread+feb*144].chy = chy + fy * 18;
eid2mapmt[nread+feb*144].pmtx = pmtx ;
eid2mapmt[nread+feb*144].pmty = pmty ;
}
}
nread++;
}
sa02Printf("eid2pmtxy %d records read.\n",nread);
return 0;
}
int icfa_GetPosition(unsigned int febchid, int *chx, int *chy) {
if (febchid>=144*4) return -1;
*chx = eid2mapmt[febchid].chx;
*chy = eid2mapmt[febchid].chy ;
return 0;
}
int icfa_Fill(int febchid, double weight){
int x;
int y;
icfa_GetPosition(febchid, &x, &y);
H2DFillBin(hxy, x,y,weight);
return 0;
}
int icfa_Init(){ // read mapmt mapping
icfa_LoadElectronicMap("eid2pmtxy.map");
//short VMUSB_VME_DGG(unsigned short channel, unsigned short trigger,unsigned short output,
// long delay, unsigned short gate, unsigned short invert, unsigned short latch);
// set DGG 1 to create ADC gate, I1 -> O1 delay:0, gate 0x400 = 12us
WIENER_VMUSB_VME_DGG(0,1,0,0x0,0x100,0,0);
/*
channel
[in] The DGG channel you wish to modify. Valid values are:
0 For DGG channel A
1 For DGG channel B
trigger
[in] Determines the start of the DGG. Valid Values are:
0 Channel Disabled
1 NIM input 1
2 NIM input 2
3 Event Trigger
4 - End of Event
5 - USB Trigger
6 - Pulser
output
[in] Determines the NIM ouput used for the DGG channel. Valid values are:
0 NIM O1
1 NIM O2
delay
[in] 32 bit word in steps of 12.5ns between trigger and start of gate consisting of
lower 16 bits: Delay_fine
upper 16 bits: Delay_coarse
gate
[in] Sets the length of the gate in units of 12.5ns
invert
[in] Determines whether or not the DGG is inverted. Valid values are:
0 Not inverted
1 Is inverted
latch
[in] Determines whether or not the DGG is latched. Valid values are:
0 Not latched
1 Is latched
*/
// User Devices Source Selector - Read/Write Offset = 16 0x10
// xxusb_register_write(udev, XXUSB_ACTION_REGISTER, 0x2); // USB Trigger
WIENER_VMUSB_VME_DGG(0,5,0,0xF,0xF,0,0);
//WIENER_VMUSB_VME_Output_settings(0,7,0,0); // nekaj od tega bi moralo delat
WIENER_VMUSB_register_write(10,0x2); // USB Trigger
return 0;
}
int icfa_Histo(){ // read mapmt mapping
H2DInit(hxy,"minirich","MiniRICH hit map",36,0, 1,36,0, 1);
H2DSetTitleX(hxy,"x (channels)");
H2DSetTitleY(hxy,"y (channels)");
return 0;
}
int icfa_Draw(){
if (minirichplothandle > 0 ) {
DeleteGraphPlot (p6h, MINIRICH_GRAPH, minirichplothandle, VAL_IMMEDIATE_DRAW);
minirichplothandle=0;
}
H2DDraw(hxy,p6h, MINIRICH_GRAPH, &minirichplothandle);
return 0;
}
int icfa_Event(uint32_t *d, int mode){
if (!mode) H2DClear(hxy);
for (int i=0;i<144*4;i++){
if (d[i]>0) icfa_Fill(i,d[i]);
}
return 0;
}
int CVICALLBACK daq_icfa(void *functionData) {
int neve;
int threshold;
icfa_Init();
GetCtrlVal(p6h,MINIRICH_NEVE, &neve);
GetCtrlVal(p6h,MINIRICH_THR, &threshold);
icfa_Histo();
for (int k=0;k<144*4;k++){
int i = Random(0,144*4);
icfa_Fill(k,1);
}
icfa_Draw(hxy);
return 0;
}
int CVICALLBACK rich_Draw_RingCB (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2) {
static int oh1=0;
static int oh2=0;
switch (event) {
case EVENT_COMMIT: {
double min_r;
double max_r;
double x,y;
GetCtrlVal (p6h, MINIRICH_R_MIN, &min_r ) ;
GetCtrlVal (p6h, MINIRICH_R_MAX, &max_r ) ;
double r1=min_r*2.0 ;
double r2=max_r*2.0 ;
GetGraphCursor (p6h, MINIRICH_GRAPH, 1, &x, &y);
sa02Printf ( "x:%5.2f\ty:%5.2f OH1=%d OH2=%d\n", x, y, oh1, oh2 ) ;
if (oh1 > 0 ) {
DeleteGraphPlot (p6h, MINIRICH_GRAPH, oh1, VAL_IMMEDIATE_DRAW);
oh1=0;
}
if (oh2 > 0 ) {
DeleteGraphPlot (p6h, MINIRICH_GRAPH, oh2, VAL_IMMEDIATE_DRAW);
oh2=0;
}
oh1 = PlotOval (p6h, MINIRICH_GRAPH, x-r1, y-r1, x+r1, y+r1, VAL_RED, VAL_TRANSPARENT);
oh2 = PlotOval (p6h, MINIRICH_GRAPH, x-r2, y-r2, x+r2, y+r2, VAL_RED, VAL_TRANSPARENT);
break;
}
}
return 0;
}
int CVICALLBACK rich_Histo_ClearCB (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2) {
switch (event) {
case EVENT_COMMIT:
H2DClear(hxy);
icfa_Draw();
break;
}
return 0;
}
int CVICALLBACK icfa_StartCB (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2) {
switch (event) {
case EVENT_COMMIT:
daq_icfa(NULL);
break;
}
return 0;
}
extern HColorMap *colormap;
int CVICALLBACK SetMaximum (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2) {
switch (event) {
case EVENT_COMMIT:
if (minirichplothandle> 0 ) DeleteGraphPlot (panel, MINIRICH_GRAPH, minirichplothandle, VAL_IMMEDIATE_DRAW);
int histogram=hxy;
double min = H2DGetMin(histogram);
double fx=0;
GetCtrlVal(p6h,MINIRICH_MAXZ, &fx);
double max = H2DGetMax(histogram)*fx/100.;
RangeColors(min,max);
minirichplothandle = PlotScaledIntensity (panel, MINIRICH_GRAPH,
H2DGetData(histogram),
H2DGetNbinsX(histogram),
H2DGetNbinsY(histogram),
VAL_DOUBLE,
H2DGetStepY(histogram),
H2DGetMinY(histogram),
H2DGetStepX(histogram),
H2DGetMinX(histogram),
colormap->array,
colormap->HiColor,
colormap->numberofColors, 1, 0);
break;
}
return 0;
}