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