Subversion Repositories f9daq

Compare Revisions

Ignore whitespace Rev 32 → Rev 33

/sipmScan/src/analysisBias.cpp
0,0 → 1,523
//////////////////////////////////////////
// Data to root conversion root script
//
// Contributors: Rok Pestotnik, Rok Dolenec, Dino Tahirovic
//
// 3/1/2014 TDC cut relative to tdc offset
//
 
#include "stdio.h"
#include "TROOT.h"
#include "TFile.h"
#include "TNtuple.h"
#include "TH1F.h"
#include "TH2F.h"
#include "TF1.h"
#include "TMath.h"
#include "TStyle.h"
#include "TCanvas.h"
#include "TLine.h"
#include "zlib.h"
 
// ------------------------------------------------------------------------------
 
#define POSMARG 1000
 
#define READBUFFERLENGTH 10000
 
// data format
#define MAXDATA 16
#define NCH 64
#define TDC_BIN 1.0416 // 1 TDC bin in ns
#define MIKRO_BIN 0.49609/1000. //1 mikro step in mm; stage MM3MF
#define OFFSETX 5100 // Right edge of SiPM+Lightguide
#define OFFSETY 5200 // Lower edge of SiPM+Lightguide
 
#define RUNREC_ID 1
#define ENDREC_ID 2
#define POSREC_ID 3
#define EVTREC_ID 4
#define THRREC_ID 5
 
typedef struct {
unsigned int id,len;
unsigned int fver,time;
unsigned int nev,nch,ped,xy;
unsigned int thLow, thUp, thStep;
int nx,x0,dx,ny,y0,dy;
} RUNREC;
RUNREC *runrec;
RUNREC run;
 
typedef struct {
unsigned int id,len;
unsigned int time;
} ENDREC;
ENDREC *endrec;
 
typedef struct {
unsigned int id,len;
unsigned int time;
int ix;
int x;
int xset;
int iy;
int y;
int yset;
} POSREC;
POSREC *posrec;
POSREC pos;
 
typedef struct {
unsigned int id;
unsigned int len;
unsigned int nev;
} EVTREC;
EVTREC *evtrec;
 
typedef struct {
unsigned int id;
unsigned int len;
unsigned int threshold;
} THRREC;
THRREC *thrrec;
THRREC thr;
 
double padCenter[NCH][2];
 
int position(int, int, int);
 
// ------------------------------------------------------------------------------
 
