GCC Code Coverage Report


Directory: ./
File: include/na64detID/TBName.hh
Date: 2025-09-01 06:19:01
Exec Total Coverage
Lines: 4 10 40.0%
Functions: 2 4 50.0%
Branches: 0 1 0.0%

Line Branch Exec Source
1 /* This file is a part of NA64SW software.
2 * Copyright (C) 2015-2022 NA64 Collaboration, CERN
3 *
4 * NA64SW is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>. */
16
17 #pragma once
18
19 #include "na64detID/detectorID.hh"
20 #include "na64detID/trackID.hh"
21 #include "na64util/selector.hh"
22
23 #include "na64detID/chips.hh"
24 #include "na64detID/kins.hh"
25
26 #include "na64detID/TBNameErrors.hh"
27
28 /**\file
29 * \brief Defines API to convert between detector names and numeric IDs.
30 *
31 * The TBName comes from NA58 (COMPASS) nomenclature. This short string ID
32 * usually looks like "ECAL0", "GM01X1", "DC05Y2__" and should uniquely
33 * identify a particular detector assembly. This file defines
34 * `DetectorNaming` type and related functions facilitating conversion between
35 * detector names and numeric IDs.
36 */
37
38 namespace YAML {
39 class Node; //fwd
40 }
41
42 namespace na64dp {
43 namespace nameutils {
44
45 /// \brief Defines mapping of detector textual ID to numerical ones.
46 ///
47 /// This class seems to be more complex that simple task of one-to-one
48 /// correspondance would require, but actually its goal is to expose all the guts
49 /// of two-staged naming conversion scheme to user code. Considering two
50 /// use-cases:
51 ///
52 /// - Conversion of naitve DDD TBNames, like `ECAL0`, `GM03X__` to numerical ID
53 /// - Conversion of NA64DP extended TBnames like `ECAL0:1-1`, `HCAL3:2-2` to
54 /// numerical ID.
55 ///
56 /// Instance of this object is usually associated with calibration data (see
57 /// class CalibHandle).
58 class DetectorNaming : protected ChipFeaturesTable
59 , protected KinFeaturesTable {
60 protected:
61 /// Internal version of subst dictionary appending function that relies
62 /// on chin&kin features being already found in tables
63 static void _append_subst_dict_for( DetID did
64 , std::map<std::string, std::string> & m
65 , const ChipFeaturesTable::Features * chipFtsPt=nullptr
66 , const KinFeaturesTable::Features * kinFtsPtr=nullptr );
67 public:
68 typedef ChipFeaturesTable::Features ChipFeatures;
69 using ChipFeaturesTable::chip_id;
70 using ChipFeaturesTable::chip_features;
71 using ChipFeaturesTable::chip_add;
72 using ChipFeaturesTable::has_chip;
73
74 typedef KinFeaturesTable::Features KinFeatures;
75 using KinFeaturesTable::KinID;
76 using KinFeaturesTable::kin_id;
77 using KinFeaturesTable::kin_features;
78 using KinFeaturesTable::define_kin;
79 using KinFeaturesTable::has_kin;
80
81 /// Wipes out all added entries.
82 void clear();
83
84 /// Defines new detector kin entry with chip given by its name
85 KinFeatures & define_kin( const std::string & kinName
86 , DetKin_t kinID
87 , const std::string & chipName
88 , const std::string & fmt="{kin}{statNum}"
89 , const std::string & kinDescription=""
90 , const std::string & histsTPath="" );
91
92 /// Returns DAQ detector entity name by numerical detector identifier.
93 std::string name( DetID did ) const;
94 /// Returns partial DAQ detector entity name by numerical detector
95 /// identifier, corresponds to a single station.
96 /// \todo Additional checks and assertions
97 std::string name( StationKey sk ) const;
98
99 /// Shortcut for "name()"
100 8 std::string operator[]( DetID did ) const { return name(did); }
101 /// Shortcut for "name()" based on station key
102 std::string operator[]( StationKey sk ) const { return name(sk); }
103 // ...TODO: other shortcuts?
104
105 /// Returns detector numerical identifier by given DAQ name
106 DetID id(const std::string & nm, bool noThrow=false) const;
107 /// Shortcut for "id()"
108 10 DetID operator[]( const std::string & nm ) const { return id(nm); }
109
110 /// Appends given map with detector-specific substitution entries
111 void append_subst_dict_for(DetID_t, std::map<std::string, std::string> &) const;
112
113 /// Appends given map with track-specific substitution entries
114 void append_subst_dict_for(TrackID, std::map<std::string, std::string> &) const;
115
116 /// Returns string template for histograms
117 const std::string & name_template(DetID) const;
118
119 /// Returns path string template for detector (by ID)
120 const std::string & path_template(StationKey detID) const;
121 };
122
123 /// Defines names to be emitted into string substitution context for certain
124 /// identifier type
125 template<typename EntityIDT, typename=void> struct DetectorIDStrSubstTraits;
126
127 /// Specialization for usual detectorID
128 template< typename EntityIDT >
129 struct DetectorIDStrSubstTraits<EntityIDT
130 , typename std::enable_if<std::is_base_of<HitTypeKey, EntityIDT>::value>::type
131 > {
132 static void append_context( const nameutils::DetectorNaming & nm
133 , EntityIDT did
134 , util::StrSubstDict & ctx
135 ) {
136 nm.append_subst_dict_for(DetID(did), ctx);
137 try {
138 ctx["TBName"] = nm[did];
139 } catch( errors::IncompleteDetectorID & e ) {
140 ; // pass (no TBname available)
141 }
142 }
143 static const std::string * path_template_for( const nameutils::DetectorNaming & nm
144 , EntityIDT did ) {
145 return &nm.path_template(did);
146 }
147 };
148
149 /// Specialization for track IDs
150 template<>
151 struct DetectorIDStrSubstTraits<TrackID> {
152 /// Appends context with "first.* and "second.*" entities got from first
153 /// and second detector ID
154 static void append_context( const nameutils::DetectorNaming &
155 , TrackID tid
156 , util::StrSubstDict & ctx
157 ) {
158 char bf[32];
159 snprintf(bf, sizeof(bf), "%zu", (size_t) tid.code);
160 ctx["id"] = bf;
161 snprintf(bf, sizeof(bf), "%#x", tid.zones() );
162 ctx["trackZonePattern"] = bf;
163 }
164 /// Returns a default path template string for all the binary keys
165 static const std::string * path_template_for( const nameutils::DetectorNaming & //nm
166 , TrackID //tid
167 ) {
168 static const std::string tmpl("byTrackID/{trackZonePattern}-{id}/{hist}");
169 return &tmpl;
170 }
171 };
172
173 /// Specialization for zone IDs
174 template<>
175 struct DetectorIDStrSubstTraits<ZoneID> {
176 /// Appends context with "first.* and "second.*" entities got from first
177 /// and second detector ID
178 static void append_context( const nameutils::DetectorNaming &
179 , ZoneID tid
180 , util::StrSubstDict & ctx
181 ) {
182 char bf[32];
183 snprintf(bf, sizeof(bf), "%#x", (int) tid.zoneCode );
184 ctx["trackZonePattern"] = bf;
185 }
186 /// Returns a default path template string for all the binary keys
187 static const std::string * path_template_for( const nameutils::DetectorNaming & //nm
188 , ZoneID //tid
189 ) {
190 static const std::string tmpl("byTrackZone/{trackZonePattern}/{hist}");
191 return &tmpl;
192 }
193 };
194
195 /// Specialization for integral (vertex) IDs
196 template<>
197 struct DetectorIDStrSubstTraits<size_t> {
198 /// Appends context with "first.* and "second.*" entities got from first
199 /// and second detector ID
200 static void append_context( const nameutils::DetectorNaming &
201 , size_t id
202 , util::StrSubstDict & ctx
203 ) {
204 char bf[32];
205 snprintf(bf, sizeof(bf), "%zu", id);
206 ctx["id"] = bf;
207 }
208 /// Returns a default path template string for all the binary keys
209 static const std::string * path_template_for( const nameutils::DetectorNaming & //nm
210 , size_t //id
211 ) {
212 static const std::string tmpl("byID/{hist}-{id}");
213 return &tmpl;
214 }
215 };
216
217 /// Specialization for pair of detector IDs
218 template<>
219 struct DetectorIDStrSubstTraits< std::pair<DetID, DetID> > {
220 /// Appends context with "first.* and "second.*" entities got from first
221 /// and second detector ID
222 static void append_context( const nameutils::DetectorNaming & nm
223 , std::pair<DetID, DetID> did
224 , util::StrSubstDict & ctx
225 );
226 /// Returns a default path template string for all the binary keys
227 static const std::string * path_template_for( const nameutils::DetectorNaming & nm
228 , std::pair<DetID, DetID> k );
229 };
230
231
232 namespace detail {
233 template<std::string KinFeaturesTable::Features::*attr> struct DetStrFts;
234 // nameFormat
235 template<>
236 struct DetStrFts<&KinFeaturesTable::Features::nameFormat> {
237 constexpr static std::string ChipFeaturesTable::Features::*chipPtr
238 = &ChipFeaturesTable::Features::nameFormat;
239 };
240 // dddNameFormat
241 template<>
242 struct DetStrFts<&KinFeaturesTable::Features::dddNameFormat> {
243 constexpr static std::string ChipFeaturesTable::Features::*chipPtr
244 = &ChipFeaturesTable::Features::dddNameFormat;
245 };
246 // pathFormat
247 template<>
248 struct DetStrFts<&KinFeaturesTable::Features::pathFormat> {
249 constexpr static std::string ChipFeaturesTable::Features::*chipPtr
250 = &ChipFeaturesTable::Features::pathFormat;
251 };
252 };
253
254 /// Returns detector string pattern according to order of overriding
255 template<std::string KinFeaturesTable::Features::*attr> const std::string &
256 8 detector_string_patern( const ChipFeaturesTable::Features & chipFts
257 , const KinFeaturesTable::Features & kinFts ) {
258 8 if( ! ((kinFts.*attr).empty()) ) return kinFts.*attr;
259 return chipFts.*(detail::DetStrFts<attr>::chipPtr);
260 }
261
262 } // namespace na64dp::utils
263
264
265 /// Alias for common detector ID selection type
266 typedef dsul::Selector<DetID, nameutils::DetectorNaming> DetSelect;
267
268 } // namespace na64dp
269