MARLEY (Model of Argon Reaction Low Energy Yields) v2.0.0
A Monte Carlo event generator for tens-of-MeV neutrino interactions
Loading...
Searching...
No Matches
OutputFile.hh
1
4//
5// This file is part of MARLEY (Model of Argon Reaction Low Energy Yields)
6//
7// MARLEY is free software: you can redistribute it and/or modify it under the
8// terms of version 3 of the GNU General Public License as published by the
9// Free Software Foundation.
10//
11// For the full text of the license please see COPYING or
12// visit http://opensource.org/licenses/GPL-3.0
13//
14// Please respect the MCnet academic usage guidelines. See GUIDELINES
15// or visit https://www.montecarlonet.org/GUIDELINES for details.
16
17#pragma once
18
19// standard library includes
20#include <fstream>
21#include <memory>
22#include <string>
23
24namespace HepMC3 {
25 class GenRunInfo;
26}
27
28namespace marley {
29
30 class Generator;
31 class JSON;
32
35 class OutputFile {
36
37 protected:
38
39 // The input JSON object typically appears under the "output" key in the
40 // "generate" section of a MARLEY job configuration file
41 OutputFile( const JSON& output_config );
42
43 public:
44
45 // Factory method that constructs an appropriate derived object given the
46 // input JSON configuration
47 static std::shared_ptr< OutputFile > make_OutputFile(
48 const JSON& output_config );
49
50 virtual ~OutputFile() = default;
51
52 const std::string& name() const { return name_; }
53
62 virtual bool resume( std::unique_ptr<marley::Generator>& gen,
63 long& num_previous_events ) = 0;
64
67 virtual int_fast64_t bytes_written() = 0;
68
72 virtual void write_event( HepMC3::GenEvent* event ) = 0;
73
74 bool mode_is_resume() const { return mode_ == Mode::RESUME; }
75
76 // Add more formats to the enum class as needed. This should include
77 // every event format that MARLEY knows how to write. The "ASCII" format
78 // is MARLEY's native format for textual input and output of
79 // HepMC3::GenEvent objects.
80 enum class Format { ASCII, ROOT };
81
82 protected:
83
85 inline bool check_if_file_exists( const std::string& filename ) {
86 std::ifstream test_stream( filename );
87 return test_stream.good();
88 }
89
95
98 std::unique_ptr< marley::Generator > restore_generator(
99 const marley::JSON& config );
100
107 HepMC3::GenRunInfo& run_info, marley::Generator& gen );
108
109 // Modes to use when writing output to files that are not initially empty
110 // OVERWRITE = removes any previous contents of the file, then writes new
111 // events in the requested format.
112 // RESUME = uses saved metadata in a file to restore a previous MARLEY
113 // configuration (including the random number generator state), then
114 // appends new events after those currently saved in the file.
115 enum class Mode { OVERWRITE, RESUME };
116
117 std::string name_;
118 Format format_;
119 Mode mode_;
120
121 // Whether to force the current output mode without prompting the user.
122 // Currently only used when setting the OVERWRITE output mode.
123 bool force_;
124 };
125
126}
Stores event-related information.
Definition GenEvent.h:47
Stores run-related information.
Definition GenRunInfo.h:33
The MARLEY Event generator.
Definition Generator.hh:54
virtual int_fast64_t bytes_written()=0
The number of bytes that have been written during the current MARLEY session to this file.
virtual void write_event(HepMC3::GenEvent *event)=0
Write a new HepMC3::GenEvent to this output file.
Format format_
Format to use when writing events to the file.
std::unique_ptr< marley::Generator > restore_generator(const marley::JSON &config)
Helper function for resume() that instantiates a marley::Generator object given the previous JSON con...
Definition OutputFile.cc:55
void prompt_before_overwrite()
If mode_ is OVERWRITE and the output file already exists and force_ is false, prompt the user before ...
Definition OutputFile.cc:63
static void merge_reweight_provenance_weights(HepMC3::GenRunInfo &run_info, marley::Generator &gen)
When resuming from a file that has been through one or more reweight passes, merge the reweight prove...
Definition OutputFile.cc:80
virtual bool resume(std::unique_ptr< marley::Generator > &gen, long &num_previous_events)=0
Load a marley::Generator object whose configuration and state were saved to the metadata in the outpu...
bool check_if_file_exists(const std::string &filename)
Checks that a given file exists (and is readable)
Definition OutputFile.hh:85
std::string name_
Name of the file to receive output.