int d2r(char* dfile0="test", int dbg=0, double tdcCut=5.0)
{
 
const double c_tdcOffset = +2.5; // ns
printf(" Data to root conversion program\nUsage:\nd2r(input file name <without .dat>, debug on/off, TDC cut +-[ns])\n\n");
char fullname[256];
char sbuff[256];
FILE *fp;
//Chanel information
double tdcOffset[NCH];
sprintf(fullname, "d2r.ini");
if( (fp=fopen(fullname, "rt")) == NULL )
printf("Cannot open pad centers file %s !!!\n", fullname);
else {
printf("Opened pad centers file %s\n", fullname);
char* result = fgets(sbuff,256, fp);
if (dbg) printf("Read buffer %s\n", result);
printf("%s", sbuff);
for(int i=0; i<NCH; i++) {
int channel;
int message = fscanf(fp, "%d %lf %lf %lf\n", &channel, &padCenter[i][0], &padCenter[i][1], &tdcOffset[i]);
if (dbg) printf("Read d2r.ini returned %d\n", message);
}
fclose(fp);
}
for(int i=0; i<NCH; i++) {
tdcOffset[i] += c_tdcOffset;
printf("%.2lf %.2lf %.2lf\n", padCenter[i][0], padCenter[i][1], tdcOffset[i]);
}
//TDC correction parameters
/*
double corpar[NCH][3]={ {-0.8, 25, 0},
{-0.8, 25, 0},
{-0.8, 25, 0},
{-0.8, 25, 0},
{-0.8, 25, 0},
{-0.8, 25, 0},
{-0.8, 25, 0},
{-0.8, 25, 0} };
sprintf(fullname, "data/%s_cor.txt", dfile0);
if( (fp=fopen(fullname, "rt")) == NULL )
printf("Cannot open parameter file %s !!!\n", fullname);
else {
printf("Opened parameter file %s\n", fullname);
for(int i=0; i<NCH; i++) {
fscanf(fp, "%lf %lf %lf\n", &corpar[i][0], &corpar[i][1], &corpar[i][2]);*/
/*// check if parameters make sense
if( (corpar[i][0] < (tdcmi-0.2*tdcmi)) || ((tdcma+0.2*tdcma) < corpar[i][0]) ||
(corpar[i][1] < 0) || (1e4*TDC_BIN < corpar[i][1]) ||
(1e4 < TMath::Abs(corpar[i][2])) ) {
printf("Warning: parameters for ch%d out of limits -> using default!\n", i);
corpar[i][0]=2200*TDC_BIN; corpar[i][1]=1000*TDC_BIN; corpar[i][2]=-100*TDC_BIN;
}*/
/* }
fclose(fp);
}*/
//for(int i=0; i<NCH; i++) printf("%.2lf %.2lf %.2lf\n", corpar[i][0], corpar[i][1], corpar[i][2]);
//histograms
char hname[256];
//double tdc;
TH2F *htdc;
TH2F* h_correctedTDC;
TH1F *hnhitsx[NCH], *hnhitsy[NCH];
TH2F *h2d[NCH];
TH2F *h_threshold;
TH2F *h_ch33;
TNtuple *nt;
//data buffer
unsigned int readbuf[READBUFFERLENGTH];
unsigned int buf[READBUFFERLENGTH];
//data file
gzFile dfp;
char dfile[256];
int ftype=0;
int fcount=1;
do {
switch(ftype++) {
case 0:
sprintf(dfile, "./data/%s_file%02d.dat", dfile0, fcount);
break;
case 1:
sprintf(dfile, "./data/%s_file%02d.dat.gz", dfile0, fcount);
break;
case 2:
sprintf(dfile, "./data/%s_file%02d.gz", dfile0, fcount);
break;
default:
printf(" Cannot find data file for %s !!!\n", dfile0);
return -1;
}
dfp=gzopen(dfile,"rb");
} while(!dfp);
printf("Opened data file %s\n", dfile);
//opens ROOT file
//TFile *rootfile;
char fnameroot[256];
sprintf(fnameroot, "root/%s.root", dfile0);
//rootfile = (TFile *) gROOT->FindObjectAny(dfile0);
//if (rootfile!=NULL) {printf("!!!\n");rootfile->Close();}
//rootfile = new TFile(fnameroot);
//if(rootfile) rootfile->Close();
TFile* rootfile = new TFile(fnameroot,"RECREATE",dfile0);
// -----------------------------------------------
// loop trough records
unsigned int rec_id, rec_len;
unsigned int ulsize = sizeof(unsigned int);
//unsigned int ulsize = 4;
if (dbg) printf("Size of unsigned int: %lu\n", sizeof(unsigned int));
int ceve=0;
int end_of_file = 0;
int ii;
int nint;
int nb;
int nSteps;
int status;
while(1) {
if(gzeof(dfp)) end_of_file = 1;
 
gzread(dfp, (voidp)&readbuf, 2*ulsize);
rec_id=readbuf[0];
rec_len=readbuf[1];
if(dbg) printf("-----------------------------------------------\n");
if(dbg) printf("[%d] rec_id = %d | rec_len = %u\n", ceve, rec_id, rec_len);
 
switch(rec_id)
{
case RUNREC_ID:
gzread(dfp, (voidp)&readbuf[2], (rec_len-2*ulsize));
runrec = (RUNREC*) readbuf;
run = *runrec;
if(dbg) {
printf("RUNREC_ID\n");
printf("id = %d, len = %d, time = %d\n", run.id, run.len, run.time);
printf("nev = %d, nch = %d\n", run.nev, run.nch);
printf("nx = %d, x0 = %d, dx = %d\n", run.nx, run.x0, run.dx);
printf("ny = %d, y0 = %d, dy = %d\n", run.ny, run.y0, run.dy);
printf("thLow = %d, thUp = %d, thStep = %d\n", run.thLow, run.thUp, run.thStep);
}
//create histograms
nt = new TNtuple("nt", "nt", "ch:x:y:tdc");
sprintf(hname, "htdc");
htdc = (TH2F*) gROOT->FindObject(hname);
if (htdc) delete htdc;
htdc = new TH2F("htdc","Raw TDC;TDC channel;SiPM channel",512,0,512,NCH,0,NCH);
h_correctedTDC = (TH2F*) gROOT->FindObject("h_correctedTDC");
if (h_correctedTDC) delete h_correctedTDC;
h_correctedTDC = new TH2F("h_correctedTDC","Corrected TDC;t [ns];SiPM channel",33, -16.5*TDC_BIN,16.5*TDC_BIN,NCH,0,NCH);
//TH1F* gsumV673A[NCH/16] = new TH1F(hn,hname,256,-0.5,255.5);
for(int i=0; i<NCH; i++) {
/*
sprintf(hname, "htdcpos%d", i);
htdcpos[i] = (TH1F*)gROOT->FindObject(hname);
if(htdcpos[i]) delete htdcpos[i];
htdcpos[i] = new TH1F(hname, hname, 512, 0, 512*TDC_BIN);
sprintf(hname, "htdc%d", i);
htdc[i] = (TH1F*)gROOT->FindObject(hname);
if(htdc[i]) delete htdc[i];
htdc[i] = new TH1F(hname, hname, 512, 0*TDC_BIN, 512*TDC_BIN);
*/
sprintf(hname, "hnhitsx%d", i);
hnhitsx[i] = (TH1F*)gROOT->FindObject(hname);
if(hnhitsx[i]) delete hnhitsx[i];
hnhitsx[i] = new TH1F(hname, hname, run.nx,
(run.x0 - OFFSETX - 0.5*run.dx)*MIKRO_BIN,
(run.x0 - OFFSETX + (run.nx-0.5)*run.dx)*MIKRO_BIN);
sprintf(hname, "hnhitsy%d", i);
hnhitsy[i] = (TH1F*)gROOT->FindObject(hname);
if(hnhitsy[i]) delete hnhitsy[i];
//hnhitsy[i] = new TH1F(hname, hname, run.ny, (run.y0-0.5*run.dy)*MIKRO_BIN, (run.y0+(run.ny-0.5)*run.dy)*MIKRO_BIN);
hnhitsy[i] = new TH1F(hname, hname, run.ny,
(run.y0 - 0.5*run.dy - OFFSETY)*MIKRO_BIN,
(run.y0 + (run.ny-0.5)*run.dy - OFFSETY)*MIKRO_BIN);
//hnhitsy[i] = new TH1F(hname, hname, 100, 0,100);
sprintf(hname, "h2d%d", i);
h2d[i] = (TH2F*)gROOT->FindObject(hname);
if(h2d[i]) delete h2d[i];
h2d[i] = new TH2F(hname, hname, run.nx,
(run.x0 - OFFSETX - 0.5*run.dx)*MIKRO_BIN,
(run.x0 - OFFSETX + (run.nx-0.5)*run.dx)*MIKRO_BIN,
run.ny,
(run.y0 - OFFSETY - 0.5*run.dy)*MIKRO_BIN,
(run.y0 - OFFSETY + (run.ny-0.5)*run.dy)*MIKRO_BIN);
}
if (h_threshold) delete h_threshold;
nSteps = (run.thUp - run.thLow)/double(run.thStep) + 1;
if (dbg) printf("nSteps %d\n", nSteps);
h_threshold = new TH2F("h_threshold","Threshold scan;SiPM ch;Threshold[mV]",64,-0.5,63.5,
nSteps,
run.thLow - 0.5*run.thStep,
run.thUp + 0.5*run.thStep);
//h_threshold = new TH2F("h_threshold","Threshold scan;SiPM ch;Threshold[mV]",64,-0.5,63.5,
// 101,995,2005);
if (h_ch33) delete h_ch33;
h_ch33 = new TH2F("h_ch33","ch. 33;x;y",100,20000,30000,100,0,10000);
break;
case POSREC_ID:
gzread(dfp, (voidp)&readbuf[2], (rec_len-2*ulsize));
posrec = (POSREC *) readbuf;
pos=*posrec;
if(dbg) {
printf("POSREC_ID\n");
printf("id = %d, len = %d, time = %d\n", posrec->id, posrec->len, posrec->time);
printf("ix = %d, x = %d, xset = %d\n", posrec->ix, posrec->x, posrec->xset);
printf("iy = %d, y = %d, yset = %d\n", posrec->iy, posrec->y, posrec->yset);
} else printf(" [%d,%d] %d, %d\n", pos.ix, pos.iy, pos.xset, pos.yset);
break;
case EVTREC_ID:
gzread(dfp, (voidp)&readbuf[2], ulsize); // last field of event record
evtrec = (EVTREC *) readbuf;
//evtrec->nev = buf[0];
//if (rec_len < 0 || rec_len > 10000) {
if (rec_len > READBUFFERLENGTH) {
printf("Len %u\n", rec_len);
return(0);
}
nb = rec_len - 3*ulsize; // no. of bytes to read
gzread(dfp, (voidp)&buf, nb);
if(dbg) {
printf("EVTREC_ID\n");
printf("id = %d, len = %d, nev = %d\n", evtrec->id, evtrec->len, evtrec->nev);
//for(int datai = 0; datai < NCH; datai++) printf("%u ", evtrec->data[datai]);
//printf("\n");
//for(int datai = NCH; datai < NCH+NCH; datai++) printf("%u ", evtrec->data[datai]);
//printf("\n");
break;
}
nint = nb / ulsize; // no. of subrecords
if (dbg) printf("No. of subrecords %d \n", nint);
ii=0;
while (ii<nint){
int recid = buf[ii++];
int len = buf[ii++];
if (dbg) printf("Buffer pointer %d\n", ii);
unsigned int *dbuf = (unsigned int *)&buf[ii];
//if (n%1000==0)
if (dbg) printf("%d 0x%03x Len=%d\n",evtrec->nev,recid,len);
//unsigned short edge;
//int nhits;
if (recid==0x140 || recid==0x141) {
for (int i=0; i<len; i++) {
int data = dbuf[i] & 0xFFFF ;
int edge_type = (dbuf[i]>>16)&0x1 ;
int overflow = (dbuf[i]>>17)&0x1 ;
int tdc_num = (dbuf[i]>>25)&0x1 ;
int channel = ((dbuf[i]>>18)&0x1F) | tdc_num<<5 ;
int ev_dat = (dbuf[i]>>23)&0x1 ;
int last_dat = (dbuf[i]>>30)&0x1 ;
int nval_dat = (dbuf[i]>>31)&0x1 ;
if (dbg){
if (ev_dat) printf("Event %d\n",data);
else printf("ch=%d edge=%d ev=%d data=%d last=%d nval=%d\n",
channel, edge_type,ev_dat,data,last_dat,nval_dat);
}
if (!ev_dat){
if (!edge_type && !overflow) {
htdc->Fill(data, channel);
if(dbg) printf("ch: %d tdc: %d\n", channel, data);
if (dbg) nt->Fill(channel, pos.ix, pos.iy, data);
double tdcmin=tdcOffset[channel] - tdcCut;
double tdcmax=tdcOffset[channel] + tdcCut;
double time = data*TDC_BIN;
if(time >= tdcmin && time <= tdcmax) {
h_correctedTDC->Fill((time - tdcOffset[channel]), channel);
hnhitsx[channel]->Fill((pos.xset - OFFSETX) * MIKRO_BIN);
hnhitsy[channel]->Fill((pos.yset - OFFSETY) * MIKRO_BIN);
h2d[channel]->Fill((pos.xset - OFFSETX) * MIKRO_BIN, (pos.yset - OFFSETY) * MIKRO_BIN);
//h_threshold->Fill(channel, thr.threshold);
if (position(pos.xset-OFFSETX, pos.yset-OFFSETY, channel)) {
h_ch33->Fill(pos.xset-OFFSETX, pos.yset-OFFSETY);
h_threshold->Fill(channel, thr.threshold);
}
}
//gV673A->Fill(data,channel);
//gsumV673A[channel/16]->Fill(data);
}
}
if (last_dat) break;
}
} // if (recid== 0x140 || recid== 0x141)
ii += len;
} //while
 
// events ------------------------------------------------------------------------------------------
// fill histograms
/*
for(int i=0; i<NCH; i++) {
//tdc=((double)evtrec->data[i])*TDC_BIN - tdcoffset[i];
//adc=(double)evtrec->data[i+NCH]-adcoffset[i];
if (gNtWrite) nt->Fill(i,pos.ix,pos.iy,tdc);
//if( (qdcmi < adc) && (adc < qdcma) ) {
htdc[i]->Fill(tdc);
//}
//hadc[i]->Fill(adc);
//hcor[i]->Fill(adc,tdc);
//if(adc > corpar[i][2])
//hctdc[i]->Fill( tdc - (corpar[i][0] + corpar[i][1]/TMath::Sqrt(adc - corpar[i][2])) );
 
if( (abs(padcenter[i][0] - pos.xset) < POSMARG) && (abs(padcenter[i][1] - pos.yset) < POSMARG) ) {
htdcpos[i]->Fill(tdc);
//hadcpos[i]->Fill(adc);
//hcorpos[i]->Fill(adc,tdc);
//if(adc > corpar[i][2])
//hctdcpos[i]->Fill( tdc - (corpar[i][0] + corpar[i][1]/TMath::Sqrt(adc - corpar[i][2])) );
}
if((tdcmi < tdc) && (tdc < tdcma)) {
//hadc_cut[i]->Fill(adc);
hnhitsx[i]->Fill((pos.xset - OFFSETX) * MIKRO_BIN);
hnhitsy[i]->Fill((pos.yset - OFFSETY) * MIKRO_BIN);
h2d[i]->Fill((pos.xset - OFFSETX) * MIKRO_BIN, (pos.yset - OFFSETY) * MIKRO_BIN);
//if(i==4) adc1=adc;
//if(i==5) adc2=adc;
}
} */
break;
case THRREC_ID:
status = gzread(dfp, (voidp)&readbuf[2], (rec_len-2*ulsize));
thrrec = (THRREC*) readbuf;
thr = *thrrec;
if (dbg) printf("THRREC id = %d len = %d threshold %d\n",
thrrec->id, thrrec->len, thrrec->threshold);
break;
case ENDREC_ID:
gzread(dfp, (voidp)&readbuf[2], (rec_len-2*ulsize));
endrec = (ENDREC *) readbuf;
if(dbg) {
printf("ENDREC_ID\n");
printf("id = %d, len = %d, time = %d\n", endrec->id, endrec->len, endrec->time);
} else printf(" ENDREC\n");
fcount++;
switch(ftype-1) {
case 0:
sprintf(dfile, "./data/%s_file%02d.dat", dfile0, fcount);
break;
case 1:
sprintf(dfile, "./data/%s_file%02d.dat.gz", dfile0, fcount);
break;
case 2:
sprintf(dfile, "./data/%s_file%02d.gz", dfile0, fcount);
break;
}
if(dfp) gzclose(dfp);
dfp=gzopen(dfile,"rb");
if(!dfp) {
printf(" Cannot open data file: %s ---> Exiting\n", dfile);
end_of_file = 1;
} else {
printf(" Opened data file: %s\n", dfile);
end_of_file = 0;
}
break;
default:
printf("switch(rec_id): default !!!\n");
end_of_file = 1;
break;
}
ceve++;
if( (ceve%50000) == 0) printf(" Current event = %d\n", ceve);
//if(dbg) if( ceve>dbg ) break;
if(end_of_file) break;
}
 
if(dfp) {
gzclose(dfp);
delete dfp;
}
if(dbg) return 1;
if(rootfile) {
nt->Write();
rootfile->Write();
printf("Saved to %s\n", fnameroot);
rootfile->Close();
delete rootfile;
}
return 1;
}
 
