BOSS 6.6.4.p03
BESIII Offline Software System
Loading...
Searching...
No Matches
raw_ofstream.cxx
Go to the documentation of this file.
3#include <cstdlib>
4
5#define MAX_RAWFILE_SiZE 2000000000 //an approximate value, not exactly
6
7//static data members
8int raw_ofstream::_nHandler = 0;
9raw_ofstream* raw_ofstream::_instance = 0;
10pthread_mutex_t raw_ofstream::_pthread_lock = PTHREAD_MUTEX_INITIALIZER;
11
12
13raw_ofstream* raw_ofstream::instance(const std::string& fname)
14{
15 lock();
16
17 if ( _instance == 0 ) {
18 _instance = new raw_ofstream(fname);
19 }
20
21 ++_nHandler;
22
23 unlock();
24
25 return _instance;
26}
27
29{
30 lock();
31
32 if ( _nHandler > 0 && --_nHandler == 0 ) {
33 delete _instance;
34 _instance = 0;
35 }
36
37 unlock();
38}
39
40raw_ofstream::raw_ofstream(const std::string& fname)
41 : m_nevt(0),
42 m_nfile(0),
43 m_fname(fname)
44{
45 init_fstream();
46}
47
48raw_ofstream::~raw_ofstream()
49{
50 this->close();
51}
52
53int raw_ofstream::write_event(const char* pbuf, int size)
54{
55 uint32_t fsize = tellp();
56 if ( fsize >= MAX_RAWFILE_SiZE ) {
57 this->close();
58 init_fstream();
59 }
60
61 m_dataSeparatorRecord.setDataBlockNumber(++m_nevt);
62 m_dataSeparatorRecord.setDataBlockSize(size);
63
64 (*this) << m_dataSeparatorRecord;
65 std::ofstream::write(pbuf, size);
66
67 return m_nfile;
68}
69
71{
72 if ( is_open() ) {
73 m_fileEndRecord.setEventsInFile(m_nevt);
74 m_nevt = 0;
75
76 (*this) << m_fileEndRecord;
77 std::ofstream::close();
78
79 std::cout << "[RawFile] Finished writing file: " << real_fname() << std::endl;
80 }
81}
82
83void raw_ofstream::init_fstream()
84{
85 ++m_nfile;
86
87 std::string fname = real_fname();
88
89 if ( access( fname.c_str(), F_OK ) == 0 ) {
90 std::cerr << "[RawFile] Attempt to create an exist file: " << fname << std::endl;
91 exit(1);
92 }
93
94 std::cout << "[RawFile] Creating a new file: " << real_fname() << std::endl;
95 open( fname.c_str(), std::ios::binary );
96
97 (*this) << m_fileStartRecord << m_fileNameStrings << m_runParametersRecord;
98}
99
100std::string raw_ofstream::real_fname()
101{
102 std::string fname = m_fname;
103
104 if ( m_nfile > 1 ) {
105 fname += ".part" + RawFileTools::itoa( m_nfile );
106 }
107
108 return fname;
109}
m_outputFile open("YYYY/m_txt_dir/LumTau_XXXX.txt", ios_base::app)
void setDataBlockSize(uint32_t ds)
Definition: RawFileUtil.h:136
void setDataBlockNumber(uint32_t dn)
Definition: RawFileUtil.h:135
void setEventsInFile(uint32_t file_nevt)
Definition: RawFileUtil.h:169
static void release()
int write_event(const char *pbuf, int size)
static raw_ofstream * instance(const std::string &fname)
static void lock()
Definition: raw_ofstream.h:18
static void unlock()
Definition: raw_ofstream.h:22
std::string itoa(int i)
#define MAX_RAWFILE_SiZE
Definition: raw_ofstream.cxx:5