Line data Source code
1 :
2 : #include "triggeralgs/TPWindow.hpp"
3 :
4 : #include <chrono>
5 : #include <map>
6 : #include <memory>
7 : #include <string>
8 : #include <unordered_set>
9 : #include <utility>
10 :
11 : namespace triggeralgs {
12 :
13 : bool
14 0 : TPWindow::is_empty() const
15 : {
16 0 : return inputs.empty();
17 : }
18 :
19 : void
20 0 : TPWindow::add(TriggerPrimitive const& input_tp)
21 : {
22 : // Add the input TP's contribution to the total ADC, increase hit
23 : // channel's hit count and add it to the TP list.
24 0 : adc_integral += input_tp.adc_integral;
25 0 : channel_states[input_tp.channel]++;
26 0 : inputs.push_back(input_tp);
27 0 : }
28 :
29 : void
30 0 : TPWindow::clear()
31 : {
32 0 : inputs.clear();
33 0 : channel_states.clear();
34 0 : time_start = 0;
35 0 : adc_integral = 0;
36 0 : }
37 :
38 : uint16_t
39 0 : TPWindow::n_channels_hit()
40 : {
41 0 : return channel_states.size();
42 : }
43 :
44 : void
45 0 : TPWindow::move(TriggerPrimitive const& input_tp, timestamp_t const& window_length)
46 : {
47 : // Find all of the TPs in the window that need to be removed
48 : // if the input_tp is to be added and the size of the window
49 : // is to be conserved.
50 : // Substract those TPs' contribution from the total window ADC and remove their
51 : // contributions to the hit counts.
52 0 : uint32_t n_tps_to_erase = 0;
53 0 : for (auto tp : inputs) {
54 0 : if (!(input_tp.time_start - tp.time_start < window_length)) {
55 0 : n_tps_to_erase++;
56 0 : adc_integral -= tp.adc_integral;
57 0 : channel_states[tp.channel]--;
58 : // If a TP being removed from the window results in a channel no longer having
59 : // any hits, remove from the states map so map.size() can be used for number
60 : // channels hit.
61 0 : if (channel_states[tp.channel] == 0)
62 0 : channel_states.erase(tp.channel);
63 : } else
64 : break;
65 : }
66 : // Erase the TPs from the window.
67 0 : inputs.erase(inputs.begin(), inputs.begin() + n_tps_to_erase);
68 : // Make the window start time the start time of what is now the first TP.
69 :
70 0 : if (inputs.size() != 0) {
71 0 : time_start = inputs.front().time_start;
72 0 : add(input_tp);
73 : } else {
74 0 : reset(input_tp);
75 : }
76 0 : }
77 :
78 : void
79 0 : TPWindow::reset(TriggerPrimitive const& input_tp)
80 : {
81 :
82 : // Empty the channel and TP lists.
83 0 : channel_states.clear();
84 0 : inputs.clear();
85 : // Set the start time of the window to be the start time of theinput_tp.
86 0 : time_start = input_tp.time_start;
87 : // Start the total ADC integral.
88 0 : adc_integral = input_tp.adc_integral;
89 : // Start hit count for the hit channel.
90 0 : channel_states[input_tp.channel]++;
91 : // Add the input TP to the TP list.
92 0 : inputs.push_back(input_tp);
93 : // std::cout << "Number of channels hit: " << n_channels_hit() << std::endl;
94 0 : }
95 :
96 : std::ostream&
97 0 : operator<<(std::ostream& os, const TPWindow& window)
98 : {
99 0 : if (window.is_empty()) {
100 0 : os << "Window is empty!\n";
101 : } else {
102 0 : os << "Window start: " << window.time_start << ", end: " << window.inputs.back().time_start;
103 0 : os << ". Total of: " << window.adc_integral << " ADC counts with " << window.inputs.size() << " TPs.\n";
104 0 : os << window.channel_states.size() << " independent channels have hits.\n";
105 : }
106 0 : return os;
107 : }
108 :
109 : } // namespace triggeralgs
|