ImproveReferenceTimestamp.cpp 3.44 KB
Newer Older
fpipper's avatar
fpipper committed
1
#include "ImproveReferenceTimestamp.h"
2
#include "objects/SpidrSignal.hpp"
3
4
5
6

using namespace corryvreckan;
using namespace std;

7
ImproveReferenceTimestamp::ImproveReferenceTimestamp(Configuration config, std::vector<std::shared_ptr<Detector>> detectors)
8
    : Module(std::move(config), std::move(detectors)) {
9
10
11
    m_method = m_config.get<int>("improvement_method", 1);
    m_source = m_config.get<std::string>("signal_source", "W0013_G02");
    m_triggerLatency = m_config.get<double>("trigger_latency", static_cast<double>(Units::convert(0, "ns")));
12
13
}

fpipper's avatar
fpipper committed
14
void ImproveReferenceTimestamp::initialise() {
15
16
17
18
    // Initialise member variables
    m_eventNumber = 0;
}

19
StatusCode ImproveReferenceTimestamp::run(std::shared_ptr<Clipboard> clipboard) {
20
21

    // Recieved triggers
22
    std::vector<double> trigger_times;
23
24

    // Get trigger signals
25
26
    SpidrSignals* spidrData = reinterpret_cast<SpidrSignals*>(clipboard->get(m_source, "SpidrSignals"));
    if(spidrData != nullptr) {
27
        // Loop over all signals registered
28
        for(auto& signal : (*spidrData)) {
29
            if(signal->type() == "trigger") {
30
                trigger_times.push_back(signal->timestamp() - m_triggerLatency);
31
32
33
34
35
36
            }
        }
        LOG(DEBUG) << "Number of triggers found: " << trigger_times.size();
    }

    // Get the tracks from the clipboard
37
38
    Tracks* tracks = reinterpret_cast<Tracks*>(clipboard->get("tracks"));
    if(tracks == nullptr) {
39
        LOG(DEBUG) << "No tracks on the clipboard";
40
        return StatusCode::Success;
41
42
43
44
45
46
    }
    LOG(DEBUG) << "Number of tracks found: " << tracks->size();

    // Loop over all tracks
    for(auto& track : (*tracks)) {

47
        double improved_time = track->timestamp();
48

fpipper's avatar
fpipper committed
49
        // Use trigger timestamp
50
        if(m_method == 0) {
51
            // Find trigger timestamp closest in time
52
            double diff = std::numeric_limits<double>::max();
fpipper's avatar
fpipper committed
53
54
            for(auto& trigger_time : trigger_times) {
                LOG(DEBUG) << " track: " << track->timestamp() << " trigger: " << trigger_time
55
                           << " diff: " << Units::display(abs(trigger_time - track->timestamp()), {"ns", "us", "s"})
56
                           << " diff stored: " << Units::display(diff, {"ns", "us", "s"});
57
                if(abs(trigger_time - track->timestamp()) < diff) {
fpipper's avatar
fpipper committed
58
                    improved_time = trigger_time;
59
                    diff = abs(trigger_time - track->timestamp());
60
61
62
63
                }
            }
        }

fpipper's avatar
fpipper committed
64
65
        // Use average track timestamp
        else if(m_method == 1) {
66
            int nhits = 0;
67
            double avg_track_time = 0;
68
69
70
71
            for(auto& cluster : track->clusters()) {
                avg_track_time += cluster->timestamp();
                nhits++;
            }
72
            improved_time = round(avg_track_time / nhits);
73
74
            LOG(DEBUG) << setprecision(12) << "Reference track time "
                       << Units::display(track->timestamp(), {"ns", "us", "s"});
75
            LOG(DEBUG) << setprecision(12) << "Average track time " << Units::display(improved_time, {"ns", "us", "s"});
76
77
78
79
80
81
82
83
84
85
86
87
88
        }

        // Set improved reference timestamp
        track->setTimestamp(improved_time);

        LOG(DEBUG) << "End of track";
    }

    // Increment event counter
    m_eventNumber++;

    // Return value telling analysis to keep running
    LOG(DEBUG) << "End of event";
89
    return StatusCode::Success;
90
91
}

fpipper's avatar
fpipper committed
92
void ImproveReferenceTimestamp::finalise() {
93
94
95

    LOG(DEBUG) << "Analysed " << m_eventNumber << " events";
}