Subversion Repositories f9daq

Compare Revisions

Ignore whitespace Rev 32 → Rev 33

/sipmScan/sipmBias.prj
0,0 → 1,261
[Project Header]
Version = 551
Platform Code = 4
Pathname = "/c/HOME/dino/sipmScan/sipmBias.prj"
CVI Dir = "/c/measurementstudio/cvi"
VXIplug&play Framework Dir = "/C/VXIpnp/winnt"
Number of Files = 8
Sort Type = "No Sort"
Target Type = "Executable"
Build Configuration = "Debug"
Warn User If Debugging Release = 1
Flags = 17
Drag Bar Left = 354
Window Top = 478
Window Left = 778
Window Bottom = 982
Window Right = 1522
 
[File 0001]
File Type = "User Interface Resource"
Path = "/c/HOME/dino/sipmScan/include/sipmBias_ui.uir"
Res Id = 1
Exclude = False
Disk Date = 3479111557
Project Flags = 0
Window Top = 0
Window Left = 0
Window Height = 0
Window Width = 0
 
[File 0002]
File Type = "CSource"
Path = "/c/HOME/dino/sipmScan/src/sipmBias.c"
Res Id = 2
Exclude = False
Disk Date = 3479111999
Project Flags = 0
Compile Into Object File = False
Object Format = "Unknown"
ForceCompile_Debug = True
ForceCompile_Release = True
Window Top = 122
Window Left = 54
Window Height = 0
Window Width = 0
Source Window State = "1,150,150,150,4,37,38,0,0,146,0,0,0,0,0,53,117,0,149,33,"
 
[File 0003]
File Type = "Include"
Path = "/c/HOME/dino/sipmScan/include/camac.h"
Res Id = 3
Exclude = False
Disk Date = 3261994151
Project Flags = 0
Window Top = 0
Window Left = 0
Window Height = 0
Window Width = 0
Source Window State = "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,"
 
[File 0004]
File Type = "Include"
Path = "/c/MeasurementStudio/cvi/instr/WIENVME_DLL/wienvme_dll.h"
Res Id = 4
Exclude = False
Disk Date = 3263052572
Project Flags = 0
Window Top = 152
Window Left = 70
Window Height = 0
Window Width = 0
Source Window State = "0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,"
 
[File 0005]
File Type = "Function Panel"
Path = "/c/MeasurementStudio/cvi/instr/MIKRO/MIKRO.fp"
Res Id = 5
Exclude = False
Disk Date = 3228639698
Project Flags = 0
Window Top = 0
Window Left = 0
Window Height = 0
Window Width = 0
 
[File 0006]
File Type = "Function Panel"
Path = "/c/MeasurementStudio/cvi/instr/CAENV673A/CAENV673A.fp"
Res Id = 6
Exclude = False
Disk Date = 3244094708
Project Flags = 0
Window Top = 0
Window Left = 0
Window Height = 0
Window Width = 0
 
[File 0007]
File Type = "Function Panel"
Path = "/c/MeasurementStudio/cvi/instr/WIENVME_DLL/wienvme_dll.fp"
Res Id = 7
Exclude = False
Disk Date = 3258264254
Project Flags = 0
Window Top = 0
Window Left = 0
Window Height = 0
Window Width = 0
 
[File 0008]
File Type = "Function Panel"
Path = "/c/MeasurementStudio/cvi/instr/CAENV462/CAENV462.fp"
Res Id = 8
Exclude = False
Disk Date = 3243842134
Project Flags = 0
Window Top = 0
Window Left = 0
Window Height = 0
Window Width = 0
 
[SCC Options]
Use global settings = True
SCC Provider = ""
SCC Project = ""
Local Path = ""
Auxiliary Path = ""
Perform Same Action For .h File As For .uir File = "Ask"
Comment = ""
Username = ""
Use Default Comment = False
Use Default Username = False
Do Not Include PRJ File in Actions = True
Suppress CVI Error Messages = False
 
[Compiler Options]
Default Calling Convention = "cdecl"
Max Number Of Errors = 10
Require Prototypes = True
Require Return Values = True
Enable Pointer Mismatch Warning = False
Enable Unreachable Code Warning = False
Track Include File Dependencies = True
Prompt For Missing Includes = True
Stop On First Error File = False
Bring Up Err Win For Warnings = True
Show Build Dialog = False
O Option Compatible With 5.0 = False
 
[Run Options]
Stack Size = 500000
Debugging Level = "Standard"
Save Changes Before Running = "Ask"
Break On Library Errors = True
Break On First Chance Exceptions = False
Hide Windows = False
Break At First Statement = False
 
[Compiler Defines]
Compiler Defines = "/DWIN32_LEAN_AND_MEAN /DWIENVME"
 
[Command Line Args]
Command Line Args = ""
 
[Include Paths]
Include Path 1 = "/c/MeasurementStudio/cvi/instr/PCIVME_DLL"
Include Path 2 = "/c/MeasurementStudio/cvi/instr/WIENVME_DLL"
Include Path 3 = "/c/HOME/dino/sipmScan/include"
 
