Subversion Repositories f9daq

Compare Revisions

Ignore whitespace Rev 263 → Rev 264

/cvi/apps/petdemo/PETProjDataMgr.c
0,0 → 1,645
//
// ../bin/FBP2D FBP2D.par
//
// ********************************************************************
// * License and Disclaimer *
// * *
// * The GAMOS software is copyright of the Copyright Holders of *
// * the GAMOS Collaboration. It is provided under the terms and *
// * conditions of the GAMOS Software License, included in the file *
// * LICENSE and available at http://fismed.ciemat.es/GAMOS/license .*
// * These include a list of copyright holders. *
// * *
// * Neither the authors of this software system, nor their employing *
// * institutes,nor the agencies providing financial support for this *
// * work make any representation or warranty, express or implied, *
// * regarding this software system or assume any liability for its *
// * use. Please see the license in the file LICENSE and URL above *
// * for the full disclaimer and the limitation of liability. *
// * *
// * This code implementation is the result of the scientific and *
// * technical work of the GAMOS collaboration. *
// * By using, copying, modifying or distributing the software (or *
// * any work based on the software) you agree to acknowledge its *
// * use in resulting scientific publications, and indicate your *
// * acceptance of all terms of the GAMOS Software license. *
// ********************************************************************
//
#include "PETProjDataMgr.h"
 
 
static struct PETProjDataMgr mgr;
 
void SetNPlanes(int n) {
mgr.m_NOfPlanes=n;
};
void SetNBin(int n) {
mgr.m_NOfBins=n;
};
void SetNAng(int n) {
mgr.m_NOfAngles=n;
};
void SetDebug(int n) {
mgr.m_Debug=n;
};
void SetNumberOfChannels(int n) {
mgr.m_nch=n;
};
 
void SetRingDiameter( double x) {
mgr.m_RingDiameter = x;
};
void SetAxialDistance( double x) {
mgr.m_AxialDistance = x;
};
 
//----------------------------------------------------------------------
 
//----------------------------------------------------------------------
struct PETProjDataMgr *GetInstance() {
 
 
return &mgr;
 
}
 
//-----------------------------------------------------------------------
int PETProjDataMgrInit() {
 
/*
// ARGUMENTS:
" cout << " -------------------------- \n"
" Arguments convention: \n"
" -a Axial FOV (mm), <theDist_axial=100.0> \n"
" -d Diameter Transaxial FOV (mm), <m_RingDiameter=300.0> \n"
" -i Type of the input file (by default: 0 = Arce_binary), <typeINfile=0> \n"
" \n"
" -m Maximum ring difference (by default: -1 = m_NOfPlanes), <m_MaxRingDifferenceiff> \n"
" -n Name of output file, <m_Filename> \n"
" -p Axial number of planes, <m_NOfPlanes> \n"
" -r Number of bins, \"distancias\", <m_NOfBins> \n"
// -s Span TO DO:span !!!!!
" -t Number of angular views, \"direcciones\", <m_NOfAngles> \n"
" -v Verbosity (by default: 0=silent, 3=debug), <verbos> \n"
" -x Maximum number of coincidences to be stored (by default: -1 = no limit), <Max_Coinci> \n"
" -o Output type (by default: 0 = mcc Interfile, 1 = STIR Interfile), <OutType> \n"
 
" \n"
" PET Reconstruction. CIEMAT 2009-11 \n"
" mario.canadas@ciemat.es \n"
" -------------------------- \n";
*/
 
mgr.m_AxialDistance = (9-1)*2.25; // Axial pixel dimension*NOfPlanes
mgr.m_RingDiameter = 100.0; // notranji premer peta
mgr.m_NOfPlanes = 9; // stevilo ravnin
mgr.m_NOfBins = 128; // stevilo binov v razdalji
mgr.m_nch = 128; // stevilo padov okoli in okoli
mgr.m_NOfAngles = fabs(mgr.m_nch)/2; // stevilo kotov = stevilo padov okoli in okoli /2
 
mgr.m_MaxRingDifference = -1;
//mgr.m_MaxRingDifference = 3; // najvecja razdalja med padi
// toDo: theSpan = int(GmParameterMgr::GetInstance()->GetNumericValue("PET:ProjData:Span",1));
 
 
mgr.m_OutFormat = 1; // 1.. projections 0 .. image
 
mgr.m_Debug=1;
 
if (mgr.m_MaxRingDifference==-1) mgr.m_MaxRingDifference=mgr.m_NOfPlanes-1;
 
 
mgr.m_TotalAxialPlanes=mgr.m_NOfPlanes*mgr.m_NOfPlanes;
if (mgr.m_OutFormat==1) mgr.m_TotalAxialPlanes= (2*mgr.m_NOfPlanes-1 - mgr.m_MaxRingDifference)*mgr.m_MaxRingDifference + mgr.m_NOfPlanes; // total number of Axial planes (segments*planes) in STIR format
 
/*--- Initialize sino3D ---*/
mgr.m_projections = (SINO_TYPE ** *) malloc(mgr.m_NOfBins*sizeof(SINO_TYPE **));
for(int i=0; i<mgr.m_NOfBins; i++) {
mgr.m_projections[i] = (SINO_TYPE **) malloc(mgr.m_NOfAngles*sizeof(SINO_TYPE *));
for(int j=0; j<mgr.m_NOfAngles; j++) {
mgr.m_projections[i][j] = (SINO_TYPE *) malloc(mgr.m_TotalAxialPlanes *sizeof(SINO_TYPE)); /// ! If mgr.m_OutFormat==1 (STIR output):Matrix size depends on the MAX_Ring_Difference
for(int k=0; k<mgr.m_TotalAxialPlanes; k++) {
mgr.m_projections[i][j][k]=0;
}
}
}
 
mgr.m_TotalProjectionCoincidences=0;
mgr.m_TotalCoincidences=0;
//OutputType = "pet";
return 0;
}
 
//-----------------------------------------------------------------------
void SetProjection( int axialplane, int id) {
for(int i=0; i<mgr.m_NOfBins; i++) {
for(int j=0; j<mgr.m_NOfAngles; j++) {
mgr.m_projections[i][j][axialplane]=H2D_GetBinContent(id,i+1,j+1);
}
}
 
}
//-----------------------------------------------------------------------
 
//-----------------------------------------------------------
// from Gate
//------------------------------------------------------------
double ComputeSinogramS(double X1, double Y1, double X2, double Y2) {
double s;
 
double denom = (Y1-Y2) * (Y1-Y2) + (X2-X1) * (X2-X1);
 
if (denom!=0.) {
denom = sqrt(denom);
s = ( X1 * (Y2-Y1) + Y1 * (X1-X2) ) / denom;
} else {
s = 0.;
}
 
double theta;
if ((X1-X2)!=0.) {
theta=atan((X1-X2) /(Y1-Y2));
} else {
theta=3.1416/2.;
}
if ((theta > 0.) && ((X1-X2) > 0.)) s = -s;
if ((theta < 0.) && ((X1-X2) < 0.)) s = -s;
if ( theta < 0.) {
theta = theta+3.1416;
s = -s;
}
return s;
}
 
 
void AddEvent( const HVector3 pos1 , const HVector3 pos2) {
int z1_i, z2_i;
//for discretization on the crystal: int x1_i, x2_i, y1_i, y2_i;
 
double z1_abs=pos1.x[2]+mgr.m_AxialDistance/2;
double z2_abs=pos2.x[2]+mgr.m_AxialDistance/2;
double a, b, phi, dis;
int phi_i, dis_i;
int ring_diff;
 
double _PI=2*asin(1);
 
mgr.m_TotalCoincidences++;
 
z1_i=(int)(mgr.m_NOfPlanes* z1_abs/mgr.m_AxialDistance); //round --> mgr.m_NOfPlanes+1 ...
z2_i=(int)(mgr.m_NOfPlanes* z2_abs/mgr.m_AxialDistance);
 
// control; if z_i out of range: return
 
if ( (pos1.x[0]==pos2.x[0]) && (pos1.x[1]==pos2.x[1]) ) {
#ifndef GAMOS_NO_VERBOSE
if( mgr.m_Debug ) {
printf( "AddEvent:WARNING! Event_1 == Event_2 ; x= %f y= %f z= %f\n", pos2.x[0], pos2.x[1], pos2.x[2] );
}
#endif
return;
}
 
if ( (z1_i<0) || (z2_i<0) || (z1_i>= mgr.m_NOfPlanes) || (z2_i>= mgr.m_NOfPlanes) ) {
#ifndef GAMOS_NO_VERBOSE
if( mgr.m_Debug ) {
printf("PETProjDataMgr::AddEvent:WARNING! Event out of bounds (Axial):");
printf( "x1= %f y1= %f z1= %f ", pos1.x[0], pos1.x[1], pos1.x[2] );
printf( "x2= %f y2= %f z2= %f \n", pos2.x[0], pos2.x[1], pos2.x[2] );
}
#endif
return;
}
 
ring_diff = (int)fabs(z1_i-z2_i);
 
// max ring difference; control:
if (ring_diff > mgr.m_MaxRingDifference) {
#ifndef GAMOS_NO_VERBOSE
if( mgr.m_Debug ) {
printf("PETProjDataMgr::AddEvent:WARNING! Event out of bounds (Max. Ring Diff.): %f>%f",ring_diff , mgr.m_MaxRingDifference );
printf( "x1= %f y1= %f z1= %f ", pos1.x[0], pos1.x[1], pos1.x[2] );
printf( "x2= %f y2= %f z2= %f \n", pos2.x[0], pos2.x[1], pos2.x[2] );
}
#endif
return;
}
 
a=(double)(pos2.x[1]- pos1.x[1]);
b=(double)(pos2.x[0]- pos1.x[0]);
 
if (a==0.0) {
phi=_PI*0.5;
} else {
phi=atan(b/a);
}
 
if (phi<0) phi = phi +_PI;
 
dis=pos1.x[0]*cos(phi) - pos1.x[1]*sin(phi);
//dis=ComputeSinogramS(pos1.x(), pos1.y(), pos2.x(), pos2.x());
// control; transaxial FOV
if ( fabs(dis) > mgr.m_RingDiameter*0.5 ) {
#ifndef GAMOS_NO_VERBOSE
if( mgr.m_Debug ) {
printf("PETProjDataMgr::AddEvent:WARNING! Event out of bounds (Transaxial):" );
printf( "x1= %f y1= %f z1= %f ", pos1.x[0], pos1.x[1], pos1.x[2] );
printf( "x2= %f y2= %f z2= %f \n", pos2.x[0], pos2.x[1], pos2.x[2] );
}
#endif
return;
}
 
dis = dis + mgr.m_RingDiameter*0.5;
 
// discret values:
phi_i=RoundRealToNearestInteger( (double)(mgr.m_NOfAngles-1)*phi/_PI );
dis_i=RoundRealToNearestInteger( (double)(mgr.m_NOfBins-1)*dis/(double)mgr.m_RingDiameter );
 
if ((phi_i>=mgr.m_NOfAngles) || (dis_i>=mgr.m_NOfBins)) return; // only possible "=" because 'round' check it..
 
// OLD: (SRRB included) sino3D[dis_i][phi_i][ (z1_i+z2_i)+ring_diff*(mgr.m_NOfPlanes-1) ]++;
 
int Zpos;
 
if (mgr.m_OutFormat==0) {
Zpos = (z1_i*mgr.m_NOfPlanes + z2_i);
} else {
 
if (z1_i>=z2_i) { // SIN Max Ring_Diff: Zpos= ( ((mgr.m_NOfPlanes-ring_diff)*(mgr.m_NOfPlanes-1-ring_diff))/2 + z2_i );
 
Zpos= ( ((2*mgr.m_NOfPlanes-1 - mgr.m_MaxRingDifference - ring_diff)*(mgr.m_MaxRingDifference - ring_diff))/2 + z2_i);
 
} else {
Zpos= ( (mgr.m_TotalAxialPlanes) - ((2*mgr.m_NOfPlanes-1 - mgr.m_MaxRingDifference - ring_diff +1)*(mgr.m_MaxRingDifference - ring_diff +1))/2 + z1_i );
 
}
}
 
mgr.m_projections[dis_i][phi_i][ Zpos ]++;
mgr.m_TotalProjectionCoincidences++;
 
#ifndef GAMOS_NO_VERBOSE
if( mgr.m_Debug >1) {
printf("PETProjDataMgr::AddEvent:");
printf( "x1= %f y1= %f z1= %f ", pos1.x[0], pos1.x[1], pos1.x[2] );
printf( "x2= %f y2= %f z2= %f \n", pos2.x[0], pos2.x[1], pos2.x[2] );
printf("PETProjDataMgr::AddEvent: Sinogram pos.: distance(s)= %f angular view(phi)= %f Zpos= %f Segment (Ring diff.) =%f\n" , dis_i , phi_i, Zpos , ring_diff );
}
#endif
 
 
}
 
//-----------------------------------------------------------------------
void PETProjDataMgrFree() {
int i,j;
if (mgr.m_projections){
for(i=0; i<mgr.m_NOfBins; i++) {
for(j=0; j<mgr.m_NOfAngles; j++) {
if (mgr.m_projections[i][j]) free(mgr.m_projections[i][j]);
mgr.m_projections[i][j]= NULL;
}
if (mgr.m_projections[i]) free(mgr.m_projections[i]);
mgr.m_projections[i]= NULL;
 
}
free(mgr.m_projections);
mgr.m_projections= NULL;
}
 
}
 
 
//-----------------------------------------------------------------------
void WriteInterfile(const char * Filename ) {
 
char name_hv[512];
char name_v[512];
FILE *fp;
if (mgr.m_OutFormat==0) {
 
strcpy(name_hv, Filename);
strcpy(name_v, Filename);
strcat(name_hv, ".hv");
strcat(name_v, ".v");
fp = fopen(name_hv, "w");
 
fprintf (fp, "!INTERFILE := \n");
fprintf (fp, "name of data file := %s\n", name_v);
fprintf (fp, "!GENERAL DATA := \n");
fprintf (fp, "!GENERAL IMAGE DATA :=\n");
fprintf (fp, "!type of data := tomographic\n");
fprintf (fp, "!version of keys := 3.3\n");
fprintf (fp, "!data offset in bytes := 0\n");
fprintf (fp, "imagedata byte order := littleendian\n");
fprintf (fp, "!PET STUDY (General) :=\n");
fprintf (fp, "!PET data type := 3D-Sinogram\n");
fprintf (fp, "process status := Reconstructed\n");
fprintf (fp, "!number format := unsigned short\n");
fprintf (fp, "!number of bytes per pixel := 2\n");
fprintf (fp, "number of dimensions := 3\n");
fprintf (fp, "matrix axis label [1] := x\n");
fprintf (fp, "!matrix size [1] := %i\n",mgr.m_NOfBins);
fprintf (fp, "scaling factor (mm/pixel) [1] := %f\n",(float)(mgr.m_RingDiameter/(mgr.m_NOfBins-1.0)));
 
fprintf (fp, "matrix axis label [2] := y\n");
fprintf (fp, "!matrix size [2] := %i\n",mgr.m_NOfAngles);
 
fprintf (fp, "scaling factor (degree/pixel) [2] := %f\n",(float)(360./mgr.m_NOfAngles));
 
fprintf (fp, "matrix axis label [3] := z\n");
fprintf (fp, "!matrix size [3] := %i\n",mgr.m_NOfPlanes*mgr.m_NOfPlanes);
fprintf (fp, "scaling factor (mm/pixel) [3] := %f\n",(float)(mgr.m_AxialDistance/(mgr.m_NOfPlanes-1.0)));
 
fprintf (fp, "number of slices := %i\n",mgr.m_NOfPlanes*mgr.m_NOfPlanes);
fprintf (fp, "number of time frames := 1\n");
fprintf (fp, "image scaling factor[1] := 1\n");
fprintf (fp, "data offset in bytes[1] := 0\n");
fprintf (fp, "quantification units := 1\n");
fprintf (fp, "!END OF INTERFILE := \n");
 
fclose(fp);
//(size_t)(mgr.m_NOfBins*mgr.m_NOfAngles*mgr.m_NOfPlanes*mgr.m_NOfPlanes);
 
} else {
 
strcpy(name_hv, Filename);
strcpy(name_v, Filename);
 
strcat(name_hv, ".hs"); // STIR extension: .hs .s
strcat(name_v, ".s");
fp =fopen(name_hv, "w");
 
fprintf (fp, "!INTERFILE := \n");
fprintf (fp, "name of data file := %s\n",name_v);
fprintf (fp, "!GENERAL DATA := \n");
fprintf (fp, "!GENERAL IMAGE DATA :=\n");
fprintf (fp, "!type of data := PET\n");
// fprintf (fp, "!version of keys := 3.3\n"); STIR format is not 3.3 (almost but not completely), ERROR in STIR if it is not removed
// fprintf (fp, "!data offset in bytes := 0\n");
fprintf (fp, "imagedata byte order := littleendian\n");
fprintf (fp, "!PET STUDY (General) :=\n");
fprintf (fp, "!PET data type := Emission\n");
fprintf (fp, "applied corrections := {arc correction}\n"); // {none}\n");
// fprintf (fp, "process status := Reconstructed\n");
fprintf (fp, "!number format := unsigned integer\n");
fprintf (fp, "!number of bytes per pixel := 2\n");
 
fprintf (fp, "number of dimensions := 4\n");
fprintf (fp, "matrix axis label [4] := segment\n");
fprintf (fp, "!matrix size [4] := %i\n",mgr.m_MaxRingDifference*2 + 1);
// fprintf (fp, "scaling factor (mm/pixel) [1] := %f\n",(float)(d_FOV/(mgr.m_NOfBins-1)));
fprintf (fp, "matrix axis label [3] := axial coordinate\n");
fprintf (fp, "!matrix size [3] := { ");
if (mgr.m_MaxRingDifference==0) {
fprintf (fp, "%i}\n", mgr.m_NOfPlanes);
} else {
int m;
for(m=mgr.m_NOfPlanes-mgr.m_MaxRingDifference; m<=mgr.m_NOfPlanes; m++) fprintf (fp, "%i,", m);
for(m=mgr.m_NOfPlanes-1; m>mgr.m_NOfPlanes-mgr.m_MaxRingDifference; m--) fprintf (fp, "%i,", m);
fprintf (fp, "%i}\n", mgr.m_NOfPlanes-mgr.m_MaxRingDifference);
}
fprintf (fp, "matrix axis label [2] := view\n");
fprintf (fp, "!matrix size [2] := %i\n",mgr.m_NOfAngles);
fprintf (fp, "matrix axis label [1] := tangential coordinate\n");
fprintf (fp, "!matrix size [1] := %i\n",mgr.m_NOfBins);
 
fprintf (fp, "minimum ring difference per segment := {"); // TO DO : add SPAN (mgr.m_MaxRingDifferenceiff per seg. variable)
fprintf (fp, "%i", -mgr.m_MaxRingDifference);
int m;
for(m=-mgr.m_MaxRingDifference+1; m<=mgr.m_MaxRingDifference; m++) fprintf (fp, ",%i", m);
fprintf (fp, "}\n");
fprintf (fp, "maximum ring difference per segment := {"); // TO DO : add SPAN (mgr.m_MaxRingDifferenceiff per seg. variable)
fprintf (fp, "%i", -mgr.m_MaxRingDifference);
for(m=-mgr.m_MaxRingDifference+1; m<=mgr.m_MaxRingDifference; m++) fprintf (fp, ",%i", m);
fprintf (fp, "}\n");
 
fprintf (fp, "inner ring diameter (cm) := %f\n", mgr.m_RingDiameter/10); // STIR Required parameter, now assigned to FOV (not detectors)
fprintf (fp, "average depth of interaction (cm) := 0.0001\n");
fprintf (fp, "default bin size (cm) := %f\n",0.1*((float)mgr.m_RingDiameter/((float)mgr.m_NOfBins-1.0)) );
fprintf (fp, "number of rings := %i\n",mgr.m_NOfPlanes );
fprintf (fp, "distance between rings (cm) := %f\n", 0.1*((float)mgr.m_AxialDistance/(float)(mgr.m_NOfPlanes-1)) ); // Axial pixel dimension
 
fprintf (fp, "number of detectors per ring := %i\n",mgr.m_NOfAngles*2 );
// fprintf (fp, "number of slices := %i\n",mgr.m_NOfPlanes*mgr.m_NOfPlanes);
fprintf (fp, "number of time frames := 1\n");
fprintf (fp, "image scaling factor[1] := 1\n");
fprintf (fp, "data offset in bytes[1] := 0\n");
fprintf (fp, "quantification units := 1\n");
fprintf (fp, "!END OF INTERFILE := \n");
 
fclose(fp);
 
}
int mbsize = mgr.m_NOfBins*mgr.m_NOfAngles*mgr.m_TotalAxialPlanes*sizeof(SINO_TYPE);
mgr.m_Buffer = (SINO_TYPE *) malloc( mbsize );
 
long unsigned int cont=0;
int i,j,k;
 
for(k=0; k<mgr.m_TotalAxialPlanes; k++) {
for(j=0; j<mgr.m_NOfAngles; j++) {
for(i=0; i<mgr.m_NOfBins; i++) {
mgr.m_Buffer[cont]=mgr.m_projections[i][j][k];
cont++;
}
}
}
 
fp=fopen(name_v, "wb");
 
//printf(4096*sizeof(SINO_TYPE) );
int nb=fwrite(mgr.m_Buffer,1,mbsize, fp);
fclose(fp);
 
#ifndef GAMOS_NO_VERBOSE
printf("PETProjDataMgr::WriteInterfile: File name: %s\n", Filename );
printf("PETProjDataMgr::WriteInterfile: Numer of bytes written: %d\n" , nb );
printf("PETProjDataMgr::WriteInterfile: Planes = %d bins = %d ang_views = %d\n", mgr.m_NOfPlanes, mgr.m_NOfBins, mgr.m_NOfAngles );
printf("PETProjDataMgr::WriteInterfile: Dimensions (mm): Transaxial FOV = %f ; Axial FOV = %f ; Transaxial_pix =%f ; Plane width = %f\n", mgr.m_RingDiameter , mgr.m_AxialDistance , mgr.m_RingDiameter/(mgr.m_NOfBins-1) , mgr.m_AxialDistance/(mgr.m_NOfPlanes-1) ); // Image Axial Pixel(ssrb) == 0.5*(Plane_Width);
printf("... " );
 
printf("PETProjDataMgr::WriteInterfile: Total Coinci: %d\n" , mgr.m_TotalCoincidences );
printf("PETProjDataMgr::WriteInterfile: Sino3D Coinci: %d\n" ,mgr.m_TotalProjectionCoincidences );
#endif
 
}
 