int position(int x, int y, int channel)
{
int flag = 0;
if ( (x > (padCenter[channel][0] - 5000)) && (x < (padCenter[channel][0] + 5000)) &&
(y > (padCenter[channel][1] - 5000)) && (y < (padCenter[channel][1] + 5000)) ) flag = 1;
return flag;
}
/sipmScan/src/sipmBias.c
0,0 → 1,686
#define USE_DAQ
#define USE_MIKRO
 
// Izberi ustrezni interface v meniju projektnega okna
// Options->CompilerDefines (dodaj /DSISVME ali /DWIENVME)
#ifdef USE_DAQ
# define USE_CAMAC
#include "c:\HOME\dino\sipmScan\include\camac.h"
# ifdef SISVME
# include "sisvme_dll.h"
# endif
# ifdef WIENVME
# include "wienvme_dll.h"
# endif
# include "CAENV673A.h"
# include "CAENV462.h"
#endif
 
#ifdef USE_MIKRO
# include "MIKRO.h"
#endif
 
#include <userint.h>
#include <ansi_c.h>
#include <utility.h>
#include <analysis.h>
 
#include "c:\HOME\dino\sipmScan\include\sipmBias_ui.h"
 
#ifdef USE_DAQ
# define USE_CAMAC
# include "camac.h"
# define NDAC 1
 
