MarlinMT  0.1.0
LCIOFileSource.cc
Go to the documentation of this file.
1 #ifndef MARLINMT_LCIOFILESOURCE_h
2 #define MARLINMT_LCIOFILESOURCE_h 1
3 
5 
6 // -- marlinmt headers
9 #include <marlinmt/Logging.h>
10 #include <marlinmt/EventStore.h>
11 #include <marlinmt/RunHeader.h>
12 #include <jenkinsHash.h>
13 
14 // -- lcio headers
15 #include <EVENT/LCEvent.h>
16 #include <EVENT/LCRunHeader.h>
17 #include <MT/LCReader.h>
18 #include <MT/LCReaderListener.h>
19 
20 // -- std headers
21 #include <functional>
22 
23 using namespace std::placeholders ;
24 
25 namespace marlinmt {
26 
31  using FileReader = MT::LCReader ;
32  using FileReaderPtr = std::shared_ptr<FileReader> ;
33 
34  public:
35  LCIOFileSource() ;
36  ~LCIOFileSource() = default ;
37 
38  // from base
39  void initialize() override ;
40  bool readOne() override ;
41 
42  private:
43  void onLCEventRead( std::shared_ptr<EVENT::LCEvent> event ) ;
44  void onLCRunHeaderRead( std::shared_ptr<EVENT::LCRunHeader> rhdr ) ;
45 
46  private:
47  StringVectorParameter _inputFileNames {*this, "LCIOInputFiles",
48  "The list of LCIO input files" } ;
49 
50  IntParameter _maxRecordNumber {*this, "MaxRecordNumber",
51  "The maximum number of records to read", 0 } ;
52 
53  IntParameter _skipNEvents {*this, "SkipNEvents",
54  "The number of events to skip on file open", 0 } ;
55 
56  StringVectorParameter _readCollectionNames {*this, "LCIOReadCollectionNames",
57  "An optional list of LCIO collections to read from event" } ;
58 
59  BoolParameter _lazyUnpack {*this, "LazyUnpack",
60  "Set to true to perform a lazy unpacking after reading out an event", false } ;
61 
63  ReaderListener _listener {} ;
65  FileReaderPtr _fileReader {nullptr} ;
67  int _currentReadRecords {0} ;
68  };
69 
70  //--------------------------------------------------------------------------
71  //--------------------------------------------------------------------------
72 
73  LCIOFileSource::LCIOFileSource() :
74  DataSourcePlugin("LCIOReader") {
75  setDescription( "Read LCIO events and run headers from files on disk" ) ;
76  }
77 
78  //--------------------------------------------------------------------------
79 
82  auto flag = FileReader::directAccess ;
83  if( _lazyUnpack ) {
84  flag |= FileReader::lazyUnpack ;
85  }
86  _fileReader = std::make_shared<FileReader>( flag ) ;
88  _listener.onEventRead( std::bind( &LCIOFileSource::processEvent, this, _1 ) ) ;
89 
90  if( _inputFileNames.empty() ) {
91  throw Exception( "LCIOFileSource::init: LCIO input file list is empty" ) ;
92  }
93  _fileReader->open( _inputFileNames ) ;
94  if ( _skipNEvents > 0 ) {
95  log<WARNING>() << " --- Will skip first " << _skipNEvents << " event(s)" << std::endl ;
96  _fileReader->skipNEvents( _skipNEvents ) ;
97  }
98  if ( not _readCollectionNames.empty() ) {
99  log<WARNING>()
100  << " *********** Parameter LCIOReadCollectionNames given - will only read the following collections: **** "
101  << std::endl ;
102  for( auto collection : _readCollectionNames ) {
103  log<WARNING>() << " " << collection << std::endl ;
104  }
105  log<WARNING>()
106  << " *************************************************************************************************** " << std::endl ;
107  _fileReader->setReadCollectionNames( _readCollectionNames ) ;
108  }
109  }
110 
111  //--------------------------------------------------------------------------
112 
114  try {
115  _fileReader->readNextRecord( &_listener ) ;
118  return false ;
119  }
120  }
121  catch( IO::EndOfDataException &e ) {
122  return false ;
123  }
124  return true ;
125  }
126 
128 
129 }
130 
131 #endif
void initialize() override
Init data source.
void processRunHeader(std::shared_ptr< RunHeader > rhdr)
Must be called by daughter classes in readStream() to process an event in the framework.
std::shared_ptr< FileReader > FileReaderPtr
FileReaderPtr _fileReader
The current number of read records.
#define MARLINMT_DECLARE_DATASOURCE_NAME(Class, NameStr)
Definition: PluginManager.h:45
void processEvent(std::shared_ptr< EventStore > event)
Must be called by daughter classes in readStream() to process an event in the framework.
DataSourcePlugin class Responsible for reading/getting LCEvent and LCRunHeader in the framework for f...
ReaderListener class.
void setDescription(const std::string &desc)
Set the component description.
Definition: Component.cc:41
void onEventRead(EventFunction func)
Set the callback function to process on event read.
LCIOFileSource class.
StringVectorParameter _readCollectionNames
BoolParameter _lazyUnpack
The LCIO file listener.
virtual void initialize() override
Init data source.
void onRunHeaderRead(RunHeaderFunction func)
Set the callback function to process on run info read.
StringVectorParameter _inputFileNames
ReaderListener _listener
The LCIO file reader.
Exception class.
Definition: Exceptions.h:60
bool readOne() override
Read one record from the input stream.