double Mag2(HVector3 x) {
return x.x[0]*x.x[0]+x.x[1]*x.x[1]+x.x[2]*x.x[2];
}
 
 
double Mag(HVector3 x) {
return sqrt(Mag2(x));
}
 
void SetPhi(HVector3 *r,double phi) {
 
}
void SetTheta(HVector3 *r,double phi) {
 
}
 
double Product(HVector3 a, HVector3 b) {
double c=0;
for (int i=0; i<3; i++) c += a.x[i] * b.x[i];
return c;
}
 
HVector3 Multiply(double a, HVector3 b) {
HVector3 c;
for (int i=0; i<3; i++) c.x[i]= a* b.x[i];
return c;
}
 
HVector3 Add(HVector3 a, HVector3 b) {
HVector3 c;
for (int i=0; i<3; i++) c.x[i]= a.x[i] + b.x[i];
return c;
}
 
HVector3 Subtract(HVector3 a, HVector3 b) {
HVector3 c;
for (int i=0; i<3; i++) c.x[i]= a.x[i] - b.x[i];
return c;
}
 
 
HVector3 Hits2Digits(const HVector3 r) {
if (!mgr.m_nch) return r;
float smear=0.5;
 
if (mgr.m_nch<0) smear=Random(0,1);
 
double angle = atan2(r.x[0],r.x[1]); // vrne kot med -pi in pi
double twopi=2*Pi();
if (angle<0) angle+=twopi;
 
angle= ((int)(angle/twopi*fabs(mgr.m_nch))+smear)*twopi/fabs(mgr.m_nch);
//(mgr.m_rnd->Rndm()-0.5)*mgr.m_AxialDistance;
HVector3 x;
x.x[0]=sin(angle);
x.x[1]=cos(angle);
x.x[2]=0;
return x; // z coordinata ni cisto v redu
 
}
 
int FwdProject(double x,double y, double z, int nmax, int h) {
HVector3 r;
r.x[0]=x;
r.x[1]=y;
r.x[2]=z;
int h2d=1;
double tfac=mgr.m_RingDiameter*mgr.m_RingDiameter/4-Mag2(r);
double rfac= mgr.m_AxialDistance/mgr.m_RingDiameter;
for (int i=0; i<nmax; i++) {
 
double phi= Random(0,Pi());
HVector3 s;
s.x[0]=1;
s.x[1]=0;
s.x[2]=0;
SetPhi(&s,phi);
double sign = (Random(0,1)>0.5)? 1 : 0;
double theta = acos(Random(0,rfac));
theta+=sign*Pi();
 
SetTheta(&s,theta);
double t=Product(r,s);
HVector3 rx=Add(r,Multiply(-t,s));
 
double d=sqrt(t*t+tfac);
 
HVector3 r1= Add(rx,Multiply(d,s));
HVector3 r2= Add(rx,Multiply(-d,s));
 
//r1=Hits2Digits(r1);
//r2=Hits2Digits(r2);
 
HVector3 s1=Subtract(r2,r1);
double s1len= Mag(s1);
int niter=(int) (100*s1len/mgr.m_RingDiameter);
for (int j=0; j<niter; j++) {
r2=Add(r1,Multiply(Random(0,1),s1));
if (h2d) H2D_Fill(h,r2.x[0],r2.x[1],1);
else H3D_Fill(h,r2.x[0],r2.x[1],r2.x[2],1);
}
}
return 0;
}
 
int FwdProject2d(int img, int h) {
 
for (int i=0; i<H2D_GetNbinsX(img); i++) {
double x_=H2D_GetXBinCenter( img, i+1 );
for (int j=0; j<H2D_GetNbinsY(img); j++) {
double y_=H2D_GetYBinCenter(img, j+1 );
double density= H2D_GetBinContent(img,i+1,j+1);
if (density>0) FwdProject(x_,y_,mgr.m_AxialDistance*(Random(-0.5,0.5)), density,h);
}
}
return 0;
}
 
 
int FwdProject3d(int img, int h) {
 
for (int i=0; i<H3D_GetNbinsX(img); i++) {
double x_=H3D_GetXBinCenter( img, i+1 );
for (int j=0; j<H3D_GetNbinsY(img); j++) {
double y_=H3D_GetYBinCenter(img, j+1 );
for (int k=0; k<H3D_GetNbinsZ(img); k++) {
double z_=H3D_GetZBinCenter( img, k+1 );
double density= H3D_GetBinContent(img, i+1,j+1,k+1);
if (density>0) FwdProject(x_,y_,z_, density,h);
}
}
}
return 0;
}
 
 
int Phantom(int kaj) {
int img = H2D_Init(1, "img","Original Image",100,-50,50,100,-50,50);
int i=0;
// izberi sliko 0: kroglice, 1: point source 2: central ball
switch (kaj) {
 
case 0:
for (i=0; i<H2D_GetNbinsX(img); i++) {
for (int j=0; j<H2D_GetNbinsY(img); j++) {
double x_=H2D_GetXBinCenter( img, i+1 );
double y_=H2D_GetYBinCenter( img, j+1 );
double density=1000;
if ((x_*x_+y_*y_)<6) H2D_SetBinContent(img, i+1,j+1,density);
 
density=500;
if ((x_-25)*(x_-25)+y_*y_<12) H2D_SetBinContent(img,i+1,j+1,density);
density=2000;
if ((y_-25)*(y_-25)+x_*x_<2) H2D_SetBinContent(img,i+1,j+1,density);
}
}
break;
 
case 2:
for (i=0; i<H2D_GetNbinsX(img); i++) {
for (int j=0; j<H2D_GetNbinsY(img); j++) {
double x_=H2D_GetXBinCenter( img, i+1 );
double y_=H2D_GetYBinCenter( img, j+1 );
double density=1000;
if ((x_*x_+y_*y_)<12.5) H2D_SetBinContent(img, i+1,j+1,density);
}
}
break;
 
case 1:
H2D_Fill(img, 25,25,10000);
break;
 
}
 
return img;
}
/cvi/apps/petdemo/PETProjDataMgr.h
0,0 → 1,110
//
// ********************************************************************
// * License and Disclaimer *
// * *
// * The GAMOS software is copyright of the Copyright Holders of *
// * the GAMOS Collaboration. It is provided under the terms and *
// * conditions of the GAMOS Software License, included in the file *
// * LICENSE and available at http://fismed.ciemat.es/GAMOS/license .*
// * These include a list of copyright holders. *
// * *
// * Neither the authors of this software system, nor their employing *
// * institutes,nor the agencies providing financial support for this *
// * work make any representation or warranty, express or implied, *
// * regarding this software system or assume any liability for its *
// * use. Please see the license in the file LICENSE and URL above *
// * for the full disclaimer and the limitation of liability. *
// * *
// * This code implementation is the result of the scientific and *
// * technical work of the GAMOS collaboration. *
// * By using, copying, modifying or distributing the software (or *
// * any work based on the software) you agree to acknowledge its *
// * use in resulting scientific publications, and indicate your *
// * acceptance of all terms of the GAMOS Software license. *
// ********************************************************************
//
 
#ifndef PETProjDataMgr_h
#define PETProjDataMgr_h
 
/*---------------------------------------------------------------------------
ClassName: PETProjDataMgr
Author: M. Canadas, P. Arce
Changes: 01/11: creation
 
-------------------------------------------------------------------------
// Description
PET output (List-mode X Y Z) into projection data (sinograms).
 
Output data: Sinograms for PET image reconstruction. Interfile format, STIR compatible (.hs/.s),
STIR Software for Tomographic Image Reconstruction: http://stir.sourceforge.net/main.htm
 
-----------------------------------------------------------------------*/
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include "H1D.h"
#include "H2D.h"
#include "H3D.h"
 
#include "daq.h"
 
 
typedef unsigned short SINO_TYPE; //!!NOTE: Check "Write_sino3D" (.hv file) if SINO_TYPE changes !!
 
struct PETProjDataMgr {
int m_Debug;
double m_AxialDistance;
double m_RingDiameter;
int m_NOfPlanes;
int m_NOfBins;
int m_NOfAngles;
int m_MaxRingDifference;
int m_OutFormat;
int m_nch;
int m_TotalAxialPlanes;
char m_Filename;
 
//G4bool bDumpCout;
SINO_TYPE ***m_projections;
SINO_TYPE *m_Buffer;
unsigned long int m_TotalCoincidences;
unsigned long int m_TotalProjectionCoincidences;
 
 
};
//-----------------------------------------------------------------------
int PETProjDataMgrInit();
void PETProjDataMgrFree();
void AddEvent(const HVector3 pos1, const HVector3 pos2);
//void ReadFile();
//PETOutput ReadEvent( G4bool& bEof );
void SetProjection(int axialplane, int h2d);
void WriteInterfile(const char *fname);
void SetNPlanes(int n);
void SetNBin(int n);
void SetNAng(int n);
void SetDebug(int n);
void SetNumberOfChannels(int n);
 
void SetRingDiameter( double x);
void SetAxialDistance( double x);
int Phantom(int kaj);
int FwdProject2d(int img2d, int h2d);
int FwdProject3d(int img3d, int h3d);
 
int FwdProject(double x,double y, double z, int nmax, int h1d);
HVector3 Hits2Digits(const HVector3 r);
 
 
struct PETProjDataMgr* GetInstance();
#endif
 
/cvi/apps/petdemo/analyse.c
0,0 → 1,351
 
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include "daq.h"
#include "H1D.h"
#include "H2D.h"
#include "PETProjDataMgr.h"
 
double gAdc[MAXCH]; // raw ADC ji
float gNtdata[MAXCH*3];
int m_write=0;
 
double gData[MAXCH]; // korigirani ADC ji
 
float gSum[MAXPMT];
float gRawSum[MAXPMT];
float gMax[MAXPMT];
float gSumCluster[MAXPMT];
int gNabove[MAXPMT];
 
 
 
 
 
double GetEnergy(int ch, int adc) {
return (adc-conf.apedestals[ch])/(conf.apeak[ch]-conf.apedestals[ch])*conf.peakscaling;
}
 
 
 
int GetGlobalPosition(int ipmt, double angle, float cx, float cy, float *gposition) {
 
double phi = angle + conf.module[ipmt].phi;
double r = conf.module[ipmt].r;
double sinphi = sin(phi);
double cosphi = cos(phi);
 
gposition[0]= cosphi* r + sinphi * cx ;
gposition[1]= sinphi* r - cosphi * cx ;
gposition[2]= cy;
 
//if (m_debug) printf( "[%d] phi=%f %f global ( %f , %f , %f )\n" ,ipmt, module[ipmt].phi,angle, rv.x(), rv.y(), rv.z());
return 0;
}
 
 
int GetRealPosition(int ipmt, float px,float py,float *cx,float *cy) {
// iz CoG izracuna pozicijo na kristalu
 
int binx = 0; //####m_crystalid[ipmt]->GetXaxis()->FindBin(px);
int biny = 0; //####m_crystalid[ipmt]->GetYaxis()->FindBin(py);
int crystalid = 0;//####m_crystalid[ipmt]->GetBinContent(binx,biny);
*cx= ( crystalid % conf.nofcrystalsx - conf.nofcrystalsy/2. + 0.5 ) * conf.crystalpitchx;
*cy= ( crystalid / conf.nofcrystalsx - conf.nofcrystalsx/2. + 0.5 ) * conf.crystalpitchy;
 
 
// razmazi pozicijo po povrsini kristala
double rndx = Random(-0.5,0.5)* conf.crystalpitchx;
double rndy = Random(-0.5,0.5)* conf.crystalpitchy;
*cx+= rndx;
*cy+= rndy;
return crystalid;
}
 
 
int HistogramsInit() {
 
if (conf.write) {
char varlist[1024], tmplist[1024];
sprintf( varlist,"sum0:sum1:sum2:sum3:nh0:nh1:nh2:nh3:c0:c1:c2:c3:px0:px1:px2:px3:py0:py1:py2:py3:rx0:rx1:rx2:rx3:ry0:ry1:ry2:ry3:max0:max1:max2:max3");
for (int i=0; i<MAXCH; i++) {
sprintf(tmplist,"%s",varlist);
sprintf(varlist,"%s:a%d",tmplist,i);
}
/*
energija .. (adc -pedestal) * scaling
 
sum0 .. sum3 vsota energij po vseh kanalih fotopomnozevalke
nh0 .. nh4 stevilo zadetkov na fotopomnozevalki (adc nad thresholdom)
c0 .. c4 vsota energij za kanale nad thresholdom
px0 .. px4 x koordinata COG na fotopomnozevalki
py0 .. py4 y koordinata COG na fotopomnozevalki
max0 .. max3 maximalna energija na kanalu na fotopomnozevalki
a0 .. a63 energija na i-tem kanalu
*/
//gNtuple = new TNtuple("nt","Pet RAW data",varlist);
printf ("#%s#\n",varlist);
}
m_Nhits = H2D_Init(301,"hnabove","Stevilo zadetkov nad nivojem diskriminacije",4,-0.5,3.5, 16,+0.5,16.5 ); // stevilo hitov nad thresholdom
m_GlobalPosition =H2D_Init(302,"globalxy","Rekonstruirana koordinata zadetka",200,-80,80,200,-80,80); // reconstruirana koordinata v globalnem sistemu
 
 
char hname[0xFF];
char hn[0xFF];
int i;
for (i=0; i<MAXCH; i++) {
sprintf(hname,"Raw ADC Ch. %d ;ADC;N",i);
sprintf(hn,"ach%d",i);
//m_Adc[i] = H1D_Init(i,hn,hname,4000,-0.5,3999.5); // osnovni adcji
m_Adc[i] = H1D_Init(i,hn,hname,400,500-0.5,899.5); // osnovni adcji
sprintf(hname,"cADC za dogodke z manj kot x hitov na pmtju Ch. %d ;ADC;N",i);
sprintf(hn,"cutch%d",i);
m_AdcCut[i] = H1D_Init(i+MAXCH, hn,hname,4000,-0.5,3999.5); // adcji za zadetke z manj kot 7 hiti na PMTju
sprintf(hname,"cADC proti stevilu zadetkov na pmtju Ch. %d ;ADC;N",i);
sprintf(hn,"singlech%d",i);
m_Adc_vs_Nhits[i] = H2D_Init(i,hn,hname,200,0,4000, 17,-0.5,16.5); // adc proti stevilu zadetkov na PMTju
sprintf(hname,"cADC proti vsoti kanalov na pmtju, Ch. %d ;ADC;ADCsum",i);
sprintf(hn,"corrch%d",i);
m_Adc_vs_Sum[i] = H2D_Init(i+MAXCH,hn,hname,200,0,4000, 200,0,12000 ); // raw adc proti vsoti kanalov na PMTju
sprintf(hname,"Raw ADC proti vsoti kanalov na pmtju, Ch. %d ;ADC;ADCsum",i);
sprintf(hn,"adcvssumch%d",i);
m_Adc_vs_Sum_Uncorected[i] =H2D_Init(i+MAXCH*2,hn,hname,100,0,3500, 100,5000,12000 ); // adc proti vsoti kanalov na PMTju
}
 
for (i=0; i<MAXPMT; i++) {
sprintf(hname,"Vsota cADC na PMTju %d ;ADC;N",i);
sprintf(hn,"pmt%d",i);
m_AdcSum[i] =H1D_Init(200+i,hn,hname,200,0,20000); // vsota adcjev na PMTju
sprintf(hname,"Vsota cADC na PMTju za dogodke z manj kot x zadetki %d ;ADC;N",i);
sprintf(hn,"clusterpmt%d",i);
m_AdcSumCluster[i] = H1D_Init(216+i,hn,hname,200,0,20000); // vsota adcjev na pmtju, ki so nad threshholdom, za dogodke z manj kot 5 zadetki na PMTju
 
sprintf(hname,"Center naboja CoG PMT %d ;x;y",i);
sprintf(hn,"pmt1%d",i);
m_CenterOfGravity[i] =H2D_Init(200+i,hn,hname,200,-1.25,4.25,200,-1.25,4.25); // center naboja na PMTju
 
sprintf(hname,"Center naboja CoG za zadetke nad thresholdom PMT %d ;x;y",i);
sprintf(hn,"pmt2%d",i);
m_CenterOfGravityforChAboveThreshold[i] = H2D_Init(216+i,hn,hname,200,0,3,200,0,3); // center naboja za zadetke nad thresholdom
 
sprintf(hname,"Rekonstruirana koordinata PMT %d ;x(mm);y(mm)",i);
sprintf(hn,"pmt3%d",i);
m_ReconstructedPosition[i] = H2D_Init(232+i,hn,hname,200,-12,12,200,-12,12); // korigirana koordinata na pmtju
 
sprintf(hname,"SumAdc PMT %d ;SumAdc(a.u.);crystal ID",i);
sprintf(hn,"sumadc%d",i);
m_SumAdc[i] =H2D_Init(248+i,hn,hname,200,0,12000,81,-0.5,80.5); // SumADC spekter za razlicne kristale
}
 
// store mapping
//TH1I *hx= new TH1I("mapping", "ch mapping", 16, -0.5,15.5);
//for (int j=0;j<16;j++) hx->Fill(j,m_geo->ch[j].idx);
 
return 0;
};
 
int HistogramsWrite(const char *fname){
FILE *fp = fopen(fname, "wb");
if (fp){
int i;
for (i=0;i<H1DMAX;i++){ if (H1D_Exist(i)) H1D_Write2File(i, fp); }
for (i=0;i<H2DMAX;i++){ if (H2D_Exist(i)) H2D_Write2File(i, fp); }
for (i=0;i<H3DMAX;i++){ if (H3D_Exist(i)) H3D_Write2File(i, fp); }
fclose(fp);
}
 
return 0;
}
 
 
int HistogramsFill() {
int i=0, ipmt;
for (i=0; i<MAXCH; i++) { // zanka cez vse elektronske kanale;
float x=gData[i];
int ipmt= i/16;
float s=gSum[ipmt];
// int idx= 2*(ipmt)+64;
// const float fSlope1=2;
// const float fSlope2=3;
// if ( ((s-gSumPedestals[ipmt] ) > fSlope1*(x-gPedestals[i])) && ((s-gSumPedestals[ipmt] ) < fSlope2*(x-gPedestals[ipmt])) ) {
if (gNabove[ipmt]<7) {
H2D_Fill(m_Adc_vs_Sum[i],x,s,1);
H2D_Fill(m_Adc_vs_Sum_Uncorected[i], gAdc[i],gRawSum[ipmt],1);
H1D_Fill(m_AdcCut[i],x,1);
}
}
 
HVector3 r_coincidence[2];
for (int k=0; k<2; k++) for (int i1=0; i1<3; i1++) r_coincidence[k].x[i1]=0;
int f_coincidence[2]= {0,0};
 
for (ipmt=0; ipmt<4; ipmt++) { // zanka preko pmtjev
int j2= (ipmt/2)*2+1-ipmt%2; // sosednja fotopomnozevalka
 
float posx[2]= {0,0};
float posy[2]= {0,0};
float sum[2]= {0,0};
for (int ich=0; ich<16; ich++) { // zanka preko elektronskih kanalov na fotopomnozevalki
int ch= ich+ipmt*16;
if (gMax[ipmt]>conf.adcthreshold) {
posx[0]+= gData[ch]*conf.channel[ich].ix;
posy[0]+= gData[ch]*conf.channel[ich].iy;
sum[0] += gData[ch];
 
if (gData[ch]> 0.1*gMax[ipmt]) { // pri racunanju pozicije upostevaj le kanale, ki imajo vrednost vecjo od ratio*maksimalna na tisti fotopomnozevalki ********
posx[1]+= gData[ch]*conf.channel[ich].ix;
posy[1]+= gData[ch]*conf.channel[ich].iy;
sum[1] += gData[ch];
}
}
}
 
if (m_write) {
gNtdata[12+ipmt]=posx[0]/sum[0];
gNtdata[16+ipmt]=posy[0]/sum[0];
gNtdata[20+ipmt]=(sum[1]>0)?posx[1]/sum[1]:-10;
gNtdata[24+ipmt]=(sum[1]>0)?posy[1]/sum[1]:-10;
gNtdata[28+ipmt]=sum[0];
}
if (gMax[ipmt]>gMax[j2]) { // izberi fotopomnozevalko, ki ima vec zadetkov kot soseda TRG= (P1|P2) & (P3|P4)
 
if ( sum[0] > 0 ) {
float px=posx[0]/sum[0];
float py=posy[0]/sum[0];
 
H2D_Fill(m_CenterOfGravity[ipmt],px,py,1);
 
float cx=0; //koordinata na pmtju
float cy=0;
int crystalID = GetRealPosition(ipmt,px,py,&cx,&cy);
H2D_Fill(m_ReconstructedPosition[ipmt],cx,cy,1);
H2D_Fill(m_SumAdc[ipmt],gSum[ipmt], crystalID,1 );
if (debug > 2) printf("ipmt=%d cx=%f cy=%f\n",ipmt,cx,cy);
GetGlobalPosition(ipmt, conf.rotation, cx, cy,r_coincidence[ipmt/2].x) ;
f_coincidence[ipmt/2] =1;
H2D_Fill(m_GlobalPosition,r_coincidence[ipmt/2].x[0],r_coincidence[ipmt/2].x[1],1);
 
}
if ( sum[1] > 0 ) {
 
float px=posx[1]/sum[1];
float py=posy[1]/sum[1];
// float px0=posx[0]/sum[0];
// float py0=posy[0]/sum[0];
// if (px0<0.25 || px0>2.75 || py0< 0.25 || py0> 2.75)
// if (((py0>0.75 && py0<1.25) || (py0>1.75 && py0<2.25)) && (px0< 0.25 || px0>2.75 ) )
H2D_Fill(m_CenterOfGravityforChAboveThreshold[ipmt],px,py,1);
}
 
}
}
 
for (i=0; i<MAXCH; i++) if (gData[i]>conf.adcthreshold) {
H2D_Fill(m_Adc_vs_Nhits[i],gData[i],gNabove[i/16],1);
}
for (i=0; i<MAXCH; i++) if (gData[i]>conf.adcthreshold && gNabove[i/16]<5 ) {
gSumCluster[i/16]+=gData[i];
}
for (ipmt=0; ipmt<4; ipmt++) {
H1D_Fill(m_AdcSum[ipmt],gSum[ipmt],1);
H1D_Fill(m_AdcSumCluster[ipmt],gSumCluster[ipmt],1);
}
 
for (i=0; i<4; i++) H1D_Fill(m_Nhits,i,gNabove[i]);
 
if (m_write) {
for (i=0; i<4; i++) {
gNtdata[i]=gSum[i];
gNtdata[4+i]=gNabove[i];
gNtdata[8+i]=gSumCluster[i];
}
for (i=0; i<MAXCH; i++) gNtdata[32+i]=gData[i];
//gNtuple->Fill(gNtdata);
}
//
// coincidences
//
if (f_coincidence[0]&&f_coincidence[1]) {
conf.coincidences++;
if (debug > 1) {
printf( "Coincidence ( %f , %f , %f ) ( %f , %f , %f )\n" ,
r_coincidence[0].x[0], r_coincidence[0].x[1], r_coincidence[0].x[2],
r_coincidence[1].x[0], r_coincidence[1].x[1], r_coincidence[1].x[2] );
}
AddEvent( r_coincidence[0] , r_coincidence[1]);
}
return 0;
};
 
 
 
