Subversion Repositories f9daq

Rev

Rev 175 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. #include "TH1.h"
  2. #include "TH2.h"
  3. #include "TGraph.h"
  4. #include "TCanvas.h"
  5. #include "TStyle.h"
  6. #include "TLegend.h"
  7. //#include "TROOT.h"
  8.  
  9. #include <stdio.h>
  10.  
  11. #define DATA_PATH  "./data/"
  12. #define CFRAC 0.4
  13. #define DWIDTH 1024
  14. void drs(int nev=1000, int updfrq=20, const char *FileName="./data/x1.dat", int trgch=0, int debug=0)
  15. {
  16.   int i,j,ich,iev;
  17.   int stat;
  18.   char histname[128];
  19. //  char FileName[300]="./data/n2.dat";
  20.   FILE *fin;
  21.  
  22.   char recid[5]="SAMO";
  23.   unsigned short *srecid = (unsigned short *) &recid[0];
  24.   unsigned int * irecid;
  25.   struct evrec {
  26.     unsigned long evn;
  27.     unsigned short tYear,tMonth,tDay,tHour,tMin,tSec,tmSec,reserved;
  28.   } evrec;
  29.  
  30.   float dtdata[4][DWIDTH];
  31.   float tcdata[4][DWIDTH];
  32.   float tccalib[4];
  33.  
  34.   short lwfm[4];
  35.   int nwfm[4];
  36.   int board;
  37.  
  38. //  int imin[4]={250,250,250,250};
  39. //  int imax[4]={750,750,750,750};
  40.   unsigned short ichdat[4][DWIDTH];
  41.   float chdat[4][DWIDTH],achdat[4][DWIDTH],chmax[4][2],chmin[4][2];
  42.   float letime[4][2],cftime[4][2],qdc[4];
  43. //  float threshold[4][2]={{0.,-0.02},{0.,0.},{0.,0.},{0.,-0.25}};
  44. //  float twin[4][2]={{120.,130.},{100.,150.},{100.,150.},{70.,90.}};
  45. //  float adcgate[4][2]={{124.,136.},{120.,140.},{120.,140.},{70.,90.}};
  46. //  float threshold[4][2]={{0.,-0.05},{0.,-0.05},{0.,-0.25},{0.,-0.25}};
  47. //  float twin[4][2]={{80.,100.},{80.,100.},{90.,110.},{50.,70.}};
  48. //  float adcgate[4][2]={{80.,110.},{80.,110.},{90.,110.},{50.,70.}};
  49.   float threshold[4][2]={{0.,-0.1},{0.,-0.05},{0.,-0.25},{0.,-0.25}};
  50.   float twin[4][2]={{100.,120.},{100.,120.},{40.,130.},{60.,80.}};
  51.   float adcgate[4][2]={{105.,125.},{105.,125.},{110.,130.},{60.,80.}};
  52.   float vcut[4][2]={{-1,1},{-1,1},{-1,1},{-1,1}};
  53.  
  54. //  gROOT->Reset();
  55.  
  56.   gStyle->SetOptStat(1111111);
  57. //  gStyle->SetPalette(52);
  58.  
  59.   TH1F *h1_chmax[4];
  60.   TH1F *h1_chmaxt[4];
  61.   TH1F *h1_chmin[4];
  62.   TH1F *h1_chmint[4];
  63.   TH1F *h1_letime[4][2];
  64.   TH1F *h1_cftime[4][2];
  65.   TH1F *h1_cftdif[4];
  66.   TH1F *h1_qdc[4];
  67.   TH2F *h2_dpo[4];
  68.   TH2F *h2_tdcadc[4];
  69.   TH2F *h2_ctdcadc[4];
  70.   TH2F *h2_ctalk;
  71.   TGraph *gr_wfm[4];
  72.   TCanvas *c_wfm= new TCanvas("c_wfm","WFM",50,50,700,700);
  73.   TLegend *leg = new TLegend(0.8, 0.8, 1, 1);
  74.   c_wfm->Divide(1,1);
  75.   c_wfm->cd(1);
  76.  
  77.   for (i=0;i<4;i++) {
  78.     sprintf(histname,"chmax_%02d",i);
  79.     h1_chmax[i]=new TH1F(histname,histname,1100,-0.55,0.55);
  80.     sprintf(histname,"chmaxt_%02d",i);
  81.     h1_chmaxt[i]=new TH1F(histname,histname,DWIDTH,-0.1,204.7);
  82.     sprintf(histname,"chmin_%02d",i);
  83.     h1_chmin[i]=new TH1F(histname,histname,1100,-0.55,0.55);
  84.     sprintf(histname,"chmint_%02d",i);
  85.     h1_chmint[i]=new TH1F(histname,histname,DWIDTH,-0.1,204.7);
  86.     sprintf(histname,"letimep_%02d",i);
  87.     h1_letime[i][0]=new TH1F(histname,histname,500,twin[i][0],twin[i][1]);
  88.     sprintf(histname,"letimen_%02d",i);
  89.     h1_letime[i][1]=new TH1F(histname,histname,500,twin[i][0],twin[i][1]);
  90.     sprintf(histname,"cftimep_%02d",i);
  91.     h1_cftime[i][0]=new TH1F(histname,histname,500,twin[i][0],twin[i][1]);
  92.     sprintf(histname,"cftimen_%02d",i);
  93.     h1_cftime[i][1]=new TH1F(histname,histname,500,twin[i][0],twin[i][1]);
  94.     sprintf(histname,"cftdif_%02d",i);
  95.     h1_cftdif[i]=new TH1F(histname,histname,2000,twin[i][0],twin[i][1]);
  96.     sprintf(histname,"qdc_%02d",i);
  97.     h1_qdc[i]=new TH1F(histname,histname,500,-0.1,4.9);
  98.     sprintf(histname,"dpo_%02d",i);
  99.     h2_dpo[i]=new TH2F(histname,histname,DWIDTH,-0.1,204.7,1100,-0.55,0.55);
  100.     sprintf(histname,"tdcadc_%02d",i);
  101.     h2_tdcadc[i]=new TH2F(histname,histname,250,0.,0.5,400,twin[i][0],twin[i][1]);
  102.     sprintf(histname,"ctdcadc_%02d",i);
  103.     h2_ctdcadc[i]=new TH2F(histname,histname,250,0.,0.5,400,twin[i][0],twin[i][1]);
  104.     gr_wfm[i]=new TGraph(DWIDTH);
  105.     gr_wfm[i]->SetMinimum(-0.55);
  106.     gr_wfm[i]->SetMaximum(0.55);
  107.     gr_wfm[i]->SetLineColor(i+1);
  108.     gr_wfm[i]->SetMarkerColor(i+1);
  109.     sprintf(histname,"ch %d",i);
  110.     leg->AddEntry(gr_wfm[i], histname, "l");
  111.     if (i) gr_wfm[i]->Draw("LP"); else gr_wfm[i]->Draw("ALP");
  112.    
  113.   }
  114.   leg->Draw();
  115.   h2_ctalk=new TH2F("ctalk","ctalk",250,0.,0.5,250,0.,0.5);
  116.  
  117.   fin=fopen(FileName,"rb");
  118.  
  119.   printf("%s, %lu\n",recid,sizeof(evrec));
  120.  
  121.  
  122.   for (i=0;i<4;i++){
  123.     nwfm[i]=0;
  124.     for (j=0;j<DWIDTH;j++){
  125.       achdat[i][j]=0;
  126.     }
  127.   }
  128.   unsigned int trgcell = 0;
  129.   int event = -1;
  130.  
  131.   iev=0;
  132.   while(!feof(fin)){
  133.     if (iev>=nev) break;
  134.     stat=fread(recid,1,4,fin);
  135.     if (debug){
  136.       if (recid[1]=='#') printf("%c%c%u\n",recid[0],recid[1],srecid[1]);
  137.       else  printf("%s\n",recid );
  138.     }
  139.     switch (recid[0]){
  140.        case 'T':
  141.            switch (recid[1]){
  142.              case 'I': event=0; break;
  143.              case '#': trgcell = srecid[1]; break;
  144.            }
  145.            break;
  146.        case 'B':
  147.            board = srecid[1];
  148.            break;
  149.        case 'C':
  150.            sscanf(recid,"C%d",&ich);
  151.            ich=ich-1;
  152.            if (event) {
  153.              stat=fread(ichdat[ich],1,sizeof(ichdat[ich]),fin);
  154.            } else {
  155.              stat=fread(dtdata[ich],1,sizeof(float)*DWIDTH,fin);
  156.              
  157.            }
  158.            break;
  159.        case 'E':
  160.            stat=fread(&evrec.evn,1,sizeof(evrec),fin);
  161.            iev++;
  162.            event=1;
  163.            break;
  164.        default:
  165.            printf("Unknown header!\n");
  166.            break;
  167.     }
  168.  
  169.  
  170.     if (recid[0] == 'E'){
  171.          for ( i=0;i<4;i++){
  172.            if (!lwfm[i]) continue;
  173.            if ((chmin[i][0]>vcut[i][0])&&(chmin[i][0]<vcut[i][1])) {
  174.              h1_cftdif[i]->Fill(cftime[i][1]-cftime[trgch][1]+110.);
  175.            }
  176. //      h2_tdcadc[i]->Fill(-chmin[i][0],letime[i][1]-cftime[trgch][1]+72);
  177. //      h2_ctdcadc[i]->Fill(-chmin[i][0],cftime[i][1]-cftime[trgch][1]+72);
  178.            h2_tdcadc[i]->Fill(-chmin[0][0],letime[i][1]-cftime[trgch][1]+72);
  179.            h2_ctdcadc[i]->Fill(-chmin[0][0],cftime[i][1]-cftime[trgch][1]+72);
  180.          }
  181.          h2_ctalk->Fill(-chmin[0][0],-chmin[1][0]);
  182.     }
  183.     if (event<1) continue;
  184.     if (recid[0]!='C') continue;
  185.  
  186.    
  187.    
  188.     lwfm[ich]=0;
  189.     qdc[ich]=0;
  190.     chmax[ich][0]=-1; chmax[ich][1]=-1;
  191.     chmin[ich][0]=1; chmin[ich][1]=-1;
  192.     letime[ich][0]=-1.; letime[ich][1]=-1.;
  193.     cftime[ich][0]=-1.; cftime[ich][1]=-1.;
  194.    
  195.  
  196.       lwfm[ich]=1;
  197.       nwfm[ich]+=1;
  198.  
  199.       float sum=0;
  200.       for (i=0;i<DWIDTH;i++) {
  201.          sum+= dtdata[ich][(i+trgcell)%DWIDTH];
  202.          tcdata[ich][i]=sum;        
  203.       }
  204.       tccalib[ich]=tcdata[ich][(DWIDTH-trgcell)%DWIDTH];
  205.  
  206.       for (i=0;i<DWIDTH;i++) {
  207.          tcdata[ich][i]-=(tccalib[ich]-tccalib[0]); // assume ich 0 is connected and always first in the data          
  208.       }
  209.  
  210.       for (i=0;i<DWIDTH;i++) {
  211. //      for (i=imin[ich];i<imax[ich];i++) {
  212.         chdat[ich][i]=-0.5+(float)ichdat[ich][i]/0xFFFF;
  213.         achdat[ich][i]+=chdat[ich][i];
  214.         if (chdat[ich][i]>chmax[ich][0]) {
  215.           chmax[ich][0]=chdat[ich][i];
  216.           chmax[ich][1]=tcdata[ich][i];
  217.         }
  218.         if (chdat[ich][i]<chmin[ich][0]) {
  219.           chmin[ich][0]=chdat[ich][i];
  220.           chmin[ich][1]=tcdata[ich][i];
  221.         }
  222.         float tdata = tcdata[ich][i];
  223.         float tdatap = tcdata[ich][i+1];
  224.         float tdatan = tcdata[ich][i-1];
  225.  
  226.         if ((tdata>adcgate[ich][0])&&(tdata<adcgate[ich][1])) {
  227.           qdc[ich]+=-chdat[ich][i]*(tdatap-tdatan)/2;
  228.         }
  229.         if ((tdata>twin[ich][0])&&(tdata<twin[ich][1])) {
  230.           if ((chdat[ich][i]<threshold[ich][1])&&(letime[ich][1]<0.)) {
  231.             letime[ich][1]=tdatan+(tdata-tdatan)*
  232.                            (threshold[ich][1]-chdat[ich][i-1])/(chdat[ich][i]-chdat[ich][i-1]);
  233. // preveri za 0!!                          
  234.           }
  235.         }
  236.       }
  237.    
  238.  
  239.       i=ich;
  240.    
  241.      
  242.       for (j=0;j<DWIDTH;j++) {
  243.         float tdata  = tcdata[i][j];
  244.         float tdatap = tcdata[i][j+1];
  245.         float tdatan = tcdata[i][j-1];
  246.         h2_dpo[i]->Fill(tdata,chdat[i][j]);
  247.         gr_wfm[i]->SetPoint(j,tdata,chdat[i][j]+0.05*(i-1));
  248.         if ((tdata>twin[i][0])&&(tdata<twin[i][1])) {
  249.           if ((chmin[i][0]<threshold[i][1])&&(chdat[i][j]<chmin[i][0]*CFRAC)&&(cftime[i][1]<0.)) {
  250.             cftime[i][1]=tdatan+(tdata-tdatan)*
  251.                            (chmin[i][0]*CFRAC-chdat[i][j-1])/(chdat[i][j]-chdat[i][j-1]);
  252. // preveri za 0!!                          
  253.           }
  254.         }
  255.       }
  256.    
  257.    
  258.       if ((chmax[i][1]>twin[i][0])&&(chmax[i][1]<twin[i][1])) {
  259.         h1_chmax[i]->Fill(chmax[i][0]);
  260.         if (chmax[i][0]>threshold[i][0]) h1_chmaxt[i]->Fill(chmax[i][1]);
  261.       }
  262.       if ((chmin[i][1]>twin[i][0])&&(chmin[i][1]<twin[i][1])) {
  263.         h1_chmin[i]->Fill(chmin[i][0]);
  264.         if (chmin[i][0]<threshold[i][1]) h1_chmint[i]->Fill(chmin[i][1]);
  265.       }
  266.       h1_qdc[i]->Fill(qdc[i]);
  267.       h1_letime[i][1]->Fill(letime[i][1]);
  268.       h1_cftime[i][1]->Fill(cftime[i][1]);
  269. //84,56
  270.  
  271.  
  272.      
  273.  
  274.     if (!(iev%updfrq)) {
  275.       c_wfm->Update();
  276.       for (j=0;j<DWIDTH;j++) {
  277.         achdat[i][j]/=nwfm[i];
  278.         gr_wfm[i]->SetPoint(j,tcdata[i][j],achdat[i][j]+0.05*(i-1));
  279.       }
  280.       printf("processing event %d\n",iev);
  281.     }
  282.  
  283.  
  284.    
  285.  
  286.   }
  287.  
  288.  
  289.   printf("number of events: %d\n",iev);
  290.   fclose(fin);
  291.  
  292.   TCanvas* c_dpo= new TCanvas("c_dpo","DPO",750,50,700,700);
  293.   c_dpo->Divide(2,2);
  294.   for (i=0;i<4;i++) {c_dpo->cd(i+1);h2_dpo[i]->Draw("colz");}
  295.  
  296.   TCanvas* c_cftdif= new TCanvas("c_cftdif","DPO",750,50,700,700);
  297.   c_cftdif->Divide(2,2);
  298.   for (i=0;i<4;i++) {c_cftdif->cd(i+1);h1_cftdif[i]->Draw();}
  299.  
  300.   return;
  301. }
  302.