Skip to content

File NPDetectorManager.cxx

File List > core > NPDetectorManager.cxx

Go to the documentation of this file

#include "NPDetectorManager.h"
#include "NPApplication.h"
#include "NPException.h"
#include "NPFunction.h"
#include "NPInputParser.h"
#include <functional>
#include <iostream>
void nptool::DetectorManager::InitDetectorManager() {
  m_app = nptool::Application::GetApplication();
  std::vector<std::string> detector_arg;
  // Detector manager
  if (m_app->HasFlag("--detector")) {
    detector_arg = m_app->GetVectorArg("--detector");
  }
  else if (m_app->HasFlag("-D")) {
    detector_arg = m_app->GetVectorArg("-D");
  }
  else {
    // throw
  }

  for (const auto& file : detector_arg) {
    m_app->AddASCIIFile(file, "detector file", file);
    ReadConfiguration(file);
  }
};

void nptool::DetectorManager::ReadConfiguration(std::string file) {
  nptool::InputParser parser(file);
  auto blocks_token = parser.GetAllBlocksToken();

  std::set<std::string> viewed;
  for (const auto& token : blocks_token) {
    if (viewed.find(token) == viewed.end()) {
      viewed.insert(token);
      m_app->LoadPlugin(token, true);
      auto det = m_app->ConstructDetector(token);
      if (det) {
        det->ReadConfiguration(parser);
        m_detectors[token] = det;
      }
      else {
        message("yellow", "core", "nptool::DetectorManager", token + " does not name a detector");
      }
    }
  }
  m_nbr_det = m_detectors.size();
  return;
}
void nptool::DetectorManager::InitializeDataInputConversion(std::shared_ptr<nptool::VDataInput> input) {
  for (const auto& det : m_detectors) {
    det.second->InitializeDataInputConversion(input);
  }
}
void nptool::DetectorManager::InitializeDataInputRaw(std::shared_ptr<nptool::VDataInput> input) {
  for (const auto& det : m_detectors) {
    det.second->InitializeDataInputRaw(input);
  }
}
void nptool::DetectorManager::InitializeDataInputPhysics(std::shared_ptr<nptool::VDataInput> input) {
  for (const auto& det : m_detectors) {
    det.second->InitializeDataInputPhysics(input);
  }
}
void nptool::DetectorManager::InitializeDataOutputRaw(std::shared_ptr<nptool::VDataOutput> output) {
  for (const auto& det : m_detectors) {
    det.second->InitializeDataOutputRaw(output);
  }
}
void nptool::DetectorManager::InitializeDataOutputPhysics(std::shared_ptr<nptool::VDataOutput> output) {
  for (const auto& det : m_detectors) {
    det.second->InitializeDataOutputPhysics(output);
  }
}

void nptool::DetectorManager::BuildRawEvent(const std::string& daq_name, const std::string& label, void* ptr_to_data,
                                            bool allow_MT) {
  // avoid overhead penalty when single detector analysis is performed
  if (!allow_MT || !m_allowMT) {
    for (const auto& [name, det] : m_detectors) {
      det->BuildRawEvent(daq_name, label, ptr_to_data);
    }
  }
  else {
    m_res.clear();
    // submit job
    for (const auto& [name, det] : m_detectors) {
      m_res.push_back(m_app->submit(
          [&det, &daq_name, &label, &ptr_to_data]() { det->BuildRawEvent(daq_name, label, ptr_to_data); }));
    }

    // wait for results
    for (const auto& r : m_res)
      r.wait();
  }
}
void nptool::DetectorManager::BuildPhysicalEvent(bool allow_MT) {
  // avoid overhead penalty when single detector analysis is performed
  if (!allow_MT || !m_allowMT) {
    for (const auto& [name, det] : m_detectors) {
      det->ClearAndBuild();
    }
  }
  else {
    m_res.clear();
    // submit job
    for (const auto& [name, det] : m_detectors) {
      m_res.push_back(m_app->submit(std::bind(&nptool::VDetector::ClearAndBuild, det)));
    }
    // wait for results
    for (const auto& r : m_res)
      r.wait();
  }
}

