BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
raw_ofstream.cxx
Go to the documentation of this file.
1#include "RawFile/raw_ofstream.h"
2#include "RawFile/RawFileTools.h"
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
13raw_ofstream* raw_ofstream::instance( const std::string& fname, int run ) {
14 lock();
15
16 if ( _instance == 0 ) { _instance = new raw_ofstream( fname, run ); }
17
18 ++_nHandler;
19
20 unlock();
21
22 return _instance;
23}
24
26 lock();
27
28 if ( _nHandler > 0 && --_nHandler == 0 )
29 {
30 delete _instance;
31 _instance = 0;
32 }
33
34 unlock();
35}
36
37raw_ofstream::raw_ofstream( const std::string& fname, int run )
38 : m_nevt( 0 ), m_nfile( 0 ), m_fname( fname ) {
39 m_runParametersRecord.setRunNumber( run );
40 init_fstream();
41}
42
43raw_ofstream::~raw_ofstream() { this->close(); }
44
45int raw_ofstream::write_event( const char* pbuf, int size ) {
46 uint32_t fsize = tellp();
47 if ( fsize >= MAX_RAWFILE_SiZE )
48 {
49 this->close();
50 init_fstream();
51 }
52
53 m_dataSeparatorRecord.setDataBlockNumber( ++m_nevt );
54 m_dataSeparatorRecord.setDataBlockSize( size );
55
56 ( *this ) << m_dataSeparatorRecord;
57 std::ofstream::write( pbuf, size );
58
59 return m_nfile;
60}
61
63 if ( is_open() )
64 {
65 m_fileEndRecord.setEventsInFile( m_nevt );
66 m_nevt = 0;
67
68 ( *this ) << m_fileEndRecord;
69 std::ofstream::close();
70
71 std::cout << "[RawFile] Finished writing file: " << real_fname() << std::endl;
72 }
73}
74
75void raw_ofstream::init_fstream() {
76 ++m_nfile;
77
78 std::string fname = real_fname();
79
80 if ( access( fname.c_str(), F_OK ) == 0 )
81 {
82 std::cerr << "[RawFile] Attempt to create an exist file: " << fname << std::endl;
83 exit( 1 );
84 }
85
86 std::cout << "[RawFile] Creating a new file: " << real_fname() << std::endl;
87 open( fname.c_str(), std::ios::binary );
88
89 ( *this ) << m_fileStartRecord << m_fileNameStrings << m_runParametersRecord;
90}
91
92std::string raw_ofstream::real_fname() {
93 std::string fname = m_fname;
94
95 if ( m_nfile > 1 ) { fname += ".part" + RawFileTools::itoa( m_nfile ); }
96
97 return fname;
98}
m_outputFile open("YYYY/m_txt_dir/LumTau_XXXX.txt", ios_base::app)
static void release()
static raw_ofstream * instance(const std::string &fname, int run=0)
int write_event(const char *pbuf, int size)
std::string itoa(int i)
#define MAX_RAWFILE_SiZE