ATLAS Offline Software
Loading...
Searching...
No Matches
IdDictParser.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
9
13#include "IdDict/IdDictField.h"
15#include "IdDict/IdDictLabel.h"
16#include "IdDict/IdDictRange.h"
18#include "IdDict/IdDictRegion.h"
21
22#include <cstdlib>
23#include <iostream>
24
26 class Debugger {
27 public:
28 static bool get_debug_state(){
29 return ::getenv ("XMLDEBUG") != 0;
30 }
31 //
32 static bool debug (){
33 static const bool debug_state = get_debug_state();
34 return debug_state;
35 }
36 //
37 static void tab (int n) {
38 std::cout << std::string(n, ' ');
39 }
40 };
41
43 public:
44 void do_start (XMLCoreParser& parser, const XMLCoreNode& node);
45 void do_end (XMLCoreParser& parser, const XMLCoreNode& node);
46 virtual void idd_start (IdDictParser& parser, const XMLCoreNode& node);
47 virtual void idd_end (IdDictParser& parser, const XMLCoreNode& node);
48 };
49
51 public:
52 void idd_start (IdDictParser& parser, const XMLCoreNode& node);
53 void idd_end (IdDictParser& parser, const XMLCoreNode& node);
54 };
55
57 public:
58 void idd_start (IdDictParser& parser, const XMLCoreNode& node);
59 void idd_end (IdDictParser& parser, const XMLCoreNode& node);
60 };
61
63 public:
64 void idd_start (IdDictParser& parser, const XMLCoreNode& node);
65 void idd_end (IdDictParser& parser, const XMLCoreNode& node);
66 };
67
69 public:
70 void idd_start (IdDictParser& parser, const XMLCoreNode& node);
71 void idd_end (IdDictParser& parser, const XMLCoreNode& node);
72 };
73
75 public:
76 void idd_start (IdDictParser& parser, const XMLCoreNode& node);
77 void idd_end (IdDictParser& parser, const XMLCoreNode& node);
78 };
79
81 public:
82 void idd_start (IdDictParser& parser, const XMLCoreNode& node);
83 void idd_end (IdDictParser& parser, const XMLCoreNode& node);
84 };
85
87 public:
88 void idd_start (IdDictParser& parser, const XMLCoreNode& node);
89 void idd_end (IdDictParser& parser, const XMLCoreNode& node);
90 };
91
93 public:
94 virtual void idd_start (IdDictParser& parser, const XMLCoreNode& node);
95 virtual void idd_end (IdDictParser& parser, const XMLCoreNode& node);
96 };
97
99 public:
100 void idd_start (IdDictParser& parser, const XMLCoreNode& node);
101 };
102
104 public:
105 void idd_start (IdDictParser& parser, const XMLCoreNode& node);
106 };
107
109 public:
110 void idd_start (IdDictParser& parser, const XMLCoreNode& node);
111 };
112}
113
114using namespace IdDictParserNS;
115
117 register_factory ("IdDict", std::make_unique<IdDictMgrFactory>());
118 register_factory ("IdDictionary", std::make_unique<DictionaryFactory>());
119 register_factory ("field", std::make_unique<FieldFactory>());
120 register_factory ("label", std::make_unique<LabelFactory>());
121 register_factory ("alternate_regions", std::make_unique<AltRegionsFactory>());
122 register_factory ("region", std::make_unique<RegionFactory>());
123 register_factory ("subregion", std::make_unique<SubRegionFactory>());
124 register_factory ("range", std::make_unique<RangeFactory>());
125 register_factory ("reference", std::make_unique<ReferenceFactory>());
126 register_factory ("dictionary", std::make_unique<DictionaryRefFactory>());
127}
128
129IdDictParser::~IdDictParser () = default;
130
131IdDictMgr&
132IdDictParser::parse (const std::string& file_name, const std::string& tag) {
133 m_idd.clear ();
134 XMLCoreParser::visit (file_name);
135 if (Debugger::debug ()) {
136 std::cout << "IdDictParser::parse1>" << std::endl;
137 }
138 m_idd.resolve_references ();
139 if (Debugger::debug ()) {
140 std::cout << "IdDictParser::parse2>" << std::endl;
141 }
142 m_idd.generate_implementation (tag);
143 if (Debugger::debug ()) {
144 std::cout << "IdDictParser::parse3>" << std::endl;
145 }
146 return (m_idd);
147}
148
149void
151 const XMLCoreNode& node) {
152 parser.up ();
153 idd_start (static_cast<IdDictParser&>(parser), node);
154}
155
156void
158 idd_end (static_cast<IdDictParser&>(parser), node);
159 parser.down ();
160}
161
163 const XMLCoreNode& /*node*/) {
164}
165
167 const XMLCoreNode& /*node*/) {
168}
169
170
171
172void
174 if (Debugger::debug ()) {
175 Debugger::tab (parser.level());
176 std::cout << "IdDictMgrFactoryFactory::idd_start>" << std::endl;
177 }
178 parser.m_idd.set_DTD_version(get_value (node, "DTD_version"));
179}
180
181void
183 const XMLCoreNode& /*node*/) {
184 if (Debugger::debug ()) {
185 Debugger::tab (parser.level());
186 std::cout << "IdDictMgrFactoryFactory::idd_end>" << std::endl;
187 }
188}
189
190void
192 if (Debugger::debug ()) {
193 Debugger::tab (parser.level());
194 std::cout << "DictionaryFactory::idd_start>" << std::endl;
195 }
196 parser.m_dictionary = std::make_unique<IdDictDictionary>
197 (get_value (node, "name"),
198 get_value (node, "version"),
199 get_value (node, "date"),
200 get_value (node, "author"));
201}
202
203void
205 if (Debugger::debug ()) {
206 Debugger::tab (parser.level());
207 std::cout << "DictionaryFactory::idd_end> d=" << parser.m_dictionary.get() << std::endl;
208 }
209
210 if (parser.m_dictionary != 0){
211 parser.m_idd.add_dictionary (std::move(parser.m_dictionary));
212 }
213}
214
215void
217 if (Debugger::debug ()) {
218 Debugger::tab (parser.level());
219 std::cout << "FieldFactory::idd_start>" << std::endl;
220 }
221 parser.m_field = std::make_unique<IdDictField>(get_value (node, "name"));
222}
223
224void
225FieldFactory::idd_end (IdDictParser& parser, const XMLCoreNode& /*node*/) {
226 if (Debugger::debug ()) {
227 Debugger::tab (parser.level());
228 std::cout << "FieldFactory::idd_end>" << std::endl;
229 }
230
231 if (parser.m_field != 0) {
232 if (parser.m_dictionary != 0) parser.m_dictionary->add_field (std::move(parser.m_field));
233 else parser.m_field.reset();
234 }
235}
236
237void
239 if (Debugger::debug ()) {
240 Debugger::tab (parser.level());
241 std::cout << "LabelFactory::idd_start>" << std::endl;
242 }
243
244 std::unique_ptr<IdDictLabel> label;
245 if (has_attribute (node, "value")) {
246 label = std::make_unique<IdDictLabel> (get_value (node, "name"), get_int (node, "value"));
247 } else {
248 label = std::make_unique<IdDictLabel> (get_value (node, "name"));
249 }
250 if (parser.m_field != 0) parser.m_field->add_label (std::move(label));
251 else label.reset();
252}
253
254void
255LabelFactory::idd_end (IdDictParser& parser, const XMLCoreNode& /*node*/) {
256 if (Debugger::debug ()) {
257 Debugger::tab (parser.level());
258 std::cout << "LabelFactory::idd_end>" << std::endl;
259 }
260}
261
262void
264 if (Debugger::debug ()) {
265 Debugger::tab (parser.level());
266 std::cout << "AltRegionsFactory::idd_start>" << std::endl;
267 }
268 auto altregions = std::make_unique<IdDictAltRegions>();
269 if (Debugger::debug ()) {
270 Debugger::tab (parser.level());
271 std::cout << "AltRegionsFactory::idd_start> previous=" << parser.m_altregions.get()
272 << " new=" << altregions.get()
273 << std::endl;
274 }
275 parser.m_altregions = std::move(altregions);
276}
277
278void
280 if (Debugger::debug ()) {
281 Debugger::tab (parser.level());
282 std::cout << "AltRegionsFactory::idd_end>" << std::endl;
283 }
284 if (parser.m_altregions != 0) {
285 // Set altregions to point to the default region
286 parser.m_altregions->select_region ("");
287
288 // add to dict
289 if (parser.m_dictionary != 0) parser.m_dictionary->add_dictentry (std::move(parser.m_altregions));
290 else parser.m_altregions.reset();
291 }
292}
293
294void
296 if (Debugger::debug ()) {
297 Debugger::tab (parser.level());
298 std::cout << "RegionFactory::idd_start>" << std::endl;
299 }
300
301 parser.m_region = std::make_unique<IdDictRegion> (get_value (node, "name"),
302 get_value (node, "group"),
303 get_value (node, "tag"));
304
305 // check for next region in absolute eta
306 if (has_attribute (node, "next_abs_eta")) {
307 parser.m_region->set_next_abs_eta_name (get_value (node, "next_abs_eta"));
308 }
309
310 // Look for regions in the previous sampling
311 if (has_attribute (node, "prev_samp")) {
312 std::string names = get_value (node, "prev_samp");
313 std::string::size_type pos = 0;
314 for (;;) {
315 std::string::size_type next;
316 next = names.find_first_not_of (" ", pos);
317 if (next == std::string::npos) break;
318 pos = next;
319 next = names.find_first_of (" ", pos);
320 std::string name;
321 name = names.substr (pos, next - pos);
322 parser.m_region->add_prev_samp_name (name);
323 if (next == std::string::npos) {
324 break;
325 } else {
326 pos = next;
327 }
328 }
329 }
330 // Look for regions in the next sampling
331 if (has_attribute (node, "next_samp")) {
332 std::string names = get_value (node, "next_samp");
333 std::string::size_type pos = 0;
334 for (;;) {
335 std::string::size_type next;
336
337 next = names.find_first_not_of (" ", pos);
338 if (next == std::string::npos) break;
339 pos = next;
340 next = names.find_first_of (" ", pos);
341
342 std::string name;
343 name = names.substr (pos, next - pos);
344 parser.m_region->add_next_samp_name (name);
345
346 if (next == std::string::npos) {
347 break;
348 } else {
349 pos = next;
350 }
351 }
352 }
353 // Look for regions in the prev subdet
354 if (has_attribute (node, "prev_subdet")) {
355 std::string names = get_value (node, "prev_subdet");
356 std::string::size_type pos = 0;
357 for (;;) {
358 std::string::size_type prev;
359 prev = names.find_first_not_of (" ", pos);
360 if (prev == std::string::npos) break;
361 pos = prev;
362 prev = names.find_first_of (" ", pos);
363 std::string name;
364 name = names.substr (pos, prev - pos);
365 parser.m_region->add_prev_subdet_name (name);
366 if (prev == std::string::npos) {
367 break;
368 } else {
369 pos = prev;
370 }
371 }
372 }
373 // Look for regions in the next subdet
374 if (has_attribute (node, "next_subdet")) {
375 std::string names = get_value (node, "next_subdet");
376 std::string::size_type pos = 0;
377 for (;;) {
378 std::string::size_type next;
379 next = names.find_first_not_of (" ", pos);
380 if (next == std::string::npos) break;
381 pos = next;
382 next = names.find_first_of (" ", pos);
383 std::string name;
384 name = names.substr (pos, next - pos);
385 parser.m_region->add_next_subdet_name (name);
386 if (next == std::string::npos) {
387 break;
388 } else {
389 pos = next;
390 }
391 }
392 }
393
394 parser.m_region->set_etaphi (get_double (node, "eta0"),
395 get_double (node, "deta"),
396 get_double (node, "phi0"),
397 get_double (node, "dphi"));
398
399 if (Debugger::debug ()) {
400 Debugger::tab (parser.level());
401 std::cout << "RegionFactory::idd_start> name, group, tag, next eta, prev/next samp, prev/next subdet "
402 << parser.m_region->name() << " "
403 << parser.m_region->group_name() << " "
404 << parser.m_region->tag() << " "
405 << parser.m_region->next_abs_eta() << " ";
406 for (const std::string& s : parser.m_region->prev_samp_names()) {
407 std::cout << s << " ";
408 }
409 for (const std::string& s : parser.m_region->next_samp_names()) {
410 std::cout << s << " ";
411 }
412 for (const std::string& s : parser.m_region->prev_subdet_names()) {
413 std::cout << s << " ";
414 }
415 for (const std::string& s : parser.m_region->next_subdet_names()) {
416 std::cout << s << " ";
417 }
418 std::cout << parser.m_region->eta0() << " "
419 << parser.m_region->deta() << " "
420 << parser.m_region->phi0() << " "
421 << parser.m_region->dphi() << " "
422 << std::endl;
423 }
424}
425
426void
428 if (Debugger::debug ()){
429 Debugger::tab (parser.level());
430 std::cout << "RegionFactory::idd_end>" << std::endl;
431 }
432 if (parser.m_region != 0){
433 if (parser.m_dictionary != 0) parser.m_dictionary->add_region (parser.m_region.get());
434 // Check whether region is empty, i.e. no region entries have
435 // been found and added
436 if (parser.m_region->n_entries() == 0) {
437 parser.m_region->set_is_empty();
438 }
439
440 if (parser.m_altregions != 0) {
441 parser.m_altregions->add_region (std::move(parser.m_region));
442 } else if (parser.m_dictionary != 0) {
443 parser.m_dictionary->add_dictentry (std::move(parser.m_region));
444 } else {
445 parser.m_region.reset();
446 }
447 }
448}
449
450void
452 if (Debugger::debug ()){
453 Debugger::tab (parser.level());
454 std::cout << "SubRegionFactory::idd_start>" << std::endl;
455 }
456 parser.m_subregion = std::make_unique<IdDictSubRegion> (get_value (node, "name"), "", "");
457}
458
459void
461 if (Debugger::debug ()) {
462 Debugger::tab (parser.level());
463 std::cout << "SubRegionFactory::idd_end>" << std::endl;
464 }
465 if (parser.m_subregion != 0) {
466 if (parser.m_dictionary != 0) parser.m_dictionary->add_subregion (std::move(parser.m_subregion));
467 else parser.m_subregion.reset();
468 }
469}
470
471void
473 if (Debugger::debug ()){
474 Debugger::tab (parser.level());
475 std::cout << "RegionEntryFactory::idd_start>" << std::endl;
476 }
477}
478
479void
481 if (Debugger::debug ()) {
482 Debugger::tab (parser.level());
483 std::cout << "RegionEntryFactory::idd_end>" << std::endl;
484 }
485 if (parser.m_regionentry != 0){
486 if (parser.m_region != 0) parser.m_region->add_entry (std::move(parser.m_regionentry));
487 else if (parser.m_subregion != 0) parser.m_subregion->add_entry (std::move(parser.m_regionentry));
488 else parser.m_regionentry.reset();
489 }
490}
491
492void
494 if (Debugger::debug ()) {
495 Debugger::tab (parser.level());
496 std::cout << "RangeFactory::idd_start>" << std::endl;
497 }
498 auto range = std::make_unique<IdDictRange> (get_value (node, "field"));
499 if (has_attribute (node, "value")){
500 range->set_range (get_value (node, "value"));
501 } else if (has_attribute (node, "values")) {
502 std::string labels = get_value (node, "values");
503 std::string::size_type pos = 0;
504 std::vector<std::string> label_vec;
505 for (;;){
506 std::string::size_type next;
507 next = labels.find_first_not_of (" ", pos);
508 if (next == std::string::npos) break;
509 pos = next;
510 next = labels.find_first_of (" ", pos);
511 label_vec.push_back (labels.substr (pos, next - pos));
512 if (next == std::string::npos) {
513 break;
514 } else {
515 pos = next;
516 }
517 }
518 range->set_range (label_vec);
519 } else {
520 const bool hasMin = has_attribute (node, "minvalue");
521 const bool hasMax = has_attribute (node, "maxvalue");
522 if (hasMin and hasMax) {
523 range->set_range (get_int (node, "minvalue"),
524 get_int (node, "maxvalue"));
525 }
526 //falls through to a case where there is *no* attribute value, values, minvalue, maxvalue
527 //https://its.cern.ch/jira/browse/ATLASSIM-7295
528 }
529 if (has_attribute (node, "wraparound")){
530 bool wraparound = get_boolean (node, "wraparound");
531 if (wraparound) range->set_wrap_around();
532 }
533 if (has_attribute (node, "prev_value")){
534 range->set_prev (get_int (node, "prev_value"));
535 }
536 if (has_attribute (node, "next_value")) {
537 range->set_next (get_int (node, "next_value"));
538 }
539 parser.m_regionentry = std::move(range);
540}
541
542void
544 if (Debugger::debug ()){
545 Debugger::tab (parser.level());
546 std::cout << "ReferenceFactory::idd_start>" << std::endl;
547 }
548 parser.m_regionentry = std::make_unique<IdDictReference>(get_value (node, "subregion"));
549}
550
551void
553 if (Debugger::debug ()) {
554 Debugger::tab (parser.level());
555 std::cout << "DictionaryRefFactory::idd_start>" << std::endl;
556 }
557 auto dictionaryref = std::make_unique<IdDictDictionaryRef> (get_value (node, "name"));
558 // Add dictionary name to subdictionaries
559 if (dictionaryref->dictionary_name() != "") {
560 parser.m_idd.add_subdictionary_name (dictionaryref->dictionary_name());
561 if (parser.m_dictionary != 0) parser.m_dictionary->add_subdictionary_name (dictionaryref->dictionary_name());
562 }
563 parser.m_regionentry = std::move(dictionaryref);
564}
565
static bool debug()
Definition Debugger.h:18
void idd_end(IdDictParser &parser, const XMLCoreNode &node)
void idd_start(IdDictParser &parser, const XMLCoreNode &node)
static bool get_debug_state()
static void tab(int n)
void idd_end(IdDictParser &parser, const XMLCoreNode &node)
void idd_start(IdDictParser &parser, const XMLCoreNode &node)
void idd_start(IdDictParser &parser, const XMLCoreNode &node)
void idd_start(IdDictParser &parser, const XMLCoreNode &node)
void idd_end(IdDictParser &parser, const XMLCoreNode &node)
virtual void idd_start(IdDictParser &parser, const XMLCoreNode &node)
virtual void idd_end(IdDictParser &parser, const XMLCoreNode &node)
void do_end(XMLCoreParser &parser, const XMLCoreNode &node)
void do_start(XMLCoreParser &parser, const XMLCoreNode &node)
void idd_start(IdDictParser &parser, const XMLCoreNode &node)
void idd_end(IdDictParser &parser, const XMLCoreNode &node)
void idd_end(IdDictParser &parser, const XMLCoreNode &node)
void idd_start(IdDictParser &parser, const XMLCoreNode &node)
void idd_start(IdDictParser &parser, const XMLCoreNode &node)
void idd_start(IdDictParser &parser, const XMLCoreNode &node)
virtual void idd_start(IdDictParser &parser, const XMLCoreNode &node)
virtual void idd_end(IdDictParser &parser, const XMLCoreNode &node)
void idd_start(IdDictParser &parser, const XMLCoreNode &node)
void idd_end(IdDictParser &parser, const XMLCoreNode &node)
void idd_start(IdDictParser &parser, const XMLCoreNode &node)
void idd_end(IdDictParser &parser, const XMLCoreNode &node)
IdDictMgr & parse(const std::string &file_name, const std::string &tag="")
IdDictMgr m_idd
static std::string get_value(const XMLCoreNode &node, const std::string &name)
static bool has_attribute(const XMLCoreNode &node, const std::string &name)
static double get_double(const XMLCoreNode &node, const std::string &name)
static int get_int(const XMLCoreNode &node, const std::string &name)
static bool get_boolean(const XMLCoreNode &node, const std::string &name)
void visit(const std::string &file_name)
void register_factory(const std::string &name, std::unique_ptr< XMLCoreFactory > factory)
Definition node.h:24
std::string label(const std::string &format, int i)
Definition label.h:19