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