DRS4 Forum
  DRS4 Discussion Forum  Not logged in ELOG logo
Entry  Tue Jun 18 14:19:39 2013, Stefan Ritt, ROOT program to decode binary data from DRSOsc decode.Cc1.gif
    Reply  Wed Jul 30 17:05:06 2014, Stefan Ritt, ROOT program to decode binary data from DRSOsc read_binary.Cread_binary.cpp
Message ID: 262     Entry time: Tue Jun 18 14:19:39 2013     Reply to this: 361
Author: Stefan Ritt 
Subject: ROOT program to decode binary data from DRSOsc 

Please find attached a simple ROOT based program (http://root.cern.ch) to decode binary data from the DRSOsc program. It assumes that all four channels were recorded. If this is not the case, the program can be adjusted accordingly.

To use it, simply type (assuming that you have written a data file "test.dat" with DRSOsc):

root [0] .L decode.C+
Info in <TUnixSystem::ACLiC>: creating shared library /tmp/./decode_C.so
root [1] decode("test");
Info in <TCanvas::MakeDefCanvas>:  created default TCanvas with name c1
1927 events processed
"test.root" written
root [2] 

If you have turned on the clock on channel4 of the DRS4 evaluation board, it will produce a plot like this:
 
c1.gif 

 

/Stefan

Attachment 1: decode.C  2 kB  | Hide | Hide all
#include <string.h>
#include <stdio.h>
#include "TFile.h"
#include "TTree.h"
#include "TString.h"
#include <iostream>

struct Header_t {
   char           event_header[4];
   unsigned int   serial_number;
   unsigned short year;
   unsigned short month;
   unsigned short day;
   unsigned short hour;
   unsigned short minute;
   unsigned short second;
   unsigned short millisecond;
   unsigned short reserved1;
   float time[1024];
};

struct Waveform_t {
   char           chn1_header[4];
   unsigned short chn1[1024];
   char           chn2_header[4];
   unsigned short chn2[1024];
   char           chn3_header[4];
   unsigned short chn3[1024];
   char           chn4_header[4];
   unsigned short chn4[1024];
};

void decode(char *filename) {
   Header_t header;
   Waveform_t waveform;
   Double_t t[1024], chn1[1024], chn2[1024], chn3[1024], chn4[1024];
   Int_t n;

   // open the binary waveform file
   FILE *f = fopen(Form("%s.dat", filename), "r");

   //open the root file
   TFile *outfile = new TFile(Form("%s.root", filename), "RECREATE");
   
   // define the rec tree
   TTree *rec = new TTree("rec","rec");
   rec->Branch("t", &t   ,"t[1024]/D");  
   rec->Branch("chn1", &chn1 ,"chn1[1024]/D");
   rec->Branch("chn2", &chn2 ,"chn2[1024]/D");
   rec->Branch("chn3", &chn3 ,"chn3[1024]/D");
   rec->Branch("chn4", &chn4 ,"chn4[1024]/D");
  
   // loop over all events in data file
   for (n=0 ; fread(&header, sizeof(header), 1, f) > 0; n++) {

      // decode time      
      for (Int_t i=0; i<1024; i++)
         t[i] = (Double_t) header.time[i];
      
      fread(&waveform, sizeof(waveform), 1, f);
      
      // decode amplitudes in mV
      for (Int_t i=0; i<1024; i++) {
         chn1[i] = (Double_t) ((waveform.chn1[i]) / 65535. - 0.5) * 1000;   
         chn2[i] = (Double_t) ((waveform.chn2[i]) / 65535. - 0.5) * 1000;   
         chn3[i] = (Double_t) ((waveform.chn3[i]) / 65535. - 0.5) * 1000;   
         chn4[i] = (Double_t) ((waveform.chn4[i]) / 65535. - 0.5) * 1000;   
      }
      rec->Fill();
   }
   
   // draw channel #4
   rec->Draw("chn4:t");
   
   // print number of events
   cout<<n<<" events processed"<<endl;
   cout<<"\""<<Form("%s.root", filename)<<"\" written"<<endl;
   
   // save and close root file
   rec->Write();
   outfile->Close();
}
ELOG V3.1.4-80633ba