GRSISort "v4.0.0.5"
An extension of the ROOT analysis Framework
Loading...
Searching...
No Matches
TRlmdFile.cxx
Go to the documentation of this file.
1#include <iostream>
2#include <bitset>
3#include <fstream>
4#include <sstream>
5#include <cstdio>
6#include <cstring>
7#include <sys/types.h>
8#include <sys/stat.h>
9#include <fcntl.h>
10#include <cerrno>
11#include <cassert>
12#include <cstdlib>
13#include <locale>
14#include <time.h>
15
16#ifdef HAVE_ZLIB
17#include <zlib.h>
18#endif
19
20#include "TString.h"
21
22#include "TRlmdFile.h"
23#include "TRlmdEvent.h"
24#include "TRunInfo.h"
26#include "THILMnemonic.h"
27#include "HILDataVersion.h"
28
29TRlmdFile::TRlmdFile(const char* filename, TRawFile::EOpenType open_type) : TRlmdFile()
30{
31 switch(open_type) {
32 case TRawFile::EOpenType::kRead: Open(filename); break;
34 }
35}
36
38{
39 // Default dtor. It closes the read in lmd file as well as the output lmd file.
40 Close();
41}
42
43std::string TRlmdFile::Status(bool)
44{
45 return Form(HIDE_CURSOR " Processed event, have processed %.2fMB/%.2f MB " SHOW_CURSOR "\r",
46 (fInputStream.tellg() / 1000000.0), (FileSize() / 1000000.0));
47}
48
49/// Open a lmd .lmd file with given file name.
50///
51/// Remote files can be accessed using these special file names:
52/// - pipein://command - read data produced by given command, see examples below
53/// - ssh://username\@hostname/path/file.mid - read remote file through an ssh pipe
54/// - ssh://username\@hostname/path/file.mid.gz and file.mid.bz2 - same for compressed files
55/// - dccp://path/file.mid (also file.mid.gz and file.mid.bz2) - read data from dcache, requires dccp in the PATH
56///
57/// Examples:
58/// - ./event_dump.exe /ladd/data9/t2km11/data/run02696.mid.gz - read normal compressed file
59/// - ./event_dump.exe ssh://ladd09//ladd/data9/t2km11/data/run02696.mid.gz - read compressed file through ssh to ladd09
60/// (note double "/")
61/// - ./event_dump.exe pipein://"cat /ladd/data9/t2km11/data/run02696.mid.gz | gzip -dc" - read data piped from a
62/// command or script (note quotes)
63/// - ./event_dump.exe pipein://"gzip -dc /ladd/data9/t2km11/data/run02696.mid.gz" - another way to read compressed
64/// files
65/// - ./event_dump.exe dccp:///pnfs/triumf.ca/data/t2km11/aug2008/run02837.mid.gz - read file directly from a dcache
66/// pool (note triple "/")
67///
68/// \param[in] filename The file to open.
69/// \returns "true" for succes, "false" for error, use GetLastError() to see why
70bool TRlmdFile::Open(const char* filename)
71{
72 Filename(filename);
73
74 try {
75 fInputStream.open(GetFilename(), std::ifstream::in | std::ifstream::binary);
76 fInputStream.seekg(0, std::ifstream::end);
77 if(fInputStream.tellg() < 0) {
78 std::cout << R"(Failed to open ")" << GetFilename() << "/" << Filename() << R"("!)" << std::endl;
79 return false;
80 }
81 FileSize(fInputStream.tellg());
82 fInputStream.seekg(0, std::ifstream::beg);
83 //std::ofstream debugFile("debug.txt", std::ios::app);
84 //debugFile<<"Total file size is "<<FileSize()<<" bytes"<<std::endl;
85
86 // Read Header Information
87 RlmdFileHeader header;
88 fInputStream.read(reinterpret_cast<char*>(&header), sizeof(RlmdFileHeader));
89
90 //debugFile<<"read header of size "<<sizeof(RlmdFileHeader)<<" bytes"<<std::endl;
91 fStartDate = header.date;
92 fStartDate = fStartDate.substr(0, 12);
93 fStartTime = header.time;
94 fStartTime = fStartTime.substr(0, 8);
95 fTemplate = header.templateName;
96 fTemplate = fTemplate.substr(0, 1480);
97 } catch(std::exception& e) {
98 std::cout << "Caught " << e.what() << " at " << __FILE__ << " : " << __LINE__ << std::endl;
99 }
100
101 // setup TChannel to use our mnemonics
102 TChannel::SetMnemonicClass(THILMnemonic::Class());
103
107 TRunInfo::SetVersion(HILDATA_RELEASE);
108
109 BytesRead(fInputStream.tellg());
110 std::cout << "Successfully read " << BytesRead() << " bytes of header with start date " << fStartDate << " " << fStartTime << " and template " << fTemplate << "!" << std::endl;
111
114
115 return true;
116}
117
119{
120 fInputStream.close();
121 BytesRead(FileSize()); // just to make sure we don't try to read more data after the file was closed
122}
123
124/// \param [in] Event Pointer to an empty TRlmdEvent
125/// \returns "true" for success, "false" for failure, see GetLastError() to see why
126///
127/// EDITED FROM THE ORIGINAL TO RETURN TOTAL SUCESSFULLY BYTES READ INSTEAD OF TRUE/FALSE, PCB
128///
129int TRlmdFile::Read(std::shared_ptr<TRawEvent> Event)
130{
131 if(Event == nullptr) return -1;
132
133 size_t LastReadSize = 0;
134 std::shared_ptr<TRlmdEvent> RlmdEvent = std::static_pointer_cast<TRlmdEvent>(Event);
135 RlmdEvent->Clear();
136
137 if(BytesRead() < FileSize()) {
138 //std::ofstream debugFile("debug.txt", std::ios::app);
139 //debugFile<<std::hex;
140 //debugFile<<"buffer 0x"<<fBuffersRead<<": starting to read at 0x"<<fInputStream.tellg()<<std::endl;
141 // Read the buffer header
142 fInputStream.read(reinterpret_cast<char*>(&fBufferHeader), sizeof(RlmdBufferHeader));
143 LastReadSize += static_cast<size_t>(fInputStream.gcount());
144
145 switch(fBufferHeader.type) {
146 case 2: // normal event so we read the buffer data into the event and send it on
147 //debugFile<<"read buffer header of size 0x"<<sizeof(RlmdBufferHeader)<<", reading buffer of size 0x"<<2*fBufferHeader.dataLength<<" starting at 0x"<<fInputStream.tellg()<<std::endl;
148 try {
149 // the data length never changes, so this should only resize the vector once
150 ResizeBuffer(2 * fBufferHeader.dataLength); // 2* accounts for the size of uint16_t
152 LastReadSize += static_cast<size_t>(fInputStream.gcount());
153 IncrementBytesRead(LastReadSize);
154 //std::cout<<"Read normal buffer with "<<2*fBufferHeader.dataLength<<"/"<<LastReadSize<<" bytes, now at byte "<<BytesRead()<<std::endl;
155 RlmdEvent->SetData(ReadBuffer());
156 ++fBuffersRead;
157 } catch(std::exception& e) {
158 std::cout << "Caught " << e.what() << " at " << __FILE__ << " : " << __LINE__ << std::endl;
159 return -1;
160 }
161 break;
162 case 3: // file footer so we "unread" the buffer header, read the footer and then close the file
163 fInputStream.seekg(-sizeof(RlmdBufferHeader), std::ifstream::cur);
164 RlmdFileFooter footer;
165 //debugFile<<"tail buffer 0x"<<fBuffersRead<<": starting to read at 0x"<<fInputStream.tellg()<<std::endl;
166 fInputStream.read(reinterpret_cast<char*>(&footer), sizeof(RlmdFileFooter));
167 fStopDate = footer.date;
168 fStopDate = fStopDate.substr(0, 12);
169 fStopTime = footer.time;
170 fStopTime = fStopTime.substr(0, 8);
174 Close();
175 LastReadSize = 0; // setting this to zero as it doesn't matter in this case?
176 break;
177 default:
178 std::cerr << "Unknown buffer type " << fBufferHeader.type << std::endl;
179 break;
180 }
181 }
182 return LastReadSize;
183}
184
185void TRlmdFile::Skip(size_t)
186{
187 // this might be wrong, in the case of the rlmd file we could check the data length from the buffer header, skip that far ahead
188 // and read the next buffer header
189 std::cerr << "Sorry, but we can't skip events in an RLMD file, the whole file is treated as a single event!" << std::endl;
190 return;
191}
192
194{
195 // Parse the run number from the current TRlmdFile. This assumes a format of
196 // <name>###.rlmd.
197 if(Filename().length() == 0) {
198 return 0;
199 }
200 std::size_t found = Filename().rfind(".rlmd");
201 if(found == std::string::npos) {
202 return 0;
203 }
204 std::string temp;
205 temp = Filename().substr(found - 3, 3);
206 return atoi(temp.c_str());
207}
208
210{
211 // There are no subruns in .lmd files
212 return -1;
213}
214
215time_t TRlmdFile::ConvertToEpoch(const std::string& date, const std::string& time)
216{
217 std::stringstream str;
218 str << date << time;
219 std::tm t = {};
220 str >> std::get_time(&t, "%d-%b-%Y %H:%M:S");
221 return std::mktime(&t);
222}
223
224// 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
void IncrementBytesRead(size_t val=1)
Definition TRawFile.h:63
virtual size_t BytesRead()
Definition TRawFile.h:61
void ResizeBuffer(size_t newSize)
Definition TRawFile.h:74
RlmdBufferHeader fBufferHeader
Definition TRlmdFile.h:130
std::ifstream fInputStream
Definition TRlmdFile.h:128
int GetSubRunNumber() override
void Close() override
Close input file.
int Read(std::shared_ptr< TRawEvent > rlmdEvent) override
Read one event from the file.
std::string fTemplate
Definition TRlmdFile.h:127
~TRlmdFile() override
destructor
Definition TRlmdFile.cxx:37
std::string fStopDate
Definition TRlmdFile.h:125
std::string Status(bool long_file_description=true) override
Definition TRlmdFile.cxx:43
std::string fStartDate
Definition TRlmdFile.h:123
std::string fStartTime
Definition TRlmdFile.h:124
time_t ConvertToEpoch(const std::string &date, const std::string &time)
TRlmdFile()=default
default constructor
std::string fStopTime
Definition TRlmdFile.h:126
int GetRunNumber() override
size_t fBuffersRead
Definition TRlmdFile.h:131
void Skip(size_t nofEvents) override
Skip nofEvents from the file.
bool Open(const char *filename) override
Open input file.
Definition TRlmdFile.cxx:70
static void ClearVersion()
Definition TRunInfo.h:142
static void SetRunStart(double tmp)
Definition TRunInfo.h:228
static void SetRunStop(double tmp)
Definition TRunInfo.h:229
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
static void SetRunLength()
Definition TRunInfo.h:231
uint16_t dataLength
Definition TRlmdFile.h:82
uint16_t type
Definition TRlmdFile.h:84
char time[8]
Definition TRlmdFile.h:51
char templateName[TEMPLEN]
Definition TRlmdFile.h:67
char date[12]
Definition TRlmdFile.h:50