int analyse(int nb, uint32_t *buf) {
 
int idx=1;
int neve=buf[0]/2;
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
for (int nev=0; nev<neve; nev++) {
int len=buf[idx];
int sb =buf[idx+1];
unsigned int *pbuf=&buf[idx+2];
if (sb!=0xffab) {
printf("0x%04x!0xffab len=%d\n",sb,len);
break;
}
// postavi na nic
#define InitArrayWithAValue(arr,n,x) {for (int i=0;i<n;i++) arr[i]=x;}
 
//------------------------------------------------------------
// postavi na nic
 
InitArrayWithAValue( gSum , MAXPMT , 0);
InitArrayWithAValue( gRawSum , MAXPMT , 0);
InitArrayWithAValue( gMax , MAXPMT , 0);
InitArrayWithAValue( gSumCluster, MAXPMT , 0);
InitArrayWithAValue( gNabove , MAXPMT , 0);
InitArrayWithAValue( gData , MAXCH, 0);
InitArrayWithAValue( gAdc , MAXCH, 0);
for (int i0=0; i0<len-2; i0+=2) {
 
int data0 = pbuf[i0];
int data1 = pbuf[i0+1];
int geo = (data1 >> 11) & 0x1f;
int ch = (data1&0x1f) | (geo<<5);
int dtype = (data1>>9)&0x3;
int adc = data0&0xfff;
if (dtype == 0 & ch<MAXCH) {
if (debug) printf("%d ADC %d %d\n", geo, ch, adc);
if (ch<MAXCH) H1D_Fill(ch, adc,1);
int ipmt = ch/16;
 
gAdc[ch]=adc;
gRawSum[ipmt]+=adc;
 
gData[ch]=GetEnergy(ch,adc);
 
gSum[ipmt]+=gData[ch];
if (gData[ch] >gMax[ipmt] ) gMax[ipmt]= gData[ch];
if (gData[ch] >conf.adcthreshold ) gNabove[ipmt]++;
//#### m_Adc[ch]->Fill(adc);
}
};// for (int i0=0;i0<len-2;i0+=2)
//------------------------------------------------------------
 
idx+=len+1;
HistogramsFill();
if (m_write) {
gNtdata[0]=0;
for (int i=0; i<MAXCH; i++) gNtdata[i+1]=gAdc[i];
//####gNtuple->Fill(gNtdata);
}
 
} // for (int nev=0;nev<neve;nev++)
 
 
 
 
return 0;
}
/cvi/apps/petdemo/config.ini
0,0 → 1,20
[scintillator]
 
nofcrystalsx=9
nofcrystalsy=9
crystalpitchx=2.25
crystalpitchy=2.25
 
 
[sensor]
modules=ini/modules.map
channels=ini/m16.map
 
 
[calibration]
 
sumpedestals=ini/sumpedestals.dat
pedestals=ini/pedestals_new.dat
photopeak=ini/fotovrh.dat
channelcalibration=ini/calibration_all.root
adcthreshold=300
/cvi/apps/petdemo/daq.h
0,0 → 1,117
#ifndef _daq_h_
#define _daq_h_
 
#define MAXCH 64
#define MAXPMT 16
 
#define NSTEPS 4790 // en krog je 4790 korakov
 
typedef struct {
float x[3];
} HVector3;
 
struct Module {
double r;
double phi;
};
 
 
 
struct Channel {
int ix;
int iy;
};
 
 
struct Config {
int nofcrystalsx;
int nofcrystalsy;
double crystalpitchx;
double crystalpitchy;
char *modules;
char *channels;
char *sumpedestals;
char *pedestals;
char *photopeak;
char *channelcalibration;
int adcthreshold;
double peakscaling;
struct Module module[16];
struct Channel channel[16];
float apedestals[MAXCH];
float apeak[MAXCH];
float rotation;
int write;
int coincidences; // stevilo koincidenc
} conf;
 
 
 
#define NUM_CHANNELS 1
#define PEDESTAL 255
#define SCAN_TYPE 1
 
#define RUNREC_ID 1
#define ENDREC_ID 2
#define POSREC_ID 3
#define EVTREC_ID 4
 
typedef struct {
unsigned int id,length;
unsigned int fver,time;
unsigned int num_events,num_channels,pedestal,xy;
int nx,x0,dx,ny,y0,dy;
} RUNREC;
typedef struct {
unsigned int id,length;
unsigned int time,dum;
} ENDREC;
 
typedef struct {
unsigned int id,length;
unsigned int time;
int num_iter_x,mikro_pos_x,set_pos_x;
int num_iter_y,mikro_pos_y,set_pos_y;
} POSREC;
 
int debug;
int fPedestal;
int fThresholdEnable;
int fStop;
int fMode;
int clear();
int end();
int event(unsigned int *, int,int *, int );
int init();
int vmconnect();
int vmdisconnect();
int vmacquire (int neve,FILE *fp, char *fpedname);
 
 
int HistogramsInit();
int HistogramsFill();
int HistogramsWrite(const char *fname);
int GetGlobalPosition(int ipmt, double angle, float cx, float cy, float *gposition);
int analyse(int nb, uint32_t *data);
 
 
//-----------------
 
int m_Adc[MAXCH];
int m_AdcCut[MAXCH];
int m_Adc_vs_Nhits[MAXCH];
int m_Adc_vs_Sum[MAXCH];
int m_Adc_vs_Sum_Uncorected[MAXCH];
int m_Nhits;
int m_AdcSum[MAXPMT];
int m_CenterOfGravity[MAXPMT];
int m_ReconstructedPosition[MAXPMT];
int m_CenterOfGravityforChAboveThreshold[MAXPMT];
int m_GlobalPosition;
int m_AdcSumCluster[MAXPMT];
int m_MaxAdc[MAXPMT];
int m_SumAdc[MAXPMT];
 
//-----------------
 
