MarlinMT  0.1.0
StdHepFileSource.cc
Go to the documentation of this file.
1 
2 // -- marlinmt headers
6 #include <marlinmt/Logging.h>
7 #include <marlinmt/EventStore.h>
8 #include <marlinmt/RunHeader.h>
9 #include <jenkinsHash.h>
10 
11 // -- lcio headers
12 #include <IMPL/LCEventImpl.h>
13 #include <IMPL/LCRunHeaderImpl.h>
14 #include <UTIL/LCStdHepRdr.h>
15 #include <UTIL/LCTOOLS.h>
16 
17 // -- std headers
18 #include <functional>
19 
20 using namespace std::placeholders ;
21 
22 namespace marlinmt {
23 
28  using FileReader = std::shared_ptr<UTIL::LCStdHepRdr> ;
29 
30  public:
32  ~StdHepFileSource() = default ;
33 
34  // from DataSourcePlugin
35  void initialize() override ;
36  bool readOne() override ;
37 
38  private:
39  StringParameter _fileName {*this, "StdHepFileName",
40  "The StdHep input file name" } ;
41 
42  IntParameter _maxRecordNumber {*this, "MaxRecordNumber",
43  "The maximum number of events to read", 0 } ;
44 
45  StringParameter _collectionName {*this, "CollectionName",
46  "The name of the MCParticle collection to add in the event", "MCParticle" } ;
47 
49  FileReader _fileReader {nullptr} ;
51  bool _isFirstEvent {true} ;
53  int _currentReadEvents {0} ;
54  };
55 
56  //--------------------------------------------------------------------------
57  //--------------------------------------------------------------------------
58 
59  StdHepFileSource::StdHepFileSource() :
60  DataSourcePlugin("StdHepReader") {
61  setDescription( "Reads StdHep files as input and creates LCIO events with MCParticle collections" ) ;
62  }
63 
64  //--------------------------------------------------------------------------
65 
68  // create the file reader
69  _fileReader = std::make_shared<FileReader::element_type>( _fileName.get().c_str() ) ;
70  }
71 
72  //--------------------------------------------------------------------------
73 
75  // first call is a run header
76  if( _isFirstEvent ) {
77  auto rhdr = std::make_shared<RunHeader>() ;
78  rhdr->setDescription( " Events read from stdhep input file: " + _fileName.get() ) ;
79  rhdr->setRunNumber( 0 ) ;
80  processRunHeader( rhdr ) ;
81  _isFirstEvent = false ;
82  return true ;
83  }
84  // next calls are events from the stdhep reader
85  auto collection = _fileReader->readEvent() ;
86  if( nullptr == collection ) {
87  return false ;
88  }
90  delete collection ;
91  return false ;
92  }
93  auto event = std::make_shared<IMPL::LCEventImpl>() ;
94  event->setRunNumber( 0 ) ;
95  event->setEventNumber( _currentReadEvents ) ;
96  event->addCollection( collection, _collectionName ) ;
97  auto store = std::make_shared<EventStore>() ;
98  store->setEvent( event ) ;
99  // generate the event unique id
100  auto evtn = event->getEventNumber() ;
101  auto runn = event->getRunNumber() ;
102  unsigned char * c = (unsigned char *) &evtn ;
103  unsigned int uid = jenkins_hash( c, sizeof evtn, 0) ;
104  c = (unsigned char *) &runn ;
105  uid = jenkins_hash( c, sizeof runn, 0) ;
106  store->setUID( uid ) ;
107  processEvent( store ) ;
109  return true ;
110  }
111 
113 
114 }
std::shared_ptr< UTIL::LCStdHepRdr > FileReader
bool _isFirstEvent
The current number of read events.
void processRunHeader(std::shared_ptr< RunHeader > rhdr)
Must be called by daughter classes in readStream() to process an event in the framework.
bool readOne() override
Read one record from the input stream.
StdHepFileSource class.
#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.
unsigned jenkins_hash(unsigned char *k, unsigned length, unsigned initval)
Definition: jenkinsHash.h:94
StringParameter _collectionName
The stdhep file reader.
DataSourcePlugin class Responsible for reading/getting LCEvent and LCRunHeader in the framework for f...
void setDescription(const std::string &desc)
Set the component description.
Definition: Component.cc:41
void initialize() override
Init data source.
virtual void initialize() override
Init data source.
T get() const
Get the parameter value.
Definition: Parameter.h:556
FileReader _fileReader
Whether we are processing the first event.