Version: SMASH-3.4
rivetoutput.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (c) 2021 Christian Holm Christensen
4  * Copyright (c) 2021-2022,2024
5  * SMASH Team
6  *
7  * GNU General Public License (GPLv3 or later)
8  *
9  */
10 
11 #ifndef SRC_INCLUDE_SMASH_RIVETOUTPUT_H_
12 #define SRC_INCLUDE_SMASH_RIVETOUTPUT_H_
13 
14 #include <filesystem>
15 #include <memory>
16 #include <string>
17 #include <utility>
18 
19 #include "Rivet/AnalysisHandler.hh"
20 
21 #include "smash/fpenvironment.h"
22 #include "smash/hepmcinterface.h"
23 
24 namespace smash {
25 
26 /**
27  * \ingroup output
28  * \brief SMASH output to Rivet analyses
29  *
30  * This class passes HepMC events to Rivet for analysis. The event is kept
31  * in-memory which significantly speeds up processing (at last a factor 10)
32  * as compared to writing the HepMC event to disk or pipe and then decoding
33  * in Rivet.
34  *
35  * More details of the output format can be found in the User Guide.
36  */
37 class RivetOutput : public HepMcInterface {
38  public:
39  /**
40  * Create Rivet output.
41  *
42  * \param[in] path Output path.
43  * \param[in] name Name of the output.
44  * \param[in] full_event Whether the full event or only final-state particles
45  * are printed in the output
46  * \param[in] rivet_par Rivet parameters from SMASH configuration
47  */
48  RivetOutput(const std::filesystem::path& path, std::string name,
49  const bool full_event, const RivetOutputParameters& rivet_par);
50  /**
51  * Destructor. Finalises the analzyses and writes out results to file
52  */
53  ~RivetOutput();
54  /**
55  * Add the final particles information of an event to the central vertex.
56  * Store impact parameter and write event.
57  *
58  * \param[in] particles Current list of particles.
59  * \param[in] event_label Event/ensemble numbers
60  * \param[in] event Event info, see \ref event_info
61  */
62  void at_eventend(const Particles& particles, const EventLabel& event_label,
63  const EventInfo& event) override;
64  /**
65  * Add an analysis or analyses to Rivet
66  *
67  * \param[in] name Name of analysis
68  */
69  void add_analysis(const std::string& name);
70  /**
71  * Add a load path to the Rivet handler
72  */
73  void add_path(const std::string& path);
74  /**
75  * Add preload to Rivet handler.
76  */
77  void add_preload(const std::string& file);
78  /**
79  * Do not insist on appropriate beams for analyses
80  */
81  void set_ignore_beams(bool ignore = true);
82  /**
83  * Set log level in Rivet
84  */
85  void set_log_level(const std::string& name, const std::string& level);
86  /**
87  * Set X-section
88  *
89  * \param[in] xs Cross-section value in pb
90  * \param[in] xserr Uncertainty on cross-section in pb
91  */
92  void set_cross_section(double xs, double xserr);
93 
94  private:
95  /**
96  * Setup Rivet using SMASH configuration parameters
97  *
98  * \param[in] params The SMASH parameters already extracted from the
99  * input configuration
100  */
101  void setup(const RivetOutputParameters& params);
102 
103  /**
104  * A proxy object that wraps all Rivet::AnalysisHandler calls in an
105  * environment where FP errors are disabled.
106  */
107  struct Proxy {
108  /** Type of handler */
109  using Handler = std::shared_ptr<Rivet::AnalysisHandler>;
110  /** Construct proxy */
111  explicit Proxy(Handler h) : h_(h), g_(0) {}
112  /** Move construct */
113  Proxy(Proxy&& p) : h_(p.h_), g_(std::move(p.g_)) {}
114  /** Forward calls to handler */
115  Handler operator->() { return h_; }
116  /// Handler
118  /// FP traps temporaryly disabled
120  };
121  /** Return a proxy that temporarily disables FP exceptions */
123 
124  /** Rivet analysis handler */
125  std::shared_ptr<Rivet::AnalysisHandler> handler_;
126  /** Output file */
127  std::filesystem::path filename_;
128  /** Whether we need initialisation */
130 };
131 
132 } // namespace smash
133 
134 #endif // SRC_INCLUDE_SMASH_RIVETOUTPUT_H_
Guard type that safely disables floating point traps for the scope in which it is placed.
Definition: fpenvironment.h:79
Base class for output handlers that need the HepMC3 structure.
The Particles class abstracts the storage and manipulation of particles.
Definition: particles.h:33
SMASH output to Rivet analyses.
Definition: rivetoutput.h:37
Proxy analysis_handler_proxy()
Return a proxy that temporarily disables FP exceptions.
Definition: rivetoutput.h:122
void add_path(const std::string &path)
Add a load path to the Rivet handler.
Definition: rivetoutput.cc:135
void add_analysis(const std::string &name)
Add an analysis or analyses to Rivet.
Definition: rivetoutput.cc:131
std::filesystem::path filename_
Output file.
Definition: rivetoutput.h:127
std::shared_ptr< Rivet::AnalysisHandler > handler_
Rivet analysis handler.
Definition: rivetoutput.h:125
bool need_init_
Whether we need initialisation.
Definition: rivetoutput.h:129
~RivetOutput()
Destructor.
Definition: rivetoutput.cc:107
void set_cross_section(double xs, double xserr)
Set X-section.
Definition: rivetoutput.cc:169
void add_preload(const std::string &file)
Add preload to Rivet handler.
Definition: rivetoutput.cc:140
void setup(const RivetOutputParameters &params)
Setup Rivet using SMASH configuration parameters.
Definition: rivetoutput.cc:173
void set_log_level(const std::string &name, const std::string &level)
Set log level in Rivet.
Definition: rivetoutput.cc:149
void at_eventend(const Particles &particles, const EventLabel &event_label, const EventInfo &event) override
Add the final particles information of an event to the central vertex.
Definition: rivetoutput.cc:113
RivetOutput(const std::filesystem::path &path, std::string name, const bool full_event, const RivetOutputParameters &rivet_par)
Create Rivet output.
Definition: rivetoutput.cc:96
void set_ignore_beams(bool ignore=true)
Do not insist on appropriate beams for analyses.
Definition: rivetoutput.cc:144
constexpr int p
Proton.
Definition: action.h:24
Structure to contain custom data for output.
Structure to contain information about the event and ensemble numbers.
Helper structure for OutputParameters in order to store and hand over Rivet parameters.
A proxy object that wraps all Rivet::AnalysisHandler calls in an environment where FP errors are disa...
Definition: rivetoutput.h:107
DisableFloatTraps g_
FP traps temporaryly disabled.
Definition: rivetoutput.h:119
Proxy(Proxy &&p)
Move construct.
Definition: rivetoutput.h:113
std::shared_ptr< Rivet::AnalysisHandler > Handler
Type of handler.
Definition: rivetoutput.h:109
Handler operator->()
Forward calls to handler.
Definition: rivetoutput.h:115
Proxy(Handler h)
Construct proxy.
Definition: rivetoutput.h:111