# define CAEN_V673A 0x22220000 // IJS
//#define CAEN_V673A 0x10110000 // FMF
# define CAEN_V462 0x100300
#endif
 
#ifdef USE_MIKRO
# define MIKRO_COM 5
# define MIKRO_X 1
# define USE_MIKRO_Y
# define MIKRO_Y 2
# define USE_MIKRO_Z
# define MIKRO_Z 3
# define STEP_TOLERANCE 50
#endif
 
#define MAXCH 512
#define MAX_THREADS 10
 
#define IWAIT 200
 
#define NCH 64
 
static int p1h, pID, rID, tfID;
static int ph_tdc, ph_adc;
static int dtdc[NCH][2][MAXCH];
static int something[NCH][2][MAXCH];
 
static int daq_on;
static int poolHandle = 0;
static int ntics,dummy;
 
/************************************************************/
void wait_loop(unsigned long iloop)
 
{
int i;
for (i=0;i<iloop;i++);
return;
}
 
int CVICALLBACK cb_timer (int panel, int control, int event, void *callbackData,
int eventData1, int eventData2)
{
QueueUserEvent (9000, p1h, P1_TIMER);
return (0);
}
 
int update_plots (void)
{
int irange, ch;
GetCtrlVal (p1h, P1_PLCH, &ch);
if (ph_tdc>0) DeleteGraphPlot (p1h, P1_TDC, ph_tdc, VAL_DELAYED_DRAW);
GetCtrlVal (p1h, P1_TDCHL, &irange);
ph_tdc = PlotY (p1h, P1_TDC, &dtdc[ch][irange], MAXCH, VAL_INTEGER,
VAL_VERTICAL_BAR, VAL_EMPTY_SQUARE, VAL_SOLID, 1, VAL_RED);
 
if (ph_adc>0) DeleteGraphPlot (p1h, P1_ADC, ph_adc, VAL_DELAYED_DRAW);
GetCtrlVal (p1h, P1_ADCHL, &irange);
ph_adc = PlotY (p1h, P1_ADC, &something[ch][irange], MAXCH, VAL_INTEGER,
VAL_VERTICAL_BAR, VAL_EMPTY_SQUARE, VAL_SOLID, 1, VAL_BLUE);
return (0);
}
 
 
int module_header(int recid,unsigned long *data,int len){
data[0] = recid;
data[1] = (len >0)? len : 0 ;
return data[1]+2;
}
 
