Skip to content

Analysis conversion #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Mar 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ set(PROJECT_SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/src")
set(PROJECT_HEADER_DIR "${CMAKE_CURRENT_SOURCE_DIR}/include")
set(PROJECT_CONFIG_DIR "${CMAKE_CURRENT_SOURCE_DIR}/config")

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/../bin")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/output/bin")
include("${CMAKE_CURRENT_SOURCE_DIR}/cmake/version.cmake")


Expand Down Expand Up @@ -74,6 +74,7 @@ set(CLUSTER_SOURCE_FILES
"${PROJECT_SRC_DIR}/utility/parameters.cpp"
"${PROJECT_SRC_DIR}/utility/configuration.cpp"
"${PROJECT_SRC_DIR}/utility/resourcetracker.cpp"
"${PROJECT_SRC_DIR}/utility/uppermatrix.cpp"
"${PROJECT_SRC_DIR}/utility/restservice.cpp"
"${PROJECT_SRC_DIR}/utility/scopeguard.cpp"

Expand Down Expand Up @@ -117,6 +118,7 @@ set(CLUSTER_HEADER_FILES
"${PROJECT_HEADER_DIR}/utility/parameters.h"
"${PROJECT_HEADER_DIR}/utility/configuration.h"
"${PROJECT_HEADER_DIR}/utility/resourcetracker.h"
"${PROJECT_HEADER_DIR}/utility/uppermatrix.h"
"${PROJECT_HEADER_DIR}/utility/restservice.h"
"${PROJECT_HEADER_DIR}/utility/base64.h"
"${PROJECT_HEADER_DIR}/utility/scopeguard.h"
Expand Down Expand Up @@ -218,6 +220,6 @@ set(CPACK_DEBIAN_PACKAGE_DESCRIPTION " Daemon which calculates coincidences for
publishes them to another mqtt topic or writes them to a database.
It is licensed under the GNU Lesser General Public License version 3 (LGPL v3).")
set(CPACK_DEBIAN_PACKAGE_MAINTAINER "MuonPi <developer@muonpi.org>")
set(CPACK_PACKAGE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/../packages/")
set(CPACK_PACKAGE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/output/packages/")

include(CPack)
12 changes: 12 additions & 0 deletions include/detector.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "messages/detectorinfo.h"
#include "messages/detectorsummary.h"
#include "messages/userinfo.h"
#include "utility/analysis.h"
#include "utility/threadrunner.h"
#include "utility/analysis.h"

Expand Down Expand Up @@ -82,10 +83,21 @@ class detector {
*/
[[nodiscard]] auto factor() const -> double;

/**
* @brief step Gets called by the detector_tracker with a guaranteed maximum time delay. May be called more often.
*/
void step();

/**
* @brief current_log_data gets the current log data.
* @return
*/
[[nodiscard]] auto current_log_data() -> detetor_summary_t;

/**
* @brief change_log_data gets the current log data when the detector has experienced a change in status.
* @return
*/
[[nodiscard]] auto change_log_data() -> detetor_summary_t;

/**
Expand Down
4 changes: 4 additions & 0 deletions include/supervision/state.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "detector.h"
#include "messages/clusterlog.h"
#include "sink/base.h"
#include "utility/analysis.h"
#include "utility/resourcetracker.h"
#include "utility/analysis.h"

Expand Down Expand Up @@ -66,6 +67,9 @@ class state_supervisor : public source::base<cluster_log_t> {
*/
void add_thread(thread_runner* thread);

/**
* @brief stop Signals all threads to stop execution
*/
void stop();

private:
Expand Down
156 changes: 156 additions & 0 deletions include/utility/uppermatrix.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
#ifndef UPPERMATRIX_H
#define UPPERMATRIX_H

#include <vector>
#include <unordered_map>
#include <map>

namespace MuonPi {

template <typename T>
class upper_matrix
{
public:
/**
* @brief upper_matrix Constructs an upper triangle matrix with a dimension of n x n
* @param n The dimension of the matrix
*/
upper_matrix(std::size_t n);

/**
* @brief at Gets a reference to the element at position x,y
* Note that this function does not check for validity of the location, you have to do this yourself.
* @param x The x coordinate
* @param y The y coordinate
* @return A reference to the element
*/
[[nodiscard]] auto at(std::size_t x, std::size_t y) -> T&;

/**
* @brief remove_index Removes a specific index from the matrix.
* Complexity should be O(n)
* @param index The index to remove
*/
void remove_index(std::size_t index);

/**
* @brief increase The number of elements by one
* @return The index of the new element
*/
auto increase() -> std::size_t;

/**
* @brief swap_last Swaps all elements associated with the one given as parameter with the last column
* @param index the index to swap with the last column
*/
void swap_last(std::size_t index);

private:
/**
* @brief position Calculates the position in the vector for a given tuple
* @param x The x coordinate
* @param y The y coordinate
* @return The offset from the beginning of the vector
*/
[[nodiscard]] inline auto position(std::size_t x, std::size_t y) const -> std::size_t
{
return 1/2 * (x*x - x) + y;
}

/**
* @brief iterator Get an iterator to the element at position x,y
* @param x the x coordinate
* @param y the y coordinate
* @return the iterator
*/
[[nodiscard]] inline auto iterator(std::size_t x, std::size_t y) const
{
return m_elements.begin() + position(std::move(x), std::move(y));
}

std::size_t m_columns;
std::vector<T> m_elements;
};

class detector_pairs
{
public:
void add_detector(std::size_t hash);
void remove_detector(std::size_t hash);

void increase_count(std::size_t hash_1, std::size_t hash_2);
[[nodiscard]] auto get_counts(std::size_t hash) -> std::unordered_map<std::size_t, std::size_t>;

private:
std::vector<std::size_t> m_detectors {};
upper_matrix<std::size_t> m_data {0};
};

template <typename T>
upper_matrix<T>::upper_matrix(std::size_t n)
: m_columns { n }
, m_elements { std::vector<T>{position(n, 0)} }
{
}

template <typename T>
auto upper_matrix<T>::at(std::size_t x, std::size_t y) -> T&
{
return m_elements.at(position(std::move(x), std::move(y)));
}

template <typename T>
void upper_matrix<T>::remove_index(std::size_t index)
{
if (index >= m_columns) {
return;
}
// if the column is the last, it is enough to resize the vector to a size without the elements in the last column
if (index == (m_columns - 1)) {
m_columns--;
m_elements.resize(position(m_columns, 0));
return;
}
// Swaps all elements for the index with the last column and resizes the vector.
// This will effectively delete the elements associated with the index.
swap_last(index);
m_columns--;
m_elements.resize(position(m_columns, 0));
if (index == (m_columns - 1)) {
return;
}
// Swap the elements back to its original location to keep integrity
swap_last(index);
}

template <typename T>
auto upper_matrix<T>::increase() -> std::size_t
{
m_columns++;
m_elements.resize(position(m_columns, 0));
return m_columns - 1;
}

template <typename T>
void upper_matrix<T>::swap_last(std::size_t first)
{
if (first >= (m_columns - 1)) {
return;
}

for (std::size_t y { 0 }; y < first; y++) {
T temp {at(first, y)};
at(first, y) = at(m_columns - 1, y);
at(m_columns - 1, y) = temp;
}

for (std::size_t x { first + 1}; x < m_columns; x++) {
T temp {at(x, first)};
at(x, first) = at(m_columns - 1, x - 1);
at(m_columns - 1, x - 1) = temp;
}
}


}
#endif // UPPERMATRIX_H
3 changes: 1 addition & 2 deletions include/utility/utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ class MessageParser {
std::vector<std::pair<std::string::iterator, std::string::iterator>> m_fields {};
};

// class GUID
class GUID {
public:
GUID(std::size_t hash, std::uint64_t time);
Expand All @@ -108,7 +107,6 @@ class GUID {
std::uint64_t m_first { 0 };
std::uint64_t m_second { 0 };
};
// -------------------------------


template <typename T, std::enable_if_t<std::is_integral<T>::value, bool> = true>
Expand All @@ -118,5 +116,6 @@ template <typename T, std::enable_if_t<std::is_integral<T>::value, bool> = true>
ss << std::setfill('0') << std::setw(width) << std::hex << (val | 0);
return ss.str();
}

}
#endif // UTILITY_H
70 changes: 70 additions & 0 deletions src/utility/uppermatrix.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include "utility/uppermatrix.h"

#include <algorithm>
#include <cinttypes>

namespace MuonPi {

void detector_pairs::add_detector(std::size_t hash)
{
m_data.increase();
m_detectors.emplace_back(hash);
}

void detector_pairs::remove_detector(std::size_t hash)
{
auto it = std::find(std::begin(m_detectors), std::end(m_detectors), hash);
if (it == m_detectors.end()) {
return;
}
m_data.remove_index(std::distance(m_detectors.begin(), it));
m_detectors.erase(it);
}

void detector_pairs::increase_count(std::size_t hash_1, std::size_t hash_2)
{
std::size_t first { 0 };
std::size_t second { 0 };
std::uint8_t found { 0x3 };
for (std::size_t i { 0 }; i < m_detectors.size(); i++) {
if (m_detectors[i] == hash_1) {
first = i;
found &= ~0x1U;
} else if (m_detectors[i] == hash_2){
second = i;
found &= ~0x2U;
}
if (found == 0) {
break;
}
}
if (found > 0) {
return;
}
m_data.at(first, second) += 1;
}

auto detector_pairs::get_counts(std::size_t hash) -> std::unordered_map<std::size_t, std::size_t>
{
auto it = std::find(std::begin(m_detectors), std::end(m_detectors), hash);
if (it == m_detectors.end()) {
return {};
}
std::size_t index {static_cast<std::size_t>(std::distance(m_detectors.begin(), it))};

std::unordered_map<std::size_t, std::size_t> counts {};
for (std::size_t i { 0 }; i < m_detectors.size(); i++) {
auto detector {m_detectors.at(i)};
if (detector == hash) {
continue;
}
if (i < index) {
counts.emplace(detector, m_data.at(index, i));
} else {
counts.emplace(detector, m_data.at(i, index));
}
}
return counts;
}

}