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