int SetDac(int, double);
 
int CVICALLBACK daq_run(void *functionData)
{
// ***
// variable declarations
int i,j;
int dtype,ch,rg,adc,cres,bsr;
//unsigned short aa[NCH][4];
unsigned long tdc[NCH];
unsigned long step_minutes, end_time_s, cur_time_s;
int start_hours, start_minutes, start_seconds;
 
int status,fmax,fcount,fev;
int popupret;
int mikroX; // check the position
int mikroY; // check the position
 
char dfile[MAX_PATHNAME_LEN],dfile0[MAX_PATHNAME_LEN];
int supr0,tdcmin,fseed;
float frac;
double fracg;
unsigned short dum16;
int count;
int print = 0;
int timer_out = 0;
//time_t t,told, tstart;
int ntrig;
int chip, len;
unsigned long *pdata;
 
FILE *fp;
#define RUNREC_ID 1
#define ENDREC_ID 2
#define POSREC_ID 3
#define EVTREC_ID 4
#define THRREC_ID 5
 
typedef struct {
unsigned long id,len;
unsigned long fver,time;
unsigned long nev,nch,ped,xy;
long nx,x0,dx,ny,y0,dy;
} RUNREC;
RUNREC runrec;
typedef struct {
unsigned long id,len;
unsigned long time;
} ENDREC;
ENDREC endrec;
typedef struct {
unsigned long id,len;
unsigned long time;
long ix,x,xset,iy,y,yset;
} POSREC;
POSREC posrec;
 
typedef struct {
unsigned long id,len;
unsigned long nev;
//unsigned short data[NCH];
} EVTREC;
EVTREC evtrec;
typedef struct {
unsigned long id;
unsigned long len;
unsigned long thLow, thUp, thStep;
unsigned long threshold;
} THRREC;
THRREC thrrec;
double thresholdLow;
double thresholdUp;
double thresholdStep;
 
#define BSIZE 20000
int maxn = BSIZE-4; // 2 words x 2 headers
int tdcdata, edge_type, overflow, tdc_num, channel, ev_dat, last_dat, nval_dat;
// end of declarations
// ***
runrec.id = RUNREC_ID;
runrec.len = sizeof(runrec);
//runrec.fver = 0x10000;
runrec.nch = NCH;
GetCtrlVal(p1h, P1_ADCHLSAVE, &runrec.xy);//runrec.xy = 1;
GetCtrlVal(p1h, P1_DEBUG, &print);
endrec.id = ENDREC_ID;
endrec.len = sizeof(endrec);
posrec.id = POSREC_ID;
posrec.len = sizeof(posrec);
evtrec.id = EVTREC_ID;
evtrec.len = sizeof(evtrec);
thrrec.id = THRREC_ID;
thrrec.len = sizeof(thrrec);
cres = 0;
GetCtrlVal (p1h, P1_NEVE, &runrec.nev);
GetCtrlVal (p1h, P1_PEDESTAL, &runrec.ped);
GetCtrlVal (p1h, P1_NX, &runrec.nx);
GetCtrlVal (p1h, P1_XSTEP, &runrec.dx);
GetCtrlVal (p1h, P1_XMIN, &runrec.x0);
GetCtrlVal (p1h, P1_NY, &runrec.ny);
GetCtrlVal (p1h, P1_YSTEP, &runrec.dy);
GetCtrlVal (p1h, P1_YMIN, &runrec.y0);
GetCtrlVal (p1h, P1_NMIN, &step_minutes);
GetSystemTime(&start_hours, &start_minutes, &start_seconds);
//cur_time_s = start_hours*3600 + start_minutes*60 + start_seconds;
time(&cur_time_s);
end_time_s = cur_time_s + step_minutes*60;
printf("START:%2d-%2d-%2d (cur_time = %u s, end_time = %u s)\n", start_hours, start_minutes, start_seconds, cur_time_s, end_time_s);
 
//GetCtrlVal (p1h, P1_DSAVE, &dsave);
//if (dsave) {
GetCtrlVal (p1h, P1_DFILE, dfile0);
fev=0;
fcount=1;
GetCtrlVal (p1h, P1_NEWF, &fmax);
fmax*=1000000;//fmax in Mega Bytes
//}
GetCtrlVal (p1h, P1_SUPR, &supr0);
if (supr0) {
GetCtrlVal (p1h, P1_TDCMIN, &tdcmin);
GetCtrlVal (p1h, P1_FRAC, &frac);
}
GetCtrlVal (p1h, P1_STHRLOW, &thresholdLow);
runrec.thLow = thresholdLow*1000; // mV
GetCtrlVal (p1h, P1_STHRUP, &thresholdUp);
runrec.thUp = thresholdUp*1000; // mV
GetCtrlVal (p1h, P1_STEP, &thresholdStep);
runrec.thStep = thresholdStep*1000; // mV
 
#ifdef USE_DAQ
V673A_map(0,CAEN_V673A,0);
V673A_init(0);
V462_map(0,CAEN_V462,0);
V462_set0(0,1);
#endif
 
//if (dsave) {
sprintf(dfile,"%s_file%02d.dat",dfile0,fcount);
fp = fopen (dfile, "wb");
time (&runrec.time);
status = fwrite (&runrec, 1, runrec.len, fp);
//}
if (supr0) {
fseed = runrec.time & 0x7fffffff;
Uniform (1, fseed, &fracg);
}
thrrec.threshold = (unsigned long) ceil(thresholdLow*1000); // threshold in mV
while (thrrec.threshold <= thresholdUp*1000) {
SetDac(0, thrrec.threshold/1000.0);
SetCtrlVal(p1h, P1_STHRLOW, thrrec.threshold/1000.0);
//wait_loop(100000);
status = fwrite(&thrrec, 1, thrrec.len, fp);
if (print) printf("THRREC status %d len %d threshold %d\n", status, thrrec.len, thrrec.threshold);
for (posrec.ix=0; posrec.ix<runrec.nx; posrec.ix++) {
posrec.xset = runrec.x0 + posrec.ix*runrec.dx;
#ifdef USE_MIKRO
//printf("MIKRO_MoveTo (1, x);%d\n",posrec.x);
do {
MIKRO_MoveTo (MIKRO_X, posrec.xset);
MIKRO_GetPosition(MIKRO_X, &mikroX);
} while (abs(posrec.xset - mikroX) > STEP_TOLERANCE);
//printf("->MIKRO_MoveTo (1, x);%d\n",posrec.xset);
#endif
//SetCtrlVal (p1h, P1_X, posrec.xset);
posrec.xset = mikroX; // set the true value
SetCtrlVal(p1h, P1_X, mikroX);
SetCtrlVal (p1h, P1_IX, posrec.ix);
for (posrec.iy=0; posrec.iy<runrec.ny; posrec.iy++) {
posrec.yset = runrec.y0 + posrec.iy*runrec.dy;
#ifdef USE_MIKRO_Y
//printf("MIKRO_MoveTo (2, y);%d\n",y);
do {
MIKRO_MoveTo (MIKRO_Y, posrec.yset);
MIKRO_GetPosition(MIKRO_Y, &mikroY);
} while (abs(posrec.yset - mikroY) > STEP_TOLERANCE);
//printf("->MIKRO_MoveTo (2, y);%d\n",posrec.yset);
#endif
//SetCtrlVal (p1h, P1_Y, posrec.yset);
posrec.yset = mikroY; // set the true value
SetCtrlVal (p1h, P1_Y, mikroY);
SetCtrlVal (p1h, P1_IY, posrec.iy);
//if (dsave) {
if (fmax && (ftell(fp) > fmax)) {
fcount+=1;
sprintf(dfile,"%s_file%02d.dat",dfile0,fcount);
fclose(fp);
fp = fopen (dfile, "wb");
}
time (&posrec.time);
status = fwrite (&posrec, 1, posrec.len, fp);
if (print) printf("POSREC status %d len %d\n", status, posrec.len);
//}
// clear the plots
for (j=0;j<NCH;j++) {
for (i=0;i<MAXCH;i++){
dtdc[j][0][i]=0;
dtdc[j][1][i]=0;
}
}
 
evtrec.nev=1;
//time(&t);
//tstart=t;
if (print) printf("RUN START.\n");
do {
unsigned long word[BSIZE];
count=0;
ntics=0;
if (print) printf("Event counter: %d\n", evtrec.nev);
//if (t!=told ) printf("%d in %2.2f min daq::event() %s\n",i, (double)(t-tstart)/60., ctime(&t));
#ifdef USE_DAQ
#ifdef CAEN_V673A
 
V462_start0(0);
//tmlnk (tout);
do {
ntrig = V673A_ntrig(0);
} while (ntrig==0 &&(ntics<2));
//tmulk();
//if (timer_out) {
// fprintf(stderr,"-----------------> V673a timeout !!!\n");
// V673A_clallb(0);
// continue;
//}
if (ntics>=2) {
fprintf(stderr,"-----------------> V673a timeout !!!\n");
printf("Timer tics %d\n", ntics);
ntics = 0;
evtrec.nev--;
V673A_clallb(0);
continue;
}
 
if (ntrig>0){
// chip readout
for (chip=0;chip<2;chip++){
if (chip) len = V673A_read1 (0, &word[count+2],maxn-count-2);
else len = V673A_read0 (0, &word[count+2],maxn-count-2);
if (len>16*32*4){
if (print) printf("Length > 2048 per chip!\n");
chip=2;
V673A_clallb(0);
evtrec.nev--;
continue;
}
pdata=&word[count+2];
for(i=0;i< len;i++){
tdcdata = pdata[i] & 0xFFFF ;
edge_type = (pdata[i]>>16)&0x1 ;
overflow = (pdata[i]>>17)&0x1 ;
tdc_num = (pdata[i]>>25)&0x1 ;
channel = ((pdata[i]>>18)&0x1F) |tdc_num<<5 ;
ev_dat = (pdata[i]>>23)&0x1 ;
last_dat = (pdata[i]>>30)&0x1 ;
nval_dat = (pdata[i]>>31)&0x1 ;
if (ev_dat) {
if(print) printf("Event %d\n",tdcdata);
} else {
dtdc[channel][edge_type][tdcdata]++;
if (print) printf
("ch=%d edge=%d ev=%d data=%d last=%d nval=%d\n",channel, edge_type,ev_dat,tdcdata,last_dat,nval_dat);
}
}
if (count+2 < maxn) count+=module_header(0x140+chip,&word[count],len);
}
if (print) printf("Received triggers: %d \n", ntrig);
} else {
if (ntrig==0) {
fprintf(stderr,"-----------------> no trigger");
break;
} else {
if (print) fprintf(stderr,"-----------------> wrong number of triggers in chips V673a !!!\n");
}
V673A_clallb(0);
evtrec.nev--;
continue;
}
V673A_clallb(0);
#endif
#endif
 
GetCtrlVal(p1h, P1_ADCHLSAVE, &runrec.xy);
evtrec.len = count*sizeof(unsigned long) + sizeof(evtrec);
if (print) printf("EVTREC.len = %d bytes.\n", evtrec.len);
status = fwrite (&evtrec, 1, sizeof(evtrec), fp);
status = fwrite (&word, 1, count*sizeof(unsigned long), fp);
if (!(evtrec.nev%1000)) SetCtrlVal (p1h, P1_CEVE, evtrec.nev);
if (!((evtrec.nev+1)%1000)) {
if (fmax && (ftell(fp) > fmax)) {
time (&endrec.time);
status = fwrite (&endrec, 1, endrec.len, fp);
fcount+=1;
sprintf(dfile,"%s_file%02d.dat",dfile0,fcount);
fclose(fp);
fp = fopen (dfile, "wb");
}
}
if(step_minutes > 0) {
GetSystemTime(&start_hours, &start_minutes, &start_seconds);
//cur_time_s = start_hours*3600 + start_minutes*60 + start_seconds;
time(&cur_time_s);
if(cur_time_s >= end_time_s) {
end_time_s = cur_time_s + step_minutes*60;
printf("STEP (nev):%2d-%2d-%2d @ %d\n", start_hours, start_minutes, start_seconds, posrec.xset);
break;
}
}
//told=t;
GetCtrlVal(p1h, P1_DAQ, &daq_on);
} while (evtrec.nev++<runrec.nev && daq_on);
if (!daq_on) break;
} // x loop
if (!daq_on) break;
} // y loop
if (!daq_on) break;
thrrec.threshold += thresholdStep*1000;
} // threhsold loop
//if (dsave) {
time (&endrec.time);
status = fwrite (&endrec, 1, endrec.len, fp);
if (print) printf("ENDREC status %d len %d\n", status, endrec.len);
fclose(fp);
//}
GetSystemTime(&start_hours, &start_minutes, &start_seconds);
printf("STOP:%2d-%2d-%2d (start_time = %u s, end_time = %u s)\n", start_hours, start_minutes, start_seconds, runrec.time, endrec.time);
printf("Elapsed time: %u s.\n", endrec.time-runrec.time);
daq_on=0;
SetCtrlVal (p1h, P1_DAQ, daq_on);
SetCtrlVal (p1h, P1_CEVE, evtrec.nev);
if (print) printf("RUN END. \n\n");
 
return 0;
}
 
 
int SetDac(int ch, double val){
const unsigned int maxdac=0xFFF;
const double RANGE = +9.9976; //V
unsigned int dac;
if ((val > RANGE) || (val < 0)) {
printf("DAC value OUT OF RANGE!!!\n");
return(-1);
}
dac = (val/RANGE)*maxdac;
CSSA_W(NDAC,ch,16,&dac);
printf("DAC ch %d set to %f V dac=%d\n", ch, val, dac);
return 0;
}
 
