TPCCLIB
Loading...
Searching...
No Matches
ecatmatrixlist.c File Reference

Functions for processing ECAT matrix list. More...

#include "tpcclibConfig.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <string.h>
#include "tpcextensions.h"
#include "tpcift.h"
#include "tpcecat.h"

Go to the source code of this file.

Functions

void ecatMListInit (ECAT_MATRIXLIST *ml)
 
void ecatMListFree (ECAT_MATRIXLIST *ml)
 
void ecatMListReadId (int format, unsigned int id, ECAT_MATVAL *mv)
 
unsigned int ecatMListMakeId (int format, unsigned int frame, unsigned int plane, unsigned int gate, unsigned int data, unsigned int bed)
 
unsigned int ecatMValToId (int format, ECAT_MATVAL *mv)
 
int ecatMListRead (int format, FILE *fp, ECAT_MATRIXLIST *ml, TPCSTATUS *status)
 
void ecatMListPrint (int format, ECAT_MATRIXLIST *ml, FILE *fp)
 

Detailed Description

Functions for processing ECAT matrix list.

Definition in file ecatmatrixlist.c.

Function Documentation

◆ ecatMListFree()

void ecatMListFree ( ECAT_MATRIXLIST * ml)

Free memory allocated for ECAT matrix list. All contents are destroyed.

Precondition
Before first use initialize the struct with ecatMListInit().
See also
ecatMListInit
Parameters
mlPointer to matrix list.

Definition at line 38 of file ecatmatrixlist.c.

41 {
42 if(ml==NULL) return;
43 if(ml->matrixSpace>0) free((char*)(ml->matdir));
44 ml->matrixSpace=ml->matrixNr=0;
45}
ECAT_MATDIR * matdir
Definition tpcecat.h:58
unsigned int matrixNr
Definition tpcecat.h:54
unsigned int matrixSpace
Definition tpcecat.h:56

Referenced by ecatMListRead().

◆ ecatMListInit()

void ecatMListInit ( ECAT_MATRIXLIST * ml)

Initiate the ECAT matrix list struct before any use.

See also
ecatMListFree, ecatMListRead, ecatMListPrint
Parameters
mlPointer to matrix list.

Definition at line 23 of file ecatmatrixlist.c.

26 {
27 if(ml==NULL) return;
28 ml->matdir=NULL;
29 ml->matrixSpace=ml->matrixNr=0;
30}

◆ ecatMListMakeId()

unsigned int ecatMListMakeId ( int format,
unsigned int frame,
unsigned int plane,
unsigned int gate,
unsigned int data,
unsigned int bed )

Make matrix identifier (matnum) code for specific matrix.

See also
ecatMListReadId, ecatMValToId
Returns
The matrix identifier.
Parameters
formatECAT format: 6 or 7.
frameFrame [0..511] for ECAT7, [0..4095] for ECAT6.
planePlane [0..1023] for ECAT7, [0..255] for ECAT6.
gateGate [0..63].
dataData [0..7] for ECAT7, [0..3] for ECAT6.
bedBed position [0..15].

Definition at line 84 of file ecatmatrixlist.c.

97 {
98 if(format==7) {
99 return(
100 (frame & 0x1FF) | /* frame */
101 ((plane & 0x300) << 1) | /* plane high */
102 ((data & 0x4) << 9) | /* data high */
103 ((bed & 0xF) << 12) | /* bed */
104 ((plane & 0xFF) << 16) | /* plane low */
105 ((gate & 0x3F) << 24) | /* gate */
106 ((data & 0x3) << 30) /* data low */
107 );
108 } else if(format==6) {
109 return(
110 (frame&0xFFF) |
111 ((bed&0xF)<<12) |
112 ((plane&0xFF)<<16) |
113 ((gate&0x3F)<<24) |
114 ((data&0x3)<<30)
115 );
116 }
117 return(0);
118}

Referenced by ecatMValToId().

◆ ecatMListPrint()

void ecatMListPrint ( int format,
ECAT_MATRIXLIST * ml,
FILE * fp )

Print ECAT matrix list.

See also
ecatMListInit, ecatMListRead, ecatMListFree
Parameters
formatECAT format: 6 or 7.
mlPointer to matrix list to print.
fpFile pointer to print to; usually stdout.

Definition at line 250 of file ecatmatrixlist.c.

