New class for raw data processing (T.Kuhr)
[u/mrichter/AliRoot.git] / STEER / AliRawReader.cxx
CommitLineData
a6e7b125 1/**************************************************************************
2 * Copyright(c) 1998-1999, ALICE Experiment at CERN, All rights reserved. *
3 * *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
6 * *
7 * Permission to use, copy, modify and distribute this software and its *
8 * documentation strictly for non-commercial purposes is hereby granted *
9 * without fee, provided that the above copyright notice appears in all *
10 * copies and that both the copyright notice and this permission notice *
11 * appear in the supporting documentation. The authors make no claims *
12 * about the suitability of this software for any purpose. It is *
13 * provided "as is" without express or implied warranty. *
14 **************************************************************************/
15
16///////////////////////////////////////////////////////////////////////////////
17//
18// This is the base class for reading a raw data file and providing
19// information about digits
20//
21///////////////////////////////////////////////////////////////////////////////
22
23#include "AliRawReader.h"
24
25ClassImp(AliRawReader)
26
27
28AliRawReader::AliRawReader(const char* fileName, Bool_t addNumber)
29{
30// create an object to read digits from the given input file(s)
31// if addNumber is true, a number starting at 1 is appended to the file name
32
33 fFileName = fileName;
34 if (!addNumber) {
35 fFileNumber = -1;
36 fStream = new fstream(fileName, ios::binary|ios::in);
37 } else {
38 fFileNumber = 0;
39 fStream = NULL;
40 OpenNextFile();
41 }
42 fCount = 0;
43}
44
45AliRawReader::~AliRawReader()
46{
47// close the input file
48
49 if (fStream) {
50 if (fStream->is_open()) fStream->close();
51 delete fStream;
52 }
53}
54
55
56Bool_t AliRawReader::OpenNextFile()
57{
58 if (fStream) {
59 if (fStream->is_open()) fStream->close();
60 delete fStream;
61 fStream = NULL;
62 }
63 if (fFileNumber < 0) return kFALSE;
64
65 fFileNumber++;
66 char fileName[256];
67 sprintf(fileName, "%s%d", fFileName, fFileNumber);
68 fStream = new fstream(fileName, ios::binary|ios::in);
69 return (fStream->is_open());
70}
71
72
73Bool_t AliRawReader::ReadMiniHeader()
74{
75// read a mini header at the current stream position
76// returns kFALSE if the mini header could not be read
77
78 if (!fStream) return kFALSE;
79 while (!fStream->read((char*) &fMiniHeader, sizeof(fMiniHeader))) {
80 if (!OpenNextFile()) return kFALSE;
81 }
82 if ((fMiniHeader.fMagicWord[2] != 0x12) ||
83 (fMiniHeader.fMagicWord[1] != 0x34) ||
84 (fMiniHeader.fMagicWord[0] != 0x56))
85 Error("ReadMiniHeader", "wrong magic word!");
86 fCount = fMiniHeader.fSize;
87 return kTRUE;
88}
89
90Bool_t AliRawReader::ReadNextInt(UInt_t& data)
91{
92// reads the next 4 bytes at the current stream position
93// returns kFALSE if the data not be read
94
95 while (fCount == 0) {
96 if (!ReadMiniHeader()) return kFALSE;
97 }
98 if (!fStream->read((char*) &data, sizeof(data))) {
99 Error("ReadNextInt", "could not read data!");
100 return kFALSE;
101 }
102 fCount -= sizeof(data);
103 return kTRUE;
104}
105
106Bool_t AliRawReader::ReadNextShort(UShort_t& data)
107{
108// reads the next 2 bytes at the current stream position
109// returns kFALSE if the data not be read
110
111 while (fCount == 0) {
112 if (!ReadMiniHeader()) return kFALSE;
113 }
114 if (!fStream->read((char*) &data, sizeof(data))) {
115 Error("ReadNextShort", "could not read data!");
116 return kFALSE;
117 }
118 fCount -= sizeof(data);
119 return kTRUE;
120}
121
122Bool_t AliRawReader::ReadNextChar(UChar_t& data)
123{
124// reads the next 1 byte at the current stream position
125// returns kFALSE if the data not be read
126
127 while (fCount == 0) {
128 if (!ReadMiniHeader()) return kFALSE;
129 }
130 if (!fStream->read((char*) &data, sizeof(data))) {
131 Error("ReadNextChar", "could not read data!");
132 return kFALSE;
133 }
134 fCount -= sizeof(data);
135 return kTRUE;
136}
137