<commit_before>#include "fixed_probe_sequencer.hh"

namespace g2field {

FixedProbeSequencer::FixedProbeSequencer(std::string conf_file, int num_probes) :
  EventManagerBase()
{
  conf_file_ = conf_file;
  num_probes_ = num_probes;

  Init();
}

FixedProbeSequencer::~FixedProbeSequencer()
{
  for (auto &val : mux_boards_) {
     delete val;
  }

  for (auto &val : dio_triggers_) {
     delete val;
  }
}

int FixedProbeSequencer::Init()
{
  go_time_ = false;
  thread_live_ = false;

  sequence_in_progress_ = false;
  builder_has_finished_ = true;
  mux_round_configured_ = false;
  analyze_fids_online_ = false;
  use_fast_fids_class_ = false;
  got_software_trg_ = false;
  got_start_trg_ = false;

  // Change the logfile if there is one in the config.
  boost::property_tree::ptree conf;
  boost::property_tree::read_json(conf_file_, conf);
  SetLogfile(conf.get<std::string>("logfile", logfile_));
}

int FixedProbeSequencer::BeginOfRun()
{
  boost::property_tree::ptree conf;
  boost::property_tree::read_json(conf_file_, conf);

  LogMessage("BeginOfRun executed - configuring with %s", conf_file_.c_str());

  // First set the config-dir if there is one.
  hw::conf_dir = conf.get<std::string>("config_dir", hw::conf_dir);
  fid_conf_file_ = conf.get<std::string>("fid_conf_file", "");
  analyze_fids_online_ = conf.get<bool>("analyze_fids_online", false);
  use_fast_fids_class_ = conf.get<bool>("use_fast_fids_class", false);

  generate_software_triggers_ = 
    conf.get<bool>("generate_software_triggers", false);

  if (fid_conf_file_ != std::string("")) {

    if ((fid_conf_file_[0] != '/') && (fid_conf_file_[0] != '\\')) {
      fid::load_params(hw::conf_dir + fid_conf_file_);

    } else {

      fid::load_params(fid_conf_file_);
    }
  }

  int sis_idx = 0;
  for (auto &v : conf.get_child("devices.sis_3302")) {

    std::string name(v.first);
    std::string dev_conf_file = std::string(v.second.data());

    if (dev_conf_file[0] != '/') {
      dev_conf_file = hw::conf_dir + std::string(v.second.data());
    }

    sis_idx_map_[name] = sis_idx++;

    LogDebug("loading hw: %s, %s", name.c_str(), dev_conf_file.c_str());
    workers_.PushBack(new hw::Sis3302(name, 
  				      dev_conf_file, 
  				      NMR_FID_LENGTH_ONLINE));
  }

  for (auto &v : conf.get_child("devices.sis_3316")) {

    std::string name(v.first);
    std::string dev_conf_file = std::string(v.second.data());

    if (dev_conf_file[0] != '/') {
      dev_conf_file = hw::conf_dir + std::string(v.second.data());
    }

    sis_idx_map_[name] = sis_idx++;

    LogDebug("loading hw: %s, %s", name.c_str(), dev_conf_file.c_str());
    workers_.PushBack(new hw::Sis3316(name, 
   				      dev_conf_file, 
   				      NMR_FID_LENGTH_ONLINE));
  }



  // Set up the NMR pulser triggers
  dio_triggers_.resize(0);

  for (auto &v : conf.get_child("devices.dio_triggers")) {
    
    char bid = v.second.get<char>("dio_board_id");
    int port = v.second.get<int>("dio_port_num");
    int trg_mask = v.second.get<int>("dio_trg_mask");

    switch (bid) {
      case 'a':
	LogDebug("setting NMR pulser trigger on dio board A, port %i", port);
	dio_triggers_.push_back(new hw::DioTriggerBoard(0x0, hw::BOARD_A, port));
	break;
	
      case 'b':
	LogDebug("setting NMR pulser trigger on dio board B, port %i", port);
	dio_triggers_.push_back(new hw::DioTriggerBoard(0x0, hw::BOARD_B, port));
	break;
      
      case 'c':
	LogDebug("setting NMR pulser trigger on dio board C, port %i", port);
	dio_triggers_.push_back(new hw::DioTriggerBoard(0x0, hw::BOARD_C, port));
	break;

      default:
	LogDebug("setting NMR pulser trigger on dio board D, port %i", port);
	dio_triggers_.push_back(new hw::DioTriggerBoard(0x0, hw::BOARD_D, port));
	break;
    }

    (*dio_triggers_.end())->SetTriggerMask(trg_mask);
  }

  min_event_time_ = conf.get<int>("min_event_time", 1000);
  max_event_time_ = conf.get<int>("max_event_time", 10000);
  mux_switch_time_ = conf.get<int>("mux_switch_time", 10000);

  trg_seq_file_ = conf.get<std::string>("trg_seq_file");

  if (trg_seq_file_[0] != '/') {
    trg_seq_file_ = hw::conf_dir + conf.get<std::string>("trg_seq_file");
  }

  mux_conf_file_ = conf.get<std::string>("mux_conf_file");

  if (mux_conf_file_[0] != '/') {
    mux_conf_file_ = hw::conf_dir + conf.get<std::string>("mux_conf_file");
  }

  // Load trigger sequence
  LogMessage("loading trigger sequence from %s", trg_seq_file_.c_str());
  boost::property_tree::read_json(trg_seq_file_, conf);

  for (auto &mux : conf) {

    int count = 0;
    for (auto &chan : mux.second) {

      // If we are adding a new mux, resize.<commit_msg>Corrects a pointer arithmetic error.<commit_after>#include "fixed_probe_sequencer.hh"

namespace g2field {

FixedProbeSequencer::FixedProbeSequencer(std::string conf_file, int num_probes) :
  EventManagerBase()
{
  conf_file_ = conf_file;
  num_probes_ = num_probes;

  Init();
}

FixedProbeSequencer::~FixedProbeSequencer()
{
  for (auto &val : mux_boards_) {
     delete val;
  }

  for (auto &val : dio_triggers_) {
     delete val;
  }
}

int FixedProbeSequencer::Init()
{
  go_time_ = false;
  thread_live_ = false;

  sequence_in_progress_ = false;
  builder_has_finished_ = true;
  mux_round_configured_ = false;
  analyze_fids_online_ = false;
  use_fast_fids_class_ = false;
  got_software_trg_ = false;
  got_start_trg_ = false;

  // Change the logfile if there is one in the config.
  boost::property_tree::ptree conf;
  boost::property_tree::read_json(conf_file_, conf);
  SetLogfile(conf.get<std::string>("logfile", logfile_));
}

int FixedProbeSequencer::BeginOfRun()
{
  boost::property_tree::ptree conf;
  boost::property_tree::read_json(conf_file_, conf);

  LogMessage("BeginOfRun executed - configuring with %s", conf_file_.c_str());

  // First set the config-dir if there is one.
  hw::conf_dir = conf.get<std::string>("config_dir", hw::conf_dir);
  fid_conf_file_ = conf.get<std::string>("fid_conf_file", "");
  analyze_fids_online_ = conf.get<bool>("analyze_fids_online", false);
  use_fast_fids_class_ = conf.get<bool>("use_fast_fids_class", false);

  generate_software_triggers_ = 
    conf.get<bool>("generate_software_triggers", false);

  if (fid_conf_file_ != std::string("")) {

    if ((fid_conf_file_[0] != '/') && (fid_conf_file_[0] != '\\')) {
      fid::load_params(hw::conf_dir + fid_conf_file_);

    } else {

      fid::load_params(fid_conf_file_);
    }
  }

  int sis_idx = 0;
  for (auto &v : conf.get_child("devices.sis_3302")) {

    std::string name(v.first);
    std::string dev_conf_file = std::string(v.second.data());

    if (dev_conf_file[0] != '/') {
      dev_conf_file = hw::conf_dir + std::string(v.second.data());
    }

    sis_idx_map_[name] = sis_idx++;

    LogDebug("loading hw: %s, %s", name.c_str(), dev_conf_file.c_str());
    workers_.PushBack(new hw::Sis3302(name, 
  				      dev_conf_file, 
  				      NMR_FID_LENGTH_ONLINE));
  }

  for (auto &v : conf.get_child("devices.sis_3316")) {

    std::string name(v.first);
    std::string dev_conf_file = std::string(v.second.data());

    if (dev_conf_file[0] != '/') {
      dev_conf_file = hw::conf_dir + std::string(v.second.data());
    }

    sis_idx_map_[name] = sis_idx++;

    LogDebug("loading hw: %s, %s", name.c_str(), dev_conf_file.c_str());
    workers_.PushBack(new hw::Sis3316(name, 
   				      dev_conf_file, 
   				      NMR_FID_LENGTH_ONLINE));
  }



  // Set up the NMR pulser triggers
  dio_triggers_.resize(0);

  for (auto &v : conf.get_child("devices.dio_triggers")) {
    
    char bid = v.second.get<char>("dio_board_id");
    int port = v.second.get<int>("dio_port_num");
    int trg_mask = v.second.get<int>("dio_trg_mask");

    switch (bid) {
      case 'a':
	LogDebug("setting NMR pulser trigger on dio board A, port %i", port);
	dio_triggers_.push_back(new hw::DioTriggerBoard(0x0, hw::BOARD_A, port));
	break;
	
      case 'b':
	LogDebug("setting NMR pulser trigger on dio board B, port %i", port);
	dio_triggers_.push_back(new hw::DioTriggerBoard(0x0, hw::BOARD_B, port));
	break;
      
      case 'c':
	LogDebug("setting NMR pulser trigger on dio board C, port %i", port);
	dio_triggers_.push_back(new hw::DioTriggerBoard(0x0, hw::BOARD_C, port));
	break;

      default:
	LogDebug("setting NMR pulser trigger on dio board D, port %i", port);
	dio_triggers_.push_back(new hw::DioTriggerBoard(0x0, hw::BOARD_D, port));
	break;
    }

    // Set the trigger mask.
    (*(dio_triggers_.end() - 1))->SetTriggerMask(trg_mask);
  }

  min_event_time_ = conf.get<int>("min_event_time", 1000);
  max_event_time_ = conf.get<int>("max_event_time", 10000);
  mux_switch_time_ = conf.get<int>("mux_switch_time", 10000);

  trg_seq_file_ = conf.get<std::string>("trg_seq_file");

  if (trg_seq_file_[0] != '/') {
    trg_seq_file_ = hw::conf_dir + conf.get<std::string>("trg_seq_file");
  }

  mux_conf_file_ = conf.get<std::string>("mux_conf_file");

  if (mux_conf_file_[0] != '/') {
    mux_conf_file_ = hw::conf_dir + conf.get<std::string>("mux_conf_file");
  }

  // Load trigger sequence
  LogMessage("loading trigger sequence from %s", trg_seq_file_.c_str());
  boost::property_tree::read_json(trg_seq_file_, conf);

  for (auto &mux : conf) {

    int count = 0;
    for (auto &chan : mux.second) {

      // If we are adding a new mux, resize.
{'fraction_non_alphanumeric': 0.15456298200514137, 'fraction_numerical': 0.010389888603256212, 'mean_word_length': 4.167127836192584, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '972949', 'n_tokens_mistral': 3713, 'n_tokens_neox': 3562, 'n_words': 626}