void nptool::DetectorManager::ClearEventPhysics(bool allow_MT) {
  if (!allow_MT || !m_allowMT) {
    for (const auto& [name, det] : m_detectors) {
      det->ClearEventPhysics();
    }
  }
  else {
    m_res.clear();
    // submit job
    for (const auto& [name, det] : m_detectors) {
      m_res.push_back(m_app->submit(std::bind(&nptool::VDetector::ClearEventPhysics, det)));
    }

    // wait for results
    for (const auto& r : m_res)
      r.wait();
  }
}

void nptool::DetectorManager::ClearEventData(bool allow_MT) {
  if (!allow_MT || !m_allowMT) {
    for (const auto& [name, det] : m_detectors) {
      det->ClearEventData();
    }
  }
  else {
    m_res.clear();
    // submit job
    for (const auto& [name, det] : m_detectors) {
      m_res.push_back(m_app->submit(std::bind(&nptool::VDetector::ClearEventData, det)));
    }

    // wait for results
    for (auto& r : m_res)
      r.wait();
  }
}
void nptool::DetectorManager::InitSimulation(std::string simtype) {
  for (const auto& [name, det] : m_detectors) {
    det->InitSimulation(simtype);
  }
}
void nptool::DetectorManager::ConstructGeometry() {
  for (const auto& [name, det] : m_detectors) {
    det->ConstructGeometry();
  }
}
void nptool::DetectorManager::ReadSensitive() {
  for (const auto& [name, det] : m_detectors) {
    det->ReadSensitive();
  }
}

void nptool::DetectorManager::InitializeScorers() {
  for (const auto& [name, det] : m_detectors) {
    det->InitializeScorers();
  }
}
std::shared_ptr<nptool::VDetector> nptool::DetectorManager::GetDetector(std::string name) {
  auto it_ptr = m_detectors.find(name);
  if (it_ptr != m_detectors.end()) {
    return it_ptr->second;
  }
  else {
    throw(nptool::Error("nptool::DetectorManager", "Detector " + name + " requested but not found"));
  }
}

void nptool::DetectorManager::InitSpectra() {
  for (const auto& [name, det] : m_detectors) {
    det->InitSpectra();
  }
};

void nptool::DetectorManager::FillSpectra() {
  if (!m_allowMT) {
    for (const auto& [name, det] : m_detectors) {
      det->FillSpectra();
    }
  }
  else {
    m_res.clear();
    // submit job
    for (const auto& [name, det] : m_detectors) {
      m_res.push_back(m_app->submit(std::bind(&nptool::VDetector::FillSpectra, det)));
    }
    // wait for results
    for (const auto& r : m_res)
      r.wait();
  }
};

void nptool::DetectorManager::WriteSpectra() {
  for (const auto& [name, det] : m_detectors) {
    det->WriteSpectra();
  }
};

void nptool::DetectorManager::CheckSpectra() {
  if (!m_allowMT) {
    for (const auto& [name, det] : m_detectors) {
      det->CheckSpectra();
    }
  }
  else {
    m_res.clear();
    // submit job
    for (const auto& [name, det] : m_detectors) {
      m_res.push_back(m_app->submit(std::bind(&nptool::VDetector::CheckSpectra, det)));
    }
    // wait for results
    for (const auto& r : m_res)
      r.wait();
  }
};

void nptool::DetectorManager::ClearSpectra() {
  if (!m_allowMT) {
    for (const auto& [name, det] : m_detectors) {
      det->ClearSpectra();
    }
  }
  else {
    m_res.clear();
    // submit job
    for (const auto& [name, det] : m_detectors) {
      m_res.push_back(m_app->submit(std::bind(&nptool::VDetector::ClearSpectra, det)));
    }
    // wait for results
    for (const auto& r : m_res)
      r.wait();
  }
};