Changeset 10117 for fact/FADctrl/FAD.cc


Ignore:
Timestamp:
01/24/11 16:48:50 (13 years ago)
Author:
ogrimm
Message:
Amplitude calibration integrated into event loop
File:
1 edited

Legend:

Unmodified
Added
Removed
  • fact/FADctrl/FAD.cc

    r10114 r10117  
    3737   //{"rmode", &FAD::cmd_rmode, 0, "<first|stop>", "Readout start at first bin or stop position (DRS4)"},
    3838   //{"dmode", &FAD::cmd_dmode, 0, "<single|continuous>", "Domino wave single shot or continuous"},
    39    {"cancel", &FAD::cmd_cancel, false, 0, "", "Cancel current operation/run"},
     39   {"stop", &FAD::cmd_stop, false, 0, "", "Stop current operation/run"},
    4040   {"update", &FAD::cmd_update, false, 1, "<sec>", "Minimum delay between updates to DIM event service"},                 
    4141   {"socketmode", &FAD::cmd_socketmode, true, 1, "<com|daq>", "Choose which Sockets are used for data transmission"},             
     
    167167          // Check if idle mode required
    168168          if (CommandList[i].NeedIdle && Mode != idle) {
    169                 PrintMessage("Current mode is not idle ('cancel' will stop current operation)\n");
     169                PrintMessage("Current mode is not idle ('stop' will stop current operation)\n");
    170170                return;
    171171          }
     
    285285  if (Match(Parameter[1],"enable")) PrintMessage("all active boards accept now incoming triggers\n");
    286286  else if (Match(Parameter[1],"disable")) PrintMessage("no active board accepts any incoming trigger anymore.\n");
    287  // else PrintUsage();
    288 
    289  
    290287}
    291288
     
    483480void FAD::cmd_acalib() {
    484481
    485   pthread_t Thread;
    486   int Code, Count;
     482  int Count;
    487483  FILE *File;
    488484  vector<string> Items;
     
    495491
    496492  // Read calibration data from file?
    497   if (Parameter.size() == 2 && !ConvertToInt(Parameter[1], &NumCalibEvents)) {
     493  //if (Parameter.size() == 2 && !ConvertToInt(Parameter[1], &NumCalibEvents)) {
     494  if (Parameter.size() == 2 && !ConvertToInt(Parameter[1], &NumEventsRequested)) {
    498495    // Open file
    499496        if ((File = fopen(Parameter[1].c_str(), "r")) == NULL) {
     
    546543
    547544  // Set number of events required for calibration
    548   if (Parameter.size()==1 || !ConvertToInt(Parameter[1], &NumCalibEvents) || NumCalibEvents<=0) {
    549         NumCalibEvents = DEFAULT_NUM_CALIB_EVENTS;
    550   }
    551 
    552   // Set mode before launching thread
     545  if (Parameter.size()==1 || !ConvertToInt(Parameter[1], &NumEventsRequested) || NumEventsRequested<=0) {
     546        NumEventsRequested = DEFAULT_NUM_CALIB_EVENTS;
     547  }
     548
     549  // Start ca;ibration by setting mode
    553550  Mode = acalib;
    554   Cancel = false;
    555  
    556   // Create detached thread
    557   if ((Code = pthread_create(&Thread, NULL, (void * (*)(void *)) FAD::LaunchAmplitudeCalibration,(void *) this)) != 0) {
    558     Message(ERROR, "pthread_create() failed in FAD::cmd_acalib() (%s)\n", strerror(Code));
    559         Mode = idle;
    560         return;
    561   }
    562 
    563   if ((Code = pthread_detach(Thread)) != 0) {
    564         Message(ERROR, "pthread_detach() failed in FAD::cmd_acalib() (%s)\n", strerror(Code));
    565   }
    566551}
    567552
     
    590575          if (Boards[i]->Active) PrintMessage(" %d", i);
    591576        }
     577
     578        // Current mode
     579        if (Mode == idle) PrintMessage("Current mode is IDLE\n");
     580        else if (Mode == acalib) PrintMessage("Current mode is ACALIB (3x%d events)\n", NumEventsRequested);
     581        else if (Mode == datarun) PrintMessage("Current mode is DATARUN (%d events requested, %d events taken)\n", NumEventsRequested, NumEvents);
     582
    592583        return;
    593584  }   
     
    711702// Cancel current operation
    712703//
    713 void FAD::cmd_cancel() {
     704void FAD::cmd_stop() {
    714705
    715706  static char Stop[] = "stop";
    716707
    717   if (Mode == idle) PrintMessage("Nothing to cancel\n");
    718   else {
     708  if (Mode == idle) {
     709        PrintMessage("Nothing to stop\n");
     710        return;
     711  }
     712 
     713  if (Mode == acalib) Mode = idle;
     714 
     715  if (Mode == datarun) { 
    719716        // Inform event thread to stop run in case datarun active
    720717        if (write(Pipe[1], Stop, strlen(Stop)+1) == -1) {
    721718          Message(ERROR, "write() to Pipe[1] failed in FAD::cmd_cancel() (%s)", strerror(errno));
    722719        }
    723     Cancel = true;
    724     PrintMessage("Requested cancelation of current operation\n");
    725   }
     720  }
     721 
     722  PrintMessage("Requested stopping of current operation\n");
    726723}
    727724
     
    741738
    742739//
    743 // Amplitude calibration (lauched as thread by cmd_acalib())
    744 //
    745 void FAD::AmplitudeCalibration() {
    746 
    747   vector<struct FADBoard::BoardStatus> Status;
    748   vector<unsigned short> ROICmd;
    749   unsigned short DACCmd[] = {htons(CMD_Write | (BADDR_DAC + 1)), 0, htons(CMD_Write | (BADDR_DAC + 2)), 0, htons(CMD_Write | (BADDR_DAC + 3)), 0};
    750 
    751   PrintMessage("Staring amplitude calibration of all active boards (%d events)\n"
    752                            "   Note: No input signals must be connected\n", NumCalibEvents);
    753  
    754   // Prepare command to set all ROIs to 1024
    755   for (unsigned int i=0; i<NChips*NChannels; i++) {
    756         ROICmd.push_back(htons(CMD_Write | (BADDR_ROI + i)));
    757         ROICmd.push_back(htons(NBins));
    758   }
    759 
    760   // ====== Part A: Baseline measurement =====
    761 
    762   // Initialise settings for baseline measurement
    763   for (unsigned int Brd=0; Brd<Boards.size(); Brd++) {
    764 
    765         // Invalidate current calibration
    766         Boards[Brd]->ACalibTime = -1;
    767 
    768         // Save initial board status
    769         Status.push_back(Boards[Brd]->GetStatus());
    770        
    771     // Set all ROI to 1024
    772         Boards[Brd]->Send(&ROICmd[0], ROICmd.size()*sizeof(unsigned short));
    773 
    774     // Set DAC values and start accumulation
    775     DACCmd[1] = htons(0);
    776     DACCmd[3] = htons(0);
    777     DACCmd[5] = htons(0);
    778         Boards[Brd]->Send(DACCmd, sizeof(DACCmd));
    779 
    780     Boards[Brd]->AccumulateSum(NumCalibEvents, true);   
    781   }
    782 
    783   // Wait until data for all boards taken
    784   bool Done = false;
    785   while (!Done && !Cancel) {
    786     usleep(300000);
    787         for (unsigned int Brd=0; Brd<Boards.size(); Brd++) {
    788           Done = true;
    789           if (Boards[Brd]->Active && Boards[Brd]->DoSum) Done = false;
    790         }
    791   }
    792 
    793   // Determine baseline
    794   for (unsigned int Brd=0; Brd<Boards.size(); Brd++) {
    795         for (unsigned int i=0; i<NChips; i++) for (unsigned int j=0; j<NChannels; j++) {
    796           for (unsigned int k=0; k<NBins; k++) {
    797                 Boards[Brd]->Baseline[i][j][k] = Boards[Brd]->Sum[i][j][k] / NumCalibEvents;
    798           }
    799         }
    800   }
    801   PrintMessage("Baseline measurement finished, next is gain measurement\n");
    802 
    803   // ====== Part B: Gain measurement =====
    804 
    805   // Set new DAC values and start accumulation
    806   for (unsigned int Brd=0; Brd<Boards.size(); Brd++) {
    807         DACCmd[1] = htons(50000);
    808         DACCmd[3] = htons(50000);
    809         DACCmd[5] = htons(50000);
    810         Boards[Brd]->Send(DACCmd, sizeof(DACCmd));
    811 
    812     Boards[Brd]->AccumulateSum(NumCalibEvents, true);   
    813   }
    814 
    815   // Wait until data for all boards taken
    816   Done = false;
    817   while (!Done && !Cancel) {
    818     usleep(300000);
    819         for (unsigned int Brd=0; Brd<Boards.size(); Brd++) {
    820           Done = true;
    821           if (Boards[Brd]->Active && Boards[Brd]->DoSum) Done = false;
    822         }
    823   }
    824  
    825   // Determine gain
    826   for (unsigned int Brd=0; Brd<Boards.size(); Brd++) {
    827         for (unsigned int i=0; i<NChips; i++) for (unsigned int j=0; j<NChannels; j++) {
    828           for (unsigned int k=0; k<NBins; k++) {
    829                 Boards[Brd]->Gain[i][j][k] = (Boards[Brd]->Sum[i][j][k] / NumCalibEvents)-Boards[Brd]->Baseline[i][j][k];
    830           }
    831         }
    832   }
    833   PrintMessage("Gain measurement finished, next is secondary calibration\n");
    834 
    835   // ====== Part C: Secondary calibration =====
    836 
    837   // Initialise settings for secondary calibration and start accumulation
    838   for (unsigned int Brd=0; Brd<Boards.size(); Brd++) {
    839     DACCmd[1] = htons(0);
    840     DACCmd[3] = htons(0);
    841     DACCmd[5] = htons(0);
    842         Boards[Brd]->Send(DACCmd, sizeof(DACCmd));
    843 
    844     Boards[Brd]->AccumulateSum(NumCalibEvents, false); 
    845   }
    846 
    847   // Wait until data for all boards taken
    848   Done = false;
    849   while (!Done && !Cancel) {
    850     usleep(300000);
    851         for (unsigned int Brd=0; Brd<Boards.size(); Brd++) {
    852           Done = true;
    853           if (Boards[Brd]->Active && Boards[Brd]->DoSum) Done = false;
    854         }
    855   }
    856 
    857   // Determine secondary calibration
    858   for (unsigned int Brd=0; Brd<Boards.size(); Brd++) {
    859         for (unsigned int i=0; i<NChips; i++) for (unsigned int j=0; j<NChannels; j++) {
    860           for (unsigned int k=0; k<NBins; k++) {
    861                 Boards[Brd]->SecondaryBaseline[i][j][k] = Boards[Brd]->Sum[i][j][k] / (double) NumCalibEvents;
    862           }
    863         }
    864   }
    865 
    866   // ===== Part D: Finish calibration =====
    867  
    868   // Write back original ROI and DAC settings
    869   for (unsigned int Brd=0; Brd<Boards.size(); Brd++) {
    870 
    871         // Determine gain
    872         for (unsigned int i=0; i<NChips; i++) for (unsigned int j=0; j<NChannels; j++) {
    873           for (unsigned int k=0; k<NBins; k++) {
    874                 Boards[Brd]->Gain[i][j][k] = (Boards[Brd]->Sum[i][j][k] / NumCalibEvents)-Boards[Brd]->Baseline[i][j][k];
    875           }
    876         }
    877 
    878         ROICmd.clear();
    879         for (unsigned int i=0; i<NChips*NChannels; i++) {
    880           ROICmd.push_back(htons(CMD_Write | (BADDR_ROI + i)));
    881           ROICmd.push_back(htons(Status[Brd].ROI[i/NChannels][i%NChannels]));
    882         }
    883         Boards[Brd]->Send(&ROICmd[0], ROICmd.size()*sizeof(unsigned short));
    884  
    885     DACCmd[1] = htons(Status[Brd].DAC[1]);
    886     DACCmd[3] = htons(Status[Brd].DAC[2]);
    887     DACCmd[5] = htons(Status[Brd].DAC[3]);
    888         Boards[Brd]->Send(DACCmd, sizeof(DACCmd));
    889        
    890         // Store calibration time and temperature
    891         Boards[Brd]->ACalibTime = Cancel ? -1 : time(NULL);
    892         Boards[Brd]->ACalibTemp = 0;
    893         for (unsigned int i=0; i<NTemp; i++) Boards[Brd]->ACalibTemp += Status[Brd].Temp[i] / NTemp;
    894   }
    895 
    896   Mode = idle;
    897  
    898   if (Cancel) {
    899     PrintMessage("Amplitude calibration cancelled\n");
    900         return;
    901   }
    902  
     740// Save amplitude calibration data to file
     741//
     742void FAD::SaveAmplitudeCalibration() {
     743
    903744  PrintMessage("Amplitude calibration of all active boards finished, original ROI and DAC set\n");
    904745 
     
    942783}
    943784
    944 // Launch read thread inside class
    945 void FAD::LaunchAmplitudeCalibration(class FAD *m) {
    946 
    947   m->AmplitudeCalibration();
    948 }
    949 
    950 
    951785//
    952786// Event thread (publishes/writes M0 format)
     
    958792  struct FADBoard::BoardStatus S;
    959793  vector<unsigned long> EventNumbers(Boards.size());
     794  vector<bool> AcalibDone(Boards.size());
    960795  double Temp;
    961796  string IDString;
     
    1012847        IDString = string(Buffer, Ret);
    1013848       
     849        // If amplitude calibration mode, check if board finished procedure
     850        if (Mode == acalib) {
     851          bool Done = true;
     852          for (unsigned int i=0; i<Boards.size(); i++) {
     853                if (IDString.find(string("ACALIBDONE")+Boards[i]->Name) != string::npos) AcalibDone[i] = true;
     854                if (!AcalibDone[i]) Done = false;
     855          }
     856          // Amplitude calibration finished?
     857          if (Done) {
     858            SaveAmplitudeCalibration();
     859                Mode = idle;
     860          }
     861        }
     862        else for (unsigned int i=0; i<Boards.size(); i++) AcalibDone[i] = false;
     863
    1014864        // Update run and event header with current time
    1015865        gettimeofday(&Time, NULL);
     
    1053903        for (unsigned int Brd=0; Brd<Boards.size(); Brd++) {
    1054904          // Identify board
    1055           if (IDString.find(Boards[Brd]->Name) == string::npos) continue;
     905          if (IDString.find(string("EVENT")+Boards[Brd]->Name) == string::npos) continue;
    1056906
    1057907          // Fill M0 BoardStructure             
     
    1140990        NumEvents++;
    1141991        FileSize += EventSize-Offset;
    1142        
    1143         printf("Wrote event %d\n", NumEvents-1);
    1144992  }
    1145993
Note: See TracChangeset for help on using the changeset viewer.