-
Notifications
You must be signed in to change notification settings - Fork 494
Expand file tree
/
Copy pathTimeframeWriterDevice.cxx
More file actions
96 lines (83 loc) · 3.06 KB
/
TimeframeWriterDevice.cxx
File metadata and controls
96 lines (83 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright CERN and copyright holders of ALICE O2. This software is
// distributed under the terms of the GNU General Public License v3 (GPL
// Version 3), copied verbatim in the file "COPYING".
//
// See http://alice-o2.web.cern.ch/license for full licensing information.
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
/// @file TimeframeValidatorDevice.cxx
/// @author Giulio Eulisse, Matthias Richter, Sandro Wenzel
/// @since 2017-02-07
/// @brief Validator device for a full time frame
#include <thread> // this_thread::sleep_for
#include <chrono>
#include "DataFlow/TimeframeWriterDevice.h"
#include "DataFlow/TimeframeParser.h"
#include "TimeFrame/TimeFrame.h"
#include "Headers/SubframeMetadata.h"
#include "Headers/DataHeader.h"
#include "O2Device/Compatibility.h"
#include <options/FairMQProgOptions.h>
#include <boost/filesystem.hpp>
using DataHeader = o2::header::DataHeader;
using IndexElement = o2::dataformats::IndexElement;
namespace o2
{
namespace data_flow
{
TimeframeWriterDevice::TimeframeWriterDevice()
: O2Device{}, mInChannelName{}, mFile{}, mMaxTimeframes{}, mMaxFileSize{}, mMaxFiles{}, mFileCount{0}
{
}
void TimeframeWriterDevice::InitTask()
{
mInChannelName = GetConfig()->GetValue<std::string>(OptionKeyInputChannelName);
mOutFileName = GetConfig()->GetValue<std::string>(OptionKeyOutputFileName);
mMaxTimeframes = GetConfig()->GetValue<size_t>(OptionKeyMaxTimeframesPerFile);
mMaxFileSize = GetConfig()->GetValue<size_t>(OptionKeyMaxFileSize);
mMaxFiles = GetConfig()->GetValue<size_t>(OptionKeyMaxFiles);
}
void TimeframeWriterDevice::Run()
{
boost::filesystem::path p(mOutFileName);
size_t streamedTimeframes = 0;
bool needsNewFile = true;
while (compatibility::FairMQ13<FairMQDevice>::IsRunning(this) && mFileCount < mMaxFiles) {
// In case we need to process more than one file,
// the filename is split in basename and extension
// and we call the files `<basename><count>.<extension>`.
if (needsNewFile) {
std::string filename = mOutFileName;
if (mMaxFiles > 1) {
std::string base_path(mOutFileName, 0, mOutFileName.find_last_of("."));
std::string extension(mOutFileName, mOutFileName.find_last_of("."));
filename = base_path + std::to_string(mFileCount) + extension;
}
LOG(INFO) << "Opening " << filename << " for output\n";
mFile.open(filename.c_str(), std::ofstream::out | std::ofstream::binary);
needsNewFile = false;
}
FairMQParts timeframeParts;
if (Receive(timeframeParts, mInChannelName, 0, 100) <= 0) {
continue;
}
streamTimeframe(mFile, timeframeParts);
if ((mFile.tellp() > mMaxFileSize) || (streamedTimeframes++ > mMaxTimeframes)) {
mFile.flush();
mFile.close();
mFileCount++;
needsNewFile = true;
}
}
}
void TimeframeWriterDevice::PostRun()
{
if (mFile.is_open()) {
mFile.flush();
mFile.close();
}
}
} // namespace data_flow
} // namespace o2