#include "TFile.h"
#include "TH1.h"
#include "TH2.h"
#include "TFile.h"
#include "TGraph.h"
#include "TCanvas.h"
#include "TStyle.h"
#include "TLegend.h"
#include "TFile.h"
//#include "TROOT.h"
#include <stdio.h>
#include <time.h>
#define DATA_PATH "./data/"
#define CFRAC 0.4
#define DWIDTH 1024
#define NCH 4
#include "drs.h"
ClassImp (drs)
TCanvas *c_wfm;
TFile *froot;
TH1F *h1_chmax[NCH];
TH1F *h1_chmaxt[NCH];
TH1F *h1_chmin[NCH];
TH1F *h1_chmint[NCH];
TH1F *h1_letime[NCH];
TH1F *h1_cftime[NCH];
TH1F *h1_cftdif[NCH];
TH1F *h1_qdc[NCH];
TH2F *h2_dpo[NCH];
TH2F *h2_tdcadc[NCH];
TH2F *h2_ctdcadc[NCH];
TH2F *h2_ctalk;
TH2F *h2_pos;
TH1F *h1_qsumx;
TH1F *h1_qsumy;
TGraph *gr_wfm[NCH];
TLegend *leg;
int drs::histoinit( int nch, DrsChannel* d, int trgch, double ymin, double ymax){
char histname[128];
c_wfm= new TCanvas("c_wfm","WFM",50,50,700,700);
c_wfm->Divide(1,1);
c_wfm->cd(1);
const double maxt=1800;
leg = new TLegend(0.8, 0.8, 1, 1);
for (int i=0; i<nch; i++) {
h1_chmax[i]=new TH1F(histname,histname,1500,ymin,ymax);
h1_chmaxt[i]=new TH1F(histname,histname,DWIDTH,-0.1,maxt);
h1_chmin[i]=new TH1F(histname,histname,1500,ymin,ymax);
h1_chmint[i]=new TH1F(histname,histname,DWIDTH,-0.1,maxt);
h1_letime[i]=new TH1F(histname,histname,500,d[i].twin[0],d[i].twin[1]);
h1_cftime[i]=new TH1F(histname,histname,500,d[i].twin[0],d[i].twin[1]);
h1_cftdif[i]=new TH1F(histname,histname,2000,d[i].twin[0]-d[trgch].twin[0]-40,d[i].twin[1]-d[trgch].twin[0]+20);
h1_qdc[i]=new TH1F(histname,histname,500,-4.5,4.5);
h2_dpo[i]=new TH2F(histname,histname,DWIDTH,-0.1,maxt,1500,ymin,ymax);
h2_tdcadc[i]=new TH2F(histname,histname,250,ymin,ymax,400,d[i].twin[0]-d[trgch].twin[0],d[i].twin[1]-d[trgch].twin[0]);
sprintf(histname
,"ctdcadc_%02d",i
);
h2_ctdcadc[i]=new TH2F(histname,histname,250,ymin,ymax,400,d[i].twin[0]-d[trgch].twin[0],d[i].twin[1]-d[trgch].twin[0]);
gr_wfm[i]=new TGraph(DWIDTH);
gr_wfm[i]->SetMinimum(ymin);
gr_wfm[i]->SetMaximum(ymax);
gr_wfm[i]->SetLineColor(i+1);
gr_wfm[i]->SetMarkerColor(i+1);
leg->AddEntry(gr_wfm[i], histname, "l");
if (i) gr_wfm[i]->Draw("LP"); else gr_wfm[i]->Draw("ALP");
gr_wfm[i]->GetHistogram()->SetTitle("WaveForms");
gr_wfm[i]->GetHistogram()->SetDirectory(0);
}
leg->Draw();
h2_ctalk=new TH2F("ctalk","ctalk",250,0.,0.5,250,0.,0.5);
h2_pos=new TH2F("cpos","cpos",40,-1,1,40,-1,1);
h1_qsumx=new TH1F("qsumx","qsumx",100,-1,6);
h1_qsumy=new TH1F("qsumy","qsumy",100,-1,6);
return 0;
}
drs::drs( const char *FileName, int nch, DrsChannel* d, int trgch,int first, int neve, int updfrq,double ymin, double ymax)
{
int debug=0;
int i,ich,iev;
int stat;
int nev = first + neve;
// char FileName[300]="./data/n2.dat";
FILE *fin;
char recid[5]="SAMO";
unsigned short *srecid = (unsigned short *) &recid[0];
struct evrec {
unsigned long evn;
unsigned short tYear,tMonth,tDay,tHour,tMin,tSec,tmSec,rc;
} evrec;
float dtdata[NCH][DWIDTH];
float tcdata[NCH][DWIDTH];
float tccalib[NCH];
float scaler[NCH];
float *t;
float *y;
float *ay;
short lwfm[NCH];
int nwfm[NCH];
int board;
int version;
unsigned short ichdat[NCH][DWIDTH];
float chdat[NCH][DWIDTH];
float achdat[NCH][DWIDTH];
float chmax[NCH][2];
float chmin[NCH][2];
float letime[NCH],cftime[NCH],qdc[NCH];
// gROOT->Reset();
gStyle->SetOptStat(1111111);
// gStyle->SetPalette(52);
histoinit(nch, d, trgch, ymin,ymax);
char rootname[0xff];
sprintf(rootname
, "%s_x%d_y%d.root", FileName
, 0, 0);
froot = new TFile(rootname,"RECREATE");
fin
=fopen(FileName
,"rb");
if (fin==NULL) {
printf("Error opening file %s\n",FileName
);
return;
}
printf("%s, %lu\n",recid
,sizeof(evrec
));
for (ich=0; ich<nch; ich++) {
nwfm[ich]=0;
for (i=0; i<DWIDTH; i++) {
achdat[ich][i]=0;
}
}
unsigned int trgcell = 0;
int event = -1;
iev=0;
time_t t0,told;
told=t0-1;
int posrec[9];
if (iev==nev) break;
stat
=fread(recid
,1,4,fin
);
if (debug) {
if (recid
[1]=='#') printf("%c%c%u\n",recid
[0],recid
[1],srecid
[1]);
}
switch (recid[0]) {
case 'P':{ // position record
int len;
stat
=fread(&len
,1,4,fin
);
if (len>0 && len<37) {
stat
=fread(posrec
,1,len
,fin
);
if (recid[3]=='R'){
if (iev>=first){
froot->Write();
froot->Close();
sprintf(rootname
, "%s_x%d_y%d.root", FileName
, posrec
[3], posrec
[4]);
froot = new TFile(rootname,"RECREATE");
histoinit(nch,d, trgch, ymin,ymax);
}
printf("Position len=%d x=%d y=%d ix=%d iy=%d\n", len
, posrec
[0], posrec
[1], posrec
[3], posrec
[4]);
} else
printf("Position record buffer length %d\n", len
);
} else {
printf("Wrong buffer length %d\n", len
);
}
break;
}
case 'D': // DRS
version
= atoi(&recid
[3]);
break;
case 'T': // TIME
switch (recid[1]) {
case 'I':
event=0;
if (version ==0) version++;
break;
case '#': trgcell = srecid[1]; break;
}
break;
case 'B':
board = srecid[1];
break;
case 'C':
ich=ich-1;
if (event) {
if (version
>1) stat
=fread(&scaler
[ich
],1,sizeof(float),fin
);
stat
=fread(ichdat
[ich
],1,sizeof(ichdat
[ich
]),fin
);
} else {
stat
=fread(dtdata
[ich
],1,sizeof(float)*DWIDTH
,fin
);
}
break;
case 'E':
stat
=fread(&evrec.
evn,1,sizeof(evrec
),fin
);
iev++;
if (!version) {
stat
=fread(dtdata
[0],1,sizeof(float)*DWIDTH
,fin
);
}
event=1;
break;
default:
printf("Unknown header! %s\n", recid
);
break;
}
if (iev<first) continue;
if (recid[0] == 'E') {
for ( i=0; i<nch; i++) {
if (!lwfm[i]) continue;
double pulseheight = (d[i].edge)? chmax[i][0]: chmin[i][0];
if ((pulseheight >d[i].vcut[0])&&
(pulseheight <d[i].vcut[1]) ) {
h1_cftdif[i]->Fill(cftime[i]-cftime[trgch]);
}
if (!d[i].edge) pulseheight = - pulseheight;
h2_tdcadc[i] ->Fill(pulseheight,letime[i]-cftime[trgch]);
h2_ctdcadc[i]->Fill(pulseheight,cftime[i]-cftime[trgch]);
//if (i==1) printf("ph=%f %f\n", pulseheight, letime[i]-cftime[trgch]);
}
double qsumx=qdc[0]+qdc[1];
double qsumy=qdc[2]+qdc[3];
double x0=(qsumx)?(qdc[0]-qdc[1])/qsumx:-2;
double x1=(qsumy)?(qdc[2]-qdc[3])/qsumy:-2;
h2_pos->Fill(x0,x1);
h1_qsumx->Fill(qsumx);
h1_qsumy->Fill(qsumy);
double ph0 = (d[0].edge)? chmax[0][0]: -chmin[0][0];
double ph1 = (d[1].edge)? chmax[1][0]: -chmin[1][0];
h2_ctalk->Fill(ph0,ph1);
}
if (event<1) continue;
if (recid[0]!='C') continue;
if (ich>=nch) continue;
lwfm[ich]=0;
qdc[ich]=d[ich].offset;
chmax[ich][0]=-1; chmax[ich][1]=-1;
chmin[ich][0]=1; chmin[ich][1]=-1;
letime[ich]=-1.; letime[ich]=-1.;
cftime[ich]=-1.; cftime[ich]=-1.;
lwfm[ich]=1;
nwfm[ich]+=1;
t = &tcdata[ich][0];
y = &chdat[ich][0];
ay = &achdat[ich][0];
float sum=0;
for (i=0; i<DWIDTH; i++) {
sum+= dtdata[ich][(i+trgcell)%DWIDTH];
if (version) t[i]=sum;
else t[i]=dtdata[0][i]; // old format
}
tccalib[ich]=t[(DWIDTH-trgcell)%DWIDTH];
for (i=0; i<DWIDTH; i++) {
if (version) t[i]-=(tccalib[ich]-tccalib[0]); // assume ich 0 is connected and always first in the data
y[i]=-0.5+(float)ichdat[ich][i]/0xFFFF;
if (version) y[i]+=evrec.rc/1000.;
h2_dpo[ich]->Fill(t[i],y[i]);
gr_wfm[ich]->SetPoint(i,t[i],y[i]+0.05*(ich-1));
}
for (i=0; i<DWIDTH; i++) {
// accumulated waveforms
ay[i]+=y[i];
// signal minumum
if (y[i]>chmax[ich][0]) {
chmax[ich][0]=y[i];
chmax[ich][1]=t[i];
}
// signal maximum
if (y[i]<chmin[ich][0]) {
chmin[ich][0]=y[i];
chmin[ich][1]=t[i];
}
float t0 = t[i];
float t0p = t[i+1];
float t0n = t[i-1];
// charge in the range
if ((t0>d[ich].adcgate[0])&&(t0<d[ich].adcgate[1])) {
float sign = (d[ich].edge)? 1: -1;
qdc[ich]+=sign*y[i]*(t0p-t0n)/2.;
}
}
for (i=1; i<DWIDTH-1; i++) {
float t0 = t[i];
float t0p = t[i+1];
float t0n = t[i-1];
// leading edge in the range
if ((t0>d[ich].twin[0])&&(t0<d[ich].twin[1])) {
if ( ((!d[ich].edge)&&(y[i]<d[ich].threshold) ||
( d[ich].edge)&&(y[i]>d[ich].threshold ) )
&&(letime[ich]<0.)) {
letime[ich]=t0n+(t0-t0n)*(d[ich].threshold-y[i-1])/(y[i]-y[i-1]);
}
// constant fraction time in the range
if (cftime[ich]<0.){
// negative signals
if ((!d[ich].edge) && (chmin[ich][0]<d[ich].threshold)&&(y[i]<chmin[ich][0]*d[ich].cfrac )){
cftime[ich]=t0n+(t0-t0n)*(chmin[ich][0]*d[ich].cfrac-y[i-1])/(y[i]-y[i-1]);
}
// positive signals
if (( d[ich].edge) && (chmax[ich][0]>d[ich].threshold)&&(y[i]>chmax[ich][0]*d[ich].cfrac)){
cftime[ich]=t0n+(t0-t0n)*(chmax[ich][0]*d[ich].cfrac-y[i-1])/(y[i]-y[i-1]);
}
}
}
}
if ((chmax[ich][1]>d[ich].twin[0])&&(chmax[ich][1]<d[ich].twin[1])) {
h1_chmax[ich]->Fill(chmax[ich][0]);
if (chmax[ich][0]>d[ich].threshold) h1_chmaxt[ich]->Fill(chmax[ich][1]);
}
if ((chmin[ich][1]>d[ich].twin[0])&&(chmin[ich][1]<d[ich].twin[1])) {
h1_chmin[ich]->Fill(chmin[ich][0]);
if (chmin[ich][0]<d[ich].threshold) h1_chmint[ich]->Fill(chmin[ich][1]);
}
h1_qdc[ich]->Fill(qdc[ich]);
h1_letime[ich]->Fill(letime[ich]);
h1_cftime[ich]->Fill(cftime[ich]);
if ((t0!=told) || ((updfrq>0)&&(!(iev%updfrq) ) ) ) {
c_wfm->Update();
for (i=0; i<DWIDTH; i++) {
ay[i]/=nwfm[ich];
gr_wfm[ich]->SetPoint(i,t[i],ay[i]+0.00*(ich-1));
}
printf("processing event %d\n",iev
);
told=t0;
}
}
printf("number of events: %d\n",iev
);
TString pdfname = Form("%s.pdf",FileName);
gStyle->SetOptStat(1111111);
TCanvas* c_dpo= new TCanvas("c_dpo","DPO",750,50,700,700);
c_dpo->Divide(2,2);
for (i=0; i<nch; i++) {c_dpo->cd(i+1)->SetLogz(1); h2_dpo[i]->DrawCopy("colz");}
TCanvas* c_cftdif= new TCanvas("c_cftdif","DPO",750,50,700,700);
c_cftdif->Divide(2,2);
for (i=0; i<nch; i++) {c_cftdif->cd(i+1); h1_cftdif[i]->DrawCopy();}
TCanvas* c_cftcorr= new TCanvas("c_cftcorr","pulseheight vs timing",750,50,700,700);
c_cftcorr->Divide(2,2);
for (i=0; i<nch; i++) {c_cftcorr->cd(i+1); h2_tdcadc[i]->DrawCopy("colz");}
TCanvas* c_adc= new TCanvas("c_adc","adc",750,50,700,700);
c_adc->Divide(2,2);
for (i=0; i<nch; i++) {c_adc->cd(i+1); h1_qdc[i]->DrawCopy();}
TCanvas* c_qsum= new TCanvas("c_qsum","qsum",750,50,700,700);
c_qsum->Divide(1,2);
c_qsum->cd(1); h1_qsumx->DrawCopy();
c_qsum->cd(2); h1_qsumy->DrawCopy();
gStyle->SetOptStat(0);
TCanvas* c_pos= new TCanvas("c_pos","position",750,50,700,700);
c_pos->cd();
h2_pos->DrawCopy("colz");
c_pos->Print(pdfname+"(","pdf");
c_wfm->Print(pdfname,"pdf");
c_adc->Print(pdfname,"pdf");
c_qsum->Print(pdfname,"pdf");
c_dpo->Print(pdfname,"pdf");
c_cftdif->Print(pdfname + ")","pdf");
froot->Write();
froot->Close();
for (i=0; i<nch; i++) {c_cftdif->cd(i+1); h1_cftdif[i]->DrawCopy();}
froot->Write();
froot->Close();
return;
}
void drsana(int nev=1000, int updfrq=20, char *FileName="./data/x1.dat", int trgch=0){
// float threshold[nch][2]={{0.,-0.02},{0.,0.},{0.,0.},{0.,-0.25}};
// float twin[nch][2]={{120.,130.},{100.,150.},{100.,150.},{70.,90.}};
// float adcgate[nch][2]={{12nch.,136.},{120.,140.},{120.,140.},{70.,90.}};
// float threshold[4][2]={{0.,-0.05},{0.,-0.05},{0.,-0.25},{0.,-0.25}};
// float twin[4][2]={{80.,100.},{80.,100.},{90.,110.},{50.,70.}};
// float adcgate[4][2]={{80.,110.},{80.,110.},{90.,110.},{50.,70.}};
double threshold[4][2]={{0.,-0.05},{0.,-0.05},{0.,-0.25},{0.,-0.25}};
float twin[4][2]={{100.,120.},{100.,120.},{110.,130.},{60.,80.}};
float adcgate[4][2]={{105.,125.},{105.,125.},{110.,130.},{60.,80.}};
double vcut[4][2]={{-0.49,-0.07},{-1,1},{-1,1},{-1,1}};
int edge[4]={1,1,1,1};// 0 -- rising, 1 -- falling
float offset[4]={0.25,0.25,0.25,0.25};//
DrsChannel c[2];
c[0].cfrac = CFRAC;
c[0].threshold = threshold[0][0];
c[0].twin[0] = twin[0][0];
c[0].twin[1] = twin[0][1];
c[0].adcgate[0] = adcgate[0][0];
c[0].adcgate[1] = adcgate[0][1];
c[0].vcut[0] = vcut[0][0];
c[0].vcut[1] = vcut[0][1];
c[0].edge = edge[0];
c[0].edge = offset[0];
c[1].cfrac = CFRAC;
c[1].threshold = threshold[1][0];
c[1].twin[0] = twin[1][0];
c[1].twin[1] = twin[1][1];
c[1].adcgate[0] = adcgate[1][0];
c[1].adcgate[1] = adcgate[1][1];
c[1].vcut[0] = vcut[1][0];
c[1].vcut[1] = vcut[1][1];
c[1].edge = edge[1];
c[1].offset = offset[1];
drs * d = new drs(FileName, 2, c, trgch, nev, updfrq, -0.55,0.55 );
}
void sensl(int nev=1000, int updfrq=20, char *FileName="./data/x1.dat", int trgch=0){
// nev = -1 all events
double threshold[4]={-0.005,-0.005,-0.005,-0.005}; // threshold for determination of timing
float twin[4][2]={{980.,1150.},{980.,1150.},{980.,1150.},{980,1150.}}; // window for calculation of the timing
float adcgate[4][2]={{980.,1150.},{980.,1150.},{980.,1150.},{980.,1150.}}; //integration window
double vcut[4][2]={{-1,1},{-1,1},{-1,1},{-1,1}}; // cut on the pulseheight for filling the timing histograms
int edge[4]={0,0,0,0};// 1 -- rising edge, 0 -- falling edge
float offset[4]={0.25,0.25,0.25,0.25};// qdc offset
DrsChannel c[NCH];
for (int k=0;k<NCH;k++){
c[k].cfrac = CFRAC;
c[k].threshold = threshold[k];
for (int j=0;j<2;j++){
c[k].twin[j] = twin[k][j];
c[k].adcgate[j] = adcgate[k][j];
c[k].vcut[j] = vcut[k][j];
c[k].edge = edge[k];
c[k].offset = offset[k];
}
}
drs * d = new drs(FileName, NCH, c, trgch, nev, updfrq , -0.1,0.12);
}