[Included Headers]
Header 0057 = "/c/HOME/dino/sipmScan/include/camac.h"
Header 0029 = "/c/MeasurementStudio/cvi/instr/WIENVME_DLL/wienvme_dll.h"
Header 0001 = "/c/MeasurementStudio/cvi/include/utility.h"
Header 0002 = "/c/MeasurementStudio/cvi/include/cvidef.h"
Header 0003 = "/c/MeasurementStudio/cvi/include/cvirte.h"
Header 0004 = "/c/MeasurementStudio/cvi/include/formatio.h"
Header 0005 = "/c/MeasurementStudio/cvi/include/ansi_c.h"
Header 0006 = "/c/MeasurementStudio/cvi/include/ansi/assert.h"
Header 0007 = "/c/MeasurementStudio/cvi/include/ansi/ctype.h"
Header 0008 = "/c/MeasurementStudio/cvi/include/ansi/errno.h"
Header 0009 = "/c/MeasurementStudio/cvi/include/ansi/float.h"
Header 0010 = "/c/MeasurementStudio/cvi/include/ansi/limits.h"
Header 0011 = "/c/MeasurementStudio/cvi/include/ansi/locale.h"
Header 0012 = "/c/MeasurementStudio/cvi/include/ansi/math.h"
Header 0013 = "/c/MeasurementStudio/cvi/include/ansi/setjmp.h"
Header 0014 = "/c/MeasurementStudio/cvi/include/ansi/signal.h"
Header 0015 = "/c/MeasurementStudio/cvi/include/ansi/stdarg.h"
Header 0016 = "/c/MeasurementStudio/cvi/include/ansi/stddef.h"
Header 0017 = "/c/MeasurementStudio/cvi/include/ansi/stdio.h"
Header 0018 = "/c/MeasurementStudio/cvi/include/ansi/stdlib.h"
Header 0019 = "/c/MeasurementStudio/cvi/include/ansi/string.h"
Header 0020 = "/c/MeasurementStudio/cvi/include/ansi/time.h"
Header 0021 = "/c/MeasurementStudio/cvi/instr/MIKRO/MIKRO.h"
Header 0022 = "/c/MeasurementStudio/cvi/include/ivi.h"
Header 0023 = "/c/MeasurementStudio/cvi/include/visa.h"
Header 0024 = "/c/MeasurementStudio/cvi/include/visatype.h"
Header 0025 = "/c/MeasurementStudio/cvi/include/vpptype.h"
Header 0026 = "/c/MeasurementStudio/cvi/include/rs232.h"
Header 0027 = "/c/MeasurementStudio/cvi/instr/CAENV673A/CAENV673A.h"
Header 0028 = "/c/MeasurementStudio/cvi/instr/CAENV673A/CAENV673A_DEF.h"
Header 0030 = "/c/MeasurementStudio/cvi/sdk/include/windows.h"
Header 0031 = "/c/MeasurementStudio/cvi/sdk/include/windef.h"
Header 0032 = "/c/MeasurementStudio/cvi/sdk/include/winnt.h"
Header 0033 = "/c/MeasurementStudio/cvi/sdk/include/basetsd.h"
Header 0034 = "/c/MeasurementStudio/cvi/sdk/include/Guiddef.h"
Header 0035 = "/c/MeasurementStudio/cvi/sdk/include/pshpack4.h"
Header 0036 = "/c/MeasurementStudio/cvi/sdk/include/poppack.h"
Header 0037 = "/c/MeasurementStudio/cvi/sdk/include/pshpack2.h"
Header 0038 = "/c/MeasurementStudio/cvi/sdk/include/pshpack8.h"
Header 0039 = "/c/MeasurementStudio/cvi/sdk/include/winbase.h"
Header 0040 = "/c/MeasurementStudio/cvi/sdk/include/winerror.h"
Header 0041 = "/c/MeasurementStudio/cvi/sdk/include/wingdi.h"
Header 0042 = "/c/MeasurementStudio/cvi/sdk/include/pshpack1.h"
Header 0043 = "/c/MeasurementStudio/cvi/sdk/include/winuser.h"
Header 0044 = "/c/MeasurementStudio/cvi/sdk/include/tvout.h"
Header 0045 = "/c/MeasurementStudio/cvi/sdk/include/winnls.h"
Header 0046 = "/c/MeasurementStudio/cvi/sdk/include/wincon.h"
Header 0047 = "/c/MeasurementStudio/cvi/sdk/include/winver.h"
Header 0048 = "/c/MeasurementStudio/cvi/sdk/include/winreg.h"
Header 0049 = "/c/MeasurementStudio/cvi/sdk/include/winnetwk.h"
Header 0050 = "/c/MeasurementStudio/cvi/sdk/include/winsvc.h"
Header 0051 = "/c/MeasurementStudio/cvi/sdk/include/mcx.h"
Header 0052 = "/c/MeasurementStudio/cvi/sdk/include/imm.h"
Header 0053 = "/c/MeasurementStudio/cvi/instr/CAENV462/CAENV462.h"
Header 0054 = "/c/MeasurementStudio/cvi/instr/CAENV462/CAENV462_DEF.h"
Max Header Number = 57
 
