Skip to content
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

Improved space gap calculation #765

Draft
wants to merge 11 commits into
base: master
Choose a base branch
from
24 changes: 12 additions & 12 deletions src/common/reduction/clustering/Hierarchical2DClusterer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,16 @@ void Hierarchical2DClusterer::cluster_by_x() {
continue; // skip points that have already been visited
}
XTRACE(DATA, DEB, "Starting new cluster");
Hit2DVector space_cluster; // initialize a new cluster
space_cluster.push_back(current_time_cluster_[i]);
Cluster2D space_cluster; // initialize a new cluster
space_cluster.insert(current_time_cluster_[i]);
visited[i] = true;
for (uint j = i + 1; j < clusterSize; j++) {
if (visited[j]) {
continue; // skip points that have already been visited
}
double x_distance = (double)current_time_cluster_[i].x_coordinate -
double x_distance = space_cluster.xCoordCenter() -
(double)current_time_cluster_[j].x_coordinate;
double y_distance = (double)current_time_cluster_[i].y_coordinate -
double y_distance = space_cluster.yCoordCenter() -
(double)current_time_cluster_[j].y_coordinate;

// Calculate distance according to d^2 = dx^2 + dy^2 to remove sqrt calculation
Expand All @@ -81,7 +81,7 @@ void Hierarchical2DClusterer::cluster_by_x() {
// Compare with the squere of the max_coord_gap to save computation time on sqrt above
if (distance_sqr < max_coord_gap_sqr_) {
XTRACE(DATA, DEB, "Adding to existing cluster");
space_cluster.push_back(
space_cluster.insert(
current_time_cluster_[j]); // add point to current cluster
visited[j] = true;
} else {
Expand All @@ -92,13 +92,13 @@ void Hierarchical2DClusterer::cluster_by_x() {
}
}

void Hierarchical2DClusterer::stash_cluster(Hit2DVector &xz_cluster) {
Cluster2D cluster;
for (const auto &hit : xz_cluster) {
cluster.insert(hit);
}
Abstract2DClusterer::stash_cluster(cluster);
}
// void Hierarchical2DClusterer::stash_cluster(Hit2DVector &xz_cluster) {
// Cluster2D cluster;
// for (const auto &hit : xz_cluster) {
// cluster.insert(hit);
// }
// Abstract2DClusterer::stash_cluster(cluster);
// }

std::string Hierarchical2DClusterer::config(const std::string &prepend) const {
std::stringstream ss;
Expand Down
2 changes: 1 addition & 1 deletion src/common/reduction/clustering/Hierarchical2DClusterer.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,5 @@ class Hierarchical2DClusterer : public Abstract2DClusterer {
/// \brief helper function to clusters hits in current_time_cluster_
void cluster_by_x();

void stash_cluster(Hit2DVector &xz_cluster);
// void stash_cluster(Hit2DVector &xz_cluster);
};
1 change: 1 addition & 0 deletions src/modules/timepix3/Counters.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ struct Counters {
int64_t TofNegative{0};
int64_t PrevTofCount{0};
int64_t ClusterSizeTooSmall{0};
int64_t ClusterToShort{0};

int64_t PixelReadouts{0};
int64_t InvalidPixelReadout{0};
Expand Down
1 change: 1 addition & 0 deletions src/modules/timepix3/Timepix3Base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ Timepix3Base::Timepix3Base(BaseSettings const &settings)
Stats.create("handlers.pixelevent.tof_count", Counters.TofCount);
Stats.create("handlers.pixelevent.tof_neg", Counters.TofNegative);
Stats.create("handlers.pixelevent.cluster_size_to_small", Counters.ClusterSizeTooSmall);
Stats.create("handlers.pixelevent.cluster_to_short", Counters.ClusterToShort);
Stats.create("handlers.pixelevent.prevtof_count", Counters.PrevTofCount);

// Events
Expand Down
2 changes: 1 addition & 1 deletion src/modules/timepix3/configs/timepix3.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
"MaxCoordinateGap": 4,
"MinEventSizeHits": 9,
"FrequencyHz": 10
}
}
21 changes: 14 additions & 7 deletions src/modules/timepix3/handlers/PixelEventHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,32 +131,39 @@ void PixelEventHandler::publishEvents(Cluster2DContainer &clusters) {
// detector, it is the time the first photon in the cluster hit the
// detector.

if (cluster.hitCount() < TimepixConfiguration.MinEventSizeHits || cluster.weightSum() < TimepixConfiguration.MinimumToTSum) {
if (cluster.hitCount() < TimepixConfiguration.MinEventSizeHits ||
cluster.weightSum() < TimepixConfiguration.MinimumToTSum) {

statCounters.ClusterSizeTooSmall++;
continue;
}

if (cluster.timeSpan() < TimepixConfiguration.MinEventTimeSpan) {
statCounters.ClusterToShort++;
continue;
}

uint64_t eventTime = cluster.timeStart();
long eventTof = eventTime - lastEpochESSPulseTime->pulseTimeInEpochNs;
long eventTofNs = eventTime - lastEpochESSPulseTime->pulseTimeInEpochNs;
statCounters.TofCount++;

if (eventTof < 0) {
if (eventTofNs < 0) {
statCounters.TofNegative++;
continue;
}

if (eventTof > FrequencyPeriodNs.count()) {
// If the event would be the next pulse, move it to the beginning of the
// current pulse to keep up statistics
if (eventTofNs > FrequencyPeriodNs.count()) {
XTRACE(EVENT, WAR,
"Event is for the next pulse, EventTime: %u, Current "
"pulse time: %u, Difference: %u",
eventTime, lastEpochESSPulseTime->pulseTimeInEpochNs, eventTof);
eventTime, lastEpochESSPulseTime->pulseTimeInEpochNs, eventTofNs);
statCounters.EventTimeForNextPulse++;
continue;
// Normalize the event time to the current pulse
eventTofNs = eventTofNs % FrequencyPeriodNs.count(); // Ensure eventTofNs is within the period
}

double EventCoordX = cluster.xCoordCenter();
double EventCoordY = cluster.yCoordCenter();
uint32_t PixelId = geometry->calcPixelId(EventCoordX, EventCoordY);
Expand All @@ -168,7 +175,7 @@ void PixelEventHandler::publishEvents(Cluster2DContainer &clusters) {
continue;
}
XTRACE(EVENT, DEB, "New event, Time: %u, PixelId: %u", eventTime, PixelId);
serializer.addEvent(eventTof, PixelId);
serializer.addEvent(eventTofNs, PixelId);
statCounters.Events++;
}
clusters.clear();
Expand Down
40 changes: 32 additions & 8 deletions src/modules/timepix3/test/Timepix3PixelEventHandlerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
/// \file
//===----------------------------------------------------------------------===//

#include <chrono>
#include <common/kafka/EV44Serializer.h>
#include <common/testutils/TestBase.h>
#include <common/utils/EfuUtils.h>
#include <cstdint>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <modules/timepix3/dto/TimepixDataTypes.h>
Expand Down Expand Up @@ -50,12 +52,17 @@ class PixelTimeTestHelper {

const uint64_t pixelClockTime;

PixelTimeTestHelper(int16_t ToA, uint8_t fToA)
PixelTimeTestHelper(uint16_t ToA, uint8_t fToA)
: ToA(ToA), fToA(fToA), ToT(200), spidrTime(41503),
tdcClockInPixelTime(16999999997),
pixelClockTime(calculatePixelClockTime(spidrTime, ToA, fToA)) {}

PixelTimeTestHelper(int16_t ToA, uint8_t fToA, uint32_t spidrTime,
PixelTimeTestHelper(uint16_t ToA, uint8_t fToA, uint32_t spidrTime)
: ToA(ToA), fToA(fToA), ToT(200), spidrTime(spidrTime),
tdcClockInPixelTime(16999999997),
pixelClockTime(calculatePixelClockTime(spidrTime, ToA, fToA)) {}

PixelTimeTestHelper(uint16_t ToA, uint8_t fToA, uint32_t spidrTime,
uint64_t tdcClockInPixelTime)
: ToA(ToA), fToA(fToA), ToT(200), spidrTime(spidrTime),
tdcClockInPixelTime(tdcClockInPixelTime),
Expand Down Expand Up @@ -145,22 +152,39 @@ TEST_F(Timepix3PixelEventHandlerTest, TestInvalidPixelReadout) {
}

TEST_F(Timepix3PixelEventHandlerTest, TestIfEventIsLaterThenNextTdc) {
// This value ensures that the pixel time is later then the tdc clock
uint32_t spidrLateArrival = 55000;
nanoseconds FrequencyPeriodNs = hzToNanoseconds(Timepix3Config.FrequencyHz);

// This value ensures that the pixel time is later then the next tdc clock
uint32_t spidrDelayToPulse = 176;

PixelTimeTestHelper PixelTimeInNextPulse{
TEST_DEFAULT_PIXEL_TIME.ToA, TEST_DEFAULT_PIXEL_TIME.fToA,
TEST_DEFAULT_PIXEL_TIME.spidrTime + spidrDelayToPulse,
TEST_DEFAULT_PIXEL_TIME.tdcClockInPixelTime};

serializer.pulseTimeToCompare = TEST_PULSE_TIME_NS;
serializer.pixelIdToCompare = TEST_PIXEL_ID;

// Pixel tof should be normalized with the frequency period to fir to the
// current pulse
serializer.eventTimeToCompare =
PixelTimeInNextPulse.getEventTof() % FrequencyPeriodNs.count();

serializer.pulseTimeToCompare = TEST_PULSE_TIME_NS;

testEventHandler.applyData(
{TEST_PULSE_TIME_NS, TEST_DEFAULT_PIXEL_TIME.tdcClockInPixelTime});

testEventHandler.applyData(
PixelReadout{TEST_DCOL, TEST_SPIX, TEST_PIX, TEST_DEFAULT_PIXEL_TIME.ToA,
TEST_DEFAULT_PIXEL_TIME.ToT, TEST_DEFAULT_PIXEL_TIME.fToA,
spidrLateArrival});
PixelReadout{TEST_DCOL, TEST_SPIX, TEST_PIX, PixelTimeInNextPulse.ToA,
PixelTimeInNextPulse.ToT, PixelTimeInNextPulse.fToA,
PixelTimeInNextPulse.spidrTime});

testEventHandler.pushDataToKafka();
EXPECT_EQ(counters.EventTimeForNextPulse, 1);
EXPECT_EQ(serializer.addEventCallCounter, 0);

// We always add event since we normalize it to current pulse.
EXPECT_EQ(serializer.addEventCallCounter, 1);
}

TEST_F(Timepix3PixelEventHandlerTest, TestEventTimeShortlyAfterTdcPublished) {
Expand Down