Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 09:17:16

0001 //==========================================================================
0002 //  AIDA Detector description implementation 
0003 //--------------------------------------------------------------------------
0004 // Copyright (C) Organisation europeenne pour la Recherche nucleaire (CERN)
0005 // All rights reserved.
0006 //
0007 // For the licensing terms see $DD4hepINSTALL/LICENSE.
0008 // For the list of contributors see $DD4hepINSTALL/doc/CREDITS.
0009 //
0010 // Author     : M.Frank
0011 //
0012 //==========================================================================
0013 
0014 // Framework include files
0015 #include "LCIOEventHandler.h"
0016 #include <DD4hep/Printout.h>
0017 #include <DD4hep/Objects.h>
0018 #include <DD4hep/Factories.h>
0019 
0020 #include <IO/LCReader.h>
0021 #include <EVENT/LCCollection.h>
0022 #include <EVENT/SimCalorimeterHit.h>
0023 #include <EVENT/SimTrackerHit.h>
0024 #include <EVENT/MCParticle.h>
0025 
0026 #include <TSystem.h>
0027 #include <TGMsgBox.h>
0028 
0029 // C/C++ include files
0030 #include <stdexcept>
0031 #include <climits>
0032 
0033 using namespace lcio;
0034 using namespace dd4hep;
0035 using namespace EVENT;
0036 using namespace IMPL;
0037 
0038 const void* _fill(const SimTrackerHit* hit, DDEveHit* target)   {
0039   const double* p = hit->getPosition();
0040   target->x = p[0];
0041   target->y = p[1];
0042   target->z = p[2];
0043   target->deposit = hit->getEDep();
0044   return hit;
0045 }
0046 const void* _fill(const SimCalorimeterHit* hit, DDEveHit* target)   {
0047   const float* p = hit->getPosition();
0048   target->x = p[0];
0049   target->y = p[1];
0050   target->z = p[2];
0051   target->deposit = hit->getEnergy();
0052   return hit;
0053 }
0054 
0055 static const void* _convertHitFunc(const LCObject* source, DDEveHit* target)  {
0056   const SimTrackerHit* t = dynamic_cast<const SimTrackerHit*>(source);
0057   if (t && _fill(t,target)) return t;
0058   const SimCalorimeterHit* c = dynamic_cast<const SimCalorimeterHit*>(source);
0059   if (c && _fill(c,target)) return c;
0060   return 0;
0061 }
0062 static const void* _convertParticleFunc(const LCObject* source, DDEveParticle* target)  {
0063   if ( source && target )  {}
0064   return 0;
0065 }
0066 
0067 static void* _create(const char*)  {
0068   EventHandler* eh = new LCIOEventHandler();
0069   return eh;
0070 }
0071 using namespace dd4hep::detail;
0072 DECLARE_CONSTRUCTOR(DD4hep_DDEve_LCIOEventHandler,_create)
0073 
0074 /// Standard constructor
0075 LCIOEventHandler::LCIOEventHandler() : EventHandler(), m_lcReader(0), m_event(0) {
0076   m_lcReader = LCFactory::getInstance()->createLCReader() ;
0077 }
0078 
0079 /// Default destructor
0080 LCIOEventHandler::~LCIOEventHandler()   {
0081   delete m_lcReader;
0082 }
0083 
0084 /// Access the number of events on the current input data source (-1 if no data source connected)
0085 long LCIOEventHandler::numEvents() const   {
0086   if ( hasFile() )  {
0087     return m_lcReader->getNumberOfEvents();
0088   }
0089   return -1;
0090 }
0091 
0092 /// Access to the collection type by name
0093 EventHandler::CollectionType LCIOEventHandler::collectionType(const std::string& /* collection */) const {
0094   return CALO_HIT_COLLECTION;
0095 #if 0
0096   if ( cl == cl_calo ) return CALO_HIT_COLLECTION;
0097   else if ( cl == cl_tracker ) return TRACKER_HIT_COLLECTION;
0098   else if ( cl == cl_particles ) return PARTICLE_COLLECTION;
0099   else return NO_COLLECTION;
0100 #endif
0101 }
0102 
0103 /// Call functor on hit collection
0104 size_t LCIOEventHandler::collectionLoop(const std::string& collection, DDEveHitActor& actor)   {
0105   Branches::const_iterator ibr = m_branches.find(collection);
0106   if ( ibr != m_branches.end() )   {
0107     LCCollection* c = (*ibr).second;
0108     if ( c )  {
0109       DDEveHit hit;
0110       int n = c->getNumberOfElements();
0111       actor.setSize(n);
0112       for(int i=0; i<n; ++i)  {
0113         LCObject* ptr = c->getElementAt(i);
0114         if ( _convertHitFunc(ptr,&hit) )    {
0115           actor(hit);
0116         }
0117       }
0118       return n;
0119     }
0120   }
0121   return 0;
0122 }
0123 
0124 /// Loop over collection and extract particle data
0125 size_t LCIOEventHandler::collectionLoop(const std::string& collection, DDEveParticleActor& actor)    {
0126   Branches::const_iterator ibr = m_branches.find(collection);
0127   if ( ibr != m_branches.end() )   {
0128     LCCollection* c = (*ibr).second;
0129     if ( c )  {
0130       DDEveParticle part;
0131       int n = c->getNumberOfElements();
0132       actor.setSize(n);
0133       for(int i=0; i<n; ++i)  {
0134         LCObject* ptr = c->getElementAt(i);
0135         if ( _convertParticleFunc(ptr,&part) )    {
0136           actor(part);
0137         }
0138       }
0139       return n;
0140     }
0141   }
0142   return 0;
0143 }
0144 
0145 /// Open new data file
0146 bool LCIOEventHandler::Open(const std::string&, const std::string& name)   {
0147   if ( m_hasFile ) m_lcReader->close();
0148   m_hasFile = false;
0149   m_hasEvent = false;
0150   m_event = 0;
0151   m_branches.clear();
0152   m_lcReader->open(name);
0153   m_hasFile = true;
0154   return true;
0155 }
0156 
0157 /// Load the next event
0158 bool LCIOEventHandler::NextEvent()   {
0159   m_data.clear();
0160   m_hasEvent = false;
0161   m_branches.clear();
0162   m_data.clear();
0163   if ( hasFile() )  {
0164     m_event = m_lcReader->readNextEvent();
0165     if ( m_event )   {
0166       typedef std::vector<std::string> _S;
0167       const _S* collnames = m_event->getCollectionNames();
0168       for( _S::const_iterator i = collnames->begin(); i != collnames->end(); ++i) {
0169         LCCollection* c = m_event->getCollection(*i);
0170         m_data[c->getTypeName()].push_back(std::make_pair((*i).c_str(),c->getNumberOfElements()));
0171         m_branches[*i] = c;
0172       }
0173       m_hasEvent = true;
0174       return 1;
0175     }
0176     throw std::runtime_error("+++ EventHandler::readEvent: Failed to read event");
0177   }
0178   throw std::runtime_error("+++ EventHandler::readEvent: No file open!");
0179 }
0180 
0181 /// Load the previous event
0182 bool LCIOEventHandler::PreviousEvent()   {
0183   throw std::runtime_error("+++ This version of the LCIO reader can only access files sequentially!\n"
0184                            "+++ Access to the previous event is not supported.");
0185 }
0186 
0187 /// Goto a specified event in the file
0188 bool LCIOEventHandler::GotoEvent(long /* event_number */)   {
0189   throw std::runtime_error("+++ This version of the LCIO reader can only access files sequentially!\n"
0190                            "+++ Random access is not supported.");
0191 }