Skip to content

Commit

Permalink
Merge branch 'fchinu:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
Roberta-Ferioli authored Mar 21, 2024
2 parents 7a759ef + 878ffba commit 2ed646f
Show file tree
Hide file tree
Showing 5 changed files with 657 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,6 @@
*.exe
*.out
*.app

# vscode
.vscode
90 changes: 90 additions & 0 deletions Clustering/src/clusterer.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@

#include <iostream>
#include <unordered_map>
#include <algorithm>

#include "clusterer.h"
#include "../../Utils/include/fileManager.h"

Clusterer::Clusterer()
{
}

Clusterer::~Clusterer()
{
}

void Clusterer::Clustering(const char * inputFile)
{

// add filemanager
FileManager inputData(inputFile);

std::unordered_map<int, std::vector<int>> event_indices;

double *event = inputData.getColumn("event_count");
double *chip_id = inputData.getColumn("chip_id(decimal)");
double *x = inputData.getColumn("hit_x");
double *y = inputData.getColumn("hit_y");

for (int irow = 0; irow < inputData.getNRows(); irow++)
{
event_indices[event[irow]].push_back(irow);
}

for (auto it = event_indices.begin(); it != event_indices.end(); ++it)
{
int ievent = it->first;
std::vector<int>& indices = it->second;

std::cout << "Event " << ievent << std::endl;
std::cout << "Indices size: " << indices.size() << std::endl;

while (!indices.empty())
{

std::vector<int> next_indices;
double mean_ix = x[indices[0]], mean_iy = y[indices[0]];
int cluster_size = 1;

for (int i = 1; i < indices.size(); i++)
{
int index = indices[i];

if (chip_id[index] != chip_id[indices[0]]) continue;

bool isValid = false;
if ((std::abs(x[index] - x[indices[0]]) + std::abs(y[index] - y[indices[0]])) <= 1) isValid = true;
for (int j : next_indices)
{
if (std::abs(x[index] - x[j]) == 1 && std::abs(y[index] - y[j]) == 1) isValid = true;
}

if (isValid)
{
next_indices.push_back(index);
mean_ix += x[index];
mean_iy += y[index];
cluster_size++;
}
}

mean_ix /= cluster_size;
mean_iy /= cluster_size;

// cluster class implementation needed
Cluster cluster( static_cast<int>(chip_id[indices[0]]),
static_cast<int>(ievent),
mean_ix,
mean_iy,
cluster_size);
fClusters.push_back(cluster);

// Remove processed indices from the vector
indices.erase(std::remove_if(indices.begin(), indices.end(),
[&next_indices](int i) { return std::find(next_indices.begin(), next_indices.end(), i) != next_indices.end(); }),
indices.end());
indices.erase(indices.begin());
}
}
}
26 changes: 26 additions & 0 deletions Clustering/src/clusterer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

#include <vector>
#include "../include/cluster.h"

class Clusterer
{

public:
Clusterer();
~Clusterer();

void Clustering(const char * inputFile);

protected:

void clearClusters() {fClusters.clear();};

private:

std::vector<Cluster> fClusters;




};
Loading

0 comments on commit 2ed646f

Please sign in to comment.