257 {
258 if(ml==NULL || fp==NULL) return;
259 ECAT_MATVAL mv;
260
261 fprintf(fp, "nr\tmatrix\tpl\tfr\tgate\tbed\tstartblk\tblknr\n");
262 for(unsigned int i=0; i<ml->matrixNr; i++) {
263 ecatMListReadId(format, ml->matdir[i].id, &mv);
264 fprintf(fp, "%u\t%u\t%u\t%u\t%u\t%u\t%u\t%u\n", i+1, ml->matdir[i].id,
265 mv.plane, mv.frame, mv.gate, mv.bed,
266 ml->matdir[i].strtblk, 1+ml->matdir[i].endblk-ml->matdir[i].strtblk);
267 }
268 return;
269}
void ecatMListReadId(int format, unsigned int id, ECAT_MATVAL *mv)
unsigned int endblk
Definition tpcecat.h:46
unsigned int id
Definition tpcecat.h:42
unsigned int strtblk
Definition tpcecat.h:44
unsigned int gate
Definition tpcecat.h:68
unsigned int plane
Definition tpcecat.h:66
unsigned int frame
Definition tpcecat.h:64
unsigned int bed
Definition tpcecat.h:72

◆ ecatMListRead()

int ecatMListRead ( int format,
FILE * fp,
ECAT_MATRIXLIST * ml,
TPCSTATUS * status )

Read ECAT matrix list from ECAT file.

See also
ecatMListInit, ecatMListPrint, ecatMListFree
Returns
enum tpcerror (TPCERROR_OK when successful).
Parameters
formatECAT format: 6 or 7.
fpFile pointer of file, opened with fp=fopen(filename, "rb"), to read the matrix list from.
mlPointer to initiated matrix list; any previous content is deleted.
Precondition
Matrix list must be initiated (once) before calling this.
Parameters
statusPointer to status data; enter NULL if not needed.

Definition at line 140 of file ecatmatrixlist.c.

150 {
151 int verbose=0; if(status!=NULL) verbose=status->verbose;
152 if(verbose>0) {printf("%s(%d, ...)\n", __func__, format); fflush(stdout);}
153
154 if(fp==NULL || ml==NULL) {
155 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_FAIL);
156 return(TPCERROR_FAIL);
157 }
158
159 /* Is current platform little endian (1) or not (0) ? */
160 int little=endianLittle();
161 if(verbose>2) {
162 if(little) printf("little endian platform\n"); else printf("big endian platform\n");
163 }
164
165 /* Make sure that matrix list is empty */
166 ecatMListFree(ml);
167
168 /* Read the blocks belonging to the matrix list */
169 unsigned int blk=2; // Matrix list starts at block 2
170 unsigned int blkNext=0, blkPrev=0, nrFree=0, nrUsed=0;
171 unsigned int dirbuf[ECATBLKSIZE/4];
172 do {
173 /* Read the data block */
174 if(verbose>1) printf(" reading block %u\n", blk);
175 int ret=ecatReadBlock(NULL, fp, blk, (unsigned char*)dirbuf);
176 if(ret!=TPCERROR_OK) {
177 if(verbose>0) fprintf(stderr, "Error: cannot read block %u\n", blk);
178 ecatMListFree(ml);
179 statusSet(status, __func__, __FILE__, __LINE__, ret);
180 return(ret);
181 }
182 /* Byte order conversion for integers in little/big endian platforms */
183 if(little && format==7) swawbip(dirbuf, ECATBLKSIZE);
184 if(!little && format==6) swawbip(dirbuf, ECATBLKSIZE);
185 /* Read the "header" integers from the block */
186 nrFree = dirbuf[0];
187 blkNext = dirbuf[1];
188 blkPrev = dirbuf[2];
189 nrUsed = dirbuf[3];
190 if(verbose>2)
191 printf("nrFree=%u blkNext=%u blkPrev=%u nrUsed=%u\n", nrFree, blkNext, blkPrev, nrUsed);
192 /* Allocate (more) memory for the matrix list */
193 if(ml->matrixSpace==0) {
195 ml->matdir=(ECAT_MATDIR*)malloc(ml->matrixSpace*sizeof(ECAT_MATDIR));
196 } else if(ml->matrixSpace<(ml->matrixNr+ECATBLKSIZE/4)) {
198 ml->matdir=(ECAT_MATDIR*)realloc(ml->matdir, sizeof(ECAT_MATDIR)*ml->matrixSpace);
199 }
200 if(ml->matdir==NULL) {
201 ecatMListFree(ml);
202 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OUT_OF_MEMORY);
204 }
205 /* Add the content of this block to the matrix list */
206 for(unsigned int i=4; i<ECATBLKSIZE/4; i+=4) if(dirbuf[i]>0) {
207 ml->matdir[ml->matrixNr].id=dirbuf[i];
208 ml->matdir[ml->matrixNr].strtblk=dirbuf[i+1];
209 ml->matdir[ml->matrixNr].endblk=dirbuf[i+2];
210 ml->matdir[ml->matrixNr].status=dirbuf[i+3];
211 if(verbose>3) {
212 printf("matnum=%u strtblk=%u endblk=%u matstat=%u matrixNr=%u\n",
213 ml->matdir[ml->matrixNr].id, ml->matdir[ml->matrixNr].strtblk,
214 ml->matdir[ml->matrixNr].endblk, ml->matdir[ml->matrixNr].status,
215 ml->matrixNr);
216 }
217 /* verify that referred data block can be found in the file */
218 fpos_t current_fp; fgetpos(fp, &current_fp); // save current file position
219 int ret=fseeko(fp, (off_t)(ml->matdir[ml->matrixNr].endblk-1)*ECATBLKSIZE, SEEK_SET);
220 fsetpos(fp, &current_fp); // back to saved file position
221 if(ret==0) { // end block can be found
222 ml->matrixNr++; // save this matrix into the list
223 } else { // not found, file probably broken
224 if(verbose>0) {
225 printf("matnum %d points to data outside of file.\n", ml->matdir[ml->matrixNr].id);
226 fflush(stdout);
227 }
228 // matrixNr not incremented, thus this matrix is left out of the list
229 }
230 }
231 blk=blkNext;
232 } while(feof(fp)==0 && blk>2);
233
234 if(ml->matrixNr==0) {
235 ecatMListFree(ml);
236 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_NO_DATA);
237 return(TPCERROR_NO_DATA);
238 }
239 if(verbose>1) printf(" matrixNr := %u\n", ml->matrixNr);
240
241 statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
242 return(TPCERROR_OK);
243}
int ecatReadBlock(const char *filename, FILE *fp, const unsigned int blocknumber, unsigned char *data)
Definition ecatio.c:23
void ecatMListFree(ECAT_MATRIXLIST *ml)
int endianLittle()
Definition endian.c:53
void swawbip(void *buf, int size)
Definition endian.c:138
void statusSet(TPCSTATUS *s, const char *func, const char *srcfile, int srcline, tpcerror error)
Definition statusmsg.c:142
int status
Definition tpcecat.h:48
int verbose
Verbose level, used by statusPrint() etc.
#define ECATBLKSIZE
Definition tpcecat.h:37
@ TPCERROR_FAIL
General error.
@ TPCERROR_OUT_OF_MEMORY
Cannot allocate memory.
@ TPCERROR_OK
No error.
@ TPCERROR_NO_DATA
File contains no data.

