GRSISort "v4.0.0.5"
An extension of the ROOT analysis Framework
Loading...
Searching...
No Matches
TTdrFile.cxx
Go to the documentation of this file.
1#include <iostream>
2#include <fstream>
3#include <cstdio>
4#include <cstring>
5#include <sys/types.h>
6#include <sys/stat.h>
7#include <fcntl.h>
8#include <cerrno>
9#include <cassert>
10#include <cstdlib>
11
12#ifdef HAVE_ZLIB
13#include <zlib.h>
14#endif
15
16#include "TString.h"
17
18#include "TTdrFile.h"
19#include "TTdrEvent.h"
20#include "TRunInfo.h"
22#include "TTdrMnemonic.h"
23#include "iThembaDataVersion.h"
24
25/// \cond CLASSIMP
27/// \endcond
28
30{
31 // Default Constructor
32 fBytesRead = 0;
33 fFileSize = 0;
34}
35
36TTdrFile::TTdrFile(const char* filename, TRawFile::EOpenType open_type) : TTdrFile()
37{
38 switch(open_type) {
39 case TRawFile::EOpenType::kRead: Open(filename); break;
40
42 }
43}
44
46{
47 // Default dtor. It closes the read in midas file as well as the output midas file.
48 Close();
49}
50
51std::string TTdrFile::Status(bool)
52{
53 return Form(HIDE_CURSOR " Processed event, have processed %.2fMB/%.2f MB " SHOW_CURSOR "\r",
54 (fBytesRead / 1000000.0), (fFileSize / 1000000.0));
55}
56
57/// Open a tdr file with given file name.
58///
59/// \param[in] filename The file to open.
60/// \returns "true" for succes, "false" for error, use GetLastError() to see why
61bool TTdrFile::Open(const char* filename)
62{
63 fFilename = filename;
64 try {
65 fInputFile.open(GetFilename(), std::ifstream::in | std::ifstream::binary);
66 fInputFile.seekg(0, std::ifstream::end);
67 if(fInputFile.tellg() < 0) {
68 std::cout<<R"(Failed to open ")"<<GetFilename()<<"/"<<fFilename<<R"("!)"<<std::endl;
69 return false;
70 }
71 fFileSize = fInputFile.tellg();
72 fInputFile.seekg(0, std::ifstream::beg);
73 fReadBuffer.resize(0x10000);
74 } catch(std::exception& e) {
75 std::cout<<"Caught "<<e.what()<<std::endl;
76 }
77// Do we need these?
78// signal(SIGPIPE,SIG_IGN); // crash if reading from closed pipe
79// signal(SIGXFSZ,SIG_IGN); // crash if reading from file >2GB without O_LARGEFILE
80
81#ifndef O_LARGEFILE
82#define O_LARGEFILE 0
83#endif
84
85 // setup TChannel to use our mnemonics
86 TChannel::SetMnemonicClass(TTdrMnemonic::Class());
87
90 TRunInfo::SetVersion(ITHEMBADATA_RELEASE);
91
92 std::cout<<"Successfully opened file with "<<fFileSize<<" bytes!"<<std::endl;
93
94 //std::cout<<std::hex<<std::setfill('0');
95 //for(size_t i = 0; i < fReadBuffer.size() && i < 256; ++i) {
96 // std::cout<<std::setw(2)<<(static_cast<int16_t>(fReadBuffer[i])&0xff)<<" ";
97 // if(i%16 == 15) std::cout<<std::endl;
98 //}
99 //std::cout<<std::dec<<std::setfill(' ');
100
103
104 return true;
105}
106
108{
109}
110
111/// \param [in] tdrEvent Pointer to an empty TTdrEvent
112/// \returns "true" for success, "false" for failure, see GetLastError() to see why
113///
114/// EDITED FROM THE ORIGINAL TO RETURN TOTAL SUCESSFULLY BYTES READ INSTEAD OF TRUE/FALSE, PCB
115///
116int TTdrFile::Read(std::shared_ptr<TRawEvent> tdrEvent)
117{
118 if(!fInputFile.is_open()) {
119 return 0;
120 }
121 // try to read next 64k buffer
122 fInputFile.read(fReadBuffer.data(), 0x10000);
123 if(!fInputFile.good()) {
124 std::cout<<"Failed to read next 64k buffer, currently at "<<fBytesRead<<"/"<<fFileSize<<std::endl;
125 fInputFile.close();
126 return 0;
127 }
128 //read 24 byte header
129 if(strncmp(fReadBuffer.data(), "EBYEDATA", 8) != 0) {
130 std::cerr<<fBytesRead<<": Failed to find 'EBYEDATA' (or 0x45 42 59 45 44 41 54 41) at beginning of header (0x"<<std::hex<<std::setfill('0')<<std::setw(8)<<*reinterpret_cast<uint64_t*>(fReadBuffer.data())<<std::hex<<std::setfill(' ')<<")"<<std::endl;
131 return 0;
132 }
133 try {
134 std::static_pointer_cast<TTdrEvent>(tdrEvent)->SetHeader(fReadBuffer.data());
135 } catch(std::exception& e) {
136 std::cout<<e.what()<<std::endl;
137 }
138 uint32_t dataSize = std::static_pointer_cast<TTdrEvent>(tdrEvent)->GetHeader().fDataLength;
139 if(24 + dataSize < 0x10000) {
140 try {
141 std::static_pointer_cast<TTdrEvent>(tdrEvent)->SetData(std::vector<char>(fReadBuffer.begin() + 24, fReadBuffer.begin() + 24 + dataSize));
142 } catch(std::exception& e) {
143 std::cout<<e.what()<<std::endl;
144 }
145 fBytesRead = fInputFile.tellg();
146 if(fBytesRead == fFileSize) {
147 fInputFile.close();
148 }
149 return 0x10000;
150 }
151 return 0;
152}
153
154void TTdrFile::Skip(size_t nofEvents)
155{
156 if(!fInputFile.is_open()) {
157 std::cerr<<__PRETTY_FUNCTION__<<": input file is not open!"<<std::endl;
158 return;
159 }
160 // try to skip next nofEvents 64k buffer
161 fInputFile.seekg(nofEvents * 0x10000, std::ifstream::cur);
162 if(!fInputFile.good()) {
163 std::cout<<"Failed to skip next "<<nofEvents<<" 64k buffer(s), currently at "<<fBytesRead<<"/"<<fFileSize<<std::endl;
164 fInputFile.close();
165 }
166}
167
169{
170 // Parse the run number from the current TMidasFile. This assumes a format of
171 // R#*_#* or R#* (#* denoting one or more digits).
172 if(fFilename.length() == 0) {
173 return 0;
174 }
175 std::size_t foundslash = fFilename.rfind('/');
176 std::size_t found = fFilename.rfind('R');
177 if(found < foundslash || found ==std::string::npos) {
178 std::cout<<"Warning, failed to find 'R' in filename '"<<fFilename<<"'!"<<std::endl;
179 return 0;
180 }
181 std::size_t found2 = fFilename.rfind('-');
182 if((found2 < foundslash && foundslash != std::string::npos) || found2 == std::string::npos) {
183 found2 = fFilename.rfind('_');
184 }
185 // printf("found 2 = %i\n",found2);
186 if(found2 < foundslash && foundslash != std::string::npos) {
187 found2 = std::string::npos;
188 }
189 std::string temp;
190 if(found2 == std::string::npos) {
191 // no subrun number found, use rest of filename
192 temp = fFilename.substr(found + 1);
193 } else {
194 // subrun number found, use everything between 'R' and '_'/'-'
195 temp = fFilename.substr(found + 1, found2 - (found + 1));
196 }
197 return atoi(temp.c_str());
198}
199
201{
202 // Parse the sub run number from the current TMidasFile. This assumes a format of
203 // R#*_#* or R#* (#* denoting one or more digits).
204 if(fFilename.length() == 0) {
205 return -1;
206 }
207 std::size_t foundslash = fFilename.rfind('/');
208 std::size_t found = fFilename.rfind('-');
209 if((found < foundslash && foundslash != std::string::npos) || found == std::string::npos) {
210 found = fFilename.rfind('_');
211 }
212 if(found < foundslash && foundslash != std::string::npos) {
213 found = std::string::npos;
214 }
215 if(found != std::string::npos) {
216 std::string temp = fFilename.substr(found + 1);
217 return atoi(temp.c_str());
218 }
219 return -1;
220}
221
222// end
#define SHOW_CURSOR
Definition Globals.h:33
#define HIDE_CURSOR
Definition Globals.h:32
ClassImp(THILMnemonic) void THILMnemonic
static void SetMnemonicClass(const TClassRef &cls)
Definition TChannel.h:80
size_t fFileSize
Definition TRawFile.h:88
virtual const char * GetFilename() const
Get the name of this file.
Definition TRawFile.h:56
size_t fBytesRead
Definition TRawFile.h:87
std::vector< char > fReadBuffer
Definition TRawFile.h:85
std::string fFilename
name of the currently open file
Definition TRawFile.h:83
static void ClearVersion()
Definition TRunInfo.h:136
static void SetRunInfo(int runnum=0, int subrunnum=-1)
Definition TRunInfo.cxx:135
static void SetVersion(const char *ver)
Definition TRunInfo.h:137
static void SetDetectorInformation(TDetectorInformation *inf)
Definition TRunInfo.h:271
Reader for MIDAS .mid files.
Definition TTdrFile.h:32
int Read(std::shared_ptr< TRawEvent > lstEvent) override
Read one event from the file.
Definition TTdrFile.cxx:116
void Close() override
Close input file.
Definition TTdrFile.cxx:107
std::string Status(bool long_file_description=true) override
Definition TTdrFile.cxx:51
bool Open(const char *filename) override
Open input file.
Definition TTdrFile.cxx:61
std::ifstream fInputFile
Definition TTdrFile.h:58
TTdrFile()
default constructor
Definition TTdrFile.cxx:29
int GetRunNumber() override
Definition TTdrFile.cxx:168
int GetSubRunNumber() override
Definition TTdrFile.cxx:200
void Skip(size_t nofEvents) override
Skip nofEvents from the file.
Definition TTdrFile.cxx:154
~TTdrFile() override
destructor
Definition TTdrFile.cxx:45