GRSISort "v4.0.0.5"
An extension of the ROOT analysis Framework
Loading...
Searching...
No Matches
TLstFile.cxx
Go to the documentation of this file.
1#include <iostream>
2#include <bitset>
3#include <fstream>
4#include <cstdio>
5#include <cstring>
6#include <sys/types.h>
7#include <sys/stat.h>
8#include <fcntl.h>
9#include <cerrno>
10#include <cassert>
11#include <cstdlib>
12
13#ifdef HAVE_ZLIB
14#include <zlib.h>
15#endif
16
17#include "TString.h"
18
19#include "TLstFile.h"
20#include "TLstEvent.h"
21#include "TRunInfo.h"
23#include "TILLMnemonic.h"
24#include "ILLDataVersion.h"
25
26#define READ_EVENT_SIZE 10000
27
28TLstFile::TLstFile(const char* filename, TRawFile::EOpenType open_type) : TLstFile()
29{
30 switch(open_type) {
31 case TRawFile::EOpenType::kRead: Open(filename); break;
32
34 }
35}
36
38{
39 // Default dtor. It closes the read in lst file as well as the output lst file.
40 if(fBoardHeaders != nullptr)
41 delete[] fBoardHeaders;
42 Close();
43}
44
45std::string TLstFile::Status(bool)
46{
47 return Form(HIDE_CURSOR " Processed event, have processed %.2fMB/%.2f MB " SHOW_CURSOR "\r",
48 (BytesRead() / 1000000.0), (FileSize() / 1000000.0));
49}
50
51/// Open a lst .lst file with given file name.
52///
53/// Remote files can be accessed using these special file names:
54/// - pipein://command - read data produced by given command, see examples below
55/// - ssh://username\@hostname/path/file.mid - read remote file through an ssh pipe
56/// - ssh://username\@hostname/path/file.mid.gz and file.mid.bz2 - same for compressed files
57/// - dccp://path/file.mid (also file.mid.gz and file.mid.bz2) - read data from dcache, requires dccp in the PATH
58///
59/// Examples:
60/// - ./event_dump.exe /ladd/data9/t2km11/data/run02696.mid.gz - read normal compressed file
61/// - ./event_dump.exe ssh://ladd09//ladd/data9/t2km11/data/run02696.mid.gz - read compressed file through ssh to ladd09
62/// (note double "/")
63/// - ./event_dump.exe pipein://"cat /ladd/data9/t2km11/data/run02696.mid.gz | gzip -dc" - read data piped from a
64/// command or script (note quotes)
65/// - ./event_dump.exe pipein://"gzip -dc /ladd/data9/t2km11/data/run02696.mid.gz" - another way to read compressed
66/// files
67/// - ./event_dump.exe dccp:///pnfs/triumf.ca/data/t2km11/aug2008/run02837.mid.gz - read file directly from a dcache
68/// pool (note triple "/")
69///
70/// \param[in] filename The file to open.
71/// \returns "true" for succes, "false" for error, use GetLastError() to see why
72bool TLstFile::Open(const char* filename)
73{
74 Filename(filename);
75 int32_t headerSize = 0; // Count the number of bytes in the header
76
77 //int32_t* boardHeaders = new int32_t[nbBoards]
78
79 try {
80 fInputStream.open(GetFilename(), std::ifstream::in | std::ifstream::binary);
81 fInputStream.seekg(0, std::ifstream::end);
82 if(fInputStream.tellg() < 0) {
83 std::cout << R"(Failed to open ")" << GetFilename() << "/" << Filename() << R"("!)" << std::endl;
84 return false;
85 }
86 FileSize(fInputStream.tellg());
87
88 // Read Header Information
89 fInputStream.seekg(0, std::ifstream::beg);
90
91 fInputStream.read(reinterpret_cast<char*>(&fVersion), sizeof(int32_t));
92 fInputStream.read(reinterpret_cast<char*>(&fTimeBase), sizeof(int32_t));
93 fInputStream.read(reinterpret_cast<char*>(&fNbEvents), sizeof(int32_t));
94 fInputStream.read(reinterpret_cast<char*>(&fNbBoards), sizeof(int32_t));
95 headerSize += 4 * 4; // 4 chucks of 4 Bytes
96
97 // Read Board Headers
98 fBoardHeaders = new int32_t[fNbBoards];
99 fInputStream.read(reinterpret_cast<char*>(fBoardHeaders), fNbBoards * sizeof(uint32_t));
100 headerSize += 4 * fNbBoards;
101
102 ResizeBuffer(READ_EVENT_SIZE * 4 * sizeof(int32_t));
103 fInputStream.seekg(headerSize, std::ifstream::beg);
104 } catch(std::exception& e) {
105 std::cout << "Caught " << e.what() << " at " << __FILE__ << " : " << __LINE__ << std::endl;
106 }
107
108 // setup TChannel to use our mnemonics
109 TChannel::SetMnemonicClass(TILLMnemonic::Class());
110
111 // parse header information
112 ParseHeaders();
113
117 TRunInfo::SetVersion(ILLDATA_RELEASE);
118
119 std::cout << "Successfully read " << FileSize() - headerSize << " bytes into buffer!" << std::endl;
120
123
124 return true;
125}
126
128{
129 // loop over all board headers
130 for(uint8_t board = 0; board < fNbBoards; ++board) {
131 // get the board information
132 uint8_t crate = (fBoardHeaders[board] >> 12) & 0xf;
133 uint16_t eventType = (fBoardHeaders[board] >> 16) & 0xffff; // not used except for printing
134 uint8_t nbChannels = (fBoardHeaders[board] >> 6) & 0x3f;
135 uint8_t boardType = fBoardHeaders[board] & 0x3f;
136 std::string boardName = "unset";
137 switch(boardType) {
138 // only V1724, V1725, V1730, and V1751 are implemented
139 // the case IDs are taken from CrateBoard.h from the ILL (assuming no difference between PHA, PSD, and waveform types)
140 case 2:
141 case 32:
142 boardName = "V1724";
143 break;
144 case 7:
145 case 34:
146 boardName = "V1725";
147 break;
148 case 3:
149 case 4:
150 case 33:
151 boardName = "V1730";
152 break;
153 case 1:
154 case 31:
155 boardName = "V1751";
156 break;
157 default:
158 std::cout << "Warning, unknown board type " << boardType << " encountered, don't know what digitizer type is." << std::endl;
159 break;
160 }
161 std::cout << "For " << static_cast<int>(board) << ". board got header " << hex(fBoardHeaders[board], 8) << ": crate " << static_cast<int>(crate) << ", event type " << eventType << ", board type " << static_cast<int>(boardType) << " = " << boardName << " with " << static_cast<int>(nbChannels) << " channels." << std::endl;
162 for(uint8_t channel = 0; channel < nbChannels; ++channel) {
163 // channel address is 4 bit crate, 6 bit board, 6 bit channel
164 unsigned int address = (static_cast<unsigned int>(crate) << 12) | (static_cast<unsigned int>(board) << 6) | channel;
165
166 TChannel* tmpChan = TChannel::GetChannel(address);
167 if(tmpChan == nullptr) {
168 // ignoring crate here, so if Fipps ever changes to multiple crates this needs to be updated
169 tmpChan = new TChannel(Form("TMP%02dXX%02dX", board, channel));
170 tmpChan->SetAddress(address);
171 }
173 TChannel::AddChannel(tmpChan);
174 }
175 }
176}
177
179{
180 fInputStream.close();
181}
182
183/// \param [in] Event Pointer to an empty TLstEvent
184/// \returns "true" for success, "false" for failure, see GetLastError() to see why
185///
186int TLstFile::Read(std::shared_ptr<TRawEvent> event)
187{
188 if(event == nullptr)
189 return -1;
190
191 size_t LastReadSize = 0;
192 std::shared_ptr<TLstEvent> LstEvent = std::static_pointer_cast<TLstEvent>(event);
193 LstEvent->Clear();
194
195 LstEvent->SetLstVersion(fVersion);
196
197 if(BytesRead() < FileSize()) {
198 // Fill the buffer
199 char tempBuff[READ_EVENT_SIZE * 4 * sizeof(int32_t)];
200 try {
201 fInputStream.read(tempBuff, READ_EVENT_SIZE * 4 * sizeof(int32_t));
202 LastReadSize = static_cast<size_t>(fInputStream.gcount());
203 IncrementBytesRead(LastReadSize);
204
205 ClearBuffer();
206 for(size_t i = 0; i < LastReadSize; i++) {
207 ReadBuffer().push_back(tempBuff[i]);
208 }
209
210 } catch(std::exception& e) {
211 std::cout << "Caught " << e.what() << " at " << __FILE__ << " : " << __LINE__ << std::endl;
212 }
213
214 // Write data to event
215 LstEvent->SetData(ReadBuffer());
216
217 return LastReadSize;
218 }
219 return 0;
220}
221
222void TLstFile::Skip(size_t)
223{
224 std::cerr << "Sorry, but we can't skip events in an LST file, the whole file is treated as a single event!" << std::endl;
225 return;
226}
227
229{
230 // Parse the run number from the current TLstFile. This assumes a format of
231 // run#####_###.lst or run#####.lst.
232 if(Filename().length() == 0) {
233 return 0;
234 }
235 std::size_t foundslash = Filename().rfind('/');
236 std::size_t found = Filename().rfind(".lst");
237 if(found == std::string::npos) {
238 return 0;
239 }
240 std::size_t found2 = Filename().rfind('-');
241 if((found2 < foundslash && foundslash != std::string::npos) || found2 == std::string::npos) {
242 found2 = Filename().rfind('_');
243 }
244 if(found2 < foundslash && foundslash != std::string::npos) {
245 found2 = std::string::npos;
246 }
247 std::string temp;
248 if(found2 == std::string::npos || Filename().compare(found2 + 4, 4, ".lst") != 0) {
249 temp = Filename().substr(found - 5, 5);
250 } else {
251 temp = Filename().substr(found - 9, 5);
252 }
253 return atoi(temp.c_str());
254}
255
257{
258 // There are no subruns in .lst files
259 return -1;
260}
261
262// end
#define SHOW_CURSOR
Definition Globals.h:33
std::string hex(T val, int width=-1)
Definition Globals.h:129
#define HIDE_CURSOR
Definition Globals.h:32
#define READ_EVENT_SIZE
Definition TLstFile.cxx:26
static void SetMnemonicClass(const TClassRef &cls)
Definition TChannel.h:80
void SetAddress(unsigned int tmpadd)
Definition TChannel.cxx:540
static TChannel * GetChannel(unsigned int temp_address, bool warn=false)
Definition TChannel.cxx:459
static void AddChannel(TChannel *, Option_t *opt="")
Definition TChannel.cxx:285
void SetDigitizerType(const TPriorityValue< std::string > &tmp)
Reader for ILL .lst files.
Definition TLstFile.h:31
std::string Status(bool long_file_description=true) override
Definition TLstFile.cxx:45
void Skip(size_t nofEvents) override
Skip nofEvents from the file.
Definition TLstFile.cxx:222
int32_t fVersion
Definition TLstFile.h:66
int32_t fNbEvents
Definition TLstFile.h:68
int GetRunNumber() override
Definition TLstFile.cxx:228
int32_t fNbBoards
Definition TLstFile.h:69
std::ifstream fInputStream
Definition TLstFile.h:71
void Close() override
Close input file.
Definition TLstFile.cxx:178
void ParseHeaders()
Definition TLstFile.cxx:127
bool Open(const char *filename) override
Open input file.
Definition TLstFile.cxx:72
TLstFile()=default
default constructor
~TLstFile() override
destructor
Definition TLstFile.cxx:37
int GetSubRunNumber() override
Definition TLstFile.cxx:256
int32_t fTimeBase
Definition TLstFile.h:67
int32_t * fBoardHeaders
Definition TLstFile.h:70
int Read(std::shared_ptr< TRawEvent > lstEvent) override
Read one event from the file.
Definition TLstFile.cxx:186
void ClearBuffer()
Definition TRawFile.h:73
std::vector< char > & ReadBuffer()
Definition TRawFile.h:70
virtual const char * GetFilename() const
Get the name of this file.
Definition TRawFile.h:56
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
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
static void SetRunLength()
Definition TRunInfo.h:231