BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
RawFileMerger.cxx
Go to the documentation of this file.
1#include "RawFile/RawFileMerger.h"
2#include <filesystem>
3#include <memory>
4#include <cstring>
5
6RawFileMerger::RawFileMerger( const std::string& srcfile ) : m_srcfname( srcfile ) {
7 m_srcfile = new RawDataMining( srcfile );
8 m_fidx = new CgemIndexFile();
9 m_cache = new RawDataCache( 1024 * 1024 );
10}
11
13 delete m_cache;
14 delete m_fidx;
15 delete m_srcfile;
16}
17
18bool RawFileMerger::mergeWithIndexFile( const std::string& file ) {
19 if ( !m_fidx->open4read( file ) ) {
20 std::cerr << "Error: failed to open index file " << file << std::endl;
21 return false;
22 }
23
24 if ( !openDestFile( m_fcount ) ) {
25 std::cerr << "Error: failed to open destination file" << std::endl;
26 return false;
27 }
28
29 uint32_t* srcEvt = m_srcfile->nextEvent();
30
31 while ( srcEvt != nullptr ) {
32 // check if the file is too large
33 if ( m_fs.tellp() > 0xFFF00000 /* nearly to 4GB */ ) {
34 m_fs << m_srcfile->fileEndRecord();
35 m_fs.close();
36 if ( !openDestFile( ++m_fcount ) ) return false;
37 }
38
39 // find and read the CGEM data
40 uint32_t l1id = srcEvt[10 + srcEvt[5]] + 1; // +1 to match with the CGEM L1ID
41 uint32_t* cgemData = m_fidx->getCgemData( l1id );
42 if ( cgemData[1] != 0 ) {
43 // merge the CGEM data to the original event
44 uint32_t sizeWords = srcEvt[1] + cgemData[1];
45 uint32_t sizeBytes = sizeWords * sizeof( uint32_t );
46 uint32_t* destEvt = m_cache->reserve<uint32_t>( sizeWords );
47 auto nptr = mempcpy( destEvt, srcEvt, srcEvt[1] * sizeof( uint32_t ) );
48 memcpy( nptr, cgemData, cgemData[1] * sizeof( uint32_t ) );
49
50 destEvt[1] = sizeWords;
51 auto destDSR = m_srcfile->dataSeparatorRecord();
52 destDSR.setDataBlockSize( sizeBytes );
53
54 // write out the merged event
55 m_fs << destDSR;
56 m_fs.write( reinterpret_cast<const char*>( destEvt ), sizeBytes );
57 }
58 else {
59 std::cout << "Warning: CGEM with L1ID " << l1id << " will be ignored!!!" << std::endl;
60 ++m_ignored;
61 }
62
63 srcEvt = m_srcfile->nextEvent();
64 }
65
66 if ( m_srcfile->eof() ) {
67 m_fs << m_srcfile->fileEndRecord();
68 }
69 m_fs.close();
70
71 return true;
72}
73
74bool RawFileMerger::openDestFile( char fcount ) {
75 auto fp = m_srcfname.rfind( "/" );
76 std::string _srcfile = (fp != std::string::npos) ? m_srcfname.substr( fp+1 ) : m_srcfname;
77
78 auto ip = _srcfile.rfind( "_file" );
79 if ( ip == std::string::npos ) {
80 std::cerr << "Error: nonstandard raw file name " << m_srcfname << std::endl;
81 exit( 1 );
82 }
83
84 std::string destfname = m_destdir;
85 if ( !destfname.empty() && destfname.back() != '/' ) {
86 destfname += "/";
87 }
88 destfname += _srcfile.substr( 0, ip );
89 destfname += "_merge";
90 destfname += fcount;
91 destfname += _srcfile.substr( ip );
92
93 std::cout << "RawFileMerger: create new file " << destfname << std::endl;
94 if ( std::filesystem::exists( destfname ) ) {
95 std::cerr << "Error: file " << destfname << " already exists" << std::endl;
96 return false;
97 }
98
99 m_fs.open( destfname, std::ios::out | std::ios::binary );
100 if ( !m_fs.is_open() ) {
101 std::cerr << "Error: cannot open file " << destfname << std::endl;
102 return false;
103 }
104
105 m_fs << m_srcfile->fileStartRecord() << m_srcfile->fileNameStrings() << m_srcfile->runParametersRecord();
106
107 return true;
108}
char * file
Definition DQA_TO_DB.cxx:16
bool mergeWithIndexFile(const std::string &file)
RawFileMerger()=delete