◆ ecatMListReadId()

void ecatMListReadId ( int format,
unsigned int id,
ECAT_MATVAL * mv )

Read matrix identifier code (matnum) into matrix value structure.

See also
ecatMListMakeId, ecatMValToId, ecatMListPrint
Parameters
formatECAT format: 6 or 7.
idMatrix identifier code (matnum) to decode.
mvPointer to data structure in where frame, plane etc numbers are written.

Definition at line 52 of file ecatmatrixlist.c.

59 {
60 if(mv==NULL) return;
61 mv->frame=mv->plane=mv->gate=mv->data=mv->bed=0;
62 if(format==7) {
63 mv->frame = id & 0x1FF;
64 mv->plane = ((id >> 16) & 0xFF) + ((id >> 1) & 0x300);
65 mv->gate = (id >> 24) & 0x3F;
66 mv->data = ((id >> 30) & 0x3) + ((id >> 9) & 0x4);
67 mv->bed = (id >> 12) & 0xF;
68 } else if(format==6) {
69 mv->frame = id&0xFFF;
70 mv->plane = (id>>16)&0xFF;
71 mv->gate = (id>>24)&0x3F;
72 mv->data = (id>>30)&0x3;
73 mv->bed = (id>>12)&0xF;
74 }
75 return;
76}
unsigned int data
Definition tpcecat.h:70

Referenced by ecatMListPrint().

◆ ecatMValToId()

unsigned int ecatMValToId ( int format,
ECAT_MATVAL * mv )

Make matrix identifier (matnum) code for specific matrix.

See also
ecatMListReadId, ecatMListMakeId
Returns
The matrix identifier.
Parameters
formatECAT format: 6 or 7.
mvPointer to data structure from where frame, plane etc numbers are read.

Definition at line 124 of file ecatmatrixlist.c.

129 {
130 if(mv==NULL) return(0);
131 return(ecatMListMakeId(format, mv->frame, mv->plane, mv->gate, mv->data, mv->bed));
132}
unsigned int ecatMListMakeId(int format, unsigned int frame, unsigned int plane, unsigned int gate, unsigned int data, unsigned int bed)