cepgen is hosted by Hepforge, IPPP Durham
CepGen 1.2.5
Central exclusive processes event generator
Loading...
Searching...
No Matches
ProcessIntegrand.cpp
Go to the documentation of this file.
1/*
2 * CepGen: a central exclusive processes event generator
3 * Copyright (C) 2013-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 <numeric>
20
23#include "CepGen/Event/Event.h"
29#include "CepGen/Utils/Math.h"
31
32namespace cepgen {
33 ProcessIntegrand::ProcessIntegrand(const proc::Process& proc) : params_(new RunParameters), tmr_(new utils::Timer) {
34 setProcess(proc);
35 }
36
37 ProcessIntegrand::ProcessIntegrand(const RunParameters* params) : params_(params), tmr_(new utils::Timer) {
38 if (!params_)
39 throw CG_FATAL("ProcessIntegrand") << "Invalid runtime parameters specified.";
40 if (!params_->hasProcess())
41 throw CG_FATAL("ProcessIntegrand") << "No process defined in runtime parameters.";
42 setProcess(params_->process());
43 }
44
45 size_t ProcessIntegrand::size() const { return process().ndim(); }
46
47 void ProcessIntegrand::setProcess(const proc::Process& proc) {
48 //--- each integrand object has its own clone of the process
49 process_ = proc.clone(); // note: kinematics is already set by the process copy constructor
50
51 CG_DEBUG("ProcessIntegrand:setProcess")
52 << "New '" << process().name() << "' process cloned from '" << proc.name() << "' process.";
54
55 //--- first-run preparation
56 CG_DEBUG("ProcessIntegrand:setProcess").log([this](auto& dbg) {
57 dbg << "Run started for " << process().name() << " process " << std::hex << (void*)process_.get() << std::dec
58 << ".\n\t";
59 const auto& beams = process().kinematics().incomingBeams();
60 dbg << "Process mode considered: " << beams.mode() << "\n\t"
61 << " positive-z beam: " << beams.positive() << "\n\t"
62 << " negative-z beam: " << beams.negative();
63 if (!beams.structureFunctions().empty())
64 dbg << "\n\t structure functions: " << beams.structureFunctions();
65 process().dumpVariables(&dbg.stream());
66 });
68
69 CG_DEBUG("ProcessIntegrand:setProcess")
70 << "Process integrand defined for dimension-" << size() << " process '" << process().name() << "'.";
71 }
72
74 if (!process_)
75 throw CG_FATAL("ProcessIntegrand:process") << "Process was not properly cloned!";
76 return *process_;
77 }
78
80 if (!process_)
81 throw CG_FATAL("ProcessIntegrand:process") << "Process was not properly cloned!";
82 return *process_;
83 }
84
85 double ProcessIntegrand::eval(const std::vector<double>& x) {
86 CG_TICKER(const_cast<RunParameters*>(params_)->timeKeeper());
87
88 //--- start the timer
89 tmr_->reset();
91
92 //--- specify the phase space point to probe and calculate weight
93 auto weight = process().weight(x);
94
95 //--- invalidate any unphysical behaviour
96 if (!utils::positive(weight))
97 return 0.;
98
99 //--- speed up the integration process if no event is to be generated
100 if (!process_->hasEvent())
101 return weight;
102
103 process_->setKinematics(); // fill in the process' Event object
104 auto* event = process_->eventPtr(); // prepare the event content
105
106 // once kinematics variables computed, can apply taming functions
107 for (const auto& tam : params_->tamingFunctions())
108 if (const auto val = (*tam)(bws_.get(*event, tam->variables().at(0))) != 0.)
109 weight *= val;
110 else
111 return 0.;
112
113 if (storage_)
114 event->metadata["time:generation"] = tmr_->elapsed(); // pure CepGen part of the event generation
115
116 { // trigger all event modification algorithms
117 double br = -1.;
118 const auto fast_mode = !storage_;
119 for (auto& mod : params_->eventModifiersSequence()) {
120 if (!mod->run(*event, br, fast_mode) || br == 0.)
121 return 0.;
122 weight *= br; // branching fraction for all decays
123 }
124 }
125 { // apply cuts on final state system (after event modification algorithms)
126 const auto& kin = process_->kinematics();
127 // (polish your cuts, as this might be very time-consuming...)
128 if (!kin.cuts().central.contain((*event)(Particle::Role::CentralSystem)))
129 return 0.;
130 if (!kin.cuts().central_particles.empty())
131 for (const auto& part : (*event)(Particle::Role::CentralSystem)) {
132 // retrieve all cuts associated to this final state particle in the central system
133 if (kin.cuts().central_particles.count(part.pdgId()) > 0 &&
134 !kin.cuts().central_particles.at(part.pdgId()).contain({part}))
135 return 0.;
136 }
137 if (!kin.incomingBeams().positive().elastic() &&
138 !kin.cuts().remnants.contain((*event)(Particle::Role::OutgoingBeam1), event))
139 return 0.;
140 if (!kin.incomingBeams().negative().elastic() &&
141 !kin.cuts().remnants.contain((*event)(Particle::Role::OutgoingBeam2), event))
142 return 0.;
143
144 if (storage_) {
145 // add generation metadata to the event
146 event->metadata["weight"] = weight;
147 event->metadata["time:total"] = tmr_->elapsed();
148 }
149
150 CG_DEBUG_LOOP("ProcessIntegrand") << "[process " << std::hex << (void*)process_.get() << std::dec << "]\n\t"
151 << "Generation time: " << event->metadata("time:generation") * 1.e3 << " ms\n\t"
152 << "Total time (gen+hadr+cuts): " << event->metadata("time:total") * 1.e3
153 << " ms";
154
155 // a bit of debugging information
156 CG_DEBUG_LOOP("ProcessIntegrand") << "f value for dim-" << x.size() << " point " << x << ": " << weight << ".";
157 }
158 return weight;
159 }
160} // namespace cepgen
#define CG_FATAL(mod)
Definition Exception.h:61
#define CG_DEBUG_LOOP(mod)
Definition Message.h:224
#define CG_DEBUG(mod)
Definition Message.h:220
#define CG_TICKER(tmr)
Definition TimeKeeper.h:30
mode::Kinematics mode() const
Type of kinematics to consider for the phase space.
void setParameters(const ParametersList &) override
Set module parameters.
IncomingBeams & incomingBeams()
Beam/primary particle kinematics.
Definition Kinematics.h:36
const ParametersList & parameters() const override
List containing all parameters handled.
const std::string & name() const
Module unique indexing name.
Definition NamedModule.h:42
@ OutgoingBeam1
outgoing beam state/particle
Definition Particle.h:54
@ OutgoingBeam2
outgoing beam state/particle
Definition Particle.h:55
@ CentralSystem
Central particles system.
Definition Particle.h:56
proc::Process & process()
Thread-local physics process.
ProcessIntegrand(const proc::Process &)
double eval(const std::vector< double > &x) override
Compute the integrand for a given phase space point (or "event")
size_t size() const override
Phase space dimension.
List of parameters used to start and run the simulation job.
proc::Process & process()
Process object for cross-section computation/events generation.
const TamingFunctionsSequence & tamingFunctions() const
List of all taming functions definitions.
bool hasProcess() const
Is this parameters collection holding any physics process?
EventModifiersSequence & eventModifiersSequence()
List of event modification algos List of event modification algos.
Class template to define any process to compute using this MC integrator/events generator.
Definition Process.h:34
void clearEvent()
Restore the event object to its initial state.
Definition Process.cpp:262
const Kinematics & kinematics() const
Constant reference to the process kinematics.
Definition Process.h:50
void initialise()
Initialise the process once the kinematics has been set.
Definition Process.cpp:285
virtual std::unique_ptr< Process > clone() const
Copy all process attributes into a new object.
Definition Process.cpp:85
void dumpVariables(std::ostream *=nullptr) const
List all variables handled by this generic process.
Definition Process.cpp:137
double weight(const std::vector< double > &)
Compute the weight for a phase-space point.
Definition Process.cpp:234
size_t ndim() const
Number of dimensions on which the integration is performed.
Definition Process.h:60
double get(const Event &ev, const std::string &var) const
Get/compute a variable value.
bool positive(const T &val)
Check if a number is positive and finite.
Definition Math.cpp:26
Common namespace for this Monte Carlo generator.