Version: SMASH-3.4
rootoutput.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright (c) 2014-2020,2022-2026
4  * SMASH Team
5  *
6  * GNU General Public License (GPLv3 or later)
7  *
8  */
9 
10 #ifndef SRC_INCLUDE_SMASH_ROOTOUTPUT_H_
11 #define SRC_INCLUDE_SMASH_ROOTOUTPUT_H_
12 
13 #include <filesystem>
14 #include <memory>
15 #include <string>
16 #include <vector>
17 
18 #include "TFile.h"
19 #include "TTree.h"
20 #include "forwarddeclarations.h"
21 #include "outputinterface.h"
22 #include "outputparameters.h"
23 
24 namespace smash {
25 class Particles;
26 
27 /**
28  * \ingroup output
29  *
30  * \brief <h2> SMASH output to ROOT file </h2>
31  *
32  * SMASH supports ROOT output as an option (see http://root.cern.ch).
33  * The ROOT framework needs to be installed before building SMASH, otherwise
34  * ROOT support will be disabled.
35  *
36  * This class produces a file Particles.root, which contains a ROOT TTree. The
37  * TTree contains information about particles from all SMASH events comprising a
38  * simulation. If Collisions output is requested, this class also produces a
39  * file Collisions.root, which contains a ROOT TTRee with information about all
40  * collisions.
41  *
42  * See \ref doxypage_output_root for more informatioon.
43  *
44  */
45 class RootOutput : public OutputInterface {
46  public:
47  /**
48  * Construct ROOT output.
49  *
50  * \param[in] path Output path.
51  * \param[in] name Name of the ouput.
52  * \param[in] out_par A structure containing parameters of the output.
53  */
54  RootOutput(const std::filesystem::path &path, const std::string &name,
55  const OutputParameters &out_par);
56 
57  /// Destructor
58  ~RootOutput();
59 
60  /**
61  * update event number and writes intermediate particles to a tree.
62  * \param[in] particles Particles to be written to output.
63  * \param[in] event_label Numbers of event and ensemble.
64  * \param[in] event Event info, see \ref event_info
65  */
66  void at_eventstart(const Particles &particles, const EventLabel &event_label,
67  const EventInfo &event) override;
68  /**
69  * update event number and impact parameter,
70  * and writes intermediate particles to a tree.
71  * \param[in] particles Particles to be written to output.
72  * \param[in] event_label Numbers of event and ensemble.
73  * \param[in] event Event info, see \ref event_info
74  */
75  void at_eventend(const Particles &particles, const EventLabel &event_label,
76  const EventInfo &event) override;
77  /**
78  * Writes intermediate particles to a tree defined by treename,
79  * if it is allowed (i.e., particles_only_final_ is No).
80  * \param[in] particles Particles to be written to output.
81  * \param[in] clock Unused, needed since inherited.
82  * \param[in] dens_param Unused, needed since inherited.
83  * \param[in] event_label Numbers of event and ensemble.
84  * \param[in] event Event info, see \ref event_info
85  */
86  void at_intermediate_time(const Particles &particles,
87  const std::unique_ptr<Clock> &clock,
88  const DensityParameters &dens_param,
89  const EventLabel &event_label,
90  const EventInfo &event) override;
91  /**
92  * Writes collisions to a tree defined by treename.
93  * \param[in] action an Action object containing incoming, outgoing particles
94  * and type of interactions.
95  * \param[in] density Unused, needed since inherited.
96  */
97  void at_interaction(const Action &action, const double density) override;
98 
99  private:
100  /// Filename of output
101  const std::filesystem::path filename_;
102  /// Filename of output as long as simulation is still running.
103  std::filesystem::path filename_unfinished_;
104  /// Pointer to root output file.
105  std::unique_ptr<TFile> root_out_file_;
106  /**
107  * TTree for particles output.
108  *
109  * TFile takes ownership of all TTrees.
110  * That's why TTree is not a unique pointer.
111  */
113  /**
114  * TTree for collision output.
115  *
116  * TFile takes ownership of all TTrees.
117  * That's why TTree is not a unique pointer.
118  */
120  /**
121  * Writes particles to a tree defined by treename.
122  * \param[in] particles Particles or ParticleList to be written to output.
123  */
124  template <typename T>
125  void particles_to_tree(T &particles);
126  /**
127  * Writes collisions to a tree defined by treename.
128  * \param[in] incoming Incoming particles to be written to output.
129  * \param[in] outgoing Outgoing particles to be written to output.
130  * \param[in] weight Total weight of the collision.
131  * \param[in] partial_weight Partial weight of the collision
132  */
133  void collisions_to_tree(const ParticleList &incoming,
134  const ParticleList &outgoing, const double weight,
135  const double partial_weight);
136  /// Number of output in a given event.
138  /// Number of current event.
139  int current_event_ = 0;
140  /// Number of current ensemble.
142 
143  /**
144  * Maximal buffer size.
145  * When the number of particles N exceeds the buffer size B, data is flushed
146  * to the ROOT file every B particles. This creates ceil(N/B) entries in the
147  * ROOT Tree at every output.
148  */
149  static const int max_buffer_size_;
150 
151  /** @name Buffer for filling TTree
152  * See class documentation for definitions.
153  */
154  //@{
155  /// Property that is written to ROOT output.
156  int ev_{};
157  int ens_{};
158  int tcounter_{};
159  int npart_{};
160  int test_p_{};
161  double modus_l_{};
162  double current_t_{};
163  double impact_b_{};
164  bool empty_event_{};
165  std::vector<int> id_ = std::vector<int>(max_buffer_size_, 0);
166  std::vector<int> pdgcode_ = std::vector<int>(max_buffer_size_, 0);
167  std::vector<int> charge_ = std::vector<int>(max_buffer_size_, 0);
168  std::vector<double> formation_time_ =
169  std::vector<double>(max_buffer_size_, 0.0);
170  std::vector<double> time_last_collision_ =
171  std::vector<double>(max_buffer_size_, 0.0);
172  std::vector<double> p0_ = std::vector<double>(max_buffer_size_, 0.0);
173  std::vector<double> px_ = std::vector<double>(max_buffer_size_, 0.0);
174  std::vector<double> py_ = std::vector<double>(max_buffer_size_, 0.0);
175  std::vector<double> pz_ = std::vector<double>(max_buffer_size_, 0.0);
176  std::vector<double> t_ = std::vector<double>(max_buffer_size_, 0.0);
177  std::vector<double> x_ = std::vector<double>(max_buffer_size_, 0.0);
178  std::vector<double> y_ = std::vector<double>(max_buffer_size_, 0.0);
179  std::vector<double> z_ = std::vector<double>(max_buffer_size_, 0.0);
180  double E_kinetic_tot_{};
181  double E_fields_tot_{};
182  double E_tot_{};
183  std::vector<int> coll_per_part_ = std::vector<int>(max_buffer_size_, 0);
184  std::vector<double> xsec_factor_ = std::vector<double>(max_buffer_size_, 0.0);
185  std::vector<int> proc_id_origin_ = std::vector<int>(max_buffer_size_, 0);
186  std::vector<int> proc_type_origin_ = std::vector<int>(max_buffer_size_, 0);
187  std::vector<int> pdg_mother1_ = std::vector<int>(max_buffer_size_, 0);
188  std::vector<int> pdg_mother2_ = std::vector<int>(max_buffer_size_, 0);
189  std::vector<int> baryon_number_ = std::vector<int>(max_buffer_size_, 0);
190  std::vector<int> strangeness_ = std::vector<int>(max_buffer_size_, 0);
191  int nin_{};
192  int nout_{};
193  double wgt_{};
194  double par_wgt_{};
195  //@}
196 
197  /// Option to write collisions tree.
199 
200  /// Option to write particles tree.
202 
203  /// Option to write particles tree for initial conditions
205 
206  /// Print only final particles in the event (no intermediate output).
208 
209  /**
210  * ROOT file cannot be read if it was not properly closed and finalized.
211  * It can happen that SMASH simulation crashed and ROOT file was not closed.
212  * To save results of simulation in such case, "AutoSave" is applied every N
213  * events. If multiple ensembles are used, N is divided by the number of
214  * ensembles per event. This makes sense especially in case of a large number
215  * of ensembles and, at the same time, it ensures that all ensembles belonging
216  * to the same event are saved. The autosave_frequency_ sets this N (at the
217  * moment N=1000 is hard-coded). Note that "AutoSave" operation is very
218  * time-consuming, so the autosave frequency is always a compromise between
219  * safety and speed.
220  */
222 
223  /// Whether extended particle output is on
224  const bool part_extended_;
225  /// Whether extended collisions output is on
226  const bool coll_extended_;
227  /// Whether extended ic output is on
228  const bool ic_extended_;
229 
230  /**
231  * Basic initialization routine, creating the TTree objects
232  * for particles and collisions.
233  */
234  void init_trees();
235 };
236 
237 } // namespace smash
238 
239 #endif // SRC_INCLUDE_SMASH_ROOTOUTPUT_H_
Action is the base class for a generic process that takes a number of incoming particles and transfor...
Definition: action.h:35
A class to pre-calculate and store parameters relevant for density calculation.
Definition: density.h:92
Abstraction of generic output.
The Particles class abstracts the storage and manipulation of particles.
Definition: particles.h:33
void at_interaction(const Action &action, const double density) override
Writes collisions to a tree defined by treename.
Definition: rootoutput.cc:485
std::vector< double > t_
Property that is written to ROOT output.
Definition: rootoutput.h:176
std::vector< double > y_
Property that is written to ROOT output.
Definition: rootoutput.h:178
void at_intermediate_time(const Particles &particles, const std::unique_ptr< Clock > &clock, const DensityParameters &dens_param, const EventLabel &event_label, const EventInfo &event) override
Writes intermediate particles to a tree defined by treename, if it is allowed (i.e....
Definition: rootoutput.cc:424
double current_t_
Property that is written to ROOT output.
Definition: rootoutput.h:162
OutputOnlyFinal particles_only_final_
Print only final particles in the event (no intermediate output).
Definition: rootoutput.h:207
int current_event_
Number of current event.
Definition: rootoutput.h:139
TTree * collisions_tree_
TTree for collision output.
Definition: rootoutput.h:119
~RootOutput()
Destructor.
Definition: rootoutput.cc:393
double E_kinetic_tot_
Property that is written to ROOT output.
Definition: rootoutput.h:180
int test_p_
Property that is written to ROOT output.
Definition: rootoutput.h:160
void collisions_to_tree(const ParticleList &incoming, const ParticleList &outgoing, const double weight, const double partial_weight)
Writes collisions to a tree defined by treename.
Definition: rootoutput.cc:565
void at_eventstart(const Particles &particles, const EventLabel &event_label, const EventInfo &event) override
update event number and writes intermediate particles to a tree.
Definition: rootoutput.cc:400
int autosave_frequency_
ROOT file cannot be read if it was not properly closed and finalized.
Definition: rootoutput.h:221
std::vector< int > proc_type_origin_
Property that is written to ROOT output.
Definition: rootoutput.h:186
double modus_l_
Property that is written to ROOT output.
Definition: rootoutput.h:161
std::vector< int > coll_per_part_
Property that is written to ROOT output.
Definition: rootoutput.h:183
int tcounter_
Property that is written to ROOT output.
Definition: rootoutput.h:158
std::vector< int > baryon_number_
Property that is written to ROOT output.
Definition: rootoutput.h:189
int npart_
Property that is written to ROOT output.
Definition: rootoutput.h:159
std::vector< int > pdg_mother1_
Property that is written to ROOT output.
Definition: rootoutput.h:187
int nin_
Property that is written to ROOT output.
Definition: rootoutput.h:191
int ev_
Property that is written to ROOT output.
Definition: rootoutput.h:156
bool write_collisions_
Option to write collisions tree.
Definition: rootoutput.h:198
std::vector< int > charge_
Property that is written to ROOT output.
Definition: rootoutput.h:167
std::vector< double > px_
Property that is written to ROOT output.
Definition: rootoutput.h:173
const std::filesystem::path filename_
Filename of output.
Definition: rootoutput.h:101
std::vector< int > strangeness_
Property that is written to ROOT output.
Definition: rootoutput.h:190
bool write_initial_conditions_
Option to write particles tree for initial conditions.
Definition: rootoutput.h:204
double wgt_
Property that is written to ROOT output.
Definition: rootoutput.h:193
std::vector< double > py_
Property that is written to ROOT output.
Definition: rootoutput.h:174
std::vector< int > proc_id_origin_
Property that is written to ROOT output.
Definition: rootoutput.h:185
void particles_to_tree(T &particles)
Writes particles to a tree defined by treename.
Definition: rootoutput.cc:500
const bool part_extended_
Whether extended particle output is on.
Definition: rootoutput.h:224
double impact_b_
Property that is written to ROOT output.
Definition: rootoutput.h:163
RootOutput(const std::filesystem::path &path, const std::string &name, const OutputParameters &out_par)
Construct ROOT output.
Definition: rootoutput.cc:266
std::vector< double > z_
Property that is written to ROOT output.
Definition: rootoutput.h:179
std::vector< int > id_
Property that is written to ROOT output.
Definition: rootoutput.h:165
std::vector< double > formation_time_
Property that is written to ROOT output.
Definition: rootoutput.h:168
int current_ensemble_
Number of current ensemble.
Definition: rootoutput.h:141
void at_eventend(const Particles &particles, const EventLabel &event_label, const EventInfo &event) override
update event number and impact parameter, and writes intermediate particles to a tree.
Definition: rootoutput.cc:443
std::vector< double > x_
Property that is written to ROOT output.
Definition: rootoutput.h:177
std::vector< double > p0_
Property that is written to ROOT output.
Definition: rootoutput.h:172
bool empty_event_
Property that is written to ROOT output.
Definition: rootoutput.h:164
static const int max_buffer_size_
Maximal buffer size.
Definition: rootoutput.h:149
std::unique_ptr< TFile > root_out_file_
Pointer to root output file.
Definition: rootoutput.h:105
std::vector< double > xsec_factor_
Property that is written to ROOT output.
Definition: rootoutput.h:184
double E_fields_tot_
Property that is written to ROOT output.
Definition: rootoutput.h:181
std::filesystem::path filename_unfinished_
Filename of output as long as simulation is still running.
Definition: rootoutput.h:103
const bool coll_extended_
Whether extended collisions output is on.
Definition: rootoutput.h:226
std::vector< double > time_last_collision_
Property that is written to ROOT output.
Definition: rootoutput.h:170
double par_wgt_
Property that is written to ROOT output.
Definition: rootoutput.h:194
void init_trees()
Basic initialization routine, creating the TTree objects for particles and collisions.
Definition: rootoutput.cc:285
int output_counter_
Number of output in a given event.
Definition: rootoutput.h:137
double E_tot_
Property that is written to ROOT output.
Definition: rootoutput.h:182
TTree * particles_tree_
TTree for particles output.
Definition: rootoutput.h:112
int ens_
Property that is written to ROOT output.
Definition: rootoutput.h:157
std::vector< double > pz_
Property that is written to ROOT output.
Definition: rootoutput.h:175
int nout_
Property that is written to ROOT output.
Definition: rootoutput.h:192
const bool ic_extended_
Whether extended ic output is on.
Definition: rootoutput.h:228
bool write_particles_
Option to write particles tree.
Definition: rootoutput.h:201
std::vector< int > pdgcode_
Property that is written to ROOT output.
Definition: rootoutput.h:166
std::vector< int > pdg_mother2_
Property that is written to ROOT output.
Definition: rootoutput.h:188
OutputOnlyFinal
Whether and when only final state particles should be printed.
Definition: action.h:24
Structure to contain custom data for output.
Structure to contain information about the event and ensemble numbers.
Helper structure for Experiment to hold output options and parameters.