diff --git a/convertDRS4/README.txt b/convertDRS4/README.txt index db013ede3ecdd2b75f7bb50e7335d77425eacfe3..c6b143c13e5262e84689d64a4b18faeeb8578516 100644 --- a/convertDRS4/README.txt +++ b/convertDRS4/README.txt @@ -1 +1,8 @@ Application for convertation of binary files acquired by DRS4 to ROOT file. + +1. g++ -o read_binary read_binary.cpp -lm `root-config --cflags --libs` +This compiles read_binary.cpp file and makes object file read_binary. +2. ./read_binary ./data/rawDataDSR4/file_name.dat +With the help of the object file it is possible to convert binary file .dat to file .root (which we call raw /home/dariak/NeuRad_tests/data/rawDataDSR4) +3. read_root.C +This script reads raw file and make another file .root with simple analysis /home/dariak/NeuRad_tests/data/dataDSR4 diff --git a/convertDRS4/read_binary b/convertDRS4/read_binary new file mode 100755 index 0000000000000000000000000000000000000000..08c2bf85158446538a502c57962e2ccfa521ec3c Binary files /dev/null and b/convertDRS4/read_binary differ diff --git a/convertDRS4/read_binary.cpp b/convertDRS4/read_binary.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9edec3d01ab45a25a9d58432134f4a60939c456a --- /dev/null +++ b/convertDRS4/read_binary.cpp @@ -0,0 +1,321 @@ +/* + Name: read_binary.cpp + Created by: Stefan Ritt + Date: July 30th, 2014 + + Purpose: Example file to read binary data saved by DRSOsc. + + Compile and run it with: + + gcc -o read_binary read_binary.cpp + + ./read_binary + + This program assumes that a pulse from a signal generator is split + and fed into channels #1 and #2. It then calculates the time difference + between these two pulses to show the performance of the DRS board + for time measurements. + + $Id: read_binary.cpp 22290 2016-04-27 14:51:37Z ritt $ +*/ + +#include +#include +#include +#include +#include + +//for ROOT +#include "TROOT.h" +#include "TFile.h" +#include "TTree.h" +#include "TH1F.h" + +typedef struct { + char tag[3]; + char version; +} FHEADER; + +typedef struct { + char time_header[4]; +} THEADER; + +typedef struct { + char bn[2]; + unsigned short board_serial_number; +} BHEADER; + +typedef struct { + char event_header[4]; + unsigned int event_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 range; // range center in mV +} EHEADER; + +typedef struct { + char tc[2]; + unsigned short trigger_cell; +} TCHEADER; + +typedef struct { + char c[1]; + char cn[3]; +} CHEADER; + +/*-----------------------------------------------------------------------------*/ + +int main(int argc, const char * argv[]) +{ + FHEADER fh; + THEADER th; + BHEADER bh; + EHEADER eh; + TCHEADER tch; + CHEADER ch; + + unsigned int scaler; + unsigned short voltage[1024]; + double waveform[16][4][1024], time[16][4][1024]; + float bin_width[16][4][1024]; + int i, j, b, chn, n, chn_index, n_boards; + double t1, t2, t3, t4, dt, dt34; + char filename[256]; + + int ndt; + double threshold, sumdt, sumdt2; + + +//for ROOT + + TFile* rfile = new TFile("/home/dariak/NeuRad_tests/data/rawDataDSR4/NeuRad_test_08_2.root", "RECREATE"); + TTree* rtree = new TTree("rtree", "tree for drs4 analysis"); + //rtree->Branch("t1", &t1, "t1/D"); //br for time of threshold crossing signal in 1 ch + // rtree->Branch("t2", &t2, "t2/D"); //br for time of threshold crossing signal in 2 ch + int ncell; + const int ncellMax = 1030; + double amp_ch1[ncellMax], time_ch1[ncellMax]; //variable size array +//------for other channels +// double amp_ch2[ncellMax], time_ch2[ncellMax]; + + rtree->Branch("ncell", &ncell, "ncell/I"); + rtree->Branch("amp_ch1", amp_ch1, "amp_ch1[ncell]/D"); + rtree->Branch("time_ch1", time_ch1, "time_ch1[ncell]/D"); + +//------for other channels +// rtree->Branch("amp_ch2", amp_ch2, "amp_ch2[ncell]/D"); +// rtree->Branch("time_ch2", time_ch2, "time_ch2[ncell]/D"); + + if (argc > 1) + strcpy(filename, argv[1]); + else { + printf("Usage: read_binary \n"); + return 0; + } + + // open the binary waveform file + FILE *f = fopen(filename, "r"); + if (f == NULL) { + printf("Cannot find file \'%s\'\n", filename); + return 0; + } + + // read file header + fread(&fh, sizeof(fh), 1, f); + if (fh.tag[0] != 'D' || fh.tag[1] != 'R' || fh.tag[2] != 'S') { + printf("Found invalid file header in file \'%s\', aborting.\n", filename); + return 0; + } + + if (fh.version != '2') { + printf("Found invalid file version \'%c\' in file \'%s\', should be \'2\', aborting.\n", fh.version, filename); + return 0; + } + + // read time header + fread(&th, sizeof(th), 1, f); + if (memcmp(th.time_header, "TIME", 4) != 0) { + printf("Invalid time header in file \'%s\', aborting.\n", filename); + return 0; + } + + for (b = 0 ; ; b++) { + // read board header + fread(&bh, sizeof(bh), 1, f); + if (memcmp(bh.bn, "B#", 2) != 0) { + // probably event header found + fseek(f, -4, SEEK_CUR); + break; + } + + printf("Found data for board #%d\n", bh.board_serial_number); + + // read time bin widths + memset(bin_width[b], sizeof(bin_width[0]), 0); + for (chn=0 ; chn<5 ; chn++) { + fread(&ch, sizeof(ch), 1, f); + if (ch.c[0] != 'C') { + // event header found + fseek(f, -4, SEEK_CUR); + break; + } + i = ch.cn[2] - '0' - 1; + printf("Found timing calibration for channel #%d\n", i+1); + fread(&bin_width[b][i][0], sizeof(float), 1024, f); + /*my printf + printf("bin width %d \n", bin_width[b][i][10]); */ + // fix for 2048 bin mode: double channel + if (bin_width[b][i][1023] > 10 || bin_width[b][i][1023] < 0.01) { + for (j=0 ; j<512 ; j++) + bin_width[b][i][j+512] = bin_width[b][i][j]; + /*my printf + printf("bin width %d \n", bin_width[b][i][j+512]); */ + } + } + } + n_boards = b; + + // initialize statistics + ndt = 0; + sumdt = sumdt2 = 0; + + // loop over all events in the data file + for (n=0 ; ; n++) { + // read event header + i = (int)fread(&eh, sizeof(eh), 1, f); + if (i < 1) + break; + + if ( !(eh.event_serial_number%100) ) { + printf("Found event #%d\n", eh.event_serial_number); + } + + // loop over all boards in data file + for (b=0 ; b 1) + printf("Found data for board #%d\n", bh.board_serial_number); + + // reach channel data + for (chn=0 ; chn<4 ; chn++) { + + // read channel header + fread(&ch, sizeof(ch), 1, f); + if (ch.c[0] != 'C') { + // event header found + fseek(f, -4, SEEK_CUR); + break; + } + chn_index = ch.cn[2] - '0' - 1; + // printf("print channel %d \n",chn_index); + fread(&scaler, sizeof(int), 1, f); + fread(voltage, sizeof(short), 1024, f); + + for (i=0 ; i<1024 ; i++) { + // convert data to volts + waveform[b][chn_index][i] = (voltage[i] / 65536. + eh.range/1000.0 - 0.5); //calculation of amplitudes values for each cell + + //for ROOT + ncell = i; + if(chn_index == 0) {amp_ch1[i] = waveform[b][chn_index][i];} + // if(chn_index == 1) {amp_ch2[i] = waveform[b][chn_index][i];} + + // calculate time for this cell + for (j=0,time[b][chn_index][i]=0 ; j= threshold) { + t1 = (threshold-waveform[b][0][i])/(waveform[b][0][i+1]-waveform[b][0][i])*(time[b][0][i+1]-time[b][0][i])+time[b][0][i]; + //my prinf + //printf("t1 recalc %1.6lf %d \n",t1, i); + break; + } + + } + + // find peak in channel 2 above threshold + for (i=0 ; i<1022 ; i++) { + if (waveform[b][1][i] < threshold && waveform[b][1][i+1] >= threshold) { + t2 = (threshold-waveform[b][1][i])/(waveform[b][1][i+1]-waveform[b][1][i])*(time[b][1][i+1]-time[b][1][i])+time[b][1][i]; + //my prinf + //printf("t2 recalc %1.6lf \n",t2); + break; + } + } + + // calculate distance of peaks with statistics + if (t1 > 0 && t2 > 0) { + ndt++; + dt = t2 - t1; + sumdt += dt; + sumdt2 += dt*dt; + } + } //end of the boards loop + + rtree->Fill(); + } // end of the events loop + + // print statistics + printf("dT = %1.3lfns +- %1.1lfps\n", sumdt/ndt, 1000*sqrt(1.0/(ndt-1)*(sumdt2-1.0/ndt*sumdt*sumdt))); + + rfile->Write(); + rfile->Close(); + return 1; +} + diff --git a/convertDRS4/read_root.C b/convertDRS4/read_root.C new file mode 100644 index 0000000000000000000000000000000000000000..ca691829030c073a0fd3f87cc84dd4c0dc6b2b1b --- /dev/null +++ b/convertDRS4/read_root.C @@ -0,0 +1,90 @@ +void read_root() +{ + TFile *f = new TFile("/home/dariak/NeuRad_tests/data/rawDataDSR4/NeuRad_test_07_1.root"); + TTree *tr = (TTree*)f->Get("rtree"); + tr->SetMakeClass(1); + +// TArrayD ampl1; +// TArrayD time1; +// ampl1.Set(ncellMax); +// time1.Set(ncellMax); + + const int ncellMax = 1030; + Double_t ampl1[ncellMax]; + Double_t time1[ncellMax]; + + TFile *fw = new TFile("/home/dariak/NeuRad_tests/data/dataDSR4/analysis_07_1.root", "RECREATE"); + TTree *tw = new TTree("drs4analysis", "title of drs4 analysis tree"); + Double_t minAmplitude1; + tw->Branch("minAmplitude1", &minAmplitude1, "minAmplitude1/D"); +/* Double_t ampl1[1024]; + Double_t time1[1024];*/ + + tr->SetBranchAddress("amp_ch1", ampl1); + tr->SetBranchAddress("time_ch1", time1); + + const Long64_t nEntries = tr->GetEntries(); + cout <<"Number of entries: "<Divide(3,4); + + for(Int_t i=12; i<24; i++) { + c1->cd(i-11); + tr->GetEntry(i); + TGraph *gr1 = new TGraph(1023, time1, ampl1); + gr1->SetTitle("Signal shape for one event"); + gr1->GetXaxis(); + gr1->Draw("Al*"); + } + + tr->GetEntry(428); + TGraph *gr1 = new TGraph(1023, time1, ampl1); + gr1->SetTitle("Signal shape for one event"); + gr1->Draw("Al*");*/ +//----- + Double_t minAmpl1 = 0.; + + for(Int_t i=0; iGetEntries(); i++) { + tr->GetEntry(i); + minAmpl1 = ampl1[0]; + + //find minimum by hand + for(Int_t j=0; j<1023; j++) { + //cout< 0.9*minAmpl1) { //we have negative signals, amplitude between 10 and 90 proc from minimum + /*cout<<"rjgnfre"<SetTitle("stupido"); + gr2->Fit("pol1"); + gr2->Draw("Al*");*/ + + } + } + //getting integral + + TGraph *gr3 = new TGraph(1023, time1, ampl1); + //for(Int_t k=120; k<180; k++) { + gr3->Integral(120,180); + cout<<"INtegral "<Integral(120,180)<Fill(); + } + fw->cd(); + tw->Write(); + fw->Close(); +} diff --git a/data/dataDSR4/analysis_07_1.root b/data/dataDSR4/analysis_07_1.root new file mode 100644 index 0000000000000000000000000000000000000000..7c495ba3f79138280dc4643197aefff4dd0f0c10 Binary files /dev/null and b/data/dataDSR4/analysis_07_1.root differ diff --git a/data/rawDataDSR4/NeuRad_test_071216_1.dat b/data/rawDataDSR4/NeuRad_test_071216_1.dat new file mode 100644 index 0000000000000000000000000000000000000000..02c9e0faf8aae0d12cd7a63db69be9def9026de1 Binary files /dev/null and b/data/rawDataDSR4/NeuRad_test_071216_1.dat differ diff --git a/data/rawDataDSR4/NeuRad_test_07_1.root b/data/rawDataDSR4/NeuRad_test_07_1.root new file mode 100644 index 0000000000000000000000000000000000000000..7bd82a1afe26dec229f4170f10b2a1fd7abd1aba Binary files /dev/null and b/data/rawDataDSR4/NeuRad_test_07_1.root differ