Project
Loading...
Searching...
No Matches
ReadRawFileSpec.cxx
Go to the documentation of this file.
1// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
11
18
19#include <random>
20#include <iostream>
21#include <fstream>
22#include <stdexcept>
23
28#include "Framework/Lifetime.h"
29#include "Framework/Output.h"
30#include "Framework/Task.h"
32#include "Framework/Logger.h"
33
37
39
40namespace o2
41{
42namespace hmpid
43{
44
45using namespace o2;
46using namespace o2::framework;
48
50{
51 LOG(info) << "Raw file reader init ";
52
53 // read input parameters
54 mPrint = ic.options().get<bool>("print");
55 std::string inFileName = ic.options().get<std::string>("raw-file");
56 mInputFile.open(inFileName, std::ios::binary);
57 if (!mInputFile.is_open()) {
58 throw std::invalid_argument("Cannot open input file \"" + inFileName + "\"");
59 }
60
61 auto stop = [this]() {
62 LOG(info) << "stop file reader"; // close the input file
63 this->mInputFile.close();
64 };
65 ic.services().get<CallbackService>().set<CallbackService::Id::Stop>(stop);
66
67 mExTimer.start();
68 return;
69}
70
72{
73
74 RDH rdh;
75 char* outBuffer{nullptr};
76 size_t bufSize{0};
77 int numberOfFrames = 0;
78 LOG(info) << "Sleep 1 sec for sync";
79 sleep(1);
80
81 while (true) {
82 // usleep(100);
83 mInputFile.read((char*)(&rdh), sizeof(RDH)); // read the next RDH, stop if no more data is available
84 if (mInputFile.fail()) {
85 free(outBuffer);
86 mInputFile.close();
89 break;
90 }
91 auto rdhVersion = o2::raw::RDHUtils::getVersion(rdh);
92 auto rdhHeaderSize = o2::raw::RDHUtils::getHeaderSize(rdh);
93 LOG(debug) << "header_version=" << (int)rdhVersion;
94 if (rdhVersion < 6 || rdhHeaderSize != 64) {
95 LOG(info) << "Old or corrupted raw file, abort !";
96 return;
97 }
98 auto frameSize = o2::raw::RDHUtils::getOffsetToNext(rdh); // get the frame size
99 LOG(debug) << "frameSize=" << frameSize;
100 if (frameSize < rdhHeaderSize) { // stop if the frame size is too small
101 LOG(info) << "Wrong Frame size - frameSize too small: " << frameSize;
103 return;
104 }
105 numberOfFrames++;
106 LOG(debug) << "Process page " << numberOfFrames << " dim = " << frameSize;
107
108 outBuffer = (char*)realloc(outBuffer, bufSize + frameSize); // allocate the buffer
109 if (outBuffer == nullptr) {
110 LOG(info) << "Buffer allocation error. Abort !";
112 return;
113 }
114 memcpy(outBuffer, &rdh, rdhHeaderSize); // fill the buffer
115 mInputFile.read(outBuffer + rdhHeaderSize, frameSize - rdhHeaderSize);
116 if (mInputFile.fail()) { // Could be EoF
117 free(outBuffer);
120 break; // probably reached eof
121 }
122 bufSize = frameSize; // Set the buffer pointer
123 pc.outputs().snapshot(Output{"HMP", "RAWDATA"}, outBuffer, bufSize);
124 // std::cout << mExTimer.mTimer.CpuTime() << " " << mExTimer.mLastLogTime << std::endl;
125 mExTimer.elapseMes("... Reading... Number of Pages = " + std::to_string(numberOfFrames));
126 } // while (true)
127
128 mExTimer.logMes("End of file ! Number of frames processed = " + std::to_string(numberOfFrames));
129 mExTimer.stop();
130 return;
131}
132
133//_________________________________________________________________________________________________
134// clang-format off
136{
137 std::vector<o2::framework::InputSpec> inputs;
138 return DataProcessorSpec{
139 "HMP-ReadRawFile",
140 inputs,
141 Outputs{OutputSpec{"HMP", "RAWDATA", 0, Lifetime::Timeframe}},
142 AlgorithmSpec{adaptFromTask<RawFileReaderTask>()},
143 Options{{"raw-file", VariantType::String, "", {"Raw input file name"}},
144 {"print", VariantType::Bool, false, {"verbose output"}}}};
145}
146// clang-format on
147
148} // end namespace hmpid
149} // end namespace o2
A raw page parser for DPL input.
Definition of the RAW Data Header.
std::ostringstream debug
void snapshot(const Output &spec, T const &object)
ServiceRegistryRef services()
Definition InitContext.h:34
ConfigParamRegistry const & options()
Definition InitContext.h:33
DataAllocator & outputs()
The data allocator is used to allocate memory for the output data.
ServiceRegistryRef services()
The services registry associated with this processing context.
virtual void stop()
This is invoked on stop.
Definition Task.h:53
virtual void endOfStream(EndOfStreamContext &context)
This is invoked whenever we have an EndOfStream event.
Definition Task.h:43
void stop()
stop : stops the timer
Definition Common.h:73
void elapseMes(std::string const message)
Definition Common.h:91
void start()
start : starts the timer
Definition Common.h:64
void logMes(std::string const message)
Definition Common.h:81
void init(framework::InitContext &ic) final
void run(framework::ProcessingContext &pc) final
GLuint GLsizei bufSize
Definition glcorearb.h:790
Defining PrimaryVertex explicitly as messageable.
Definition TFIDInfo.h:20
@ Me
Only quit this data processor.
std::vector< ConfigParamSpec > Options
std::vector< OutputSpec > Outputs
o2::framework::DataProcessorSpec getReadRawFileSpec(std::string inputSpec="HMP/RAWDATA")
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...
std::string to_string(gsl::span< T, Size > span)
Definition common.h:52
static constexpr int getVersion()
get numeric version of the RDH
Definition RDHUtils.h:58
LOG(info)<< "Compressed in "<< sw.CpuTime()<< " s"