int __stdcall WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpszCmdLine, int nCmdShow)
{
int i,j,status,refon;
long int xpos, ypos, zpos;
char dfile[300];
FILE *fp;
typedef struct {
unsigned long id,len;
unsigned long fver,time;
unsigned long nev,nch,ped,xy;
long nx,x0,dx,ny,y0,dy;
} RUNREC;
RUNREC runrec;
 
if (InitCVIRTE (hInstance, 0, 0) == 0)
return -1; /* out of memory */
 
SetSleepPolicy(VAL_SLEEP_MORE);
CmtNewThreadPool (MAX_THREADS, &poolHandle);
 
SetStdioWindowOptions (1000, 0, 0);
SetStdioWindowSize (200, 560);
SetStdioWindowPosition (825, 250);
 
#ifdef USE_DAQ
VME_START(NULL);
#endif
 
#ifdef USE_MIKRO
MIKRO_Open (MIKRO_COM);
MIKRO_Init (MIKRO_X,0);
#ifdef USE_MIKRO_Y
MIKRO_Init (MIKRO_Y,0);
#endif
#ifdef USE_MIKRO_Z
MIKRO_Init (MIKRO_Z,0);
#endif
#endif
 
if ((p1h = LoadPanel (0, "include/sipmBias_ui.uir", P1)) < 0) return -1;
DisplayPanel (p1h);
SetCtrlAttribute (p1h, P1_PLCH, ATTR_MAX_VALUE, NCH-1);
GetCtrlVal(p1h, P1_ADCHLSAVE, &runrec.xy);
SetCtrlVal(p1h, P1_ADCHL, runrec.xy-2);
 
QueueUserEvent (1000, p1h, P1_RESET);
//QueueUserEvent (1000, p1h, P1_ASD8THR);
QueueUserEvent (1000, p1h, P1_AMPDISSHTHR);
do {
GetUserEvent (1, &pID, &rID);
switch (rID) {
case P1_TIMER:
ntics+=1;
GetCtrlVal (p1h, P1_REFON, &refon);
if (refon) update_plots();
break;
case P1_REFRESH:
update_plots();
break;
case P1_DAQ:
GetCtrlVal (p1h, P1_DAQ, &daq_on);
if (daq_on) {
CmtScheduleThreadPoolFunction (poolHandle, daq_run, (void *)&dummy, &tfID);
} else {
CmtWaitForThreadPoolFunctionCompletion (poolHandle, tfID,
OPT_TP_PROCESS_EVENTS_WHILE_WAITING);
CmtReleaseThreadPoolFunctionID (poolHandle, tfID);
}
break;
case P1_ZSET:
if (!daq_on) {
GetCtrlVal (p1h, P1_ZSET, &zpos);
#ifdef USE_MIKRO_Z
MIKRO_MoveTo (MIKRO_Z, zpos);
#endif
}
break;
case P1_REREAD:
if (!daq_on) {
status = FileSelectPopup ("", "*.dat", ".dat",
"Choose the previous file",
VAL_LOAD_BUTTON, 0, 0, 1, 0, dfile);
if (status==1) {
fp = fopen (dfile, "rb");
status = fread (&runrec, 1, sizeof(runrec), fp);
fclose(fp);
if (runrec.id==1) {
SetCtrlVal (p1h, P1_NX, runrec.nx);
SetCtrlVal (p1h, P1_XSTEP, runrec.dx);
SetCtrlVal (p1h, P1_XMIN, runrec.x0);
SetCtrlVal (p1h, P1_NY, runrec.ny);
SetCtrlVal (p1h, P1_YSTEP, runrec.dy);
SetCtrlVal (p1h, P1_YMIN, runrec.y0);
SetCtrlVal (p1h, P1_NEVE, runrec.nev);
}
}
}
break;
case P1_MGET:
#ifdef USE_MIKRO
MIKRO_GetPosition(MIKRO_X,&xpos);
Delay(0.01);
SetCtrlVal (p1h, P1_X, xpos);
#ifdef USE_MIKRO_Y
MIKRO_GetPosition(MIKRO_Y,&ypos);
Delay(0.01);
SetCtrlVal (p1h, P1_Y, ypos);
#endif
#ifdef USE_MIKRO_Z
MIKRO_GetPosition(MIKRO_Z,&zpos);
Delay(0.01);
SetCtrlVal (p1h, P1_Z, zpos);
#endif
#endif
break;
case P1_HO:
if (!daq_on) {
SetWaitCursor (1);
#ifdef USE_MIKRO
MIKRO_ReferenceMove (MIKRO_X);
#ifdef USE_MIKRO_Y
MIKRO_ReferenceMove (MIKRO_Y);
#endif
#ifdef USE_MIKRO_Z
MIKRO_ReferenceMove (MIKRO_Z);
#endif
#endif
SetWaitCursor (0);
}
break;
case P1_RESET:
for (j=0;j<NCH;j++) {
for (i=0;i<MAXCH;i++){
dtdc[j][0][i]=0;
dtdc[j][1][i]=0;
}
}
update_plots();
break;
case P1_TDCLOG:
GetCtrlVal (p1h, P1_TDCLOG, &status);
SetCtrlAttribute (p1h, P1_TDC, ATTR_YMAP_MODE, status);
update_plots();
break;
case P1_ADCLOG:
GetCtrlVal (p1h, P1_ADCLOG, &status);
SetCtrlAttribute (p1h, P1_ADC, ATTR_YMAP_MODE, status);
update_plots();
break;
/*case P1_ASD8THR:
{
double value;
GetCtrlVal (p1h, P1_SASD8THR, &value);
SetDac(0, value);
}
break;*/
case P1_AMPDISSHTHR:
{
double value;
GetCtrlVal (p1h, P1_SAMPDISSHTHR, &value);
SetDac(1, value);
 
}
break;
}
} while ((rID != P1_EXIT)||daq_on);
CmtDiscardThreadPool (poolHandle);
DiscardPanel (p1h);
 
#ifdef USE_MIKRO
MIKRO_Close ();
#endif
 
#ifdef USE_DAQ
VME_STOP();
#endif
return 0;
}