cepgen is hosted by Hepforge, IPPP Durham
CepGen 1.2.5
Central exclusive processes event generator
Loading...
Searching...
No Matches
EventBrowser.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
20#include "CepGen/Event/Event.h"
22#include "CepGen/Utils/String.h"
23
24namespace cepgen {
25 namespace utils {
26 const std::regex EventBrowser::rgx_select_id_("([a-zA-Z0-9]+)\\(([0-9]+)\\)", std::regex_constants::extended);
27 const std::regex EventBrowser::rgx_select_id2_("([a-zA-Z0-9]+)\\(([0-9]+),([0-9]+)\\)",
28 std::regex_constants::extended);
29 const std::regex EventBrowser::rgx_select_role_("([a-zA-Z0-9]+)\\(([a-z]+[0-9]?)\\)",
30 std::regex_constants::extended);
31 const std::regex EventBrowser::rgx_select_role2_("([a-zA-Z0-9]+)\\(([a-z]+[0-9]?),([a-z]+[0-9]?)\\)",
32 std::regex_constants::extended);
33
34 double EventBrowser::get(const Event& ev, const std::string& var) const {
35 std::smatch sm;
36 //--- particle-level variables (indexed by integer id)
37 if (std::regex_match(var, sm, rgx_select_id_)) {
38 const auto& var_name = sm[1].str();
39 const auto& part = ev(std::stoul(sm[2].str()));
40 return variable(ev, part, var_name);
41 }
42 if (std::regex_match(var, sm, rgx_select_id2_)) {
43 const auto& var_name = sm[1].str();
44 const auto& part1 = ev(std::stoul(sm[2].str()));
45 const auto& part2 = ev(std::stoul(sm[3].str()));
46 return variable(ev, part1, part2, var_name);
47 }
48 //--- particle-level variables (indexed by role)
49 const auto check_role = [&](const std::string& role, const std::string& var) -> bool {
50 bool ret = role_str_.count(role) > 0;
51 if (!ret)
52 CG_WARNING("EventBrowser") << "Invalid particle role retrieved from configuration: \"" << role << "\".\n\t"
53 << "Skipping the variable \"" << var << "\" in the output module.";
54 return ret;
55 };
56 if (std::regex_match(var, sm, rgx_select_role_)) {
57 const auto& var_name = sm[1].str();
58 const auto& str_role = sm[2].str();
59 if (!check_role(str_role, var))
60 return INVALID_OUTPUT;
61 const auto& part = ev(role_str_.at(str_role))[0];
62 return variable(ev, part, var_name);
63 }
64 if (std::regex_match(var, sm, rgx_select_role2_)) {
65 const auto& var_name = sm[1].str();
66 const auto& str_role1 = sm[2].str();
67 const auto& str_role2 = sm[3].str();
68 if (!check_role(str_role1, var) || !check_role(str_role2, var))
69 return INVALID_OUTPUT;
70 const auto& part1 = ev(role_str_.at(str_role1))[0];
71 const auto& part2 = ev(role_str_.at(str_role2))[0];
72 return variable(ev, part1, part2, var_name);
73 }
74 //--- event-level variables
75 return variable(ev, var);
76 }
77
78 double EventBrowser::variable(const Event& ev, const Particle& part, const std::string& var) const {
79 if (m_mom_str_.count(var)) {
80 const auto& meth = m_mom_str_.at(var);
81 return (part.momentum().*meth)();
82 }
83 if (var == "xi") {
84 const auto& moth = part.mothers();
85 if (moth.empty()) {
86 CG_WARNING("EventBrowser") << "Failed to retrieve parent particle to compute xi "
87 << "for the following particle:\n"
88 << part;
89 return INVALID_OUTPUT;
90 }
91 return 1. - part.momentum().energy() / ev(int(*moth.begin())).momentum().energy();
92 }
93 if (var == "pdg")
94 return (double)part.integerPdgId();
95 if (var == "charge")
96 return part.charge();
97 if (var == "status")
98 return (double)part.status();
99 throw CG_ERROR("EventBrowser") << "Failed to retrieve variable \"" << var << "\".";
100 }
101
102 double EventBrowser::variable(const Event&,
103 const Particle& part1,
104 const Particle& part2,
105 const std::string& var) const {
106 if (m_two_mom_str_.count(var)) {
107 const auto& meth = m_two_mom_str_.at(var);
108 return (part1.momentum().*meth)(part2.momentum());
109 }
110 if (m_mom_str_.count(var)) {
111 const auto& meth = m_mom_str_.at(var);
112 return ((part1.momentum() + part2.momentum()).*meth)();
113 }
114 if (var == "acop")
115 return 1. - fabs(part1.momentum().deltaPhi(part2.momentum()) * M_1_PI);
116 throw CG_ERROR("EventBrowser") << "Failed to retrieve variable \"" << var << "\".";
117 }
118
119 double EventBrowser::variable(const Event& ev, const std::string& var) {
120 if (var == "np")
121 return (double)ev.size();
122 //if ( var == "nev" )
123 // return (double)num_evts_+1;
124 if (var == "nob1" || var == "nob2") {
125 const auto& bparts = ev(var == "nob1" ? Particle::Role::OutgoingBeam1 : Particle::Role::OutgoingBeam2);
126 return (double)std::count_if(
127 bparts.begin(), bparts.end(), [](const auto& part) { return (int)part.status() > 0; });
128 }
129 if (var == "met")
130 return ev.missingMomentum().pt();
131 if (var == "mephi")
132 return ev.missingMomentum().phi();
133 if (utils::startsWith(var, "meta:"))
134 return ev.metadata(var.substr(5));
135 throw CG_ERROR("EventBrowser") << "Failed to retrieve the event-level variable \"" << var << "\".";
136 }
137 } // namespace utils
138} // namespace cepgen
#define CG_ERROR(mod)
Definition Exception.h:60
#define CG_WARNING(mod)
Definition Message.h:228
Container for the information on the in- and outgoing particles' kinematics.
Definition Event.h:28
double energy() const
Energy (in GeV)
Definition Momentum.h:136
Kinematic information for one particle.
Definition Particle.h:33
Momentum & momentum()
Retrieve the momentum object associated with this particle Retrieve the momentum object associated wi...
Definition Particle.h:123
long integerPdgId() const
Retrieve the integer value of the PDG identifier.
Definition Particle.cpp:102
ParticlesIds mothers() const
Identifier to the mother particles.
Definition Particle.h:144
float charge() const
Electric charge (given as a float number, for the quarks and bound states) Set whether we are coping ...
Definition Particle.cpp:45
@ OutgoingBeam1
outgoing beam state/particle
Definition Particle.h:54
Status status() const
Particle status Set the particle decay/stability status.
Definition Particle.h:94
double get(const Event &ev, const std::string &var) const
Get/compute a variable value.
bool startsWith(const std::string &str, const std::string &beg)
Check if a string starts with a given token.
Definition String.cpp:365
Common namespace for this Monte Carlo generator.