Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:14:12

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 <DDEve/GenericEventHandler.h>
0016 #include <DD4hep/Primitives.h>
0017 #include <DD4hep/Factories.h>
0018 #include <DD4hep/Plugins.h>
0019 #include <stdexcept>
0020 
0021 /// ROOT include files
0022 #include <TROOT.h>
0023 #include <TGMsgBox.h>
0024 #include <TSystem.h>
0025 #include <climits>
0026 
0027 using namespace dd4hep;
0028 
0029 ClassImp(GenericEventHandler)
0030 
0031 /// Standard constructor
0032 GenericEventHandler::GenericEventHandler() : m_current(0) {
0033 }
0034 
0035 /// Default destructor
0036 GenericEventHandler::~GenericEventHandler()  {
0037   m_subscriptions.clear();
0038   detail::deletePtr(m_current);
0039 }
0040 
0041 EventHandler* GenericEventHandler::current() const   {
0042   if ( m_current )  {
0043     return m_current;
0044   }
0045   throw std::runtime_error("Invalid event handler");
0046 }
0047 
0048 /// Notfy all subscribers
0049 void GenericEventHandler::NotifySubscribers(void (EventConsumer::*pmf)(EventHandler&))   {
0050   for(Subscriptions::iterator i=m_subscriptions.begin(); i!=m_subscriptions.end();++i)
0051     ((*i)->*pmf)(*this);
0052 }
0053 
0054 /// Subscribe to notification of new data present
0055 void GenericEventHandler::Subscribe(EventConsumer* consumer)  {
0056   m_subscriptions.insert(consumer);
0057 }
0058 
0059 /// Unsubscribe from notification of new data present
0060 void GenericEventHandler::Unsubscribe(EventConsumer* consumer)  {
0061   Subscriptions::iterator i=m_subscriptions.find(consumer);
0062   if ( i != m_subscriptions.end() ) m_subscriptions.erase(i);
0063 }
0064 
0065 /// Access the number of events on the current input data source (-1 if no data source connected)
0066 long GenericEventHandler::numEvents() const   {
0067   return current()->numEvents();
0068 }
0069 
0070 /// Access the data source name
0071 std::string GenericEventHandler::datasourceName() const {
0072   return current()->datasourceName();
0073 }
0074 
0075 /// Access to the collection type by name
0076 EventHandler::CollectionType GenericEventHandler::collectionType(const std::string& collection) const   {
0077   if ( m_current && m_current->hasEvent() )  {
0078     return m_current->collectionType(collection);
0079   }
0080   return NO_COLLECTION;
0081 }
0082 
0083 /// Loop over collection and extract data
0084 std::size_t GenericEventHandler::collectionLoop(const std::string& collection, DDEveHitActor& actor) {
0085   if ( m_current && m_current->hasEvent() )  {
0086     return m_current->collectionLoop(collection,actor);
0087   }
0088   return 0;
0089 }
0090 
0091 /// Loop over collection and extract particle data
0092 std::size_t GenericEventHandler::collectionLoop(const std::string& collection, DDEveParticleActor& actor)    {
0093   if ( m_current && m_current->hasEvent() )  {
0094     return m_current->collectionLoop(collection,actor);
0095   }
0096   return 0;
0097 }
0098 
0099 /// Open a new event data file
0100 bool GenericEventHandler::Open(const std::string& file_type, const std::string& file_name)   {
0101   std::size_t idx = file_name.find("lcio");
0102   std::size_t idr = file_name.find("root");
0103   std::string err;
0104   m_hasFile = false;
0105   m_hasEvent = false;
0106   try  {
0107     detail::deletePtr(m_current);
0108     //  prefer event handler configured in xml
0109     if ( file_type.find("FCC") != std::string::npos ) {
0110       m_current = (EventHandler*)PluginService::Create<void*>("DD4hep_DDEve_FCCEventHandler",(const char*)0);
0111     }
0112     // fall back to defaults according to file ending
0113     else if ( idx != std::string::npos )   {
0114       m_current = (EventHandler*)PluginService::Create<void*>("DD4hep_DDEve_LCIOEventHandler",(const char*)0);
0115     }
0116     else if ( idr != std::string::npos )   {
0117       m_current = (EventHandler*)PluginService::Create<void*>("DD4hep_DDEve_DDG4EventHandler",(const char*)0);
0118     }
0119     else   {
0120       throw std::runtime_error("Attempt to open file:"+file_name+" of unknown type:"+file_type);
0121     }
0122     if ( m_current )   {
0123       if ( m_current->Open(file_type, file_name) )   {
0124         m_hasFile = true;
0125         NotifySubscribers(&EventConsumer::OnFileOpen);
0126         return true;
0127       }
0128       err = "+++ Failed to open the data file:"+file_name;
0129       detail::deletePtr(m_current);   
0130     }
0131     else  {
0132       err = "+++ Failed to create fikle reader for file '"+file_name+"' of type '"+file_type+"'";
0133     }
0134   }
0135   catch(const std::exception& e)  {
0136     err = "\nAn exception occurred \n"
0137       "while opening event data:\n" + std::string(e.what()) + "\n\n";
0138   }
0139   std::string path = TString::Format("%s/stop_t.xpm", TROOT::GetIconPath().Data()).Data();
0140   const TGPicture* pic = gClient->GetPicture(path.c_str());
0141   new TGMsgBox(gClient->GetRoot(),0,"Failed to open event data",err.c_str(),pic,
0142                kMBDismiss,0,kVerticalFrame,kTextLeft|kTextCenterY);
0143   return false;
0144 }
0145 
0146 /// Load the next event
0147 bool GenericEventHandler::NextEvent()   {
0148   m_hasEvent = false;
0149   try {
0150     if ( m_hasFile )   {
0151       if ( current()->NextEvent() > 0 )   {
0152         m_hasEvent = true;
0153         NotifySubscribers(&EventConsumer::OnNewEvent);
0154         return 1;
0155       }
0156     }
0157     throw std::runtime_error("+++ EventHandler::readEvent: No file open!");
0158   }
0159   catch(const std::exception& e)  {
0160     std::string path = TString::Format("%s/stop_t.xpm", TROOT::GetIconPath().Data()).Data();
0161     std::string err = "\nAn exception occurred \n"
0162       "while reading a new event:\n" + std::string(e.what()) + "\n\n";
0163     const TGPicture* pic = gClient->GetPicture(path.c_str());
0164     new TGMsgBox(gClient->GetRoot(),0,"Failed to read event", err.c_str(),pic,
0165                  kMBDismiss,0,kVerticalFrame,kTextLeft|kTextCenterY);
0166   }
0167   return -1;
0168 }
0169 
0170 /// User overloadable function: Load the previous event
0171 bool GenericEventHandler::PreviousEvent()    {
0172   m_hasEvent = false;
0173   if ( m_hasFile && current()->PreviousEvent() > 0 )   {
0174     m_hasEvent = true;
0175     NotifySubscribers(&EventConsumer::OnNewEvent);
0176     return 1;
0177   }
0178   return -1;
0179 }
0180 
0181 /// Goto a specified event in the file
0182 bool GenericEventHandler::GotoEvent(long event_number)   {
0183   m_hasEvent = false;
0184   if ( m_hasFile && current()->GotoEvent(event_number) > 0 )   {
0185     m_hasEvent = true;
0186     NotifySubscribers(&EventConsumer::OnNewEvent);
0187     return 1;
0188   }
0189   return -1;
0190 }