Changeset 88 for tools/matlabread


Ignore:
Timestamp:
07/13/09 09:50:05 (15 years ago)
Author:
ogrimm
Message:
Headers now available in Matlab
File:
1 edited

Legend:

Unmodified
Added
Removed
  • tools/matlabread/matlabread.cc

    r65 r88  
    11// Function to read rawdata into Matlab
    22//
     3// Use 'make' on console to generate RawDataCTX.o.
    34// 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
    58
    69#include <unistd.h>
     10#include <string.h>
    711
    812#include "/ihp/local/Matlab08/extern/include/mex.h"
    913#include "../../drsdaq/RawDataCTX.h"
    1014
    11 // Print text date in temporary file, than clear file
    12 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 }
    1815
    1916// Interface function to Matlab
     
    2118 
    2219  RawDataCTX* RD = new RawDataCTX(true);        // Instantiate without console output
    23   FILE *Tmpfile = tmpfile();                    // Temporary file for printing of headers
    2420
    2521  // 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)");
    2824  }
    29   if (nlhs != 2) mexErrMsgTxt ("Need two return values.");
    3025         
    31   switch (RD->OpenDataFile(mxArrayToString(prhs[0]), Tmpfile)) {
     26  switch (RD->OpenDataFile(mxArrayToString(prhs[0]), NULL)) {
    3227    case CTX_FOPEN:   mexErrMsgTxt("Could not open file.");
    3328    case CTX_RHEADER: mexErrMsgTxt("Could not read run header.");
     
    5045 
    5146  // 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) {
    5348    mexErrMsgTxt("Could not read event.");
    5449  }
    55   else PrintTempFile(Tmpfile);
    5650
    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 
    5855  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
    6756  unsigned int Count = 0;
    6857  for(unsigned int i=0; i<Boards; i++) {
     
    7160    }
    7261  }
    73 
    74   // Resize data array into boards, channels and samples
     62  // Resize into boards, channels and samples array
    7563  mwSize Dimensions[] = {Samples, Chips*Channels, Boards};
    7664  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
    77178 
    78   // Clean up
    79   fclose(Tmpfile);
    80   RD->CloseDataFile();     
     179 
     180  delete RD;   
    81181}
Note: See TracChangeset for help on using the changeset viewer.