Belle II Software development
PXDLocalDAQFile.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9#include <pxd/unpacking/PXDLocalDAQFile.h>
10#include <framework/logging/Logger.h>
11
12#include <ios>
13#include <fcntl.h>
14#include <cerrno>
15
16#include <boost/iostreams/device/file_descriptor.hpp>
17#include <boost/iostreams/filter/bzip2.hpp>
18#include <boost/iostreams/filtering_stream.hpp>
19#include <boost/format.hpp>
20
21using namespace Belle2;
22using namespace std;
23namespace io = boost::iostreams;
24
25PXDLocalDAQFile::PXDLocalDAQFile(const std::string& filename):
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}
51
52void PXDLocalDAQFile::openFile(std::string filename)
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}
64
66{
67 B2INFO("Closing PXDLocalDAQFile ");
68 //closed automatically by m_stream.
69}
70
72{
73 return m_fd;
74}
75
76int PXDLocalDAQFile::read(char* buf, int size)
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}
106
107int PXDLocalDAQFile::read_data(char* data, size_t len)
108{
109 size_t l = read(data, len);
110 if (l != len) return 0;
111 return l;
112}
113
int read_data(char *data, size_t len)
Read a record from a file.
void openFile(std::string filename)
actually open the file
std::unique_ptr< std::ios > m_stream
pointer to the filtering input or output stream
bool m_compressed
is file bzip2 compressed?
int status() const
Returns status after constructor call.
std::string m_filename
Name of the opened file.
int m_fd
file descriptor.
PXDLocalDAQFile(const std::string &filename)
Constructor.
int read(char *buf, int max)
Read a record from a file.
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
Abstract base class for different kinds of events.
STL namespace.