ATLAS Offline Software
Loading...
Searching...
No Matches
DecayParser.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
6// DecayParser.cxx
7// Implementation file for class DecayParser
8// Author: S.Binet<binet@cern.ch>
10
11// STL includes
12#include <algorithm>
13#include <cctype>
14#include <iostream>
15#include <ranges>
16#include <stdexcept>
17#include <sstream>
18#include <string_view>
19
20// McParticleUtils includes
22
23namespace {
24
25std::vector<McUtils::Strings> process_block(std::string_view cmd)
26{
27 std::vector<McUtils::Strings> result;
28 if (cmd.empty()) {
29 return result;
30 }
31
32 std::vector<std::string> slots;
33 for (auto const& token : cmd | std::views::split('+')) {
34 slots.emplace_back(token.begin(), token.end());
35 }
36
37 result.reserve(slots.size());
38 for (auto const& slot : slots) {
40 for (auto const& token : slot | std::views::split('|')) {
41 candidates.emplace_back(token.begin(), token.end());
42 }
43 std::ranges::sort(candidates);
44 auto [first, last] = std::ranges::unique(candidates);
45 candidates.erase(first, last);
46 result.emplace_back(std::move(candidates));
47 }
48
49 return result;
50}
51
52} // anonymous namespace
53
57
60
61DecayParser::DecayParser( const std::string& cmd ) :
62 m_parents ( ),
63 m_children ( )
64{
65 parse(cmd);
66}
67
68
74
79{
80 std::cout << "--- Parents ---" << std::endl;
82
83 std::cout << "--- Children ---" << std::endl;
85}
86
87int DecayParser::pdgId( const std::string& pdgIdString ) const
88{
89 int pdgID = 0;
90 int iPDG = 0;
91 std::stringstream( pdgIdString ) >> iPDG;
92 pdgID = iPDG;
93
94 return pdgID;
95}
96
99void DecayParser::parse( const std::string& inputCmd )
100{
101 if ( inputCmd.empty() ) {
102 return;
103 }
104
105 std::string cmd;
106 cmd.reserve(inputCmd.size());
107 for (unsigned char c : inputCmd) {
108 if (!std::isspace(c)) {
109 cmd.push_back(c);
110 }
111 }
112 if (cmd.empty()) {
113 return;
114 }
115
116 // Reset the parents and children lists
117 m_parents.clear();
118 m_children.clear();
119
120 if (cmd == "->") {
121 return;
122 }
123
124 const std::size_t arrowPos = cmd.find("->");
125 if (arrowPos == std::string::npos) {
126 std::string error = "missing '->' in command [" + inputCmd + "]";
127 throw std::runtime_error(error);
128 }
129
130 if (cmd.find("->", arrowPos + 2) != std::string::npos) {
131 std::string error = "multiple '->' separators in command [" + inputCmd + "]";
132 throw std::runtime_error(error);
133 }
134
135 const std::string_view cmdView{cmd};
136 const auto parentsBlock = cmdView.substr(0, arrowPos);
137 const auto childrenBlock = cmdView.substr(arrowPos + 2);
138
139 m_parents = process_block(parentsBlock);
140 m_children = process_block(childrenBlock);
141}
142
143
144void
145DecayParser::printMcUtilsStrings( const std::vector<McUtils::Strings>& list ) const
146{
147 unsigned int iSlot = 0;
148 for( std::vector<McUtils::Strings>::const_iterator itr = list.begin();
149 itr != list.end();
150 ++itr,++iSlot ) {
151 std::stringstream iSlotStr;
152 iSlotStr << iSlot;
153 const McUtils::Strings::const_iterator candEnd = itr->end();
154 std::cout << "slot #" << iSlotStr.str() << ": candidates= [ ";
155 for( McUtils::Strings::const_iterator candidate = itr->begin();
156 candidate != candEnd;
157 ++candidate ) {
158 std::cout << *candidate;
159 if ( candidate+1 != candEnd ) {
160 std::cout << " | ";
161 }
162 }
163 std::cout << " ]" << std::endl;
164 }
165 return;
166}
167
168
170// Operators:
173{
174 if ( this != &rhs ) {
175 m_parents = rhs.m_parents;
177 }
178 return *this;
179}
DecayParser()
Default constructor:
void printMcUtilsStrings(const std::vector< McUtils::Strings > &list) const
Print the content of a vector of McUtils::Strings to std::cout.
void dump() const
Const methods:
virtual ~DecayParser()
Destructor:
DecayParser(const std::string &cmd)
Constructor with parameters:
std::vector< McUtils::Strings > m_parents
List of parents : each slot of the vector is a list of candidates So one could have something like : ...
Definition DecayParser.h:90
void parse(const std::string &cmd)
Non-const methods:
int pdgId(const std::string &pdgIdString) const
DecayParser & operator=(const DecayParser &obj)
Assignment operator:
std::vector< McUtils::Strings > m_children
List of children : each slot of the vector is a list of candidates So one could have something like :...
Definition DecayParser.h:96
bool first
Definition DeMoScan.py:534
std::vector< std::string > Strings
Definition DecayParser.h:26