Jpp 19.3.0-rc.1
the software that should make you happy
Loading...
Searching...
No Matches
dump_file.hh
Go to the documentation of this file.
1#ifndef DATAQUEUE_FRAMEFACTORY_DUMP_FILE_HH
2#define DATAQUEUE_FRAMEFACTORY_DUMP_FILE_HH
3
4#include <fstream>
5#include <sstream>
6#include <iomanip>
7
8/**
9 * \author cpellegrino
10 */
11
13{
14 std::string m_prefix;
15 std::string m_postfix;
16 std::size_t m_file_no;
17 unsigned int m_detector_id;
18
19 public:
20
21 FilenameGenerator(const std::string& prefix,
22 const std::string& postfix)
23 :
24 m_prefix(prefix),
25 m_postfix(postfix),
26 m_file_no(0)
27 {}
28
29 void generate()
30 {
31 ++m_file_no;
32 }
33
34 std::string name(std::size_t run_number, unsigned int detector_id) const
35 {
36 std::ostringstream oss;
37 oss << m_prefix
38 << "_D"
39 << std::setfill('0') << std::setw(8)
40 << detector_id
41 << "_R"
42 << std::setfill('0') << std::setw(8)
43 << run_number
44 << "_N"
45 << std::setfill('0') << std::setw(8)
46 << m_file_no
47 << m_postfix;
48
49 return oss.str();
50 }
51};
52
53class DumpFile
54{
55 std::ofstream m_outfile;
58 std::size_t const m_max_file_size;
59
60 public:
61
62 DumpFile(const FilenameGenerator& fg, std::size_t max_file_size)
63 :
64 m_fg(fg),
66 m_max_file_size(max_file_size)
67 {}
68
70 {
71 m_outfile.close();
72 }
73
74 bool write(const CLBDataGram* data, unsigned int detector_id)
75 {
76 if (m_outfile.is_open())
77 {
78 if (data->size() + m_current_file_size + sizeof(int) > m_max_file_size)
79 {
80 m_fg.generate();
81 m_outfile.close();
82
83 m_outfile.open(m_fg.name(data->getRunNumber(), detector_id).c_str());
85 }
86 }
87 else
88 {
89 m_outfile.open(m_fg.name(data->getRunNumber(), detector_id).c_str());
91 }
92
93 if (!m_outfile.is_open())
94 {
95 return false;
96 }
97
98 unsigned int const packet_size = data->size();
99 m_outfile.write(
100 static_cast<const char*>(
101 static_cast<const void*>(
102 &packet_size)),
103 sizeof(packet_size));
104
105 if (m_outfile.good())
106 {
107 m_current_file_size += sizeof(packet_size);
108 if (m_outfile.write(data->raw(), data->size()).good())
109 {
110 m_current_file_size += data->size();
111 return true;
112 }
113 else
114 {
115 return false;
116 }
117 }
118 return false;
119 }
120};
121
122#endif // DATAQUEUE_FRAMEFACTORY_DUMP_FILE_HH
bool write(const CLBDataGram *data, unsigned int detector_id)
Definition dump_file.hh:74
std::size_t const m_max_file_size
Definition dump_file.hh:58
DumpFile(const FilenameGenerator &fg, std::size_t max_file_size)
Definition dump_file.hh:62
FilenameGenerator m_fg
Definition dump_file.hh:56
std::size_t m_current_file_size
Definition dump_file.hh:57
std::ofstream m_outfile
Definition dump_file.hh:55
std::size_t m_file_no
Definition dump_file.hh:16
std::string m_prefix
Definition dump_file.hh:14
std::string name(std::size_t run_number, unsigned int detector_id) const
Definition dump_file.hh:34
FilenameGenerator(const std::string &prefix, const std::string &postfix)
Definition dump_file.hh:21
unsigned int m_detector_id
Definition dump_file.hh:17
std::string m_postfix
Definition dump_file.hh:15