DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
LCIOFileReader.cpp
Go to the documentation of this file.
1 //==========================================================================
2 // AIDA Detector description implementation
3 //--------------------------------------------------------------------------
4 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
5 // All rights reserved.
6 //
7 // For the licensing terms see $DD4hepINSTALL/LICENSE.
8 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
9 //
10 // Author : M.Frank
11 //
12 //==========================================================================
13 
24 #ifndef DD4HEP_DDG4_LCIOFILEREADER_H
25 #define DD4HEP_DDG4_LCIOFILEREADER_H
26 
27 // Framework include files
28 #include "LCIOEventReader.h"
29 
30 #include <DDG4/EventParameters.h>
31 
32 #include <lcio.h>
33 
34 using namespace lcio ;
35 
36 // Forward declarations
37 namespace IO { class LCReader; }
38 
40 namespace dd4hep {
41 
43  namespace sim {
44 
46  template <class T=EVENT::LCParameters> void EventParameters::ingestParameters(T const& source) {
47  EVENT::StringVec intKeys;
48  EVENT::StringVec floatKeys;
49  EVENT::StringVec stringKeys;
50  source.getIntKeys(intKeys);
51  source.getFloatKeys(floatKeys);
52  source.getStringKeys(stringKeys);
53  for(auto const& key: intKeys) {
54  EVENT::IntVec intVec;
55  source.getIntVals(key,intVec);
56  m_intValues[key] = std::move(intVec);
57  }
58  for(auto const& key: floatKeys) {
59  EVENT::FloatVec floatVec;
60  source.getFloatVals(key,floatVec);
61  m_fltValues[key] = std::move(floatVec);
62  }
63  for(auto const& key: stringKeys) {
64  EVENT::StringVec stringVec;
65  source.getStringVals(key,stringVec);
66  m_strValues[key] = std::move(stringVec);
67  }
68  }
69 
71 
79  protected:
81  IO::LCReader* m_reader;
82  std::string m_collectionName;
83  public:
85  LCIOFileReader(const std::string& nam);
87  virtual ~LCIOFileReader();
88 
90  virtual EventReaderStatus readParticleCollection(int event_number, EVENT::LCCollection** particles);
91  virtual EventReaderStatus moveToEvent(int event_number);
92  virtual EventReaderStatus skipEvent() { return EVENT_READER_OK; }
93  virtual EventReaderStatus setParameters(std::map< std::string, std::string >& parameters);
94  };
95  }
96 }
97 #endif // DD4HEP_DDG4_LCIOFILEREADER_H
98 
99 #include <DD4hep/Printout.h>
100 #include <DDG4/Factories.h>
101 #include <UTIL/ILDConf.h>
102 
103 using namespace dd4hep::sim;
104 
105 // Factory entry
107 
108 dd4hep::sim::LCIOFileReader::LCIOFileReader(const std::string& nam)
110 : LCIOEventReader(nam)
111 , m_collectionName(LCIO::MCPARTICLE)
112 {
113  m_reader = ::lcio::LCFactory::getInstance()->createLCReader(LCReader::directAccess);
114  printout(INFO,"LCIOFileReader","Created file reader. Try to open input %s",nam.c_str());
115  m_reader->open(nam);
116  m_directAccess = false;
117 }
118 
121  dd4hep::detail::deletePtr(m_reader);
122 }
123 
124 
128  // ::lcio::LCEvent* evt = m_reader->readEvent(/*runNumber*/ 0, event_number);
129  // fg: direct access does not work if run number is different from 0 and/or event numbers are not stored consecutively
130  if( m_currEvent == 0 && event_number != 0 ) {
131  m_reader->skipNEvents( event_number ) ;
132  printout(INFO,"LCIOFileReader::moveToEvent","Skipping the first %d events ", event_number );
133  printout(INFO,"LCIOFileReader::moveToEvent","Event number before skipping: %d", m_currEvent );
134  m_currEvent = event_number;
135  printout(INFO,"LCIOFileReader::moveToEvent","Event number after skipping: %d", m_currEvent );
136  }
137  return EVENT_READER_OK;
138 }
139 
142 dd4hep::sim::LCIOFileReader::readParticleCollection(int /*event_number*/, EVENT::LCCollection** particles) {
143 
144  ::lcio::LCEvent* evt = m_reader->readNextEvent(); // simply read the events sequentially
145  ++m_currEvent ;
146 
147  if ( evt ) {
148  *particles = evt->getCollection(m_collectionName);
149  if ( *particles ) {
150  printout(INFO,"LCIOFileReader","read collection %s from event %d in run %d ",
151  m_collectionName.c_str(), evt->getEventNumber(), evt->getRunNumber());
152 
153  // Create input event parameters context
154  try {
155  Geant4Context* ctx = context();
156  EventParameters *parameters = new EventParameters();
157  parameters->setRunNumber(evt->getRunNumber());
158  parameters->setEventNumber(evt->getEventNumber());
159  parameters->ingestParameters(evt->parameters());
160  ctx->event().addExtension<EventParameters>(parameters);
161  }
162  catch(std::exception &)
163  {
164  }
165  return EVENT_READER_OK;
166  }
167  }
168  return EVENT_READER_EOF;
169 }
170 
174 dd4hep::sim::LCIOFileReader::setParameters( std::map< std::string, std::string > & parameters ) {
175  _getParameterValue( parameters, "MCParticleCollectionName", m_collectionName, std::string(LCIO::MCPARTICLE));
176  return EVENT_READER_OK;
177 }
dd4hep::sim::LCIOFileReader::m_collectionName
std::string m_collectionName
Definition: LCIOFileReader.cpp:82
dd4hep::sim::LCIOFileReader::moveToEvent
virtual EventReaderStatus moveToEvent(int event_number)
moveToSpecifiedEvent, a.k.a. skipNEvents
Definition: LCIOFileReader.cpp:127
dd4hep::sim::Geant4EventReader::EventReaderStatus
EventReaderStatus
Status codes of the event reader object. Anything with NOT low-bit set is an error.
Definition: Geant4InputAction.h:68
dd4hep::exception
void exception(const std::string &src, const std::string &msg)
Definition: RootDictionary.h:69
dd4hep::sim::LCIOEventReader
Base class to read lcio files.
Definition: LCIOEventReader.h:35
dd4hep::sim::EventParameters::setRunNumber
void setRunNumber(int runNumber)
Set the event parameters.
Definition: EventParameters.cpp:20
LCIOFileReader
Plugin to read lcio files.
dd4hep::sim::LCIOFileReader::readParticleCollection
virtual EventReaderStatus readParticleCollection(int event_number, EVENT::LCCollection **particles)
Read an event and fill a vector of MCParticles.
Definition: LCIOFileReader.cpp:142
dd4hep::sim::EventParameters::setEventNumber
void setEventNumber(int eventNumber)
Definition: EventParameters.cpp:23
DECLARE_GEANT4_EVENT_READER_NS
#define DECLARE_GEANT4_EVENT_READER_NS(name_space, name)
Plugin defintion to create event reader objects.
Definition: Factories.h:240
dd4hep::sim::Geant4Context::event
Geant4Event & event() const
Access the geant4 event – valid only between BeginEvent() and EndEvent()!
Definition: Geant4Context.cpp:84
m_reader
UriReader * m_reader
Pointer to URI reader.
Definition: DocumentHandler.cpp:155
dd4hep::sim::LCIOFileReader::LCIOFileReader
LCIOFileReader(const std::string &nam)
Initializing constructor.
Definition: LCIOFileReader.cpp:109
LCIOEventReader.h
dd4hep::sim::LCIOFileReader::skipEvent
virtual EventReaderStatus skipEvent()
Skip event. To be implemented for sequential sources.
Definition: LCIOFileReader.cpp:92
EventParameters.h
dd4hep::sim::EventParameters::ingestParameters
void ingestParameters(T const &source)
Copy the parameters from source.
Definition: HepMC3FileReader.cpp:41
IO
LCIO namespace. See http://lcio.desy.de.
Definition: LCIOFileReader.cpp:37
dd4hep::sim::LCIOFileReader::m_reader
IO::LCReader * m_reader
Reference to reader object.
Definition: LCIOFileReader.cpp:81
dd4hep::sim::Geant4EventReader::m_directAccess
bool m_directAccess
Flag if direct event access is supported. To be explicitly set by subclass constructors.
Definition: Geant4InputAction.h:81
dd4hep::sim::Geant4Event::addExtension
void * addExtension(unsigned long long int k, ExtensionEntry *e)
Add an extension object to the detector element.
Definition: Geant4Context.h:140
Factories.h
key
unsigned char key
Definition: AlignmentsCalculator.cpp:69
dd4hep::sim
Namespace for the Geant4 based simulation part of the AIDA detector description toolkit.
Definition: Geant4Output2EDM4hep.cpp:49
dd4hep::sim::EventParameters
Event extension to pass input event data to output event.
Definition: EventParameters.h:31
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
dd4hep::sim::LCIOFileReader::setParameters
virtual EventReaderStatus setParameters(std::map< std::string, std::string > &parameters)
Definition: LCIOFileReader.cpp:174
Printout.h
dd4hep::sim::LCIOFileReader::~LCIOFileReader
virtual ~LCIOFileReader()
Default destructor.
Definition: LCIOFileReader.cpp:120
dd4hep::sim::Geant4Context
Generic context to extend user, run and event information.
Definition: Geant4Context.h:201