/* ======================================================= DAQ for cosmic ray air shower array ======================================================= Author : Supriya Das (with help from KC Ravindran, CRL, TIFR) Company : CAPSS, Bose Institute, Kolkata E-mail : supriya@bosemain.boseinst.ac.in Description : CAMAC based DAQ code Date : 08/05/2009 Version : 1.00 Revision : ======================================================= */ #ifdef WIN32 #include #include #include #else #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #endif #include "crate_lib.h" //#include "daq_root.h" //ROOT headers #include "TFile.h" #include "TNtuple.h" #include "TH1.h" #include "TCanvas.h" #include "TPostScript.h" #define GATE_NUM 1 short irq_cnt, lam_cnt, retcode; CRATE_OP op; int i,j,slot,k; int channel,data; int evt=0; int subaa, subat; short crate_id; char resp[32],cmd1[32],cmd2[32],cmd3[32]; char filename[20]; char filenameroot[20]; // define pulse parameters for pedestal trigger int period; int width; int polarity; int tdc[16]; int adc[16]; int adcbin[16][4096]; int kchan, kbins,ibin; int runno, evlim;; FILE* fp; /* TFile* rootfile; TCanvas* vC1; TNtuple* ntuple = new TNtuple("ntuple","TNtuple","channel:bin:adc"); */ //const int nchan = 16; char hadcname[40]; char htdcname[40]; TH1I *hadc[16]; TH1I *htdc[16]; for(int iloop=0; iloop<16; iloop++){ sprintf(hadcname,"adc_channel_%d",iloop); sprintf(htdcname,"tdc_channel_%d",iloop); hadc[iloop] = new TH1I(hadcname,"",4096,0,4095); htdc[iloop] = new TH1I(htdcname,"",4096,0,4095); } //TH1I* hadc = new TH1I("hadc","",4500,0,4499); //H1I* htdc = new TH1I("htdc","",4500,0,4499); int readData(short); void IRQHandler(short crate_id, short irq_type, unsigned int irq_data) { char nimo, value; switch (irq_type) { case LAM_INT: printf("Got LAM IRQ %d, Going to read data\n",irq_type); readData(crate_id); // Issue CLEAR to BUSY sprintf(cmd2, "nim_setouts 2 1\r"); sprintf(cmd3, "nim_setouts 2 0\r"); retcode = CMDSR(crate_id, cmd2, resp, 32); retcode = CMDSR(crate_id, cmd3, resp, 32); printf("BUSY cleared\n"); // LACK(crate_id); break; case COMBO_INT: printf("COMBO Reg: %x\n", irq_data); break; case DEFAULT_INT: nimo = (irq_cnt % 4) + 1; value = 1 - (irq_cnt / 4); printf("Default button pressed!\n"); NOSOS(crate_id, nimo, value); irq_cnt++; break; } return; } int main(int argc, char *argv[]) { // Initialize TDC, ADC array // printf("Initialize TDC, ADC array\n"); for (i=0;i<16;i++){ tdc[i]=0; adc[i]=0; for(ibin=0;ibin<4096;ibin++){ adcbin[i][ibin]=0; } } // printf("Initialization finished\n"); system("clear"); printf(" ========================================================="); printf("\n"); printf(" = Welcome to DAQ of Cosmic ray air shower array ="); printf("\n"); printf(" ========================================================="); printf("\n"); printf("\n"); printf("Enter run no. : "); scanf("%d",&runno); sprintf(filename,"run_%04d.dat",runno); sprintf(filenameroot,"run_%04d.root",runno); //printf("File named %s will be created\n",filename); printf("Enter number of events to be processed : "); scanf("%d",&evlim); // IP address of JENET Board is 192.68.60.61 if (argc == 2) crate_id = CROPEN(argv[1]); else crate_id = CROPEN("192.168.60.61"); if (crate_id >= 0) { printf("Communication with CAMAC Crate %d established\n",crate_id); // setting pulser for PEDESTAL trigger sprintf(cmd1, "nim_setpulse 1023 3 0\r"); //freq: 10 Hz, width: 176 ns retcode = CMDSR(crate_id, cmd1, resp, 32); printf("Pulser set for PEDESTAL\n"); // // Clear & Init crate retcode = CCCC(crate_id); retcode = CCCZ(crate_id); // Enable LAM on dataway display for TDC module on slot 16 op.F = 26; op.N = 16; retcode = CSSA(crate_id, &op); // Enable LAM on dataway display for QDC module on slot 18 op.F = 26; op.N = 18; retcode = CSSA(crate_id, &op); // Acknowledge the LAM and call IRQ retcode = LACK(crate_id); retcode = CRIRQ(crate_id, IRQHandler); if (retcode != CRATE_OK) { printf("ERROR: %d - Unable to register IRQ Callback!\n", retcode); CRCLOSE(crate_id); return 0; } while (1){} // keeps checking the LAM } else { printf("ERROR: %d - Unable to connect with CAMAC crate on specified IP address!\n", crate_id); } printf("Bye bye!\n"); return 0; } int readData(short crate_id){ // int resp, block_data_size, total_data_size; unsigned int blk_transf_buf[8192]; char cmd[64], response[64]; BLK_TRANSF_INFO blk_info; memset(blk_transf_buf, 0, 8192); char c; evt++; printf("Event no. %d\n",evt); // Reading TDC (slot 16) Data op.F = 0; op.N = 16; for (subat=0; subat<16;subat++){ op.A = subat; retcode = CSSA(crate_id, &op); tdc[subat] = (op.DATA & 0x0fff); htdc[subat]->Fill(tdc[subat]); } // Reading ADC (slot 18) Data op.F = 0; op.N = 18; for (subaa=0; subaa<16;subaa++){ op.A = subaa; retcode = CSSA(crate_id, &op); adc[subaa] = (op.DATA & 0x0fff); hadc[subaa]->Fill(adc[subaa]); //bining data adcbin[subaa][adc[subaa]]++; } // print data for(j=0;j<16;j++){ printf("TDC ch %d : %x , ADC ch %d : %x ",j,tdc[j],j,adc[j]); printf("\n"); } // /* //test block transfer block_data_size = 48; //16-bit data total_data_size = (GATE_NUM * block_data_size); //16 numbers of 16-bit data printf("Start block transfer read %d :\n", total_data_size); //blk_info.opcode = OP_BLKFS; blk_info.opcode = OP_BLKSA; blk_info.F = 0; blk_info.N = 12; // blk_info.A = 0; blk_info.totsize = total_data_size; blk_info.blksize = block_data_size; blk_info.timeout = 0; resp = BLKTRANSF(crate_id, &blk_info, blk_transf_buf); if (resp != CRATE_OK) { printf("ERROR: Negative response from socket server\n"); return 0; } printf("Total data read: %d\n", blk_info.totsize); for (i = 0; i < blk_info.totsize; i++) { // Show received buffer if ((i > 0) && ((i % 10) == 0)) { printf("\n"); } printf("%04x ", blk_transf_buf[i]); } printf("\nEnd of block transfer read\n"); //block transfer test ends */ // CLEAR CRATE retcode = CCCC(crate_id); //Enable LAM on both slots op.F = 26; op.N = 16; retcode = CSSA(crate_id, &op); op.F = 26; op.N = 18; retcode = CSSA(crate_id, &op); if(evt == evlim){ printf("Finished %d events, going to write in file\n",evt); TFile* rootfile; TNtuple* ntuple; TCanvas* vC1; TPostScript* ps; fp = fopen(filename,"w"); rootfile = new TFile(filenameroot,"RECREATE"); rootfile->cd(); ntuple = new TNtuple("ntuple","TNtuple","channel:bin:adc"); for(kchan=0;kchan<16;kchan++){ for(kbins=0;kbins<4096;kbins++){ fprintf(fp,"%d %d %d\n",kchan,kbins,adcbin[kchan][kbins]); //if(adcbin[kchan][kbins]!=0)printf("%d %d %d\n",kchan,kbins,adcbin[kchan][kbins]); ntuple->Fill(float(kchan),float(kbins),float(adcbin[kchan][kbins])); //ntuple->Fill(kchan,kbins,1); //if(adcbin[kchan][kbins]!=0)printf("ntuple filled %d \n", adcbin[kchan][kbins]); } } ntuple->Write(); //ntuple->Show(); for (int iloop=0;iloop<16;iloop++){ hadc[iloop]->Write(); htdc[iloop]->Write(); } fclose(fp); ps = new TPostScript("plot.ps",111); vC1 = new TCanvas("vC1","Test"); //ntuple->Draw("adc:bin","channel==1 && bin<500"); // hadc->Draw(); ps->Close(); //gSystem->Exec("kghostview plot.ps&"); system("kghostview plot.ps"); getchar(); rootfile->Close(); //hadc->Draw(); //printf("Data written to file, press to continue, q to quit\n"); /* //if((c=getchar()) != EOF && (c=getchar()) != 'q'){ if((c=getchar()) != 'q'){ printf("%c\n",c); evt=0; runno++; sprintf(filename,"run_%04d.dat",runno); } else exit(0); */ exit(0); } // evt++; }