Belle II Software development
PXDLocalDAQFile Class Reference

A class to manage I/O for a chain of blocked files. More...

#include <PXDLocalDAQFile.h>

Public Member Functions

 PXDLocalDAQFile (const std::string &filename)
 Constructor.
 
 ~PXDLocalDAQFile ()
 Destructor.
 
 PXDLocalDAQFile (const PXDLocalDAQFile &)=delete
 No copying.
 
PXDLocalDAQFileoperator= (const PXDLocalDAQFile &)=delete
 No assignment.
 
int status () const
 Returns status after constructor call.
 
int read (char *buf, int max)
 Read a record from a file.
 
int read_data (char *data, size_t len)
 Read a record from a file.
 

Private Member Functions

void openFile (std::string filename)
 actually open the file
 

Private Attributes

std::string m_filename
 Name of the opened file.
 
int m_fd { -1}
 file descriptor.
 
bool m_compressed {false}
 is file bzip2 compressed?
 
std::unique_ptr< std::ios > m_stream
 pointer to the filtering input or output stream
 

Detailed Description

A class to manage I/O for a chain of blocked files.

Definition at line 22 of file PXDLocalDAQFile.h.

Constructor & Destructor Documentation

◆ PXDLocalDAQFile()

PXDLocalDAQFile ( const std::string &  filename)
explicit

Constructor.

Definition at line 25 of file PXDLocalDAQFile.cc.

25 :
26 m_filename(filename)
27{
28 if (filename.empty()) {
29 B2ERROR("PXDLocalDAQFile: Empty filename given");
30 return;
31 }
32 // is the file already compressed?
33 m_compressed = filename.size() > 4 && filename.compare(filename.size() - 4, 4, ".bz2") == 0;
34
35 // open the file
37 if (m_fd < 0 && !m_compressed) {
38 B2WARNING("PXDLocalDAQFile: error opening '" << filename << "': " << strerror(errno)
39 << ", trying again with '.bz2'");
40 m_compressed = true;
42 }
43 // is the file open now?
44 if (m_fd < 0) {
45 B2ERROR("PXDLocalDAQFile: error opening '" << filename << "': " << strerror(errno));
46 } else {
47 B2INFO("PXDLocalDAQFile: " << m_filename << " opened (fd=" << m_fd << ")");
48 }
49
50}
void openFile(std::string filename)
actually open the file
bool m_compressed
is file bzip2 compressed?
std::string m_filename
Name of the opened file.
int m_fd
file descriptor.

◆ ~PXDLocalDAQFile()

Destructor.

Definition at line 65 of file PXDLocalDAQFile.cc.

66{
67 B2INFO("Closing PXDLocalDAQFile ");
68 //closed automatically by m_stream.
69}

Member Function Documentation

◆ openFile()

void openFile ( std::string  filename)
private

actually open the file

Definition at line 52 of file PXDLocalDAQFile.cc.

53{
54
55 //open file in read mode and set stream correctly
56 m_fd = open(filename.c_str(), O_RDONLY);
57 auto* filter = new io::filtering_istream();
58 if (m_compressed) filter->push(io::bzip2_decompressor());
59 filter->push(io::file_descriptor_source(m_fd, io::close_handle));
60 filter->exceptions(ios_base::badbit | ios_base::failbit);
61 m_stream.reset(filter);
62
63}
std::unique_ptr< std::ios > m_stream
pointer to the filtering input or output stream
std::map< ExpRun, std::pair< double, double > > filter(const std::map< ExpRun, std::pair< double, double > > &runs, double cut, std::map< ExpRun, std::pair< double, double > > &runsRemoved)
filter events to remove runs shorter than cut, it stores removed runs in runsRemoved
Definition: Splitter.cc:38

◆ read()

int read ( char *  buf,
int  max 
)

Read a record from a file.

Returns the data size read.

Definition at line 76 of file PXDLocalDAQFile.cc.

77{
78 // cast stream object
79 auto in = dynamic_cast<std::istream*>(m_stream.get());
80 if (!in) {
81 B2FATAL("PXDLocalDAQFile::read() cannot get input file");
82 }
83 //trigger eof if there's nothing left int the file. Could throw an error on decompress failure
84 try {
85 in->peek();
86 } catch (ios_base::failure& e) {
87 B2ERROR("PXDLocalDAQFile::read() cannot read file: " << e.what());
88 return -1;
89 }
90 // return -1 if eof
91 if (in->eof()) {
92 B2DEBUG(29, "PXDLocalDAQFile::read() eof");
93 return -1;
94 }
95
96 // read from file
97 try {
98 in->read(buf, size);
99 } catch (ios_base::failure& e) {
100 B2ERROR("PXDLocalDAQFile::read() " << e.what() << ": could only read " << in->gcount() << " bytes, expected " << size);
101 return -1;
102 }
103
104 return in->gcount();
105}

◆ read_data()

int read_data ( char *  data,
size_t  len 
)

Read a record from a file.

Returns the data size if successfully read, otherwise 0.

Definition at line 107 of file PXDLocalDAQFile.cc.

108{
109 size_t l = read(data, len);
110 if (l != len) return 0;
111 return l;
112}
int read(char *buf, int max)
Read a record from a file.

◆ status()

int status ( ) const

Returns status after constructor call.

If success, fd is returned. If not, -1

Definition at line 71 of file PXDLocalDAQFile.cc.

72{
73 return m_fd;
74}

Member Data Documentation

◆ m_compressed

bool m_compressed {false}
private

is file bzip2 compressed?

Definition at line 48 of file PXDLocalDAQFile.h.

◆ m_fd

int m_fd { -1}
private

file descriptor.

Definition at line 47 of file PXDLocalDAQFile.h.

◆ m_filename

std::string m_filename
private

Name of the opened file.

Definition at line 46 of file PXDLocalDAQFile.h.

◆ m_stream

std::unique_ptr<std::ios> m_stream
private

pointer to the filtering input or output stream

Definition at line 49 of file PXDLocalDAQFile.h.


The documentation for this class was generated from the following files: