GCC Code Coverage Report


Directory: ./
File: src/dp/abstractHitHandler.cc
Date: 2025-09-01 06:19:01
Exec Total Coverage
Lines: 0 82 0.0%
Functions: 0 7 0.0%
Branches: 0 38 0.0%

Line Branch Exec Source
1 #include "na64dp/abstractHitHandler.hh"
2 #include "na64util/str-fmt.hh"
3 #include <regex>
4
5 namespace na64dp {
6 namespace aux {
7
8 std::string
9 retrieve_det_selection( const YAML::Node &cfg ) {
10 if( cfg["applyTo"] ) {
11 //YAML_ASSERT_STR( cfg, "applyTo" );
12 return cfg["applyTo"].as<std::string>();
13 }
14 return "";
15 }
16
17 std::string
18 get_naming_class(const YAML::Node & cfg) {
19 return cfg["detectorNamingClass"]
20 ? cfg["detectorNamingClass"].as<std::string>()
21 : "default";
22 }
23
24 std::string
25 get_placements_class(const YAML::Node & cfg) {
26 return cfg["detectorPlacementsClass"]
27 ? cfg["detectorPlacementsClass"].as<std::string>()
28 : "default";
29 }
30
31 std::string
32 get_val_entry_type(const std::string & str) {
33 // TODO: when event x-macro will be implemented, extracted token shall
34 // be verified against existing SADC hits set to provide getters of single
35 // field within an event (like `mcTruth`).
36 size_t n = str.find('/');
37 if( std::string::npos == n ) return "event";
38 return str.substr(0, n);
39 }
40
41 } // namespace ::na64dp::aux
42
43 AbstractHitHandler<event::Track>::AbstractHitHandler( calib::Dispatcher & cdsp
44 , const std::string & selection_
45 , log4cpp::Category & logCat
46 , const std::string & namingCat
47 ) : HitHandlerStats(logCat)
48 , _permittedZones(0x0)
49 , _invertZonePatternMatching(false)
50 {
51 std::string selection(selection_);
52 if(selection.empty() || selection == "*") {
53 _log.debug("Handler %p takes all tracks in an event.", this);
54 return; // match all tracks
55 }
56 //if((!namingCat.empty()) && namingCat != "default") {
57 // _log.warn("Naming class \"%s\" ignored by track handler.");
58 //}
59
60 const std::regex rxSel(R"rx(^(0x[0-9a-fA-F]+)(\s*\|\s*0x[0-9a-fA-F]+)*$)rx");
61 // ^^^ examples: "0x1", "0x1|0x2", "0xDEAD | 0xBEEF", "0x0|0x0"
62 if(!std::regex_match(selection, rxSel)) {
63 NA64DP_RUNTIME_ERROR("Expression \"%s\" is not a tracking zone"
64 " selection pattern.", selection.c_str());
65 }
66
67 std::unordered_set<int> parsed;
68 const std::regex rxTok(R"rx(0x[0-9a-fA-F]+)rx");
69
70 std::smatch sm;
71 while(std::regex_search(selection, sm, rxTok)) {
72 int code = std::stoi(sm.str(), nullptr, 16);
73 parsed.insert(code);
74 selection = sm.suffix();
75 }
76 assert(!parsed.empty());
77 if(parsed.size() == 1) {
78 _permittedZones = *parsed.begin();
79 _log.debug("Handler %p takes tracks matching at least one zone in"
80 " pattern %#x.", this, _permittedZones);
81 } else {
82 std::ostringstream oss;
83 bool isFirst = true;
84 for( auto code : parsed ) {
85 if(!code) continue;
86 _permittedZonePatterns.emplace(code);
87 oss << (isFirst ? "" : ", ") << code;
88 }
89 _log.debug("Handler %p set up to handle tracks with zone patterns: %s."
90 , this, oss.str().c_str() );
91 }
92 assert( ((bool)_permittedZones) == _permittedZonePatterns.empty() );
93 }
94
95 bool
96 AbstractHitHandler<event::Track>::zone_pattern_matches(int tp) const {
97 bool matches = false;
98 if( (!_permittedZones) && _permittedZonePatterns.empty() ) return true;
99 assert( !(_permittedZones && (!_permittedZonePatterns.empty())) );
100 if(_permittedZones) {
101 matches = tp & _permittedZones;
102 } else if( !_permittedZonePatterns.empty() ) {
103 auto it = _permittedZonePatterns.find(tp);
104 matches = (it != _permittedZonePatterns.end());
105 }
106 if( matches ) {
107 return _invertZonePatternMatching ? false : true;
108 } else {
109 return _invertZonePatternMatching ? true : false;
110 }
111 }
112
113 AbstractHandler::ProcRes
114 AbstractHitHandler<event::Track>::process_event(event::Event & e) {
115 typedef event::Association<event::Event, event::Track> Association;
116 _pr = kOk;
117 bool keepGoing = false;
118 std::vector<Association::Collection::iterator> tracksToRemove;
119 _cEvPtr = &e;
120 for( _cTrackIt = e.tracks.begin(); e.tracks.end() != _cTrackIt; ++_cTrackIt ) {
121 if( ! zone_pattern_matches(_cTrackIt->first.zones()) ) {
122 _log << log4cpp::Priority::DEBUG
123 << "Track with zone pattern " << (int) _cTrackIt->first.zones()
124 << " ignored (doesn't match).";
125 continue;
126 }
127 ++_nHitsConsidered;
128 _removeFieldEntry = false;
129 keepGoing = process_hit( e.id, _cTrackIt->first, *(_cTrack = _cTrackIt->second) );
130 if( _removeFieldEntry ) {
131 tracksToRemove.push_back(_cTrackIt);
132 }
133 if(!keepGoing) break;
134 }
135 _cTrack.reset();
136 for( auto it : tracksToRemove ) {
137 _log << log4cpp::Priority::DEBUG
138 << "Track " << it->first.code << " discriminated."; // TODO: ostream op-r for TrackID
139 Association::remove(e.tracks, it);
140 }
141 _nHitsDiscriminated += tracksToRemove.size();
142 _cEvPtr = nullptr;
143 return _pr;
144 }
145
146 } // namespace na64dp
147
148