#define RESET "\033[0m" #define BLACK "\033[30m" /* Black */ #define RED "\033[31m" /* Red */ #define GREEN "\033[32m" /* Green */ #define YELLOW "\033[33m" /* Yellow */ #define BLUE "\033[34m" /* Blue */ #define MAGENTA "\033[35m" /* Magenta */ #define CYAN "\033[36m" /* Cyan */ #define WHITE "\033[37m" /* White */ #define BOLDBLACK "\033[1m\033[30m" /* Bold Black */ #define BOLDRED "\033[1m\033[31m" /* Bold Red */ #define BOLDGREEN "\033[1m\033[32m" /* Bold Green */ #define BOLDYELLOW "\033[1m\033[33m" /* Bold Yellow */ #define BOLDBLUE "\033[1m\033[34m" /* Bold Blue */ #define BOLDMAGENTA "\033[1m\033[35m" /* Bold Magenta */ #define BOLDCYAN "\033[1m\033[36m" /* Bold Cyan */ #define BOLDWHITE "\033[1m\033[37m" /* Bold White */ using namespace std; #include "RootHeader.h" #include "Riostream.h" #include "signal.h" #include "Bytes.h" #include "VMERemote.h" #include "TProfile.h" #include #define MAXCH 16 #define DETECTOR "tof" /* * VMERemote * */ class VMERemote : public TObject { public: VMERemote(const char *pHost, int port) { host = pHost; this->port = port; // Socket = new TSocket(host, port, 32768); Socket = new TSocket(host, port, 32900); if(!Socket->IsValid()) { printf("Failed to connected to host: %s\n", pHost); } else printf("Successfully connected to host: %s\n" , pHost); /* set board type */ BoardType = 5; } /********/ Bool_t Reconnect() { if(Socket) { Socket->Close("force"); }; // Socket = Socket = new TSocket(host, port, 32768); Socket = new TSocket(host, port, 32900); return (Socket->IsValid()); } /********/ Bool_t WriteVME_SC(unsigned int addr, unsigned int val, unsigned int Status = 0, unsigned int Slot = 0) { if(!Socket->IsValid()) { printf("WriteVME_SC FAILED\n"); return kFALSE; } Cmd_Write_SC *pCmd_Write32; OutgoingMsg.Length = HTONL(REMOTE_MSG_SIZE(Cmd_Write_SC)-4); OutgoingMsg.BoardType = HTONL(BoardType); OutgoingMsg.MsgType = HTONL(REMOTE_CMD_WRITE_SC); pCmd_Write32 = (Cmd_Write_SC *)OutgoingMsg.Msg; pCmd_Write32->ID = HTONL(Slot); pCmd_Write32->Status = HTONL(Status); pCmd_Write32->Address = HTONL(addr); pCmd_Write32->Value = HTONL(val); Socket->SendRaw(&OutgoingMsg, REMOTE_MSG_SIZE(Cmd_Write_SC)); cout << " REMOTE_MSG_SIZE = " << REMOTE_MSG_SIZE(Cmd_Write_SC) << endl; cout << " Outgoing: Length = " << OutgoingMsg.Length << " " << REMOTE_MSG_SIZE(Cmd_Write_SC)-4 << " Board Type = " << OutgoingMsg.BoardType << " " << BoardType << " MsgType = " << OutgoingMsg.MsgType << " " << REMOTE_CMD_WRITE_SC << " Slot = " << pCmd_Write32->ID << " " << Slot << " Status = " << pCmd_Write32->Status << " " << Status << " Address = " << pCmd_Write32->Address << " " << addr << " Value = " << pCmd_Write32->Value << " " << val << endl; // Close(); return kTRUE; } /********/ Bool_t ReadVME32(unsigned int addr, unsigned int *val, unsigned int Status = 0) { if(!Socket->IsValid()) { printf("ReadVME32 FAILED\n"); return kFALSE; } Cmd_Read32 *pCmd_Read32; OutgoingMsg.Length = HTONL(REMOTE_MSG_SIZE(Cmd_Read32)-4); OutgoingMsg.BoardType = HTONL(BoardType); OutgoingMsg.MsgType = HTONL(REMOTE_CMD_READ32); pCmd_Read32 = (Cmd_Read32 *)OutgoingMsg.Msg; pCmd_Read32->Address = HTONL(addr); Socket->SendRaw(&OutgoingMsg, REMOTE_MSG_SIZE(Cmd_Read32)); if( (Socket->RecvRaw(&IncomingMsg.Length, 4) == 4) && (NTOHL(IncomingMsg.Length) <= sizeof(RemoteMsgStruct)) && (Socket->RecvRaw(&IncomingMsg.BoardType, NTOHL(IncomingMsg.Length)) == (int)NTOHL(IncomingMsg.Length)) ) { IncomingMsg.Length = NTOHL(IncomingMsg.Length); IncomingMsg.BoardType = NTOHL(IncomingMsg.BoardType); IncomingMsg.MsgType = NTOHL(IncomingMsg.MsgType); unsigned int *p = (unsigned int *)IncomingMsg.Msg; // if(IncomingMsg.MsgType == CMD_RSP(REMOTE_CMD_READ32)) // { *val = HTONL(*p); return kTRUE; // } } // Close(); return kFALSE; } Bool_t ReadVME_PED_ALL(unsigned int *val, unsigned int Status = 0, unsigned int sl_first = 0, unsigned int sl_last = 0 ) { unsigned int debug_read_ped = 0; if(!Socket->IsValid()) { printf("ReadVME_PED_ALL FAILED\n"); return kFALSE; } Cmd_Read_PED_ALL *pCmd_Read_PED_ALL; pCmd_Read_PED_ALL = (Cmd_Read_PED_ALL *)OutgoingMsg.Msg; OutgoingMsg.Length = HTONL(REMOTE_MSG_SIZE(Cmd_Read_PED_ALL)-4); OutgoingMsg.BoardType = HTONL(BoardType); OutgoingMsg.MsgType = HTONL(REMOTE_CMD_READ_PED_ALL); pCmd_Read_PED_ALL->IDF = HTONL(sl_first); pCmd_Read_PED_ALL->IDL = HTONL(sl_last); pCmd_Read_PED_ALL->Status = HTONL(Status); Socket->SendRaw(&OutgoingMsg, REMOTE_MSG_SIZE(Cmd_Read_PED_ALL)); cout << " ReadVME_PED_ALL SEND request. Length = " << REMOTE_MSG_SIZE(Cmd_Read_PED_ALL) << endl; if( (Socket->RecvRaw(&IncomingMsg.Length, 4) == 4) && (NTOHL(IncomingMsg.Length) <= sizeof(RemoteMsgStruct))) { Socket->RecvRaw(&IncomingMsg.BoardType, NTOHL(IncomingMsg.Length)); cout << " ReadVME_PED_ALL receive Scalers " << endl; IncomingMsg.Length = NTOHL(IncomingMsg.Length); IncomingMsg.BoardType = NTOHL(IncomingMsg.BoardType); IncomingMsg.MsgType = NTOHL(IncomingMsg.MsgType); cout << " Length = " << IncomingMsg.Length << " Board = " << IncomingMsg.BoardType << " Type = " << IncomingMsg.MsgType << endl; Cmd_Read_PED_ALL *p = (Cmd_Read_PED_ALL *)IncomingMsg.Msg; int status = 0; int slotf = NTOHL(p->IDF); int slotl = NTOHL(p->IDL); status = NTOHL(p->Status); cout << " Incoming SLOT FIRST: "<< slotf << " LAST: " << slotl << " Incoming SATUS: " << status << endl; if(status < 0) return kFALSE; for(unsigned int bd = 0; bd < 16; bd++){ if(debug_read_ped){ int bd_slot; if(bd < 8) bd_slot = bd + 3; else bd_slot = bd + 5; cout << " FADC in slot " << bd_slot << endl; } for(unsigned int ch = 0; ch < 8; ch++){ val[bd*8 + ch] = NTOHL(p->Value[bd*8 + ch]); if(debug_read_ped){ unsigned int ped1 = val[bd*8 + ch] & 0xFFFF; unsigned int ped2 = (val[bd*8 + ch] & 0xFFFF0000) >> 16; cout << " Ch = " << 2*ch << " " << ped1 << " Ch = " << 2*ch + 1 << " ped2 " << ped2 << endl; } } cout << endl; } return kTRUE; } return kFALSE; } Bool_t ReadVME_SC_ALL(unsigned int *val, unsigned int Status = 0, unsigned int sl_first = 0, unsigned int sl_last = 0 ) { if(!Socket->IsValid()) { printf("ReadVME_SC_ALL FAILED\n"); return kFALSE; } Cmd_Read_SC_ALL *pCmd_Read_SC_ALL; pCmd_Read_SC_ALL = (Cmd_Read_SC_ALL *)OutgoingMsg.Msg; OutgoingMsg.Length = HTONL(REMOTE_MSG_SIZE(Cmd_Read_SC_ALL)-4); OutgoingMsg.BoardType = HTONL(BoardType); OutgoingMsg.MsgType = HTONL(REMOTE_CMD_READ_SC_ALL); pCmd_Read_SC_ALL->IDF = HTONL(sl_first); pCmd_Read_SC_ALL->IDL = HTONL(sl_last); pCmd_Read_SC_ALL->Status = HTONL(Status); Socket->SendRaw(&OutgoingMsg, REMOTE_MSG_SIZE(Cmd_Read_SC_ALL)); cout << " ReadVME_SC_ALL SEND request. Length = " << REMOTE_MSG_SIZE(Cmd_Read_SC_ALL) << endl; if( (Socket->RecvRaw(&IncomingMsg.Length, 4) == 4) && (NTOHL(IncomingMsg.Length) <= sizeof(RemoteMsgStruct))) { Socket->RecvRaw(&IncomingMsg.BoardType, NTOHL(IncomingMsg.Length)); cout << " ReadVME_SC_ALL receive Scalers " << endl; IncomingMsg.Length = NTOHL(IncomingMsg.Length); IncomingMsg.BoardType = NTOHL(IncomingMsg.BoardType); IncomingMsg.MsgType = NTOHL(IncomingMsg.MsgType); cout << " Length = " << IncomingMsg.Length << " Board = " << IncomingMsg.BoardType << " Type = " << IncomingMsg.MsgType << endl; Cmd_Read_SC_ALL *p = (Cmd_Read_SC_ALL *)IncomingMsg.Msg; int status = 0; int slotf = NTOHL(p->IDF); int slotl = NTOHL(p->IDL); status = NTOHL(p->Status); cout << " Incoming SLOT FIRST: "<< slotf << " LAST: " << slotl << " Incoming SATUS: " << status << endl; if(status < 0) return kFALSE; for(unsigned int bd = 0; bd < 16; bd++){ int bd_slot; if(bd < 8) bd_slot = bd + 3; else bd_slot = bd + 5; cout << " FADC in slot " << bd_slot << endl; for(unsigned int ch = 0; ch < 17; ch++){ val[bd*17 + ch] = NTOHL(p->Value[bd*17 + ch]); cout << val[bd*17 + ch] << " "; } cout << endl; } return kTRUE; } return kFALSE; } Bool_t ReadVME_SC(unsigned int *val, unsigned int Status = 0, unsigned int Slot = 0 ) { if(!Socket->IsValid()) { printf("ReadVMESC FAILED\n"); return kFALSE; } // unsigned int fadc_id = 16; Cmd_Read_SC *pCmd_Read_SC; pCmd_Read_SC = (Cmd_Read_SC *)OutgoingMsg.Msg; OutgoingMsg.Length = HTONL(REMOTE_MSG_SIZE(Cmd_Read_SC)-4); OutgoingMsg.BoardType = HTONL(BoardType); OutgoingMsg.MsgType = HTONL(REMOTE_CMD_READ_SC); cout<< "SLOT :"<ID = HTONL(Slot); pCmd_Read_SC->Status = HTONL(Status); Socket->SendRaw(&OutgoingMsg, REMOTE_MSG_SIZE(Cmd_Read_SC)); cout << " ReadVME_SC SEND request. Length = " << REMOTE_MSG_SIZE(Cmd_Read_SC) << endl; if( (Socket->RecvRaw(&IncomingMsg.Length, 4) == 4) && (NTOHL(IncomingMsg.Length) <= sizeof(RemoteMsgStruct))) { // cout << " TEST = " << NTOHL(IncomingMsg.Length) << endl; Socket->RecvRaw(&IncomingMsg.BoardType, NTOHL(IncomingMsg.Length)); // cout << " TEST = " << RecvRaw(&IncomingMsg.BoardType, NTOHL(IncomingMsg.Length)) << endl; // (RecvRaw(&IncomingMsg.BoardType, NTOHL(IncomingMsg.Length)) == (int)NTOHL(IncomingMsg.Length)) ) // { cout << " ReadVME_SC receive Scalers " << endl; IncomingMsg.Length = NTOHL(IncomingMsg.Length); IncomingMsg.BoardType = NTOHL(IncomingMsg.BoardType); IncomingMsg.MsgType = NTOHL(IncomingMsg.MsgType); cout << " Length = " << IncomingMsg.Length << " Board = " << IncomingMsg.BoardType << " Type = " << IncomingMsg.MsgType << endl; Cmd_Read_SC *p = (Cmd_Read_SC *)IncomingMsg.Msg; int status =0; int fslot = NTOHL(p->ID); status = NTOHL(p->Status); cout << " Incoming SLOT: "<< fslot <<" Incoming SATUS: " << status << endl; if(status < 0) return kFALSE; for(unsigned int ch = 0; ch < 17; ch++){ val[ch] = NTOHL(p->Value[ch]); cout << val[ch] << " "; } cout << endl; return kTRUE; } // Close(); return kFALSE; } TSocket *Socket; /* socket for connection */ const char *host; int port; int BoardType; private: RemoteMsgStruct IncomingMsg; RemoteMsgStruct OutgoingMsg; }; class My_test : public TGMainFrame{ public: My_test(const TGWindow *p) : TGMainFrame(p, 900, 450){ FILE *fd; // char rname[20] = "roctof1"; // char rname[20] = "rocbcal5"; // char rname[20] = "rocps1"; char rname[20] = "roctof1"; // char rname[20] = "rocfcal10"; // char rname[20] = "rocfcal3"; SetCleanup(kDeepCleanup); // Check if client program is already running somewhere CHECK_CLIENT_RUNNING(DETECTOR) // Determine port number sprintf(fname,"%s/%s.gui", fGUI_dir, rname); if((fd = fopen(fname,"r")) == NULL){ printf(BOLDRED "\n Can't open config file >%s<\n\n" RESET, fname); printf("\n Try to connect using the default port 9090 \n"); Port = 9090; } else { fgets(str_tmp, 255, fd); sscanf (str_tmp, "%d\n", &Port); printf(BOLDBLUE "\n get Port = %d for %s from file >%s<\n\n" RESET,Port, rname, fname); fclose(fd); } pClient = new VMERemote(rname, Port); int nch = 44; Float_t x_min = 0.5, x_max = 44.5, y_min = -10, y_max = 1000000; for(int plot = 0; plot < 4; plot++){ char tmp[20] = ""; if(plot == 0) sprintf(tmp,"TOF North"); if(plot == 1) sprintf(tmp,"TOF Up"); if(plot == 2) sprintf(tmp,"TOF South"); if(plot == 3) sprintf(tmp,"TOF Down"); hfadc[plot] = new TProfile(tmp, tmp, nch, x_min, x_max, y_min, y_max,""); hfadc[plot]->GetYaxis()->SetTitle(" Rate (Hz) "); hfadc[plot]->GetXaxis()->SetTitle(" Paddle"); hfadc[plot]->SetLabelSize(0.075,"xy"); hfadc[plot]->SetTitleSize(0.08,"xy"); hfadc[plot]->SetTitleOffset(1.1,"x"); hfadc[plot]->SetTitleOffset(0.7,"y"); } for(int plot = 0; plot < 4; plot++){ char tmp[20] = ""; if(plot == 0) sprintf(tmp,"TOF North"); if(plot == 1) sprintf(tmp,"TOF Up"); if(plot == 2) sprintf(tmp,"TOF South"); if(plot == 3) sprintf(tmp,"TOF Down"); hped[plot] = new TProfile(tmp, tmp, nch, x_min, x_max, y_min, y_max,""); hped[plot]->GetYaxis()->SetTitle(" fadc count "); hped[plot]->GetXaxis()->SetTitle(" Paddle"); hped[plot]->SetLabelSize(0.075,"xy"); hped[plot]->SetTitleSize(0.08,"xy"); hped[plot]->SetTitleOffset(1.1,"x"); hped[plot]->SetTitleOffset(0.7,"y"); } gStyle->SetOptStat(0); gStyle->SetLineWidth(1.5); gStyle->SetTextSize(1.5); gStyle->SetTitleFont(30,"xy"); gStyle->SetLabelFont(30,"xy"); AddFrame(pFrameTop = new TGVerticalFrame(this), new TGLayoutHints(kLHintsExpandX | kLHintsExpandY)); //| kLHintsCenterY pFrameTop->AddFrame(pFrameTimer = new TGHorizontalFrame(pFrameTop), new TGLayoutHints(kLHintsExpandX | kLHintsTop, 0, 0, 40, 40 )); pFrameTimer->AddFrame(fExitButton = new TGTextButton(pFrameTimer, " &Exit ", 515), new TGLayoutHints( kLHintsRight | kLHintsTop, 0, 20, 0, 0)); pFrameTimer->AddFrame(pLabelTimer1 = new TGLabel(pFrameTimer, new TGString("Update Timer (sec): ")), new TGLayoutHints(kLHintsLeft, 20, 2, 2, 0)); pFrameTimer->AddFrame(pValueTimer = new TGNumberEntry(pFrameTimer, 0, 5, 500, TGNumberFormat::kNESInteger, TGNumberFormat::kNEAAnyNumber, TGNumberFormat::kNELLimitMinMax, 1, 20), new TGLayoutHints(kLHintsLeft, 0, 20, 0, 0)); pFrameTimer->AddFrame(fTStartButton = new TGTextButton(pFrameTimer, " &Start ", 510), new TGLayoutHints( kLHintsLeft | kLHintsTop, 0, 20, 0, 0)); pFrameTimer->AddFrame(fTStopButton = new TGTextButton(pFrameTimer, " &Stop ", 511), new TGLayoutHints( kLHintsLeft | kLHintsTop, 0, 20, 0, 0)); pFrameTimer->AddFrame(fTResetButton = new TGTextButton(pFrameTimer, " &Reset ", 512), new TGLayoutHints( kLHintsLeft | kLHintsTop, 0, 20, 0, 0)); pFrameTimer->AddFrame(fTCalibButton = new TGTextButton(pFrameTimer, " &Calib ", 520), new TGLayoutHints( kLHintsLeft | kLHintsTop, 40, 20, 0, 0)); pValueTimer->SetIntNumber(1); pValueTimer->SetWidth(50); pFrameTop->AddFrame(fF1 = new TGCompositeFrame(pFrameTop, 100,200,kHorizontalFrame), new TGLayoutHints(kLHintsTop | kLHintsLeft | kLHintsExpandX | kLHintsExpandY, 5, 5, 0, 0)); for(int frame = 0; frame < 2; frame++){ char tmp[20]; sprintf(tmp,"TOF_%d",frame + 1); fF1->AddFrame(fEcanvas[frame] = new TRootEmbeddedCanvas("tmp",fF1,100,200), new TGLayoutHints(kLHintsLeft | kLHintsExpandX | kLHintsExpandY , 20, 0, 0, 10)); } pFrameTop->AddFrame(fF2 = new TGCompositeFrame(pFrameTop, 100, 200, kHorizontalFrame), new TGLayoutHints(kLHintsTop | kLHintsLeft | kLHintsExpandX | kLHintsExpandY, 5, 5, 0, 0)); for(int frame = 2; frame < 4; frame++){ char tmp[20]; sprintf(tmp,"TOF_%d",frame + 1); fF2->AddFrame(fEcanvas[frame] = new TRootEmbeddedCanvas("tmp",fF2,100,200), new TGLayoutHints(kLHintsLeft | kLHintsExpandX | kLHintsExpandY , 20, 0, 0, 10)); } pFrameTop->AddFrame(fF3 = new TGCompositeFrame(pFrameTop, 100, 200, kHorizontalFrame), new TGLayoutHints(kLHintsTop | kLHintsLeft | kLHintsExpandX | kLHintsExpandY, 5, 5, 0, 0)); for(int frame = 4; frame < 6; frame++){ char tmp[20]; sprintf(tmp,"TOF_%d",frame + 1); fF3->AddFrame(fEcanvas[frame] = new TRootEmbeddedCanvas("tmp",fF3,100,200), new TGLayoutHints(kLHintsLeft | kLHintsExpandX | kLHintsExpandY , 20, 0, 0, 10)); } pFrameTop->AddFrame(fF4 = new TGCompositeFrame(pFrameTop, 100, 200, kHorizontalFrame), new TGLayoutHints(kLHintsTop | kLHintsLeft | kLHintsExpandX | kLHintsExpandY, 5, 5, 0, 0)); for(int frame = 6; frame < 8; frame++){ char tmp[20]; sprintf(tmp,"TOF_%d",frame + 1); fF4->AddFrame(fEcanvas[frame] = new TRootEmbeddedCanvas("tmp",fF4,100,200), new TGLayoutHints(kLHintsLeft | kLHintsExpandX | kLHintsExpandY , 20, 0, 0, 10)); } // gStyle->SetOptStat(0); gStyle->SetTitleFontSize(0.08); for(int frame = 0; frame < 4; frame++){ fCanvas[frame] = fEcanvas[frame]->GetCanvas(); fCanvas[frame]->cd(); hfadc[frame]->GetXaxis()->SetNdivisions(508,-1); hfadc[frame]->Draw(); gPad->SetBottomMargin(0.18); gPad->SetLeftMargin(0.15); } for(int frame = 4; frame < 8; frame++){ fCanvas[frame] = fEcanvas[frame]->GetCanvas(); fCanvas[frame]->cd(); hped[frame - 4]->GetXaxis()->SetNdivisions(508,-1); hped[frame - 4]->Draw("err"); gPad->SetBottomMargin(0.18); gPad->SetLeftMargin(0.15); } pValueTimer->Associate(this); fTStartButton->Associate(this); fTStopButton->Associate(this); fTResetButton->Associate(this); fExitButton->Associate(this); fTCalibButton->Associate(this); my_timer = new TTimer(this, 10, kTRUE); MapSubwindows(); Resize(GetDefaultSize()); Resize(900,800); MapWindow(); }; /*******/ Bool_t ProcessMessage(Long_t msg, Long_t parm1, Long_t){ cout << " I am here " << GET_MSG(msg) << endl; switch (GET_MSG(msg)) { case kC_COMMAND: { switch (GET_SUBMSG(msg)) { case kCM_BUTTON: { switch (parm1) { case 510: { cout << " Clicked Start Timer Button: Activate Timer " << " " << pValueTimer->GetIntNumber() << " s " << endl; my_timer->Stop(); // SASCHA my_timer->Start(10*pValueTimer->GetIntNumber(),kFALSE); // my_timer->Start(100*pValueTimer->GetIntNumber(),kFALSE); break; } case 511: { cout << " Clicked Stop Timer Button " << endl; my_timer->Stop(); break; } case 512: { cout << " Clicked Reset Button. Reset Plots " << endl; for(int plot = 0; plot < 4; plot++){ hped[plot]->Reset(); } break; } case 520: { cout << " Clicked Calib Button. Calib Baseline " << endl; #if 0 // Check Read/Write DAC int board = 5; float ped_3200[17][1000]; memset(ped_3200,0,sizeof(ped_3200)); unsigned int dac_tmp[17]; if( pClient->ReadVME_SC(&dac_tmp[0], 2, board) ){ cout << endl; for(int ii = 0; ii < 16; ii++){ cout << " Channel = " << ii << " DAC = " << dac_tmp[ii] << endl; } } read_fadc_ped(pClient, board, board, fadc_ped, 0); for(int ii = 1; ii < 17; ii++){ pClient->WriteVME_SC(ii, 3200 + ii, 2, board); } cout << " Read New DAQ values " << endl; if( pClient->ReadVME_SC(&dac_tmp[0], 2, board) ){ cout << endl; for(int ii = 0; ii < 16; ii++){ cout << " Channel = " << ii << " DAC = " << dac_tmp[ii] << endl; } } #endif int amp_ped[256][2048]; float mean_3200[256]; float mean_3300[256]; float mean_refine[256]; float amp_sum[256]; int samp_sum[256]; float mean_amp[256]; int adc_index[256]; int adc_max[256]; int entries[256]; int entries1[256]; int amp_mean[256]; memset(amp_ped, 0, sizeof(amp_ped)); memset(mean_3200, 0, sizeof(mean_3200)); memset(mean_3300, 0, sizeof(mean_3300)); memset(mean_refine, 0, sizeof(mean_refine)); memset(samp_sum, 0, sizeof(samp_sum)); memset(mean_amp, 0, sizeof(mean_amp)); memset(amp_sum, 0, sizeof(amp_sum)); memset(adc_index, 0, sizeof(adc_index)); memset(adc_max, 0, sizeof(adc_max)); memset(entries, 0, sizeof(entries)); memset(entries1, 0, sizeof(entries1)); memset(amp_mean, 0, sizeof(amp_mean)); // // STEP 1 // // // Set DAC to 3200 for(int slot = 3; slot < 16; slot++){ if( (slot == 11) || (slot == 12)) continue; for(int ch = 1; ch < 17; ch++){ pClient->WriteVME_SC(ch, 3200, 2, slot); pClient->WriteVME_SC(ch, 3200, 2, slot); } } // Read baseline scalers for(int samp = 0; samp < 5000; samp++){ read_fadc_ped(pClient, 3, 15, fadc_ped, 0); for(int ch = 0; ch < 256; ch++){ int amp_tmp = round(float(fadc_ped[ch]) / 4.); if( (amp_tmp > 0) && (amp_tmp < 2048)){ amp_ped[ch][amp_tmp] ++; } } } // Find peak for(int ch = 0; ch < 256; ch++){ for(int adc_samp = 0; adc_samp < 2048; adc_samp ++){ if(amp_ped[ch][adc_samp] > adc_max[ch]){ adc_index[ch] = adc_samp; adc_max[ch] = amp_ped[ch][adc_samp]; } } } // Compute mean amplitude +- 6 channels around the peak for(int ch = 0; ch < 256; ch++){ int range_min = adc_index[ch] - 5; int range_max = adc_index[ch] + 5; if(range_min > 5 && range_max < 2048){ for(int sample = range_min; sample <= range_max; sample++){ amp_mean[ch] += amp_ped[ch][sample]*sample; entries1[ch] += amp_ped[ch][sample]; } } if(entries1[ch] > 0){ mean_3200[ch] = float(amp_mean[ch])/entries1[ch]; } } // // STEP 2 // // memset(amp_ped, 0, sizeof(amp_ped)); for(int ch = 0; ch < 256; ch++){ adc_index[ch] = 0; adc_max[ch] = 0; amp_mean[ch] = 0; entries[ch] = 0; } // Set DAC to 3300 for(int slot = 3; slot < 16; slot++){ if( (slot == 11) || (slot == 12)) continue; for(int ch = 1; ch < 17; ch++){ pClient->WriteVME_SC(ch, 3250, 2, slot); pClient->WriteVME_SC(ch, 3250, 2, slot); } } // Read baseline scalers for(int samp = 0; samp < 5000; samp++){ read_fadc_ped(pClient, 3, 15, fadc_ped, 0); for(int ch = 0; ch < 256; ch++){ int amp_tmp = round(float(fadc_ped[ch]) / 4.); if( (amp_tmp > 0) && (amp_tmp < 2048)){ amp_ped[ch][amp_tmp] ++; } } } // Find peak for(int ch = 0; ch < 256; ch++){ for(int adc_samp = 0; adc_samp < 2048; adc_samp ++){ if(amp_ped[ch][adc_samp] > adc_max[ch]){ adc_index[ch] = adc_samp; adc_max[ch] = amp_ped[ch][adc_samp]; } } cout << " CH = " << ch << " adc_index = " << adc_index[ch] << endl; } for(int ch = 0; ch < 256; ch++){ cout << " CH = " << ch << endl; for(int adc_samp = 0; adc_samp < adc_index[ch] + 100; adc_samp ++){ cout << " " << amp_ped[ch][adc_samp]; } cout << endl; int range_min = adc_index[ch] - 5; int range_max = adc_index[ch] + 5; if(range_min > 5 && range_max < 2048){ for(int sample = range_min; sample <= range_max; sample++){ entries[ch] += amp_ped[ch][sample]; } } cout << " Entries = " << entries[ch] << " ADC index = " << adc_index[ch] << endl; } for(int ch = 0; ch < 256; ch++){ entries[ch] = 0; } // Compute mean amplitude +- 6 channels around the peak for(int ch = 0; ch < 256; ch++){ int range_min = adc_index[ch] - 5; int range_max = adc_index[ch] + 5; if(range_min > 5 && range_max < 2048){ for(int sample = range_min; sample <= range_max; sample++){ amp_mean[ch] += amp_ped[ch][sample]*sample; entries[ch] += amp_ped[ch][sample]; } } else { cout << " Out of range " << ch << " " << adc_index[ch] << endl; } if(entries[ch] > 0){ mean_3300[ch] = float(amp_mean[ch])/entries[ch]; } } // // STEP 3 // // float slope[256]; float b[256]; float set_dac[256]; for(int ch = 0; ch < 256; ch++){ if(mean_3200[ch] < 0) continue; if(mean_3300[ch] < 0) continue; set_dac[ch] = 0; slope[ch] = (mean_3300[ch] - mean_3200[ch]) / 50.; b[ch] = mean_3300[ch] - slope[ch]*3250.; if( (mean_3200[ch] != 0) && (mean_3300[ch] != 0) && (slope[ch] != 0)) set_dac[ch] = (100. - b[ch])/slope[ch]; cout << "CH = " << ch << " Ent = " << entries[ch] << " " << amp_mean[ch] << " DAC 3300 = " << mean_3300[ch] << " DAC 3200 = " << mean_3200[ch] << " Ent = " << entries1[ch] << " SLOPE = " << slope[ch] << " B = " << b[ch] << " SET DAC = " << set_dac[ch] << " SET DAC = " << round(set_dac[ch]) << endl; } unsigned int set_tmp = 0; cout << endl; // Set calibrated DAC for(int slot = 3; slot < 16; slot++){ if( (slot == 11) || (slot == 12)) continue; int index = 0; cout << "========================= " << endl; cout << "FADC250_SLOTS " << slot << endl; cout << "========================= " << endl; cout << endl; cout << "FADC250_ALLCH_DAC "; for(int ch = 0; ch < 16; ch++){ if(slot < 11) index = 16*(slot - 3) + ch; else index = 16*(slot - 5) + ch; set_tmp = round(set_dac[index]); cout << set_tmp << " "; } cout << endl << endl; } getchar(); // Write new DAC values for(int slot = 3; slot < 16; slot++){ if( (slot == 11) || (slot == 12)) continue; int index = 0; for(int ch = 0; ch < 16; ch++){ if(slot < 11) index = 16*(slot - 3) + ch; else index = 16*(slot - 5) + ch; set_tmp = round(set_dac[index]); if( (set_tmp > 3200) && (set_tmp < 3400)){ pClient->WriteVME_SC(ch + 1, set_tmp , 2, slot); } } } getchar(); read_fadc_ped(pClient, 3, 15, fadc_ped, 0); // // STEP 4 // // int refine_calib = 0; if(refine_calib){ memset(amp_ped, 0, sizeof(amp_ped)); for(int ch = 0; ch < 256; ch++){ adc_index[ch] = 0; adc_max[ch] = 0; amp_mean[ch] = 0; entries1[ch] = 0; } // Read baseline scalers for(int samp = 0; samp < 1000; samp++){ read_fadc_ped(pClient, 3, 15, fadc_ped, 0); for(int ch = 0; ch < 256; ch++){ int amp_tmp = round(float(fadc_ped[ch]) / 4.); if( (amp_tmp > 0) && (amp_tmp < 2048)){ amp_ped[ch][amp_tmp] ++; } } } // Find peak for(int ch = 0; ch < 256; ch++){ for(int adc_samp = 0; adc_samp < 2048; adc_samp ++){ if(amp_ped[ch][adc_samp] > adc_max[ch]){ adc_index[ch] = adc_samp; adc_max[ch] = amp_ped[ch][adc_samp]; } } } // Compute mean amplitude +- 6 channels around the peak for(int ch = 0; ch < 256; ch++){ int range_min = adc_index[ch] - 5; int range_max = adc_index[ch] + 5; if(range_min > 5 && range_max < 2048){ for(int sample = range_min; sample <= range_max; sample++){ amp_mean[ch] += amp_ped[ch][sample]*sample; entries1[ch] += amp_ped[ch][sample]; } } if(entries1[ch] > 0){ mean_refine[ch] = float(amp_mean[ch])/entries1[ch]; } } // Write new DAC values for(int slot = 3; slot < 16; slot++){ if( (slot == 11) || (slot == 12)) continue; int index = 0; cout << " SLOT = " << slot << endl; for(int ch = 0; ch < 16; ch++){ if(slot < 11) index = 16*(slot - 3) + ch; else index = 16*(slot - 5) + ch; set_tmp = round(set_dac[index]); if(mean_refine[index] > 104.){ set_tmp = round(set_dac[index]) + 10; cout << " ADD" << endl; } if(mean_refine[index] < 96.){ set_tmp = round(set_dac[index]) - 10; cout << " SUBTRACT " << endl; } if( (set_tmp > 3200) && (set_tmp < 3400)){ pClient->WriteVME_SC(ch + 1, set_tmp , 2, slot); } cout << " " << mean_refine[index]; // cout << " " << set_tmp; } cout << endl; } } //Bool_t WriteVME_SC(unsigned int addr, unsigned int val, unsigned int Status = 0, unsigned int Slot = 0) break; } case 515: { cout << " Exit " << endl; sprintf(fname,"%s/%s_client.log", fGUI_dir, dname); if((remove(fname)) == 0) printf(" Log file deleted successfully \n\n"); else printf(" Error: unable to delete log file \n\n"); gApplication->Terminate(); break; } break; } break; } } break; } case kC_TEXTENTRY: { switch(GET_SUBMSG(msg)) { case kTE_TEXTCHANGED: { switch(parm1) { case 500: { cout << " Timer rate will be set after pressing Start button : " << pValueTimer->GetIntNumber() << endl; break; } } break; } break; } break; } } return kTRUE; }; int read_fadc_ped(VMERemote *pClient_tmp, unsigned int slot_min, unsigned int slot_max, float ped_tmp[256], int crate){ unsigned int ped_all[128]; memset(ped_all, 0, sizeof(ped_all)); int debug = 0; if( pClient_tmp->ReadVME_PED_ALL(&ped_all[0], 0, slot_min, slot_max) ){ for(int bd = 0; bd < 16; bd++){ unsigned int bd_slot; if(bd < 8) bd_slot = bd + 3; else bd_slot = bd + 5; if(bd_slot >= slot_min && bd_slot <= slot_max) if(debug) cout << "read_fadc_ped: Slot = " << bd_slot << endl; for(int ch = 0; ch < 8; ch++) { int ind_ch = bd*16 + ch*2; unsigned int ped1 = ped_all[bd*8 + ch] & 0x3FFF; unsigned int ped2 = (ped_all[bd*8 + ch] & 0x3FFF0000) >> 16; ped_tmp[ind_ch] = float(ped1); ped_tmp[ind_ch+1] = float(ped2); if(bd_slot >= slot_min && bd_slot <= slot_max) if(debug) cout << " Ch = " << 2*ch << " " << ped1 << " Ch = " << 2*ch + 1 << " ped2 " << ped2 << endl; } } } else { cout << " : Error reading pedestals from server: " << (pClient_tmp->Socket->GetInetAddress()).GetHostName()<<" : "<< (pClient_tmp->Socket->GetInetAddress()).GetHostAddress()<< endl; //** need to reconnect here? if(!pClient_tmp->Reconnect()) cout << "-->> ERROR : Cannot reconnect to server: " << (pClient_tmp->Socket->GetInetAddress()).GetHostName()<<" : "<< (pClient_tmp->Socket->GetInetAddress()).GetHostAddress()<< endl; else{ cout << "**>> Connection to server restored: " << (pClient_tmp->Socket->GetInetAddress()).GetHostName()<<" : "<< (pClient_tmp->Socket->GetInetAddress()).GetHostAddress()<< endl; } } return 0; } int read_fadc_sc(VMERemote *pClient_tmp, unsigned int slot_min, unsigned int slot_max, float rate_tmp[256], int fadc_time_prev[16][16], int fadc_sc_prev[16][256], int crate){ Float_t time = 0.; int fadc_time; unsigned int sc_all[272]; memset(sc_all, 0, sizeof(sc_all)); // cout << " I am here ==== " << " Slot min = " << slot_min << " Slot_max = " // << slot_max << "pClient " << pClient_tmp << endl; if( pClient_tmp->ReadVME_SC_ALL(&sc_all[0], 0, slot_min, slot_max) ){ for(int bd = 0; bd < 16; bd++){ int time_cnt = 17*bd + 16; fadc_time = sc_all[time_cnt]; time = ((Float_t) (fadc_time - fadc_time_prev[crate][bd]))*2048/1.e9; // time = ((Float_t) sc_all[time_cnt])*2048/1.e9; /* time in sec between scalers readout (one count of scaler timer(last channel from scaler readout) is 2048ns */ // cout << " Board = " << bd << " FADC time prev = " << fadc_time_prev[crate][bd] << " FADC time = " << fadc_time << endl; // cout << " Board = " << bd << " Timer = " << time <<" sec" << endl << endl; if(time > 0) { for(int ch = 0; ch < MAXCH; ch++) { int ch_ind = 17*bd + ch; int rate_ind = 16*bd + ch; if( (sc_all[ch_ind] - fadc_sc_prev[crate][rate_ind]) <= 0){ rate_tmp[rate_ind] = 0; } else rate_tmp[rate_ind] = (sc_all[ch_ind] - fadc_sc_prev[crate][rate_ind]) /time; /*rate in Hz */ // cout << " CH " << ch_ind << " " << sc_all[ch_ind] << " " << rate[ch] << " Hz/s "; fadc_sc_prev[crate][rate_ind] = sc_all[ch_ind]; } } else { int my_slot; if(bd < 8) my_slot = bd + 3; else my_slot = bd + 5; // cout << "-->> ERROR: Slot: " << my_slot << " Timer channel(16) returns zero: Don't update Scalers rate " << endl; } // time fadc_time_prev[crate][bd] = fadc_time; } // loop over boards } else { cout << " : Error reading scalers from server: " << (pClient_tmp->Socket->GetInetAddress()).GetHostName()<<" : "<< (pClient_tmp->Socket->GetInetAddress()).GetHostAddress()<< endl; //** need to reconnect here? if(!pClient_tmp->Reconnect()) cout << "-->> ERROR : Cannot reconnect to server: " << (pClient_tmp->Socket->GetInetAddress()).GetHostName()<<" : "<< (pClient_tmp->Socket->GetInetAddress()).GetHostAddress()<< endl; else{ cout << "**>> Connection to server restored: " << (pClient_tmp->Socket->GetInetAddress()).GetHostName()<<" : "<< (pClient_tmp->Socket->GetInetAddress()).GetHostAddress()<< endl; } } return 0; } int read_ctp_sc(VMERemote *pClient_tmp, float ctp_rate[7], unsigned long long &ctp_time_prev){ unsigned int ctp_sc[17]; unsigned long long ctp_time; int debug_ctp = 0; memset(ctp_sc,0,sizeof(ctp_sc)); if( pClient_tmp->ReadVME_SC(&ctp_sc[0], 10, 3) ) { cout << " Timestamp: " << std::hex << ctp_sc[0] << " " << ctp_sc[1] << std::dec << endl; cout << "Left Arm Counter: " << ctp_sc[2] << endl; cout << "Right Arm Counter: " << ctp_sc[3] << endl; cout << "Coinc Counter: " << ctp_sc[4] << endl; int hit_right = ctp_sc[5]; int hit_left = ctp_sc[6]; cout << "Hit channel (R) : " << hit_right << " (L) : " << hit_left << endl; ctp_time = (((uint64_t) ctp_sc[0] <<32 ) | ctp_sc[1] ); Float_t ctp_time_stamp = ((Float_t) (ctp_time - ctp_time_prev))*4./1.e9; if(debug_ctp){ cout << std::hex << " OLD TIME = " << ctp_time_prev << " NEW TIME = " << ctp_time << std::dec << endl; cout << " TIME DIFFERENCE = " << ctp_time_stamp << endl; } if( ctp_time != ctp_time_prev ){ ctp_rate[2] = ctp_sc[2] / ctp_time_stamp; ctp_rate[3] = ctp_sc[3] / ctp_time_stamp; ctp_rate[4] = ctp_sc[4] / ctp_time_stamp; ctp_rate[5] = ctp_sc[5]; ctp_rate[6] = ctp_sc[6]; } else { ctp_rate[2] = 0; ctp_rate[3] = 0; ctp_rate[4] = 0; ctp_rate[5] = 0; ctp_rate[6] = 0; } cout << " ------------------- " << endl; cout << " RATE FOR HIGH-GRAN COUNTERS = " << ctp_rate[4] << endl; cout << " ------------------- " << endl; ctp_time_prev = ctp_time; } return 0; } /*******/ virtual Bool_t HandleTimer(TTimer *t) { unsigned int debug_scalers = 0; unsigned int debug_ped = 0; // static unsigned long long ctp_time_prev_ps; // static unsigned long long ctp_time_prev_psc; static int fadc_sc_prev[16][256]; static int fadc_time_prev_all[16][16]; memset(fadc_rate, 0, sizeof(fadc_rate)); memset(fadc_rate, 0, sizeof(fadc_ped)); read_fadc_sc(pClient, 3, 15, fadc_rate, fadc_time_prev_all, fadc_sc_prev, 0); read_fadc_ped(pClient, 3, 15, fadc_ped, 0); if(debug_scalers){ cout << " ===== Print Scalers ==== " << endl; for(int ii = 0; ii < 16; ii++){ for(int jj = 0; jj < 16; jj++){ int ind = 16*ii + jj; cout << " " << fadc_rate[ind]; } cout << endl; } } UpdatePlot(); cout << " Timer " << endl; return kTRUE; }; void UpdatePlot(){ unsigned int debug_plot = 0; for(int plot = 0; plot < 4; plot++){ hfadc[plot]->Reset(); if(plot == 0){ if(debug_plot) cout << " Plot 0" << endl; for(int ch = 0; ch < 44; ch++){ hfadc[plot]->Fill( (Float_t) ch + 1, fadc_rate[ch]); hped[plot]->Fill( (Float_t) ch + 1, fadc_ped[ch]); if(debug_plot) cout << " " << fadc_rate[ch]; } } cout << endl; if(plot == 1){ if(debug_plot) cout << " Plot 1" << endl; for(int ch = 88; ch < 132; ch++){ hfadc[plot]->Fill( (Float_t) ch - 87, fadc_rate[ch]); hped[plot]->Fill( (Float_t) ch - 87, fadc_ped[ch]); if(debug_plot) cout << " " << fadc_rate[ch]; } } cout << endl; if(plot == 2){ if(debug_plot) cout << " Plot 2" << endl; for(int ch = 44; ch < 88; ch++){ hfadc[plot]->Fill( (Float_t) ch - 43, fadc_rate[ch]); hped[plot]->Fill( (Float_t) ch - 43, fadc_ped[ch]); if(debug_plot) cout << " " << fadc_rate[ch]; } } cout << endl; if(plot == 3 || plot == 8){ if(debug_plot) cout << " Plot 3" << endl; for(int ch = 132; ch < 176; ch++){ hfadc[plot]->Fill( (Float_t) ch - 131, fadc_rate[ch]); hped[plot]->Fill( (Float_t) ch - 131, fadc_ped[ch]); if(debug_plot) cout << " " << fadc_rate[ch]; } } cout << endl; } for(int plot = 0; plot < 4; plot++){ fCanvas[plot]->cd(); hfadc[plot]->Draw("histo"); fCanvas[plot]->Update(); } for(int plot = 0; plot < 4; plot++){ fCanvas[plot+4]->cd(); hped[plot]->Draw("err"); fCanvas[plot+4]->Update(); } } private: TGVerticalFrame *pFrameTop; TGHorizontalFrame *pFrameBot; TGVerticalFrame *pFrameLeft; TGVerticalFrame *pFrameRight; TGHorizontalFrame *pFrameLabel; TGHorizontalFrame *pFrameLabel1; TGHorizontalFrame *pFrameTimer; TGLabel *pLabelHost; TGLabel *pLabelTimer1; TGLabel *pLabelRate; TGLabel *pLabelRate1; TGNumberEntry *pValueTimer; TGTextButton *fTStartButton, *fTStopButton, *fTResetButton, *fExitButton, *fTCalibButton; TProfile *hfadc[4]; TProfile *hped[4]; TH1F *hctp_ps_nleft, *hctp_ps_nright; TH1F *hctp_psc_nleft, *hctp_psc_nright; TH1F *hctp_en; TProfile *hctp_ps, *hctp_psc; TProfile *hctp; TH1F *hctp_nleft, *hctp_nright; TCanvas *fCanvas[8]; TRootEmbeddedCanvas *fEcanvas[8]; TGCompositeFrame *fF1, *fF2, *fF3, *fF4; TTimer *my_timer; TGTextBuffer *tbuf_host, *tbuf_rate, *tbuf_rate1; TGTextEntry *tent_host, *tent_rate, *tent_rate1; Float_t fadc_rate[256]; /* fadc scalers for all boards in the crate, PS */ Float_t fadc_ped[256]; /* fadc pedestals */ Float_t ctp_rate_ps[7]; Float_t ctp_rate_psc[7]; VMERemote *pClient; int Port; // Port string Host; // Host }; /***************************************************/ /***************************************************/ int main(int argc, char* argv[]) { int cargc = 1; TApplication App("Diagnostic GUI", &cargc, argv); // signal(SIGILL, SIG_DFL); // signal(SIGSEGV, SIG_DFL); // signal(SIGBUS, SIG_DFL); if (signal(SIGINT, sig_handler) == SIG_ERR) printf("\ncan't catch SIGINT\n"); if (signal(SIGTERM, sig_handler) == SIG_ERR) printf("\ncan't catch SIGTERM\n"); if(gROOT->IsBatch()) { fprintf(stderr, "%s: cannot run in batch mode\n", argv[0]); return 1; } My_test *a = new My_test(gClient->GetRoot()); a->SetWindowName("TOF FADC scalers"); /* example to set the same threshold for all channels */ // if(a->pClient->WriteVME_SC(0, 150, 5, 16)) /* send default thresholds 150mv for all channels in slot 16*/ // for(int i=0;i<16;i++) a->SetGUIThreshold(i,150); App.Run(); return 0; }