Project
Loading...
Searching...
No Matches
DigitIOV3.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
12#include "DigitIOV3.h"
15#include "DigitFileFormat.h"
16#include "DigitSampler.h"
17#include "IO.h"
18#include "IOStruct.h"
19#include <iostream>
20
21namespace o2::mch::io::impl
22{
23
24void DigitSamplerV3::count(std::istream& in, size_t& ntfs, size_t& nrofs, size_t& ndigits)
25{
26 rewind(in);
27 ndigits = 0;
28 nrofs = 0;
29 ntfs = 0;
30 std::pair<int, int> pairs;
31 std::pair<int, int> invalid{-1, -1};
32
33 while ((pairs = advanceOneEvent(in, 3)) != invalid) {
34 ndigits += pairs.second;
35 nrofs += pairs.first;
36 ++ntfs;
37 }
38 rewind(in);
39}
40
41bool DigitSamplerV3::read(std::istream& in,
42 std::vector<Digit>& digits,
43 std::vector<ROFRecord>& rofs)
44{
45 if (in.peek() == EOF) {
46 return false;
47 }
48 // note the input vectors are not cleared as this is the responsability
49 // of the calling class, if need be.
50
51 int nrofs = readNofItems(in, "rofs");
52 if (in.fail()) {
53 return false;
54 }
55
56 for (auto i = 0; i < nrofs; i++) {
57 uint16_t bc;
58 uint32_t orbit;
59 uint32_t firstIdx;
60 uint32_t nentries;
61 in.read(reinterpret_cast<char*>(&bc), sizeof(uint16_t));
62 in.read(reinterpret_cast<char*>(&orbit), sizeof(uint32_t));
63 in.read(reinterpret_cast<char*>(&firstIdx), sizeof(uint32_t));
64 in.read(reinterpret_cast<char*>(&nentries), sizeof(uint32_t));
65 rofs.emplace_back(o2::InteractionRecord{bc, orbit}, firstIdx, nentries);
66 if (in.fail()) {
67 return false;
68 }
69 }
70
71 int ndigits = readNofItems(in, "digits");
72 if (in.fail()) {
73 return false;
74 }
75
76 for (int i = 0; i < ndigits; i++) {
77 uint32_t tfTime;
78 uint16_t nofSamples;
79 uint32_t deID;
80 uint32_t padID;
81 uint32_t adc;
82 uint8_t sat;
83 in.read(reinterpret_cast<char*>(&tfTime), sizeof(uint32_t));
84 in.read(reinterpret_cast<char*>(&nofSamples), sizeof(uint16_t));
85 in.read(reinterpret_cast<char*>(&sat), sizeof(uint8_t));
86 in.read(reinterpret_cast<char*>(&deID), sizeof(uint32_t));
87 in.read(reinterpret_cast<char*>(&padID), sizeof(uint32_t));
88 in.read(reinterpret_cast<char*>(&adc), sizeof(uint32_t));
89 digits.emplace_back(deID, padID, adc, tfTime, nofSamples, sat > 0);
90 if (in.fail()) {
91 return false;
92 }
93 }
94 return true;
95}
96
97void DigitSamplerV3::rewind(std::istream& in)
98{
100}
101
102bool DigitSinkV3::write(std::ostream& out,
103 gsl::span<const Digit> digits,
104 gsl::span<const ROFRecord> rofs)
105{
106 if (rofs.empty()) {
107 return false;
108 }
109 writeNofItems(out, rofs.size());
110 if (out.fail()) {
111 return false;
112 }
113 for (auto r : rofs) {
114 uint16_t bc = r.getBCData().bc;
115 uint32_t orbit = r.getBCData().orbit;
116 uint32_t firstIdx = r.getFirstIdx();
117 uint32_t nentries = r.getNEntries();
118 out.write(reinterpret_cast<const char*>(&bc), sizeof(uint16_t));
119 out.write(reinterpret_cast<const char*>(&orbit), sizeof(uint32_t));
120 out.write(reinterpret_cast<const char*>(&firstIdx), sizeof(uint32_t));
121 out.write(reinterpret_cast<const char*>(&nentries), sizeof(uint32_t));
122 if (out.fail()) {
123 return false;
124 }
125 }
126
127 writeNofItems(out, digits.size());
128 if (out.fail()) {
129 return false;
130 }
131 for (const auto& d : digits) {
132 uint32_t tfTime = d.getTime();
133 uint16_t nofSamples = d.getNofSamples();
134 uint32_t deID = d.getDetID();
135 uint32_t padID = d.getPadID();
136 uint32_t adc = d.getADC();
137 uint8_t sat = d.isSaturated();
138 out.write(reinterpret_cast<const char*>(&tfTime), sizeof(uint32_t));
139 out.write(reinterpret_cast<const char*>(&nofSamples), sizeof(uint16_t));
140 out.write(reinterpret_cast<const char*>(&sat), sizeof(uint8_t));
141 out.write(reinterpret_cast<const char*>(&deID), sizeof(uint32_t));
142 out.write(reinterpret_cast<const char*>(&padID), sizeof(uint32_t));
143 out.write(reinterpret_cast<const char*>(&adc), sizeof(uint32_t));
144 if (out.fail()) {
145 return false;
146 }
147 }
148 return true;
149}
150
151} // namespace o2::mch::io::impl
uint64_t orbit
Definition RawEventData.h:6
uint64_t bc
Definition RawEventData.h:5
int32_t i
Definition of the MCH ROFrame record.
bool read(std::istream &in, std::vector< Digit > &digits, std::vector< ROFRecord > &rofs) override
Definition DigitIOV3.cxx:41
void count(std::istream &in, size_t &ntfs, size_t &nrofs, size_t &ndigits) override
Definition DigitIOV3.cxx:24
void rewind(std::istream &in)
Definition DigitIOV3.cxx:97
GLboolean r
Definition glcorearb.h:1233
std::pair< int, int > advanceOneEvent(std::istream &in, int fileFormatVersion)
Definition IO.cxx:48
void writeNofItems(std::ostream &out, uint32_t nofItems)
Definition IO.cxx:30
int readNofItems(std::istream &in, const char *itemName)
Definition IO.cxx:20
bool write(std::ostream &out, gsl::span< const Digit > digits, gsl::span< const ROFRecord > rofs) override
std::vector< Digit > digits
ArrayADC adc