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