DD4hep  1.30.0
Detector Description Toolkit for High Energy Physics
GenericEventHandler.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
16 #include <DD4hep/Primitives.h>
17 #include <DD4hep/Factories.h>
18 #include <DD4hep/Plugins.h>
19 #include <stdexcept>
20 
22 #include <TROOT.h>
23 #include <TGMsgBox.h>
24 #include <TSystem.h>
25 #include <climits>
26 
27 using namespace dd4hep;
28 
30 
33 }
34 
36 GenericEventHandler::~GenericEventHandler() {
37  m_subscriptions.clear();
38  detail::deletePtr(m_current);
39 }
40 
42  if ( m_current ) {
43  return m_current;
44  }
45  throw std::runtime_error("Invalid event handler");
46 }
47 
50  for(Subscriptions::iterator i=m_subscriptions.begin(); i!=m_subscriptions.end();++i)
51  ((*i)->*pmf)(*this);
52 }
53 
56  m_subscriptions.insert(consumer);
57 }
58 
61  Subscriptions::iterator i=m_subscriptions.find(consumer);
62  if ( i != m_subscriptions.end() ) m_subscriptions.erase(i);
63 }
64 
67  return current()->numEvents();
68 }
69 
72  return current()->datasourceName();
73 }
74 
77  if ( m_current && m_current->hasEvent() ) {
78  return m_current->collectionType(collection);
79  }
80  return NO_COLLECTION;
81 }
82 
84 std::size_t GenericEventHandler::collectionLoop(const std::string& collection, DDEveHitActor& actor) {
85  if ( m_current && m_current->hasEvent() ) {
86  return m_current->collectionLoop(collection,actor);
87  }
88  return 0;
89 }
90 
92 std::size_t GenericEventHandler::collectionLoop(const std::string& collection, DDEveParticleActor& actor) {
93  if ( m_current && m_current->hasEvent() ) {
94  return m_current->collectionLoop(collection,actor);
95  }
96  return 0;
97 }
98 
100 bool GenericEventHandler::Open(const std::string& file_type, const std::string& file_name) {
101  std::size_t idx = file_name.find("lcio");
102  std::size_t idr = file_name.find("root");
103  std::string err;
104  m_hasFile = false;
105  m_hasEvent = false;
106  try {
107  detail::deletePtr(m_current);
108  // prefer event handler configured in xml
109  if ( file_type.find("FCC") != std::string::npos ) {
110  m_current = (EventHandler*)PluginService::Create<void*>("DD4hep_DDEve_FCCEventHandler",(const char*)0);
111  }
112  // fall back to defaults according to file ending
113  else if ( idx != std::string::npos ) {
114  m_current = (EventHandler*)PluginService::Create<void*>("DD4hep_DDEve_LCIOEventHandler",(const char*)0);
115  }
116  else if ( idr != std::string::npos ) {
117  m_current = (EventHandler*)PluginService::Create<void*>("DD4hep_DDEve_DDG4EventHandler",(const char*)0);
118  }
119  else {
120  throw std::runtime_error("Attempt to open file:"+file_name+" of unknown type:"+file_type);
121  }
122  if ( m_current ) {
123  if ( m_current->Open(file_type, file_name) ) {
124  m_hasFile = true;
126  return true;
127  }
128  err = "+++ Failed to open the data file:"+file_name;
129  detail::deletePtr(m_current);
130  }
131  else {
132  err = "+++ Failed to create fikle reader for file '"+file_name+"' of type '"+file_type+"'";
133  }
134  }
135  catch(const std::exception& e) {
136  err = "\nAn exception occurred \n"
137  "while opening event data:\n" + std::string(e.what()) + "\n\n";
138  }
139  std::string path = TString::Format("%s/stop_t.xpm", TROOT::GetIconPath().Data()).Data();
140  const TGPicture* pic = gClient->GetPicture(path.c_str());
141  new TGMsgBox(gClient->GetRoot(),0,"Failed to open event data",err.c_str(),pic,
142  kMBDismiss,0,kVerticalFrame,kTextLeft|kTextCenterY);
143  return false;
144 }
145 
148  m_hasEvent = false;
149  try {
150  if ( m_hasFile ) {
151  if ( current()->NextEvent() > 0 ) {
152  m_hasEvent = true;
154  return 1;
155  }
156  }
157  throw std::runtime_error("+++ EventHandler::readEvent: No file open!");
158  }
159  catch(const std::exception& e) {
160  std::string path = TString::Format("%s/stop_t.xpm", TROOT::GetIconPath().Data()).Data();
161  std::string err = "\nAn exception occurred \n"
162  "while reading a new event:\n" + std::string(e.what()) + "\n\n";
163  const TGPicture* pic = gClient->GetPicture(path.c_str());
164  new TGMsgBox(gClient->GetRoot(),0,"Failed to read event", err.c_str(),pic,
165  kMBDismiss,0,kVerticalFrame,kTextLeft|kTextCenterY);
166  }
167  return -1;
168 }
169 
172  m_hasEvent = false;
173  if ( m_hasFile && current()->PreviousEvent() > 0 ) {
174  m_hasEvent = true;
176  return 1;
177  }
178  return -1;
179 }
180 
182 bool GenericEventHandler::GotoEvent(long event_number) {
183  m_hasEvent = false;
184  if ( m_hasFile && current()->GotoEvent(event_number) > 0 ) {
185  m_hasEvent = true;
187  return 1;
188  }
189  return -1;
190 }
dd4hep::EventHandler::collectionType
virtual CollectionType collectionType(const std::string &collection) const =0
Access to the collection type by name.
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::GenericEventHandler
Event handler base class. Interface to all DDEve I/O actions.
Definition: GenericEventHandler.h:30
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::exception
void exception(const std::string &src, const std::string &msg)
Definition: RootDictionary.h:69
dd4hep::GenericEventHandler::collectionLoop
virtual size_t collectionLoop(const std::string &collection, DDEveHitActor &actor) override
Loop over collection and extract data.
Definition: GenericEventHandler.cpp:84
ClassImp
ClassImp(GenericEventHandler) GenericEventHandler
Standard constructor.
Definition: GenericEventHandler.cpp:29
dd4hep::GenericEventHandler::Subscribe
virtual void Subscribe(EventConsumer *display)
Subscribe to notification of new data present.
Definition: GenericEventHandler.cpp:55
dd4hep::GenericEventHandler::GenericEventHandler
GenericEventHandler()
Standard constructor.
GenericEventHandler.h
dd4hep::DDEveHitActor
Event data actor base class for hits. Used to extract data from concrete classes.
Definition: EventHandler.h:43
dd4hep::GenericEventHandler::datasourceName
virtual std::string datasourceName() const override
Access the data source name.
Definition: GenericEventHandler.cpp:71
Factories.h
dd4hep::EventHandler::Open
virtual bool Open(const std::string &type, const std::string &file_name)=0
Open a new event data file.
dd4hep::EventHandler::datasourceName
virtual std::string datasourceName() const =0
Access the data source name.
dd4hep::EventHandler::hasEvent
virtual bool hasEvent() const
Check if an event is present in memory.
Definition: EventHandler.h:93
dd4hep::GenericEventHandler::NotifySubscribers
virtual void NotifySubscribers(void(EventConsumer::*pmf)(EventHandler &))
Notfy all subscribers.
Definition: GenericEventHandler.cpp:49
dd4hep::EventHandler::m_hasFile
bool m_hasFile
Flag to indicate that a file is opened.
Definition: EventHandler.h:84
dd4hep::GenericEventHandler::NextEvent
virtual bool NextEvent() override
Load the next event.
Definition: GenericEventHandler.cpp:147
dd4hep::EventConsumer
Event event consumer base class for DDEve: Interface class for event I/O.
Definition: EventHandler.h:127
dd4hep::EventConsumer::OnNewEvent
virtual void OnNewEvent(EventHandler &)=0
Consumer event data callback.
dd4hep::GenericEventHandler::GotoEvent
virtual bool GotoEvent(long event_number) override
Goto a specified event in the file.
Definition: GenericEventHandler.cpp:182
dd4hep::GenericEventHandler::m_subscriptions
Subscriptions m_subscriptions
Data subscriptions (unordered)
Definition: GenericEventHandler.h:37
dd4hep::GenericEventHandler::PreviousEvent
virtual bool PreviousEvent() override
User overloadable function: Load the previous event.
Definition: GenericEventHandler.cpp:171
dd4hep::EventConsumer::OnFileOpen
virtual void OnFileOpen(EventHandler &)=0
Consumer file open callback.
Plugins.h
dd4hep::EventHandler::CollectionType
CollectionType
Definition: EventHandler.h:69
dd4hep::GenericEventHandler::current
EventHandler * current() const
Definition: GenericEventHandler.cpp:41
dd4hep::GenericEventHandler::collectionType
virtual CollectionType collectionType(const std::string &collection) const override
Access to the collection type by name.
Definition: GenericEventHandler.cpp:76
dd4hep::GenericEventHandler::Open
virtual bool Open(const std::string &type, const std::string &file_name) override
Open a new event data file.
Definition: GenericEventHandler.cpp:100
Primitives.h
dd4hep::GenericEventHandler::numEvents
virtual long numEvents() const override
Access the number of events on the current input data source (-1 if no data source connected)
Definition: GenericEventHandler.cpp:66
dd4hep
Namespace for the AIDA detector description toolkit.
Definition: AlignmentsCalib.h:28
dd4hep::EventHandler::collectionLoop
virtual size_t collectionLoop(const std::string &collection, DDEveHitActor &actor)=0
Loop over collection and extract hit data.
dd4hep::GenericEventHandler::m_current
EventHandler * m_current
Definition: GenericEventHandler.h:35
dd4hep::EventHandler::numEvents
virtual long numEvents() const =0
Access the number of events on the current input data source (-1 if no data source connected)
dd4hep::GenericEventHandler::Unsubscribe
virtual void Unsubscribe(EventConsumer *display)
Unsubscribe from notification of new data present.
Definition: GenericEventHandler.cpp:60