[Create Executable]
Executable File_Debug = "/c/HOME/dino/sipmScan/sipmThr_dbg.exe"
Target Creation Date_Debug = 3479104583
Force Creation of Target_Debug = True
Executable File_Release = "/c/HOME/dino/sipmScan/sipmThr.exe"
Target Creation Date_Release = 0
Force Creation of Target_Release = True
Icon File = ""
Application Title = ""
DLL Exports = "Include File Symbols"
DLL Import Library Choice = "Gen Lib For Current Mode"
Use VXIPNP Subdirectories for Import Libraries = False
Use Dflt Import Lib Base Name = True
Where to Copy DLL = "Do not copy"
Add Type Lib To DLL = False
Include Type Lib Help Links = False
Type Lib FP File = ""
Type Lib Guid = ""
Uses DataSocket = 0
Uses NIReports = 0
Uses DCom95 = 0
Instrument Driver Support Only = False
 
[External Compiler Support]
UIR Callbacks File Option = 0
Using LoadExternalModule = False
Create Project Symbols File = True
UIR Callbacks Obj File = ""
Project Symbols H File = ""
Project Symbols Obj File = ""
 
[DLL Debugging Support]
External Process Path = ""
 
/sipmScan/include/sipmBias_ui.uir
Cannot display: file marked as a binary type.
svn:mime-type = application/octet-stream
Property changes:
Added: svn:mime-type
## -0,0 +1 ##
+application/octet-stream
\ No newline at end of property
Index: include/sipmScan_ui.uir
===================================================================
Cannot display: file marked as a binary type.
svn:mime-type = application/octet-stream
Index: sipmScan.prj
===================================================================
--- sipmScan.prj (revision 32)
+++ sipmScan.prj (revision 33)
@@ -71,7 +71,7 @@
Window Width = 0
Source Window State = "1,251,251,251,8,60,61,0,0,125,0,0,0,0,0,52,234,0,260,62,"
Header Dependencies Line0001 = "1,2,3,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,27,29,30,31,32,3"
-Header Dependencies Line0002 = "3,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,56,57,58,59,"
+Header Dependencies Line0002 = "3,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,55,56,57,58,"
[File 0005]
File Type = "User Interface Resource"
@@ -78,12 +78,12 @@
Path = "/c/HOME/dino/sipmScan/include/sipmScan_ui.uir"
Res Id = 5
Exclude = False
-Disk Date = 3478368979
+Disk Date = 3479104847
Project Flags = 0
-Window Top = 152
-Window Left = 70
-Window Height = 823
-Window Width = 1248
+Window Top = 31
+Window Left = 6
+Window Height = 844
+Window Width = 1241
[File 0006]
File Type = "Include"
@@ -184,8 +184,8 @@
[Included Headers]
Header 0029 = "/c/MeasurementStudio/cvi/instr/WIENVME_DLL/wienvme_dll.h"
-Header 0059 = "/c/HOME/dino/sipmScan/include/sipmScan_ui.h"
-Header 0058 = "/c/HOME/dino/sipmScan/include/camac.h"
+Header 0058 = "/c/HOME/dino/sipmScan/include/sipmScan_ui.h"
+Header 0057 = "/c/HOME/dino/sipmScan/include/camac.h"
Header 0001 = "/c/MeasurementStudio/cvi/include/utility.h"
Header 0002 = "/c/MeasurementStudio/cvi/include/cvidef.h"
Header 0003 = "/c/MeasurementStudio/cvi/include/cvirte.h"
@@ -235,17 +235,17 @@
Header 0051 = "/c/MeasurementStudio/cvi/sdk/include/mcx.h"
Header 0052 = "/c/MeasurementStudio/cvi/sdk/include/imm.h"
Header 0053 = "/c/MeasurementStudio/cvi/instr/CAENV462/CAENV462.h"
-Header 0056 = "/c/MeasurementStudio/cvi/include/userint.h"
-Header 0057 = "/c/MeasurementStudio/cvi/include/analysis.h"
+Header 0055 = "/c/MeasurementStudio/cvi/include/userint.h"
+Header 0056 = "/c/MeasurementStudio/cvi/include/analysis.h"
Header 0004 = "/c/MeasurementStudio/cvi/include/formatio.h"
Header 0026 = "/c/MeasurementStudio/cvi/include/rs232.h"
Header 0028 = "/c/MeasurementStudio/cvi/instr/CAENV673A/CAENV673A_DEF.h"
Header 0054 = "/c/MeasurementStudio/cvi/instr/CAENV462/CAENV462_DEF.h"
-Max Header Number = 59
+Max Header Number = 58
[Create Executable]
Executable File_Debug = "/c/HOME/dino/sipmScan/sipmScan_dbg.exe"
-Target Creation Date_Debug = 3479103989
+Target Creation Date_Debug = 3479104853
Force Creation of Target_Debug = False
Executable File_Release = "/c/HOME/dino/sipmScan/sipmScan.exe"
Target Creation Date_Release = 0
/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;
}