DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
LCIOEventHandler.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 
14 // Framework include files
15 #include "LCIOEventHandler.h"
16 #include <DD4hep/Printout.h>
17 #include <DD4hep/Objects.h>
18 #include <DD4hep/Factories.h>
19 
20 #include <IO/LCReader.h>
21 #include <EVENT/LCCollection.h>
22 #include <EVENT/SimCalorimeterHit.h>
23 #include <EVENT/SimTrackerHit.h>
24 #include <EVENT/MCParticle.h>
25 
26 #include <TSystem.h>
27 #include <TGMsgBox.h>
28 
29 // C/C++ include files
30 #include <stdexcept>
31 #include <climits>
32 
33 using namespace lcio;
34 using namespace dd4hep;
35 using namespace EVENT;
36 using namespace IMPL;
37 
38 const void* _fill(const SimTrackerHit* hit, DDEveHit* target) {
39  const double* p = hit->getPosition();
40  target->x = p[0];
41  target->y = p[1];
42  target->z = p[2];
43  target->deposit = hit->getEDep();
44  return hit;
45 }
46 const void* _fill(const SimCalorimeterHit* hit, DDEveHit* target) {
47  const float* p = hit->getPosition();
48  target->x = p[0];
49  target->y = p[1];
50  target->z = p[2];
51  target->deposit = hit->getEnergy();
52  return hit;
53 }
54 
55 static const void* _convertHitFunc(const LCObject* source, DDEveHit* target) {
56  const SimTrackerHit* t = dynamic_cast<const SimTrackerHit*>(source);
57  if (t && _fill(t,target)) return t;
58  const SimCalorimeterHit* c = dynamic_cast<const SimCalorimeterHit*>(source);
59  if (c && _fill(c,target)) return c;
60  return 0;
61 }
62 static const void* _convertParticleFunc(const LCObject* source, DDEveParticle* target) {
63  if ( source && target ) {}
64  return 0;
65 }
66 
67 static void* _create(const char*) {
68  EventHandler* eh = new LCIOEventHandler();
69  return eh;
70 }
71 using namespace dd4hep::detail;
72 DECLARE_CONSTRUCTOR(DD4hep_DDEve_LCIOEventHandler,_create)
73 
74 LCIOEventHandler::LCIOEventHandler() : EventHandler(), m_lcReader(0), m_event(0) {
76  m_lcReader = LCFactory::getInstance()->createLCReader() ;
77 }
78 
80 LCIOEventHandler::~LCIOEventHandler() {
81  delete m_lcReader;
82 }
83 
86  if ( hasFile() ) {
87  return m_lcReader->getNumberOfEvents();
88  }
89  return -1;
90 }
91 
93 EventHandler::CollectionType LCIOEventHandler::collectionType(const std::string& /* collection */) const {
94  return CALO_HIT_COLLECTION;
95 #if 0
96  if ( cl == cl_calo ) return CALO_HIT_COLLECTION;
97  else if ( cl == cl_tracker ) return TRACKER_HIT_COLLECTION;
98  else if ( cl == cl_particles ) return PARTICLE_COLLECTION;
99  else return NO_COLLECTION;
100 #endif
101 }
102 
104 size_t LCIOEventHandler::collectionLoop(const std::string& collection, DDEveHitActor& actor) {
105  Branches::const_iterator ibr = m_branches.find(collection);
106  if ( ibr != m_branches.end() ) {
107  LCCollection* c = (*ibr).second;
108  if ( c ) {
109  DDEveHit hit;
110  int n = c->getNumberOfElements();
111  actor.setSize(n);
112  for(int i=0; i<n; ++i) {
113  LCObject* ptr = c->getElementAt(i);
114  if ( _convertHitFunc(ptr,&hit) ) {
115  actor(hit);
116  }
117  }
118  return n;
119  }
120  }
121  return 0;
122 }
123 
125 size_t LCIOEventHandler::collectionLoop(const std::string& collection, DDEveParticleActor& actor) {
126  Branches::const_iterator ibr = m_branches.find(collection);
127  if ( ibr != m_branches.end() ) {
128  LCCollection* c = (*ibr).second;
129  if ( c ) {
130  DDEveParticle part;
131  int n = c->getNumberOfElements();
132  actor.setSize(n);
133  for(int i=0; i<n; ++i) {
134  LCObject* ptr = c->getElementAt(i);
135  if ( _convertParticleFunc(ptr,&part) ) {
136  actor(part);
137  }
138  }
139  return n;
140  }
141  }
142  return 0;
143 }
144 
146 bool LCIOEventHandler::Open(const std::string&, const std::string& name) {
147  if ( m_hasFile ) m_lcReader->close();
148  m_hasFile = false;
149  m_hasEvent = false;
150  m_event = 0;
151  m_branches.clear();
152  m_lcReader->open(name);
153  m_hasFile = true;
154  return true;
155 }
156 
159  m_data.clear();
160  m_hasEvent = false;
161  m_branches.clear();
162  m_data.clear();
163  if ( hasFile() ) {
164  m_event = m_lcReader->readNextEvent();
165  if ( m_event ) {
166  typedef std::vector<std::string> _S;
167  const _S* collnames = m_event->getCollectionNames();
168  for( _S::const_iterator i = collnames->begin(); i != collnames->end(); ++i) {
169  LCCollection* c = m_event->getCollection(*i);
170  m_data[c->getTypeName()].push_back(std::make_pair((*i).c_str(),c->getNumberOfElements()));
171  m_branches[*i] = c;
172  }
173  m_hasEvent = true;
174  return 1;
175  }
176  throw std::runtime_error("+++ EventHandler::readEvent: Failed to read event");
177  }
178  throw std::runtime_error("+++ EventHandler::readEvent: No file open!");
179 }
180 
183  throw std::runtime_error("+++ This version of the LCIO reader can only access files sequentially!\n"
184  "+++ Access to the previous event is not supported.");
185 }
186 
188 bool LCIOEventHandler::GotoEvent(long /* event_number */) {
189  throw std::runtime_error("+++ This version of the LCIO reader can only access files sequentially!\n"
190  "+++ Random access is not supported.");
191 }
dd4hep::DDEveParticleActor
Event data actor base class for particles. Used to extract data from concrete classes.
Definition: EventHandler.h:55
dd4hep::EventHandler::NO_COLLECTION
@ NO_COLLECTION
Definition: EventHandler.h:70
dd4hep::DDEveHit
DDEve event classes: Basic hit.
Definition: DDEveEventData.h:29
dd4hep::LCIOEventHandler::m_branches
Branches m_branches
Branch map.
Definition: LCIOEventHandler.h:49
Objects.h
dd4hep::EventHandler::m_hasEvent
bool m_hasEvent
Flag to indicate that an event is loaded.
Definition: EventHandler.h:86
dd4hep::EventHandler
Event handler base class: Interface to all DDEve I/O actions.
Definition: EventHandler.h:67
dd4hep::LCIOEventHandler::Open
virtual bool Open(const std::string &type, const std::string &file_name)
Open new data file.
Definition: LCIOEventHandler.cpp:146
dd4hep::EventHandler::TRACKER_HIT_COLLECTION
@ TRACKER_HIT_COLLECTION
Definition: EventHandler.h:74
dd4hep::LCIOEventHandler::numEvents
virtual long numEvents() const
Access the number of events on the current input data source (-1 if no data source connected)
Definition: LCIOEventHandler.cpp:85
dd4hep::DDEveHitActor
Event data actor base class for hits. Used to extract data from concrete classes.
Definition: EventHandler.h:43
Factories.h
EVENT
LCIO namespace. See http://lcio.desy.de.
Definition: LCIOEventReader.h:20
dd4hep::EventHandler::m_hasFile
bool m_hasFile
Flag to indicate that a file is opened.
Definition: EventHandler.h:84
dd4hep::LCIOEventHandler::m_event
lcio::LCEvent * m_event
Reference to the current LCIO event.
Definition: LCIOEventHandler.h:45
dd4hep::LCIOEventHandler::GotoEvent
virtual bool GotoEvent(long event_number)
Goto a specified event in the file.
Definition: LCIOEventHandler.cpp:188
DECLARE_CONSTRUCTOR
#define DECLARE_CONSTRUCTOR(name, func)
Definition: Factories.h:286
dd4hep::DDEveHit::x
float x
Hit position.
Definition: DDEveEventData.h:34
dd4hep::EventHandler::hasFile
virtual bool hasFile() const
Check if a data file is connected to the handler.
Definition: EventHandler.h:95
dd4hep::detail
DD4hep internal namespace.
Definition: Alignments.h:32
dd4hep::DDEveHit::deposit
float deposit
Energy deposit.
Definition: DDEveEventData.h:36
dd4hep::LCIOEventHandler::NextEvent
virtual bool NextEvent()
User overloadable function: Load the next event.
Definition: LCIOEventHandler.cpp:158
LCIOEventHandler.h
dd4hep::EventHandler::CALO_HIT_COLLECTION
@ CALO_HIT_COLLECTION
Definition: EventHandler.h:73
dd4hep::EventHandler::CollectionType
CollectionType
Definition: EventHandler.h:69
dd4hep::DDEveParticleActor::setSize
virtual void setSize(size_t)
Definition: EventHandler.h:58
dd4hep::EventHandler::PARTICLE_COLLECTION
@ PARTICLE_COLLECTION
Definition: EventHandler.h:71
dd4hep::LCIOEventHandler::m_data
TypedEventCollections m_data
Data collection map.
Definition: LCIOEventHandler.h:51
_fill
const void * _fill(const SimTrackerHit *hit, DDEveHit *target)
Definition: LCIOEventHandler.cpp:38
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
dd4hep::LCIOEventHandler
LCIO input event handler for DDEve: Interface class for event I/O.
Definition: LCIOEventHandler.h:37
dd4hep::LCIOEventHandler::PreviousEvent
virtual bool PreviousEvent()
User overloadable function: Load the previous event.
Definition: LCIOEventHandler.cpp:182
dd4hep::DDEveHit::y
float y
Definition: DDEveEventData.h:34
IMPL
LCIO namespace. See http://lcio.desy.de.
Definition: DD4hepGroups.h:79
dd4hep::LCIOEventHandler::m_lcReader
lcio::LCReader * m_lcReader
Reference to data file reader.
Definition: LCIOEventHandler.h:43
dd4hep::DDEveHit::z
float z
Definition: DDEveEventData.h:34
dd4hep::DDEveParticle
Data structure to store the MC particle information.
Definition: DDEveEventData.h:56
Printout.h
dd4hep::LCIOEventHandler::collectionType
virtual CollectionType collectionType(const std::string &collection) const
Access to the collection type by name.
Definition: LCIOEventHandler.cpp:93
dd4hep::DDEveHitActor::setSize
virtual void setSize(size_t)
Definition: EventHandler.h:46
dd4hep::LCIOEventHandler::collectionLoop
virtual size_t collectionLoop(const std::string &collection, DDEveHitActor &actor)
Call functor on hit collection.
Definition: LCIOEventHandler.cpp:104