HIPO4 C++ Library 4.4.1
Columnar I/O library for CLAS12 physics data
Loading...
Searching...
No Matches
reader.h
Go to the documentation of this file.
1//******************************************************************************
2//* ██╗ ██╗██╗██████╗ ██████╗ ██╗ ██╗ ██████╗ *
3//* ██║ ██║██║██╔══██╗██╔═══██╗ ██║ ██║ ██╔═████╗ *
4//* ███████║██║██████╔╝██║ ██║ ███████║ ██║██╔██║ *
5//* ██╔══██║██║██╔═══╝ ██║ ██║ ╚════██║ ████╔╝██║ *
6//* ██║ ██║██║██║ ╚██████╔╝ ██║██╗╚██████╔╝ *
7//* ╚═╝ ╚═╝╚═╝╚═╝ ╚═════╝ ╚═╝╚═╝ ╚═════╝ *
8//************************ Jefferson National Lab (2017) ***********************
9/*
10 * Copyright (c) 2017. Jefferson Lab (JLab). All rights reserved. Permission
11 * to use, copy, modify, and distribute this software and its documentation
12 * for educational, research, and not-for-profit purposes, without fee and
13 * without a signed licensing agreement.
14 *
15 * IN NO EVENT SHALL JLAB BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL
16 * INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING
17 * OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF JLAB HAS
18 * BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19 *
20 * JLAB SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE. THE HIPO DATA FORMAT SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF
23 * ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS". JLAB HAS NO OBLIGATION TO
24 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
25 *
26 * This software was developed under the United States Government license.
27 * For more information contact author at gavalian@jlab.org
28 * Department of Experimental Nuclear Physics, Jefferson Lab.
29 */
87/*
88 * File: reader.h
89 * Author: gavalian
90 *
91 * Created on April 11, 2017, 2:07 PM
92 */
93
94#ifndef HIPOREADER_H
95#define HIPOREADER_H
96
97
98
99#include <iostream>
100#include <utility>
101#include <vector>
102#include <fstream>
103#include <cstdio>
104#include <cstdlib>
105#include <memory>
106#include <thread>
107#include <climits>
108#include <mutex>
109#include "record.h"
110#include "utils.h"
111#include "bank.h"
112
113namespace hipo {
114
115 // typedef struct fileHeader_t {
117 int uniqueid{};
119 int headerLength{}; // in words (usually 14)
121 int indexArrayLength{}; // in bytes
122 int bitInfo{};
123 int version{};
129 };// fileHeader_t;
130
131
132 // typedef struct recordInfo_t {
139 };// recordInfo_t;
140
141
151
152 private:
153
154 std::vector<int> recordEvents;
155 std::vector<long> recordPosition;
156
157 int currentRecord{};
158 int currentEvent{};
159 int currentRecordEvent{};
160
161 public:
162
163 readerIndex()= default;;
164 ~readerIndex()= default;;
165
166 bool canAdvance();
167 bool advance();
168
169 //dglazier
170 bool canAdvanceInRecord();
171 bool loadRecord(int irec);
172 bool gotoEvent(int eventNumber);
173 bool gotoRecord(int irec);
174
175 int getEventNumber() { return currentEvent;}
176 int getRecordNumber() { return currentRecord;}
177 int getRecordEventNumber() { return currentRecordEvent;}
178 int getMaxEvents();
179 void addSize(int size);
180 void addPosition(long position){ recordPosition.push_back(position);}
181 long getPosition(int index) { return recordPosition[index];}
182
183 //dglazier
184 int getNRecords() const {return recordEvents.size();}
185 void show();
186 void rewind(){
187 currentRecord = -1; currentEvent = -1; currentRecordEvent = -1;
188 }
189 void clear(){
190 recordEvents.clear(); recordPosition.clear();
191 }
192 void reset(){
193 currentRecord = 0; currentEvent = 0; currentRecordEvent = 0;
194 }
195 };
196
197 class reader {
198
199 private:
200
201 fileHeader_t header{};
202 hipo::utils hipoutils;
203 std::ifstream inputStream;
204 long inputStreamSize{};
205
206 hipo::record inputRecord;
207 hipo::readerIndex readerEventIndex;
208 std::vector<long> tagsToRead;
209
210 short _verbose = {0} ;
211
212 std::map<std::string,std::string> userConfig;
213
214
216 hipo::dictionary factory;
217
218 void readHeader();
219 void readIndex();
220
221
222 public:
223
224 reader();
225 reader(const char *file){ open(file);}
226
227 reader(const char *file, std::vector<int> tags){
228 //for(int t = 0; t < tags.size(); t++) setTags(tags[t]);
229 for(auto tag : tags) setTags(tag);
230 open(file);
231 }
232
233 reader(const reader &r){}
234
235 ~reader();
236
237 void about();
238 void rewind(){ readerEventIndex.rewind();}
240 void getStructure(hipo::structure &structure,int group, int item);
241 void getStructureNoCopy(hipo::structure &structure,int group, int item);
242
243 void readUserConfig(std::map<std::string,std::string> &mapConfig);
244 //std::string getUserConfig(const char *key);
245
246 void open(const char *filename);
247 bool is_open(){ return inputStream.is_open();}
248 void setTags(int tag){ tagsToRead.push_back(tag);}
249 void setTags(std::vector<long> tags){ tagsToRead=std::move(tags);}
250 void setVerbose(short level=1){_verbose=level;}
251
252 bool hasNext();
253 bool next();
254 bool gotoEvent(int eventNumber);
255 bool gotoRecord(int irec);
256 bool next(hipo::event &dataevent);
257
258 bool next(std::vector<hipo::bank> &list);
259 std::vector<hipo::bank> getBanks(std::vector<std::string> names);
260
261 void read(hipo::event &dataevent);
262 void printWarning();
263 //void showUserConfig();
264 int getNRecords() const {return readerEventIndex.getNRecords()-1;}
265 bool nextInRecord();
266 bool loadRecord(int irec);
267 bool loadRecord(hipo::record &record, int irec);
268 int getEntries(){return readerEventIndex.getMaxEvents();}
269 std::vector<int> getInt( const char *bank, const char *column, int max = -1);
270 std::vector<float> getFloat(const char *bank, const char *column, int max = -1);
271 };
272
274 private:
275 hipo::reader hr;
276 //hipo::writer hw;
277 hipo::dictionary factory;
278 std::mutex obj;
279 long nProcessed = 0;
280 long nDataLimit = -1;
281 public:
282
283 //static int eof_printout;
284
285 readerstream(){ /*datastream::eof_printout = 0;*/}
286
287 virtual ~readerstream(){}
288 void open(const char *input){
289 hr.open(input);
290 hr.readDictionary(factory);
291 //hw.addDictionary(factory);
292 //hw.open(output);
293 }
294
295 void setLimit(long limit){
296 nDataLimit = limit;
297 }
298
299 void run(std::function<int(int)> &&function, int nthreads){
300 std::vector<std::thread*> threads;
301 for(int i = 0; i < nthreads; i++){
302 threads.push_back(new std::thread(function,i));
303 }
304 printf("-- created denoiser with %lu threads\n", threads.size());
305 for(int k = 0; k < (int) threads.size(); k++) threads[k]->join();
306 for(int k = 0; k < (int) threads.size(); k++) delete threads[k];
307 }
308
309 hipo::reader &reader(){return hr;}
310 hipo::dictionary &dictionary(){ return factory;}
311
312 void pull(hipo::record &record, int index){
313 std::unique_lock<std::mutex> lock(obj);
314 hr.loadRecord(record,index);
315 }
316
317 void pull(std::vector<hipo::event> &events){
318
319 std::unique_lock<std::mutex> lock(obj);
320 bool finished = false;
321 if(nDataLimit>0){ if(nProcessed>nDataLimit) finished = true;}
322
323 if(hr.hasNext()==false){ printf("\n");}
324
325 for(int n = 0; n < (int) events.size(); n++){
326 // write the event in the output if it's not empty
327 //if(events[n].getSize()>16){ hw.addEvent(events[n]);}
328 // reset event and read next in the file if any left
329 events[n].reset();
330 if(hr.next()==true&&finished==false){
331 hr.read(events[n]); nProcessed++;
332 if(nProcessed%250==0) { printf("."); fflush(stdout);}
333 if(nProcessed%10000==0) printf(" : %9lu \n",nProcessed);
334 }
335 }
336 }
337 };
338
339}
340#endif /* HIPOREADER_H */
Definition bank.h:210
Collection of schema definitions, typically read from a HIPO file header.
Definition dictionary.h:157
Definition event.h:62
READER index class is used to construct entire events sequence from all records, and provides ability...
Definition reader.h:150
readerIndex()=default
~readerIndex()=default
int getMaxEvents()
Returns maximum number of events available to read.
Definition reader.cpp:549
void addPosition(long position)
Definition reader.h:180
bool gotoRecord(int irec)
Sets the event pointer to the first even of provided record.
Definition reader.cpp:561
bool loadRecord(int irec)
Implemented by Derek for clas12tool purposes (I think - therefore I am)
Definition reader.cpp:576
void clear()
Definition reader.h:189
void show()
Definition reader.cpp:540
void reset()
Definition reader.h:192
bool canAdvanceInRecord()
Checks to verify if the next event is in the same record as current one.
Definition reader.cpp:597
void rewind()
Definition reader.h:186
int getRecordEventNumber()
Definition reader.h:177
bool canAdvance()
Checks to determine if there are events left in the index buffer.
Definition reader.cpp:486
int getEventNumber()
Definition reader.h:175
bool advance()
Advances the event pointer to next.
Definition reader.cpp:495
void addSize(int size)
Adds record size (number of events) to the list of records.
Definition reader.cpp:473
int getRecordNumber()
Definition reader.h:176
bool gotoEvent(int eventNumber)
Moves the event pointer to give event number.
Definition reader.cpp:517
int getNRecords() const
Definition reader.h:184
long getPosition(int index)
Definition reader.h:181
Definition reader.h:197
void read(hipo::event &dataevent)
Reads current event from the record without advancing the current event position.
Definition reader.cpp:233
std::vector< hipo::bank > getBanks(std::vector< std::string > names)
Definition reader.cpp:279
void readDictionary(hipo::dictionary &dict)
Reads the dictionary for the file.
Definition reader.cpp:318
void getStructureNoCopy(hipo::structure &structure, int group, int item)
Reads the structure from the current event without copying the event buffer and without copying the s...
Definition reader.cpp:260
void setTags(std::vector< long > tags)
Definition reader.h:249
~reader()
Default destructor.
Definition reader.cpp:62
bool nextInRecord()
Definition reader.cpp:444
reader(const char *file)
Definition reader.h:225
void printWarning()
Prints out warning if the LZ4 library was not linked to the compiled library.
Definition reader.cpp:453
reader()
The constructor for reader, printWarning routine will printout a warning message if the library was n...
Definition reader.cpp:54
void rewind()
Definition reader.h:238
reader(const char *file, std::vector< int > tags)
Definition reader.h:227
void setVerbose(short level=1)
Definition reader.h:250
bool is_open()
Definition reader.h:247
void setTags(int tag)
Definition reader.h:248
bool loadRecord(int irec)
Definition reader.cpp:401
void getStructure(hipo::structure &structure, int group, int item)
Reads the structure from the current event without copying the event buffer.
Definition reader.cpp:244
reader(const reader &r)
Definition reader.h:233
int getEntries()
Definition reader.h:268
bool next()
Advances the event pointer to the next event.
Definition reader.cpp:341
int getNRecords() const
Definition reader.h:264
std::vector< int > getInt(const char *bank, const char *column, int max=-1)
Definition reader.cpp:413
void open(const char *filename)
Open file, if file stream is open, it is closed first.
Definition reader.cpp:78
bool hasNext()
Checks if there are more events in the file to advance to.
Definition reader.cpp:204
void about()
Definition reader.cpp:68
bool gotoRecord(int irec)
Moves the current event pointer to the new record # irec.
Definition reader.cpp:390
bool gotoEvent(int eventNumber)
Moves the pointer of the event to the event number provided.
Definition reader.cpp:362
std::vector< float > getFloat(const char *bank, const char *column, int max=-1)
Definition reader.cpp:428
void readUserConfig(std::map< std::string, std::string > &mapConfig)
Definition reader.cpp:289
Definition reader.h:273
void pull(hipo::record &record, int index)
Definition reader.h:312
void run(std::function< int(int)> &&function, int nthreads)
Definition reader.h:299
void pull(std::vector< hipo::event > &events)
Definition reader.h:317
void open(const char *input)
Definition reader.h:288
virtual ~readerstream()
Definition reader.h:287
void setLimit(long limit)
Definition reader.h:295
hipo::reader & reader()
Definition reader.h:309
hipo::dictionary & dictionary()
Definition reader.h:310
readerstream()
Definition reader.h:285
Definition record.h:119
Definition bank.h:54
Definition utils.h:26
HIPO namespace is used for the classes that read/write files and records.
Definition bank.cpp:45
Definition reader.h:116
int userHeaderLength
Definition reader.h:124
long userRegister
Definition reader.h:126
int filenumber
Definition reader.h:118
int recordCount
Definition reader.h:120
int magicNumber
Definition reader.h:125
int bitInfo
Definition reader.h:122
int indexArrayLength
Definition reader.h:121
int uniqueid
Definition reader.h:117
long firstRecordPosition
Definition reader.h:128
long trailerPosition
Definition reader.h:127
int headerLength
Definition reader.h:119
int version
Definition reader.h:123
Definition reader.h:133
long userWordOne
Definition reader.h:137
long userWordTwo
Definition reader.h:138
int recordLength
Definition reader.h:135
long recordPosition
Definition reader.h:134
int recordEntries
Definition reader.h:136