RawCorsikaFile.cc
Go to the documentation of this file.
1 #include <io/RawCorsikaFile.h>
2 #include <io/CorsikaBlock.h>
3 #include <io/CorsikaIOException.h>
4 #include <utl/ErrorLogger.h>
5 
6 #include <string>
7 #include <sstream>
8 #include <iostream>
9 
10 using namespace std;
11 using std::string;
12 using namespace io::Corsika;
13 
14 
15 RawFile::RawFile(const std::string& name) :
16  fDiskStream(name.c_str())
17 {
18  if (!fDiskStream) {
19  const string msg = "Error opening Corsika file '" + name + "'.";
20  ERROR(msg);
21  throw io::CorsikaIOException(msg);
22  }
23 }
24 
25 
26 bool
27 RawFile::Open(const std::string& name, const bool noException)
28 {
29  if (fDiskStream.is_open()) {
30  if (!noException) {
31  const string msg = "Cannot open Corsika file '" + name +
32  "' - *this is already open";
33  ERROR(msg);
34  throw io::CorsikaIOException(msg);
35  }
36  return false;
37  }
38 
39  fDiskStream.open(name.c_str());
40 
41  if (!fDiskStream.is_open()) {
42  if (!noException) {
43  const string msg = "Error opening Corsika file '" + name + "'.";
44  ERROR(msg);
45  throw io::CorsikaIOException(msg);
46  }
47  return false;
48  }
49 
50  // RU Mo 10. Okt 01:47:15 CEST 2016
51  // search for string RUNH in first bytes of file.
52  // the offset to RUNH is '4' in 32 bit linux, but different on different systems.
53  // if RUNH is not found, the file is most certainly corrupt and will fail later
54  bool foundRUNH = false;
55  unsigned int posRUNH = 0;
56 
57  const unsigned int toRead = 20;
58  char buff[toRead] = { '\0' };
59  if (fDiskStream.read(buff, toRead)) {
60  for ( ; posRUNH+3 < toRead; ++posRUNH) {
61  if (buff[posRUNH+0] == 'R' &&
62  buff[posRUNH+1] == 'U' &&
63  buff[posRUNH+2] == 'N' &&
64  buff[posRUNH+3] == 'H') {
65  foundRUNH = true;
66  break;
67  }
68  }
69  }
70 
71  if (!foundRUNH) { // did not find anything. This is no corsika data.
72  //fPaddingSize = 4;
73  fDiskStream.close();
74  return false;
75  }
76 
77  fPaddingSize = posRUNH;
78 
79  fDiskStream.seekg(0, std::ios::beg); // now go back to beginning
80 
83  fBlockBufferValid = false;
84  return true;
85 }
86 
87 
88 void
90 {
91  if (fDiskStream.is_open())
92  fDiskStream.close();
93 }
94 
95 
96 bool
98 {
99  const bool thinned = true;
100 
101  if (!fBlockBufferValid && !ReadDiskBlock(thinned))
102  return false;
103 
107  fIndexInDiskBlock = 0;
108  fBlockBufferValid = false;
109  }
110 
111  return true;
112 }
113 
114 
115 bool
117 {
118  const bool thinned = false;
119 
120  if (!fBlockBufferValid && !ReadDiskBlock(thinned))
121  return false;
122 
126  fIndexInDiskBlock = 0;
127  fBlockBufferValid = false;
128  }
129 
130  return true;
131 }
132 
133 
134 
135 bool
136 RawFile::ReadDiskBlock(const bool thinned)
137 {
138  if (fDiskStream.tellg() < 0)
139  fDiskStream.clear();
140 
141  const unsigned int size = thinned ? sizeof(ThinnedDiskBlock) : sizeof(UnthinnedDiskBlock);
142  const unsigned int sizeDisk = size + 2*fPaddingSize;
143 
144  if (!fDiskStream.seekg(fCurrentBlockNumber * sizeDisk))
145  return false;
146 
147  char padding[fPaddingSize];
148  if (!fDiskStream.read(padding, fPaddingSize))
149  return false;
150 
151  char* const buff = thinned ?
152  reinterpret_cast<char*>(&fDiskThinnBlockBuffer) : reinterpret_cast<char*>(&fDiskUnthinnBlockBuffer);
153 
154  if (!fDiskStream.read(buff, size))
155  return false;
156 
157  if (!fDiskStream.read(padding, fPaddingSize))
158  return false;
159 
160  fBlockBufferValid = true;
161  return true;
162 }
163 
164 
167  const
168 {
170 }
171 
172 
173 void
174 RawFile::SeekTo(const PositionType position, const bool reset)
175 {
176  PositionType newBlockNumber = position / kBlocksInDiskBlock;
177  if (fCurrentBlockNumber != newBlockNumber) {
178  fCurrentBlockNumber = newBlockNumber;
179  fBlockBufferValid = false;
180  }
182  if (reset)
183  fBlockBufferValid = false;
184 }
185 
186 
187 bool
189  const
190 {
191  return fDiskStream.is_open();
192 }
PositionType fCurrentBlockNumber
static const unsigned int kBlocksInDiskBlock
Padding bytes at the beginning of a raw block.
void Close()
Close file (no-op for closed file).
ThinnedDiskBlock fDiskThinnBlockBuffer
BlockUnthinned fBlock[kBlocksInDiskBlock]
initial padding - works also for size 0
bool GetNextBlock(Block &block)
Read one block and advance.
unsigned int fIndexInDiskBlock
bool IsOpen() const
Check if the file is open.
Block fBlock[kBlocksInDiskBlock]
initial padding - works also for size 0
Base for exceptions in the CORSIKA reader.
bool GetNextBlockUnthinned(BlockUnthinned &block)
Read one block and advance.
void SeekTo(const PositionType position, const bool reset=false)
Seek to a given block, the next block will be thePosition.
std::ifstream fDiskStream
bool ReadDiskBlock(const bool thinned)
Read the block at the current position from disk.
PositionType GetNextPosition() const
Number of the block read by the next call to GetNextBlock.
bool Open(const std::string &name, const bool noException=false)
#define ERROR(message)
Macro for logging error messages.
Definition: ErrorLogger.h:165
UnthinnedDiskBlock fDiskUnthinnBlockBuffer
unsigned long int PositionType

, generated on Tue Sep 26 2023.