Changeset 88 for tools/matlabread
- Timestamp:
- 07/13/09 09:50:05 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
tools/matlabread/matlabread.cc
r65 r88 1 1 // Function to read rawdata into Matlab 2 2 // 3 // Use 'make' on console to generate RawDataCTX.o. 3 4 // Use 'mex matlabread.cc RawDataCTX.o' in Matlab to compile. 4 // Use 'make' on console to generate RawDataCTX.o. 5 // 6 // Oliver Grimm, July 2009 7 5 8 6 9 #include <unistd.h> 10 #include <string.h> 7 11 8 12 #include "/ihp/local/Matlab08/extern/include/mex.h" 9 13 #include "../../drsdaq/RawDataCTX.h" 10 14 11 // Print text date in temporary file, than clear file12 void 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 15 19 16 // Interface function to Matlab … … 21 18 22 19 RawDataCTX* RD = new RawDataCTX(true); // Instantiate without console output 23 FILE *Tmpfile = tmpfile(); // Temporary file for printing of headers24 20 25 21 // 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.");22 if (nrhs<1 || nrhs>2 || (nrhs==2 && !mxIsDouble(prhs[1])) || nlhs<1 || nlhs>5) { 23 mexErrMsgTxt("Usage: [Data TrigCells RHeader BStructs EHeader] = matlabread('Filename', EventNo)"); 28 24 } 29 if (nlhs != 2) mexErrMsgTxt ("Need two return values.");30 25 31 switch (RD->OpenDataFile(mxArrayToString(prhs[0]), Tmpfile)) {26 switch (RD->OpenDataFile(mxArrayToString(prhs[0]), NULL)) { 32 27 case CTX_FOPEN: mexErrMsgTxt("Could not open file."); 33 28 case CTX_RHEADER: mexErrMsgTxt("Could not read run header."); … … 50 45 51 46 // Print header data 52 if(RD->ReadEvent(mxIsDouble(prhs[1]) ? (int) mxGetScalar(prhs[1]):0, Tmpfile) != CTX_OK) {47 if(RD->ReadEvent(mxIsDouble(prhs[1]) ? (int) mxGetScalar(prhs[1]):0, NULL) != CTX_OK) { 53 48 mexErrMsgTxt("Could not read event."); 54 49 } 55 else PrintTempFile(Tmpfile);56 50 57 // Allocate memory 51 while(true) { // Dummy loop to allow different number of return values (break will be issued depending on nlhs) 52 53 // ========= Data array ========== 54 58 55 plhs[0] = mxCreateDoubleMatrix(Boards*Chips*Channels*Samples, 1, mxREAL); 59 plhs[1] = mxCreateDoubleMatrix(Chips, Boards, mxREAL);60 61 // Fill trigger cell array62 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 array67 56 unsigned int Count = 0; 68 57 for(unsigned int i=0; i<Boards; i++) { … … 71 60 } 72 61 } 73 74 // Resize data array into boards, channels and samples 62 // Resize into boards, channels and samples array 75 63 mwSize Dimensions[] = {Samples, Chips*Channels, Boards}; 76 64 mxSetDimensions(plhs[0], Dimensions, sizeof(Dimensions)/sizeof(mwSize)); 65 66 if (nlhs==1) break; 67 68 // ========= Trigger cell array ========== 69 70 plhs[1] = mxCreateDoubleMatrix(Chips, Boards, mxREAL); 71 for(unsigned int i=0; i<Boards*Chips; i++) { 72 *(mxGetPr(plhs[1])+i) = *((int *) (RD->Data+i*sizeof(int))); 73 } 74 if (nlhs==2) break; 75 76 // ========= Run header structure ========== 77 78 const char *RH_Fields[] = {"MagicNum", "DataFormat", "RunHeaderSize", "EventHeaderSize", "BoardStructureSize", "SoftwareRevision", "Identification", "Type","RunNumber", "FileNumber", "Description", "NBoards", "NChips", "NChannels", "Samples", "Offset", "Events", "NBytes", "StartSecond", "StartMicrosecond", "EndSecond", "EndMicrosecond"}; 79 const mwSize RH_Dim[] = {1, 1}; 80 plhs[2] = mxCreateStructArray(sizeof(RH_Dim)/sizeof(mwSize), RH_Dim, sizeof(RH_Fields)/sizeof(char *), RH_Fields); 81 mxArray *Data[sizeof(RH_Fields)/sizeof(char *)]; 82 83 Data[0] = mxCreateNumericMatrix(1, 1, mxUINT32_CLASS, mxREAL); 84 memcpy(mxGetPr(Data[0]), &RD->RHeader->MagicNum, 4); 85 Data[1] = mxCreateNumericMatrix(1, 1, mxUINT32_CLASS, mxREAL); 86 memcpy(mxGetPr(Data[1]), &RD->RHeader->DataFormat, 4); 87 Data[2] = mxCreateNumericMatrix(1, 1, mxUINT32_CLASS, mxREAL); 88 memcpy(mxGetPr(Data[2]), &RD->RHeader->RunHeaderSize, 4); 89 Data[3] = mxCreateNumericMatrix(1, 1, mxUINT32_CLASS, mxREAL); 90 memcpy(mxGetPr(Data[3]), &RD->RHeader->EventHeaderSize, 4); 91 Data[4] = mxCreateNumericMatrix(1, 1, mxUINT32_CLASS, mxREAL); 92 memcpy(mxGetPr(Data[4]), &RD->RHeader->BoardStructureSize, 4); 93 Data[5] = mxCreateNumericMatrix(1, 1, mxINT32_CLASS, mxREAL); 94 memcpy(mxGetPr(Data[5]), &RD->RHeader->SoftwareRevision, 4); 95 Data[6] = mxCreateNumericMatrix(1, 1, mxUINT32_CLASS, mxREAL); 96 memcpy(mxGetPr(Data[6]), &RD->RHeader->Identification, 4); 97 Data[7] = mxCreateNumericMatrix(1, 1, mxUINT32_CLASS, mxREAL); 98 memcpy(mxGetPr(Data[7]), &RD->RHeader->Type, 4); 99 Data[8] = mxCreateNumericMatrix(1, 1, mxUINT32_CLASS, mxREAL); 100 memcpy(mxGetPr(Data[8]), &RD->RHeader->RunNumber, 4); 101 Data[9] = mxCreateNumericMatrix(1, 1, mxUINT32_CLASS, mxREAL); 102 memcpy(mxGetPr(Data[9]), &RD->RHeader->FileNumber, 4); 103 Data[10] = mxCreateString(RD->RHeader->Description); 104 Data[11] = mxCreateNumericMatrix(1, 1, mxUINT32_CLASS, mxREAL); 105 memcpy(mxGetPr(Data[11]), &RD->RHeader->NBoards, 4); 106 Data[12] = mxCreateNumericMatrix(1, 1, mxUINT32_CLASS, mxREAL); 107 memcpy(mxGetPr(Data[12]), &RD->RHeader->NChips, 4); 108 Data[13] = mxCreateNumericMatrix(1, 1, mxUINT32_CLASS, mxREAL); 109 memcpy(mxGetPr(Data[13]), &RD->RHeader->NChannels, 4); 110 Data[14] = mxCreateNumericMatrix(1, 1, mxUINT32_CLASS, mxREAL); 111 memcpy(mxGetPr(Data[14]), &RD->RHeader->Samples, 4); 112 Data[15] = mxCreateNumericMatrix(1, 1, mxUINT32_CLASS, mxREAL); 113 memcpy(mxGetPr(Data[15]), &RD->RHeader->Offset, 4); 114 Data[16] = mxCreateNumericMatrix(1, 1, mxUINT32_CLASS, mxREAL); 115 memcpy(mxGetPr(Data[16]), &RD->RHeader->Events, 4); 116 Data[17] = mxCreateNumericMatrix(1, 1, mxUINT32_CLASS, mxREAL); 117 memcpy(mxGetPr(Data[17]), &RD->RHeader->NBytes, 4); 118 Data[18] = mxCreateNumericMatrix(1, 1, mxUINT32_CLASS, mxREAL); 119 memcpy(mxGetPr(Data[18]), &RD->RHeader->StartSecond, 4); 120 Data[19] = mxCreateNumericMatrix(1, 1, mxUINT32_CLASS, mxREAL); 121 memcpy(mxGetPr(Data[19]), &RD->RHeader->StartMicrosecond, 4); 122 Data[20] = mxCreateNumericMatrix(1, 1, mxUINT32_CLASS, mxREAL); 123 memcpy(mxGetPr(Data[20]), &RD->RHeader->EndSecond, 4); 124 Data[21] = mxCreateNumericMatrix(1, 1, mxUINT32_CLASS, mxREAL); 125 memcpy(mxGetPr(Data[21]), &RD->RHeader->EndMicrosecond, 4); 126 127 for (int i=0; i<sizeof(RH_Fields)/sizeof(char *); i++) { 128 mxSetField(plhs[2], 0, RH_Fields[i], Data[i]); 129 } 130 131 if (nlhs==3) break; 132 133 // ========= Board structures ========== 134 135 const char *BS_Fields[] = {"SerialNo","NomFreq","BoardTemp","ScaleFactor"}; 136 const mwSize BS_Dim[] = {1, Boards}; 137 plhs[3] = mxCreateStructArray(sizeof(BS_Dim)/sizeof(mwSize), BS_Dim, sizeof(BS_Fields)/sizeof(char *), BS_Fields); 138 mxArray* (*BSData)[sizeof(BS_Fields)/sizeof(char *)] = new mxArray* [Boards][sizeof(BS_Fields)/sizeof(char *)]; 139 140 for (int j=0; j<Boards; j++) { 141 BSData[j][0] = mxCreateNumericMatrix(1, 1, mxUINT32_CLASS, mxREAL); 142 BSData[j][1] = mxCreateNumericMatrix(1, 1, mxSINGLE_CLASS, mxREAL); 143 BSData[j][2] = mxCreateNumericMatrix(1, 1, mxSINGLE_CLASS, mxREAL); 144 BSData[j][3] = mxCreateNumericMatrix(1, 1, mxSINGLE_CLASS, mxREAL); 145 146 memcpy(mxGetPr(BSData[j][0]), &RD->BStruct[j].SerialNo, 4); 147 memcpy(mxGetPr(BSData[j][1]), &RD->BStruct[j].NomFreq, 4); 148 memcpy(mxGetPr(BSData[j][2]), &RD->BStruct[j].BoardTemp, 4); 149 memcpy(mxGetPr(BSData[j][3]), &RD->BStruct[j].ScaleFactor, 4); 150 151 for (int i=0; i<sizeof(BS_Fields)/sizeof(char *); i++) { 152 mxSetField(plhs[3], j, BS_Fields[i], BSData[j][i]); 153 } 154 } 155 if (nlhs==4) break; 156 157 // ========= Event header structure ========== 158 159 const char *EH_Fields[] = {"EventNumber", "Second", "Microsecond", "TriggerType", "EventSize"}; 160 const mwSize EH_Dim[] = {1, 1}; 161 plhs[4] = mxCreateStructArray(sizeof(EH_Dim)/sizeof(mwSize), EH_Dim, sizeof(EH_Fields)/sizeof(char *), EH_Fields); 162 mxArray *EHData[sizeof(EH_Fields)/sizeof(char *)]; 163 164 for (int i=0; i<sizeof(EH_Fields)/sizeof(char *); i++) { 165 EHData[i] = mxCreateNumericMatrix(1, 1, mxUINT32_CLASS, mxREAL); 166 } 167 memcpy(mxGetPr(EHData[0]), &RD->EHeader->EventNumber, 4); 168 memcpy(mxGetPr(EHData[1]), &RD->EHeader->Second, 4); 169 memcpy(mxGetPr(EHData[2]), &RD->EHeader->Microsecond, 4); 170 memcpy(mxGetPr(EHData[3]), &RD->EHeader->TriggerType, 4); 171 memcpy(mxGetPr(EHData[4]), &RD->EHeader->EventSize, 4); 172 for (int i=0; i<sizeof(EH_Fields)/sizeof(char *); i++) { 173 mxSetField(plhs[4], 0, EH_Fields[i], EHData[i]); 174 } 175 if (nlhs>=5) break; 176 177 } // Dummy while loop 77 178 78 // Clean up 79 fclose(Tmpfile); 80 RD->CloseDataFile(); 179 180 delete RD; 81 181 }
Note:
See TracChangeset
for help on using the changeset viewer.