source: tools/matlabread/matlabread.cc@ 75

Last change on this file since 75 was 65, checked in by ogrimm, 15 years ago
Matlab interface for reading raw FACT data
File size: 2.8 KB
Line 
1// Function to read rawdata into Matlab
2//
3// Use 'mex matlabread.cc RawDataCTX.o' in Matlab to compile.
4// Use 'make' on console to generate RawDataCTX.o.
5
6#include <unistd.h>
7
8#include "/ihp/local/Matlab08/extern/include/mex.h"
9#include "../../drsdaq/RawDataCTX.h"
10
11// Print text date in temporary file, than clear file
12void PrintTempFile(FILE* File) {
13 char Buffer[1000];
14 rewind(File);
15 while(fgets(Buffer, sizeof(Buffer), File) != NULL) printf("%s", Buffer);
16 ftruncate(fileno(File),0);
17}
18
19// Interface function to Matlab
20void mexFunction (int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
21
22 RawDataCTX* RD = new RawDataCTX(true); // Instantiate without console output
23 FILE *Tmpfile = tmpfile(); // Temporary file for printing of headers
24
25 // Check inputs and output arguments
26 if (nrhs<1 || nrhs>2 || (nrhs==2 && !mxIsDouble(prhs[1]))) {
27 mexErrMsgTxt("Parameters must be filename and (optional) event number.");
28 }
29 if (nlhs != 2) mexErrMsgTxt ("Need two return values.");
30
31 switch (RD->OpenDataFile(mxArrayToString(prhs[0]), Tmpfile)) {
32 case CTX_FOPEN: mexErrMsgTxt("Could not open file.");
33 case CTX_RHEADER: mexErrMsgTxt("Could not read run header.");
34 case CTX_BSTRUCT: mexErrMsgTxt("Could not read board structures.");
35 }
36 if (RD->RHeader->MagicNum == MAGICNUM_OPEN) {
37 mexWarnMsgTxt("Magic number in run header indicates that the file has not "
38 "been closed properly.");
39 }
40 if (RD->RHeader->MagicNum == MAGICNUM_ERROR) {
41 mexWarnMsgTxt("Magic number in run header indicates that an error occurred "
42 "while writing the file.");
43 }
44
45 // ...some abbrevation for convenience
46 unsigned int Boards = RD->RHeader->NBoards;
47 unsigned int Channels = RD->RHeader->NChannels;
48 unsigned int Chips = RD->RHeader->NChips;
49 unsigned int Samples = RD->RHeader->Samples;
50
51 // Print header data
52 if(RD->ReadEvent(mxIsDouble(prhs[1]) ? (int) mxGetScalar(prhs[1]):0, Tmpfile) != CTX_OK) {
53 mexErrMsgTxt("Could not read event.");
54 }
55 else PrintTempFile(Tmpfile);
56
57 // Allocate memory
58 plhs[0] = mxCreateDoubleMatrix(Boards*Chips*Channels*Samples, 1, mxREAL);
59 plhs[1] = mxCreateDoubleMatrix(Chips, Boards, mxREAL);
60
61 // Fill trigger cell array
62 for(unsigned int i=0; i<Boards*Chips; i++) {
63 *(mxGetPr(plhs[1])+i) = *((int *) (RD->Data+i*sizeof(int)));
64 }
65
66 // Fill data array
67 unsigned int Count = 0;
68 for(unsigned int i=0; i<Boards; i++) {
69 for(unsigned int j=0; j<Chips*Channels*Samples; j++) {
70 *(mxGetPr(plhs[0])+Count) = (double) *((short *) (RD->Data+Boards*Chips*sizeof(int)) + Count++) * RD->BStruct[i].ScaleFactor;
71 }
72 }
73
74 // Resize data array into boards, channels and samples
75 mwSize Dimensions[] = {Samples, Chips*Channels, Boards};
76 mxSetDimensions(plhs[0], Dimensions, sizeof(Dimensions)/sizeof(mwSize));
77
78 // Clean up
79 fclose(Tmpfile);
80 RD->CloseDataFile();
81}
Note: See TracBrowser for help on using the repository browser.