cepgen is hosted by Hepforge, IPPP Durham
CepGen 1.2.5
Central exclusive processes event generator
Loading...
Searching...
No Matches
MCDFileParser.cpp
Go to the documentation of this file.
1/*
2 * CepGen: a central exclusive processes event generator
3 * Copyright (C) 2019-2024 Laurent Forthomme
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include <fstream>
20#include <string>
21
24#include "CepGen/Physics/PDG.h"
26#include "CepGen/Utils/String.h"
27
28namespace pdg {
29 const std::unordered_map<std::string, short> MCDFileParser::MAP_CHARGE_STR = {
30 {"-", -3}, {"--", -6}, {"+", +3}, {"++", +6}, {"0", 0}, {"-1/3", -1}, {"-2/3", -2}, {"+1/3", +1}, {"+2/3", +2}};
31
32 void MCDFileParser::parse(const std::string& path) {
33 for (const auto& line : cepgen::utils::split(cepgen::utils::readFile(path), '\n')) {
34 if (line[0] == '*') // skip comments
35 continue;
36 std::vector<int> pdg_ids, charges;
37 double mass{0.}, width{0.};
38 std::string part_name;
39 { // pdg ids
40 std::istringstream ss(line.substr(PDG_BEG, PDG_END));
41 std::string buf;
42 // split for each PDG id
43 while (ss >> buf)
44 pdg_ids.emplace_back(std::stoi(buf));
45 }
46 { // mass + error(s)
47 double mass_err_low{0.}, mass_err_high{0.}; // unused
48 const auto mass_substr = cepgen::utils::trim(line.substr(MASS_BEG, MASS_END));
49 if (!mass_substr.empty()) {
50 std::istringstream oss(mass_substr);
51 oss >> mass >> mass_err_low >> mass_err_high;
52 }
53 }
54 { // width + error(s)
55 double width_err_low{0.}, width_err_high{0.}; // unused
56 const auto width_substr = cepgen::utils::trim(line.substr(WIDTH_BEG, WIDTH_END));
57 if (!width_substr.empty()) {
58 std::istringstream oss(width_substr);
59 oss >> width >> width_err_low >> width_err_high;
60 }
61 }
62 { // name + charge
63 std::istringstream oss(line.substr(AUX_BEG));
64 std::string part_charge_int;
65 oss >> part_name >> part_charge_int;
66 std::istringstream oss_ch(part_charge_int);
67 std::string charge_int;
68 // split by ','
69 while (std::getline(oss_ch, charge_int, ',')) {
70 if (MAP_CHARGE_STR.count(charge_int) == 0)
71 throw CG_FATAL("MCDFileParser") << "Failed to retrieve an integer charge "
72 << "for string \"" << charge_int << "\"!";
73 charges.emplace_back(MAP_CHARGE_STR.at(charge_int));
74 }
75 }
76 if (pdg_ids.size() != charges.size())
77 throw CG_FATAL("MCDFileParser") << "Error while parsing the MCD file \"" << path << "\".\n\t"
78 << "Invalid PDG ids / charges vectors sizes: " << pdg_ids.size()
79 << " != " << charges.size() << ".";
81 prop.name = part_name;
82 prop.descr = part_name;
83 prop.colours = 1;
84 prop.mass = mass;
85 prop.width = width;
86 prop.fermion = false;
87 for (size_t i = 0; i < pdg_ids.size(); ++i) {
88 prop.pdgid = (cepgen::pdgid_t)pdg_ids.at(i);
89 if (const auto ch = charges.at(i); ch != 0)
90 prop.charges = {ch, -ch};
91 switch (pdg_ids.at(i)) {
92 // start with quarks
93 case 1:
94 case 2:
95 case 3:
96 case 4:
97 case 5:
98 case 6:
99 prop.colours = 3;
100 prop.fermion = true;
101 break;
102 // then move to leptons/neutrinos
103 case 11:
104 case 12:
105 case 13:
106 case 14:
107 case 15:
108 case 16:
109 prop.colours = 1;
110 prop.fermion = true;
111 break;
112 // then gluons
113 case 21:
114 prop.colours = 9;
115 prop.fermion = false;
116 break;
117 // and finally the rest
118 default:
119 break;
120 }
121 cepgen::PDG::get().define(prop);
122 }
123 }
124 CG_DEBUG("MCDFileParser") << cepgen::utils::s("particle", cepgen::PDG::get().size()) << " defined from \"" << path
125 << "\". ";
126 }
127} // namespace pdg
#define CG_FATAL(mod)
Definition Exception.h:61
#define CG_DEBUG(mod)
Definition Message.h:220
void define(const ParticleProperties &)
Add a new particle definition to the library.
Definition PDG.cpp:60
static PDG & get()
Retrieve a unique instance of this particles info collection.
Definition PDG.cpp:41
static void parse(const std::string &path)
Parse an external MCD file and retrieve all particles definition.
std::string s(const std::string &word, float num, bool show_number)
Add a trailing "s" when needed.
Definition String.cpp:228
std::string trim(const std::string &str)
Trim leading and trailing spaces.
Definition String.h:174
std::string readFile(const std::string &filename)
Read the content of a file into a string buffer.
std::vector< std::string > split(const std::string &str, char delim, bool trim)
Split a string according to a separation character.
Definition String.cpp:233
unsigned long long pdgid_t
Alias for the integer-like particle PDG id.
A collection of physics constants associated to a single particle.
std::string descr
Human-readable name.
double mass
Mass, in GeV/c .
pdgid_t pdgid
PDG identifier.
std::vector< int > charges
Electric charges, in /3.
std::string name
Particle name.
double width
Decay width, in GeV/c .
bool fermion
Is the particle a fermion?