#endif
/cvi/apps/petdemo/ini/calibration_all.root
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: petdemo/ini/config.xml
===================================================================
--- petdemo/ini/config.xml (nonexistent)
+++ petdemo/ini/config.xml (revision 264)
@@ -0,0 +1,24 @@
+<?xml version="1.0"?>
+<pet>
+
+<scintillator>
+<nofcrystalsx>9</nofcrystalsx>
+<nofcrystalsy>9</nofcrystalsy>
+<crystalpitchx>2.25</crystalpitchx>
+<crystalpitchy>2.25</crystalpitchy>
+</scintillator>
+
+<sensor>
+<modules>ini/modules.map</modules>
+<channels>ini/m16.map</channels>
+</sensor>
+
+<calibration>
+<sumpedestals>ini/sumpedestals.dat</sumpedestals>
+<pedestals>ini/pedestals_new.dat</pedestals>
+<photopeak>ini/fotovrh.dat</photopeak>
+<channelcalibration>ini/calibration_all.root</channelcalibration>
+<adcthreshold>300</adcthreshold>
+</calibration>
+
+</pet>
Index: petdemo/ini/fotovrh.dat
===================================================================
--- petdemo/ini/fotovrh.dat (nonexistent)
+++ petdemo/ini/fotovrh.dat (revision 264)
@@ -0,0 +1,64 @@
+0 3196.160841
+1 1711.854801
+2 1635.564402
+3 1597.419203
+4 1711.854801
+5 1584.704136
+6 1355.832941
+7 1432.123339
+8 1533.843871
+9 1648.279469
+10 1597.419203
+11 1521.128804
+12 1470.268539
+13 1686.424668
+14 1622.849336
+15 1660.994535
+16 2522.262320
+17 2220.457458
+18 2423.898521
+19 2334.893056
+20 2551.049185
+21 2449.328654
+22 2703.629982
+23 2614.624517
+24 2487.473853
+25 2347.608122
+26 2156.882126
+27 2233.172524
+28 2589.194384
+29 2233.172524
+30 1864.435598
+31 1915.295864
+32 2852.854047
+33 1915.295864
+34 1915.295864
+35 2156.882126
+36 1991.586262
+37 1966.156129
+38 1915.295864
+39 1902.580797
+40 1940.725996
+41 1877.150664
+42 1940.725996
+43 1877.150664
+44 1928.010930
+45 1940.725996
+46 1991.586262
+47 1928.010930
+48 2852.854047
+49 2080.591727
+50 1813.575332
+51 1978.871196
+52 2245.887591
+53 2080.591727
+54 1813.575332
+55 1940.725996
+56 1902.580797
+57 1889.865731
+58 1673.709601
+59 1699.139734
+60 1813.575332
+61 1877.150664
+62 1508.413738
+63 1686.424668
Index: petdemo/ini/m16.map
===================================================================
--- petdemo/ini/m16.map (nonexistent)
+++ petdemo/ini/m16.map (revision 264)
@@ -0,0 +1,17 @@
+#CH IX IY
+15 0 0
+14 0 1
+13 1 0
+12 1 1
+11 0 3
+10 0 2
+9 1 3
+8 1 2
+7 2 3
+6 2 2
+5 3 3
+4 3 2
+3 2 0
+2 2 1
+1 3 0
+0 3 1
Index: petdemo/ini/m16_reverse.map
===================================================================
--- petdemo/ini/m16_reverse.map (nonexistent)
+++ petdemo/ini/m16_reverse.map (revision 264)
@@ -0,0 +1,17 @@
+#CH IX IY
+0 0 0
+1 0 1
+2 1 0
+3 1 1
+4 0 3
+5 0 2
+6 1 3
+7 1 2
+8 2 3
+9 2 2
+10 3 3
+11 3 2
+12 2 0
+13 2 1
+14 3 0
+15 3 1
Index: petdemo/ini/modules.map
===================================================================
--- petdemo/ini/modules.map (nonexistent)
+++ petdemo/ini/modules.map (revision 264)
@@ -0,0 +1,5 @@
+#module ID (connector) r(mm) angle(deg)
+0 61 0
+1 61 22.5
+2 61 180
+3 61 202.5
Index: petdemo/ini/pedestals.dat
===================================================================
--- petdemo/ini/pedestals.dat (nonexistent)
+++ petdemo/ini/pedestals.dat (revision 264)
@@ -0,0 +1,64 @@
+0 339.764
+1 356.526
+2 328.481
+3 283.352
+4 307.915
+5 365.996
+6 316.9
+7 316.175
+8 348.054
+9 338.349
+10 385.901
+11 342.318
+12 353.552
+13 348.903
+14 340.133
+15 335.819
+16 337.356
+17 334.646
+18 322.328
+19 320.371
+20 304.205
+21 347.213
+22 336.6
+23 325.069
+24 351.203
+25 347.073
+26 307.865
+27 285.498
+28 332.387
+29 325.668
+30 352.567
+31 343.887
+32 360.176
+33 328.103
+34 337.231
+35 324.995
+36 294.699
+37 354.216
+38 304.069
+39 369.572
+40 375.065
+41 347.03
+42 341.669
+43 311.298
+44 338.887
+45 322.734
+46 388.779
+47 320.116
+48 323.118
+49 350.691
+50 344.091
+51 334.575
+52 349.252
+53 351.313
+54 338.096
+55 330.029
+56 348.157
+57 304.036
+58 300.07
+59 304.476
+60 307.644
+61 296.693
+62 327.384
+63 345.163
Index: petdemo/ini/pedestals_new.dat
===================================================================
--- petdemo/ini/pedestals_new.dat (nonexistent)
+++ petdemo/ini/pedestals_new.dat (revision 264)
@@ -0,0 +1,64 @@
+0 339.13
+1 354.26
+2 327.546
+3 282.69
+4 312.863
+5 365.795
+6 317.055
+7 316.198
+8 348.804
+9 337.947
+10 385.535
+11 341.414
+12 351.877
+13 346.92
+14 339.204
+15 334.106
+16 331.704
+17 333.163
+18 302.789
+19 319.067
+20 325.209
+21 348.417
+22 337.329
+23 326.671
+24 351.699
+25 348.799
+26 308.549
+27 286.519
+28 332.446
+29 324.031
+30 351.92
+31 342.048
+32 359.25
+33 325.419
+34 334.892
+35 322.25
+36 293.95
+37 354.971
+38 304.267
+39 370.495
+40 374.192
+41 347.656
+42 340.723
+43 311.266
+44 301.842
+45 320.301
+46 387.6
+47 318.137
+48 321.878
+49 348.859
+50 342.913
+51 332.14
+52 346.995
+53 350.787
+54 337.321
+55 329.6
+56 346.56
+57 303.365
+58 298.587
+59 302.891
+60 306.159
+61 293.716
+62 324.878
+63 341.973
Index: petdemo/ini/ph511.dat
===================================================================
--- petdemo/ini/ph511.dat (nonexistent)
+++ petdemo/ini/ph511.dat (revision 264)
@@ -0,0 +1,84 @@
+0 1700
+1 1900
+2 1680
+3 1700
+4 1870
+5 1680
+6 1430
+7 1460
+8 1680
+9 1700
+10 1713
+11 1650
+12 1630
+13 1830
+14 1750
+15 1740
+16 2440
+17 2320
+18 2640
+19 2500
+20 2790
+21 2600
+22 2900
+23 2820
+24 2660
+25 2470
+26 2310
+27 2330
+28 2817
+29 2860
+30 2140
+31 2140
+32 2120
+33 2410
+34 1990
+35 2480
+36 2200
+37 2100
+38 2040
+39 2060
+40 2090
+41 1970
+42 2032
+43 1970
+44 2054
+45 2120
+46 2076
+47 1920
+48 2206
+49 2420
+50 1930
+51 2290
+52 2400
+53 2425
+54 1950
+55 2400
+56 2076
+57 2010
+58 1792
+59 1830
+60 2000
+61 2032
+62 1640
+63 1614
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
Index: petdemo/ini/sumpedestals.dat
===================================================================
--- petdemo/ini/sumpedestals.dat (nonexistent)
+++ petdemo/ini/sumpedestals.dat (revision 264)
@@ -0,0 +1,4 @@
+0 5413.22
+1 5264.29
+2 5394.42
+3 5261.11
Index: petdemo/libxxusb.cpp
===================================================================
--- petdemo/libxxusb.cpp (nonexistent)
+++ petdemo/libxxusb.cpp (revision 264)
@@ -0,0 +1,1651 @@
+
+// libxxusb.cpp : Defines the entry point for the DLL application.
+//
+
+
+
+#include <string.h>
+#include <malloc.h>
+#include "usb.h"
+#include "libxxusb.h"
+#include <time.h>
+
+
+
+
+// 03/09/06 Release 3.00 changes
+// 07/28/06 correction CAMAC write for F to be in range 16...23
+// 10/09/06 correction CAMAC read for F to be in range <16 OR >23
+// 10/16/06 CAMAC DGG corrected
+// 12/28/07 Open corrected for bug when calling register after opening
+/*
+******** xxusb_longstack_execute ************************
+
+ Executes stack array passed to the function and returns the data read from the VME bus
+
+ Paramters:
+ hdev: USB device handle returned from an open function
+ DataBuffer: pointer to the dual use buffer
+ when calling , DataBuffer contains (unsigned short) stack data, with first word serving
+ as a placeholder
+ upon successful return, DataBuffer contains (unsigned short) VME data
+ lDataLen: The number of bytes to be fetched from VME bus - not less than the actual number
+ expected, or the function will return -5 code. For stack consisting only of write operations,
+ lDataLen may be set to 1.
+ timeout: The time in ms that should be spent tryimg to write data.
+
+ Returns:
+ When Successful, the number of bytes read from xxusb.
+ Upon failure, a negative number
+
+ Note:
+ The function must pass a pointer to an array of unsigned integer stack data, in which the first word
+ is left empty to serve as a placeholder.
+ The function is intended for executing long stacks, up to 4 MBytes long, both "write" and "read"
+ oriented, such as using multi-block transfer operations.
+ Structure upon call:
+ DataBuffer(0) = 0(don't care place holder)
+ DataBuffer(1) = (unsigned short)StackLength bits 0-15
+ DataBuffer(2) = (unsigned short)StackLength bits 16-20
+ DataBuffer(3 - StackLength +2) (unsigned short) stack data
+ StackLength represents the number of words following DataBuffer(1) word, thus the total number
+ of words is StackLength+2
+ Structure upon return:
+ DataBuffer(0 - (ReturnValue/2-1)) - (unsigned short)array of returned data when ReturnValue>0
+*/
+
+int xxusb_longstack_execute(usb_dev_handle *hDev, void *DataBuffer, int lDataLen, int timeout)
+{
+ int ret;
+ char *cbuf;
+ unsigned short *usbuf;
+ int bufsize;
+
+ cbuf = (char *)DataBuffer;
+ usbuf = (unsigned short *)DataBuffer;
+ cbuf[0]=12;
+ cbuf[1]=0;
+ bufsize = 2*(usbuf[1]+0x10000*usbuf[2])+4;
+ ret=usb_bulk_write(hDev, XXUSB_ENDPOINT_OUT, cbuf, bufsize, timeout);
+ if (ret>0)
+ ret=usb_bulk_read(hDev, XXUSB_ENDPOINT_IN, cbuf, lDataLen, timeout);
+
+ return ret;
+}
+
+/*
+******** xxusb_bulk_read ************************
+
+ Reads the content of the usbfifo whenever "FIFO full" flag is set,
+ otherwise times out.
+
+ Paramters:
+ hdev: USB device handle returned from an open function
+ DataBuffer: pointer to an array to store data that is read from the VME bus;
+ the array may be declared as byte, unsigned short, or unsigned long
+ lDatalen: The number of bytes to read from xxusb
+ timeout: The time in ms that should be spent waiting for data.
+
+ Returns:
+ When Successful, the number of bytes read from xxusb.
+ Upon failure, a negative number
+
+ Note:
+ Depending upon the actual need, the function may be used to return the data in a form
+ of an array of bytes, unsigned short integers (16 bits), or unsigned long integers (32 bits).
+ The latter option of passing a pointer to an array of unsigned long integers is meaningful when
+ xxusb data buffering option is used (bit 7=128 of the global register) that requires data
+ 32-bit data alignment.
+
+*/
+int xxusb_bulk_read(usb_dev_handle *hDev, void *DataBuffer, int lDataLen, int timeout)
+{
+int ret;
+char *cbuf;
+cbuf = (char *)DataBuffer;
+ ret = usb_bulk_read(hDev, XXUSB_ENDPOINT_IN, cbuf, lDataLen, timeout);
+ return ret;
+}
+
+/*
+******** xxusb_bulk_write ************************
+
+ Writes the content of an array of bytes, unsigned short integers, or unsigned long integers
+ to the USB port fifo; times out when the USB fifo is full (e.g., when xxusb is busy).
+
+ Paramters:
+ hdev: USB device handle returned from an open function
+ DataBuffer: pointer to an array storing the data to be sent;
+ the array may be declared as byte, unsigned short, or unsigned long
+ lDatalen: The number of bytes to to send to xxusb
+ timeout: The time in ms that should be spent waiting for data.
+
+ Returns:
+ When Successful, the number of bytes passed to xxusb.
+ Upon failure, a negative number
+
+ Note:
+ Depending upon the actual need, the function may be used to pass to xxusb the data in a form
+ of an array of bytes, unsigned short integers (16 bits), or unsigned long integers (32 bits).
+*/
+int xxusb_bulk_write(usb_dev_handle *hDev, void *DataBuffer, int lDataLen, int timeout)
+{
+int ret;
+char *cbuf;
+cbuf = (char *)DataBuffer;
+ ret = usb_bulk_write(hDev, XXUSB_ENDPOINT_OUT, cbuf, lDataLen, timeout);
+ return ret;
+}
+
+/*
+******** xxusb_usbfifo_read ************************
+
+ Reads data stored in the xxusb fifo and packs them in an array of long integers.
+
+ Paramters:
+ hdev: USB device handle returned from an open function
+ DataBuffer: pointer to an array of long to store data that is read
+ the data occupy only the least significant 16 bits of the 32-bit data words
+ lDatalen: The number of bytes to read from the xxusb
+ timeout: The time in ms that should be spent waiting for data.
+
+ Returns:
+ When Successful, the number of bytes read from xxusb.
+ Upon failure, a negative number
+
+ Note:
+ The function is not economical as it wastes half of the space required for storing
+ the data received. Also, it is relatively slow, as it performs extensive data repacking.
+ It is recommended to use xxusb_bulk_read with a pointer to an array of unsigned short
+ integers.
+*/
+int xxusb_usbfifo_read(usb_dev_handle *hDev, int *DataBuffer, int lDataLen, int timeout)
+{
+int ret;
+char *cbuf;
+unsigned short *usbuf;
+int i;
+
+cbuf = (char *)DataBuffer;
+usbuf = (unsigned short *)DataBuffer;
+
+ ret = usb_bulk_read(hDev, XXUSB_ENDPOINT_IN, cbuf, lDataLen, timeout);
+ if (ret > 0)
+ for (i=ret/2-1; i >= 0; i=i-1)
+ {
+ usbuf[i*2]=usbuf[i];
+ usbuf[i*2+1]=0;
+ }
+ return ret;
+}
+
+
+//******************************************************//
+//******************* GENERAL XX_USB *******************//
+//******************************************************//
+// The following are functions used for both VM_USB & CC_USB
+
+
+/*
+******** xxusb_register_write ************************
+
+ Writes Data to the xxusb register selected by RedAddr. For
+ acceptable values for RegData and RegAddr see the manual
+ the module you are using.
+
+ Parameters:
+ hdev: usb device handle returned from open device
+ RegAddr: The internal address if the xxusb
+ RegData: The Data to be written to the register
+
+ Returns:
+ Number of bytes sent to xxusb if successful
+ 0 if the register is write only
+ Negative numbers if the call fails
+*/
+short xxusb_register_write(usb_dev_handle *hDev, short RegAddr, long RegData)
+{
+ long RegD;
+ char buf[8]={5,0,0,0,0,0,0,0};
+ int ret;
+ int lDataLen;
+ int timeout;
+ if ((RegAddr==0) || (RegAddr==12) || (RegAddr==15))
+ return 0;
+ buf[2]=(char)(RegAddr & 15);
+ buf[4]=(char)(RegData & 255);
+
+ RegD = RegData >> 8;
+ buf[5]=(char)(RegD & 255);
+ RegD = RegD >>8;
+ if (RegAddr==8)
+ {
+ buf[6]=(char)(RegD & 255);
+ lDataLen=8;
+ }
+ else
+ lDataLen=6;
+ timeout=10;
+ ret=xxusb_bulk_write(hDev, buf, lDataLen, timeout);
+ return ret;
+}
+
+/*
+******** xxusb_stack_write ************************
+
+ Writes a stack of VME/CAMAC calls to the VM_USB/CC_USB
+ to be executed upon trigger.
+
+ Parameters:
+ hdev: usb device handle returned from an open function
+ StackAddr: internal register to which the stack should be written
+ lpStackData: Pointer to an array holding the stack
+
+ Returns:
+ The number of Bytes written to the xxusb when successful
+ A negative number upon failure
+*/
+short xxusb_stack_write(usb_dev_handle *hDev, short StackAddr, long *intbuf)
+{
+ int timeout;
+ short ret;
+ short lDataLen;
+ char buf[2000];
+ short i;
+ int bufsize;
+
+ buf[0]=(char)((StackAddr & 51) + 4);
+ buf[1]=0;
+ lDataLen=(short)(intbuf[0] & 0xFFF);
+ buf[2]=(char)(lDataLen & 255);
+ lDataLen = lDataLen >> 8;
+ buf[3] = (char)(lDataLen & 255);
+ bufsize=intbuf[0]*2+4;
+ if (intbuf[0]==0)
+ return 0;
+ for (i=1; i <= intbuf[0]; i++)
+ {
+ buf[2+2*i] = (char)(intbuf[i] & 255);
+ buf[3+2*i] = (char)((intbuf[i] >>8) & 255);
+ }
+ timeout=50;
+ ret=usb_bulk_write(hDev, XXUSB_ENDPOINT_OUT, buf, bufsize, timeout);
+ return ret;
+}
+
+/*
+******** xxusb_stack_execute **********************
+
+ Writes, executes and returns the value of a DAQ stack.
+
+ Parameters:
+ hdev: USB device handle returned from an open function
+ intbuf: Pointer to an array holding the values stack. Upon return
+ Pointer value is the Data returned from the stack.
+
+ Returns:
+ When successful, the number of Bytes read from xxusb
+ Upon Failure, a negative number.
+*/
+short xxusb_stack_execute(usb_dev_handle *hDev, long *intbuf)
+{
+ int timeout;
+ short ret;
+ short lDataLen;
+ char buf[26700];
+ short i;
+ int bufsize;
+ int ii = 0;
+
+ buf[0]=12;
+ buf[1]=0;
+ lDataLen=(short)(intbuf[0] & 0xFFF);
+ buf[2]=(char)(lDataLen & 255);
+ lDataLen = lDataLen >> 8;
+ buf[3] = (char)(lDataLen & 15);
+ bufsize=intbuf[0]*2+4;
+ if (intbuf[0]==0)
+ return 0;
+ for (i=1; i <= intbuf[0]; i++)
+ {
+ buf[2+2*i] = (char)(intbuf[i] & 255);
+ buf[3+2*i] = (char)((intbuf[i] >>8) & 255);
+ }
+ timeout=2000;
+ ret=usb_bulk_write(hDev, XXUSB_ENDPOINT_OUT, buf, bufsize, timeout);
+ if (ret>0)
+ {
+ lDataLen=26700;
+ timeout=6000;
+ ret=usb_bulk_read(hDev, XXUSB_ENDPOINT_IN, buf, lDataLen, timeout);
+ if (ret>0)
+ for (i=0; i < ret; i=i+2)
+ intbuf[ii++]=(UCHAR)(buf[i]) +(UCHAR)( buf[i+1])*256;
+ }
+ return ret;
+}
+
+/*
+******** xxusb_stack_read ************************
+
+ Reads the current DAQ stack stored by xxusb
+
+ Parameters:
+ hdev: USB device handle returned by an open function
+ StackAddr: Indicates which stack to read, primary or secondary
+ intbuf: Pointer to a array where the stack can be stored
+
+ Returns:
+ Number of bytes read from xxusb when successful
+ Upon failure, a negative number
+*/
+short xxusb_stack_read(usb_dev_handle *hDev, short StackAddr, long *intbuf)
+{
+ int timeout;
+ short ret;
+ short lDataLen;
+ short bufsize;
+ char buf[1600];
+ int i;
+
+ buf[0]=(char)(StackAddr & 51);
+ buf[1]=0;
+ lDataLen = 2;
+ timeout=100;
+ ret=usb_bulk_write(hDev, XXUSB_ENDPOINT_OUT, buf, lDataLen, timeout);
+ if (ret < 0)
+ return ret;
+ else
+ bufsize=1600;
+ int ii=0;
+ {
+ ret=usb_bulk_read(hDev, XXUSB_ENDPOINT_IN, buf, bufsize, timeout);
+ if (ret>0)
+ for (i=0; i < ret; i=i+2)
+ intbuf[ii++]=(UCHAR)(buf[i]) + (UCHAR)(buf[i+1])*256;
+ return ret;
+
+ }
+}
+
+/*
+******** xxusb_register_read ************************
+
+ Reads the current contents of an internal xxusb register
+
+ Parameters:
+ hdev: USB device handle returned from an open function
+ RegAddr: The internal address of the register from which to read
+ RegData: Pointer to a long to hold the data.
+
+ Returns:
+ When Successful, the number of bytes read from xxusb.
+ Upon failure, a negative number
+*/
+short xxusb_register_read(usb_dev_handle *hDev, short RegAddr, long *RegData)
+{
+//long RegD;
+int timeout;
+ char buf[4]={1,0,0,0};
+ int ret;
+ int lDataLen;
+
+ buf[2]=(char)(RegAddr & 15);
+ timeout=10;
+ lDataLen=4;
+ ret=xxusb_bulk_write(hDev, buf, lDataLen, timeout);
+ if (ret < 0)
+ return (short)ret;
+ else
+ {
+ lDataLen=8;
+ timeout=100;
+ ret=xxusb_bulk_read(hDev, buf, lDataLen, timeout);
+ if (ret<0)
+ return (short)ret;
+ else
+ {
+ *RegData=(UCHAR)(buf[0])+256*(UCHAR)(buf[1]);
+ if (ret==4)
+ *RegData=*RegData+0x10000*(UCHAR)(buf[2]);
+ return (short)ret;
+ }
+ }
+}
+
+/*
+******** xxusb_reset_toggle ************************
+
+ Toggles the reset state of the FPGA while the xxusb in programming mode
+
+ Parameters
+ hdev: US B device handle returned from an open function
+
+ Returns:
+ Upon failure, a negative number
+*/
+short xxusb_reset_toggle(usb_dev_handle *hDev)
+{
+ short ret;
+ char buf[2] = {(char)255,(char)255};
+ int lDataLen=2;
+ int timeout=1000;
+ ret = usb_bulk_write(hDev, XXUSB_ENDPOINT_OUT, buf,lDataLen, timeout);
+ return (short)ret;
+}
+
+/*
+******** xxusb_devices_find ************************
+
+ Determines the number and parameters of all xxusb devices attched to
+ the computer.
+
+ Parameters:
+ xxdev: pointer to an array on which the device parameters are stored
+
+ Returns:
+ Upon success, returns the number of devices found
+ Upon Failure returns a negative number
+*/
+short xxusb_devices_find(xxusb_device_type *xxdev)
+{
+ short DevFound = 0;
+ usb_dev_handle *udev;
+ struct usb_bus *bus;
+ struct usb_device *dev;
+struct usb_bus *usb_busses;
+ char string[256];
+ short ret;
+ usb_init();
+ usb_find_busses();
+ usb_busses=usb_get_busses();
+ usb_find_devices();
+ for (bus=usb_busses; bus; bus = bus->next)
+ {
+ for (dev = bus->devices; dev; dev= dev->next)
+ {
+ if (dev->descriptor.idVendor==XXUSB_WIENER_VENDOR_ID)
+ {
+ udev = usb_open(dev);
+ if (udev)
+ {
+ ret = usb_get_string_simple(udev, dev->descriptor.iSerialNumber, string, sizeof(string));
+ if (ret >0 )
+ {
+ xxdev[DevFound].usbdev=dev;
+ strcpy(xxdev[DevFound].SerialString, string);
+ DevFound++;
+ }
+ usb_close(udev);
+ }
+ else return -1;
+ }
+ }
+ }
+ return DevFound;
+}
+
+/*
+******** xxusb_device_close ************************
+
+ Closes an xxusb device
+
+ Parameters:
+ hdev: USB device handle returned from an open function
+
+ Returns: 1
+*/
+short xxusb_device_close(usb_dev_handle *hDev)
+{
+ short ret;
+ ret=usb_release_interface(hDev,0);
+ usb_close(hDev);
+ return 1;
+}
+
+/*
+******** xxusb_device_open ************************
+
+ Opens an xxusb device found by xxusb_device_find
+
+ Parameters:
+ dev: a usb device
+
+ Returns:
+ A USB device handle
+*/
+usb_dev_handle* xxusb_device_open(struct usb_device *dev)
+{
+ short ret;
+ long val;
+ int count =0;
+ usb_dev_handle *udev;
+ udev = usb_open(dev);
+ ret = usb_set_configuration(udev,1);
+ ret = usb_claim_interface(udev,0);
+// RESET USB (added 10/16/06 Andreas Ruben)
+ ret=xxusb_register_write(udev, 10, 0x04);
+// Loop to find known state (added 12/28/07 TH / AR)
+ ret =-1;
+ while ((ret <0) && (count <10))
+ {
+ xxusb_register_read(udev, 0, &val);
+ count++;
+ }
+
+ return udev;
+}
+
+/*
+******** xxusb_flash_program ************************
+
+ --Untested and therefore uncommented--
+*/
+short xxusb_flash_program(usb_dev_handle *hDev, char *config, short nsect)
+{
+ int i=0;
+ int k=0;
+ short ret=0;
+ time_t t1,t2;
+
+ char *pconfig;
+ char *pbuf;
+ pconfig=config;
+ char buf[518] ={(char)0xAA,(char)0xAA,(char)0x55,(char)0x55,(char)0xA0,(char)0xA0};
+ while (*pconfig++ != -1);
+ for (i=0; i<nsect; i++)
+ {
+ pbuf=buf+6;
+ for (k=0; k<256; k++)
+ {
+ *(pbuf++)=*(pconfig);
+ *(pbuf++)=*(pconfig++);
+ }
+ ret = usb_bulk_write(hDev, XXUSB_ENDPOINT_OUT, buf, 518, 2000);
+ if (ret<0)
+ return ret;
+ t1=clock()+(time_t)(0.03*CLOCKS_PER_SEC);
+ while (t1>clock());
+ t2=clock();
+ }
+ return ret;
+}
+
+/*
+******** xxusb_flashblock_program ************************
+
+ --Untested and therefore uncommented--
+*/
+short xxusb_flashblock_program(usb_dev_handle *hDev, UCHAR *config)
+{
+ int k=0;
+ short ret=0;
+
+ UCHAR *pconfig;
+ char *pbuf;
+ pconfig=config;
+ char buf[518] ={(char)0xAA,(char)0xAA,(char)0x55,(char)0x55,(char)0xA0,(char)0xA0};
+ pbuf=buf+6;
+ for (k=0; k<256; k++)
+ {
+ *(pbuf++)=(UCHAR)(*(pconfig));
+ *(pbuf++)=(UCHAR)(*(pconfig++));
+ }
+ ret = usb_bulk_write(hDev, XXUSB_ENDPOINT_OUT, buf, 518, 2000);
+ return ret;
+}
+
+/*
+******** xxusb_serial_open ************************
+
+ Opens a xxusb device whose serial number is given
+
+ Parameters:
+ SerialString: a char string that gives the serial number of
+ the device you wish to open. It takes the form:
+ VM0009 - for a vm_usb with serial number 9 or
+ CC0009 - for a cc_usb with serial number 9
+
+ Returns:
+ A USB device handle
+*/
+usb_dev_handle* xxusb_serial_open(char *SerialString)
+{
+ short DevFound = 0;
+ usb_dev_handle *udev = NULL;
+ struct usb_bus *bus;
+ struct usb_device *dev;
+struct usb_bus *usb_busses;
+char string[7];
+ short ret;
+// usb_set_debug(4);
+ usb_init();
+ usb_find_busses();
+ usb_busses=usb_get_busses();
+ usb_find_devices();
+ for (bus=usb_busses; bus; bus = bus->next)
+ {
+ for (dev = bus->devices; dev; dev= dev->next)
+ {
+ if (dev->descriptor.idVendor==XXUSB_WIENER_VENDOR_ID)
+ {
+ udev = xxusb_device_open(dev);
+ if (udev)
+ {
+ ret = usb_get_string_simple(udev, dev->descriptor.iSerialNumber, string, sizeof(string));
+ if (ret >0 )
+ {
+ if (strcmp(string,SerialString)==0)
+ return udev;
+ }
+ usb_close(udev);
+ }
+ }
+ }
+ }
+ udev = NULL;
+ return udev;
+}
+
+
+//******************************************************//
+//****************** EZ_VME Functions ******************//
+//******************************************************//
+// The following are functions used to perform simple
+// VME Functions with the VM_USB
+
+/*
+******** VME_write_32 ************************
+
+ Writes a 32 bit data word to the VME bus
+
+ Parameters:
+ hdev: USB devcie handle returned from an open function
+ Address_Modifier: VME address modifier for the VME call
+ VME_Address: Address to write the data to
+ Data: 32 bit data word to be written to VME_Address
+
+ Returns:
+ Number of bytes read from xxusb when successful
+ Upon failure, a negative number
+*/
+short VME_write_32(usb_dev_handle *hdev, short Address_Modifier, long VME_Address, long Data)
+{
+ long intbuf[1000];
+ short ret;
+ intbuf[0]=7;
+ intbuf[1]=0;
+ intbuf[2]=Address_Modifier;
+ intbuf[3]=0;
+ intbuf[4]=(VME_Address & 0xffff);
+ intbuf[5]=((VME_Address >>16) & 0xffff);
+ intbuf[6]=(Data & 0xffff);
+ intbuf[7]=((Data >> 16) & 0xffff);
+ ret = xxusb_stack_execute(hdev, intbuf);
+ return ret;
+}
+
+/*
+******** VME_read_32 ************************
+
+
+ Reads a 32 bit data word from a VME address
+
+ Parameters:
+ hdev: USB devcie handle returned from an open function
+ Address_Modifier: VME address modifier for the VME call
+ VME_Address: Address to read the data from
+ Data: 32 bit data word read from VME_Address
+
+ Returns:
+ Number of bytes read from xxusb when successful
+ Upon failure, a negative number
+*/
+short VME_read_32(usb_dev_handle *hdev, short Address_Modifier, long VME_Address, long *Data)
+{
+ long intbuf[1000];
+ short ret;
+ intbuf[0]=5;
+ intbuf[1]=0;
+ intbuf[2]=Address_Modifier +0x100;
+ intbuf[3]=0;
+ intbuf[4]=(VME_Address & 0xffff);
+ intbuf[5]=((VME_Address >>16) & 0xffff);
+ ret = xxusb_stack_execute(hdev, intbuf);
+ *Data=intbuf[0] + (intbuf[1] * 0x10000);
+ return ret;
+}
+
+/*
+******** VME_write_16 ************************
+
+ Writes a 16 bit data word to the VME bus
+
+ Parameters:
+ hdev: USB devcie handle returned from an open function
+ Address_Modifier: VME address modifier for the VME call
+ VME_Address: Address to write the data to
+ Data: word to be written to VME_Address
+
+ Returns:
+ Number of bytes read from xxusb when successful
+ Upon failure, a negative number
+*/
+short VME_write_16(usb_dev_handle *hdev, short Address_Modifier, long VME_Address, long Data)
+{
+ long intbuf[1000];
+ short ret;
+ intbuf[0]=7;
+ intbuf[1]=0;
+ intbuf[2]=Address_Modifier;
+ intbuf[3]=0;
+ intbuf[4]=(VME_Address & 0xffff)+ 0x01;
+ intbuf[5]=((VME_Address >>16) & 0xffff);
+ intbuf[6]=(Data & 0xffff);
+ intbuf[7]=0;
+ ret = xxusb_stack_execute(hdev, intbuf);
+ return ret;
+}
+
+/*
+******** VME_read_16 ************************
+
+ Reads a 16 bit data word from a VME address
+
+ Parameters:
+ hdev: USB devcie handle returned from an open function
+ Address_Modifier: VME address modifier for the VME call
+ VME_Address: Address to read the data from
+ Data: word read from VME_Address
+
+ Returns:
+ Number of bytes read from xxusb when successful
+ Upon failure, a negative number
+*/
+short VME_read_16(usb_dev_handle *hdev,short Address_Modifier, long VME_Address, long *Data)
+{
+ long intbuf[1000];
+ short ret;
+ intbuf[0]=5;
+ intbuf[1]=0;
+ intbuf[2]=Address_Modifier +0x100;
+ intbuf[3]=0;
+ intbuf[4]=(VME_Address & 0xffff)+ 0x01;
+ intbuf[5]=((VME_Address >>16) & 0xffff);
+ ret = xxusb_stack_execute(hdev, intbuf);
+ *Data=intbuf[0];
+ return ret;
+}
+
+/*
+******** VME_BLT_read_32 ************************
+
+ Performs block transfer of 32 bit words from a VME address
+
+ Parameters:
+ hdev: USB devcie handle returned from an open function
+ Address_Modifier: VME address modifier for the VME call
+ count: number of data words to read
+ VME_Address: Address to read the data from
+ Data: pointer to an array to hold the data words
+
+ Returns:
+ Number of bytes read from xxusb when successful
+ Upon failure, a negative number
+*/
+short VME_BLT_read_32(usb_dev_handle *hdev, short Adress_Modifier, int count, long VME_Address, long Data[])
+{
+ long intbuf[1000];
+ short ret;
+ int i=0;
+ if (count > 255) return -1;
+ intbuf[0]=5;
+ intbuf[1]=0;
+ intbuf[2]=Adress_Modifier +0x100;
+ intbuf[3]=(count << 8);
+ intbuf[4]=(VME_Address & 0xffff);
+ intbuf[5]=((VME_Address >>16) & 0xffff);
+ ret = xxusb_stack_execute(hdev, intbuf);
+ int j=0;
+ for (i=0;i<(2*count);i=i+2)
+ {
+ Data[j]=intbuf[i] + (intbuf[i+1] * 0x10000);
+ j++;
+ }
+ return ret;
+}
+
+//******************************************************//
+//****************** VM_USB Registers ******************//
+//******************************************************//
+// The following are functions used to set the registers
+// in the VM_USB
+
+/*
+******** VME_register_write ************************
+
+ Writes to the vmusb registers that are accessible through
+ VME style calls
+
+ Parameters:
+ hdev: USB devcie handle returned from an open function
+ VME_Address: The VME Address of the internal register
+ Data: Data to be written to VME_Address
+
+ Returns:
+ Number of bytes read from xxusb when successful
+ Upon failure, a negative number
+*/
+short VME_register_write(usb_dev_handle *hdev, long VME_Address, long Data)
+{
+ long intbuf[1000];
+ short ret;
+
+ intbuf[0]=7;
+ intbuf[1]=0;
+ intbuf[2]=0x1000;
+ intbuf[3]=0;
+ intbuf[4]=(VME_Address & 0xffff);
+ intbuf[5]=((VME_Address >>16) & 0xffff);
+ intbuf[6]=(Data & 0xffff);
+ intbuf[7]=((Data >> 16) & 0xffff);
+ ret = xxusb_stack_execute(hdev, intbuf);
+ return ret;
+}
+
+/*
+******** VME_register_read ************************
+
+ Reads from the vmusb registers that are accessible trough VME style calls
+
+ Parameters:
+ hdev: USB devcie handle returned from an open function
+ VME_Address: The VME Address of the internal register
+ Data: Data read from VME_Address
+
+ Returns:
+ Number of bytes read from xxusb when successful
+ Upon failure, a negative number
+*/
+short VME_register_read(usb_dev_handle *hdev, long VME_Address, long *Data)
+{
+ long intbuf[1000];
+ short ret;
+
+ intbuf[0]=5;
+ intbuf[1]=0;
+ intbuf[2]=0x1100;
+ intbuf[3]=0;
+ intbuf[4]=(VME_Address & 0xffff);
+ intbuf[5]=((VME_Address >>16) & 0xffff);
+ ret = xxusb_stack_execute(hdev, intbuf);
+ *Data=intbuf[0] + (intbuf[1] * 0x10000);
+ return ret;
+}
+
+/*
+******** VME_LED_settings ************************
+
+ Sets the vmusb LED's
+
+ Parameters:
+ hdev: USB devcie handle returned from an open function
+ LED: The number which corresponds to an LED values are:
+ 0 - for Top YELLOW LED
+ 1 - for RED LED
+ 2 - for GREEN LED
+ 3 - for Bottom YELLOW LED
+ code: The LED aource selector code, valid values for each LED
+ are listed in the manual
+ invert: to invert the LED lighting
+ latch: sets LED latch bit
+
+ Returns:
+ Number of bytes read from xxusb when successful
+ Upon failure, a negative number
+*/
+short VME_LED_settings(usb_dev_handle *hdev, int LED, int code, int invert, int latch)
+{
+ short ret;
+// long internal;
+ long Data;
+
+ if( (LED <0) ||(LED > 3) || (code < 0) || (code > 7)) return -1;
+
+ VME_register_read(hdev,0xc,&Data);
+ if(LED == 0)
+ {
+ Data = Data & 0xFFFFFF00;
+ Data = Data | code;
+ if (invert == 1 && latch == 1) Data = Data | 0x18;
+ if (invert == 1 && latch == 0) Data = Data | 0x08;
+ if (invert == 0 && latch == 1) Data = Data | 0x10;
+ }
+ if(LED == 1)
+ {
+ Data = Data & 0xFFFF00FF;
+ Data = Data | (code * 0x0100);
+ if (invert == 1 && latch == 1) Data = Data | 0x1800;
+ if (invert == 1 && latch == 0) Data = Data | 0x0800;
+ if (invert == 0 && latch == 1) Data = Data | 0x1000;
+ }
+ if(LED == 2)
+ {
+ Data = Data & 0xFF00FFFF;
+ Data = Data | (code * 0x10000);
+ if (invert == 1 && latch == 1) Data = Data | 0x180000;
+ if (invert == 1 && latch == 0) Data = Data | 0x080000;
+ if (invert == 0 && latch == 1) Data = Data | 0x100000;
+ }
+ if(LED == 3)
+ {
+ Data = Data & 0x00FFFFFF;
+ Data = Data | (code * 0x10000);
+ if (invert == 1 && latch == 1) Data = Data | 0x18000000;
+ if (invert == 1 && latch == 0) Data = Data | 0x08000000;
+ if (invert == 0 && latch == 1) Data = Data | 0x10000000;
+ }
+ ret = VME_register_write(hdev, 0xc, Data);
+ return ret;
+}
+
+/*
+******** VME_DGG ************************
+
+ Sets the parameters for Gate & Delay channel A of vmusb
+
+ Parameters:
+ hdev: USB devcie handle returned from an open function
+ channel: Which DGG channel to use Valid Values are:
+ 0 - For DGG A
+ 1 - For DGG B
+ trigger: Determines what triggers 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: Determines which NIM output to use for the channel, Vaild values are:
+ 0 - for NIM O1
+ 1 - for NIM O2
+ delay: 32 bit word consisting of
+ lower 16 bits: Delay_fine in steps of 12.5ns between trigger and start of gate
+ upper 16 bits: Delay_coarse in steps of 81.7us between trigger and start of gate
+ gate: the time the gate should stay open in steps of 12.5ns
+ invert: is 1 if you wish to invert the DGG channel output
+ latch: is 1 if you wish to run the DGG channel latched
+
+ Returns:
+ Returns 1 when successful
+ Upon failure, a negative number
+*/
+short VME_DGG(usb_dev_handle *hdev, unsigned short channel, unsigned short trigger, unsigned short output,
+ long delay, unsigned short gate, unsigned short invert, unsigned short latch)
+{
+ long Data, DGData, Delay_ext;
+ long internal;
+ short ret;
+
+
+ ret = VME_register_read(hdev, 0x10, &Data);
+ // check and correct values
+ if(ret<=0) return -1;
+
+ if(channel >1) channel =1;
+ if(invert >1) invert =1;
+ if(latch >1) latch =1;
+ if(output >1) output =1;
+ if(trigger >6) trigger =0;
+
+ // define Delay and Gate data
+ DGData = gate * 0x10000;
+ DGData += (unsigned short) delay;
+
+ // Set channel, output, invert, latch
+ if (output == 0)
+ {
+ Data = Data & 0xFFFFFF00;
+ Data += 0x04 + channel +0x08*invert + 0x10*latch;
+ }
+ if (output == 1)
+ {
+ Data = Data & 0xFFFF00FF;
+ Data += (0x04 + channel +0x08*invert + 0x10*latch)*0x100;
+ }
+
+ // Set trigger, delay, gate
+
+ if(channel ==0) // CHANNEL DGG_A
+ {
+ internal = (trigger * 0x1000000) ;
+ Data= Data & 0xF0FFFFFF;
+ Data += internal;
+ ret = VME_register_write(hdev,0x10,Data);
+ if(ret<=0) return -1;
+ ret=VME_register_write(hdev,0x14,DGData);
+ if(ret<=0) return -1;
+ // Set coarse delay in DGG_Extended register
+ ret = VME_register_read(hdev,0x38,&Data);
+ Delay_ext= (Data & 0xffff0000);
+ Delay_ext+= ((delay/0x10000) & 0xffff);
+ ret = VME_register_write(hdev,0x38,Delay_ext);
+ }
+ if( channel ==1) // CHANNEL DGG_B
+ {
+ internal = (trigger * 0x10000000) ;
+ Data= Data & 0x0FFFFFFF;
+ Data += internal;
+ ret = VME_register_write(hdev,0x10,Data);
+ if(ret<=0) return -1;
+ ret=VME_register_write(hdev,0x18,DGData);
+ if(ret<=0) return -1;
+ // Set coarse delay in DGG_Extended register
+ ret = VME_register_read(hdev,0x38,&Data);
+ Delay_ext= (Data & 0x0000ffff);
+ Delay_ext+= (delay & 0xffff0000);
+ ret = VME_register_write(hdev,0x38,Delay_ext);
+ }
+ return 1;
+
+}
+
+/*
+******** VME_Output_settings ************************
+
+ Sets the vmusb NIM output register
+
+ Parameters:
+ hdev: USB devcie handle returned from an open function
+ Channel: The number which corresponds to an output:
+ 1 - for Output 1
+ 2 - for Output 2
+ code: The Output selector code, valid values
+ are listed in the manual
+ invert: to invert the output
+ latch: sets latch bit
+
+ Returns:
+ Number of bytes read from xxusb when successful
+ Upon failure, a negative number
+*/
+short VME_Output_settings(usb_dev_handle *hdev, int Channel, int code, int invert, int latch)
+{
+
+ short ret;
+// long internal;
+ long Data;
+
+ if( (Channel <1) ||(Channel > 2) || (code < 0) || (code > 7)) return -1;
+ VME_register_read(hdev,0x10,&Data);
+ if(Channel == 1)
+ {
+ Data = Data & 0xFFFF00;
+ Data = Data | code;
+ if (invert == 1 && latch == 1) Data = Data | 0x18;
+ if (invert == 1 && latch == 0) Data = Data | 0x08;
+ if (invert == 0 && latch == 1) Data = Data | 0x10;
+ }
+ if(Channel == 2)
+ {
+ Data = Data & 0xFF00FF;
+ Data = Data | (code * 0x0100);
+ if (invert == 1 && latch == 1) Data = Data | 0x1800;
+ if (invert == 1 && latch == 0) Data = Data | 0x0800;
+ if (invert == 0 && latch == 1) Data = Data | 0x1000;
+ }
+ ret = VME_register_write(hdev, 0x10, Data);
+ return ret;
+}
+
+
+//******************************************************//
+//****************** CC_USB Registers ******************//
+//******************************************************//
+// The following are functions used to set the registers
+// in the CAMAC_USB
+
+/*
+******** CAMAC_register_write *****************
+
+ Performs a CAMAC write to CC_USB register
+
+ Parameters:
+ hdev: USB device handle returned from an open function
+ A: CAMAC Subaddress
+ F: CAMAC Function
+ Data: data to be written
+
+ Returns:
+ Number of bytes written to xxusb when successful
+ Upon failure, a negative number
+*/
+short CAMAC_register_write(usb_dev_handle *hdev, int A, long Data)
+{
+ int F = 16;
+ int N = 25;
+ long intbuf[4];
+ int ret;
+
+ intbuf[0]=1;
+ intbuf[1]=(long)(F+A*32+N*512 + 0x4000);
+ intbuf[0]=3;
+ intbuf[2]=(Data & 0xffff);
+ intbuf[3]=((Data >>16) & 0xffff);
+ ret = xxusb_stack_execute(hdev, intbuf);
+
+ return ret;
+}
+
+/*
+******** CAMAC_register_read ************************
+
+ Performs a CAMAC read from CC_USB register
+
+ Parameters:
+ hdev: USB device handle returned from an open function
+ N: CAMAC Station Number
+ A: CAMAC Subaddress
+ F: CAMAC Function
+ Q: The Q response from the CAMAC dataway
+ X: The comment accepted response from CAMAC dataway
+
+ Returns:
+ Number of bytes read from xxusb when successful
+ Upon failure, a negative number
+*/
+short CAMAC_register_read(usb_dev_handle *hdev, int A, long *Data)
+{
+ int F = 0;
+ int N = 25;
+ long intbuf[4];
+ int ret;
+
+ intbuf[0]=1;
+ intbuf[1]=(long)(F+A*32+N*512 + 0x4000);
+ ret = xxusb_stack_execute(hdev, intbuf);
+ *Data=intbuf[0] + (intbuf[1] * 0x10000);
+
+ return ret;
+}
+
+/*
+******** CAMAC_DGG ************************
+
+ Sets the parameters for Gate & Delay channel A of CC_USB
+
+ Parameters:
+ hdev: USB devcie handle returned from an open function
+ channel: Which DGG channel to use Valid Values are:
+ 0 - For DGG A
+ 1 - For DGG B
+ trigger: Determines what triggers the start of the DGG Valid values are:
+ 0 - Channel disabled
+ 1 - NIM input 1
+ 2 - NIM input 2
+ 3 - NIM input 2
+ 4 - Event Trigger
+ 5 - End of Event
+ 6 - USB Trigger
+ 7 - Pulser
+ output: Determines which NIM output to use for the channel, Vaild values are:
+ 1 - for NIM O1
+ 2 - for NIM O2
+ 3 - for NIM O3
+ delay: Delay in steps of 12.5ns between trigger and start of gate
+ gate: the time the gate should stay open in steps of 12.5ns
+ invert: is 1 if you wish to invert the DGG channel output
+ latch: is 1 if you wish to run the DGG channel latched
+
+ Returns:
+ Returns 1 when successful
+ Upon failure, a negative number
+*/
+short CAMAC_DGG(usb_dev_handle *hdev, short channel, short trigger, short output,
+ int delay, int gate, short invert, short latch)
+
+
+
+{
+// short channel_ID;
+ long Data;
+ long internal;
+ short ret;
+ long Delay_ext;
+
+ ret = CAMAC_register_read(hdev,5,&Data);
+ //Set trigger
+ if((output < 1 ) || (output >3) || (channel < 0 ) || (channel > 1))
+ return -1;
+ if(output ==1)
+ {
+ if(channel ==0)
+ {
+ internal = 0x03;
+ } else {
+ internal = 0x04;
+ }
+ }
+ if(output ==2)
+ {
+ if(channel ==0)
+ {
+ internal = 0x04;
+ } else {
+ internal = 0x05;
+ }
+ }
+ if(output ==3)
+ {
+ if(channel ==0)
+ {
+ internal = 0x05;
+ } else {
+ internal = 0x06;
+ }
+ }
+
+
+// Set invert bit
+ if(invert ==1)
+ internal = internal | 0x10;
+ else
+ internal = internal & 0x0F;
+ // Set Latch Bit
+ if(latch==1)
+ internal = internal | 0x20;
+ else
+ internal = internal & 0x1F;
+// Add new data to old
+ if(output == 1)
+ {
+ Data = Data & 0xFFFF00;
+ Data = Data | internal;
+ }
+ if(output == 2)
+ {
+ Data = Data & 0xFF00FF;
+ Data = Data |(internal * 0x100);
+ }
+ if(output == 3)
+ {
+ Data = Data & 0x00FFFF;
+ Data = Data | (internal * 0x10000) ;
+ }
+ CAMAC_register_write(hdev, 5, Data);
+ ret = CAMAC_register_read(hdev,6,&Data);
+//Set Trigger
+ if(trigger <0 || trigger > 7)
+ return -1;
+ if(channel ==0)
+ {
+ Data = Data & 0xFF00FFFF;
+ internal = trigger * 0x10000;
+ Data = Data | internal;
+ } else {
+ Data = Data & 0x00FFFFFF;
+ internal = trigger * 0x1000000;
+ Data = Data | internal;
+ }
+ ret = CAMAC_register_write(hdev, 6, Data);
+ if(channel == 0)
+ {
+// Write Delay and Gate info
+ ret = CAMAC_register_read(hdev,13,&Data);
+ Delay_ext= (Data & 0xffff0000);
+ Delay_ext+= ((delay/0x10000) & 0xffff);
+ internal = gate * 0x10000;
+ Data = internal + (delay & 0xffff);
+ ret=CAMAC_register_write(hdev,7,Data);
+// Set coarse delay in DGG_Extended register
+ ret=CAMAC_register_write(hdev,13,Delay_ext);
+ }
+ else
+ {
+ ret=CAMAC_register_write(hdev,8,Data);
+ ret = CAMAC_register_read(hdev,13,&Data);
+ Delay_ext= (Data & 0x0000ffff);
+ Delay_ext+= (delay & 0xffff0000);
+ internal = gate * 0x10000;
+ Data = internal + (delay & 0xffff);
+// Set coarse delay in DGG_Extended register
+ ret=CAMAC_register_write(hdev,13,Delay_ext);
+ }
+ return 1;
+}
+
+/*
+******** CAMAC_LED_settings ************************
+
+ Writes a data word to the vmusb LED register
+
+ Parameters:
+ hdev: USB devcie handle returned from an open function
+ LED: The number which corresponds to an LED values are:
+ 1 - for RED LED
+ 2 - for GREEN LED
+ 3 - for Yellow LED
+ code: The LED aource selector code, valid values for each LED
+ are listed in the manual
+ invert: to invert the LED lighting
+ latch: sets LED latch bit
+
+ Returns:
+ Number of bytes read from xxusb when successful
+ Upon failure, a negative number
+*/
+short CAMAC_LED_settings(usb_dev_handle *hdev, int LED, int code, int invert, int latch)
+{
+
+ short ret;
+// long internal;
+ long Data;
+
+ if( (LED <1) ||(LED > 3) || (code < 0) || (code > 7))
+ return -1;
+
+ CAMAC_register_read(hdev,4,&Data);
+
+ if(LED == 1)
+ {
+ Data = Data & 0xFFFF00;
+ Data = Data | code;
+ if (invert == 1 && latch == 1)
+ Data = Data | 0x30;
+ if (invert == 1 && latch == 0)
+ Data = Data | 0x10;
+ if (invert == 0 && latch == 1)
+ Data = Data | 0x20;
+ }
+ if(LED == 2)
+ {
+ Data = Data & 0xFF00FF;
+ Data = Data | (code * 0x0100);
+ if (invert == 1 && latch == 1)
+ Data = Data | 0x3000;
+ if (invert == 1 && latch == 0)
+ Data = Data | 0x1000;
+ if (invert == 0 && latch == 1)
+ Data = Data | 0x2000;
+ }
+ if(LED == 3)
+ {
+ Data = Data & 0x00FFFF;
+ Data = Data | (code * 0x10000);
+ if (invert == 1 && latch == 1)
+ Data = Data | 0x300000;
+ if (invert == 1 && latch == 0)
+ Data = Data | 0x100000;
+ if (invert == 0 && latch == 1)
+ Data = Data | 0x200000;
+ }
+ ret = CAMAC_register_write(hdev, 4, Data);
+ return ret;
+}
+
+/*
+******** CAMAC_Output_settings ************************
+
+ Writes a data word to the vmusb LED register
+
+ Parameters:
+ hdev: USB devcie handle returned from an open function
+ Channel: The number which corresponds to an output:
+ 1 - for Output 1
+ 2 - for Output 2
+ 3 - for Output 3
+ code: The Output selector code, valid values
+ are listed in the manual
+ invert: to invert the output
+ latch: sets latch bit
+
+ Returns:
+ Number of bytes read from xxusb when successful
+ Upon failure, a negative number
+*/
+short CAMAC_Output_settings(usb_dev_handle *hdev, int Channel, int code, int invert, int latch)
+{
+ short ret;
+// long internal;
+ long Data;
+
+ if( (Channel <1) ||(Channel > 3) || (code < 0) || (code > 7))
+ return -1;
+
+ CAMAC_register_read(hdev,5,&Data);
+
+ if(Channel == 1)
+ {
+ Data = Data & 0xFFFF00;
+ Data = Data | code;
+ if (invert == 1 && latch == 1)
+ Data = Data | 0x30;
+ if (invert == 1 && latch == 0)
+ Data = Data | 0x10;
+ if (invert == 0 && latch == 1)
+ Data = Data | 0x20;
+ }
+ if(Channel == 2)
+ {
+ Data = Data & 0xFF00FF;
+ Data = Data | (code * 0x0100);
+ if (invert == 1 && latch == 1)
+ Data = Data | 0x3000;
+ if (invert == 1 && latch == 0)
+ Data = Data | 0x1000;
+ if (invert == 0 && latch == 1)
+ Data = Data | 0x2000;
+ }
+ if(Channel == 3)
+ {
+ Data = Data & 0x00FFFF;
+ Data = Data | (code * 0x10000);
+ if (invert == 1 && latch == 1)
+ Data = Data | 0x300000;
+ if (invert == 1 && latch == 0)
+ Data = Data | 0x100000;
+ if (invert == 0 && latch == 1)
+ Data = Data | 0x200000;
+ }
+ ret = CAMAC_register_write(hdev, 5, Data);
+ return ret;
+}
+
+/*
+******** CAMAC_write_LAM_mask ************************
+
+ Writes the data word to the LAM mask register
+
+ Parameters:
+ hdev: USB devcie handle returned from an open function
+ Data: LAM mask to write
+
+ Returns:
+ Number of bytes read from xxusb when successful
+ Upon failure, a negative number
+*/
+short CAMAC_write_LAM_mask(usb_dev_handle *hdev, long Data)
+{
+ short ret;
+ ret = CAMAC_register_write(hdev, 9, Data);
+
+ return ret;
+}
+
+/*
+******** CAMAC_read_LAM_mask ************************
+
+ Writes the data word to the LAM mask register
+
+ Parameters:
+ hdev: USB devcie handle returned from an open function
+ Data: LAM mask to write
+
+ Returns:
+ Number of bytes read from xxusb when successful
+ Upon failure, a negative number
+*/
+short CAMAC_read_LAM_mask(usb_dev_handle *hdev, long *Data)
+{
+ long intbuf[4];
+ int ret;
+ int N = 25;
+ int F = 0;
+ int A = 9;
+
+ // CAMAC direct read function
+ intbuf[0]=1;
+ intbuf[1]=(long)(F+A*32+N*512 + 0x4000);
+ ret = xxusb_stack_execute(hdev, intbuf);
+ *Data=intbuf[0] + (intbuf[1] & 255) * 0x10000;
+ return ret;
+}
+
+
+//******************************************************//
+//**************** EZ_CAMAC Functions ******************//
+//******************************************************//
+// The following are functions used to perform simple
+// CAMAC Functions with the CC_USB
+
+
+/*
+******** CAMAC_write ************************
+
+ Performs a CAMAC write using NAF comments
+
+ Parameters:
+ hdev: USB device handle returned from an open function
+ N: CAMAC Station Number
+ A: CAMAC Subaddress
+ F: CAMAC Function (16...23)
+ Q: The Q response from the CAMAC dataway
+ X: The comment accepted response from CAMAC dataway
+
+ Returns:
+ Number of bytes written to xxusb when successful
+ Upon failure, a negative number
+*/
+short CAMAC_write(usb_dev_handle *hdev, int N, int A, int F, long Data, int *Q, int *X)
+{
+ long intbuf[4];
+ int ret;
+// CAMAC direct write function
+ intbuf[0]=1;
+ intbuf[1]=(long)(F+A*32+N*512 + 0x4000);
+ if ((F > 15) && (F < 24))
+ {
+ intbuf[0]=3;
+ intbuf[2]=(Data & 0xffff);
+ intbuf[3]=((Data >>16) & 255);
+ ret = xxusb_stack_execute(hdev, intbuf);
+ *Q = (intbuf[0] & 1);
+ *X = ((intbuf[0] >> 1) & 1);
+ }
+ return ret;
+}
+
+/*
+******** CAMAC_read ************************
+
+ Performs a CAMAC read using NAF comments
+
+ Parameters:
+ hdev: USB device handle returned from an open function
+ N: CAMAC Station Number
+ A: CAMAC Subaddress
+ F: CAMAC Function (F<16 or F>23)
+ Q: The Q response from the CAMAC dataway
+ X: The comment accepted response from CAMAC dataway
+
+ Returns:
+ Number of bytes read from xxusb when successful
+ Upon failure, a negative number
+*/
+short CAMAC_read(usb_dev_handle *hdev, int N, int A, int F, long *Data, int *Q, int *X)
+{
+ long intbuf[4];
+ int ret;
+ // CAMAC direct read function
+ intbuf[0]=1;
+ intbuf[1]=(long)(F+A*32+N*512 + 0x4000);
+ ret = xxusb_stack_execute(hdev, intbuf);
+ if ((F < 16) || (F >23))
+ {
+ *Data=intbuf[0] + (intbuf[1] & 255) * 0x10000; //24-bit word
+ *Q = ((intbuf[1] >> 8) & 1);
+ *X = ((intbuf[1] >> 9) & 1);
+ }
+ return ret;
+}
+
+/*
+******** CAMAC_Z ************************
+
+ Performs a CAMAC init
+
+ Parameters:
+ hdev: USB device handle returned from an open function
+
+ Returns:
+ Number of bytes written to xxusb when successful
+ Upon failure, a negative number
+*/
+short CAMAC_Z(usb_dev_handle *hdev)
+{
+ long intbuf[4];
+ int ret;
+// CAMAC Z = N(28) A(8) F(29)
+ intbuf[0]=1;
+ intbuf[1]=(long)(29+8*32+28*512 + 0x4000);
+ ret = xxusb_stack_execute(hdev, intbuf);
+ return ret;
+}
+
+/*
+******** CAMAC_C ************************
+
+ Performs a CAMAC clear
+
+ Parameters:
+ hdev: USB device handle returned from an open function
+
+ Returns:
+ Number of bytes written to xxusb when successful
+ Upon failure, a negative number
+*/
+short CAMAC_C(usb_dev_handle *hdev)
+{
+ long intbuf[4];
+ int ret;
+ intbuf[0]=1;
+ intbuf[1]=(long)(29+9*32+28*512 + 0x4000);
+ ret = xxusb_stack_execute(hdev, intbuf);
+ return ret;
+}
+
+/*
+******** CAMAC_I ************************
+
+ Set CAMAC inhibit
+
+ Parameters:
+ hdev: USB device handle returned from an open function
+
+ Returns:
+ Number of bytes written to xxusb when successful
+ Upon failure, a negative number
+*/
+short CAMAC_I(usb_dev_handle *hdev, int inhibit)
+{
+ long intbuf[4];
+ int ret;
+ intbuf[0]=1;
+ if (inhibit) intbuf[1]=(long)(24+9*32+29*512 + 0x4000);
+ else intbuf[1]=(long)(26+9*32+29*512 + 0x4000);
+ ret = xxusb_stack_execute(hdev, intbuf);
+ return ret;
+}
+
+
Index: petdemo/libxxusb.dll
===================================================================
Cannot display: file marked as a binary type.
svn:mime-type = application/octet-stream
/petdemo/libxxusb.dll
Property changes:
Added: svn:mime-type
## -0,0 +1 ##
+application/octet-stream
\ No newline at end of property
Index: petdemo/libxxusb.h
===================================================================
--- petdemo/libxxusb.h (nonexistent)
+++ petdemo/libxxusb.h (revision 264)
@@ -0,0 +1,111 @@
+#include <usb.h>
+
+
+#define XXUSB_WIENER_VENDOR_ID 0x16DC /* Wiener, Plein & Baus */
+#define XXUSB_VMUSB_PRODUCT_ID 0x000B /* VM-USB */
+#define XXUSB_CCUSB_PRODUCT_ID 0x0001 /* CC-USB */
+#define XXUSB_ENDPOINT_OUT 2 /* Endpoint 2 Out*/
+#define XXUSB_ENDPOINT_IN 0x86 /* Endpoint 6 In */
+#define XXUSB_FIRMWARE_REGISTER 0
+#define XXUSB_GLOBAL_REGISTER 1
+#define XXUSB_ACTION_REGISTER 10
+#define XXUSB_DELAYS_REGISTER 2
+#define XXUSB_WATCHDOG_REGISTER 3
+#define XXUSB_SELLEDA_REGISTER 6
+#define XXUSB_SELNIM_REGISTER 7
+#define XXUSB_SELLEDB_REGISTER 4
+#define XXUSB_SERIAL_REGISTER 15
+#define XXUSB_LAMMASK_REGISTER 8
+#define XXUSB_LAM_REGISTER 12
+#define XXUSB_READOUT_STACK 2
+#define XXUSB_SCALER_STACK 3
+#define XXUSB_NAF_DIRECT 12
+
+struct XXUSB_STACK
+{
+long Data;
+short Hit;
+short APatt;
+short Num;
+short HitMask;
+};
+
+struct XXUSB_CC_COMMAND_TYPE
+{
+short Crate;
+short F;
+short A;
+short N;
+long Data;
+short NoS2;
+short LongD;
+short HitPatt;
+short QStop;
+short LAMMode;
+short UseHit;
+short Repeat;
+short AddrScan;
+short FastCam;
+short NumMod;
+short AddrPatt;
+long HitMask[4];
+long Num;
+};
+
+struct xxusb_device_typ
+{
+ struct usb_device *usbdev;
+ char SerialString[7];
+};
+
+typedef struct xxusb_device_typ xxusb_device_type;
+typedef unsigned char UCHAR;
+typedef struct usb_bus usb_busx;
+
+
+int xxusb_longstack_execute(usb_dev_handle *hDev, void *DataBuffer, int lDataLen, int timeout);
+int xxusb_bulk_read(usb_dev_handle *hDev, void *DataBuffer, int lDataLen, int timeout);
+int xxusb_bulk_write(usb_dev_handle *hDev, void *DataBuffer, int lDataLen, int timeout);
+int xxusb_usbfifo_read(usb_dev_handle *hDev, int *DataBuffer, int lDataLen, int timeout);
+
+short xxusb_register_read(usb_dev_handle *hDev, short RegAddr, long *RegData);
+short xxusb_stack_read(usb_dev_handle *hDev, short StackAddr, long *StackData);
+short xxusb_stack_write(usb_dev_handle *hDev, short StackAddr, long *StackData);
+short xxusb_stack_execute(usb_dev_handle *hDev, long *StackData);
+short xxusb_register_write(usb_dev_handle *hDev, short RegAddr, long RegData);
+short xxusb_reset_toggle(usb_dev_handle *hDev);
+
+short xxusb_devices_find(xxusb_device_type *xxusbDev);
+short xxusb_device_close(usb_dev_handle *hDev);
+usb_dev_handle* xxusb_device_open(struct usb_device *dev);
+short xxusb_flash_program(usb_dev_handle *hDev, char *config, short nsect);
+short xxusb_flashblock_program(usb_dev_handle *hDev, UCHAR *config);
+usb_dev_handle* xxusb_serial_open(char *SerialString);
+
+short VME_register_write(usb_dev_handle *hdev, long VME_Address, long Data);
+short VME_register_read(usb_dev_handle *hdev, long VME_Address, long *Data);
+short VME_LED_settings(usb_dev_handle *hdev, int LED, int code, int invert, int latch);
+short VME_DGG(usb_dev_handle *hdev, unsigned short channel, unsigned short trigger,unsigned short output, long delay, unsigned short gate, unsigned short invert, unsigned short latch);
+
+short VME_Output_settings(usb_dev_handle *hdev, int Channel, int code, int invert, int latch);
+
+short VME_read_16(usb_dev_handle *hdev,short Address_Modifier, long VME_Address, long *Data);
+short VME_read_32(usb_dev_handle *hdev, short Address_Modifier, long VME_Address, long *Data);
+short VME_BLT_read_32(usb_dev_handle *hdev, short Address_Modifier, int count, long VME_Address, long Data[]);
+short VME_write_16(usb_dev_handle *hdev, short Address_Modifier, long VME_Address, long Data);
+short VME_write_32(usb_dev_handle *hdev, short Address_Modifier, long VME_Address, long Data);
+
+short CAMAC_DGG(usb_dev_handle *hdev, short channel, short trigger, short output, int delay, int gate, short invert, short latch);
+short CAMAC_register_read(usb_dev_handle *hdev, int A, long *Data);
+short CAMAC_register_write(usb_dev_handle *hdev, int A, long Data);
+short CAMAC_LED_settings(usb_dev_handle *hdev, int LED, int code, int invert, int latch);
+short CAMAC_Output_settings(usb_dev_handle *hdev, int Channel, int code, int invert, int latch);
+short CAMAC_read_LAM_mask(usb_dev_handle *hdev, long *Data);
+short CAMAC_write_LAM_mask(usb_dev_handle *hdev, long Data);
+
+short CAMAC_write(usb_dev_handle *hdev, int N, int A, int F, long Data, int *Q, int *X);
+short CAMAC_read(usb_dev_handle *hdev, int N, int A, int F, long *Data, int *Q, int *X);
+short CAMAC_Z(usb_dev_handle *hdev);
+short CAMAC_C(usb_dev_handle *hdev);
+short CAMAC_I(usb_dev_handle *hdev, int inhibit);
+
Index: petdemo/libxxusb.lib
===================================================================
Cannot display: file marked as a binary type.
svn:mime-type = application/octet-stream
/petdemo/libxxusb.lib
Property changes:
Added: svn:mime-type
## -0,0 +1 ##
+application/octet-stream
\ No newline at end of property
Index: petdemo/petdemo.c
===================================================================
--- petdemo/petdemo.c (nonexistent)
+++ petdemo/petdemo.c (revision 264)
@@ -0,0 +1,511 @@
+#include "H2D.h"
+#include "H1D.h"
+#include "inifile.h"
+#include <utility.h>
+#include <ansi_c.h>
+#include <cvirte.h>
+#include <userint.h>
+#include "petdemo_uir.h"
+
+#include "daq.h"
+#include "PETProjDataMgr.h"
+#include <math.h>
+
+#define uSMC_USB
+#ifdef uSMC_USB
+# include "uSMC.h"
+# define uSMC_SERIAL "0000000000005660"
+const char serials[3][16]= {uSMC_SERIAL};
+#endif /* uSMC_USB */
+
+static int node;
+static int CurPos = 0;
+
+int p1;
+#define MAX_THREADS 10
+static CmtThreadPoolHandle poolHandle = 0;
+int ctrl_c=0;
+int daq_on=0;
+int gadcchannel;
+static int tfID;
+static int controlID;
+static int plothandle[0xFF];
+char strbuf[0xFF];
+
+int gLog=0;
+
+
+
+int printf(const char *format, ...) {
+ va_list aptr;
+ int ret;
+ FILE *flog;
+
+ va_start(aptr, format);
+ ret = vsprintf(strbuf, format, aptr);
+ va_end(aptr);
+ SetCtrlVal(p1,P1_STDIO,strbuf);
+
+ if (gLog) {
+ flog = fopen ("stdio.log", "a");
+ fprintf (flog, "%s", strbuf);
+ fclose (flog);
+ }
+ return(ret);
+}
+
+void CVICALLBACK EndOfThread ( CmtThreadPoolHandle poolhandle,
+ CmtThreadFunctionID functionID, unsigned int event,
+ int value, void *callbackData ) {
+
+ daq_on=0;
+ //SetDimming(0);
+ printf("End of Thread \n");
+ return ;
+
+}
+
+
+
+
+int __stdcall WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
+ LPSTR lpszCmdLine, int nCmdShow) {
+ if (InitCVIRTE (hInstance, 0, 0) == 0)
+ return -1; /* out of memory */
+ if ((p1 = LoadPanel (0, "petdemo_uir.uir", P1)) < 0)
+ return -1;
+ SetStdioPort (CVI_STDIO_WINDOW);
+ SetSleepPolicy(VAL_SLEEP_MORE);
+ CmtNewThreadPool (MAX_THREADS, &poolHandle);
+#ifdef uSMC_USB
+ uSMC_Open();
+ node=uSMC_FindSerial(serials)+1;
+ uSMC_Init(node,1);
+ SetCtrlVal(p1, P1_STAGELED,1);
+
+ uSMC_GetPosition(node,&CurPos);
+ SetCtrlVal(p1,P1_CPOSITION,CurPos);
+
+ SetCtrlVal(p1, P1_STAGELED,0);
+
+#endif /* uSMC_USB */
+
+ DisplayPanel (p1);
+ RunUserInterface ();
+ DiscardPanel (p1);
+ CmtDiscardThreadPool (poolHandle);
+
+#ifdef uSMC_USB
+ uSMC_PowerOff(node);
+ uSMC_Close();
+#endif /* uSMC_USB */
+
+ return 0;
+}
+
+
+
+
+int addheader(FILE *fp, int type, int *data) {
+
+
+ time_t t;
+
+
+
+ switch(type) {
+ case RUNREC_ID: {
+
+
+
+ RUNREC runrec; // start header: appears once at the start of the file
+
+ runrec.id = RUNREC_ID;
+ runrec.length = sizeof(runrec);
+ runrec.fver = 0x10000;
+ time(&t);
+ runrec.time=t;
+ runrec.num_events = data[6];
+ runrec.num_channels = NUM_CHANNELS;
+ runrec.pedestal = PEDESTAL;
+ runrec.xy = SCAN_TYPE; // 0 -> single data scan :: 1 -> XY position scan
+ runrec.nx = data[0];
+ runrec.x0 = data[1];
+ runrec.dx = data[2];
+ runrec.ny = data[3];
+ runrec.y0 = data[4];
+ runrec.dy = data[5];
+
+ printf("Writing header to file\n");
+ printf("RECID = %u\n",runrec.id);
+ printf("Length = %u\n",runrec.length);
+ printf("File version = %u\n",runrec.fver);
+ printf("Time = %u\n",runrec.time);
+ printf("Number of events per step = %u\n",runrec.num_events);
+ printf("Number of channels measured = %u\n",runrec.num_channels);
+ printf("Pedestal = %u\n",runrec.pedestal);
+ printf("Scan type = %u :: 0 -> single data scan :: 1 -> XY position scan\n",runrec.xy);
+ printf("Number of steps in X = %d\n",runrec.nx);
+ printf("Start position X = %d\n",runrec.x0);
+ printf("Step size direction X = %d\n",runrec.dx);
+ printf("Number of steps in Y = %d\n",runrec.ny);
+ printf("Start position Y = %d\n",runrec.y0);
+ printf("Step size direction Y = %d\n",runrec.dy);
+
+ if (fp) fwrite(&runrec, runrec.length,1,fp);
+
+ }
+ break;
+
+
+ case ENDREC_ID: {
+
+
+ ENDREC endrec; // end header: appears once at the end of the file
+
+ endrec.id = ENDREC_ID;
+ endrec.length = sizeof(endrec);
+ time(&t);
+ endrec.time=t;
+
+ printf("Writing header to file\n");
+ printf("RECID = %u\n",endrec.id);
+ printf("Length = %u\n",endrec.length);
+ printf("Time = %u\n",endrec.time);
+
+ if (fp) fwrite(&endrec, endrec.length,1,fp);
+
+
+ break;
+ }
+ case POSREC_ID: {
+
+
+ POSREC posrec; // position header: appears at every change of position
+
+ posrec.id = POSREC_ID;
+ posrec.length = sizeof(posrec);
+ time(&t);
+ posrec.time = t;
+ posrec.num_iter_x = data[0];
+ posrec.mikro_pos_x = data[1];
+ posrec.set_pos_x = data[2];
+ posrec.num_iter_y = data[3];
+ posrec.mikro_pos_y = data[4];
+ posrec.set_pos_y = data[5];
+
+ printf("Writing header to file\n");
+ printf("RECID = %u\n",posrec.id);
+ printf("Length = %u\n",posrec.length);
+ printf("Time = %u\n",posrec.time);
+ printf("Iteration X = %d\n",posrec.num_iter_x);
+ printf("MIKRO Position X = %d\n",posrec.mikro_pos_x);
+ printf("Set position X = %d\n",posrec.set_pos_x);
+ printf("Iteration Y = %d\n",posrec.num_iter_y);
+ printf("MIKRO Position Y = %d\n",posrec.mikro_pos_y);
+ printf("Set position Y = %d\n",posrec.set_pos_y);
+
+ if (fp) fwrite(&posrec, posrec.length,1,fp);
+
+
+
+ break;
+ }
+ }
+
+ return 0;
+}
+
+
+int CVICALLBACK ExitCB (int panel, int control, int event,
+ void *callbackData, int eventData1, int eventData2) {
+ switch (event) {
+ case EVENT_COMMIT:
+ QuitUserInterface (0);
+ break;
+ }
+ return 0;
+}
+
+
+int Fi2Pos(float x) {
+ // x in degrees
+ return (int) (x/360*NSTEPS);
+}
+
+
+int mdaq_init(const char *fname){
+
+ IniText iniText;
+char pathName[MAX_PATHNAME_LEN];
+char dirName[MAX_PATHNAME_LEN];
+
+
+/* set up the pathName for the .ini file */
+GetProjectDir (dirName);
+MakePathname (dirName, fname, pathName);
+
+/* create object for holding the value/tag pairs */
+iniText = Ini_New (TRUE); /* TRUE for automatic sorting */
+
+/* read in the tag/value pairs */
+Ini_ReadFromFile (iniText, pathName);
+
+/* create the in–memory tag/value pairs */
+
+Ini_GetInt (iniText, "scintillator", "nofcrystalsx", &conf.nofcrystalsx);
+Ini_GetInt (iniText, "scintillator", "nofcrystalsy", &conf.nofcrystalsy);
+
+Ini_GetDouble (iniText, "scintillator", "crystalpitchx", &conf.crystalpitchx);
+Ini_GetDouble (iniText, "scintillator", "crystalpitchy", &conf.crystalpitchy);
+
+
+Ini_GetStringCopy (iniText, "sensor", "modules", &conf.modules);
+Ini_GetStringCopy (iniText, "sensor", "channels", &conf.channels);
+
+
+Ini_GetStringCopy (iniText, "calibration", "sumpedestals", &conf.sumpedestals);
+Ini_GetStringCopy (iniText, "calibration", "pedestals", &conf.pedestals);
+Ini_GetStringCopy (iniText, "calibration", "photopeak", &conf.photopeak);
+Ini_GetStringCopy (iniText, "calibration", "channelcalibration", &conf.channelcalibration);
+Ini_GetInt (iniText, "calibration", "adcthreshold", &conf.adcthreshold);
+
+/* dispose of the in–memory tag/value pairs */
+Ini_Dispose (iniText);
+return 0;
+}
+
+
+void ReadModuleMap(const char *fname){
+ int id;
+ float r,phi;
+ char line[400];
+ const int ndim=400;
+ FILE *fp=fopen(fname,"r");
+ if (!fp) return;
+ while (fgets(line,ndim,fp)!=NULL) {
+ sscanf(line,"%d%f%f",&id,&r,&phi);
+ if (id<16) {
+ conf.module[id].r=r;
+ conf.module[id].phi=phi*Pi()/180.;
+ if (debug) printf("%s %d %f %f\n",fname,id, r, phi);
+ }
+ }
+ fclose(fp);
+
+}
+
+void ReadChannelMap(const char *fname){
+ int id;
+ int ix, iy;
+ char line[400];
+ const int ndim=400;
+ FILE *fp=fopen(fname,"r");
+ if (!fp) return;
+ while (fgets(line,ndim,fp)!=NULL) {
+ sscanf(line,"%d%d%d",&id,&ix,&iy);
+ if (id<16) {
+ conf.channel[id].ix=ix;
+ conf.channel[id].iy=iy;
+ if (debug) printf("%s %d %d %d\n",fname,id, ix, iy);
+ }
+ }
+ fclose(fp);
+
+}
+
+ int readfile(const char *fname, float *x, int nmax, int defaultvalue){
+ int id;
+ float ix;
+ char line[400];
+ const int ndim=400;
+ FILE *fp=fopen(fname,"r");
+ for (int i=0;i<nmax;i++){
+ x[i]=defaultvalue;
+ }
+ if (!fp) return -1;
+ while (fgets(line,ndim,fp)!=NULL) {
+ sscanf(line,"%d%f",&id,&ix);
+
+ if (id<nmax) x[id]=ix;
+ if (debug) printf("%s %d %f\n",fname,id, ix);
+
+ }
+ fclose(fp);
+ return 0;
+ }
+
+
+
+
+
+
+
+
+
+
+
+
+int Geometry(const char *fnameconfig){
+
+ mdaq_init(fnameconfig);
+
+ ReadModuleMap(conf.modules);
+ ReadChannelMap(conf.channels);
+
+ printf( "Reading ...%s\n " ,conf.channelcalibration );
+ /*
+ m_calibration = new TFile(m_calibrationrootName);
+ for (int i=0; i<4;i++) {
+ char hn[256];
+ sprintf(hn,"pmt1%d_calib",i);
+ m_crystalid[i]= (TH1I *) m_calibration->Get(hn);
+ m_crystalid[i]->ls();
+ }
+ */
+
+ conf.peakscaling=3000;
+
+ readfile(conf.pedestals,conf.apedestals,4*16, 0);
+ readfile(conf.photopeak,conf.apeak,4*16, conf.peakscaling);
+ return 0;
+ };
+
+
+int CVICALLBACK mdaq(void *functionData) {
+
+ double dfi, fi0;
+ unsigned int nsteps;
+ char filename[0xFF];
+ char fname[0xFF];
+ int daqtime;
+ daq_on = 1;
+ ctrl_c=0;
+ GetCtrlVal(p1,P1_DFI, &dfi);
+ GetCtrlVal(p1,P1_FI0, &fi0);
+ GetCtrlVal(p1,P1_NFI, &nsteps);
+ GetCtrlVal(p1,P1_DAQTIME, &daqtime);
+ GetCtrlVal(p1,P1_FILENAME, filename);
+ GetCtrlVal(p1,P1_DEBUG, &debug);
+ sprintf(fname,"%s.dat", filename);
+ Geometry("config.ini");
+ HistogramsInit();
+ PETProjDataMgrFree();
+ PETProjDataMgrInit();
+
+ SetDebug(debug);
+ SetRingDiameter(2*conf.module[0].r);
+
+
+
+
+ FILE *fp = fopen(fname, "wb");
+ int runhdr[7]= {nsteps, Fi2Pos(fi0), Fi2Pos(dfi), 1,0,1, -daqtime};
+ addheader(fp,RUNREC_ID, runhdr );
+
+
+ vmconnect();
+ for (int i=0; i<nsteps; i++) {
+
+ double fi=fi0+dfi*i;
+ conf.rotation = fi*Pi()/180;
+#ifdef uSMC_USB
+ SetCtrlVal(p1,P1_STAGELED,1);
+ int SetPos = Fi2Pos(fi);
+ uSMC_MoveTo(node,SetPos);
+ uSMC_GetPosition (node,&CurPos);
+ SetCtrlVal(p1,P1_CPOSITION,CurPos);
+
+ SetCtrlVal(p1,P1_STAGELED,0);
+ if (ctrl_c) break;
+ int poshdr[6]= {i, CurPos, SetPos, 0,0,0};
+ addheader(fp, POSREC_ID, poshdr );
+ vmacquire(-daqtime,fp, NULL);
+#endif /* uSMC_USB */
+ SetCtrlVal(p1,P1_CEVE,i);
+ printf("Step =%d angle =%f\n", i, fi);
+
+ }
+ addheader(fp, ENDREC_ID, NULL);
+ fclose(fp);
+ daq_on = 1;
+
+ WriteInterfile(filename);
+ sprintf(fname,"%s.sroot", filename);
+ HistogramsWrite(fname);
+
+
+ vmdisconnect();
+ return 0;
+}
+
+
+
+int CVICALLBACK StartCB (int panel, int control, int event,
+ void *callbackData, int eventData1, int eventData2) {
+ ThreadFunctionPtr mythread = NULL;
+ switch (event) {
+
+ case EVENT_COMMIT:
+
+ controlID=0;
+ if (panel == p1 && control == P1_START) {
+ mythread = mdaq;
+ controlID= control;
+ }
+ if (mythread!=NULL) {
+ printf("New Thread panel=%d button=%d\n", panel, control);
+
+ // SetDimming(1);
+
+
+ if (!daq_on) CmtScheduleThreadPoolFunctionAdv (poolHandle, mythread, &controlID,
+ DEFAULT_THREAD_PRIORITY,
+ EndOfThread,
+ EVENT_TP_THREAD_FUNCTION_END,
+ NULL, RUN_IN_SCHEDULED_THREAD,
+ &tfID);
+ }
+ break;
+ }
+ return 0;
+}
+
+int CVICALLBACK StopCB (int panel, int control, int event,
+ void *callbackData, int eventData1, int eventData2) {
+ switch (event) {
+ case EVENT_COMMIT:
+ ctrl_c=1;
+ break;
+ }
+ return 0;
+}
+
+int CVICALLBACK DebugCB (int panel, int control, int event,
+ void *callbackData, int eventData1, int eventData2) {
+ switch (event) {
+ case EVENT_COMMIT:
+ GetCtrlVal(p1,P1_DEBUG, &debug);
+ break;
+ }
+ return 0;
+}
+
+
+
+int CVICALLBACK RedrawCB (int panel, int control, int event,
+ void *callbackData, int eventData1, int eventData2) {
+ int pmtid;
+ switch (event) {
+ case EVENT_COMMIT:
+ case EVENT_TIMER_TICK:
+ GetCtrlVal(p1,P1_CH, &gadcchannel);
+ pmtid = gadcchannel/16;
+ if (H1D_Exist(m_Adc[gadcchannel])) H1D_Draw(m_Adc[gadcchannel], p1,P1_GADC, &plothandle[0]);
+ if (H2D_Exist( m_CenterOfGravity[pmtid])) H2D_Draw(m_CenterOfGravity[pmtid], p1,P1_GXY, &plothandle[1]);
+ if (H1D_Exist(m_AdcSum[pmtid])) H1D_Draw(m_AdcSum[pmtid], p1,P1_GSUMADC, &plothandle[2]);
+ break;
+ }
+ return 0;
+}
+
Index: petdemo/petdemo.prj
===================================================================
--- petdemo/petdemo.prj (nonexistent)
+++ petdemo/petdemo.prj (revision 264)
@@ -0,0 +1,617 @@
+[Project Header]
+Version = 1302
+Pathname = "/c/home/cvi/apps/petdemo/petdemo.prj"
+CVI Dir = "/c/program files/national instruments/cvi2013"
+CVI Shared Dir = "/C/Program Files/National Instruments/Shared/CVI"
+CVI Pub Local Dir = "/C/ProgramData/National Instruments/CVI2013"
+CVI Pub Global Dir = "/C/ProgramData/National Instruments/CVI"
+IVI Standard Root Dir = "/C/Program Files/IVI Foundation/IVI"
+VXIplug&play Framework Dir = "/C/Program Files/IVI Foundation/VISA/winnt"
+IVI Standard Root 64-bit Dir = "/C/Program Files/IVI Foundation/IVI"
+VXIplug&play Framework 64-bit Dir = "/C/Program Files/IVI Foundation/VISA/win64"
+Number of Files = 17
+Target Type = "Executable"
+Flags = 2064
+Copied From Locked InstrDrv Directory = False
+Copied from VXIPNP Directory = False
+Locked InstrDrv Name = ""
+Don't Display Deploy InstrDrv Dialog = False
+
+[Folders]
+Folder 0 = "User Interface Files"
+FolderEx 0 = "User Interface Files"
+Folder 1 = "Source Files"
+FolderEx 1 = "Source Files"
+Folder 2 = "Instrument Files"
+FolderEx 2 = "Instrument Files"
+Folder 3 = "Library Files"
+FolderEx 3 = "Library Files"
+Folder 4 = "Include Files"
+FolderEx 4 = "Include Files"
+
+[File 0001]
+File Type = "User Interface Resource"
+Res Id = 1
+Path Is Rel = True
+Path Rel To = "Project"
+Path Rel Path = "petdemo_uir.uir"
+Path = "/c/home/cvi/apps/petdemo/petdemo_uir.uir"
+Exclude = False
+Project Flags = 0
+Folder = "User Interface Files"
+Folder Id = 0
+
+[File 0002]
+File Type = "CSource"
+Res Id = 2
+Path Is Rel = True
+Path Rel To = "Project"
+Path Rel Path = "analyse.c"
+Path = "/c/home/cvi/apps/petdemo/analyse.c"
+Exclude = False
+Compile Into Object File = False
+Project Flags = 0
+Folder = "Source Files"
+Folder Id = 1
+
+[File 0003]
+File Type = "CSource"
+Res Id = 3
+Path Is Rel = True
+Path Rel To = "Project"
+Path Rel Path = "petdemo.c"
+Path = "/c/home/cvi/apps/petdemo/petdemo.c"
+Exclude = False
+Compile Into Object File = False
+Project Flags = 0
+Folder = "Source Files"
+Folder Id = 1
+
+[File 0004]
+File Type = "CSource"
+Res Id = 4
+Path Is Rel = True
+Path Rel To = "Project"
+Path Rel Path = "PETProjDataMgr.c"
+Path = "/c/home/cvi/apps/petdemo/PETProjDataMgr.c"
+Exclude = False
+Compile Into Object File = False
+Project Flags = 0
+Folder = "Source Files"
+Folder Id = 1
+
+[File 0005]
+File Type = "CSource"
+Res Id = 5
+Path Is Rel = True
+Path Rel To = "Project"
+Path Rel Path = "vmusb.c"
+Path = "/c/home/cvi/apps/petdemo/vmusb.c"
+Exclude = False
+Compile Into Object File = False
+Project Flags = 0
+Folder = "Source Files"
+Folder Id = 1
+
+[File 0006]
+File Type = "CSource"
+Res Id = 6
+Path Is Rel = True
+Path Rel To = "Project"
+Path Rel Path = "../../instr/WUSBVME_DLL/wusbvme_dll.c"
+Path = "/c/home/cvi/instr/WUSBVME_DLL/wusbvme_dll.c"
+Exclude = False
+Compile Into Object File = False
+Project Flags = 0
+Folder = "Source Files"
+Folder Id = 1
+
+[File 0007]
+File Type = "Function Panel"
+Res Id = 7
+Path Is Rel = True
+Path Rel To = "Project"
+Path Rel Path = "../../instr/HISTO/H1D.fp"
+Path = "/c/home/cvi/instr/HISTO/H1D.fp"
+Exclude = False
+Project Flags = 0
+Folder = "Instrument Files"
+Folder Id = 2
+
+[File 0008]
+File Type = "Function Panel"
+Res Id = 8
+Path Is Rel = True
+Path Rel To = "Project"
+Path Rel Path = "../../instr/HISTO/H2D.fp"
+Path = "/c/home/cvi/instr/HISTO/H2D.fp"
+Exclude = False
+Project Flags = 0
+Folder = "Instrument Files"
+Folder Id = 2
+
+[File 0009]
+File Type = "Function Panel"
+Res Id = 9
+Path Is Rel = True
+Path Rel To = "Project"
+Path Rel Path = "../../instr/HISTO/H3D.fp"
+Path = "/c/home/cvi/instr/HISTO/H3D.fp"
+Exclude = False
+Project Flags = 0
+Folder = "Instrument Files"
+Folder Id = 2
+
+[File 0010]
+File Type = "Function Panel"
+Res Id = 10
+Path Is Rel = True
+Path Rel To = "CVI"
+Path Rel To Override = "CVI"
+Path Rel Path = "toolslib/toolbox/inifile.fp"
+Path = "/c/Program Files/National Instruments/CVI2013/toolslib/toolbox/inifile.fp"
+Exclude = False
+Project Flags = 0
+Folder = "Instrument Files"
+Folder Id = 2
+
+[File 0011]
+File Type = "Function Panel"
+Res Id = 11
+Path Is Rel = True
+Path Rel To = "Project"
+Path Rel Path = "../../instr/uSMC/uSMC.fp"
+Path = "/c/home/cvi/instr/uSMC/uSMC.fp"
+Exclude = False
+Project Flags = 0
+Folder = "Instrument Files"
+Folder Id = 2
+
+[File 0012]
+File Type = "Library"
+Res Id = 12
+Path Is Rel = True
+Path Rel To = "Project"
+Path Rel Path = "../../instr/vme/msvc/libusb.lib"
+Path = "/c/home/cvi/instr/vme/msvc/libusb.lib"
+Exclude = False
+Project Flags = 0
+Folder = "Library Files"
+Folder Id = 3
+
+[File 0013]
+File Type = "Library"
+Res Id = 13
+Path Is Rel = True
+Path Rel To = "Project"
+Path Rel Path = "libxxusb.lib"
+Path = "/c/home/cvi/apps/petdemo/libxxusb.lib"
+Exclude = False
+Project Flags = 0
+Folder = "Library Files"
+Folder Id = 3
+
+[File 0014]
+File Type = "Library"
+Res Id = 14
+Path Is Rel = True
+Path Rel To = "Project"
+Path Rel Path = "../../instr/uSMC/msvc/USMCDLL.lib"
+Path = "/c/home/cvi/instr/uSMC/msvc/USMCDLL.lib"
+Exclude = False
+Project Flags = 0
+Folder = "Library Files"
+Folder Id = 3
+
+[File 0015]
+File Type = "Include"
+Res Id = 15
+Path Is Rel = True
+Path Rel To = "Project"
+Path Rel Path = "daq.h"
+Path = "/c/home/cvi/apps/petdemo/daq.h"
+Exclude = False
+Project Flags = 0
+Folder = "Include Files"
+Folder Id = 4
+
+[File 0016]
+File Type = "Include"
+Res Id = 16
+Path Is Rel = True
+Path Rel To = "Project"
+Path Rel Path = "PETProjDataMgr.h"
+Path = "/c/home/cvi/apps/petdemo/PETProjDataMgr.h"
+Exclude = False
+Project Flags = 0
+Folder = "Include Files"
+Folder Id = 4
+
+[File 0017]
+File Type = "Include"
+Res Id = 17
+Path Is Rel = True
+Path Rel To = "Project"
+Path Rel Path = "../../instr/vme/vme.h"
+Path = "/c/home/cvi/instr/vme/vme.h"
+Exclude = False
+Project Flags = 0
+Folder = "Include Files"
+Folder Id = 4
+
+[Custom Build Configs]
+Num Custom Build Configs = 0
+
+[Default Build Config Debug]
+Config Name = "Debug"
+Is 64-Bit = False
+Is Release = False
+Default Calling Convention = "cdecl"
+Optimization Level = "Optimize for speed (level 2)"
+Require Prototypes = True
+Show Warning IDs in Build Output = False
+Selected Warning Level = "None"
+Warning List None = "4,9,84,105,106,107,108,109,110,111"
+Warning List Common = ""
+Warning List Extended = ""
+Warning List All = ""
+Warning Mode = 0
+Enable Unreferenced Identifiers Warning = False
+Enable Pointer Mismatch Warning = False
+Enable Unreachable Code Warning = False
+Enable Assignment In Conditional Warning = False
+Uninitialized Locals Compile Warning = "Aggressive"
+Require Return Values = True
+Enable C99 Extensions = False
+Enable OpenMP Extensions = False
+Stack Size = 250000
+Stack Reserve = 1048576
+Stack Commit = 4096
+Image Base Address = 4194304
+Image Base Address x64 = 4194304
+Compiler Defines = "/DWIN32_LEAN_AND_MEAN /DXXDLL"
+Sign = False
+Sign Store = ""
+Sign Certificate = ""
+Sign Timestamp URL = ""
+Sign URL = ""
+Manifest Embed = False
+Icon File Is Rel = False
+Icon File = ""
+Application Title = ""
+Use IVI Subdirectories for Import Libraries = False
+Use VXIPNP Subdirectories for Import Libraries = False
+Use Dflt Import Lib Base Name = True
+Where to Copy DLL = "Do not copy"
+Custom Directory to Copy DLL Is Rel = False
+Custom Directory to Copy DLL = ""
+Generate Source Documentation = "None"
+Runtime Support = "Full Runtime Support"
+Runtime Binding = "Shared"
+Embed Project .UIRs = False
+Generate Map File = False
+Embed Timestamp = True
+Create Console Application = False
+Using LoadExternalModule = False
+DLL Exports = "Include File Symbols"
+Register ActiveX Server = False
+Numeric File Version = "1,0,0,0"
+Numeric Prod Version = "1,0,0,0"
+Comments = ""
+Comments Ex = ""
+Company Name = ""
+Company Name Ex = "%company"
+File Description = "petdemo (Debug x86)"
+File Description Ex = "%application (%rel_dbg %arch)"
+File Version = "1.0"
+File Version Ex = "%f1.%f2"
+Internal Name = "petdemo"
+Internal Name Ex = "%basename"
+Legal Copyright = "Copyright © 2017"
+Legal Copyright Ex = "Copyright © %company %Y"
+Legal Trademarks = ""
+Legal Trademarks Ex = ""
+Original Filename = "petdemo.exe"
+Original Filename Ex = "%filename"
+Private Build = ""
+Private Build Ex = ""
+Product Name = " petdemo"
+Product Name Ex = "%company %application"
+Product Version = "1.0"
+Product Version Ex = "%p1.%p2"
+Special Build = ""
+Special Build Ex = ""
+Add Type Lib To DLL = False
+Include Type Lib Help Links = False
+TLB Help Style = "HLP"
+Type Lib FP File Is Rel = False
+Type Lib FP File = ""
+
+[Default Build Config Release]
+Config Name = "Release"
+Is 64-Bit = False
+Is Release = True
+Default Calling Convention = "cdecl"
+Optimization Level = "Optimize for speed (level 2)"
+Require Prototypes = True
+Show Warning IDs in Build Output = False
+Selected Warning Level = "None"
+Warning List None = "4,9,84,105,106,107,108,109,110,111"
+Warning List Common = ""
+Warning List Extended = ""
+Warning List All = ""
+Warning Mode = 0
+Enable Unreferenced Identifiers Warning = False
+Enable Pointer Mismatch Warning = False
+Enable Unreachable Code Warning = False
+Enable Assignment In Conditional Warning = False
+Uninitialized Locals Compile Warning = "Aggressive"
+Require Return Values = True
+Enable C99 Extensions = False
+Enable OpenMP Extensions = False
+Stack Size = 250000
+Stack Reserve = 1048576
+Stack Commit = 4096
+Image Base Address = 4194304
+Image Base Address x64 = 4194304
+Compiler Defines = "/DWIN32_LEAN_AND_MEAN"
+Sign = False
+Sign Store = ""
+Sign Certificate = ""
+Sign Timestamp URL = ""
+Sign URL = ""
+Manifest Embed = False
+Icon File Is Rel = False
+Icon File = ""
+Application Title = ""
+Use IVI Subdirectories for Import Libraries = False
+Use VXIPNP Subdirectories for Import Libraries = False
+Use Dflt Import Lib Base Name = True
+Where to Copy DLL = "Do not copy"
+Custom Directory to Copy DLL Is Rel = False
+Custom Directory to Copy DLL = ""
+Generate Source Documentation = "None"
+Runtime Support = "Full Runtime Support"
+Runtime Binding = "Shared"
+Embed Project .UIRs = False
+Generate Map File = False
+Embed Timestamp = True
+Create Console Application = False
+Using LoadExternalModule = False
+DLL Exports = "Include File Symbols"
+Register ActiveX Server = False
+Add Type Lib To DLL = False
+Include Type Lib Help Links = False
+TLB Help Style = "HLP"
+Type Lib FP File Is Rel = False
+Type Lib FP File = ""
+
+[Default Build Config Debug64]
+Config Name = "Debug64"
+Is 64-Bit = True
+Is Release = False
+Default Calling Convention = "cdecl"
+Optimization Level = "Optimize for speed (level 2)"
+Require Prototypes = True
+Show Warning IDs in Build Output = False
+Selected Warning Level = "None"
+Warning List None = "4,9,84,105,106,107,108,109,110,111"
+Warning List Common = ""
+Warning List Extended = ""
+Warning List All = ""
+Warning Mode = 0
+Enable Unreferenced Identifiers Warning = False
+Enable Pointer Mismatch Warning = False
+Enable Unreachable Code Warning = False
+Enable Assignment In Conditional Warning = False
+Uninitialized Locals Compile Warning = "Aggressive"
+Require Return Values = True
+Enable C99 Extensions = False
+Enable OpenMP Extensions = False
+Stack Size = 250000
+Stack Reserve = 1048576
+Stack Commit = 4096
+Image Base Address = 4194304
+Image Base Address x64 = 4194304
+Compiler Defines = "/DWIN32_LEAN_AND_MEAN"
+Sign = False
+Sign Store = ""
+Sign Certificate = ""
+Sign Timestamp URL = ""
+Sign URL = ""
+Manifest Embed = False
+Icon File Is Rel = False
+Icon File = ""
+Application Title = ""
+Use IVI Subdirectories for Import Libraries = False
+Use VXIPNP Subdirectories for Import Libraries = False
+Use Dflt Import Lib Base Name = True
+Where to Copy DLL = "Do not copy"
+Custom Directory to Copy DLL Is Rel = False
+Custom Directory to Copy DLL = ""
+Generate Source Documentation = "None"
+Runtime Support = "Full Runtime Support"
+Runtime Binding = "Shared"
+Embed Project .UIRs = False
+Generate Map File = False
+Embed Timestamp = True
+Create Console Application = False
+Using LoadExternalModule = False
+DLL Exports = "Include File Symbols"
+Register ActiveX Server = False
+Add Type Lib To DLL = False
+Include Type Lib Help Links = False
+TLB Help Style = "HLP"
+Type Lib FP File Is Rel = False
+Type Lib FP File = ""
+
+[Default Build Config Release64]
+Config Name = "Release64"
+Is 64-Bit = True
+Is Release = True
+Default Calling Convention = "cdecl"
+Optimization Level = "Optimize for speed (level 2)"
+Require Prototypes = True
+Show Warning IDs in Build Output = False
+Selected Warning Level = "None"
+Warning List None = "4,9,84,105,106,107,108,109,110,111"
+Warning List Common = ""
+Warning List Extended = ""
+Warning List All = ""
+Warning Mode = 0
+Enable Unreferenced Identifiers Warning = False
+Enable Pointer Mismatch Warning = False
+Enable Unreachable Code Warning = False
+Enable Assignment In Conditional Warning = False
+Uninitialized Locals Compile Warning = "Aggressive"
+Require Return Values = True
+Enable C99 Extensions = False
+Enable OpenMP Extensions = False
+Stack Size = 250000
+Stack Reserve = 1048576
+Stack Commit = 4096
+Image Base Address = 4194304
+Image Base Address x64 = 4194304
+Compiler Defines = "/DWIN32_LEAN_AND_MEAN"
+Sign = False
+Sign Store = ""
+Sign Certificate = ""
+Sign Timestamp URL = ""
+Sign URL = ""
+Manifest Embed = False
+Icon File Is Rel = False
+Icon File = ""
+Application Title = ""
+Use IVI Subdirectories for Import Libraries = False
+Use VXIPNP Subdirectories for Import Libraries = False
+Use Dflt Import Lib Base Name = True
+Where to Copy DLL = "Do not copy"
+Custom Directory to Copy DLL Is Rel = False
+Custom Directory to Copy DLL = ""
+Generate Source Documentation = "None"
+Runtime Support = "Full Runtime Support"
+Runtime Binding = "Shared"
+Embed Project .UIRs = False
+Generate Map File = False
+Embed Timestamp = True
+Create Console Application = False
+Using LoadExternalModule = False
+DLL Exports = "Include File Symbols"
+Register ActiveX Server = False
+Add Type Lib To DLL = False
+Include Type Lib Help Links = False
+TLB Help Style = "HLP"
+Type Lib FP File Is Rel = False
+Type Lib FP File = ""
+
+[Compiler Options]
+Default Calling Convention = "cdecl"
+Require Prototypes = True
+Require Return Values = True
+Enable Pointer Mismatch Warning = False
+Enable Unreachable Code Warning = False
+Enable Unreferenced Identifiers Warning = False
+Enable Assignment In Conditional Warning = False
+O Option Compatible With 5.0 = False
+Enable C99 Extensions = False
+Uninitialized Locals Compile Warning = "Aggressive"
+Precompile Prefix Header = False
+Prefix Header File = ""
+
+[Run Options]
+Stack Size = 250000
+Stack Commit = 4096
+Image Base Address = 4194304
+Image Base Address x64 = 4194304
+
+[Compiler Defines]
+Compiler Defines = "/DWIN32_LEAN_AND_MEAN"
+
+[Include Paths]
+Include Path 1 Is Rel = True
+Include Path 1 Rel To = "Project"
+Include Path 1 Rel Path = "../../instr/CAENV965"
+Include Path 1 = "/c/home/cvi/instr/CAENV965"
+Include Path 2 Is Rel = True
+Include Path 2 Rel To = "Project"
+Include Path 2 Rel Path = "../../instr/WUSBVME_DLL"
+Include Path 2 = "/c/home/cvi/instr/WUSBVME_DLL"
+
+[Create Executable]
+Executable File_Debug Is Rel = True
+Executable File_Debug Rel To = "Project"
+Executable File_Debug Rel Path = "petdemo.exe"
+Executable File_Debug = "/c/home/cvi/apps/petdemo/petdemo.exe"
+Executable File_Release Is Rel = True
+Executable File_Release Rel To = "Project"
+Executable File_Release Rel Path = "petdemo.exe"
+Executable File_Release = "/c/home/cvi/apps/petdemo/petdemo.exe"
+Executable File_Debug64 Is Rel = True
+Executable File_Debug64 Rel To = "Project"
+Executable File_Debug64 Rel Path = "petdemo.exe"
+Executable File_Debug64 = "/c/home/cvi/apps/petdemo/petdemo.exe"
+Executable File_Release64 Is Rel = True
+Executable File_Release64 Rel To = "Project"
+Executable File_Release64 Rel Path = "petdemo.exe"
+Executable File_Release64 = "/c/home/cvi/apps/petdemo/petdemo.exe"
+Icon File Is Rel = False
+Icon File = ""
+Application Title = ""
+DLL Exports = "Include File Symbols"
+Use IVI Subdirectories for Import Libraries = False
+Use VXIPNP Subdirectories for Import Libraries = False
+Use Dflt Import Lib Base Name = True
+Where to Copy DLL = "Do not copy"
+Custom Directory to Copy DLL Is Rel = False
+Custom Directory to Copy DLL = ""
+Generate Source Documentation = "None"
+Add Type Lib To DLL = False
+Include Type Lib Help Links = False
+TLB Help Style = "HLP"
+Type Lib FP File Is Rel = False
+Type Lib FP File = ""
+Type Lib Guid = ""
+Runtime Support = "Full Runtime Support"
+Instrument Driver Support Only = False
+Embed Project .UIRs = False
+Generate Map File = False
+
+[External Compiler Support]
+UIR Callbacks File Option = 0
+Using LoadExternalModule = False
+Create Project Symbols File = True
+UIR Callbacks Obj File Is Rel = False
+UIR Callbacks Obj File = ""
+Project Symbols H File Is Rel = False
+Project Symbols H File = ""
+Project Symbols Obj File Is Rel = False
+Project Symbols Obj File = ""
+
+[ActiveX Server Options]
+Specification File Is Rel = False
+Specification File = ""
+Source File Is Rel = False
+Source File = ""
+Include File Is Rel = False
+Include File = ""
+IDL File Is Rel = False
+IDL File = ""
+Register ActiveX Server = False
+
+[Signing Info]
+Sign = False
+Sign Debug Build = False
+Store = ""
+Certificate = ""
+Timestamp URL = ""
+URL = ""
+
+[Manifest Info]
+Embed = False
+
+[tpcSection]
+tpcEnabled = 0
+tpcOverrideEnvironment = 0
+tpcEnabled x64 = 0
+tpcOverrideEnvironment x64 = 0
+
Index: petdemo/petdemo_uir.h
===================================================================
--- petdemo/petdemo_uir.h (nonexistent)
+++ petdemo/petdemo_uir.h (revision 264)
@@ -0,0 +1,61 @@
+/**************************************************************************/
+/* LabWindows/CVI User Interface Resource (UIR) Include File */
+/* */
+/* WARNING: Do not add to, delete from, or otherwise modify the contents */
+/* of this include file. */
+/**************************************************************************/
+
+#include <userint.h>
+
+#ifdef __cplusplus
+ extern "C" {
+#endif
+
+ /* Panels and Controls: */
+
+#define P1 1
+#define P1_EXIT 2 /* control type: command, callback function: ExitCB */
+#define P1_STOP 3 /* control type: command, callback function: StopCB */
+#define P1_START 4 /* control type: command, callback function: StartCB */
+#define P1_CPOSITION 5 /* control type: numeric, callback function: (none) */
+#define P1_NFI 6 /* control type: numeric, callback function: (none) */
+#define P1_DFI 7 /* control type: numeric, callback function: (none) */
+#define P1_FI0 8 /* control type: numeric, callback function: (none) */
+#define P1_CEVE 9 /* control type: numeric, callback function: (none) */
+#define P1_DAQTIME 10 /* control type: numeric, callback function: (none) */
+#define P1_GXY 11 /* control type: graph, callback function: (none) */
+#define P1_GSUMADC 12 /* control type: graph, callback function: (none) */
+#define P1_GADC 13 /* control type: graph, callback function: (none) */
+#define P1_GSINOGRAM 14 /* control type: graph, callback function: (none) */
+#define P1_PROGRESS 15 /* control type: slide, callback function: (none) */
+#define P1_STAGELED 16 /* control type: LED, callback function: (none) */
+#define P1_STDIO 17 /* control type: textBox, callback function: (none) */
+#define P1_FILENAME 18 /* control type: string, callback function: (none) */
+#define P1_DEBUG 19 /* control type: radioButton, callback function: DebugCB */
+#define P1_CH 20 /* control type: numeric, callback function: RedrawCB */
+#define P1_TIMER 21 /* control type: timer, callback function: RedrawCB */
+#define P1_DAQRATE 22 /* control type: strip, callback function: (none) */
+
+
+ /* Control Arrays: */
+
+ /* (no control arrays in the resource file) */
+
+
+ /* Menu Bars, Menus, and Menu Items: */
+
+ /* (no menu bars in the resource file) */
+
+
+ /* Callback Prototypes: */
+
+int CVICALLBACK DebugCB(int panel, int control, int event, void *callbackData, int eventData1, int eventData2);
+int CVICALLBACK ExitCB(int panel, int control, int event, void *callbackData, int eventData1, int eventData2);
+int CVICALLBACK RedrawCB(int panel, int control, int event, void *callbackData, int eventData1, int eventData2);
+int CVICALLBACK StartCB(int panel, int control, int event, void *callbackData, int eventData1, int eventData2);
+int CVICALLBACK StopCB(int panel, int control, int event, void *callbackData, int eventData1, int eventData2);
+
+
+#ifdef __cplusplus
+ }
+#endif
Index: petdemo/petdemo_uir.uir
===================================================================
Cannot display: file marked as a binary type.
svn:mime-type = application/octet-stream
/petdemo/petdemo_uir.uir
Property changes:
Added: svn:mime-type
## -0,0 +1 ##
+application/octet-stream
\ No newline at end of property
Index: petdemo/vmusb.c
===================================================================
--- petdemo/vmusb.c (nonexistent)
+++ petdemo/vmusb.c (revision 264)
@@ -0,0 +1,433 @@
+//#define XXDLL
+//#include "vme.h"
+
+
+
+typedef unsigned short ADDRESS_MODIFIER;
+#define Std_NoPriv_Data (ADDRESS_MODIFIER)0x39
+
+#include "wusbvme_dll.h"
+#include "toolbox.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+
+#include <errno.h>
+#include <ctype.h>
+#include <time.h>
+
+
+#include <signal.h>
+
+
+#include "CAENV965_DEF.h"
+
+#include "petdemo_uir.h"
+#include "daq.h"
+
+extern int p1;
+extern int ctrl_c;
+
+
+#define VERSION 1.0
+#define TIMEOUT 3
+
+int *fThreshold = NULL;
+
+/* VME modules */
+
+#define CAEN_V792 0x340000 // IJS V792
+#define CAEN_V792_1 0x530000 // FMF1 V792
+#define CAEN_V792_2 0x630000 // FMF2 V792
+#define CAEN_V965 0x350000 // IJS V965
+int addr[3]= {CAEN_V792,CAEN_V792_1,CAEN_V965 };
+const int nadc=2;
+#define BUFF_L 2048
+static int stackwrite[10000];
+static int stackdata[10000],stackdump[27000];
+
+
+/************************************************/
+int weight_while(int num) {
+ int i, tmp;
+
+ for ( i =0; i <num; i++ ) tmp = 0;
+ return 0;
+}
+#define WWHILE weight_while(0)
+
+#define TRUE 1
+#define FALSE 0
+
+int timer_out;
+
+void timerast (int signumber) {
+ timer_out = TRUE;
+ fprintf(stderr,"TIMEOUT !!!\n");
+}
+
+void tmlnk (int tout) {
+
+}
+
+void tmulk () {
+
+}
+
+int fexist( char *path) {
+
+
+ if(!path || !*path) return 0;
+ ssize_t fileSize;
+ int res = FileExists (path, &fileSize);
+
+
+ if (res>0) {
+ return 1;
+ } else {
+ return 0;
+ }
+}
+
+
+
+
+int init() {
+
+ xxusb_register_write(WUSB_udev,1,0x0); // Stop DAQ mode
+ while (xxusb_usbfifo_read(WUSB_udev,(int *) stackdump,BUFF_L,100)>0);
+
+
+ int rate=1000;
+ int freq=80000000/rate-40; // 80 MHz
+ if (freq<72) freq=72;
+ // Set DGG channel A as a pulser, output on O1,
+ // with delay =500 x 12.5ns,
+ // and width = 500 x 12.5ns,
+ // not latching or inverting
+ // VME_DGG(udev,0,6,0,24000,6000,0,0);
+ VME_DGG(WUSB_udev,0,6,0,freq,40,0,0);
+ // Set DGG channel B to trigger on NIM1, output on O2,
+ // with delay =200 x 12.5ns,
+ // and width = 200 x 12.5ns,
+ // not latching or inverting
+ VME_DGG(WUSB_udev,1,1,1,0,10,0,1);
+ printf("CAEN V965 Pedestal set to %d\n", fPedestal);
+
+// INIT stackdata
+ int fPedestal=255;
+ WIENER_VMUSB_StackInit();
+
+ for (int i=0; i<nadc; i++) {
+ WIENER_VMUSB_StackWriteA24D16( addr[i] + CAENV965_CRN , 0x0);
+ WIENER_VMUSB_StackWriteA24D16( addr[i] + CAENV965_GEO , i);
+ WIENER_VMUSB_StackReadA24D16(addr[i] + CAENV965_GEO);
+ for (int j=0; j<32; j++) {
+ WIENER_VMUSB_StackWriteA24D16(addr[i] + CAENV965_THM + 0x02*j, fThreshold[j+i*32]); // threshold/kill for 32 channels
+ }
+ WIENER_VMUSB_StackWriteA24D16( addr[i] + CAENV965_BS1, 0x80 ); // soft reset
+ WIENER_VMUSB_StackWriteA24D16( addr[i] + CAENV965_BC1, 0x80 ); // soft reset
+ WIENER_VMUSB_StackWriteA24D16( addr[i] + CAENV965_PED, fPedestal ); // pedestal
+ WIENER_VMUSB_StackWriteA24D16( addr[i] + CAENV965_BS2,0x5000);
+ WIENER_VMUSB_StackWriteA24D16( addr[i] + CAENV965_BS2,0x4); // clear module
+ WIENER_VMUSB_StackWriteA24D16( addr[i] + CAENV965_BC2,0x4);
+ }
+ WIENER_VMUSB_StackMarker(0xFAFC);
+ //WIENER_VMUSB_StackPrint();
+
+
+ VME_LED_settings(WUSB_udev, 0,0,0,0); // Set Yellow LED to light with with USB out buffer not empty
+ VME_LED_settings(WUSB_udev, 1,1,0,0); // Set Red LED to light with NIM1
+ VME_LED_settings(WUSB_udev,2,0,0,0); // Set Green LED to light when stack is not empty
+
+ unsigned int vmereg;
+ VME_register_read(WUSB_udev,0x00,&vmereg);
+ printf("VMUSB Firmware ID -> 0x%08X\n",vmereg);
+
+ VME_register_read(WUSB_udev,0x04,&vmereg);
+ printf("VMUSB Global Mode -> 0x%08X\n",vmereg);
+
+ vmereg=(vmereg&0xF000)|0x0004;
+ VME_register_write(WUSB_udev,0x04,vmereg);
+ VME_register_write(WUSB_udev,0x08,0x00000080);
+ VME_register_write(WUSB_udev,0x28,0x0);
+ VME_register_write(WUSB_udev,0x2C,0x0);
+ VME_register_write(WUSB_udev,0x30,0x0);
+ VME_register_write(WUSB_udev,0x34,0x0);
+ VME_register_write(WUSB_udev,0x3C,0x000);
+
+ int nb = WIENER_VMUSB_StackGetUint32(10000,stackdata);
+ int ret= xxusb_stack_execute(WUSB_udev,(uint32_t *)stackdata);
+ printf("Init::%d ret=%d\n",nb,ret);
+ if (ret>0) for (int k=0; k<ret/2; k++) printf ("stackdata=0x%08X\n",stackdata[k]);
+
+
+ WIENER_VMUSB_StackClear();
+ WIENER_VMUSB_StackMarker(0xFFAB);
+ for (int j=0; j<36; j++) WIENER_VMUSB_StackReadA24D32(addr[0] + CAENV965_OB);
+ WIENER_VMUSB_StackMarker(0xFAFB);
+ for (int k0=0; k0<nadc; k0++) {
+ WIENER_VMUSB_StackWriteA24D16(addr[k0] + CAENV965_BS2,0x4); // clear module
+ WIENER_VMUSB_StackWriteA24D16(addr[k0] + CAENV965_BC2,0x4);
+ }
+
+ int nb0= WIENER_VMUSB_StackGetUint32(10000,&stackwrite[0]);
+ if (nb0>768) {
+ fprintf(stderr,"nb0=%d > 768 error xxusb_stack_write\n", nb0);
+ exit(-1);
+ }
+ nb =xxusb_stack_write(WUSB_udev,0x2,(uint32_t *) stackwrite);
+ nb0=xxusb_stack_read(WUSB_udev,0x2,(uint32_t *) stackdata);
+ for (int k1=0; k1<stackwrite[0]+1; k1++) {
+ if (stackdata[k1]!=stackwrite[k1]) printf("%d %d init err %x %x\n",nb,nb0,
+ stackwrite[k1], stackdata[k1]);
+ }
+
+ if (fMode==2) xxusb_register_write(WUSB_udev,1,0x1); // Start DAQ mode
+
+ printf("daq::init() \n");
+ return 0;
+}
+
+int vmconnect() {
+ WIENER_VMUSB_VME_START("VM0120");
+ //VME_START(WIENER_VMUSB);
+
+ printf("daq::connect()\n");
+ return 0;
+}
+
+int vmdisconnect() {
+ /* zakljuci */
+ WIENER_VMUSB_VME_STOP();
+ printf("daq::disconnect()\n");
+ return 0;
+}
+
+int clear() {
+ return 0;
+}
+
+int module_header(int recid,uint32_t *data,int len) {
+ data[0] = recid;
+ data[1] = (len >0)? len : 0 ;
+ return data[1]+2;
+}
+
+
+int event(unsigned int *data, int maxn, int *ctr, int print) {
+ int tout=200; /* 1/100 of a second */
+ const int lsize=sizeof(uint32_t);
+
+
+ ctr[0]++;
+ ctr[1]++;
+
+ int count=0;
+ switch (fMode) {
+ case 0: { // normal calls
+ unsigned short clr= 0x4;
+ unsigned int status=0;
+ uint32_t mdata;
+
+ for (int i=0; i<nadc; i++) {
+ // wait for trg
+ tmlnk (tout);
+ do WIENER_VMUSB_VME_A24D16_R( addr[i] + CAENV965_SR1, &status);
+ while ( (status&0x1)==0 && timer_out==0 && ctrl_c==0 );
+ tmulk();
+ // readout data
+ if (timer_out) return 0;
+ if (ctrl_c) return 0;
+ int len=0;
+
+ do {
+ WIENER_VMUSB_VME_A24D32_R(addr[i] + CAENV965_OB, &mdata);
+ mdata=data[count++];
+ len++;
+ } while ( (mdata & 0x4000000)==0 && timer_out==0) ; // bit 26 EOB or not valid datum
+ // clear
+ WIENER_VMUSB_VME_A24D16_W( addr[i] + CAENV965_BS2, &clr);
+ WIENER_VMUSB_VME_A24D16_W( addr[i] + CAENV965_BC2, &clr);
+
+ if (count+2<maxn) {
+ if (print) printf("V965 %3d\n",len);
+ count+=module_header(0x130+i,&data[count],len);
+ ctr[2]++;
+ ctr[3]+=len;
+ }
+
+ timer_out=0;
+ }
+ }
+ break;
+ case 1: { // stack execute
+ WIENER_VMUSB_StackGetUint32(10000,(int *)data);
+ int ret=xxusb_stack_execute(WUSB_udev,(uint32_t *) data); //The first element of the array is the number of bytes.
+ if (ret< 0 ) {
+ printf ("xxusb_stack_execute error err=%d\n",ret); \
+ count = 0;
+ } else count= ret/lsize;
+
+ }
+ break;
+ case 2: { // stack load
+ int ret=xxusb_usbfifo_read(WUSB_udev,(int *) data,BUFF_L,100);
+ if (ret< 0 ) {
+ if (ret!=-110) {
+ printf ("xxusb_usbfifo_read error err=%d\n",ret);
+ end();
+ init();
+ }
+ count = 0;
+ } else {
+ if (debug) {
+ for (int i=0; i<100; i++) {
+ printf ("%4d fifodata=0x%08X\n",i, data[i]);
+ if (data[i]==0xFAFB) break;
+ }
+ /*
+
+ 0 fifodata=0x0000000D
+ 1 fifodata=0x00000049
+ 2 fifodata=0x0000FFAB
+ 3 fifodata=0x00002000
+ 4 fifodata=0x00000200
+ 5 fifodata=0x00004141
+ 6 fifodata=0x00000000
+ 7 fifodata=0x00004057
+
+ 70 fifodata=0x00000400
+ 71 fifodata=0x00000035
+ 72 fifodata=0x00000600
+ 73 fifodata=0x00000035
+ 74 fifodata=0x0000FAFB
+
+
+ */
+ }
+ static double t0=0;
+ static int nc =0;
+ nc += data[0];
+ if (print) {
+ //printf("##------------------ret=%d data[0]=%d\n",ret,(int)data[0]);
+ double t1 = Timer();
+ double dt = t1-t0;
+ t0 = t1;
+ float rate = (dt>0)?nc/dt:0;
+
+ //printf("--------ret=%d data[0]=%f nc=%d dt=%f\n",ret,rate,nc,dt);
+ PlotStripChart (p1, P1_DAQRATE, &rate, 1, 0, 0,VAL_FLOAT);
+ nc =0;
+ }
+ count= ret/lsize;
+ ctr[2]+=data[0];
+ ctr[3]+=count;
+ }
+ }
+ break;
+ }
+ return count*lsize;
+}
+
+int end() {
+
+ xxusb_register_write(WUSB_udev,1,0x0); // Stop DAQ mode
+ while (xxusb_usbfifo_read(WUSB_udev,(int *) stackdata,BUFF_L,30)>0);
+ printf("daq::end()\n");
+ return 0;
+}
+
+
+
+
+int vmacquire (int neve,FILE *fp, char *fpedname) {
+ // neve .... negative argument time ( in s )limited event loop
+ if (fThreshold== NULL) fThreshold = (int *) malloc (sizeof(int)*128);
+ // print welcome message
+ time_t t, told;
+ double tstart, tstop;
+ time(&t);
+ fThresholdEnable=0;
+
+#define BSIZE 10000
+ uint32_t data[10000];
+
+ fMode = 2;
+ fPedestal=255;
+ for (int ki=0; ki<128; ki++) {
+ if (ki<72) fThreshold[ki]=0;
+ else fThreshold[ki]=0x1<<8; // samo 4 kanali na zadnjem modulu so enablani
+ }
+ fThresholdEnable=0;
+ fStop=0;
+
+
+
+ if (fpedname !=NULL) {
+
+ FILE *fped=fopen(fpedname,"r");
+
+ int j=0;
+ int ndim=400;
+ char line[ndim];
+ int val=0;
+ while (fgets(line,ndim,fped)!=NULL) {
+ sscanf(line,"%d",&val);
+ fThreshold[j++]=val;
+ }
+ fThresholdEnable=1;
+ fclose(fped);
+
+ }
+
+
+
+ init();
+
+
+ int hdr[4]= {2}; // recid
+ int i=0;
+ int ntotal=0;
+ int counters[30]= {0,0,0,0,0, 0,0,0,0,0,0,0};
+ char names[10][20]= {"TRG","CAEN V965"};
+ time(&t);
+ tstart=Timer();
+ told =t;
+ tstop=tstart+360000;
+ if (neve<-1) {
+ tstop=tstart-neve;
+ neve=-1;
+ }
+ for (i=0; i!=neve && !ctrl_c && Timer()<tstop; i++) {
+ time(&t);
+ if (t!=told ) printf("%d in %2.2f min daq::event() %s\n",i, (double)(Timer()-tstart)/60., ctime(&t));
+ int nb=event(data,BSIZE, counters,t!=told);
+ analyse(nb, data);
+ if (nb>0) {
+ // zapis v datoteko
+ hdr[1]=nb+4*sizeof(int);
+ hdr[2]=time(NULL);
+ hdr[3]=i;
+
+ fwrite(hdr, 1, sizeof(int)*4, fp); //gzip
+ ntotal += fwrite(data,1, nb, fp);
+ told=t;
+ } else i--;
+ }
+
+ end();
+
+ printf("Number of Events: %d\n",i);
+ if (ctrl_c) printf("User Program termination CTRL-C\n");
+ if (Timer()>tstop ) printf("Timeout termination tstart# t>tstop: %d# %d >%d\n",(int)t, (int)tstart, (int) tstop);
+
+
+ printf("%d bytes written to file\nCounts:\n", (int) (ntotal*sizeof(int)));
+ for (i=0; i<2; i++) printf("%s\t%d\t%d\n",names[i],counters[2*i],counters[2*i+1]) ;
+
+
+ return 0;
+}
+